Commit | Line | Data |
---|---|---|
155e7524 FI |
1 | /* |
2 | * linux/fs/nfs/blocklayout/blocklayout.c | |
3 | * | |
4 | * Module for the NFSv4.1 pNFS block layout driver. | |
5 | * | |
6 | * Copyright (c) 2006 The Regents of the University of Michigan. | |
7 | * All rights reserved. | |
8 | * | |
9 | * Andy Adamson <andros@citi.umich.edu> | |
10 | * Fred Isaman <iisaman@umich.edu> | |
11 | * | |
12 | * permission is granted to use, copy, create derivative works and | |
13 | * redistribute this software and such derivative works for any purpose, | |
14 | * so long as the name of the university of michigan is not used in | |
15 | * any advertising or publicity pertaining to the use or distribution | |
16 | * of this software without specific, written prior authorization. if | |
17 | * the above copyright notice or any other identification of the | |
18 | * university of michigan is included in any copy of any portion of | |
19 | * this software, then the disclaimer below must also be included. | |
20 | * | |
21 | * this software is provided as is, without representation from the | |
22 | * university of michigan as to its fitness for any purpose, and without | |
23 | * warranty by the university of michigan of any kind, either express | |
24 | * or implied, including without limitation the implied warranties of | |
25 | * merchantability and fitness for a particular purpose. the regents | |
26 | * of the university of michigan shall not be liable for any damages, | |
27 | * including special, indirect, incidental, or consequential damages, | |
28 | * with respect to any claim arising out or in connection with the use | |
29 | * of the software, even if it has been or is hereafter advised of the | |
30 | * possibility of such damages. | |
31 | */ | |
9549ec01 | 32 | |
155e7524 FI |
33 | #include <linux/module.h> |
34 | #include <linux/init.h> | |
fe0a9b74 JR |
35 | #include <linux/mount.h> |
36 | #include <linux/namei.h> | |
9549ec01 | 37 | #include <linux/bio.h> /* struct bio */ |
71cdd40f | 38 | #include <linux/buffer_head.h> /* various write calls */ |
88c9e421 | 39 | #include <linux/prefetch.h> |
155e7524 FI |
40 | |
41 | #include "blocklayout.h" | |
42 | ||
43 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD | |
44 | ||
45 | MODULE_LICENSE("GPL"); | |
46 | MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>"); | |
47 | MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver"); | |
48 | ||
fe0a9b74 JR |
49 | struct dentry *bl_device_pipe; |
50 | wait_queue_head_t bl_wq; | |
51 | ||
9549ec01 FI |
52 | static void print_page(struct page *page) |
53 | { | |
54 | dprintk("PRINTPAGE page %p\n", page); | |
55 | dprintk(" PagePrivate %d\n", PagePrivate(page)); | |
56 | dprintk(" PageUptodate %d\n", PageUptodate(page)); | |
57 | dprintk(" PageError %d\n", PageError(page)); | |
58 | dprintk(" PageDirty %d\n", PageDirty(page)); | |
59 | dprintk(" PageReferenced %d\n", PageReferenced(page)); | |
60 | dprintk(" PageLocked %d\n", PageLocked(page)); | |
61 | dprintk(" PageWriteback %d\n", PageWriteback(page)); | |
62 | dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page)); | |
63 | dprintk("\n"); | |
64 | } | |
65 | ||
66 | /* Given the be associated with isect, determine if page data needs to be | |
67 | * initialized. | |
68 | */ | |
69 | static int is_hole(struct pnfs_block_extent *be, sector_t isect) | |
70 | { | |
71 | if (be->be_state == PNFS_BLOCK_NONE_DATA) | |
72 | return 1; | |
73 | else if (be->be_state != PNFS_BLOCK_INVALID_DATA) | |
74 | return 0; | |
75 | else | |
76 | return !bl_is_sector_init(be->be_inval, isect); | |
77 | } | |
78 | ||
650e2d39 FI |
79 | /* Given the be associated with isect, determine if page data can be |
80 | * written to disk. | |
81 | */ | |
82 | static int is_writable(struct pnfs_block_extent *be, sector_t isect) | |
83 | { | |
71cdd40f PT |
84 | return (be->be_state == PNFS_BLOCK_READWRITE_DATA || |
85 | be->be_state == PNFS_BLOCK_INVALID_DATA); | |
650e2d39 FI |
86 | } |
87 | ||
9549ec01 FI |
88 | /* The data we are handed might be spread across several bios. We need |
89 | * to track when the last one is finished. | |
90 | */ | |
91 | struct parallel_io { | |
92 | struct kref refcnt; | |
93 | struct rpc_call_ops call_ops; | |
94 | void (*pnfs_callback) (void *data); | |
95 | void *data; | |
96 | }; | |
97 | ||
98 | static inline struct parallel_io *alloc_parallel(void *data) | |
99 | { | |
100 | struct parallel_io *rv; | |
101 | ||
102 | rv = kmalloc(sizeof(*rv), GFP_NOFS); | |
103 | if (rv) { | |
104 | rv->data = data; | |
105 | kref_init(&rv->refcnt); | |
106 | } | |
107 | return rv; | |
108 | } | |
109 | ||
110 | static inline void get_parallel(struct parallel_io *p) | |
111 | { | |
112 | kref_get(&p->refcnt); | |
113 | } | |
114 | ||
115 | static void destroy_parallel(struct kref *kref) | |
116 | { | |
117 | struct parallel_io *p = container_of(kref, struct parallel_io, refcnt); | |
118 | ||
119 | dprintk("%s enter\n", __func__); | |
120 | p->pnfs_callback(p->data); | |
121 | kfree(p); | |
122 | } | |
123 | ||
124 | static inline void put_parallel(struct parallel_io *p) | |
125 | { | |
126 | kref_put(&p->refcnt, destroy_parallel); | |
127 | } | |
128 | ||
129 | static struct bio * | |
130 | bl_submit_bio(int rw, struct bio *bio) | |
131 | { | |
132 | if (bio) { | |
133 | get_parallel(bio->bi_private); | |
134 | dprintk("%s submitting %s bio %u@%llu\n", __func__, | |
135 | rw == READ ? "read" : "write", | |
136 | bio->bi_size, (unsigned long long)bio->bi_sector); | |
137 | submit_bio(rw, bio); | |
138 | } | |
139 | return NULL; | |
140 | } | |
141 | ||
142 | static struct bio *bl_alloc_init_bio(int npg, sector_t isect, | |
143 | struct pnfs_block_extent *be, | |
144 | void (*end_io)(struct bio *, int err), | |
145 | struct parallel_io *par) | |
146 | { | |
147 | struct bio *bio; | |
148 | ||
149 | bio = bio_alloc(GFP_NOIO, npg); | |
150 | if (!bio) | |
151 | return NULL; | |
152 | ||
153 | bio->bi_sector = isect - be->be_f_offset + be->be_v_offset; | |
154 | bio->bi_bdev = be->be_mdev; | |
155 | bio->bi_end_io = end_io; | |
156 | bio->bi_private = par; | |
157 | return bio; | |
158 | } | |
159 | ||
160 | static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw, | |
161 | sector_t isect, struct page *page, | |
162 | struct pnfs_block_extent *be, | |
163 | void (*end_io)(struct bio *, int err), | |
164 | struct parallel_io *par) | |
165 | { | |
166 | retry: | |
167 | if (!bio) { | |
168 | bio = bl_alloc_init_bio(npg, isect, be, end_io, par); | |
169 | if (!bio) | |
170 | return ERR_PTR(-ENOMEM); | |
171 | } | |
172 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { | |
173 | bio = bl_submit_bio(rw, bio); | |
174 | goto retry; | |
175 | } | |
176 | return bio; | |
177 | } | |
178 | ||
9549ec01 FI |
179 | /* This is basically copied from mpage_end_io_read */ |
180 | static void bl_end_io_read(struct bio *bio, int err) | |
181 | { | |
182 | struct parallel_io *par = bio->bi_private; | |
183 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
184 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
185 | struct nfs_read_data *rdata = (struct nfs_read_data *)par->data; | |
186 | ||
187 | do { | |
188 | struct page *page = bvec->bv_page; | |
189 | ||
190 | if (--bvec >= bio->bi_io_vec) | |
191 | prefetchw(&bvec->bv_page->flags); | |
192 | if (uptodate) | |
193 | SetPageUptodate(page); | |
194 | } while (bvec >= bio->bi_io_vec); | |
195 | if (!uptodate) { | |
196 | if (!rdata->pnfs_error) | |
197 | rdata->pnfs_error = -EIO; | |
1b0ae068 | 198 | pnfs_set_lo_fail(rdata->lseg); |
9549ec01 FI |
199 | } |
200 | bio_put(bio); | |
201 | put_parallel(par); | |
202 | } | |
203 | ||
204 | static void bl_read_cleanup(struct work_struct *work) | |
205 | { | |
206 | struct rpc_task *task; | |
207 | struct nfs_read_data *rdata; | |
208 | dprintk("%s enter\n", __func__); | |
209 | task = container_of(work, struct rpc_task, u.tk_work); | |
210 | rdata = container_of(task, struct nfs_read_data, task); | |
211 | pnfs_ld_read_done(rdata); | |
212 | } | |
213 | ||
214 | static void | |
215 | bl_end_par_io_read(void *data) | |
216 | { | |
217 | struct nfs_read_data *rdata = data; | |
218 | ||
219 | INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup); | |
220 | schedule_work(&rdata->task.u.tk_work); | |
221 | } | |
222 | ||
223 | /* We don't want normal .rpc_call_done callback used, so we replace it | |
224 | * with this stub. | |
225 | */ | |
226 | static void bl_rpc_do_nothing(struct rpc_task *task, void *calldata) | |
227 | { | |
228 | return; | |
229 | } | |
230 | ||
155e7524 FI |
231 | static enum pnfs_try_status |
232 | bl_read_pagelist(struct nfs_read_data *rdata) | |
233 | { | |
9549ec01 FI |
234 | int i, hole; |
235 | struct bio *bio = NULL; | |
236 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; | |
237 | sector_t isect, extent_length = 0; | |
238 | struct parallel_io *par; | |
239 | loff_t f_offset = rdata->args.offset; | |
240 | size_t count = rdata->args.count; | |
241 | struct page **pages = rdata->args.pages; | |
242 | int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
243 | ||
244 | dprintk("%s enter nr_pages %u offset %lld count %Zd\n", __func__, | |
245 | rdata->npages, f_offset, count); | |
246 | ||
247 | par = alloc_parallel(rdata); | |
248 | if (!par) | |
249 | goto use_mds; | |
250 | par->call_ops = *rdata->mds_ops; | |
251 | par->call_ops.rpc_call_done = bl_rpc_do_nothing; | |
252 | par->pnfs_callback = bl_end_par_io_read; | |
253 | /* At this point, we can no longer jump to use_mds */ | |
254 | ||
255 | isect = (sector_t) (f_offset >> SECTOR_SHIFT); | |
256 | /* Code assumes extents are page-aligned */ | |
257 | for (i = pg_index; i < rdata->npages; i++) { | |
258 | if (!extent_length) { | |
259 | /* We've used up the previous extent */ | |
260 | bl_put_extent(be); | |
261 | bl_put_extent(cow_read); | |
262 | bio = bl_submit_bio(READ, bio); | |
263 | /* Get the next one */ | |
264 | be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg), | |
265 | isect, &cow_read); | |
266 | if (!be) { | |
267 | rdata->pnfs_error = -EIO; | |
268 | goto out; | |
269 | } | |
270 | extent_length = be->be_length - | |
271 | (isect - be->be_f_offset); | |
272 | if (cow_read) { | |
273 | sector_t cow_length = cow_read->be_length - | |
274 | (isect - cow_read->be_f_offset); | |
275 | extent_length = min(extent_length, cow_length); | |
276 | } | |
277 | } | |
278 | hole = is_hole(be, isect); | |
279 | if (hole && !cow_read) { | |
280 | bio = bl_submit_bio(READ, bio); | |
281 | /* Fill hole w/ zeroes w/o accessing device */ | |
282 | dprintk("%s Zeroing page for hole\n", __func__); | |
283 | zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE); | |
284 | print_page(pages[i]); | |
285 | SetPageUptodate(pages[i]); | |
286 | } else { | |
287 | struct pnfs_block_extent *be_read; | |
288 | ||
289 | be_read = (hole && cow_read) ? cow_read : be; | |
290 | bio = bl_add_page_to_bio(bio, rdata->npages - i, READ, | |
291 | isect, pages[i], be_read, | |
292 | bl_end_io_read, par); | |
293 | if (IS_ERR(bio)) { | |
294 | rdata->pnfs_error = PTR_ERR(bio); | |
295 | goto out; | |
296 | } | |
297 | } | |
298 | isect += PAGE_CACHE_SECTORS; | |
299 | extent_length -= PAGE_CACHE_SECTORS; | |
300 | } | |
301 | if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) { | |
302 | rdata->res.eof = 1; | |
303 | rdata->res.count = rdata->inode->i_size - f_offset; | |
304 | } else { | |
305 | rdata->res.count = (isect << SECTOR_SHIFT) - f_offset; | |
306 | } | |
307 | out: | |
308 | bl_put_extent(be); | |
309 | bl_put_extent(cow_read); | |
310 | bl_submit_bio(READ, bio); | |
311 | put_parallel(par); | |
312 | return PNFS_ATTEMPTED; | |
313 | ||
314 | use_mds: | |
315 | dprintk("Giving up and using normal NFS\n"); | |
155e7524 FI |
316 | return PNFS_NOT_ATTEMPTED; |
317 | } | |
318 | ||
31e6306a FI |
319 | static void mark_extents_written(struct pnfs_block_layout *bl, |
320 | __u64 offset, __u32 count) | |
321 | { | |
322 | sector_t isect, end; | |
323 | struct pnfs_block_extent *be; | |
324 | ||
325 | dprintk("%s(%llu, %u)\n", __func__, offset, count); | |
326 | if (count == 0) | |
327 | return; | |
328 | isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT; | |
329 | end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK); | |
330 | end >>= SECTOR_SHIFT; | |
331 | while (isect < end) { | |
332 | sector_t len; | |
333 | be = bl_find_get_extent(bl, isect, NULL); | |
334 | BUG_ON(!be); /* FIXME */ | |
335 | len = min(end, be->be_f_offset + be->be_length) - isect; | |
336 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) | |
337 | bl_mark_for_commit(be, isect, len); /* What if fails? */ | |
338 | isect += len; | |
339 | bl_put_extent(be); | |
340 | } | |
341 | } | |
342 | ||
71cdd40f PT |
343 | static void bl_end_io_write_zero(struct bio *bio, int err) |
344 | { | |
345 | struct parallel_io *par = bio->bi_private; | |
346 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
347 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
348 | struct nfs_write_data *wdata = (struct nfs_write_data *)par->data; | |
349 | ||
350 | do { | |
351 | struct page *page = bvec->bv_page; | |
352 | ||
353 | if (--bvec >= bio->bi_io_vec) | |
354 | prefetchw(&bvec->bv_page->flags); | |
355 | /* This is the zeroing page we added */ | |
356 | end_page_writeback(page); | |
357 | page_cache_release(page); | |
358 | } while (bvec >= bio->bi_io_vec); | |
359 | if (!uptodate) { | |
360 | if (!wdata->pnfs_error) | |
361 | wdata->pnfs_error = -EIO; | |
1b0ae068 | 362 | pnfs_set_lo_fail(wdata->lseg); |
71cdd40f PT |
363 | } |
364 | bio_put(bio); | |
365 | put_parallel(par); | |
366 | } | |
367 | ||
650e2d39 FI |
368 | /* This is basically copied from mpage_end_io_read */ |
369 | static void bl_end_io_write(struct bio *bio, int err) | |
370 | { | |
371 | struct parallel_io *par = bio->bi_private; | |
372 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
373 | struct nfs_write_data *wdata = (struct nfs_write_data *)par->data; | |
374 | ||
375 | if (!uptodate) { | |
376 | if (!wdata->pnfs_error) | |
377 | wdata->pnfs_error = -EIO; | |
1b0ae068 | 378 | pnfs_set_lo_fail(wdata->lseg); |
650e2d39 FI |
379 | } |
380 | bio_put(bio); | |
381 | put_parallel(par); | |
382 | } | |
383 | ||
384 | /* Function scheduled for call during bl_end_par_io_write, | |
385 | * it marks sectors as written and extends the commitlist. | |
386 | */ | |
387 | static void bl_write_cleanup(struct work_struct *work) | |
388 | { | |
389 | struct rpc_task *task; | |
390 | struct nfs_write_data *wdata; | |
391 | dprintk("%s enter\n", __func__); | |
392 | task = container_of(work, struct rpc_task, u.tk_work); | |
393 | wdata = container_of(task, struct nfs_write_data, task); | |
71cdd40f | 394 | if (!wdata->pnfs_error) { |
31e6306a | 395 | /* Marks for LAYOUTCOMMIT */ |
31e6306a FI |
396 | mark_extents_written(BLK_LSEG2EXT(wdata->lseg), |
397 | wdata->args.offset, wdata->args.count); | |
398 | } | |
650e2d39 FI |
399 | pnfs_ld_write_done(wdata); |
400 | } | |
401 | ||
402 | /* Called when last of bios associated with a bl_write_pagelist call finishes */ | |
71cdd40f | 403 | static void bl_end_par_io_write(void *data) |
650e2d39 FI |
404 | { |
405 | struct nfs_write_data *wdata = data; | |
406 | ||
650e2d39 FI |
407 | wdata->task.tk_status = 0; |
408 | wdata->verf.committed = NFS_FILE_SYNC; | |
409 | INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup); | |
410 | schedule_work(&wdata->task.u.tk_work); | |
411 | } | |
412 | ||
71cdd40f PT |
413 | /* FIXME STUB - mark intersection of layout and page as bad, so is not |
414 | * used again. | |
415 | */ | |
416 | static void mark_bad_read(void) | |
417 | { | |
418 | return; | |
419 | } | |
420 | ||
421 | /* | |
422 | * map_block: map a requested I/0 block (isect) into an offset in the LVM | |
423 | * block_device | |
424 | */ | |
425 | static void | |
426 | map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be) | |
427 | { | |
428 | dprintk("%s enter be=%p\n", __func__, be); | |
429 | ||
430 | set_buffer_mapped(bh); | |
431 | bh->b_bdev = be->be_mdev; | |
432 | bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >> | |
433 | (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT); | |
434 | ||
435 | dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n", | |
436 | __func__, (unsigned long long)isect, (long)bh->b_blocknr, | |
437 | bh->b_size); | |
438 | return; | |
439 | } | |
440 | ||
441 | /* Given an unmapped page, zero it or read in page for COW, page is locked | |
442 | * by caller. | |
443 | */ | |
444 | static int | |
445 | init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read) | |
446 | { | |
447 | struct buffer_head *bh = NULL; | |
448 | int ret = 0; | |
449 | sector_t isect; | |
450 | ||
451 | dprintk("%s enter, %p\n", __func__, page); | |
452 | BUG_ON(PageUptodate(page)); | |
453 | if (!cow_read) { | |
454 | zero_user_segment(page, 0, PAGE_SIZE); | |
455 | SetPageUptodate(page); | |
456 | goto cleanup; | |
457 | } | |
458 | ||
459 | bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0); | |
460 | if (!bh) { | |
461 | ret = -ENOMEM; | |
462 | goto cleanup; | |
463 | } | |
464 | ||
465 | isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT; | |
466 | map_block(bh, isect, cow_read); | |
467 | if (!bh_uptodate_or_lock(bh)) | |
468 | ret = bh_submit_read(bh); | |
469 | if (ret) | |
470 | goto cleanup; | |
471 | SetPageUptodate(page); | |
472 | ||
473 | cleanup: | |
474 | bl_put_extent(cow_read); | |
475 | if (bh) | |
476 | free_buffer_head(bh); | |
477 | if (ret) { | |
478 | /* Need to mark layout with bad read...should now | |
479 | * just use nfs4 for reads and writes. | |
480 | */ | |
481 | mark_bad_read(); | |
482 | } | |
483 | return ret; | |
484 | } | |
485 | ||
155e7524 | 486 | static enum pnfs_try_status |
650e2d39 | 487 | bl_write_pagelist(struct nfs_write_data *wdata, int sync) |
155e7524 | 488 | { |
71cdd40f | 489 | int i, ret, npg_zero, pg_index, last = 0; |
650e2d39 | 490 | struct bio *bio = NULL; |
71cdd40f PT |
491 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; |
492 | sector_t isect, last_isect = 0, extent_length = 0; | |
650e2d39 FI |
493 | struct parallel_io *par; |
494 | loff_t offset = wdata->args.offset; | |
495 | size_t count = wdata->args.count; | |
496 | struct page **pages = wdata->args.pages; | |
71cdd40f PT |
497 | struct page *page; |
498 | pgoff_t index; | |
499 | u64 temp; | |
500 | int npg_per_block = | |
501 | NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT; | |
650e2d39 FI |
502 | |
503 | dprintk("%s enter, %Zu@%lld\n", __func__, count, offset); | |
504 | /* At this point, wdata->pages is a (sequential) list of nfs_pages. | |
71cdd40f PT |
505 | * We want to write each, and if there is an error set pnfs_error |
506 | * to have it redone using nfs. | |
650e2d39 FI |
507 | */ |
508 | par = alloc_parallel(wdata); | |
509 | if (!par) | |
510 | return PNFS_NOT_ATTEMPTED; | |
511 | par->call_ops = *wdata->mds_ops; | |
512 | par->call_ops.rpc_call_done = bl_rpc_do_nothing; | |
513 | par->pnfs_callback = bl_end_par_io_write; | |
514 | /* At this point, have to be more careful with error handling */ | |
515 | ||
516 | isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
71cdd40f PT |
517 | be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read); |
518 | if (!be || !is_writable(be, isect)) { | |
519 | dprintk("%s no matching extents!\n", __func__); | |
520 | wdata->pnfs_error = -EINVAL; | |
521 | goto out; | |
522 | } | |
523 | ||
524 | /* First page inside INVALID extent */ | |
525 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
526 | temp = offset >> PAGE_CACHE_SHIFT; | |
527 | npg_zero = do_div(temp, npg_per_block); | |
528 | isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) & | |
529 | (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
530 | extent_length = be->be_length - (isect - be->be_f_offset); | |
531 | ||
532 | fill_invalid_ext: | |
533 | dprintk("%s need to zero %d pages\n", __func__, npg_zero); | |
534 | for (;npg_zero > 0; npg_zero--) { | |
535 | /* page ref released in bl_end_io_write_zero */ | |
536 | index = isect >> PAGE_CACHE_SECTOR_SHIFT; | |
537 | dprintk("%s zero %dth page: index %lu isect %llu\n", | |
538 | __func__, npg_zero, index, | |
539 | (unsigned long long)isect); | |
540 | page = | |
541 | find_or_create_page(wdata->inode->i_mapping, index, | |
542 | GFP_NOFS); | |
543 | if (!page) { | |
544 | dprintk("%s oom\n", __func__); | |
545 | wdata->pnfs_error = -ENOMEM; | |
546 | goto out; | |
547 | } | |
548 | ||
549 | /* PageDirty: Other will write this out | |
550 | * PageWriteback: Other is writing this out | |
551 | * PageUptodate: It was read before | |
552 | * sector_initialized: already written out | |
553 | */ | |
554 | if (PageDirty(page) || PageWriteback(page) || | |
555 | bl_is_sector_init(be->be_inval, isect)) { | |
556 | print_page(page); | |
557 | unlock_page(page); | |
558 | page_cache_release(page); | |
559 | goto next_page; | |
560 | } | |
561 | if (!PageUptodate(page)) { | |
562 | /* New page, readin or zero it */ | |
563 | init_page_for_write(page, cow_read); | |
564 | } | |
565 | set_page_writeback(page); | |
566 | unlock_page(page); | |
567 | ||
568 | ret = bl_mark_sectors_init(be->be_inval, isect, | |
569 | PAGE_CACHE_SECTORS, | |
570 | NULL); | |
571 | if (unlikely(ret)) { | |
572 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
573 | __func__, ret); | |
574 | end_page_writeback(page); | |
575 | page_cache_release(page); | |
576 | wdata->pnfs_error = ret; | |
577 | goto out; | |
578 | } | |
579 | bio = bl_add_page_to_bio(bio, npg_zero, WRITE, | |
580 | isect, page, be, | |
581 | bl_end_io_write_zero, par); | |
582 | if (IS_ERR(bio)) { | |
583 | wdata->pnfs_error = PTR_ERR(bio); | |
584 | goto out; | |
585 | } | |
586 | /* FIXME: This should be done in bi_end_io */ | |
587 | mark_extents_written(BLK_LSEG2EXT(wdata->lseg), | |
588 | page->index << PAGE_CACHE_SHIFT, | |
589 | PAGE_CACHE_SIZE); | |
590 | next_page: | |
591 | isect += PAGE_CACHE_SECTORS; | |
592 | extent_length -= PAGE_CACHE_SECTORS; | |
593 | } | |
594 | if (last) | |
595 | goto write_done; | |
596 | } | |
597 | bio = bl_submit_bio(WRITE, bio); | |
598 | ||
599 | /* Middle pages */ | |
600 | pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
601 | for (i = pg_index; i < wdata->npages; i++) { | |
650e2d39 FI |
602 | if (!extent_length) { |
603 | /* We've used up the previous extent */ | |
604 | bl_put_extent(be); | |
605 | bio = bl_submit_bio(WRITE, bio); | |
606 | /* Get the next one */ | |
607 | be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), | |
608 | isect, NULL); | |
609 | if (!be || !is_writable(be, isect)) { | |
71cdd40f | 610 | wdata->pnfs_error = -EINVAL; |
650e2d39 FI |
611 | goto out; |
612 | } | |
613 | extent_length = be->be_length - | |
71cdd40f | 614 | (isect - be->be_f_offset); |
650e2d39 | 615 | } |
71cdd40f PT |
616 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
617 | ret = bl_mark_sectors_init(be->be_inval, isect, | |
618 | PAGE_CACHE_SECTORS, | |
619 | NULL); | |
620 | if (unlikely(ret)) { | |
621 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
622 | __func__, ret); | |
623 | wdata->pnfs_error = ret; | |
624 | goto out; | |
650e2d39 | 625 | } |
71cdd40f PT |
626 | } |
627 | bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE, | |
628 | isect, pages[i], be, | |
629 | bl_end_io_write, par); | |
630 | if (IS_ERR(bio)) { | |
631 | wdata->pnfs_error = PTR_ERR(bio); | |
632 | goto out; | |
650e2d39 FI |
633 | } |
634 | isect += PAGE_CACHE_SECTORS; | |
71cdd40f | 635 | last_isect = isect; |
650e2d39 FI |
636 | extent_length -= PAGE_CACHE_SECTORS; |
637 | } | |
71cdd40f PT |
638 | |
639 | /* Last page inside INVALID extent */ | |
640 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
641 | bio = bl_submit_bio(WRITE, bio); | |
642 | temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT; | |
643 | npg_zero = npg_per_block - do_div(temp, npg_per_block); | |
644 | if (npg_zero < npg_per_block) { | |
645 | last = 1; | |
646 | goto fill_invalid_ext; | |
647 | } | |
648 | } | |
649 | ||
650 | write_done: | |
651 | wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset); | |
652 | if (count < wdata->res.count) { | |
650e2d39 | 653 | wdata->res.count = count; |
71cdd40f | 654 | } |
650e2d39 FI |
655 | out: |
656 | bl_put_extent(be); | |
657 | bl_submit_bio(WRITE, bio); | |
658 | put_parallel(par); | |
659 | return PNFS_ATTEMPTED; | |
155e7524 FI |
660 | } |
661 | ||
9e692969 | 662 | /* FIXME - range ignored */ |
155e7524 | 663 | static void |
9e692969 | 664 | release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range) |
155e7524 | 665 | { |
9e692969 FI |
666 | int i; |
667 | struct pnfs_block_extent *be; | |
668 | ||
669 | spin_lock(&bl->bl_ext_lock); | |
670 | for (i = 0; i < EXTENT_LISTS; i++) { | |
671 | while (!list_empty(&bl->bl_extents[i])) { | |
672 | be = list_first_entry(&bl->bl_extents[i], | |
673 | struct pnfs_block_extent, | |
674 | be_node); | |
675 | list_del(&be->be_node); | |
676 | bl_put_extent(be); | |
677 | } | |
678 | } | |
679 | spin_unlock(&bl->bl_ext_lock); | |
155e7524 FI |
680 | } |
681 | ||
155e7524 FI |
682 | static void |
683 | release_inval_marks(struct pnfs_inval_markings *marks) | |
684 | { | |
c1c2a4cd FI |
685 | struct pnfs_inval_tracking *pos, *temp; |
686 | ||
687 | list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) { | |
688 | list_del(&pos->it_link); | |
689 | kfree(pos); | |
690 | } | |
155e7524 FI |
691 | return; |
692 | } | |
693 | ||
694 | static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
695 | { | |
696 | struct pnfs_block_layout *bl = BLK_LO2EXT(lo); | |
697 | ||
698 | dprintk("%s enter\n", __func__); | |
699 | release_extents(bl, NULL); | |
700 | release_inval_marks(&bl->bl_inval); | |
701 | kfree(bl); | |
702 | } | |
703 | ||
704 | static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode, | |
705 | gfp_t gfp_flags) | |
706 | { | |
707 | struct pnfs_block_layout *bl; | |
708 | ||
709 | dprintk("%s enter\n", __func__); | |
710 | bl = kzalloc(sizeof(*bl), gfp_flags); | |
711 | if (!bl) | |
712 | return NULL; | |
713 | spin_lock_init(&bl->bl_ext_lock); | |
714 | INIT_LIST_HEAD(&bl->bl_extents[0]); | |
715 | INIT_LIST_HEAD(&bl->bl_extents[1]); | |
716 | INIT_LIST_HEAD(&bl->bl_commit); | |
717 | INIT_LIST_HEAD(&bl->bl_committing); | |
718 | bl->bl_count = 0; | |
719 | bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT; | |
720 | BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize); | |
721 | return &bl->bl_layout; | |
722 | } | |
723 | ||
a60d2ebd | 724 | static void bl_free_lseg(struct pnfs_layout_segment *lseg) |
155e7524 | 725 | { |
a60d2ebd FI |
726 | dprintk("%s enter\n", __func__); |
727 | kfree(lseg); | |
155e7524 FI |
728 | } |
729 | ||
a60d2ebd FI |
730 | /* We pretty much ignore lseg, and store all data layout wide, so we |
731 | * can correctly merge. | |
732 | */ | |
733 | static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo, | |
734 | struct nfs4_layoutget_res *lgr, | |
735 | gfp_t gfp_flags) | |
155e7524 | 736 | { |
a60d2ebd FI |
737 | struct pnfs_layout_segment *lseg; |
738 | int status; | |
739 | ||
740 | dprintk("%s enter\n", __func__); | |
741 | lseg = kzalloc(sizeof(*lseg), gfp_flags); | |
742 | if (!lseg) | |
743 | return ERR_PTR(-ENOMEM); | |
744 | status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags); | |
745 | if (status) { | |
746 | /* We don't want to call the full-blown bl_free_lseg, | |
747 | * since on error extents were not touched. | |
748 | */ | |
749 | kfree(lseg); | |
750 | return ERR_PTR(status); | |
751 | } | |
752 | return lseg; | |
155e7524 FI |
753 | } |
754 | ||
755 | static void | |
756 | bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr, | |
757 | const struct nfs4_layoutcommit_args *arg) | |
758 | { | |
90ace12a FI |
759 | dprintk("%s enter\n", __func__); |
760 | encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg); | |
155e7524 FI |
761 | } |
762 | ||
763 | static void | |
764 | bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata) | |
765 | { | |
b2be7811 FI |
766 | struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout; |
767 | ||
768 | dprintk("%s enter\n", __func__); | |
769 | clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status); | |
155e7524 FI |
770 | } |
771 | ||
2f9fd182 FI |
772 | static void free_blk_mountid(struct block_mount_id *mid) |
773 | { | |
774 | if (mid) { | |
775 | struct pnfs_block_dev *dev; | |
776 | spin_lock(&mid->bm_lock); | |
777 | while (!list_empty(&mid->bm_devlist)) { | |
778 | dev = list_first_entry(&mid->bm_devlist, | |
779 | struct pnfs_block_dev, | |
780 | bm_node); | |
781 | list_del(&dev->bm_node); | |
782 | bl_free_block_dev(dev); | |
783 | } | |
784 | spin_unlock(&mid->bm_lock); | |
785 | kfree(mid); | |
786 | } | |
787 | } | |
788 | ||
789 | /* This is mostly copied from the filelayout's get_device_info function. | |
790 | * It seems much of this should be at the generic pnfs level. | |
791 | */ | |
792 | static struct pnfs_block_dev * | |
793 | nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh, | |
794 | struct nfs4_deviceid *d_id) | |
795 | { | |
796 | struct pnfs_device *dev; | |
516f2e24 | 797 | struct pnfs_block_dev *rv; |
2f9fd182 FI |
798 | u32 max_resp_sz; |
799 | int max_pages; | |
800 | struct page **pages = NULL; | |
801 | int i, rc; | |
802 | ||
803 | /* | |
804 | * Use the session max response size as the basis for setting | |
805 | * GETDEVICEINFO's maxcount | |
806 | */ | |
807 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
808 | max_pages = max_resp_sz >> PAGE_SHIFT; | |
809 | dprintk("%s max_resp_sz %u max_pages %d\n", | |
810 | __func__, max_resp_sz, max_pages); | |
811 | ||
812 | dev = kmalloc(sizeof(*dev), GFP_NOFS); | |
813 | if (!dev) { | |
814 | dprintk("%s kmalloc failed\n", __func__); | |
516f2e24 | 815 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
816 | } |
817 | ||
818 | pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS); | |
819 | if (pages == NULL) { | |
820 | kfree(dev); | |
516f2e24 | 821 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
822 | } |
823 | for (i = 0; i < max_pages; i++) { | |
824 | pages[i] = alloc_page(GFP_NOFS); | |
516f2e24 JR |
825 | if (!pages[i]) { |
826 | rv = ERR_PTR(-ENOMEM); | |
2f9fd182 | 827 | goto out_free; |
516f2e24 | 828 | } |
2f9fd182 FI |
829 | } |
830 | ||
831 | memcpy(&dev->dev_id, d_id, sizeof(*d_id)); | |
832 | dev->layout_type = LAYOUT_BLOCK_VOLUME; | |
833 | dev->pages = pages; | |
834 | dev->pgbase = 0; | |
835 | dev->pglen = PAGE_SIZE * max_pages; | |
836 | dev->mincount = 0; | |
837 | ||
838 | dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data); | |
839 | rc = nfs4_proc_getdeviceinfo(server, dev); | |
840 | dprintk("%s getdevice info returns %d\n", __func__, rc); | |
516f2e24 JR |
841 | if (rc) { |
842 | rv = ERR_PTR(rc); | |
2f9fd182 | 843 | goto out_free; |
516f2e24 | 844 | } |
2f9fd182 FI |
845 | |
846 | rv = nfs4_blk_decode_device(server, dev); | |
847 | out_free: | |
848 | for (i = 0; i < max_pages; i++) | |
849 | __free_page(pages[i]); | |
850 | kfree(pages); | |
851 | kfree(dev); | |
852 | return rv; | |
853 | } | |
854 | ||
155e7524 FI |
855 | static int |
856 | bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh) | |
857 | { | |
2f9fd182 FI |
858 | struct block_mount_id *b_mt_id = NULL; |
859 | struct pnfs_devicelist *dlist = NULL; | |
860 | struct pnfs_block_dev *bdev; | |
861 | LIST_HEAD(block_disklist); | |
516f2e24 | 862 | int status, i; |
2f9fd182 | 863 | |
155e7524 | 864 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
865 | |
866 | if (server->pnfs_blksize == 0) { | |
867 | dprintk("%s Server did not return blksize\n", __func__); | |
868 | return -EINVAL; | |
869 | } | |
870 | b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS); | |
871 | if (!b_mt_id) { | |
872 | status = -ENOMEM; | |
873 | goto out_error; | |
874 | } | |
875 | /* Initialize nfs4 block layout mount id */ | |
876 | spin_lock_init(&b_mt_id->bm_lock); | |
877 | INIT_LIST_HEAD(&b_mt_id->bm_devlist); | |
878 | ||
879 | dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS); | |
880 | if (!dlist) { | |
881 | status = -ENOMEM; | |
882 | goto out_error; | |
883 | } | |
884 | dlist->eof = 0; | |
885 | while (!dlist->eof) { | |
886 | status = nfs4_proc_getdevicelist(server, fh, dlist); | |
887 | if (status) | |
888 | goto out_error; | |
889 | dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n", | |
890 | __func__, dlist->num_devs, dlist->eof); | |
891 | for (i = 0; i < dlist->num_devs; i++) { | |
892 | bdev = nfs4_blk_get_deviceinfo(server, fh, | |
893 | &dlist->dev_id[i]); | |
516f2e24 JR |
894 | if (IS_ERR(bdev)) { |
895 | status = PTR_ERR(bdev); | |
2f9fd182 FI |
896 | goto out_error; |
897 | } | |
898 | spin_lock(&b_mt_id->bm_lock); | |
899 | list_add(&bdev->bm_node, &b_mt_id->bm_devlist); | |
900 | spin_unlock(&b_mt_id->bm_lock); | |
901 | } | |
902 | } | |
903 | dprintk("%s SUCCESS\n", __func__); | |
904 | server->pnfs_ld_data = b_mt_id; | |
905 | ||
906 | out_return: | |
907 | kfree(dlist); | |
908 | return status; | |
909 | ||
910 | out_error: | |
911 | free_blk_mountid(b_mt_id); | |
912 | goto out_return; | |
155e7524 FI |
913 | } |
914 | ||
915 | static int | |
916 | bl_clear_layoutdriver(struct nfs_server *server) | |
917 | { | |
2f9fd182 FI |
918 | struct block_mount_id *b_mt_id = server->pnfs_ld_data; |
919 | ||
155e7524 | 920 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
921 | free_blk_mountid(b_mt_id); |
922 | dprintk("%s RETURNS\n", __func__); | |
155e7524 FI |
923 | return 0; |
924 | } | |
925 | ||
e9643fe8 BH |
926 | static const struct nfs_pageio_ops bl_pg_read_ops = { |
927 | .pg_init = pnfs_generic_pg_init_read, | |
928 | .pg_test = pnfs_generic_pg_test, | |
929 | .pg_doio = pnfs_generic_pg_readpages, | |
930 | }; | |
931 | ||
932 | static const struct nfs_pageio_ops bl_pg_write_ops = { | |
933 | .pg_init = pnfs_generic_pg_init_write, | |
934 | .pg_test = pnfs_generic_pg_test, | |
935 | .pg_doio = pnfs_generic_pg_writepages, | |
936 | }; | |
937 | ||
155e7524 FI |
938 | static struct pnfs_layoutdriver_type blocklayout_type = { |
939 | .id = LAYOUT_BLOCK_VOLUME, | |
940 | .name = "LAYOUT_BLOCK_VOLUME", | |
941 | .read_pagelist = bl_read_pagelist, | |
942 | .write_pagelist = bl_write_pagelist, | |
943 | .alloc_layout_hdr = bl_alloc_layout_hdr, | |
944 | .free_layout_hdr = bl_free_layout_hdr, | |
945 | .alloc_lseg = bl_alloc_lseg, | |
946 | .free_lseg = bl_free_lseg, | |
947 | .encode_layoutcommit = bl_encode_layoutcommit, | |
948 | .cleanup_layoutcommit = bl_cleanup_layoutcommit, | |
949 | .set_layoutdriver = bl_set_layoutdriver, | |
950 | .clear_layoutdriver = bl_clear_layoutdriver, | |
e9643fe8 BH |
951 | .pg_read_ops = &bl_pg_read_ops, |
952 | .pg_write_ops = &bl_pg_write_ops, | |
155e7524 FI |
953 | }; |
954 | ||
fe0a9b74 | 955 | static const struct rpc_pipe_ops bl_upcall_ops = { |
c1225158 | 956 | .upcall = rpc_pipe_generic_upcall, |
fe0a9b74 JR |
957 | .downcall = bl_pipe_downcall, |
958 | .destroy_msg = bl_pipe_destroy_msg, | |
959 | }; | |
960 | ||
155e7524 FI |
961 | static int __init nfs4blocklayout_init(void) |
962 | { | |
fe0a9b74 JR |
963 | struct vfsmount *mnt; |
964 | struct path path; | |
155e7524 FI |
965 | int ret; |
966 | ||
967 | dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__); | |
968 | ||
969 | ret = pnfs_register_layoutdriver(&blocklayout_type); | |
fe0a9b74 JR |
970 | if (ret) |
971 | goto out; | |
972 | ||
973 | init_waitqueue_head(&bl_wq); | |
974 | ||
975 | mnt = rpc_get_mount(); | |
976 | if (IS_ERR(mnt)) { | |
977 | ret = PTR_ERR(mnt); | |
978 | goto out_remove; | |
979 | } | |
980 | ||
981 | ret = vfs_path_lookup(mnt->mnt_root, | |
982 | mnt, | |
983 | NFS_PIPE_DIRNAME, 0, &path); | |
984 | if (ret) | |
760383f1 | 985 | goto out_putrpc; |
fe0a9b74 JR |
986 | |
987 | bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL, | |
988 | &bl_upcall_ops, 0); | |
760383f1 | 989 | path_put(&path); |
fe0a9b74 JR |
990 | if (IS_ERR(bl_device_pipe)) { |
991 | ret = PTR_ERR(bl_device_pipe); | |
760383f1 | 992 | goto out_putrpc; |
fe0a9b74 JR |
993 | } |
994 | out: | |
995 | return ret; | |
996 | ||
760383f1 PT |
997 | out_putrpc: |
998 | rpc_put_mount(); | |
fe0a9b74 JR |
999 | out_remove: |
1000 | pnfs_unregister_layoutdriver(&blocklayout_type); | |
155e7524 FI |
1001 | return ret; |
1002 | } | |
1003 | ||
1004 | static void __exit nfs4blocklayout_exit(void) | |
1005 | { | |
1006 | dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n", | |
1007 | __func__); | |
1008 | ||
1009 | pnfs_unregister_layoutdriver(&blocklayout_type); | |
fe0a9b74 | 1010 | rpc_unlink(bl_device_pipe); |
760383f1 | 1011 | rpc_put_mount(); |
155e7524 FI |
1012 | } |
1013 | ||
1014 | MODULE_ALIAS("nfs-layouttype4-3"); | |
1015 | ||
1016 | module_init(nfs4blocklayout_init); | |
1017 | module_exit(nfs4blocklayout_exit); |