blocklayout.c 31 KB

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