xfs_bmap_util.c 51 KB

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