xfs_bmap_util.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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.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_da_format.h"
  16. #include "xfs_defer.h"
  17. #include "xfs_inode.h"
  18. #include "xfs_btree.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_extfree_item.h"
  21. #include "xfs_alloc.h"
  22. #include "xfs_bmap.h"
  23. #include "xfs_bmap_util.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_rtalloc.h"
  26. #include "xfs_error.h"
  27. #include "xfs_quota.h"
  28. #include "xfs_trans_space.h"
  29. #include "xfs_trace.h"
  30. #include "xfs_icache.h"
  31. #include "xfs_log.h"
  32. #include "xfs_rmap_btree.h"
  33. #include "xfs_iomap.h"
  34. #include "xfs_reflink.h"
  35. #include "xfs_refcount.h"
  36. /* Kernel only BMAP related definitions and functions */
  37. /*
  38. * Convert the given file system block to a disk block. We have to treat it
  39. * differently based on whether the file is a real time file or not, because the
  40. * bmap code does.
  41. */
  42. xfs_daddr_t
  43. xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
  44. {
  45. return (XFS_IS_REALTIME_INODE(ip) ? \
  46. (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
  47. XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
  48. }
  49. /*
  50. * Routine to zero an extent on disk allocated to the specific inode.
  51. *
  52. * The VFS functions take a linearised filesystem block offset, so we have to
  53. * convert the sparse xfs fsb to the right format first.
  54. * VFS types are real funky, too.
  55. */
  56. int
  57. xfs_zero_extent(
  58. struct xfs_inode *ip,
  59. xfs_fsblock_t start_fsb,
  60. xfs_off_t count_fsb)
  61. {
  62. struct xfs_mount *mp = ip->i_mount;
  63. xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
  64. sector_t block = XFS_BB_TO_FSBT(mp, sector);
  65. return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip)),
  66. block << (mp->m_super->s_blocksize_bits - 9),
  67. count_fsb << (mp->m_super->s_blocksize_bits - 9),
  68. GFP_NOFS, 0);
  69. }
  70. #ifdef CONFIG_XFS_RT
  71. int
  72. xfs_bmap_rtalloc(
  73. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  74. {
  75. int error; /* error return value */
  76. xfs_mount_t *mp; /* mount point structure */
  77. xfs_extlen_t prod = 0; /* product factor for allocators */
  78. xfs_extlen_t mod = 0; /* product factor for allocators */
  79. xfs_extlen_t ralen = 0; /* realtime allocation length */
  80. xfs_extlen_t align; /* minimum allocation alignment */
  81. xfs_rtblock_t rtb;
  82. mp = ap->ip->i_mount;
  83. align = xfs_get_extsz_hint(ap->ip);
  84. prod = align / mp->m_sb.sb_rextsize;
  85. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  86. align, 1, ap->eof, 0,
  87. ap->conv, &ap->offset, &ap->length);
  88. if (error)
  89. return error;
  90. ASSERT(ap->length);
  91. ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
  92. /*
  93. * If the offset & length are not perfectly aligned
  94. * then kill prod, it will just get us in trouble.
  95. */
  96. div_u64_rem(ap->offset, align, &mod);
  97. if (mod || ap->length % align)
  98. prod = 1;
  99. /*
  100. * Set ralen to be the actual requested length in rtextents.
  101. */
  102. ralen = ap->length / mp->m_sb.sb_rextsize;
  103. /*
  104. * If the old value was close enough to MAXEXTLEN that
  105. * we rounded up to it, cut it back so it's valid again.
  106. * Note that if it's a really large request (bigger than
  107. * MAXEXTLEN), we don't hear about that number, and can't
  108. * adjust the starting point to match it.
  109. */
  110. if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
  111. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  112. /*
  113. * Lock out modifications to both the RT bitmap and summary inodes
  114. */
  115. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
  116. xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  117. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
  118. xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  119. /*
  120. * If it's an allocation to an empty file at offset 0,
  121. * pick an extent that will space things out in the rt area.
  122. */
  123. if (ap->eof && ap->offset == 0) {
  124. xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
  125. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  126. if (error)
  127. return error;
  128. ap->blkno = rtx * mp->m_sb.sb_rextsize;
  129. } else {
  130. ap->blkno = 0;
  131. }
  132. xfs_bmap_adjacent(ap);
  133. /*
  134. * Realtime allocation, done through xfs_rtallocate_extent.
  135. */
  136. do_div(ap->blkno, mp->m_sb.sb_rextsize);
  137. rtb = ap->blkno;
  138. ap->length = ralen;
  139. error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
  140. &ralen, ap->wasdel, prod, &rtb);
  141. if (error)
  142. return error;
  143. ap->blkno = rtb;
  144. if (ap->blkno != NULLFSBLOCK) {
  145. ap->blkno *= mp->m_sb.sb_rextsize;
  146. ralen *= mp->m_sb.sb_rextsize;
  147. ap->length = ralen;
  148. ap->ip->i_d.di_nblocks += ralen;
  149. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  150. if (ap->wasdel)
  151. ap->ip->i_delayed_blks -= ralen;
  152. /*
  153. * Adjust the disk quota also. This was reserved
  154. * earlier.
  155. */
  156. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  157. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  158. XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
  159. /* Zero the extent if we were asked to do so */
  160. if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) {
  161. error = xfs_zero_extent(ap->ip, ap->blkno, ap->length);
  162. if (error)
  163. return error;
  164. }
  165. } else {
  166. ap->length = 0;
  167. }
  168. return 0;
  169. }
  170. #endif /* CONFIG_XFS_RT */
  171. /*
  172. * Check if the endoff is outside the last extent. If so the caller will grow
  173. * the allocation to a stripe unit boundary. All offsets are considered outside
  174. * the end of file for an empty fork, so 1 is returned in *eof in that case.
  175. */
  176. int
  177. xfs_bmap_eof(
  178. struct xfs_inode *ip,
  179. xfs_fileoff_t endoff,
  180. int whichfork,
  181. int *eof)
  182. {
  183. struct xfs_bmbt_irec rec;
  184. int error;
  185. error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
  186. if (error || *eof)
  187. return error;
  188. *eof = endoff >= rec.br_startoff + rec.br_blockcount;
  189. return 0;
  190. }
  191. /*
  192. * Extent tree block counting routines.
  193. */
  194. /*
  195. * Count leaf blocks given a range of extent records. Delayed allocation
  196. * extents are not counted towards the totals.
  197. */
  198. xfs_extnum_t
  199. xfs_bmap_count_leaves(
  200. struct xfs_ifork *ifp,
  201. xfs_filblks_t *count)
  202. {
  203. struct xfs_iext_cursor icur;
  204. struct xfs_bmbt_irec got;
  205. xfs_extnum_t numrecs = 0;
  206. for_each_xfs_iext(ifp, &icur, &got) {
  207. if (!isnullstartblock(got.br_startblock)) {
  208. *count += got.br_blockcount;
  209. numrecs++;
  210. }
  211. }
  212. return numrecs;
  213. }
  214. /*
  215. * Count leaf blocks given a range of extent records originally
  216. * in btree format.
  217. */
  218. STATIC void
  219. xfs_bmap_disk_count_leaves(
  220. struct xfs_mount *mp,
  221. struct xfs_btree_block *block,
  222. int numrecs,
  223. xfs_filblks_t *count)
  224. {
  225. int b;
  226. xfs_bmbt_rec_t *frp;
  227. for (b = 1; b <= numrecs; b++) {
  228. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  229. *count += xfs_bmbt_disk_get_blockcount(frp);
  230. }
  231. }
  232. /*
  233. * Recursively walks each level of a btree
  234. * to count total fsblocks in use.
  235. */
  236. STATIC int
  237. xfs_bmap_count_tree(
  238. struct xfs_mount *mp,
  239. struct xfs_trans *tp,
  240. struct xfs_ifork *ifp,
  241. xfs_fsblock_t blockno,
  242. int levelin,
  243. xfs_extnum_t *nextents,
  244. xfs_filblks_t *count)
  245. {
  246. int error;
  247. struct xfs_buf *bp, *nbp;
  248. int level = levelin;
  249. __be64 *pp;
  250. xfs_fsblock_t bno = blockno;
  251. xfs_fsblock_t nextbno;
  252. struct xfs_btree_block *block, *nextblock;
  253. int numrecs;
  254. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
  255. &xfs_bmbt_buf_ops);
  256. if (error)
  257. return error;
  258. *count += 1;
  259. block = XFS_BUF_TO_BLOCK(bp);
  260. if (--level) {
  261. /* Not at node above leaves, count this level of nodes */
  262. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  263. while (nextbno != NULLFSBLOCK) {
  264. error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
  265. XFS_BMAP_BTREE_REF,
  266. &xfs_bmbt_buf_ops);
  267. if (error)
  268. return error;
  269. *count += 1;
  270. nextblock = XFS_BUF_TO_BLOCK(nbp);
  271. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  272. xfs_trans_brelse(tp, nbp);
  273. }
  274. /* Dive to the next level */
  275. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  276. bno = be64_to_cpu(*pp);
  277. error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, nextents,
  278. count);
  279. if (error) {
  280. xfs_trans_brelse(tp, bp);
  281. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  282. XFS_ERRLEVEL_LOW, mp);
  283. return -EFSCORRUPTED;
  284. }
  285. xfs_trans_brelse(tp, bp);
  286. } else {
  287. /* count all level 1 nodes and their leaves */
  288. for (;;) {
  289. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  290. numrecs = be16_to_cpu(block->bb_numrecs);
  291. (*nextents) += numrecs;
  292. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  293. xfs_trans_brelse(tp, bp);
  294. if (nextbno == NULLFSBLOCK)
  295. break;
  296. bno = nextbno;
  297. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  298. XFS_BMAP_BTREE_REF,
  299. &xfs_bmbt_buf_ops);
  300. if (error)
  301. return error;
  302. *count += 1;
  303. block = XFS_BUF_TO_BLOCK(bp);
  304. }
  305. }
  306. return 0;
  307. }
  308. /*
  309. * Count fsblocks of the given fork. Delayed allocation extents are
  310. * not counted towards the totals.
  311. */
  312. int
  313. xfs_bmap_count_blocks(
  314. struct xfs_trans *tp,
  315. struct xfs_inode *ip,
  316. int whichfork,
  317. xfs_extnum_t *nextents,
  318. xfs_filblks_t *count)
  319. {
  320. struct xfs_mount *mp; /* file system mount structure */
  321. __be64 *pp; /* pointer to block address */
  322. struct xfs_btree_block *block; /* current btree block */
  323. struct xfs_ifork *ifp; /* fork structure */
  324. xfs_fsblock_t bno; /* block # of "block" */
  325. int level; /* btree level, for checking */
  326. int error;
  327. bno = NULLFSBLOCK;
  328. mp = ip->i_mount;
  329. *nextents = 0;
  330. *count = 0;
  331. ifp = XFS_IFORK_PTR(ip, whichfork);
  332. if (!ifp)
  333. return 0;
  334. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  335. case XFS_DINODE_FMT_EXTENTS:
  336. *nextents = xfs_bmap_count_leaves(ifp, count);
  337. return 0;
  338. case XFS_DINODE_FMT_BTREE:
  339. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  340. error = xfs_iread_extents(tp, ip, whichfork);
  341. if (error)
  342. return error;
  343. }
  344. /*
  345. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  346. */
  347. block = ifp->if_broot;
  348. level = be16_to_cpu(block->bb_level);
  349. ASSERT(level > 0);
  350. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  351. bno = be64_to_cpu(*pp);
  352. ASSERT(bno != NULLFSBLOCK);
  353. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  354. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  355. error = xfs_bmap_count_tree(mp, tp, ifp, bno, level,
  356. nextents, count);
  357. if (error) {
  358. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)",
  359. XFS_ERRLEVEL_LOW, mp);
  360. return -EFSCORRUPTED;
  361. }
  362. return 0;
  363. }
  364. return 0;
  365. }
  366. static int
  367. xfs_getbmap_report_one(
  368. struct xfs_inode *ip,
  369. struct getbmapx *bmv,
  370. struct kgetbmap *out,
  371. int64_t bmv_end,
  372. struct xfs_bmbt_irec *got)
  373. {
  374. struct kgetbmap *p = out + bmv->bmv_entries;
  375. bool shared = false, trimmed = false;
  376. int error;
  377. error = xfs_reflink_trim_around_shared(ip, got, &shared, &trimmed);
  378. if (error)
  379. return error;
  380. if (isnullstartblock(got->br_startblock) ||
  381. got->br_startblock == DELAYSTARTBLOCK) {
  382. /*
  383. * Delalloc extents that start beyond EOF can occur due to
  384. * speculative EOF allocation when the delalloc extent is larger
  385. * than the largest freespace extent at conversion time. These
  386. * extents cannot be converted by data writeback, so can exist
  387. * here even if we are not supposed to be finding delalloc
  388. * extents.
  389. */
  390. if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip)))
  391. ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0);
  392. p->bmv_oflags |= BMV_OF_DELALLOC;
  393. p->bmv_block = -2;
  394. } else {
  395. p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
  396. }
  397. if (got->br_state == XFS_EXT_UNWRITTEN &&
  398. (bmv->bmv_iflags & BMV_IF_PREALLOC))
  399. p->bmv_oflags |= BMV_OF_PREALLOC;
  400. if (shared)
  401. p->bmv_oflags |= BMV_OF_SHARED;
  402. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
  403. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
  404. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  405. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  406. bmv->bmv_entries++;
  407. return 0;
  408. }
  409. static void
  410. xfs_getbmap_report_hole(
  411. struct xfs_inode *ip,
  412. struct getbmapx *bmv,
  413. struct kgetbmap *out,
  414. int64_t bmv_end,
  415. xfs_fileoff_t bno,
  416. xfs_fileoff_t end)
  417. {
  418. struct kgetbmap *p = out + bmv->bmv_entries;
  419. if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
  420. return;
  421. p->bmv_block = -1;
  422. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
  423. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
  424. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  425. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  426. bmv->bmv_entries++;
  427. }
  428. static inline bool
  429. xfs_getbmap_full(
  430. struct getbmapx *bmv)
  431. {
  432. return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
  433. }
  434. static bool
  435. xfs_getbmap_next_rec(
  436. struct xfs_bmbt_irec *rec,
  437. xfs_fileoff_t total_end)
  438. {
  439. xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount;
  440. if (end == total_end)
  441. return false;
  442. rec->br_startoff += rec->br_blockcount;
  443. if (!isnullstartblock(rec->br_startblock) &&
  444. rec->br_startblock != DELAYSTARTBLOCK)
  445. rec->br_startblock += rec->br_blockcount;
  446. rec->br_blockcount = total_end - end;
  447. return true;
  448. }
  449. /*
  450. * Get inode's extents as described in bmv, and format for output.
  451. * Calls formatter to fill the user's buffer until all extents
  452. * are mapped, until the passed-in bmv->bmv_count slots have
  453. * been filled, or until the formatter short-circuits the loop,
  454. * if it is tracking filled-in extents on its own.
  455. */
  456. int /* error code */
  457. xfs_getbmap(
  458. struct xfs_inode *ip,
  459. struct getbmapx *bmv, /* user bmap structure */
  460. struct kgetbmap *out)
  461. {
  462. struct xfs_mount *mp = ip->i_mount;
  463. int iflags = bmv->bmv_iflags;
  464. int whichfork, lock, error = 0;
  465. int64_t bmv_end, max_len;
  466. xfs_fileoff_t bno, first_bno;
  467. struct xfs_ifork *ifp;
  468. struct xfs_bmbt_irec got, rec;
  469. xfs_filblks_t len;
  470. struct xfs_iext_cursor icur;
  471. if (bmv->bmv_iflags & ~BMV_IF_VALID)
  472. return -EINVAL;
  473. #ifndef DEBUG
  474. /* Only allow CoW fork queries if we're debugging. */
  475. if (iflags & BMV_IF_COWFORK)
  476. return -EINVAL;
  477. #endif
  478. if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
  479. return -EINVAL;
  480. if (bmv->bmv_length < -1)
  481. return -EINVAL;
  482. bmv->bmv_entries = 0;
  483. if (bmv->bmv_length == 0)
  484. return 0;
  485. if (iflags & BMV_IF_ATTRFORK)
  486. whichfork = XFS_ATTR_FORK;
  487. else if (iflags & BMV_IF_COWFORK)
  488. whichfork = XFS_COW_FORK;
  489. else
  490. whichfork = XFS_DATA_FORK;
  491. ifp = XFS_IFORK_PTR(ip, whichfork);
  492. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  493. switch (whichfork) {
  494. case XFS_ATTR_FORK:
  495. if (!XFS_IFORK_Q(ip))
  496. goto out_unlock_iolock;
  497. max_len = 1LL << 32;
  498. lock = xfs_ilock_attr_map_shared(ip);
  499. break;
  500. case XFS_COW_FORK:
  501. /* No CoW fork? Just return */
  502. if (!ifp)
  503. goto out_unlock_iolock;
  504. if (xfs_get_cowextsz_hint(ip))
  505. max_len = mp->m_super->s_maxbytes;
  506. else
  507. max_len = XFS_ISIZE(ip);
  508. lock = XFS_ILOCK_SHARED;
  509. xfs_ilock(ip, lock);
  510. break;
  511. case XFS_DATA_FORK:
  512. if (!(iflags & BMV_IF_DELALLOC) &&
  513. (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
  514. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  515. if (error)
  516. goto out_unlock_iolock;
  517. /*
  518. * Even after flushing the inode, there can still be
  519. * delalloc blocks on the inode beyond EOF due to
  520. * speculative preallocation. These are not removed
  521. * until the release function is called or the inode
  522. * is inactivated. Hence we cannot assert here that
  523. * ip->i_delayed_blks == 0.
  524. */
  525. }
  526. if (xfs_get_extsz_hint(ip) ||
  527. (ip->i_d.di_flags &
  528. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
  529. max_len = mp->m_super->s_maxbytes;
  530. else
  531. max_len = XFS_ISIZE(ip);
  532. lock = xfs_ilock_data_map_shared(ip);
  533. break;
  534. }
  535. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  536. case XFS_DINODE_FMT_EXTENTS:
  537. case XFS_DINODE_FMT_BTREE:
  538. break;
  539. case XFS_DINODE_FMT_LOCAL:
  540. /* Local format inode forks report no extents. */
  541. goto out_unlock_ilock;
  542. default:
  543. error = -EINVAL;
  544. goto out_unlock_ilock;
  545. }
  546. if (bmv->bmv_length == -1) {
  547. max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
  548. bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
  549. }
  550. bmv_end = bmv->bmv_offset + bmv->bmv_length;
  551. first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
  552. len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
  553. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  554. error = xfs_iread_extents(NULL, ip, whichfork);
  555. if (error)
  556. goto out_unlock_ilock;
  557. }
  558. if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
  559. /*
  560. * Report a whole-file hole if the delalloc flag is set to
  561. * stay compatible with the old implementation.
  562. */
  563. if (iflags & BMV_IF_DELALLOC)
  564. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  565. XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
  566. goto out_unlock_ilock;
  567. }
  568. while (!xfs_getbmap_full(bmv)) {
  569. xfs_trim_extent(&got, first_bno, len);
  570. /*
  571. * Report an entry for a hole if this extent doesn't directly
  572. * follow the previous one.
  573. */
  574. if (got.br_startoff > bno) {
  575. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  576. got.br_startoff);
  577. if (xfs_getbmap_full(bmv))
  578. break;
  579. }
  580. /*
  581. * In order to report shared extents accurately, we report each
  582. * distinct shared / unshared part of a single bmbt record with
  583. * an individual getbmapx record.
  584. */
  585. bno = got.br_startoff + got.br_blockcount;
  586. rec = got;
  587. do {
  588. error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
  589. &rec);
  590. if (error || xfs_getbmap_full(bmv))
  591. goto out_unlock_ilock;
  592. } while (xfs_getbmap_next_rec(&rec, bno));
  593. if (!xfs_iext_next_extent(ifp, &icur, &got)) {
  594. xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  595. out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST;
  596. if (whichfork != XFS_ATTR_FORK && bno < end &&
  597. !xfs_getbmap_full(bmv)) {
  598. xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
  599. bno, end);
  600. }
  601. break;
  602. }
  603. if (bno >= first_bno + len)
  604. break;
  605. }
  606. out_unlock_ilock:
  607. xfs_iunlock(ip, lock);
  608. out_unlock_iolock:
  609. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  610. return error;
  611. }
  612. /*
  613. * Dead simple method of punching delalyed allocation blocks from a range in
  614. * the inode. This will always punch out both the start and end blocks, even
  615. * if the ranges only partially overlap them, so it is up to the caller to
  616. * ensure that partial blocks are not passed in.
  617. */
  618. int
  619. xfs_bmap_punch_delalloc_range(
  620. struct xfs_inode *ip,
  621. xfs_fileoff_t start_fsb,
  622. xfs_fileoff_t length)
  623. {
  624. struct xfs_ifork *ifp = &ip->i_df;
  625. xfs_fileoff_t end_fsb = start_fsb + length;
  626. struct xfs_bmbt_irec got, del;
  627. struct xfs_iext_cursor icur;
  628. int error = 0;
  629. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  630. xfs_ilock(ip, XFS_ILOCK_EXCL);
  631. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  632. goto out_unlock;
  633. while (got.br_startoff + got.br_blockcount > start_fsb) {
  634. del = got;
  635. xfs_trim_extent(&del, start_fsb, length);
  636. /*
  637. * A delete can push the cursor forward. Step back to the
  638. * previous extent on non-delalloc or extents outside the
  639. * target range.
  640. */
  641. if (!del.br_blockcount ||
  642. !isnullstartblock(del.br_startblock)) {
  643. if (!xfs_iext_prev_extent(ifp, &icur, &got))
  644. break;
  645. continue;
  646. }
  647. error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
  648. &got, &del);
  649. if (error || !xfs_iext_get_extent(ifp, &icur, &got))
  650. break;
  651. }
  652. out_unlock:
  653. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  654. return error;
  655. }
  656. /*
  657. * Test whether it is appropriate to check an inode for and free post EOF
  658. * blocks. The 'force' parameter determines whether we should also consider
  659. * regular files that are marked preallocated or append-only.
  660. */
  661. bool
  662. xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
  663. {
  664. /* prealloc/delalloc exists only on regular files */
  665. if (!S_ISREG(VFS_I(ip)->i_mode))
  666. return false;
  667. /*
  668. * Zero sized files with no cached pages and delalloc blocks will not
  669. * have speculative prealloc/delalloc blocks to remove.
  670. */
  671. if (VFS_I(ip)->i_size == 0 &&
  672. VFS_I(ip)->i_mapping->nrpages == 0 &&
  673. ip->i_delayed_blks == 0)
  674. return false;
  675. /* If we haven't read in the extent list, then don't do it now. */
  676. if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
  677. return false;
  678. /*
  679. * Do not free real preallocated or append-only files unless the file
  680. * has delalloc blocks and we are forced to remove them.
  681. */
  682. if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  683. if (!force || ip->i_delayed_blks == 0)
  684. return false;
  685. return true;
  686. }
  687. /*
  688. * This is called to free any blocks beyond eof. The caller must hold
  689. * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
  690. * reference to the inode.
  691. */
  692. int
  693. xfs_free_eofblocks(
  694. struct xfs_inode *ip)
  695. {
  696. struct xfs_trans *tp;
  697. int error;
  698. xfs_fileoff_t end_fsb;
  699. xfs_fileoff_t last_fsb;
  700. xfs_filblks_t map_len;
  701. int nimaps;
  702. struct xfs_bmbt_irec imap;
  703. struct xfs_mount *mp = ip->i_mount;
  704. /*
  705. * Figure out if there are any blocks beyond the end
  706. * of the file. If not, then there is nothing to do.
  707. */
  708. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  709. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  710. if (last_fsb <= end_fsb)
  711. return 0;
  712. map_len = last_fsb - end_fsb;
  713. nimaps = 1;
  714. xfs_ilock(ip, XFS_ILOCK_SHARED);
  715. error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
  716. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  717. /*
  718. * If there are blocks after the end of file, truncate the file to its
  719. * current size to free them up.
  720. */
  721. if (!error && (nimaps != 0) &&
  722. (imap.br_startblock != HOLESTARTBLOCK ||
  723. ip->i_delayed_blks)) {
  724. /*
  725. * Attach the dquots to the inode up front.
  726. */
  727. error = xfs_qm_dqattach(ip);
  728. if (error)
  729. return error;
  730. /* wait on dio to ensure i_size has settled */
  731. inode_dio_wait(VFS_I(ip));
  732. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
  733. &tp);
  734. if (error) {
  735. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  736. return error;
  737. }
  738. xfs_ilock(ip, XFS_ILOCK_EXCL);
  739. xfs_trans_ijoin(tp, ip, 0);
  740. /*
  741. * Do not update the on-disk file size. If we update the
  742. * on-disk file size and then the system crashes before the
  743. * contents of the file are flushed to disk then the files
  744. * may be full of holes (ie NULL files bug).
  745. */
  746. error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
  747. XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
  748. if (error) {
  749. /*
  750. * If we get an error at this point we simply don't
  751. * bother truncating the file.
  752. */
  753. xfs_trans_cancel(tp);
  754. } else {
  755. error = xfs_trans_commit(tp);
  756. if (!error)
  757. xfs_inode_clear_eofblocks_tag(ip);
  758. }
  759. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  760. }
  761. return error;
  762. }
  763. int
  764. xfs_alloc_file_space(
  765. struct xfs_inode *ip,
  766. xfs_off_t offset,
  767. xfs_off_t len,
  768. int alloc_type)
  769. {
  770. xfs_mount_t *mp = ip->i_mount;
  771. xfs_off_t count;
  772. xfs_filblks_t allocated_fsb;
  773. xfs_filblks_t allocatesize_fsb;
  774. xfs_extlen_t extsz, temp;
  775. xfs_fileoff_t startoffset_fsb;
  776. int nimaps;
  777. int quota_flag;
  778. int rt;
  779. xfs_trans_t *tp;
  780. xfs_bmbt_irec_t imaps[1], *imapp;
  781. uint qblocks, resblks, resrtextents;
  782. int error;
  783. trace_xfs_alloc_file_space(ip);
  784. if (XFS_FORCED_SHUTDOWN(mp))
  785. return -EIO;
  786. error = xfs_qm_dqattach(ip);
  787. if (error)
  788. return error;
  789. if (len <= 0)
  790. return -EINVAL;
  791. rt = XFS_IS_REALTIME_INODE(ip);
  792. extsz = xfs_get_extsz_hint(ip);
  793. count = len;
  794. imapp = &imaps[0];
  795. nimaps = 1;
  796. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  797. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  798. /*
  799. * Allocate file space until done or until there is an error
  800. */
  801. while (allocatesize_fsb && !error) {
  802. xfs_fileoff_t s, e;
  803. /*
  804. * Determine space reservations for data/realtime.
  805. */
  806. if (unlikely(extsz)) {
  807. s = startoffset_fsb;
  808. do_div(s, extsz);
  809. s *= extsz;
  810. e = startoffset_fsb + allocatesize_fsb;
  811. div_u64_rem(startoffset_fsb, extsz, &temp);
  812. if (temp)
  813. e += temp;
  814. div_u64_rem(e, extsz, &temp);
  815. if (temp)
  816. e += extsz - temp;
  817. } else {
  818. s = 0;
  819. e = allocatesize_fsb;
  820. }
  821. /*
  822. * The transaction reservation is limited to a 32-bit block
  823. * count, hence we need to limit the number of blocks we are
  824. * trying to reserve to avoid an overflow. We can't allocate
  825. * more than @nimaps extents, and an extent is limited on disk
  826. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  827. */
  828. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  829. if (unlikely(rt)) {
  830. resrtextents = qblocks = resblks;
  831. resrtextents /= mp->m_sb.sb_rextsize;
  832. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  833. quota_flag = XFS_QMOPT_RES_RTBLKS;
  834. } else {
  835. resrtextents = 0;
  836. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  837. quota_flag = XFS_QMOPT_RES_REGBLKS;
  838. }
  839. /*
  840. * Allocate and setup the transaction.
  841. */
  842. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
  843. resrtextents, 0, &tp);
  844. /*
  845. * Check for running out of space
  846. */
  847. if (error) {
  848. /*
  849. * Free the transaction structure.
  850. */
  851. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  852. break;
  853. }
  854. xfs_ilock(ip, XFS_ILOCK_EXCL);
  855. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  856. 0, quota_flag);
  857. if (error)
  858. goto error1;
  859. xfs_trans_ijoin(tp, ip, 0);
  860. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  861. allocatesize_fsb, alloc_type, resblks,
  862. imapp, &nimaps);
  863. if (error)
  864. goto error0;
  865. /*
  866. * Complete the transaction
  867. */
  868. error = xfs_trans_commit(tp);
  869. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  870. if (error)
  871. break;
  872. allocated_fsb = imapp->br_blockcount;
  873. if (nimaps == 0) {
  874. error = -ENOSPC;
  875. break;
  876. }
  877. startoffset_fsb += allocated_fsb;
  878. allocatesize_fsb -= allocated_fsb;
  879. }
  880. return error;
  881. error0: /* unlock inode, unreserve quota blocks, cancel trans */
  882. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  883. error1: /* Just cancel transaction */
  884. xfs_trans_cancel(tp);
  885. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  886. return error;
  887. }
  888. static int
  889. xfs_unmap_extent(
  890. struct xfs_inode *ip,
  891. xfs_fileoff_t startoffset_fsb,
  892. xfs_filblks_t len_fsb,
  893. int *done)
  894. {
  895. struct xfs_mount *mp = ip->i_mount;
  896. struct xfs_trans *tp;
  897. uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  898. int error;
  899. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  900. if (error) {
  901. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  902. return error;
  903. }
  904. xfs_ilock(ip, XFS_ILOCK_EXCL);
  905. error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
  906. ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
  907. if (error)
  908. goto out_trans_cancel;
  909. xfs_trans_ijoin(tp, ip, 0);
  910. error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
  911. if (error)
  912. goto out_trans_cancel;
  913. error = xfs_trans_commit(tp);
  914. out_unlock:
  915. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  916. return error;
  917. out_trans_cancel:
  918. xfs_trans_cancel(tp);
  919. goto out_unlock;
  920. }
  921. static int
  922. xfs_adjust_extent_unmap_boundaries(
  923. struct xfs_inode *ip,
  924. xfs_fileoff_t *startoffset_fsb,
  925. xfs_fileoff_t *endoffset_fsb)
  926. {
  927. struct xfs_mount *mp = ip->i_mount;
  928. struct xfs_bmbt_irec imap;
  929. int nimap, error;
  930. xfs_extlen_t mod = 0;
  931. nimap = 1;
  932. error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0);
  933. if (error)
  934. return error;
  935. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  936. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  937. div_u64_rem(imap.br_startblock, mp->m_sb.sb_rextsize, &mod);
  938. if (mod)
  939. *startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  940. }
  941. nimap = 1;
  942. error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0);
  943. if (error)
  944. return error;
  945. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  946. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  947. mod++;
  948. if (mod && mod != mp->m_sb.sb_rextsize)
  949. *endoffset_fsb -= mod;
  950. }
  951. return 0;
  952. }
  953. int
  954. xfs_flush_unmap_range(
  955. struct xfs_inode *ip,
  956. xfs_off_t offset,
  957. xfs_off_t len)
  958. {
  959. struct xfs_mount *mp = ip->i_mount;
  960. struct inode *inode = VFS_I(ip);
  961. xfs_off_t rounding, start, end;
  962. int error;
  963. /* wait for the completion of any pending DIOs */
  964. inode_dio_wait(inode);
  965. rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
  966. start = round_down(offset, rounding);
  967. end = round_up(offset + len, rounding) - 1;
  968. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  969. if (error)
  970. return error;
  971. truncate_pagecache_range(inode, start, end);
  972. return 0;
  973. }
  974. int
  975. xfs_free_file_space(
  976. struct xfs_inode *ip,
  977. xfs_off_t offset,
  978. xfs_off_t len)
  979. {
  980. struct xfs_mount *mp = ip->i_mount;
  981. xfs_fileoff_t startoffset_fsb;
  982. xfs_fileoff_t endoffset_fsb;
  983. int done = 0, error;
  984. trace_xfs_free_file_space(ip);
  985. error = xfs_qm_dqattach(ip);
  986. if (error)
  987. return error;
  988. if (len <= 0) /* if nothing being freed */
  989. return 0;
  990. error = xfs_flush_unmap_range(ip, offset, len);
  991. if (error)
  992. return error;
  993. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  994. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  995. /*
  996. * Need to zero the stuff we're not freeing, on disk. If it's a RT file
  997. * and we can't use unwritten extents then we actually need to ensure
  998. * to zero the whole extent, otherwise we just need to take of block
  999. * boundaries, and xfs_bunmapi will handle the rest.
  1000. */
  1001. if (XFS_IS_REALTIME_INODE(ip) &&
  1002. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1003. error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb,
  1004. &endoffset_fsb);
  1005. if (error)
  1006. return error;
  1007. }
  1008. if (endoffset_fsb > startoffset_fsb) {
  1009. while (!done) {
  1010. error = xfs_unmap_extent(ip, startoffset_fsb,
  1011. endoffset_fsb - startoffset_fsb, &done);
  1012. if (error)
  1013. return error;
  1014. }
  1015. }
  1016. /*
  1017. * Now that we've unmap all full blocks we'll have to zero out any
  1018. * partial block at the beginning and/or end. iomap_zero_range is smart
  1019. * enough to skip any holes, including those we just created, but we
  1020. * must take care not to zero beyond EOF and enlarge i_size.
  1021. */
  1022. if (offset >= XFS_ISIZE(ip))
  1023. return 0;
  1024. if (offset + len > XFS_ISIZE(ip))
  1025. len = XFS_ISIZE(ip) - offset;
  1026. error = iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
  1027. if (error)
  1028. return error;
  1029. /*
  1030. * If we zeroed right up to EOF and EOF straddles a page boundary we
  1031. * must make sure that the post-EOF area is also zeroed because the
  1032. * page could be mmap'd and iomap_zero_range doesn't do that for us.
  1033. * Writeback of the eof page will do this, albeit clumsily.
  1034. */
  1035. if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
  1036. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  1037. round_down(offset + len, PAGE_SIZE), LLONG_MAX);
  1038. }
  1039. return error;
  1040. }
  1041. /*
  1042. * Preallocate and zero a range of a file. This mechanism has the allocation
  1043. * semantics of fallocate and in addition converts data in the range to zeroes.
  1044. */
  1045. int
  1046. xfs_zero_file_space(
  1047. struct xfs_inode *ip,
  1048. xfs_off_t offset,
  1049. xfs_off_t len)
  1050. {
  1051. struct xfs_mount *mp = ip->i_mount;
  1052. uint blksize;
  1053. int error;
  1054. trace_xfs_zero_file_space(ip);
  1055. blksize = 1 << mp->m_sb.sb_blocklog;
  1056. /*
  1057. * Punch a hole and prealloc the range. We use hole punch rather than
  1058. * unwritten extent conversion for two reasons:
  1059. *
  1060. * 1.) Hole punch handles partial block zeroing for us.
  1061. *
  1062. * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
  1063. * by virtue of the hole punch.
  1064. */
  1065. error = xfs_free_file_space(ip, offset, len);
  1066. if (error)
  1067. goto out;
  1068. error = xfs_alloc_file_space(ip, round_down(offset, blksize),
  1069. round_up(offset + len, blksize) -
  1070. round_down(offset, blksize),
  1071. XFS_BMAPI_PREALLOC);
  1072. out:
  1073. return error;
  1074. }
  1075. static int
  1076. xfs_prepare_shift(
  1077. struct xfs_inode *ip,
  1078. loff_t offset)
  1079. {
  1080. int error;
  1081. /*
  1082. * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
  1083. * into the accessible region of the file.
  1084. */
  1085. if (xfs_can_free_eofblocks(ip, true)) {
  1086. error = xfs_free_eofblocks(ip);
  1087. if (error)
  1088. return error;
  1089. }
  1090. /*
  1091. * Writeback and invalidate cache for the remainder of the file as we're
  1092. * about to shift down every extent from offset to EOF.
  1093. */
  1094. error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
  1095. if (error)
  1096. return error;
  1097. /*
  1098. * Clean out anything hanging around in the cow fork now that
  1099. * we've flushed all the dirty data out to disk to avoid having
  1100. * CoW extents at the wrong offsets.
  1101. */
  1102. if (xfs_inode_has_cow_data(ip)) {
  1103. error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
  1104. true);
  1105. if (error)
  1106. return error;
  1107. }
  1108. return 0;
  1109. }
  1110. /*
  1111. * xfs_collapse_file_space()
  1112. * This routine frees disk space and shift extent for the given file.
  1113. * The first thing we do is to free data blocks in the specified range
  1114. * by calling xfs_free_file_space(). It would also sync dirty data
  1115. * and invalidate page cache over the region on which collapse range
  1116. * is working. And Shift extent records to the left to cover a hole.
  1117. * RETURNS:
  1118. * 0 on success
  1119. * errno on error
  1120. *
  1121. */
  1122. int
  1123. xfs_collapse_file_space(
  1124. struct xfs_inode *ip,
  1125. xfs_off_t offset,
  1126. xfs_off_t len)
  1127. {
  1128. struct xfs_mount *mp = ip->i_mount;
  1129. struct xfs_trans *tp;
  1130. int error;
  1131. xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len);
  1132. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  1133. uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1134. bool done = false;
  1135. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1136. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  1137. trace_xfs_collapse_file_space(ip);
  1138. error = xfs_free_file_space(ip, offset, len);
  1139. if (error)
  1140. return error;
  1141. error = xfs_prepare_shift(ip, offset);
  1142. if (error)
  1143. return error;
  1144. while (!error && !done) {
  1145. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0,
  1146. &tp);
  1147. if (error)
  1148. break;
  1149. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1150. error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
  1151. ip->i_gdquot, ip->i_pdquot, resblks, 0,
  1152. XFS_QMOPT_RES_REGBLKS);
  1153. if (error)
  1154. goto out_trans_cancel;
  1155. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1156. error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
  1157. &done);
  1158. if (error)
  1159. goto out_trans_cancel;
  1160. error = xfs_trans_commit(tp);
  1161. }
  1162. return error;
  1163. out_trans_cancel:
  1164. xfs_trans_cancel(tp);
  1165. return error;
  1166. }
  1167. /*
  1168. * xfs_insert_file_space()
  1169. * This routine create hole space by shifting extents for the given file.
  1170. * The first thing we do is to sync dirty data and invalidate page cache
  1171. * over the region on which insert range is working. And split an extent
  1172. * to two extents at given offset by calling xfs_bmap_split_extent.
  1173. * And shift all extent records which are laying between [offset,
  1174. * last allocated extent] to the right to reserve hole range.
  1175. * RETURNS:
  1176. * 0 on success
  1177. * errno on error
  1178. */
  1179. int
  1180. xfs_insert_file_space(
  1181. struct xfs_inode *ip,
  1182. loff_t offset,
  1183. loff_t len)
  1184. {
  1185. struct xfs_mount *mp = ip->i_mount;
  1186. struct xfs_trans *tp;
  1187. int error;
  1188. xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset);
  1189. xfs_fileoff_t next_fsb = NULLFSBLOCK;
  1190. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  1191. bool done = false;
  1192. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1193. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  1194. trace_xfs_insert_file_space(ip);
  1195. error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
  1196. if (error)
  1197. return error;
  1198. error = xfs_prepare_shift(ip, offset);
  1199. if (error)
  1200. return error;
  1201. /*
  1202. * The extent shifting code works on extent granularity. So, if stop_fsb
  1203. * is not the starting block of extent, we need to split the extent at
  1204. * stop_fsb.
  1205. */
  1206. error = xfs_bmap_split_extent(ip, stop_fsb);
  1207. if (error)
  1208. return error;
  1209. while (!error && !done) {
  1210. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0,
  1211. &tp);
  1212. if (error)
  1213. break;
  1214. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1215. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1216. error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
  1217. &done, stop_fsb);
  1218. if (error)
  1219. goto out_trans_cancel;
  1220. error = xfs_trans_commit(tp);
  1221. }
  1222. return error;
  1223. out_trans_cancel:
  1224. xfs_trans_cancel(tp);
  1225. return error;
  1226. }
  1227. /*
  1228. * We need to check that the format of the data fork in the temporary inode is
  1229. * valid for the target inode before doing the swap. This is not a problem with
  1230. * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
  1231. * data fork depending on the space the attribute fork is taking so we can get
  1232. * invalid formats on the target inode.
  1233. *
  1234. * E.g. target has space for 7 extents in extent format, temp inode only has
  1235. * space for 6. If we defragment down to 7 extents, then the tmp format is a
  1236. * btree, but when swapped it needs to be in extent format. Hence we can't just
  1237. * blindly swap data forks on attr2 filesystems.
  1238. *
  1239. * Note that we check the swap in both directions so that we don't end up with
  1240. * a corrupt temporary inode, either.
  1241. *
  1242. * Note that fixing the way xfs_fsr sets up the attribute fork in the source
  1243. * inode will prevent this situation from occurring, so all we do here is
  1244. * reject and log the attempt. basically we are putting the responsibility on
  1245. * userspace to get this right.
  1246. */
  1247. static int
  1248. xfs_swap_extents_check_format(
  1249. struct xfs_inode *ip, /* target inode */
  1250. struct xfs_inode *tip) /* tmp inode */
  1251. {
  1252. /* Should never get a local format */
  1253. if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
  1254. tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  1255. return -EINVAL;
  1256. /*
  1257. * if the target inode has less extents that then temporary inode then
  1258. * why did userspace call us?
  1259. */
  1260. if (ip->i_d.di_nextents < tip->i_d.di_nextents)
  1261. return -EINVAL;
  1262. /*
  1263. * If we have to use the (expensive) rmap swap method, we can
  1264. * handle any number of extents and any format.
  1265. */
  1266. if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb))
  1267. return 0;
  1268. /*
  1269. * if the target inode is in extent form and the temp inode is in btree
  1270. * form then we will end up with the target inode in the wrong format
  1271. * as we already know there are less extents in the temp inode.
  1272. */
  1273. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1274. tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1275. return -EINVAL;
  1276. /* Check temp in extent form to max in target */
  1277. if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1278. XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
  1279. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1280. return -EINVAL;
  1281. /* Check target in extent form to max in temp */
  1282. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1283. XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
  1284. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1285. return -EINVAL;
  1286. /*
  1287. * If we are in a btree format, check that the temp root block will fit
  1288. * in the target and that it has enough extents to be in btree format
  1289. * in the target.
  1290. *
  1291. * Note that we have to be careful to allow btree->extent conversions
  1292. * (a common defrag case) which will occur when the temp inode is in
  1293. * extent format...
  1294. */
  1295. if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1296. if (XFS_IFORK_Q(ip) &&
  1297. XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
  1298. return -EINVAL;
  1299. if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
  1300. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1301. return -EINVAL;
  1302. }
  1303. /* Reciprocal target->temp btree format checks */
  1304. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1305. if (XFS_IFORK_Q(tip) &&
  1306. XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
  1307. return -EINVAL;
  1308. if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
  1309. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1310. return -EINVAL;
  1311. }
  1312. return 0;
  1313. }
  1314. static int
  1315. xfs_swap_extent_flush(
  1316. struct xfs_inode *ip)
  1317. {
  1318. int error;
  1319. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1320. if (error)
  1321. return error;
  1322. truncate_pagecache_range(VFS_I(ip), 0, -1);
  1323. /* Verify O_DIRECT for ftmp */
  1324. if (VFS_I(ip)->i_mapping->nrpages)
  1325. return -EINVAL;
  1326. return 0;
  1327. }
  1328. /*
  1329. * Move extents from one file to another, when rmap is enabled.
  1330. */
  1331. STATIC int
  1332. xfs_swap_extent_rmap(
  1333. struct xfs_trans **tpp,
  1334. struct xfs_inode *ip,
  1335. struct xfs_inode *tip)
  1336. {
  1337. struct xfs_trans *tp = *tpp;
  1338. struct xfs_bmbt_irec irec;
  1339. struct xfs_bmbt_irec uirec;
  1340. struct xfs_bmbt_irec tirec;
  1341. xfs_fileoff_t offset_fsb;
  1342. xfs_fileoff_t end_fsb;
  1343. xfs_filblks_t count_fsb;
  1344. int error;
  1345. xfs_filblks_t ilen;
  1346. xfs_filblks_t rlen;
  1347. int nimaps;
  1348. uint64_t tip_flags2;
  1349. /*
  1350. * If the source file has shared blocks, we must flag the donor
  1351. * file as having shared blocks so that we get the shared-block
  1352. * rmap functions when we go to fix up the rmaps. The flags
  1353. * will be switch for reals later.
  1354. */
  1355. tip_flags2 = tip->i_d.di_flags2;
  1356. if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)
  1357. tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  1358. offset_fsb = 0;
  1359. end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
  1360. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  1361. while (count_fsb) {
  1362. /* Read extent from the donor file */
  1363. nimaps = 1;
  1364. error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
  1365. &nimaps, 0);
  1366. if (error)
  1367. goto out;
  1368. ASSERT(nimaps == 1);
  1369. ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
  1370. trace_xfs_swap_extent_rmap_remap(tip, &tirec);
  1371. ilen = tirec.br_blockcount;
  1372. /* Unmap the old blocks in the source file. */
  1373. while (tirec.br_blockcount) {
  1374. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  1375. trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
  1376. /* Read extent from the source file */
  1377. nimaps = 1;
  1378. error = xfs_bmapi_read(ip, tirec.br_startoff,
  1379. tirec.br_blockcount, &irec,
  1380. &nimaps, 0);
  1381. if (error)
  1382. goto out;
  1383. ASSERT(nimaps == 1);
  1384. ASSERT(tirec.br_startoff == irec.br_startoff);
  1385. trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
  1386. /* Trim the extent. */
  1387. uirec = tirec;
  1388. uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
  1389. tirec.br_blockcount,
  1390. irec.br_blockcount);
  1391. trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
  1392. /* Remove the mapping from the donor file. */
  1393. error = xfs_bmap_unmap_extent(tp, tip, &uirec);
  1394. if (error)
  1395. goto out;
  1396. /* Remove the mapping from the source file. */
  1397. error = xfs_bmap_unmap_extent(tp, ip, &irec);
  1398. if (error)
  1399. goto out;
  1400. /* Map the donor file's blocks into the source file. */
  1401. error = xfs_bmap_map_extent(tp, ip, &uirec);
  1402. if (error)
  1403. goto out;
  1404. /* Map the source file's blocks into the donor file. */
  1405. error = xfs_bmap_map_extent(tp, tip, &irec);
  1406. if (error)
  1407. goto out;
  1408. error = xfs_defer_finish(tpp);
  1409. tp = *tpp;
  1410. if (error)
  1411. goto out;
  1412. tirec.br_startoff += rlen;
  1413. if (tirec.br_startblock != HOLESTARTBLOCK &&
  1414. tirec.br_startblock != DELAYSTARTBLOCK)
  1415. tirec.br_startblock += rlen;
  1416. tirec.br_blockcount -= rlen;
  1417. }
  1418. /* Roll on... */
  1419. count_fsb -= ilen;
  1420. offset_fsb += ilen;
  1421. }
  1422. tip->i_d.di_flags2 = tip_flags2;
  1423. return 0;
  1424. out:
  1425. trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
  1426. tip->i_d.di_flags2 = tip_flags2;
  1427. return error;
  1428. }
  1429. /* Swap the extents of two files by swapping data forks. */
  1430. STATIC int
  1431. xfs_swap_extent_forks(
  1432. struct xfs_trans *tp,
  1433. struct xfs_inode *ip,
  1434. struct xfs_inode *tip,
  1435. int *src_log_flags,
  1436. int *target_log_flags)
  1437. {
  1438. xfs_filblks_t aforkblks = 0;
  1439. xfs_filblks_t taforkblks = 0;
  1440. xfs_extnum_t junk;
  1441. uint64_t tmp;
  1442. int error;
  1443. /*
  1444. * Count the number of extended attribute blocks
  1445. */
  1446. if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
  1447. (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1448. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
  1449. &aforkblks);
  1450. if (error)
  1451. return error;
  1452. }
  1453. if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
  1454. (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1455. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
  1456. &taforkblks);
  1457. if (error)
  1458. return error;
  1459. }
  1460. /*
  1461. * Btree format (v3) inodes have the inode number stamped in the bmbt
  1462. * block headers. We can't start changing the bmbt blocks until the
  1463. * inode owner change is logged so recovery does the right thing in the
  1464. * event of a crash. Set the owner change log flags now and leave the
  1465. * bmbt scan as the last step.
  1466. */
  1467. if (ip->i_d.di_version == 3 &&
  1468. ip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1469. (*target_log_flags) |= XFS_ILOG_DOWNER;
  1470. if (tip->i_d.di_version == 3 &&
  1471. tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1472. (*src_log_flags) |= XFS_ILOG_DOWNER;
  1473. /*
  1474. * Swap the data forks of the inodes
  1475. */
  1476. swap(ip->i_df, tip->i_df);
  1477. /*
  1478. * Fix the on-disk inode values
  1479. */
  1480. tmp = (uint64_t)ip->i_d.di_nblocks;
  1481. ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
  1482. tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
  1483. swap(ip->i_d.di_nextents, tip->i_d.di_nextents);
  1484. swap(ip->i_d.di_format, tip->i_d.di_format);
  1485. /*
  1486. * The extents in the source inode could still contain speculative
  1487. * preallocation beyond EOF (e.g. the file is open but not modified
  1488. * while defrag is in progress). In that case, we need to copy over the
  1489. * number of delalloc blocks the data fork in the source inode is
  1490. * tracking beyond EOF so that when the fork is truncated away when the
  1491. * temporary inode is unlinked we don't underrun the i_delayed_blks
  1492. * counter on that inode.
  1493. */
  1494. ASSERT(tip->i_delayed_blks == 0);
  1495. tip->i_delayed_blks = ip->i_delayed_blks;
  1496. ip->i_delayed_blks = 0;
  1497. switch (ip->i_d.di_format) {
  1498. case XFS_DINODE_FMT_EXTENTS:
  1499. (*src_log_flags) |= XFS_ILOG_DEXT;
  1500. break;
  1501. case XFS_DINODE_FMT_BTREE:
  1502. ASSERT(ip->i_d.di_version < 3 ||
  1503. (*src_log_flags & XFS_ILOG_DOWNER));
  1504. (*src_log_flags) |= XFS_ILOG_DBROOT;
  1505. break;
  1506. }
  1507. switch (tip->i_d.di_format) {
  1508. case XFS_DINODE_FMT_EXTENTS:
  1509. (*target_log_flags) |= XFS_ILOG_DEXT;
  1510. break;
  1511. case XFS_DINODE_FMT_BTREE:
  1512. (*target_log_flags) |= XFS_ILOG_DBROOT;
  1513. ASSERT(tip->i_d.di_version < 3 ||
  1514. (*target_log_flags & XFS_ILOG_DOWNER));
  1515. break;
  1516. }
  1517. return 0;
  1518. }
  1519. /*
  1520. * Fix up the owners of the bmbt blocks to refer to the current inode. The
  1521. * change owner scan attempts to order all modified buffers in the current
  1522. * transaction. In the event of ordered buffer failure, the offending buffer is
  1523. * physically logged as a fallback and the scan returns -EAGAIN. We must roll
  1524. * the transaction in this case to replenish the fallback log reservation and
  1525. * restart the scan. This process repeats until the scan completes.
  1526. */
  1527. static int
  1528. xfs_swap_change_owner(
  1529. struct xfs_trans **tpp,
  1530. struct xfs_inode *ip,
  1531. struct xfs_inode *tmpip)
  1532. {
  1533. int error;
  1534. struct xfs_trans *tp = *tpp;
  1535. do {
  1536. error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
  1537. NULL);
  1538. /* success or fatal error */
  1539. if (error != -EAGAIN)
  1540. break;
  1541. error = xfs_trans_roll(tpp);
  1542. if (error)
  1543. break;
  1544. tp = *tpp;
  1545. /*
  1546. * Redirty both inodes so they can relog and keep the log tail
  1547. * moving forward.
  1548. */
  1549. xfs_trans_ijoin(tp, ip, 0);
  1550. xfs_trans_ijoin(tp, tmpip, 0);
  1551. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1552. xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
  1553. } while (true);
  1554. return error;
  1555. }
  1556. int
  1557. xfs_swap_extents(
  1558. struct xfs_inode *ip, /* target inode */
  1559. struct xfs_inode *tip, /* tmp inode */
  1560. struct xfs_swapext *sxp)
  1561. {
  1562. struct xfs_mount *mp = ip->i_mount;
  1563. struct xfs_trans *tp;
  1564. struct xfs_bstat *sbp = &sxp->sx_stat;
  1565. int src_log_flags, target_log_flags;
  1566. int error = 0;
  1567. int lock_flags;
  1568. uint64_t f;
  1569. int resblks = 0;
  1570. /*
  1571. * Lock the inodes against other IO, page faults and truncate to
  1572. * begin with. Then we can ensure the inodes are flushed and have no
  1573. * page cache safely. Once we have done this we can take the ilocks and
  1574. * do the rest of the checks.
  1575. */
  1576. lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1577. lock_flags = XFS_MMAPLOCK_EXCL;
  1578. xfs_lock_two_inodes(ip, XFS_MMAPLOCK_EXCL, tip, XFS_MMAPLOCK_EXCL);
  1579. /* Verify that both files have the same format */
  1580. if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
  1581. error = -EINVAL;
  1582. goto out_unlock;
  1583. }
  1584. /* Verify both files are either real-time or non-realtime */
  1585. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  1586. error = -EINVAL;
  1587. goto out_unlock;
  1588. }
  1589. error = xfs_swap_extent_flush(ip);
  1590. if (error)
  1591. goto out_unlock;
  1592. error = xfs_swap_extent_flush(tip);
  1593. if (error)
  1594. goto out_unlock;
  1595. if (xfs_inode_has_cow_data(tip)) {
  1596. error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true);
  1597. if (error)
  1598. return error;
  1599. }
  1600. /*
  1601. * Extent "swapping" with rmap requires a permanent reservation and
  1602. * a block reservation because it's really just a remap operation
  1603. * performed with log redo items!
  1604. */
  1605. if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
  1606. int w = XFS_DATA_FORK;
  1607. uint32_t ipnext = XFS_IFORK_NEXTENTS(ip, w);
  1608. uint32_t tipnext = XFS_IFORK_NEXTENTS(tip, w);
  1609. /*
  1610. * Conceptually this shouldn't affect the shape of either bmbt,
  1611. * but since we atomically move extents one by one, we reserve
  1612. * enough space to rebuild both trees.
  1613. */
  1614. resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w);
  1615. resblks += XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w);
  1616. /*
  1617. * Handle the corner case where either inode might straddle the
  1618. * btree format boundary. If so, the inode could bounce between
  1619. * btree <-> extent format on unmap -> remap cycles, freeing and
  1620. * allocating a bmapbt block each time.
  1621. */
  1622. if (ipnext == (XFS_IFORK_MAXEXT(ip, w) + 1))
  1623. resblks += XFS_IFORK_MAXEXT(ip, w);
  1624. if (tipnext == (XFS_IFORK_MAXEXT(tip, w) + 1))
  1625. resblks += XFS_IFORK_MAXEXT(tip, w);
  1626. }
  1627. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  1628. if (error)
  1629. goto out_unlock;
  1630. /*
  1631. * Lock and join the inodes to the tansaction so that transaction commit
  1632. * or cancel will unlock the inodes from this point onwards.
  1633. */
  1634. xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL);
  1635. lock_flags |= XFS_ILOCK_EXCL;
  1636. xfs_trans_ijoin(tp, ip, 0);
  1637. xfs_trans_ijoin(tp, tip, 0);
  1638. /* Verify all data are being swapped */
  1639. if (sxp->sx_offset != 0 ||
  1640. sxp->sx_length != ip->i_d.di_size ||
  1641. sxp->sx_length != tip->i_d.di_size) {
  1642. error = -EFAULT;
  1643. goto out_trans_cancel;
  1644. }
  1645. trace_xfs_swap_extent_before(ip, 0);
  1646. trace_xfs_swap_extent_before(tip, 1);
  1647. /* check inode formats now that data is flushed */
  1648. error = xfs_swap_extents_check_format(ip, tip);
  1649. if (error) {
  1650. xfs_notice(mp,
  1651. "%s: inode 0x%llx format is incompatible for exchanging.",
  1652. __func__, ip->i_ino);
  1653. goto out_trans_cancel;
  1654. }
  1655. /*
  1656. * Compare the current change & modify times with that
  1657. * passed in. If they differ, we abort this swap.
  1658. * This is the mechanism used to ensure the calling
  1659. * process that the file was not changed out from
  1660. * under it.
  1661. */
  1662. if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
  1663. (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
  1664. (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
  1665. (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
  1666. error = -EBUSY;
  1667. goto out_trans_cancel;
  1668. }
  1669. /*
  1670. * Note the trickiness in setting the log flags - we set the owner log
  1671. * flag on the opposite inode (i.e. the inode we are setting the new
  1672. * owner to be) because once we swap the forks and log that, log
  1673. * recovery is going to see the fork as owned by the swapped inode,
  1674. * not the pre-swapped inodes.
  1675. */
  1676. src_log_flags = XFS_ILOG_CORE;
  1677. target_log_flags = XFS_ILOG_CORE;
  1678. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  1679. error = xfs_swap_extent_rmap(&tp, ip, tip);
  1680. else
  1681. error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
  1682. &target_log_flags);
  1683. if (error)
  1684. goto out_trans_cancel;
  1685. /* Do we have to swap reflink flags? */
  1686. if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^
  1687. (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) {
  1688. f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
  1689. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1690. ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
  1691. tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1692. tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK;
  1693. }
  1694. /* Swap the cow forks. */
  1695. if (xfs_sb_version_hasreflink(&mp->m_sb)) {
  1696. ASSERT(ip->i_cformat == XFS_DINODE_FMT_EXTENTS);
  1697. ASSERT(tip->i_cformat == XFS_DINODE_FMT_EXTENTS);
  1698. swap(ip->i_cnextents, tip->i_cnextents);
  1699. swap(ip->i_cowfp, tip->i_cowfp);
  1700. if (ip->i_cowfp && ip->i_cowfp->if_bytes)
  1701. xfs_inode_set_cowblocks_tag(ip);
  1702. else
  1703. xfs_inode_clear_cowblocks_tag(ip);
  1704. if (tip->i_cowfp && tip->i_cowfp->if_bytes)
  1705. xfs_inode_set_cowblocks_tag(tip);
  1706. else
  1707. xfs_inode_clear_cowblocks_tag(tip);
  1708. }
  1709. xfs_trans_log_inode(tp, ip, src_log_flags);
  1710. xfs_trans_log_inode(tp, tip, target_log_flags);
  1711. /*
  1712. * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
  1713. * have inode number owner values in the bmbt blocks that still refer to
  1714. * the old inode. Scan each bmbt to fix up the owner values with the
  1715. * inode number of the current inode.
  1716. */
  1717. if (src_log_flags & XFS_ILOG_DOWNER) {
  1718. error = xfs_swap_change_owner(&tp, ip, tip);
  1719. if (error)
  1720. goto out_trans_cancel;
  1721. }
  1722. if (target_log_flags & XFS_ILOG_DOWNER) {
  1723. error = xfs_swap_change_owner(&tp, tip, ip);
  1724. if (error)
  1725. goto out_trans_cancel;
  1726. }
  1727. /*
  1728. * If this is a synchronous mount, make sure that the
  1729. * transaction goes to disk before returning to the user.
  1730. */
  1731. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1732. xfs_trans_set_sync(tp);
  1733. error = xfs_trans_commit(tp);
  1734. trace_xfs_swap_extent_after(ip, 0);
  1735. trace_xfs_swap_extent_after(tip, 1);
  1736. out_unlock:
  1737. xfs_iunlock(ip, lock_flags);
  1738. xfs_iunlock(tip, lock_flags);
  1739. unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1740. return error;
  1741. out_trans_cancel:
  1742. xfs_trans_cancel(tp);
  1743. goto out_unlock;
  1744. }