xfs_bmap_util.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * Copyright (c) 2012 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs_platform.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_bit.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_trans.h"
  19. #include "xfs_alloc.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_bmap_util.h"
  22. #include "xfs_bmap_btree.h"
  23. #include "xfs_rtalloc.h"
  24. #include "xfs_error.h"
  25. #include "xfs_quota.h"
  26. #include "xfs_trans_space.h"
  27. #include "xfs_trace.h"
  28. #include "xfs_icache.h"
  29. #include "xfs_iomap.h"
  30. #include "xfs_reflink.h"
  31. #include "xfs_rtbitmap.h"
  32. #include "xfs_rtgroup.h"
  33. #include "xfs_zone_alloc.h"
  34. /* Kernel only BMAP related definitions and functions */
  35. /*
  36. * Convert the given file system block to a disk block. We have to treat it
  37. * differently based on whether the file is a real time file or not, because the
  38. * bmap code does.
  39. */
  40. xfs_daddr_t
  41. xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
  42. {
  43. if (XFS_IS_REALTIME_INODE(ip))
  44. return xfs_rtb_to_daddr(ip->i_mount, fsb);
  45. return XFS_FSB_TO_DADDR(ip->i_mount, fsb);
  46. }
  47. /*
  48. * Routine to zero an extent on disk allocated to the specific inode.
  49. */
  50. int
  51. xfs_zero_extent(
  52. struct xfs_inode *ip,
  53. xfs_fsblock_t start_fsb,
  54. xfs_off_t count_fsb)
  55. {
  56. return blkdev_issue_zeroout(xfs_inode_buftarg(ip)->bt_bdev,
  57. xfs_fsb_to_db(ip, start_fsb),
  58. XFS_FSB_TO_BB(ip->i_mount, count_fsb),
  59. GFP_KERNEL, 0);
  60. }
  61. /*
  62. * Extent tree block counting routines.
  63. */
  64. /*
  65. * Count leaf blocks given a range of extent records. Delayed allocation
  66. * extents are not counted towards the totals.
  67. */
  68. xfs_extnum_t
  69. xfs_bmap_count_leaves(
  70. struct xfs_ifork *ifp,
  71. xfs_filblks_t *count)
  72. {
  73. struct xfs_iext_cursor icur;
  74. struct xfs_bmbt_irec got;
  75. xfs_extnum_t numrecs = 0;
  76. for_each_xfs_iext(ifp, &icur, &got) {
  77. if (!isnullstartblock(got.br_startblock)) {
  78. *count += got.br_blockcount;
  79. numrecs++;
  80. }
  81. }
  82. return numrecs;
  83. }
  84. /*
  85. * Count fsblocks of the given fork. Delayed allocation extents are
  86. * not counted towards the totals.
  87. */
  88. int
  89. xfs_bmap_count_blocks(
  90. struct xfs_trans *tp,
  91. struct xfs_inode *ip,
  92. int whichfork,
  93. xfs_extnum_t *nextents,
  94. xfs_filblks_t *count)
  95. {
  96. struct xfs_mount *mp = ip->i_mount;
  97. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  98. struct xfs_btree_cur *cur;
  99. xfs_filblks_t btblocks = 0;
  100. int error;
  101. *nextents = 0;
  102. *count = 0;
  103. if (!ifp)
  104. return 0;
  105. switch (ifp->if_format) {
  106. case XFS_DINODE_FMT_BTREE:
  107. error = xfs_iread_extents(tp, ip, whichfork);
  108. if (error)
  109. return error;
  110. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  111. error = xfs_btree_count_blocks(cur, &btblocks);
  112. xfs_btree_del_cursor(cur, error);
  113. if (error)
  114. return error;
  115. /*
  116. * xfs_btree_count_blocks includes the root block contained in
  117. * the inode fork in @btblocks, so subtract one because we're
  118. * only interested in allocated disk blocks.
  119. */
  120. *count += btblocks - 1;
  121. fallthrough;
  122. case XFS_DINODE_FMT_EXTENTS:
  123. *nextents = xfs_bmap_count_leaves(ifp, count);
  124. break;
  125. }
  126. return 0;
  127. }
  128. static int
  129. xfs_getbmap_report_one(
  130. struct xfs_inode *ip,
  131. struct getbmapx *bmv,
  132. struct kgetbmap *out,
  133. int64_t bmv_end,
  134. struct xfs_bmbt_irec *got)
  135. {
  136. struct kgetbmap *p = out + bmv->bmv_entries;
  137. bool shared = false;
  138. int error;
  139. error = xfs_reflink_trim_around_shared(ip, got, &shared);
  140. if (error)
  141. return error;
  142. if (isnullstartblock(got->br_startblock) ||
  143. got->br_startblock == DELAYSTARTBLOCK) {
  144. /*
  145. * Take the flush completion as being a point-in-time snapshot
  146. * where there are no delalloc extents, and if any new ones
  147. * have been created racily, just skip them as being 'after'
  148. * the flush and so don't get reported.
  149. */
  150. if (!(bmv->bmv_iflags & BMV_IF_DELALLOC))
  151. return 0;
  152. p->bmv_oflags |= BMV_OF_DELALLOC;
  153. p->bmv_block = -2;
  154. } else {
  155. p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
  156. }
  157. if (got->br_state == XFS_EXT_UNWRITTEN &&
  158. (bmv->bmv_iflags & BMV_IF_PREALLOC))
  159. p->bmv_oflags |= BMV_OF_PREALLOC;
  160. if (shared)
  161. p->bmv_oflags |= BMV_OF_SHARED;
  162. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
  163. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
  164. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  165. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  166. bmv->bmv_entries++;
  167. return 0;
  168. }
  169. static void
  170. xfs_getbmap_report_hole(
  171. struct xfs_inode *ip,
  172. struct getbmapx *bmv,
  173. struct kgetbmap *out,
  174. int64_t bmv_end,
  175. xfs_fileoff_t bno,
  176. xfs_fileoff_t end)
  177. {
  178. struct kgetbmap *p = out + bmv->bmv_entries;
  179. if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
  180. return;
  181. p->bmv_block = -1;
  182. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
  183. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
  184. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  185. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  186. bmv->bmv_entries++;
  187. }
  188. static inline bool
  189. xfs_getbmap_full(
  190. struct getbmapx *bmv)
  191. {
  192. return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
  193. }
  194. static bool
  195. xfs_getbmap_next_rec(
  196. struct xfs_bmbt_irec *rec,
  197. xfs_fileoff_t total_end)
  198. {
  199. xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount;
  200. if (end == total_end)
  201. return false;
  202. rec->br_startoff += rec->br_blockcount;
  203. if (!isnullstartblock(rec->br_startblock) &&
  204. rec->br_startblock != DELAYSTARTBLOCK)
  205. rec->br_startblock += rec->br_blockcount;
  206. rec->br_blockcount = total_end - end;
  207. return true;
  208. }
  209. /*
  210. * Get inode's extents as described in bmv, and format for output.
  211. * Calls formatter to fill the user's buffer until all extents
  212. * are mapped, until the passed-in bmv->bmv_count slots have
  213. * been filled, or until the formatter short-circuits the loop,
  214. * if it is tracking filled-in extents on its own.
  215. */
  216. int /* error code */
  217. xfs_getbmap(
  218. struct xfs_inode *ip,
  219. struct getbmapx *bmv, /* user bmap structure */
  220. struct kgetbmap *out)
  221. {
  222. struct xfs_mount *mp = ip->i_mount;
  223. int iflags = bmv->bmv_iflags;
  224. int whichfork, lock, error = 0;
  225. int64_t bmv_end, max_len;
  226. xfs_fileoff_t bno, first_bno;
  227. struct xfs_ifork *ifp;
  228. struct xfs_bmbt_irec got, rec;
  229. xfs_filblks_t len;
  230. struct xfs_iext_cursor icur;
  231. if (bmv->bmv_iflags & ~BMV_IF_VALID)
  232. return -EINVAL;
  233. #ifndef DEBUG
  234. /* Only allow CoW fork queries if we're debugging. */
  235. if (iflags & BMV_IF_COWFORK)
  236. return -EINVAL;
  237. #endif
  238. if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
  239. return -EINVAL;
  240. if (bmv->bmv_length < -1)
  241. return -EINVAL;
  242. bmv->bmv_entries = 0;
  243. if (bmv->bmv_length == 0)
  244. return 0;
  245. if (iflags & BMV_IF_ATTRFORK)
  246. whichfork = XFS_ATTR_FORK;
  247. else if (iflags & BMV_IF_COWFORK)
  248. whichfork = XFS_COW_FORK;
  249. else
  250. whichfork = XFS_DATA_FORK;
  251. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  252. switch (whichfork) {
  253. case XFS_ATTR_FORK:
  254. lock = xfs_ilock_attr_map_shared(ip);
  255. if (!xfs_inode_has_attr_fork(ip))
  256. goto out_unlock_ilock;
  257. max_len = 1LL << 32;
  258. break;
  259. case XFS_COW_FORK:
  260. lock = XFS_ILOCK_SHARED;
  261. xfs_ilock(ip, lock);
  262. /* No CoW fork? Just return */
  263. if (!xfs_ifork_ptr(ip, whichfork))
  264. goto out_unlock_ilock;
  265. if (xfs_get_cowextsz_hint(ip))
  266. max_len = mp->m_super->s_maxbytes;
  267. else
  268. max_len = XFS_ISIZE(ip);
  269. break;
  270. case XFS_DATA_FORK:
  271. if (!(iflags & BMV_IF_DELALLOC) &&
  272. (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
  273. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  274. if (error)
  275. goto out_unlock_iolock;
  276. /*
  277. * Even after flushing the inode, there can still be
  278. * delalloc blocks on the inode beyond EOF due to
  279. * speculative preallocation. These are not removed
  280. * until the release function is called or the inode
  281. * is inactivated. Hence we cannot assert here that
  282. * ip->i_delayed_blks == 0.
  283. */
  284. }
  285. if (xfs_get_extsz_hint(ip) ||
  286. (ip->i_diflags & XFS_DIFLAG_PREALLOC))
  287. max_len = mp->m_super->s_maxbytes;
  288. else
  289. max_len = XFS_ISIZE(ip);
  290. lock = xfs_ilock_data_map_shared(ip);
  291. break;
  292. }
  293. ifp = xfs_ifork_ptr(ip, whichfork);
  294. switch (ifp->if_format) {
  295. case XFS_DINODE_FMT_EXTENTS:
  296. case XFS_DINODE_FMT_BTREE:
  297. break;
  298. case XFS_DINODE_FMT_LOCAL:
  299. /* Local format inode forks report no extents. */
  300. goto out_unlock_ilock;
  301. default:
  302. error = -EINVAL;
  303. goto out_unlock_ilock;
  304. }
  305. if (bmv->bmv_length == -1) {
  306. max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
  307. bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
  308. }
  309. bmv_end = bmv->bmv_offset + bmv->bmv_length;
  310. first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
  311. len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
  312. error = xfs_iread_extents(NULL, ip, whichfork);
  313. if (error)
  314. goto out_unlock_ilock;
  315. if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
  316. /*
  317. * Report a whole-file hole if the delalloc flag is set to
  318. * stay compatible with the old implementation.
  319. */
  320. if (iflags & BMV_IF_DELALLOC)
  321. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  322. XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
  323. goto out_unlock_ilock;
  324. }
  325. while (!xfs_getbmap_full(bmv)) {
  326. xfs_trim_extent(&got, first_bno, len);
  327. /*
  328. * Report an entry for a hole if this extent doesn't directly
  329. * follow the previous one.
  330. */
  331. if (got.br_startoff > bno) {
  332. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  333. got.br_startoff);
  334. if (xfs_getbmap_full(bmv))
  335. break;
  336. }
  337. /*
  338. * In order to report shared extents accurately, we report each
  339. * distinct shared / unshared part of a single bmbt record with
  340. * an individual getbmapx record.
  341. */
  342. bno = got.br_startoff + got.br_blockcount;
  343. rec = got;
  344. do {
  345. error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
  346. &rec);
  347. if (error || xfs_getbmap_full(bmv))
  348. goto out_unlock_ilock;
  349. } while (xfs_getbmap_next_rec(&rec, bno));
  350. if (!xfs_iext_next_extent(ifp, &icur, &got)) {
  351. xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  352. if (bmv->bmv_entries > 0)
  353. out[bmv->bmv_entries - 1].bmv_oflags |=
  354. BMV_OF_LAST;
  355. if (whichfork != XFS_ATTR_FORK && bno < end &&
  356. !xfs_getbmap_full(bmv)) {
  357. xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
  358. bno, end);
  359. }
  360. break;
  361. }
  362. if (bno >= first_bno + len)
  363. break;
  364. }
  365. out_unlock_ilock:
  366. xfs_iunlock(ip, lock);
  367. out_unlock_iolock:
  368. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  369. return error;
  370. }
  371. /*
  372. * Dead simple method of punching delalyed allocation blocks from a range in
  373. * the inode. This will always punch out both the start and end blocks, even
  374. * if the ranges only partially overlap them, so it is up to the caller to
  375. * ensure that partial blocks are not passed in.
  376. */
  377. void
  378. xfs_bmap_punch_delalloc_range(
  379. struct xfs_inode *ip,
  380. int whichfork,
  381. xfs_off_t start_byte,
  382. xfs_off_t end_byte,
  383. struct xfs_zone_alloc_ctx *ac)
  384. {
  385. struct xfs_mount *mp = ip->i_mount;
  386. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  387. xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, start_byte);
  388. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, end_byte);
  389. struct xfs_bmbt_irec got, del;
  390. struct xfs_iext_cursor icur;
  391. ASSERT(!xfs_need_iread_extents(ifp));
  392. xfs_ilock(ip, XFS_ILOCK_EXCL);
  393. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  394. goto out_unlock;
  395. while (got.br_startoff + got.br_blockcount > start_fsb) {
  396. del = got;
  397. xfs_trim_extent(&del, start_fsb, end_fsb - start_fsb);
  398. /*
  399. * A delete can push the cursor forward. Step back to the
  400. * previous extent on non-delalloc or extents outside the
  401. * target range.
  402. */
  403. if (!del.br_blockcount ||
  404. !isnullstartblock(del.br_startblock)) {
  405. if (!xfs_iext_prev_extent(ifp, &icur, &got))
  406. break;
  407. continue;
  408. }
  409. if (xfs_is_zoned_inode(ip) && ac) {
  410. /*
  411. * In a zoned buffered write context we need to return
  412. * the punched delalloc allocations to the allocation
  413. * context. This allows reusing them in the following
  414. * iomap iterations.
  415. */
  416. xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got,
  417. &del, XFS_BMAPI_REMAP);
  418. ac->reserved_blocks += del.br_blockcount;
  419. } else {
  420. xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got,
  421. &del, 0);
  422. }
  423. if (!xfs_iext_get_extent(ifp, &icur, &got))
  424. break;
  425. }
  426. if (whichfork == XFS_COW_FORK && !ifp->if_bytes)
  427. xfs_inode_clear_cowblocks_tag(ip);
  428. out_unlock:
  429. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  430. }
  431. /*
  432. * Test whether it is appropriate to check an inode for and free post EOF
  433. * blocks.
  434. */
  435. bool
  436. xfs_can_free_eofblocks(
  437. struct xfs_inode *ip)
  438. {
  439. struct xfs_mount *mp = ip->i_mount;
  440. bool found_blocks = false;
  441. xfs_fileoff_t end_fsb;
  442. xfs_fileoff_t last_fsb;
  443. struct xfs_bmbt_irec imap;
  444. struct xfs_iext_cursor icur;
  445. /*
  446. * Caller must either hold the exclusive io lock; or be inactivating
  447. * the inode, which guarantees there are no other users of the inode.
  448. */
  449. if (!(inode_state_read_once(VFS_I(ip)) & I_FREEING))
  450. xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
  451. /* prealloc/delalloc exists only on regular files */
  452. if (!S_ISREG(VFS_I(ip)->i_mode))
  453. return false;
  454. /*
  455. * Zero sized files with no cached pages and delalloc blocks will not
  456. * have speculative prealloc/delalloc blocks to remove.
  457. */
  458. if (VFS_I(ip)->i_size == 0 &&
  459. VFS_I(ip)->i_mapping->nrpages == 0 &&
  460. ip->i_delayed_blks == 0)
  461. return false;
  462. /* If we haven't read in the extent list, then don't do it now. */
  463. if (xfs_need_iread_extents(&ip->i_df))
  464. return false;
  465. /*
  466. * Do not free real extents in preallocated files unless the file has
  467. * delalloc blocks and we are forced to remove them.
  468. */
  469. if ((ip->i_diflags & XFS_DIFLAG_PREALLOC) && !ip->i_delayed_blks)
  470. return false;
  471. /*
  472. * Do not try to free post-EOF blocks if EOF is beyond the end of the
  473. * range supported by the page cache, because the truncation will loop
  474. * forever.
  475. */
  476. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  477. if (xfs_inode_has_bigrtalloc(ip))
  478. end_fsb = xfs_fileoff_roundup_rtx(mp, end_fsb);
  479. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  480. if (last_fsb <= end_fsb)
  481. return false;
  482. /*
  483. * Check if there is an post-EOF extent to free. If there are any
  484. * delalloc blocks attached to the inode (data fork delalloc
  485. * reservations or CoW extents of any kind), we need to free them so
  486. * that inactivation doesn't fail to erase them.
  487. */
  488. xfs_ilock(ip, XFS_ILOCK_SHARED);
  489. if (ip->i_delayed_blks ||
  490. xfs_iext_lookup_extent(ip, &ip->i_df, end_fsb, &icur, &imap))
  491. found_blocks = true;
  492. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  493. return found_blocks;
  494. }
  495. /*
  496. * This is called to free any blocks beyond eof. The caller must hold
  497. * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
  498. * reference to the inode.
  499. */
  500. int
  501. xfs_free_eofblocks(
  502. struct xfs_inode *ip)
  503. {
  504. struct xfs_trans *tp;
  505. struct xfs_mount *mp = ip->i_mount;
  506. int error;
  507. /* Attach the dquots to the inode up front. */
  508. error = xfs_qm_dqattach(ip);
  509. if (error)
  510. return error;
  511. /* Wait on dio to ensure i_size has settled. */
  512. inode_dio_wait(VFS_I(ip));
  513. /*
  514. * For preallocated files only free delayed allocations.
  515. *
  516. * Note that this means we also leave speculative preallocations in
  517. * place for preallocated files.
  518. */
  519. if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) {
  520. if (ip->i_delayed_blks) {
  521. xfs_bmap_punch_delalloc_range(ip, XFS_DATA_FORK,
  522. round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize),
  523. LLONG_MAX, NULL);
  524. }
  525. xfs_inode_clear_eofblocks_tag(ip);
  526. return 0;
  527. }
  528. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  529. if (error) {
  530. ASSERT(xfs_is_shutdown(mp));
  531. return error;
  532. }
  533. xfs_ilock(ip, XFS_ILOCK_EXCL);
  534. xfs_trans_ijoin(tp, ip, 0);
  535. /*
  536. * Do not update the on-disk file size. If we update the on-disk file
  537. * size and then the system crashes before the contents of the file are
  538. * flushed to disk then the files may be full of holes (ie NULL files
  539. * bug).
  540. */
  541. error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
  542. XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
  543. if (error)
  544. goto err_cancel;
  545. error = xfs_trans_commit(tp);
  546. if (error)
  547. goto out_unlock;
  548. xfs_inode_clear_eofblocks_tag(ip);
  549. goto out_unlock;
  550. err_cancel:
  551. /*
  552. * If we get an error at this point we simply don't
  553. * bother truncating the file.
  554. */
  555. xfs_trans_cancel(tp);
  556. out_unlock:
  557. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  558. return error;
  559. }
  560. int
  561. xfs_alloc_file_space(
  562. struct xfs_inode *ip,
  563. xfs_off_t offset,
  564. xfs_off_t len)
  565. {
  566. xfs_mount_t *mp = ip->i_mount;
  567. xfs_off_t count;
  568. xfs_filblks_t allocatesize_fsb;
  569. xfs_extlen_t extsz, temp;
  570. xfs_fileoff_t startoffset_fsb;
  571. xfs_fileoff_t endoffset_fsb;
  572. int rt;
  573. xfs_trans_t *tp;
  574. xfs_bmbt_irec_t imaps[1], *imapp;
  575. int error;
  576. if (xfs_is_always_cow_inode(ip))
  577. return 0;
  578. trace_xfs_alloc_file_space(ip);
  579. if (xfs_is_shutdown(mp))
  580. return -EIO;
  581. error = xfs_qm_dqattach(ip);
  582. if (error)
  583. return error;
  584. if (len <= 0)
  585. return -EINVAL;
  586. rt = XFS_IS_REALTIME_INODE(ip);
  587. extsz = xfs_get_extsz_hint(ip);
  588. count = len;
  589. imapp = &imaps[0];
  590. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  591. endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
  592. allocatesize_fsb = endoffset_fsb - startoffset_fsb;
  593. /*
  594. * Allocate file space until done or until there is an error
  595. */
  596. while (allocatesize_fsb && !error) {
  597. xfs_fileoff_t s, e;
  598. unsigned int dblocks, rblocks, resblks;
  599. int nimaps = 1;
  600. /*
  601. * Determine space reservations for data/realtime.
  602. */
  603. if (unlikely(extsz)) {
  604. s = startoffset_fsb;
  605. do_div(s, extsz);
  606. s *= extsz;
  607. e = startoffset_fsb + allocatesize_fsb;
  608. div_u64_rem(startoffset_fsb, extsz, &temp);
  609. if (temp)
  610. e += temp;
  611. div_u64_rem(e, extsz, &temp);
  612. if (temp)
  613. e += extsz - temp;
  614. } else {
  615. s = 0;
  616. e = allocatesize_fsb;
  617. }
  618. /*
  619. * The transaction reservation is limited to a 32-bit block
  620. * count, hence we need to limit the number of blocks we are
  621. * trying to reserve to avoid an overflow. We can't allocate
  622. * more than @nimaps extents, and an extent is limited on disk
  623. * to XFS_BMBT_MAX_EXTLEN (21 bits), so use that to enforce the
  624. * limit.
  625. */
  626. resblks = min_t(xfs_fileoff_t, (e - s),
  627. (XFS_MAX_BMBT_EXTLEN * nimaps));
  628. if (unlikely(rt)) {
  629. dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  630. rblocks = resblks;
  631. } else {
  632. dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  633. rblocks = 0;
  634. }
  635. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
  636. dblocks, rblocks, false, &tp);
  637. if (error)
  638. break;
  639. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
  640. XFS_IEXT_ADD_NOSPLIT_CNT);
  641. if (error)
  642. goto error;
  643. /*
  644. * If the allocator cannot find a single free extent large
  645. * enough to cover the start block of the requested range,
  646. * xfs_bmapi_write will return -ENOSR.
  647. *
  648. * In that case we simply need to keep looping with the same
  649. * startoffset_fsb so that one of the following allocations
  650. * will eventually reach the requested range.
  651. */
  652. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  653. allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
  654. &nimaps);
  655. if (error) {
  656. if (error != -ENOSR)
  657. goto error;
  658. error = 0;
  659. } else {
  660. startoffset_fsb += imapp->br_blockcount;
  661. allocatesize_fsb -= imapp->br_blockcount;
  662. }
  663. ip->i_diflags |= XFS_DIFLAG_PREALLOC;
  664. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  665. error = xfs_trans_commit(tp);
  666. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  667. }
  668. return error;
  669. error:
  670. xfs_trans_cancel(tp);
  671. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  672. return error;
  673. }
  674. static int
  675. xfs_unmap_extent(
  676. struct xfs_inode *ip,
  677. xfs_fileoff_t startoffset_fsb,
  678. xfs_filblks_t len_fsb,
  679. int *done)
  680. {
  681. struct xfs_mount *mp = ip->i_mount;
  682. struct xfs_trans *tp;
  683. uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  684. int error;
  685. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
  686. false, &tp);
  687. if (error)
  688. return error;
  689. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
  690. XFS_IEXT_PUNCH_HOLE_CNT);
  691. if (error)
  692. goto out_trans_cancel;
  693. error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
  694. if (error)
  695. goto out_trans_cancel;
  696. error = xfs_trans_commit(tp);
  697. out_unlock:
  698. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  699. return error;
  700. out_trans_cancel:
  701. xfs_trans_cancel(tp);
  702. goto out_unlock;
  703. }
  704. /* Caller must first wait for the completion of any pending DIOs if required. */
  705. int
  706. xfs_flush_unmap_range(
  707. struct xfs_inode *ip,
  708. xfs_off_t offset,
  709. xfs_off_t len)
  710. {
  711. struct inode *inode = VFS_I(ip);
  712. xfs_off_t rounding, start, end;
  713. int error;
  714. /*
  715. * Make sure we extend the flush out to extent alignment
  716. * boundaries so any extent range overlapping the start/end
  717. * of the modification we are about to do is clean and idle.
  718. */
  719. rounding = max_t(xfs_off_t, xfs_inode_alloc_unitsize(ip), PAGE_SIZE);
  720. start = rounddown_64(offset, rounding);
  721. end = roundup_64(offset + len, rounding) - 1;
  722. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  723. if (error)
  724. return error;
  725. truncate_pagecache_range(inode, start, end);
  726. return 0;
  727. }
  728. int
  729. xfs_free_file_space(
  730. struct xfs_inode *ip,
  731. xfs_off_t offset,
  732. xfs_off_t len,
  733. struct xfs_zone_alloc_ctx *ac)
  734. {
  735. struct xfs_mount *mp = ip->i_mount;
  736. xfs_fileoff_t startoffset_fsb;
  737. xfs_fileoff_t endoffset_fsb;
  738. int done = 0, error;
  739. trace_xfs_free_file_space(ip);
  740. error = xfs_qm_dqattach(ip);
  741. if (error)
  742. return error;
  743. if (len <= 0) /* if nothing being freed */
  744. return 0;
  745. /*
  746. * Now AIO and DIO has drained we flush and (if necessary) invalidate
  747. * the cached range over the first operation we are about to run.
  748. */
  749. error = xfs_flush_unmap_range(ip, offset, len);
  750. if (error)
  751. return error;
  752. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  753. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  754. /* We can only free complete realtime extents. */
  755. if (xfs_inode_has_bigrtalloc(ip)) {
  756. startoffset_fsb = xfs_fileoff_roundup_rtx(mp, startoffset_fsb);
  757. endoffset_fsb = xfs_fileoff_rounddown_rtx(mp, endoffset_fsb);
  758. }
  759. /*
  760. * Need to zero the stuff we're not freeing, on disk.
  761. */
  762. if (endoffset_fsb > startoffset_fsb) {
  763. while (!done) {
  764. error = xfs_unmap_extent(ip, startoffset_fsb,
  765. endoffset_fsb - startoffset_fsb, &done);
  766. if (error)
  767. return error;
  768. }
  769. }
  770. /*
  771. * Now that we've unmap all full blocks we'll have to zero out any
  772. * partial block at the beginning and/or end. xfs_zero_range is smart
  773. * enough to skip any holes, including those we just created, but we
  774. * must take care not to zero beyond EOF and enlarge i_size.
  775. */
  776. if (offset >= XFS_ISIZE(ip))
  777. return 0;
  778. if (offset + len > XFS_ISIZE(ip))
  779. len = XFS_ISIZE(ip) - offset;
  780. error = xfs_zero_range(ip, offset, len, ac, NULL);
  781. if (error)
  782. return error;
  783. /*
  784. * If we zeroed right up to EOF and EOF straddles a page boundary we
  785. * must make sure that the post-EOF area is also zeroed because the
  786. * page could be mmap'd and xfs_zero_range doesn't do that for us.
  787. * Writeback of the eof page will do this, albeit clumsily.
  788. */
  789. if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
  790. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  791. round_down(offset + len, PAGE_SIZE), LLONG_MAX);
  792. }
  793. return error;
  794. }
  795. static int
  796. xfs_prepare_shift(
  797. struct xfs_inode *ip,
  798. loff_t offset)
  799. {
  800. unsigned int rounding;
  801. int error;
  802. /*
  803. * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
  804. * into the accessible region of the file.
  805. */
  806. if (xfs_can_free_eofblocks(ip)) {
  807. error = xfs_free_eofblocks(ip);
  808. if (error)
  809. return error;
  810. }
  811. /*
  812. * Shift operations must stabilize the start block offset boundary along
  813. * with the full range of the operation. If we don't, a COW writeback
  814. * completion could race with an insert, front merge with the start
  815. * extent (after split) during the shift and corrupt the file. Start
  816. * with the allocation unit just prior to the start to stabilize the
  817. * boundary.
  818. */
  819. rounding = xfs_inode_alloc_unitsize(ip);
  820. offset = rounddown_64(offset, rounding);
  821. if (offset)
  822. offset -= rounding;
  823. /*
  824. * Writeback and invalidate cache for the remainder of the file as we're
  825. * about to shift down every extent from offset to EOF.
  826. */
  827. error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
  828. if (error)
  829. return error;
  830. /*
  831. * Clean out anything hanging around in the cow fork now that
  832. * we've flushed all the dirty data out to disk to avoid having
  833. * CoW extents at the wrong offsets.
  834. */
  835. if (xfs_inode_has_cow_data(ip)) {
  836. error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
  837. true);
  838. if (error)
  839. return error;
  840. }
  841. return 0;
  842. }
  843. /*
  844. * xfs_collapse_file_space()
  845. * This routine frees disk space and shift extent for the given file.
  846. * The first thing we do is to free data blocks in the specified range
  847. * by calling xfs_free_file_space(). It would also sync dirty data
  848. * and invalidate page cache over the region on which collapse range
  849. * is working. And Shift extent records to the left to cover a hole.
  850. * RETURNS:
  851. * 0 on success
  852. * errno on error
  853. *
  854. */
  855. int
  856. xfs_collapse_file_space(
  857. struct xfs_inode *ip,
  858. xfs_off_t offset,
  859. xfs_off_t len,
  860. struct xfs_zone_alloc_ctx *ac)
  861. {
  862. struct xfs_mount *mp = ip->i_mount;
  863. struct xfs_trans *tp;
  864. int error;
  865. xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len);
  866. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  867. bool done = false;
  868. xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
  869. trace_xfs_collapse_file_space(ip);
  870. error = xfs_free_file_space(ip, offset, len, ac);
  871. if (error)
  872. return error;
  873. error = xfs_prepare_shift(ip, offset);
  874. if (error)
  875. return error;
  876. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  877. if (error)
  878. return error;
  879. xfs_ilock(ip, XFS_ILOCK_EXCL);
  880. xfs_trans_ijoin(tp, ip, 0);
  881. while (!done) {
  882. error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
  883. &done);
  884. if (error)
  885. goto out_trans_cancel;
  886. if (done)
  887. break;
  888. /* finish any deferred frees and roll the transaction */
  889. error = xfs_defer_finish(&tp);
  890. if (error)
  891. goto out_trans_cancel;
  892. }
  893. error = xfs_trans_commit(tp);
  894. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  895. return error;
  896. out_trans_cancel:
  897. xfs_trans_cancel(tp);
  898. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  899. return error;
  900. }
  901. /*
  902. * xfs_insert_file_space()
  903. * This routine create hole space by shifting extents for the given file.
  904. * The first thing we do is to sync dirty data and invalidate page cache
  905. * over the region on which insert range is working. And split an extent
  906. * to two extents at given offset by calling xfs_bmap_split_extent.
  907. * And shift all extent records which are laying between [offset,
  908. * last allocated extent] to the right to reserve hole range.
  909. * RETURNS:
  910. * 0 on success
  911. * errno on error
  912. */
  913. int
  914. xfs_insert_file_space(
  915. struct xfs_inode *ip,
  916. loff_t offset,
  917. loff_t len)
  918. {
  919. struct xfs_mount *mp = ip->i_mount;
  920. struct xfs_trans *tp;
  921. int error;
  922. xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset);
  923. xfs_fileoff_t next_fsb = NULLFSBLOCK;
  924. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  925. bool done = false;
  926. xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
  927. trace_xfs_insert_file_space(ip);
  928. error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
  929. if (error)
  930. return error;
  931. error = xfs_prepare_shift(ip, offset);
  932. if (error)
  933. return error;
  934. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
  935. XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
  936. if (error)
  937. return error;
  938. xfs_ilock(ip, XFS_ILOCK_EXCL);
  939. xfs_trans_ijoin(tp, ip, 0);
  940. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
  941. XFS_IEXT_PUNCH_HOLE_CNT);
  942. if (error)
  943. goto out_trans_cancel;
  944. /*
  945. * The extent shifting code works on extent granularity. So, if stop_fsb
  946. * is not the starting block of extent, we need to split the extent at
  947. * stop_fsb.
  948. */
  949. error = xfs_bmap_split_extent(tp, ip, stop_fsb);
  950. if (error)
  951. goto out_trans_cancel;
  952. do {
  953. error = xfs_defer_finish(&tp);
  954. if (error)
  955. goto out_trans_cancel;
  956. error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
  957. &done, stop_fsb);
  958. if (error)
  959. goto out_trans_cancel;
  960. } while (!done);
  961. error = xfs_trans_commit(tp);
  962. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  963. return error;
  964. out_trans_cancel:
  965. xfs_trans_cancel(tp);
  966. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  967. return error;
  968. }
  969. /*
  970. * We need to check that the format of the data fork in the temporary inode is
  971. * valid for the target inode before doing the swap. This is not a problem with
  972. * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
  973. * data fork depending on the space the attribute fork is taking so we can get
  974. * invalid formats on the target inode.
  975. *
  976. * E.g. target has space for 7 extents in extent format, temp inode only has
  977. * space for 6. If we defragment down to 7 extents, then the tmp format is a
  978. * btree, but when swapped it needs to be in extent format. Hence we can't just
  979. * blindly swap data forks on attr2 filesystems.
  980. *
  981. * Note that we check the swap in both directions so that we don't end up with
  982. * a corrupt temporary inode, either.
  983. *
  984. * Note that fixing the way xfs_fsr sets up the attribute fork in the source
  985. * inode will prevent this situation from occurring, so all we do here is
  986. * reject and log the attempt. basically we are putting the responsibility on
  987. * userspace to get this right.
  988. */
  989. static int
  990. xfs_swap_extents_check_format(
  991. struct xfs_inode *ip, /* target inode */
  992. struct xfs_inode *tip) /* tmp inode */
  993. {
  994. struct xfs_ifork *ifp = &ip->i_df;
  995. struct xfs_ifork *tifp = &tip->i_df;
  996. /* User/group/project quota ids must match if quotas are enforced. */
  997. if (XFS_IS_QUOTA_ON(ip->i_mount) &&
  998. (!uid_eq(VFS_I(ip)->i_uid, VFS_I(tip)->i_uid) ||
  999. !gid_eq(VFS_I(ip)->i_gid, VFS_I(tip)->i_gid) ||
  1000. ip->i_projid != tip->i_projid))
  1001. return -EINVAL;
  1002. /* Should never get a local format */
  1003. if (ifp->if_format == XFS_DINODE_FMT_LOCAL ||
  1004. tifp->if_format == XFS_DINODE_FMT_LOCAL)
  1005. return -EINVAL;
  1006. /*
  1007. * if the target inode has less extents that then temporary inode then
  1008. * why did userspace call us?
  1009. */
  1010. if (ifp->if_nextents < tifp->if_nextents)
  1011. return -EINVAL;
  1012. /*
  1013. * If we have to use the (expensive) rmap swap method, we can
  1014. * handle any number of extents and any format.
  1015. */
  1016. if (xfs_has_rmapbt(ip->i_mount))
  1017. return 0;
  1018. /*
  1019. * if the target inode is in extent form and the temp inode is in btree
  1020. * form then we will end up with the target inode in the wrong format
  1021. * as we already know there are less extents in the temp inode.
  1022. */
  1023. if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
  1024. tifp->if_format == XFS_DINODE_FMT_BTREE)
  1025. return -EINVAL;
  1026. /* Check temp in extent form to max in target */
  1027. if (tifp->if_format == XFS_DINODE_FMT_EXTENTS &&
  1028. tifp->if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1029. return -EINVAL;
  1030. /* Check target in extent form to max in temp */
  1031. if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
  1032. ifp->if_nextents > XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1033. return -EINVAL;
  1034. /*
  1035. * If we are in a btree format, check that the temp root block will fit
  1036. * in the target and that it has enough extents to be in btree format
  1037. * in the target.
  1038. *
  1039. * Note that we have to be careful to allow btree->extent conversions
  1040. * (a common defrag case) which will occur when the temp inode is in
  1041. * extent format...
  1042. */
  1043. if (tifp->if_format == XFS_DINODE_FMT_BTREE) {
  1044. if (xfs_inode_has_attr_fork(ip) &&
  1045. xfs_bmap_bmdr_space(tifp->if_broot) > xfs_inode_fork_boff(ip))
  1046. return -EINVAL;
  1047. if (tifp->if_nextents <= XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1048. return -EINVAL;
  1049. }
  1050. /* Reciprocal target->temp btree format checks */
  1051. if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
  1052. if (xfs_inode_has_attr_fork(tip) &&
  1053. xfs_bmap_bmdr_space(ip->i_df.if_broot) > xfs_inode_fork_boff(tip))
  1054. return -EINVAL;
  1055. if (ifp->if_nextents <= XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1056. return -EINVAL;
  1057. }
  1058. return 0;
  1059. }
  1060. static int
  1061. xfs_swap_extent_flush(
  1062. struct xfs_inode *ip)
  1063. {
  1064. int error;
  1065. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1066. if (error)
  1067. return error;
  1068. truncate_pagecache_range(VFS_I(ip), 0, -1);
  1069. /* Verify O_DIRECT for ftmp */
  1070. if (VFS_I(ip)->i_mapping->nrpages)
  1071. return -EINVAL;
  1072. return 0;
  1073. }
  1074. /*
  1075. * Move extents from one file to another, when rmap is enabled.
  1076. */
  1077. STATIC int
  1078. xfs_swap_extent_rmap(
  1079. struct xfs_trans **tpp,
  1080. struct xfs_inode *ip,
  1081. struct xfs_inode *tip)
  1082. {
  1083. struct xfs_trans *tp = *tpp;
  1084. struct xfs_bmbt_irec irec;
  1085. struct xfs_bmbt_irec uirec;
  1086. struct xfs_bmbt_irec tirec;
  1087. xfs_fileoff_t offset_fsb;
  1088. xfs_fileoff_t end_fsb;
  1089. xfs_filblks_t count_fsb;
  1090. int error;
  1091. xfs_filblks_t ilen;
  1092. xfs_filblks_t rlen;
  1093. int nimaps;
  1094. uint64_t tip_flags2;
  1095. /*
  1096. * If the source file has shared blocks, we must flag the donor
  1097. * file as having shared blocks so that we get the shared-block
  1098. * rmap functions when we go to fix up the rmaps. The flags
  1099. * will be switch for reals later.
  1100. */
  1101. tip_flags2 = tip->i_diflags2;
  1102. if (ip->i_diflags2 & XFS_DIFLAG2_REFLINK)
  1103. tip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
  1104. offset_fsb = 0;
  1105. end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
  1106. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  1107. while (count_fsb) {
  1108. /* Read extent from the donor file */
  1109. nimaps = 1;
  1110. error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
  1111. &nimaps, 0);
  1112. if (error)
  1113. goto out;
  1114. ASSERT(nimaps == 1);
  1115. ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
  1116. trace_xfs_swap_extent_rmap_remap(tip, &tirec);
  1117. ilen = tirec.br_blockcount;
  1118. /* Unmap the old blocks in the source file. */
  1119. while (tirec.br_blockcount) {
  1120. ASSERT(tp->t_highest_agno == NULLAGNUMBER);
  1121. trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
  1122. /* Read extent from the source file */
  1123. nimaps = 1;
  1124. error = xfs_bmapi_read(ip, tirec.br_startoff,
  1125. tirec.br_blockcount, &irec,
  1126. &nimaps, 0);
  1127. if (error)
  1128. goto out;
  1129. ASSERT(nimaps == 1);
  1130. ASSERT(tirec.br_startoff == irec.br_startoff);
  1131. trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
  1132. /* Trim the extent. */
  1133. uirec = tirec;
  1134. uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
  1135. tirec.br_blockcount,
  1136. irec.br_blockcount);
  1137. trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
  1138. if (xfs_bmap_is_real_extent(&uirec)) {
  1139. error = xfs_iext_count_extend(tp, ip,
  1140. XFS_DATA_FORK,
  1141. XFS_IEXT_SWAP_RMAP_CNT);
  1142. if (error)
  1143. goto out;
  1144. }
  1145. if (xfs_bmap_is_real_extent(&irec)) {
  1146. error = xfs_iext_count_extend(tp, tip,
  1147. XFS_DATA_FORK,
  1148. XFS_IEXT_SWAP_RMAP_CNT);
  1149. if (error)
  1150. goto out;
  1151. }
  1152. /* Remove the mapping from the donor file. */
  1153. xfs_bmap_unmap_extent(tp, tip, XFS_DATA_FORK, &uirec);
  1154. /* Remove the mapping from the source file. */
  1155. xfs_bmap_unmap_extent(tp, ip, XFS_DATA_FORK, &irec);
  1156. /* Map the donor file's blocks into the source file. */
  1157. xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, &uirec);
  1158. /* Map the source file's blocks into the donor file. */
  1159. xfs_bmap_map_extent(tp, tip, XFS_DATA_FORK, &irec);
  1160. error = xfs_defer_finish(tpp);
  1161. tp = *tpp;
  1162. if (error)
  1163. goto out;
  1164. tirec.br_startoff += rlen;
  1165. if (tirec.br_startblock != HOLESTARTBLOCK &&
  1166. tirec.br_startblock != DELAYSTARTBLOCK)
  1167. tirec.br_startblock += rlen;
  1168. tirec.br_blockcount -= rlen;
  1169. }
  1170. /* Roll on... */
  1171. count_fsb -= ilen;
  1172. offset_fsb += ilen;
  1173. }
  1174. tip->i_diflags2 = tip_flags2;
  1175. return 0;
  1176. out:
  1177. trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
  1178. tip->i_diflags2 = tip_flags2;
  1179. return error;
  1180. }
  1181. /* Swap the extents of two files by swapping data forks. */
  1182. STATIC int
  1183. xfs_swap_extent_forks(
  1184. struct xfs_trans *tp,
  1185. struct xfs_inode *ip,
  1186. struct xfs_inode *tip,
  1187. int *src_log_flags,
  1188. int *target_log_flags)
  1189. {
  1190. xfs_filblks_t aforkblks = 0;
  1191. xfs_filblks_t taforkblks = 0;
  1192. xfs_extnum_t junk;
  1193. uint64_t tmp;
  1194. int error;
  1195. /*
  1196. * Count the number of extended attribute blocks
  1197. */
  1198. if (xfs_inode_has_attr_fork(ip) && ip->i_af.if_nextents > 0 &&
  1199. ip->i_af.if_format != XFS_DINODE_FMT_LOCAL) {
  1200. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
  1201. &aforkblks);
  1202. if (error)
  1203. return error;
  1204. }
  1205. if (xfs_inode_has_attr_fork(tip) && tip->i_af.if_nextents > 0 &&
  1206. tip->i_af.if_format != XFS_DINODE_FMT_LOCAL) {
  1207. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
  1208. &taforkblks);
  1209. if (error)
  1210. return error;
  1211. }
  1212. /*
  1213. * Btree format (v3) inodes have the inode number stamped in the bmbt
  1214. * block headers. We can't start changing the bmbt blocks until the
  1215. * inode owner change is logged so recovery does the right thing in the
  1216. * event of a crash. Set the owner change log flags now and leave the
  1217. * bmbt scan as the last step.
  1218. */
  1219. if (xfs_has_v3inodes(ip->i_mount)) {
  1220. if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE)
  1221. (*target_log_flags) |= XFS_ILOG_DOWNER;
  1222. if (tip->i_df.if_format == XFS_DINODE_FMT_BTREE)
  1223. (*src_log_flags) |= XFS_ILOG_DOWNER;
  1224. }
  1225. /*
  1226. * Swap the data forks of the inodes
  1227. */
  1228. swap(ip->i_df, tip->i_df);
  1229. /*
  1230. * Fix the on-disk inode values
  1231. */
  1232. tmp = (uint64_t)ip->i_nblocks;
  1233. ip->i_nblocks = tip->i_nblocks - taforkblks + aforkblks;
  1234. tip->i_nblocks = tmp + taforkblks - aforkblks;
  1235. /*
  1236. * The extents in the source inode could still contain speculative
  1237. * preallocation beyond EOF (e.g. the file is open but not modified
  1238. * while defrag is in progress). In that case, we need to copy over the
  1239. * number of delalloc blocks the data fork in the source inode is
  1240. * tracking beyond EOF so that when the fork is truncated away when the
  1241. * temporary inode is unlinked we don't underrun the i_delayed_blks
  1242. * counter on that inode.
  1243. */
  1244. ASSERT(tip->i_delayed_blks == 0);
  1245. tip->i_delayed_blks = ip->i_delayed_blks;
  1246. ip->i_delayed_blks = 0;
  1247. switch (ip->i_df.if_format) {
  1248. case XFS_DINODE_FMT_EXTENTS:
  1249. (*src_log_flags) |= XFS_ILOG_DEXT;
  1250. break;
  1251. case XFS_DINODE_FMT_BTREE:
  1252. ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
  1253. (*src_log_flags & XFS_ILOG_DOWNER));
  1254. (*src_log_flags) |= XFS_ILOG_DBROOT;
  1255. break;
  1256. }
  1257. switch (tip->i_df.if_format) {
  1258. case XFS_DINODE_FMT_EXTENTS:
  1259. (*target_log_flags) |= XFS_ILOG_DEXT;
  1260. break;
  1261. case XFS_DINODE_FMT_BTREE:
  1262. (*target_log_flags) |= XFS_ILOG_DBROOT;
  1263. ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
  1264. (*target_log_flags & XFS_ILOG_DOWNER));
  1265. break;
  1266. }
  1267. return 0;
  1268. }
  1269. /*
  1270. * Fix up the owners of the bmbt blocks to refer to the current inode. The
  1271. * change owner scan attempts to order all modified buffers in the current
  1272. * transaction. In the event of ordered buffer failure, the offending buffer is
  1273. * physically logged as a fallback and the scan returns -EAGAIN. We must roll
  1274. * the transaction in this case to replenish the fallback log reservation and
  1275. * restart the scan. This process repeats until the scan completes.
  1276. */
  1277. static int
  1278. xfs_swap_change_owner(
  1279. struct xfs_trans **tpp,
  1280. struct xfs_inode *ip,
  1281. struct xfs_inode *tmpip)
  1282. {
  1283. int error;
  1284. struct xfs_trans *tp = *tpp;
  1285. do {
  1286. error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
  1287. NULL);
  1288. /* success or fatal error */
  1289. if (error != -EAGAIN)
  1290. break;
  1291. error = xfs_trans_roll(tpp);
  1292. if (error)
  1293. break;
  1294. tp = *tpp;
  1295. /*
  1296. * Redirty both inodes so they can relog and keep the log tail
  1297. * moving forward.
  1298. */
  1299. xfs_trans_ijoin(tp, ip, 0);
  1300. xfs_trans_ijoin(tp, tmpip, 0);
  1301. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1302. xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
  1303. } while (true);
  1304. return error;
  1305. }
  1306. int
  1307. xfs_swap_extents(
  1308. struct xfs_inode *ip, /* target inode */
  1309. struct xfs_inode *tip, /* tmp inode */
  1310. struct xfs_swapext *sxp)
  1311. {
  1312. struct xfs_mount *mp = ip->i_mount;
  1313. struct xfs_trans *tp;
  1314. struct xfs_bstat *sbp = &sxp->sx_stat;
  1315. int src_log_flags, target_log_flags;
  1316. int error = 0;
  1317. uint64_t f;
  1318. int resblks = 0;
  1319. unsigned int flags = 0;
  1320. struct timespec64 ctime, mtime;
  1321. /*
  1322. * Lock the inodes against other IO, page faults and truncate to
  1323. * begin with. Then we can ensure the inodes are flushed and have no
  1324. * page cache safely. Once we have done this we can take the ilocks and
  1325. * do the rest of the checks.
  1326. */
  1327. lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1328. filemap_invalidate_lock_two(VFS_I(ip)->i_mapping,
  1329. VFS_I(tip)->i_mapping);
  1330. /* Verify that both files have the same format */
  1331. if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
  1332. error = -EINVAL;
  1333. goto out_unlock;
  1334. }
  1335. /* Verify both files are either real-time or non-realtime */
  1336. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  1337. error = -EINVAL;
  1338. goto out_unlock;
  1339. }
  1340. /*
  1341. * The rmapbt implementation is unable to resume a swapext operation
  1342. * after a crash if the allocation unit size is larger than a block.
  1343. * This (deprecated) interface will not be upgraded to handle this
  1344. * situation. Defragmentation must be performed with the commit range
  1345. * ioctl.
  1346. */
  1347. if (XFS_IS_REALTIME_INODE(ip) && xfs_has_rtgroups(ip->i_mount)) {
  1348. error = -EOPNOTSUPP;
  1349. goto out_unlock;
  1350. }
  1351. error = xfs_qm_dqattach(ip);
  1352. if (error)
  1353. goto out_unlock;
  1354. error = xfs_qm_dqattach(tip);
  1355. if (error)
  1356. goto out_unlock;
  1357. error = xfs_swap_extent_flush(ip);
  1358. if (error)
  1359. goto out_unlock;
  1360. error = xfs_swap_extent_flush(tip);
  1361. if (error)
  1362. goto out_unlock;
  1363. if (xfs_inode_has_cow_data(tip)) {
  1364. error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true);
  1365. if (error)
  1366. goto out_unlock;
  1367. }
  1368. /*
  1369. * Extent "swapping" with rmap requires a permanent reservation and
  1370. * a block reservation because it's really just a remap operation
  1371. * performed with log redo items!
  1372. */
  1373. if (xfs_has_rmapbt(mp)) {
  1374. int w = XFS_DATA_FORK;
  1375. uint32_t ipnext = ip->i_df.if_nextents;
  1376. uint32_t tipnext = tip->i_df.if_nextents;
  1377. /*
  1378. * Conceptually this shouldn't affect the shape of either bmbt,
  1379. * but since we atomically move extents one by one, we reserve
  1380. * enough space to rebuild both trees.
  1381. */
  1382. resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w);
  1383. resblks += XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w);
  1384. /*
  1385. * If either inode straddles a bmapbt block allocation boundary,
  1386. * the rmapbt algorithm triggers repeated allocs and frees as
  1387. * extents are remapped. This can exhaust the block reservation
  1388. * prematurely and cause shutdown. Return freed blocks to the
  1389. * transaction reservation to counter this behavior.
  1390. */
  1391. flags |= XFS_TRANS_RES_FDBLKS;
  1392. }
  1393. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, flags,
  1394. &tp);
  1395. if (error)
  1396. goto out_unlock;
  1397. /*
  1398. * Lock and join the inodes to the tansaction so that transaction commit
  1399. * or cancel will unlock the inodes from this point onwards.
  1400. */
  1401. xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL);
  1402. xfs_trans_ijoin(tp, ip, 0);
  1403. xfs_trans_ijoin(tp, tip, 0);
  1404. /* Verify all data are being swapped */
  1405. if (sxp->sx_offset != 0 ||
  1406. sxp->sx_length != ip->i_disk_size ||
  1407. sxp->sx_length != tip->i_disk_size) {
  1408. error = -EFAULT;
  1409. goto out_trans_cancel;
  1410. }
  1411. trace_xfs_swap_extent_before(ip, 0);
  1412. trace_xfs_swap_extent_before(tip, 1);
  1413. /* check inode formats now that data is flushed */
  1414. error = xfs_swap_extents_check_format(ip, tip);
  1415. if (error) {
  1416. xfs_notice(mp,
  1417. "%s: inode 0x%llx format is incompatible for exchanging.",
  1418. __func__, ip->i_ino);
  1419. goto out_trans_cancel;
  1420. }
  1421. /*
  1422. * Compare the current change & modify times with that
  1423. * passed in. If they differ, we abort this swap.
  1424. * This is the mechanism used to ensure the calling
  1425. * process that the file was not changed out from
  1426. * under it.
  1427. */
  1428. ctime = inode_get_ctime(VFS_I(ip));
  1429. mtime = inode_get_mtime(VFS_I(ip));
  1430. if ((sbp->bs_ctime.tv_sec != ctime.tv_sec) ||
  1431. (sbp->bs_ctime.tv_nsec != ctime.tv_nsec) ||
  1432. (sbp->bs_mtime.tv_sec != mtime.tv_sec) ||
  1433. (sbp->bs_mtime.tv_nsec != mtime.tv_nsec)) {
  1434. error = -EBUSY;
  1435. goto out_trans_cancel;
  1436. }
  1437. /*
  1438. * Note the trickiness in setting the log flags - we set the owner log
  1439. * flag on the opposite inode (i.e. the inode we are setting the new
  1440. * owner to be) because once we swap the forks and log that, log
  1441. * recovery is going to see the fork as owned by the swapped inode,
  1442. * not the pre-swapped inodes.
  1443. */
  1444. src_log_flags = XFS_ILOG_CORE;
  1445. target_log_flags = XFS_ILOG_CORE;
  1446. if (xfs_has_rmapbt(mp))
  1447. error = xfs_swap_extent_rmap(&tp, ip, tip);
  1448. else
  1449. error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
  1450. &target_log_flags);
  1451. if (error)
  1452. goto out_trans_cancel;
  1453. /* Do we have to swap reflink flags? */
  1454. if ((ip->i_diflags2 & XFS_DIFLAG2_REFLINK) ^
  1455. (tip->i_diflags2 & XFS_DIFLAG2_REFLINK)) {
  1456. f = ip->i_diflags2 & XFS_DIFLAG2_REFLINK;
  1457. ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  1458. ip->i_diflags2 |= tip->i_diflags2 & XFS_DIFLAG2_REFLINK;
  1459. tip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  1460. tip->i_diflags2 |= f & XFS_DIFLAG2_REFLINK;
  1461. }
  1462. /* Swap the cow forks. */
  1463. if (xfs_has_reflink(mp)) {
  1464. ASSERT(!ip->i_cowfp ||
  1465. ip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
  1466. ASSERT(!tip->i_cowfp ||
  1467. tip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
  1468. swap(ip->i_cowfp, tip->i_cowfp);
  1469. if (ip->i_cowfp && ip->i_cowfp->if_bytes)
  1470. xfs_inode_set_cowblocks_tag(ip);
  1471. else
  1472. xfs_inode_clear_cowblocks_tag(ip);
  1473. if (tip->i_cowfp && tip->i_cowfp->if_bytes)
  1474. xfs_inode_set_cowblocks_tag(tip);
  1475. else
  1476. xfs_inode_clear_cowblocks_tag(tip);
  1477. }
  1478. xfs_trans_log_inode(tp, ip, src_log_flags);
  1479. xfs_trans_log_inode(tp, tip, target_log_flags);
  1480. /*
  1481. * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
  1482. * have inode number owner values in the bmbt blocks that still refer to
  1483. * the old inode. Scan each bmbt to fix up the owner values with the
  1484. * inode number of the current inode.
  1485. */
  1486. if (src_log_flags & XFS_ILOG_DOWNER) {
  1487. error = xfs_swap_change_owner(&tp, ip, tip);
  1488. if (error)
  1489. goto out_trans_cancel;
  1490. }
  1491. if (target_log_flags & XFS_ILOG_DOWNER) {
  1492. error = xfs_swap_change_owner(&tp, tip, ip);
  1493. if (error)
  1494. goto out_trans_cancel;
  1495. }
  1496. /*
  1497. * If this is a synchronous mount, make sure that the
  1498. * transaction goes to disk before returning to the user.
  1499. */
  1500. if (xfs_has_wsync(mp))
  1501. xfs_trans_set_sync(tp);
  1502. error = xfs_trans_commit(tp);
  1503. trace_xfs_swap_extent_after(ip, 0);
  1504. trace_xfs_swap_extent_after(tip, 1);
  1505. out_unlock_ilock:
  1506. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1507. xfs_iunlock(tip, XFS_ILOCK_EXCL);
  1508. out_unlock:
  1509. filemap_invalidate_unlock_two(VFS_I(ip)->i_mapping,
  1510. VFS_I(tip)->i_mapping);
  1511. unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1512. return error;
  1513. out_trans_cancel:
  1514. xfs_trans_cancel(tp);
  1515. goto out_unlock_ilock;
  1516. }