xfs_bmap_util.c 51 KB

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