xfs_bmap_util.c 49 KB

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