xfs_bmap_util.c 49 KB

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