xfs_bmap_util.c 52 KB

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