blob: a1d8620e8cb7c8f8bec0dc175b4f81cb337a1836 [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"
Trond Myklebustcc668ab2013-08-14 15:31:28 -040036#include "nfs4trace.h"
Trond Myklebust40dd4b72015-01-24 13:54:37 -050037#include "delegation.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040038
39#define NFSDBG_FACILITY NFSDBG_PNFS
Trond Myklebust25c75332012-09-18 17:01:12 -040040#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040041
Fred Isaman02c35fc2010-10-20 00:17:59 -040042/* Locking:
43 *
44 * pnfs_spinlock:
45 * protects pnfs_modules_tbl.
46 */
47static DEFINE_SPINLOCK(pnfs_spinlock);
48
49/*
50 * pnfs_modules_tbl holds all pnfs modules
51 */
52static LIST_HEAD(pnfs_modules_tbl);
53
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080054static int
55pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
Peng Tao6c166052014-11-17 09:30:40 +080056 enum pnfs_iomode iomode, bool sync);
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080057
Fred Isaman02c35fc2010-10-20 00:17:59 -040058/* Return the registered pnfs layout driver module matching given id */
59static struct pnfs_layoutdriver_type *
60find_pnfs_driver_locked(u32 id)
61{
62 struct pnfs_layoutdriver_type *local;
63
64 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
65 if (local->id == id)
66 goto out;
67 local = NULL;
68out:
69 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
70 return local;
71}
72
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040073static struct pnfs_layoutdriver_type *
74find_pnfs_driver(u32 id)
75{
Fred Isaman02c35fc2010-10-20 00:17:59 -040076 struct pnfs_layoutdriver_type *local;
77
78 spin_lock(&pnfs_spinlock);
79 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040080 if (local != NULL && !try_module_get(local->owner)) {
81 dprintk("%s: Could not grab reference on module\n", __func__);
82 local = NULL;
83 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040084 spin_unlock(&pnfs_spinlock);
85 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040086}
87
88void
89unset_pnfs_layoutdriver(struct nfs_server *nfss)
90{
Benny Halevy738fd0f32011-07-30 20:52:36 -040091 if (nfss->pnfs_curr_ld) {
92 if (nfss->pnfs_curr_ld->clear_layoutdriver)
93 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040094 /* Decrement the MDS count. Purge the deviceid cache if zero */
95 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
96 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -040097 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -040098 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040099 nfss->pnfs_curr_ld = NULL;
100}
101
102/*
103 * Try to set the server's pnfs module to the pnfs layout type specified by id.
104 * Currently only one pNFS layout driver per filesystem is supported.
105 *
106 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
107 */
108void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400109set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
110 u32 id)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400111{
112 struct pnfs_layoutdriver_type *ld_type = NULL;
113
114 if (id == 0)
115 goto out_no_driver;
116 if (!(server->nfs_client->cl_exchange_flags &
117 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500118 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
119 __func__, id, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400120 goto out_no_driver;
121 }
122 ld_type = find_pnfs_driver(id);
123 if (!ld_type) {
124 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
125 ld_type = find_pnfs_driver(id);
126 if (!ld_type) {
127 dprintk("%s: No pNFS module found for %u.\n",
128 __func__, id);
129 goto out_no_driver;
130 }
131 }
132 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400133 if (ld_type->set_layoutdriver
134 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500135 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
136 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400137 module_put(ld_type->owner);
138 goto out_no_driver;
139 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400140 /* Bump the MDS count */
141 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000142
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400143 dprintk("%s: pNFS module for %u set\n", __func__, id);
144 return;
145
146out_no_driver:
147 dprintk("%s: Using NFSv4 I/O\n", __func__);
148 server->pnfs_curr_ld = NULL;
149}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400150
151int
152pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
153{
154 int status = -EINVAL;
155 struct pnfs_layoutdriver_type *tmp;
156
157 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500158 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400159 return status;
160 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400161 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500162 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400163 "alloc_lseg and free_lseg.\n", __func__);
164 return status;
165 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400166
167 spin_lock(&pnfs_spinlock);
168 tmp = find_pnfs_driver_locked(ld_type->id);
169 if (!tmp) {
170 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
171 status = 0;
172 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
173 ld_type->name);
174 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500175 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400176 __func__, ld_type->id);
177 }
178 spin_unlock(&pnfs_spinlock);
179
180 return status;
181}
182EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
183
184void
185pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
186{
187 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
188 spin_lock(&pnfs_spinlock);
189 list_del(&ld_type->pnfs_tblid);
190 spin_unlock(&pnfs_spinlock);
191}
192EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400193
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400194/*
195 * pNFS client layout cache
196 */
197
Fred Isamancc6e5342011-01-06 11:36:28 +0000198/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000199void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400200pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400201{
Fred Isamancc6e5342011-01-06 11:36:28 +0000202 atomic_inc(&lo->plh_refcount);
203}
204
Benny Halevy636fb9c2011-05-22 19:51:33 +0300205static struct pnfs_layout_hdr *
206pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
207{
208 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
Trond Myklebust57934272012-09-20 20:37:23 -0400209 return ld->alloc_layout_hdr(ino, gfp_flags);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300210}
211
212static void
213pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
214{
Trond Myklebust9c626382012-09-20 17:31:43 -0400215 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
216 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
217
218 if (!list_empty(&lo->plh_layouts)) {
219 struct nfs_client *clp = server->nfs_client;
220
221 spin_lock(&clp->cl_lock);
222 list_del_init(&lo->plh_layouts);
223 spin_unlock(&clp->cl_lock);
224 }
Peng Tao9fa40752011-07-30 20:52:32 -0400225 put_rpccred(lo->plh_lc_cred);
Trond Myklebust57934272012-09-20 20:37:23 -0400226 return ld->free_layout_hdr(lo);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300227}
228
Fred Isamancc6e5342011-01-06 11:36:28 +0000229static void
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400230pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
Fred Isamancc6e5342011-01-06 11:36:28 +0000231{
Trond Myklebustbb346f62012-09-20 15:52:13 -0400232 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
Fred Isamancc6e5342011-01-06 11:36:28 +0000233 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Trond Myklebustbb346f62012-09-20 15:52:13 -0400234 nfsi->layout = NULL;
235 /* Reset MDS Threshold I/O counters */
236 nfsi->write_io = 0;
237 nfsi->read_io = 0;
Benny Halevye5e94012010-10-20 00:18:01 -0400238}
239
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400240void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400241pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400242{
Fred Isamancc6e5342011-01-06 11:36:28 +0000243 struct inode *inode = lo->plh_inode;
244
245 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
Peng Tao566f8732014-10-10 23:25:46 +0800246 if (!list_empty(&lo->plh_segs))
247 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400248 pnfs_detach_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000249 spin_unlock(&inode->i_lock);
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400250 pnfs_free_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000251 }
Andy Adamson974cec82010-10-20 00:18:02 -0400252}
253
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400254static int
255pnfs_iomode_to_fail_bit(u32 iomode)
256{
257 return iomode == IOMODE_RW ?
258 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
259}
260
261static void
Trond Myklebust3e621212012-09-24 13:07:16 -0400262pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400263{
Trond Myklebust25c75332012-09-18 17:01:12 -0400264 lo->plh_retry_timestamp = jiffies;
Yanchuan Nian39e88fc2013-01-04 20:19:49 +0800265 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
Trond Myklebust3e621212012-09-24 13:07:16 -0400266 atomic_inc(&lo->plh_refcount);
267}
268
269static void
270pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
271{
272 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
273 atomic_dec(&lo->plh_refcount);
274}
275
276static void
277pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
278{
279 struct inode *inode = lo->plh_inode;
Trond Myklebust115ce572012-09-20 21:19:43 -0400280 struct pnfs_layout_range range = {
281 .iomode = iomode,
282 .offset = 0,
283 .length = NFS4_MAX_UINT64,
284 };
285 LIST_HEAD(head);
Trond Myklebust3e621212012-09-24 13:07:16 -0400286
287 spin_lock(&inode->i_lock);
288 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
Trond Myklebust115ce572012-09-20 21:19:43 -0400289 pnfs_mark_matching_lsegs_invalid(lo, &head, &range);
Trond Myklebust3e621212012-09-24 13:07:16 -0400290 spin_unlock(&inode->i_lock);
Trond Myklebust115ce572012-09-20 21:19:43 -0400291 pnfs_free_lseg_list(&head);
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400292 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
293 iomode == IOMODE_RW ? "RW" : "READ");
294}
295
296static bool
297pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
298{
Trond Myklebust25c75332012-09-18 17:01:12 -0400299 unsigned long start, end;
Trond Myklebust3e621212012-09-24 13:07:16 -0400300 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
301
302 if (test_bit(fail_bit, &lo->plh_flags) == 0)
Trond Myklebust25c75332012-09-18 17:01:12 -0400303 return false;
304 end = jiffies;
305 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
306 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
307 /* It is time to retry the failed layoutgets */
Trond Myklebust3e621212012-09-24 13:07:16 -0400308 pnfs_layout_clear_fail_bit(lo, fail_bit);
Trond Myklebust25c75332012-09-18 17:01:12 -0400309 return false;
310 }
311 return true;
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400312}
313
Andy Adamson974cec82010-10-20 00:18:02 -0400314static void
315init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
316{
Fred Isaman566052c2011-01-06 11:36:20 +0000317 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400318 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000319 atomic_set(&lseg->pls_refcount, 1);
320 smp_mb();
321 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000322 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400323}
324
Trond Myklebust905ca192012-09-20 20:46:49 -0400325static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400326{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000327 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400328
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400329 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400330}
331
Fred Isamand684d2a2011-03-01 01:34:13 +0000332static void
Trond Myklebust57036a32012-09-20 16:33:30 -0400333pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
334 struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400335{
Trond Myklebust57036a32012-09-20 16:33:30 -0400336 struct inode *inode = lo->plh_inode;
Fred Isamand684d2a2011-03-01 01:34:13 +0000337
Benny Halevyd20581a2011-05-22 19:52:03 +0300338 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000339 list_del_init(&lseg->pls_list);
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400340 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
341 atomic_dec(&lo->plh_refcount);
Trond Myklebust173f77e2012-09-21 15:49:42 -0400342 if (list_empty(&lo->plh_segs))
343 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isamand684d2a2011-03-01 01:34:13 +0000344 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
345}
346
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800347/* Return true if layoutreturn is needed */
348static bool
349pnfs_layout_need_return(struct pnfs_layout_hdr *lo,
Peng Tao27b6f532014-10-20 14:44:38 +0800350 struct pnfs_layout_segment *lseg)
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800351{
352 struct pnfs_layout_segment *s;
353
354 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
355 return false;
356
357 list_for_each_entry(s, &lo->plh_segs, pls_list)
Peng Tao27b6f532014-10-20 14:44:38 +0800358 if (s != lseg && test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800359 return false;
360
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800361 return true;
362}
363
Peng Tao27b6f532014-10-20 14:44:38 +0800364static void pnfs_layoutreturn_free_lseg(struct work_struct *work)
365{
366 struct pnfs_layout_segment *lseg;
367 struct pnfs_layout_hdr *lo;
368 struct inode *inode;
369
370 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
371 WARN_ON(atomic_read(&lseg->pls_refcount));
372 lo = lseg->pls_layout;
373 inode = lo->plh_inode;
374
375 spin_lock(&inode->i_lock);
376 if (pnfs_layout_need_return(lo, lseg)) {
377 nfs4_stateid stateid;
378 enum pnfs_iomode iomode;
379
380 stateid = lo->plh_stateid;
381 iomode = lo->plh_return_iomode;
382 /* decreased in pnfs_send_layoutreturn() */
383 lo->plh_block_lgets++;
384 lo->plh_return_iomode = 0;
385 spin_unlock(&inode->i_lock);
386
387 pnfs_send_layoutreturn(lo, stateid, iomode, true);
388 spin_lock(&inode->i_lock);
389 } else
390 /* match pnfs_get_layout_hdr #2 in pnfs_put_lseg */
391 pnfs_put_layout_hdr(lo);
392 pnfs_layout_remove_lseg(lo, lseg);
393 spin_unlock(&inode->i_lock);
394 pnfs_free_lseg(lseg);
395 /* match pnfs_get_layout_hdr #1 in pnfs_put_lseg */
396 pnfs_put_layout_hdr(lo);
397}
398
399static void
400pnfs_layoutreturn_free_lseg_async(struct pnfs_layout_segment *lseg)
401{
402 INIT_WORK(&lseg->pls_work, pnfs_layoutreturn_free_lseg);
403 queue_work(nfsiod_workqueue, &lseg->pls_work);
404}
405
Fred Isamanbae724e2011-03-01 01:34:15 +0000406void
Trond Myklebust9369a432012-09-18 20:57:08 -0400407pnfs_put_lseg(struct pnfs_layout_segment *lseg)
Fred Isamand684d2a2011-03-01 01:34:13 +0000408{
Trond Myklebust57036a32012-09-20 16:33:30 -0400409 struct pnfs_layout_hdr *lo;
Fred Isamand684d2a2011-03-01 01:34:13 +0000410 struct inode *inode;
411
412 if (!lseg)
413 return;
414
Fred Isaman4541d162011-01-06 11:36:23 +0000415 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
416 atomic_read(&lseg->pls_refcount),
417 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Trond Myklebust57036a32012-09-20 16:33:30 -0400418 lo = lseg->pls_layout;
419 inode = lo->plh_inode;
Fred Isamand684d2a2011-03-01 01:34:13 +0000420 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400421 pnfs_get_layout_hdr(lo);
Peng Tao27b6f532014-10-20 14:44:38 +0800422 if (pnfs_layout_need_return(lo, lseg)) {
423 spin_unlock(&inode->i_lock);
424 /* hdr reference dropped in nfs4_layoutreturn_release */
425 pnfs_get_layout_hdr(lo);
426 pnfs_layoutreturn_free_lseg_async(lseg);
427 } else {
428 pnfs_layout_remove_lseg(lo, lseg);
429 spin_unlock(&inode->i_lock);
430 pnfs_free_lseg(lseg);
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800431 pnfs_put_layout_hdr(lo);
Peng Tao27b6f532014-10-20 14:44:38 +0800432 }
Fred Isaman4541d162011-01-06 11:36:23 +0000433 }
Andy Adamson974cec82010-10-20 00:18:02 -0400434}
Trond Myklebust9369a432012-09-18 20:57:08 -0400435EXPORT_SYMBOL_GPL(pnfs_put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400436
Trond Myklebust6543f802014-10-08 16:39:12 -0400437static void pnfs_free_lseg_async_work(struct work_struct *work)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400438{
439 struct pnfs_layout_segment *lseg;
Trond Myklebust6543f802014-10-08 16:39:12 -0400440 struct pnfs_layout_hdr *lo;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400441
442 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
Trond Myklebust6543f802014-10-08 16:39:12 -0400443 lo = lseg->pls_layout;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400444
Trond Myklebust6543f802014-10-08 16:39:12 -0400445 pnfs_free_lseg(lseg);
446 pnfs_put_layout_hdr(lo);
447}
448
449static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
450{
451 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
452 schedule_work(&lseg->pls_work);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400453}
454
455void
Trond Myklebust6543f802014-10-08 16:39:12 -0400456pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400457{
Trond Myklebust6543f802014-10-08 16:39:12 -0400458 if (!lseg)
459 return;
460
461 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
462
463 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
464 atomic_read(&lseg->pls_refcount),
465 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
466 if (atomic_dec_and_test(&lseg->pls_refcount)) {
467 struct pnfs_layout_hdr *lo = lseg->pls_layout;
468 pnfs_get_layout_hdr(lo);
469 pnfs_layout_remove_lseg(lo, lseg);
470 pnfs_free_lseg_async(lseg);
471 }
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400472}
Trond Myklebust6543f802014-10-08 16:39:12 -0400473EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400474
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400475static u64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300476end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000477{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300478 u64 end;
479
480 end = start + len;
481 return end >= start ? end : NFS4_MAX_UINT64;
482}
483
Benny Halevyfb3296e2011-05-22 19:47:26 +0300484/*
485 * is l2 fully contained in l1?
486 * start1 end1
487 * [----------------------------------)
488 * start2 end2
489 * [----------------)
490 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400491static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400492pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400493 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300494{
495 u64 start1 = l1->offset;
496 u64 end1 = end_offset(start1, l1->length);
497 u64 start2 = l2->offset;
498 u64 end2 = end_offset(start2, l2->length);
499
500 return (start1 <= start2) && (end1 >= end2);
501}
502
503/*
504 * is l1 and l2 intersecting?
505 * start1 end1
506 * [----------------------------------)
507 * start2 end2
508 * [----------------)
509 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400510static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400511pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400512 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300513{
514 u64 start1 = l1->offset;
515 u64 end1 = end_offset(start1, l1->length);
516 u64 start2 = l2->offset;
517 u64 end2 = end_offset(start2, l2->length);
518
519 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
520 (end2 == NFS4_MAX_UINT64 || end2 > start1);
521}
522
Fred Isaman4541d162011-01-06 11:36:23 +0000523static bool
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400524should_free_lseg(const struct pnfs_layout_range *lseg_range,
525 const struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000526{
Benny Halevy778b5502011-05-22 19:48:02 +0300527 return (recall_range->iomode == IOMODE_ANY ||
528 lseg_range->iomode == recall_range->iomode) &&
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400529 pnfs_lseg_range_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000530}
531
Trond Myklebust24956802013-03-20 13:03:00 -0400532static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
533 struct list_head *tmp_list)
534{
535 if (!atomic_dec_and_test(&lseg->pls_refcount))
536 return false;
537 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
538 list_add(&lseg->pls_list, tmp_list);
539 return true;
540}
541
Fred Isaman4541d162011-01-06 11:36:23 +0000542/* Returns 1 if lseg is removed from list, 0 otherwise */
543static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
544 struct list_head *tmp_list)
545{
546 int rv = 0;
547
548 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
549 /* Remove the reference keeping the lseg in the
550 * list. It will now be removed when all
551 * outstanding io is finished.
552 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000553 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
554 atomic_read(&lseg->pls_refcount));
Trond Myklebust24956802013-03-20 13:03:00 -0400555 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
Fred Isamand684d2a2011-03-01 01:34:13 +0000556 rv = 1;
Fred Isaman4541d162011-01-06 11:36:23 +0000557 }
558 return rv;
559}
560
561/* Returns count of number of matching invalid lsegs remaining in list
562 * after call.
563 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000564int
Trond Myklebust49a85062012-09-18 20:43:31 -0400565pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
Fred Isaman4541d162011-01-06 11:36:23 +0000566 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300567 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400568{
569 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000570 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400571
572 dprintk("%s:Begin lo %p\n", __func__, lo);
573
Trond Myklebust8006bfb2012-09-21 14:48:04 -0400574 if (list_empty(&lo->plh_segs))
Fred Isaman38511722011-02-03 18:28:50 +0000575 return 0;
Fred Isaman4541d162011-01-06 11:36:23 +0000576 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300577 if (!recall_range ||
578 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000579 dprintk("%s: freeing lseg %p iomode %d "
580 "offset %llu length %llu\n", __func__,
581 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
582 lseg->pls_range.length);
583 invalid++;
584 removed += mark_lseg_invalid(lseg, tmp_list);
585 }
586 dprintk("%s:Return %i\n", __func__, invalid - removed);
587 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400588}
589
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000590/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000591void
Fred Isaman4541d162011-01-06 11:36:23 +0000592pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400593{
Fred Isaman4541d162011-01-06 11:36:23 +0000594 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400595
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000596 if (list_empty(free_me))
597 return;
598
Fred Isaman4541d162011-01-06 11:36:23 +0000599 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000600 list_del(&lseg->pls_list);
Trond Myklebust905ca192012-09-20 20:46:49 -0400601 pnfs_free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400602 }
603}
604
Benny Halevye5e94012010-10-20 00:18:01 -0400605void
606pnfs_destroy_layout(struct nfs_inode *nfsi)
607{
608 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400609 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400610
611 spin_lock(&nfsi->vfs_inode.i_lock);
612 lo = nfsi->layout;
613 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000614 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Trond Myklebust49a85062012-09-18 20:43:31 -0400615 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Trond Myklebust3e621212012-09-24 13:07:16 -0400616 pnfs_get_layout_hdr(lo);
617 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
618 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
Peng Taoc8290132014-12-01 08:22:18 +0800619 pnfs_clear_retry_layoutget(lo);
Trond Myklebust3e621212012-09-24 13:07:16 -0400620 spin_unlock(&nfsi->vfs_inode.i_lock);
621 pnfs_free_lseg_list(&tmp_list);
622 pnfs_put_layout_hdr(lo);
623 } else
624 spin_unlock(&nfsi->vfs_inode.i_lock);
Benny Halevye5e94012010-10-20 00:18:01 -0400625}
Andy Adamson041245c2012-04-27 17:53:53 -0400626EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400627
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500628static bool
629pnfs_layout_add_bulk_destroy_list(struct inode *inode,
630 struct list_head *layout_list)
631{
632 struct pnfs_layout_hdr *lo;
633 bool ret = false;
634
635 spin_lock(&inode->i_lock);
636 lo = NFS_I(inode)->layout;
637 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
638 pnfs_get_layout_hdr(lo);
639 list_add(&lo->plh_bulk_destroy, layout_list);
640 ret = true;
641 }
642 spin_unlock(&inode->i_lock);
643 return ret;
644}
645
646/* Caller must hold rcu_read_lock and clp->cl_lock */
647static int
648pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
649 struct nfs_server *server,
650 struct list_head *layout_list)
651{
652 struct pnfs_layout_hdr *lo, *next;
653 struct inode *inode;
654
655 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
656 inode = igrab(lo->plh_inode);
657 if (inode == NULL)
658 continue;
659 list_del_init(&lo->plh_layouts);
660 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
661 continue;
662 rcu_read_unlock();
663 spin_unlock(&clp->cl_lock);
664 iput(inode);
665 spin_lock(&clp->cl_lock);
666 rcu_read_lock();
667 return -EAGAIN;
668 }
669 return 0;
670}
671
672static int
673pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
674 bool is_bulk_recall)
675{
676 struct pnfs_layout_hdr *lo;
677 struct inode *inode;
678 struct pnfs_layout_range range = {
679 .iomode = IOMODE_ANY,
680 .offset = 0,
681 .length = NFS4_MAX_UINT64,
682 };
683 LIST_HEAD(lseg_list);
684 int ret = 0;
685
686 while (!list_empty(layout_list)) {
687 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
688 plh_bulk_destroy);
689 dprintk("%s freeing layout for inode %lu\n", __func__,
690 lo->plh_inode->i_ino);
691 inode = lo->plh_inode;
Christoph Hellwig7c5d1872014-09-10 08:23:29 -0700692
693 pnfs_layoutcommit_inode(inode, false);
694
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500695 spin_lock(&inode->i_lock);
696 list_del_init(&lo->plh_bulk_destroy);
697 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
698 if (is_bulk_recall)
699 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
700 if (pnfs_mark_matching_lsegs_invalid(lo, &lseg_list, &range))
701 ret = -EAGAIN;
702 spin_unlock(&inode->i_lock);
703 pnfs_free_lseg_list(&lseg_list);
704 pnfs_put_layout_hdr(lo);
705 iput(inode);
706 }
707 return ret;
708}
709
710int
711pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
712 struct nfs_fsid *fsid,
713 bool is_recall)
714{
715 struct nfs_server *server;
716 LIST_HEAD(layout_list);
717
718 spin_lock(&clp->cl_lock);
719 rcu_read_lock();
720restart:
721 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
722 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
723 continue;
724 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
725 server,
726 &layout_list) != 0)
727 goto restart;
728 }
729 rcu_read_unlock();
730 spin_unlock(&clp->cl_lock);
731
732 if (list_empty(&layout_list))
733 return 0;
734 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
735}
736
737int
738pnfs_destroy_layouts_byclid(struct nfs_client *clp,
739 bool is_recall)
740{
741 struct nfs_server *server;
742 LIST_HEAD(layout_list);
743
744 spin_lock(&clp->cl_lock);
745 rcu_read_lock();
746restart:
747 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
748 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
749 server,
750 &layout_list) != 0)
751 goto restart;
752 }
753 rcu_read_unlock();
754 spin_unlock(&clp->cl_lock);
755
756 if (list_empty(&layout_list))
757 return 0;
758 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
759}
760
Andy Adamson974cec82010-10-20 00:18:02 -0400761/*
762 * Called by the state manger to remove all layouts established under an
763 * expired lease.
764 */
765void
766pnfs_destroy_all_layouts(struct nfs_client *clp)
767{
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400768 nfs4_deviceid_mark_client_invalid(clp);
769 nfs4_deviceid_purge_client(clp);
770
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500771 pnfs_destroy_layouts_byclid(clp, false);
Andy Adamson974cec82010-10-20 00:18:02 -0400772}
773
Trond Myklebust5a655032012-10-02 16:47:14 -0700774/*
775 * Compare 2 layout stateid sequence ids, to see which is newer,
776 * taking into account wraparound issues.
777 */
778static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
779{
Trond Myklebust2c64c572014-02-12 09:36:59 -0500780 return (s32)(s1 - s2) > 0;
Trond Myklebust5a655032012-10-02 16:47:14 -0700781}
782
Fred Isamanfd6002e2011-01-06 11:36:22 +0000783/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000784void
785pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
786 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400787{
Trond Myklebust22aaf712012-10-04 16:32:22 -0700788 u32 oldseq, newseq, new_barrier;
789 int empty = list_empty(&lo->plh_segs);
Andy Adamson974cec82010-10-20 00:18:02 -0400790
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500791 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
792 newseq = be32_to_cpu(new->seqid);
Trond Myklebust22aaf712012-10-04 16:32:22 -0700793 if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500794 nfs4_stateid_copy(&lo->plh_stateid, new);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000795 if (update_barrier) {
Trond Myklebust22aaf712012-10-04 16:32:22 -0700796 new_barrier = be32_to_cpu(new->seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000797 } else {
798 /* Because of wraparound, we want to keep the barrier
Trond Myklebust22aaf712012-10-04 16:32:22 -0700799 * "close" to the current seqids.
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000800 */
Trond Myklebust22aaf712012-10-04 16:32:22 -0700801 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000802 }
Trond Myklebust22aaf712012-10-04 16:32:22 -0700803 if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
804 lo->plh_barrier = new_barrier;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000805 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400806}
807
Trond Myklebust19c54ab2012-10-05 16:56:58 -0700808static bool
809pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
810 const nfs4_stateid *stateid)
811{
812 u32 seqid = be32_to_cpu(stateid->seqid);
813
814 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
815}
816
Peng Taoce6ab4f2014-09-06 00:53:24 +0800817static bool
818pnfs_layout_returning(const struct pnfs_layout_hdr *lo,
819 struct pnfs_layout_range *range)
820{
821 return test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
822 (lo->plh_return_iomode == IOMODE_ANY ||
823 lo->plh_return_iomode == range->iomode);
824}
825
Fred Isamancf7d63f2011-01-06 11:36:25 +0000826/* lget is set to 1 if called from inside send_layoutget call chain */
827static bool
Peng Taoce6ab4f2014-09-06 00:53:24 +0800828pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo,
829 struct pnfs_layout_range *range, int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000830{
Fred Isamanf7e89172011-01-06 11:36:32 +0000831 return lo->plh_block_lgets ||
832 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000833 (list_empty(&lo->plh_segs) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +0800834 (atomic_read(&lo->plh_outstanding) > lget)) ||
835 pnfs_layout_returning(lo, range);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000836}
837
Fred Isamanfd6002e2011-01-06 11:36:22 +0000838int
839pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
Peng Taoce6ab4f2014-09-06 00:53:24 +0800840 struct pnfs_layout_range *range,
Fred Isamanfd6002e2011-01-06 11:36:22 +0000841 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400842{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000843 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400844
845 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000846 spin_lock(&lo->plh_inode->i_lock);
Peng Taoce6ab4f2014-09-06 00:53:24 +0800847 if (pnfs_layoutgets_blocked(lo, range, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000848 status = -EAGAIN;
Trond Myklebust5d422302013-03-14 16:57:48 -0400849 } else if (!nfs4_valid_open_stateid(open_state)) {
850 status = -EBADF;
Christoph Hellwig47abade2014-08-21 11:09:22 -0500851 } else if (list_empty(&lo->plh_segs) ||
852 test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000853 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400854
Fred Isamanfd6002e2011-01-06 11:36:22 +0000855 do {
856 seq = read_seqbegin(&open_state->seqlock);
Trond Myklebustf597c532012-03-04 18:13:56 -0500857 nfs4_stateid_copy(dst, &open_state->stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000858 } while (read_seqretry(&open_state->seqlock, seq));
859 } else
Trond Myklebustf597c532012-03-04 18:13:56 -0500860 nfs4_stateid_copy(dst, &lo->plh_stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000861 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400862 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000863 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400864}
865
866/*
867* Get layout from server.
868* for now, assume that whole file layouts are requested.
869* arg->offset: 0
870* arg->length: all ones
871*/
Benny Halevye5e94012010-10-20 00:18:01 -0400872static struct pnfs_layout_segment *
873send_layoutget(struct pnfs_layout_hdr *lo,
874 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300875 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400876 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400877{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000878 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400879 struct nfs_server *server = NFS_SERVER(ino);
880 struct nfs4_layoutget *lgp;
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400881 struct pnfs_layout_segment *lseg;
Benny Halevye5e94012010-10-20 00:18:01 -0400882
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400883 dprintk("--> %s\n", __func__);
884
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400885 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000886 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400887 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400888
Benny Halevyfb3296e2011-05-22 19:47:26 +0300889 lgp->args.minlength = PAGE_CACHE_SIZE;
890 if (lgp->args.minlength > range->length)
891 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400892 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300893 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400894 lgp->args.type = server->pnfs_curr_ld->id;
895 lgp->args.inode = ino;
896 lgp->args.ctx = get_nfs_open_context(ctx);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400897 lgp->gfp_flags = gfp_flags;
Trond Myklebust6ab59342013-05-20 10:49:34 -0400898 lgp->cred = lo->plh_lc_cred;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400899
900 /* Synchronously retrieve layout information from server and
901 * store in lseg.
902 */
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400903 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
904 if (IS_ERR(lseg)) {
905 switch (PTR_ERR(lseg)) {
906 case -ENOMEM:
907 case -ERESTARTSYS:
908 break;
909 default:
910 /* remember that LAYOUTGET failed and suspend trying */
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400911 pnfs_layout_io_set_failed(lo, range->iomode);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400912 }
913 return NULL;
Tom Haynesd67ae822014-12-11 17:02:04 -0500914 } else
915 pnfs_layout_clear_fail_bit(lo,
916 pnfs_iomode_to_fail_bit(range->iomode));
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400917
Andy Adamson974cec82010-10-20 00:18:02 -0400918 return lseg;
919}
920
Trond Myklebust24956802013-03-20 13:03:00 -0400921static void pnfs_clear_layoutcommit(struct inode *inode,
922 struct list_head *head)
923{
924 struct nfs_inode *nfsi = NFS_I(inode);
925 struct pnfs_layout_segment *lseg, *tmp;
926
927 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
928 return;
929 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
930 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
931 continue;
932 pnfs_lseg_dec_and_remove_zero(lseg, head);
933 }
934}
935
Tom Haynesd67ae822014-12-11 17:02:04 -0500936void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
937{
938 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
939 smp_mb__after_atomic();
940 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
941}
942
Peng Taof40eb5d2014-09-06 00:53:22 +0800943static int
944pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
Peng Tao6c166052014-11-17 09:30:40 +0800945 enum pnfs_iomode iomode, bool sync)
Peng Taof40eb5d2014-09-06 00:53:22 +0800946{
947 struct inode *ino = lo->plh_inode;
948 struct nfs4_layoutreturn *lrp;
949 int status = 0;
950
Trond Myklebuste4af4402015-02-05 17:05:08 -0500951 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
Peng Taof40eb5d2014-09-06 00:53:22 +0800952 if (unlikely(lrp == NULL)) {
953 status = -ENOMEM;
954 spin_lock(&ino->i_lock);
955 lo->plh_block_lgets--;
Tom Haynesd67ae822014-12-11 17:02:04 -0500956 pnfs_clear_layoutreturn_waitbit(lo);
Peng Tao193e3aa2014-11-17 09:30:41 +0800957 rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq);
Peng Taof40eb5d2014-09-06 00:53:22 +0800958 spin_unlock(&ino->i_lock);
959 pnfs_put_layout_hdr(lo);
960 goto out;
961 }
962
963 lrp->args.stateid = stateid;
964 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
965 lrp->args.inode = ino;
Peng Tao15eb67c2014-11-17 09:30:36 +0800966 lrp->args.range.iomode = iomode;
967 lrp->args.range.offset = 0;
968 lrp->args.range.length = NFS4_MAX_UINT64;
Peng Taof40eb5d2014-09-06 00:53:22 +0800969 lrp->args.layout = lo;
970 lrp->clp = NFS_SERVER(ino)->nfs_client;
971 lrp->cred = lo->plh_lc_cred;
972
Peng Tao6c166052014-11-17 09:30:40 +0800973 status = nfs4_proc_layoutreturn(lrp, sync);
Peng Taof40eb5d2014-09-06 00:53:22 +0800974out:
975 dprintk("<-- %s status: %d\n", __func__, status);
976 return status;
977}
978
Andy Adamson293b3b02012-06-20 15:03:34 -0400979/*
980 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
981 * when the layout segment list is empty.
982 *
983 * Note that a pnfs_layout_hdr can exist with an empty layout segment
984 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
985 * deviceid is marked invalid.
986 */
Benny Halevycbe82602011-05-22 19:52:37 +0300987int
988_pnfs_return_layout(struct inode *ino)
989{
990 struct pnfs_layout_hdr *lo = NULL;
991 struct nfs_inode *nfsi = NFS_I(ino);
992 LIST_HEAD(tmp_list);
Benny Halevycbe82602011-05-22 19:52:37 +0300993 nfs4_stateid stateid;
Andy Adamson293b3b02012-06-20 15:03:34 -0400994 int status = 0, empty;
Benny Halevycbe82602011-05-22 19:52:37 +0300995
Andy Adamson366d5052012-06-20 15:03:33 -0400996 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +0300997
998 spin_lock(&ino->i_lock);
999 lo = nfsi->layout;
Trond Myklebuste5929f32012-09-21 16:37:02 -04001000 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +03001001 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -04001002 dprintk("NFS: %s no layout to return\n", __func__);
1003 goto out;
Benny Halevycbe82602011-05-22 19:52:37 +03001004 }
1005 stateid = nfsi->layout->plh_stateid;
1006 /* Reference matched in nfs4_layoutreturn_release */
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001007 pnfs_get_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -04001008 empty = list_empty(&lo->plh_segs);
Trond Myklebust24956802013-03-20 13:03:00 -04001009 pnfs_clear_layoutcommit(ino, &tmp_list);
Trond Myklebust49a85062012-09-18 20:43:31 -04001010 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Christoph Hellwigc88953d2014-09-10 08:23:31 -07001011
1012 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1013 struct pnfs_layout_range range = {
1014 .iomode = IOMODE_ANY,
1015 .offset = 0,
1016 .length = NFS4_MAX_UINT64,
1017 };
1018 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1019 }
1020
Andy Adamson293b3b02012-06-20 15:03:34 -04001021 /* Don't send a LAYOUTRETURN if list was initially empty */
1022 if (empty) {
1023 spin_unlock(&ino->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001024 pnfs_put_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -04001025 dprintk("NFS: %s no layout segments to return\n", __func__);
1026 goto out;
1027 }
Christoph Hellwig47abade2014-08-21 11:09:22 -05001028
1029 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Fred Isamanea0ded72011-06-15 12:31:02 -04001030 lo->plh_block_lgets++;
Benny Halevycbe82602011-05-22 19:52:37 +03001031 spin_unlock(&ino->i_lock);
1032 pnfs_free_lseg_list(&tmp_list);
1033
Peng Tao6c166052014-11-17 09:30:40 +08001034 status = pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
Benny Halevycbe82602011-05-22 19:52:37 +03001035out:
1036 dprintk("<-- %s status: %d\n", __func__, status);
1037 return status;
1038}
Andy Adamson0a57cda2012-04-27 17:53:50 -04001039EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +03001040
Trond Myklebust24028672013-03-20 13:23:33 -04001041int
1042pnfs_commit_and_return_layout(struct inode *inode)
1043{
1044 struct pnfs_layout_hdr *lo;
1045 int ret;
1046
1047 spin_lock(&inode->i_lock);
1048 lo = NFS_I(inode)->layout;
1049 if (lo == NULL) {
1050 spin_unlock(&inode->i_lock);
1051 return 0;
1052 }
1053 pnfs_get_layout_hdr(lo);
1054 /* Block new layoutgets and read/write to ds */
1055 lo->plh_block_lgets++;
1056 spin_unlock(&inode->i_lock);
1057 filemap_fdatawait(inode->i_mapping);
1058 ret = pnfs_layoutcommit_inode(inode, true);
1059 if (ret == 0)
1060 ret = _pnfs_return_layout(inode);
1061 spin_lock(&inode->i_lock);
1062 lo->plh_block_lgets--;
1063 spin_unlock(&inode->i_lock);
1064 pnfs_put_layout_hdr(lo);
1065 return ret;
1066}
1067
Fred Isamanf7e89172011-01-06 11:36:32 +00001068bool pnfs_roc(struct inode *ino)
1069{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001070 struct nfs_inode *nfsi = NFS_I(ino);
1071 struct nfs_open_context *ctx;
1072 struct nfs4_state *state;
Fred Isamanf7e89172011-01-06 11:36:32 +00001073 struct pnfs_layout_hdr *lo;
1074 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao193e3aa2014-11-17 09:30:41 +08001075 nfs4_stateid stateid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001076 LIST_HEAD(tmp_list);
Peng Tao193e3aa2014-11-17 09:30:41 +08001077 bool found = false, layoutreturn = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001078
1079 spin_lock(&ino->i_lock);
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001080 lo = nfsi->layout;
Fred Isamanf7e89172011-01-06 11:36:32 +00001081 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
1082 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001083 goto out_noroc;
1084
1085 /* Don't return layout if we hold a delegation */
1086 if (nfs4_check_delegation(ino, FMODE_READ))
1087 goto out_noroc;
1088
1089 list_for_each_entry(ctx, &nfsi->open_files, list) {
1090 state = ctx->state;
1091 /* Don't return layout if there is open file state */
1092 if (state != NULL && state->state != 0)
1093 goto out_noroc;
1094 }
1095
Trond Myklebuste2c63e02015-02-03 16:01:27 -05001096 goto out_noroc;
Peng Taoc8290132014-12-01 08:22:18 +08001097 pnfs_clear_retry_layoutget(lo);
Fred Isamanf7e89172011-01-06 11:36:32 +00001098 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
1099 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1100 mark_lseg_invalid(lseg, &tmp_list);
1101 found = true;
1102 }
1103 if (!found)
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001104 goto out_noroc;
Fred Isamanf7e89172011-01-06 11:36:32 +00001105 lo->plh_block_lgets++;
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001106 pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
Fred Isamanf7e89172011-01-06 11:36:32 +00001107 spin_unlock(&ino->i_lock);
1108 pnfs_free_lseg_list(&tmp_list);
1109 return true;
1110
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001111out_noroc:
Peng Tao193e3aa2014-11-17 09:30:41 +08001112 if (lo) {
1113 stateid = lo->plh_stateid;
1114 layoutreturn =
1115 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1116 &lo->plh_flags);
1117 if (layoutreturn) {
1118 lo->plh_block_lgets++;
1119 pnfs_get_layout_hdr(lo);
1120 }
1121 }
Fred Isamanf7e89172011-01-06 11:36:32 +00001122 spin_unlock(&ino->i_lock);
Peng Tao193e3aa2014-11-17 09:30:41 +08001123 if (layoutreturn)
Peng Tao27b6f532014-10-20 14:44:38 +08001124 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
Fred Isamanf7e89172011-01-06 11:36:32 +00001125 return false;
1126}
1127
1128void pnfs_roc_release(struct inode *ino)
1129{
1130 struct pnfs_layout_hdr *lo;
1131
1132 spin_lock(&ino->i_lock);
1133 lo = NFS_I(ino)->layout;
1134 lo->plh_block_lgets--;
Trond Myklebust6622c3e2012-09-20 17:23:11 -04001135 if (atomic_dec_and_test(&lo->plh_refcount)) {
1136 pnfs_detach_layout_hdr(lo);
1137 spin_unlock(&ino->i_lock);
1138 pnfs_free_layout_hdr(lo);
1139 } else
1140 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001141}
1142
1143void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1144{
1145 struct pnfs_layout_hdr *lo;
1146
1147 spin_lock(&ino->i_lock);
1148 lo = NFS_I(ino)->layout;
Trond Myklebust0f35ad62012-10-04 16:28:17 -07001149 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
Fred Isamanf7e89172011-01-06 11:36:32 +00001150 lo->plh_barrier = barrier;
1151 spin_unlock(&ino->i_lock);
1152}
1153
Trond Myklebust7fdab062012-09-20 20:15:57 -04001154bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
Fred Isamanf7e89172011-01-06 11:36:32 +00001155{
1156 struct nfs_inode *nfsi = NFS_I(ino);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001157 struct pnfs_layout_hdr *lo;
Fred Isamanf7e89172011-01-06 11:36:32 +00001158 struct pnfs_layout_segment *lseg;
Peng Tao193e3aa2014-11-17 09:30:41 +08001159 nfs4_stateid stateid;
Trond Myklebust7fdab062012-09-20 20:15:57 -04001160 u32 current_seqid;
Peng Tao193e3aa2014-11-17 09:30:41 +08001161 bool found = false, layoutreturn = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001162
1163 spin_lock(&ino->i_lock);
1164 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
1165 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
Trond Myklebust7fdab062012-09-20 20:15:57 -04001166 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
Fred Isamanf7e89172011-01-06 11:36:32 +00001167 found = true;
Trond Myklebust7fdab062012-09-20 20:15:57 -04001168 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00001169 }
Trond Myklebust7fdab062012-09-20 20:15:57 -04001170 lo = nfsi->layout;
1171 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
Fred Isamanf7e89172011-01-06 11:36:32 +00001172
Trond Myklebust7fdab062012-09-20 20:15:57 -04001173 /* Since close does not return a layout stateid for use as
1174 * a barrier, we choose the worst-case barrier.
1175 */
1176 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
1177out:
Peng Tao193e3aa2014-11-17 09:30:41 +08001178 if (!found) {
1179 stateid = lo->plh_stateid;
1180 layoutreturn =
1181 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1182 &lo->plh_flags);
1183 if (layoutreturn) {
1184 lo->plh_block_lgets++;
1185 pnfs_get_layout_hdr(lo);
1186 }
1187 }
Fred Isamanf7e89172011-01-06 11:36:32 +00001188 spin_unlock(&ino->i_lock);
Peng Tao193e3aa2014-11-17 09:30:41 +08001189 if (layoutreturn) {
1190 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
Peng Tao27b6f532014-10-20 14:44:38 +08001191 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, false);
Peng Tao193e3aa2014-11-17 09:30:41 +08001192 }
Fred Isamanf7e89172011-01-06 11:36:32 +00001193 return found;
1194}
1195
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001196/*
1197 * Compare two layout segments for sorting into layout cache.
1198 * We want to preferentially return RW over RO layouts, so ensure those
1199 * are seen first.
1200 */
1201static s64
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001202pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001203 const struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001204{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001205 s64 d;
1206
1207 /* high offset > low offset */
1208 d = l1->offset - l2->offset;
1209 if (d)
1210 return d;
1211
1212 /* short length > long length */
1213 d = l2->length - l1->length;
1214 if (d)
1215 return d;
1216
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001217 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001218 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001219}
1220
Andy Adamson974cec82010-10-20 00:18:02 -04001221static void
Trond Myklebust57036a32012-09-20 16:33:30 -04001222pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
Andy Adamson974cec82010-10-20 00:18:02 -04001223 struct pnfs_layout_segment *lseg)
1224{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001225 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001226
Andy Adamson974cec82010-10-20 00:18:02 -04001227 dprintk("%s:Begin\n", __func__);
1228
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001229 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001230 if (pnfs_lseg_range_cmp(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001231 continue;
Fred Isaman566052c2011-01-06 11:36:20 +00001232 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001233 dprintk("%s: inserted lseg %p "
1234 "iomode %d offset %llu length %llu before "
1235 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +00001236 __func__, lseg, lseg->pls_range.iomode,
1237 lseg->pls_range.offset, lseg->pls_range.length,
1238 lp, lp->pls_range.iomode, lp->pls_range.offset,
1239 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001240 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -04001241 }
Benny Halevyfb3296e2011-05-22 19:47:26 +03001242 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1243 dprintk("%s: inserted lseg %p "
1244 "iomode %d offset %llu length %llu at tail\n",
1245 __func__, lseg, lseg->pls_range.iomode,
1246 lseg->pls_range.offset, lseg->pls_range.length);
1247out:
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001248 pnfs_get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -04001249
1250 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001251}
1252
1253static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001254alloc_init_layout_hdr(struct inode *ino,
1255 struct nfs_open_context *ctx,
1256 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001257{
1258 struct pnfs_layout_hdr *lo;
1259
Benny Halevy636fb9c2011-05-22 19:51:33 +03001260 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001261 if (!lo)
1262 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +00001263 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001264 INIT_LIST_HEAD(&lo->plh_layouts);
1265 INIT_LIST_HEAD(&lo->plh_segs);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -05001266 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001267 lo->plh_inode = ino;
Trond Myklebust5cc22162013-05-21 09:26:49 -04001268 lo->plh_lc_cred = get_rpccred(ctx->cred);
Benny Halevye5e94012010-10-20 00:18:01 -04001269 return lo;
1270}
1271
1272static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001273pnfs_find_alloc_layout(struct inode *ino,
1274 struct nfs_open_context *ctx,
1275 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001276{
1277 struct nfs_inode *nfsi = NFS_I(ino);
1278 struct pnfs_layout_hdr *new = NULL;
1279
1280 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1281
Trond Myklebust251ec412012-10-02 15:41:05 -07001282 if (nfsi->layout != NULL)
1283 goto out_existing;
Benny Halevye5e94012010-10-20 00:18:01 -04001284 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001285 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001286 spin_lock(&ino->i_lock);
1287
Trond Myklebust251ec412012-10-02 15:41:05 -07001288 if (likely(nfsi->layout == NULL)) { /* Won the race? */
Benny Halevye5e94012010-10-20 00:18:01 -04001289 nfsi->layout = new;
Trond Myklebust251ec412012-10-02 15:41:05 -07001290 return new;
Yanchuan Nian7175fe92012-10-31 16:05:48 +08001291 } else if (new != NULL)
1292 pnfs_free_layout_hdr(new);
Trond Myklebust251ec412012-10-02 15:41:05 -07001293out_existing:
1294 pnfs_get_layout_hdr(nfsi->layout);
Benny Halevye5e94012010-10-20 00:18:01 -04001295 return nfsi->layout;
1296}
1297
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001298/*
1299 * iomode matching rules:
1300 * iomode lseg match
1301 * ----- ----- -----
1302 * ANY READ true
1303 * ANY RW true
1304 * RW READ false
1305 * RW RW true
1306 * READ READ true
1307 * READ RW true
1308 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001309static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001310pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001311 const struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001312{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001313 struct pnfs_layout_range range1;
1314
1315 if ((range->iomode == IOMODE_RW &&
1316 ls_range->iomode != IOMODE_RW) ||
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001317 !pnfs_lseg_range_intersecting(ls_range, range))
Benny Halevyfb3296e2011-05-22 19:47:26 +03001318 return 0;
1319
1320 /* range1 covers only the first byte in the range */
1321 range1 = *range;
1322 range1.length = 1;
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001323 return pnfs_lseg_range_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001324}
1325
1326/*
1327 * lookup range in layout
1328 */
Benny Halevye5e94012010-10-20 00:18:01 -04001329static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +03001330pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1331 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -04001332{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001333 struct pnfs_layout_segment *lseg, *ret = NULL;
1334
1335 dprintk("%s:Begin\n", __func__);
1336
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001337 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +00001338 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +08001339 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001340 pnfs_lseg_range_match(&lseg->pls_range, range)) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001341 ret = pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001342 break;
1343 }
Benny Halevyd771e3a2011-06-14 16:30:16 -04001344 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001345 break;
1346 }
1347
1348 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +00001349 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001350 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -04001351}
1352
1353/*
Andy Adamsond23d61c2012-05-23 05:02:37 -04001354 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1355 * to the MDS or over pNFS
1356 *
1357 * The nfs_inode read_io and write_io fields are cumulative counters reset
1358 * when there are no layout segments. Note that in pnfs_update_layout iomode
1359 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1360 * WRITE request.
1361 *
1362 * A return of true means use MDS I/O.
1363 *
1364 * From rfc 5661:
1365 * If a file's size is smaller than the file size threshold, data accesses
1366 * SHOULD be sent to the metadata server. If an I/O request has a length that
1367 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1368 * server. If both file size and I/O size are provided, the client SHOULD
1369 * reach or exceed both thresholds before sending its read or write
1370 * requests to the data server.
1371 */
1372static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1373 struct inode *ino, int iomode)
1374{
1375 struct nfs4_threshold *t = ctx->mdsthreshold;
1376 struct nfs_inode *nfsi = NFS_I(ino);
1377 loff_t fsize = i_size_read(ino);
1378 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1379
1380 if (t == NULL)
1381 return ret;
1382
1383 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1384 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1385
1386 switch (iomode) {
1387 case IOMODE_READ:
1388 if (t->bm & THRESHOLD_RD) {
1389 dprintk("%s fsize %llu\n", __func__, fsize);
1390 size_set = true;
1391 if (fsize < t->rd_sz)
1392 size = true;
1393 }
1394 if (t->bm & THRESHOLD_RD_IO) {
1395 dprintk("%s nfsi->read_io %llu\n", __func__,
1396 nfsi->read_io);
1397 io_set = true;
1398 if (nfsi->read_io < t->rd_io_sz)
1399 io = true;
1400 }
1401 break;
1402 case IOMODE_RW:
1403 if (t->bm & THRESHOLD_WR) {
1404 dprintk("%s fsize %llu\n", __func__, fsize);
1405 size_set = true;
1406 if (fsize < t->wr_sz)
1407 size = true;
1408 }
1409 if (t->bm & THRESHOLD_WR_IO) {
1410 dprintk("%s nfsi->write_io %llu\n", __func__,
1411 nfsi->write_io);
1412 io_set = true;
1413 if (nfsi->write_io < t->wr_io_sz)
1414 io = true;
1415 }
1416 break;
1417 }
1418 if (size_set && io_set) {
1419 if (size && io)
1420 ret = true;
1421 } else if (size || io)
1422 ret = true;
1423
1424 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1425 return ret;
1426}
1427
Peng Taoaa8a45e2014-12-01 08:22:23 +08001428/* stop waiting if someone clears NFS_LAYOUT_RETRY_LAYOUTGET bit. */
1429static int pnfs_layoutget_retry_bit_wait(struct wait_bit_key *key)
1430{
1431 if (!test_bit(NFS_LAYOUT_RETRY_LAYOUTGET, key->flags))
1432 return 1;
1433 return nfs_wait_bit_killable(key);
1434}
1435
1436static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1437{
1438 /*
1439 * send layoutcommit as it can hold up layoutreturn due to lseg
1440 * reference
1441 */
1442 pnfs_layoutcommit_inode(lo->plh_inode, false);
1443 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1444 pnfs_layoutget_retry_bit_wait,
1445 TASK_UNINTERRUPTIBLE);
1446}
1447
Tom Haynesd67ae822014-12-11 17:02:04 -05001448static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1449{
1450 unsigned long *bitlock = &lo->plh_flags;
1451
1452 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1453 smp_mb__after_atomic();
1454 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1455}
1456
Andy Adamsond23d61c2012-05-23 05:02:37 -04001457/*
Benny Halevye5e94012010-10-20 00:18:01 -04001458 * Layout segment is retreived from the server if not cached.
1459 * The appropriate layout segment is referenced and returned to the caller.
1460 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001461struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001462pnfs_update_layout(struct inode *ino,
1463 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001464 loff_t pos,
1465 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001466 enum pnfs_iomode iomode,
1467 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001468{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001469 struct pnfs_layout_range arg = {
1470 .iomode = iomode,
1471 .offset = pos,
1472 .length = count,
1473 };
Benny Halevy707ed5f2011-05-22 19:47:46 +03001474 unsigned pg_offset;
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001475 struct nfs_server *server = NFS_SERVER(ino);
1476 struct nfs_client *clp = server->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -04001477 struct pnfs_layout_hdr *lo;
1478 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05001479 bool first;
Benny Halevye5e94012010-10-20 00:18:01 -04001480
1481 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001482 goto out;
Andy Adamsond23d61c2012-05-23 05:02:37 -04001483
1484 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001485 goto out;
Andy Adamsond23d61c2012-05-23 05:02:37 -04001486
Peng Tao9bf87482014-08-22 17:37:41 +08001487lookup_again:
1488 first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001489 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001490 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001491 if (lo == NULL) {
1492 spin_unlock(&ino->i_lock);
1493 goto out;
1494 }
Benny Halevye5e94012010-10-20 00:18:01 -04001495
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001496 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001497 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001498 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001499 goto out_unlock;
1500 }
1501
1502 /* if LAYOUTGET already failed once we don't try again */
Peng Taoaa8a45e2014-12-01 08:22:23 +08001503 if (pnfs_layout_io_test_failed(lo, iomode) &&
1504 !pnfs_should_retry_layoutget(lo))
Benny Halevye5e94012010-10-20 00:18:01 -04001505 goto out_unlock;
1506
Peng Tao9bf87482014-08-22 17:37:41 +08001507 first = list_empty(&lo->plh_segs);
1508 if (first) {
1509 /* The first layoutget for the file. Need to serialize per
1510 * RFC 5661 Errata 3208.
1511 */
1512 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1513 &lo->plh_flags)) {
1514 spin_unlock(&ino->i_lock);
1515 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1516 TASK_UNINTERRUPTIBLE);
1517 pnfs_put_layout_hdr(lo);
1518 goto lookup_again;
1519 }
1520 } else {
1521 /* Check to see if the layout for the given range
1522 * already exists
1523 */
1524 lseg = pnfs_find_lseg(lo, &arg);
1525 if (lseg)
1526 goto out_unlock;
1527 }
Andy Adamson568e8c42011-03-01 01:34:22 +00001528
Peng Taoaa8a45e2014-12-01 08:22:23 +08001529 /*
1530 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1531 * for LAYOUTRETURN even if first is true.
1532 */
1533 if (!lseg && pnfs_should_retry_layoutget(lo) &&
1534 test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1535 spin_unlock(&ino->i_lock);
1536 dprintk("%s wait for layoutreturn\n", __func__);
1537 if (pnfs_prepare_to_retry_layoutget(lo)) {
Tom Haynesd67ae822014-12-11 17:02:04 -05001538 if (first)
1539 pnfs_clear_first_layoutget(lo);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001540 pnfs_put_layout_hdr(lo);
1541 dprintk("%s retrying\n", __func__);
1542 goto lookup_again;
1543 }
1544 goto out_put_layout_hdr;
1545 }
1546
Peng Taoce6ab4f2014-09-06 00:53:24 +08001547 if (pnfs_layoutgets_blocked(lo, &arg, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +00001548 goto out_unlock;
1549 atomic_inc(&lo->plh_outstanding);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001550 spin_unlock(&ino->i_lock);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001551
Peng Taoabb9a002014-08-22 17:37:40 +08001552 if (list_empty(&lo->plh_layouts)) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001553 /* The lo must be on the clp list if there is any
1554 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1555 */
1556 spin_lock(&clp->cl_lock);
Peng Taoabb9a002014-08-22 17:37:40 +08001557 if (list_empty(&lo->plh_layouts))
1558 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001559 spin_unlock(&clp->cl_lock);
1560 }
Benny Halevye5e94012010-10-20 00:18:01 -04001561
Benny Halevy707ed5f2011-05-22 19:47:46 +03001562 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1563 if (pg_offset) {
1564 arg.offset -= pg_offset;
1565 arg.length += pg_offset;
1566 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001567 if (arg.length != NFS4_MAX_UINT64)
1568 arg.length = PAGE_CACHE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001569
Benny Halevyfb3296e2011-05-22 19:47:26 +03001570 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Peng Taoc8290132014-12-01 08:22:18 +08001571 pnfs_clear_retry_layoutget(lo);
Fred Isamancf7d63f2011-01-06 11:36:25 +00001572 atomic_dec(&lo->plh_outstanding);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001573out_put_layout_hdr:
Tom Haynesd67ae822014-12-11 17:02:04 -05001574 if (first)
1575 pnfs_clear_first_layoutget(lo);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001576 pnfs_put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001577out:
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001578 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1579 "(%s, offset: %llu, length: %llu)\n",
1580 __func__, ino->i_sb->s_id,
1581 (unsigned long long)NFS_FILEID(ino),
1582 lseg == NULL ? "not found" : "found",
1583 iomode==IOMODE_RW ? "read/write" : "read-only",
1584 (unsigned long long)pos,
1585 (unsigned long long)count);
Benny Halevye5e94012010-10-20 00:18:01 -04001586 return lseg;
1587out_unlock:
1588 spin_unlock(&ino->i_lock);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001589 goto out_put_layout_hdr;
Benny Halevye5e94012010-10-20 00:18:01 -04001590}
Andy Adamson7c24d942011-06-13 18:22:38 -04001591EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001592
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001593struct pnfs_layout_segment *
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001594pnfs_layout_process(struct nfs4_layoutget *lgp)
1595{
1596 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1597 struct nfs4_layoutget_res *res = &lgp->res;
1598 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001599 struct inode *ino = lo->plh_inode;
Trond Myklebust78096cc2014-02-12 10:02:27 -05001600 LIST_HEAD(free_me);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001601 int status = 0;
1602
1603 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001604 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001605 if (!lseg || IS_ERR(lseg)) {
1606 if (!lseg)
1607 status = -ENOMEM;
1608 else
1609 status = PTR_ERR(lseg);
1610 dprintk("%s: Could not allocate layout: error %d\n",
1611 __func__, status);
1612 goto out;
1613 }
1614
Christoph Hellwig1013df612014-08-21 11:09:18 -05001615 init_lseg(lo, lseg);
1616 lseg->pls_range = res->range;
1617
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001618 spin_lock(&ino->i_lock);
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001619 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001620 dprintk("%s forget reply due to recall\n", __func__);
1621 goto out_forget_reply;
1622 }
1623
Peng Taoce6ab4f2014-09-06 00:53:24 +08001624 if (pnfs_layoutgets_blocked(lo, &lgp->args.range, 1)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001625 dprintk("%s forget reply due to state\n", __func__);
1626 goto out_forget_reply;
1627 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001628
Christoph Hellwig362f7472014-08-21 11:09:20 -05001629 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1630 /* existing state ID, make sure the sequence number matches. */
1631 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1632 dprintk("%s forget reply due to sequence\n", __func__);
1633 goto out_forget_reply;
1634 }
1635 pnfs_set_layout_stateid(lo, &res->stateid, false);
1636 } else {
1637 /*
1638 * We got an entirely new state ID. Mark all segments for the
1639 * inode invalid, and don't bother validating the stateid
1640 * sequence number.
1641 */
1642 pnfs_mark_matching_lsegs_invalid(lo, &free_me, NULL);
1643
1644 nfs4_stateid_copy(&lo->plh_stateid, &res->stateid);
1645 lo->plh_barrier = be32_to_cpu(res->stateid.seqid);
1646 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001647
Christoph Hellwig47abade2014-08-21 11:09:22 -05001648 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1649
Trond Myklebust9369a432012-09-18 20:57:08 -04001650 pnfs_get_lseg(lseg);
Trond Myklebust57036a32012-09-20 16:33:30 -04001651 pnfs_layout_insert_lseg(lo, lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001652
Fred Isamanf7e89172011-01-06 11:36:32 +00001653 if (res->return_on_close) {
1654 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1655 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1656 }
1657
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001658 spin_unlock(&ino->i_lock);
Trond Myklebust78096cc2014-02-12 10:02:27 -05001659 pnfs_free_lseg_list(&free_me);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001660 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001661out:
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001662 return ERR_PTR(status);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001663
1664out_forget_reply:
1665 spin_unlock(&ino->i_lock);
1666 lseg->pls_layout = lo;
1667 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1668 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001669}
1670
Peng Tao016256d2014-09-06 00:53:23 +08001671static void
1672pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1673 struct list_head *tmp_list,
1674 struct pnfs_layout_range *return_range)
1675{
1676 struct pnfs_layout_segment *lseg, *next;
1677
1678 dprintk("%s:Begin lo %p\n", __func__, lo);
1679
1680 if (list_empty(&lo->plh_segs))
1681 return;
1682
1683 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
1684 if (should_free_lseg(&lseg->pls_range, return_range)) {
1685 dprintk("%s: marking lseg %p iomode %d "
1686 "offset %llu length %llu\n", __func__,
1687 lseg, lseg->pls_range.iomode,
1688 lseg->pls_range.offset,
1689 lseg->pls_range.length);
1690 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1691 mark_lseg_invalid(lseg, tmp_list);
1692 }
1693}
1694
1695void pnfs_error_mark_layout_for_return(struct inode *inode,
1696 struct pnfs_layout_segment *lseg)
1697{
1698 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
1699 int iomode = pnfs_iomode_to_fail_bit(lseg->pls_range.iomode);
1700 struct pnfs_layout_range range = {
1701 .iomode = lseg->pls_range.iomode,
1702 .offset = 0,
1703 .length = NFS4_MAX_UINT64,
1704 };
1705 LIST_HEAD(free_me);
1706
1707 spin_lock(&inode->i_lock);
1708 /* set failure bit so that pnfs path will be retried later */
1709 pnfs_layout_set_fail_bit(lo, iomode);
1710 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1711 if (lo->plh_return_iomode == 0)
1712 lo->plh_return_iomode = range.iomode;
1713 else if (lo->plh_return_iomode != range.iomode)
1714 lo->plh_return_iomode = IOMODE_ANY;
1715 /*
1716 * mark all matching lsegs so that we are sure to have no live
1717 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1718 * for how it works.
1719 */
1720 pnfs_mark_matching_lsegs_return(lo, &free_me, &range);
1721 spin_unlock(&inode->i_lock);
1722 pnfs_free_lseg_list(&free_me);
1723}
1724EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1725
Trond Myklebustd8007d42011-06-10 13:30:23 -04001726void
1727pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1728{
Peng Tao1fd937b2012-09-25 14:55:57 +08001729 u64 rd_size = req->wb_bytes;
1730
Peng Taocb5d04b2015-01-24 22:14:52 +08001731 if (pgio->pg_lseg == NULL) {
1732 if (pgio->pg_dreq == NULL)
1733 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1734 else
1735 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001736
Peng Taocb5d04b2015-01-24 22:14:52 +08001737 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1738 req->wb_context,
1739 req_offset(req),
1740 rd_size,
1741 IOMODE_READ,
1742 GFP_KERNEL);
1743 }
Trond Myklebuste885de12011-06-10 13:30:23 -04001744 /* If no lseg, fall back to read through mds */
1745 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001746 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001747
Trond Myklebustd8007d42011-06-10 13:30:23 -04001748}
1749EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1750
1751void
Peng Tao62965562012-09-25 14:55:57 +08001752pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
1753 struct nfs_page *req, u64 wb_size)
Trond Myklebustd8007d42011-06-10 13:30:23 -04001754{
Peng Taocb5d04b2015-01-24 22:14:52 +08001755 if (pgio->pg_lseg == NULL)
1756 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1757 req->wb_context,
1758 req_offset(req),
1759 wb_size,
1760 IOMODE_RW,
1761 GFP_NOFS);
Trond Myklebuste885de12011-06-10 13:30:23 -04001762 /* If no lseg, fall back to write through mds */
1763 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001764 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001765}
1766EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1767
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04001768void
1769pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
1770{
1771 if (desc->pg_lseg) {
1772 pnfs_put_lseg(desc->pg_lseg);
1773 desc->pg_lseg = NULL;
1774 }
1775}
1776EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
1777
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -04001778/*
1779 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
1780 * of bytes (maximum @req->wb_bytes) that can be coalesced.
1781 */
1782size_t
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001783pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
1784 struct nfs_page *prev, struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001785{
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001786 unsigned int size;
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001787 u64 seg_end, req_start, seg_left;
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001788
1789 size = nfs_generic_pg_test(pgio, prev, req);
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001790 if (!size)
1791 return 0;
Benny Halevy89a58e32011-05-25 20:54:40 +03001792
Trond Myklebust19982ba2011-06-10 13:30:23 -04001793 /*
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001794 * 'size' contains the number of bytes left in the current page (up
1795 * to the original size asked for in @req->wb_bytes).
1796 *
1797 * Calculate how many bytes are left in the layout segment
1798 * and if there are less bytes than 'size', return that instead.
Trond Myklebust19982ba2011-06-10 13:30:23 -04001799 *
1800 * Please also note that 'end_offset' is actually the offset of the
1801 * first byte that lies outside the pnfs_layout_range. FIXME?
1802 *
1803 */
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001804 if (pgio->pg_lseg) {
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001805 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
1806 pgio->pg_lseg->pls_range.length);
1807 req_start = req_offset(req);
Weston Andros Adamson7c137892015-01-30 11:01:02 -05001808 WARN_ON_ONCE(req_start >= seg_end);
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001809 /* start of request is past the last byte of this segment */
Weston Andros Adamson7c137892015-01-30 11:01:02 -05001810 if (req_start >= seg_end) {
1811 /* reference the new lseg */
1812 if (pgio->pg_ops->pg_cleanup)
1813 pgio->pg_ops->pg_cleanup(pgio);
1814 if (pgio->pg_ops->pg_init)
1815 pgio->pg_ops->pg_init(pgio, req);
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001816 return 0;
Weston Andros Adamson7c137892015-01-30 11:01:02 -05001817 }
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001818
1819 /* adjust 'size' iff there are fewer bytes left in the
1820 * segment than what nfs_generic_pg_test returned */
1821 seg_left = seg_end - req_start;
1822 if (seg_left < size)
1823 size = (unsigned int)seg_left;
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001824 }
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001825
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001826 return size;
Fred Isamanbae724e2011-03-01 01:34:15 +00001827}
Benny Halevy89a58e32011-05-25 20:54:40 +03001828EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001829
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001830int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001831{
1832 struct nfs_pageio_descriptor pgio;
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001833
1834 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001835 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
1836 hdr->completion_ops);
1837 return nfs_pageio_resend(&pgio, hdr);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001838}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001839EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001840
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001841static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001842{
Fred Isamancd841602012-04-20 14:47:44 -04001843
1844 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1845 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001846 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001847 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001848 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04001849 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001850 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001851}
1852
Benny Halevyd20581a2011-05-22 19:52:03 +03001853/*
1854 * Called by non rpc-based layout drivers
1855 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001856void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001857{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001858 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
Fred Isamancd841602012-04-20 14:47:44 -04001859 if (!hdr->pnfs_error) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001860 pnfs_set_layoutcommit(hdr);
1861 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001862 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001863 pnfs_ld_handle_write_error(hdr);
1864 hdr->mds_ops->rpc_release(hdr);
Fred Isaman44b83792011-03-03 15:13:44 +00001865}
Benny Halevyd20581a2011-05-22 19:52:03 +03001866EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001867
Trond Myklebustdce81292011-07-13 15:59:19 -04001868static void
1869pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001870 struct nfs_pgio_header *hdr)
Trond Myklebustdce81292011-07-13 15:59:19 -04001871{
Peng Tao48d635f2014-11-10 08:35:35 +08001872 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001873
Fred Isaman6c75dc02012-04-20 14:47:47 -04001874 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001875 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001876 nfs_pageio_reset_write_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001877 mirror->pg_recoalesce = 1;
Fred Isaman6c75dc02012-04-20 14:47:47 -04001878 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001879 nfs_pgio_data_destroy(hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04001880}
1881
1882static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001883pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
Trond Myklebustdce81292011-07-13 15:59:19 -04001884 const struct rpc_call_ops *call_ops,
1885 struct pnfs_layout_segment *lseg,
1886 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00001887{
Fred Isamancd841602012-04-20 14:47:44 -04001888 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00001889 enum pnfs_try_status trypnfs;
1890 struct nfs_server *nfss = NFS_SERVER(inode);
1891
Fred Isamancd841602012-04-20 14:47:44 -04001892 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00001893
1894 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001895 inode->i_ino, hdr->args.count, hdr->args.offset, how);
1896 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001897 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00001898 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00001899 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1900 return trypnfs;
1901}
1902
Trond Myklebustdce81292011-07-13 15:59:19 -04001903static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001904pnfs_do_write(struct nfs_pageio_descriptor *desc,
1905 struct nfs_pgio_header *hdr, int how)
Trond Myklebustdce81292011-07-13 15:59:19 -04001906{
Trond Myklebustdce81292011-07-13 15:59:19 -04001907 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1908 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001909 enum pnfs_try_status trypnfs;
Trond Myklebustdce81292011-07-13 15:59:19 -04001910
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001911 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001912 if (trypnfs == PNFS_NOT_ATTEMPTED)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001913 pnfs_write_through_mds(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04001914}
1915
Fred Isaman6c75dc02012-04-20 14:47:47 -04001916static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1917{
Trond Myklebust9369a432012-09-18 20:57:08 -04001918 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04001919 nfs_pgio_header_free(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001920}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001921EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001922
Trond Myklebustdce81292011-07-13 15:59:19 -04001923int
1924pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1925{
Peng Tao48d635f2014-11-10 08:35:35 +08001926 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001927
Fred Isaman6c75dc02012-04-20 14:47:47 -04001928 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04001929 int ret;
1930
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04001931 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1932 if (!hdr) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001933 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001934 return -ENOMEM;
1935 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04001936 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04001937
Trond Myklebust9369a432012-09-18 20:57:08 -04001938 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04001939 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04001940 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001941 pnfs_do_write(desc, hdr, desc->pg_ioflags);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001942
Fred Isaman6c75dc02012-04-20 14:47:47 -04001943 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04001944}
1945EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1946
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001947int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebust62e4a762011-11-10 14:30:37 -05001948{
1949 struct nfs_pageio_descriptor pgio;
1950
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001951 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001952 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
1953 return nfs_pageio_resend(&pgio, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001954}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001955EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001956
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001957static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001958{
Fred Isamancd841602012-04-20 14:47:44 -04001959 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1960 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001961 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001962 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001963 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001964 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001965 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001966}
1967
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001968/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001969 * Called by non rpc-based layout drivers
1970 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001971void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03001972{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001973 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
Fred Isamancd841602012-04-20 14:47:44 -04001974 if (likely(!hdr->pnfs_error)) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001975 __nfs4_read_done_cb(hdr);
1976 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001977 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001978 pnfs_ld_handle_read_error(hdr);
1979 hdr->mds_ops->rpc_release(hdr);
Benny Halevyd20581a2011-05-22 19:52:03 +03001980}
1981EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1982
Trond Myklebust493292d2011-07-13 15:58:28 -04001983static void
1984pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001985 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04001986{
Peng Tao48d635f2014-11-10 08:35:35 +08001987 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001988
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001989 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001990 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001991 nfs_pageio_reset_read_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001992 mirror->pg_recoalesce = 1;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001993 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001994 nfs_pgio_data_destroy(hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04001995}
1996
Benny Halevyd20581a2011-05-22 19:52:03 +03001997/*
Andy Adamson64419a92011-03-01 01:34:16 +00001998 * Call the appropriate parallel I/O subsystem read function.
1999 */
Trond Myklebust493292d2011-07-13 15:58:28 -04002000static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002001pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
Trond Myklebust493292d2011-07-13 15:58:28 -04002002 const struct rpc_call_ops *call_ops,
2003 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00002004{
Fred Isamancd841602012-04-20 14:47:44 -04002005 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00002006 struct nfs_server *nfss = NFS_SERVER(inode);
2007 enum pnfs_try_status trypnfs;
2008
Fred Isamancd841602012-04-20 14:47:44 -04002009 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00002010
2011 dprintk("%s: Reading ino:%lu %u@%llu\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002012 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
Andy Adamson64419a92011-03-01 01:34:16 +00002013
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002014 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002015 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00002016 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00002017 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2018 return trypnfs;
2019}
Andy Adamson863a3c62011-03-23 13:27:54 +00002020
Peng Taoceb11e12014-11-10 08:35:38 +08002021/* Resend all requests through pnfs. */
2022int pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
2023{
2024 struct nfs_pageio_descriptor pgio;
2025
2026 nfs_pageio_init_read(&pgio, hdr->inode, false, hdr->completion_ops);
2027 return nfs_pageio_resend(&pgio, hdr);
2028}
2029EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2030
Trond Myklebust493292d2011-07-13 15:58:28 -04002031static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002032pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002033{
Trond Myklebust493292d2011-07-13 15:58:28 -04002034 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2035 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002036 enum pnfs_try_status trypnfs;
Peng Taoceb11e12014-11-10 08:35:38 +08002037 int err = 0;
Trond Myklebust493292d2011-07-13 15:58:28 -04002038
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002039 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
Peng Taoceb11e12014-11-10 08:35:38 +08002040 if (trypnfs == PNFS_TRY_AGAIN)
2041 err = pnfs_read_resend_pnfs(hdr);
2042 if (trypnfs == PNFS_NOT_ATTEMPTED || err)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002043 pnfs_read_through_mds(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002044}
2045
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002046static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2047{
Trond Myklebust9369a432012-09-18 20:57:08 -04002048 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002049 nfs_pgio_header_free(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002050}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002051EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002052
Trond Myklebust493292d2011-07-13 15:58:28 -04002053int
2054pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2055{
Peng Tao48d635f2014-11-10 08:35:35 +08002056 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002057
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002058 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04002059 int ret;
2060
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002061 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2062 if (!hdr) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002063 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002064 return -ENOMEM;
Trond Myklebust493292d2011-07-13 15:58:28 -04002065 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002066 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04002067 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002068 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002069 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002070 pnfs_do_read(desc, hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002071 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04002072}
2073EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2074
Trond Myklebust71244d92014-01-13 13:34:36 -05002075static void pnfs_clear_layoutcommitting(struct inode *inode)
2076{
2077 unsigned long *bitlock = &NFS_I(inode)->flags;
2078
2079 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002080 smp_mb__after_atomic();
Trond Myklebust71244d92014-01-13 13:34:36 -05002081 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2082}
2083
Andy Adamson863a3c62011-03-23 13:27:54 +00002084/*
Peng Taoa9bae562011-07-30 20:52:33 -04002085 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00002086 */
Peng Taoa9bae562011-07-30 20:52:33 -04002087static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00002088{
Peng Taoa9bae562011-07-30 20:52:33 -04002089 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00002090
Peng Taoa9bae562011-07-30 20:52:33 -04002091 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2092 if (lseg->pls_range.iomode == IOMODE_RW &&
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002093 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
Peng Taoa9bae562011-07-30 20:52:33 -04002094 list_add(&lseg->pls_lc_list, listp);
2095 }
Andy Adamson863a3c62011-03-23 13:27:54 +00002096}
2097
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002098static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2099{
2100 struct pnfs_layout_segment *lseg, *tmp;
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002101
2102 /* Matched by references in pnfs_set_layoutcommit */
2103 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2104 list_del_init(&lseg->pls_lc_list);
2105 pnfs_put_lseg(lseg);
2106 }
2107
Trond Myklebust71244d92014-01-13 13:34:36 -05002108 pnfs_clear_layoutcommitting(inode);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002109}
2110
Peng Tao1b0ae062011-09-22 21:50:12 -04002111void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2112{
Trond Myklebustb9e028f2012-09-18 16:41:18 -04002113 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
Peng Tao1b0ae062011-09-22 21:50:12 -04002114}
2115EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2116
Andy Adamson863a3c62011-03-23 13:27:54 +00002117void
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002118pnfs_set_layoutcommit(struct nfs_pgio_header *hdr)
Andy Adamson863a3c62011-03-23 13:27:54 +00002119{
Fred Isamancd841602012-04-20 14:47:44 -04002120 struct inode *inode = hdr->inode;
2121 struct nfs_inode *nfsi = NFS_I(inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002122 loff_t end_pos = hdr->mds_offset + hdr->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002123 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00002124
Fred Isamancd841602012-04-20 14:47:44 -04002125 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00002126 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002127 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00002128 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04002129 __func__, inode->i_ino);
Andy Adamson863a3c62011-03-23 13:27:54 +00002130 }
Fred Isamancd841602012-04-20 14:47:44 -04002131 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04002132 /* references matched in nfs4_layoutcommit_release */
Trond Myklebust9369a432012-09-18 20:57:08 -04002133 pnfs_get_lseg(hdr->lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04002134 }
Peng Taoacff58802011-07-30 20:52:31 -04002135 if (end_pos > nfsi->layout->plh_lwb)
2136 nfsi->layout->plh_lwb = end_pos;
Fred Isamancd841602012-04-20 14:47:44 -04002137 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04002138 dprintk("%s: lseg %p end_pos %llu\n",
Fred Isamancd841602012-04-20 14:47:44 -04002139 __func__, hdr->lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002140
2141 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2142 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2143 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04002144 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002145}
2146EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2147
Peng Tao378520b2014-08-07 10:15:02 +08002148void pnfs_commit_set_layoutcommit(struct nfs_commit_data *data)
2149{
2150 struct inode *inode = data->inode;
2151 struct nfs_inode *nfsi = NFS_I(inode);
2152 bool mark_as_dirty = false;
2153
2154 spin_lock(&inode->i_lock);
2155 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2156 mark_as_dirty = true;
2157 dprintk("%s: Set layoutcommit for inode %lu ",
2158 __func__, inode->i_ino);
2159 }
2160 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &data->lseg->pls_flags)) {
2161 /* references matched in nfs4_layoutcommit_release */
2162 pnfs_get_lseg(data->lseg);
2163 }
2164 if (data->lwb > nfsi->layout->plh_lwb)
2165 nfsi->layout->plh_lwb = data->lwb;
2166 spin_unlock(&inode->i_lock);
2167 dprintk("%s: lseg %p end_pos %llu\n",
2168 __func__, data->lseg, nfsi->layout->plh_lwb);
2169
2170 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2171 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2172 if (mark_as_dirty)
2173 mark_inode_dirty_sync(inode);
2174}
2175EXPORT_SYMBOL_GPL(pnfs_commit_set_layoutcommit);
2176
Andy Adamsondb29c082011-07-30 20:52:38 -04002177void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2178{
2179 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2180
2181 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2182 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002183 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
Andy Adamsondb29c082011-07-30 20:52:38 -04002184}
2185
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002186/*
2187 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2188 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2189 * data to disk to allow the server to recover the data if it crashes.
2190 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2191 * is off, and a COMMIT is sent to a data server, or
2192 * if WRITEs to a data server return NFS_DATA_SYNC.
2193 */
Andy Adamson863a3c62011-03-23 13:27:54 +00002194int
Andy Adamsonef311532011-03-12 02:58:10 -05002195pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00002196{
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002197 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
Andy Adamson863a3c62011-03-23 13:27:54 +00002198 struct nfs4_layoutcommit_data *data;
2199 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002200 loff_t end_pos;
Trond Myklebust71244d92014-01-13 13:34:36 -05002201 int status;
2202
2203 if (!pnfs_layoutcommit_outstanding(inode))
2204 return 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002205
2206 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2207
Trond Myklebust71244d92014-01-13 13:34:36 -05002208 status = -EAGAIN;
2209 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2210 if (!sync)
2211 goto out;
NeilBrown74316202014-07-07 15:16:04 +10002212 status = wait_on_bit_lock_action(&nfsi->flags,
Trond Myklebust71244d92014-01-13 13:34:36 -05002213 NFS_INO_LAYOUTCOMMITTING,
2214 nfs_wait_bit_killable,
2215 TASK_KILLABLE);
2216 if (status)
2217 goto out;
2218 }
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002219
Trond Myklebust71244d92014-01-13 13:34:36 -05002220 status = -ENOMEM;
Andy Adamson863a3c62011-03-23 13:27:54 +00002221 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2222 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebust71244d92014-01-13 13:34:36 -05002223 if (!data)
2224 goto clear_layoutcommitting;
Andy Adamson863a3c62011-03-23 13:27:54 +00002225
Trond Myklebust71244d92014-01-13 13:34:36 -05002226 status = 0;
2227 spin_lock(&inode->i_lock);
2228 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2229 goto out_unlock;
Peng Tao92407e72011-10-23 20:21:17 -07002230
Peng Taoa9bae562011-07-30 20:52:33 -04002231 INIT_LIST_HEAD(&data->lseg_list);
Peng Taoa9bae562011-07-30 20:52:33 -04002232 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00002233
Peng Taoacff58802011-07-30 20:52:31 -04002234 end_pos = nfsi->layout->plh_lwb;
Peng Taoacff58802011-07-30 20:52:31 -04002235 nfsi->layout->plh_lwb = 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002236
Trond Myklebustf597c532012-03-04 18:13:56 -05002237 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00002238 spin_unlock(&inode->i_lock);
2239
2240 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04002241 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00002242 nfs_fattr_init(&data->fattr);
2243 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2244 data->res.fattr = &data->fattr;
2245 data->args.lastbytewritten = end_pos - 1;
2246 data->res.server = NFS_SERVER(inode);
2247
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002248 if (ld->prepare_layoutcommit) {
2249 status = ld->prepare_layoutcommit(&data->args);
2250 if (status) {
2251 spin_lock(&inode->i_lock);
2252 if (end_pos < nfsi->layout->plh_lwb)
2253 nfsi->layout->plh_lwb = end_pos;
2254 spin_unlock(&inode->i_lock);
2255 put_rpccred(data->cred);
2256 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2257 goto clear_layoutcommitting;
2258 }
2259 }
2260
2261
Andy Adamson863a3c62011-03-23 13:27:54 +00002262 status = nfs4_proc_layoutcommit(data, sync);
2263out:
Peng Tao92407e72011-10-23 20:21:17 -07002264 if (status)
2265 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002266 dprintk("<-- %s status %d\n", __func__, status);
2267 return status;
Trond Myklebust71244d92014-01-13 13:34:36 -05002268out_unlock:
2269 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07002270 kfree(data);
Trond Myklebust71244d92014-01-13 13:34:36 -05002271clear_layoutcommitting:
2272 pnfs_clear_layoutcommitting(inode);
Peng Tao92407e72011-10-23 20:21:17 -07002273 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00002274}
Peng Tao72cff442014-08-07 10:12:38 +08002275EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
Andy Adamson82be4172012-05-23 05:02:35 -04002276
2277struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2278{
2279 struct nfs4_threshold *thp;
2280
2281 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2282 if (!thp) {
2283 dprintk("%s mdsthreshold allocation failed\n", __func__);
2284 return NULL;
2285 }
2286 return thp;
2287}