xfs_bmap_util.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  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_defer.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_btree.h"
  31. #include "xfs_trans.h"
  32. #include "xfs_extfree_item.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_bmap_util.h"
  36. #include "xfs_bmap_btree.h"
  37. #include "xfs_rtalloc.h"
  38. #include "xfs_error.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_trans_space.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_icache.h"
  43. #include "xfs_log.h"
  44. #include "xfs_rmap_btree.h"
  45. #include "xfs_iomap.h"
  46. #include "xfs_reflink.h"
  47. #include "xfs_refcount.h"
  48. /* Kernel only BMAP related definitions and functions */
  49. /*
  50. * Convert the given file system block to a disk block. We have to treat it
  51. * differently based on whether the file is a real time file or not, because the
  52. * bmap code does.
  53. */
  54. xfs_daddr_t
  55. xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
  56. {
  57. return (XFS_IS_REALTIME_INODE(ip) ? \
  58. (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
  59. XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
  60. }
  61. /*
  62. * Routine to zero an extent on disk allocated to the specific inode.
  63. *
  64. * The VFS functions take a linearised filesystem block offset, so we have to
  65. * convert the sparse xfs fsb to the right format first.
  66. * VFS types are real funky, too.
  67. */
  68. int
  69. xfs_zero_extent(
  70. struct xfs_inode *ip,
  71. xfs_fsblock_t start_fsb,
  72. xfs_off_t count_fsb)
  73. {
  74. struct xfs_mount *mp = ip->i_mount;
  75. xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
  76. sector_t block = XFS_BB_TO_FSBT(mp, sector);
  77. return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip)),
  78. block << (mp->m_super->s_blocksize_bits - 9),
  79. count_fsb << (mp->m_super->s_blocksize_bits - 9),
  80. GFP_NOFS, 0);
  81. }
  82. #ifdef CONFIG_XFS_RT
  83. int
  84. xfs_bmap_rtalloc(
  85. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  86. {
  87. int error; /* error return value */
  88. xfs_mount_t *mp; /* mount point structure */
  89. xfs_extlen_t prod = 0; /* product factor for allocators */
  90. xfs_extlen_t ralen = 0; /* realtime allocation length */
  91. xfs_extlen_t align; /* minimum allocation alignment */
  92. xfs_rtblock_t rtb;
  93. mp = ap->ip->i_mount;
  94. align = xfs_get_extsz_hint(ap->ip);
  95. prod = align / mp->m_sb.sb_rextsize;
  96. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  97. align, 1, ap->eof, 0,
  98. ap->conv, &ap->offset, &ap->length);
  99. if (error)
  100. return error;
  101. ASSERT(ap->length);
  102. ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
  103. /*
  104. * If the offset & length are not perfectly aligned
  105. * then kill prod, it will just get us in trouble.
  106. */
  107. if (do_mod(ap->offset, align) || 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 MAXEXTLEN 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. * MAXEXTLEN), 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 >= MAXEXTLEN)
  121. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  122. /*
  123. * Lock out modifications to both the RT bitmap and summary inodes
  124. */
  125. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
  126. xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  127. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
  128. xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  129. /*
  130. * If it's an allocation to an empty file at offset 0,
  131. * pick an extent that will space things out in the rt area.
  132. */
  133. if (ap->eof && ap->offset == 0) {
  134. xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
  135. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  136. if (error)
  137. return error;
  138. ap->blkno = rtx * mp->m_sb.sb_rextsize;
  139. } else {
  140. ap->blkno = 0;
  141. }
  142. xfs_bmap_adjacent(ap);
  143. /*
  144. * Realtime allocation, done through xfs_rtallocate_extent.
  145. */
  146. do_div(ap->blkno, mp->m_sb.sb_rextsize);
  147. rtb = ap->blkno;
  148. ap->length = ralen;
  149. error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
  150. &ralen, ap->wasdel, prod, &rtb);
  151. if (error)
  152. return error;
  153. ap->blkno = rtb;
  154. if (ap->blkno != NULLFSBLOCK) {
  155. ap->blkno *= mp->m_sb.sb_rextsize;
  156. ralen *= mp->m_sb.sb_rextsize;
  157. ap->length = ralen;
  158. ap->ip->i_d.di_nblocks += ralen;
  159. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  160. if (ap->wasdel)
  161. ap->ip->i_delayed_blks -= ralen;
  162. /*
  163. * Adjust the disk quota also. This was reserved
  164. * earlier.
  165. */
  166. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  167. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  168. XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
  169. /* Zero the extent if we were asked to do so */
  170. if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) {
  171. error = xfs_zero_extent(ap->ip, ap->blkno, ap->length);
  172. if (error)
  173. return error;
  174. }
  175. } else {
  176. ap->length = 0;
  177. }
  178. return 0;
  179. }
  180. #endif /* CONFIG_XFS_RT */
  181. /*
  182. * Check if the endoff is outside the last extent. If so the caller will grow
  183. * the allocation to a stripe unit boundary. All offsets are considered outside
  184. * the end of file for an empty fork, so 1 is returned in *eof in that case.
  185. */
  186. int
  187. xfs_bmap_eof(
  188. struct xfs_inode *ip,
  189. xfs_fileoff_t endoff,
  190. int whichfork,
  191. int *eof)
  192. {
  193. struct xfs_bmbt_irec rec;
  194. int error;
  195. error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
  196. if (error || *eof)
  197. return error;
  198. *eof = endoff >= rec.br_startoff + rec.br_blockcount;
  199. return 0;
  200. }
  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_bmbt_irec got;
  214. xfs_extnum_t numrecs = 0, i = 0;
  215. while (xfs_iext_get_extent(ifp, i++, &got)) {
  216. if (!isnullstartblock(got.br_startblock)) {
  217. *count += got.br_blockcount;
  218. numrecs++;
  219. }
  220. }
  221. return numrecs;
  222. }
  223. /*
  224. * Count leaf blocks given a range of extent records originally
  225. * in btree format.
  226. */
  227. STATIC void
  228. xfs_bmap_disk_count_leaves(
  229. struct xfs_mount *mp,
  230. struct xfs_btree_block *block,
  231. int numrecs,
  232. xfs_filblks_t *count)
  233. {
  234. int b;
  235. xfs_bmbt_rec_t *frp;
  236. for (b = 1; b <= numrecs; b++) {
  237. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  238. *count += xfs_bmbt_disk_get_blockcount(frp);
  239. }
  240. }
  241. /*
  242. * Recursively walks each level of a btree
  243. * to count total fsblocks in use.
  244. */
  245. STATIC int
  246. xfs_bmap_count_tree(
  247. struct xfs_mount *mp,
  248. struct xfs_trans *tp,
  249. struct xfs_ifork *ifp,
  250. xfs_fsblock_t blockno,
  251. int levelin,
  252. xfs_extnum_t *nextents,
  253. xfs_filblks_t *count)
  254. {
  255. int error;
  256. struct xfs_buf *bp, *nbp;
  257. int level = levelin;
  258. __be64 *pp;
  259. xfs_fsblock_t bno = blockno;
  260. xfs_fsblock_t nextbno;
  261. struct xfs_btree_block *block, *nextblock;
  262. int numrecs;
  263. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
  264. &xfs_bmbt_buf_ops);
  265. if (error)
  266. return error;
  267. *count += 1;
  268. block = XFS_BUF_TO_BLOCK(bp);
  269. if (--level) {
  270. /* Not at node above leaves, count this level of nodes */
  271. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  272. while (nextbno != NULLFSBLOCK) {
  273. error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
  274. XFS_BMAP_BTREE_REF,
  275. &xfs_bmbt_buf_ops);
  276. if (error)
  277. return error;
  278. *count += 1;
  279. nextblock = XFS_BUF_TO_BLOCK(nbp);
  280. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  281. xfs_trans_brelse(tp, nbp);
  282. }
  283. /* Dive to the next level */
  284. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  285. bno = be64_to_cpu(*pp);
  286. error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, nextents,
  287. count);
  288. if (error) {
  289. xfs_trans_brelse(tp, bp);
  290. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  291. XFS_ERRLEVEL_LOW, mp);
  292. return -EFSCORRUPTED;
  293. }
  294. xfs_trans_brelse(tp, bp);
  295. } else {
  296. /* count all level 1 nodes and their leaves */
  297. for (;;) {
  298. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  299. numrecs = be16_to_cpu(block->bb_numrecs);
  300. (*nextents) += numrecs;
  301. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  302. xfs_trans_brelse(tp, bp);
  303. if (nextbno == NULLFSBLOCK)
  304. break;
  305. bno = nextbno;
  306. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  307. XFS_BMAP_BTREE_REF,
  308. &xfs_bmbt_buf_ops);
  309. if (error)
  310. return error;
  311. *count += 1;
  312. block = XFS_BUF_TO_BLOCK(bp);
  313. }
  314. }
  315. return 0;
  316. }
  317. /*
  318. * Count fsblocks of the given fork. Delayed allocation extents are
  319. * not counted towards the totals.
  320. */
  321. int
  322. xfs_bmap_count_blocks(
  323. struct xfs_trans *tp,
  324. struct xfs_inode *ip,
  325. int whichfork,
  326. xfs_extnum_t *nextents,
  327. xfs_filblks_t *count)
  328. {
  329. struct xfs_mount *mp; /* file system mount structure */
  330. __be64 *pp; /* pointer to block address */
  331. struct xfs_btree_block *block; /* current btree block */
  332. struct xfs_ifork *ifp; /* fork structure */
  333. xfs_fsblock_t bno; /* block # of "block" */
  334. int level; /* btree level, for checking */
  335. int error;
  336. bno = NULLFSBLOCK;
  337. mp = ip->i_mount;
  338. *nextents = 0;
  339. *count = 0;
  340. ifp = XFS_IFORK_PTR(ip, whichfork);
  341. if (!ifp)
  342. return 0;
  343. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  344. case XFS_DINODE_FMT_EXTENTS:
  345. *nextents = xfs_bmap_count_leaves(ifp, count);
  346. return 0;
  347. case XFS_DINODE_FMT_BTREE:
  348. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  349. error = xfs_iread_extents(tp, ip, whichfork);
  350. if (error)
  351. return error;
  352. }
  353. /*
  354. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  355. */
  356. block = ifp->if_broot;
  357. level = be16_to_cpu(block->bb_level);
  358. ASSERT(level > 0);
  359. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  360. bno = be64_to_cpu(*pp);
  361. ASSERT(bno != NULLFSBLOCK);
  362. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  363. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  364. error = xfs_bmap_count_tree(mp, tp, ifp, bno, level,
  365. nextents, count);
  366. if (error) {
  367. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)",
  368. XFS_ERRLEVEL_LOW, mp);
  369. return -EFSCORRUPTED;
  370. }
  371. return 0;
  372. }
  373. return 0;
  374. }
  375. static int
  376. xfs_getbmap_report_one(
  377. struct xfs_inode *ip,
  378. struct getbmapx *bmv,
  379. struct kgetbmap *out,
  380. int64_t bmv_end,
  381. struct xfs_bmbt_irec *got)
  382. {
  383. struct kgetbmap *p = out + bmv->bmv_entries;
  384. bool shared = false, trimmed = false;
  385. int error;
  386. error = xfs_reflink_trim_around_shared(ip, got, &shared, &trimmed);
  387. if (error)
  388. return error;
  389. if (isnullstartblock(got->br_startblock) ||
  390. got->br_startblock == DELAYSTARTBLOCK) {
  391. /*
  392. * Delalloc extents that start beyond EOF can occur due to
  393. * speculative EOF allocation when the delalloc extent is larger
  394. * than the largest freespace extent at conversion time. These
  395. * extents cannot be converted by data writeback, so can exist
  396. * here even if we are not supposed to be finding delalloc
  397. * extents.
  398. */
  399. if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip)))
  400. ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0);
  401. p->bmv_oflags |= BMV_OF_DELALLOC;
  402. p->bmv_block = -2;
  403. } else {
  404. p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
  405. }
  406. if (got->br_state == XFS_EXT_UNWRITTEN &&
  407. (bmv->bmv_iflags & BMV_IF_PREALLOC))
  408. p->bmv_oflags |= BMV_OF_PREALLOC;
  409. if (shared)
  410. p->bmv_oflags |= BMV_OF_SHARED;
  411. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
  412. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
  413. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  414. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  415. bmv->bmv_entries++;
  416. return 0;
  417. }
  418. static void
  419. xfs_getbmap_report_hole(
  420. struct xfs_inode *ip,
  421. struct getbmapx *bmv,
  422. struct kgetbmap *out,
  423. int64_t bmv_end,
  424. xfs_fileoff_t bno,
  425. xfs_fileoff_t end)
  426. {
  427. struct kgetbmap *p = out + bmv->bmv_entries;
  428. if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
  429. return;
  430. p->bmv_block = -1;
  431. p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
  432. p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
  433. bmv->bmv_offset = p->bmv_offset + p->bmv_length;
  434. bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
  435. bmv->bmv_entries++;
  436. }
  437. static inline bool
  438. xfs_getbmap_full(
  439. struct getbmapx *bmv)
  440. {
  441. return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
  442. }
  443. static bool
  444. xfs_getbmap_next_rec(
  445. struct xfs_bmbt_irec *rec,
  446. xfs_fileoff_t total_end)
  447. {
  448. xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount;
  449. if (end == total_end)
  450. return false;
  451. rec->br_startoff += rec->br_blockcount;
  452. if (!isnullstartblock(rec->br_startblock) &&
  453. rec->br_startblock != DELAYSTARTBLOCK)
  454. rec->br_startblock += rec->br_blockcount;
  455. rec->br_blockcount = total_end - end;
  456. return true;
  457. }
  458. /*
  459. * Get inode's extents as described in bmv, and format for output.
  460. * Calls formatter to fill the user's buffer until all extents
  461. * are mapped, until the passed-in bmv->bmv_count slots have
  462. * been filled, or until the formatter short-circuits the loop,
  463. * if it is tracking filled-in extents on its own.
  464. */
  465. int /* error code */
  466. xfs_getbmap(
  467. struct xfs_inode *ip,
  468. struct getbmapx *bmv, /* user bmap structure */
  469. struct kgetbmap *out)
  470. {
  471. struct xfs_mount *mp = ip->i_mount;
  472. int iflags = bmv->bmv_iflags;
  473. int whichfork, lock, error = 0;
  474. int64_t bmv_end, max_len;
  475. xfs_fileoff_t bno, first_bno;
  476. struct xfs_ifork *ifp;
  477. struct xfs_bmbt_irec got, rec;
  478. xfs_filblks_t len;
  479. xfs_extnum_t idx;
  480. if (bmv->bmv_iflags & ~BMV_IF_VALID)
  481. return -EINVAL;
  482. #ifndef DEBUG
  483. /* Only allow CoW fork queries if we're debugging. */
  484. if (iflags & BMV_IF_COWFORK)
  485. return -EINVAL;
  486. #endif
  487. if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
  488. return -EINVAL;
  489. if (bmv->bmv_length < -1)
  490. return -EINVAL;
  491. bmv->bmv_entries = 0;
  492. if (bmv->bmv_length == 0)
  493. return 0;
  494. if (iflags & BMV_IF_ATTRFORK)
  495. whichfork = XFS_ATTR_FORK;
  496. else if (iflags & BMV_IF_COWFORK)
  497. whichfork = XFS_COW_FORK;
  498. else
  499. whichfork = XFS_DATA_FORK;
  500. ifp = XFS_IFORK_PTR(ip, whichfork);
  501. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  502. switch (whichfork) {
  503. case XFS_ATTR_FORK:
  504. if (!XFS_IFORK_Q(ip))
  505. goto out_unlock_iolock;
  506. max_len = 1LL << 32;
  507. lock = xfs_ilock_attr_map_shared(ip);
  508. break;
  509. case XFS_COW_FORK:
  510. /* No CoW fork? Just return */
  511. if (!ifp)
  512. goto out_unlock_iolock;
  513. if (xfs_get_cowextsz_hint(ip))
  514. max_len = mp->m_super->s_maxbytes;
  515. else
  516. max_len = XFS_ISIZE(ip);
  517. lock = XFS_ILOCK_SHARED;
  518. xfs_ilock(ip, lock);
  519. break;
  520. case XFS_DATA_FORK:
  521. if (!(iflags & BMV_IF_DELALLOC) &&
  522. (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
  523. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  524. if (error)
  525. goto out_unlock_iolock;
  526. /*
  527. * Even after flushing the inode, there can still be
  528. * delalloc blocks on the inode beyond EOF due to
  529. * speculative preallocation. These are not removed
  530. * until the release function is called or the inode
  531. * is inactivated. Hence we cannot assert here that
  532. * ip->i_delayed_blks == 0.
  533. */
  534. }
  535. if (xfs_get_extsz_hint(ip) ||
  536. (ip->i_d.di_flags &
  537. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
  538. max_len = mp->m_super->s_maxbytes;
  539. else
  540. max_len = XFS_ISIZE(ip);
  541. lock = xfs_ilock_data_map_shared(ip);
  542. break;
  543. }
  544. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  545. case XFS_DINODE_FMT_EXTENTS:
  546. case XFS_DINODE_FMT_BTREE:
  547. break;
  548. case XFS_DINODE_FMT_LOCAL:
  549. /* Local format inode forks report no extents. */
  550. goto out_unlock_ilock;
  551. default:
  552. error = -EINVAL;
  553. goto out_unlock_ilock;
  554. }
  555. if (bmv->bmv_length == -1) {
  556. max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
  557. bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
  558. }
  559. bmv_end = bmv->bmv_offset + bmv->bmv_length;
  560. first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
  561. len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
  562. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  563. error = xfs_iread_extents(NULL, ip, whichfork);
  564. if (error)
  565. goto out_unlock_ilock;
  566. }
  567. if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
  568. /*
  569. * Report a whole-file hole if the delalloc flag is set to
  570. * stay compatible with the old implementation.
  571. */
  572. if (iflags & BMV_IF_DELALLOC)
  573. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  574. XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
  575. goto out_unlock_ilock;
  576. }
  577. while (!xfs_getbmap_full(bmv)) {
  578. xfs_trim_extent(&got, first_bno, len);
  579. /*
  580. * Report an entry for a hole if this extent doesn't directly
  581. * follow the previous one.
  582. */
  583. if (got.br_startoff > bno) {
  584. xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
  585. got.br_startoff);
  586. if (xfs_getbmap_full(bmv))
  587. break;
  588. }
  589. /*
  590. * In order to report shared extents accurately, we report each
  591. * distinct shared / unshared part of a single bmbt record with
  592. * an individual getbmapx record.
  593. */
  594. bno = got.br_startoff + got.br_blockcount;
  595. rec = got;
  596. do {
  597. error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
  598. &rec);
  599. if (error || xfs_getbmap_full(bmv))
  600. goto out_unlock_ilock;
  601. } while (xfs_getbmap_next_rec(&rec, bno));
  602. if (!xfs_iext_get_extent(ifp, ++idx, &got)) {
  603. xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  604. out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST;
  605. if (whichfork != XFS_ATTR_FORK && bno < end &&
  606. !xfs_getbmap_full(bmv)) {
  607. xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
  608. bno, end);
  609. }
  610. break;
  611. }
  612. if (bno >= first_bno + len)
  613. break;
  614. }
  615. out_unlock_ilock:
  616. xfs_iunlock(ip, lock);
  617. out_unlock_iolock:
  618. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  619. return error;
  620. }
  621. /*
  622. * dead simple method of punching delalyed allocation blocks from a range in
  623. * the inode. Walks a block at a time so will be slow, but is only executed in
  624. * rare error cases so the overhead is not critical. This will always punch out
  625. * both the start and end blocks, even if the ranges only partially overlap
  626. * them, so it is up to the caller to ensure that partial blocks are not
  627. * passed in.
  628. */
  629. int
  630. xfs_bmap_punch_delalloc_range(
  631. struct xfs_inode *ip,
  632. xfs_fileoff_t start_fsb,
  633. xfs_fileoff_t length)
  634. {
  635. xfs_fileoff_t remaining = length;
  636. int error = 0;
  637. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  638. do {
  639. int done;
  640. xfs_bmbt_irec_t imap;
  641. int nimaps = 1;
  642. xfs_fsblock_t firstblock;
  643. struct xfs_defer_ops dfops;
  644. /*
  645. * Map the range first and check that it is a delalloc extent
  646. * before trying to unmap the range. Otherwise we will be
  647. * trying to remove a real extent (which requires a
  648. * transaction) or a hole, which is probably a bad idea...
  649. */
  650. error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
  651. XFS_BMAPI_ENTIRE);
  652. if (error) {
  653. /* something screwed, just bail */
  654. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  655. xfs_alert(ip->i_mount,
  656. "Failed delalloc mapping lookup ino %lld fsb %lld.",
  657. ip->i_ino, start_fsb);
  658. }
  659. break;
  660. }
  661. if (!nimaps) {
  662. /* nothing there */
  663. goto next_block;
  664. }
  665. if (imap.br_startblock != DELAYSTARTBLOCK) {
  666. /* been converted, ignore */
  667. goto next_block;
  668. }
  669. WARN_ON(imap.br_blockcount == 0);
  670. /*
  671. * Note: while we initialise the firstblock/dfops pair, they
  672. * should never be used because blocks should never be
  673. * allocated or freed for a delalloc extent and hence we need
  674. * don't cancel or finish them after the xfs_bunmapi() call.
  675. */
  676. xfs_defer_init(&dfops, &firstblock);
  677. error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
  678. &dfops, &done);
  679. if (error)
  680. break;
  681. ASSERT(!xfs_defer_has_unfinished_work(&dfops));
  682. next_block:
  683. start_fsb++;
  684. remaining--;
  685. } while(remaining > 0);
  686. return error;
  687. }
  688. /*
  689. * Test whether it is appropriate to check an inode for and free post EOF
  690. * blocks. The 'force' parameter determines whether we should also consider
  691. * regular files that are marked preallocated or append-only.
  692. */
  693. bool
  694. xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
  695. {
  696. /* prealloc/delalloc exists only on regular files */
  697. if (!S_ISREG(VFS_I(ip)->i_mode))
  698. return false;
  699. /*
  700. * Zero sized files with no cached pages and delalloc blocks will not
  701. * have speculative prealloc/delalloc blocks to remove.
  702. */
  703. if (VFS_I(ip)->i_size == 0 &&
  704. VFS_I(ip)->i_mapping->nrpages == 0 &&
  705. ip->i_delayed_blks == 0)
  706. return false;
  707. /* If we haven't read in the extent list, then don't do it now. */
  708. if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
  709. return false;
  710. /*
  711. * Do not free real preallocated or append-only files unless the file
  712. * has delalloc blocks and we are forced to remove them.
  713. */
  714. if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  715. if (!force || ip->i_delayed_blks == 0)
  716. return false;
  717. return true;
  718. }
  719. /*
  720. * This is called to free any blocks beyond eof. The caller must hold
  721. * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
  722. * reference to the inode.
  723. */
  724. int
  725. xfs_free_eofblocks(
  726. struct xfs_inode *ip)
  727. {
  728. struct xfs_trans *tp;
  729. int error;
  730. xfs_fileoff_t end_fsb;
  731. xfs_fileoff_t last_fsb;
  732. xfs_filblks_t map_len;
  733. int nimaps;
  734. struct xfs_bmbt_irec imap;
  735. struct xfs_mount *mp = ip->i_mount;
  736. /*
  737. * Figure out if there are any blocks beyond the end
  738. * of the file. If not, then there is nothing to do.
  739. */
  740. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  741. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  742. if (last_fsb <= end_fsb)
  743. return 0;
  744. map_len = last_fsb - end_fsb;
  745. nimaps = 1;
  746. xfs_ilock(ip, XFS_ILOCK_SHARED);
  747. error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
  748. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  749. /*
  750. * If there are blocks after the end of file, truncate the file to its
  751. * current size to free them up.
  752. */
  753. if (!error && (nimaps != 0) &&
  754. (imap.br_startblock != HOLESTARTBLOCK ||
  755. ip->i_delayed_blks)) {
  756. /*
  757. * Attach the dquots to the inode up front.
  758. */
  759. error = xfs_qm_dqattach(ip, 0);
  760. if (error)
  761. return error;
  762. /* wait on dio to ensure i_size has settled */
  763. inode_dio_wait(VFS_I(ip));
  764. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
  765. &tp);
  766. if (error) {
  767. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  768. return error;
  769. }
  770. xfs_ilock(ip, XFS_ILOCK_EXCL);
  771. xfs_trans_ijoin(tp, ip, 0);
  772. /*
  773. * Do not update the on-disk file size. If we update the
  774. * on-disk file size and then the system crashes before the
  775. * contents of the file are flushed to disk then the files
  776. * may be full of holes (ie NULL files bug).
  777. */
  778. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
  779. XFS_ISIZE(ip));
  780. if (error) {
  781. /*
  782. * If we get an error at this point we simply don't
  783. * bother truncating the file.
  784. */
  785. xfs_trans_cancel(tp);
  786. } else {
  787. error = xfs_trans_commit(tp);
  788. if (!error)
  789. xfs_inode_clear_eofblocks_tag(ip);
  790. }
  791. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  792. }
  793. return error;
  794. }
  795. int
  796. xfs_alloc_file_space(
  797. struct xfs_inode *ip,
  798. xfs_off_t offset,
  799. xfs_off_t len,
  800. int alloc_type)
  801. {
  802. xfs_mount_t *mp = ip->i_mount;
  803. xfs_off_t count;
  804. xfs_filblks_t allocated_fsb;
  805. xfs_filblks_t allocatesize_fsb;
  806. xfs_extlen_t extsz, temp;
  807. xfs_fileoff_t startoffset_fsb;
  808. xfs_fsblock_t firstfsb;
  809. int nimaps;
  810. int quota_flag;
  811. int rt;
  812. xfs_trans_t *tp;
  813. xfs_bmbt_irec_t imaps[1], *imapp;
  814. struct xfs_defer_ops dfops;
  815. uint qblocks, resblks, resrtextents;
  816. int error;
  817. trace_xfs_alloc_file_space(ip);
  818. if (XFS_FORCED_SHUTDOWN(mp))
  819. return -EIO;
  820. error = xfs_qm_dqattach(ip, 0);
  821. if (error)
  822. return error;
  823. if (len <= 0)
  824. return -EINVAL;
  825. rt = XFS_IS_REALTIME_INODE(ip);
  826. extsz = xfs_get_extsz_hint(ip);
  827. count = len;
  828. imapp = &imaps[0];
  829. nimaps = 1;
  830. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  831. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  832. /*
  833. * Allocate file space until done or until there is an error
  834. */
  835. while (allocatesize_fsb && !error) {
  836. xfs_fileoff_t s, e;
  837. /*
  838. * Determine space reservations for data/realtime.
  839. */
  840. if (unlikely(extsz)) {
  841. s = startoffset_fsb;
  842. do_div(s, extsz);
  843. s *= extsz;
  844. e = startoffset_fsb + allocatesize_fsb;
  845. if ((temp = do_mod(startoffset_fsb, extsz)))
  846. e += temp;
  847. if ((temp = do_mod(e, extsz)))
  848. e += extsz - temp;
  849. } else {
  850. s = 0;
  851. e = allocatesize_fsb;
  852. }
  853. /*
  854. * The transaction reservation is limited to a 32-bit block
  855. * count, hence we need to limit the number of blocks we are
  856. * trying to reserve to avoid an overflow. We can't allocate
  857. * more than @nimaps extents, and an extent is limited on disk
  858. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  859. */
  860. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  861. if (unlikely(rt)) {
  862. resrtextents = qblocks = resblks;
  863. resrtextents /= mp->m_sb.sb_rextsize;
  864. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  865. quota_flag = XFS_QMOPT_RES_RTBLKS;
  866. } else {
  867. resrtextents = 0;
  868. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  869. quota_flag = XFS_QMOPT_RES_REGBLKS;
  870. }
  871. /*
  872. * Allocate and setup the transaction.
  873. */
  874. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
  875. resrtextents, 0, &tp);
  876. /*
  877. * Check for running out of space
  878. */
  879. if (error) {
  880. /*
  881. * Free the transaction structure.
  882. */
  883. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  884. break;
  885. }
  886. xfs_ilock(ip, XFS_ILOCK_EXCL);
  887. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  888. 0, quota_flag);
  889. if (error)
  890. goto error1;
  891. xfs_trans_ijoin(tp, ip, 0);
  892. xfs_defer_init(&dfops, &firstfsb);
  893. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  894. allocatesize_fsb, alloc_type, &firstfsb,
  895. resblks, imapp, &nimaps, &dfops);
  896. if (error)
  897. goto error0;
  898. /*
  899. * Complete the transaction
  900. */
  901. error = xfs_defer_finish(&tp, &dfops);
  902. if (error)
  903. goto error0;
  904. error = xfs_trans_commit(tp);
  905. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  906. if (error)
  907. break;
  908. allocated_fsb = imapp->br_blockcount;
  909. if (nimaps == 0) {
  910. error = -ENOSPC;
  911. break;
  912. }
  913. startoffset_fsb += allocated_fsb;
  914. allocatesize_fsb -= allocated_fsb;
  915. }
  916. return error;
  917. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  918. xfs_defer_cancel(&dfops);
  919. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  920. error1: /* Just cancel transaction */
  921. xfs_trans_cancel(tp);
  922. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  923. return error;
  924. }
  925. static int
  926. xfs_unmap_extent(
  927. struct xfs_inode *ip,
  928. xfs_fileoff_t startoffset_fsb,
  929. xfs_filblks_t len_fsb,
  930. int *done)
  931. {
  932. struct xfs_mount *mp = ip->i_mount;
  933. struct xfs_trans *tp;
  934. struct xfs_defer_ops dfops;
  935. xfs_fsblock_t firstfsb;
  936. uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  937. int error;
  938. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  939. if (error) {
  940. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  941. return error;
  942. }
  943. xfs_ilock(ip, XFS_ILOCK_EXCL);
  944. error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
  945. ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
  946. if (error)
  947. goto out_trans_cancel;
  948. xfs_trans_ijoin(tp, ip, 0);
  949. xfs_defer_init(&dfops, &firstfsb);
  950. error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, &firstfsb,
  951. &dfops, done);
  952. if (error)
  953. goto out_bmap_cancel;
  954. xfs_defer_ijoin(&dfops, ip);
  955. error = xfs_defer_finish(&tp, &dfops);
  956. if (error)
  957. goto out_bmap_cancel;
  958. error = xfs_trans_commit(tp);
  959. out_unlock:
  960. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  961. return error;
  962. out_bmap_cancel:
  963. xfs_defer_cancel(&dfops);
  964. out_trans_cancel:
  965. xfs_trans_cancel(tp);
  966. goto out_unlock;
  967. }
  968. static int
  969. xfs_adjust_extent_unmap_boundaries(
  970. struct xfs_inode *ip,
  971. xfs_fileoff_t *startoffset_fsb,
  972. xfs_fileoff_t *endoffset_fsb)
  973. {
  974. struct xfs_mount *mp = ip->i_mount;
  975. struct xfs_bmbt_irec imap;
  976. int nimap, error;
  977. xfs_extlen_t mod = 0;
  978. nimap = 1;
  979. error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0);
  980. if (error)
  981. return error;
  982. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  983. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  984. mod = do_mod(imap.br_startblock, mp->m_sb.sb_rextsize);
  985. if (mod)
  986. *startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  987. }
  988. nimap = 1;
  989. error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0);
  990. if (error)
  991. return error;
  992. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  993. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  994. mod++;
  995. if (mod && mod != mp->m_sb.sb_rextsize)
  996. *endoffset_fsb -= mod;
  997. }
  998. return 0;
  999. }
  1000. static int
  1001. xfs_flush_unmap_range(
  1002. struct xfs_inode *ip,
  1003. xfs_off_t offset,
  1004. xfs_off_t len)
  1005. {
  1006. struct xfs_mount *mp = ip->i_mount;
  1007. struct inode *inode = VFS_I(ip);
  1008. xfs_off_t rounding, start, end;
  1009. int error;
  1010. /* wait for the completion of any pending DIOs */
  1011. inode_dio_wait(inode);
  1012. rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
  1013. start = round_down(offset, rounding);
  1014. end = round_up(offset + len, rounding) - 1;
  1015. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1016. if (error)
  1017. return error;
  1018. truncate_pagecache_range(inode, start, end);
  1019. return 0;
  1020. }
  1021. int
  1022. xfs_free_file_space(
  1023. struct xfs_inode *ip,
  1024. xfs_off_t offset,
  1025. xfs_off_t len)
  1026. {
  1027. struct xfs_mount *mp = ip->i_mount;
  1028. xfs_fileoff_t startoffset_fsb;
  1029. xfs_fileoff_t endoffset_fsb;
  1030. int done = 0, error;
  1031. trace_xfs_free_file_space(ip);
  1032. error = xfs_qm_dqattach(ip, 0);
  1033. if (error)
  1034. return error;
  1035. if (len <= 0) /* if nothing being freed */
  1036. return 0;
  1037. error = xfs_flush_unmap_range(ip, offset, len);
  1038. if (error)
  1039. return error;
  1040. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1041. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1042. /*
  1043. * Need to zero the stuff we're not freeing, on disk. If it's a RT file
  1044. * and we can't use unwritten extents then we actually need to ensure
  1045. * to zero the whole extent, otherwise we just need to take of block
  1046. * boundaries, and xfs_bunmapi will handle the rest.
  1047. */
  1048. if (XFS_IS_REALTIME_INODE(ip) &&
  1049. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1050. error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb,
  1051. &endoffset_fsb);
  1052. if (error)
  1053. return error;
  1054. }
  1055. if (endoffset_fsb > startoffset_fsb) {
  1056. while (!done) {
  1057. error = xfs_unmap_extent(ip, startoffset_fsb,
  1058. endoffset_fsb - startoffset_fsb, &done);
  1059. if (error)
  1060. return error;
  1061. }
  1062. }
  1063. /*
  1064. * Now that we've unmap all full blocks we'll have to zero out any
  1065. * partial block at the beginning and/or end. xfs_zero_range is
  1066. * smart enough to skip any holes, including those we just created,
  1067. * but we must take care not to zero beyond EOF and enlarge i_size.
  1068. */
  1069. if (offset >= XFS_ISIZE(ip))
  1070. return 0;
  1071. if (offset + len > XFS_ISIZE(ip))
  1072. len = XFS_ISIZE(ip) - offset;
  1073. return xfs_zero_range(ip, offset, len, NULL);
  1074. }
  1075. /*
  1076. * Preallocate and zero a range of a file. This mechanism has the allocation
  1077. * semantics of fallocate and in addition converts data in the range to zeroes.
  1078. */
  1079. int
  1080. xfs_zero_file_space(
  1081. struct xfs_inode *ip,
  1082. xfs_off_t offset,
  1083. xfs_off_t len)
  1084. {
  1085. struct xfs_mount *mp = ip->i_mount;
  1086. uint blksize;
  1087. int error;
  1088. trace_xfs_zero_file_space(ip);
  1089. blksize = 1 << mp->m_sb.sb_blocklog;
  1090. /*
  1091. * Punch a hole and prealloc the range. We use hole punch rather than
  1092. * unwritten extent conversion for two reasons:
  1093. *
  1094. * 1.) Hole punch handles partial block zeroing for us.
  1095. *
  1096. * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
  1097. * by virtue of the hole punch.
  1098. */
  1099. error = xfs_free_file_space(ip, offset, len);
  1100. if (error)
  1101. goto out;
  1102. error = xfs_alloc_file_space(ip, round_down(offset, blksize),
  1103. round_up(offset + len, blksize) -
  1104. round_down(offset, blksize),
  1105. XFS_BMAPI_PREALLOC);
  1106. out:
  1107. return error;
  1108. }
  1109. static int
  1110. xfs_prepare_shift(
  1111. struct xfs_inode *ip,
  1112. loff_t offset)
  1113. {
  1114. int error;
  1115. /*
  1116. * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
  1117. * into the accessible region of the file.
  1118. */
  1119. if (xfs_can_free_eofblocks(ip, true)) {
  1120. error = xfs_free_eofblocks(ip);
  1121. if (error)
  1122. return error;
  1123. }
  1124. /*
  1125. * Writeback and invalidate cache for the remainder of the file as we're
  1126. * about to shift down every extent from offset to EOF.
  1127. */
  1128. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, offset, -1);
  1129. if (error)
  1130. return error;
  1131. error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
  1132. offset >> PAGE_SHIFT, -1);
  1133. if (error)
  1134. return error;
  1135. /*
  1136. * Clean out anything hanging around in the cow fork now that
  1137. * we've flushed all the dirty data out to disk to avoid having
  1138. * CoW extents at the wrong offsets.
  1139. */
  1140. if (xfs_is_reflink_inode(ip)) {
  1141. error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
  1142. true);
  1143. if (error)
  1144. return error;
  1145. }
  1146. return 0;
  1147. }
  1148. /*
  1149. * xfs_collapse_file_space()
  1150. * This routine frees disk space and shift extent for the given file.
  1151. * The first thing we do is to free data blocks in the specified range
  1152. * by calling xfs_free_file_space(). It would also sync dirty data
  1153. * and invalidate page cache over the region on which collapse range
  1154. * is working. And Shift extent records to the left to cover a hole.
  1155. * RETURNS:
  1156. * 0 on success
  1157. * errno on error
  1158. *
  1159. */
  1160. int
  1161. xfs_collapse_file_space(
  1162. struct xfs_inode *ip,
  1163. xfs_off_t offset,
  1164. xfs_off_t len)
  1165. {
  1166. struct xfs_mount *mp = ip->i_mount;
  1167. struct xfs_trans *tp;
  1168. int error;
  1169. struct xfs_defer_ops dfops;
  1170. xfs_fsblock_t first_block;
  1171. xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, VFS_I(ip)->i_size);
  1172. xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len);
  1173. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  1174. uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1175. bool done = false;
  1176. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1177. trace_xfs_collapse_file_space(ip);
  1178. error = xfs_free_file_space(ip, offset, len);
  1179. if (error)
  1180. return error;
  1181. error = xfs_prepare_shift(ip, offset);
  1182. if (error)
  1183. return error;
  1184. while (!error && !done) {
  1185. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0,
  1186. &tp);
  1187. if (error)
  1188. break;
  1189. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1190. error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
  1191. ip->i_gdquot, ip->i_pdquot, resblks, 0,
  1192. XFS_QMOPT_RES_REGBLKS);
  1193. if (error)
  1194. goto out_trans_cancel;
  1195. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1196. xfs_defer_init(&dfops, &first_block);
  1197. error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
  1198. &done, stop_fsb, &first_block, &dfops);
  1199. if (error)
  1200. goto out_bmap_cancel;
  1201. error = xfs_defer_finish(&tp, &dfops);
  1202. if (error)
  1203. goto out_bmap_cancel;
  1204. error = xfs_trans_commit(tp);
  1205. }
  1206. return error;
  1207. out_bmap_cancel:
  1208. xfs_defer_cancel(&dfops);
  1209. out_trans_cancel:
  1210. xfs_trans_cancel(tp);
  1211. return error;
  1212. }
  1213. /*
  1214. * xfs_insert_file_space()
  1215. * This routine create hole space by shifting extents for the given file.
  1216. * The first thing we do is to sync dirty data and invalidate page cache
  1217. * over the region on which insert range is working. And split an extent
  1218. * to two extents at given offset by calling xfs_bmap_split_extent.
  1219. * And shift all extent records which are laying between [offset,
  1220. * last allocated extent] to the right to reserve hole range.
  1221. * RETURNS:
  1222. * 0 on success
  1223. * errno on error
  1224. */
  1225. int
  1226. xfs_insert_file_space(
  1227. struct xfs_inode *ip,
  1228. loff_t offset,
  1229. loff_t len)
  1230. {
  1231. struct xfs_mount *mp = ip->i_mount;
  1232. struct xfs_trans *tp;
  1233. int error;
  1234. struct xfs_defer_ops dfops;
  1235. xfs_fsblock_t first_block;
  1236. xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset);
  1237. xfs_fileoff_t next_fsb = NULLFSBLOCK;
  1238. xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
  1239. bool done = false;
  1240. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1241. trace_xfs_insert_file_space(ip);
  1242. error = xfs_prepare_shift(ip, offset);
  1243. if (error)
  1244. return error;
  1245. /*
  1246. * The extent shifting code works on extent granularity. So, if stop_fsb
  1247. * is not the starting block of extent, we need to split the extent at
  1248. * stop_fsb.
  1249. */
  1250. error = xfs_bmap_split_extent(ip, stop_fsb);
  1251. if (error)
  1252. return error;
  1253. while (!error && !done) {
  1254. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0,
  1255. &tp);
  1256. if (error)
  1257. break;
  1258. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1259. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1260. xfs_defer_init(&dfops, &first_block);
  1261. error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
  1262. &done, stop_fsb, &first_block, &dfops);
  1263. if (error)
  1264. goto out_bmap_cancel;
  1265. error = xfs_defer_finish(&tp, &dfops);
  1266. if (error)
  1267. goto out_bmap_cancel;
  1268. error = xfs_trans_commit(tp);
  1269. }
  1270. return error;
  1271. out_bmap_cancel:
  1272. xfs_defer_cancel(&dfops);
  1273. xfs_trans_cancel(tp);
  1274. return error;
  1275. }
  1276. /*
  1277. * We need to check that the format of the data fork in the temporary inode is
  1278. * valid for the target inode before doing the swap. This is not a problem with
  1279. * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
  1280. * data fork depending on the space the attribute fork is taking so we can get
  1281. * invalid formats on the target inode.
  1282. *
  1283. * E.g. target has space for 7 extents in extent format, temp inode only has
  1284. * space for 6. If we defragment down to 7 extents, then the tmp format is a
  1285. * btree, but when swapped it needs to be in extent format. Hence we can't just
  1286. * blindly swap data forks on attr2 filesystems.
  1287. *
  1288. * Note that we check the swap in both directions so that we don't end up with
  1289. * a corrupt temporary inode, either.
  1290. *
  1291. * Note that fixing the way xfs_fsr sets up the attribute fork in the source
  1292. * inode will prevent this situation from occurring, so all we do here is
  1293. * reject and log the attempt. basically we are putting the responsibility on
  1294. * userspace to get this right.
  1295. */
  1296. static int
  1297. xfs_swap_extents_check_format(
  1298. struct xfs_inode *ip, /* target inode */
  1299. struct xfs_inode *tip) /* tmp inode */
  1300. {
  1301. /* Should never get a local format */
  1302. if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
  1303. tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  1304. return -EINVAL;
  1305. /*
  1306. * if the target inode has less extents that then temporary inode then
  1307. * why did userspace call us?
  1308. */
  1309. if (ip->i_d.di_nextents < tip->i_d.di_nextents)
  1310. return -EINVAL;
  1311. /*
  1312. * If we have to use the (expensive) rmap swap method, we can
  1313. * handle any number of extents and any format.
  1314. */
  1315. if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb))
  1316. return 0;
  1317. /*
  1318. * if the target inode is in extent form and the temp inode is in btree
  1319. * form then we will end up with the target inode in the wrong format
  1320. * as we already know there are less extents in the temp inode.
  1321. */
  1322. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1323. tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1324. return -EINVAL;
  1325. /* Check temp in extent form to max in target */
  1326. if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1327. XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
  1328. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1329. return -EINVAL;
  1330. /* Check target in extent form to max in temp */
  1331. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1332. XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
  1333. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1334. return -EINVAL;
  1335. /*
  1336. * If we are in a btree format, check that the temp root block will fit
  1337. * in the target and that it has enough extents to be in btree format
  1338. * in the target.
  1339. *
  1340. * Note that we have to be careful to allow btree->extent conversions
  1341. * (a common defrag case) which will occur when the temp inode is in
  1342. * extent format...
  1343. */
  1344. if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1345. if (XFS_IFORK_Q(ip) &&
  1346. XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
  1347. return -EINVAL;
  1348. if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
  1349. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1350. return -EINVAL;
  1351. }
  1352. /* Reciprocal target->temp btree format checks */
  1353. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1354. if (XFS_IFORK_Q(tip) &&
  1355. XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
  1356. return -EINVAL;
  1357. if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
  1358. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1359. return -EINVAL;
  1360. }
  1361. return 0;
  1362. }
  1363. static int
  1364. xfs_swap_extent_flush(
  1365. struct xfs_inode *ip)
  1366. {
  1367. int error;
  1368. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1369. if (error)
  1370. return error;
  1371. truncate_pagecache_range(VFS_I(ip), 0, -1);
  1372. /* Verify O_DIRECT for ftmp */
  1373. if (VFS_I(ip)->i_mapping->nrpages)
  1374. return -EINVAL;
  1375. return 0;
  1376. }
  1377. /*
  1378. * Move extents from one file to another, when rmap is enabled.
  1379. */
  1380. STATIC int
  1381. xfs_swap_extent_rmap(
  1382. struct xfs_trans **tpp,
  1383. struct xfs_inode *ip,
  1384. struct xfs_inode *tip)
  1385. {
  1386. struct xfs_bmbt_irec irec;
  1387. struct xfs_bmbt_irec uirec;
  1388. struct xfs_bmbt_irec tirec;
  1389. xfs_fileoff_t offset_fsb;
  1390. xfs_fileoff_t end_fsb;
  1391. xfs_filblks_t count_fsb;
  1392. xfs_fsblock_t firstfsb;
  1393. struct xfs_defer_ops dfops;
  1394. int error;
  1395. xfs_filblks_t ilen;
  1396. xfs_filblks_t rlen;
  1397. int nimaps;
  1398. uint64_t tip_flags2;
  1399. /*
  1400. * If the source file has shared blocks, we must flag the donor
  1401. * file as having shared blocks so that we get the shared-block
  1402. * rmap functions when we go to fix up the rmaps. The flags
  1403. * will be switch for reals later.
  1404. */
  1405. tip_flags2 = tip->i_d.di_flags2;
  1406. if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)
  1407. tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  1408. offset_fsb = 0;
  1409. end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
  1410. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  1411. while (count_fsb) {
  1412. /* Read extent from the donor file */
  1413. nimaps = 1;
  1414. error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
  1415. &nimaps, 0);
  1416. if (error)
  1417. goto out;
  1418. ASSERT(nimaps == 1);
  1419. ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
  1420. trace_xfs_swap_extent_rmap_remap(tip, &tirec);
  1421. ilen = tirec.br_blockcount;
  1422. /* Unmap the old blocks in the source file. */
  1423. while (tirec.br_blockcount) {
  1424. xfs_defer_init(&dfops, &firstfsb);
  1425. trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
  1426. /* Read extent from the source file */
  1427. nimaps = 1;
  1428. error = xfs_bmapi_read(ip, tirec.br_startoff,
  1429. tirec.br_blockcount, &irec,
  1430. &nimaps, 0);
  1431. if (error)
  1432. goto out_defer;
  1433. ASSERT(nimaps == 1);
  1434. ASSERT(tirec.br_startoff == irec.br_startoff);
  1435. trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
  1436. /* Trim the extent. */
  1437. uirec = tirec;
  1438. uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
  1439. tirec.br_blockcount,
  1440. irec.br_blockcount);
  1441. trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
  1442. /* Remove the mapping from the donor file. */
  1443. error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
  1444. tip, &uirec);
  1445. if (error)
  1446. goto out_defer;
  1447. /* Remove the mapping from the source file. */
  1448. error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
  1449. ip, &irec);
  1450. if (error)
  1451. goto out_defer;
  1452. /* Map the donor file's blocks into the source file. */
  1453. error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
  1454. ip, &uirec);
  1455. if (error)
  1456. goto out_defer;
  1457. /* Map the source file's blocks into the donor file. */
  1458. error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
  1459. tip, &irec);
  1460. if (error)
  1461. goto out_defer;
  1462. xfs_defer_ijoin(&dfops, ip);
  1463. error = xfs_defer_finish(tpp, &dfops);
  1464. if (error)
  1465. goto out_defer;
  1466. tirec.br_startoff += rlen;
  1467. if (tirec.br_startblock != HOLESTARTBLOCK &&
  1468. tirec.br_startblock != DELAYSTARTBLOCK)
  1469. tirec.br_startblock += rlen;
  1470. tirec.br_blockcount -= rlen;
  1471. }
  1472. /* Roll on... */
  1473. count_fsb -= ilen;
  1474. offset_fsb += ilen;
  1475. }
  1476. tip->i_d.di_flags2 = tip_flags2;
  1477. return 0;
  1478. out_defer:
  1479. xfs_defer_cancel(&dfops);
  1480. out:
  1481. trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
  1482. tip->i_d.di_flags2 = tip_flags2;
  1483. return error;
  1484. }
  1485. /* Swap the extents of two files by swapping data forks. */
  1486. STATIC int
  1487. xfs_swap_extent_forks(
  1488. struct xfs_trans *tp,
  1489. struct xfs_inode *ip,
  1490. struct xfs_inode *tip,
  1491. int *src_log_flags,
  1492. int *target_log_flags)
  1493. {
  1494. struct xfs_ifork tempifp, *ifp, *tifp;
  1495. xfs_filblks_t aforkblks = 0;
  1496. xfs_filblks_t taforkblks = 0;
  1497. xfs_extnum_t junk;
  1498. xfs_extnum_t nextents;
  1499. uint64_t tmp;
  1500. int error;
  1501. /*
  1502. * Count the number of extended attribute blocks
  1503. */
  1504. if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
  1505. (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1506. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
  1507. &aforkblks);
  1508. if (error)
  1509. return error;
  1510. }
  1511. if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
  1512. (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1513. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
  1514. &taforkblks);
  1515. if (error)
  1516. return error;
  1517. }
  1518. /*
  1519. * Btree format (v3) inodes have the inode number stamped in the bmbt
  1520. * block headers. We can't start changing the bmbt blocks until the
  1521. * inode owner change is logged so recovery does the right thing in the
  1522. * event of a crash. Set the owner change log flags now and leave the
  1523. * bmbt scan as the last step.
  1524. */
  1525. if (ip->i_d.di_version == 3 &&
  1526. ip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1527. (*target_log_flags) |= XFS_ILOG_DOWNER;
  1528. if (tip->i_d.di_version == 3 &&
  1529. tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1530. (*src_log_flags) |= XFS_ILOG_DOWNER;
  1531. /*
  1532. * Swap the data forks of the inodes
  1533. */
  1534. ifp = &ip->i_df;
  1535. tifp = &tip->i_df;
  1536. tempifp = *ifp; /* struct copy */
  1537. *ifp = *tifp; /* struct copy */
  1538. *tifp = tempifp; /* struct copy */
  1539. /*
  1540. * Fix the on-disk inode values
  1541. */
  1542. tmp = (uint64_t)ip->i_d.di_nblocks;
  1543. ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
  1544. tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
  1545. tmp = (uint64_t) ip->i_d.di_nextents;
  1546. ip->i_d.di_nextents = tip->i_d.di_nextents;
  1547. tip->i_d.di_nextents = tmp;
  1548. tmp = (uint64_t) ip->i_d.di_format;
  1549. ip->i_d.di_format = tip->i_d.di_format;
  1550. tip->i_d.di_format = tmp;
  1551. /*
  1552. * The extents in the source inode could still contain speculative
  1553. * preallocation beyond EOF (e.g. the file is open but not modified
  1554. * while defrag is in progress). In that case, we need to copy over the
  1555. * number of delalloc blocks the data fork in the source inode is
  1556. * tracking beyond EOF so that when the fork is truncated away when the
  1557. * temporary inode is unlinked we don't underrun the i_delayed_blks
  1558. * counter on that inode.
  1559. */
  1560. ASSERT(tip->i_delayed_blks == 0);
  1561. tip->i_delayed_blks = ip->i_delayed_blks;
  1562. ip->i_delayed_blks = 0;
  1563. switch (ip->i_d.di_format) {
  1564. case XFS_DINODE_FMT_EXTENTS:
  1565. /*
  1566. * If the extents fit in the inode, fix the pointer. Otherwise
  1567. * it's already NULL or pointing to the extent.
  1568. */
  1569. nextents = xfs_iext_count(&ip->i_df);
  1570. if (nextents <= XFS_INLINE_EXTS)
  1571. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  1572. (*src_log_flags) |= XFS_ILOG_DEXT;
  1573. break;
  1574. case XFS_DINODE_FMT_BTREE:
  1575. ASSERT(ip->i_d.di_version < 3 ||
  1576. (*src_log_flags & XFS_ILOG_DOWNER));
  1577. (*src_log_flags) |= XFS_ILOG_DBROOT;
  1578. break;
  1579. }
  1580. switch (tip->i_d.di_format) {
  1581. case XFS_DINODE_FMT_EXTENTS:
  1582. /*
  1583. * If the extents fit in the inode, fix the pointer. Otherwise
  1584. * it's already NULL or pointing to the extent.
  1585. */
  1586. nextents = xfs_iext_count(&tip->i_df);
  1587. if (nextents <= XFS_INLINE_EXTS)
  1588. tifp->if_u1.if_extents = tifp->if_u2.if_inline_ext;
  1589. (*target_log_flags) |= XFS_ILOG_DEXT;
  1590. break;
  1591. case XFS_DINODE_FMT_BTREE:
  1592. (*target_log_flags) |= XFS_ILOG_DBROOT;
  1593. ASSERT(tip->i_d.di_version < 3 ||
  1594. (*target_log_flags & XFS_ILOG_DOWNER));
  1595. break;
  1596. }
  1597. return 0;
  1598. }
  1599. /*
  1600. * Fix up the owners of the bmbt blocks to refer to the current inode. The
  1601. * change owner scan attempts to order all modified buffers in the current
  1602. * transaction. In the event of ordered buffer failure, the offending buffer is
  1603. * physically logged as a fallback and the scan returns -EAGAIN. We must roll
  1604. * the transaction in this case to replenish the fallback log reservation and
  1605. * restart the scan. This process repeats until the scan completes.
  1606. */
  1607. static int
  1608. xfs_swap_change_owner(
  1609. struct xfs_trans **tpp,
  1610. struct xfs_inode *ip,
  1611. struct xfs_inode *tmpip)
  1612. {
  1613. int error;
  1614. struct xfs_trans *tp = *tpp;
  1615. do {
  1616. error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
  1617. NULL);
  1618. /* success or fatal error */
  1619. if (error != -EAGAIN)
  1620. break;
  1621. error = xfs_trans_roll(tpp);
  1622. if (error)
  1623. break;
  1624. tp = *tpp;
  1625. /*
  1626. * Redirty both inodes so they can relog and keep the log tail
  1627. * moving forward.
  1628. */
  1629. xfs_trans_ijoin(tp, ip, 0);
  1630. xfs_trans_ijoin(tp, tmpip, 0);
  1631. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1632. xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
  1633. } while (true);
  1634. return error;
  1635. }
  1636. int
  1637. xfs_swap_extents(
  1638. struct xfs_inode *ip, /* target inode */
  1639. struct xfs_inode *tip, /* tmp inode */
  1640. struct xfs_swapext *sxp)
  1641. {
  1642. struct xfs_mount *mp = ip->i_mount;
  1643. struct xfs_trans *tp;
  1644. struct xfs_bstat *sbp = &sxp->sx_stat;
  1645. int src_log_flags, target_log_flags;
  1646. int error = 0;
  1647. int lock_flags;
  1648. struct xfs_ifork *cowfp;
  1649. uint64_t f;
  1650. int resblks = 0;
  1651. /*
  1652. * Lock the inodes against other IO, page faults and truncate to
  1653. * begin with. Then we can ensure the inodes are flushed and have no
  1654. * page cache safely. Once we have done this we can take the ilocks and
  1655. * do the rest of the checks.
  1656. */
  1657. lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1658. lock_flags = XFS_MMAPLOCK_EXCL;
  1659. xfs_lock_two_inodes(ip, tip, XFS_MMAPLOCK_EXCL);
  1660. /* Verify that both files have the same format */
  1661. if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
  1662. error = -EINVAL;
  1663. goto out_unlock;
  1664. }
  1665. /* Verify both files are either real-time or non-realtime */
  1666. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  1667. error = -EINVAL;
  1668. goto out_unlock;
  1669. }
  1670. error = xfs_swap_extent_flush(ip);
  1671. if (error)
  1672. goto out_unlock;
  1673. error = xfs_swap_extent_flush(tip);
  1674. if (error)
  1675. goto out_unlock;
  1676. /*
  1677. * Extent "swapping" with rmap requires a permanent reservation and
  1678. * a block reservation because it's really just a remap operation
  1679. * performed with log redo items!
  1680. */
  1681. if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
  1682. /*
  1683. * Conceptually this shouldn't affect the shape of either
  1684. * bmbt, but since we atomically move extents one by one,
  1685. * we reserve enough space to rebuild both trees.
  1686. */
  1687. resblks = XFS_SWAP_RMAP_SPACE_RES(mp,
  1688. XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK),
  1689. XFS_DATA_FORK) +
  1690. XFS_SWAP_RMAP_SPACE_RES(mp,
  1691. XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK),
  1692. XFS_DATA_FORK);
  1693. }
  1694. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  1695. if (error)
  1696. goto out_unlock;
  1697. /*
  1698. * Lock and join the inodes to the tansaction so that transaction commit
  1699. * or cancel will unlock the inodes from this point onwards.
  1700. */
  1701. xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
  1702. lock_flags |= XFS_ILOCK_EXCL;
  1703. xfs_trans_ijoin(tp, ip, 0);
  1704. xfs_trans_ijoin(tp, tip, 0);
  1705. /* Verify all data are being swapped */
  1706. if (sxp->sx_offset != 0 ||
  1707. sxp->sx_length != ip->i_d.di_size ||
  1708. sxp->sx_length != tip->i_d.di_size) {
  1709. error = -EFAULT;
  1710. goto out_trans_cancel;
  1711. }
  1712. trace_xfs_swap_extent_before(ip, 0);
  1713. trace_xfs_swap_extent_before(tip, 1);
  1714. /* check inode formats now that data is flushed */
  1715. error = xfs_swap_extents_check_format(ip, tip);
  1716. if (error) {
  1717. xfs_notice(mp,
  1718. "%s: inode 0x%llx format is incompatible for exchanging.",
  1719. __func__, ip->i_ino);
  1720. goto out_trans_cancel;
  1721. }
  1722. /*
  1723. * Compare the current change & modify times with that
  1724. * passed in. If they differ, we abort this swap.
  1725. * This is the mechanism used to ensure the calling
  1726. * process that the file was not changed out from
  1727. * under it.
  1728. */
  1729. if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
  1730. (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
  1731. (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
  1732. (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
  1733. error = -EBUSY;
  1734. goto out_trans_cancel;
  1735. }
  1736. /*
  1737. * Note the trickiness in setting the log flags - we set the owner log
  1738. * flag on the opposite inode (i.e. the inode we are setting the new
  1739. * owner to be) because once we swap the forks and log that, log
  1740. * recovery is going to see the fork as owned by the swapped inode,
  1741. * not the pre-swapped inodes.
  1742. */
  1743. src_log_flags = XFS_ILOG_CORE;
  1744. target_log_flags = XFS_ILOG_CORE;
  1745. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  1746. error = xfs_swap_extent_rmap(&tp, ip, tip);
  1747. else
  1748. error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
  1749. &target_log_flags);
  1750. if (error)
  1751. goto out_trans_cancel;
  1752. /* Do we have to swap reflink flags? */
  1753. if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^
  1754. (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) {
  1755. f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
  1756. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1757. ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
  1758. tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1759. tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK;
  1760. }
  1761. /* Swap the cow forks. */
  1762. if (xfs_sb_version_hasreflink(&mp->m_sb)) {
  1763. xfs_extnum_t extnum;
  1764. ASSERT(ip->i_cformat == XFS_DINODE_FMT_EXTENTS);
  1765. ASSERT(tip->i_cformat == XFS_DINODE_FMT_EXTENTS);
  1766. extnum = ip->i_cnextents;
  1767. ip->i_cnextents = tip->i_cnextents;
  1768. tip->i_cnextents = extnum;
  1769. cowfp = ip->i_cowfp;
  1770. ip->i_cowfp = tip->i_cowfp;
  1771. tip->i_cowfp = cowfp;
  1772. if (ip->i_cowfp && ip->i_cnextents)
  1773. xfs_inode_set_cowblocks_tag(ip);
  1774. else
  1775. xfs_inode_clear_cowblocks_tag(ip);
  1776. if (tip->i_cowfp && tip->i_cnextents)
  1777. xfs_inode_set_cowblocks_tag(tip);
  1778. else
  1779. xfs_inode_clear_cowblocks_tag(tip);
  1780. }
  1781. xfs_trans_log_inode(tp, ip, src_log_flags);
  1782. xfs_trans_log_inode(tp, tip, target_log_flags);
  1783. /*
  1784. * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
  1785. * have inode number owner values in the bmbt blocks that still refer to
  1786. * the old inode. Scan each bmbt to fix up the owner values with the
  1787. * inode number of the current inode.
  1788. */
  1789. if (src_log_flags & XFS_ILOG_DOWNER) {
  1790. error = xfs_swap_change_owner(&tp, ip, tip);
  1791. if (error)
  1792. goto out_trans_cancel;
  1793. }
  1794. if (target_log_flags & XFS_ILOG_DOWNER) {
  1795. error = xfs_swap_change_owner(&tp, tip, ip);
  1796. if (error)
  1797. goto out_trans_cancel;
  1798. }
  1799. /*
  1800. * If this is a synchronous mount, make sure that the
  1801. * transaction goes to disk before returning to the user.
  1802. */
  1803. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1804. xfs_trans_set_sync(tp);
  1805. error = xfs_trans_commit(tp);
  1806. trace_xfs_swap_extent_after(ip, 0);
  1807. trace_xfs_swap_extent_after(tip, 1);
  1808. out_unlock:
  1809. xfs_iunlock(ip, lock_flags);
  1810. xfs_iunlock(tip, lock_flags);
  1811. unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
  1812. return error;
  1813. out_trans_cancel:
  1814. xfs_trans_cancel(tp);
  1815. goto out_unlock;
  1816. }