xfs_bmap_util.c 52 KB

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