xfs_bmap_util.c 52 KB

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