xfs_bmap_util.c 56 KB

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