blocklayout.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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. .owner = THIS_MODULE,
  1134. .read_pagelist = bl_read_pagelist,
  1135. .write_pagelist = bl_write_pagelist,
  1136. .alloc_layout_hdr = bl_alloc_layout_hdr,
  1137. .free_layout_hdr = bl_free_layout_hdr,
  1138. .alloc_lseg = bl_alloc_lseg,
  1139. .free_lseg = bl_free_lseg,
  1140. .encode_layoutcommit = bl_encode_layoutcommit,
  1141. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  1142. .set_layoutdriver = bl_set_layoutdriver,
  1143. .clear_layoutdriver = bl_clear_layoutdriver,
  1144. .pg_read_ops = &bl_pg_read_ops,
  1145. .pg_write_ops = &bl_pg_write_ops,
  1146. };
  1147. static const struct rpc_pipe_ops bl_upcall_ops = {
  1148. .upcall = rpc_pipe_generic_upcall,
  1149. .downcall = bl_pipe_downcall,
  1150. .destroy_msg = bl_pipe_destroy_msg,
  1151. };
  1152. static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
  1153. struct rpc_pipe *pipe)
  1154. {
  1155. struct dentry *dir, *dentry;
  1156. dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
  1157. if (dir == NULL)
  1158. return ERR_PTR(-ENOENT);
  1159. dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
  1160. dput(dir);
  1161. return dentry;
  1162. }
  1163. static void nfs4blocklayout_unregister_sb(struct super_block *sb,
  1164. struct rpc_pipe *pipe)
  1165. {
  1166. if (pipe->dentry)
  1167. rpc_unlink(pipe->dentry);
  1168. }
  1169. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  1170. void *ptr)
  1171. {
  1172. struct super_block *sb = ptr;
  1173. struct net *net = sb->s_fs_info;
  1174. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1175. struct dentry *dentry;
  1176. int ret = 0;
  1177. if (!try_module_get(THIS_MODULE))
  1178. return 0;
  1179. if (nn->bl_device_pipe == NULL) {
  1180. module_put(THIS_MODULE);
  1181. return 0;
  1182. }
  1183. switch (event) {
  1184. case RPC_PIPEFS_MOUNT:
  1185. dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
  1186. if (IS_ERR(dentry)) {
  1187. ret = PTR_ERR(dentry);
  1188. break;
  1189. }
  1190. nn->bl_device_pipe->dentry = dentry;
  1191. break;
  1192. case RPC_PIPEFS_UMOUNT:
  1193. if (nn->bl_device_pipe->dentry)
  1194. nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
  1195. break;
  1196. default:
  1197. ret = -ENOTSUPP;
  1198. break;
  1199. }
  1200. module_put(THIS_MODULE);
  1201. return ret;
  1202. }
  1203. static struct notifier_block nfs4blocklayout_block = {
  1204. .notifier_call = rpc_pipefs_event,
  1205. };
  1206. static struct dentry *nfs4blocklayout_register_net(struct net *net,
  1207. struct rpc_pipe *pipe)
  1208. {
  1209. struct super_block *pipefs_sb;
  1210. struct dentry *dentry;
  1211. pipefs_sb = rpc_get_sb_net(net);
  1212. if (!pipefs_sb)
  1213. return NULL;
  1214. dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
  1215. rpc_put_sb_net(net);
  1216. return dentry;
  1217. }
  1218. static void nfs4blocklayout_unregister_net(struct net *net,
  1219. struct rpc_pipe *pipe)
  1220. {
  1221. struct super_block *pipefs_sb;
  1222. pipefs_sb = rpc_get_sb_net(net);
  1223. if (pipefs_sb) {
  1224. nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
  1225. rpc_put_sb_net(net);
  1226. }
  1227. }
  1228. static int nfs4blocklayout_net_init(struct net *net)
  1229. {
  1230. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1231. struct dentry *dentry;
  1232. init_waitqueue_head(&nn->bl_wq);
  1233. nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
  1234. if (IS_ERR(nn->bl_device_pipe))
  1235. return PTR_ERR(nn->bl_device_pipe);
  1236. dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
  1237. if (IS_ERR(dentry)) {
  1238. rpc_destroy_pipe_data(nn->bl_device_pipe);
  1239. return PTR_ERR(dentry);
  1240. }
  1241. nn->bl_device_pipe->dentry = dentry;
  1242. return 0;
  1243. }
  1244. static void nfs4blocklayout_net_exit(struct net *net)
  1245. {
  1246. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1247. nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
  1248. rpc_destroy_pipe_data(nn->bl_device_pipe);
  1249. nn->bl_device_pipe = NULL;
  1250. }
  1251. static struct pernet_operations nfs4blocklayout_net_ops = {
  1252. .init = nfs4blocklayout_net_init,
  1253. .exit = nfs4blocklayout_net_exit,
  1254. };
  1255. static int __init nfs4blocklayout_init(void)
  1256. {
  1257. int ret;
  1258. dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
  1259. ret = pnfs_register_layoutdriver(&blocklayout_type);
  1260. if (ret)
  1261. goto out;
  1262. ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
  1263. if (ret)
  1264. goto out_remove;
  1265. ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
  1266. if (ret)
  1267. goto out_notifier;
  1268. out:
  1269. return ret;
  1270. out_notifier:
  1271. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  1272. out_remove:
  1273. pnfs_unregister_layoutdriver(&blocklayout_type);
  1274. return ret;
  1275. }
  1276. static void __exit nfs4blocklayout_exit(void)
  1277. {
  1278. dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
  1279. __func__);
  1280. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  1281. unregister_pernet_subsys(&nfs4blocklayout_net_ops);
  1282. pnfs_unregister_layoutdriver(&blocklayout_type);
  1283. }
  1284. MODULE_ALIAS("nfs-layouttype4-3");
  1285. module_init(nfs4blocklayout_init);
  1286. module_exit(nfs4blocklayout_exit);