xfs_bmap_util.c 51 KB

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