xfs_bmap_util.c 52 KB

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