blocklayout.c 38 KB

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