xfs_bmap_util.c 57 KB

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