xfs_bmap_util.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * Copyright (c) 2012 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_btree.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_extfree_item.h"
  32. #include "xfs_alloc.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_bmap_util.h"
  35. #include "xfs_bmap_btree.h"
  36. #include "xfs_rtalloc.h"
  37. #include "xfs_error.h"
  38. #include "xfs_quota.h"
  39. #include "xfs_trans_space.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_icache.h"
  42. #include "xfs_log.h"
  43. /* Kernel only BMAP related definitions and functions */
  44. /*
  45. * Convert the given file system block to a disk block. We have to treat it
  46. * differently based on whether the file is a real time file or not, because the
  47. * bmap code does.
  48. */
  49. xfs_daddr_t
  50. xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
  51. {
  52. return (XFS_IS_REALTIME_INODE(ip) ? \
  53. (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
  54. XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
  55. }
  56. /*
  57. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  58. * caller. Frees all the extents that need freeing, which must be done
  59. * last due to locking considerations. We never free any extents in
  60. * the first transaction.
  61. *
  62. * Return 1 if the given transaction was committed and a new one
  63. * started, and 0 otherwise in the committed parameter.
  64. */
  65. int /* error */
  66. xfs_bmap_finish(
  67. xfs_trans_t **tp, /* transaction pointer addr */
  68. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  69. int *committed) /* xact committed or not */
  70. {
  71. xfs_efd_log_item_t *efd; /* extent free data */
  72. xfs_efi_log_item_t *efi; /* extent free intention */
  73. int error; /* error return value */
  74. xfs_bmap_free_item_t *free; /* free extent item */
  75. struct xfs_trans_res tres; /* new log reservation */
  76. xfs_mount_t *mp; /* filesystem mount structure */
  77. xfs_bmap_free_item_t *next; /* next item on free list */
  78. xfs_trans_t *ntp; /* new transaction pointer */
  79. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  80. if (flist->xbf_count == 0) {
  81. *committed = 0;
  82. return 0;
  83. }
  84. ntp = *tp;
  85. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  86. for (free = flist->xbf_first; free; free = free->xbfi_next)
  87. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  88. free->xbfi_blockcount);
  89. tres.tr_logres = ntp->t_log_res;
  90. tres.tr_logcount = ntp->t_log_count;
  91. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  92. ntp = xfs_trans_dup(*tp);
  93. error = xfs_trans_commit(*tp, 0);
  94. *tp = ntp;
  95. *committed = 1;
  96. /*
  97. * We have a new transaction, so we should return committed=1,
  98. * even though we're returning an error.
  99. */
  100. if (error)
  101. return error;
  102. /*
  103. * transaction commit worked ok so we can drop the extra ticket
  104. * reference that we gained in xfs_trans_dup()
  105. */
  106. xfs_log_ticket_put(ntp->t_ticket);
  107. error = xfs_trans_reserve(ntp, &tres, 0, 0);
  108. if (error)
  109. return error;
  110. efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
  111. for (free = flist->xbf_first; free != NULL; free = next) {
  112. next = free->xbfi_next;
  113. if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
  114. free->xbfi_blockcount))) {
  115. /*
  116. * The bmap free list will be cleaned up at a
  117. * higher level. The EFI will be canceled when
  118. * this transaction is aborted.
  119. * Need to force shutdown here to make sure it
  120. * happens, since this transaction may not be
  121. * dirty yet.
  122. */
  123. mp = ntp->t_mountp;
  124. if (!XFS_FORCED_SHUTDOWN(mp))
  125. xfs_force_shutdown(mp,
  126. (error == -EFSCORRUPTED) ?
  127. SHUTDOWN_CORRUPT_INCORE :
  128. SHUTDOWN_META_IO_ERROR);
  129. return error;
  130. }
  131. xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
  132. free->xbfi_blockcount);
  133. xfs_bmap_del_free(flist, NULL, free);
  134. }
  135. return 0;
  136. }
  137. int
  138. xfs_bmap_rtalloc(
  139. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  140. {
  141. xfs_alloctype_t atype = 0; /* type for allocation routines */
  142. int error; /* error return value */
  143. xfs_mount_t *mp; /* mount point structure */
  144. xfs_extlen_t prod = 0; /* product factor for allocators */
  145. xfs_extlen_t ralen = 0; /* realtime allocation length */
  146. xfs_extlen_t align; /* minimum allocation alignment */
  147. xfs_rtblock_t rtb;
  148. mp = ap->ip->i_mount;
  149. align = xfs_get_extsz_hint(ap->ip);
  150. prod = align / mp->m_sb.sb_rextsize;
  151. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  152. align, 1, ap->eof, 0,
  153. ap->conv, &ap->offset, &ap->length);
  154. if (error)
  155. return error;
  156. ASSERT(ap->length);
  157. ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
  158. /*
  159. * If the offset & length are not perfectly aligned
  160. * then kill prod, it will just get us in trouble.
  161. */
  162. if (do_mod(ap->offset, align) || ap->length % align)
  163. prod = 1;
  164. /*
  165. * Set ralen to be the actual requested length in rtextents.
  166. */
  167. ralen = ap->length / mp->m_sb.sb_rextsize;
  168. /*
  169. * If the old value was close enough to MAXEXTLEN that
  170. * we rounded up to it, cut it back so it's valid again.
  171. * Note that if it's a really large request (bigger than
  172. * MAXEXTLEN), we don't hear about that number, and can't
  173. * adjust the starting point to match it.
  174. */
  175. if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
  176. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  177. /*
  178. * Lock out other modifications to the RT bitmap inode.
  179. */
  180. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  181. xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  182. /*
  183. * If it's an allocation to an empty file at offset 0,
  184. * pick an extent that will space things out in the rt area.
  185. */
  186. if (ap->eof && ap->offset == 0) {
  187. xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
  188. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  189. if (error)
  190. return error;
  191. ap->blkno = rtx * mp->m_sb.sb_rextsize;
  192. } else {
  193. ap->blkno = 0;
  194. }
  195. xfs_bmap_adjacent(ap);
  196. /*
  197. * Realtime allocation, done through xfs_rtallocate_extent.
  198. */
  199. atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
  200. do_div(ap->blkno, mp->m_sb.sb_rextsize);
  201. rtb = ap->blkno;
  202. ap->length = ralen;
  203. if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
  204. &ralen, atype, ap->wasdel, prod, &rtb)))
  205. return error;
  206. if (rtb == NULLFSBLOCK && prod > 1 &&
  207. (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
  208. ap->length, &ralen, atype,
  209. ap->wasdel, 1, &rtb)))
  210. return error;
  211. ap->blkno = rtb;
  212. if (ap->blkno != NULLFSBLOCK) {
  213. ap->blkno *= mp->m_sb.sb_rextsize;
  214. ralen *= mp->m_sb.sb_rextsize;
  215. ap->length = ralen;
  216. ap->ip->i_d.di_nblocks += ralen;
  217. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  218. if (ap->wasdel)
  219. ap->ip->i_delayed_blks -= ralen;
  220. /*
  221. * Adjust the disk quota also. This was reserved
  222. * earlier.
  223. */
  224. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  225. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  226. XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
  227. } else {
  228. ap->length = 0;
  229. }
  230. return 0;
  231. }
  232. /*
  233. * Check if the endoff is outside the last extent. If so the caller will grow
  234. * the allocation to a stripe unit boundary. All offsets are considered outside
  235. * the end of file for an empty fork, so 1 is returned in *eof in that case.
  236. */
  237. int
  238. xfs_bmap_eof(
  239. struct xfs_inode *ip,
  240. xfs_fileoff_t endoff,
  241. int whichfork,
  242. int *eof)
  243. {
  244. struct xfs_bmbt_irec rec;
  245. int error;
  246. error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
  247. if (error || *eof)
  248. return error;
  249. *eof = endoff >= rec.br_startoff + rec.br_blockcount;
  250. return 0;
  251. }
  252. /*
  253. * Extent tree block counting routines.
  254. */
  255. /*
  256. * Count leaf blocks given a range of extent records.
  257. */
  258. STATIC void
  259. xfs_bmap_count_leaves(
  260. xfs_ifork_t *ifp,
  261. xfs_extnum_t idx,
  262. int numrecs,
  263. int *count)
  264. {
  265. int b;
  266. for (b = 0; b < numrecs; b++) {
  267. xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
  268. *count += xfs_bmbt_get_blockcount(frp);
  269. }
  270. }
  271. /*
  272. * Count leaf blocks given a range of extent records originally
  273. * in btree format.
  274. */
  275. STATIC void
  276. xfs_bmap_disk_count_leaves(
  277. struct xfs_mount *mp,
  278. struct xfs_btree_block *block,
  279. int numrecs,
  280. int *count)
  281. {
  282. int b;
  283. xfs_bmbt_rec_t *frp;
  284. for (b = 1; b <= numrecs; b++) {
  285. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  286. *count += xfs_bmbt_disk_get_blockcount(frp);
  287. }
  288. }
  289. /*
  290. * Recursively walks each level of a btree
  291. * to count total fsblocks in use.
  292. */
  293. STATIC int /* error */
  294. xfs_bmap_count_tree(
  295. xfs_mount_t *mp, /* file system mount point */
  296. xfs_trans_t *tp, /* transaction pointer */
  297. xfs_ifork_t *ifp, /* inode fork pointer */
  298. xfs_fsblock_t blockno, /* file system block number */
  299. int levelin, /* level in btree */
  300. int *count) /* Count of blocks */
  301. {
  302. int error;
  303. xfs_buf_t *bp, *nbp;
  304. int level = levelin;
  305. __be64 *pp;
  306. xfs_fsblock_t bno = blockno;
  307. xfs_fsblock_t nextbno;
  308. struct xfs_btree_block *block, *nextblock;
  309. int numrecs;
  310. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
  311. &xfs_bmbt_buf_ops);
  312. if (error)
  313. return error;
  314. *count += 1;
  315. block = XFS_BUF_TO_BLOCK(bp);
  316. if (--level) {
  317. /* Not at node above leaves, count this level of nodes */
  318. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  319. while (nextbno != NULLFSBLOCK) {
  320. error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
  321. XFS_BMAP_BTREE_REF,
  322. &xfs_bmbt_buf_ops);
  323. if (error)
  324. return error;
  325. *count += 1;
  326. nextblock = XFS_BUF_TO_BLOCK(nbp);
  327. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  328. xfs_trans_brelse(tp, nbp);
  329. }
  330. /* Dive to the next level */
  331. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  332. bno = be64_to_cpu(*pp);
  333. if (unlikely((error =
  334. xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
  335. xfs_trans_brelse(tp, bp);
  336. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  337. XFS_ERRLEVEL_LOW, mp);
  338. return -EFSCORRUPTED;
  339. }
  340. xfs_trans_brelse(tp, bp);
  341. } else {
  342. /* count all level 1 nodes and their leaves */
  343. for (;;) {
  344. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  345. numrecs = be16_to_cpu(block->bb_numrecs);
  346. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  347. xfs_trans_brelse(tp, bp);
  348. if (nextbno == NULLFSBLOCK)
  349. break;
  350. bno = nextbno;
  351. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  352. XFS_BMAP_BTREE_REF,
  353. &xfs_bmbt_buf_ops);
  354. if (error)
  355. return error;
  356. *count += 1;
  357. block = XFS_BUF_TO_BLOCK(bp);
  358. }
  359. }
  360. return 0;
  361. }
  362. /*
  363. * Count fsblocks of the given fork.
  364. */
  365. int /* error */
  366. xfs_bmap_count_blocks(
  367. xfs_trans_t *tp, /* transaction pointer */
  368. xfs_inode_t *ip, /* incore inode */
  369. int whichfork, /* data or attr fork */
  370. int *count) /* out: count of blocks */
  371. {
  372. struct xfs_btree_block *block; /* current btree block */
  373. xfs_fsblock_t bno; /* block # of "block" */
  374. xfs_ifork_t *ifp; /* fork structure */
  375. int level; /* btree level, for checking */
  376. xfs_mount_t *mp; /* file system mount structure */
  377. __be64 *pp; /* pointer to block address */
  378. bno = NULLFSBLOCK;
  379. mp = ip->i_mount;
  380. ifp = XFS_IFORK_PTR(ip, whichfork);
  381. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  382. xfs_bmap_count_leaves(ifp, 0,
  383. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
  384. count);
  385. return 0;
  386. }
  387. /*
  388. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  389. */
  390. block = ifp->if_broot;
  391. level = be16_to_cpu(block->bb_level);
  392. ASSERT(level > 0);
  393. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  394. bno = be64_to_cpu(*pp);
  395. ASSERT(bno != NULLFSBLOCK);
  396. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  397. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  398. if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
  399. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  400. mp);
  401. return -EFSCORRUPTED;
  402. }
  403. return 0;
  404. }
  405. /*
  406. * returns 1 for success, 0 if we failed to map the extent.
  407. */
  408. STATIC int
  409. xfs_getbmapx_fix_eof_hole(
  410. xfs_inode_t *ip, /* xfs incore inode pointer */
  411. struct getbmapx *out, /* output structure */
  412. int prealloced, /* this is a file with
  413. * preallocated data space */
  414. __int64_t end, /* last block requested */
  415. xfs_fsblock_t startblock)
  416. {
  417. __int64_t fixlen;
  418. xfs_mount_t *mp; /* file system mount point */
  419. xfs_ifork_t *ifp; /* inode fork pointer */
  420. xfs_extnum_t lastx; /* last extent pointer */
  421. xfs_fileoff_t fileblock;
  422. if (startblock == HOLESTARTBLOCK) {
  423. mp = ip->i_mount;
  424. out->bmv_block = -1;
  425. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
  426. fixlen -= out->bmv_offset;
  427. if (prealloced && out->bmv_offset + out->bmv_length == end) {
  428. /* Came to hole at EOF. Trim it. */
  429. if (fixlen <= 0)
  430. return 0;
  431. out->bmv_length = fixlen;
  432. }
  433. } else {
  434. if (startblock == DELAYSTARTBLOCK)
  435. out->bmv_block = -2;
  436. else
  437. out->bmv_block = xfs_fsb_to_db(ip, startblock);
  438. fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
  439. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  440. if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
  441. (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
  442. out->bmv_oflags |= BMV_OF_LAST;
  443. }
  444. return 1;
  445. }
  446. /*
  447. * Get inode's extents as described in bmv, and format for output.
  448. * Calls formatter to fill the user's buffer until all extents
  449. * are mapped, until the passed-in bmv->bmv_count slots have
  450. * been filled, or until the formatter short-circuits the loop,
  451. * if it is tracking filled-in extents on its own.
  452. */
  453. int /* error code */
  454. xfs_getbmap(
  455. xfs_inode_t *ip,
  456. struct getbmapx *bmv, /* user bmap structure */
  457. xfs_bmap_format_t formatter, /* format to user */
  458. void *arg) /* formatter arg */
  459. {
  460. __int64_t bmvend; /* last block requested */
  461. int error = 0; /* return value */
  462. __int64_t fixlen; /* length for -1 case */
  463. int i; /* extent number */
  464. int lock; /* lock state */
  465. xfs_bmbt_irec_t *map; /* buffer for user's data */
  466. xfs_mount_t *mp; /* file system mount point */
  467. int nex; /* # of user extents can do */
  468. int nexleft; /* # of user extents left */
  469. int subnex; /* # of bmapi's can do */
  470. int nmap; /* number of map entries */
  471. struct getbmapx *out; /* output structure */
  472. int whichfork; /* data or attr fork */
  473. int prealloced; /* this is a file with
  474. * preallocated data space */
  475. int iflags; /* interface flags */
  476. int bmapi_flags; /* flags for xfs_bmapi */
  477. int cur_ext = 0;
  478. mp = ip->i_mount;
  479. iflags = bmv->bmv_iflags;
  480. whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
  481. if (whichfork == XFS_ATTR_FORK) {
  482. if (XFS_IFORK_Q(ip)) {
  483. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  484. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  485. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  486. return -EINVAL;
  487. } else if (unlikely(
  488. ip->i_d.di_aformat != 0 &&
  489. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  490. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  491. ip->i_mount);
  492. return -EFSCORRUPTED;
  493. }
  494. prealloced = 0;
  495. fixlen = 1LL << 32;
  496. } else {
  497. if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  498. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  499. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  500. return -EINVAL;
  501. if (xfs_get_extsz_hint(ip) ||
  502. ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
  503. prealloced = 1;
  504. fixlen = mp->m_super->s_maxbytes;
  505. } else {
  506. prealloced = 0;
  507. fixlen = XFS_ISIZE(ip);
  508. }
  509. }
  510. if (bmv->bmv_length == -1) {
  511. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  512. bmv->bmv_length =
  513. max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
  514. } else if (bmv->bmv_length == 0) {
  515. bmv->bmv_entries = 0;
  516. return 0;
  517. } else if (bmv->bmv_length < 0) {
  518. return -EINVAL;
  519. }
  520. nex = bmv->bmv_count - 1;
  521. if (nex <= 0)
  522. return -EINVAL;
  523. bmvend = bmv->bmv_offset + bmv->bmv_length;
  524. if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
  525. return -ENOMEM;
  526. out = kmem_zalloc_large(bmv->bmv_count * sizeof(struct getbmapx), 0);
  527. if (!out)
  528. return -ENOMEM;
  529. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  530. if (whichfork == XFS_DATA_FORK) {
  531. if (!(iflags & BMV_IF_DELALLOC) &&
  532. (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
  533. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  534. if (error)
  535. goto out_unlock_iolock;
  536. /*
  537. * Even after flushing the inode, there can still be
  538. * delalloc blocks on the inode beyond EOF due to
  539. * speculative preallocation. These are not removed
  540. * until the release function is called or the inode
  541. * is inactivated. Hence we cannot assert here that
  542. * ip->i_delayed_blks == 0.
  543. */
  544. }
  545. lock = xfs_ilock_data_map_shared(ip);
  546. } else {
  547. lock = xfs_ilock_attr_map_shared(ip);
  548. }
  549. /*
  550. * Don't let nex be bigger than the number of extents
  551. * we can have assuming alternating holes and real extents.
  552. */
  553. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  554. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  555. bmapi_flags = xfs_bmapi_aflag(whichfork);
  556. if (!(iflags & BMV_IF_PREALLOC))
  557. bmapi_flags |= XFS_BMAPI_IGSTATE;
  558. /*
  559. * Allocate enough space to handle "subnex" maps at a time.
  560. */
  561. error = -ENOMEM;
  562. subnex = 16;
  563. map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
  564. if (!map)
  565. goto out_unlock_ilock;
  566. bmv->bmv_entries = 0;
  567. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
  568. (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
  569. error = 0;
  570. goto out_free_map;
  571. }
  572. nexleft = nex;
  573. do {
  574. nmap = (nexleft > subnex) ? subnex : nexleft;
  575. error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  576. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  577. map, &nmap, bmapi_flags);
  578. if (error)
  579. goto out_free_map;
  580. ASSERT(nmap <= subnex);
  581. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  582. out[cur_ext].bmv_oflags = 0;
  583. if (map[i].br_state == XFS_EXT_UNWRITTEN)
  584. out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
  585. else if (map[i].br_startblock == DELAYSTARTBLOCK)
  586. out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
  587. out[cur_ext].bmv_offset =
  588. XFS_FSB_TO_BB(mp, map[i].br_startoff);
  589. out[cur_ext].bmv_length =
  590. XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  591. out[cur_ext].bmv_unused1 = 0;
  592. out[cur_ext].bmv_unused2 = 0;
  593. /*
  594. * delayed allocation extents that start beyond EOF can
  595. * occur due to speculative EOF allocation when the
  596. * delalloc extent is larger than the largest freespace
  597. * extent at conversion time. These extents cannot be
  598. * converted by data writeback, so can exist here even
  599. * if we are not supposed to be finding delalloc
  600. * extents.
  601. */
  602. if (map[i].br_startblock == DELAYSTARTBLOCK &&
  603. map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
  604. ASSERT((iflags & BMV_IF_DELALLOC) != 0);
  605. if (map[i].br_startblock == HOLESTARTBLOCK &&
  606. whichfork == XFS_ATTR_FORK) {
  607. /* came to the end of attribute fork */
  608. out[cur_ext].bmv_oflags |= BMV_OF_LAST;
  609. goto out_free_map;
  610. }
  611. if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
  612. prealloced, bmvend,
  613. map[i].br_startblock))
  614. goto out_free_map;
  615. bmv->bmv_offset =
  616. out[cur_ext].bmv_offset +
  617. out[cur_ext].bmv_length;
  618. bmv->bmv_length =
  619. max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
  620. /*
  621. * In case we don't want to return the hole,
  622. * don't increase cur_ext so that we can reuse
  623. * it in the next loop.
  624. */
  625. if ((iflags & BMV_IF_NO_HOLES) &&
  626. map[i].br_startblock == HOLESTARTBLOCK) {
  627. memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
  628. continue;
  629. }
  630. nexleft--;
  631. bmv->bmv_entries++;
  632. cur_ext++;
  633. }
  634. } while (nmap && nexleft && bmv->bmv_length);
  635. out_free_map:
  636. kmem_free(map);
  637. out_unlock_ilock:
  638. xfs_iunlock(ip, lock);
  639. out_unlock_iolock:
  640. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  641. for (i = 0; i < cur_ext; i++) {
  642. int full = 0; /* user array is full */
  643. /* format results & advance arg */
  644. error = formatter(&arg, &out[i], &full);
  645. if (error || full)
  646. break;
  647. }
  648. kmem_free(out);
  649. return error;
  650. }
  651. /*
  652. * dead simple method of punching delalyed allocation blocks from a range in
  653. * the inode. Walks a block at a time so will be slow, but is only executed in
  654. * rare error cases so the overhead is not critical. This will always punch out
  655. * both the start and end blocks, even if the ranges only partially overlap
  656. * them, so it is up to the caller to ensure that partial blocks are not
  657. * passed in.
  658. */
  659. int
  660. xfs_bmap_punch_delalloc_range(
  661. struct xfs_inode *ip,
  662. xfs_fileoff_t start_fsb,
  663. xfs_fileoff_t length)
  664. {
  665. xfs_fileoff_t remaining = length;
  666. int error = 0;
  667. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  668. do {
  669. int done;
  670. xfs_bmbt_irec_t imap;
  671. int nimaps = 1;
  672. xfs_fsblock_t firstblock;
  673. xfs_bmap_free_t flist;
  674. /*
  675. * Map the range first and check that it is a delalloc extent
  676. * before trying to unmap the range. Otherwise we will be
  677. * trying to remove a real extent (which requires a
  678. * transaction) or a hole, which is probably a bad idea...
  679. */
  680. error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
  681. XFS_BMAPI_ENTIRE);
  682. if (error) {
  683. /* something screwed, just bail */
  684. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  685. xfs_alert(ip->i_mount,
  686. "Failed delalloc mapping lookup ino %lld fsb %lld.",
  687. ip->i_ino, start_fsb);
  688. }
  689. break;
  690. }
  691. if (!nimaps) {
  692. /* nothing there */
  693. goto next_block;
  694. }
  695. if (imap.br_startblock != DELAYSTARTBLOCK) {
  696. /* been converted, ignore */
  697. goto next_block;
  698. }
  699. WARN_ON(imap.br_blockcount == 0);
  700. /*
  701. * Note: while we initialise the firstblock/flist pair, they
  702. * should never be used because blocks should never be
  703. * allocated or freed for a delalloc extent and hence we need
  704. * don't cancel or finish them after the xfs_bunmapi() call.
  705. */
  706. xfs_bmap_init(&flist, &firstblock);
  707. error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
  708. &flist, &done);
  709. if (error)
  710. break;
  711. ASSERT(!flist.xbf_count && !flist.xbf_first);
  712. next_block:
  713. start_fsb++;
  714. remaining--;
  715. } while(remaining > 0);
  716. return error;
  717. }
  718. /*
  719. * Test whether it is appropriate to check an inode for and free post EOF
  720. * blocks. The 'force' parameter determines whether we should also consider
  721. * regular files that are marked preallocated or append-only.
  722. */
  723. bool
  724. xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
  725. {
  726. /* prealloc/delalloc exists only on regular files */
  727. if (!S_ISREG(ip->i_d.di_mode))
  728. return false;
  729. /*
  730. * Zero sized files with no cached pages and delalloc blocks will not
  731. * have speculative prealloc/delalloc blocks to remove.
  732. */
  733. if (VFS_I(ip)->i_size == 0 &&
  734. VFS_I(ip)->i_mapping->nrpages == 0 &&
  735. ip->i_delayed_blks == 0)
  736. return false;
  737. /* If we haven't read in the extent list, then don't do it now. */
  738. if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
  739. return false;
  740. /*
  741. * Do not free real preallocated or append-only files unless the file
  742. * has delalloc blocks and we are forced to remove them.
  743. */
  744. if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  745. if (!force || ip->i_delayed_blks == 0)
  746. return false;
  747. return true;
  748. }
  749. /*
  750. * This is called by xfs_inactive to free any blocks beyond eof
  751. * when the link count isn't zero and by xfs_dm_punch_hole() when
  752. * punching a hole to EOF.
  753. */
  754. int
  755. xfs_free_eofblocks(
  756. xfs_mount_t *mp,
  757. xfs_inode_t *ip,
  758. bool need_iolock)
  759. {
  760. xfs_trans_t *tp;
  761. int error;
  762. xfs_fileoff_t end_fsb;
  763. xfs_fileoff_t last_fsb;
  764. xfs_filblks_t map_len;
  765. int nimaps;
  766. xfs_bmbt_irec_t imap;
  767. /*
  768. * Figure out if there are any blocks beyond the end
  769. * of the file. If not, then there is nothing to do.
  770. */
  771. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  772. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  773. if (last_fsb <= end_fsb)
  774. return 0;
  775. map_len = last_fsb - end_fsb;
  776. nimaps = 1;
  777. xfs_ilock(ip, XFS_ILOCK_SHARED);
  778. error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
  779. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  780. if (!error && (nimaps != 0) &&
  781. (imap.br_startblock != HOLESTARTBLOCK ||
  782. ip->i_delayed_blks)) {
  783. /*
  784. * Attach the dquots to the inode up front.
  785. */
  786. error = xfs_qm_dqattach(ip, 0);
  787. if (error)
  788. return error;
  789. /*
  790. * There are blocks after the end of file.
  791. * Free them up now by truncating the file to
  792. * its current size.
  793. */
  794. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  795. if (need_iolock) {
  796. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
  797. xfs_trans_cancel(tp, 0);
  798. return -EAGAIN;
  799. }
  800. }
  801. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  802. if (error) {
  803. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  804. xfs_trans_cancel(tp, 0);
  805. if (need_iolock)
  806. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  807. return error;
  808. }
  809. xfs_ilock(ip, XFS_ILOCK_EXCL);
  810. xfs_trans_ijoin(tp, ip, 0);
  811. /*
  812. * Do not update the on-disk file size. If we update the
  813. * on-disk file size and then the system crashes before the
  814. * contents of the file are flushed to disk then the files
  815. * may be full of holes (ie NULL files bug).
  816. */
  817. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
  818. XFS_ISIZE(ip));
  819. if (error) {
  820. /*
  821. * If we get an error at this point we simply don't
  822. * bother truncating the file.
  823. */
  824. xfs_trans_cancel(tp,
  825. (XFS_TRANS_RELEASE_LOG_RES |
  826. XFS_TRANS_ABORT));
  827. } else {
  828. error = xfs_trans_commit(tp,
  829. XFS_TRANS_RELEASE_LOG_RES);
  830. if (!error)
  831. xfs_inode_clear_eofblocks_tag(ip);
  832. }
  833. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  834. if (need_iolock)
  835. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  836. }
  837. return error;
  838. }
  839. int
  840. xfs_alloc_file_space(
  841. struct xfs_inode *ip,
  842. xfs_off_t offset,
  843. xfs_off_t len,
  844. int alloc_type)
  845. {
  846. xfs_mount_t *mp = ip->i_mount;
  847. xfs_off_t count;
  848. xfs_filblks_t allocated_fsb;
  849. xfs_filblks_t allocatesize_fsb;
  850. xfs_extlen_t extsz, temp;
  851. xfs_fileoff_t startoffset_fsb;
  852. xfs_fsblock_t firstfsb;
  853. int nimaps;
  854. int quota_flag;
  855. int rt;
  856. xfs_trans_t *tp;
  857. xfs_bmbt_irec_t imaps[1], *imapp;
  858. xfs_bmap_free_t free_list;
  859. uint qblocks, resblks, resrtextents;
  860. int committed;
  861. int error;
  862. trace_xfs_alloc_file_space(ip);
  863. if (XFS_FORCED_SHUTDOWN(mp))
  864. return -EIO;
  865. error = xfs_qm_dqattach(ip, 0);
  866. if (error)
  867. return error;
  868. if (len <= 0)
  869. return -EINVAL;
  870. rt = XFS_IS_REALTIME_INODE(ip);
  871. extsz = xfs_get_extsz_hint(ip);
  872. count = len;
  873. imapp = &imaps[0];
  874. nimaps = 1;
  875. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  876. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  877. /*
  878. * Allocate file space until done or until there is an error
  879. */
  880. while (allocatesize_fsb && !error) {
  881. xfs_fileoff_t s, e;
  882. /*
  883. * Determine space reservations for data/realtime.
  884. */
  885. if (unlikely(extsz)) {
  886. s = startoffset_fsb;
  887. do_div(s, extsz);
  888. s *= extsz;
  889. e = startoffset_fsb + allocatesize_fsb;
  890. if ((temp = do_mod(startoffset_fsb, extsz)))
  891. e += temp;
  892. if ((temp = do_mod(e, extsz)))
  893. e += extsz - temp;
  894. } else {
  895. s = 0;
  896. e = allocatesize_fsb;
  897. }
  898. /*
  899. * The transaction reservation is limited to a 32-bit block
  900. * count, hence we need to limit the number of blocks we are
  901. * trying to reserve to avoid an overflow. We can't allocate
  902. * more than @nimaps extents, and an extent is limited on disk
  903. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  904. */
  905. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  906. if (unlikely(rt)) {
  907. resrtextents = qblocks = resblks;
  908. resrtextents /= mp->m_sb.sb_rextsize;
  909. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  910. quota_flag = XFS_QMOPT_RES_RTBLKS;
  911. } else {
  912. resrtextents = 0;
  913. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  914. quota_flag = XFS_QMOPT_RES_REGBLKS;
  915. }
  916. /*
  917. * Allocate and setup the transaction.
  918. */
  919. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  920. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  921. resblks, resrtextents);
  922. /*
  923. * Check for running out of space
  924. */
  925. if (error) {
  926. /*
  927. * Free the transaction structure.
  928. */
  929. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  930. xfs_trans_cancel(tp, 0);
  931. break;
  932. }
  933. xfs_ilock(ip, XFS_ILOCK_EXCL);
  934. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  935. 0, quota_flag);
  936. if (error)
  937. goto error1;
  938. xfs_trans_ijoin(tp, ip, 0);
  939. xfs_bmap_init(&free_list, &firstfsb);
  940. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  941. allocatesize_fsb, alloc_type, &firstfsb,
  942. 0, imapp, &nimaps, &free_list);
  943. if (error) {
  944. goto error0;
  945. }
  946. /*
  947. * Complete the transaction
  948. */
  949. error = xfs_bmap_finish(&tp, &free_list, &committed);
  950. if (error) {
  951. goto error0;
  952. }
  953. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  954. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  955. if (error) {
  956. break;
  957. }
  958. allocated_fsb = imapp->br_blockcount;
  959. if (nimaps == 0) {
  960. error = -ENOSPC;
  961. break;
  962. }
  963. startoffset_fsb += allocated_fsb;
  964. allocatesize_fsb -= allocated_fsb;
  965. }
  966. return error;
  967. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  968. xfs_bmap_cancel(&free_list);
  969. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  970. error1: /* Just cancel transaction */
  971. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  972. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  973. return error;
  974. }
  975. /*
  976. * Zero file bytes between startoff and endoff inclusive.
  977. * The iolock is held exclusive and no blocks are buffered.
  978. *
  979. * This function is used by xfs_free_file_space() to zero
  980. * partial blocks when the range to free is not block aligned.
  981. * When unreserving space with boundaries that are not block
  982. * aligned we round up the start and round down the end
  983. * boundaries and then use this function to zero the parts of
  984. * the blocks that got dropped during the rounding.
  985. */
  986. STATIC int
  987. xfs_zero_remaining_bytes(
  988. xfs_inode_t *ip,
  989. xfs_off_t startoff,
  990. xfs_off_t endoff)
  991. {
  992. xfs_bmbt_irec_t imap;
  993. xfs_fileoff_t offset_fsb;
  994. xfs_off_t lastoffset;
  995. xfs_off_t offset;
  996. xfs_buf_t *bp;
  997. xfs_mount_t *mp = ip->i_mount;
  998. int nimap;
  999. int error = 0;
  1000. /*
  1001. * Avoid doing I/O beyond eof - it's not necessary
  1002. * since nothing can read beyond eof. The space will
  1003. * be zeroed when the file is extended anyway.
  1004. */
  1005. if (startoff >= XFS_ISIZE(ip))
  1006. return 0;
  1007. if (endoff > XFS_ISIZE(ip))
  1008. endoff = XFS_ISIZE(ip);
  1009. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  1010. uint lock_mode;
  1011. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1012. nimap = 1;
  1013. lock_mode = xfs_ilock_data_map_shared(ip);
  1014. error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
  1015. xfs_iunlock(ip, lock_mode);
  1016. if (error || nimap < 1)
  1017. break;
  1018. ASSERT(imap.br_blockcount >= 1);
  1019. ASSERT(imap.br_startoff == offset_fsb);
  1020. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  1021. if (lastoffset > endoff)
  1022. lastoffset = endoff;
  1023. if (imap.br_startblock == HOLESTARTBLOCK)
  1024. continue;
  1025. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1026. if (imap.br_state == XFS_EXT_UNWRITTEN)
  1027. continue;
  1028. error = xfs_buf_read_uncached(XFS_IS_REALTIME_INODE(ip) ?
  1029. mp->m_rtdev_targp : mp->m_ddev_targp,
  1030. xfs_fsb_to_db(ip, imap.br_startblock),
  1031. BTOBB(mp->m_sb.sb_blocksize),
  1032. 0, &bp, NULL);
  1033. if (error)
  1034. return error;
  1035. memset(bp->b_addr +
  1036. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  1037. 0, lastoffset - offset + 1);
  1038. error = xfs_bwrite(bp);
  1039. xfs_buf_relse(bp);
  1040. if (error)
  1041. return error;
  1042. }
  1043. return error;
  1044. }
  1045. int
  1046. xfs_free_file_space(
  1047. struct xfs_inode *ip,
  1048. xfs_off_t offset,
  1049. xfs_off_t len)
  1050. {
  1051. int committed;
  1052. int done;
  1053. xfs_fileoff_t endoffset_fsb;
  1054. int error;
  1055. xfs_fsblock_t firstfsb;
  1056. xfs_bmap_free_t free_list;
  1057. xfs_bmbt_irec_t imap;
  1058. xfs_off_t ioffset;
  1059. xfs_off_t iendoffset;
  1060. xfs_extlen_t mod=0;
  1061. xfs_mount_t *mp;
  1062. int nimap;
  1063. uint resblks;
  1064. xfs_off_t rounding;
  1065. int rt;
  1066. xfs_fileoff_t startoffset_fsb;
  1067. xfs_trans_t *tp;
  1068. mp = ip->i_mount;
  1069. trace_xfs_free_file_space(ip);
  1070. error = xfs_qm_dqattach(ip, 0);
  1071. if (error)
  1072. return error;
  1073. error = 0;
  1074. if (len <= 0) /* if nothing being freed */
  1075. return error;
  1076. rt = XFS_IS_REALTIME_INODE(ip);
  1077. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1078. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1079. /* wait for the completion of any pending DIOs */
  1080. inode_dio_wait(VFS_I(ip));
  1081. rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1082. ioffset = round_down(offset, rounding);
  1083. iendoffset = round_up(offset + len, rounding) - 1;
  1084. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, ioffset,
  1085. iendoffset);
  1086. if (error)
  1087. goto out;
  1088. truncate_pagecache_range(VFS_I(ip), ioffset, iendoffset);
  1089. /*
  1090. * Need to zero the stuff we're not freeing, on disk.
  1091. * If it's a realtime file & can't use unwritten extents then we
  1092. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  1093. * will take care of it for us.
  1094. */
  1095. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1096. nimap = 1;
  1097. error = xfs_bmapi_read(ip, startoffset_fsb, 1,
  1098. &imap, &nimap, 0);
  1099. if (error)
  1100. goto out;
  1101. ASSERT(nimap == 0 || nimap == 1);
  1102. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1103. xfs_daddr_t block;
  1104. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1105. block = imap.br_startblock;
  1106. mod = do_div(block, mp->m_sb.sb_rextsize);
  1107. if (mod)
  1108. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  1109. }
  1110. nimap = 1;
  1111. error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
  1112. &imap, &nimap, 0);
  1113. if (error)
  1114. goto out;
  1115. ASSERT(nimap == 0 || nimap == 1);
  1116. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1117. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1118. mod++;
  1119. if (mod && (mod != mp->m_sb.sb_rextsize))
  1120. endoffset_fsb -= mod;
  1121. }
  1122. }
  1123. if ((done = (endoffset_fsb <= startoffset_fsb)))
  1124. /*
  1125. * One contiguous piece to clear
  1126. */
  1127. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  1128. else {
  1129. /*
  1130. * Some full blocks, possibly two pieces to clear
  1131. */
  1132. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  1133. error = xfs_zero_remaining_bytes(ip, offset,
  1134. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  1135. if (!error &&
  1136. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  1137. error = xfs_zero_remaining_bytes(ip,
  1138. XFS_FSB_TO_B(mp, endoffset_fsb),
  1139. offset + len - 1);
  1140. }
  1141. /*
  1142. * free file space until done or until there is an error
  1143. */
  1144. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1145. while (!error && !done) {
  1146. /*
  1147. * allocate and setup the transaction. Allow this
  1148. * transaction to dip into the reserve blocks to ensure
  1149. * the freeing of the space succeeds at ENOSPC.
  1150. */
  1151. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1152. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write, resblks, 0);
  1153. /*
  1154. * check for running out of space
  1155. */
  1156. if (error) {
  1157. /*
  1158. * Free the transaction structure.
  1159. */
  1160. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1161. xfs_trans_cancel(tp, 0);
  1162. break;
  1163. }
  1164. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1165. error = xfs_trans_reserve_quota(tp, mp,
  1166. ip->i_udquot, ip->i_gdquot, ip->i_pdquot,
  1167. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  1168. if (error)
  1169. goto error1;
  1170. xfs_trans_ijoin(tp, ip, 0);
  1171. /*
  1172. * issue the bunmapi() call to free the blocks
  1173. */
  1174. xfs_bmap_init(&free_list, &firstfsb);
  1175. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  1176. endoffset_fsb - startoffset_fsb,
  1177. 0, 2, &firstfsb, &free_list, &done);
  1178. if (error) {
  1179. goto error0;
  1180. }
  1181. /*
  1182. * complete the transaction
  1183. */
  1184. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1185. if (error) {
  1186. goto error0;
  1187. }
  1188. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1189. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1190. }
  1191. out:
  1192. return error;
  1193. error0:
  1194. xfs_bmap_cancel(&free_list);
  1195. error1:
  1196. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1197. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1198. goto out;
  1199. }
  1200. /*
  1201. * Preallocate and zero a range of a file. This mechanism has the allocation
  1202. * semantics of fallocate and in addition converts data in the range to zeroes.
  1203. */
  1204. int
  1205. xfs_zero_file_space(
  1206. struct xfs_inode *ip,
  1207. xfs_off_t offset,
  1208. xfs_off_t len)
  1209. {
  1210. struct xfs_mount *mp = ip->i_mount;
  1211. uint blksize;
  1212. int error;
  1213. trace_xfs_zero_file_space(ip);
  1214. blksize = 1 << mp->m_sb.sb_blocklog;
  1215. /*
  1216. * Punch a hole and prealloc the range. We use hole punch rather than
  1217. * unwritten extent conversion for two reasons:
  1218. *
  1219. * 1.) Hole punch handles partial block zeroing for us.
  1220. *
  1221. * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
  1222. * by virtue of the hole punch.
  1223. */
  1224. error = xfs_free_file_space(ip, offset, len);
  1225. if (error)
  1226. goto out;
  1227. error = xfs_alloc_file_space(ip, round_down(offset, blksize),
  1228. round_up(offset + len, blksize) -
  1229. round_down(offset, blksize),
  1230. XFS_BMAPI_PREALLOC);
  1231. out:
  1232. return error;
  1233. }
  1234. /*
  1235. * xfs_collapse_file_space()
  1236. * This routine frees disk space and shift extent for the given file.
  1237. * The first thing we do is to free data blocks in the specified range
  1238. * by calling xfs_free_file_space(). It would also sync dirty data
  1239. * and invalidate page cache over the region on which collapse range
  1240. * is working. And Shift extent records to the left to cover a hole.
  1241. * RETURNS:
  1242. * 0 on success
  1243. * errno on error
  1244. *
  1245. */
  1246. int
  1247. xfs_collapse_file_space(
  1248. struct xfs_inode *ip,
  1249. xfs_off_t offset,
  1250. xfs_off_t len)
  1251. {
  1252. int done = 0;
  1253. struct xfs_mount *mp = ip->i_mount;
  1254. struct xfs_trans *tp;
  1255. int error;
  1256. struct xfs_bmap_free free_list;
  1257. xfs_fsblock_t first_block;
  1258. int committed;
  1259. xfs_fileoff_t start_fsb;
  1260. xfs_fileoff_t next_fsb;
  1261. xfs_fileoff_t shift_fsb;
  1262. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1263. trace_xfs_collapse_file_space(ip);
  1264. next_fsb = XFS_B_TO_FSB(mp, offset + len);
  1265. shift_fsb = XFS_B_TO_FSB(mp, len);
  1266. error = xfs_free_file_space(ip, offset, len);
  1267. if (error)
  1268. return error;
  1269. /*
  1270. * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
  1271. * into the accessible region of the file.
  1272. */
  1273. if (xfs_can_free_eofblocks(ip, true)) {
  1274. error = xfs_free_eofblocks(mp, ip, false);
  1275. if (error)
  1276. return error;
  1277. }
  1278. /*
  1279. * Writeback and invalidate cache for the remainder of the file as we're
  1280. * about to shift down every extent from the collapse range to EOF. The
  1281. * free of the collapse range above might have already done some of
  1282. * this, but we shouldn't rely on it to do anything outside of the range
  1283. * that was freed.
  1284. */
  1285. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  1286. offset + len, -1);
  1287. if (error)
  1288. return error;
  1289. error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
  1290. (offset + len) >> PAGE_CACHE_SHIFT, -1);
  1291. if (error)
  1292. return error;
  1293. while (!error && !done) {
  1294. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1295. /*
  1296. * We would need to reserve permanent block for transaction.
  1297. * This will come into picture when after shifting extent into
  1298. * hole we found that adjacent extents can be merged which
  1299. * may lead to freeing of a block during record update.
  1300. */
  1301. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  1302. XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
  1303. if (error) {
  1304. xfs_trans_cancel(tp, 0);
  1305. break;
  1306. }
  1307. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1308. error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
  1309. ip->i_gdquot, ip->i_pdquot,
  1310. XFS_DIOSTRAT_SPACE_RES(mp, 0), 0,
  1311. XFS_QMOPT_RES_REGBLKS);
  1312. if (error)
  1313. goto out;
  1314. xfs_trans_ijoin(tp, ip, 0);
  1315. xfs_bmap_init(&free_list, &first_block);
  1316. /*
  1317. * We are using the write transaction in which max 2 bmbt
  1318. * updates are allowed
  1319. */
  1320. start_fsb = next_fsb;
  1321. error = xfs_bmap_shift_extents(tp, ip, start_fsb, shift_fsb,
  1322. &done, &next_fsb, &first_block, &free_list,
  1323. XFS_BMAP_MAX_SHIFT_EXTENTS);
  1324. if (error)
  1325. goto out;
  1326. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1327. if (error)
  1328. goto out;
  1329. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1330. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1331. }
  1332. return error;
  1333. out:
  1334. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1335. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1336. return error;
  1337. }
  1338. /*
  1339. * We need to check that the format of the data fork in the temporary inode is
  1340. * valid for the target inode before doing the swap. This is not a problem with
  1341. * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
  1342. * data fork depending on the space the attribute fork is taking so we can get
  1343. * invalid formats on the target inode.
  1344. *
  1345. * E.g. target has space for 7 extents in extent format, temp inode only has
  1346. * space for 6. If we defragment down to 7 extents, then the tmp format is a
  1347. * btree, but when swapped it needs to be in extent format. Hence we can't just
  1348. * blindly swap data forks on attr2 filesystems.
  1349. *
  1350. * Note that we check the swap in both directions so that we don't end up with
  1351. * a corrupt temporary inode, either.
  1352. *
  1353. * Note that fixing the way xfs_fsr sets up the attribute fork in the source
  1354. * inode will prevent this situation from occurring, so all we do here is
  1355. * reject and log the attempt. basically we are putting the responsibility on
  1356. * userspace to get this right.
  1357. */
  1358. static int
  1359. xfs_swap_extents_check_format(
  1360. xfs_inode_t *ip, /* target inode */
  1361. xfs_inode_t *tip) /* tmp inode */
  1362. {
  1363. /* Should never get a local format */
  1364. if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
  1365. tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  1366. return -EINVAL;
  1367. /*
  1368. * if the target inode has less extents that then temporary inode then
  1369. * why did userspace call us?
  1370. */
  1371. if (ip->i_d.di_nextents < tip->i_d.di_nextents)
  1372. return -EINVAL;
  1373. /*
  1374. * if the target inode is in extent form and the temp inode is in btree
  1375. * form then we will end up with the target inode in the wrong format
  1376. * as we already know there are less extents in the temp inode.
  1377. */
  1378. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1379. tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1380. return -EINVAL;
  1381. /* Check temp in extent form to max in target */
  1382. if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1383. XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
  1384. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1385. return -EINVAL;
  1386. /* Check target in extent form to max in temp */
  1387. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1388. XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
  1389. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1390. return -EINVAL;
  1391. /*
  1392. * If we are in a btree format, check that the temp root block will fit
  1393. * in the target and that it has enough extents to be in btree format
  1394. * in the target.
  1395. *
  1396. * Note that we have to be careful to allow btree->extent conversions
  1397. * (a common defrag case) which will occur when the temp inode is in
  1398. * extent format...
  1399. */
  1400. if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1401. if (XFS_IFORK_BOFF(ip) &&
  1402. XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
  1403. return -EINVAL;
  1404. if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
  1405. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1406. return -EINVAL;
  1407. }
  1408. /* Reciprocal target->temp btree format checks */
  1409. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1410. if (XFS_IFORK_BOFF(tip) &&
  1411. XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
  1412. return -EINVAL;
  1413. if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
  1414. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1415. return -EINVAL;
  1416. }
  1417. return 0;
  1418. }
  1419. static int
  1420. xfs_swap_extent_flush(
  1421. struct xfs_inode *ip)
  1422. {
  1423. int error;
  1424. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1425. if (error)
  1426. return error;
  1427. truncate_pagecache_range(VFS_I(ip), 0, -1);
  1428. /* Verify O_DIRECT for ftmp */
  1429. if (VFS_I(ip)->i_mapping->nrpages)
  1430. return -EINVAL;
  1431. /*
  1432. * Don't try to swap extents on mmap()d files because we can't lock
  1433. * out races against page faults safely.
  1434. */
  1435. if (mapping_mapped(VFS_I(ip)->i_mapping))
  1436. return -EBUSY;
  1437. return 0;
  1438. }
  1439. int
  1440. xfs_swap_extents(
  1441. xfs_inode_t *ip, /* target inode */
  1442. xfs_inode_t *tip, /* tmp inode */
  1443. xfs_swapext_t *sxp)
  1444. {
  1445. xfs_mount_t *mp = ip->i_mount;
  1446. xfs_trans_t *tp;
  1447. xfs_bstat_t *sbp = &sxp->sx_stat;
  1448. xfs_ifork_t *tempifp, *ifp, *tifp;
  1449. int src_log_flags, target_log_flags;
  1450. int error = 0;
  1451. int aforkblks = 0;
  1452. int taforkblks = 0;
  1453. __uint64_t tmp;
  1454. int lock_flags;
  1455. tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
  1456. if (!tempifp) {
  1457. error = -ENOMEM;
  1458. goto out;
  1459. }
  1460. /*
  1461. * Lock up the inodes against other IO and truncate to begin with.
  1462. * Then we can ensure the inodes are flushed and have no page cache
  1463. * safely. Once we have done this we can take the ilocks and do the rest
  1464. * of the checks.
  1465. */
  1466. lock_flags = XFS_IOLOCK_EXCL;
  1467. xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
  1468. /* Verify that both files have the same format */
  1469. if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
  1470. error = -EINVAL;
  1471. goto out_unlock;
  1472. }
  1473. /* Verify both files are either real-time or non-realtime */
  1474. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  1475. error = -EINVAL;
  1476. goto out_unlock;
  1477. }
  1478. error = xfs_swap_extent_flush(ip);
  1479. if (error)
  1480. goto out_unlock;
  1481. error = xfs_swap_extent_flush(tip);
  1482. if (error)
  1483. goto out_unlock;
  1484. tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
  1485. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  1486. if (error) {
  1487. xfs_trans_cancel(tp, 0);
  1488. goto out_unlock;
  1489. }
  1490. xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
  1491. lock_flags |= XFS_ILOCK_EXCL;
  1492. /* Verify all data are being swapped */
  1493. if (sxp->sx_offset != 0 ||
  1494. sxp->sx_length != ip->i_d.di_size ||
  1495. sxp->sx_length != tip->i_d.di_size) {
  1496. error = -EFAULT;
  1497. goto out_trans_cancel;
  1498. }
  1499. trace_xfs_swap_extent_before(ip, 0);
  1500. trace_xfs_swap_extent_before(tip, 1);
  1501. /* check inode formats now that data is flushed */
  1502. error = xfs_swap_extents_check_format(ip, tip);
  1503. if (error) {
  1504. xfs_notice(mp,
  1505. "%s: inode 0x%llx format is incompatible for exchanging.",
  1506. __func__, ip->i_ino);
  1507. goto out_trans_cancel;
  1508. }
  1509. /*
  1510. * Compare the current change & modify times with that
  1511. * passed in. If they differ, we abort this swap.
  1512. * This is the mechanism used to ensure the calling
  1513. * process that the file was not changed out from
  1514. * under it.
  1515. */
  1516. if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
  1517. (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
  1518. (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
  1519. (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
  1520. error = -EBUSY;
  1521. goto out_trans_cancel;
  1522. }
  1523. /*
  1524. * Count the number of extended attribute blocks
  1525. */
  1526. if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
  1527. (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1528. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
  1529. if (error)
  1530. goto out_trans_cancel;
  1531. }
  1532. if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
  1533. (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1534. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
  1535. &taforkblks);
  1536. if (error)
  1537. goto out_trans_cancel;
  1538. }
  1539. xfs_trans_ijoin(tp, ip, lock_flags);
  1540. xfs_trans_ijoin(tp, tip, lock_flags);
  1541. /*
  1542. * Before we've swapped the forks, lets set the owners of the forks
  1543. * appropriately. We have to do this as we are demand paging the btree
  1544. * buffers, and so the validation done on read will expect the owner
  1545. * field to be correctly set. Once we change the owners, we can swap the
  1546. * inode forks.
  1547. *
  1548. * Note the trickiness in setting the log flags - we set the owner log
  1549. * flag on the opposite inode (i.e. the inode we are setting the new
  1550. * owner to be) because once we swap the forks and log that, log
  1551. * recovery is going to see the fork as owned by the swapped inode,
  1552. * not the pre-swapped inodes.
  1553. */
  1554. src_log_flags = XFS_ILOG_CORE;
  1555. target_log_flags = XFS_ILOG_CORE;
  1556. if (ip->i_d.di_version == 3 &&
  1557. ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1558. target_log_flags |= XFS_ILOG_DOWNER;
  1559. error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK,
  1560. tip->i_ino, NULL);
  1561. if (error)
  1562. goto out_trans_cancel;
  1563. }
  1564. if (tip->i_d.di_version == 3 &&
  1565. tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1566. src_log_flags |= XFS_ILOG_DOWNER;
  1567. error = xfs_bmbt_change_owner(tp, tip, XFS_DATA_FORK,
  1568. ip->i_ino, NULL);
  1569. if (error)
  1570. goto out_trans_cancel;
  1571. }
  1572. /*
  1573. * Swap the data forks of the inodes
  1574. */
  1575. ifp = &ip->i_df;
  1576. tifp = &tip->i_df;
  1577. *tempifp = *ifp; /* struct copy */
  1578. *ifp = *tifp; /* struct copy */
  1579. *tifp = *tempifp; /* struct copy */
  1580. /*
  1581. * Fix the on-disk inode values
  1582. */
  1583. tmp = (__uint64_t)ip->i_d.di_nblocks;
  1584. ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
  1585. tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
  1586. tmp = (__uint64_t) ip->i_d.di_nextents;
  1587. ip->i_d.di_nextents = tip->i_d.di_nextents;
  1588. tip->i_d.di_nextents = tmp;
  1589. tmp = (__uint64_t) ip->i_d.di_format;
  1590. ip->i_d.di_format = tip->i_d.di_format;
  1591. tip->i_d.di_format = tmp;
  1592. /*
  1593. * The extents in the source inode could still contain speculative
  1594. * preallocation beyond EOF (e.g. the file is open but not modified
  1595. * while defrag is in progress). In that case, we need to copy over the
  1596. * number of delalloc blocks the data fork in the source inode is
  1597. * tracking beyond EOF so that when the fork is truncated away when the
  1598. * temporary inode is unlinked we don't underrun the i_delayed_blks
  1599. * counter on that inode.
  1600. */
  1601. ASSERT(tip->i_delayed_blks == 0);
  1602. tip->i_delayed_blks = ip->i_delayed_blks;
  1603. ip->i_delayed_blks = 0;
  1604. switch (ip->i_d.di_format) {
  1605. case XFS_DINODE_FMT_EXTENTS:
  1606. /* If the extents fit in the inode, fix the
  1607. * pointer. Otherwise it's already NULL or
  1608. * pointing to the extent.
  1609. */
  1610. if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  1611. ifp->if_u1.if_extents =
  1612. ifp->if_u2.if_inline_ext;
  1613. }
  1614. src_log_flags |= XFS_ILOG_DEXT;
  1615. break;
  1616. case XFS_DINODE_FMT_BTREE:
  1617. ASSERT(ip->i_d.di_version < 3 ||
  1618. (src_log_flags & XFS_ILOG_DOWNER));
  1619. src_log_flags |= XFS_ILOG_DBROOT;
  1620. break;
  1621. }
  1622. switch (tip->i_d.di_format) {
  1623. case XFS_DINODE_FMT_EXTENTS:
  1624. /* If the extents fit in the inode, fix the
  1625. * pointer. Otherwise it's already NULL or
  1626. * pointing to the extent.
  1627. */
  1628. if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  1629. tifp->if_u1.if_extents =
  1630. tifp->if_u2.if_inline_ext;
  1631. }
  1632. target_log_flags |= XFS_ILOG_DEXT;
  1633. break;
  1634. case XFS_DINODE_FMT_BTREE:
  1635. target_log_flags |= XFS_ILOG_DBROOT;
  1636. ASSERT(tip->i_d.di_version < 3 ||
  1637. (target_log_flags & XFS_ILOG_DOWNER));
  1638. break;
  1639. }
  1640. xfs_trans_log_inode(tp, ip, src_log_flags);
  1641. xfs_trans_log_inode(tp, tip, target_log_flags);
  1642. /*
  1643. * If this is a synchronous mount, make sure that the
  1644. * transaction goes to disk before returning to the user.
  1645. */
  1646. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1647. xfs_trans_set_sync(tp);
  1648. error = xfs_trans_commit(tp, 0);
  1649. trace_xfs_swap_extent_after(ip, 0);
  1650. trace_xfs_swap_extent_after(tip, 1);
  1651. out:
  1652. kmem_free(tempifp);
  1653. return error;
  1654. out_unlock:
  1655. xfs_iunlock(ip, lock_flags);
  1656. xfs_iunlock(tip, lock_flags);
  1657. goto out;
  1658. out_trans_cancel:
  1659. xfs_trans_cancel(tp, 0);
  1660. goto out_unlock;
  1661. }