]>
Commit | Line | Data |
---|---|---|
85e174ba RL |
1 | /* |
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> | |
493292dd | 31 | #include <linux/nfs_page.h> |
143cb494 | 32 | #include <linux/module.h> |
974cec8c | 33 | #include "internal.h" |
85e174ba | 34 | #include "pnfs.h" |
64419a9b | 35 | #include "iostat.h" |
cc668ab3 | 36 | #include "nfs4trace.h" |
40dd4b7a | 37 | #include "delegation.h" |
8733408d | 38 | #include "nfs42.h" |
85e174ba RL |
39 | |
40 | #define NFSDBG_FACILITY NFSDBG_PNFS | |
25c75333 | 41 | #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ) |
85e174ba | 42 | |
02c35fca FI |
43 | /* Locking: |
44 | * | |
45 | * pnfs_spinlock: | |
46 | * protects pnfs_modules_tbl. | |
47 | */ | |
48 | static DEFINE_SPINLOCK(pnfs_spinlock); | |
49 | ||
50 | /* | |
51 | * pnfs_modules_tbl holds all pnfs modules | |
52 | */ | |
53 | static LIST_HEAD(pnfs_modules_tbl); | |
54 | ||
13c13a6a | 55 | static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo); |
aa1e0e3a | 56 | |
02c35fca FI |
57 | /* Return the registered pnfs layout driver module matching given id */ |
58 | static struct pnfs_layoutdriver_type * | |
59 | find_pnfs_driver_locked(u32 id) | |
60 | { | |
61 | struct pnfs_layoutdriver_type *local; | |
62 | ||
63 | list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) | |
64 | if (local->id == id) | |
65 | goto out; | |
66 | local = NULL; | |
67 | out: | |
68 | dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); | |
69 | return local; | |
70 | } | |
71 | ||
85e174ba RL |
72 | static struct pnfs_layoutdriver_type * |
73 | find_pnfs_driver(u32 id) | |
74 | { | |
02c35fca FI |
75 | struct pnfs_layoutdriver_type *local; |
76 | ||
77 | spin_lock(&pnfs_spinlock); | |
78 | local = find_pnfs_driver_locked(id); | |
0a9c63fa TM |
79 | if (local != NULL && !try_module_get(local->owner)) { |
80 | dprintk("%s: Could not grab reference on module\n", __func__); | |
81 | local = NULL; | |
82 | } | |
02c35fca FI |
83 | spin_unlock(&pnfs_spinlock); |
84 | return local; | |
85e174ba RL |
85 | } |
86 | ||
87 | void | |
88 | unset_pnfs_layoutdriver(struct nfs_server *nfss) | |
89 | { | |
738fd0f3 BH |
90 | if (nfss->pnfs_curr_ld) { |
91 | if (nfss->pnfs_curr_ld->clear_layoutdriver) | |
92 | nfss->pnfs_curr_ld->clear_layoutdriver(nfss); | |
2a4c8994 TM |
93 | /* Decrement the MDS count. Purge the deviceid cache if zero */ |
94 | if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count)) | |
95 | nfs4_deviceid_purge_client(nfss->nfs_client); | |
02c35fca | 96 | module_put(nfss->pnfs_curr_ld->owner); |
738fd0f3 | 97 | } |
85e174ba RL |
98 | nfss->pnfs_curr_ld = NULL; |
99 | } | |
100 | ||
101 | /* | |
102 | * Try to set the server's pnfs module to the pnfs layout type specified by id. | |
103 | * Currently only one pNFS layout driver per filesystem is supported. | |
104 | * | |
105 | * @id layout type. Zero (illegal layout type) indicates pNFS not in use. | |
106 | */ | |
107 | void | |
738fd0f3 BH |
108 | set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh, |
109 | u32 id) | |
85e174ba RL |
110 | { |
111 | struct pnfs_layoutdriver_type *ld_type = NULL; | |
112 | ||
113 | if (id == 0) | |
114 | goto out_no_driver; | |
115 | if (!(server->nfs_client->cl_exchange_flags & | |
116 | (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { | |
a030889a WAA |
117 | printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n", |
118 | __func__, id, server->nfs_client->cl_exchange_flags); | |
85e174ba RL |
119 | goto out_no_driver; |
120 | } | |
121 | ld_type = find_pnfs_driver(id); | |
122 | if (!ld_type) { | |
123 | request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id); | |
124 | ld_type = find_pnfs_driver(id); | |
125 | if (!ld_type) { | |
126 | dprintk("%s: No pNFS module found for %u.\n", | |
127 | __func__, id); | |
128 | goto out_no_driver; | |
129 | } | |
130 | } | |
131 | server->pnfs_curr_ld = ld_type; | |
738fd0f3 BH |
132 | if (ld_type->set_layoutdriver |
133 | && ld_type->set_layoutdriver(server, mntfh)) { | |
a030889a WAA |
134 | printk(KERN_ERR "NFS: %s: Error initializing pNFS layout " |
135 | "driver %u.\n", __func__, id); | |
738fd0f3 BH |
136 | module_put(ld_type->owner); |
137 | goto out_no_driver; | |
138 | } | |
2a4c8994 TM |
139 | /* Bump the MDS count */ |
140 | atomic_inc(&server->nfs_client->cl_mds_count); | |
ea8eecdd | 141 | |
85e174ba RL |
142 | dprintk("%s: pNFS module for %u set\n", __func__, id); |
143 | return; | |
144 | ||
145 | out_no_driver: | |
146 | dprintk("%s: Using NFSv4 I/O\n", __func__); | |
147 | server->pnfs_curr_ld = NULL; | |
148 | } | |
02c35fca FI |
149 | |
150 | int | |
151 | pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) | |
152 | { | |
153 | int status = -EINVAL; | |
154 | struct pnfs_layoutdriver_type *tmp; | |
155 | ||
156 | if (ld_type->id == 0) { | |
a030889a | 157 | printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__); |
02c35fca FI |
158 | return status; |
159 | } | |
b1f69b75 | 160 | if (!ld_type->alloc_lseg || !ld_type->free_lseg) { |
a030889a | 161 | printk(KERN_ERR "NFS: %s Layout driver must provide " |
b1f69b75 AA |
162 | "alloc_lseg and free_lseg.\n", __func__); |
163 | return status; | |
164 | } | |
02c35fca FI |
165 | |
166 | spin_lock(&pnfs_spinlock); | |
167 | tmp = find_pnfs_driver_locked(ld_type->id); | |
168 | if (!tmp) { | |
169 | list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); | |
170 | status = 0; | |
171 | dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, | |
172 | ld_type->name); | |
173 | } else { | |
a030889a | 174 | printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n", |
02c35fca FI |
175 | __func__, ld_type->id); |
176 | } | |
177 | spin_unlock(&pnfs_spinlock); | |
178 | ||
179 | return status; | |
180 | } | |
181 | EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); | |
182 | ||
183 | void | |
184 | pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) | |
185 | { | |
186 | dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); | |
187 | spin_lock(&pnfs_spinlock); | |
188 | list_del(&ld_type->pnfs_tblid); | |
189 | spin_unlock(&pnfs_spinlock); | |
190 | } | |
191 | EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); | |
e5e94017 | 192 | |
b1f69b75 AA |
193 | /* |
194 | * pNFS client layout cache | |
195 | */ | |
196 | ||
cc6e5340 | 197 | /* Need to hold i_lock if caller does not already hold reference */ |
43f1b3da | 198 | void |
70c3bd2b | 199 | pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo) |
e5e94017 | 200 | { |
cc6e5340 | 201 | atomic_inc(&lo->plh_refcount); |
e5e94017 BH |
202 | } |
203 | ||
636fb9c8 BH |
204 | static struct pnfs_layout_hdr * |
205 | pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags) | |
206 | { | |
207 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; | |
57934278 | 208 | return ld->alloc_layout_hdr(ino, gfp_flags); |
636fb9c8 BH |
209 | } |
210 | ||
211 | static void | |
212 | pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
213 | { | |
9c626381 TM |
214 | struct nfs_server *server = NFS_SERVER(lo->plh_inode); |
215 | struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld; | |
216 | ||
217 | if (!list_empty(&lo->plh_layouts)) { | |
218 | struct nfs_client *clp = server->nfs_client; | |
219 | ||
220 | spin_lock(&clp->cl_lock); | |
221 | list_del_init(&lo->plh_layouts); | |
222 | spin_unlock(&clp->cl_lock); | |
223 | } | |
9fa40758 | 224 | put_rpccred(lo->plh_lc_cred); |
57934278 | 225 | return ld->free_layout_hdr(lo); |
636fb9c8 BH |
226 | } |
227 | ||
e5e94017 | 228 | static void |
6622c3ea | 229 | pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo) |
e5e94017 | 230 | { |
bb346f63 | 231 | struct nfs_inode *nfsi = NFS_I(lo->plh_inode); |
cc6e5340 | 232 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
bb346f63 TM |
233 | nfsi->layout = NULL; |
234 | /* Reset MDS Threshold I/O counters */ | |
235 | nfsi->write_io = 0; | |
236 | nfsi->read_io = 0; | |
e5e94017 BH |
237 | } |
238 | ||
b1f69b75 | 239 | void |
70c3bd2b | 240 | pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) |
974cec8c | 241 | { |
cc6e5340 FI |
242 | struct inode *inode = lo->plh_inode; |
243 | ||
13c13a6a TM |
244 | pnfs_layoutreturn_before_put_layout_hdr(lo); |
245 | ||
cc6e5340 | 246 | if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { |
566f8737 PT |
247 | if (!list_empty(&lo->plh_segs)) |
248 | WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n"); | |
6622c3ea | 249 | pnfs_detach_layout_hdr(lo); |
cc6e5340 | 250 | spin_unlock(&inode->i_lock); |
6622c3ea | 251 | pnfs_free_layout_hdr(lo); |
cc6e5340 | 252 | } |
974cec8c AA |
253 | } |
254 | ||
2454dfea TM |
255 | /* |
256 | * Mark a pnfs_layout_hdr and all associated layout segments as invalid | |
257 | * | |
258 | * In order to continue using the pnfs_layout_hdr, a full recovery | |
259 | * is required. | |
260 | * Note that caller must hold inode->i_lock. | |
261 | */ | |
5f46be04 | 262 | int |
2454dfea TM |
263 | pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo, |
264 | struct list_head *lseg_list) | |
265 | { | |
266 | struct pnfs_layout_range range = { | |
267 | .iomode = IOMODE_ANY, | |
268 | .offset = 0, | |
269 | .length = NFS4_MAX_UINT64, | |
270 | }; | |
271 | ||
272 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); | |
6d597e17 | 273 | return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0); |
2454dfea TM |
274 | } |
275 | ||
b9e028fd TM |
276 | static int |
277 | pnfs_iomode_to_fail_bit(u32 iomode) | |
278 | { | |
279 | return iomode == IOMODE_RW ? | |
280 | NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED; | |
281 | } | |
282 | ||
283 | static void | |
3e621214 | 284 | pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) |
b9e028fd | 285 | { |
25c75333 | 286 | lo->plh_retry_timestamp = jiffies; |
39e88fcf | 287 | if (!test_and_set_bit(fail_bit, &lo->plh_flags)) |
3e621214 TM |
288 | atomic_inc(&lo->plh_refcount); |
289 | } | |
290 | ||
291 | static void | |
292 | pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) | |
293 | { | |
294 | if (test_and_clear_bit(fail_bit, &lo->plh_flags)) | |
295 | atomic_dec(&lo->plh_refcount); | |
296 | } | |
297 | ||
298 | static void | |
299 | pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode) | |
300 | { | |
301 | struct inode *inode = lo->plh_inode; | |
115ce575 TM |
302 | struct pnfs_layout_range range = { |
303 | .iomode = iomode, | |
304 | .offset = 0, | |
305 | .length = NFS4_MAX_UINT64, | |
306 | }; | |
307 | LIST_HEAD(head); | |
3e621214 TM |
308 | |
309 | spin_lock(&inode->i_lock); | |
310 | pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); | |
6d597e17 | 311 | pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0); |
3e621214 | 312 | spin_unlock(&inode->i_lock); |
115ce575 | 313 | pnfs_free_lseg_list(&head); |
b9e028fd TM |
314 | dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__, |
315 | iomode == IOMODE_RW ? "RW" : "READ"); | |
316 | } | |
317 | ||
318 | static bool | |
319 | pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode) | |
320 | { | |
25c75333 | 321 | unsigned long start, end; |
3e621214 TM |
322 | int fail_bit = pnfs_iomode_to_fail_bit(iomode); |
323 | ||
324 | if (test_bit(fail_bit, &lo->plh_flags) == 0) | |
25c75333 TM |
325 | return false; |
326 | end = jiffies; | |
327 | start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT; | |
328 | if (!time_in_range(lo->plh_retry_timestamp, start, end)) { | |
329 | /* It is time to retry the failed layoutgets */ | |
3e621214 | 330 | pnfs_layout_clear_fail_bit(lo, fail_bit); |
25c75333 TM |
331 | return false; |
332 | } | |
333 | return true; | |
b9e028fd TM |
334 | } |
335 | ||
974cec8c | 336 | static void |
119cef97 TM |
337 | pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg, |
338 | const struct pnfs_layout_range *range, | |
339 | const nfs4_stateid *stateid) | |
974cec8c | 340 | { |
566052c5 | 341 | INIT_LIST_HEAD(&lseg->pls_list); |
a9bae566 | 342 | INIT_LIST_HEAD(&lseg->pls_lc_list); |
4541d16c | 343 | atomic_set(&lseg->pls_refcount, 1); |
4541d16c | 344 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
566052c5 | 345 | lseg->pls_layout = lo; |
119cef97 TM |
346 | lseg->pls_range = *range; |
347 | lseg->pls_seq = be32_to_cpu(stateid->seqid); | |
974cec8c AA |
348 | } |
349 | ||
905ca191 | 350 | static void pnfs_free_lseg(struct pnfs_layout_segment *lseg) |
974cec8c | 351 | { |
b7edfaa1 | 352 | struct inode *ino = lseg->pls_layout->plh_inode; |
974cec8c | 353 | |
b1f69b75 | 354 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
974cec8c AA |
355 | } |
356 | ||
d684d2ae | 357 | static void |
57036a37 TM |
358 | pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo, |
359 | struct pnfs_layout_segment *lseg) | |
d684d2ae | 360 | { |
57036a37 | 361 | struct inode *inode = lo->plh_inode; |
d684d2ae | 362 | |
d20581aa | 363 | WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
d684d2ae | 364 | list_del_init(&lseg->pls_list); |
8f0d27dc TM |
365 | /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */ |
366 | atomic_dec(&lo->plh_refcount); | |
2d148c7e TM |
367 | if (list_empty(&lo->plh_segs)) { |
368 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); | |
173f77e9 | 369 | clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
2d148c7e | 370 | } |
d684d2ae FI |
371 | rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq); |
372 | } | |
373 | ||
bae724ef | 374 | void |
9369a431 | 375 | pnfs_put_lseg(struct pnfs_layout_segment *lseg) |
974cec8c | 376 | { |
57036a37 | 377 | struct pnfs_layout_hdr *lo; |
d684d2ae FI |
378 | struct inode *inode; |
379 | ||
380 | if (!lseg) | |
381 | return; | |
382 | ||
4541d16c FI |
383 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
384 | atomic_read(&lseg->pls_refcount), | |
385 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); | |
4ef2e4f8 | 386 | |
57036a37 TM |
387 | lo = lseg->pls_layout; |
388 | inode = lo->plh_inode; | |
4ef2e4f8 | 389 | |
d684d2ae | 390 | if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { |
faa4a54f TM |
391 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
392 | spin_unlock(&inode->i_lock); | |
393 | return; | |
394 | } | |
8f0d27dc | 395 | pnfs_get_layout_hdr(lo); |
4ef2e4f8 TM |
396 | pnfs_layout_remove_lseg(lo, lseg); |
397 | spin_unlock(&inode->i_lock); | |
398 | pnfs_free_lseg(lseg); | |
399 | pnfs_put_layout_hdr(lo); | |
4541d16c | 400 | } |
4541d16c | 401 | } |
9369a431 | 402 | EXPORT_SYMBOL_GPL(pnfs_put_lseg); |
974cec8c | 403 | |
6543f803 | 404 | static void pnfs_free_lseg_async_work(struct work_struct *work) |
e6cf82d1 WAA |
405 | { |
406 | struct pnfs_layout_segment *lseg; | |
6543f803 | 407 | struct pnfs_layout_hdr *lo; |
e6cf82d1 WAA |
408 | |
409 | lseg = container_of(work, struct pnfs_layout_segment, pls_work); | |
6543f803 | 410 | lo = lseg->pls_layout; |
e6cf82d1 | 411 | |
6543f803 TM |
412 | pnfs_free_lseg(lseg); |
413 | pnfs_put_layout_hdr(lo); | |
e6cf82d1 WAA |
414 | } |
415 | ||
6543f803 | 416 | static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg) |
e6cf82d1 | 417 | { |
6543f803 | 418 | INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work); |
e6cf82d1 WAA |
419 | schedule_work(&lseg->pls_work); |
420 | } | |
6543f803 TM |
421 | |
422 | void | |
423 | pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg) | |
424 | { | |
425 | if (!lseg) | |
426 | return; | |
427 | ||
428 | assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock); | |
429 | ||
430 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, | |
431 | atomic_read(&lseg->pls_refcount), | |
432 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); | |
433 | if (atomic_dec_and_test(&lseg->pls_refcount)) { | |
434 | struct pnfs_layout_hdr *lo = lseg->pls_layout; | |
faa4a54f TM |
435 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) |
436 | return; | |
6543f803 TM |
437 | pnfs_get_layout_hdr(lo); |
438 | pnfs_layout_remove_lseg(lo, lseg); | |
439 | pnfs_free_lseg_async(lseg); | |
440 | } | |
441 | } | |
442 | EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked); | |
e6cf82d1 | 443 | |
3cb2df17 | 444 | static u64 |
fb3296eb BH |
445 | end_offset(u64 start, u64 len) |
446 | { | |
447 | u64 end; | |
448 | ||
449 | end = start + len; | |
450 | return end >= start ? end : NFS4_MAX_UINT64; | |
451 | } | |
452 | ||
fb3296eb BH |
453 | /* |
454 | * is l2 fully contained in l1? | |
455 | * start1 end1 | |
456 | * [----------------------------------) | |
457 | * start2 end2 | |
458 | * [----------------) | |
459 | */ | |
3cb2df17 | 460 | static bool |
7dc0ac70 | 461 | pnfs_lseg_range_contained(const struct pnfs_layout_range *l1, |
3cb2df17 | 462 | const struct pnfs_layout_range *l2) |
fb3296eb BH |
463 | { |
464 | u64 start1 = l1->offset; | |
465 | u64 end1 = end_offset(start1, l1->length); | |
466 | u64 start2 = l2->offset; | |
467 | u64 end2 = end_offset(start2, l2->length); | |
468 | ||
469 | return (start1 <= start2) && (end1 >= end2); | |
470 | } | |
471 | ||
472 | /* | |
473 | * is l1 and l2 intersecting? | |
474 | * start1 end1 | |
475 | * [----------------------------------) | |
476 | * start2 end2 | |
477 | * [----------------) | |
478 | */ | |
3cb2df17 | 479 | static bool |
7dc0ac70 | 480 | pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1, |
3cb2df17 | 481 | const struct pnfs_layout_range *l2) |
fb3296eb BH |
482 | { |
483 | u64 start1 = l1->offset; | |
484 | u64 end1 = end_offset(start1, l1->length); | |
485 | u64 start2 = l2->offset; | |
486 | u64 end2 = end_offset(start2, l2->length); | |
487 | ||
488 | return (end1 == NFS4_MAX_UINT64 || end1 > start2) && | |
489 | (end2 == NFS4_MAX_UINT64 || end2 > start1); | |
490 | } | |
491 | ||
24956804 TM |
492 | static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg, |
493 | struct list_head *tmp_list) | |
494 | { | |
495 | if (!atomic_dec_and_test(&lseg->pls_refcount)) | |
496 | return false; | |
497 | pnfs_layout_remove_lseg(lseg->pls_layout, lseg); | |
498 | list_add(&lseg->pls_list, tmp_list); | |
499 | return true; | |
500 | } | |
501 | ||
4541d16c FI |
502 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
503 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, | |
504 | struct list_head *tmp_list) | |
505 | { | |
506 | int rv = 0; | |
507 | ||
508 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { | |
509 | /* Remove the reference keeping the lseg in the | |
510 | * list. It will now be removed when all | |
511 | * outstanding io is finished. | |
512 | */ | |
d684d2ae FI |
513 | dprintk("%s: lseg %p ref %d\n", __func__, lseg, |
514 | atomic_read(&lseg->pls_refcount)); | |
24956804 | 515 | if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list)) |
d684d2ae | 516 | rv = 1; |
4541d16c FI |
517 | } |
518 | return rv; | |
519 | } | |
520 | ||
6d597e17 JL |
521 | /* |
522 | * Compare 2 layout stateid sequence ids, to see which is newer, | |
523 | * taking into account wraparound issues. | |
524 | */ | |
525 | static bool pnfs_seqid_is_newer(u32 s1, u32 s2) | |
526 | { | |
527 | return (s32)(s1 - s2) > 0; | |
528 | } | |
529 | ||
e036f464 TM |
530 | static bool |
531 | pnfs_should_free_range(const struct pnfs_layout_range *lseg_range, | |
532 | const struct pnfs_layout_range *recall_range) | |
533 | { | |
534 | return (recall_range->iomode == IOMODE_ANY || | |
535 | lseg_range->iomode == recall_range->iomode) && | |
536 | pnfs_lseg_range_intersecting(lseg_range, recall_range); | |
537 | } | |
538 | ||
539 | static bool | |
540 | pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg, | |
541 | const struct pnfs_layout_range *recall_range, | |
542 | u32 seq) | |
543 | { | |
544 | if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq)) | |
545 | return false; | |
546 | if (recall_range == NULL) | |
547 | return true; | |
548 | return pnfs_should_free_range(&lseg->pls_range, recall_range); | |
549 | } | |
550 | ||
6d597e17 JL |
551 | /** |
552 | * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later | |
553 | * @lo: layout header containing the lsegs | |
554 | * @tmp_list: list head where doomed lsegs should go | |
555 | * @recall_range: optional recall range argument to match (may be NULL) | |
556 | * @seq: only invalidate lsegs obtained prior to this sequence (may be 0) | |
557 | * | |
558 | * Walk the list of lsegs in the layout header, and tear down any that should | |
559 | * be destroyed. If "recall_range" is specified then the segment must match | |
560 | * that range. If "seq" is non-zero, then only match segments that were handed | |
561 | * out at or before that sequence. | |
562 | * | |
563 | * Returns number of matching invalid lsegs remaining in list after scanning | |
564 | * it and purging them. | |
4541d16c | 565 | */ |
43f1b3da | 566 | int |
49a85061 | 567 | pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
4541d16c | 568 | struct list_head *tmp_list, |
6d597e17 JL |
569 | const struct pnfs_layout_range *recall_range, |
570 | u32 seq) | |
974cec8c AA |
571 | { |
572 | struct pnfs_layout_segment *lseg, *next; | |
71b39854 | 573 | int remaining = 0; |
974cec8c AA |
574 | |
575 | dprintk("%s:Begin lo %p\n", __func__, lo); | |
576 | ||
8006bfba | 577 | if (list_empty(&lo->plh_segs)) |
38511722 | 578 | return 0; |
4541d16c | 579 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
e036f464 | 580 | if (pnfs_match_lseg_recall(lseg, recall_range, seq)) { |
6d597e17 | 581 | dprintk("%s: freeing lseg %p iomode %d seq %u" |
4541d16c | 582 | "offset %llu length %llu\n", __func__, |
6d597e17 JL |
583 | lseg, lseg->pls_range.iomode, lseg->pls_seq, |
584 | lseg->pls_range.offset, lseg->pls_range.length); | |
71b39854 TM |
585 | if (!mark_lseg_invalid(lseg, tmp_list)) |
586 | remaining++; | |
4541d16c | 587 | } |
71b39854 TM |
588 | dprintk("%s:Return %i\n", __func__, remaining); |
589 | return remaining; | |
974cec8c AA |
590 | } |
591 | ||
f49f9baa | 592 | /* note free_me must contain lsegs from a single layout_hdr */ |
43f1b3da | 593 | void |
4541d16c | 594 | pnfs_free_lseg_list(struct list_head *free_me) |
974cec8c | 595 | { |
4541d16c | 596 | struct pnfs_layout_segment *lseg, *tmp; |
f49f9baa FI |
597 | |
598 | if (list_empty(free_me)) | |
599 | return; | |
600 | ||
4541d16c | 601 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
566052c5 | 602 | list_del(&lseg->pls_list); |
905ca191 | 603 | pnfs_free_lseg(lseg); |
974cec8c AA |
604 | } |
605 | } | |
606 | ||
e5e94017 BH |
607 | void |
608 | pnfs_destroy_layout(struct nfs_inode *nfsi) | |
609 | { | |
610 | struct pnfs_layout_hdr *lo; | |
974cec8c | 611 | LIST_HEAD(tmp_list); |
e5e94017 BH |
612 | |
613 | spin_lock(&nfsi->vfs_inode.i_lock); | |
614 | lo = nfsi->layout; | |
615 | if (lo) { | |
3e621214 | 616 | pnfs_get_layout_hdr(lo); |
2454dfea | 617 | pnfs_mark_layout_stateid_invalid(lo, &tmp_list); |
3e621214 TM |
618 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED); |
619 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED); | |
620 | 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); | |
974cec8c | 625 | } |
041245c8 | 626 | EXPORT_SYMBOL_GPL(pnfs_destroy_layout); |
974cec8c | 627 | |
fd9a8d71 TM |
628 | static bool |
629 | pnfs_layout_add_bulk_destroy_list(struct inode *inode, | |
630 | struct list_head *layout_list) | |
974cec8c AA |
631 | { |
632 | struct pnfs_layout_hdr *lo; | |
fd9a8d71 | 633 | bool ret = false; |
974cec8c | 634 | |
fd9a8d71 TM |
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 */ | |
647 | static int | |
648 | pnfs_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 | ||
672 | static int | |
673 | pnfs_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; | |
fd9a8d71 TM |
678 | LIST_HEAD(lseg_list); |
679 | int ret = 0; | |
680 | ||
681 | while (!list_empty(layout_list)) { | |
682 | lo = list_entry(layout_list->next, struct pnfs_layout_hdr, | |
683 | plh_bulk_destroy); | |
684 | dprintk("%s freeing layout for inode %lu\n", __func__, | |
685 | lo->plh_inode->i_ino); | |
686 | inode = lo->plh_inode; | |
7c5d1875 CH |
687 | |
688 | pnfs_layoutcommit_inode(inode, false); | |
689 | ||
fd9a8d71 TM |
690 | spin_lock(&inode->i_lock); |
691 | list_del_init(&lo->plh_bulk_destroy); | |
9fd4b9fc TM |
692 | if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) { |
693 | if (is_bulk_recall) | |
694 | set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); | |
fd9a8d71 | 695 | ret = -EAGAIN; |
9fd4b9fc | 696 | } |
fd9a8d71 TM |
697 | spin_unlock(&inode->i_lock); |
698 | pnfs_free_lseg_list(&lseg_list); | |
b20135d0 TM |
699 | /* Free all lsegs that are attached to commit buckets */ |
700 | nfs_commit_inode(inode, 0); | |
fd9a8d71 TM |
701 | pnfs_put_layout_hdr(lo); |
702 | iput(inode); | |
703 | } | |
704 | return ret; | |
705 | } | |
706 | ||
707 | int | |
708 | pnfs_destroy_layouts_byfsid(struct nfs_client *clp, | |
709 | struct nfs_fsid *fsid, | |
710 | bool is_recall) | |
711 | { | |
712 | struct nfs_server *server; | |
713 | LIST_HEAD(layout_list); | |
c47abcf8 | 714 | |
974cec8c | 715 | spin_lock(&clp->cl_lock); |
6382a441 | 716 | rcu_read_lock(); |
fd9a8d71 | 717 | restart: |
6382a441 | 718 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
fd9a8d71 TM |
719 | if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0) |
720 | continue; | |
721 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, | |
722 | server, | |
723 | &layout_list) != 0) | |
724 | goto restart; | |
6382a441 WAA |
725 | } |
726 | rcu_read_unlock(); | |
974cec8c AA |
727 | spin_unlock(&clp->cl_lock); |
728 | ||
fd9a8d71 TM |
729 | if (list_empty(&layout_list)) |
730 | return 0; | |
731 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); | |
732 | } | |
733 | ||
734 | int | |
735 | pnfs_destroy_layouts_byclid(struct nfs_client *clp, | |
736 | bool is_recall) | |
737 | { | |
738 | struct nfs_server *server; | |
739 | LIST_HEAD(layout_list); | |
740 | ||
741 | spin_lock(&clp->cl_lock); | |
742 | rcu_read_lock(); | |
743 | restart: | |
744 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | |
745 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, | |
746 | server, | |
747 | &layout_list) != 0) | |
748 | goto restart; | |
974cec8c | 749 | } |
fd9a8d71 TM |
750 | rcu_read_unlock(); |
751 | spin_unlock(&clp->cl_lock); | |
752 | ||
753 | if (list_empty(&layout_list)) | |
754 | return 0; | |
755 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); | |
756 | } | |
757 | ||
758 | /* | |
759 | * Called by the state manger to remove all layouts established under an | |
760 | * expired lease. | |
761 | */ | |
762 | void | |
763 | pnfs_destroy_all_layouts(struct nfs_client *clp) | |
764 | { | |
765 | nfs4_deviceid_mark_client_invalid(clp); | |
766 | nfs4_deviceid_purge_client(clp); | |
767 | ||
768 | pnfs_destroy_layouts_byclid(clp, false); | |
e5e94017 BH |
769 | } |
770 | ||
fd6002e9 | 771 | /* update lo->plh_stateid with new if is more recent */ |
43f1b3da FI |
772 | void |
773 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, | |
774 | bool update_barrier) | |
b1f69b75 | 775 | { |
ecebb80b TM |
776 | u32 oldseq, newseq, new_barrier = 0; |
777 | bool invalid = !pnfs_layout_is_valid(lo); | |
b1f69b75 | 778 | |
2d2f24ad TM |
779 | oldseq = be32_to_cpu(lo->plh_stateid.seqid); |
780 | newseq = be32_to_cpu(new->seqid); | |
ecebb80b | 781 | if (invalid || pnfs_seqid_is_newer(newseq, oldseq)) { |
f597c537 | 782 | nfs4_stateid_copy(&lo->plh_stateid, new); |
ecebb80b TM |
783 | /* |
784 | * Because of wraparound, we want to keep the barrier | |
785 | * "close" to the current seqids. | |
786 | */ | |
787 | new_barrier = newseq - atomic_read(&lo->plh_outstanding); | |
43f1b3da | 788 | } |
ecebb80b TM |
789 | if (update_barrier) |
790 | new_barrier = be32_to_cpu(new->seqid); | |
791 | else if (new_barrier == 0) | |
792 | return; | |
793 | if (invalid || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier)) | |
794 | lo->plh_barrier = new_barrier; | |
b1f69b75 AA |
795 | } |
796 | ||
cf7d63f1 | 797 | static bool |
19c54aba TM |
798 | pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo, |
799 | const nfs4_stateid *stateid) | |
43f1b3da | 800 | { |
19c54aba | 801 | u32 seqid = be32_to_cpu(stateid->seqid); |
25a1a621 | 802 | |
19c54aba TM |
803 | return !pnfs_seqid_is_newer(seqid, lo->plh_barrier); |
804 | } | |
805 | ||
806 | /* lget is set to 1 if called from inside send_layoutget call chain */ | |
807 | static bool | |
e1c06f80 | 808 | pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo) |
19c54aba | 809 | { |
f7e8917a | 810 | return lo->plh_block_lgets || |
e1c06f80 | 811 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
cf7d63f1 FI |
812 | } |
813 | ||
b1f69b75 | 814 | /* |
183d9e7b JL |
815 | * Get layout from server. |
816 | * for now, assume that whole file layouts are requested. | |
817 | * arg->offset: 0 | |
818 | * arg->length: all ones | |
819 | */ | |
e5e94017 BH |
820 | static struct pnfs_layout_segment * |
821 | send_layoutget(struct pnfs_layout_hdr *lo, | |
822 | struct nfs_open_context *ctx, | |
183d9e7b | 823 | nfs4_stateid *stateid, |
e144e539 | 824 | const struct pnfs_layout_range *range, |
183d9e7b | 825 | long *timeout, gfp_t gfp_flags) |
e5e94017 | 826 | { |
b7edfaa1 | 827 | struct inode *ino = lo->plh_inode; |
b1f69b75 AA |
828 | struct nfs_server *server = NFS_SERVER(ino); |
829 | struct nfs4_layoutget *lgp; | |
2d89a1d3 | 830 | loff_t i_size; |
b1f69b75 AA |
831 | |
832 | dprintk("--> %s\n", __func__); | |
e5e94017 | 833 | |
4f2e9dce JL |
834 | /* |
835 | * Synchronously retrieve layout information from server and | |
836 | * store in lseg. If we race with a concurrent seqid morphing | |
837 | * op, then re-send the LAYOUTGET. | |
838 | */ | |
83026d80 JL |
839 | lgp = kzalloc(sizeof(*lgp), gfp_flags); |
840 | if (lgp == NULL) | |
841 | return ERR_PTR(-ENOMEM); | |
842 | ||
843 | i_size = i_size_read(ino); | |
844 | ||
845 | lgp->args.minlength = PAGE_SIZE; | |
846 | if (lgp->args.minlength > range->length) | |
847 | lgp->args.minlength = range->length; | |
848 | if (range->iomode == IOMODE_READ) { | |
849 | if (range->offset >= i_size) | |
850 | lgp->args.minlength = 0; | |
851 | else if (i_size - range->offset < lgp->args.minlength) | |
852 | lgp->args.minlength = i_size - range->offset; | |
d03ab29d | 853 | } |
83026d80 JL |
854 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
855 | pnfs_copy_range(&lgp->args.range, range); | |
856 | lgp->args.type = server->pnfs_curr_ld->id; | |
857 | lgp->args.inode = ino; | |
858 | lgp->args.ctx = get_nfs_open_context(ctx); | |
183d9e7b | 859 | nfs4_stateid_copy(&lgp->args.stateid, stateid); |
83026d80 JL |
860 | lgp->gfp_flags = gfp_flags; |
861 | lgp->cred = lo->plh_lc_cred; | |
35124a09 | 862 | |
183d9e7b | 863 | return nfs4_proc_layoutget(lgp, timeout, gfp_flags); |
974cec8c AA |
864 | } |
865 | ||
24956804 TM |
866 | static void pnfs_clear_layoutcommit(struct inode *inode, |
867 | struct list_head *head) | |
868 | { | |
869 | struct nfs_inode *nfsi = NFS_I(inode); | |
870 | struct pnfs_layout_segment *lseg, *tmp; | |
871 | ||
872 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) | |
873 | return; | |
874 | list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) { | |
875 | if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) | |
876 | continue; | |
877 | pnfs_lseg_dec_and_remove_zero(lseg, head); | |
878 | } | |
879 | } | |
880 | ||
d67ae825 TH |
881 | void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo) |
882 | { | |
883 | clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags); | |
884 | smp_mb__after_atomic(); | |
885 | wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN); | |
7f27392c | 886 | rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq); |
d67ae825 TH |
887 | } |
888 | ||
8e0acf90 TM |
889 | static void |
890 | pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo) | |
891 | { | |
892 | lo->plh_return_iomode = 0; | |
893 | lo->plh_return_seq = 0; | |
894 | clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); | |
895 | } | |
896 | ||
13c13a6a | 897 | static bool |
e5fd1904 TM |
898 | pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo, |
899 | nfs4_stateid *stateid, | |
900 | enum pnfs_iomode *iomode) | |
13c13a6a | 901 | { |
bf0291dd TM |
902 | /* Serialise LAYOUTGET/LAYOUTRETURN */ |
903 | if (atomic_read(&lo->plh_outstanding) != 0) | |
904 | return false; | |
13c13a6a TM |
905 | if (test_and_set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) |
906 | return false; | |
13c13a6a | 907 | pnfs_get_layout_hdr(lo); |
e5fd1904 TM |
908 | if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) { |
909 | if (stateid != NULL) { | |
910 | nfs4_stateid_copy(stateid, &lo->plh_stateid); | |
911 | if (lo->plh_return_seq != 0) | |
912 | stateid->seqid = cpu_to_be32(lo->plh_return_seq); | |
913 | } | |
914 | if (iomode != NULL) | |
915 | *iomode = lo->plh_return_iomode; | |
916 | pnfs_clear_layoutreturn_info(lo); | |
917 | return true; | |
918 | } | |
919 | if (stateid != NULL) | |
920 | nfs4_stateid_copy(stateid, &lo->plh_stateid); | |
921 | if (iomode != NULL) | |
922 | *iomode = IOMODE_ANY; | |
13c13a6a TM |
923 | return true; |
924 | } | |
925 | ||
f40eb5d0 | 926 | static int |
ed429d6b | 927 | pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid, |
6c16605d | 928 | enum pnfs_iomode iomode, bool sync) |
f40eb5d0 PT |
929 | { |
930 | struct inode *ino = lo->plh_inode; | |
931 | struct nfs4_layoutreturn *lrp; | |
932 | int status = 0; | |
933 | ||
e4af440a | 934 | lrp = kzalloc(sizeof(*lrp), GFP_NOFS); |
f40eb5d0 PT |
935 | if (unlikely(lrp == NULL)) { |
936 | status = -ENOMEM; | |
937 | spin_lock(&ino->i_lock); | |
d67ae825 | 938 | pnfs_clear_layoutreturn_waitbit(lo); |
f40eb5d0 PT |
939 | spin_unlock(&ino->i_lock); |
940 | pnfs_put_layout_hdr(lo); | |
941 | goto out; | |
942 | } | |
943 | ||
ed429d6b | 944 | nfs4_stateid_copy(&lrp->args.stateid, stateid); |
f40eb5d0 PT |
945 | lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id; |
946 | lrp->args.inode = ino; | |
15eb67c1 PT |
947 | lrp->args.range.iomode = iomode; |
948 | lrp->args.range.offset = 0; | |
949 | lrp->args.range.length = NFS4_MAX_UINT64; | |
f40eb5d0 PT |
950 | lrp->args.layout = lo; |
951 | lrp->clp = NFS_SERVER(ino)->nfs_client; | |
952 | lrp->cred = lo->plh_lc_cred; | |
953 | ||
6c16605d | 954 | status = nfs4_proc_layoutreturn(lrp, sync); |
f40eb5d0 PT |
955 | out: |
956 | dprintk("<-- %s status: %d\n", __func__, status); | |
957 | return status; | |
958 | } | |
959 | ||
13c13a6a TM |
960 | /* Return true if layoutreturn is needed */ |
961 | static bool | |
962 | pnfs_layout_need_return(struct pnfs_layout_hdr *lo) | |
963 | { | |
964 | struct pnfs_layout_segment *s; | |
965 | ||
2370abda | 966 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
13c13a6a TM |
967 | return false; |
968 | ||
969 | /* Defer layoutreturn until all lsegs are done */ | |
970 | list_for_each_entry(s, &lo->plh_segs, pls_list) { | |
971 | if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags)) | |
972 | return false; | |
973 | } | |
974 | ||
975 | return true; | |
976 | } | |
977 | ||
978 | static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo) | |
979 | { | |
980 | struct inode *inode= lo->plh_inode; | |
981 | ||
2370abda | 982 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
13c13a6a TM |
983 | return; |
984 | spin_lock(&inode->i_lock); | |
985 | if (pnfs_layout_need_return(lo)) { | |
986 | nfs4_stateid stateid; | |
987 | enum pnfs_iomode iomode; | |
988 | bool send; | |
989 | ||
e5fd1904 | 990 | send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode); |
13c13a6a TM |
991 | spin_unlock(&inode->i_lock); |
992 | if (send) { | |
993 | /* Send an async layoutreturn so we dont deadlock */ | |
994 | pnfs_send_layoutreturn(lo, &stateid, iomode, false); | |
995 | } | |
996 | } else | |
997 | spin_unlock(&inode->i_lock); | |
998 | } | |
999 | ||
293b3b06 AA |
1000 | /* |
1001 | * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr | |
1002 | * when the layout segment list is empty. | |
1003 | * | |
1004 | * Note that a pnfs_layout_hdr can exist with an empty layout segment | |
1005 | * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the | |
1006 | * deviceid is marked invalid. | |
1007 | */ | |
cbe82603 BH |
1008 | int |
1009 | _pnfs_return_layout(struct inode *ino) | |
1010 | { | |
1011 | struct pnfs_layout_hdr *lo = NULL; | |
1012 | struct nfs_inode *nfsi = NFS_I(ino); | |
1013 | LIST_HEAD(tmp_list); | |
cbe82603 | 1014 | nfs4_stateid stateid; |
293b3b06 | 1015 | int status = 0, empty; |
7f27392c | 1016 | bool send; |
cbe82603 | 1017 | |
366d5052 | 1018 | dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino); |
cbe82603 BH |
1019 | |
1020 | spin_lock(&ino->i_lock); | |
1021 | lo = nfsi->layout; | |
e5929f3c | 1022 | if (!lo) { |
cbe82603 | 1023 | spin_unlock(&ino->i_lock); |
293b3b06 AA |
1024 | dprintk("NFS: %s no layout to return\n", __func__); |
1025 | goto out; | |
cbe82603 | 1026 | } |
cbe82603 | 1027 | /* Reference matched in nfs4_layoutreturn_release */ |
70c3bd2b | 1028 | pnfs_get_layout_hdr(lo); |
293b3b06 | 1029 | empty = list_empty(&lo->plh_segs); |
24956804 | 1030 | pnfs_clear_layoutcommit(ino, &tmp_list); |
6d597e17 | 1031 | pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0); |
c88953d8 CH |
1032 | |
1033 | if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) { | |
1034 | struct pnfs_layout_range range = { | |
1035 | .iomode = IOMODE_ANY, | |
1036 | .offset = 0, | |
1037 | .length = NFS4_MAX_UINT64, | |
1038 | }; | |
1039 | NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range); | |
1040 | } | |
1041 | ||
293b3b06 AA |
1042 | /* Don't send a LAYOUTRETURN if list was initially empty */ |
1043 | if (empty) { | |
1044 | spin_unlock(&ino->i_lock); | |
293b3b06 | 1045 | dprintk("NFS: %s no layout segments to return\n", __func__); |
7f27392c | 1046 | goto out_put_layout_hdr; |
293b3b06 | 1047 | } |
47abadef | 1048 | |
e5fd1904 | 1049 | send = pnfs_prepare_layoutreturn(lo, &stateid, NULL); |
cbe82603 BH |
1050 | spin_unlock(&ino->i_lock); |
1051 | pnfs_free_lseg_list(&tmp_list); | |
7f27392c | 1052 | if (send) |
ed429d6b | 1053 | status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true); |
7f27392c TM |
1054 | out_put_layout_hdr: |
1055 | pnfs_put_layout_hdr(lo); | |
cbe82603 BH |
1056 | out: |
1057 | dprintk("<-- %s status: %d\n", __func__, status); | |
1058 | return status; | |
1059 | } | |
0a57cdac | 1060 | EXPORT_SYMBOL_GPL(_pnfs_return_layout); |
cbe82603 | 1061 | |
24028672 TM |
1062 | int |
1063 | pnfs_commit_and_return_layout(struct inode *inode) | |
1064 | { | |
1065 | struct pnfs_layout_hdr *lo; | |
1066 | int ret; | |
1067 | ||
1068 | spin_lock(&inode->i_lock); | |
1069 | lo = NFS_I(inode)->layout; | |
1070 | if (lo == NULL) { | |
1071 | spin_unlock(&inode->i_lock); | |
1072 | return 0; | |
1073 | } | |
1074 | pnfs_get_layout_hdr(lo); | |
1075 | /* Block new layoutgets and read/write to ds */ | |
1076 | lo->plh_block_lgets++; | |
1077 | spin_unlock(&inode->i_lock); | |
1078 | filemap_fdatawait(inode->i_mapping); | |
1079 | ret = pnfs_layoutcommit_inode(inode, true); | |
1080 | if (ret == 0) | |
1081 | ret = _pnfs_return_layout(inode); | |
1082 | spin_lock(&inode->i_lock); | |
1083 | lo->plh_block_lgets--; | |
1084 | spin_unlock(&inode->i_lock); | |
1085 | pnfs_put_layout_hdr(lo); | |
1086 | return ret; | |
1087 | } | |
1088 | ||
f7e8917a FI |
1089 | bool pnfs_roc(struct inode *ino) |
1090 | { | |
40dd4b7a TM |
1091 | struct nfs_inode *nfsi = NFS_I(ino); |
1092 | struct nfs_open_context *ctx; | |
1093 | struct nfs4_state *state; | |
f7e8917a FI |
1094 | struct pnfs_layout_hdr *lo; |
1095 | struct pnfs_layout_segment *lseg, *tmp; | |
193e3aa2 | 1096 | nfs4_stateid stateid; |
f7e8917a | 1097 | LIST_HEAD(tmp_list); |
e755d638 | 1098 | bool found = false, layoutreturn = false, roc = false; |
f7e8917a FI |
1099 | |
1100 | spin_lock(&ino->i_lock); | |
40dd4b7a | 1101 | lo = nfsi->layout; |
3976143b | 1102 | if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) |
40dd4b7a TM |
1103 | goto out_noroc; |
1104 | ||
e755d638 | 1105 | /* no roc if we hold a delegation */ |
40dd4b7a TM |
1106 | if (nfs4_check_delegation(ino, FMODE_READ)) |
1107 | goto out_noroc; | |
1108 | ||
1109 | list_for_each_entry(ctx, &nfsi->open_files, list) { | |
1110 | state = ctx->state; | |
1111 | /* Don't return layout if there is open file state */ | |
1112 | if (state != NULL && state->state != 0) | |
1113 | goto out_noroc; | |
1114 | } | |
1115 | ||
e755d638 | 1116 | /* always send layoutreturn if being marked so */ |
e5fd1904 TM |
1117 | if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
1118 | layoutreturn = pnfs_prepare_layoutreturn(lo, | |
1119 | &stateid, NULL); | |
e755d638 | 1120 | |
f7e8917a | 1121 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) |
e755d638 PT |
1122 | /* If we are sending layoutreturn, invalidate all valid lsegs */ |
1123 | if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { | |
f7e8917a FI |
1124 | mark_lseg_invalid(lseg, &tmp_list); |
1125 | found = true; | |
1126 | } | |
500d701f | 1127 | /* ROC in two conditions: |
e755d638 PT |
1128 | * 1. there are ROC lsegs |
1129 | * 2. we don't send layoutreturn | |
e755d638 | 1130 | */ |
500d701f PT |
1131 | if (found && !layoutreturn) { |
1132 | /* lo ref dropped in pnfs_roc_release() */ | |
1133 | pnfs_get_layout_hdr(lo); | |
e755d638 | 1134 | roc = true; |
500d701f | 1135 | } |
f7e8917a | 1136 | |
40dd4b7a | 1137 | out_noroc: |
f7e8917a | 1138 | spin_unlock(&ino->i_lock); |
e755d638 PT |
1139 | pnfs_free_lseg_list(&tmp_list); |
1140 | pnfs_layoutcommit_inode(ino, true); | |
1141 | if (layoutreturn) | |
ed429d6b | 1142 | pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true); |
e755d638 | 1143 | return roc; |
f7e8917a FI |
1144 | } |
1145 | ||
1146 | void pnfs_roc_release(struct inode *ino) | |
1147 | { | |
1148 | struct pnfs_layout_hdr *lo; | |
1149 | ||
1150 | spin_lock(&ino->i_lock); | |
1151 | lo = NFS_I(ino)->layout; | |
5c4a79fb | 1152 | pnfs_clear_layoutreturn_waitbit(lo); |
6622c3ea TM |
1153 | if (atomic_dec_and_test(&lo->plh_refcount)) { |
1154 | pnfs_detach_layout_hdr(lo); | |
1155 | spin_unlock(&ino->i_lock); | |
1156 | pnfs_free_layout_hdr(lo); | |
1157 | } else | |
1158 | spin_unlock(&ino->i_lock); | |
f7e8917a FI |
1159 | } |
1160 | ||
1161 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) | |
1162 | { | |
1163 | struct pnfs_layout_hdr *lo; | |
1164 | ||
1165 | spin_lock(&ino->i_lock); | |
1166 | lo = NFS_I(ino)->layout; | |
0f35ad6f | 1167 | if (pnfs_seqid_is_newer(barrier, lo->plh_barrier)) |
f7e8917a FI |
1168 | lo->plh_barrier = barrier; |
1169 | spin_unlock(&ino->i_lock); | |
6a463beb | 1170 | trace_nfs4_layoutreturn_on_close(ino, 0); |
f7e8917a FI |
1171 | } |
1172 | ||
4ff376fe | 1173 | void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier) |
f7e8917a FI |
1174 | { |
1175 | struct nfs_inode *nfsi = NFS_I(ino); | |
7fdab069 | 1176 | struct pnfs_layout_hdr *lo; |
7fdab069 | 1177 | u32 current_seqid; |
f7e8917a FI |
1178 | |
1179 | spin_lock(&ino->i_lock); | |
7fdab069 TM |
1180 | lo = nfsi->layout; |
1181 | current_seqid = be32_to_cpu(lo->plh_stateid.seqid); | |
f7e8917a | 1182 | |
7fdab069 TM |
1183 | /* Since close does not return a layout stateid for use as |
1184 | * a barrier, we choose the worst-case barrier. | |
1185 | */ | |
1186 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); | |
f7e8917a | 1187 | spin_unlock(&ino->i_lock); |
f7e8917a FI |
1188 | } |
1189 | ||
500d701f PT |
1190 | bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task) |
1191 | { | |
1192 | struct nfs_inode *nfsi = NFS_I(ino); | |
1193 | struct pnfs_layout_hdr *lo; | |
1194 | bool sleep = false; | |
1195 | ||
1196 | /* we might not have grabbed lo reference. so need to check under | |
1197 | * i_lock */ | |
1198 | spin_lock(&ino->i_lock); | |
1199 | lo = nfsi->layout; | |
1200 | if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) | |
1201 | sleep = true; | |
1202 | spin_unlock(&ino->i_lock); | |
1203 | ||
1204 | if (sleep) | |
1205 | rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL); | |
1206 | ||
1207 | return sleep; | |
1208 | } | |
1209 | ||
b1f69b75 AA |
1210 | /* |
1211 | * Compare two layout segments for sorting into layout cache. | |
1212 | * We want to preferentially return RW over RO layouts, so ensure those | |
1213 | * are seen first. | |
1214 | */ | |
1215 | static s64 | |
7dc0ac70 | 1216 | pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1, |
3cb2df17 | 1217 | const struct pnfs_layout_range *l2) |
b1f69b75 | 1218 | { |
fb3296eb BH |
1219 | s64 d; |
1220 | ||
1221 | /* high offset > low offset */ | |
1222 | d = l1->offset - l2->offset; | |
1223 | if (d) | |
1224 | return d; | |
1225 | ||
1226 | /* short length > long length */ | |
1227 | d = l2->length - l1->length; | |
1228 | if (d) | |
1229 | return d; | |
1230 | ||
b1f69b75 | 1231 | /* read > read/write */ |
fb3296eb | 1232 | return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ); |
b1f69b75 AA |
1233 | } |
1234 | ||
03772d2f TM |
1235 | static bool |
1236 | pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1, | |
1237 | const struct pnfs_layout_range *l2) | |
1238 | { | |
1239 | return pnfs_lseg_range_cmp(l1, l2) > 0; | |
1240 | } | |
1241 | ||
1242 | static bool | |
1243 | pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg, | |
1244 | struct pnfs_layout_segment *old) | |
1245 | { | |
1246 | return false; | |
1247 | } | |
1248 | ||
1249 | void | |
1250 | pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo, | |
1251 | struct pnfs_layout_segment *lseg, | |
1252 | bool (*is_after)(const struct pnfs_layout_range *, | |
1253 | const struct pnfs_layout_range *), | |
1254 | bool (*do_merge)(struct pnfs_layout_segment *, | |
1255 | struct pnfs_layout_segment *), | |
1256 | struct list_head *free_me) | |
974cec8c | 1257 | { |
03772d2f | 1258 | struct pnfs_layout_segment *lp, *tmp; |
b1f69b75 | 1259 | |
974cec8c AA |
1260 | dprintk("%s:Begin\n", __func__); |
1261 | ||
03772d2f TM |
1262 | list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) { |
1263 | if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0) | |
1264 | continue; | |
1265 | if (do_merge(lseg, lp)) { | |
1266 | mark_lseg_invalid(lp, free_me); | |
1267 | continue; | |
1268 | } | |
1269 | if (is_after(&lseg->pls_range, &lp->pls_range)) | |
b1f69b75 | 1270 | continue; |
566052c5 | 1271 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
b1f69b75 AA |
1272 | dprintk("%s: inserted lseg %p " |
1273 | "iomode %d offset %llu length %llu before " | |
1274 | "lp %p iomode %d offset %llu length %llu\n", | |
566052c5 FI |
1275 | __func__, lseg, lseg->pls_range.iomode, |
1276 | lseg->pls_range.offset, lseg->pls_range.length, | |
1277 | lp, lp->pls_range.iomode, lp->pls_range.offset, | |
1278 | lp->pls_range.length); | |
fb3296eb | 1279 | goto out; |
974cec8c | 1280 | } |
fb3296eb BH |
1281 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
1282 | dprintk("%s: inserted lseg %p " | |
1283 | "iomode %d offset %llu length %llu at tail\n", | |
1284 | __func__, lseg, lseg->pls_range.iomode, | |
1285 | lseg->pls_range.offset, lseg->pls_range.length); | |
1286 | out: | |
70c3bd2b | 1287 | pnfs_get_layout_hdr(lo); |
974cec8c AA |
1288 | |
1289 | dprintk("%s:Return\n", __func__); | |
e5e94017 | 1290 | } |
03772d2f TM |
1291 | EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg); |
1292 | ||
1293 | static void | |
1294 | pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo, | |
1295 | struct pnfs_layout_segment *lseg, | |
1296 | struct list_head *free_me) | |
1297 | { | |
1298 | struct inode *inode = lo->plh_inode; | |
1299 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; | |
1300 | ||
1301 | if (ld->add_lseg != NULL) | |
1302 | ld->add_lseg(lo, lseg, free_me); | |
1303 | else | |
1304 | pnfs_generic_layout_insert_lseg(lo, lseg, | |
1305 | pnfs_lseg_range_is_after, | |
1306 | pnfs_lseg_no_merge, | |
1307 | free_me); | |
1308 | } | |
e5e94017 BH |
1309 | |
1310 | static struct pnfs_layout_hdr * | |
9fa40758 PT |
1311 | alloc_init_layout_hdr(struct inode *ino, |
1312 | struct nfs_open_context *ctx, | |
1313 | gfp_t gfp_flags) | |
e5e94017 BH |
1314 | { |
1315 | struct pnfs_layout_hdr *lo; | |
1316 | ||
636fb9c8 | 1317 | lo = pnfs_alloc_layout_hdr(ino, gfp_flags); |
e5e94017 BH |
1318 | if (!lo) |
1319 | return NULL; | |
cc6e5340 | 1320 | atomic_set(&lo->plh_refcount, 1); |
b7edfaa1 FI |
1321 | INIT_LIST_HEAD(&lo->plh_layouts); |
1322 | INIT_LIST_HEAD(&lo->plh_segs); | |
fd9a8d71 | 1323 | INIT_LIST_HEAD(&lo->plh_bulk_destroy); |
b7edfaa1 | 1324 | lo->plh_inode = ino; |
5cc2216d | 1325 | lo->plh_lc_cred = get_rpccred(ctx->cred); |
67a3b721 | 1326 | lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID; |
e5e94017 BH |
1327 | return lo; |
1328 | } | |
1329 | ||
1330 | static struct pnfs_layout_hdr * | |
9fa40758 PT |
1331 | pnfs_find_alloc_layout(struct inode *ino, |
1332 | struct nfs_open_context *ctx, | |
1333 | gfp_t gfp_flags) | |
e5241e43 TM |
1334 | __releases(&ino->i_lock) |
1335 | __acquires(&ino->i_lock) | |
e5e94017 BH |
1336 | { |
1337 | struct nfs_inode *nfsi = NFS_I(ino); | |
1338 | struct pnfs_layout_hdr *new = NULL; | |
1339 | ||
1340 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); | |
1341 | ||
251ec410 TM |
1342 | if (nfsi->layout != NULL) |
1343 | goto out_existing; | |
e5e94017 | 1344 | spin_unlock(&ino->i_lock); |
9fa40758 | 1345 | new = alloc_init_layout_hdr(ino, ctx, gfp_flags); |
e5e94017 BH |
1346 | spin_lock(&ino->i_lock); |
1347 | ||
251ec410 | 1348 | if (likely(nfsi->layout == NULL)) { /* Won the race? */ |
e5e94017 | 1349 | nfsi->layout = new; |
251ec410 | 1350 | return new; |
7175fe90 YN |
1351 | } else if (new != NULL) |
1352 | pnfs_free_layout_hdr(new); | |
251ec410 TM |
1353 | out_existing: |
1354 | pnfs_get_layout_hdr(nfsi->layout); | |
e5e94017 BH |
1355 | return nfsi->layout; |
1356 | } | |
1357 | ||
b1f69b75 AA |
1358 | /* |
1359 | * iomode matching rules: | |
c7d73af2 TH |
1360 | * iomode lseg strict match |
1361 | * iomode | |
1362 | * ----- ----- ------ ----- | |
1363 | * ANY READ N/A true | |
1364 | * ANY RW N/A true | |
1365 | * RW READ N/A false | |
1366 | * RW RW N/A true | |
1367 | * READ READ N/A true | |
1368 | * READ RW true false | |
1369 | * READ RW false true | |
b1f69b75 | 1370 | */ |
3cb2df17 | 1371 | static bool |
7dc0ac70 | 1372 | pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range, |
c7d73af2 TH |
1373 | const struct pnfs_layout_range *range, |
1374 | bool strict_iomode) | |
b1f69b75 | 1375 | { |
fb3296eb BH |
1376 | struct pnfs_layout_range range1; |
1377 | ||
1378 | if ((range->iomode == IOMODE_RW && | |
1379 | ls_range->iomode != IOMODE_RW) || | |
c7d73af2 TH |
1380 | (range->iomode != ls_range->iomode && |
1381 | strict_iomode == true) || | |
7dc0ac70 | 1382 | !pnfs_lseg_range_intersecting(ls_range, range)) |
fb3296eb BH |
1383 | return 0; |
1384 | ||
1385 | /* range1 covers only the first byte in the range */ | |
1386 | range1 = *range; | |
1387 | range1.length = 1; | |
7dc0ac70 | 1388 | return pnfs_lseg_range_contained(ls_range, &range1); |
b1f69b75 AA |
1389 | } |
1390 | ||
1391 | /* | |
1392 | * lookup range in layout | |
1393 | */ | |
e5e94017 | 1394 | static struct pnfs_layout_segment * |
fb3296eb | 1395 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, |
c7d73af2 TH |
1396 | struct pnfs_layout_range *range, |
1397 | bool strict_iomode) | |
e5e94017 | 1398 | { |
b1f69b75 AA |
1399 | struct pnfs_layout_segment *lseg, *ret = NULL; |
1400 | ||
1401 | dprintk("%s:Begin\n", __func__); | |
1402 | ||
b7edfaa1 | 1403 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
4541d16c | 1404 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
ce6ab4f2 | 1405 | !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) && |
c7d73af2 TH |
1406 | pnfs_lseg_range_match(&lseg->pls_range, range, |
1407 | strict_iomode)) { | |
9369a431 | 1408 | ret = pnfs_get_lseg(lseg); |
b1f69b75 AA |
1409 | break; |
1410 | } | |
b1f69b75 AA |
1411 | } |
1412 | ||
1413 | dprintk("%s:Return lseg %p ref %d\n", | |
4541d16c | 1414 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
b1f69b75 | 1415 | return ret; |
e5e94017 BH |
1416 | } |
1417 | ||
d23d61c8 AA |
1418 | /* |
1419 | * Use mdsthreshold hints set at each OPEN to determine if I/O should go | |
1420 | * to the MDS or over pNFS | |
1421 | * | |
1422 | * The nfs_inode read_io and write_io fields are cumulative counters reset | |
1423 | * when there are no layout segments. Note that in pnfs_update_layout iomode | |
1424 | * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a | |
1425 | * WRITE request. | |
1426 | * | |
1427 | * A return of true means use MDS I/O. | |
1428 | * | |
1429 | * From rfc 5661: | |
1430 | * If a file's size is smaller than the file size threshold, data accesses | |
1431 | * SHOULD be sent to the metadata server. If an I/O request has a length that | |
1432 | * is below the I/O size threshold, the I/O SHOULD be sent to the metadata | |
1433 | * server. If both file size and I/O size are provided, the client SHOULD | |
1434 | * reach or exceed both thresholds before sending its read or write | |
1435 | * requests to the data server. | |
1436 | */ | |
1437 | static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx, | |
1438 | struct inode *ino, int iomode) | |
1439 | { | |
1440 | struct nfs4_threshold *t = ctx->mdsthreshold; | |
1441 | struct nfs_inode *nfsi = NFS_I(ino); | |
1442 | loff_t fsize = i_size_read(ino); | |
1443 | bool size = false, size_set = false, io = false, io_set = false, ret = false; | |
1444 | ||
1445 | if (t == NULL) | |
1446 | return ret; | |
1447 | ||
1448 | dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n", | |
1449 | __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz); | |
1450 | ||
1451 | switch (iomode) { | |
1452 | case IOMODE_READ: | |
1453 | if (t->bm & THRESHOLD_RD) { | |
1454 | dprintk("%s fsize %llu\n", __func__, fsize); | |
1455 | size_set = true; | |
1456 | if (fsize < t->rd_sz) | |
1457 | size = true; | |
1458 | } | |
1459 | if (t->bm & THRESHOLD_RD_IO) { | |
1460 | dprintk("%s nfsi->read_io %llu\n", __func__, | |
1461 | nfsi->read_io); | |
1462 | io_set = true; | |
1463 | if (nfsi->read_io < t->rd_io_sz) | |
1464 | io = true; | |
1465 | } | |
1466 | break; | |
1467 | case IOMODE_RW: | |
1468 | if (t->bm & THRESHOLD_WR) { | |
1469 | dprintk("%s fsize %llu\n", __func__, fsize); | |
1470 | size_set = true; | |
1471 | if (fsize < t->wr_sz) | |
1472 | size = true; | |
1473 | } | |
1474 | if (t->bm & THRESHOLD_WR_IO) { | |
1475 | dprintk("%s nfsi->write_io %llu\n", __func__, | |
1476 | nfsi->write_io); | |
1477 | io_set = true; | |
1478 | if (nfsi->write_io < t->wr_io_sz) | |
1479 | io = true; | |
1480 | } | |
1481 | break; | |
1482 | } | |
1483 | if (size_set && io_set) { | |
1484 | if (size && io) | |
1485 | ret = true; | |
1486 | } else if (size || io) | |
1487 | ret = true; | |
1488 | ||
1489 | dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret); | |
1490 | return ret; | |
1491 | } | |
1492 | ||
aa8a45ee PT |
1493 | static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo) |
1494 | { | |
1495 | /* | |
1496 | * send layoutcommit as it can hold up layoutreturn due to lseg | |
1497 | * reference | |
1498 | */ | |
1499 | pnfs_layoutcommit_inode(lo->plh_inode, false); | |
1500 | return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN, | |
2e5b29f0 | 1501 | nfs_wait_bit_killable, |
aa8a45ee PT |
1502 | TASK_UNINTERRUPTIBLE); |
1503 | } | |
1504 | ||
d67ae825 TH |
1505 | static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo) |
1506 | { | |
1507 | unsigned long *bitlock = &lo->plh_flags; | |
1508 | ||
1509 | clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock); | |
1510 | smp_mb__after_atomic(); | |
1511 | wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET); | |
1512 | } | |
1513 | ||
e5e94017 BH |
1514 | /* |
1515 | * Layout segment is retreived from the server if not cached. | |
1516 | * The appropriate layout segment is referenced and returned to the caller. | |
1517 | */ | |
7c24d948 | 1518 | struct pnfs_layout_segment * |
e5e94017 BH |
1519 | pnfs_update_layout(struct inode *ino, |
1520 | struct nfs_open_context *ctx, | |
fb3296eb BH |
1521 | loff_t pos, |
1522 | u64 count, | |
a75b9df9 | 1523 | enum pnfs_iomode iomode, |
c7d73af2 | 1524 | bool strict_iomode, |
a75b9df9 | 1525 | gfp_t gfp_flags) |
e5e94017 | 1526 | { |
fb3296eb BH |
1527 | struct pnfs_layout_range arg = { |
1528 | .iomode = iomode, | |
1529 | .offset = pos, | |
1530 | .length = count, | |
1531 | }; | |
183d9e7b | 1532 | unsigned pg_offset, seq; |
6382a441 WAA |
1533 | struct nfs_server *server = NFS_SERVER(ino); |
1534 | struct nfs_client *clp = server->nfs_client; | |
183d9e7b | 1535 | struct pnfs_layout_hdr *lo = NULL; |
e5e94017 | 1536 | struct pnfs_layout_segment *lseg = NULL; |
183d9e7b JL |
1537 | nfs4_stateid stateid; |
1538 | long timeout = 0; | |
66b53f32 | 1539 | unsigned long giveup = jiffies + (clp->cl_lease_time << 1); |
30005121 | 1540 | bool first; |
e5e94017 | 1541 | |
9a4bf31d | 1542 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) { |
183d9e7b | 1543 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1544 | PNFS_UPDATE_LAYOUT_NO_PNFS); |
f86bbcf8 | 1545 | goto out; |
9a4bf31d | 1546 | } |
d23d61c8 | 1547 | |
9a4bf31d | 1548 | if (iomode == IOMODE_READ && i_size_read(ino) == 0) { |
183d9e7b | 1549 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1550 | PNFS_UPDATE_LAYOUT_RD_ZEROLEN); |
4ae93560 | 1551 | goto out; |
9a4bf31d | 1552 | } |
4ae93560 | 1553 | |
9a4bf31d | 1554 | if (pnfs_within_mdsthreshold(ctx, ino, iomode)) { |
183d9e7b | 1555 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1556 | PNFS_UPDATE_LAYOUT_MDSTHRESH); |
f86bbcf8 | 1557 | goto out; |
9a4bf31d | 1558 | } |
d23d61c8 | 1559 | |
9bf87482 | 1560 | lookup_again: |
b88fa69e | 1561 | nfs4_client_recover_expired_lease(clp); |
9bf87482 | 1562 | first = false; |
e5e94017 | 1563 | spin_lock(&ino->i_lock); |
9fa40758 | 1564 | lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags); |
830ffb56 TM |
1565 | if (lo == NULL) { |
1566 | spin_unlock(&ino->i_lock); | |
183d9e7b | 1567 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1568 | PNFS_UPDATE_LAYOUT_NOMEM); |
830ffb56 TM |
1569 | goto out; |
1570 | } | |
e5e94017 | 1571 | |
43f1b3da | 1572 | /* Do we even need to bother with this? */ |
a59c30ac | 1573 | if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
183d9e7b | 1574 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1575 | PNFS_UPDATE_LAYOUT_BULK_RECALL); |
43f1b3da | 1576 | dprintk("%s matches recall, use MDS\n", __func__); |
e5e94017 BH |
1577 | goto out_unlock; |
1578 | } | |
1579 | ||
1580 | /* if LAYOUTGET already failed once we don't try again */ | |
2e5b29f0 | 1581 | if (pnfs_layout_io_test_failed(lo, iomode)) { |
183d9e7b | 1582 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1583 | PNFS_UPDATE_LAYOUT_IO_TEST_FAIL); |
e5e94017 | 1584 | goto out_unlock; |
9a4bf31d | 1585 | } |
e5e94017 | 1586 | |
c7d73af2 | 1587 | lseg = pnfs_find_lseg(lo, &arg, strict_iomode); |
183d9e7b JL |
1588 | if (lseg) { |
1589 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, | |
1590 | PNFS_UPDATE_LAYOUT_FOUND_CACHED); | |
1591 | goto out_unlock; | |
1592 | } | |
1593 | ||
1594 | if (!nfs4_valid_open_stateid(ctx->state)) { | |
1595 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, | |
1596 | PNFS_UPDATE_LAYOUT_INVALID_OPEN); | |
1597 | goto out_unlock; | |
1598 | } | |
1599 | ||
1600 | /* | |
1601 | * Choose a stateid for the LAYOUTGET. If we don't have a layout | |
1602 | * stateid, or it has been invalidated, then we must use the open | |
1603 | * stateid. | |
1604 | */ | |
67a3b721 | 1605 | if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) { |
183d9e7b JL |
1606 | |
1607 | /* | |
1608 | * The first layoutget for the file. Need to serialize per | |
9bf87482 PT |
1609 | * RFC 5661 Errata 3208. |
1610 | */ | |
1611 | if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, | |
1612 | &lo->plh_flags)) { | |
1613 | spin_unlock(&ino->i_lock); | |
1614 | wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET, | |
1615 | TASK_UNINTERRUPTIBLE); | |
1616 | pnfs_put_layout_hdr(lo); | |
183d9e7b | 1617 | dprintk("%s retrying\n", __func__); |
9bf87482 PT |
1618 | goto lookup_again; |
1619 | } | |
183d9e7b JL |
1620 | |
1621 | first = true; | |
1622 | do { | |
1623 | seq = read_seqbegin(&ctx->state->seqlock); | |
1624 | nfs4_stateid_copy(&stateid, &ctx->state->stateid); | |
1625 | } while (read_seqretry(&ctx->state->seqlock, seq)); | |
9bf87482 | 1626 | } else { |
183d9e7b | 1627 | nfs4_stateid_copy(&stateid, &lo->plh_stateid); |
9bf87482 | 1628 | } |
568e8c49 | 1629 | |
aa8a45ee PT |
1630 | /* |
1631 | * Because we free lsegs before sending LAYOUTRETURN, we need to wait | |
1632 | * for LAYOUTRETURN even if first is true. | |
1633 | */ | |
8f70f53a | 1634 | if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { |
aa8a45ee PT |
1635 | spin_unlock(&ino->i_lock); |
1636 | dprintk("%s wait for layoutreturn\n", __func__); | |
1637 | if (pnfs_prepare_to_retry_layoutget(lo)) { | |
d67ae825 TH |
1638 | if (first) |
1639 | pnfs_clear_first_layoutget(lo); | |
aa8a45ee PT |
1640 | pnfs_put_layout_hdr(lo); |
1641 | dprintk("%s retrying\n", __func__); | |
183d9e7b JL |
1642 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, |
1643 | lseg, PNFS_UPDATE_LAYOUT_RETRY); | |
aa8a45ee PT |
1644 | goto lookup_again; |
1645 | } | |
183d9e7b | 1646 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1647 | PNFS_UPDATE_LAYOUT_RETURN); |
aa8a45ee PT |
1648 | goto out_put_layout_hdr; |
1649 | } | |
1650 | ||
9a4bf31d | 1651 | if (pnfs_layoutgets_blocked(lo)) { |
183d9e7b | 1652 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1653 | PNFS_UPDATE_LAYOUT_BLOCKED); |
cf7d63f1 | 1654 | goto out_unlock; |
9a4bf31d | 1655 | } |
cf7d63f1 | 1656 | atomic_inc(&lo->plh_outstanding); |
f49f9baa | 1657 | spin_unlock(&ino->i_lock); |
30005121 | 1658 | |
abb9a007 | 1659 | if (list_empty(&lo->plh_layouts)) { |
2130ff66 FI |
1660 | /* The lo must be on the clp list if there is any |
1661 | * chance of a CB_LAYOUTRECALL(FILE) coming in. | |
1662 | */ | |
1663 | spin_lock(&clp->cl_lock); | |
abb9a007 PT |
1664 | if (list_empty(&lo->plh_layouts)) |
1665 | list_add_tail(&lo->plh_layouts, &server->layouts); | |
2130ff66 FI |
1666 | spin_unlock(&clp->cl_lock); |
1667 | } | |
e5e94017 | 1668 | |
09cbfeaf | 1669 | pg_offset = arg.offset & ~PAGE_MASK; |
707ed5fd BH |
1670 | if (pg_offset) { |
1671 | arg.offset -= pg_offset; | |
1672 | arg.length += pg_offset; | |
1673 | } | |
7c24d948 | 1674 | if (arg.length != NFS4_MAX_UINT64) |
09cbfeaf | 1675 | arg.length = PAGE_ALIGN(arg.length); |
707ed5fd | 1676 | |
183d9e7b JL |
1677 | lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags); |
1678 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, | |
1679 | PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET); | |
56b38a1f | 1680 | atomic_dec(&lo->plh_outstanding); |
83026d80 | 1681 | if (IS_ERR(lseg)) { |
183d9e7b | 1682 | switch(PTR_ERR(lseg)) { |
e85d7ee4 | 1683 | case -EBUSY: |
183d9e7b JL |
1684 | if (time_after(jiffies, giveup)) |
1685 | lseg = NULL; | |
66b53f32 TM |
1686 | break; |
1687 | case -ERECALLCONFLICT: | |
1688 | /* Huh? We hold no layouts, how is there a recall? */ | |
1689 | if (first) { | |
1690 | lseg = NULL; | |
1691 | break; | |
1692 | } | |
1693 | /* Destroy the existing layout and start over */ | |
1694 | if (time_after(jiffies, giveup)) | |
1695 | pnfs_destroy_layout(NFS_I(ino)); | |
183d9e7b JL |
1696 | /* Fallthrough */ |
1697 | case -EAGAIN: | |
56b38a1f | 1698 | break; |
183d9e7b JL |
1699 | default: |
1700 | if (!nfs_error_is_fatal(PTR_ERR(lseg))) { | |
1701 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); | |
1702 | lseg = NULL; | |
1703 | } | |
56b38a1f TM |
1704 | goto out_put_layout_hdr; |
1705 | } | |
1706 | if (lseg) { | |
1707 | if (first) | |
1708 | pnfs_clear_first_layoutget(lo); | |
1709 | trace_pnfs_update_layout(ino, pos, count, | |
1710 | iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY); | |
1711 | pnfs_put_layout_hdr(lo); | |
1712 | goto lookup_again; | |
83026d80 JL |
1713 | } |
1714 | } else { | |
1715 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); | |
1716 | } | |
1717 | ||
830ffb56 | 1718 | out_put_layout_hdr: |
d67ae825 TH |
1719 | if (first) |
1720 | pnfs_clear_first_layoutget(lo); | |
70c3bd2b | 1721 | pnfs_put_layout_hdr(lo); |
e5e94017 | 1722 | out: |
f86bbcf8 TM |
1723 | dprintk("%s: inode %s/%llu pNFS layout segment %s for " |
1724 | "(%s, offset: %llu, length: %llu)\n", | |
1725 | __func__, ino->i_sb->s_id, | |
1726 | (unsigned long long)NFS_FILEID(ino), | |
d600ad1f | 1727 | IS_ERR_OR_NULL(lseg) ? "not found" : "found", |
f86bbcf8 TM |
1728 | iomode==IOMODE_RW ? "read/write" : "read-only", |
1729 | (unsigned long long)pos, | |
1730 | (unsigned long long)count); | |
e5e94017 BH |
1731 | return lseg; |
1732 | out_unlock: | |
1733 | spin_unlock(&ino->i_lock); | |
830ffb56 | 1734 | goto out_put_layout_hdr; |
e5e94017 | 1735 | } |
7c24d948 | 1736 | EXPORT_SYMBOL_GPL(pnfs_update_layout); |
b1f69b75 | 1737 | |
540d9864 TM |
1738 | static bool |
1739 | pnfs_sanity_check_layout_range(struct pnfs_layout_range *range) | |
1740 | { | |
1741 | switch (range->iomode) { | |
1742 | case IOMODE_READ: | |
1743 | case IOMODE_RW: | |
1744 | break; | |
1745 | default: | |
1746 | return false; | |
1747 | } | |
1748 | if (range->offset == NFS4_MAX_UINT64) | |
1749 | return false; | |
1750 | if (range->length == 0) | |
1751 | return false; | |
1752 | if (range->length != NFS4_MAX_UINT64 && | |
1753 | range->length > NFS4_MAX_UINT64 - range->offset) | |
1754 | return false; | |
1755 | return true; | |
1756 | } | |
1757 | ||
a0b0a6e3 | 1758 | struct pnfs_layout_segment * |
b1f69b75 AA |
1759 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
1760 | { | |
1761 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; | |
1762 | struct nfs4_layoutget_res *res = &lgp->res; | |
1763 | struct pnfs_layout_segment *lseg; | |
b7edfaa1 | 1764 | struct inode *ino = lo->plh_inode; |
78096cca | 1765 | LIST_HEAD(free_me); |
540d9864 TM |
1766 | |
1767 | if (!pnfs_sanity_check_layout_range(&res->range)) | |
1b3c6d07 | 1768 | return ERR_PTR(-EINVAL); |
b1f69b75 AA |
1769 | |
1770 | /* Inject layout blob into I/O device driver */ | |
a75b9df9 | 1771 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags); |
1b3c6d07 | 1772 | if (IS_ERR_OR_NULL(lseg)) { |
b1f69b75 | 1773 | if (!lseg) |
1b3c6d07 JL |
1774 | lseg = ERR_PTR(-ENOMEM); |
1775 | ||
1776 | dprintk("%s: Could not allocate layout: error %ld\n", | |
1777 | __func__, PTR_ERR(lseg)); | |
1778 | return lseg; | |
b1f69b75 AA |
1779 | } |
1780 | ||
119cef97 | 1781 | pnfs_init_lseg(lo, lseg, &res->range, &res->stateid); |
1013df61 | 1782 | |
b1f69b75 | 1783 | spin_lock(&ino->i_lock); |
e1c06f80 | 1784 | if (pnfs_layoutgets_blocked(lo)) { |
43f1b3da | 1785 | dprintk("%s forget reply due to state\n", __func__); |
1b3c6d07 | 1786 | goto out_forget; |
43f1b3da | 1787 | } |
038d6493 | 1788 | |
362f7474 CH |
1789 | if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) { |
1790 | /* existing state ID, make sure the sequence number matches. */ | |
1791 | if (pnfs_layout_stateid_blocked(lo, &res->stateid)) { | |
1792 | dprintk("%s forget reply due to sequence\n", __func__); | |
1b3c6d07 | 1793 | goto out_forget; |
362f7474 CH |
1794 | } |
1795 | pnfs_set_layout_stateid(lo, &res->stateid, false); | |
1796 | } else { | |
1797 | /* | |
1798 | * We got an entirely new state ID. Mark all segments for the | |
1799 | * inode invalid, and don't bother validating the stateid | |
1800 | * sequence number. | |
1801 | */ | |
d9b61708 | 1802 | pnfs_mark_layout_stateid_invalid(lo, &free_me); |
362f7474 CH |
1803 | |
1804 | nfs4_stateid_copy(&lo->plh_stateid, &res->stateid); | |
1805 | lo->plh_barrier = be32_to_cpu(res->stateid.seqid); | |
1806 | } | |
038d6493 | 1807 | |
9369a431 | 1808 | pnfs_get_lseg(lseg); |
03772d2f | 1809 | pnfs_layout_insert_lseg(lo, lseg, &free_me); |
8e0acf90 TM |
1810 | if (!pnfs_layout_is_valid(lo)) { |
1811 | pnfs_clear_layoutreturn_info(lo); | |
1812 | clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); | |
1813 | } | |
1814 | ||
b1f69b75 | 1815 | |
3976143b | 1816 | if (res->return_on_close) |
f7e8917a | 1817 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
f7e8917a | 1818 | |
b1f69b75 | 1819 | spin_unlock(&ino->i_lock); |
78096cca | 1820 | pnfs_free_lseg_list(&free_me); |
a0b0a6e3 | 1821 | return lseg; |
43f1b3da | 1822 | |
1b3c6d07 | 1823 | out_forget: |
43f1b3da FI |
1824 | spin_unlock(&ino->i_lock); |
1825 | lseg->pls_layout = lo; | |
1826 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); | |
1b3c6d07 | 1827 | return ERR_PTR(-EAGAIN); |
b1f69b75 AA |
1828 | } |
1829 | ||
016256df | 1830 | static void |
3982a6a2 JL |
1831 | pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode, |
1832 | u32 seq) | |
5c97f5de | 1833 | { |
2d6cf5ab | 1834 | if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode) |
5c97f5de TM |
1835 | iomode = IOMODE_ANY; |
1836 | lo->plh_return_iomode = iomode; | |
e0fa0d01 | 1837 | set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); |
2d6cf5ab TM |
1838 | if (seq != 0) { |
1839 | WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq); | |
3982a6a2 | 1840 | lo->plh_return_seq = seq; |
2d6cf5ab | 1841 | } |
5c97f5de TM |
1842 | } |
1843 | ||
2f215968 TM |
1844 | /** |
1845 | * pnfs_mark_matching_lsegs_return - Free or return matching layout segments | |
1846 | * @lo: pointer to layout header | |
1847 | * @tmp_list: list header to be used with pnfs_free_lseg_list() | |
1848 | * @return_range: describe layout segment ranges to be returned | |
1849 | * | |
1850 | * This function is mainly intended for use by layoutrecall. It attempts | |
1851 | * to free the layout segment immediately, or else to mark it for return | |
1852 | * as soon as its reference count drops to zero. | |
1853 | */ | |
10335556 | 1854 | int |
016256df PT |
1855 | pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo, |
1856 | struct list_head *tmp_list, | |
6d597e17 JL |
1857 | const struct pnfs_layout_range *return_range, |
1858 | u32 seq) | |
016256df PT |
1859 | { |
1860 | struct pnfs_layout_segment *lseg, *next; | |
10335556 | 1861 | int remaining = 0; |
016256df PT |
1862 | |
1863 | dprintk("%s:Begin lo %p\n", __func__, lo); | |
1864 | ||
1865 | if (list_empty(&lo->plh_segs)) | |
10335556 | 1866 | return 0; |
016256df | 1867 | |
fc7ff367 | 1868 | assert_spin_locked(&lo->plh_inode->i_lock); |
016256df PT |
1869 | |
1870 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) | |
e036f464 | 1871 | if (pnfs_match_lseg_recall(lseg, return_range, seq)) { |
016256df PT |
1872 | dprintk("%s: marking lseg %p iomode %d " |
1873 | "offset %llu length %llu\n", __func__, | |
1874 | lseg, lseg->pls_range.iomode, | |
1875 | lseg->pls_range.offset, | |
1876 | lseg->pls_range.length); | |
2f215968 TM |
1877 | if (mark_lseg_invalid(lseg, tmp_list)) |
1878 | continue; | |
1879 | remaining++; | |
016256df | 1880 | set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); |
016256df | 1881 | } |
6d597e17 JL |
1882 | |
1883 | if (remaining) | |
1884 | pnfs_set_plh_return_info(lo, return_range->iomode, seq); | |
1885 | ||
10335556 | 1886 | return remaining; |
016256df PT |
1887 | } |
1888 | ||
1889 | void pnfs_error_mark_layout_for_return(struct inode *inode, | |
1890 | struct pnfs_layout_segment *lseg) | |
1891 | { | |
1892 | struct pnfs_layout_hdr *lo = NFS_I(inode)->layout; | |
016256df PT |
1893 | struct pnfs_layout_range range = { |
1894 | .iomode = lseg->pls_range.iomode, | |
1895 | .offset = 0, | |
1896 | .length = NFS4_MAX_UINT64, | |
1897 | }; | |
1898 | LIST_HEAD(free_me); | |
10335556 | 1899 | bool return_now = false; |
016256df PT |
1900 | |
1901 | spin_lock(&inode->i_lock); | |
2d6cf5ab | 1902 | pnfs_set_plh_return_info(lo, range.iomode, 0); |
016256df PT |
1903 | /* |
1904 | * mark all matching lsegs so that we are sure to have no live | |
1905 | * segments at hand when sending layoutreturn. See pnfs_put_lseg() | |
1906 | * for how it works. | |
1907 | */ | |
2d6cf5ab | 1908 | if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) { |
10335556 | 1909 | nfs4_stateid stateid; |
e5fd1904 | 1910 | enum pnfs_iomode iomode; |
10335556 | 1911 | |
e5fd1904 | 1912 | return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode); |
10335556 TM |
1913 | spin_unlock(&inode->i_lock); |
1914 | if (return_now) | |
1915 | pnfs_send_layoutreturn(lo, &stateid, iomode, false); | |
1916 | } else { | |
1917 | spin_unlock(&inode->i_lock); | |
1918 | nfs_commit_inode(inode, 0); | |
1919 | } | |
016256df PT |
1920 | pnfs_free_lseg_list(&free_me); |
1921 | } | |
1922 | EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return); | |
1923 | ||
d8007d4d TM |
1924 | void |
1925 | pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) | |
1926 | { | |
1fd937bd PT |
1927 | u64 rd_size = req->wb_bytes; |
1928 | ||
cb5d04bc PT |
1929 | if (pgio->pg_lseg == NULL) { |
1930 | if (pgio->pg_dreq == NULL) | |
1931 | rd_size = i_size_read(pgio->pg_inode) - req_offset(req); | |
1932 | else | |
1933 | rd_size = nfs_dreq_bytes_left(pgio->pg_dreq); | |
1934 | ||
1935 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, | |
1936 | req->wb_context, | |
1937 | req_offset(req), | |
1938 | rd_size, | |
1939 | IOMODE_READ, | |
c7d73af2 | 1940 | false, |
cb5d04bc | 1941 | GFP_KERNEL); |
d600ad1f PT |
1942 | if (IS_ERR(pgio->pg_lseg)) { |
1943 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); | |
1944 | pgio->pg_lseg = NULL; | |
1945 | return; | |
1946 | } | |
cb5d04bc | 1947 | } |
e885de1a TM |
1948 | /* If no lseg, fall back to read through mds */ |
1949 | if (pgio->pg_lseg == NULL) | |
1f945357 | 1950 | nfs_pageio_reset_read_mds(pgio); |
e885de1a | 1951 | |
d8007d4d TM |
1952 | } |
1953 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read); | |
1954 | ||
1955 | void | |
6296556f PT |
1956 | pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, |
1957 | struct nfs_page *req, u64 wb_size) | |
d8007d4d | 1958 | { |
d600ad1f | 1959 | if (pgio->pg_lseg == NULL) { |
cb5d04bc PT |
1960 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
1961 | req->wb_context, | |
1962 | req_offset(req), | |
1963 | wb_size, | |
1964 | IOMODE_RW, | |
c7d73af2 | 1965 | false, |
cb5d04bc | 1966 | GFP_NOFS); |
d600ad1f PT |
1967 | if (IS_ERR(pgio->pg_lseg)) { |
1968 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); | |
1969 | pgio->pg_lseg = NULL; | |
1970 | return; | |
1971 | } | |
1972 | } | |
e885de1a TM |
1973 | /* If no lseg, fall back to write through mds */ |
1974 | if (pgio->pg_lseg == NULL) | |
1f945357 | 1975 | nfs_pageio_reset_write_mds(pgio); |
d8007d4d TM |
1976 | } |
1977 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write); | |
1978 | ||
180bb5ec WAA |
1979 | void |
1980 | pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc) | |
1981 | { | |
1982 | if (desc->pg_lseg) { | |
1983 | pnfs_put_lseg(desc->pg_lseg); | |
1984 | desc->pg_lseg = NULL; | |
1985 | } | |
1986 | } | |
1987 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup); | |
1988 | ||
b4fdac1a WAA |
1989 | /* |
1990 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number | |
1991 | * of bytes (maximum @req->wb_bytes) that can be coalesced. | |
1992 | */ | |
1993 | size_t | |
a7d42ddb WAA |
1994 | pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, |
1995 | struct nfs_page *prev, struct nfs_page *req) | |
94ad1c80 | 1996 | { |
0f9c429e | 1997 | unsigned int size; |
c5e20cb7 | 1998 | u64 seg_end, req_start, seg_left; |
0f9c429e WAA |
1999 | |
2000 | size = nfs_generic_pg_test(pgio, prev, req); | |
0f9c429e WAA |
2001 | if (!size) |
2002 | return 0; | |
94ad1c80 | 2003 | |
19982ba8 | 2004 | /* |
c5e20cb7 WAA |
2005 | * 'size' contains the number of bytes left in the current page (up |
2006 | * to the original size asked for in @req->wb_bytes). | |
2007 | * | |
2008 | * Calculate how many bytes are left in the layout segment | |
2009 | * and if there are less bytes than 'size', return that instead. | |
19982ba8 TM |
2010 | * |
2011 | * Please also note that 'end_offset' is actually the offset of the | |
2012 | * first byte that lies outside the pnfs_layout_range. FIXME? | |
2013 | * | |
2014 | */ | |
19b54848 | 2015 | if (pgio->pg_lseg) { |
c5e20cb7 WAA |
2016 | seg_end = end_offset(pgio->pg_lseg->pls_range.offset, |
2017 | pgio->pg_lseg->pls_range.length); | |
2018 | req_start = req_offset(req); | |
7c13789e | 2019 | WARN_ON_ONCE(req_start >= seg_end); |
c5e20cb7 | 2020 | /* start of request is past the last byte of this segment */ |
7c13789e WAA |
2021 | if (req_start >= seg_end) { |
2022 | /* reference the new lseg */ | |
2023 | if (pgio->pg_ops->pg_cleanup) | |
2024 | pgio->pg_ops->pg_cleanup(pgio); | |
2025 | if (pgio->pg_ops->pg_init) | |
2026 | pgio->pg_ops->pg_init(pgio, req); | |
19b54848 | 2027 | return 0; |
7c13789e | 2028 | } |
c5e20cb7 WAA |
2029 | |
2030 | /* adjust 'size' iff there are fewer bytes left in the | |
2031 | * segment than what nfs_generic_pg_test returned */ | |
2032 | seg_left = seg_end - req_start; | |
2033 | if (seg_left < size) | |
2034 | size = (unsigned int)seg_left; | |
19b54848 | 2035 | } |
0f9c429e | 2036 | |
19b54848 | 2037 | return size; |
94ad1c80 | 2038 | } |
89a58e32 | 2039 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_test); |
94ad1c80 | 2040 | |
53113ad3 | 2041 | int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr) |
e2fecb21 TM |
2042 | { |
2043 | struct nfs_pageio_descriptor pgio; | |
e2fecb21 TM |
2044 | |
2045 | /* Resend all requests through the MDS */ | |
53113ad3 WAA |
2046 | nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true, |
2047 | hdr->completion_ops); | |
c7070113 | 2048 | set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags); |
53113ad3 | 2049 | return nfs_pageio_resend(&pgio, hdr); |
e2fecb21 | 2050 | } |
e7dd79af | 2051 | EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds); |
e2fecb21 | 2052 | |
d45f60c6 | 2053 | static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr) |
1acbbb4e | 2054 | { |
cd841605 FI |
2055 | |
2056 | dprintk("pnfs write error = %d\n", hdr->pnfs_error); | |
2057 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & | |
1acbbb4e | 2058 | PNFS_LAYOUTRET_ON_ERROR) { |
cd841605 | 2059 | pnfs_return_layout(hdr->inode); |
1acbbb4e | 2060 | } |
6c75dc0d | 2061 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
53113ad3 | 2062 | hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr); |
1acbbb4e FI |
2063 | } |
2064 | ||
d20581aa BH |
2065 | /* |
2066 | * Called by non rpc-based layout drivers | |
2067 | */ | |
d45f60c6 | 2068 | void pnfs_ld_write_done(struct nfs_pgio_header *hdr) |
44b83799 | 2069 | { |
f8417b48 | 2070 | if (likely(!hdr->pnfs_error)) { |
67af7611 TM |
2071 | pnfs_set_layoutcommit(hdr->inode, hdr->lseg, |
2072 | hdr->mds_offset + hdr->res.count); | |
d45f60c6 | 2073 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); |
f8417b48 KM |
2074 | } |
2075 | trace_nfs4_pnfs_write(hdr, hdr->pnfs_error); | |
2076 | if (unlikely(hdr->pnfs_error)) | |
d45f60c6 WAA |
2077 | pnfs_ld_handle_write_error(hdr); |
2078 | hdr->mds_ops->rpc_release(hdr); | |
44b83799 | 2079 | } |
d20581aa | 2080 | EXPORT_SYMBOL_GPL(pnfs_ld_write_done); |
44b83799 | 2081 | |
dce81290 TM |
2082 | static void |
2083 | pnfs_write_through_mds(struct nfs_pageio_descriptor *desc, | |
d45f60c6 | 2084 | struct nfs_pgio_header *hdr) |
dce81290 | 2085 | { |
48d635f1 | 2086 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
a7d42ddb | 2087 | |
6c75dc0d | 2088 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
a7d42ddb | 2089 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
6c75dc0d | 2090 | nfs_pageio_reset_write_mds(desc); |
a7d42ddb | 2091 | mirror->pg_recoalesce = 1; |
6c75dc0d | 2092 | } |
d45f60c6 | 2093 | nfs_pgio_data_destroy(hdr); |
1ca018d2 | 2094 | hdr->release(hdr); |
dce81290 TM |
2095 | } |
2096 | ||
2097 | static enum pnfs_try_status | |
d45f60c6 | 2098 | pnfs_try_to_write_data(struct nfs_pgio_header *hdr, |
dce81290 TM |
2099 | const struct rpc_call_ops *call_ops, |
2100 | struct pnfs_layout_segment *lseg, | |
2101 | int how) | |
0382b744 | 2102 | { |
cd841605 | 2103 | struct inode *inode = hdr->inode; |
0382b744 AA |
2104 | enum pnfs_try_status trypnfs; |
2105 | struct nfs_server *nfss = NFS_SERVER(inode); | |
2106 | ||
cd841605 | 2107 | hdr->mds_ops = call_ops; |
0382b744 AA |
2108 | |
2109 | dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, | |
d45f60c6 WAA |
2110 | inode->i_ino, hdr->args.count, hdr->args.offset, how); |
2111 | trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how); | |
6c75dc0d | 2112 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
0382b744 | 2113 | nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); |
0382b744 AA |
2114 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
2115 | return trypnfs; | |
2116 | } | |
2117 | ||
dce81290 | 2118 | static void |
7f714720 WAA |
2119 | pnfs_do_write(struct nfs_pageio_descriptor *desc, |
2120 | struct nfs_pgio_header *hdr, int how) | |
dce81290 | 2121 | { |
dce81290 TM |
2122 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
2123 | struct pnfs_layout_segment *lseg = desc->pg_lseg; | |
7f714720 | 2124 | enum pnfs_try_status trypnfs; |
dce81290 | 2125 | |
d45f60c6 | 2126 | trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how); |
7f714720 | 2127 | if (trypnfs == PNFS_NOT_ATTEMPTED) |
d45f60c6 | 2128 | pnfs_write_through_mds(desc, hdr); |
dce81290 TM |
2129 | } |
2130 | ||
6c75dc0d FI |
2131 | static void pnfs_writehdr_free(struct nfs_pgio_header *hdr) |
2132 | { | |
9369a431 | 2133 | pnfs_put_lseg(hdr->lseg); |
1e7f3a48 | 2134 | nfs_pgio_header_free(hdr); |
6c75dc0d FI |
2135 | } |
2136 | ||
dce81290 TM |
2137 | int |
2138 | pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) | |
2139 | { | |
6c75dc0d | 2140 | struct nfs_pgio_header *hdr; |
dce81290 TM |
2141 | int ret; |
2142 | ||
1e7f3a48 WAA |
2143 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
2144 | if (!hdr) { | |
2bff2288 PT |
2145 | desc->pg_error = -ENOMEM; |
2146 | return desc->pg_error; | |
dce81290 | 2147 | } |
6c75dc0d | 2148 | nfs_pgheader_init(desc, hdr, pnfs_writehdr_free); |
180bb5ec | 2149 | |
9369a431 | 2150 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
ef2c488c | 2151 | ret = nfs_generic_pgio(desc, hdr); |
180bb5ec | 2152 | if (!ret) |
7f714720 | 2153 | pnfs_do_write(desc, hdr, desc->pg_ioflags); |
a7d42ddb | 2154 | |
6c75dc0d | 2155 | return ret; |
dce81290 TM |
2156 | } |
2157 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages); | |
2158 | ||
53113ad3 | 2159 | int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr) |
62e4a769 TM |
2160 | { |
2161 | struct nfs_pageio_descriptor pgio; | |
2162 | ||
1acbbb4e | 2163 | /* Resend all requests through the MDS */ |
53113ad3 WAA |
2164 | nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops); |
2165 | return nfs_pageio_resend(&pgio, hdr); | |
1acbbb4e | 2166 | } |
e7dd79af | 2167 | EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds); |
1acbbb4e | 2168 | |
d45f60c6 | 2169 | static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr) |
1acbbb4e | 2170 | { |
cd841605 FI |
2171 | dprintk("pnfs read error = %d\n", hdr->pnfs_error); |
2172 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & | |
1acbbb4e | 2173 | PNFS_LAYOUTRET_ON_ERROR) { |
cd841605 | 2174 | pnfs_return_layout(hdr->inode); |
1acbbb4e | 2175 | } |
4db6e0b7 | 2176 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
53113ad3 | 2177 | hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr); |
62e4a769 TM |
2178 | } |
2179 | ||
d20581aa BH |
2180 | /* |
2181 | * Called by non rpc-based layout drivers | |
2182 | */ | |
d45f60c6 | 2183 | void pnfs_ld_read_done(struct nfs_pgio_header *hdr) |
d20581aa | 2184 | { |
cd841605 | 2185 | if (likely(!hdr->pnfs_error)) { |
d45f60c6 WAA |
2186 | __nfs4_read_done_cb(hdr); |
2187 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); | |
f8417b48 KM |
2188 | } |
2189 | trace_nfs4_pnfs_read(hdr, hdr->pnfs_error); | |
2190 | if (unlikely(hdr->pnfs_error)) | |
d45f60c6 WAA |
2191 | pnfs_ld_handle_read_error(hdr); |
2192 | hdr->mds_ops->rpc_release(hdr); | |
d20581aa BH |
2193 | } |
2194 | EXPORT_SYMBOL_GPL(pnfs_ld_read_done); | |
2195 | ||
493292dd TM |
2196 | static void |
2197 | pnfs_read_through_mds(struct nfs_pageio_descriptor *desc, | |
d45f60c6 | 2198 | struct nfs_pgio_header *hdr) |
493292dd | 2199 | { |
48d635f1 | 2200 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
a7d42ddb | 2201 | |
4db6e0b7 | 2202 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
a7d42ddb | 2203 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
4db6e0b7 | 2204 | nfs_pageio_reset_read_mds(desc); |
a7d42ddb | 2205 | mirror->pg_recoalesce = 1; |
4db6e0b7 | 2206 | } |
d45f60c6 | 2207 | nfs_pgio_data_destroy(hdr); |
1ca018d2 | 2208 | hdr->release(hdr); |
493292dd TM |
2209 | } |
2210 | ||
64419a9b AA |
2211 | /* |
2212 | * Call the appropriate parallel I/O subsystem read function. | |
2213 | */ | |
493292dd | 2214 | static enum pnfs_try_status |
d45f60c6 | 2215 | pnfs_try_to_read_data(struct nfs_pgio_header *hdr, |
493292dd TM |
2216 | const struct rpc_call_ops *call_ops, |
2217 | struct pnfs_layout_segment *lseg) | |
64419a9b | 2218 | { |
cd841605 | 2219 | struct inode *inode = hdr->inode; |
64419a9b AA |
2220 | struct nfs_server *nfss = NFS_SERVER(inode); |
2221 | enum pnfs_try_status trypnfs; | |
2222 | ||
cd841605 | 2223 | hdr->mds_ops = call_ops; |
64419a9b AA |
2224 | |
2225 | dprintk("%s: Reading ino:%lu %u@%llu\n", | |
d45f60c6 | 2226 | __func__, inode->i_ino, hdr->args.count, hdr->args.offset); |
64419a9b | 2227 | |
d45f60c6 | 2228 | trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr); |
4db6e0b7 | 2229 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
64419a9b | 2230 | nfs_inc_stats(inode, NFSIOS_PNFS_READ); |
64419a9b AA |
2231 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
2232 | return trypnfs; | |
2233 | } | |
863a3c6c | 2234 | |
ceb11e13 | 2235 | /* Resend all requests through pnfs. */ |
1b1bc66b | 2236 | void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr) |
ceb11e13 PT |
2237 | { |
2238 | struct nfs_pageio_descriptor pgio; | |
2239 | ||
1b1bc66b WAA |
2240 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
2241 | nfs_pageio_init_read(&pgio, hdr->inode, false, | |
2242 | hdr->completion_ops); | |
2243 | hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr); | |
2244 | } | |
ceb11e13 PT |
2245 | } |
2246 | EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs); | |
2247 | ||
493292dd | 2248 | static void |
7f714720 | 2249 | pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr) |
493292dd | 2250 | { |
493292dd TM |
2251 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
2252 | struct pnfs_layout_segment *lseg = desc->pg_lseg; | |
7f714720 | 2253 | enum pnfs_try_status trypnfs; |
493292dd | 2254 | |
d45f60c6 | 2255 | trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg); |
ceb11e13 | 2256 | if (trypnfs == PNFS_TRY_AGAIN) |
1b1bc66b WAA |
2257 | pnfs_read_resend_pnfs(hdr); |
2258 | if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status) | |
d45f60c6 | 2259 | pnfs_read_through_mds(desc, hdr); |
493292dd TM |
2260 | } |
2261 | ||
4db6e0b7 FI |
2262 | static void pnfs_readhdr_free(struct nfs_pgio_header *hdr) |
2263 | { | |
9369a431 | 2264 | pnfs_put_lseg(hdr->lseg); |
1e7f3a48 | 2265 | nfs_pgio_header_free(hdr); |
4db6e0b7 FI |
2266 | } |
2267 | ||
493292dd TM |
2268 | int |
2269 | pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) | |
2270 | { | |
4db6e0b7 | 2271 | struct nfs_pgio_header *hdr; |
493292dd TM |
2272 | int ret; |
2273 | ||
1e7f3a48 WAA |
2274 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
2275 | if (!hdr) { | |
2bff2288 PT |
2276 | desc->pg_error = -ENOMEM; |
2277 | return desc->pg_error; | |
493292dd | 2278 | } |
4db6e0b7 | 2279 | nfs_pgheader_init(desc, hdr, pnfs_readhdr_free); |
9369a431 | 2280 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
ef2c488c | 2281 | ret = nfs_generic_pgio(desc, hdr); |
180bb5ec | 2282 | if (!ret) |
7f714720 | 2283 | pnfs_do_read(desc, hdr); |
4db6e0b7 | 2284 | return ret; |
493292dd TM |
2285 | } |
2286 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages); | |
2287 | ||
71244d9b TM |
2288 | static void pnfs_clear_layoutcommitting(struct inode *inode) |
2289 | { | |
2290 | unsigned long *bitlock = &NFS_I(inode)->flags; | |
2291 | ||
2292 | clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock); | |
4e857c58 | 2293 | smp_mb__after_atomic(); |
71244d9b TM |
2294 | wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING); |
2295 | } | |
2296 | ||
863a3c6c | 2297 | /* |
a9bae566 | 2298 | * There can be multiple RW segments. |
863a3c6c | 2299 | */ |
a9bae566 | 2300 | static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp) |
863a3c6c | 2301 | { |
a9bae566 | 2302 | struct pnfs_layout_segment *lseg; |
863a3c6c | 2303 | |
a9bae566 PT |
2304 | list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) { |
2305 | if (lseg->pls_range.iomode == IOMODE_RW && | |
a073dbff | 2306 | test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) |
a9bae566 PT |
2307 | list_add(&lseg->pls_lc_list, listp); |
2308 | } | |
863a3c6c AA |
2309 | } |
2310 | ||
a073dbff TM |
2311 | static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp) |
2312 | { | |
2313 | struct pnfs_layout_segment *lseg, *tmp; | |
a073dbff TM |
2314 | |
2315 | /* Matched by references in pnfs_set_layoutcommit */ | |
2316 | list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) { | |
2317 | list_del_init(&lseg->pls_lc_list); | |
2318 | pnfs_put_lseg(lseg); | |
2319 | } | |
2320 | ||
71244d9b | 2321 | pnfs_clear_layoutcommitting(inode); |
a073dbff TM |
2322 | } |
2323 | ||
1b0ae068 PT |
2324 | void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg) |
2325 | { | |
b9e028fd | 2326 | pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode); |
1b0ae068 PT |
2327 | } |
2328 | EXPORT_SYMBOL_GPL(pnfs_set_lo_fail); | |
2329 | ||
863a3c6c | 2330 | void |
67af7611 TM |
2331 | pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg, |
2332 | loff_t end_pos) | |
863a3c6c | 2333 | { |
cd841605 | 2334 | struct nfs_inode *nfsi = NFS_I(inode); |
79a48a1f | 2335 | bool mark_as_dirty = false; |
863a3c6c | 2336 | |
cd841605 | 2337 | spin_lock(&inode->i_lock); |
863a3c6c | 2338 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
29559b11 | 2339 | nfsi->layout->plh_lwb = end_pos; |
79a48a1f | 2340 | mark_as_dirty = true; |
863a3c6c | 2341 | dprintk("%s: Set layoutcommit for inode %lu ", |
cd841605 | 2342 | __func__, inode->i_ino); |
29559b11 TM |
2343 | } else if (end_pos > nfsi->layout->plh_lwb) |
2344 | nfsi->layout->plh_lwb = end_pos; | |
67af7611 | 2345 | if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) { |
a9bae566 | 2346 | /* references matched in nfs4_layoutcommit_release */ |
67af7611 | 2347 | pnfs_get_lseg(lseg); |
a9bae566 | 2348 | } |
cd841605 | 2349 | spin_unlock(&inode->i_lock); |
acff5880 | 2350 | dprintk("%s: lseg %p end_pos %llu\n", |
67af7611 | 2351 | __func__, lseg, nfsi->layout->plh_lwb); |
79a48a1f WAA |
2352 | |
2353 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one | |
2354 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ | |
2355 | if (mark_as_dirty) | |
cd841605 | 2356 | mark_inode_dirty_sync(inode); |
863a3c6c AA |
2357 | } |
2358 | EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); | |
2359 | ||
db29c089 AA |
2360 | void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data) |
2361 | { | |
2362 | struct nfs_server *nfss = NFS_SERVER(data->args.inode); | |
2363 | ||
2364 | if (nfss->pnfs_curr_ld->cleanup_layoutcommit) | |
2365 | nfss->pnfs_curr_ld->cleanup_layoutcommit(data); | |
a073dbff | 2366 | pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list); |
db29c089 AA |
2367 | } |
2368 | ||
de4b15c7 AA |
2369 | /* |
2370 | * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and | |
2371 | * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough | |
2372 | * data to disk to allow the server to recover the data if it crashes. | |
2373 | * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag | |
2374 | * is off, and a COMMIT is sent to a data server, or | |
2375 | * if WRITEs to a data server return NFS_DATA_SYNC. | |
2376 | */ | |
863a3c6c | 2377 | int |
ef311537 | 2378 | pnfs_layoutcommit_inode(struct inode *inode, bool sync) |
863a3c6c | 2379 | { |
5f919c9f | 2380 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; |
863a3c6c AA |
2381 | struct nfs4_layoutcommit_data *data; |
2382 | struct nfs_inode *nfsi = NFS_I(inode); | |
863a3c6c | 2383 | loff_t end_pos; |
71244d9b | 2384 | int status; |
863a3c6c | 2385 | |
71244d9b | 2386 | if (!pnfs_layoutcommit_outstanding(inode)) |
de4b15c7 AA |
2387 | return 0; |
2388 | ||
71244d9b | 2389 | dprintk("--> %s inode %lu\n", __func__, inode->i_ino); |
92407e75 | 2390 | |
71244d9b | 2391 | status = -EAGAIN; |
92407e75 | 2392 | if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) { |
71244d9b TM |
2393 | if (!sync) |
2394 | goto out; | |
74316201 | 2395 | status = wait_on_bit_lock_action(&nfsi->flags, |
71244d9b TM |
2396 | NFS_INO_LAYOUTCOMMITTING, |
2397 | nfs_wait_bit_killable, | |
2398 | TASK_KILLABLE); | |
92407e75 | 2399 | if (status) |
71244d9b | 2400 | goto out; |
92407e75 PT |
2401 | } |
2402 | ||
71244d9b TM |
2403 | status = -ENOMEM; |
2404 | /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ | |
2405 | data = kzalloc(sizeof(*data), GFP_NOFS); | |
2406 | if (!data) | |
2407 | goto clear_layoutcommitting; | |
2408 | ||
2409 | status = 0; | |
de4b15c7 | 2410 | spin_lock(&inode->i_lock); |
71244d9b TM |
2411 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
2412 | goto out_unlock; | |
a9bae566 | 2413 | |
71244d9b | 2414 | INIT_LIST_HEAD(&data->lseg_list); |
a9bae566 | 2415 | pnfs_list_write_lseg(inode, &data->lseg_list); |
863a3c6c | 2416 | |
acff5880 | 2417 | end_pos = nfsi->layout->plh_lwb; |
863a3c6c | 2418 | |
f597c537 | 2419 | nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid); |
863a3c6c AA |
2420 | spin_unlock(&inode->i_lock); |
2421 | ||
2422 | data->args.inode = inode; | |
9fa40758 | 2423 | data->cred = get_rpccred(nfsi->layout->plh_lc_cred); |
863a3c6c AA |
2424 | nfs_fattr_init(&data->fattr); |
2425 | data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; | |
2426 | data->res.fattr = &data->fattr; | |
2e18d4d8 TM |
2427 | if (end_pos != 0) |
2428 | data->args.lastbytewritten = end_pos - 1; | |
2429 | else | |
2430 | data->args.lastbytewritten = U64_MAX; | |
863a3c6c AA |
2431 | data->res.server = NFS_SERVER(inode); |
2432 | ||
5f919c9f CH |
2433 | if (ld->prepare_layoutcommit) { |
2434 | status = ld->prepare_layoutcommit(&data->args); | |
2435 | if (status) { | |
3471648a | 2436 | put_rpccred(data->cred); |
5f919c9f | 2437 | spin_lock(&inode->i_lock); |
29559b11 TM |
2438 | set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags); |
2439 | if (end_pos > nfsi->layout->plh_lwb) | |
5f919c9f | 2440 | nfsi->layout->plh_lwb = end_pos; |
3471648a | 2441 | goto out_unlock; |
5f919c9f CH |
2442 | } |
2443 | } | |
2444 | ||
2445 | ||
863a3c6c AA |
2446 | status = nfs4_proc_layoutcommit(data, sync); |
2447 | out: | |
92407e75 PT |
2448 | if (status) |
2449 | mark_inode_dirty_sync(inode); | |
863a3c6c AA |
2450 | dprintk("<-- %s status %d\n", __func__, status); |
2451 | return status; | |
71244d9b TM |
2452 | out_unlock: |
2453 | spin_unlock(&inode->i_lock); | |
92407e75 | 2454 | kfree(data); |
71244d9b TM |
2455 | clear_layoutcommitting: |
2456 | pnfs_clear_layoutcommitting(inode); | |
92407e75 | 2457 | goto out; |
863a3c6c | 2458 | } |
72cff449 | 2459 | EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode); |
82be417a | 2460 | |
5bb89b47 TM |
2461 | int |
2462 | pnfs_generic_sync(struct inode *inode, bool datasync) | |
2463 | { | |
2464 | return pnfs_layoutcommit_inode(inode, true); | |
2465 | } | |
2466 | EXPORT_SYMBOL_GPL(pnfs_generic_sync); | |
2467 | ||
82be417a AA |
2468 | struct nfs4_threshold *pnfs_mdsthreshold_alloc(void) |
2469 | { | |
2470 | struct nfs4_threshold *thp; | |
2471 | ||
2472 | thp = kzalloc(sizeof(*thp), GFP_NOFS); | |
2473 | if (!thp) { | |
2474 | dprintk("%s mdsthreshold allocation failed\n", __func__); | |
2475 | return NULL; | |
2476 | } | |
2477 | return thp; | |
2478 | } | |
8733408d | 2479 | |
865a7ecb | 2480 | #if IS_ENABLED(CONFIG_NFS_V4_2) |
8733408d | 2481 | int |
c8ad8894 | 2482 | pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags) |
8733408d PT |
2483 | { |
2484 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; | |
2485 | struct nfs_server *server = NFS_SERVER(inode); | |
1bfe3b25 | 2486 | struct nfs_inode *nfsi = NFS_I(inode); |
8733408d PT |
2487 | struct nfs42_layoutstat_data *data; |
2488 | struct pnfs_layout_hdr *hdr; | |
2489 | int status = 0; | |
2490 | ||
2491 | if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats) | |
2492 | goto out; | |
2493 | ||
6c5a0d89 TM |
2494 | if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS)) |
2495 | goto out; | |
2496 | ||
1bfe3b25 PT |
2497 | if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags)) |
2498 | goto out; | |
2499 | ||
8733408d PT |
2500 | spin_lock(&inode->i_lock); |
2501 | if (!NFS_I(inode)->layout) { | |
2502 | spin_unlock(&inode->i_lock); | |
f538d0ba | 2503 | goto out_clear_layoutstats; |
8733408d PT |
2504 | } |
2505 | hdr = NFS_I(inode)->layout; | |
2506 | pnfs_get_layout_hdr(hdr); | |
2507 | spin_unlock(&inode->i_lock); | |
2508 | ||
c8ad8894 | 2509 | data = kzalloc(sizeof(*data), gfp_flags); |
8733408d PT |
2510 | if (!data) { |
2511 | status = -ENOMEM; | |
2512 | goto out_put; | |
2513 | } | |
2514 | ||
2515 | data->args.fh = NFS_FH(inode); | |
2516 | data->args.inode = inode; | |
8733408d PT |
2517 | status = ld->prepare_layoutstats(&data->args); |
2518 | if (status) | |
2519 | goto out_free; | |
2520 | ||
2521 | status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data); | |
2522 | ||
2523 | out: | |
2524 | dprintk("%s returns %d\n", __func__, status); | |
2525 | return status; | |
2526 | ||
2527 | out_free: | |
2528 | kfree(data); | |
2529 | out_put: | |
2530 | pnfs_put_layout_hdr(hdr); | |
f538d0ba | 2531 | out_clear_layoutstats: |
1bfe3b25 PT |
2532 | smp_mb__before_atomic(); |
2533 | clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags); | |
2534 | smp_mb__after_atomic(); | |
8733408d PT |
2535 | goto out; |
2536 | } | |
2537 | EXPORT_SYMBOL_GPL(pnfs_report_layoutstat); | |
865a7ecb | 2538 | #endif |
bbf58bf3 TM |
2539 | |
2540 | unsigned int layoutstats_timer; | |
2541 | module_param(layoutstats_timer, uint, 0644); | |
2542 | EXPORT_SYMBOL_GPL(layoutstats_timer); |