xfs_bmap_util.c 52 KB

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