blocklayout.c 27 KB

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