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