xfs_aops.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_inum.h"
  34. #include "xfs_log.h"
  35. #include "xfs_sb.h"
  36. #include "xfs_dir.h"
  37. #include "xfs_dir2.h"
  38. #include "xfs_trans.h"
  39. #include "xfs_dmapi.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_bmap_btree.h"
  42. #include "xfs_alloc_btree.h"
  43. #include "xfs_ialloc_btree.h"
  44. #include "xfs_alloc.h"
  45. #include "xfs_btree.h"
  46. #include "xfs_attr_sf.h"
  47. #include "xfs_dir_sf.h"
  48. #include "xfs_dir2_sf.h"
  49. #include "xfs_dinode.h"
  50. #include "xfs_inode.h"
  51. #include "xfs_error.h"
  52. #include "xfs_rw.h"
  53. #include "xfs_iomap.h"
  54. #include <linux/mpage.h>
  55. #include <linux/writeback.h>
  56. STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
  57. STATIC void xfs_convert_page(struct inode *, struct page *, xfs_iomap_t *,
  58. struct writeback_control *wbc, void *, int, int);
  59. #if defined(XFS_RW_TRACE)
  60. void
  61. xfs_page_trace(
  62. int tag,
  63. struct inode *inode,
  64. struct page *page,
  65. int mask)
  66. {
  67. xfs_inode_t *ip;
  68. bhv_desc_t *bdp;
  69. vnode_t *vp = LINVFS_GET_VP(inode);
  70. loff_t isize = i_size_read(inode);
  71. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  72. int delalloc = -1, unmapped = -1, unwritten = -1;
  73. if (page_has_buffers(page))
  74. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  75. bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
  76. ip = XFS_BHVTOI(bdp);
  77. if (!ip->i_rwtrace)
  78. return;
  79. ktrace_enter(ip->i_rwtrace,
  80. (void *)((unsigned long)tag),
  81. (void *)ip,
  82. (void *)inode,
  83. (void *)page,
  84. (void *)((unsigned long)mask),
  85. (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
  86. (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
  87. (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
  88. (void *)((unsigned long)(isize & 0xffffffff)),
  89. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  90. (void *)((unsigned long)(offset & 0xffffffff)),
  91. (void *)((unsigned long)delalloc),
  92. (void *)((unsigned long)unmapped),
  93. (void *)((unsigned long)unwritten),
  94. (void *)NULL,
  95. (void *)NULL);
  96. }
  97. #else
  98. #define xfs_page_trace(tag, inode, page, mask)
  99. #endif
  100. void
  101. linvfs_unwritten_done(
  102. struct buffer_head *bh,
  103. int uptodate)
  104. {
  105. xfs_buf_t *pb = (xfs_buf_t *)bh->b_private;
  106. ASSERT(buffer_unwritten(bh));
  107. bh->b_end_io = NULL;
  108. clear_buffer_unwritten(bh);
  109. if (!uptodate)
  110. pagebuf_ioerror(pb, EIO);
  111. if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
  112. pagebuf_iodone(pb, 1, 1);
  113. }
  114. end_buffer_async_write(bh, uptodate);
  115. }
  116. /*
  117. * Issue transactions to convert a buffer range from unwritten
  118. * to written extents (buffered IO).
  119. */
  120. STATIC void
  121. linvfs_unwritten_convert(
  122. xfs_buf_t *bp)
  123. {
  124. vnode_t *vp = XFS_BUF_FSPRIVATE(bp, vnode_t *);
  125. int error;
  126. BUG_ON(atomic_read(&bp->pb_hold) < 1);
  127. VOP_BMAP(vp, XFS_BUF_OFFSET(bp), XFS_BUF_SIZE(bp),
  128. BMAPI_UNWRITTEN, NULL, NULL, error);
  129. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  130. XFS_BUF_CLR_IODONE_FUNC(bp);
  131. XFS_BUF_UNDATAIO(bp);
  132. iput(LINVFS_GET_IP(vp));
  133. pagebuf_iodone(bp, 0, 0);
  134. }
  135. /*
  136. * Issue transactions to convert a buffer range from unwritten
  137. * to written extents (direct IO).
  138. */
  139. STATIC void
  140. linvfs_unwritten_convert_direct(
  141. struct inode *inode,
  142. loff_t offset,
  143. ssize_t size,
  144. void *private)
  145. {
  146. ASSERT(!private || inode == (struct inode *)private);
  147. /* private indicates an unwritten extent lay beneath this IO */
  148. if (private && size > 0) {
  149. vnode_t *vp = LINVFS_GET_VP(inode);
  150. int error;
  151. VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
  152. }
  153. }
  154. STATIC int
  155. xfs_map_blocks(
  156. struct inode *inode,
  157. loff_t offset,
  158. ssize_t count,
  159. xfs_iomap_t *mapp,
  160. int flags)
  161. {
  162. vnode_t *vp = LINVFS_GET_VP(inode);
  163. int error, nmaps = 1;
  164. VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
  165. if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
  166. VMODIFY(vp);
  167. return -error;
  168. }
  169. /*
  170. * Finds the corresponding mapping in block @map array of the
  171. * given @offset within a @page.
  172. */
  173. STATIC xfs_iomap_t *
  174. xfs_offset_to_map(
  175. struct page *page,
  176. xfs_iomap_t *iomapp,
  177. unsigned long offset)
  178. {
  179. loff_t full_offset; /* offset from start of file */
  180. ASSERT(offset < PAGE_CACHE_SIZE);
  181. full_offset = page->index; /* NB: using 64bit number */
  182. full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
  183. full_offset += offset; /* offset from page start */
  184. if (full_offset < iomapp->iomap_offset)
  185. return NULL;
  186. if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
  187. return iomapp;
  188. return NULL;
  189. }
  190. STATIC void
  191. xfs_map_at_offset(
  192. struct page *page,
  193. struct buffer_head *bh,
  194. unsigned long offset,
  195. int block_bits,
  196. xfs_iomap_t *iomapp)
  197. {
  198. xfs_daddr_t bn;
  199. loff_t delta;
  200. int sector_shift;
  201. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  202. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  203. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  204. delta = page->index;
  205. delta <<= PAGE_CACHE_SHIFT;
  206. delta += offset;
  207. delta -= iomapp->iomap_offset;
  208. delta >>= block_bits;
  209. sector_shift = block_bits - BBSHIFT;
  210. bn = iomapp->iomap_bn >> sector_shift;
  211. bn += delta;
  212. BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
  213. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  214. lock_buffer(bh);
  215. bh->b_blocknr = bn;
  216. bh->b_bdev = iomapp->iomap_target->pbr_bdev;
  217. set_buffer_mapped(bh);
  218. clear_buffer_delay(bh);
  219. }
  220. /*
  221. * Look for a page at index which is unlocked and contains our
  222. * unwritten extent flagged buffers at its head. Returns page
  223. * locked and with an extra reference count, and length of the
  224. * unwritten extent component on this page that we can write,
  225. * in units of filesystem blocks.
  226. */
  227. STATIC struct page *
  228. xfs_probe_unwritten_page(
  229. struct address_space *mapping,
  230. pgoff_t index,
  231. xfs_iomap_t *iomapp,
  232. xfs_buf_t *pb,
  233. unsigned long max_offset,
  234. unsigned long *fsbs,
  235. unsigned int bbits)
  236. {
  237. struct page *page;
  238. page = find_trylock_page(mapping, index);
  239. if (!page)
  240. return NULL;
  241. if (PageWriteback(page))
  242. goto out;
  243. if (page->mapping && page_has_buffers(page)) {
  244. struct buffer_head *bh, *head;
  245. unsigned long p_offset = 0;
  246. *fsbs = 0;
  247. bh = head = page_buffers(page);
  248. do {
  249. if (!buffer_unwritten(bh) || !buffer_uptodate(bh))
  250. break;
  251. if (!xfs_offset_to_map(page, iomapp, p_offset))
  252. break;
  253. if (p_offset >= max_offset)
  254. break;
  255. xfs_map_at_offset(page, bh, p_offset, bbits, iomapp);
  256. set_buffer_unwritten_io(bh);
  257. bh->b_private = pb;
  258. p_offset += bh->b_size;
  259. (*fsbs)++;
  260. } while ((bh = bh->b_this_page) != head);
  261. if (p_offset)
  262. return page;
  263. }
  264. out:
  265. unlock_page(page);
  266. return NULL;
  267. }
  268. /*
  269. * Look for a page at index which is unlocked and not mapped
  270. * yet - clustering for mmap write case.
  271. */
  272. STATIC unsigned int
  273. xfs_probe_unmapped_page(
  274. struct address_space *mapping,
  275. pgoff_t index,
  276. unsigned int pg_offset)
  277. {
  278. struct page *page;
  279. int ret = 0;
  280. page = find_trylock_page(mapping, index);
  281. if (!page)
  282. return 0;
  283. if (PageWriteback(page))
  284. goto out;
  285. if (page->mapping && PageDirty(page)) {
  286. if (page_has_buffers(page)) {
  287. struct buffer_head *bh, *head;
  288. bh = head = page_buffers(page);
  289. do {
  290. if (buffer_mapped(bh) || !buffer_uptodate(bh))
  291. break;
  292. ret += bh->b_size;
  293. if (ret >= pg_offset)
  294. break;
  295. } while ((bh = bh->b_this_page) != head);
  296. } else
  297. ret = PAGE_CACHE_SIZE;
  298. }
  299. out:
  300. unlock_page(page);
  301. return ret;
  302. }
  303. STATIC unsigned int
  304. xfs_probe_unmapped_cluster(
  305. struct inode *inode,
  306. struct page *startpage,
  307. struct buffer_head *bh,
  308. struct buffer_head *head)
  309. {
  310. pgoff_t tindex, tlast, tloff;
  311. unsigned int pg_offset, len, total = 0;
  312. struct address_space *mapping = inode->i_mapping;
  313. /* First sum forwards in this page */
  314. do {
  315. if (buffer_mapped(bh))
  316. break;
  317. total += bh->b_size;
  318. } while ((bh = bh->b_this_page) != head);
  319. /* If we reached the end of the page, sum forwards in
  320. * following pages.
  321. */
  322. if (bh == head) {
  323. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  324. /* Prune this back to avoid pathological behavior */
  325. tloff = min(tlast, startpage->index + 64);
  326. for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
  327. len = xfs_probe_unmapped_page(mapping, tindex,
  328. PAGE_CACHE_SIZE);
  329. if (!len)
  330. return total;
  331. total += len;
  332. }
  333. if (tindex == tlast &&
  334. (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  335. total += xfs_probe_unmapped_page(mapping,
  336. tindex, pg_offset);
  337. }
  338. }
  339. return total;
  340. }
  341. /*
  342. * Probe for a given page (index) in the inode and test if it is delayed
  343. * and without unwritten buffers. Returns page locked and with an extra
  344. * reference count.
  345. */
  346. STATIC struct page *
  347. xfs_probe_delalloc_page(
  348. struct inode *inode,
  349. pgoff_t index)
  350. {
  351. struct page *page;
  352. page = find_trylock_page(inode->i_mapping, index);
  353. if (!page)
  354. return NULL;
  355. if (PageWriteback(page))
  356. goto out;
  357. if (page->mapping && page_has_buffers(page)) {
  358. struct buffer_head *bh, *head;
  359. int acceptable = 0;
  360. bh = head = page_buffers(page);
  361. do {
  362. if (buffer_unwritten(bh)) {
  363. acceptable = 0;
  364. break;
  365. } else if (buffer_delay(bh)) {
  366. acceptable = 1;
  367. }
  368. } while ((bh = bh->b_this_page) != head);
  369. if (acceptable)
  370. return page;
  371. }
  372. out:
  373. unlock_page(page);
  374. return NULL;
  375. }
  376. STATIC int
  377. xfs_map_unwritten(
  378. struct inode *inode,
  379. struct page *start_page,
  380. struct buffer_head *head,
  381. struct buffer_head *curr,
  382. unsigned long p_offset,
  383. int block_bits,
  384. xfs_iomap_t *iomapp,
  385. struct writeback_control *wbc,
  386. int startio,
  387. int all_bh)
  388. {
  389. struct buffer_head *bh = curr;
  390. xfs_iomap_t *tmp;
  391. xfs_buf_t *pb;
  392. loff_t offset, size;
  393. unsigned long nblocks = 0;
  394. offset = start_page->index;
  395. offset <<= PAGE_CACHE_SHIFT;
  396. offset += p_offset;
  397. /* get an "empty" pagebuf to manage IO completion
  398. * Proper values will be set before returning */
  399. pb = pagebuf_lookup(iomapp->iomap_target, 0, 0, 0);
  400. if (!pb)
  401. return -EAGAIN;
  402. /* Take a reference to the inode to prevent it from
  403. * being reclaimed while we have outstanding unwritten
  404. * extent IO on it.
  405. */
  406. if ((igrab(inode)) != inode) {
  407. pagebuf_free(pb);
  408. return -EAGAIN;
  409. }
  410. /* Set the count to 1 initially, this will stop an I/O
  411. * completion callout which happens before we have started
  412. * all the I/O from calling pagebuf_iodone too early.
  413. */
  414. atomic_set(&pb->pb_io_remaining, 1);
  415. /* First map forwards in the page consecutive buffers
  416. * covering this unwritten extent
  417. */
  418. do {
  419. if (!buffer_unwritten(bh))
  420. break;
  421. tmp = xfs_offset_to_map(start_page, iomapp, p_offset);
  422. if (!tmp)
  423. break;
  424. xfs_map_at_offset(start_page, bh, p_offset, block_bits, iomapp);
  425. set_buffer_unwritten_io(bh);
  426. bh->b_private = pb;
  427. p_offset += bh->b_size;
  428. nblocks++;
  429. } while ((bh = bh->b_this_page) != head);
  430. atomic_add(nblocks, &pb->pb_io_remaining);
  431. /* If we reached the end of the page, map forwards in any
  432. * following pages which are also covered by this extent.
  433. */
  434. if (bh == head) {
  435. struct address_space *mapping = inode->i_mapping;
  436. pgoff_t tindex, tloff, tlast;
  437. unsigned long bs;
  438. unsigned int pg_offset, bbits = inode->i_blkbits;
  439. struct page *page;
  440. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  441. tloff = (iomapp->iomap_offset + iomapp->iomap_bsize) >> PAGE_CACHE_SHIFT;
  442. tloff = min(tlast, tloff);
  443. for (tindex = start_page->index + 1; tindex < tloff; tindex++) {
  444. page = xfs_probe_unwritten_page(mapping,
  445. tindex, iomapp, pb,
  446. PAGE_CACHE_SIZE, &bs, bbits);
  447. if (!page)
  448. break;
  449. nblocks += bs;
  450. atomic_add(bs, &pb->pb_io_remaining);
  451. xfs_convert_page(inode, page, iomapp, wbc, pb,
  452. startio, all_bh);
  453. /* stop if converting the next page might add
  454. * enough blocks that the corresponding byte
  455. * count won't fit in our ulong page buf length */
  456. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  457. goto enough;
  458. }
  459. if (tindex == tlast &&
  460. (pg_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1)))) {
  461. page = xfs_probe_unwritten_page(mapping,
  462. tindex, iomapp, pb,
  463. pg_offset, &bs, bbits);
  464. if (page) {
  465. nblocks += bs;
  466. atomic_add(bs, &pb->pb_io_remaining);
  467. xfs_convert_page(inode, page, iomapp, wbc, pb,
  468. startio, all_bh);
  469. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  470. goto enough;
  471. }
  472. }
  473. }
  474. enough:
  475. size = nblocks; /* NB: using 64bit number here */
  476. size <<= block_bits; /* convert fsb's to byte range */
  477. XFS_BUF_DATAIO(pb);
  478. XFS_BUF_ASYNC(pb);
  479. XFS_BUF_SET_SIZE(pb, size);
  480. XFS_BUF_SET_COUNT(pb, size);
  481. XFS_BUF_SET_OFFSET(pb, offset);
  482. XFS_BUF_SET_FSPRIVATE(pb, LINVFS_GET_VP(inode));
  483. XFS_BUF_SET_IODONE_FUNC(pb, linvfs_unwritten_convert);
  484. if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
  485. pagebuf_iodone(pb, 1, 1);
  486. }
  487. return 0;
  488. }
  489. STATIC void
  490. xfs_submit_page(
  491. struct page *page,
  492. struct writeback_control *wbc,
  493. struct buffer_head *bh_arr[],
  494. int bh_count,
  495. int probed_page,
  496. int clear_dirty)
  497. {
  498. struct buffer_head *bh;
  499. int i;
  500. BUG_ON(PageWriteback(page));
  501. set_page_writeback(page);
  502. if (clear_dirty)
  503. clear_page_dirty(page);
  504. unlock_page(page);
  505. if (bh_count) {
  506. for (i = 0; i < bh_count; i++) {
  507. bh = bh_arr[i];
  508. mark_buffer_async_write(bh);
  509. if (buffer_unwritten(bh))
  510. set_buffer_unwritten_io(bh);
  511. set_buffer_uptodate(bh);
  512. clear_buffer_dirty(bh);
  513. }
  514. for (i = 0; i < bh_count; i++)
  515. submit_bh(WRITE, bh_arr[i]);
  516. if (probed_page && clear_dirty)
  517. wbc->nr_to_write--; /* Wrote an "extra" page */
  518. } else {
  519. end_page_writeback(page);
  520. wbc->pages_skipped++; /* We didn't write this page */
  521. }
  522. }
  523. /*
  524. * Allocate & map buffers for page given the extent map. Write it out.
  525. * except for the original page of a writepage, this is called on
  526. * delalloc/unwritten pages only, for the original page it is possible
  527. * that the page has no mapping at all.
  528. */
  529. STATIC void
  530. xfs_convert_page(
  531. struct inode *inode,
  532. struct page *page,
  533. xfs_iomap_t *iomapp,
  534. struct writeback_control *wbc,
  535. void *private,
  536. int startio,
  537. int all_bh)
  538. {
  539. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  540. xfs_iomap_t *mp = iomapp, *tmp;
  541. unsigned long end, offset;
  542. pgoff_t end_index;
  543. int i = 0, index = 0;
  544. int bbits = inode->i_blkbits;
  545. end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  546. if (page->index < end_index) {
  547. end = PAGE_CACHE_SIZE;
  548. } else {
  549. end = i_size_read(inode) & (PAGE_CACHE_SIZE-1);
  550. }
  551. bh = head = page_buffers(page);
  552. do {
  553. offset = i << bbits;
  554. if (offset >= end)
  555. break;
  556. if (!(PageUptodate(page) || buffer_uptodate(bh)))
  557. continue;
  558. if (buffer_mapped(bh) && all_bh &&
  559. !(buffer_unwritten(bh) || buffer_delay(bh))) {
  560. if (startio) {
  561. lock_buffer(bh);
  562. bh_arr[index++] = bh;
  563. }
  564. continue;
  565. }
  566. tmp = xfs_offset_to_map(page, mp, offset);
  567. if (!tmp)
  568. continue;
  569. ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
  570. ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
  571. /* If this is a new unwritten extent buffer (i.e. one
  572. * that we haven't passed in private data for, we must
  573. * now map this buffer too.
  574. */
  575. if (buffer_unwritten(bh) && !bh->b_end_io) {
  576. ASSERT(tmp->iomap_flags & IOMAP_UNWRITTEN);
  577. xfs_map_unwritten(inode, page, head, bh, offset,
  578. bbits, tmp, wbc, startio, all_bh);
  579. } else if (! (buffer_unwritten(bh) && buffer_locked(bh))) {
  580. xfs_map_at_offset(page, bh, offset, bbits, tmp);
  581. if (buffer_unwritten(bh)) {
  582. set_buffer_unwritten_io(bh);
  583. bh->b_private = private;
  584. ASSERT(private);
  585. }
  586. }
  587. if (startio) {
  588. bh_arr[index++] = bh;
  589. } else {
  590. set_buffer_dirty(bh);
  591. unlock_buffer(bh);
  592. mark_buffer_dirty(bh);
  593. }
  594. } while (i++, (bh = bh->b_this_page) != head);
  595. if (startio) {
  596. xfs_submit_page(page, wbc, bh_arr, index, 1, index == i);
  597. } else {
  598. unlock_page(page);
  599. }
  600. }
  601. /*
  602. * Convert & write out a cluster of pages in the same extent as defined
  603. * by mp and following the start page.
  604. */
  605. STATIC void
  606. xfs_cluster_write(
  607. struct inode *inode,
  608. pgoff_t tindex,
  609. xfs_iomap_t *iomapp,
  610. struct writeback_control *wbc,
  611. int startio,
  612. int all_bh,
  613. pgoff_t tlast)
  614. {
  615. struct page *page;
  616. for (; tindex <= tlast; tindex++) {
  617. page = xfs_probe_delalloc_page(inode, tindex);
  618. if (!page)
  619. break;
  620. xfs_convert_page(inode, page, iomapp, wbc, NULL,
  621. startio, all_bh);
  622. }
  623. }
  624. /*
  625. * Calling this without startio set means we are being asked to make a dirty
  626. * page ready for freeing it's buffers. When called with startio set then
  627. * we are coming from writepage.
  628. *
  629. * When called with startio set it is important that we write the WHOLE
  630. * page if possible.
  631. * The bh->b_state's cannot know if any of the blocks or which block for
  632. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  633. * only vaild if the page itself isn't completely uptodate. Some layers
  634. * may clear the page dirty flag prior to calling write page, under the
  635. * assumption the entire page will be written out; by not writing out the
  636. * whole page the page can be reused before all valid dirty data is
  637. * written out. Note: in the case of a page that has been dirty'd by
  638. * mapwrite and but partially setup by block_prepare_write the
  639. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  640. * valid state, thus the whole page must be written out thing.
  641. */
  642. STATIC int
  643. xfs_page_state_convert(
  644. struct inode *inode,
  645. struct page *page,
  646. struct writeback_control *wbc,
  647. int startio,
  648. int unmapped) /* also implies page uptodate */
  649. {
  650. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  651. xfs_iomap_t *iomp, iomap;
  652. loff_t offset;
  653. unsigned long p_offset = 0;
  654. __uint64_t end_offset;
  655. pgoff_t end_index, last_index, tlast;
  656. int len, err, i, cnt = 0, uptodate = 1;
  657. int flags;
  658. int page_dirty, delalloc = 0;
  659. /* wait for other IO threads? */
  660. flags = (startio && wbc->sync_mode != WB_SYNC_NONE) ? 0 : BMAPI_TRYLOCK;
  661. /* Is this page beyond the end of the file? */
  662. offset = i_size_read(inode);
  663. end_index = offset >> PAGE_CACHE_SHIFT;
  664. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  665. if (page->index >= end_index) {
  666. if ((page->index >= end_index + 1) ||
  667. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  668. err = -EIO;
  669. goto error;
  670. }
  671. }
  672. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  673. end_offset = min_t(unsigned long long,
  674. offset + PAGE_CACHE_SIZE, i_size_read(inode));
  675. bh = head = page_buffers(page);
  676. iomp = NULL;
  677. /*
  678. * page_dirty is initially a count of buffers on the page and
  679. * is decrememted as we move each into a cleanable state.
  680. */
  681. len = bh->b_size;
  682. page_dirty = PAGE_CACHE_SIZE / len;
  683. do {
  684. if (offset >= end_offset)
  685. break;
  686. if (!buffer_uptodate(bh))
  687. uptodate = 0;
  688. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio)
  689. continue;
  690. if (iomp) {
  691. iomp = xfs_offset_to_map(page, &iomap, p_offset);
  692. }
  693. /*
  694. * First case, map an unwritten extent and prepare for
  695. * extent state conversion transaction on completion.
  696. */
  697. if (buffer_unwritten(bh)) {
  698. if (!startio)
  699. continue;
  700. if (!iomp) {
  701. err = xfs_map_blocks(inode, offset, len, &iomap,
  702. BMAPI_READ|BMAPI_IGNSTATE);
  703. if (err) {
  704. goto error;
  705. }
  706. iomp = xfs_offset_to_map(page, &iomap,
  707. p_offset);
  708. }
  709. if (iomp) {
  710. if (!bh->b_end_io) {
  711. err = xfs_map_unwritten(inode, page,
  712. head, bh, p_offset,
  713. inode->i_blkbits, iomp,
  714. wbc, startio, unmapped);
  715. if (err) {
  716. goto error;
  717. }
  718. } else {
  719. set_bit(BH_Lock, &bh->b_state);
  720. }
  721. BUG_ON(!buffer_locked(bh));
  722. bh_arr[cnt++] = bh;
  723. page_dirty--;
  724. }
  725. /*
  726. * Second case, allocate space for a delalloc buffer.
  727. * We can return EAGAIN here in the release page case.
  728. */
  729. } else if (buffer_delay(bh)) {
  730. if (!iomp) {
  731. delalloc = 1;
  732. err = xfs_map_blocks(inode, offset, len, &iomap,
  733. BMAPI_ALLOCATE | flags);
  734. if (err) {
  735. goto error;
  736. }
  737. iomp = xfs_offset_to_map(page, &iomap,
  738. p_offset);
  739. }
  740. if (iomp) {
  741. xfs_map_at_offset(page, bh, p_offset,
  742. inode->i_blkbits, iomp);
  743. if (startio) {
  744. bh_arr[cnt++] = bh;
  745. } else {
  746. set_buffer_dirty(bh);
  747. unlock_buffer(bh);
  748. mark_buffer_dirty(bh);
  749. }
  750. page_dirty--;
  751. }
  752. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  753. (unmapped || startio)) {
  754. if (!buffer_mapped(bh)) {
  755. int size;
  756. /*
  757. * Getting here implies an unmapped buffer
  758. * was found, and we are in a path where we
  759. * need to write the whole page out.
  760. */
  761. if (!iomp) {
  762. size = xfs_probe_unmapped_cluster(
  763. inode, page, bh, head);
  764. err = xfs_map_blocks(inode, offset,
  765. size, &iomap,
  766. BMAPI_WRITE|BMAPI_MMAP);
  767. if (err) {
  768. goto error;
  769. }
  770. iomp = xfs_offset_to_map(page, &iomap,
  771. p_offset);
  772. }
  773. if (iomp) {
  774. xfs_map_at_offset(page,
  775. bh, p_offset,
  776. inode->i_blkbits, iomp);
  777. if (startio) {
  778. bh_arr[cnt++] = bh;
  779. } else {
  780. set_buffer_dirty(bh);
  781. unlock_buffer(bh);
  782. mark_buffer_dirty(bh);
  783. }
  784. page_dirty--;
  785. }
  786. } else if (startio) {
  787. if (buffer_uptodate(bh) &&
  788. !test_and_set_bit(BH_Lock, &bh->b_state)) {
  789. bh_arr[cnt++] = bh;
  790. page_dirty--;
  791. }
  792. }
  793. }
  794. } while (offset += len, p_offset += len,
  795. ((bh = bh->b_this_page) != head));
  796. if (uptodate && bh == head)
  797. SetPageUptodate(page);
  798. if (startio)
  799. xfs_submit_page(page, wbc, bh_arr, cnt, 0, 1);
  800. if (iomp) {
  801. tlast = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
  802. PAGE_CACHE_SHIFT;
  803. if (delalloc && (tlast > last_index))
  804. tlast = last_index;
  805. xfs_cluster_write(inode, page->index + 1, iomp, wbc,
  806. startio, unmapped, tlast);
  807. }
  808. return page_dirty;
  809. error:
  810. for (i = 0; i < cnt; i++) {
  811. unlock_buffer(bh_arr[i]);
  812. }
  813. /*
  814. * If it's delalloc and we have nowhere to put it,
  815. * throw it away, unless the lower layers told
  816. * us to try again.
  817. */
  818. if (err != -EAGAIN) {
  819. if (!unmapped) {
  820. block_invalidatepage(page, 0);
  821. }
  822. ClearPageUptodate(page);
  823. }
  824. return err;
  825. }
  826. STATIC int
  827. __linvfs_get_block(
  828. struct inode *inode,
  829. sector_t iblock,
  830. unsigned long blocks,
  831. struct buffer_head *bh_result,
  832. int create,
  833. int direct,
  834. bmapi_flags_t flags)
  835. {
  836. vnode_t *vp = LINVFS_GET_VP(inode);
  837. xfs_iomap_t iomap;
  838. int retpbbm = 1;
  839. int error;
  840. ssize_t size;
  841. loff_t offset = (loff_t)iblock << inode->i_blkbits;
  842. if (blocks)
  843. size = blocks << inode->i_blkbits;
  844. else
  845. size = 1 << inode->i_blkbits;
  846. VOP_BMAP(vp, offset, size,
  847. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  848. if (error)
  849. return -error;
  850. if (retpbbm == 0)
  851. return 0;
  852. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  853. xfs_daddr_t bn;
  854. loff_t delta;
  855. /* For unwritten extents do not report a disk address on
  856. * the read case (treat as if we're reading into a hole).
  857. */
  858. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  859. delta = offset - iomap.iomap_offset;
  860. delta >>= inode->i_blkbits;
  861. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  862. bn += delta;
  863. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  864. bh_result->b_blocknr = bn;
  865. set_buffer_mapped(bh_result);
  866. }
  867. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  868. if (direct)
  869. bh_result->b_private = inode;
  870. set_buffer_unwritten(bh_result);
  871. set_buffer_delay(bh_result);
  872. }
  873. }
  874. /* If this is a realtime file, data might be on a new device */
  875. bh_result->b_bdev = iomap.iomap_target->pbr_bdev;
  876. /* If we previously allocated a block out beyond eof and
  877. * we are now coming back to use it then we will need to
  878. * flag it as new even if it has a disk address.
  879. */
  880. if (create &&
  881. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  882. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW))) {
  883. set_buffer_new(bh_result);
  884. }
  885. if (iomap.iomap_flags & IOMAP_DELAY) {
  886. BUG_ON(direct);
  887. if (create) {
  888. set_buffer_uptodate(bh_result);
  889. set_buffer_mapped(bh_result);
  890. set_buffer_delay(bh_result);
  891. }
  892. }
  893. if (blocks) {
  894. bh_result->b_size = (ssize_t)min(
  895. (loff_t)(iomap.iomap_bsize - iomap.iomap_delta),
  896. (loff_t)(blocks << inode->i_blkbits));
  897. }
  898. return 0;
  899. }
  900. int
  901. linvfs_get_block(
  902. struct inode *inode,
  903. sector_t iblock,
  904. struct buffer_head *bh_result,
  905. int create)
  906. {
  907. return __linvfs_get_block(inode, iblock, 0, bh_result,
  908. create, 0, BMAPI_WRITE);
  909. }
  910. STATIC int
  911. linvfs_get_blocks_direct(
  912. struct inode *inode,
  913. sector_t iblock,
  914. unsigned long max_blocks,
  915. struct buffer_head *bh_result,
  916. int create)
  917. {
  918. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  919. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  920. }
  921. STATIC ssize_t
  922. linvfs_direct_IO(
  923. int rw,
  924. struct kiocb *iocb,
  925. const struct iovec *iov,
  926. loff_t offset,
  927. unsigned long nr_segs)
  928. {
  929. struct file *file = iocb->ki_filp;
  930. struct inode *inode = file->f_mapping->host;
  931. vnode_t *vp = LINVFS_GET_VP(inode);
  932. xfs_iomap_t iomap;
  933. int maps = 1;
  934. int error;
  935. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  936. if (error)
  937. return -error;
  938. return blockdev_direct_IO_own_locking(rw, iocb, inode,
  939. iomap.iomap_target->pbr_bdev,
  940. iov, offset, nr_segs,
  941. linvfs_get_blocks_direct,
  942. linvfs_unwritten_convert_direct);
  943. }
  944. STATIC sector_t
  945. linvfs_bmap(
  946. struct address_space *mapping,
  947. sector_t block)
  948. {
  949. struct inode *inode = (struct inode *)mapping->host;
  950. vnode_t *vp = LINVFS_GET_VP(inode);
  951. int error;
  952. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  953. VOP_RWLOCK(vp, VRWLOCK_READ);
  954. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  955. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  956. return generic_block_bmap(mapping, block, linvfs_get_block);
  957. }
  958. STATIC int
  959. linvfs_readpage(
  960. struct file *unused,
  961. struct page *page)
  962. {
  963. return mpage_readpage(page, linvfs_get_block);
  964. }
  965. STATIC int
  966. linvfs_readpages(
  967. struct file *unused,
  968. struct address_space *mapping,
  969. struct list_head *pages,
  970. unsigned nr_pages)
  971. {
  972. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  973. }
  974. STATIC void
  975. xfs_count_page_state(
  976. struct page *page,
  977. int *delalloc,
  978. int *unmapped,
  979. int *unwritten)
  980. {
  981. struct buffer_head *bh, *head;
  982. *delalloc = *unmapped = *unwritten = 0;
  983. bh = head = page_buffers(page);
  984. do {
  985. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  986. (*unmapped) = 1;
  987. else if (buffer_unwritten(bh) && !buffer_delay(bh))
  988. clear_buffer_unwritten(bh);
  989. else if (buffer_unwritten(bh))
  990. (*unwritten) = 1;
  991. else if (buffer_delay(bh))
  992. (*delalloc) = 1;
  993. } while ((bh = bh->b_this_page) != head);
  994. }
  995. /*
  996. * writepage: Called from one of two places:
  997. *
  998. * 1. we are flushing a delalloc buffer head.
  999. *
  1000. * 2. we are writing out a dirty page. Typically the page dirty
  1001. * state is cleared before we get here. In this case is it
  1002. * conceivable we have no buffer heads.
  1003. *
  1004. * For delalloc space on the page we need to allocate space and
  1005. * flush it. For unmapped buffer heads on the page we should
  1006. * allocate space if the page is uptodate. For any other dirty
  1007. * buffer heads on the page we should flush them.
  1008. *
  1009. * If we detect that a transaction would be required to flush
  1010. * the page, we have to check the process flags first, if we
  1011. * are already in a transaction or disk I/O during allocations
  1012. * is off, we need to fail the writepage and redirty the page.
  1013. */
  1014. STATIC int
  1015. linvfs_writepage(
  1016. struct page *page,
  1017. struct writeback_control *wbc)
  1018. {
  1019. int error;
  1020. int need_trans;
  1021. int delalloc, unmapped, unwritten;
  1022. struct inode *inode = page->mapping->host;
  1023. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1024. /*
  1025. * We need a transaction if:
  1026. * 1. There are delalloc buffers on the page
  1027. * 2. The page is uptodate and we have unmapped buffers
  1028. * 3. The page is uptodate and we have no buffers
  1029. * 4. There are unwritten buffers on the page
  1030. */
  1031. if (!page_has_buffers(page)) {
  1032. unmapped = 1;
  1033. need_trans = 1;
  1034. } else {
  1035. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1036. if (!PageUptodate(page))
  1037. unmapped = 0;
  1038. need_trans = delalloc + unmapped + unwritten;
  1039. }
  1040. /*
  1041. * If we need a transaction and the process flags say
  1042. * we are already in a transaction, or no IO is allowed
  1043. * then mark the page dirty again and leave the page
  1044. * as is.
  1045. */
  1046. if (PFLAGS_TEST_FSTRANS() && need_trans)
  1047. goto out_fail;
  1048. /*
  1049. * Delay hooking up buffer heads until we have
  1050. * made our go/no-go decision.
  1051. */
  1052. if (!page_has_buffers(page))
  1053. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1054. /*
  1055. * Convert delayed allocate, unwritten or unmapped space
  1056. * to real space and flush out to disk.
  1057. */
  1058. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1059. if (error == -EAGAIN)
  1060. goto out_fail;
  1061. if (unlikely(error < 0))
  1062. goto out_unlock;
  1063. return 0;
  1064. out_fail:
  1065. redirty_page_for_writepage(wbc, page);
  1066. unlock_page(page);
  1067. return 0;
  1068. out_unlock:
  1069. unlock_page(page);
  1070. return error;
  1071. }
  1072. /*
  1073. * Called to move a page into cleanable state - and from there
  1074. * to be released. Possibly the page is already clean. We always
  1075. * have buffer heads in this call.
  1076. *
  1077. * Returns 0 if the page is ok to release, 1 otherwise.
  1078. *
  1079. * Possible scenarios are:
  1080. *
  1081. * 1. We are being called to release a page which has been written
  1082. * to via regular I/O. buffer heads will be dirty and possibly
  1083. * delalloc. If no delalloc buffer heads in this case then we
  1084. * can just return zero.
  1085. *
  1086. * 2. We are called to release a page which has been written via
  1087. * mmap, all we need to do is ensure there is no delalloc
  1088. * state in the buffer heads, if not we can let the caller
  1089. * free them and we should come back later via writepage.
  1090. */
  1091. STATIC int
  1092. linvfs_release_page(
  1093. struct page *page,
  1094. int gfp_mask)
  1095. {
  1096. struct inode *inode = page->mapping->host;
  1097. int dirty, delalloc, unmapped, unwritten;
  1098. struct writeback_control wbc = {
  1099. .sync_mode = WB_SYNC_ALL,
  1100. .nr_to_write = 1,
  1101. };
  1102. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1103. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1104. if (!delalloc && !unwritten)
  1105. goto free_buffers;
  1106. if (!(gfp_mask & __GFP_FS))
  1107. return 0;
  1108. /* If we are already inside a transaction or the thread cannot
  1109. * do I/O, we cannot release this page.
  1110. */
  1111. if (PFLAGS_TEST_FSTRANS())
  1112. return 0;
  1113. /*
  1114. * Convert delalloc space to real space, do not flush the
  1115. * data out to disk, that will be done by the caller.
  1116. * Never need to allocate space here - we will always
  1117. * come back to writepage in that case.
  1118. */
  1119. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1120. if (dirty == 0 && !unwritten)
  1121. goto free_buffers;
  1122. return 0;
  1123. free_buffers:
  1124. return try_to_free_buffers(page);
  1125. }
  1126. STATIC int
  1127. linvfs_prepare_write(
  1128. struct file *file,
  1129. struct page *page,
  1130. unsigned int from,
  1131. unsigned int to)
  1132. {
  1133. return block_prepare_write(page, from, to, linvfs_get_block);
  1134. }
  1135. struct address_space_operations linvfs_aops = {
  1136. .readpage = linvfs_readpage,
  1137. .readpages = linvfs_readpages,
  1138. .writepage = linvfs_writepage,
  1139. .sync_page = block_sync_page,
  1140. .releasepage = linvfs_release_page,
  1141. .prepare_write = linvfs_prepare_write,
  1142. .commit_write = generic_commit_write,
  1143. .bmap = linvfs_bmap,
  1144. .direct_IO = linvfs_direct_IO,
  1145. };