xfs_bmap_util.c 53 KB

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