blocklayout.c 38 KB

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