xfs_aops.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  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_shared.h"
  20. #include "xfs_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_mount.h"
  24. #include "xfs_inode.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_inode_item.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_error.h"
  29. #include "xfs_iomap.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_bmap_util.h"
  33. #include "xfs_bmap_btree.h"
  34. #include <linux/gfp.h>
  35. #include <linux/mpage.h>
  36. #include <linux/pagevec.h>
  37. #include <linux/writeback.h>
  38. /* flags for direct write completions */
  39. #define XFS_DIO_FLAG_UNWRITTEN (1 << 0)
  40. #define XFS_DIO_FLAG_APPEND (1 << 1)
  41. /*
  42. * structure owned by writepages passed to individual writepage calls
  43. */
  44. struct xfs_writepage_ctx {
  45. struct xfs_bmbt_irec imap;
  46. bool imap_valid;
  47. unsigned int io_type;
  48. struct xfs_ioend *ioend;
  49. sector_t last_block;
  50. };
  51. void
  52. xfs_count_page_state(
  53. struct page *page,
  54. int *delalloc,
  55. int *unwritten)
  56. {
  57. struct buffer_head *bh, *head;
  58. *delalloc = *unwritten = 0;
  59. bh = head = page_buffers(page);
  60. do {
  61. if (buffer_unwritten(bh))
  62. (*unwritten) = 1;
  63. else if (buffer_delay(bh))
  64. (*delalloc) = 1;
  65. } while ((bh = bh->b_this_page) != head);
  66. }
  67. struct block_device *
  68. xfs_find_bdev_for_inode(
  69. struct inode *inode)
  70. {
  71. struct xfs_inode *ip = XFS_I(inode);
  72. struct xfs_mount *mp = ip->i_mount;
  73. if (XFS_IS_REALTIME_INODE(ip))
  74. return mp->m_rtdev_targp->bt_bdev;
  75. else
  76. return mp->m_ddev_targp->bt_bdev;
  77. }
  78. /*
  79. * We're now finished for good with this page. Update the page state via the
  80. * associated buffer_heads, paying attention to the start and end offsets that
  81. * we need to process on the page.
  82. */
  83. static void
  84. xfs_finish_page_writeback(
  85. struct inode *inode,
  86. struct bio_vec *bvec,
  87. int error)
  88. {
  89. unsigned int end = bvec->bv_offset + bvec->bv_len - 1;
  90. struct buffer_head *head, *bh;
  91. unsigned int off = 0;
  92. ASSERT(bvec->bv_offset < PAGE_SIZE);
  93. ASSERT((bvec->bv_offset & ((1 << inode->i_blkbits) - 1)) == 0);
  94. ASSERT(end < PAGE_SIZE);
  95. ASSERT((bvec->bv_len & ((1 << inode->i_blkbits) - 1)) == 0);
  96. bh = head = page_buffers(bvec->bv_page);
  97. do {
  98. if (off < bvec->bv_offset)
  99. goto next_bh;
  100. if (off > end)
  101. break;
  102. bh->b_end_io(bh, !error);
  103. next_bh:
  104. off += bh->b_size;
  105. } while ((bh = bh->b_this_page) != head);
  106. }
  107. /*
  108. * We're now finished for good with this ioend structure. Update the page
  109. * state, release holds on bios, and finally free up memory. Do not use the
  110. * ioend after this.
  111. */
  112. STATIC void
  113. xfs_destroy_ioend(
  114. struct xfs_ioend *ioend,
  115. int error)
  116. {
  117. struct inode *inode = ioend->io_inode;
  118. struct bio *last = ioend->io_bio;
  119. struct bio *bio, *next;
  120. for (bio = &ioend->io_inline_bio; bio; bio = next) {
  121. struct bio_vec *bvec;
  122. int i;
  123. /*
  124. * For the last bio, bi_private points to the ioend, so we
  125. * need to explicitly end the iteration here.
  126. */
  127. if (bio == last)
  128. next = NULL;
  129. else
  130. next = bio->bi_private;
  131. /* walk each page on bio, ending page IO on them */
  132. bio_for_each_segment_all(bvec, bio, i)
  133. xfs_finish_page_writeback(inode, bvec, error);
  134. bio_put(bio);
  135. }
  136. }
  137. /*
  138. * Fast and loose check if this write could update the on-disk inode size.
  139. */
  140. static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
  141. {
  142. return ioend->io_offset + ioend->io_size >
  143. XFS_I(ioend->io_inode)->i_d.di_size;
  144. }
  145. STATIC int
  146. xfs_setfilesize_trans_alloc(
  147. struct xfs_ioend *ioend)
  148. {
  149. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  150. struct xfs_trans *tp;
  151. int error;
  152. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  153. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
  154. if (error) {
  155. xfs_trans_cancel(tp);
  156. return error;
  157. }
  158. ioend->io_append_trans = tp;
  159. /*
  160. * We may pass freeze protection with a transaction. So tell lockdep
  161. * we released it.
  162. */
  163. __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
  164. /*
  165. * We hand off the transaction to the completion thread now, so
  166. * clear the flag here.
  167. */
  168. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  169. return 0;
  170. }
  171. /*
  172. * Update on-disk file size now that data has been written to disk.
  173. */
  174. STATIC int
  175. xfs_setfilesize(
  176. struct xfs_inode *ip,
  177. struct xfs_trans *tp,
  178. xfs_off_t offset,
  179. size_t size)
  180. {
  181. xfs_fsize_t isize;
  182. xfs_ilock(ip, XFS_ILOCK_EXCL);
  183. isize = xfs_new_eof(ip, offset + size);
  184. if (!isize) {
  185. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  186. xfs_trans_cancel(tp);
  187. return 0;
  188. }
  189. trace_xfs_setfilesize(ip, offset, size);
  190. ip->i_d.di_size = isize;
  191. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  192. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  193. return xfs_trans_commit(tp);
  194. }
  195. STATIC int
  196. xfs_setfilesize_ioend(
  197. struct xfs_ioend *ioend,
  198. int error)
  199. {
  200. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  201. struct xfs_trans *tp = ioend->io_append_trans;
  202. /*
  203. * The transaction may have been allocated in the I/O submission thread,
  204. * thus we need to mark ourselves as being in a transaction manually.
  205. * Similarly for freeze protection.
  206. */
  207. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  208. __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
  209. /* we abort the update if there was an IO error */
  210. if (error) {
  211. xfs_trans_cancel(tp);
  212. return error;
  213. }
  214. return xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
  215. }
  216. /*
  217. * IO write completion.
  218. */
  219. STATIC void
  220. xfs_end_io(
  221. struct work_struct *work)
  222. {
  223. struct xfs_ioend *ioend =
  224. container_of(work, struct xfs_ioend, io_work);
  225. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  226. int error = ioend->io_bio->bi_error;
  227. /*
  228. * Set an error if the mount has shut down and proceed with end I/O
  229. * processing so it can perform whatever cleanups are necessary.
  230. */
  231. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  232. error = -EIO;
  233. /*
  234. * For unwritten extents we need to issue transactions to convert a
  235. * range to normal written extens after the data I/O has finished.
  236. * Detecting and handling completion IO errors is done individually
  237. * for each case as different cleanup operations need to be performed
  238. * on error.
  239. */
  240. if (ioend->io_type == XFS_IO_UNWRITTEN) {
  241. if (error)
  242. goto done;
  243. error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
  244. ioend->io_size);
  245. } else if (ioend->io_append_trans) {
  246. error = xfs_setfilesize_ioend(ioend, error);
  247. } else {
  248. ASSERT(!xfs_ioend_is_append(ioend));
  249. }
  250. done:
  251. xfs_destroy_ioend(ioend, error);
  252. }
  253. STATIC void
  254. xfs_end_bio(
  255. struct bio *bio)
  256. {
  257. struct xfs_ioend *ioend = bio->bi_private;
  258. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  259. if (ioend->io_type == XFS_IO_UNWRITTEN)
  260. queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
  261. else if (ioend->io_append_trans)
  262. queue_work(mp->m_data_workqueue, &ioend->io_work);
  263. else
  264. xfs_destroy_ioend(ioend, bio->bi_error);
  265. }
  266. STATIC int
  267. xfs_map_blocks(
  268. struct inode *inode,
  269. loff_t offset,
  270. struct xfs_bmbt_irec *imap,
  271. int type)
  272. {
  273. struct xfs_inode *ip = XFS_I(inode);
  274. struct xfs_mount *mp = ip->i_mount;
  275. ssize_t count = 1 << inode->i_blkbits;
  276. xfs_fileoff_t offset_fsb, end_fsb;
  277. int error = 0;
  278. int bmapi_flags = XFS_BMAPI_ENTIRE;
  279. int nimaps = 1;
  280. if (XFS_FORCED_SHUTDOWN(mp))
  281. return -EIO;
  282. if (type == XFS_IO_UNWRITTEN)
  283. bmapi_flags |= XFS_BMAPI_IGSTATE;
  284. xfs_ilock(ip, XFS_ILOCK_SHARED);
  285. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  286. (ip->i_df.if_flags & XFS_IFEXTENTS));
  287. ASSERT(offset <= mp->m_super->s_maxbytes);
  288. if (offset + count > mp->m_super->s_maxbytes)
  289. count = mp->m_super->s_maxbytes - offset;
  290. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  291. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  292. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  293. imap, &nimaps, bmapi_flags);
  294. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  295. if (error)
  296. return error;
  297. if (type == XFS_IO_DELALLOC &&
  298. (!nimaps || isnullstartblock(imap->br_startblock))) {
  299. error = xfs_iomap_write_allocate(ip, offset, imap);
  300. if (!error)
  301. trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
  302. return error;
  303. }
  304. #ifdef DEBUG
  305. if (type == XFS_IO_UNWRITTEN) {
  306. ASSERT(nimaps);
  307. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  308. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  309. }
  310. #endif
  311. if (nimaps)
  312. trace_xfs_map_blocks_found(ip, offset, count, type, imap);
  313. return 0;
  314. }
  315. STATIC bool
  316. xfs_imap_valid(
  317. struct inode *inode,
  318. struct xfs_bmbt_irec *imap,
  319. xfs_off_t offset)
  320. {
  321. offset >>= inode->i_blkbits;
  322. return offset >= imap->br_startoff &&
  323. offset < imap->br_startoff + imap->br_blockcount;
  324. }
  325. STATIC void
  326. xfs_start_buffer_writeback(
  327. struct buffer_head *bh)
  328. {
  329. ASSERT(buffer_mapped(bh));
  330. ASSERT(buffer_locked(bh));
  331. ASSERT(!buffer_delay(bh));
  332. ASSERT(!buffer_unwritten(bh));
  333. mark_buffer_async_write(bh);
  334. set_buffer_uptodate(bh);
  335. clear_buffer_dirty(bh);
  336. }
  337. STATIC void
  338. xfs_start_page_writeback(
  339. struct page *page,
  340. int clear_dirty)
  341. {
  342. ASSERT(PageLocked(page));
  343. ASSERT(!PageWriteback(page));
  344. /*
  345. * if the page was not fully cleaned, we need to ensure that the higher
  346. * layers come back to it correctly. That means we need to keep the page
  347. * dirty, and for WB_SYNC_ALL writeback we need to ensure the
  348. * PAGECACHE_TAG_TOWRITE index mark is not removed so another attempt to
  349. * write this page in this writeback sweep will be made.
  350. */
  351. if (clear_dirty) {
  352. clear_page_dirty_for_io(page);
  353. set_page_writeback(page);
  354. } else
  355. set_page_writeback_keepwrite(page);
  356. unlock_page(page);
  357. }
  358. static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  359. {
  360. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  361. }
  362. /*
  363. * Submit the bio for an ioend. We are passed an ioend with a bio attached to
  364. * it, and we submit that bio. The ioend may be used for multiple bio
  365. * submissions, so we only want to allocate an append transaction for the ioend
  366. * once. In the case of multiple bio submission, each bio will take an IO
  367. * reference to the ioend to ensure that the ioend completion is only done once
  368. * all bios have been submitted and the ioend is really done.
  369. *
  370. * If @fail is non-zero, it means that we have a situation where some part of
  371. * the submission process has failed after we have marked paged for writeback
  372. * and unlocked them. In this situation, we need to fail the bio and ioend
  373. * rather than submit it to IO. This typically only happens on a filesystem
  374. * shutdown.
  375. */
  376. STATIC int
  377. xfs_submit_ioend(
  378. struct writeback_control *wbc,
  379. struct xfs_ioend *ioend,
  380. int status)
  381. {
  382. /* Reserve log space if we might write beyond the on-disk inode size. */
  383. if (!status &&
  384. ioend->io_type != XFS_IO_UNWRITTEN &&
  385. xfs_ioend_is_append(ioend) &&
  386. !ioend->io_append_trans)
  387. status = xfs_setfilesize_trans_alloc(ioend);
  388. ioend->io_bio->bi_private = ioend;
  389. ioend->io_bio->bi_end_io = xfs_end_bio;
  390. /*
  391. * If we are failing the IO now, just mark the ioend with an
  392. * error and finish it. This will run IO completion immediately
  393. * as there is only one reference to the ioend at this point in
  394. * time.
  395. */
  396. if (status) {
  397. ioend->io_bio->bi_error = status;
  398. bio_endio(ioend->io_bio);
  399. return status;
  400. }
  401. submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE,
  402. ioend->io_bio);
  403. return 0;
  404. }
  405. static void
  406. xfs_init_bio_from_bh(
  407. struct bio *bio,
  408. struct buffer_head *bh)
  409. {
  410. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  411. bio->bi_bdev = bh->b_bdev;
  412. }
  413. static struct xfs_ioend *
  414. xfs_alloc_ioend(
  415. struct inode *inode,
  416. unsigned int type,
  417. xfs_off_t offset,
  418. struct buffer_head *bh)
  419. {
  420. struct xfs_ioend *ioend;
  421. struct bio *bio;
  422. bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, xfs_ioend_bioset);
  423. xfs_init_bio_from_bh(bio, bh);
  424. ioend = container_of(bio, struct xfs_ioend, io_inline_bio);
  425. INIT_LIST_HEAD(&ioend->io_list);
  426. ioend->io_type = type;
  427. ioend->io_inode = inode;
  428. ioend->io_size = 0;
  429. ioend->io_offset = offset;
  430. INIT_WORK(&ioend->io_work, xfs_end_io);
  431. ioend->io_append_trans = NULL;
  432. ioend->io_bio = bio;
  433. return ioend;
  434. }
  435. /*
  436. * Allocate a new bio, and chain the old bio to the new one.
  437. *
  438. * Note that we have to do perform the chaining in this unintuitive order
  439. * so that the bi_private linkage is set up in the right direction for the
  440. * traversal in xfs_destroy_ioend().
  441. */
  442. static void
  443. xfs_chain_bio(
  444. struct xfs_ioend *ioend,
  445. struct writeback_control *wbc,
  446. struct buffer_head *bh)
  447. {
  448. struct bio *new;
  449. new = bio_alloc(GFP_NOFS, BIO_MAX_PAGES);
  450. xfs_init_bio_from_bh(new, bh);
  451. bio_chain(ioend->io_bio, new);
  452. bio_get(ioend->io_bio); /* for xfs_destroy_ioend */
  453. submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE,
  454. ioend->io_bio);
  455. ioend->io_bio = new;
  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 the ioend we finished off so that the caller can submit it
  462. * once it has finished processing the dirty page.
  463. */
  464. STATIC void
  465. xfs_add_to_ioend(
  466. struct inode *inode,
  467. struct buffer_head *bh,
  468. xfs_off_t offset,
  469. struct xfs_writepage_ctx *wpc,
  470. struct writeback_control *wbc,
  471. struct list_head *iolist)
  472. {
  473. if (!wpc->ioend || wpc->io_type != wpc->ioend->io_type ||
  474. bh->b_blocknr != wpc->last_block + 1 ||
  475. offset != wpc->ioend->io_offset + wpc->ioend->io_size) {
  476. if (wpc->ioend)
  477. list_add(&wpc->ioend->io_list, iolist);
  478. wpc->ioend = xfs_alloc_ioend(inode, wpc->io_type, offset, bh);
  479. }
  480. /*
  481. * If the buffer doesn't fit into the bio we need to allocate a new
  482. * one. This shouldn't happen more than once for a given buffer.
  483. */
  484. while (xfs_bio_add_buffer(wpc->ioend->io_bio, bh) != bh->b_size)
  485. xfs_chain_bio(wpc->ioend, wbc, bh);
  486. wpc->ioend->io_size += bh->b_size;
  487. wpc->last_block = bh->b_blocknr;
  488. xfs_start_buffer_writeback(bh);
  489. }
  490. STATIC void
  491. xfs_map_buffer(
  492. struct inode *inode,
  493. struct buffer_head *bh,
  494. struct xfs_bmbt_irec *imap,
  495. xfs_off_t offset)
  496. {
  497. sector_t bn;
  498. struct xfs_mount *m = XFS_I(inode)->i_mount;
  499. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
  500. xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
  501. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  502. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  503. bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
  504. ((offset - iomap_offset) >> inode->i_blkbits);
  505. ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
  506. bh->b_blocknr = bn;
  507. set_buffer_mapped(bh);
  508. }
  509. STATIC void
  510. xfs_map_at_offset(
  511. struct inode *inode,
  512. struct buffer_head *bh,
  513. struct xfs_bmbt_irec *imap,
  514. xfs_off_t offset)
  515. {
  516. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  517. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  518. xfs_map_buffer(inode, bh, imap, offset);
  519. set_buffer_mapped(bh);
  520. clear_buffer_delay(bh);
  521. clear_buffer_unwritten(bh);
  522. }
  523. /*
  524. * Test if a given page contains at least one buffer of a given @type.
  525. * If @check_all_buffers is true, then we walk all the buffers in the page to
  526. * try to find one of the type passed in. If it is not set, then the caller only
  527. * needs to check the first buffer on the page for a match.
  528. */
  529. STATIC bool
  530. xfs_check_page_type(
  531. struct page *page,
  532. unsigned int type,
  533. bool check_all_buffers)
  534. {
  535. struct buffer_head *bh;
  536. struct buffer_head *head;
  537. if (PageWriteback(page))
  538. return false;
  539. if (!page->mapping)
  540. return false;
  541. if (!page_has_buffers(page))
  542. return false;
  543. bh = head = page_buffers(page);
  544. do {
  545. if (buffer_unwritten(bh)) {
  546. if (type == XFS_IO_UNWRITTEN)
  547. return true;
  548. } else if (buffer_delay(bh)) {
  549. if (type == XFS_IO_DELALLOC)
  550. return true;
  551. } else if (buffer_dirty(bh) && buffer_mapped(bh)) {
  552. if (type == XFS_IO_OVERWRITE)
  553. return true;
  554. }
  555. /* If we are only checking the first buffer, we are done now. */
  556. if (!check_all_buffers)
  557. break;
  558. } while ((bh = bh->b_this_page) != head);
  559. return false;
  560. }
  561. STATIC void
  562. xfs_vm_invalidatepage(
  563. struct page *page,
  564. unsigned int offset,
  565. unsigned int length)
  566. {
  567. trace_xfs_invalidatepage(page->mapping->host, page, offset,
  568. length);
  569. block_invalidatepage(page, offset, length);
  570. }
  571. /*
  572. * If the page has delalloc buffers on it, we need to punch them out before we
  573. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  574. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  575. * is done on that same region - the delalloc extent is returned when none is
  576. * supposed to be there.
  577. *
  578. * We prevent this by truncating away the delalloc regions on the page before
  579. * invalidating it. Because they are delalloc, we can do this without needing a
  580. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  581. * truncation without a transaction as there is no space left for block
  582. * reservation (typically why we see a ENOSPC in writeback).
  583. *
  584. * This is not a performance critical path, so for now just do the punching a
  585. * buffer head at a time.
  586. */
  587. STATIC void
  588. xfs_aops_discard_page(
  589. struct page *page)
  590. {
  591. struct inode *inode = page->mapping->host;
  592. struct xfs_inode *ip = XFS_I(inode);
  593. struct buffer_head *bh, *head;
  594. loff_t offset = page_offset(page);
  595. if (!xfs_check_page_type(page, XFS_IO_DELALLOC, true))
  596. goto out_invalidate;
  597. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  598. goto out_invalidate;
  599. xfs_alert(ip->i_mount,
  600. "page discard on page %p, inode 0x%llx, offset %llu.",
  601. page, ip->i_ino, offset);
  602. xfs_ilock(ip, XFS_ILOCK_EXCL);
  603. bh = head = page_buffers(page);
  604. do {
  605. int error;
  606. xfs_fileoff_t start_fsb;
  607. if (!buffer_delay(bh))
  608. goto next_buffer;
  609. start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  610. error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
  611. if (error) {
  612. /* something screwed, just bail */
  613. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  614. xfs_alert(ip->i_mount,
  615. "page discard unable to remove delalloc mapping.");
  616. }
  617. break;
  618. }
  619. next_buffer:
  620. offset += 1 << inode->i_blkbits;
  621. } while ((bh = bh->b_this_page) != head);
  622. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  623. out_invalidate:
  624. xfs_vm_invalidatepage(page, 0, PAGE_CACHE_SIZE);
  625. return;
  626. }
  627. /*
  628. * We implement an immediate ioend submission policy here to avoid needing to
  629. * chain multiple ioends and hence nest mempool allocations which can violate
  630. * forward progress guarantees we need to provide. The current ioend we are
  631. * adding buffers to is cached on the writepage context, and if the new buffer
  632. * does not append to the cached ioend it will create a new ioend and cache that
  633. * instead.
  634. *
  635. * If a new ioend is created and cached, the old ioend is returned and queued
  636. * locally for submission once the entire page is processed or an error has been
  637. * detected. While ioends are submitted immediately after they are completed,
  638. * batching optimisations are provided by higher level block plugging.
  639. *
  640. * At the end of a writeback pass, there will be a cached ioend remaining on the
  641. * writepage context that the caller will need to submit.
  642. */
  643. static int
  644. xfs_writepage_map(
  645. struct xfs_writepage_ctx *wpc,
  646. struct writeback_control *wbc,
  647. struct inode *inode,
  648. struct page *page,
  649. loff_t offset,
  650. __uint64_t end_offset)
  651. {
  652. LIST_HEAD(submit_list);
  653. struct xfs_ioend *ioend, *next;
  654. struct buffer_head *bh, *head;
  655. ssize_t len = 1 << inode->i_blkbits;
  656. int error = 0;
  657. int count = 0;
  658. int uptodate = 1;
  659. bh = head = page_buffers(page);
  660. offset = page_offset(page);
  661. do {
  662. if (offset >= end_offset)
  663. break;
  664. if (!buffer_uptodate(bh))
  665. uptodate = 0;
  666. /*
  667. * set_page_dirty dirties all buffers in a page, independent
  668. * of their state. The dirty state however is entirely
  669. * meaningless for holes (!mapped && uptodate), so skip
  670. * buffers covering holes here.
  671. */
  672. if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
  673. wpc->imap_valid = false;
  674. continue;
  675. }
  676. if (buffer_unwritten(bh)) {
  677. if (wpc->io_type != XFS_IO_UNWRITTEN) {
  678. wpc->io_type = XFS_IO_UNWRITTEN;
  679. wpc->imap_valid = false;
  680. }
  681. } else if (buffer_delay(bh)) {
  682. if (wpc->io_type != XFS_IO_DELALLOC) {
  683. wpc->io_type = XFS_IO_DELALLOC;
  684. wpc->imap_valid = false;
  685. }
  686. } else if (buffer_uptodate(bh)) {
  687. if (wpc->io_type != XFS_IO_OVERWRITE) {
  688. wpc->io_type = XFS_IO_OVERWRITE;
  689. wpc->imap_valid = false;
  690. }
  691. } else {
  692. if (PageUptodate(page))
  693. ASSERT(buffer_mapped(bh));
  694. /*
  695. * This buffer is not uptodate and will not be
  696. * written to disk. Ensure that we will put any
  697. * subsequent writeable buffers into a new
  698. * ioend.
  699. */
  700. wpc->imap_valid = false;
  701. continue;
  702. }
  703. if (wpc->imap_valid)
  704. wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap,
  705. offset);
  706. if (!wpc->imap_valid) {
  707. error = xfs_map_blocks(inode, offset, &wpc->imap,
  708. wpc->io_type);
  709. if (error)
  710. goto out;
  711. wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap,
  712. offset);
  713. }
  714. if (wpc->imap_valid) {
  715. lock_buffer(bh);
  716. if (wpc->io_type != XFS_IO_OVERWRITE)
  717. xfs_map_at_offset(inode, bh, &wpc->imap, offset);
  718. xfs_add_to_ioend(inode, bh, offset, wpc, wbc, &submit_list);
  719. count++;
  720. }
  721. } while (offset += len, ((bh = bh->b_this_page) != head));
  722. if (uptodate && bh == head)
  723. SetPageUptodate(page);
  724. ASSERT(wpc->ioend || list_empty(&submit_list));
  725. out:
  726. /*
  727. * On error, we have to fail the ioend here because we have locked
  728. * buffers in the ioend. If we don't do this, we'll deadlock
  729. * invalidating the page as that tries to lock the buffers on the page.
  730. * Also, because we may have set pages under writeback, we have to make
  731. * sure we run IO completion to mark the error state of the IO
  732. * appropriately, so we can't cancel the ioend directly here. That means
  733. * we have to mark this page as under writeback if we included any
  734. * buffers from it in the ioend chain so that completion treats it
  735. * correctly.
  736. *
  737. * If we didn't include the page in the ioend, the on error we can
  738. * simply discard and unlock it as there are no other users of the page
  739. * or it's buffers right now. The caller will still need to trigger
  740. * submission of outstanding ioends on the writepage context so they are
  741. * treated correctly on error.
  742. */
  743. if (count) {
  744. xfs_start_page_writeback(page, !error);
  745. /*
  746. * Preserve the original error if there was one, otherwise catch
  747. * submission errors here and propagate into subsequent ioend
  748. * submissions.
  749. */
  750. list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
  751. int error2;
  752. list_del_init(&ioend->io_list);
  753. error2 = xfs_submit_ioend(wbc, ioend, error);
  754. if (error2 && !error)
  755. error = error2;
  756. }
  757. } else if (error) {
  758. xfs_aops_discard_page(page);
  759. ClearPageUptodate(page);
  760. unlock_page(page);
  761. } else {
  762. /*
  763. * We can end up here with no error and nothing to write if we
  764. * race with a partial page truncate on a sub-page block sized
  765. * filesystem. In that case we need to mark the page clean.
  766. */
  767. xfs_start_page_writeback(page, 1);
  768. end_page_writeback(page);
  769. }
  770. mapping_set_error(page->mapping, error);
  771. return error;
  772. }
  773. /*
  774. * Write out a dirty page.
  775. *
  776. * For delalloc space on the page we need to allocate space and flush it.
  777. * For unwritten space on the page we need to start the conversion to
  778. * regular allocated space.
  779. * For any other dirty buffer heads on the page we should flush them.
  780. */
  781. STATIC int
  782. xfs_do_writepage(
  783. struct page *page,
  784. struct writeback_control *wbc,
  785. void *data)
  786. {
  787. struct xfs_writepage_ctx *wpc = data;
  788. struct inode *inode = page->mapping->host;
  789. loff_t offset;
  790. __uint64_t end_offset;
  791. pgoff_t end_index;
  792. trace_xfs_writepage(inode, page, 0, 0);
  793. ASSERT(page_has_buffers(page));
  794. /*
  795. * Refuse to write the page out if we are called from reclaim context.
  796. *
  797. * This avoids stack overflows when called from deeply used stacks in
  798. * random callers for direct reclaim or memcg reclaim. We explicitly
  799. * allow reclaim from kswapd as the stack usage there is relatively low.
  800. *
  801. * This should never happen except in the case of a VM regression so
  802. * warn about it.
  803. */
  804. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  805. PF_MEMALLOC))
  806. goto redirty;
  807. /*
  808. * Given that we do not allow direct reclaim to call us, we should
  809. * never be called while in a filesystem transaction.
  810. */
  811. if (WARN_ON_ONCE(current->flags & PF_FSTRANS))
  812. goto redirty;
  813. /*
  814. * Is this page beyond the end of the file?
  815. *
  816. * The page index is less than the end_index, adjust the end_offset
  817. * to the highest offset that this page should represent.
  818. * -----------------------------------------------------
  819. * | file mapping | <EOF> |
  820. * -----------------------------------------------------
  821. * | Page ... | Page N-2 | Page N-1 | Page N | |
  822. * ^--------------------------------^----------|--------
  823. * | desired writeback range | see else |
  824. * ---------------------------------^------------------|
  825. */
  826. offset = i_size_read(inode);
  827. end_index = offset >> PAGE_CACHE_SHIFT;
  828. if (page->index < end_index)
  829. end_offset = (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT;
  830. else {
  831. /*
  832. * Check whether the page to write out is beyond or straddles
  833. * i_size or not.
  834. * -------------------------------------------------------
  835. * | file mapping | <EOF> |
  836. * -------------------------------------------------------
  837. * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
  838. * ^--------------------------------^-----------|---------
  839. * | | Straddles |
  840. * ---------------------------------^-----------|--------|
  841. */
  842. unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1);
  843. /*
  844. * Skip the page if it is fully outside i_size, e.g. due to a
  845. * truncate operation that is in progress. We must redirty the
  846. * page so that reclaim stops reclaiming it. Otherwise
  847. * xfs_vm_releasepage() is called on it and gets confused.
  848. *
  849. * Note that the end_index is unsigned long, it would overflow
  850. * if the given offset is greater than 16TB on 32-bit system
  851. * and if we do check the page is fully outside i_size or not
  852. * via "if (page->index >= end_index + 1)" as "end_index + 1"
  853. * will be evaluated to 0. Hence this page will be redirtied
  854. * and be written out repeatedly which would result in an
  855. * infinite loop, the user program that perform this operation
  856. * will hang. Instead, we can verify this situation by checking
  857. * if the page to write is totally beyond the i_size or if it's
  858. * offset is just equal to the EOF.
  859. */
  860. if (page->index > end_index ||
  861. (page->index == end_index && offset_into_page == 0))
  862. goto redirty;
  863. /*
  864. * The page straddles i_size. It must be zeroed out on each
  865. * and every writepage invocation because it may be mmapped.
  866. * "A file is mapped in multiples of the page size. For a file
  867. * that is not a multiple of the page size, the remaining
  868. * memory is zeroed when mapped, and writes to that region are
  869. * not written out to the file."
  870. */
  871. zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE);
  872. /* Adjust the end_offset to the end of file */
  873. end_offset = offset;
  874. }
  875. return xfs_writepage_map(wpc, wbc, inode, page, offset, end_offset);
  876. redirty:
  877. redirty_page_for_writepage(wbc, page);
  878. unlock_page(page);
  879. return 0;
  880. }
  881. STATIC int
  882. xfs_vm_writepage(
  883. struct page *page,
  884. struct writeback_control *wbc)
  885. {
  886. struct xfs_writepage_ctx wpc = {
  887. .io_type = XFS_IO_INVALID,
  888. };
  889. int ret;
  890. ret = xfs_do_writepage(page, wbc, &wpc);
  891. if (wpc.ioend)
  892. ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
  893. return ret;
  894. }
  895. STATIC int
  896. xfs_vm_writepages(
  897. struct address_space *mapping,
  898. struct writeback_control *wbc)
  899. {
  900. struct xfs_writepage_ctx wpc = {
  901. .io_type = XFS_IO_INVALID,
  902. };
  903. int ret;
  904. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  905. if (dax_mapping(mapping))
  906. return dax_writeback_mapping_range(mapping,
  907. xfs_find_bdev_for_inode(mapping->host), wbc);
  908. ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc);
  909. if (wpc.ioend)
  910. ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
  911. return ret;
  912. }
  913. /*
  914. * Called to move a page into cleanable state - and from there
  915. * to be released. The page should already be clean. We always
  916. * have buffer heads in this call.
  917. *
  918. * Returns 1 if the page is ok to release, 0 otherwise.
  919. */
  920. STATIC int
  921. xfs_vm_releasepage(
  922. struct page *page,
  923. gfp_t gfp_mask)
  924. {
  925. int delalloc, unwritten;
  926. trace_xfs_releasepage(page->mapping->host, page, 0, 0);
  927. xfs_count_page_state(page, &delalloc, &unwritten);
  928. if (WARN_ON_ONCE(delalloc))
  929. return 0;
  930. if (WARN_ON_ONCE(unwritten))
  931. return 0;
  932. return try_to_free_buffers(page);
  933. }
  934. /*
  935. * When we map a DIO buffer, we may need to pass flags to
  936. * xfs_end_io_direct_write to tell it what kind of write IO we are doing.
  937. *
  938. * Note that for DIO, an IO to the highest supported file block offset (i.e.
  939. * 2^63 - 1FSB bytes) will result in the offset + count overflowing a signed 64
  940. * bit variable. Hence if we see this overflow, we have to assume that the IO is
  941. * extending the file size. We won't know for sure until IO completion is run
  942. * and the actual max write offset is communicated to the IO completion
  943. * routine.
  944. */
  945. static void
  946. xfs_map_direct(
  947. struct inode *inode,
  948. struct buffer_head *bh_result,
  949. struct xfs_bmbt_irec *imap,
  950. xfs_off_t offset)
  951. {
  952. uintptr_t *flags = (uintptr_t *)&bh_result->b_private;
  953. xfs_off_t size = bh_result->b_size;
  954. trace_xfs_get_blocks_map_direct(XFS_I(inode), offset, size,
  955. ISUNWRITTEN(imap) ? XFS_IO_UNWRITTEN : XFS_IO_OVERWRITE, imap);
  956. if (ISUNWRITTEN(imap)) {
  957. *flags |= XFS_DIO_FLAG_UNWRITTEN;
  958. set_buffer_defer_completion(bh_result);
  959. } else if (offset + size > i_size_read(inode) || offset + size < 0) {
  960. *flags |= XFS_DIO_FLAG_APPEND;
  961. set_buffer_defer_completion(bh_result);
  962. }
  963. }
  964. /*
  965. * If this is O_DIRECT or the mpage code calling tell them how large the mapping
  966. * is, so that we can avoid repeated get_blocks calls.
  967. *
  968. * If the mapping spans EOF, then we have to break the mapping up as the mapping
  969. * for blocks beyond EOF must be marked new so that sub block regions can be
  970. * correctly zeroed. We can't do this for mappings within EOF unless the mapping
  971. * was just allocated or is unwritten, otherwise the callers would overwrite
  972. * existing data with zeros. Hence we have to split the mapping into a range up
  973. * to and including EOF, and a second mapping for beyond EOF.
  974. */
  975. static void
  976. xfs_map_trim_size(
  977. struct inode *inode,
  978. sector_t iblock,
  979. struct buffer_head *bh_result,
  980. struct xfs_bmbt_irec *imap,
  981. xfs_off_t offset,
  982. ssize_t size)
  983. {
  984. xfs_off_t mapping_size;
  985. mapping_size = imap->br_startoff + imap->br_blockcount - iblock;
  986. mapping_size <<= inode->i_blkbits;
  987. ASSERT(mapping_size > 0);
  988. if (mapping_size > size)
  989. mapping_size = size;
  990. if (offset < i_size_read(inode) &&
  991. offset + mapping_size >= i_size_read(inode)) {
  992. /* limit mapping to block that spans EOF */
  993. mapping_size = roundup_64(i_size_read(inode) - offset,
  994. 1 << inode->i_blkbits);
  995. }
  996. if (mapping_size > LONG_MAX)
  997. mapping_size = LONG_MAX;
  998. bh_result->b_size = mapping_size;
  999. }
  1000. STATIC int
  1001. __xfs_get_blocks(
  1002. struct inode *inode,
  1003. sector_t iblock,
  1004. struct buffer_head *bh_result,
  1005. int create,
  1006. bool direct,
  1007. bool dax_fault)
  1008. {
  1009. struct xfs_inode *ip = XFS_I(inode);
  1010. struct xfs_mount *mp = ip->i_mount;
  1011. xfs_fileoff_t offset_fsb, end_fsb;
  1012. int error = 0;
  1013. int lockmode = 0;
  1014. struct xfs_bmbt_irec imap;
  1015. int nimaps = 1;
  1016. xfs_off_t offset;
  1017. ssize_t size;
  1018. int new = 0;
  1019. if (XFS_FORCED_SHUTDOWN(mp))
  1020. return -EIO;
  1021. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1022. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1023. size = bh_result->b_size;
  1024. if (!create && direct && offset >= i_size_read(inode))
  1025. return 0;
  1026. /*
  1027. * Direct I/O is usually done on preallocated files, so try getting
  1028. * a block mapping without an exclusive lock first. For buffered
  1029. * writes we already have the exclusive iolock anyway, so avoiding
  1030. * a lock roundtrip here by taking the ilock exclusive from the
  1031. * beginning is a useful micro optimization.
  1032. */
  1033. if (create && !direct) {
  1034. lockmode = XFS_ILOCK_EXCL;
  1035. xfs_ilock(ip, lockmode);
  1036. } else {
  1037. lockmode = xfs_ilock_data_map_shared(ip);
  1038. }
  1039. ASSERT(offset <= mp->m_super->s_maxbytes);
  1040. if (offset + size > mp->m_super->s_maxbytes)
  1041. size = mp->m_super->s_maxbytes - offset;
  1042. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
  1043. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1044. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  1045. &imap, &nimaps, XFS_BMAPI_ENTIRE);
  1046. if (error)
  1047. goto out_unlock;
  1048. /* for DAX, we convert unwritten extents directly */
  1049. if (create &&
  1050. (!nimaps ||
  1051. (imap.br_startblock == HOLESTARTBLOCK ||
  1052. imap.br_startblock == DELAYSTARTBLOCK) ||
  1053. (IS_DAX(inode) && ISUNWRITTEN(&imap)))) {
  1054. if (direct || xfs_get_extsz_hint(ip)) {
  1055. /*
  1056. * xfs_iomap_write_direct() expects the shared lock. It
  1057. * is unlocked on return.
  1058. */
  1059. if (lockmode == XFS_ILOCK_EXCL)
  1060. xfs_ilock_demote(ip, lockmode);
  1061. error = xfs_iomap_write_direct(ip, offset, size,
  1062. &imap, nimaps);
  1063. if (error)
  1064. return error;
  1065. new = 1;
  1066. } else {
  1067. /*
  1068. * Delalloc reservations do not require a transaction,
  1069. * we can go on without dropping the lock here. If we
  1070. * are allocating a new delalloc block, make sure that
  1071. * we set the new flag so that we mark the buffer new so
  1072. * that we know that it is newly allocated if the write
  1073. * fails.
  1074. */
  1075. if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
  1076. new = 1;
  1077. error = xfs_iomap_write_delay(ip, offset, size, &imap);
  1078. if (error)
  1079. goto out_unlock;
  1080. xfs_iunlock(ip, lockmode);
  1081. }
  1082. trace_xfs_get_blocks_alloc(ip, offset, size,
  1083. ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
  1084. : XFS_IO_DELALLOC, &imap);
  1085. } else if (nimaps) {
  1086. trace_xfs_get_blocks_found(ip, offset, size,
  1087. ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
  1088. : XFS_IO_OVERWRITE, &imap);
  1089. xfs_iunlock(ip, lockmode);
  1090. } else {
  1091. trace_xfs_get_blocks_notfound(ip, offset, size);
  1092. goto out_unlock;
  1093. }
  1094. if (IS_DAX(inode) && create) {
  1095. ASSERT(!ISUNWRITTEN(&imap));
  1096. /* zeroing is not needed at a higher layer */
  1097. new = 0;
  1098. }
  1099. /* trim mapping down to size requested */
  1100. if (direct || size > (1 << inode->i_blkbits))
  1101. xfs_map_trim_size(inode, iblock, bh_result,
  1102. &imap, offset, size);
  1103. /*
  1104. * For unwritten extents do not report a disk address in the buffered
  1105. * read case (treat as if we're reading into a hole).
  1106. */
  1107. if (imap.br_startblock != HOLESTARTBLOCK &&
  1108. imap.br_startblock != DELAYSTARTBLOCK &&
  1109. (create || !ISUNWRITTEN(&imap))) {
  1110. xfs_map_buffer(inode, bh_result, &imap, offset);
  1111. if (ISUNWRITTEN(&imap))
  1112. set_buffer_unwritten(bh_result);
  1113. /* direct IO needs special help */
  1114. if (create && direct) {
  1115. if (dax_fault)
  1116. ASSERT(!ISUNWRITTEN(&imap));
  1117. else
  1118. xfs_map_direct(inode, bh_result, &imap, offset);
  1119. }
  1120. }
  1121. /*
  1122. * If this is a realtime file, data may be on a different device.
  1123. * to that pointed to from the buffer_head b_bdev currently.
  1124. */
  1125. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1126. /*
  1127. * If we previously allocated a block out beyond eof and we are now
  1128. * coming back to use it then we will need to flag it as new even if it
  1129. * has a disk address.
  1130. *
  1131. * With sub-block writes into unwritten extents we also need to mark
  1132. * the buffer as new so that the unwritten parts of the buffer gets
  1133. * correctly zeroed.
  1134. */
  1135. if (create &&
  1136. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1137. (offset >= i_size_read(inode)) ||
  1138. (new || ISUNWRITTEN(&imap))))
  1139. set_buffer_new(bh_result);
  1140. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1141. BUG_ON(direct);
  1142. if (create) {
  1143. set_buffer_uptodate(bh_result);
  1144. set_buffer_mapped(bh_result);
  1145. set_buffer_delay(bh_result);
  1146. }
  1147. }
  1148. return 0;
  1149. out_unlock:
  1150. xfs_iunlock(ip, lockmode);
  1151. return error;
  1152. }
  1153. int
  1154. xfs_get_blocks(
  1155. struct inode *inode,
  1156. sector_t iblock,
  1157. struct buffer_head *bh_result,
  1158. int create)
  1159. {
  1160. return __xfs_get_blocks(inode, iblock, bh_result, create, false, false);
  1161. }
  1162. int
  1163. xfs_get_blocks_direct(
  1164. struct inode *inode,
  1165. sector_t iblock,
  1166. struct buffer_head *bh_result,
  1167. int create)
  1168. {
  1169. return __xfs_get_blocks(inode, iblock, bh_result, create, true, false);
  1170. }
  1171. int
  1172. xfs_get_blocks_dax_fault(
  1173. struct inode *inode,
  1174. sector_t iblock,
  1175. struct buffer_head *bh_result,
  1176. int create)
  1177. {
  1178. return __xfs_get_blocks(inode, iblock, bh_result, create, true, true);
  1179. }
  1180. /*
  1181. * Complete a direct I/O write request.
  1182. *
  1183. * xfs_map_direct passes us some flags in the private data to tell us what to
  1184. * do. If no flags are set, then the write IO is an overwrite wholly within
  1185. * the existing allocated file size and so there is nothing for us to do.
  1186. *
  1187. * Note that in this case the completion can be called in interrupt context,
  1188. * whereas if we have flags set we will always be called in task context
  1189. * (i.e. from a workqueue).
  1190. */
  1191. STATIC int
  1192. xfs_end_io_direct_write(
  1193. struct kiocb *iocb,
  1194. loff_t offset,
  1195. ssize_t size,
  1196. void *private)
  1197. {
  1198. struct inode *inode = file_inode(iocb->ki_filp);
  1199. struct xfs_inode *ip = XFS_I(inode);
  1200. struct xfs_mount *mp = ip->i_mount;
  1201. uintptr_t flags = (uintptr_t)private;
  1202. int error = 0;
  1203. trace_xfs_end_io_direct_write(ip, offset, size);
  1204. if (XFS_FORCED_SHUTDOWN(mp))
  1205. return -EIO;
  1206. if (size <= 0)
  1207. return size;
  1208. /*
  1209. * The flags tell us whether we are doing unwritten extent conversions
  1210. * or an append transaction that updates the on-disk file size. These
  1211. * cases are the only cases where we should *potentially* be needing
  1212. * to update the VFS inode size.
  1213. */
  1214. if (flags == 0) {
  1215. ASSERT(offset + size <= i_size_read(inode));
  1216. return 0;
  1217. }
  1218. /*
  1219. * We need to update the in-core inode size here so that we don't end up
  1220. * with the on-disk inode size being outside the in-core inode size. We
  1221. * have no other method of updating EOF for AIO, so always do it here
  1222. * if necessary.
  1223. *
  1224. * We need to lock the test/set EOF update as we can be racing with
  1225. * other IO completions here to update the EOF. Failing to serialise
  1226. * here can result in EOF moving backwards and Bad Things Happen when
  1227. * that occurs.
  1228. */
  1229. spin_lock(&ip->i_flags_lock);
  1230. if (offset + size > i_size_read(inode))
  1231. i_size_write(inode, offset + size);
  1232. spin_unlock(&ip->i_flags_lock);
  1233. if (flags & XFS_DIO_FLAG_UNWRITTEN) {
  1234. trace_xfs_end_io_direct_write_unwritten(ip, offset, size);
  1235. error = xfs_iomap_write_unwritten(ip, offset, size);
  1236. } else if (flags & XFS_DIO_FLAG_APPEND) {
  1237. struct xfs_trans *tp;
  1238. trace_xfs_end_io_direct_write_append(ip, offset, size);
  1239. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  1240. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
  1241. if (error) {
  1242. xfs_trans_cancel(tp);
  1243. return error;
  1244. }
  1245. error = xfs_setfilesize(ip, tp, offset, size);
  1246. }
  1247. return error;
  1248. }
  1249. STATIC ssize_t
  1250. xfs_vm_direct_IO(
  1251. struct kiocb *iocb,
  1252. struct iov_iter *iter,
  1253. loff_t offset)
  1254. {
  1255. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1256. dio_iodone_t *endio = NULL;
  1257. int flags = 0;
  1258. struct block_device *bdev;
  1259. if (iov_iter_rw(iter) == WRITE) {
  1260. endio = xfs_end_io_direct_write;
  1261. flags = DIO_ASYNC_EXTEND;
  1262. }
  1263. if (IS_DAX(inode)) {
  1264. return dax_do_io(iocb, inode, iter, offset,
  1265. xfs_get_blocks_direct, endio, 0);
  1266. }
  1267. bdev = xfs_find_bdev_for_inode(inode);
  1268. return __blockdev_direct_IO(iocb, inode, bdev, iter, offset,
  1269. xfs_get_blocks_direct, endio, NULL, flags);
  1270. }
  1271. /*
  1272. * Punch out the delalloc blocks we have already allocated.
  1273. *
  1274. * Don't bother with xfs_setattr given that nothing can have made it to disk yet
  1275. * as the page is still locked at this point.
  1276. */
  1277. STATIC void
  1278. xfs_vm_kill_delalloc_range(
  1279. struct inode *inode,
  1280. loff_t start,
  1281. loff_t end)
  1282. {
  1283. struct xfs_inode *ip = XFS_I(inode);
  1284. xfs_fileoff_t start_fsb;
  1285. xfs_fileoff_t end_fsb;
  1286. int error;
  1287. start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
  1288. end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
  1289. if (end_fsb <= start_fsb)
  1290. return;
  1291. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1292. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1293. end_fsb - start_fsb);
  1294. if (error) {
  1295. /* something screwed, just bail */
  1296. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1297. xfs_alert(ip->i_mount,
  1298. "xfs_vm_write_failed: unable to clean up ino %lld",
  1299. ip->i_ino);
  1300. }
  1301. }
  1302. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1303. }
  1304. STATIC void
  1305. xfs_vm_write_failed(
  1306. struct inode *inode,
  1307. struct page *page,
  1308. loff_t pos,
  1309. unsigned len)
  1310. {
  1311. loff_t block_offset;
  1312. loff_t block_start;
  1313. loff_t block_end;
  1314. loff_t from = pos & (PAGE_CACHE_SIZE - 1);
  1315. loff_t to = from + len;
  1316. struct buffer_head *bh, *head;
  1317. struct xfs_mount *mp = XFS_I(inode)->i_mount;
  1318. /*
  1319. * The request pos offset might be 32 or 64 bit, this is all fine
  1320. * on 64-bit platform. However, for 64-bit pos request on 32-bit
  1321. * platform, the high 32-bit will be masked off if we evaluate the
  1322. * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is
  1323. * 0xfffff000 as an unsigned long, hence the result is incorrect
  1324. * which could cause the following ASSERT failed in most cases.
  1325. * In order to avoid this, we can evaluate the block_offset of the
  1326. * start of the page by using shifts rather than masks the mismatch
  1327. * problem.
  1328. */
  1329. block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT;
  1330. ASSERT(block_offset + from == pos);
  1331. head = page_buffers(page);
  1332. block_start = 0;
  1333. for (bh = head; bh != head || !block_start;
  1334. bh = bh->b_this_page, block_start = block_end,
  1335. block_offset += bh->b_size) {
  1336. block_end = block_start + bh->b_size;
  1337. /* skip buffers before the write */
  1338. if (block_end <= from)
  1339. continue;
  1340. /* if the buffer is after the write, we're done */
  1341. if (block_start >= to)
  1342. break;
  1343. /*
  1344. * Process delalloc and unwritten buffers beyond EOF. We can
  1345. * encounter unwritten buffers in the event that a file has
  1346. * post-EOF unwritten extents and an extending write happens to
  1347. * fail (e.g., an unaligned write that also involves a delalloc
  1348. * to the same page).
  1349. */
  1350. if (!buffer_delay(bh) && !buffer_unwritten(bh))
  1351. continue;
  1352. if (!xfs_mp_fail_writes(mp) && !buffer_new(bh) &&
  1353. block_offset < i_size_read(inode))
  1354. continue;
  1355. if (buffer_delay(bh))
  1356. xfs_vm_kill_delalloc_range(inode, block_offset,
  1357. block_offset + bh->b_size);
  1358. /*
  1359. * This buffer does not contain data anymore. make sure anyone
  1360. * who finds it knows that for certain.
  1361. */
  1362. clear_buffer_delay(bh);
  1363. clear_buffer_uptodate(bh);
  1364. clear_buffer_mapped(bh);
  1365. clear_buffer_new(bh);
  1366. clear_buffer_dirty(bh);
  1367. clear_buffer_unwritten(bh);
  1368. }
  1369. }
  1370. /*
  1371. * This used to call block_write_begin(), but it unlocks and releases the page
  1372. * on error, and we need that page to be able to punch stale delalloc blocks out
  1373. * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
  1374. * the appropriate point.
  1375. */
  1376. STATIC int
  1377. xfs_vm_write_begin(
  1378. struct file *file,
  1379. struct address_space *mapping,
  1380. loff_t pos,
  1381. unsigned len,
  1382. unsigned flags,
  1383. struct page **pagep,
  1384. void **fsdata)
  1385. {
  1386. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1387. struct page *page;
  1388. int status;
  1389. struct xfs_mount *mp = XFS_I(mapping->host)->i_mount;
  1390. ASSERT(len <= PAGE_CACHE_SIZE);
  1391. page = grab_cache_page_write_begin(mapping, index, flags);
  1392. if (!page)
  1393. return -ENOMEM;
  1394. status = __block_write_begin(page, pos, len, xfs_get_blocks);
  1395. if (xfs_mp_fail_writes(mp))
  1396. status = -EIO;
  1397. if (unlikely(status)) {
  1398. struct inode *inode = mapping->host;
  1399. size_t isize = i_size_read(inode);
  1400. xfs_vm_write_failed(inode, page, pos, len);
  1401. unlock_page(page);
  1402. /*
  1403. * If the write is beyond EOF, we only want to kill blocks
  1404. * allocated in this write, not blocks that were previously
  1405. * written successfully.
  1406. */
  1407. if (xfs_mp_fail_writes(mp))
  1408. isize = 0;
  1409. if (pos + len > isize) {
  1410. ssize_t start = max_t(ssize_t, pos, isize);
  1411. truncate_pagecache_range(inode, start, pos + len);
  1412. }
  1413. page_cache_release(page);
  1414. page = NULL;
  1415. }
  1416. *pagep = page;
  1417. return status;
  1418. }
  1419. /*
  1420. * On failure, we only need to kill delalloc blocks beyond EOF in the range of
  1421. * this specific write because they will never be written. Previous writes
  1422. * beyond EOF where block allocation succeeded do not need to be trashed, so
  1423. * only new blocks from this write should be trashed. For blocks within
  1424. * EOF, generic_write_end() zeros them so they are safe to leave alone and be
  1425. * written with all the other valid data.
  1426. */
  1427. STATIC int
  1428. xfs_vm_write_end(
  1429. struct file *file,
  1430. struct address_space *mapping,
  1431. loff_t pos,
  1432. unsigned len,
  1433. unsigned copied,
  1434. struct page *page,
  1435. void *fsdata)
  1436. {
  1437. int ret;
  1438. ASSERT(len <= PAGE_CACHE_SIZE);
  1439. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  1440. if (unlikely(ret < len)) {
  1441. struct inode *inode = mapping->host;
  1442. size_t isize = i_size_read(inode);
  1443. loff_t to = pos + len;
  1444. if (to > isize) {
  1445. /* only kill blocks in this write beyond EOF */
  1446. if (pos > isize)
  1447. isize = pos;
  1448. xfs_vm_kill_delalloc_range(inode, isize, to);
  1449. truncate_pagecache_range(inode, isize, to);
  1450. }
  1451. }
  1452. return ret;
  1453. }
  1454. STATIC sector_t
  1455. xfs_vm_bmap(
  1456. struct address_space *mapping,
  1457. sector_t block)
  1458. {
  1459. struct inode *inode = (struct inode *)mapping->host;
  1460. struct xfs_inode *ip = XFS_I(inode);
  1461. trace_xfs_vm_bmap(XFS_I(inode));
  1462. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1463. filemap_write_and_wait(mapping);
  1464. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1465. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1466. }
  1467. STATIC int
  1468. xfs_vm_readpage(
  1469. struct file *unused,
  1470. struct page *page)
  1471. {
  1472. trace_xfs_vm_readpage(page->mapping->host, 1);
  1473. return mpage_readpage(page, xfs_get_blocks);
  1474. }
  1475. STATIC int
  1476. xfs_vm_readpages(
  1477. struct file *unused,
  1478. struct address_space *mapping,
  1479. struct list_head *pages,
  1480. unsigned nr_pages)
  1481. {
  1482. trace_xfs_vm_readpages(mapping->host, nr_pages);
  1483. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1484. }
  1485. /*
  1486. * This is basically a copy of __set_page_dirty_buffers() with one
  1487. * small tweak: buffers beyond EOF do not get marked dirty. If we mark them
  1488. * dirty, we'll never be able to clean them because we don't write buffers
  1489. * beyond EOF, and that means we can't invalidate pages that span EOF
  1490. * that have been marked dirty. Further, the dirty state can leak into
  1491. * the file interior if the file is extended, resulting in all sorts of
  1492. * bad things happening as the state does not match the underlying data.
  1493. *
  1494. * XXX: this really indicates that bufferheads in XFS need to die. Warts like
  1495. * this only exist because of bufferheads and how the generic code manages them.
  1496. */
  1497. STATIC int
  1498. xfs_vm_set_page_dirty(
  1499. struct page *page)
  1500. {
  1501. struct address_space *mapping = page->mapping;
  1502. struct inode *inode = mapping->host;
  1503. loff_t end_offset;
  1504. loff_t offset;
  1505. int newly_dirty;
  1506. if (unlikely(!mapping))
  1507. return !TestSetPageDirty(page);
  1508. end_offset = i_size_read(inode);
  1509. offset = page_offset(page);
  1510. spin_lock(&mapping->private_lock);
  1511. if (page_has_buffers(page)) {
  1512. struct buffer_head *head = page_buffers(page);
  1513. struct buffer_head *bh = head;
  1514. do {
  1515. if (offset < end_offset)
  1516. set_buffer_dirty(bh);
  1517. bh = bh->b_this_page;
  1518. offset += 1 << inode->i_blkbits;
  1519. } while (bh != head);
  1520. }
  1521. /*
  1522. * Lock out page->mem_cgroup migration to keep PageDirty
  1523. * synchronized with per-memcg dirty page counters.
  1524. */
  1525. lock_page_memcg(page);
  1526. newly_dirty = !TestSetPageDirty(page);
  1527. spin_unlock(&mapping->private_lock);
  1528. if (newly_dirty) {
  1529. /* sigh - __set_page_dirty() is static, so copy it here, too */
  1530. unsigned long flags;
  1531. spin_lock_irqsave(&mapping->tree_lock, flags);
  1532. if (page->mapping) { /* Race with truncate? */
  1533. WARN_ON_ONCE(!PageUptodate(page));
  1534. account_page_dirtied(page, mapping);
  1535. radix_tree_tag_set(&mapping->page_tree,
  1536. page_index(page), PAGECACHE_TAG_DIRTY);
  1537. }
  1538. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1539. }
  1540. unlock_page_memcg(page);
  1541. if (newly_dirty)
  1542. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1543. return newly_dirty;
  1544. }
  1545. const struct address_space_operations xfs_address_space_operations = {
  1546. .readpage = xfs_vm_readpage,
  1547. .readpages = xfs_vm_readpages,
  1548. .writepage = xfs_vm_writepage,
  1549. .writepages = xfs_vm_writepages,
  1550. .set_page_dirty = xfs_vm_set_page_dirty,
  1551. .releasepage = xfs_vm_releasepage,
  1552. .invalidatepage = xfs_vm_invalidatepage,
  1553. .write_begin = xfs_vm_write_begin,
  1554. .write_end = xfs_vm_write_end,
  1555. .bmap = xfs_vm_bmap,
  1556. .direct_IO = xfs_vm_direct_IO,
  1557. .migratepage = buffer_migrate_page,
  1558. .is_partially_uptodate = block_is_partially_uptodate,
  1559. .error_remove_page = generic_error_remove_page,
  1560. };