xfs_aops.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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. /*
  101. * Schedule IO completion handling on a xfsdatad if this was
  102. * the final hold on this ioend.
  103. */
  104. STATIC void
  105. xfs_finish_ioend(
  106. xfs_ioend_t *ioend)
  107. {
  108. if (atomic_dec_and_test(&ioend->io_remaining))
  109. queue_work(xfsdatad_workqueue, &ioend->io_work);
  110. }
  111. STATIC void
  112. xfs_destroy_ioend(
  113. xfs_ioend_t *ioend)
  114. {
  115. vn_iowake(ioend->io_vnode);
  116. mempool_free(ioend, xfs_ioend_pool);
  117. }
  118. /*
  119. * Issue transactions to convert a buffer range from unwritten
  120. * to written extents.
  121. */
  122. STATIC void
  123. xfs_end_bio_unwritten(
  124. void *data)
  125. {
  126. xfs_ioend_t *ioend = data;
  127. vnode_t *vp = ioend->io_vnode;
  128. xfs_off_t offset = ioend->io_offset;
  129. size_t size = ioend->io_size;
  130. struct buffer_head *bh, *next;
  131. int error;
  132. if (ioend->io_uptodate)
  133. VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
  134. /* ioend->io_buffer_head is only non-NULL for buffered I/O */
  135. for (bh = ioend->io_buffer_head; bh; bh = next) {
  136. next = bh->b_private;
  137. bh->b_end_io = NULL;
  138. clear_buffer_unwritten(bh);
  139. end_buffer_async_write(bh, ioend->io_uptodate);
  140. }
  141. xfs_destroy_ioend(ioend);
  142. }
  143. /*
  144. * Allocate and initialise an IO completion structure.
  145. * We need to track unwritten extent write completion here initially.
  146. * We'll need to extend this for updating the ondisk inode size later
  147. * (vs. incore size).
  148. */
  149. STATIC xfs_ioend_t *
  150. xfs_alloc_ioend(
  151. struct inode *inode)
  152. {
  153. xfs_ioend_t *ioend;
  154. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  155. /*
  156. * Set the count to 1 initially, which will prevent an I/O
  157. * completion callback from happening before we have started
  158. * all the I/O from calling the completion routine too early.
  159. */
  160. atomic_set(&ioend->io_remaining, 1);
  161. ioend->io_uptodate = 1; /* cleared if any I/O fails */
  162. ioend->io_vnode = LINVFS_GET_VP(inode);
  163. ioend->io_buffer_head = NULL;
  164. atomic_inc(&ioend->io_vnode->v_iocount);
  165. ioend->io_offset = 0;
  166. ioend->io_size = 0;
  167. INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
  168. return ioend;
  169. }
  170. void
  171. linvfs_unwritten_done(
  172. struct buffer_head *bh,
  173. int uptodate)
  174. {
  175. xfs_ioend_t *ioend = bh->b_private;
  176. static spinlock_t unwritten_done_lock = SPIN_LOCK_UNLOCKED;
  177. unsigned long flags;
  178. ASSERT(buffer_unwritten(bh));
  179. bh->b_end_io = NULL;
  180. if (!uptodate)
  181. ioend->io_uptodate = 0;
  182. /*
  183. * Deep magic here. We reuse b_private in the buffer_heads to build
  184. * a chain for completing the I/O from user context after we've issued
  185. * a transaction to convert the unwritten extent.
  186. */
  187. spin_lock_irqsave(&unwritten_done_lock, flags);
  188. bh->b_private = ioend->io_buffer_head;
  189. ioend->io_buffer_head = bh;
  190. spin_unlock_irqrestore(&unwritten_done_lock, flags);
  191. xfs_finish_ioend(ioend);
  192. }
  193. STATIC int
  194. xfs_map_blocks(
  195. struct inode *inode,
  196. loff_t offset,
  197. ssize_t count,
  198. xfs_iomap_t *mapp,
  199. int flags)
  200. {
  201. vnode_t *vp = LINVFS_GET_VP(inode);
  202. int error, nmaps = 1;
  203. VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
  204. if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
  205. VMODIFY(vp);
  206. return -error;
  207. }
  208. /*
  209. * Finds the corresponding mapping in block @map array of the
  210. * given @offset within a @page.
  211. */
  212. STATIC xfs_iomap_t *
  213. xfs_offset_to_map(
  214. struct page *page,
  215. xfs_iomap_t *iomapp,
  216. unsigned long offset)
  217. {
  218. loff_t full_offset; /* offset from start of file */
  219. ASSERT(offset < PAGE_CACHE_SIZE);
  220. full_offset = page->index; /* NB: using 64bit number */
  221. full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
  222. full_offset += offset; /* offset from page start */
  223. if (full_offset < iomapp->iomap_offset)
  224. return NULL;
  225. if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
  226. return iomapp;
  227. return NULL;
  228. }
  229. STATIC void
  230. xfs_map_at_offset(
  231. struct page *page,
  232. struct buffer_head *bh,
  233. unsigned long offset,
  234. int block_bits,
  235. xfs_iomap_t *iomapp)
  236. {
  237. xfs_daddr_t bn;
  238. loff_t delta;
  239. int sector_shift;
  240. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  241. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  242. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  243. delta = page->index;
  244. delta <<= PAGE_CACHE_SHIFT;
  245. delta += offset;
  246. delta -= iomapp->iomap_offset;
  247. delta >>= block_bits;
  248. sector_shift = block_bits - BBSHIFT;
  249. bn = iomapp->iomap_bn >> sector_shift;
  250. bn += delta;
  251. BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
  252. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  253. lock_buffer(bh);
  254. bh->b_blocknr = bn;
  255. bh->b_bdev = iomapp->iomap_target->pbr_bdev;
  256. set_buffer_mapped(bh);
  257. clear_buffer_delay(bh);
  258. }
  259. /*
  260. * Look for a page at index which is unlocked and contains our
  261. * unwritten extent flagged buffers at its head. Returns page
  262. * locked and with an extra reference count, and length of the
  263. * unwritten extent component on this page that we can write,
  264. * in units of filesystem blocks.
  265. */
  266. STATIC struct page *
  267. xfs_probe_unwritten_page(
  268. struct address_space *mapping,
  269. pgoff_t index,
  270. xfs_iomap_t *iomapp,
  271. xfs_ioend_t *ioend,
  272. unsigned long max_offset,
  273. unsigned long *fsbs,
  274. unsigned int bbits)
  275. {
  276. struct page *page;
  277. page = find_trylock_page(mapping, index);
  278. if (!page)
  279. return NULL;
  280. if (PageWriteback(page))
  281. goto out;
  282. if (page->mapping && page_has_buffers(page)) {
  283. struct buffer_head *bh, *head;
  284. unsigned long p_offset = 0;
  285. *fsbs = 0;
  286. bh = head = page_buffers(page);
  287. do {
  288. if (!buffer_unwritten(bh) || !buffer_uptodate(bh))
  289. break;
  290. if (!xfs_offset_to_map(page, iomapp, p_offset))
  291. break;
  292. if (p_offset >= max_offset)
  293. break;
  294. xfs_map_at_offset(page, bh, p_offset, bbits, iomapp);
  295. set_buffer_unwritten_io(bh);
  296. bh->b_private = ioend;
  297. p_offset += bh->b_size;
  298. (*fsbs)++;
  299. } while ((bh = bh->b_this_page) != head);
  300. if (p_offset)
  301. return page;
  302. }
  303. out:
  304. unlock_page(page);
  305. return NULL;
  306. }
  307. /*
  308. * Look for a page at index which is unlocked and not mapped
  309. * yet - clustering for mmap write case.
  310. */
  311. STATIC unsigned int
  312. xfs_probe_unmapped_page(
  313. struct address_space *mapping,
  314. pgoff_t index,
  315. unsigned int pg_offset)
  316. {
  317. struct page *page;
  318. int ret = 0;
  319. page = find_trylock_page(mapping, index);
  320. if (!page)
  321. return 0;
  322. if (PageWriteback(page))
  323. goto out;
  324. if (page->mapping && PageDirty(page)) {
  325. if (page_has_buffers(page)) {
  326. struct buffer_head *bh, *head;
  327. bh = head = page_buffers(page);
  328. do {
  329. if (buffer_mapped(bh) || !buffer_uptodate(bh))
  330. break;
  331. ret += bh->b_size;
  332. if (ret >= pg_offset)
  333. break;
  334. } while ((bh = bh->b_this_page) != head);
  335. } else
  336. ret = PAGE_CACHE_SIZE;
  337. }
  338. out:
  339. unlock_page(page);
  340. return ret;
  341. }
  342. STATIC unsigned int
  343. xfs_probe_unmapped_cluster(
  344. struct inode *inode,
  345. struct page *startpage,
  346. struct buffer_head *bh,
  347. struct buffer_head *head)
  348. {
  349. pgoff_t tindex, tlast, tloff;
  350. unsigned int pg_offset, len, total = 0;
  351. struct address_space *mapping = inode->i_mapping;
  352. /* First sum forwards in this page */
  353. do {
  354. if (buffer_mapped(bh))
  355. break;
  356. total += bh->b_size;
  357. } while ((bh = bh->b_this_page) != head);
  358. /* If we reached the end of the page, sum forwards in
  359. * following pages.
  360. */
  361. if (bh == head) {
  362. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  363. /* Prune this back to avoid pathological behavior */
  364. tloff = min(tlast, startpage->index + 64);
  365. for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
  366. len = xfs_probe_unmapped_page(mapping, tindex,
  367. PAGE_CACHE_SIZE);
  368. if (!len)
  369. return total;
  370. total += len;
  371. }
  372. if (tindex == tlast &&
  373. (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  374. total += xfs_probe_unmapped_page(mapping,
  375. tindex, pg_offset);
  376. }
  377. }
  378. return total;
  379. }
  380. /*
  381. * Probe for a given page (index) in the inode and test if it is delayed
  382. * and without unwritten buffers. Returns page locked and with an extra
  383. * reference count.
  384. */
  385. STATIC struct page *
  386. xfs_probe_delalloc_page(
  387. struct inode *inode,
  388. pgoff_t index)
  389. {
  390. struct page *page;
  391. page = find_trylock_page(inode->i_mapping, index);
  392. if (!page)
  393. return NULL;
  394. if (PageWriteback(page))
  395. goto out;
  396. if (page->mapping && page_has_buffers(page)) {
  397. struct buffer_head *bh, *head;
  398. int acceptable = 0;
  399. bh = head = page_buffers(page);
  400. do {
  401. if (buffer_unwritten(bh)) {
  402. acceptable = 0;
  403. break;
  404. } else if (buffer_delay(bh)) {
  405. acceptable = 1;
  406. }
  407. } while ((bh = bh->b_this_page) != head);
  408. if (acceptable)
  409. return page;
  410. }
  411. out:
  412. unlock_page(page);
  413. return NULL;
  414. }
  415. STATIC int
  416. xfs_map_unwritten(
  417. struct inode *inode,
  418. struct page *start_page,
  419. struct buffer_head *head,
  420. struct buffer_head *curr,
  421. unsigned long p_offset,
  422. int block_bits,
  423. xfs_iomap_t *iomapp,
  424. struct writeback_control *wbc,
  425. int startio,
  426. int all_bh)
  427. {
  428. struct buffer_head *bh = curr;
  429. xfs_iomap_t *tmp;
  430. xfs_ioend_t *ioend;
  431. loff_t offset;
  432. unsigned long nblocks = 0;
  433. offset = start_page->index;
  434. offset <<= PAGE_CACHE_SHIFT;
  435. offset += p_offset;
  436. ioend = xfs_alloc_ioend(inode);
  437. /* First map forwards in the page consecutive buffers
  438. * covering this unwritten extent
  439. */
  440. do {
  441. if (!buffer_unwritten(bh))
  442. break;
  443. tmp = xfs_offset_to_map(start_page, iomapp, p_offset);
  444. if (!tmp)
  445. break;
  446. xfs_map_at_offset(start_page, bh, p_offset, block_bits, iomapp);
  447. set_buffer_unwritten_io(bh);
  448. bh->b_private = ioend;
  449. p_offset += bh->b_size;
  450. nblocks++;
  451. } while ((bh = bh->b_this_page) != head);
  452. atomic_add(nblocks, &ioend->io_remaining);
  453. /* If we reached the end of the page, map forwards in any
  454. * following pages which are also covered by this extent.
  455. */
  456. if (bh == head) {
  457. struct address_space *mapping = inode->i_mapping;
  458. pgoff_t tindex, tloff, tlast;
  459. unsigned long bs;
  460. unsigned int pg_offset, bbits = inode->i_blkbits;
  461. struct page *page;
  462. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  463. tloff = (iomapp->iomap_offset + iomapp->iomap_bsize) >> PAGE_CACHE_SHIFT;
  464. tloff = min(tlast, tloff);
  465. for (tindex = start_page->index + 1; tindex < tloff; tindex++) {
  466. page = xfs_probe_unwritten_page(mapping,
  467. tindex, iomapp, ioend,
  468. PAGE_CACHE_SIZE, &bs, bbits);
  469. if (!page)
  470. break;
  471. nblocks += bs;
  472. atomic_add(bs, &ioend->io_remaining);
  473. xfs_convert_page(inode, page, iomapp, wbc, ioend,
  474. startio, all_bh);
  475. /* stop if converting the next page might add
  476. * enough blocks that the corresponding byte
  477. * count won't fit in our ulong page buf length */
  478. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  479. goto enough;
  480. }
  481. if (tindex == tlast &&
  482. (pg_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1)))) {
  483. page = xfs_probe_unwritten_page(mapping,
  484. tindex, iomapp, ioend,
  485. pg_offset, &bs, bbits);
  486. if (page) {
  487. nblocks += bs;
  488. atomic_add(bs, &ioend->io_remaining);
  489. xfs_convert_page(inode, page, iomapp, wbc, ioend,
  490. startio, all_bh);
  491. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  492. goto enough;
  493. }
  494. }
  495. }
  496. enough:
  497. ioend->io_size = (xfs_off_t)nblocks << block_bits;
  498. ioend->io_offset = offset;
  499. xfs_finish_ioend(ioend);
  500. return 0;
  501. }
  502. STATIC void
  503. xfs_submit_page(
  504. struct page *page,
  505. struct writeback_control *wbc,
  506. struct buffer_head *bh_arr[],
  507. int bh_count,
  508. int probed_page,
  509. int clear_dirty)
  510. {
  511. struct buffer_head *bh;
  512. int i;
  513. BUG_ON(PageWriteback(page));
  514. if (bh_count)
  515. set_page_writeback(page);
  516. if (clear_dirty)
  517. clear_page_dirty(page);
  518. unlock_page(page);
  519. if (bh_count) {
  520. for (i = 0; i < bh_count; i++) {
  521. bh = bh_arr[i];
  522. mark_buffer_async_write(bh);
  523. if (buffer_unwritten(bh))
  524. set_buffer_unwritten_io(bh);
  525. set_buffer_uptodate(bh);
  526. clear_buffer_dirty(bh);
  527. }
  528. for (i = 0; i < bh_count; i++)
  529. submit_bh(WRITE, bh_arr[i]);
  530. if (probed_page && clear_dirty)
  531. wbc->nr_to_write--; /* Wrote an "extra" page */
  532. }
  533. }
  534. /*
  535. * Allocate & map buffers for page given the extent map. Write it out.
  536. * except for the original page of a writepage, this is called on
  537. * delalloc/unwritten pages only, for the original page it is possible
  538. * that the page has no mapping at all.
  539. */
  540. STATIC void
  541. xfs_convert_page(
  542. struct inode *inode,
  543. struct page *page,
  544. xfs_iomap_t *iomapp,
  545. struct writeback_control *wbc,
  546. void *private,
  547. int startio,
  548. int all_bh)
  549. {
  550. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  551. xfs_iomap_t *mp = iomapp, *tmp;
  552. unsigned long offset, end_offset;
  553. int index = 0;
  554. int bbits = inode->i_blkbits;
  555. int len, page_dirty;
  556. end_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1));
  557. /*
  558. * page_dirty is initially a count of buffers on the page before
  559. * EOF and is decrememted as we move each into a cleanable state.
  560. */
  561. len = 1 << inode->i_blkbits;
  562. end_offset = max(end_offset, PAGE_CACHE_SIZE);
  563. end_offset = roundup(end_offset, len);
  564. page_dirty = end_offset / len;
  565. offset = 0;
  566. bh = head = page_buffers(page);
  567. do {
  568. if (offset >= end_offset)
  569. break;
  570. if (!(PageUptodate(page) || buffer_uptodate(bh)))
  571. continue;
  572. if (buffer_mapped(bh) && all_bh &&
  573. !(buffer_unwritten(bh) || buffer_delay(bh))) {
  574. if (startio) {
  575. lock_buffer(bh);
  576. bh_arr[index++] = bh;
  577. page_dirty--;
  578. }
  579. continue;
  580. }
  581. tmp = xfs_offset_to_map(page, mp, offset);
  582. if (!tmp)
  583. continue;
  584. ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
  585. ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
  586. /* If this is a new unwritten extent buffer (i.e. one
  587. * that we haven't passed in private data for, we must
  588. * now map this buffer too.
  589. */
  590. if (buffer_unwritten(bh) && !bh->b_end_io) {
  591. ASSERT(tmp->iomap_flags & IOMAP_UNWRITTEN);
  592. xfs_map_unwritten(inode, page, head, bh, offset,
  593. bbits, tmp, wbc, startio, all_bh);
  594. } else if (! (buffer_unwritten(bh) && buffer_locked(bh))) {
  595. xfs_map_at_offset(page, bh, offset, bbits, tmp);
  596. if (buffer_unwritten(bh)) {
  597. set_buffer_unwritten_io(bh);
  598. bh->b_private = private;
  599. ASSERT(private);
  600. }
  601. }
  602. if (startio) {
  603. bh_arr[index++] = bh;
  604. } else {
  605. set_buffer_dirty(bh);
  606. unlock_buffer(bh);
  607. mark_buffer_dirty(bh);
  608. }
  609. page_dirty--;
  610. } while (offset += len, (bh = bh->b_this_page) != head);
  611. if (startio && index) {
  612. xfs_submit_page(page, wbc, bh_arr, index, 1, !page_dirty);
  613. } else {
  614. unlock_page(page);
  615. }
  616. }
  617. /*
  618. * Convert & write out a cluster of pages in the same extent as defined
  619. * by mp and following the start page.
  620. */
  621. STATIC void
  622. xfs_cluster_write(
  623. struct inode *inode,
  624. pgoff_t tindex,
  625. xfs_iomap_t *iomapp,
  626. struct writeback_control *wbc,
  627. int startio,
  628. int all_bh,
  629. pgoff_t tlast)
  630. {
  631. struct page *page;
  632. for (; tindex <= tlast; tindex++) {
  633. page = xfs_probe_delalloc_page(inode, tindex);
  634. if (!page)
  635. break;
  636. xfs_convert_page(inode, page, iomapp, wbc, NULL,
  637. startio, all_bh);
  638. }
  639. }
  640. /*
  641. * Calling this without startio set means we are being asked to make a dirty
  642. * page ready for freeing it's buffers. When called with startio set then
  643. * we are coming from writepage.
  644. *
  645. * When called with startio set it is important that we write the WHOLE
  646. * page if possible.
  647. * The bh->b_state's cannot know if any of the blocks or which block for
  648. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  649. * only vaild if the page itself isn't completely uptodate. Some layers
  650. * may clear the page dirty flag prior to calling write page, under the
  651. * assumption the entire page will be written out; by not writing out the
  652. * whole page the page can be reused before all valid dirty data is
  653. * written out. Note: in the case of a page that has been dirty'd by
  654. * mapwrite and but partially setup by block_prepare_write the
  655. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  656. * valid state, thus the whole page must be written out thing.
  657. */
  658. STATIC int
  659. xfs_page_state_convert(
  660. struct inode *inode,
  661. struct page *page,
  662. struct writeback_control *wbc,
  663. int startio,
  664. int unmapped) /* also implies page uptodate */
  665. {
  666. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  667. xfs_iomap_t *iomp, iomap;
  668. loff_t offset;
  669. unsigned long p_offset = 0;
  670. __uint64_t end_offset;
  671. pgoff_t end_index, last_index, tlast;
  672. int len, err, i, cnt = 0, uptodate = 1;
  673. int flags;
  674. int page_dirty;
  675. /* wait for other IO threads? */
  676. flags = (startio && wbc->sync_mode != WB_SYNC_NONE) ? 0 : BMAPI_TRYLOCK;
  677. /* Is this page beyond the end of the file? */
  678. offset = i_size_read(inode);
  679. end_index = offset >> PAGE_CACHE_SHIFT;
  680. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  681. if (page->index >= end_index) {
  682. if ((page->index >= end_index + 1) ||
  683. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  684. err = -EIO;
  685. goto error;
  686. }
  687. }
  688. end_offset = min_t(unsigned long long,
  689. (loff_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  690. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  691. /*
  692. * page_dirty is initially a count of buffers on the page before
  693. * EOF and is decrememted as we move each into a cleanable state.
  694. */
  695. len = 1 << inode->i_blkbits;
  696. p_offset = max(p_offset, PAGE_CACHE_SIZE);
  697. p_offset = roundup(p_offset, len);
  698. page_dirty = p_offset / len;
  699. iomp = NULL;
  700. p_offset = 0;
  701. bh = head = page_buffers(page);
  702. do {
  703. if (offset >= end_offset)
  704. break;
  705. if (!buffer_uptodate(bh))
  706. uptodate = 0;
  707. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio)
  708. continue;
  709. if (iomp) {
  710. iomp = xfs_offset_to_map(page, &iomap, p_offset);
  711. }
  712. /*
  713. * First case, map an unwritten extent and prepare for
  714. * extent state conversion transaction on completion.
  715. */
  716. if (buffer_unwritten(bh)) {
  717. if (!startio)
  718. continue;
  719. if (!iomp) {
  720. err = xfs_map_blocks(inode, offset, len, &iomap,
  721. BMAPI_WRITE|BMAPI_IGNSTATE);
  722. if (err) {
  723. goto error;
  724. }
  725. iomp = xfs_offset_to_map(page, &iomap,
  726. p_offset);
  727. }
  728. if (iomp) {
  729. if (!bh->b_end_io) {
  730. err = xfs_map_unwritten(inode, page,
  731. head, bh, p_offset,
  732. inode->i_blkbits, iomp,
  733. wbc, startio, unmapped);
  734. if (err) {
  735. goto error;
  736. }
  737. } else {
  738. set_bit(BH_Lock, &bh->b_state);
  739. }
  740. BUG_ON(!buffer_locked(bh));
  741. bh_arr[cnt++] = bh;
  742. page_dirty--;
  743. }
  744. /*
  745. * Second case, allocate space for a delalloc buffer.
  746. * We can return EAGAIN here in the release page case.
  747. */
  748. } else if (buffer_delay(bh)) {
  749. if (!iomp) {
  750. err = xfs_map_blocks(inode, offset, len, &iomap,
  751. BMAPI_ALLOCATE | flags);
  752. if (err) {
  753. goto error;
  754. }
  755. iomp = xfs_offset_to_map(page, &iomap,
  756. p_offset);
  757. }
  758. if (iomp) {
  759. xfs_map_at_offset(page, bh, p_offset,
  760. inode->i_blkbits, iomp);
  761. if (startio) {
  762. bh_arr[cnt++] = bh;
  763. } else {
  764. set_buffer_dirty(bh);
  765. unlock_buffer(bh);
  766. mark_buffer_dirty(bh);
  767. }
  768. page_dirty--;
  769. }
  770. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  771. (unmapped || startio)) {
  772. if (!buffer_mapped(bh)) {
  773. int size;
  774. /*
  775. * Getting here implies an unmapped buffer
  776. * was found, and we are in a path where we
  777. * need to write the whole page out.
  778. */
  779. if (!iomp) {
  780. size = xfs_probe_unmapped_cluster(
  781. inode, page, bh, head);
  782. err = xfs_map_blocks(inode, offset,
  783. size, &iomap,
  784. BMAPI_WRITE|BMAPI_MMAP);
  785. if (err) {
  786. goto error;
  787. }
  788. iomp = xfs_offset_to_map(page, &iomap,
  789. p_offset);
  790. }
  791. if (iomp) {
  792. xfs_map_at_offset(page,
  793. bh, p_offset,
  794. inode->i_blkbits, iomp);
  795. if (startio) {
  796. bh_arr[cnt++] = bh;
  797. } else {
  798. set_buffer_dirty(bh);
  799. unlock_buffer(bh);
  800. mark_buffer_dirty(bh);
  801. }
  802. page_dirty--;
  803. }
  804. } else if (startio) {
  805. if (buffer_uptodate(bh) &&
  806. !test_and_set_bit(BH_Lock, &bh->b_state)) {
  807. bh_arr[cnt++] = bh;
  808. page_dirty--;
  809. }
  810. }
  811. }
  812. } while (offset += len, p_offset += len,
  813. ((bh = bh->b_this_page) != head));
  814. if (uptodate && bh == head)
  815. SetPageUptodate(page);
  816. if (startio) {
  817. xfs_submit_page(page, wbc, bh_arr, cnt, 0, !page_dirty);
  818. }
  819. if (iomp) {
  820. offset = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
  821. PAGE_CACHE_SHIFT;
  822. tlast = min_t(pgoff_t, offset, last_index);
  823. xfs_cluster_write(inode, page->index + 1, iomp, wbc,
  824. startio, unmapped, tlast);
  825. }
  826. return page_dirty;
  827. error:
  828. for (i = 0; i < cnt; i++) {
  829. unlock_buffer(bh_arr[i]);
  830. }
  831. /*
  832. * If it's delalloc and we have nowhere to put it,
  833. * throw it away, unless the lower layers told
  834. * us to try again.
  835. */
  836. if (err != -EAGAIN) {
  837. if (!unmapped) {
  838. block_invalidatepage(page, 0);
  839. }
  840. ClearPageUptodate(page);
  841. }
  842. return err;
  843. }
  844. STATIC int
  845. __linvfs_get_block(
  846. struct inode *inode,
  847. sector_t iblock,
  848. unsigned long blocks,
  849. struct buffer_head *bh_result,
  850. int create,
  851. int direct,
  852. bmapi_flags_t flags)
  853. {
  854. vnode_t *vp = LINVFS_GET_VP(inode);
  855. xfs_iomap_t iomap;
  856. int retpbbm = 1;
  857. int error;
  858. ssize_t size;
  859. loff_t offset = (loff_t)iblock << inode->i_blkbits;
  860. if (blocks)
  861. size = blocks << inode->i_blkbits;
  862. else
  863. size = 1 << inode->i_blkbits;
  864. VOP_BMAP(vp, offset, size,
  865. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  866. if (error)
  867. return -error;
  868. if (retpbbm == 0)
  869. return 0;
  870. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  871. xfs_daddr_t bn;
  872. loff_t delta;
  873. /* For unwritten extents do not report a disk address on
  874. * the read case (treat as if we're reading into a hole).
  875. */
  876. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  877. delta = offset - iomap.iomap_offset;
  878. delta >>= inode->i_blkbits;
  879. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  880. bn += delta;
  881. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  882. bh_result->b_blocknr = bn;
  883. set_buffer_mapped(bh_result);
  884. }
  885. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  886. if (direct)
  887. bh_result->b_private = inode;
  888. set_buffer_unwritten(bh_result);
  889. set_buffer_delay(bh_result);
  890. }
  891. }
  892. /* If this is a realtime file, data might be on a new device */
  893. bh_result->b_bdev = iomap.iomap_target->pbr_bdev;
  894. /* If we previously allocated a block out beyond eof and
  895. * we are now coming back to use it then we will need to
  896. * flag it as new even if it has a disk address.
  897. */
  898. if (create &&
  899. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  900. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW))) {
  901. set_buffer_new(bh_result);
  902. }
  903. if (iomap.iomap_flags & IOMAP_DELAY) {
  904. BUG_ON(direct);
  905. if (create) {
  906. set_buffer_uptodate(bh_result);
  907. set_buffer_mapped(bh_result);
  908. set_buffer_delay(bh_result);
  909. }
  910. }
  911. if (blocks) {
  912. bh_result->b_size = (ssize_t)min(
  913. (loff_t)(iomap.iomap_bsize - iomap.iomap_delta),
  914. (loff_t)(blocks << inode->i_blkbits));
  915. }
  916. return 0;
  917. }
  918. int
  919. linvfs_get_block(
  920. struct inode *inode,
  921. sector_t iblock,
  922. struct buffer_head *bh_result,
  923. int create)
  924. {
  925. return __linvfs_get_block(inode, iblock, 0, bh_result,
  926. create, 0, BMAPI_WRITE);
  927. }
  928. STATIC int
  929. linvfs_get_blocks_direct(
  930. struct inode *inode,
  931. sector_t iblock,
  932. unsigned long max_blocks,
  933. struct buffer_head *bh_result,
  934. int create)
  935. {
  936. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  937. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  938. }
  939. STATIC void
  940. linvfs_end_io_direct(
  941. struct kiocb *iocb,
  942. loff_t offset,
  943. ssize_t size,
  944. void *private)
  945. {
  946. xfs_ioend_t *ioend = iocb->private;
  947. /*
  948. * Non-NULL private data means we need to issue a transaction to
  949. * convert a range from unwritten to written extents. This needs
  950. * to happen from process contect but aio+dio I/O completion
  951. * happens from irq context so we need to defer it to a workqueue.
  952. * This is not nessecary for synchronous direct I/O, but we do
  953. * it anyway to keep the code uniform and simpler.
  954. *
  955. * The core direct I/O code might be changed to always call the
  956. * completion handler in the future, in which case all this can
  957. * go away.
  958. */
  959. if (private && size > 0) {
  960. ioend->io_offset = offset;
  961. ioend->io_size = size;
  962. xfs_finish_ioend(ioend);
  963. } else {
  964. ASSERT(size >= 0);
  965. xfs_destroy_ioend(ioend);
  966. }
  967. /*
  968. * blockdev_direct_IO can return an error even afer the I/O
  969. * completion handler was called. Thus we need to protect
  970. * against double-freeing.
  971. */
  972. iocb->private = NULL;
  973. }
  974. STATIC ssize_t
  975. linvfs_direct_IO(
  976. int rw,
  977. struct kiocb *iocb,
  978. const struct iovec *iov,
  979. loff_t offset,
  980. unsigned long nr_segs)
  981. {
  982. struct file *file = iocb->ki_filp;
  983. struct inode *inode = file->f_mapping->host;
  984. vnode_t *vp = LINVFS_GET_VP(inode);
  985. xfs_iomap_t iomap;
  986. int maps = 1;
  987. int error;
  988. ssize_t ret;
  989. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  990. if (error)
  991. return -error;
  992. iocb->private = xfs_alloc_ioend(inode);
  993. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  994. iomap.iomap_target->pbr_bdev,
  995. iov, offset, nr_segs,
  996. linvfs_get_blocks_direct,
  997. linvfs_end_io_direct);
  998. if (unlikely(ret <= 0 && iocb->private))
  999. xfs_destroy_ioend(iocb->private);
  1000. return ret;
  1001. }
  1002. STATIC sector_t
  1003. linvfs_bmap(
  1004. struct address_space *mapping,
  1005. sector_t block)
  1006. {
  1007. struct inode *inode = (struct inode *)mapping->host;
  1008. vnode_t *vp = LINVFS_GET_VP(inode);
  1009. int error;
  1010. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  1011. VOP_RWLOCK(vp, VRWLOCK_READ);
  1012. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  1013. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  1014. return generic_block_bmap(mapping, block, linvfs_get_block);
  1015. }
  1016. STATIC int
  1017. linvfs_readpage(
  1018. struct file *unused,
  1019. struct page *page)
  1020. {
  1021. return mpage_readpage(page, linvfs_get_block);
  1022. }
  1023. STATIC int
  1024. linvfs_readpages(
  1025. struct file *unused,
  1026. struct address_space *mapping,
  1027. struct list_head *pages,
  1028. unsigned nr_pages)
  1029. {
  1030. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  1031. }
  1032. STATIC void
  1033. xfs_count_page_state(
  1034. struct page *page,
  1035. int *delalloc,
  1036. int *unmapped,
  1037. int *unwritten)
  1038. {
  1039. struct buffer_head *bh, *head;
  1040. *delalloc = *unmapped = *unwritten = 0;
  1041. bh = head = page_buffers(page);
  1042. do {
  1043. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  1044. (*unmapped) = 1;
  1045. else if (buffer_unwritten(bh) && !buffer_delay(bh))
  1046. clear_buffer_unwritten(bh);
  1047. else if (buffer_unwritten(bh))
  1048. (*unwritten) = 1;
  1049. else if (buffer_delay(bh))
  1050. (*delalloc) = 1;
  1051. } while ((bh = bh->b_this_page) != head);
  1052. }
  1053. /*
  1054. * writepage: Called from one of two places:
  1055. *
  1056. * 1. we are flushing a delalloc buffer head.
  1057. *
  1058. * 2. we are writing out a dirty page. Typically the page dirty
  1059. * state is cleared before we get here. In this case is it
  1060. * conceivable we have no buffer heads.
  1061. *
  1062. * For delalloc space on the page we need to allocate space and
  1063. * flush it. For unmapped buffer heads on the page we should
  1064. * allocate space if the page is uptodate. For any other dirty
  1065. * buffer heads on the page we should flush them.
  1066. *
  1067. * If we detect that a transaction would be required to flush
  1068. * the page, we have to check the process flags first, if we
  1069. * are already in a transaction or disk I/O during allocations
  1070. * is off, we need to fail the writepage and redirty the page.
  1071. */
  1072. STATIC int
  1073. linvfs_writepage(
  1074. struct page *page,
  1075. struct writeback_control *wbc)
  1076. {
  1077. int error;
  1078. int need_trans;
  1079. int delalloc, unmapped, unwritten;
  1080. struct inode *inode = page->mapping->host;
  1081. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1082. /*
  1083. * We need a transaction if:
  1084. * 1. There are delalloc buffers on the page
  1085. * 2. The page is uptodate and we have unmapped buffers
  1086. * 3. The page is uptodate and we have no buffers
  1087. * 4. There are unwritten buffers on the page
  1088. */
  1089. if (!page_has_buffers(page)) {
  1090. unmapped = 1;
  1091. need_trans = 1;
  1092. } else {
  1093. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1094. if (!PageUptodate(page))
  1095. unmapped = 0;
  1096. need_trans = delalloc + unmapped + unwritten;
  1097. }
  1098. /*
  1099. * If we need a transaction and the process flags say
  1100. * we are already in a transaction, or no IO is allowed
  1101. * then mark the page dirty again and leave the page
  1102. * as is.
  1103. */
  1104. if (PFLAGS_TEST_FSTRANS() && need_trans)
  1105. goto out_fail;
  1106. /*
  1107. * Delay hooking up buffer heads until we have
  1108. * made our go/no-go decision.
  1109. */
  1110. if (!page_has_buffers(page))
  1111. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1112. /*
  1113. * Convert delayed allocate, unwritten or unmapped space
  1114. * to real space and flush out to disk.
  1115. */
  1116. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1117. if (error == -EAGAIN)
  1118. goto out_fail;
  1119. if (unlikely(error < 0))
  1120. goto out_unlock;
  1121. return 0;
  1122. out_fail:
  1123. redirty_page_for_writepage(wbc, page);
  1124. unlock_page(page);
  1125. return 0;
  1126. out_unlock:
  1127. unlock_page(page);
  1128. return error;
  1129. }
  1130. STATIC int
  1131. linvfs_invalidate_page(
  1132. struct page *page,
  1133. unsigned long offset)
  1134. {
  1135. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1136. page->mapping->host, page, offset);
  1137. return block_invalidatepage(page, offset);
  1138. }
  1139. /*
  1140. * Called to move a page into cleanable state - and from there
  1141. * to be released. Possibly the page is already clean. We always
  1142. * have buffer heads in this call.
  1143. *
  1144. * Returns 0 if the page is ok to release, 1 otherwise.
  1145. *
  1146. * Possible scenarios are:
  1147. *
  1148. * 1. We are being called to release a page which has been written
  1149. * to via regular I/O. buffer heads will be dirty and possibly
  1150. * delalloc. If no delalloc buffer heads in this case then we
  1151. * can just return zero.
  1152. *
  1153. * 2. We are called to release a page which has been written via
  1154. * mmap, all we need to do is ensure there is no delalloc
  1155. * state in the buffer heads, if not we can let the caller
  1156. * free them and we should come back later via writepage.
  1157. */
  1158. STATIC int
  1159. linvfs_release_page(
  1160. struct page *page,
  1161. gfp_t gfp_mask)
  1162. {
  1163. struct inode *inode = page->mapping->host;
  1164. int dirty, delalloc, unmapped, unwritten;
  1165. struct writeback_control wbc = {
  1166. .sync_mode = WB_SYNC_ALL,
  1167. .nr_to_write = 1,
  1168. };
  1169. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1170. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1171. if (!delalloc && !unwritten)
  1172. goto free_buffers;
  1173. if (!(gfp_mask & __GFP_FS))
  1174. return 0;
  1175. /* If we are already inside a transaction or the thread cannot
  1176. * do I/O, we cannot release this page.
  1177. */
  1178. if (PFLAGS_TEST_FSTRANS())
  1179. return 0;
  1180. /*
  1181. * Convert delalloc space to real space, do not flush the
  1182. * data out to disk, that will be done by the caller.
  1183. * Never need to allocate space here - we will always
  1184. * come back to writepage in that case.
  1185. */
  1186. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1187. if (dirty == 0 && !unwritten)
  1188. goto free_buffers;
  1189. return 0;
  1190. free_buffers:
  1191. return try_to_free_buffers(page);
  1192. }
  1193. STATIC int
  1194. linvfs_prepare_write(
  1195. struct file *file,
  1196. struct page *page,
  1197. unsigned int from,
  1198. unsigned int to)
  1199. {
  1200. return block_prepare_write(page, from, to, linvfs_get_block);
  1201. }
  1202. struct address_space_operations linvfs_aops = {
  1203. .readpage = linvfs_readpage,
  1204. .readpages = linvfs_readpages,
  1205. .writepage = linvfs_writepage,
  1206. .sync_page = block_sync_page,
  1207. .releasepage = linvfs_release_page,
  1208. .invalidatepage = linvfs_invalidate_page,
  1209. .prepare_write = linvfs_prepare_write,
  1210. .commit_write = generic_commit_write,
  1211. .bmap = linvfs_bmap,
  1212. .direct_IO = linvfs_direct_IO,
  1213. };