xfs_aops.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_inum.h"
  22. #include "xfs_sb.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_dir2.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_dmapi.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dir2_sf.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_btree.h"
  37. #include "xfs_error.h"
  38. #include "xfs_rw.h"
  39. #include "xfs_iomap.h"
  40. #include "xfs_vnodeops.h"
  41. #include <linux/mpage.h>
  42. #include <linux/pagevec.h>
  43. #include <linux/writeback.h>
  44. STATIC void
  45. xfs_count_page_state(
  46. struct page *page,
  47. int *delalloc,
  48. int *unmapped,
  49. int *unwritten)
  50. {
  51. struct buffer_head *bh, *head;
  52. *delalloc = *unmapped = *unwritten = 0;
  53. bh = head = page_buffers(page);
  54. do {
  55. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  56. (*unmapped) = 1;
  57. else if (buffer_unwritten(bh))
  58. (*unwritten) = 1;
  59. else if (buffer_delay(bh))
  60. (*delalloc) = 1;
  61. } while ((bh = bh->b_this_page) != head);
  62. }
  63. #if defined(XFS_RW_TRACE)
  64. void
  65. xfs_page_trace(
  66. int tag,
  67. struct inode *inode,
  68. struct page *page,
  69. unsigned long pgoff)
  70. {
  71. xfs_inode_t *ip;
  72. bhv_vnode_t *vp = vn_from_inode(inode);
  73. loff_t isize = i_size_read(inode);
  74. loff_t offset = page_offset(page);
  75. int delalloc = -1, unmapped = -1, unwritten = -1;
  76. if (page_has_buffers(page))
  77. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  78. ip = xfs_vtoi(vp);
  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 *)pgoff,
  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 *)((unsigned long)current_pid()),
  97. (void *)NULL);
  98. }
  99. #else
  100. #define xfs_page_trace(tag, inode, page, pgoff)
  101. #endif
  102. STATIC struct block_device *
  103. xfs_find_bdev_for_inode(
  104. struct xfs_inode *ip)
  105. {
  106. struct xfs_mount *mp = ip->i_mount;
  107. if (XFS_IS_REALTIME_INODE(ip))
  108. return mp->m_rtdev_targp->bt_bdev;
  109. else
  110. return mp->m_ddev_targp->bt_bdev;
  111. }
  112. /*
  113. * Schedule IO completion handling on a xfsdatad if this was
  114. * the final hold on this ioend. If we are asked to wait,
  115. * flush the workqueue.
  116. */
  117. STATIC void
  118. xfs_finish_ioend(
  119. xfs_ioend_t *ioend,
  120. int wait)
  121. {
  122. if (atomic_dec_and_test(&ioend->io_remaining)) {
  123. queue_work(xfsdatad_workqueue, &ioend->io_work);
  124. if (wait)
  125. flush_workqueue(xfsdatad_workqueue);
  126. }
  127. }
  128. /*
  129. * We're now finished for good with this ioend structure.
  130. * Update the page state via the associated buffer_heads,
  131. * release holds on the inode and bio, and finally free
  132. * up memory. Do not use the ioend after this.
  133. */
  134. STATIC void
  135. xfs_destroy_ioend(
  136. xfs_ioend_t *ioend)
  137. {
  138. struct buffer_head *bh, *next;
  139. for (bh = ioend->io_buffer_head; bh; bh = next) {
  140. next = bh->b_private;
  141. bh->b_end_io(bh, !ioend->io_error);
  142. }
  143. if (unlikely(ioend->io_error)) {
  144. vn_ioerror(XFS_I(ioend->io_inode), ioend->io_error,
  145. __FILE__,__LINE__);
  146. }
  147. vn_iowake(XFS_I(ioend->io_inode));
  148. mempool_free(ioend, xfs_ioend_pool);
  149. }
  150. /*
  151. * Update on-disk file size now that data has been written to disk.
  152. * The current in-memory file size is i_size. If a write is beyond
  153. * eof i_new_size will be the intended file size until i_size is
  154. * updated. If this write does not extend all the way to the valid
  155. * file size then restrict this update to the end of the write.
  156. */
  157. STATIC void
  158. xfs_setfilesize(
  159. xfs_ioend_t *ioend)
  160. {
  161. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  162. xfs_fsize_t isize;
  163. xfs_fsize_t bsize;
  164. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
  165. ASSERT(ioend->io_type != IOMAP_READ);
  166. if (unlikely(ioend->io_error))
  167. return;
  168. bsize = ioend->io_offset + ioend->io_size;
  169. xfs_ilock(ip, XFS_ILOCK_EXCL);
  170. isize = MAX(ip->i_size, ip->i_new_size);
  171. isize = MIN(isize, bsize);
  172. if (ip->i_d.di_size < isize) {
  173. ip->i_d.di_size = isize;
  174. ip->i_update_core = 1;
  175. ip->i_update_size = 1;
  176. mark_inode_dirty_sync(ioend->io_inode);
  177. }
  178. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  179. }
  180. /*
  181. * Buffered IO write completion for delayed allocate extents.
  182. */
  183. STATIC void
  184. xfs_end_bio_delalloc(
  185. struct work_struct *work)
  186. {
  187. xfs_ioend_t *ioend =
  188. container_of(work, xfs_ioend_t, io_work);
  189. xfs_setfilesize(ioend);
  190. xfs_destroy_ioend(ioend);
  191. }
  192. /*
  193. * Buffered IO write completion for regular, written extents.
  194. */
  195. STATIC void
  196. xfs_end_bio_written(
  197. struct work_struct *work)
  198. {
  199. xfs_ioend_t *ioend =
  200. container_of(work, xfs_ioend_t, io_work);
  201. xfs_setfilesize(ioend);
  202. xfs_destroy_ioend(ioend);
  203. }
  204. /*
  205. * IO write completion for unwritten extents.
  206. *
  207. * Issue transactions to convert a buffer range from unwritten
  208. * to written extents.
  209. */
  210. STATIC void
  211. xfs_end_bio_unwritten(
  212. struct work_struct *work)
  213. {
  214. xfs_ioend_t *ioend =
  215. container_of(work, xfs_ioend_t, io_work);
  216. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  217. xfs_off_t offset = ioend->io_offset;
  218. size_t size = ioend->io_size;
  219. if (likely(!ioend->io_error)) {
  220. if (!XFS_FORCED_SHUTDOWN(ip->i_mount))
  221. xfs_iomap_write_unwritten(ip, offset, size);
  222. xfs_setfilesize(ioend);
  223. }
  224. xfs_destroy_ioend(ioend);
  225. }
  226. /*
  227. * IO read completion for regular, written extents.
  228. */
  229. STATIC void
  230. xfs_end_bio_read(
  231. struct work_struct *work)
  232. {
  233. xfs_ioend_t *ioend =
  234. container_of(work, xfs_ioend_t, io_work);
  235. xfs_destroy_ioend(ioend);
  236. }
  237. /*
  238. * Allocate and initialise an IO completion structure.
  239. * We need to track unwritten extent write completion here initially.
  240. * We'll need to extend this for updating the ondisk inode size later
  241. * (vs. incore size).
  242. */
  243. STATIC xfs_ioend_t *
  244. xfs_alloc_ioend(
  245. struct inode *inode,
  246. unsigned int type)
  247. {
  248. xfs_ioend_t *ioend;
  249. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  250. /*
  251. * Set the count to 1 initially, which will prevent an I/O
  252. * completion callback from happening before we have started
  253. * all the I/O from calling the completion routine too early.
  254. */
  255. atomic_set(&ioend->io_remaining, 1);
  256. ioend->io_error = 0;
  257. ioend->io_list = NULL;
  258. ioend->io_type = type;
  259. ioend->io_inode = inode;
  260. ioend->io_buffer_head = NULL;
  261. ioend->io_buffer_tail = NULL;
  262. atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
  263. ioend->io_offset = 0;
  264. ioend->io_size = 0;
  265. if (type == IOMAP_UNWRITTEN)
  266. INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten);
  267. else if (type == IOMAP_DELAY)
  268. INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc);
  269. else if (type == IOMAP_READ)
  270. INIT_WORK(&ioend->io_work, xfs_end_bio_read);
  271. else
  272. INIT_WORK(&ioend->io_work, xfs_end_bio_written);
  273. return ioend;
  274. }
  275. STATIC int
  276. xfs_map_blocks(
  277. struct inode *inode,
  278. loff_t offset,
  279. ssize_t count,
  280. xfs_iomap_t *mapp,
  281. int flags)
  282. {
  283. xfs_inode_t *ip = XFS_I(inode);
  284. int error, nmaps = 1;
  285. error = xfs_iomap(ip, offset, count,
  286. flags, mapp, &nmaps);
  287. if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
  288. xfs_iflags_set(ip, XFS_IMODIFIED);
  289. return -error;
  290. }
  291. STATIC_INLINE int
  292. xfs_iomap_valid(
  293. xfs_iomap_t *iomapp,
  294. loff_t offset)
  295. {
  296. return offset >= iomapp->iomap_offset &&
  297. offset < iomapp->iomap_offset + iomapp->iomap_bsize;
  298. }
  299. /*
  300. * BIO completion handler for buffered IO.
  301. */
  302. STATIC void
  303. xfs_end_bio(
  304. struct bio *bio,
  305. int error)
  306. {
  307. xfs_ioend_t *ioend = bio->bi_private;
  308. ASSERT(atomic_read(&bio->bi_cnt) >= 1);
  309. ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
  310. /* Toss bio and pass work off to an xfsdatad thread */
  311. bio->bi_private = NULL;
  312. bio->bi_end_io = NULL;
  313. bio_put(bio);
  314. xfs_finish_ioend(ioend, 0);
  315. }
  316. STATIC void
  317. xfs_submit_ioend_bio(
  318. xfs_ioend_t *ioend,
  319. struct bio *bio)
  320. {
  321. atomic_inc(&ioend->io_remaining);
  322. bio->bi_private = ioend;
  323. bio->bi_end_io = xfs_end_bio;
  324. submit_bio(WRITE, bio);
  325. ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
  326. bio_put(bio);
  327. }
  328. STATIC struct bio *
  329. xfs_alloc_ioend_bio(
  330. struct buffer_head *bh)
  331. {
  332. struct bio *bio;
  333. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  334. do {
  335. bio = bio_alloc(GFP_NOIO, nvecs);
  336. nvecs >>= 1;
  337. } while (!bio);
  338. ASSERT(bio->bi_private == NULL);
  339. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  340. bio->bi_bdev = bh->b_bdev;
  341. bio_get(bio);
  342. return bio;
  343. }
  344. STATIC void
  345. xfs_start_buffer_writeback(
  346. struct buffer_head *bh)
  347. {
  348. ASSERT(buffer_mapped(bh));
  349. ASSERT(buffer_locked(bh));
  350. ASSERT(!buffer_delay(bh));
  351. ASSERT(!buffer_unwritten(bh));
  352. mark_buffer_async_write(bh);
  353. set_buffer_uptodate(bh);
  354. clear_buffer_dirty(bh);
  355. }
  356. STATIC void
  357. xfs_start_page_writeback(
  358. struct page *page,
  359. struct writeback_control *wbc,
  360. int clear_dirty,
  361. int buffers)
  362. {
  363. ASSERT(PageLocked(page));
  364. ASSERT(!PageWriteback(page));
  365. if (clear_dirty)
  366. clear_page_dirty_for_io(page);
  367. set_page_writeback(page);
  368. unlock_page(page);
  369. /* If no buffers on the page are to be written, finish it here */
  370. if (!buffers)
  371. end_page_writeback(page);
  372. }
  373. static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  374. {
  375. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  376. }
  377. /*
  378. * Submit all of the bios for all of the ioends we have saved up, covering the
  379. * initial writepage page and also any probed pages.
  380. *
  381. * Because we may have multiple ioends spanning a page, we need to start
  382. * writeback on all the buffers before we submit them for I/O. If we mark the
  383. * buffers as we got, then we can end up with a page that only has buffers
  384. * marked async write and I/O complete on can occur before we mark the other
  385. * buffers async write.
  386. *
  387. * The end result of this is that we trip a bug in end_page_writeback() because
  388. * we call it twice for the one page as the code in end_buffer_async_write()
  389. * assumes that all buffers on the page are started at the same time.
  390. *
  391. * The fix is two passes across the ioend list - one to start writeback on the
  392. * buffer_heads, and then submit them for I/O on the second pass.
  393. */
  394. STATIC void
  395. xfs_submit_ioend(
  396. xfs_ioend_t *ioend)
  397. {
  398. xfs_ioend_t *head = ioend;
  399. xfs_ioend_t *next;
  400. struct buffer_head *bh;
  401. struct bio *bio;
  402. sector_t lastblock = 0;
  403. /* Pass 1 - start writeback */
  404. do {
  405. next = ioend->io_list;
  406. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  407. xfs_start_buffer_writeback(bh);
  408. }
  409. } while ((ioend = next) != NULL);
  410. /* Pass 2 - submit I/O */
  411. ioend = head;
  412. do {
  413. next = ioend->io_list;
  414. bio = NULL;
  415. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  416. if (!bio) {
  417. retry:
  418. bio = xfs_alloc_ioend_bio(bh);
  419. } else if (bh->b_blocknr != lastblock + 1) {
  420. xfs_submit_ioend_bio(ioend, bio);
  421. goto retry;
  422. }
  423. if (bio_add_buffer(bio, bh) != bh->b_size) {
  424. xfs_submit_ioend_bio(ioend, bio);
  425. goto retry;
  426. }
  427. lastblock = bh->b_blocknr;
  428. }
  429. if (bio)
  430. xfs_submit_ioend_bio(ioend, bio);
  431. xfs_finish_ioend(ioend, 0);
  432. } while ((ioend = next) != NULL);
  433. }
  434. /*
  435. * Cancel submission of all buffer_heads so far in this endio.
  436. * Toss the endio too. Only ever called for the initial page
  437. * in a writepage request, so only ever one page.
  438. */
  439. STATIC void
  440. xfs_cancel_ioend(
  441. xfs_ioend_t *ioend)
  442. {
  443. xfs_ioend_t *next;
  444. struct buffer_head *bh, *next_bh;
  445. do {
  446. next = ioend->io_list;
  447. bh = ioend->io_buffer_head;
  448. do {
  449. next_bh = bh->b_private;
  450. clear_buffer_async_write(bh);
  451. unlock_buffer(bh);
  452. } while ((bh = next_bh) != NULL);
  453. vn_iowake(XFS_I(ioend->io_inode));
  454. mempool_free(ioend, xfs_ioend_pool);
  455. } while ((ioend = next) != NULL);
  456. }
  457. /*
  458. * Test to see if we've been building up a completion structure for
  459. * earlier buffers -- if so, we try to append to this ioend if we
  460. * can, otherwise we finish off any current ioend and start another.
  461. * Return true if we've finished the given ioend.
  462. */
  463. STATIC void
  464. xfs_add_to_ioend(
  465. struct inode *inode,
  466. struct buffer_head *bh,
  467. xfs_off_t offset,
  468. unsigned int type,
  469. xfs_ioend_t **result,
  470. int need_ioend)
  471. {
  472. xfs_ioend_t *ioend = *result;
  473. if (!ioend || need_ioend || type != ioend->io_type) {
  474. xfs_ioend_t *previous = *result;
  475. ioend = xfs_alloc_ioend(inode, type);
  476. ioend->io_offset = offset;
  477. ioend->io_buffer_head = bh;
  478. ioend->io_buffer_tail = bh;
  479. if (previous)
  480. previous->io_list = ioend;
  481. *result = ioend;
  482. } else {
  483. ioend->io_buffer_tail->b_private = bh;
  484. ioend->io_buffer_tail = bh;
  485. }
  486. bh->b_private = NULL;
  487. ioend->io_size += bh->b_size;
  488. }
  489. STATIC void
  490. xfs_map_buffer(
  491. struct buffer_head *bh,
  492. xfs_iomap_t *mp,
  493. xfs_off_t offset,
  494. uint block_bits)
  495. {
  496. sector_t bn;
  497. ASSERT(mp->iomap_bn != IOMAP_DADDR_NULL);
  498. bn = (mp->iomap_bn >> (block_bits - BBSHIFT)) +
  499. ((offset - mp->iomap_offset) >> block_bits);
  500. ASSERT(bn || (mp->iomap_flags & IOMAP_REALTIME));
  501. bh->b_blocknr = bn;
  502. set_buffer_mapped(bh);
  503. }
  504. STATIC void
  505. xfs_map_at_offset(
  506. struct buffer_head *bh,
  507. loff_t offset,
  508. int block_bits,
  509. xfs_iomap_t *iomapp)
  510. {
  511. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  512. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  513. lock_buffer(bh);
  514. xfs_map_buffer(bh, iomapp, offset, block_bits);
  515. bh->b_bdev = iomapp->iomap_target->bt_bdev;
  516. set_buffer_mapped(bh);
  517. clear_buffer_delay(bh);
  518. clear_buffer_unwritten(bh);
  519. }
  520. /*
  521. * Look for a page at index that is suitable for clustering.
  522. */
  523. STATIC unsigned int
  524. xfs_probe_page(
  525. struct page *page,
  526. unsigned int pg_offset,
  527. int mapped)
  528. {
  529. int ret = 0;
  530. if (PageWriteback(page))
  531. return 0;
  532. if (page->mapping && PageDirty(page)) {
  533. if (page_has_buffers(page)) {
  534. struct buffer_head *bh, *head;
  535. bh = head = page_buffers(page);
  536. do {
  537. if (!buffer_uptodate(bh))
  538. break;
  539. if (mapped != buffer_mapped(bh))
  540. break;
  541. ret += bh->b_size;
  542. if (ret >= pg_offset)
  543. break;
  544. } while ((bh = bh->b_this_page) != head);
  545. } else
  546. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  547. }
  548. return ret;
  549. }
  550. STATIC size_t
  551. xfs_probe_cluster(
  552. struct inode *inode,
  553. struct page *startpage,
  554. struct buffer_head *bh,
  555. struct buffer_head *head,
  556. int mapped)
  557. {
  558. struct pagevec pvec;
  559. pgoff_t tindex, tlast, tloff;
  560. size_t total = 0;
  561. int done = 0, i;
  562. /* First sum forwards in this page */
  563. do {
  564. if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
  565. return total;
  566. total += bh->b_size;
  567. } while ((bh = bh->b_this_page) != head);
  568. /* if we reached the end of the page, sum forwards in following pages */
  569. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  570. tindex = startpage->index + 1;
  571. /* Prune this back to avoid pathological behavior */
  572. tloff = min(tlast, startpage->index + 64);
  573. pagevec_init(&pvec, 0);
  574. while (!done && tindex <= tloff) {
  575. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  576. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  577. break;
  578. for (i = 0; i < pagevec_count(&pvec); i++) {
  579. struct page *page = pvec.pages[i];
  580. size_t pg_offset, pg_len = 0;
  581. if (tindex == tlast) {
  582. pg_offset =
  583. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  584. if (!pg_offset) {
  585. done = 1;
  586. break;
  587. }
  588. } else
  589. pg_offset = PAGE_CACHE_SIZE;
  590. if (page->index == tindex && !TestSetPageLocked(page)) {
  591. pg_len = xfs_probe_page(page, pg_offset, mapped);
  592. unlock_page(page);
  593. }
  594. if (!pg_len) {
  595. done = 1;
  596. break;
  597. }
  598. total += pg_len;
  599. tindex++;
  600. }
  601. pagevec_release(&pvec);
  602. cond_resched();
  603. }
  604. return total;
  605. }
  606. /*
  607. * Test if a given page is suitable for writing as part of an unwritten
  608. * or delayed allocate extent.
  609. */
  610. STATIC int
  611. xfs_is_delayed_page(
  612. struct page *page,
  613. unsigned int type)
  614. {
  615. if (PageWriteback(page))
  616. return 0;
  617. if (page->mapping && page_has_buffers(page)) {
  618. struct buffer_head *bh, *head;
  619. int acceptable = 0;
  620. bh = head = page_buffers(page);
  621. do {
  622. if (buffer_unwritten(bh))
  623. acceptable = (type == IOMAP_UNWRITTEN);
  624. else if (buffer_delay(bh))
  625. acceptable = (type == IOMAP_DELAY);
  626. else if (buffer_dirty(bh) && buffer_mapped(bh))
  627. acceptable = (type == IOMAP_NEW);
  628. else
  629. break;
  630. } while ((bh = bh->b_this_page) != head);
  631. if (acceptable)
  632. return 1;
  633. }
  634. return 0;
  635. }
  636. /*
  637. * Allocate & map buffers for page given the extent map. Write it out.
  638. * except for the original page of a writepage, this is called on
  639. * delalloc/unwritten pages only, for the original page it is possible
  640. * that the page has no mapping at all.
  641. */
  642. STATIC int
  643. xfs_convert_page(
  644. struct inode *inode,
  645. struct page *page,
  646. loff_t tindex,
  647. xfs_iomap_t *mp,
  648. xfs_ioend_t **ioendp,
  649. struct writeback_control *wbc,
  650. int startio,
  651. int all_bh)
  652. {
  653. struct buffer_head *bh, *head;
  654. xfs_off_t end_offset;
  655. unsigned long p_offset;
  656. unsigned int type;
  657. int bbits = inode->i_blkbits;
  658. int len, page_dirty;
  659. int count = 0, done = 0, uptodate = 1;
  660. xfs_off_t offset = page_offset(page);
  661. if (page->index != tindex)
  662. goto fail;
  663. if (TestSetPageLocked(page))
  664. goto fail;
  665. if (PageWriteback(page))
  666. goto fail_unlock_page;
  667. if (page->mapping != inode->i_mapping)
  668. goto fail_unlock_page;
  669. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  670. goto fail_unlock_page;
  671. /*
  672. * page_dirty is initially a count of buffers on the page before
  673. * EOF and is decremented as we move each into a cleanable state.
  674. *
  675. * Derivation:
  676. *
  677. * End offset is the highest offset that this page should represent.
  678. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  679. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  680. * hence give us the correct page_dirty count. On any other page,
  681. * it will be zero and in that case we need page_dirty to be the
  682. * count of buffers on the page.
  683. */
  684. end_offset = min_t(unsigned long long,
  685. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  686. i_size_read(inode));
  687. len = 1 << inode->i_blkbits;
  688. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  689. PAGE_CACHE_SIZE);
  690. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  691. page_dirty = p_offset / len;
  692. bh = head = page_buffers(page);
  693. do {
  694. if (offset >= end_offset)
  695. break;
  696. if (!buffer_uptodate(bh))
  697. uptodate = 0;
  698. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  699. done = 1;
  700. continue;
  701. }
  702. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  703. if (buffer_unwritten(bh))
  704. type = IOMAP_UNWRITTEN;
  705. else
  706. type = IOMAP_DELAY;
  707. if (!xfs_iomap_valid(mp, offset)) {
  708. done = 1;
  709. continue;
  710. }
  711. ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
  712. ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
  713. xfs_map_at_offset(bh, offset, bbits, mp);
  714. if (startio) {
  715. xfs_add_to_ioend(inode, bh, offset,
  716. type, ioendp, done);
  717. } else {
  718. set_buffer_dirty(bh);
  719. unlock_buffer(bh);
  720. mark_buffer_dirty(bh);
  721. }
  722. page_dirty--;
  723. count++;
  724. } else {
  725. type = IOMAP_NEW;
  726. if (buffer_mapped(bh) && all_bh && startio) {
  727. lock_buffer(bh);
  728. xfs_add_to_ioend(inode, bh, offset,
  729. type, ioendp, done);
  730. count++;
  731. page_dirty--;
  732. } else {
  733. done = 1;
  734. }
  735. }
  736. } while (offset += len, (bh = bh->b_this_page) != head);
  737. if (uptodate && bh == head)
  738. SetPageUptodate(page);
  739. if (startio) {
  740. if (count) {
  741. struct backing_dev_info *bdi;
  742. bdi = inode->i_mapping->backing_dev_info;
  743. wbc->nr_to_write--;
  744. if (bdi_write_congested(bdi)) {
  745. wbc->encountered_congestion = 1;
  746. done = 1;
  747. } else if (wbc->nr_to_write <= 0) {
  748. done = 1;
  749. }
  750. }
  751. xfs_start_page_writeback(page, wbc, !page_dirty, count);
  752. }
  753. return done;
  754. fail_unlock_page:
  755. unlock_page(page);
  756. fail:
  757. return 1;
  758. }
  759. /*
  760. * Convert & write out a cluster of pages in the same extent as defined
  761. * by mp and following the start page.
  762. */
  763. STATIC void
  764. xfs_cluster_write(
  765. struct inode *inode,
  766. pgoff_t tindex,
  767. xfs_iomap_t *iomapp,
  768. xfs_ioend_t **ioendp,
  769. struct writeback_control *wbc,
  770. int startio,
  771. int all_bh,
  772. pgoff_t tlast)
  773. {
  774. struct pagevec pvec;
  775. int done = 0, i;
  776. pagevec_init(&pvec, 0);
  777. while (!done && tindex <= tlast) {
  778. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  779. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  780. break;
  781. for (i = 0; i < pagevec_count(&pvec); i++) {
  782. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  783. iomapp, ioendp, wbc, startio, all_bh);
  784. if (done)
  785. break;
  786. }
  787. pagevec_release(&pvec);
  788. cond_resched();
  789. }
  790. }
  791. /*
  792. * Calling this without startio set means we are being asked to make a dirty
  793. * page ready for freeing it's buffers. When called with startio set then
  794. * we are coming from writepage.
  795. *
  796. * When called with startio set it is important that we write the WHOLE
  797. * page if possible.
  798. * The bh->b_state's cannot know if any of the blocks or which block for
  799. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  800. * only valid if the page itself isn't completely uptodate. Some layers
  801. * may clear the page dirty flag prior to calling write page, under the
  802. * assumption the entire page will be written out; by not writing out the
  803. * whole page the page can be reused before all valid dirty data is
  804. * written out. Note: in the case of a page that has been dirty'd by
  805. * mapwrite and but partially setup by block_prepare_write the
  806. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  807. * valid state, thus the whole page must be written out thing.
  808. */
  809. STATIC int
  810. xfs_page_state_convert(
  811. struct inode *inode,
  812. struct page *page,
  813. struct writeback_control *wbc,
  814. int startio,
  815. int unmapped) /* also implies page uptodate */
  816. {
  817. struct buffer_head *bh, *head;
  818. xfs_iomap_t iomap;
  819. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  820. loff_t offset;
  821. unsigned long p_offset = 0;
  822. unsigned int type;
  823. __uint64_t end_offset;
  824. pgoff_t end_index, last_index, tlast;
  825. ssize_t size, len;
  826. int flags, err, iomap_valid = 0, uptodate = 1;
  827. int page_dirty, count = 0;
  828. int trylock = 0;
  829. int all_bh = unmapped;
  830. if (startio) {
  831. if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
  832. trylock |= BMAPI_TRYLOCK;
  833. }
  834. /* Is this page beyond the end of the file? */
  835. offset = i_size_read(inode);
  836. end_index = offset >> PAGE_CACHE_SHIFT;
  837. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  838. if (page->index >= end_index) {
  839. if ((page->index >= end_index + 1) ||
  840. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  841. if (startio)
  842. unlock_page(page);
  843. return 0;
  844. }
  845. }
  846. /*
  847. * page_dirty is initially a count of buffers on the page before
  848. * EOF and is decremented as we move each into a cleanable state.
  849. *
  850. * Derivation:
  851. *
  852. * End offset is the highest offset that this page should represent.
  853. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  854. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  855. * hence give us the correct page_dirty count. On any other page,
  856. * it will be zero and in that case we need page_dirty to be the
  857. * count of buffers on the page.
  858. */
  859. end_offset = min_t(unsigned long long,
  860. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  861. len = 1 << inode->i_blkbits;
  862. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  863. PAGE_CACHE_SIZE);
  864. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  865. page_dirty = p_offset / len;
  866. bh = head = page_buffers(page);
  867. offset = page_offset(page);
  868. flags = BMAPI_READ;
  869. type = IOMAP_NEW;
  870. /* TODO: cleanup count and page_dirty */
  871. do {
  872. if (offset >= end_offset)
  873. break;
  874. if (!buffer_uptodate(bh))
  875. uptodate = 0;
  876. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  877. /*
  878. * the iomap is actually still valid, but the ioend
  879. * isn't. shouldn't happen too often.
  880. */
  881. iomap_valid = 0;
  882. continue;
  883. }
  884. if (iomap_valid)
  885. iomap_valid = xfs_iomap_valid(&iomap, offset);
  886. /*
  887. * First case, map an unwritten extent and prepare for
  888. * extent state conversion transaction on completion.
  889. *
  890. * Second case, allocate space for a delalloc buffer.
  891. * We can return EAGAIN here in the release page case.
  892. *
  893. * Third case, an unmapped buffer was found, and we are
  894. * in a path where we need to write the whole page out.
  895. */
  896. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  897. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  898. !buffer_mapped(bh) && (unmapped || startio))) {
  899. int new_ioend = 0;
  900. /*
  901. * Make sure we don't use a read-only iomap
  902. */
  903. if (flags == BMAPI_READ)
  904. iomap_valid = 0;
  905. if (buffer_unwritten(bh)) {
  906. type = IOMAP_UNWRITTEN;
  907. flags = BMAPI_WRITE | BMAPI_IGNSTATE;
  908. } else if (buffer_delay(bh)) {
  909. type = IOMAP_DELAY;
  910. flags = BMAPI_ALLOCATE | trylock;
  911. } else {
  912. type = IOMAP_NEW;
  913. flags = BMAPI_WRITE | BMAPI_MMAP;
  914. }
  915. if (!iomap_valid) {
  916. /*
  917. * if we didn't have a valid mapping then we
  918. * need to ensure that we put the new mapping
  919. * in a new ioend structure. This needs to be
  920. * done to ensure that the ioends correctly
  921. * reflect the block mappings at io completion
  922. * for unwritten extent conversion.
  923. */
  924. new_ioend = 1;
  925. if (type == IOMAP_NEW) {
  926. size = xfs_probe_cluster(inode,
  927. page, bh, head, 0);
  928. } else {
  929. size = len;
  930. }
  931. err = xfs_map_blocks(inode, offset, size,
  932. &iomap, flags);
  933. if (err)
  934. goto error;
  935. iomap_valid = xfs_iomap_valid(&iomap, offset);
  936. }
  937. if (iomap_valid) {
  938. xfs_map_at_offset(bh, offset,
  939. inode->i_blkbits, &iomap);
  940. if (startio) {
  941. xfs_add_to_ioend(inode, bh, offset,
  942. type, &ioend,
  943. new_ioend);
  944. } else {
  945. set_buffer_dirty(bh);
  946. unlock_buffer(bh);
  947. mark_buffer_dirty(bh);
  948. }
  949. page_dirty--;
  950. count++;
  951. }
  952. } else if (buffer_uptodate(bh) && startio) {
  953. /*
  954. * we got here because the buffer is already mapped.
  955. * That means it must already have extents allocated
  956. * underneath it. Map the extent by reading it.
  957. */
  958. if (!iomap_valid || flags != BMAPI_READ) {
  959. flags = BMAPI_READ;
  960. size = xfs_probe_cluster(inode, page, bh,
  961. head, 1);
  962. err = xfs_map_blocks(inode, offset, size,
  963. &iomap, flags);
  964. if (err)
  965. goto error;
  966. iomap_valid = xfs_iomap_valid(&iomap, offset);
  967. }
  968. /*
  969. * We set the type to IOMAP_NEW in case we are doing a
  970. * small write at EOF that is extending the file but
  971. * without needing an allocation. We need to update the
  972. * file size on I/O completion in this case so it is
  973. * the same case as having just allocated a new extent
  974. * that we are writing into for the first time.
  975. */
  976. type = IOMAP_NEW;
  977. if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
  978. ASSERT(buffer_mapped(bh));
  979. if (iomap_valid)
  980. all_bh = 1;
  981. xfs_add_to_ioend(inode, bh, offset, type,
  982. &ioend, !iomap_valid);
  983. page_dirty--;
  984. count++;
  985. } else {
  986. iomap_valid = 0;
  987. }
  988. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  989. (unmapped || startio)) {
  990. iomap_valid = 0;
  991. }
  992. if (!iohead)
  993. iohead = ioend;
  994. } while (offset += len, ((bh = bh->b_this_page) != head));
  995. if (uptodate && bh == head)
  996. SetPageUptodate(page);
  997. if (startio)
  998. xfs_start_page_writeback(page, wbc, 1, count);
  999. if (ioend && iomap_valid) {
  1000. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  1001. PAGE_CACHE_SHIFT;
  1002. tlast = min_t(pgoff_t, offset, last_index);
  1003. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  1004. wbc, startio, all_bh, tlast);
  1005. }
  1006. if (iohead)
  1007. xfs_submit_ioend(iohead);
  1008. return page_dirty;
  1009. error:
  1010. if (iohead)
  1011. xfs_cancel_ioend(iohead);
  1012. /*
  1013. * If it's delalloc and we have nowhere to put it,
  1014. * throw it away, unless the lower layers told
  1015. * us to try again.
  1016. */
  1017. if (err != -EAGAIN) {
  1018. if (!unmapped)
  1019. block_invalidatepage(page, 0);
  1020. ClearPageUptodate(page);
  1021. }
  1022. return err;
  1023. }
  1024. /*
  1025. * writepage: Called from one of two places:
  1026. *
  1027. * 1. we are flushing a delalloc buffer head.
  1028. *
  1029. * 2. we are writing out a dirty page. Typically the page dirty
  1030. * state is cleared before we get here. In this case is it
  1031. * conceivable we have no buffer heads.
  1032. *
  1033. * For delalloc space on the page we need to allocate space and
  1034. * flush it. For unmapped buffer heads on the page we should
  1035. * allocate space if the page is uptodate. For any other dirty
  1036. * buffer heads on the page we should flush them.
  1037. *
  1038. * If we detect that a transaction would be required to flush
  1039. * the page, we have to check the process flags first, if we
  1040. * are already in a transaction or disk I/O during allocations
  1041. * is off, we need to fail the writepage and redirty the page.
  1042. */
  1043. STATIC int
  1044. xfs_vm_writepage(
  1045. struct page *page,
  1046. struct writeback_control *wbc)
  1047. {
  1048. int error;
  1049. int need_trans;
  1050. int delalloc, unmapped, unwritten;
  1051. struct inode *inode = page->mapping->host;
  1052. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1053. /*
  1054. * We need a transaction if:
  1055. * 1. There are delalloc buffers on the page
  1056. * 2. The page is uptodate and we have unmapped buffers
  1057. * 3. The page is uptodate and we have no buffers
  1058. * 4. There are unwritten buffers on the page
  1059. */
  1060. if (!page_has_buffers(page)) {
  1061. unmapped = 1;
  1062. need_trans = 1;
  1063. } else {
  1064. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1065. if (!PageUptodate(page))
  1066. unmapped = 0;
  1067. need_trans = delalloc + unmapped + unwritten;
  1068. }
  1069. /*
  1070. * If we need a transaction and the process flags say
  1071. * we are already in a transaction, or no IO is allowed
  1072. * then mark the page dirty again and leave the page
  1073. * as is.
  1074. */
  1075. if (current_test_flags(PF_FSTRANS) && need_trans)
  1076. goto out_fail;
  1077. /*
  1078. * Delay hooking up buffer heads until we have
  1079. * made our go/no-go decision.
  1080. */
  1081. if (!page_has_buffers(page))
  1082. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1083. /*
  1084. * Convert delayed allocate, unwritten or unmapped space
  1085. * to real space and flush out to disk.
  1086. */
  1087. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1088. if (error == -EAGAIN)
  1089. goto out_fail;
  1090. if (unlikely(error < 0))
  1091. goto out_unlock;
  1092. return 0;
  1093. out_fail:
  1094. redirty_page_for_writepage(wbc, page);
  1095. unlock_page(page);
  1096. return 0;
  1097. out_unlock:
  1098. unlock_page(page);
  1099. return error;
  1100. }
  1101. STATIC int
  1102. xfs_vm_writepages(
  1103. struct address_space *mapping,
  1104. struct writeback_control *wbc)
  1105. {
  1106. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  1107. return generic_writepages(mapping, wbc);
  1108. }
  1109. /*
  1110. * Called to move a page into cleanable state - and from there
  1111. * to be released. Possibly the page is already clean. We always
  1112. * have buffer heads in this call.
  1113. *
  1114. * Returns 0 if the page is ok to release, 1 otherwise.
  1115. *
  1116. * Possible scenarios are:
  1117. *
  1118. * 1. We are being called to release a page which has been written
  1119. * to via regular I/O. buffer heads will be dirty and possibly
  1120. * delalloc. If no delalloc buffer heads in this case then we
  1121. * can just return zero.
  1122. *
  1123. * 2. We are called to release a page which has been written via
  1124. * mmap, all we need to do is ensure there is no delalloc
  1125. * state in the buffer heads, if not we can let the caller
  1126. * free them and we should come back later via writepage.
  1127. */
  1128. STATIC int
  1129. xfs_vm_releasepage(
  1130. struct page *page,
  1131. gfp_t gfp_mask)
  1132. {
  1133. struct inode *inode = page->mapping->host;
  1134. int dirty, delalloc, unmapped, unwritten;
  1135. struct writeback_control wbc = {
  1136. .sync_mode = WB_SYNC_ALL,
  1137. .nr_to_write = 1,
  1138. };
  1139. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, 0);
  1140. if (!page_has_buffers(page))
  1141. return 0;
  1142. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1143. if (!delalloc && !unwritten)
  1144. goto free_buffers;
  1145. if (!(gfp_mask & __GFP_FS))
  1146. return 0;
  1147. /* If we are already inside a transaction or the thread cannot
  1148. * do I/O, we cannot release this page.
  1149. */
  1150. if (current_test_flags(PF_FSTRANS))
  1151. return 0;
  1152. /*
  1153. * Convert delalloc space to real space, do not flush the
  1154. * data out to disk, that will be done by the caller.
  1155. * Never need to allocate space here - we will always
  1156. * come back to writepage in that case.
  1157. */
  1158. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1159. if (dirty == 0 && !unwritten)
  1160. goto free_buffers;
  1161. return 0;
  1162. free_buffers:
  1163. return try_to_free_buffers(page);
  1164. }
  1165. STATIC int
  1166. __xfs_get_blocks(
  1167. struct inode *inode,
  1168. sector_t iblock,
  1169. struct buffer_head *bh_result,
  1170. int create,
  1171. int direct,
  1172. bmapi_flags_t flags)
  1173. {
  1174. xfs_iomap_t iomap;
  1175. xfs_off_t offset;
  1176. ssize_t size;
  1177. int niomap = 1;
  1178. int error;
  1179. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1180. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1181. size = bh_result->b_size;
  1182. error = xfs_iomap(XFS_I(inode), offset, size,
  1183. create ? flags : BMAPI_READ, &iomap, &niomap);
  1184. if (error)
  1185. return -error;
  1186. if (niomap == 0)
  1187. return 0;
  1188. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  1189. /*
  1190. * For unwritten extents do not report a disk address on
  1191. * the read case (treat as if we're reading into a hole).
  1192. */
  1193. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1194. xfs_map_buffer(bh_result, &iomap, offset,
  1195. inode->i_blkbits);
  1196. }
  1197. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1198. if (direct)
  1199. bh_result->b_private = inode;
  1200. set_buffer_unwritten(bh_result);
  1201. }
  1202. }
  1203. /*
  1204. * If this is a realtime file, data may be on a different device.
  1205. * to that pointed to from the buffer_head b_bdev currently.
  1206. */
  1207. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  1208. /*
  1209. * If we previously allocated a block out beyond eof and we are now
  1210. * coming back to use it then we will need to flag it as new even if it
  1211. * has a disk address.
  1212. *
  1213. * With sub-block writes into unwritten extents we also need to mark
  1214. * the buffer as new so that the unwritten parts of the buffer gets
  1215. * correctly zeroed.
  1216. */
  1217. if (create &&
  1218. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1219. (offset >= i_size_read(inode)) ||
  1220. (iomap.iomap_flags & (IOMAP_NEW|IOMAP_UNWRITTEN))))
  1221. set_buffer_new(bh_result);
  1222. if (iomap.iomap_flags & IOMAP_DELAY) {
  1223. BUG_ON(direct);
  1224. if (create) {
  1225. set_buffer_uptodate(bh_result);
  1226. set_buffer_mapped(bh_result);
  1227. set_buffer_delay(bh_result);
  1228. }
  1229. }
  1230. if (direct || size > (1 << inode->i_blkbits)) {
  1231. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  1232. offset = min_t(xfs_off_t,
  1233. iomap.iomap_bsize - iomap.iomap_delta, size);
  1234. bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
  1235. }
  1236. return 0;
  1237. }
  1238. int
  1239. xfs_get_blocks(
  1240. struct inode *inode,
  1241. sector_t iblock,
  1242. struct buffer_head *bh_result,
  1243. int create)
  1244. {
  1245. return __xfs_get_blocks(inode, iblock,
  1246. bh_result, create, 0, BMAPI_WRITE);
  1247. }
  1248. STATIC int
  1249. xfs_get_blocks_direct(
  1250. struct inode *inode,
  1251. sector_t iblock,
  1252. struct buffer_head *bh_result,
  1253. int create)
  1254. {
  1255. return __xfs_get_blocks(inode, iblock,
  1256. bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  1257. }
  1258. STATIC void
  1259. xfs_end_io_direct(
  1260. struct kiocb *iocb,
  1261. loff_t offset,
  1262. ssize_t size,
  1263. void *private)
  1264. {
  1265. xfs_ioend_t *ioend = iocb->private;
  1266. /*
  1267. * Non-NULL private data means we need to issue a transaction to
  1268. * convert a range from unwritten to written extents. This needs
  1269. * to happen from process context but aio+dio I/O completion
  1270. * happens from irq context so we need to defer it to a workqueue.
  1271. * This is not necessary for synchronous direct I/O, but we do
  1272. * it anyway to keep the code uniform and simpler.
  1273. *
  1274. * Well, if only it were that simple. Because synchronous direct I/O
  1275. * requires extent conversion to occur *before* we return to userspace,
  1276. * we have to wait for extent conversion to complete. Look at the
  1277. * iocb that has been passed to us to determine if this is AIO or
  1278. * not. If it is synchronous, tell xfs_finish_ioend() to kick the
  1279. * workqueue and wait for it to complete.
  1280. *
  1281. * The core direct I/O code might be changed to always call the
  1282. * completion handler in the future, in which case all this can
  1283. * go away.
  1284. */
  1285. ioend->io_offset = offset;
  1286. ioend->io_size = size;
  1287. if (ioend->io_type == IOMAP_READ) {
  1288. xfs_finish_ioend(ioend, 0);
  1289. } else if (private && size > 0) {
  1290. xfs_finish_ioend(ioend, is_sync_kiocb(iocb));
  1291. } else {
  1292. /*
  1293. * A direct I/O write ioend starts it's life in unwritten
  1294. * state in case they map an unwritten extent. This write
  1295. * didn't map an unwritten extent so switch it's completion
  1296. * handler.
  1297. */
  1298. INIT_WORK(&ioend->io_work, xfs_end_bio_written);
  1299. xfs_finish_ioend(ioend, 0);
  1300. }
  1301. /*
  1302. * blockdev_direct_IO can return an error even after the I/O
  1303. * completion handler was called. Thus we need to protect
  1304. * against double-freeing.
  1305. */
  1306. iocb->private = NULL;
  1307. }
  1308. STATIC ssize_t
  1309. xfs_vm_direct_IO(
  1310. int rw,
  1311. struct kiocb *iocb,
  1312. const struct iovec *iov,
  1313. loff_t offset,
  1314. unsigned long nr_segs)
  1315. {
  1316. struct file *file = iocb->ki_filp;
  1317. struct inode *inode = file->f_mapping->host;
  1318. struct block_device *bdev;
  1319. ssize_t ret;
  1320. bdev = xfs_find_bdev_for_inode(XFS_I(inode));
  1321. if (rw == WRITE) {
  1322. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1323. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1324. bdev, iov, offset, nr_segs,
  1325. xfs_get_blocks_direct,
  1326. xfs_end_io_direct);
  1327. } else {
  1328. iocb->private = xfs_alloc_ioend(inode, IOMAP_READ);
  1329. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  1330. bdev, iov, offset, nr_segs,
  1331. xfs_get_blocks_direct,
  1332. xfs_end_io_direct);
  1333. }
  1334. if (unlikely(ret != -EIOCBQUEUED && iocb->private))
  1335. xfs_destroy_ioend(iocb->private);
  1336. return ret;
  1337. }
  1338. STATIC int
  1339. xfs_vm_write_begin(
  1340. struct file *file,
  1341. struct address_space *mapping,
  1342. loff_t pos,
  1343. unsigned len,
  1344. unsigned flags,
  1345. struct page **pagep,
  1346. void **fsdata)
  1347. {
  1348. *pagep = NULL;
  1349. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1350. xfs_get_blocks);
  1351. }
  1352. STATIC sector_t
  1353. xfs_vm_bmap(
  1354. struct address_space *mapping,
  1355. sector_t block)
  1356. {
  1357. struct inode *inode = (struct inode *)mapping->host;
  1358. struct xfs_inode *ip = XFS_I(inode);
  1359. xfs_itrace_entry(XFS_I(inode));
  1360. xfs_rwlock(ip, VRWLOCK_READ);
  1361. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1362. xfs_rwunlock(ip, VRWLOCK_READ);
  1363. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1364. }
  1365. STATIC int
  1366. xfs_vm_readpage(
  1367. struct file *unused,
  1368. struct page *page)
  1369. {
  1370. return mpage_readpage(page, xfs_get_blocks);
  1371. }
  1372. STATIC int
  1373. xfs_vm_readpages(
  1374. struct file *unused,
  1375. struct address_space *mapping,
  1376. struct list_head *pages,
  1377. unsigned nr_pages)
  1378. {
  1379. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1380. }
  1381. STATIC void
  1382. xfs_vm_invalidatepage(
  1383. struct page *page,
  1384. unsigned long offset)
  1385. {
  1386. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1387. page->mapping->host, page, offset);
  1388. block_invalidatepage(page, offset);
  1389. }
  1390. const struct address_space_operations xfs_address_space_operations = {
  1391. .readpage = xfs_vm_readpage,
  1392. .readpages = xfs_vm_readpages,
  1393. .writepage = xfs_vm_writepage,
  1394. .writepages = xfs_vm_writepages,
  1395. .sync_page = block_sync_page,
  1396. .releasepage = xfs_vm_releasepage,
  1397. .invalidatepage = xfs_vm_invalidatepage,
  1398. .write_begin = xfs_vm_write_begin,
  1399. .write_end = generic_write_end,
  1400. .bmap = xfs_vm_bmap,
  1401. .direct_IO = xfs_vm_direct_IO,
  1402. .migratepage = buffer_migrate_page,
  1403. };