xfs_aops.c 40 KB

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