xfs_bmap_util.c 58 KB

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