xfs_bmap_util.c 50 KB

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