xfs_aops.c 33 KB

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