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 | ||
9549ec01 FI |
49 | static void print_page(struct page *page) |
50 | { | |
51 | dprintk("PRINTPAGE page %p\n", page); | |
52 | dprintk(" PagePrivate %d\n", PagePrivate(page)); | |
53 | dprintk(" PageUptodate %d\n", PageUptodate(page)); | |
54 | dprintk(" PageError %d\n", PageError(page)); | |
55 | dprintk(" PageDirty %d\n", PageDirty(page)); | |
56 | dprintk(" PageReferenced %d\n", PageReferenced(page)); | |
57 | dprintk(" PageLocked %d\n", PageLocked(page)); | |
58 | dprintk(" PageWriteback %d\n", PageWriteback(page)); | |
59 | dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page)); | |
60 | dprintk("\n"); | |
61 | } | |
62 | ||
63 | /* Given the be associated with isect, determine if page data needs to be | |
64 | * initialized. | |
65 | */ | |
66 | static int is_hole(struct pnfs_block_extent *be, sector_t isect) | |
67 | { | |
68 | if (be->be_state == PNFS_BLOCK_NONE_DATA) | |
69 | return 1; | |
70 | else if (be->be_state != PNFS_BLOCK_INVALID_DATA) | |
71 | return 0; | |
72 | else | |
73 | return !bl_is_sector_init(be->be_inval, isect); | |
74 | } | |
75 | ||
650e2d39 FI |
76 | /* Given the be associated with isect, determine if page data can be |
77 | * written to disk. | |
78 | */ | |
79 | static int is_writable(struct pnfs_block_extent *be, sector_t isect) | |
80 | { | |
71cdd40f PT |
81 | return (be->be_state == PNFS_BLOCK_READWRITE_DATA || |
82 | be->be_state == PNFS_BLOCK_INVALID_DATA); | |
650e2d39 FI |
83 | } |
84 | ||
9549ec01 FI |
85 | /* The data we are handed might be spread across several bios. We need |
86 | * to track when the last one is finished. | |
87 | */ | |
88 | struct parallel_io { | |
89 | struct kref refcnt; | |
7c5465d6 | 90 | void (*pnfs_callback) (void *data, int num_se); |
9549ec01 | 91 | void *data; |
7c5465d6 | 92 | int bse_count; |
9549ec01 FI |
93 | }; |
94 | ||
95 | static inline struct parallel_io *alloc_parallel(void *data) | |
96 | { | |
97 | struct parallel_io *rv; | |
98 | ||
99 | rv = kmalloc(sizeof(*rv), GFP_NOFS); | |
100 | if (rv) { | |
101 | rv->data = data; | |
102 | kref_init(&rv->refcnt); | |
7c5465d6 | 103 | rv->bse_count = 0; |
9549ec01 FI |
104 | } |
105 | return rv; | |
106 | } | |
107 | ||
108 | static inline void get_parallel(struct parallel_io *p) | |
109 | { | |
110 | kref_get(&p->refcnt); | |
111 | } | |
112 | ||
113 | static void destroy_parallel(struct kref *kref) | |
114 | { | |
115 | struct parallel_io *p = container_of(kref, struct parallel_io, refcnt); | |
116 | ||
117 | dprintk("%s enter\n", __func__); | |
7c5465d6 | 118 | p->pnfs_callback(p->data, p->bse_count); |
9549ec01 FI |
119 | kfree(p); |
120 | } | |
121 | ||
122 | static inline void put_parallel(struct parallel_io *p) | |
123 | { | |
124 | kref_put(&p->refcnt, destroy_parallel); | |
125 | } | |
126 | ||
127 | static struct bio * | |
128 | bl_submit_bio(int rw, struct bio *bio) | |
129 | { | |
130 | if (bio) { | |
131 | get_parallel(bio->bi_private); | |
132 | dprintk("%s submitting %s bio %u@%llu\n", __func__, | |
133 | rw == READ ? "read" : "write", | |
134 | bio->bi_size, (unsigned long long)bio->bi_sector); | |
135 | submit_bio(rw, bio); | |
136 | } | |
137 | return NULL; | |
138 | } | |
139 | ||
140 | static struct bio *bl_alloc_init_bio(int npg, sector_t isect, | |
141 | struct pnfs_block_extent *be, | |
142 | void (*end_io)(struct bio *, int err), | |
143 | struct parallel_io *par) | |
144 | { | |
145 | struct bio *bio; | |
146 | ||
74a6eeb4 | 147 | npg = min(npg, BIO_MAX_PAGES); |
9549ec01 | 148 | bio = bio_alloc(GFP_NOIO, npg); |
74a6eeb4 PT |
149 | if (!bio && (current->flags & PF_MEMALLOC)) { |
150 | while (!bio && (npg /= 2)) | |
151 | bio = bio_alloc(GFP_NOIO, npg); | |
152 | } | |
9549ec01 | 153 | |
74a6eeb4 PT |
154 | if (bio) { |
155 | bio->bi_sector = isect - be->be_f_offset + be->be_v_offset; | |
156 | bio->bi_bdev = be->be_mdev; | |
157 | bio->bi_end_io = end_io; | |
158 | bio->bi_private = par; | |
159 | } | |
9549ec01 FI |
160 | return bio; |
161 | } | |
162 | ||
163 | static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw, | |
164 | sector_t isect, struct page *page, | |
165 | struct pnfs_block_extent *be, | |
166 | void (*end_io)(struct bio *, int err), | |
167 | struct parallel_io *par) | |
168 | { | |
169 | retry: | |
170 | if (!bio) { | |
171 | bio = bl_alloc_init_bio(npg, isect, be, end_io, par); | |
172 | if (!bio) | |
173 | return ERR_PTR(-ENOMEM); | |
174 | } | |
175 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { | |
176 | bio = bl_submit_bio(rw, bio); | |
177 | goto retry; | |
178 | } | |
179 | return bio; | |
180 | } | |
181 | ||
9549ec01 FI |
182 | /* This is basically copied from mpage_end_io_read */ |
183 | static void bl_end_io_read(struct bio *bio, int err) | |
184 | { | |
185 | struct parallel_io *par = bio->bi_private; | |
186 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
187 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
188 | struct nfs_read_data *rdata = (struct nfs_read_data *)par->data; | |
189 | ||
190 | do { | |
191 | struct page *page = bvec->bv_page; | |
192 | ||
193 | if (--bvec >= bio->bi_io_vec) | |
194 | prefetchw(&bvec->bv_page->flags); | |
195 | if (uptodate) | |
196 | SetPageUptodate(page); | |
197 | } while (bvec >= bio->bi_io_vec); | |
198 | if (!uptodate) { | |
199 | if (!rdata->pnfs_error) | |
200 | rdata->pnfs_error = -EIO; | |
1b0ae068 | 201 | pnfs_set_lo_fail(rdata->lseg); |
9549ec01 FI |
202 | } |
203 | bio_put(bio); | |
204 | put_parallel(par); | |
205 | } | |
206 | ||
207 | static void bl_read_cleanup(struct work_struct *work) | |
208 | { | |
209 | struct rpc_task *task; | |
210 | struct nfs_read_data *rdata; | |
211 | dprintk("%s enter\n", __func__); | |
212 | task = container_of(work, struct rpc_task, u.tk_work); | |
213 | rdata = container_of(task, struct nfs_read_data, task); | |
214 | pnfs_ld_read_done(rdata); | |
215 | } | |
216 | ||
217 | static void | |
7c5465d6 | 218 | bl_end_par_io_read(void *data, int unused) |
9549ec01 FI |
219 | { |
220 | struct nfs_read_data *rdata = data; | |
221 | ||
82b906d6 | 222 | rdata->task.tk_status = rdata->pnfs_error; |
9549ec01 FI |
223 | INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup); |
224 | schedule_work(&rdata->task.u.tk_work); | |
225 | } | |
226 | ||
155e7524 FI |
227 | static enum pnfs_try_status |
228 | bl_read_pagelist(struct nfs_read_data *rdata) | |
229 | { | |
9549ec01 FI |
230 | int i, hole; |
231 | struct bio *bio = NULL; | |
232 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; | |
233 | sector_t isect, extent_length = 0; | |
234 | struct parallel_io *par; | |
235 | loff_t f_offset = rdata->args.offset; | |
236 | size_t count = rdata->args.count; | |
237 | struct page **pages = rdata->args.pages; | |
238 | int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
239 | ||
240 | dprintk("%s enter nr_pages %u offset %lld count %Zd\n", __func__, | |
241 | rdata->npages, f_offset, count); | |
242 | ||
243 | par = alloc_parallel(rdata); | |
244 | if (!par) | |
245 | goto use_mds; | |
9549ec01 FI |
246 | par->pnfs_callback = bl_end_par_io_read; |
247 | /* At this point, we can no longer jump to use_mds */ | |
248 | ||
249 | isect = (sector_t) (f_offset >> SECTOR_SHIFT); | |
250 | /* Code assumes extents are page-aligned */ | |
251 | for (i = pg_index; i < rdata->npages; i++) { | |
252 | if (!extent_length) { | |
253 | /* We've used up the previous extent */ | |
254 | bl_put_extent(be); | |
255 | bl_put_extent(cow_read); | |
256 | bio = bl_submit_bio(READ, bio); | |
257 | /* Get the next one */ | |
258 | be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg), | |
259 | isect, &cow_read); | |
260 | if (!be) { | |
261 | rdata->pnfs_error = -EIO; | |
262 | goto out; | |
263 | } | |
264 | extent_length = be->be_length - | |
265 | (isect - be->be_f_offset); | |
266 | if (cow_read) { | |
267 | sector_t cow_length = cow_read->be_length - | |
268 | (isect - cow_read->be_f_offset); | |
269 | extent_length = min(extent_length, cow_length); | |
270 | } | |
271 | } | |
272 | hole = is_hole(be, isect); | |
273 | if (hole && !cow_read) { | |
274 | bio = bl_submit_bio(READ, bio); | |
275 | /* Fill hole w/ zeroes w/o accessing device */ | |
276 | dprintk("%s Zeroing page for hole\n", __func__); | |
277 | zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE); | |
278 | print_page(pages[i]); | |
279 | SetPageUptodate(pages[i]); | |
280 | } else { | |
281 | struct pnfs_block_extent *be_read; | |
282 | ||
283 | be_read = (hole && cow_read) ? cow_read : be; | |
284 | bio = bl_add_page_to_bio(bio, rdata->npages - i, READ, | |
285 | isect, pages[i], be_read, | |
286 | bl_end_io_read, par); | |
287 | if (IS_ERR(bio)) { | |
288 | rdata->pnfs_error = PTR_ERR(bio); | |
e6d05a75 | 289 | bio = NULL; |
9549ec01 FI |
290 | goto out; |
291 | } | |
292 | } | |
293 | isect += PAGE_CACHE_SECTORS; | |
294 | extent_length -= PAGE_CACHE_SECTORS; | |
295 | } | |
296 | if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) { | |
297 | rdata->res.eof = 1; | |
298 | rdata->res.count = rdata->inode->i_size - f_offset; | |
299 | } else { | |
300 | rdata->res.count = (isect << SECTOR_SHIFT) - f_offset; | |
301 | } | |
302 | out: | |
303 | bl_put_extent(be); | |
304 | bl_put_extent(cow_read); | |
305 | bl_submit_bio(READ, bio); | |
306 | put_parallel(par); | |
307 | return PNFS_ATTEMPTED; | |
308 | ||
309 | use_mds: | |
310 | dprintk("Giving up and using normal NFS\n"); | |
155e7524 FI |
311 | return PNFS_NOT_ATTEMPTED; |
312 | } | |
313 | ||
31e6306a FI |
314 | static void mark_extents_written(struct pnfs_block_layout *bl, |
315 | __u64 offset, __u32 count) | |
316 | { | |
317 | sector_t isect, end; | |
318 | struct pnfs_block_extent *be; | |
7c5465d6 | 319 | struct pnfs_block_short_extent *se; |
31e6306a FI |
320 | |
321 | dprintk("%s(%llu, %u)\n", __func__, offset, count); | |
322 | if (count == 0) | |
323 | return; | |
324 | isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT; | |
325 | end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK); | |
326 | end >>= SECTOR_SHIFT; | |
327 | while (isect < end) { | |
328 | sector_t len; | |
329 | be = bl_find_get_extent(bl, isect, NULL); | |
330 | BUG_ON(!be); /* FIXME */ | |
331 | len = min(end, be->be_f_offset + be->be_length) - isect; | |
7c5465d6 PT |
332 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
333 | se = bl_pop_one_short_extent(be->be_inval); | |
334 | BUG_ON(!se); | |
335 | bl_mark_for_commit(be, isect, len, se); | |
336 | } | |
31e6306a FI |
337 | isect += len; |
338 | bl_put_extent(be); | |
339 | } | |
340 | } | |
341 | ||
71cdd40f PT |
342 | static void bl_end_io_write_zero(struct bio *bio, int err) |
343 | { | |
344 | struct parallel_io *par = bio->bi_private; | |
345 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
346 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
347 | struct nfs_write_data *wdata = (struct nfs_write_data *)par->data; | |
348 | ||
349 | do { | |
350 | struct page *page = bvec->bv_page; | |
351 | ||
352 | if (--bvec >= bio->bi_io_vec) | |
353 | prefetchw(&bvec->bv_page->flags); | |
354 | /* This is the zeroing page we added */ | |
355 | end_page_writeback(page); | |
356 | page_cache_release(page); | |
357 | } while (bvec >= bio->bi_io_vec); | |
7c5465d6 PT |
358 | |
359 | if (unlikely(!uptodate)) { | |
71cdd40f PT |
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 | static void bl_end_io_write(struct bio *bio, int err) |
369 | { | |
370 | struct parallel_io *par = bio->bi_private; | |
371 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
372 | struct nfs_write_data *wdata = (struct nfs_write_data *)par->data; | |
373 | ||
374 | if (!uptodate) { | |
375 | if (!wdata->pnfs_error) | |
376 | wdata->pnfs_error = -EIO; | |
1b0ae068 | 377 | pnfs_set_lo_fail(wdata->lseg); |
650e2d39 FI |
378 | } |
379 | bio_put(bio); | |
380 | put_parallel(par); | |
381 | } | |
382 | ||
383 | /* Function scheduled for call during bl_end_par_io_write, | |
384 | * it marks sectors as written and extends the commitlist. | |
385 | */ | |
386 | static void bl_write_cleanup(struct work_struct *work) | |
387 | { | |
388 | struct rpc_task *task; | |
389 | struct nfs_write_data *wdata; | |
390 | dprintk("%s enter\n", __func__); | |
391 | task = container_of(work, struct rpc_task, u.tk_work); | |
392 | wdata = container_of(task, struct nfs_write_data, task); | |
7c5465d6 | 393 | if (likely(!wdata->pnfs_error)) { |
31e6306a | 394 | /* Marks for LAYOUTCOMMIT */ |
31e6306a FI |
395 | mark_extents_written(BLK_LSEG2EXT(wdata->lseg), |
396 | wdata->args.offset, wdata->args.count); | |
397 | } | |
650e2d39 FI |
398 | pnfs_ld_write_done(wdata); |
399 | } | |
400 | ||
401 | /* Called when last of bios associated with a bl_write_pagelist call finishes */ | |
7c5465d6 | 402 | static void bl_end_par_io_write(void *data, int num_se) |
650e2d39 FI |
403 | { |
404 | struct nfs_write_data *wdata = data; | |
405 | ||
7c5465d6 PT |
406 | if (unlikely(wdata->pnfs_error)) { |
407 | bl_free_short_extents(&BLK_LSEG2EXT(wdata->lseg)->bl_inval, | |
408 | num_se); | |
409 | } | |
410 | ||
82b906d6 | 411 | wdata->task.tk_status = wdata->pnfs_error; |
650e2d39 FI |
412 | wdata->verf.committed = NFS_FILE_SYNC; |
413 | INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup); | |
414 | schedule_work(&wdata->task.u.tk_work); | |
415 | } | |
416 | ||
71cdd40f PT |
417 | /* FIXME STUB - mark intersection of layout and page as bad, so is not |
418 | * used again. | |
419 | */ | |
420 | static void mark_bad_read(void) | |
421 | { | |
422 | return; | |
423 | } | |
424 | ||
425 | /* | |
426 | * map_block: map a requested I/0 block (isect) into an offset in the LVM | |
427 | * block_device | |
428 | */ | |
429 | static void | |
430 | map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be) | |
431 | { | |
432 | dprintk("%s enter be=%p\n", __func__, be); | |
433 | ||
434 | set_buffer_mapped(bh); | |
435 | bh->b_bdev = be->be_mdev; | |
436 | bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >> | |
437 | (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT); | |
438 | ||
439 | dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n", | |
440 | __func__, (unsigned long long)isect, (long)bh->b_blocknr, | |
441 | bh->b_size); | |
442 | return; | |
443 | } | |
444 | ||
445 | /* Given an unmapped page, zero it or read in page for COW, page is locked | |
446 | * by caller. | |
447 | */ | |
448 | static int | |
449 | init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read) | |
450 | { | |
451 | struct buffer_head *bh = NULL; | |
452 | int ret = 0; | |
453 | sector_t isect; | |
454 | ||
455 | dprintk("%s enter, %p\n", __func__, page); | |
456 | BUG_ON(PageUptodate(page)); | |
457 | if (!cow_read) { | |
458 | zero_user_segment(page, 0, PAGE_SIZE); | |
459 | SetPageUptodate(page); | |
460 | goto cleanup; | |
461 | } | |
462 | ||
463 | bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0); | |
464 | if (!bh) { | |
465 | ret = -ENOMEM; | |
466 | goto cleanup; | |
467 | } | |
468 | ||
469 | isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT; | |
470 | map_block(bh, isect, cow_read); | |
471 | if (!bh_uptodate_or_lock(bh)) | |
472 | ret = bh_submit_read(bh); | |
473 | if (ret) | |
474 | goto cleanup; | |
475 | SetPageUptodate(page); | |
476 | ||
477 | cleanup: | |
478 | bl_put_extent(cow_read); | |
479 | if (bh) | |
480 | free_buffer_head(bh); | |
481 | if (ret) { | |
482 | /* Need to mark layout with bad read...should now | |
483 | * just use nfs4 for reads and writes. | |
484 | */ | |
485 | mark_bad_read(); | |
486 | } | |
487 | return ret; | |
488 | } | |
489 | ||
72c50887 PT |
490 | /* Find or create a zeroing page marked being writeback. |
491 | * Return ERR_PTR on error, NULL to indicate skip this page and page itself | |
492 | * to indicate write out. | |
493 | */ | |
494 | static struct page * | |
495 | bl_find_get_zeroing_page(struct inode *inode, pgoff_t index, | |
496 | struct pnfs_block_extent *cow_read) | |
497 | { | |
498 | struct page *page; | |
499 | int locked = 0; | |
500 | page = find_get_page(inode->i_mapping, index); | |
501 | if (page) | |
502 | goto check_page; | |
503 | ||
504 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | |
505 | if (unlikely(!page)) { | |
506 | dprintk("%s oom\n", __func__); | |
507 | return ERR_PTR(-ENOMEM); | |
508 | } | |
509 | locked = 1; | |
510 | ||
511 | check_page: | |
512 | /* PageDirty: Other will write this out | |
513 | * PageWriteback: Other is writing this out | |
514 | * PageUptodate: It was read before | |
515 | */ | |
516 | if (PageDirty(page) || PageWriteback(page)) { | |
517 | print_page(page); | |
518 | if (locked) | |
519 | unlock_page(page); | |
520 | page_cache_release(page); | |
521 | return NULL; | |
522 | } | |
523 | ||
524 | if (!locked) { | |
525 | lock_page(page); | |
526 | locked = 1; | |
527 | goto check_page; | |
528 | } | |
529 | if (!PageUptodate(page)) { | |
530 | /* New page, readin or zero it */ | |
531 | init_page_for_write(page, cow_read); | |
532 | } | |
533 | set_page_writeback(page); | |
534 | unlock_page(page); | |
535 | ||
536 | return page; | |
537 | } | |
538 | ||
155e7524 | 539 | static enum pnfs_try_status |
650e2d39 | 540 | bl_write_pagelist(struct nfs_write_data *wdata, int sync) |
155e7524 | 541 | { |
71cdd40f | 542 | int i, ret, npg_zero, pg_index, last = 0; |
650e2d39 | 543 | struct bio *bio = NULL; |
71cdd40f PT |
544 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; |
545 | sector_t isect, last_isect = 0, extent_length = 0; | |
650e2d39 FI |
546 | struct parallel_io *par; |
547 | loff_t offset = wdata->args.offset; | |
548 | size_t count = wdata->args.count; | |
549 | struct page **pages = wdata->args.pages; | |
71cdd40f PT |
550 | struct page *page; |
551 | pgoff_t index; | |
552 | u64 temp; | |
553 | int npg_per_block = | |
554 | NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT; | |
650e2d39 FI |
555 | |
556 | dprintk("%s enter, %Zu@%lld\n", __func__, count, offset); | |
557 | /* At this point, wdata->pages is a (sequential) list of nfs_pages. | |
71cdd40f PT |
558 | * We want to write each, and if there is an error set pnfs_error |
559 | * to have it redone using nfs. | |
650e2d39 FI |
560 | */ |
561 | par = alloc_parallel(wdata); | |
562 | if (!par) | |
7c5465d6 | 563 | goto out_mds; |
650e2d39 FI |
564 | par->pnfs_callback = bl_end_par_io_write; |
565 | /* At this point, have to be more careful with error handling */ | |
566 | ||
567 | isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
71cdd40f PT |
568 | be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read); |
569 | if (!be || !is_writable(be, isect)) { | |
570 | dprintk("%s no matching extents!\n", __func__); | |
7c5465d6 | 571 | goto out_mds; |
71cdd40f PT |
572 | } |
573 | ||
574 | /* First page inside INVALID extent */ | |
575 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
7c5465d6 PT |
576 | if (likely(!bl_push_one_short_extent(be->be_inval))) |
577 | par->bse_count++; | |
578 | else | |
579 | goto out_mds; | |
71cdd40f PT |
580 | temp = offset >> PAGE_CACHE_SHIFT; |
581 | npg_zero = do_div(temp, npg_per_block); | |
582 | isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) & | |
583 | (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
584 | extent_length = be->be_length - (isect - be->be_f_offset); | |
585 | ||
586 | fill_invalid_ext: | |
587 | dprintk("%s need to zero %d pages\n", __func__, npg_zero); | |
588 | for (;npg_zero > 0; npg_zero--) { | |
75422745 PT |
589 | if (bl_is_sector_init(be->be_inval, isect)) { |
590 | dprintk("isect %llu already init\n", | |
591 | (unsigned long long)isect); | |
592 | goto next_page; | |
593 | } | |
71cdd40f PT |
594 | /* page ref released in bl_end_io_write_zero */ |
595 | index = isect >> PAGE_CACHE_SECTOR_SHIFT; | |
596 | dprintk("%s zero %dth page: index %lu isect %llu\n", | |
597 | __func__, npg_zero, index, | |
598 | (unsigned long long)isect); | |
72c50887 PT |
599 | page = bl_find_get_zeroing_page(wdata->inode, index, |
600 | cow_read); | |
601 | if (unlikely(IS_ERR(page))) { | |
602 | wdata->pnfs_error = PTR_ERR(page); | |
71cdd40f | 603 | goto out; |
72c50887 | 604 | } else if (page == NULL) |
71cdd40f | 605 | goto next_page; |
71cdd40f PT |
606 | |
607 | ret = bl_mark_sectors_init(be->be_inval, isect, | |
60c52e3a | 608 | PAGE_CACHE_SECTORS); |
71cdd40f PT |
609 | if (unlikely(ret)) { |
610 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
611 | __func__, ret); | |
612 | end_page_writeback(page); | |
613 | page_cache_release(page); | |
614 | wdata->pnfs_error = ret; | |
615 | goto out; | |
616 | } | |
7c5465d6 PT |
617 | if (likely(!bl_push_one_short_extent(be->be_inval))) |
618 | par->bse_count++; | |
619 | else { | |
620 | end_page_writeback(page); | |
621 | page_cache_release(page); | |
622 | wdata->pnfs_error = -ENOMEM; | |
623 | goto out; | |
624 | } | |
625 | /* FIXME: This should be done in bi_end_io */ | |
626 | mark_extents_written(BLK_LSEG2EXT(wdata->lseg), | |
627 | page->index << PAGE_CACHE_SHIFT, | |
628 | PAGE_CACHE_SIZE); | |
629 | ||
71cdd40f PT |
630 | bio = bl_add_page_to_bio(bio, npg_zero, WRITE, |
631 | isect, page, be, | |
632 | bl_end_io_write_zero, par); | |
633 | if (IS_ERR(bio)) { | |
634 | wdata->pnfs_error = PTR_ERR(bio); | |
e6d05a75 | 635 | bio = NULL; |
71cdd40f PT |
636 | goto out; |
637 | } | |
71cdd40f PT |
638 | next_page: |
639 | isect += PAGE_CACHE_SECTORS; | |
640 | extent_length -= PAGE_CACHE_SECTORS; | |
641 | } | |
642 | if (last) | |
643 | goto write_done; | |
644 | } | |
645 | bio = bl_submit_bio(WRITE, bio); | |
646 | ||
647 | /* Middle pages */ | |
648 | pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
649 | for (i = pg_index; i < wdata->npages; i++) { | |
650e2d39 FI |
650 | if (!extent_length) { |
651 | /* We've used up the previous extent */ | |
652 | bl_put_extent(be); | |
653 | bio = bl_submit_bio(WRITE, bio); | |
654 | /* Get the next one */ | |
655 | be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), | |
656 | isect, NULL); | |
657 | if (!be || !is_writable(be, isect)) { | |
71cdd40f | 658 | wdata->pnfs_error = -EINVAL; |
650e2d39 FI |
659 | goto out; |
660 | } | |
7c5465d6 PT |
661 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
662 | if (likely(!bl_push_one_short_extent( | |
663 | be->be_inval))) | |
664 | par->bse_count++; | |
665 | else { | |
666 | wdata->pnfs_error = -ENOMEM; | |
667 | goto out; | |
668 | } | |
669 | } | |
650e2d39 | 670 | extent_length = be->be_length - |
71cdd40f | 671 | (isect - be->be_f_offset); |
650e2d39 | 672 | } |
71cdd40f PT |
673 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
674 | ret = bl_mark_sectors_init(be->be_inval, isect, | |
60c52e3a | 675 | PAGE_CACHE_SECTORS); |
71cdd40f PT |
676 | if (unlikely(ret)) { |
677 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
678 | __func__, ret); | |
679 | wdata->pnfs_error = ret; | |
680 | goto out; | |
650e2d39 | 681 | } |
71cdd40f PT |
682 | } |
683 | bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE, | |
684 | isect, pages[i], be, | |
685 | bl_end_io_write, par); | |
686 | if (IS_ERR(bio)) { | |
687 | wdata->pnfs_error = PTR_ERR(bio); | |
e6d05a75 | 688 | bio = NULL; |
71cdd40f | 689 | goto out; |
650e2d39 FI |
690 | } |
691 | isect += PAGE_CACHE_SECTORS; | |
71cdd40f | 692 | last_isect = isect; |
650e2d39 FI |
693 | extent_length -= PAGE_CACHE_SECTORS; |
694 | } | |
71cdd40f PT |
695 | |
696 | /* Last page inside INVALID extent */ | |
697 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
698 | bio = bl_submit_bio(WRITE, bio); | |
699 | temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT; | |
700 | npg_zero = npg_per_block - do_div(temp, npg_per_block); | |
701 | if (npg_zero < npg_per_block) { | |
702 | last = 1; | |
703 | goto fill_invalid_ext; | |
704 | } | |
705 | } | |
706 | ||
707 | write_done: | |
708 | wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset); | |
709 | if (count < wdata->res.count) { | |
650e2d39 | 710 | wdata->res.count = count; |
71cdd40f | 711 | } |
650e2d39 FI |
712 | out: |
713 | bl_put_extent(be); | |
714 | bl_submit_bio(WRITE, bio); | |
715 | put_parallel(par); | |
716 | return PNFS_ATTEMPTED; | |
7c5465d6 PT |
717 | out_mds: |
718 | bl_put_extent(be); | |
719 | kfree(par); | |
720 | return PNFS_NOT_ATTEMPTED; | |
155e7524 FI |
721 | } |
722 | ||
9e692969 | 723 | /* FIXME - range ignored */ |
155e7524 | 724 | static void |
9e692969 | 725 | release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range) |
155e7524 | 726 | { |
9e692969 FI |
727 | int i; |
728 | struct pnfs_block_extent *be; | |
729 | ||
730 | spin_lock(&bl->bl_ext_lock); | |
731 | for (i = 0; i < EXTENT_LISTS; i++) { | |
732 | while (!list_empty(&bl->bl_extents[i])) { | |
733 | be = list_first_entry(&bl->bl_extents[i], | |
734 | struct pnfs_block_extent, | |
735 | be_node); | |
736 | list_del(&be->be_node); | |
737 | bl_put_extent(be); | |
738 | } | |
739 | } | |
740 | spin_unlock(&bl->bl_ext_lock); | |
155e7524 FI |
741 | } |
742 | ||
155e7524 FI |
743 | static void |
744 | release_inval_marks(struct pnfs_inval_markings *marks) | |
745 | { | |
c1c2a4cd | 746 | struct pnfs_inval_tracking *pos, *temp; |
7c5465d6 | 747 | struct pnfs_block_short_extent *se, *stemp; |
c1c2a4cd FI |
748 | |
749 | list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) { | |
750 | list_del(&pos->it_link); | |
751 | kfree(pos); | |
752 | } | |
7c5465d6 PT |
753 | |
754 | list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) { | |
755 | list_del(&se->bse_node); | |
756 | kfree(se); | |
757 | } | |
155e7524 FI |
758 | return; |
759 | } | |
760 | ||
761 | static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
762 | { | |
763 | struct pnfs_block_layout *bl = BLK_LO2EXT(lo); | |
764 | ||
765 | dprintk("%s enter\n", __func__); | |
766 | release_extents(bl, NULL); | |
767 | release_inval_marks(&bl->bl_inval); | |
768 | kfree(bl); | |
769 | } | |
770 | ||
771 | static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode, | |
772 | gfp_t gfp_flags) | |
773 | { | |
774 | struct pnfs_block_layout *bl; | |
775 | ||
776 | dprintk("%s enter\n", __func__); | |
777 | bl = kzalloc(sizeof(*bl), gfp_flags); | |
778 | if (!bl) | |
779 | return NULL; | |
780 | spin_lock_init(&bl->bl_ext_lock); | |
781 | INIT_LIST_HEAD(&bl->bl_extents[0]); | |
782 | INIT_LIST_HEAD(&bl->bl_extents[1]); | |
783 | INIT_LIST_HEAD(&bl->bl_commit); | |
784 | INIT_LIST_HEAD(&bl->bl_committing); | |
785 | bl->bl_count = 0; | |
786 | bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT; | |
787 | BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize); | |
788 | return &bl->bl_layout; | |
789 | } | |
790 | ||
a60d2ebd | 791 | static void bl_free_lseg(struct pnfs_layout_segment *lseg) |
155e7524 | 792 | { |
a60d2ebd FI |
793 | dprintk("%s enter\n", __func__); |
794 | kfree(lseg); | |
155e7524 FI |
795 | } |
796 | ||
a60d2ebd FI |
797 | /* We pretty much ignore lseg, and store all data layout wide, so we |
798 | * can correctly merge. | |
799 | */ | |
800 | static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo, | |
801 | struct nfs4_layoutget_res *lgr, | |
802 | gfp_t gfp_flags) | |
155e7524 | 803 | { |
a60d2ebd FI |
804 | struct pnfs_layout_segment *lseg; |
805 | int status; | |
806 | ||
807 | dprintk("%s enter\n", __func__); | |
808 | lseg = kzalloc(sizeof(*lseg), gfp_flags); | |
809 | if (!lseg) | |
810 | return ERR_PTR(-ENOMEM); | |
811 | status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags); | |
812 | if (status) { | |
813 | /* We don't want to call the full-blown bl_free_lseg, | |
814 | * since on error extents were not touched. | |
815 | */ | |
816 | kfree(lseg); | |
817 | return ERR_PTR(status); | |
818 | } | |
819 | return lseg; | |
155e7524 FI |
820 | } |
821 | ||
822 | static void | |
823 | bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr, | |
824 | const struct nfs4_layoutcommit_args *arg) | |
825 | { | |
90ace12a FI |
826 | dprintk("%s enter\n", __func__); |
827 | encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg); | |
155e7524 FI |
828 | } |
829 | ||
830 | static void | |
831 | bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata) | |
832 | { | |
b2be7811 FI |
833 | struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout; |
834 | ||
835 | dprintk("%s enter\n", __func__); | |
836 | clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status); | |
155e7524 FI |
837 | } |
838 | ||
2f9fd182 FI |
839 | static void free_blk_mountid(struct block_mount_id *mid) |
840 | { | |
841 | if (mid) { | |
93a3844e PT |
842 | struct pnfs_block_dev *dev, *tmp; |
843 | ||
844 | /* No need to take bm_lock as we are last user freeing bm_devlist */ | |
845 | list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) { | |
2f9fd182 FI |
846 | list_del(&dev->bm_node); |
847 | bl_free_block_dev(dev); | |
848 | } | |
2f9fd182 FI |
849 | kfree(mid); |
850 | } | |
851 | } | |
852 | ||
853 | /* This is mostly copied from the filelayout's get_device_info function. | |
854 | * It seems much of this should be at the generic pnfs level. | |
855 | */ | |
856 | static struct pnfs_block_dev * | |
857 | nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh, | |
858 | struct nfs4_deviceid *d_id) | |
859 | { | |
860 | struct pnfs_device *dev; | |
516f2e24 | 861 | struct pnfs_block_dev *rv; |
2f9fd182 FI |
862 | u32 max_resp_sz; |
863 | int max_pages; | |
864 | struct page **pages = NULL; | |
865 | int i, rc; | |
866 | ||
867 | /* | |
868 | * Use the session max response size as the basis for setting | |
869 | * GETDEVICEINFO's maxcount | |
870 | */ | |
871 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
872 | max_pages = max_resp_sz >> PAGE_SHIFT; | |
873 | dprintk("%s max_resp_sz %u max_pages %d\n", | |
874 | __func__, max_resp_sz, max_pages); | |
875 | ||
876 | dev = kmalloc(sizeof(*dev), GFP_NOFS); | |
877 | if (!dev) { | |
878 | dprintk("%s kmalloc failed\n", __func__); | |
516f2e24 | 879 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
880 | } |
881 | ||
882 | pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS); | |
883 | if (pages == NULL) { | |
884 | kfree(dev); | |
516f2e24 | 885 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
886 | } |
887 | for (i = 0; i < max_pages; i++) { | |
888 | pages[i] = alloc_page(GFP_NOFS); | |
516f2e24 JR |
889 | if (!pages[i]) { |
890 | rv = ERR_PTR(-ENOMEM); | |
2f9fd182 | 891 | goto out_free; |
516f2e24 | 892 | } |
2f9fd182 FI |
893 | } |
894 | ||
895 | memcpy(&dev->dev_id, d_id, sizeof(*d_id)); | |
896 | dev->layout_type = LAYOUT_BLOCK_VOLUME; | |
897 | dev->pages = pages; | |
898 | dev->pgbase = 0; | |
899 | dev->pglen = PAGE_SIZE * max_pages; | |
900 | dev->mincount = 0; | |
901 | ||
902 | dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data); | |
903 | rc = nfs4_proc_getdeviceinfo(server, dev); | |
904 | dprintk("%s getdevice info returns %d\n", __func__, rc); | |
516f2e24 JR |
905 | if (rc) { |
906 | rv = ERR_PTR(rc); | |
2f9fd182 | 907 | goto out_free; |
516f2e24 | 908 | } |
2f9fd182 FI |
909 | |
910 | rv = nfs4_blk_decode_device(server, dev); | |
911 | out_free: | |
912 | for (i = 0; i < max_pages; i++) | |
913 | __free_page(pages[i]); | |
914 | kfree(pages); | |
915 | kfree(dev); | |
916 | return rv; | |
917 | } | |
918 | ||
155e7524 FI |
919 | static int |
920 | bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh) | |
921 | { | |
2f9fd182 FI |
922 | struct block_mount_id *b_mt_id = NULL; |
923 | struct pnfs_devicelist *dlist = NULL; | |
924 | struct pnfs_block_dev *bdev; | |
925 | LIST_HEAD(block_disklist); | |
516f2e24 | 926 | int status, i; |
2f9fd182 | 927 | |
155e7524 | 928 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
929 | |
930 | if (server->pnfs_blksize == 0) { | |
931 | dprintk("%s Server did not return blksize\n", __func__); | |
932 | return -EINVAL; | |
933 | } | |
934 | b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS); | |
935 | if (!b_mt_id) { | |
936 | status = -ENOMEM; | |
937 | goto out_error; | |
938 | } | |
939 | /* Initialize nfs4 block layout mount id */ | |
940 | spin_lock_init(&b_mt_id->bm_lock); | |
941 | INIT_LIST_HEAD(&b_mt_id->bm_devlist); | |
942 | ||
943 | dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS); | |
944 | if (!dlist) { | |
945 | status = -ENOMEM; | |
946 | goto out_error; | |
947 | } | |
948 | dlist->eof = 0; | |
949 | while (!dlist->eof) { | |
950 | status = nfs4_proc_getdevicelist(server, fh, dlist); | |
951 | if (status) | |
952 | goto out_error; | |
953 | dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n", | |
954 | __func__, dlist->num_devs, dlist->eof); | |
955 | for (i = 0; i < dlist->num_devs; i++) { | |
956 | bdev = nfs4_blk_get_deviceinfo(server, fh, | |
957 | &dlist->dev_id[i]); | |
516f2e24 JR |
958 | if (IS_ERR(bdev)) { |
959 | status = PTR_ERR(bdev); | |
2f9fd182 FI |
960 | goto out_error; |
961 | } | |
962 | spin_lock(&b_mt_id->bm_lock); | |
963 | list_add(&bdev->bm_node, &b_mt_id->bm_devlist); | |
964 | spin_unlock(&b_mt_id->bm_lock); | |
965 | } | |
966 | } | |
967 | dprintk("%s SUCCESS\n", __func__); | |
968 | server->pnfs_ld_data = b_mt_id; | |
969 | ||
970 | out_return: | |
971 | kfree(dlist); | |
972 | return status; | |
973 | ||
974 | out_error: | |
975 | free_blk_mountid(b_mt_id); | |
976 | goto out_return; | |
155e7524 FI |
977 | } |
978 | ||
979 | static int | |
980 | bl_clear_layoutdriver(struct nfs_server *server) | |
981 | { | |
2f9fd182 FI |
982 | struct block_mount_id *b_mt_id = server->pnfs_ld_data; |
983 | ||
155e7524 | 984 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
985 | free_blk_mountid(b_mt_id); |
986 | dprintk("%s RETURNS\n", __func__); | |
155e7524 FI |
987 | return 0; |
988 | } | |
989 | ||
e9643fe8 BH |
990 | static const struct nfs_pageio_ops bl_pg_read_ops = { |
991 | .pg_init = pnfs_generic_pg_init_read, | |
992 | .pg_test = pnfs_generic_pg_test, | |
993 | .pg_doio = pnfs_generic_pg_readpages, | |
994 | }; | |
995 | ||
996 | static const struct nfs_pageio_ops bl_pg_write_ops = { | |
997 | .pg_init = pnfs_generic_pg_init_write, | |
998 | .pg_test = pnfs_generic_pg_test, | |
999 | .pg_doio = pnfs_generic_pg_writepages, | |
1000 | }; | |
1001 | ||
155e7524 FI |
1002 | static struct pnfs_layoutdriver_type blocklayout_type = { |
1003 | .id = LAYOUT_BLOCK_VOLUME, | |
1004 | .name = "LAYOUT_BLOCK_VOLUME", | |
1005 | .read_pagelist = bl_read_pagelist, | |
1006 | .write_pagelist = bl_write_pagelist, | |
1007 | .alloc_layout_hdr = bl_alloc_layout_hdr, | |
1008 | .free_layout_hdr = bl_free_layout_hdr, | |
1009 | .alloc_lseg = bl_alloc_lseg, | |
1010 | .free_lseg = bl_free_lseg, | |
1011 | .encode_layoutcommit = bl_encode_layoutcommit, | |
1012 | .cleanup_layoutcommit = bl_cleanup_layoutcommit, | |
1013 | .set_layoutdriver = bl_set_layoutdriver, | |
1014 | .clear_layoutdriver = bl_clear_layoutdriver, | |
e9643fe8 BH |
1015 | .pg_read_ops = &bl_pg_read_ops, |
1016 | .pg_write_ops = &bl_pg_write_ops, | |
155e7524 FI |
1017 | }; |
1018 | ||
fe0a9b74 | 1019 | static const struct rpc_pipe_ops bl_upcall_ops = { |
c1225158 | 1020 | .upcall = rpc_pipe_generic_upcall, |
fe0a9b74 JR |
1021 | .downcall = bl_pipe_downcall, |
1022 | .destroy_msg = bl_pipe_destroy_msg, | |
1023 | }; | |
1024 | ||
332dfab6 SK |
1025 | static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb, |
1026 | struct rpc_pipe *pipe) | |
1027 | { | |
1028 | struct dentry *dir, *dentry; | |
1029 | ||
1030 | dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME); | |
1031 | if (dir == NULL) | |
1032 | return ERR_PTR(-ENOENT); | |
1033 | dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe); | |
1034 | dput(dir); | |
1035 | return dentry; | |
1036 | } | |
1037 | ||
1038 | static void nfs4blocklayout_unregister_sb(struct super_block *sb, | |
1039 | struct rpc_pipe *pipe) | |
1040 | { | |
1041 | if (pipe->dentry) | |
1042 | rpc_unlink(pipe->dentry); | |
1043 | } | |
1044 | ||
627f3066 SK |
1045 | static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, |
1046 | void *ptr) | |
1047 | { | |
1048 | struct super_block *sb = ptr; | |
1049 | struct net *net = sb->s_fs_info; | |
1050 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1051 | struct dentry *dentry; | |
1052 | int ret = 0; | |
1053 | ||
1054 | if (!try_module_get(THIS_MODULE)) | |
1055 | return 0; | |
1056 | ||
1057 | if (nn->bl_device_pipe == NULL) { | |
1058 | module_put(THIS_MODULE); | |
1059 | return 0; | |
1060 | } | |
1061 | ||
1062 | switch (event) { | |
1063 | case RPC_PIPEFS_MOUNT: | |
1064 | dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe); | |
1065 | if (IS_ERR(dentry)) { | |
1066 | ret = PTR_ERR(dentry); | |
1067 | break; | |
1068 | } | |
1069 | nn->bl_device_pipe->dentry = dentry; | |
1070 | break; | |
1071 | case RPC_PIPEFS_UMOUNT: | |
1072 | if (nn->bl_device_pipe->dentry) | |
1073 | nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe); | |
1074 | break; | |
1075 | default: | |
1076 | ret = -ENOTSUPP; | |
1077 | break; | |
1078 | } | |
1079 | module_put(THIS_MODULE); | |
1080 | return ret; | |
1081 | } | |
1082 | ||
1083 | static struct notifier_block nfs4blocklayout_block = { | |
1084 | .notifier_call = rpc_pipefs_event, | |
1085 | }; | |
1086 | ||
332dfab6 SK |
1087 | static struct dentry *nfs4blocklayout_register_net(struct net *net, |
1088 | struct rpc_pipe *pipe) | |
1089 | { | |
1090 | struct super_block *pipefs_sb; | |
1091 | struct dentry *dentry; | |
1092 | ||
1093 | pipefs_sb = rpc_get_sb_net(net); | |
1094 | if (!pipefs_sb) | |
2561d618 | 1095 | return NULL; |
332dfab6 SK |
1096 | dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe); |
1097 | rpc_put_sb_net(net); | |
1098 | return dentry; | |
1099 | } | |
1100 | ||
1101 | static void nfs4blocklayout_unregister_net(struct net *net, | |
1102 | struct rpc_pipe *pipe) | |
1103 | { | |
1104 | struct super_block *pipefs_sb; | |
1105 | ||
1106 | pipefs_sb = rpc_get_sb_net(net); | |
1107 | if (pipefs_sb) { | |
1108 | nfs4blocklayout_unregister_sb(pipefs_sb, pipe); | |
1109 | rpc_put_sb_net(net); | |
1110 | } | |
1111 | } | |
1112 | ||
9e2e74db SK |
1113 | static int nfs4blocklayout_net_init(struct net *net) |
1114 | { | |
1115 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1116 | struct dentry *dentry; | |
1117 | ||
5ffaf855 | 1118 | init_waitqueue_head(&nn->bl_wq); |
9e2e74db SK |
1119 | nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0); |
1120 | if (IS_ERR(nn->bl_device_pipe)) | |
1121 | return PTR_ERR(nn->bl_device_pipe); | |
1122 | dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe); | |
1123 | if (IS_ERR(dentry)) { | |
1124 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1125 | return PTR_ERR(dentry); | |
1126 | } | |
1127 | nn->bl_device_pipe->dentry = dentry; | |
1128 | return 0; | |
1129 | } | |
1130 | ||
1131 | static void nfs4blocklayout_net_exit(struct net *net) | |
1132 | { | |
1133 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1134 | ||
1135 | nfs4blocklayout_unregister_net(net, nn->bl_device_pipe); | |
1136 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1137 | nn->bl_device_pipe = NULL; | |
1138 | } | |
1139 | ||
1140 | static struct pernet_operations nfs4blocklayout_net_ops = { | |
1141 | .init = nfs4blocklayout_net_init, | |
1142 | .exit = nfs4blocklayout_net_exit, | |
1143 | }; | |
1144 | ||
155e7524 FI |
1145 | static int __init nfs4blocklayout_init(void) |
1146 | { | |
1147 | int ret; | |
1148 | ||
1149 | dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__); | |
1150 | ||
1151 | ret = pnfs_register_layoutdriver(&blocklayout_type); | |
fe0a9b74 JR |
1152 | if (ret) |
1153 | goto out; | |
1154 | ||
627f3066 | 1155 | ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block); |
9e2e74db SK |
1156 | if (ret) |
1157 | goto out_remove; | |
627f3066 SK |
1158 | ret = register_pernet_subsys(&nfs4blocklayout_net_ops); |
1159 | if (ret) | |
1160 | goto out_notifier; | |
fe0a9b74 JR |
1161 | out: |
1162 | return ret; | |
1163 | ||
627f3066 SK |
1164 | out_notifier: |
1165 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); | |
fe0a9b74 JR |
1166 | out_remove: |
1167 | pnfs_unregister_layoutdriver(&blocklayout_type); | |
155e7524 FI |
1168 | return ret; |
1169 | } | |
1170 | ||
1171 | static void __exit nfs4blocklayout_exit(void) | |
1172 | { | |
1173 | dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n", | |
1174 | __func__); | |
1175 | ||
627f3066 | 1176 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
9e2e74db | 1177 | unregister_pernet_subsys(&nfs4blocklayout_net_ops); |
155e7524 FI |
1178 | pnfs_unregister_layoutdriver(&blocklayout_type); |
1179 | } | |
1180 | ||
1181 | MODULE_ALIAS("nfs-layouttype4-3"); | |
1182 | ||
1183 | module_init(nfs4blocklayout_init); | |
1184 | module_exit(nfs4blocklayout_exit); |