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