xfs_bmap_util.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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. unsigned int logres; /* new log reservation */
  79. unsigned int logcount; /* new log count */
  80. xfs_mount_t *mp; /* filesystem mount structure */
  81. xfs_bmap_free_item_t *next; /* next item on free list */
  82. xfs_trans_t *ntp; /* new transaction pointer */
  83. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  84. if (flist->xbf_count == 0) {
  85. *committed = 0;
  86. return 0;
  87. }
  88. ntp = *tp;
  89. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  90. for (free = flist->xbf_first; free; free = free->xbfi_next)
  91. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  92. free->xbfi_blockcount);
  93. logres = ntp->t_log_res;
  94. logcount = ntp->t_log_count;
  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. if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
  111. logcount)))
  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 is 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(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
  564. if (!out) {
  565. out = kmem_zalloc_large(bmv->bmv_count *
  566. sizeof(struct getbmapx));
  567. if (!out)
  568. return XFS_ERROR(ENOMEM);
  569. }
  570. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  571. if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
  572. if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
  573. error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
  574. if (error)
  575. goto out_unlock_iolock;
  576. }
  577. /*
  578. * even after flushing the inode, there can still be delalloc
  579. * blocks on the inode beyond EOF due to speculative
  580. * preallocation. These are not removed until the release
  581. * function is called or the inode is inactivated. Hence we
  582. * cannot assert here that ip->i_delayed_blks == 0.
  583. */
  584. }
  585. lock = xfs_ilock_map_shared(ip);
  586. /*
  587. * Don't let nex be bigger than the number of extents
  588. * we can have assuming alternating holes and real extents.
  589. */
  590. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  591. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  592. bmapi_flags = xfs_bmapi_aflag(whichfork);
  593. if (!(iflags & BMV_IF_PREALLOC))
  594. bmapi_flags |= XFS_BMAPI_IGSTATE;
  595. /*
  596. * Allocate enough space to handle "subnex" maps at a time.
  597. */
  598. error = ENOMEM;
  599. subnex = 16;
  600. map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
  601. if (!map)
  602. goto out_unlock_ilock;
  603. bmv->bmv_entries = 0;
  604. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
  605. (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
  606. error = 0;
  607. goto out_free_map;
  608. }
  609. nexleft = nex;
  610. do {
  611. nmap = (nexleft > subnex) ? subnex : nexleft;
  612. error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  613. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  614. map, &nmap, bmapi_flags);
  615. if (error)
  616. goto out_free_map;
  617. ASSERT(nmap <= subnex);
  618. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  619. out[cur_ext].bmv_oflags = 0;
  620. if (map[i].br_state == XFS_EXT_UNWRITTEN)
  621. out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
  622. else if (map[i].br_startblock == DELAYSTARTBLOCK)
  623. out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
  624. out[cur_ext].bmv_offset =
  625. XFS_FSB_TO_BB(mp, map[i].br_startoff);
  626. out[cur_ext].bmv_length =
  627. XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  628. out[cur_ext].bmv_unused1 = 0;
  629. out[cur_ext].bmv_unused2 = 0;
  630. /*
  631. * delayed allocation extents that start beyond EOF can
  632. * occur due to speculative EOF allocation when the
  633. * delalloc extent is larger than the largest freespace
  634. * extent at conversion time. These extents cannot be
  635. * converted by data writeback, so can exist here even
  636. * if we are not supposed to be finding delalloc
  637. * extents.
  638. */
  639. if (map[i].br_startblock == DELAYSTARTBLOCK &&
  640. map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
  641. ASSERT((iflags & BMV_IF_DELALLOC) != 0);
  642. if (map[i].br_startblock == HOLESTARTBLOCK &&
  643. whichfork == XFS_ATTR_FORK) {
  644. /* came to the end of attribute fork */
  645. out[cur_ext].bmv_oflags |= BMV_OF_LAST;
  646. goto out_free_map;
  647. }
  648. if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
  649. prealloced, bmvend,
  650. map[i].br_startblock))
  651. goto out_free_map;
  652. bmv->bmv_offset =
  653. out[cur_ext].bmv_offset +
  654. out[cur_ext].bmv_length;
  655. bmv->bmv_length =
  656. max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
  657. /*
  658. * In case we don't want to return the hole,
  659. * don't increase cur_ext so that we can reuse
  660. * it in the next loop.
  661. */
  662. if ((iflags & BMV_IF_NO_HOLES) &&
  663. map[i].br_startblock == HOLESTARTBLOCK) {
  664. memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
  665. continue;
  666. }
  667. nexleft--;
  668. bmv->bmv_entries++;
  669. cur_ext++;
  670. }
  671. } while (nmap && nexleft && bmv->bmv_length);
  672. out_free_map:
  673. kmem_free(map);
  674. out_unlock_ilock:
  675. xfs_iunlock_map_shared(ip, lock);
  676. out_unlock_iolock:
  677. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  678. for (i = 0; i < cur_ext; i++) {
  679. int full = 0; /* user array is full */
  680. /* format results & advance arg */
  681. error = formatter(&arg, &out[i], &full);
  682. if (error || full)
  683. break;
  684. }
  685. if (is_vmalloc_addr(out))
  686. kmem_free_large(out);
  687. else
  688. kmem_free(out);
  689. return error;
  690. }
  691. /*
  692. * dead simple method of punching delalyed allocation blocks from a range in
  693. * the inode. Walks a block at a time so will be slow, but is only executed in
  694. * rare error cases so the overhead is not critical. This will alays punch out
  695. * both the start and end blocks, even if the ranges only partially overlap
  696. * them, so it is up to the caller to ensure that partial blocks are not
  697. * passed in.
  698. */
  699. int
  700. xfs_bmap_punch_delalloc_range(
  701. struct xfs_inode *ip,
  702. xfs_fileoff_t start_fsb,
  703. xfs_fileoff_t length)
  704. {
  705. xfs_fileoff_t remaining = length;
  706. int error = 0;
  707. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  708. do {
  709. int done;
  710. xfs_bmbt_irec_t imap;
  711. int nimaps = 1;
  712. xfs_fsblock_t firstblock;
  713. xfs_bmap_free_t flist;
  714. /*
  715. * Map the range first and check that it is a delalloc extent
  716. * before trying to unmap the range. Otherwise we will be
  717. * trying to remove a real extent (which requires a
  718. * transaction) or a hole, which is probably a bad idea...
  719. */
  720. error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
  721. XFS_BMAPI_ENTIRE);
  722. if (error) {
  723. /* something screwed, just bail */
  724. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  725. xfs_alert(ip->i_mount,
  726. "Failed delalloc mapping lookup ino %lld fsb %lld.",
  727. ip->i_ino, start_fsb);
  728. }
  729. break;
  730. }
  731. if (!nimaps) {
  732. /* nothing there */
  733. goto next_block;
  734. }
  735. if (imap.br_startblock != DELAYSTARTBLOCK) {
  736. /* been converted, ignore */
  737. goto next_block;
  738. }
  739. WARN_ON(imap.br_blockcount == 0);
  740. /*
  741. * Note: while we initialise the firstblock/flist pair, they
  742. * should never be used because blocks should never be
  743. * allocated or freed for a delalloc extent and hence we need
  744. * don't cancel or finish them after the xfs_bunmapi() call.
  745. */
  746. xfs_bmap_init(&flist, &firstblock);
  747. error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
  748. &flist, &done);
  749. if (error)
  750. break;
  751. ASSERT(!flist.xbf_count && !flist.xbf_first);
  752. next_block:
  753. start_fsb++;
  754. remaining--;
  755. } while(remaining > 0);
  756. return error;
  757. }
  758. /*
  759. * Test whether it is appropriate to check an inode for and free post EOF
  760. * blocks. The 'force' parameter determines whether we should also consider
  761. * regular files that are marked preallocated or append-only.
  762. */
  763. bool
  764. xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
  765. {
  766. /* prealloc/delalloc exists only on regular files */
  767. if (!S_ISREG(ip->i_d.di_mode))
  768. return false;
  769. /*
  770. * Zero sized files with no cached pages and delalloc blocks will not
  771. * have speculative prealloc/delalloc blocks to remove.
  772. */
  773. if (VFS_I(ip)->i_size == 0 &&
  774. VN_CACHED(VFS_I(ip)) == 0 &&
  775. ip->i_delayed_blks == 0)
  776. return false;
  777. /* If we haven't read in the extent list, then don't do it now. */
  778. if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
  779. return false;
  780. /*
  781. * Do not free real preallocated or append-only files unless the file
  782. * has delalloc blocks and we are forced to remove them.
  783. */
  784. if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  785. if (!force || ip->i_delayed_blks == 0)
  786. return false;
  787. return true;
  788. }
  789. /*
  790. * This is called by xfs_inactive to free any blocks beyond eof
  791. * when the link count isn't zero and by xfs_dm_punch_hole() when
  792. * punching a hole to EOF.
  793. */
  794. int
  795. xfs_free_eofblocks(
  796. xfs_mount_t *mp,
  797. xfs_inode_t *ip,
  798. bool need_iolock)
  799. {
  800. xfs_trans_t *tp;
  801. int error;
  802. xfs_fileoff_t end_fsb;
  803. xfs_fileoff_t last_fsb;
  804. xfs_filblks_t map_len;
  805. int nimaps;
  806. xfs_bmbt_irec_t imap;
  807. /*
  808. * Figure out if there are any blocks beyond the end
  809. * of the file. If not, then there is nothing to do.
  810. */
  811. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  812. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  813. if (last_fsb <= end_fsb)
  814. return 0;
  815. map_len = last_fsb - end_fsb;
  816. nimaps = 1;
  817. xfs_ilock(ip, XFS_ILOCK_SHARED);
  818. error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
  819. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  820. if (!error && (nimaps != 0) &&
  821. (imap.br_startblock != HOLESTARTBLOCK ||
  822. ip->i_delayed_blks)) {
  823. /*
  824. * Attach the dquots to the inode up front.
  825. */
  826. error = xfs_qm_dqattach(ip, 0);
  827. if (error)
  828. return error;
  829. /*
  830. * There are blocks after the end of file.
  831. * Free them up now by truncating the file to
  832. * its current size.
  833. */
  834. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  835. if (need_iolock) {
  836. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
  837. xfs_trans_cancel(tp, 0);
  838. return EAGAIN;
  839. }
  840. }
  841. error = xfs_trans_reserve(tp, 0,
  842. XFS_ITRUNCATE_LOG_RES(mp),
  843. 0, XFS_TRANS_PERM_LOG_RES,
  844. XFS_ITRUNCATE_LOG_COUNT);
  845. if (error) {
  846. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  847. xfs_trans_cancel(tp, 0);
  848. if (need_iolock)
  849. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  850. return error;
  851. }
  852. xfs_ilock(ip, XFS_ILOCK_EXCL);
  853. xfs_trans_ijoin(tp, ip, 0);
  854. /*
  855. * Do not update the on-disk file size. If we update the
  856. * on-disk file size and then the system crashes before the
  857. * contents of the file are flushed to disk then the files
  858. * may be full of holes (ie NULL files bug).
  859. */
  860. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
  861. XFS_ISIZE(ip));
  862. if (error) {
  863. /*
  864. * If we get an error at this point we simply don't
  865. * bother truncating the file.
  866. */
  867. xfs_trans_cancel(tp,
  868. (XFS_TRANS_RELEASE_LOG_RES |
  869. XFS_TRANS_ABORT));
  870. } else {
  871. error = xfs_trans_commit(tp,
  872. XFS_TRANS_RELEASE_LOG_RES);
  873. if (!error)
  874. xfs_inode_clear_eofblocks_tag(ip);
  875. }
  876. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  877. if (need_iolock)
  878. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  879. }
  880. return error;
  881. }
  882. /*
  883. * xfs_alloc_file_space()
  884. * This routine allocates disk space for the given file.
  885. *
  886. * If alloc_type == 0, this request is for an ALLOCSP type
  887. * request which will change the file size. In this case, no
  888. * DMAPI event will be generated by the call. A TRUNCATE event
  889. * will be generated later by xfs_setattr.
  890. *
  891. * If alloc_type != 0, this request is for a RESVSP type
  892. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  893. * lower block boundary byte address is less than the file's
  894. * length.
  895. *
  896. * RETURNS:
  897. * 0 on success
  898. * errno on error
  899. *
  900. */
  901. STATIC int
  902. xfs_alloc_file_space(
  903. xfs_inode_t *ip,
  904. xfs_off_t offset,
  905. xfs_off_t len,
  906. int alloc_type,
  907. int attr_flags)
  908. {
  909. xfs_mount_t *mp = ip->i_mount;
  910. xfs_off_t count;
  911. xfs_filblks_t allocated_fsb;
  912. xfs_filblks_t allocatesize_fsb;
  913. xfs_extlen_t extsz, temp;
  914. xfs_fileoff_t startoffset_fsb;
  915. xfs_fsblock_t firstfsb;
  916. int nimaps;
  917. int quota_flag;
  918. int rt;
  919. xfs_trans_t *tp;
  920. xfs_bmbt_irec_t imaps[1], *imapp;
  921. xfs_bmap_free_t free_list;
  922. uint qblocks, resblks, resrtextents;
  923. int committed;
  924. int error;
  925. trace_xfs_alloc_file_space(ip);
  926. if (XFS_FORCED_SHUTDOWN(mp))
  927. return XFS_ERROR(EIO);
  928. error = xfs_qm_dqattach(ip, 0);
  929. if (error)
  930. return error;
  931. if (len <= 0)
  932. return XFS_ERROR(EINVAL);
  933. rt = XFS_IS_REALTIME_INODE(ip);
  934. extsz = xfs_get_extsz_hint(ip);
  935. count = len;
  936. imapp = &imaps[0];
  937. nimaps = 1;
  938. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  939. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  940. /*
  941. * Allocate file space until done or until there is an error
  942. */
  943. while (allocatesize_fsb && !error) {
  944. xfs_fileoff_t s, e;
  945. /*
  946. * Determine space reservations for data/realtime.
  947. */
  948. if (unlikely(extsz)) {
  949. s = startoffset_fsb;
  950. do_div(s, extsz);
  951. s *= extsz;
  952. e = startoffset_fsb + allocatesize_fsb;
  953. if ((temp = do_mod(startoffset_fsb, extsz)))
  954. e += temp;
  955. if ((temp = do_mod(e, extsz)))
  956. e += extsz - temp;
  957. } else {
  958. s = 0;
  959. e = allocatesize_fsb;
  960. }
  961. /*
  962. * The transaction reservation is limited to a 32-bit block
  963. * count, hence we need to limit the number of blocks we are
  964. * trying to reserve to avoid an overflow. We can't allocate
  965. * more than @nimaps extents, and an extent is limited on disk
  966. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  967. */
  968. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  969. if (unlikely(rt)) {
  970. resrtextents = qblocks = resblks;
  971. resrtextents /= mp->m_sb.sb_rextsize;
  972. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  973. quota_flag = XFS_QMOPT_RES_RTBLKS;
  974. } else {
  975. resrtextents = 0;
  976. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  977. quota_flag = XFS_QMOPT_RES_REGBLKS;
  978. }
  979. /*
  980. * Allocate and setup the transaction.
  981. */
  982. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  983. error = xfs_trans_reserve(tp, resblks,
  984. XFS_WRITE_LOG_RES(mp), resrtextents,
  985. XFS_TRANS_PERM_LOG_RES,
  986. XFS_WRITE_LOG_COUNT);
  987. /*
  988. * Check for running out of space
  989. */
  990. if (error) {
  991. /*
  992. * Free the transaction structure.
  993. */
  994. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  995. xfs_trans_cancel(tp, 0);
  996. break;
  997. }
  998. xfs_ilock(ip, XFS_ILOCK_EXCL);
  999. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  1000. 0, quota_flag);
  1001. if (error)
  1002. goto error1;
  1003. xfs_trans_ijoin(tp, ip, 0);
  1004. xfs_bmap_init(&free_list, &firstfsb);
  1005. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  1006. allocatesize_fsb, alloc_type, &firstfsb,
  1007. 0, imapp, &nimaps, &free_list);
  1008. if (error) {
  1009. goto error0;
  1010. }
  1011. /*
  1012. * Complete the transaction
  1013. */
  1014. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1015. if (error) {
  1016. goto error0;
  1017. }
  1018. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1019. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1020. if (error) {
  1021. break;
  1022. }
  1023. allocated_fsb = imapp->br_blockcount;
  1024. if (nimaps == 0) {
  1025. error = XFS_ERROR(ENOSPC);
  1026. break;
  1027. }
  1028. startoffset_fsb += allocated_fsb;
  1029. allocatesize_fsb -= allocated_fsb;
  1030. }
  1031. return error;
  1032. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  1033. xfs_bmap_cancel(&free_list);
  1034. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  1035. error1: /* Just cancel transaction */
  1036. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1037. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1038. return error;
  1039. }
  1040. /*
  1041. * Zero file bytes between startoff and endoff inclusive.
  1042. * The iolock is held exclusive and no blocks are buffered.
  1043. *
  1044. * This function is used by xfs_free_file_space() to zero
  1045. * partial blocks when the range to free is not block aligned.
  1046. * When unreserving space with boundaries that are not block
  1047. * aligned we round up the start and round down the end
  1048. * boundaries and then use this function to zero the parts of
  1049. * the blocks that got dropped during the rounding.
  1050. */
  1051. STATIC int
  1052. xfs_zero_remaining_bytes(
  1053. xfs_inode_t *ip,
  1054. xfs_off_t startoff,
  1055. xfs_off_t endoff)
  1056. {
  1057. xfs_bmbt_irec_t imap;
  1058. xfs_fileoff_t offset_fsb;
  1059. xfs_off_t lastoffset;
  1060. xfs_off_t offset;
  1061. xfs_buf_t *bp;
  1062. xfs_mount_t *mp = ip->i_mount;
  1063. int nimap;
  1064. int error = 0;
  1065. /*
  1066. * Avoid doing I/O beyond eof - it's not necessary
  1067. * since nothing can read beyond eof. The space will
  1068. * be zeroed when the file is extended anyway.
  1069. */
  1070. if (startoff >= XFS_ISIZE(ip))
  1071. return 0;
  1072. if (endoff > XFS_ISIZE(ip))
  1073. endoff = XFS_ISIZE(ip);
  1074. bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
  1075. mp->m_rtdev_targp : mp->m_ddev_targp,
  1076. BTOBB(mp->m_sb.sb_blocksize), 0);
  1077. if (!bp)
  1078. return XFS_ERROR(ENOMEM);
  1079. xfs_buf_unlock(bp);
  1080. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  1081. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1082. nimap = 1;
  1083. error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
  1084. if (error || nimap < 1)
  1085. break;
  1086. ASSERT(imap.br_blockcount >= 1);
  1087. ASSERT(imap.br_startoff == offset_fsb);
  1088. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  1089. if (lastoffset > endoff)
  1090. lastoffset = endoff;
  1091. if (imap.br_startblock == HOLESTARTBLOCK)
  1092. continue;
  1093. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1094. if (imap.br_state == XFS_EXT_UNWRITTEN)
  1095. continue;
  1096. XFS_BUF_UNDONE(bp);
  1097. XFS_BUF_UNWRITE(bp);
  1098. XFS_BUF_READ(bp);
  1099. XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
  1100. xfsbdstrat(mp, bp);
  1101. error = xfs_buf_iowait(bp);
  1102. if (error) {
  1103. xfs_buf_ioerror_alert(bp,
  1104. "xfs_zero_remaining_bytes(read)");
  1105. break;
  1106. }
  1107. memset(bp->b_addr +
  1108. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  1109. 0, lastoffset - offset + 1);
  1110. XFS_BUF_UNDONE(bp);
  1111. XFS_BUF_UNREAD(bp);
  1112. XFS_BUF_WRITE(bp);
  1113. xfsbdstrat(mp, bp);
  1114. error = xfs_buf_iowait(bp);
  1115. if (error) {
  1116. xfs_buf_ioerror_alert(bp,
  1117. "xfs_zero_remaining_bytes(write)");
  1118. break;
  1119. }
  1120. }
  1121. xfs_buf_free(bp);
  1122. return error;
  1123. }
  1124. /*
  1125. * xfs_free_file_space()
  1126. * This routine frees disk space for the given file.
  1127. *
  1128. * This routine is only called by xfs_change_file_space
  1129. * for an UNRESVSP type call.
  1130. *
  1131. * RETURNS:
  1132. * 0 on success
  1133. * errno on error
  1134. *
  1135. */
  1136. STATIC int
  1137. xfs_free_file_space(
  1138. xfs_inode_t *ip,
  1139. xfs_off_t offset,
  1140. xfs_off_t len,
  1141. int attr_flags)
  1142. {
  1143. int committed;
  1144. int done;
  1145. xfs_fileoff_t endoffset_fsb;
  1146. int error;
  1147. xfs_fsblock_t firstfsb;
  1148. xfs_bmap_free_t free_list;
  1149. xfs_bmbt_irec_t imap;
  1150. xfs_off_t ioffset;
  1151. xfs_extlen_t mod=0;
  1152. xfs_mount_t *mp;
  1153. int nimap;
  1154. uint resblks;
  1155. xfs_off_t rounding;
  1156. int rt;
  1157. xfs_fileoff_t startoffset_fsb;
  1158. xfs_trans_t *tp;
  1159. int need_iolock = 1;
  1160. mp = ip->i_mount;
  1161. trace_xfs_free_file_space(ip);
  1162. error = xfs_qm_dqattach(ip, 0);
  1163. if (error)
  1164. return error;
  1165. error = 0;
  1166. if (len <= 0) /* if nothing being freed */
  1167. return error;
  1168. rt = XFS_IS_REALTIME_INODE(ip);
  1169. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1170. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1171. if (attr_flags & XFS_ATTR_NOLOCK)
  1172. need_iolock = 0;
  1173. if (need_iolock) {
  1174. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1175. /* wait for the completion of any pending DIOs */
  1176. inode_dio_wait(VFS_I(ip));
  1177. }
  1178. rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1179. ioffset = offset & ~(rounding - 1);
  1180. error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  1181. ioffset, -1);
  1182. if (error)
  1183. goto out_unlock_iolock;
  1184. truncate_pagecache_range(VFS_I(ip), ioffset, -1);
  1185. /*
  1186. * Need to zero the stuff we're not freeing, on disk.
  1187. * If it's a realtime file & can't use unwritten extents then we
  1188. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  1189. * will take care of it for us.
  1190. */
  1191. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1192. nimap = 1;
  1193. error = xfs_bmapi_read(ip, startoffset_fsb, 1,
  1194. &imap, &nimap, 0);
  1195. if (error)
  1196. goto out_unlock_iolock;
  1197. ASSERT(nimap == 0 || nimap == 1);
  1198. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1199. xfs_daddr_t block;
  1200. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1201. block = imap.br_startblock;
  1202. mod = do_div(block, mp->m_sb.sb_rextsize);
  1203. if (mod)
  1204. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  1205. }
  1206. nimap = 1;
  1207. error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
  1208. &imap, &nimap, 0);
  1209. if (error)
  1210. goto out_unlock_iolock;
  1211. ASSERT(nimap == 0 || nimap == 1);
  1212. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1213. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1214. mod++;
  1215. if (mod && (mod != mp->m_sb.sb_rextsize))
  1216. endoffset_fsb -= mod;
  1217. }
  1218. }
  1219. if ((done = (endoffset_fsb <= startoffset_fsb)))
  1220. /*
  1221. * One contiguous piece to clear
  1222. */
  1223. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  1224. else {
  1225. /*
  1226. * Some full blocks, possibly two pieces to clear
  1227. */
  1228. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  1229. error = xfs_zero_remaining_bytes(ip, offset,
  1230. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  1231. if (!error &&
  1232. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  1233. error = xfs_zero_remaining_bytes(ip,
  1234. XFS_FSB_TO_B(mp, endoffset_fsb),
  1235. offset + len - 1);
  1236. }
  1237. /*
  1238. * free file space until done or until there is an error
  1239. */
  1240. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1241. while (!error && !done) {
  1242. /*
  1243. * allocate and setup the transaction. Allow this
  1244. * transaction to dip into the reserve blocks to ensure
  1245. * the freeing of the space succeeds at ENOSPC.
  1246. */
  1247. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1248. tp->t_flags |= XFS_TRANS_RESERVE;
  1249. error = xfs_trans_reserve(tp,
  1250. resblks,
  1251. XFS_WRITE_LOG_RES(mp),
  1252. 0,
  1253. XFS_TRANS_PERM_LOG_RES,
  1254. XFS_WRITE_LOG_COUNT);
  1255. /*
  1256. * check for running out of space
  1257. */
  1258. if (error) {
  1259. /*
  1260. * Free the transaction structure.
  1261. */
  1262. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1263. xfs_trans_cancel(tp, 0);
  1264. break;
  1265. }
  1266. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1267. error = xfs_trans_reserve_quota(tp, mp,
  1268. ip->i_udquot, ip->i_gdquot, ip->i_pdquot,
  1269. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  1270. if (error)
  1271. goto error1;
  1272. xfs_trans_ijoin(tp, ip, 0);
  1273. /*
  1274. * issue the bunmapi() call to free the blocks
  1275. */
  1276. xfs_bmap_init(&free_list, &firstfsb);
  1277. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  1278. endoffset_fsb - startoffset_fsb,
  1279. 0, 2, &firstfsb, &free_list, &done);
  1280. if (error) {
  1281. goto error0;
  1282. }
  1283. /*
  1284. * complete the transaction
  1285. */
  1286. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1287. if (error) {
  1288. goto error0;
  1289. }
  1290. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1291. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1292. }
  1293. out_unlock_iolock:
  1294. if (need_iolock)
  1295. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1296. return error;
  1297. error0:
  1298. xfs_bmap_cancel(&free_list);
  1299. error1:
  1300. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1301. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  1302. XFS_ILOCK_EXCL);
  1303. return error;
  1304. }
  1305. STATIC int
  1306. xfs_zero_file_space(
  1307. struct xfs_inode *ip,
  1308. xfs_off_t offset,
  1309. xfs_off_t len,
  1310. int attr_flags)
  1311. {
  1312. struct xfs_mount *mp = ip->i_mount;
  1313. uint granularity;
  1314. xfs_off_t start_boundary;
  1315. xfs_off_t end_boundary;
  1316. int error;
  1317. granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1318. /*
  1319. * Round the range of extents we are going to convert inwards. If the
  1320. * offset is aligned, then it doesn't get changed so we zero from the
  1321. * start of the block offset points to.
  1322. */
  1323. start_boundary = round_up(offset, granularity);
  1324. end_boundary = round_down(offset + len, granularity);
  1325. ASSERT(start_boundary >= offset);
  1326. ASSERT(end_boundary <= offset + len);
  1327. if (!(attr_flags & XFS_ATTR_NOLOCK))
  1328. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1329. if (start_boundary < end_boundary - 1) {
  1330. /* punch out the page cache over the conversion range */
  1331. truncate_pagecache_range(VFS_I(ip), start_boundary,
  1332. end_boundary - 1);
  1333. /* convert the blocks */
  1334. error = xfs_alloc_file_space(ip, start_boundary,
  1335. end_boundary - start_boundary - 1,
  1336. XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT,
  1337. attr_flags);
  1338. if (error)
  1339. goto out_unlock;
  1340. /* We've handled the interior of the range, now for the edges */
  1341. if (start_boundary != offset)
  1342. error = xfs_iozero(ip, offset, start_boundary - offset);
  1343. if (error)
  1344. goto out_unlock;
  1345. if (end_boundary != offset + len)
  1346. error = xfs_iozero(ip, end_boundary,
  1347. offset + len - end_boundary);
  1348. } else {
  1349. /*
  1350. * It's either a sub-granularity range or the range spanned lies
  1351. * partially across two adjacent blocks.
  1352. */
  1353. error = xfs_iozero(ip, offset, len);
  1354. }
  1355. out_unlock:
  1356. if (!(attr_flags & XFS_ATTR_NOLOCK))
  1357. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1358. return error;
  1359. }
  1360. /*
  1361. * xfs_change_file_space()
  1362. * This routine allocates or frees disk space for the given file.
  1363. * The user specified parameters are checked for alignment and size
  1364. * limitations.
  1365. *
  1366. * RETURNS:
  1367. * 0 on success
  1368. * errno on error
  1369. *
  1370. */
  1371. int
  1372. xfs_change_file_space(
  1373. xfs_inode_t *ip,
  1374. int cmd,
  1375. xfs_flock64_t *bf,
  1376. xfs_off_t offset,
  1377. int attr_flags)
  1378. {
  1379. xfs_mount_t *mp = ip->i_mount;
  1380. int clrprealloc;
  1381. int error;
  1382. xfs_fsize_t fsize;
  1383. int setprealloc;
  1384. xfs_off_t startoffset;
  1385. xfs_trans_t *tp;
  1386. struct iattr iattr;
  1387. if (!S_ISREG(ip->i_d.di_mode))
  1388. return XFS_ERROR(EINVAL);
  1389. switch (bf->l_whence) {
  1390. case 0: /*SEEK_SET*/
  1391. break;
  1392. case 1: /*SEEK_CUR*/
  1393. bf->l_start += offset;
  1394. break;
  1395. case 2: /*SEEK_END*/
  1396. bf->l_start += XFS_ISIZE(ip);
  1397. break;
  1398. default:
  1399. return XFS_ERROR(EINVAL);
  1400. }
  1401. /*
  1402. * length of <= 0 for resv/unresv/zero is invalid. length for
  1403. * alloc/free is ignored completely and we have no idea what userspace
  1404. * might have set it to, so set it to zero to allow range
  1405. * checks to pass.
  1406. */
  1407. switch (cmd) {
  1408. case XFS_IOC_ZERO_RANGE:
  1409. case XFS_IOC_RESVSP:
  1410. case XFS_IOC_RESVSP64:
  1411. case XFS_IOC_UNRESVSP:
  1412. case XFS_IOC_UNRESVSP64:
  1413. if (bf->l_len <= 0)
  1414. return XFS_ERROR(EINVAL);
  1415. break;
  1416. default:
  1417. bf->l_len = 0;
  1418. break;
  1419. }
  1420. if (bf->l_start < 0 ||
  1421. bf->l_start > mp->m_super->s_maxbytes ||
  1422. bf->l_start + bf->l_len < 0 ||
  1423. bf->l_start + bf->l_len >= mp->m_super->s_maxbytes)
  1424. return XFS_ERROR(EINVAL);
  1425. bf->l_whence = 0;
  1426. startoffset = bf->l_start;
  1427. fsize = XFS_ISIZE(ip);
  1428. setprealloc = clrprealloc = 0;
  1429. switch (cmd) {
  1430. case XFS_IOC_ZERO_RANGE:
  1431. error = xfs_zero_file_space(ip, startoffset, bf->l_len,
  1432. attr_flags);
  1433. if (error)
  1434. return error;
  1435. setprealloc = 1;
  1436. break;
  1437. case XFS_IOC_RESVSP:
  1438. case XFS_IOC_RESVSP64:
  1439. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  1440. XFS_BMAPI_PREALLOC, attr_flags);
  1441. if (error)
  1442. return error;
  1443. setprealloc = 1;
  1444. break;
  1445. case XFS_IOC_UNRESVSP:
  1446. case XFS_IOC_UNRESVSP64:
  1447. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  1448. attr_flags)))
  1449. return error;
  1450. break;
  1451. case XFS_IOC_ALLOCSP:
  1452. case XFS_IOC_ALLOCSP64:
  1453. case XFS_IOC_FREESP:
  1454. case XFS_IOC_FREESP64:
  1455. /*
  1456. * These operations actually do IO when extending the file, but
  1457. * the allocation is done seperately to the zeroing that is
  1458. * done. This set of operations need to be serialised against
  1459. * other IO operations, such as truncate and buffered IO. We
  1460. * need to take the IOLOCK here to serialise the allocation and
  1461. * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
  1462. * truncate, direct IO) from racing against the transient
  1463. * allocated but not written state we can have here.
  1464. */
  1465. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1466. if (startoffset > fsize) {
  1467. error = xfs_alloc_file_space(ip, fsize,
  1468. startoffset - fsize, 0,
  1469. attr_flags | XFS_ATTR_NOLOCK);
  1470. if (error) {
  1471. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1472. break;
  1473. }
  1474. }
  1475. iattr.ia_valid = ATTR_SIZE;
  1476. iattr.ia_size = startoffset;
  1477. error = xfs_setattr_size(ip, &iattr,
  1478. attr_flags | XFS_ATTR_NOLOCK);
  1479. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1480. if (error)
  1481. return error;
  1482. clrprealloc = 1;
  1483. break;
  1484. default:
  1485. ASSERT(0);
  1486. return XFS_ERROR(EINVAL);
  1487. }
  1488. /*
  1489. * update the inode timestamp, mode, and prealloc flag bits
  1490. */
  1491. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  1492. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  1493. 0, 0, 0))) {
  1494. /* ASSERT(0); */
  1495. xfs_trans_cancel(tp, 0);
  1496. return error;
  1497. }
  1498. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1499. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1500. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  1501. ip->i_d.di_mode &= ~S_ISUID;
  1502. /*
  1503. * Note that we don't have to worry about mandatory
  1504. * file locking being disabled here because we only
  1505. * clear the S_ISGID bit if the Group execute bit is
  1506. * on, but if it was on then mandatory locking wouldn't
  1507. * have been enabled.
  1508. */
  1509. if (ip->i_d.di_mode & S_IXGRP)
  1510. ip->i_d.di_mode &= ~S_ISGID;
  1511. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1512. }
  1513. if (setprealloc)
  1514. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  1515. else if (clrprealloc)
  1516. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  1517. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1518. if (attr_flags & XFS_ATTR_SYNC)
  1519. xfs_trans_set_sync(tp);
  1520. return xfs_trans_commit(tp, 0);
  1521. }