xfs_bmap_util.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * Copyright (c) 2012 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_bit.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_trans.h"
  19. #include "xfs_alloc.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_bmap_util.h"
  22. #include "xfs_bmap_btree.h"
  23. #include "xfs_rtalloc.h"
  24. #include "xfs_error.h"
  25. #include "xfs_quota.h"
  26. #include "xfs_trans_space.h"
  27. #include "xfs_trace.h"
  28. #include "xfs_icache.h"
  29. #include "xfs_iomap.h"
  30. #include "xfs_reflink.h"
  31. /* Kernel only BMAP related definitions and functions */
  32. /*
  33. * Convert the given file system block to a disk block. We have to treat it
  34. * differently based on whether the file is a real time file or not, because the
  35. * bmap code does.
  36. */
  37. xfs_daddr_t
  38. xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
  39. {
  40. if (XFS_IS_REALTIME_INODE(ip))
  41. return XFS_FSB_TO_BB(ip->i_mount, fsb);
  42. return XFS_FSB_TO_DADDR(ip->i_mount, fsb);
  43. }
  44. /*
  45. * Routine to zero an extent on disk allocated to the specific inode.
  46. *
  47. * The VFS functions take a linearised filesystem block offset, so we have to
  48. * convert the sparse xfs fsb to the right format first.
  49. * VFS types are real funky, too.
  50. */
  51. int
  52. xfs_zero_extent(
  53. struct xfs_inode *ip,
  54. xfs_fsblock_t start_fsb,
  55. xfs_off_t count_fsb)
  56. {
  57. struct xfs_mount *mp = ip->i_mount;
  58. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  59. xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
  60. sector_t block = XFS_BB_TO_FSBT(mp, sector);
  61. return blkdev_issue_zeroout(target->bt_bdev,
  62. block << (mp->m_super->s_blocksize_bits - 9),
  63. count_fsb << (mp->m_super->s_blocksize_bits - 9),
  64. GFP_NOFS, 0);
  65. }
  66. #ifdef CONFIG_XFS_RT
  67. int
  68. xfs_bmap_rtalloc(
  69. struct xfs_bmalloca *ap)
  70. {
  71. struct xfs_mount *mp = ap->ip->i_mount;
  72. xfs_fileoff_t orig_offset = ap->offset;
  73. xfs_rtblock_t rtb;
  74. xfs_extlen_t prod = 0; /* product factor for allocators */
  75. xfs_extlen_t mod = 0; /* product factor for allocators */
  76. xfs_extlen_t ralen = 0; /* realtime allocation length */
  77. xfs_extlen_t align; /* minimum allocation alignment */
  78. xfs_extlen_t orig_length = ap->length;
  79. xfs_extlen_t minlen = mp->m_sb.sb_rextsize;
  80. xfs_extlen_t raminlen;
  81. bool rtlocked = false;
  82. bool ignore_locality = false;
  83. int error;
  84. align = xfs_get_extsz_hint(ap->ip);
  85. retry:
  86. prod = align / mp->m_sb.sb_rextsize;
  87. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  88. align, 1, ap->eof, 0,
  89. ap->conv, &ap->offset, &ap->length);
  90. if (error)
  91. return error;
  92. ASSERT(ap->length);
  93. ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
  94. /*
  95. * If we shifted the file offset downward to satisfy an extent size
  96. * hint, increase minlen by that amount so that the allocator won't
  97. * give us an allocation that's too short to cover at least one of the
  98. * blocks that the caller asked for.
  99. */
  100. if (ap->offset != orig_offset)
  101. minlen += orig_offset - ap->offset;
  102. /*
  103. * If the offset & length are not perfectly aligned
  104. * then kill prod, it will just get us in trouble.
  105. */
  106. div_u64_rem(ap->offset, align, &mod);
  107. if (mod || ap->length % align)
  108. prod = 1;
  109. /*
  110. * Set ralen to be the actual requested length in rtextents.
  111. */
  112. ralen = ap->length / mp->m_sb.sb_rextsize;
  113. /*
  114. * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that
  115. * we rounded up to it, cut it back so it's valid again.
  116. * Note that if it's a really large request (bigger than
  117. * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't
  118. * adjust the starting point to match it.
  119. */
  120. if (ralen * mp->m_sb.sb_rextsize >= XFS_MAX_BMBT_EXTLEN)
  121. ralen = XFS_MAX_BMBT_EXTLEN / mp->m_sb.sb_rextsize;
  122. /*
  123. * Lock out modifications to both the RT bitmap and summary inodes
  124. */
  125. if (!rtlocked) {
  126. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
  127. xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  128. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
  129. xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  130. rtlocked = true;
  131. }
  132. /*
  133. * If it's an allocation to an empty file at offset 0,
  134. * pick an extent that will space things out in the rt area.
  135. */
  136. if (ap->eof && ap->offset == 0) {
  137. xfs_rtblock_t rtx; /* realtime extent no */
  138. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  139. if (error)
  140. return error;
  141. ap->blkno = rtx * mp->m_sb.sb_rextsize;
  142. } else {
  143. ap->blkno = 0;
  144. }
  145. xfs_bmap_adjacent(ap);
  146. /*
  147. * Realtime allocation, done through xfs_rtallocate_extent.
  148. */
  149. if (ignore_locality)
  150. ap->blkno = 0;
  151. else
  152. do_div(ap->blkno, mp->m_sb.sb_rextsize);
  153. rtb = ap->blkno;
  154. ap->length = ralen;
  155. raminlen = max_t(xfs_extlen_t, 1, minlen / mp->m_sb.sb_rextsize);
  156. error = xfs_rtallocate_extent(ap->tp, ap->blkno, raminlen, ap->length,
  157. &ralen, ap->wasdel, prod, &rtb);
  158. if (error)
  159. return error;
  160. if (rtb != NULLRTBLOCK) {
  161. ap->blkno = rtb * mp->m_sb.sb_rextsize;
  162. ap->length = ralen * mp->m_sb.sb_rextsize;
  163. ap->ip->i_nblocks += ap->length;
  164. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  165. if (ap->wasdel)
  166. ap->ip->i_delayed_blks -= ap->length;
  167. /*
  168. * Adjust the disk quota also. This was reserved
  169. * earlier.
  170. */
  171. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  172. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  173. XFS_TRANS_DQ_RTBCOUNT, ap->length);
  174. return 0;
  175. }
  176. if (align > mp->m_sb.sb_rextsize) {
  177. /*
  178. * We previously enlarged the request length to try to satisfy
  179. * an extent size hint. The allocator didn't return anything,
  180. * so reset the parameters to the original values and try again
  181. * without alignment criteria.
  182. */
  183. ap->offset = orig_offset;
  184. ap->length = orig_length;
  185. minlen = align = mp->m_sb.sb_rextsize;
  186. goto retry;
  187. }
  188. if (!ignore_locality && ap->blkno != 0) {
  189. /*
  190. * If we can't allocate near a specific rt extent, try again
  191. * without locality criteria.
  192. */
  193. ignore_locality = true;
  194. goto retry;
  195. }
  196. ap->blkno = NULLFSBLOCK;
  197. ap->length = 0;
  198. return 0;
  199. }
  200. #endif /* CONFIG_XFS_RT */
  201. /*
  202. * Extent tree block counting routines.
  203. */
  204. /*
  205. * Count leaf blocks given a range of extent records. Delayed allocation
  206. * extents are not counted towards the totals.
  207. */
  208. xfs_extnum_t
  209. xfs_bmap_count_leaves(
  210. struct xfs_ifork *ifp,
  211. xfs_filblks_t *count)
  212. {
  213. struct xfs_iext_cursor icur;
  214. struct xfs_bmbt_irec got;
  215. xfs_extnum_t numrecs = 0;
  216. for_each_xfs_iext(ifp, &icur, &got) {
  217. if (!isnullstartblock(got.br_startblock)) {
  218. *count += got.br_blockcount;
  219. numrecs++;
  220. }
  221. }
  222. return numrecs;
  223. }
  224. /*
  225. * Count fsblocks of the given fork. Delayed allocation extents are
  226. * not counted towards the totals.
  227. */
  228. int
  229. xfs_bmap_count_blocks(
  230. struct xfs_trans *tp,
  231. struct xfs_inode *ip,
  232. int whichfork,
  233. xfs_extnum_t *nextents,
  234. xfs_filblks_t *count)
  235. {
  236. struct xfs_mount *mp = ip->i_mount;
  237. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  238. struct xfs_btree_cur *cur;
  239. xfs_extlen_t btblocks = 0;
  240. int error;
  241. *nextents = 0;
  242. *count = 0;
  243. if (!ifp)
  244. return 0;
  245. switch (ifp->if_format) {
  246. case XFS_DINODE_FMT_BTREE:
  247. error = xfs_iread_extents(tp, ip, whichfork);
  248. if (error)
  249. return error;
  250. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  251. error = xfs_btree_count_blocks(cur, &btblocks);
  252. xfs_btree_del_cursor(cur, error);
  253. if (error)
  254. return error;
  255. /*
  256. * xfs_btree_count_blocks includes the root block contained in
  257. * the inode fork in @btblocks, so subtract one because we're
  258. * only interested in allocated disk blocks.
  259. */
  260. *count += btblocks - 1;
  261. fallthrough;
  262. case XFS_DINODE_FMT_EXTENTS:
  263. *nextents = xfs_bmap_count_leaves(ifp, count);
  264. break;
  265. }
  266. return 0;
  267. }
  268. static int
  269. xfs_getbmap_report_one(
  270. struct xfs_inode *ip,
  271. struct getbmapx *bmv,
  272. struct kgetbmap *out,
  273. int64_t bmv_end,
  274. struct xfs_bmbt_irec *got)
  275. {
  276. struct kgetbmap *p = out + bmv->bmv_entries;
  277. bool shared = false;
  278. int error;
  279. error = xfs_reflink_trim_around_shared(ip, got, &shared);
  280. if (error)
  281. return error;
  282. if (isnullstartblock(got->br_startblock) ||
  283. got->br_startblock == DELAYSTARTBLOCK) {
  284. /*
  285. * Delalloc extents that start beyond EOF can occur due to
  286. * speculative EOF allocation when the delalloc extent is larger
  287. * than the largest freespace extent at conversion time. These
  288. * extents cannot be converted by data writeback, so can exist
  289. * here even if we are not supposed to be finding delalloc
  290. * extents.
  291. */
  292. if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip)))
  293. ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0);
  294. p->bmv_oflags |= BMV_OF_DELALLOC;
  295. p->bmv_block = -2;
  296. } else {
  297. p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
  298. }
  299. if (got->br_state == XFS_EXT_UNWRITTEN &&
  300. (bmv->bmv_iflags & BMV_IF_PREALLOC))
  301. p->bmv_oflags |= BMV_OF_PREALLOC;
  302. if (shared)
  303. p->bmv_oflags |= BMV_OF_SHARED;
  304. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
  305. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
  306. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  307. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  308. bmv->bmv_entries++;
  309. return 0;
  310. }
  311. static void
  312. xfs_getbmap_report_hole(
  313. struct xfs_inode *ip,
  314. struct getbmapx *bmv,
  315. struct kgetbmap *out,
  316. int64_t bmv_end,
  317. xfs_fileoff_t bno,
  318. xfs_fileoff_t end)
  319. {
  320. struct kgetbmap *p = out + bmv->bmv_entries;
  321. if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
  322. return;
  323. p->bmv_block = -1;
  324. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
  325. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
  326. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  327. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  328. bmv->bmv_entries++;
  329. }
  330. static inline bool
  331. xfs_getbmap_full(
  332. struct getbmapx *bmv)
  333. {
  334. return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
  335. }
  336. static bool
  337. xfs_getbmap_next_rec(
  338. struct xfs_bmbt_irec *rec,
  339. xfs_fileoff_t total_end)
  340. {
  341. xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount;
  342. if (end == total_end)
  343. return false;
  344. rec->br_startoff += rec->br_blockcount;
  345. if (!isnullstartblock(rec->br_startblock) &&
  346. rec->br_startblock != DELAYSTARTBLOCK)
  347. rec->br_startblock += rec->br_blockcount;
  348. rec->br_blockcount = total_end - end;
  349. return true;
  350. }
  351. /*
  352. * Get inode's extents as described in bmv, and format for output.
  353. * Calls formatter to fill the user's buffer until all extents
  354. * are mapped, until the passed-in bmv->bmv_count slots have
  355. * been filled, or until the formatter short-circuits the loop,
  356. * if it is tracking filled-in extents on its own.
  357. */
  358. int /* error code */
  359. xfs_getbmap(
  360. struct xfs_inode *ip,
  361. struct getbmapx *bmv, /* user bmap structure */
  362. struct kgetbmap *out)
  363. {
  364. struct xfs_mount *mp = ip->i_mount;
  365. int iflags = bmv->bmv_iflags;
  366. int whichfork, lock, error = 0;
  367. int64_t bmv_end, max_len;
  368. xfs_fileoff_t bno, first_bno;
  369. struct xfs_ifork *ifp;
  370. struct xfs_bmbt_irec got, rec;
  371. xfs_filblks_t len;
  372. struct xfs_iext_cursor icur;
  373. if (bmv->bmv_iflags & ~BMV_IF_VALID)
  374. return -EINVAL;
  375. #ifndef DEBUG
  376. /* Only allow CoW fork queries if we're debugging. */
  377. if (iflags & BMV_IF_COWFORK)
  378. return -EINVAL;
  379. #endif
  380. if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
  381. return -EINVAL;
  382. if (bmv->bmv_length < -1)
  383. return -EINVAL;
  384. bmv->bmv_entries = 0;
  385. if (bmv->bmv_length == 0)
  386. return 0;
  387. if (iflags & BMV_IF_ATTRFORK)
  388. whichfork = XFS_ATTR_FORK;
  389. else if (iflags & BMV_IF_COWFORK)
  390. whichfork = XFS_COW_FORK;
  391. else
  392. whichfork = XFS_DATA_FORK;
  393. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  394. switch (whichfork) {
  395. case XFS_ATTR_FORK:
  396. lock = xfs_ilock_attr_map_shared(ip);
  397. if (!xfs_inode_has_attr_fork(ip))
  398. goto out_unlock_ilock;
  399. max_len = 1LL << 32;
  400. break;
  401. case XFS_COW_FORK:
  402. lock = XFS_ILOCK_SHARED;
  403. xfs_ilock(ip, lock);
  404. /* No CoW fork? Just return */
  405. if (!xfs_ifork_ptr(ip, whichfork))
  406. goto out_unlock_ilock;
  407. if (xfs_get_cowextsz_hint(ip))
  408. max_len = mp->m_super->s_maxbytes;
  409. else
  410. max_len = XFS_ISIZE(ip);
  411. break;
  412. case XFS_DATA_FORK:
  413. if (!(iflags & BMV_IF_DELALLOC) &&
  414. (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
  415. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  416. if (error)
  417. goto out_unlock_iolock;
  418. /*
  419. * Even after flushing the inode, there can still be
  420. * delalloc blocks on the inode beyond EOF due to
  421. * speculative preallocation. These are not removed
  422. * until the release function is called or the inode
  423. * is inactivated. Hence we cannot assert here that
  424. * ip->i_delayed_blks == 0.
  425. */
  426. }
  427. if (xfs_get_extsz_hint(ip) ||
  428. (ip->i_diflags &
  429. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
  430. max_len = mp->m_super->s_maxbytes;
  431. else
  432. max_len = XFS_ISIZE(ip);
  433. lock = xfs_ilock_data_map_shared(ip);
  434. break;
  435. }
  436. ifp = xfs_ifork_ptr(ip, whichfork);
  437. switch (ifp->if_format) {
  438. case XFS_DINODE_FMT_EXTENTS:
  439. case XFS_DINODE_FMT_BTREE:
  440. break;
  441. case XFS_DINODE_FMT_LOCAL:
  442. /* Local format inode forks report no extents. */
  443. goto out_unlock_ilock;
  444. default:
  445. error = -EINVAL;
  446. goto out_unlock_ilock;
  447. }
  448. if (bmv->bmv_length == -1) {
  449. max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
  450. bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
  451. }
  452. bmv_end = bmv->bmv_offset + bmv->bmv_length;
  453. first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
  454. len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
  455. error = xfs_iread_extents(NULL, ip, whichfork);
  456. if (error)
  457. goto out_unlock_ilock;
  458. if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
  459. /*
  460. * Report a whole-file hole if the delalloc flag is set to
  461. * stay compatible with the old implementation.
  462. */
  463. if (iflags & BMV_IF_DELALLOC)
  464. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  465. XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
  466. goto out_unlock_ilock;
  467. }
  468. while (!xfs_getbmap_full(bmv)) {
  469. xfs_trim_extent(&got, first_bno, len);
  470. /*
  471. * Report an entry for a hole if this extent doesn't directly
  472. * follow the previous one.
  473. */
  474. if (got.br_startoff > bno) {
  475. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  476. got.br_startoff);
  477. if (xfs_getbmap_full(bmv))
  478. break;
  479. }
  480. /*
  481. * In order to report shared extents accurately, we report each
  482. * distinct shared / unshared part of a single bmbt record with
  483. * an individual getbmapx record.
  484. */
  485. bno = got.br_startoff + got.br_blockcount;
  486. rec = got;
  487. do {
  488. error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
  489. &rec);
  490. if (error || xfs_getbmap_full(bmv))
  491. goto out_unlock_ilock;
  492. } while (xfs_getbmap_next_rec(&rec, bno));
  493. if (!xfs_iext_next_extent(ifp, &icur, &got)) {
  494. xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  495. out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST;
  496. if (whichfork != XFS_ATTR_FORK && bno < end &&
  497. !xfs_getbmap_full(bmv)) {
  498. xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
  499. bno, end);
  500. }
  501. break;
  502. }
  503. if (bno >= first_bno + len)
  504. break;
  505. }
  506. out_unlock_ilock:
  507. xfs_iunlock(ip, lock);
  508. out_unlock_iolock:
  509. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  510. return error;
  511. }
  512. /*
  513. * Dead simple method of punching delalyed allocation blocks from a range in
  514. * the inode. This will always punch out both the start and end blocks, even
  515. * if the ranges only partially overlap them, so it is up to the caller to
  516. * ensure that partial blocks are not passed in.
  517. */
  518. int
  519. xfs_bmap_punch_delalloc_range(
  520. struct xfs_inode *ip,
  521. xfs_fileoff_t start_fsb,
  522. xfs_fileoff_t length)
  523. {
  524. struct xfs_ifork *ifp = &ip->i_df;
  525. xfs_fileoff_t end_fsb = start_fsb + length;
  526. struct xfs_bmbt_irec got, del;
  527. struct xfs_iext_cursor icur;
  528. int error = 0;
  529. ASSERT(!xfs_need_iread_extents(ifp));
  530. xfs_ilock(ip, XFS_ILOCK_EXCL);
  531. if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
  532. goto out_unlock;
  533. while (got.br_startoff + got.br_blockcount > start_fsb) {
  534. del = got;
  535. xfs_trim_extent(&del, start_fsb, length);
  536. /*
  537. * A delete can push the cursor forward. Step back to the
  538. * previous extent on non-delalloc or extents outside the
  539. * target range.
  540. */
  541. if (!del.br_blockcount ||
  542. !isnullstartblock(del.br_startblock)) {
  543. if (!xfs_iext_prev_extent(ifp, &icur, &got))
  544. break;
  545. continue;
  546. }
  547. error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
  548. &got, &del);
  549. if (error || !xfs_iext_get_extent(ifp, &icur, &got))
  550. break;
  551. }
  552. out_unlock:
  553. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  554. return error;
  555. }
  556. /*
  557. * Test whether it is appropriate to check an inode for and free post EOF
  558. * blocks. The 'force' parameter determines whether we should also consider
  559. * regular files that are marked preallocated or append-only.
  560. */
  561. bool
  562. xfs_can_free_eofblocks(
  563. struct xfs_inode *ip,
  564. bool force)
  565. {
  566. struct xfs_bmbt_irec imap;
  567. struct xfs_mount *mp = ip->i_mount;
  568. xfs_fileoff_t end_fsb;
  569. xfs_fileoff_t last_fsb;
  570. int nimaps = 1;
  571. int error;
  572. /*
  573. * Caller must either hold the exclusive io lock; or be inactivating
  574. * the inode, which guarantees there are no other users of the inode.
  575. */
  576. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL) ||
  577. (VFS_I(ip)->i_state & I_FREEING));
  578. /* prealloc/delalloc exists only on regular files */
  579. if (!S_ISREG(VFS_I(ip)->i_mode))
  580. return false;
  581. /*
  582. * Zero sized files with no cached pages and delalloc blocks will not
  583. * have speculative prealloc/delalloc blocks to remove.
  584. */
  585. if (VFS_I(ip)->i_size == 0 &&
  586. VFS_I(ip)->i_mapping->nrpages == 0 &&
  587. ip->i_delayed_blks == 0)
  588. return false;
  589. /* If we haven't read in the extent list, then don't do it now. */
  590. if (xfs_need_iread_extents(&ip->i_df))
  591. return false;
  592. /*
  593. * Do not free real preallocated or append-only files unless the file
  594. * has delalloc blocks and we are forced to remove them.
  595. */
  596. if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  597. if (!force || ip->i_delayed_blks == 0)
  598. return false;
  599. /*
  600. * Do not try to free post-EOF blocks if EOF is beyond the end of the
  601. * range supported by the page cache, because the truncation will loop
  602. * forever.
  603. */
  604. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  605. if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1)
  606. end_fsb = roundup_64(end_fsb, mp->m_sb.sb_rextsize);
  607. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  608. if (last_fsb <= end_fsb)
  609. return false;
  610. /*
  611. * Look up the mapping for the first block past EOF. If we can't find
  612. * it, there's nothing to free.
  613. */
  614. xfs_ilock(ip, XFS_ILOCK_SHARED);
  615. error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps,
  616. 0);
  617. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  618. if (error || nimaps == 0)
  619. return false;
  620. /*
  621. * If there's a real mapping there or there are delayed allocation
  622. * reservations, then we have post-EOF blocks to try to free.
  623. */
  624. return imap.br_startblock != HOLESTARTBLOCK || ip->i_delayed_blks;
  625. }
  626. /*
  627. * This is called to free any blocks beyond eof. The caller must hold
  628. * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
  629. * reference to the inode.
  630. */
  631. int
  632. xfs_free_eofblocks(
  633. struct xfs_inode *ip)
  634. {
  635. struct xfs_trans *tp;
  636. struct xfs_mount *mp = ip->i_mount;
  637. int error;
  638. /* Attach the dquots to the inode up front. */
  639. error = xfs_qm_dqattach(ip);
  640. if (error)
  641. return error;
  642. /* Wait on dio to ensure i_size has settled. */
  643. inode_dio_wait(VFS_I(ip));
  644. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  645. if (error) {
  646. ASSERT(xfs_is_shutdown(mp));
  647. return error;
  648. }
  649. xfs_ilock(ip, XFS_ILOCK_EXCL);
  650. xfs_trans_ijoin(tp, ip, 0);
  651. /*
  652. * Do not update the on-disk file size. If we update the on-disk file
  653. * size and then the system crashes before the contents of the file are
  654. * flushed to disk then the files may be full of holes (ie NULL files
  655. * bug).
  656. */
  657. error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
  658. XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
  659. if (error)
  660. goto err_cancel;
  661. error = xfs_trans_commit(tp);
  662. if (error)
  663. goto out_unlock;
  664. xfs_inode_clear_eofblocks_tag(ip);
  665. goto out_unlock;
  666. err_cancel:
  667. /*
  668. * If we get an error at this point we simply don't
  669. * bother truncating the file.
  670. */
  671. xfs_trans_cancel(tp);
  672. out_unlock:
  673. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  674. return error;
  675. }
  676. int
  677. xfs_alloc_file_space(
  678. struct xfs_inode *ip,
  679. xfs_off_t offset,
  680. xfs_off_t len)
  681. {
  682. xfs_mount_t *mp = ip->i_mount;
  683. xfs_off_t count;
  684. xfs_filblks_t allocated_fsb;
  685. xfs_filblks_t allocatesize_fsb;
  686. xfs_extlen_t extsz, temp;
  687. xfs_fileoff_t startoffset_fsb;
  688. xfs_fileoff_t endoffset_fsb;
  689. int nimaps;
  690. int rt;
  691. xfs_trans_t *tp;
  692. xfs_bmbt_irec_t imaps[1], *imapp;
  693. int error;
  694. trace_xfs_alloc_file_space(ip);
  695. if (xfs_is_shutdown(mp))
  696. return -EIO;
  697. error = xfs_qm_dqattach(ip);
  698. if (error)
  699. return error;
  700. if (len <= 0)
  701. return -EINVAL;
  702. rt = XFS_IS_REALTIME_INODE(ip);
  703. extsz = xfs_get_extsz_hint(ip);
  704. count = len;
  705. imapp = &imaps[0];
  706. nimaps = 1;
  707. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  708. endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
  709. allocatesize_fsb = endoffset_fsb - startoffset_fsb;
  710. /*
  711. * Allocate file space until done or until there is an error
  712. */
  713. while (allocatesize_fsb && !error) {
  714. xfs_fileoff_t s, e;
  715. unsigned int dblocks, rblocks, resblks;
  716. /*
  717. * Determine space reservations for data/realtime.
  718. */
  719. if (unlikely(extsz)) {
  720. s = startoffset_fsb;
  721. do_div(s, extsz);
  722. s *= extsz;
  723. e = startoffset_fsb + allocatesize_fsb;
  724. div_u64_rem(startoffset_fsb, extsz, &temp);
  725. if (temp)
  726. e += temp;
  727. div_u64_rem(e, extsz, &temp);
  728. if (temp)
  729. e += extsz - temp;
  730. } else {
  731. s = 0;
  732. e = allocatesize_fsb;
  733. }
  734. /*
  735. * The transaction reservation is limited to a 32-bit block
  736. * count, hence we need to limit the number of blocks we are
  737. * trying to reserve to avoid an overflow. We can't allocate
  738. * more than @nimaps extents, and an extent is limited on disk
  739. * to XFS_BMBT_MAX_EXTLEN (21 bits), so use that to enforce the
  740. * limit.
  741. */
  742. resblks = min_t(xfs_fileoff_t, (e - s),
  743. (XFS_MAX_BMBT_EXTLEN * nimaps));
  744. if (unlikely(rt)) {
  745. dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  746. rblocks = resblks;
  747. } else {
  748. dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  749. rblocks = 0;
  750. }
  751. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
  752. dblocks, rblocks, false, &tp);
  753. if (error)
  754. break;
  755. error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
  756. XFS_IEXT_ADD_NOSPLIT_CNT);
  757. if (error == -EFBIG)
  758. error = xfs_iext_count_upgrade(tp, ip,
  759. XFS_IEXT_ADD_NOSPLIT_CNT);
  760. if (error)
  761. goto error;
  762. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  763. allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
  764. &nimaps);
  765. if (error)
  766. goto error;
  767. ip->i_diflags |= XFS_DIFLAG_PREALLOC;
  768. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  769. error = xfs_trans_commit(tp);
  770. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  771. if (error)
  772. break;
  773. allocated_fsb = imapp->br_blockcount;
  774. if (nimaps == 0) {
  775. error = -ENOSPC;
  776. break;
  777. }
  778. startoffset_fsb += allocated_fsb;
  779. allocatesize_fsb -= allocated_fsb;
  780. }
  781. return error;
  782. error:
  783. xfs_trans_cancel(tp);
  784. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  785. return error;
  786. }
  787. static int
  788. xfs_unmap_extent(
  789. struct xfs_inode *ip,
  790. xfs_fileoff_t startoffset_fsb,
  791. xfs_filblks_t len_fsb,
  792. int *done)
  793. {
  794. struct xfs_mount *mp = ip->i_mount;
  795. struct xfs_trans *tp;
  796. uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  797. int error;
  798. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
  799. false, &tp);
  800. if (error)
  801. return error;
  802. error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
  803. XFS_IEXT_PUNCH_HOLE_CNT);
  804. if (error == -EFBIG)
  805. error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
  806. if (error)
  807. goto out_trans_cancel;
  808. error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
  809. if (error)
  810. goto out_trans_cancel;
  811. error = xfs_trans_commit(tp);
  812. out_unlock:
  813. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  814. return error;
  815. out_trans_cancel:
  816. xfs_trans_cancel(tp);
  817. goto out_unlock;
  818. }
  819. /* Caller must first wait for the completion of any pending DIOs if required. */
  820. int
  821. xfs_flush_unmap_range(
  822. struct xfs_inode *ip,
  823. xfs_off_t offset,
  824. xfs_off_t len)
  825. {
  826. struct xfs_mount *mp = ip->i_mount;
  827. struct inode *inode = VFS_I(ip);
  828. xfs_off_t rounding, start, end;
  829. int error;
  830. rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE);
  831. start = round_down(offset, rounding);
  832. end = round_up(offset + len, rounding) - 1;
  833. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  834. if (error)
  835. return error;
  836. truncate_pagecache_range(inode, start, end);
  837. return 0;
  838. }
  839. int
  840. xfs_free_file_space(
  841. struct xfs_inode *ip,
  842. xfs_off_t offset,
  843. xfs_off_t len)
  844. {
  845. struct xfs_mount *mp = ip->i_mount;
  846. xfs_fileoff_t startoffset_fsb;
  847. xfs_fileoff_t endoffset_fsb;
  848. int done = 0, error;
  849. trace_xfs_free_file_space(ip);
  850. error = xfs_qm_dqattach(ip);
  851. if (error)
  852. return error;
  853. if (len <= 0) /* if nothing being freed */
  854. return 0;
  855. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  856. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  857. /* We can only free complete realtime extents. */
  858. if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) {
  859. startoffset_fsb = roundup_64(startoffset_fsb,
  860. mp->m_sb.sb_rextsize);
  861. endoffset_fsb = rounddown_64(endoffset_fsb,
  862. mp->m_sb.sb_rextsize);
  863. }
  864. /*
  865. * Need to zero the stuff we're not freeing, on disk.
  866. */
  867. if (endoffset_fsb > startoffset_fsb) {
  868. while (!done) {
  869. error = xfs_unmap_extent(ip, startoffset_fsb,
  870. endoffset_fsb - startoffset_fsb, &done);
  871. if (error)
  872. return error;
  873. }
  874. }
  875. /*
  876. * Now that we've unmap all full blocks we'll have to zero out any
  877. * partial block at the beginning and/or end. xfs_zero_range is smart
  878. * enough to skip any holes, including those we just created, but we
  879. * must take care not to zero beyond EOF and enlarge i_size.
  880. */
  881. if (offset >= XFS_ISIZE(ip))
  882. return 0;
  883. if (offset + len > XFS_ISIZE(ip))
  884. len = XFS_ISIZE(ip) - offset;
  885. error = xfs_zero_range(ip, offset, len, NULL);
  886. if (error)
  887. return error;
  888. /*
  889. * If we zeroed right up to EOF and EOF straddles a page boundary we
  890. * must make sure that the post-EOF area is also zeroed because the
  891. * page could be mmap'd and xfs_zero_range doesn't do that for us.
  892. * Writeback of the eof page will do this, albeit clumsily.
  893. */
  894. if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
  895. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  896. round_down(offset + len, PAGE_SIZE), LLONG_MAX);
  897. }
  898. return error;
  899. }
  900. static int
  901. xfs_prepare_shift(
  902. struct xfs_inode *ip,
  903. loff_t offset)
  904. {
  905. struct xfs_mount *mp = ip->i_mount;
  906. int error;
  907. /*
  908. * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
  909. * into the accessible region of the file.
  910. */
  911. if (xfs_can_free_eofblocks(ip, true)) {
  912. error = xfs_free_eofblocks(ip);
  913. if (error)
  914. return error;
  915. }
  916. /*
  917. * Shift operations must stabilize the start block offset boundary along
  918. * with the full range of the operation. If we don't, a COW writeback
  919. * completion could race with an insert, front merge with the start
  920. * extent (after split) during the shift and corrupt the file. Start
  921. * with the block just prior to the start to stabilize the boundary.
  922. */
  923. offset = round_down(offset, mp->m_sb.sb_blocksize);
  924. if (offset)
  925. offset -= mp->m_sb.sb_blocksize;
  926. /*
  927. * Writeback and invalidate cache for the remainder of the file as we're
  928. * about to shift down every extent from offset to EOF.
  929. */
  930. error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
  931. if (error)
  932. return error;
  933. /*
  934. * Clean out anything hanging around in the cow fork now that
  935. * we've flushed all the dirty data out to disk to avoid having
  936. * CoW extents at the wrong offsets.
  937. */
  938. if (xfs_inode_has_cow_data(ip)) {
  939. error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
  940. true);
  941. if (error)
  942. return error;
  943. }
  944. return 0;
  945. }
  946. /*
  947. * xfs_collapse_file_space()
  948. * This routine frees disk space and shift extent for the given file.
  949. * The first thing we do is to free data blocks in the specified range
  950. * by calling xfs_free_file_space(). It would also sync dirty data
  951. * and invalidate page cache over the region on which collapse range
  952. * is working. And Shift extent records to the left to cover a hole.
  953. * RETURNS:
  954. * 0 on success
  955. * errno on error
  956. *
  957. */
  958. int
  959. xfs_collapse_file_space(
  960. struct xfs_inode *ip,
  961. xfs_off_t offset,
  962. xfs_off_t len)
  963. {
  964. struct xfs_mount *mp = ip->i_mount;
  965. struct xfs_trans *tp;
  966. int error;
  967. xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len);
  968. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  969. bool done = false;
  970. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  971. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  972. trace_xfs_collapse_file_space(ip);
  973. error = xfs_free_file_space(ip, offset, len);
  974. if (error)
  975. return error;
  976. error = xfs_prepare_shift(ip, offset);
  977. if (error)
  978. return error;
  979. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
  980. if (error)
  981. return error;
  982. xfs_ilock(ip, XFS_ILOCK_EXCL);
  983. xfs_trans_ijoin(tp, ip, 0);
  984. while (!done) {
  985. error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
  986. &done);
  987. if (error)
  988. goto out_trans_cancel;
  989. if (done)
  990. break;
  991. /* finish any deferred frees and roll the transaction */
  992. error = xfs_defer_finish(&tp);
  993. if (error)
  994. goto out_trans_cancel;
  995. }
  996. error = xfs_trans_commit(tp);
  997. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  998. return error;
  999. out_trans_cancel:
  1000. xfs_trans_cancel(tp);
  1001. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1002. return error;
  1003. }
  1004. /*
  1005. * xfs_insert_file_space()
  1006. * This routine create hole space by shifting extents for the given file.
  1007. * The first thing we do is to sync dirty data and invalidate page cache
  1008. * over the region on which insert range is working. And split an extent
  1009. * to two extents at given offset by calling xfs_bmap_split_extent.
  1010. * And shift all extent records which are laying between [offset,
  1011. * last allocated extent] to the right to reserve hole range.
  1012. * RETURNS:
  1013. * 0 on success
  1014. * errno on error
  1015. */
  1016. int
  1017. xfs_insert_file_space(
  1018. struct xfs_inode *ip,
  1019. loff_t offset,
  1020. loff_t len)
  1021. {
  1022. struct xfs_mount *mp = ip->i_mount;
  1023. struct xfs_trans *tp;
  1024. int error;
  1025. xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset);
  1026. xfs_fileoff_t next_fsb = NULLFSBLOCK;
  1027. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  1028. bool done = false;
  1029. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1030. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  1031. trace_xfs_insert_file_space(ip);
  1032. error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
  1033. if (error)
  1034. return error;
  1035. error = xfs_prepare_shift(ip, offset);
  1036. if (error)
  1037. return error;
  1038. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
  1039. XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
  1040. if (error)
  1041. return error;
  1042. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1043. xfs_trans_ijoin(tp, ip, 0);
  1044. error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
  1045. XFS_IEXT_PUNCH_HOLE_CNT);
  1046. if (error == -EFBIG)
  1047. error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
  1048. if (error)
  1049. goto out_trans_cancel;
  1050. /*
  1051. * The extent shifting code works on extent granularity. So, if stop_fsb
  1052. * is not the starting block of extent, we need to split the extent at
  1053. * stop_fsb.
  1054. */
  1055. error = xfs_bmap_split_extent(tp, ip, stop_fsb);
  1056. if (error)
  1057. goto out_trans_cancel;
  1058. do {
  1059. error = xfs_defer_finish(&tp);
  1060. if (error)
  1061. goto out_trans_cancel;
  1062. error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
  1063. &done, stop_fsb);
  1064. if (error)
  1065. goto out_trans_cancel;
  1066. } while (!done);
  1067. error = xfs_trans_commit(tp);
  1068. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1069. return error;
  1070. out_trans_cancel:
  1071. xfs_trans_cancel(tp);
  1072. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1073. return error;
  1074. }
  1075. /*
  1076. * We need to check that the format of the data fork in the temporary inode is
  1077. * valid for the target inode before doing the swap. This is not a problem with
  1078. * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
  1079. * data fork depending on the space the attribute fork is taking so we can get
  1080. * invalid formats on the target inode.
  1081. *
  1082. * E.g. target has space for 7 extents in extent format, temp inode only has
  1083. * space for 6. If we defragment down to 7 extents, then the tmp format is a
  1084. * btree, but when swapped it needs to be in extent format. Hence we can't just
  1085. * blindly swap data forks on attr2 filesystems.
  1086. *
  1087. * Note that we check the swap in both directions so that we don't end up with
  1088. * a corrupt temporary inode, either.
  1089. *
  1090. * Note that fixing the way xfs_fsr sets up the attribute fork in the source
  1091. * inode will prevent this situation from occurring, so all we do here is
  1092. * reject and log the attempt. basically we are putting the responsibility on
  1093. * userspace to get this right.
  1094. */
  1095. static int
  1096. xfs_swap_extents_check_format(
  1097. struct xfs_inode *ip, /* target inode */
  1098. struct xfs_inode *tip) /* tmp inode */
  1099. {
  1100. struct xfs_ifork *ifp = &ip->i_df;
  1101. struct xfs_ifork *tifp = &tip->i_df;
  1102. /* User/group/project quota ids must match if quotas are enforced. */
  1103. if (XFS_IS_QUOTA_ON(ip->i_mount) &&
  1104. (!uid_eq(VFS_I(ip)->i_uid, VFS_I(tip)->i_uid) ||
  1105. !gid_eq(VFS_I(ip)->i_gid, VFS_I(tip)->i_gid) ||
  1106. ip->i_projid != tip->i_projid))
  1107. return -EINVAL;
  1108. /* Should never get a local format */
  1109. if (ifp->if_format == XFS_DINODE_FMT_LOCAL ||
  1110. tifp->if_format == XFS_DINODE_FMT_LOCAL)
  1111. return -EINVAL;
  1112. /*
  1113. * if the target inode has less extents that then temporary inode then
  1114. * why did userspace call us?
  1115. */
  1116. if (ifp->if_nextents < tifp->if_nextents)
  1117. return -EINVAL;
  1118. /*
  1119. * If we have to use the (expensive) rmap swap method, we can
  1120. * handle any number of extents and any format.
  1121. */
  1122. if (xfs_has_rmapbt(ip->i_mount))
  1123. return 0;
  1124. /*
  1125. * if the target inode is in extent form and the temp inode is in btree
  1126. * form then we will end up with the target inode in the wrong format
  1127. * as we already know there are less extents in the temp inode.
  1128. */
  1129. if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
  1130. tifp->if_format == XFS_DINODE_FMT_BTREE)
  1131. return -EINVAL;
  1132. /* Check temp in extent form to max in target */
  1133. if (tifp->if_format == XFS_DINODE_FMT_EXTENTS &&
  1134. tifp->if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1135. return -EINVAL;
  1136. /* Check target in extent form to max in temp */
  1137. if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
  1138. ifp->if_nextents > XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1139. return -EINVAL;
  1140. /*
  1141. * If we are in a btree format, check that the temp root block will fit
  1142. * in the target and that it has enough extents to be in btree format
  1143. * in the target.
  1144. *
  1145. * Note that we have to be careful to allow btree->extent conversions
  1146. * (a common defrag case) which will occur when the temp inode is in
  1147. * extent format...
  1148. */
  1149. if (tifp->if_format == XFS_DINODE_FMT_BTREE) {
  1150. if (xfs_inode_has_attr_fork(ip) &&
  1151. XFS_BMAP_BMDR_SPACE(tifp->if_broot) > xfs_inode_fork_boff(ip))
  1152. return -EINVAL;
  1153. if (tifp->if_nextents <= XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1154. return -EINVAL;
  1155. }
  1156. /* Reciprocal target->temp btree format checks */
  1157. if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
  1158. if (xfs_inode_has_attr_fork(tip) &&
  1159. XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > xfs_inode_fork_boff(tip))
  1160. return -EINVAL;
  1161. if (ifp->if_nextents <= XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1162. return -EINVAL;
  1163. }
  1164. return 0;
  1165. }
  1166. static int
  1167. xfs_swap_extent_flush(
  1168. struct xfs_inode *ip)
  1169. {
  1170. int error;
  1171. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1172. if (error)
  1173. return error;
  1174. truncate_pagecache_range(VFS_I(ip), 0, -1);
  1175. /* Verify O_DIRECT for ftmp */
  1176. if (VFS_I(ip)->i_mapping->nrpages)
  1177. return -EINVAL;
  1178. return 0;
  1179. }
  1180. /*
  1181. * Move extents from one file to another, when rmap is enabled.
  1182. */
  1183. STATIC int
  1184. xfs_swap_extent_rmap(
  1185. struct xfs_trans **tpp,
  1186. struct xfs_inode *ip,
  1187. struct xfs_inode *tip)
  1188. {
  1189. struct xfs_trans *tp = *tpp;
  1190. struct xfs_bmbt_irec irec;
  1191. struct xfs_bmbt_irec uirec;
  1192. struct xfs_bmbt_irec tirec;
  1193. xfs_fileoff_t offset_fsb;
  1194. xfs_fileoff_t end_fsb;
  1195. xfs_filblks_t count_fsb;
  1196. int error;
  1197. xfs_filblks_t ilen;
  1198. xfs_filblks_t rlen;
  1199. int nimaps;
  1200. uint64_t tip_flags2;
  1201. /*
  1202. * If the source file has shared blocks, we must flag the donor
  1203. * file as having shared blocks so that we get the shared-block
  1204. * rmap functions when we go to fix up the rmaps. The flags
  1205. * will be switch for reals later.
  1206. */
  1207. tip_flags2 = tip->i_diflags2;
  1208. if (ip->i_diflags2 & XFS_DIFLAG2_REFLINK)
  1209. tip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
  1210. offset_fsb = 0;
  1211. end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
  1212. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  1213. while (count_fsb) {
  1214. /* Read extent from the donor file */
  1215. nimaps = 1;
  1216. error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
  1217. &nimaps, 0);
  1218. if (error)
  1219. goto out;
  1220. ASSERT(nimaps == 1);
  1221. ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
  1222. trace_xfs_swap_extent_rmap_remap(tip, &tirec);
  1223. ilen = tirec.br_blockcount;
  1224. /* Unmap the old blocks in the source file. */
  1225. while (tirec.br_blockcount) {
  1226. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  1227. trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
  1228. /* Read extent from the source file */
  1229. nimaps = 1;
  1230. error = xfs_bmapi_read(ip, tirec.br_startoff,
  1231. tirec.br_blockcount, &irec,
  1232. &nimaps, 0);
  1233. if (error)
  1234. goto out;
  1235. ASSERT(nimaps == 1);
  1236. ASSERT(tirec.br_startoff == irec.br_startoff);
  1237. trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
  1238. /* Trim the extent. */
  1239. uirec = tirec;
  1240. uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
  1241. tirec.br_blockcount,
  1242. irec.br_blockcount);
  1243. trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
  1244. if (xfs_bmap_is_real_extent(&uirec)) {
  1245. error = xfs_iext_count_may_overflow(ip,
  1246. XFS_DATA_FORK,
  1247. XFS_IEXT_SWAP_RMAP_CNT);
  1248. if (error == -EFBIG)
  1249. error = xfs_iext_count_upgrade(tp, ip,
  1250. XFS_IEXT_SWAP_RMAP_CNT);
  1251. if (error)
  1252. goto out;
  1253. }
  1254. if (xfs_bmap_is_real_extent(&irec)) {
  1255. error = xfs_iext_count_may_overflow(tip,
  1256. XFS_DATA_FORK,
  1257. XFS_IEXT_SWAP_RMAP_CNT);
  1258. if (error == -EFBIG)
  1259. error = xfs_iext_count_upgrade(tp, ip,
  1260. XFS_IEXT_SWAP_RMAP_CNT);
  1261. if (error)
  1262. goto out;
  1263. }
  1264. /* Remove the mapping from the donor file. */
  1265. xfs_bmap_unmap_extent(tp, tip, &uirec);
  1266. /* Remove the mapping from the source file. */
  1267. xfs_bmap_unmap_extent(tp, ip, &irec);
  1268. /* Map the donor file's blocks into the source file. */
  1269. xfs_bmap_map_extent(tp, ip, &uirec);
  1270. /* Map the source file's blocks into the donor file. */
  1271. xfs_bmap_map_extent(tp, tip, &irec);
  1272. error = xfs_defer_finish(tpp);
  1273. tp = *tpp;
  1274. if (error)
  1275. goto out;
  1276. tirec.br_startoff += rlen;
  1277. if (tirec.br_startblock != HOLESTARTBLOCK &&
  1278. tirec.br_startblock != DELAYSTARTBLOCK)
  1279. tirec.br_startblock += rlen;
  1280. tirec.br_blockcount -= rlen;
  1281. }
  1282. /* Roll on... */
  1283. count_fsb -= ilen;
  1284. offset_fsb += ilen;
  1285. }
  1286. tip->i_diflags2 = tip_flags2;
  1287. return 0;
  1288. out:
  1289. trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
  1290. tip->i_diflags2 = tip_flags2;
  1291. return error;
  1292. }
  1293. /* Swap the extents of two files by swapping data forks. */
  1294. STATIC int
  1295. xfs_swap_extent_forks(
  1296. struct xfs_trans *tp,
  1297. struct xfs_inode *ip,
  1298. struct xfs_inode *tip,
  1299. int *src_log_flags,
  1300. int *target_log_flags)
  1301. {
  1302. xfs_filblks_t aforkblks = 0;
  1303. xfs_filblks_t taforkblks = 0;
  1304. xfs_extnum_t junk;
  1305. uint64_t tmp;
  1306. int error;
  1307. /*
  1308. * Count the number of extended attribute blocks
  1309. */
  1310. if (xfs_inode_has_attr_fork(ip) && ip->i_af.if_nextents > 0 &&
  1311. ip->i_af.if_format != XFS_DINODE_FMT_LOCAL) {
  1312. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
  1313. &aforkblks);
  1314. if (error)
  1315. return error;
  1316. }
  1317. if (xfs_inode_has_attr_fork(tip) && tip->i_af.if_nextents > 0 &&
  1318. tip->i_af.if_format != XFS_DINODE_FMT_LOCAL) {
  1319. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
  1320. &taforkblks);
  1321. if (error)
  1322. return error;
  1323. }
  1324. /*
  1325. * Btree format (v3) inodes have the inode number stamped in the bmbt
  1326. * block headers. We can't start changing the bmbt blocks until the
  1327. * inode owner change is logged so recovery does the right thing in the
  1328. * event of a crash. Set the owner change log flags now and leave the
  1329. * bmbt scan as the last step.
  1330. */
  1331. if (xfs_has_v3inodes(ip->i_mount)) {
  1332. if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE)
  1333. (*target_log_flags) |= XFS_ILOG_DOWNER;
  1334. if (tip->i_df.if_format == XFS_DINODE_FMT_BTREE)
  1335. (*src_log_flags) |= XFS_ILOG_DOWNER;
  1336. }
  1337. /*
  1338. * Swap the data forks of the inodes
  1339. */
  1340. swap(ip->i_df, tip->i_df);
  1341. /*
  1342. * Fix the on-disk inode values
  1343. */
  1344. tmp = (uint64_t)ip->i_nblocks;
  1345. ip->i_nblocks = tip->i_nblocks - taforkblks + aforkblks;
  1346. tip->i_nblocks = tmp + taforkblks - aforkblks;
  1347. /*
  1348. * The extents in the source inode could still contain speculative
  1349. * preallocation beyond EOF (e.g. the file is open but not modified
  1350. * while defrag is in progress). In that case, we need to copy over the
  1351. * number of delalloc blocks the data fork in the source inode is
  1352. * tracking beyond EOF so that when the fork is truncated away when the
  1353. * temporary inode is unlinked we don't underrun the i_delayed_blks
  1354. * counter on that inode.
  1355. */
  1356. ASSERT(tip->i_delayed_blks == 0);
  1357. tip->i_delayed_blks = ip->i_delayed_blks;
  1358. ip->i_delayed_blks = 0;
  1359. switch (ip->i_df.if_format) {
  1360. case XFS_DINODE_FMT_EXTENTS:
  1361. (*src_log_flags) |= XFS_ILOG_DEXT;
  1362. break;
  1363. case XFS_DINODE_FMT_BTREE:
  1364. ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
  1365. (*src_log_flags & XFS_ILOG_DOWNER));
  1366. (*src_log_flags) |= XFS_ILOG_DBROOT;
  1367. break;
  1368. }
  1369. switch (tip->i_df.if_format) {
  1370. case XFS_DINODE_FMT_EXTENTS:
  1371. (*target_log_flags) |= XFS_ILOG_DEXT;
  1372. break;
  1373. case XFS_DINODE_FMT_BTREE:
  1374. (*target_log_flags) |= XFS_ILOG_DBROOT;
  1375. ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
  1376. (*target_log_flags & XFS_ILOG_DOWNER));
  1377. break;
  1378. }
  1379. return 0;
  1380. }
  1381. /*
  1382. * Fix up the owners of the bmbt blocks to refer to the current inode. The
  1383. * change owner scan attempts to order all modified buffers in the current
  1384. * transaction. In the event of ordered buffer failure, the offending buffer is
  1385. * physically logged as a fallback and the scan returns -EAGAIN. We must roll
  1386. * the transaction in this case to replenish the fallback log reservation and
  1387. * restart the scan. This process repeats until the scan completes.
  1388. */
  1389. static int
  1390. xfs_swap_change_owner(
  1391. struct xfs_trans **tpp,
  1392. struct xfs_inode *ip,
  1393. struct xfs_inode *tmpip)
  1394. {
  1395. int error;
  1396. struct xfs_trans *tp = *tpp;
  1397. do {
  1398. error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
  1399. NULL);
  1400. /* success or fatal error */
  1401. if (error != -EAGAIN)
  1402. break;
  1403. error = xfs_trans_roll(tpp);
  1404. if (error)
  1405. break;
  1406. tp = *tpp;
  1407. /*
  1408. * Redirty both inodes so they can relog and keep the log tail
  1409. * moving forward.
  1410. */
  1411. xfs_trans_ijoin(tp, ip, 0);
  1412. xfs_trans_ijoin(tp, tmpip, 0);
  1413. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1414. xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
  1415. } while (true);
  1416. return error;
  1417. }
  1418. int
  1419. xfs_swap_extents(
  1420. struct xfs_inode *ip, /* target inode */
  1421. struct xfs_inode *tip, /* tmp inode */
  1422. struct xfs_swapext *sxp)
  1423. {
  1424. struct xfs_mount *mp = ip->i_mount;
  1425. struct xfs_trans *tp;
  1426. struct xfs_bstat *sbp = &sxp->sx_stat;
  1427. int src_log_flags, target_log_flags;
  1428. int error = 0;
  1429. uint64_t f;
  1430. int resblks = 0;
  1431. unsigned int flags = 0;
  1432. /*
  1433. * Lock the inodes against other IO, page faults and truncate to
  1434. * begin with. Then we can ensure the inodes are flushed and have no
  1435. * page cache safely. Once we have done this we can take the ilocks and
  1436. * do the rest of the checks.
  1437. */
  1438. lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1439. filemap_invalidate_lock_two(VFS_I(ip)->i_mapping,
  1440. VFS_I(tip)->i_mapping);
  1441. /* Verify that both files have the same format */
  1442. if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
  1443. error = -EINVAL;
  1444. goto out_unlock;
  1445. }
  1446. /* Verify both files are either real-time or non-realtime */
  1447. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  1448. error = -EINVAL;
  1449. goto out_unlock;
  1450. }
  1451. error = xfs_qm_dqattach(ip);
  1452. if (error)
  1453. goto out_unlock;
  1454. error = xfs_qm_dqattach(tip);
  1455. if (error)
  1456. goto out_unlock;
  1457. error = xfs_swap_extent_flush(ip);
  1458. if (error)
  1459. goto out_unlock;
  1460. error = xfs_swap_extent_flush(tip);
  1461. if (error)
  1462. goto out_unlock;
  1463. if (xfs_inode_has_cow_data(tip)) {
  1464. error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true);
  1465. if (error)
  1466. goto out_unlock;
  1467. }
  1468. /*
  1469. * Extent "swapping" with rmap requires a permanent reservation and
  1470. * a block reservation because it's really just a remap operation
  1471. * performed with log redo items!
  1472. */
  1473. if (xfs_has_rmapbt(mp)) {
  1474. int w = XFS_DATA_FORK;
  1475. uint32_t ipnext = ip->i_df.if_nextents;
  1476. uint32_t tipnext = tip->i_df.if_nextents;
  1477. /*
  1478. * Conceptually this shouldn't affect the shape of either bmbt,
  1479. * but since we atomically move extents one by one, we reserve
  1480. * enough space to rebuild both trees.
  1481. */
  1482. resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w);
  1483. resblks += XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w);
  1484. /*
  1485. * If either inode straddles a bmapbt block allocation boundary,
  1486. * the rmapbt algorithm triggers repeated allocs and frees as
  1487. * extents are remapped. This can exhaust the block reservation
  1488. * prematurely and cause shutdown. Return freed blocks to the
  1489. * transaction reservation to counter this behavior.
  1490. */
  1491. flags |= XFS_TRANS_RES_FDBLKS;
  1492. }
  1493. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, flags,
  1494. &tp);
  1495. if (error)
  1496. goto out_unlock;
  1497. /*
  1498. * Lock and join the inodes to the tansaction so that transaction commit
  1499. * or cancel will unlock the inodes from this point onwards.
  1500. */
  1501. xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL);
  1502. xfs_trans_ijoin(tp, ip, 0);
  1503. xfs_trans_ijoin(tp, tip, 0);
  1504. /* Verify all data are being swapped */
  1505. if (sxp->sx_offset != 0 ||
  1506. sxp->sx_length != ip->i_disk_size ||
  1507. sxp->sx_length != tip->i_disk_size) {
  1508. error = -EFAULT;
  1509. goto out_trans_cancel;
  1510. }
  1511. trace_xfs_swap_extent_before(ip, 0);
  1512. trace_xfs_swap_extent_before(tip, 1);
  1513. /* check inode formats now that data is flushed */
  1514. error = xfs_swap_extents_check_format(ip, tip);
  1515. if (error) {
  1516. xfs_notice(mp,
  1517. "%s: inode 0x%llx format is incompatible for exchanging.",
  1518. __func__, ip->i_ino);
  1519. goto out_trans_cancel;
  1520. }
  1521. /*
  1522. * Compare the current change & modify times with that
  1523. * passed in. If they differ, we abort this swap.
  1524. * This is the mechanism used to ensure the calling
  1525. * process that the file was not changed out from
  1526. * under it.
  1527. */
  1528. if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
  1529. (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
  1530. (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
  1531. (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
  1532. error = -EBUSY;
  1533. goto out_trans_cancel;
  1534. }
  1535. /*
  1536. * Note the trickiness in setting the log flags - we set the owner log
  1537. * flag on the opposite inode (i.e. the inode we are setting the new
  1538. * owner to be) because once we swap the forks and log that, log
  1539. * recovery is going to see the fork as owned by the swapped inode,
  1540. * not the pre-swapped inodes.
  1541. */
  1542. src_log_flags = XFS_ILOG_CORE;
  1543. target_log_flags = XFS_ILOG_CORE;
  1544. if (xfs_has_rmapbt(mp))
  1545. error = xfs_swap_extent_rmap(&tp, ip, tip);
  1546. else
  1547. error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
  1548. &target_log_flags);
  1549. if (error)
  1550. goto out_trans_cancel;
  1551. /* Do we have to swap reflink flags? */
  1552. if ((ip->i_diflags2 & XFS_DIFLAG2_REFLINK) ^
  1553. (tip->i_diflags2 & XFS_DIFLAG2_REFLINK)) {
  1554. f = ip->i_diflags2 & XFS_DIFLAG2_REFLINK;
  1555. ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  1556. ip->i_diflags2 |= tip->i_diflags2 & XFS_DIFLAG2_REFLINK;
  1557. tip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  1558. tip->i_diflags2 |= f & XFS_DIFLAG2_REFLINK;
  1559. }
  1560. /* Swap the cow forks. */
  1561. if (xfs_has_reflink(mp)) {
  1562. ASSERT(!ip->i_cowfp ||
  1563. ip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
  1564. ASSERT(!tip->i_cowfp ||
  1565. tip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
  1566. swap(ip->i_cowfp, tip->i_cowfp);
  1567. if (ip->i_cowfp && ip->i_cowfp->if_bytes)
  1568. xfs_inode_set_cowblocks_tag(ip);
  1569. else
  1570. xfs_inode_clear_cowblocks_tag(ip);
  1571. if (tip->i_cowfp && tip->i_cowfp->if_bytes)
  1572. xfs_inode_set_cowblocks_tag(tip);
  1573. else
  1574. xfs_inode_clear_cowblocks_tag(tip);
  1575. }
  1576. xfs_trans_log_inode(tp, ip, src_log_flags);
  1577. xfs_trans_log_inode(tp, tip, target_log_flags);
  1578. /*
  1579. * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
  1580. * have inode number owner values in the bmbt blocks that still refer to
  1581. * the old inode. Scan each bmbt to fix up the owner values with the
  1582. * inode number of the current inode.
  1583. */
  1584. if (src_log_flags & XFS_ILOG_DOWNER) {
  1585. error = xfs_swap_change_owner(&tp, ip, tip);
  1586. if (error)
  1587. goto out_trans_cancel;
  1588. }
  1589. if (target_log_flags & XFS_ILOG_DOWNER) {
  1590. error = xfs_swap_change_owner(&tp, tip, ip);
  1591. if (error)
  1592. goto out_trans_cancel;
  1593. }
  1594. /*
  1595. * If this is a synchronous mount, make sure that the
  1596. * transaction goes to disk before returning to the user.
  1597. */
  1598. if (xfs_has_wsync(mp))
  1599. xfs_trans_set_sync(tp);
  1600. error = xfs_trans_commit(tp);
  1601. trace_xfs_swap_extent_after(ip, 0);
  1602. trace_xfs_swap_extent_after(tip, 1);
  1603. out_unlock_ilock:
  1604. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1605. xfs_iunlock(tip, XFS_ILOCK_EXCL);
  1606. out_unlock:
  1607. filemap_invalidate_unlock_two(VFS_I(ip)->i_mapping,
  1608. VFS_I(tip)->i_mapping);
  1609. unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1610. return error;
  1611. out_trans_cancel:
  1612. xfs_trans_cancel(tp);
  1613. goto out_unlock_ilock;
  1614. }