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