xfs_bmap_util.c 56 KB

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