blocklayout.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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. dev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
  974. dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
  975. rc = nfs4_proc_getdeviceinfo(server, dev, NULL);
  976. dprintk("%s getdevice info returns %d\n", __func__, rc);
  977. if (rc) {
  978. rv = ERR_PTR(rc);
  979. goto out_free;
  980. }
  981. rv = nfs4_blk_decode_device(server, dev);
  982. out_free:
  983. for (i = 0; i < max_pages; i++)
  984. __free_page(pages[i]);
  985. kfree(pages);
  986. kfree(dev);
  987. return rv;
  988. }
  989. static int
  990. bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
  991. {
  992. struct block_mount_id *b_mt_id = NULL;
  993. struct pnfs_devicelist *dlist = NULL;
  994. struct pnfs_block_dev *bdev;
  995. LIST_HEAD(block_disklist);
  996. int status, i;
  997. dprintk("%s enter\n", __func__);
  998. if (server->pnfs_blksize == 0) {
  999. dprintk("%s Server did not return blksize\n", __func__);
  1000. return -EINVAL;
  1001. }
  1002. b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
  1003. if (!b_mt_id) {
  1004. status = -ENOMEM;
  1005. goto out_error;
  1006. }
  1007. /* Initialize nfs4 block layout mount id */
  1008. spin_lock_init(&b_mt_id->bm_lock);
  1009. INIT_LIST_HEAD(&b_mt_id->bm_devlist);
  1010. dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
  1011. if (!dlist) {
  1012. status = -ENOMEM;
  1013. goto out_error;
  1014. }
  1015. dlist->eof = 0;
  1016. while (!dlist->eof) {
  1017. status = nfs4_proc_getdevicelist(server, fh, dlist);
  1018. if (status)
  1019. goto out_error;
  1020. dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
  1021. __func__, dlist->num_devs, dlist->eof);
  1022. for (i = 0; i < dlist->num_devs; i++) {
  1023. bdev = nfs4_blk_get_deviceinfo(server, fh,
  1024. &dlist->dev_id[i]);
  1025. if (IS_ERR(bdev)) {
  1026. status = PTR_ERR(bdev);
  1027. goto out_error;
  1028. }
  1029. spin_lock(&b_mt_id->bm_lock);
  1030. list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
  1031. spin_unlock(&b_mt_id->bm_lock);
  1032. }
  1033. }
  1034. dprintk("%s SUCCESS\n", __func__);
  1035. server->pnfs_ld_data = b_mt_id;
  1036. out_return:
  1037. kfree(dlist);
  1038. return status;
  1039. out_error:
  1040. free_blk_mountid(b_mt_id);
  1041. goto out_return;
  1042. }
  1043. static int
  1044. bl_clear_layoutdriver(struct nfs_server *server)
  1045. {
  1046. struct block_mount_id *b_mt_id = server->pnfs_ld_data;
  1047. dprintk("%s enter\n", __func__);
  1048. free_blk_mountid(b_mt_id);
  1049. dprintk("%s RETURNS\n", __func__);
  1050. return 0;
  1051. }
  1052. static bool
  1053. is_aligned_req(struct nfs_page *req, unsigned int alignment)
  1054. {
  1055. return IS_ALIGNED(req->wb_offset, alignment) &&
  1056. IS_ALIGNED(req->wb_bytes, alignment);
  1057. }
  1058. static void
  1059. bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  1060. {
  1061. if (pgio->pg_dreq != NULL &&
  1062. !is_aligned_req(req, SECTOR_SIZE))
  1063. nfs_pageio_reset_read_mds(pgio);
  1064. else
  1065. pnfs_generic_pg_init_read(pgio, req);
  1066. }
  1067. static bool
  1068. bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  1069. struct nfs_page *req)
  1070. {
  1071. if (pgio->pg_dreq != NULL &&
  1072. !is_aligned_req(req, SECTOR_SIZE))
  1073. return false;
  1074. return pnfs_generic_pg_test(pgio, prev, req);
  1075. }
  1076. /*
  1077. * Return the number of contiguous bytes for a given inode
  1078. * starting at page frame idx.
  1079. */
  1080. static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
  1081. {
  1082. struct address_space *mapping = inode->i_mapping;
  1083. pgoff_t end;
  1084. /* Optimize common case that writes from 0 to end of file */
  1085. end = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
  1086. if (end != NFS_I(inode)->npages) {
  1087. rcu_read_lock();
  1088. end = radix_tree_next_hole(&mapping->page_tree, idx + 1, ULONG_MAX);
  1089. rcu_read_unlock();
  1090. }
  1091. if (!end)
  1092. return i_size_read(inode) - (idx << PAGE_CACHE_SHIFT);
  1093. else
  1094. return (end - idx) << PAGE_CACHE_SHIFT;
  1095. }
  1096. static void
  1097. bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  1098. {
  1099. if (pgio->pg_dreq != NULL &&
  1100. !is_aligned_req(req, PAGE_CACHE_SIZE)) {
  1101. nfs_pageio_reset_write_mds(pgio);
  1102. } else {
  1103. u64 wb_size;
  1104. if (pgio->pg_dreq == NULL)
  1105. wb_size = pnfs_num_cont_bytes(pgio->pg_inode,
  1106. req->wb_index);
  1107. else
  1108. wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  1109. pnfs_generic_pg_init_write(pgio, req, wb_size);
  1110. }
  1111. }
  1112. static bool
  1113. bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  1114. struct nfs_page *req)
  1115. {
  1116. if (pgio->pg_dreq != NULL &&
  1117. !is_aligned_req(req, PAGE_CACHE_SIZE))
  1118. return false;
  1119. return pnfs_generic_pg_test(pgio, prev, req);
  1120. }
  1121. static const struct nfs_pageio_ops bl_pg_read_ops = {
  1122. .pg_init = bl_pg_init_read,
  1123. .pg_test = bl_pg_test_read,
  1124. .pg_doio = pnfs_generic_pg_readpages,
  1125. };
  1126. static const struct nfs_pageio_ops bl_pg_write_ops = {
  1127. .pg_init = bl_pg_init_write,
  1128. .pg_test = bl_pg_test_write,
  1129. .pg_doio = pnfs_generic_pg_writepages,
  1130. };
  1131. static struct pnfs_layoutdriver_type blocklayout_type = {
  1132. .id = LAYOUT_BLOCK_VOLUME,
  1133. .name = "LAYOUT_BLOCK_VOLUME",
  1134. .owner = THIS_MODULE,
  1135. .read_pagelist = bl_read_pagelist,
  1136. .write_pagelist = bl_write_pagelist,
  1137. .alloc_layout_hdr = bl_alloc_layout_hdr,
  1138. .free_layout_hdr = bl_free_layout_hdr,
  1139. .alloc_lseg = bl_alloc_lseg,
  1140. .free_lseg = bl_free_lseg,
  1141. .encode_layoutcommit = bl_encode_layoutcommit,
  1142. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  1143. .set_layoutdriver = bl_set_layoutdriver,
  1144. .clear_layoutdriver = bl_clear_layoutdriver,
  1145. .pg_read_ops = &bl_pg_read_ops,
  1146. .pg_write_ops = &bl_pg_write_ops,
  1147. };
  1148. static const struct rpc_pipe_ops bl_upcall_ops = {
  1149. .upcall = rpc_pipe_generic_upcall,
  1150. .downcall = bl_pipe_downcall,
  1151. .destroy_msg = bl_pipe_destroy_msg,
  1152. };
  1153. static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb,
  1154. struct rpc_pipe *pipe)
  1155. {
  1156. struct dentry *dir, *dentry;
  1157. dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME);
  1158. if (dir == NULL)
  1159. return ERR_PTR(-ENOENT);
  1160. dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe);
  1161. dput(dir);
  1162. return dentry;
  1163. }
  1164. static void nfs4blocklayout_unregister_sb(struct super_block *sb,
  1165. struct rpc_pipe *pipe)
  1166. {
  1167. if (pipe->dentry)
  1168. rpc_unlink(pipe->dentry);
  1169. }
  1170. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  1171. void *ptr)
  1172. {
  1173. struct super_block *sb = ptr;
  1174. struct net *net = sb->s_fs_info;
  1175. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1176. struct dentry *dentry;
  1177. int ret = 0;
  1178. if (!try_module_get(THIS_MODULE))
  1179. return 0;
  1180. if (nn->bl_device_pipe == NULL) {
  1181. module_put(THIS_MODULE);
  1182. return 0;
  1183. }
  1184. switch (event) {
  1185. case RPC_PIPEFS_MOUNT:
  1186. dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe);
  1187. if (IS_ERR(dentry)) {
  1188. ret = PTR_ERR(dentry);
  1189. break;
  1190. }
  1191. nn->bl_device_pipe->dentry = dentry;
  1192. break;
  1193. case RPC_PIPEFS_UMOUNT:
  1194. if (nn->bl_device_pipe->dentry)
  1195. nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe);
  1196. break;
  1197. default:
  1198. ret = -ENOTSUPP;
  1199. break;
  1200. }
  1201. module_put(THIS_MODULE);
  1202. return ret;
  1203. }
  1204. static struct notifier_block nfs4blocklayout_block = {
  1205. .notifier_call = rpc_pipefs_event,
  1206. };
  1207. static struct dentry *nfs4blocklayout_register_net(struct net *net,
  1208. struct rpc_pipe *pipe)
  1209. {
  1210. struct super_block *pipefs_sb;
  1211. struct dentry *dentry;
  1212. pipefs_sb = rpc_get_sb_net(net);
  1213. if (!pipefs_sb)
  1214. return NULL;
  1215. dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe);
  1216. rpc_put_sb_net(net);
  1217. return dentry;
  1218. }
  1219. static void nfs4blocklayout_unregister_net(struct net *net,
  1220. struct rpc_pipe *pipe)
  1221. {
  1222. struct super_block *pipefs_sb;
  1223. pipefs_sb = rpc_get_sb_net(net);
  1224. if (pipefs_sb) {
  1225. nfs4blocklayout_unregister_sb(pipefs_sb, pipe);
  1226. rpc_put_sb_net(net);
  1227. }
  1228. }
  1229. static int nfs4blocklayout_net_init(struct net *net)
  1230. {
  1231. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1232. struct dentry *dentry;
  1233. init_waitqueue_head(&nn->bl_wq);
  1234. nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0);
  1235. if (IS_ERR(nn->bl_device_pipe))
  1236. return PTR_ERR(nn->bl_device_pipe);
  1237. dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe);
  1238. if (IS_ERR(dentry)) {
  1239. rpc_destroy_pipe_data(nn->bl_device_pipe);
  1240. return PTR_ERR(dentry);
  1241. }
  1242. nn->bl_device_pipe->dentry = dentry;
  1243. return 0;
  1244. }
  1245. static void nfs4blocklayout_net_exit(struct net *net)
  1246. {
  1247. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1248. nfs4blocklayout_unregister_net(net, nn->bl_device_pipe);
  1249. rpc_destroy_pipe_data(nn->bl_device_pipe);
  1250. nn->bl_device_pipe = NULL;
  1251. }
  1252. static struct pernet_operations nfs4blocklayout_net_ops = {
  1253. .init = nfs4blocklayout_net_init,
  1254. .exit = nfs4blocklayout_net_exit,
  1255. };
  1256. static int __init nfs4blocklayout_init(void)
  1257. {
  1258. int ret;
  1259. dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
  1260. ret = pnfs_register_layoutdriver(&blocklayout_type);
  1261. if (ret)
  1262. goto out;
  1263. ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block);
  1264. if (ret)
  1265. goto out_remove;
  1266. ret = register_pernet_subsys(&nfs4blocklayout_net_ops);
  1267. if (ret)
  1268. goto out_notifier;
  1269. out:
  1270. return ret;
  1271. out_notifier:
  1272. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  1273. out_remove:
  1274. pnfs_unregister_layoutdriver(&blocklayout_type);
  1275. return ret;
  1276. }
  1277. static void __exit nfs4blocklayout_exit(void)
  1278. {
  1279. dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
  1280. __func__);
  1281. rpc_pipefs_notifier_unregister(&nfs4blocklayout_block);
  1282. unregister_pernet_subsys(&nfs4blocklayout_net_ops);
  1283. pnfs_unregister_layoutdriver(&blocklayout_type);
  1284. }
  1285. MODULE_ALIAS("nfs-layouttype4-3");
  1286. module_init(nfs4blocklayout_init);
  1287. module_exit(nfs4blocklayout_exit);