xfs_bmap_util.c 54 KB

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