blocklayout.c 35 KB

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