xfs_bmap_util.c 55 KB

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