balloc.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext2/balloc.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card ([email protected])
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * Enhanced block allocation by Stephen Tweedie ([email protected]), 1993
  11. * Big-endian to little-endian byte-swapping/bitmaps by
  12. * David S. Miller ([email protected]), 1995
  13. */
  14. #include "ext2.h"
  15. #include <linux/quotaops.h>
  16. #include <linux/slab.h>
  17. #include <linux/sched.h>
  18. #include <linux/cred.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/capability.h>
  21. /*
  22. * balloc.c contains the blocks allocation and deallocation routines
  23. */
  24. /*
  25. * The free blocks are managed by bitmaps. A file system contains several
  26. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  27. * block for inodes, N blocks for the inode table and data blocks.
  28. *
  29. * The file system contains group descriptors which are located after the
  30. * super block. Each descriptor contains the number of the bitmap block and
  31. * the free blocks count in the block. The descriptors are loaded in memory
  32. * when a file system is mounted (see ext2_fill_super).
  33. */
  34. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  35. struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
  36. unsigned int block_group,
  37. struct buffer_head ** bh)
  38. {
  39. unsigned long group_desc;
  40. unsigned long offset;
  41. struct ext2_group_desc * desc;
  42. struct ext2_sb_info *sbi = EXT2_SB(sb);
  43. if (block_group >= sbi->s_groups_count) {
  44. WARN(1, "block_group >= groups_count - "
  45. "block_group = %d, groups_count = %lu",
  46. block_group, sbi->s_groups_count);
  47. return NULL;
  48. }
  49. group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);
  50. offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);
  51. if (!sbi->s_group_desc[group_desc]) {
  52. WARN(1, "Group descriptor not loaded - "
  53. "block_group = %d, group_desc = %lu, desc = %lu",
  54. block_group, group_desc, offset);
  55. return NULL;
  56. }
  57. desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
  58. if (bh)
  59. *bh = sbi->s_group_desc[group_desc];
  60. return desc + offset;
  61. }
  62. static int ext2_valid_block_bitmap(struct super_block *sb,
  63. struct ext2_group_desc *desc,
  64. unsigned int block_group,
  65. struct buffer_head *bh)
  66. {
  67. ext2_grpblk_t offset;
  68. ext2_grpblk_t next_zero_bit;
  69. ext2_fsblk_t bitmap_blk;
  70. ext2_fsblk_t group_first_block;
  71. group_first_block = ext2_group_first_block_no(sb, block_group);
  72. /* check whether block bitmap block number is set */
  73. bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
  74. offset = bitmap_blk - group_first_block;
  75. if (!ext2_test_bit(offset, bh->b_data))
  76. /* bad block bitmap */
  77. goto err_out;
  78. /* check whether the inode bitmap block number is set */
  79. bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
  80. offset = bitmap_blk - group_first_block;
  81. if (!ext2_test_bit(offset, bh->b_data))
  82. /* bad block bitmap */
  83. goto err_out;
  84. /* check whether the inode table block number is set */
  85. bitmap_blk = le32_to_cpu(desc->bg_inode_table);
  86. offset = bitmap_blk - group_first_block;
  87. next_zero_bit = ext2_find_next_zero_bit(bh->b_data,
  88. offset + EXT2_SB(sb)->s_itb_per_group,
  89. offset);
  90. if (next_zero_bit >= offset + EXT2_SB(sb)->s_itb_per_group)
  91. /* good bitmap for inode tables */
  92. return 1;
  93. err_out:
  94. ext2_error(sb, __func__,
  95. "Invalid block bitmap - "
  96. "block_group = %d, block = %lu",
  97. block_group, bitmap_blk);
  98. return 0;
  99. }
  100. /*
  101. * Read the bitmap for a given block_group,and validate the
  102. * bits for block/inode/inode tables are set in the bitmaps
  103. *
  104. * Return buffer_head on success or NULL in case of failure.
  105. */
  106. static struct buffer_head *
  107. read_block_bitmap(struct super_block *sb, unsigned int block_group)
  108. {
  109. struct ext2_group_desc * desc;
  110. struct buffer_head * bh = NULL;
  111. ext2_fsblk_t bitmap_blk;
  112. int ret;
  113. desc = ext2_get_group_desc(sb, block_group, NULL);
  114. if (!desc)
  115. return NULL;
  116. bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
  117. bh = sb_getblk(sb, bitmap_blk);
  118. if (unlikely(!bh)) {
  119. ext2_error(sb, __func__,
  120. "Cannot read block bitmap - "
  121. "block_group = %d, block_bitmap = %u",
  122. block_group, le32_to_cpu(desc->bg_block_bitmap));
  123. return NULL;
  124. }
  125. ret = bh_read(bh, 0);
  126. if (ret > 0)
  127. return bh;
  128. if (ret < 0) {
  129. brelse(bh);
  130. ext2_error(sb, __func__,
  131. "Cannot read block bitmap - "
  132. "block_group = %d, block_bitmap = %u",
  133. block_group, le32_to_cpu(desc->bg_block_bitmap));
  134. return NULL;
  135. }
  136. ext2_valid_block_bitmap(sb, desc, block_group, bh);
  137. /*
  138. * file system mounted not to panic on error, continue with corrupt
  139. * bitmap
  140. */
  141. return bh;
  142. }
  143. static void group_adjust_blocks(struct super_block *sb, int group_no,
  144. struct ext2_group_desc *desc, struct buffer_head *bh, int count)
  145. {
  146. if (count) {
  147. struct ext2_sb_info *sbi = EXT2_SB(sb);
  148. unsigned free_blocks;
  149. spin_lock(sb_bgl_lock(sbi, group_no));
  150. free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
  151. desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
  152. spin_unlock(sb_bgl_lock(sbi, group_no));
  153. mark_buffer_dirty(bh);
  154. }
  155. }
  156. /*
  157. * The reservation window structure operations
  158. * --------------------------------------------
  159. * Operations include:
  160. * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
  161. *
  162. * We use a red-black tree to represent per-filesystem reservation
  163. * windows.
  164. *
  165. */
  166. /**
  167. * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
  168. * @root: root of per-filesystem reservation rb tree
  169. * @verbose: verbose mode
  170. * @fn: function which wishes to dump the reservation map
  171. *
  172. * If verbose is turned on, it will print the whole block reservation
  173. * windows(start, end). Otherwise, it will only print out the "bad" windows,
  174. * those windows that overlap with their immediate neighbors.
  175. */
  176. #if 1
  177. static void __rsv_window_dump(struct rb_root *root, int verbose,
  178. const char *fn)
  179. {
  180. struct rb_node *n;
  181. struct ext2_reserve_window_node *rsv, *prev;
  182. int bad;
  183. restart:
  184. n = rb_first(root);
  185. bad = 0;
  186. prev = NULL;
  187. printk("Block Allocation Reservation Windows Map (%s):\n", fn);
  188. while (n) {
  189. rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
  190. if (verbose)
  191. printk("reservation window 0x%p "
  192. "start: %lu, end: %lu\n",
  193. rsv, rsv->rsv_start, rsv->rsv_end);
  194. if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
  195. printk("Bad reservation %p (start >= end)\n",
  196. rsv);
  197. bad = 1;
  198. }
  199. if (prev && prev->rsv_end >= rsv->rsv_start) {
  200. printk("Bad reservation %p (prev->end >= start)\n",
  201. rsv);
  202. bad = 1;
  203. }
  204. if (bad) {
  205. if (!verbose) {
  206. printk("Restarting reservation walk in verbose mode\n");
  207. verbose = 1;
  208. goto restart;
  209. }
  210. }
  211. n = rb_next(n);
  212. prev = rsv;
  213. }
  214. printk("Window map complete.\n");
  215. BUG_ON(bad);
  216. }
  217. #define rsv_window_dump(root, verbose) \
  218. __rsv_window_dump((root), (verbose), __func__)
  219. #else
  220. #define rsv_window_dump(root, verbose) do {} while (0)
  221. #endif
  222. /**
  223. * goal_in_my_reservation()
  224. * @rsv: inode's reservation window
  225. * @grp_goal: given goal block relative to the allocation block group
  226. * @group: the current allocation block group
  227. * @sb: filesystem super block
  228. *
  229. * Test if the given goal block (group relative) is within the file's
  230. * own block reservation window range.
  231. *
  232. * If the reservation window is outside the goal allocation group, return 0;
  233. * grp_goal (given goal block) could be -1, which means no specific
  234. * goal block. In this case, always return 1.
  235. * If the goal block is within the reservation window, return 1;
  236. * otherwise, return 0;
  237. */
  238. static int
  239. goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal,
  240. unsigned int group, struct super_block * sb)
  241. {
  242. ext2_fsblk_t group_first_block, group_last_block;
  243. group_first_block = ext2_group_first_block_no(sb, group);
  244. group_last_block = ext2_group_last_block_no(sb, group);
  245. if ((rsv->_rsv_start > group_last_block) ||
  246. (rsv->_rsv_end < group_first_block))
  247. return 0;
  248. if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
  249. || (grp_goal + group_first_block > rsv->_rsv_end)))
  250. return 0;
  251. return 1;
  252. }
  253. /**
  254. * search_reserve_window()
  255. * @root: root of reservation tree
  256. * @goal: target allocation block
  257. *
  258. * Find the reserved window which includes the goal, or the previous one
  259. * if the goal is not in any window.
  260. * Returns NULL if there are no windows or if all windows start after the goal.
  261. */
  262. static struct ext2_reserve_window_node *
  263. search_reserve_window(struct rb_root *root, ext2_fsblk_t goal)
  264. {
  265. struct rb_node *n = root->rb_node;
  266. struct ext2_reserve_window_node *rsv;
  267. if (!n)
  268. return NULL;
  269. do {
  270. rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
  271. if (goal < rsv->rsv_start)
  272. n = n->rb_left;
  273. else if (goal > rsv->rsv_end)
  274. n = n->rb_right;
  275. else
  276. return rsv;
  277. } while (n);
  278. /*
  279. * We've fallen off the end of the tree: the goal wasn't inside
  280. * any particular node. OK, the previous node must be to one
  281. * side of the interval containing the goal. If it's the RHS,
  282. * we need to back up one.
  283. */
  284. if (rsv->rsv_start > goal) {
  285. n = rb_prev(&rsv->rsv_node);
  286. rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
  287. }
  288. return rsv;
  289. }
  290. /*
  291. * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree.
  292. * @sb: super block
  293. * @rsv: reservation window to add
  294. *
  295. * Must be called with rsv_lock held.
  296. */
  297. void ext2_rsv_window_add(struct super_block *sb,
  298. struct ext2_reserve_window_node *rsv)
  299. {
  300. struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root;
  301. struct rb_node *node = &rsv->rsv_node;
  302. ext2_fsblk_t start = rsv->rsv_start;
  303. struct rb_node ** p = &root->rb_node;
  304. struct rb_node * parent = NULL;
  305. struct ext2_reserve_window_node *this;
  306. while (*p)
  307. {
  308. parent = *p;
  309. this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node);
  310. if (start < this->rsv_start)
  311. p = &(*p)->rb_left;
  312. else if (start > this->rsv_end)
  313. p = &(*p)->rb_right;
  314. else {
  315. rsv_window_dump(root, 1);
  316. BUG();
  317. }
  318. }
  319. rb_link_node(node, parent, p);
  320. rb_insert_color(node, root);
  321. }
  322. /**
  323. * rsv_window_remove() -- unlink a window from the reservation rb tree
  324. * @sb: super block
  325. * @rsv: reservation window to remove
  326. *
  327. * Mark the block reservation window as not allocated, and unlink it
  328. * from the filesystem reservation window rb tree. Must be called with
  329. * rsv_lock held.
  330. */
  331. static void rsv_window_remove(struct super_block *sb,
  332. struct ext2_reserve_window_node *rsv)
  333. {
  334. rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  335. rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  336. rsv->rsv_alloc_hit = 0;
  337. rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root);
  338. }
  339. /*
  340. * rsv_is_empty() -- Check if the reservation window is allocated.
  341. * @rsv: given reservation window to check
  342. *
  343. * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED.
  344. */
  345. static inline int rsv_is_empty(struct ext2_reserve_window *rsv)
  346. {
  347. /* a valid reservation end block could not be 0 */
  348. return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED);
  349. }
  350. /**
  351. * ext2_init_block_alloc_info()
  352. * @inode: file inode structure
  353. *
  354. * Allocate and initialize the reservation window structure, and
  355. * link the window to the ext2 inode structure at last
  356. *
  357. * The reservation window structure is only dynamically allocated
  358. * and linked to ext2 inode the first time the open file
  359. * needs a new block. So, before every ext2_new_block(s) call, for
  360. * regular files, we should check whether the reservation window
  361. * structure exists or not. In the latter case, this function is called.
  362. * Fail to do so will result in block reservation being turned off for that
  363. * open file.
  364. *
  365. * This function is called from ext2_get_blocks_handle(), also called
  366. * when setting the reservation window size through ioctl before the file
  367. * is open for write (needs block allocation).
  368. *
  369. * Needs truncate_mutex protection prior to calling this function.
  370. */
  371. void ext2_init_block_alloc_info(struct inode *inode)
  372. {
  373. struct ext2_inode_info *ei = EXT2_I(inode);
  374. struct ext2_block_alloc_info *block_i;
  375. struct super_block *sb = inode->i_sb;
  376. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  377. if (block_i) {
  378. struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node;
  379. rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  380. rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  381. /*
  382. * if filesystem is mounted with NORESERVATION, the goal
  383. * reservation window size is set to zero to indicate
  384. * block reservation is off
  385. */
  386. if (!test_opt(sb, RESERVATION))
  387. rsv->rsv_goal_size = 0;
  388. else
  389. rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;
  390. rsv->rsv_alloc_hit = 0;
  391. block_i->last_alloc_logical_block = 0;
  392. block_i->last_alloc_physical_block = 0;
  393. }
  394. ei->i_block_alloc_info = block_i;
  395. }
  396. /**
  397. * ext2_discard_reservation()
  398. * @inode: inode
  399. *
  400. * Discard(free) block reservation window on last file close, or truncate
  401. * or at last iput().
  402. *
  403. * It is being called in three cases:
  404. * ext2_release_file(): last writer closes the file
  405. * ext2_clear_inode(): last iput(), when nobody links to this file.
  406. * ext2_truncate(): when the block indirect map is about to change.
  407. */
  408. void ext2_discard_reservation(struct inode *inode)
  409. {
  410. struct ext2_inode_info *ei = EXT2_I(inode);
  411. struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
  412. struct ext2_reserve_window_node *rsv;
  413. spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock;
  414. if (!block_i)
  415. return;
  416. rsv = &block_i->rsv_window_node;
  417. if (!rsv_is_empty(&rsv->rsv_window)) {
  418. spin_lock(rsv_lock);
  419. if (!rsv_is_empty(&rsv->rsv_window))
  420. rsv_window_remove(inode->i_sb, rsv);
  421. spin_unlock(rsv_lock);
  422. }
  423. }
  424. /**
  425. * ext2_free_blocks() -- Free given blocks and update quota and i_blocks
  426. * @inode: inode
  427. * @block: start physical block to free
  428. * @count: number of blocks to free
  429. */
  430. void ext2_free_blocks (struct inode * inode, unsigned long block,
  431. unsigned long count)
  432. {
  433. struct buffer_head *bitmap_bh = NULL;
  434. struct buffer_head * bh2;
  435. unsigned long block_group;
  436. unsigned long bit;
  437. unsigned long i;
  438. unsigned long overflow;
  439. struct super_block * sb = inode->i_sb;
  440. struct ext2_sb_info * sbi = EXT2_SB(sb);
  441. struct ext2_group_desc * desc;
  442. struct ext2_super_block * es = sbi->s_es;
  443. unsigned freed = 0, group_freed;
  444. if (!ext2_data_block_valid(sbi, block, count)) {
  445. ext2_error (sb, "ext2_free_blocks",
  446. "Freeing blocks not in datazone - "
  447. "block = %lu, count = %lu", block, count);
  448. goto error_return;
  449. }
  450. ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
  451. do_more:
  452. overflow = 0;
  453. block_group = (block - le32_to_cpu(es->s_first_data_block)) /
  454. EXT2_BLOCKS_PER_GROUP(sb);
  455. bit = (block - le32_to_cpu(es->s_first_data_block)) %
  456. EXT2_BLOCKS_PER_GROUP(sb);
  457. /*
  458. * Check to see if we are freeing blocks across a group
  459. * boundary.
  460. */
  461. if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
  462. overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
  463. count -= overflow;
  464. }
  465. brelse(bitmap_bh);
  466. bitmap_bh = read_block_bitmap(sb, block_group);
  467. if (!bitmap_bh)
  468. goto error_return;
  469. desc = ext2_get_group_desc (sb, block_group, &bh2);
  470. if (!desc)
  471. goto error_return;
  472. if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
  473. in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
  474. in_range (block, le32_to_cpu(desc->bg_inode_table),
  475. sbi->s_itb_per_group) ||
  476. in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
  477. sbi->s_itb_per_group)) {
  478. ext2_error (sb, "ext2_free_blocks",
  479. "Freeing blocks in system zones - "
  480. "Block = %lu, count = %lu",
  481. block, count);
  482. goto error_return;
  483. }
  484. for (i = 0, group_freed = 0; i < count; i++) {
  485. if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  486. bit + i, bitmap_bh->b_data)) {
  487. ext2_error(sb, __func__,
  488. "bit already cleared for block %lu", block + i);
  489. } else {
  490. group_freed++;
  491. }
  492. }
  493. mark_buffer_dirty(bitmap_bh);
  494. if (sb->s_flags & SB_SYNCHRONOUS)
  495. sync_dirty_buffer(bitmap_bh);
  496. group_adjust_blocks(sb, block_group, desc, bh2, group_freed);
  497. freed += group_freed;
  498. if (overflow) {
  499. block += count;
  500. count = overflow;
  501. goto do_more;
  502. }
  503. error_return:
  504. brelse(bitmap_bh);
  505. if (freed) {
  506. percpu_counter_add(&sbi->s_freeblocks_counter, freed);
  507. dquot_free_block_nodirty(inode, freed);
  508. mark_inode_dirty(inode);
  509. }
  510. }
  511. /**
  512. * bitmap_search_next_usable_block()
  513. * @start: the starting block (group relative) of the search
  514. * @bh: bufferhead contains the block group bitmap
  515. * @maxblocks: the ending block (group relative) of the reservation
  516. *
  517. * The bitmap search --- search forward through the actual bitmap on disk until
  518. * we find a bit free.
  519. */
  520. static ext2_grpblk_t
  521. bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,
  522. ext2_grpblk_t maxblocks)
  523. {
  524. ext2_grpblk_t next;
  525. next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);
  526. if (next >= maxblocks)
  527. return -1;
  528. return next;
  529. }
  530. /**
  531. * find_next_usable_block()
  532. * @start: the starting block (group relative) to find next
  533. * allocatable block in bitmap.
  534. * @bh: bufferhead contains the block group bitmap
  535. * @maxblocks: the ending block (group relative) for the search
  536. *
  537. * Find an allocatable block in a bitmap. We perform the "most
  538. * appropriate allocation" algorithm of looking for a free block near
  539. * the initial goal; then for a free byte somewhere in the bitmap;
  540. * then for any free bit in the bitmap.
  541. */
  542. static ext2_grpblk_t
  543. find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
  544. {
  545. ext2_grpblk_t here, next;
  546. char *p, *r;
  547. if (start > 0) {
  548. /*
  549. * The goal was occupied; search forward for a free
  550. * block within the next XX blocks.
  551. *
  552. * end_goal is more or less random, but it has to be
  553. * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
  554. * next 64-bit boundary is simple..
  555. */
  556. ext2_grpblk_t end_goal = (start + 63) & ~63;
  557. if (end_goal > maxblocks)
  558. end_goal = maxblocks;
  559. here = ext2_find_next_zero_bit(bh->b_data, end_goal, start);
  560. if (here < end_goal)
  561. return here;
  562. ext2_debug("Bit not found near goal\n");
  563. }
  564. here = start;
  565. if (here < 0)
  566. here = 0;
  567. p = ((char *)bh->b_data) + (here >> 3);
  568. r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
  569. next = (r - ((char *)bh->b_data)) << 3;
  570. if (next < maxblocks && next >= here)
  571. return next;
  572. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  573. return here;
  574. }
  575. /**
  576. * ext2_try_to_allocate()
  577. * @sb: superblock
  578. * @group: given allocation block group
  579. * @bitmap_bh: bufferhead holds the block bitmap
  580. * @grp_goal: given target block within the group
  581. * @count: target number of blocks to allocate
  582. * @my_rsv: reservation window
  583. *
  584. * Attempt to allocate blocks within a give range. Set the range of allocation
  585. * first, then find the first free bit(s) from the bitmap (within the range),
  586. * and at last, allocate the blocks by claiming the found free bit as allocated.
  587. *
  588. * To set the range of this allocation:
  589. * if there is a reservation window, only try to allocate block(s)
  590. * from the file's own reservation window;
  591. * Otherwise, the allocation range starts from the give goal block,
  592. * ends at the block group's last block.
  593. *
  594. * If we failed to allocate the desired block then we may end up crossing to a
  595. * new bitmap.
  596. */
  597. static int
  598. ext2_try_to_allocate(struct super_block *sb, int group,
  599. struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
  600. unsigned long *count,
  601. struct ext2_reserve_window *my_rsv)
  602. {
  603. ext2_fsblk_t group_first_block = ext2_group_first_block_no(sb, group);
  604. ext2_fsblk_t group_last_block = ext2_group_last_block_no(sb, group);
  605. ext2_grpblk_t start, end;
  606. unsigned long num = 0;
  607. start = 0;
  608. end = group_last_block - group_first_block + 1;
  609. /* we do allocation within the reservation window if we have a window */
  610. if (my_rsv) {
  611. if (my_rsv->_rsv_start >= group_first_block)
  612. start = my_rsv->_rsv_start - group_first_block;
  613. if (my_rsv->_rsv_end < group_last_block)
  614. end = my_rsv->_rsv_end - group_first_block + 1;
  615. if (grp_goal < start || grp_goal >= end)
  616. grp_goal = -1;
  617. }
  618. BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb));
  619. if (grp_goal < 0) {
  620. grp_goal = find_next_usable_block(start, bitmap_bh, end);
  621. if (grp_goal < 0)
  622. goto fail_access;
  623. if (!my_rsv) {
  624. int i;
  625. for (i = 0; i < 7 && grp_goal > start &&
  626. !ext2_test_bit(grp_goal - 1,
  627. bitmap_bh->b_data);
  628. i++, grp_goal--)
  629. ;
  630. }
  631. }
  632. for (; num < *count && grp_goal < end; grp_goal++) {
  633. if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group),
  634. grp_goal, bitmap_bh->b_data)) {
  635. if (num == 0)
  636. continue;
  637. break;
  638. }
  639. num++;
  640. }
  641. if (num == 0)
  642. goto fail_access;
  643. *count = num;
  644. return grp_goal - num;
  645. fail_access:
  646. return -1;
  647. }
  648. /**
  649. * find_next_reservable_window():
  650. * find a reservable space within the given range.
  651. * It does not allocate the reservation window for now:
  652. * alloc_new_reservation() will do the work later.
  653. *
  654. * @search_head: the head of the searching list;
  655. * This is not necessarily the list head of the whole filesystem
  656. *
  657. * We have both head and start_block to assist the search
  658. * for the reservable space. The list starts from head,
  659. * but we will shift to the place where start_block is,
  660. * then start from there, when looking for a reservable space.
  661. *
  662. * @sb: the super block.
  663. *
  664. * @start_block: the first block we consider to start the real search from
  665. *
  666. * @last_block:
  667. * the maximum block number that our goal reservable space
  668. * could start from. This is normally the last block in this
  669. * group. The search will end when we found the start of next
  670. * possible reservable space is out of this boundary.
  671. * This could handle the cross boundary reservation window
  672. * request.
  673. *
  674. * basically we search from the given range, rather than the whole
  675. * reservation double linked list, (start_block, last_block)
  676. * to find a free region that is of my size and has not
  677. * been reserved.
  678. *
  679. */
  680. static int find_next_reservable_window(
  681. struct ext2_reserve_window_node *search_head,
  682. struct ext2_reserve_window_node *my_rsv,
  683. struct super_block * sb,
  684. ext2_fsblk_t start_block,
  685. ext2_fsblk_t last_block)
  686. {
  687. struct rb_node *next;
  688. struct ext2_reserve_window_node *rsv, *prev;
  689. ext2_fsblk_t cur;
  690. int size = my_rsv->rsv_goal_size;
  691. /* TODO: make the start of the reservation window byte-aligned */
  692. /* cur = *start_block & ~7;*/
  693. cur = start_block;
  694. rsv = search_head;
  695. if (!rsv)
  696. return -1;
  697. while (1) {
  698. if (cur <= rsv->rsv_end)
  699. cur = rsv->rsv_end + 1;
  700. /* TODO?
  701. * in the case we could not find a reservable space
  702. * that is what is expected, during the re-search, we could
  703. * remember what's the largest reservable space we could have
  704. * and return that one.
  705. *
  706. * For now it will fail if we could not find the reservable
  707. * space with expected-size (or more)...
  708. */
  709. if (cur > last_block)
  710. return -1; /* fail */
  711. prev = rsv;
  712. next = rb_next(&rsv->rsv_node);
  713. rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node);
  714. /*
  715. * Reached the last reservation, we can just append to the
  716. * previous one.
  717. */
  718. if (!next)
  719. break;
  720. if (cur + size <= rsv->rsv_start) {
  721. /*
  722. * Found a reserveable space big enough. We could
  723. * have a reservation across the group boundary here
  724. */
  725. break;
  726. }
  727. }
  728. /*
  729. * we come here either :
  730. * when we reach the end of the whole list,
  731. * and there is empty reservable space after last entry in the list.
  732. * append it to the end of the list.
  733. *
  734. * or we found one reservable space in the middle of the list,
  735. * return the reservation window that we could append to.
  736. * succeed.
  737. */
  738. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  739. rsv_window_remove(sb, my_rsv);
  740. /*
  741. * Let's book the whole available window for now. We will check the
  742. * disk bitmap later and then, if there are free blocks then we adjust
  743. * the window size if it's larger than requested.
  744. * Otherwise, we will remove this node from the tree next time
  745. * call find_next_reservable_window.
  746. */
  747. my_rsv->rsv_start = cur;
  748. my_rsv->rsv_end = cur + size - 1;
  749. my_rsv->rsv_alloc_hit = 0;
  750. if (prev != my_rsv)
  751. ext2_rsv_window_add(sb, my_rsv);
  752. return 0;
  753. }
  754. /**
  755. * alloc_new_reservation()--allocate a new reservation window
  756. *
  757. * To make a new reservation, we search part of the filesystem
  758. * reservation list (the list that inside the group). We try to
  759. * allocate a new reservation window near the allocation goal,
  760. * or the beginning of the group, if there is no goal.
  761. *
  762. * We first find a reservable space after the goal, then from
  763. * there, we check the bitmap for the first free block after
  764. * it. If there is no free block until the end of group, then the
  765. * whole group is full, we failed. Otherwise, check if the free
  766. * block is inside the expected reservable space, if so, we
  767. * succeed.
  768. * If the first free block is outside the reservable space, then
  769. * start from the first free block, we search for next available
  770. * space, and go on.
  771. *
  772. * on succeed, a new reservation will be found and inserted into the list
  773. * It contains at least one free block, and it does not overlap with other
  774. * reservation windows.
  775. *
  776. * failed: we failed to find a reservation window in this group
  777. *
  778. * @my_rsv: the reservation
  779. *
  780. * @grp_goal: The goal (group-relative). It is where the search for a
  781. * free reservable space should start from.
  782. * if we have a goal(goal >0 ), then start from there,
  783. * no goal(goal = -1), we start from the first block
  784. * of the group.
  785. *
  786. * @sb: the super block
  787. * @group: the group we are trying to allocate in
  788. * @bitmap_bh: the block group block bitmap
  789. *
  790. */
  791. static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv,
  792. ext2_grpblk_t grp_goal, struct super_block *sb,
  793. unsigned int group, struct buffer_head *bitmap_bh)
  794. {
  795. struct ext2_reserve_window_node *search_head;
  796. ext2_fsblk_t group_first_block, group_end_block, start_block;
  797. ext2_grpblk_t first_free_block;
  798. struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root;
  799. unsigned long size;
  800. int ret;
  801. spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
  802. group_first_block = ext2_group_first_block_no(sb, group);
  803. group_end_block = ext2_group_last_block_no(sb, group);
  804. if (grp_goal < 0)
  805. start_block = group_first_block;
  806. else
  807. start_block = grp_goal + group_first_block;
  808. size = my_rsv->rsv_goal_size;
  809. if (!rsv_is_empty(&my_rsv->rsv_window)) {
  810. /*
  811. * if the old reservation is cross group boundary
  812. * and if the goal is inside the old reservation window,
  813. * we will come here when we just failed to allocate from
  814. * the first part of the window. We still have another part
  815. * that belongs to the next group. In this case, there is no
  816. * point to discard our window and try to allocate a new one
  817. * in this group(which will fail). we should
  818. * keep the reservation window, just simply move on.
  819. *
  820. * Maybe we could shift the start block of the reservation
  821. * window to the first block of next group.
  822. */
  823. if ((my_rsv->rsv_start <= group_end_block) &&
  824. (my_rsv->rsv_end > group_end_block) &&
  825. (start_block >= my_rsv->rsv_start))
  826. return -1;
  827. if ((my_rsv->rsv_alloc_hit >
  828. (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
  829. /*
  830. * if the previously allocation hit ratio is
  831. * greater than 1/2, then we double the size of
  832. * the reservation window the next time,
  833. * otherwise we keep the same size window
  834. */
  835. size = size * 2;
  836. if (size > EXT2_MAX_RESERVE_BLOCKS)
  837. size = EXT2_MAX_RESERVE_BLOCKS;
  838. my_rsv->rsv_goal_size= size;
  839. }
  840. }
  841. spin_lock(rsv_lock);
  842. /*
  843. * shift the search start to the window near the goal block
  844. */
  845. search_head = search_reserve_window(fs_rsv_root, start_block);
  846. /*
  847. * find_next_reservable_window() simply finds a reservable window
  848. * inside the given range(start_block, group_end_block).
  849. *
  850. * To make sure the reservation window has a free bit inside it, we
  851. * need to check the bitmap after we found a reservable window.
  852. */
  853. retry:
  854. ret = find_next_reservable_window(search_head, my_rsv, sb,
  855. start_block, group_end_block);
  856. if (ret == -1) {
  857. if (!rsv_is_empty(&my_rsv->rsv_window))
  858. rsv_window_remove(sb, my_rsv);
  859. spin_unlock(rsv_lock);
  860. return -1;
  861. }
  862. /*
  863. * On success, find_next_reservable_window() returns the
  864. * reservation window where there is a reservable space after it.
  865. * Before we reserve this reservable space, we need
  866. * to make sure there is at least a free block inside this region.
  867. *
  868. * Search the first free bit on the block bitmap. Search starts from
  869. * the start block of the reservable space we just found.
  870. */
  871. spin_unlock(rsv_lock);
  872. first_free_block = bitmap_search_next_usable_block(
  873. my_rsv->rsv_start - group_first_block,
  874. bitmap_bh, group_end_block - group_first_block + 1);
  875. if (first_free_block < 0) {
  876. /*
  877. * no free block left on the bitmap, no point
  878. * to reserve the space. return failed.
  879. */
  880. spin_lock(rsv_lock);
  881. if (!rsv_is_empty(&my_rsv->rsv_window))
  882. rsv_window_remove(sb, my_rsv);
  883. spin_unlock(rsv_lock);
  884. return -1; /* failed */
  885. }
  886. start_block = first_free_block + group_first_block;
  887. /*
  888. * check if the first free block is within the
  889. * free space we just reserved
  890. */
  891. if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
  892. return 0; /* success */
  893. /*
  894. * if the first free bit we found is out of the reservable space
  895. * continue search for next reservable space,
  896. * start from where the free block is,
  897. * we also shift the list head to where we stopped last time
  898. */
  899. search_head = my_rsv;
  900. spin_lock(rsv_lock);
  901. goto retry;
  902. }
  903. /**
  904. * try_to_extend_reservation()
  905. * @my_rsv: given reservation window
  906. * @sb: super block
  907. * @size: the delta to extend
  908. *
  909. * Attempt to expand the reservation window large enough to have
  910. * required number of free blocks
  911. *
  912. * Since ext2_try_to_allocate() will always allocate blocks within
  913. * the reservation window range, if the window size is too small,
  914. * multiple blocks allocation has to stop at the end of the reservation
  915. * window. To make this more efficient, given the total number of
  916. * blocks needed and the current size of the window, we try to
  917. * expand the reservation window size if necessary on a best-effort
  918. * basis before ext2_new_blocks() tries to allocate blocks.
  919. */
  920. static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv,
  921. struct super_block *sb, int size)
  922. {
  923. struct ext2_reserve_window_node *next_rsv;
  924. struct rb_node *next;
  925. spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
  926. if (!spin_trylock(rsv_lock))
  927. return;
  928. next = rb_next(&my_rsv->rsv_node);
  929. if (!next)
  930. my_rsv->rsv_end += size;
  931. else {
  932. next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node);
  933. if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
  934. my_rsv->rsv_end += size;
  935. else
  936. my_rsv->rsv_end = next_rsv->rsv_start - 1;
  937. }
  938. spin_unlock(rsv_lock);
  939. }
  940. /**
  941. * ext2_try_to_allocate_with_rsv()
  942. * @sb: superblock
  943. * @group: given allocation block group
  944. * @bitmap_bh: bufferhead holds the block bitmap
  945. * @grp_goal: given target block within the group
  946. * @count: target number of blocks to allocate
  947. * @my_rsv: reservation window
  948. *
  949. * This is the main function used to allocate a new block and its reservation
  950. * window.
  951. *
  952. * Each time when a new block allocation is need, first try to allocate from
  953. * its own reservation. If it does not have a reservation window, instead of
  954. * looking for a free bit on bitmap first, then look up the reservation list to
  955. * see if it is inside somebody else's reservation window, we try to allocate a
  956. * reservation window for it starting from the goal first. Then do the block
  957. * allocation within the reservation window.
  958. *
  959. * This will avoid keeping on searching the reservation list again and
  960. * again when somebody is looking for a free block (without
  961. * reservation), and there are lots of free blocks, but they are all
  962. * being reserved.
  963. *
  964. * We use a red-black tree for the per-filesystem reservation list.
  965. */
  966. static ext2_grpblk_t
  967. ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group,
  968. struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
  969. struct ext2_reserve_window_node * my_rsv,
  970. unsigned long *count)
  971. {
  972. ext2_fsblk_t group_first_block, group_last_block;
  973. ext2_grpblk_t ret = 0;
  974. unsigned long num = *count;
  975. /*
  976. * we don't deal with reservation when
  977. * filesystem is mounted without reservation
  978. * or the file is not a regular file
  979. * or last attempt to allocate a block with reservation turned on failed
  980. */
  981. if (my_rsv == NULL) {
  982. return ext2_try_to_allocate(sb, group, bitmap_bh,
  983. grp_goal, count, NULL);
  984. }
  985. /*
  986. * grp_goal is a group relative block number (if there is a goal)
  987. * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb)
  988. * first block is a filesystem wide block number
  989. * first block is the block number of the first block in this group
  990. */
  991. group_first_block = ext2_group_first_block_no(sb, group);
  992. group_last_block = ext2_group_last_block_no(sb, group);
  993. /*
  994. * Basically we will allocate a new block from inode's reservation
  995. * window.
  996. *
  997. * We need to allocate a new reservation window, if:
  998. * a) inode does not have a reservation window; or
  999. * b) last attempt to allocate a block from existing reservation
  1000. * failed; or
  1001. * c) we come here with a goal and with a reservation window
  1002. *
  1003. * We do not need to allocate a new reservation window if we come here
  1004. * at the beginning with a goal and the goal is inside the window, or
  1005. * we don't have a goal but already have a reservation window.
  1006. * then we could go to allocate from the reservation window directly.
  1007. */
  1008. while (1) {
  1009. if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
  1010. !goal_in_my_reservation(&my_rsv->rsv_window,
  1011. grp_goal, group, sb)) {
  1012. if (my_rsv->rsv_goal_size < *count)
  1013. my_rsv->rsv_goal_size = *count;
  1014. ret = alloc_new_reservation(my_rsv, grp_goal, sb,
  1015. group, bitmap_bh);
  1016. if (ret < 0)
  1017. break; /* failed */
  1018. if (!goal_in_my_reservation(&my_rsv->rsv_window,
  1019. grp_goal, group, sb))
  1020. grp_goal = -1;
  1021. } else if (grp_goal >= 0) {
  1022. int curr = my_rsv->rsv_end -
  1023. (grp_goal + group_first_block) + 1;
  1024. if (curr < *count)
  1025. try_to_extend_reservation(my_rsv, sb,
  1026. *count - curr);
  1027. }
  1028. if ((my_rsv->rsv_start > group_last_block) ||
  1029. (my_rsv->rsv_end < group_first_block)) {
  1030. rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1);
  1031. BUG();
  1032. }
  1033. ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal,
  1034. &num, &my_rsv->rsv_window);
  1035. if (ret >= 0) {
  1036. my_rsv->rsv_alloc_hit += num;
  1037. *count = num;
  1038. break; /* succeed */
  1039. }
  1040. num = *count;
  1041. }
  1042. return ret;
  1043. }
  1044. /**
  1045. * ext2_has_free_blocks()
  1046. * @sbi: in-core super block structure.
  1047. *
  1048. * Check if filesystem has at least 1 free block available for allocation.
  1049. */
  1050. static int ext2_has_free_blocks(struct ext2_sb_info *sbi)
  1051. {
  1052. ext2_fsblk_t free_blocks, root_blocks;
  1053. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  1054. root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
  1055. if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
  1056. !uid_eq(sbi->s_resuid, current_fsuid()) &&
  1057. (gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) ||
  1058. !in_group_p (sbi->s_resgid))) {
  1059. return 0;
  1060. }
  1061. return 1;
  1062. }
  1063. /*
  1064. * Returns 1 if the passed-in block region is valid; 0 if some part overlaps
  1065. * with filesystem metadata blocks.
  1066. */
  1067. int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,
  1068. unsigned int count)
  1069. {
  1070. if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
  1071. (start_blk + count - 1 < start_blk) ||
  1072. (start_blk + count - 1 >= le32_to_cpu(sbi->s_es->s_blocks_count)))
  1073. return 0;
  1074. /* Ensure we do not step over superblock */
  1075. if ((start_blk <= sbi->s_sb_block) &&
  1076. (start_blk + count - 1 >= sbi->s_sb_block))
  1077. return 0;
  1078. return 1;
  1079. }
  1080. /*
  1081. * ext2_new_blocks() -- core block(s) allocation function
  1082. * @inode: file inode
  1083. * @goal: given target block(filesystem wide)
  1084. * @count: target number of blocks to allocate
  1085. * @errp: error code
  1086. *
  1087. * ext2_new_blocks uses a goal block to assist allocation. If the goal is
  1088. * free, or there is a free block within 32 blocks of the goal, that block
  1089. * is allocated. Otherwise a forward search is made for a free block; within
  1090. * each block group the search first looks for an entire free byte in the block
  1091. * bitmap, and then for any free bit if that fails.
  1092. * This function also updates quota and i_blocks field.
  1093. */
  1094. ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,
  1095. unsigned long *count, int *errp)
  1096. {
  1097. struct buffer_head *bitmap_bh = NULL;
  1098. struct buffer_head *gdp_bh;
  1099. int group_no;
  1100. int goal_group;
  1101. ext2_grpblk_t grp_target_blk; /* blockgroup relative goal block */
  1102. ext2_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
  1103. ext2_fsblk_t ret_block; /* filesyetem-wide allocated block */
  1104. int bgi; /* blockgroup iteration index */
  1105. int performed_allocation = 0;
  1106. ext2_grpblk_t free_blocks; /* number of free blocks in a group */
  1107. struct super_block *sb;
  1108. struct ext2_group_desc *gdp;
  1109. struct ext2_super_block *es;
  1110. struct ext2_sb_info *sbi;
  1111. struct ext2_reserve_window_node *my_rsv = NULL;
  1112. struct ext2_block_alloc_info *block_i;
  1113. unsigned short windowsz = 0;
  1114. unsigned long ngroups;
  1115. unsigned long num = *count;
  1116. int ret;
  1117. *errp = -ENOSPC;
  1118. sb = inode->i_sb;
  1119. /*
  1120. * Check quota for allocation of this block.
  1121. */
  1122. ret = dquot_alloc_block(inode, num);
  1123. if (ret) {
  1124. *errp = ret;
  1125. return 0;
  1126. }
  1127. sbi = EXT2_SB(sb);
  1128. es = EXT2_SB(sb)->s_es;
  1129. ext2_debug("goal=%lu.\n", goal);
  1130. /*
  1131. * Allocate a block from reservation only when
  1132. * filesystem is mounted with reservation(default,-o reservation), and
  1133. * it's a regular file, and
  1134. * the desired window size is greater than 0 (One could use ioctl
  1135. * command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn off
  1136. * reservation on that particular file)
  1137. */
  1138. block_i = EXT2_I(inode)->i_block_alloc_info;
  1139. if (block_i) {
  1140. windowsz = block_i->rsv_window_node.rsv_goal_size;
  1141. if (windowsz > 0)
  1142. my_rsv = &block_i->rsv_window_node;
  1143. }
  1144. if (!ext2_has_free_blocks(sbi)) {
  1145. *errp = -ENOSPC;
  1146. goto out;
  1147. }
  1148. /*
  1149. * First, test whether the goal block is free.
  1150. */
  1151. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1152. goal >= le32_to_cpu(es->s_blocks_count))
  1153. goal = le32_to_cpu(es->s_first_data_block);
  1154. group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
  1155. EXT2_BLOCKS_PER_GROUP(sb);
  1156. goal_group = group_no;
  1157. retry_alloc:
  1158. gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
  1159. if (!gdp)
  1160. goto io_error;
  1161. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1162. /*
  1163. * if there is not enough free blocks to make a new resevation
  1164. * turn off reservation for this allocation
  1165. */
  1166. if (my_rsv && (free_blocks < windowsz)
  1167. && (free_blocks > 0)
  1168. && (rsv_is_empty(&my_rsv->rsv_window)))
  1169. my_rsv = NULL;
  1170. if (free_blocks > 0) {
  1171. grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
  1172. EXT2_BLOCKS_PER_GROUP(sb));
  1173. /*
  1174. * In case we retry allocation (due to fs reservation not
  1175. * working out or fs corruption), the bitmap_bh is non-null
  1176. * pointer and we have to release it before calling
  1177. * read_block_bitmap().
  1178. */
  1179. brelse(bitmap_bh);
  1180. bitmap_bh = read_block_bitmap(sb, group_no);
  1181. if (!bitmap_bh)
  1182. goto io_error;
  1183. grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
  1184. bitmap_bh, grp_target_blk,
  1185. my_rsv, &num);
  1186. if (grp_alloc_blk >= 0)
  1187. goto allocated;
  1188. }
  1189. ngroups = EXT2_SB(sb)->s_groups_count;
  1190. smp_rmb();
  1191. /*
  1192. * Now search the rest of the groups. We assume that
  1193. * group_no and gdp correctly point to the last group visited.
  1194. */
  1195. for (bgi = 0; bgi < ngroups; bgi++) {
  1196. group_no++;
  1197. if (group_no >= ngroups)
  1198. group_no = 0;
  1199. gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
  1200. if (!gdp)
  1201. goto io_error;
  1202. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1203. /*
  1204. * skip this group (and avoid loading bitmap) if there
  1205. * are no free blocks
  1206. */
  1207. if (!free_blocks)
  1208. continue;
  1209. /*
  1210. * skip this group if the number of
  1211. * free blocks is less than half of the reservation
  1212. * window size.
  1213. */
  1214. if (my_rsv && (free_blocks <= (windowsz/2)))
  1215. continue;
  1216. brelse(bitmap_bh);
  1217. bitmap_bh = read_block_bitmap(sb, group_no);
  1218. if (!bitmap_bh)
  1219. goto io_error;
  1220. /*
  1221. * try to allocate block(s) from this group, without a goal(-1).
  1222. */
  1223. grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
  1224. bitmap_bh, -1, my_rsv, &num);
  1225. if (grp_alloc_blk >= 0)
  1226. goto allocated;
  1227. }
  1228. /*
  1229. * We may end up a bogus earlier ENOSPC error due to
  1230. * filesystem is "full" of reservations, but
  1231. * there maybe indeed free blocks available on disk
  1232. * In this case, we just forget about the reservations
  1233. * just do block allocation as without reservations.
  1234. */
  1235. if (my_rsv) {
  1236. my_rsv = NULL;
  1237. windowsz = 0;
  1238. group_no = goal_group;
  1239. goto retry_alloc;
  1240. }
  1241. /* No space left on the device */
  1242. *errp = -ENOSPC;
  1243. goto out;
  1244. allocated:
  1245. ext2_debug("using block group %d(%d)\n",
  1246. group_no, gdp->bg_free_blocks_count);
  1247. ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);
  1248. if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
  1249. in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
  1250. in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
  1251. EXT2_SB(sb)->s_itb_per_group) ||
  1252. in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
  1253. EXT2_SB(sb)->s_itb_per_group)) {
  1254. ext2_error(sb, "ext2_new_blocks",
  1255. "Allocating block in system zone - "
  1256. "blocks from "E2FSBLK", length %lu",
  1257. ret_block, num);
  1258. /*
  1259. * ext2_try_to_allocate marked the blocks we allocated as in
  1260. * use. So we may want to selectively mark some of the blocks
  1261. * as free
  1262. */
  1263. num = *count;
  1264. goto retry_alloc;
  1265. }
  1266. performed_allocation = 1;
  1267. if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
  1268. ext2_error(sb, "ext2_new_blocks",
  1269. "block("E2FSBLK") >= blocks count(%d) - "
  1270. "block_group = %d, es == %p ", ret_block,
  1271. le32_to_cpu(es->s_blocks_count), group_no, es);
  1272. goto out;
  1273. }
  1274. group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);
  1275. percpu_counter_sub(&sbi->s_freeblocks_counter, num);
  1276. mark_buffer_dirty(bitmap_bh);
  1277. if (sb->s_flags & SB_SYNCHRONOUS)
  1278. sync_dirty_buffer(bitmap_bh);
  1279. *errp = 0;
  1280. brelse(bitmap_bh);
  1281. if (num < *count) {
  1282. dquot_free_block_nodirty(inode, *count-num);
  1283. mark_inode_dirty(inode);
  1284. *count = num;
  1285. }
  1286. return ret_block;
  1287. io_error:
  1288. *errp = -EIO;
  1289. out:
  1290. /*
  1291. * Undo the block allocation
  1292. */
  1293. if (!performed_allocation) {
  1294. dquot_free_block_nodirty(inode, *count);
  1295. mark_inode_dirty(inode);
  1296. }
  1297. brelse(bitmap_bh);
  1298. return 0;
  1299. }
  1300. ext2_fsblk_t ext2_new_block(struct inode *inode, unsigned long goal, int *errp)
  1301. {
  1302. unsigned long count = 1;
  1303. return ext2_new_blocks(inode, goal, &count, errp);
  1304. }
  1305. #ifdef EXT2FS_DEBUG
  1306. unsigned long ext2_count_free(struct buffer_head *map, unsigned int numchars)
  1307. {
  1308. return numchars * BITS_PER_BYTE - memweight(map->b_data, numchars);
  1309. }
  1310. #endif /* EXT2FS_DEBUG */
  1311. unsigned long ext2_count_free_blocks (struct super_block * sb)
  1312. {
  1313. struct ext2_group_desc * desc;
  1314. unsigned long desc_count = 0;
  1315. int i;
  1316. #ifdef EXT2FS_DEBUG
  1317. unsigned long bitmap_count, x;
  1318. struct ext2_super_block *es;
  1319. es = EXT2_SB(sb)->s_es;
  1320. desc_count = 0;
  1321. bitmap_count = 0;
  1322. desc = NULL;
  1323. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  1324. struct buffer_head *bitmap_bh;
  1325. desc = ext2_get_group_desc (sb, i, NULL);
  1326. if (!desc)
  1327. continue;
  1328. desc_count += le16_to_cpu(desc->bg_free_blocks_count);
  1329. bitmap_bh = read_block_bitmap(sb, i);
  1330. if (!bitmap_bh)
  1331. continue;
  1332. x = ext2_count_free(bitmap_bh, sb->s_blocksize);
  1333. printk ("group %d: stored = %d, counted = %lu\n",
  1334. i, le16_to_cpu(desc->bg_free_blocks_count), x);
  1335. bitmap_count += x;
  1336. brelse(bitmap_bh);
  1337. }
  1338. printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
  1339. (long)le32_to_cpu(es->s_free_blocks_count),
  1340. desc_count, bitmap_count);
  1341. return bitmap_count;
  1342. #else
  1343. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  1344. desc = ext2_get_group_desc (sb, i, NULL);
  1345. if (!desc)
  1346. continue;
  1347. desc_count += le16_to_cpu(desc->bg_free_blocks_count);
  1348. }
  1349. return desc_count;
  1350. #endif
  1351. }
  1352. static inline int test_root(int a, int b)
  1353. {
  1354. int num = b;
  1355. while (a > num)
  1356. num *= b;
  1357. return num == a;
  1358. }
  1359. static int ext2_group_sparse(int group)
  1360. {
  1361. if (group <= 1)
  1362. return 1;
  1363. return (test_root(group, 3) || test_root(group, 5) ||
  1364. test_root(group, 7));
  1365. }
  1366. /**
  1367. * ext2_bg_has_super - number of blocks used by the superblock in group
  1368. * @sb: superblock for filesystem
  1369. * @group: group number to check
  1370. *
  1371. * Return the number of blocks used by the superblock (primary or backup)
  1372. * in this group. Currently this will be only 0 or 1.
  1373. */
  1374. int ext2_bg_has_super(struct super_block *sb, int group)
  1375. {
  1376. if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
  1377. !ext2_group_sparse(group))
  1378. return 0;
  1379. return 1;
  1380. }
  1381. /**
  1382. * ext2_bg_num_gdb - number of blocks used by the group table in group
  1383. * @sb: superblock for filesystem
  1384. * @group: group number to check
  1385. *
  1386. * Return the number of blocks used by the group descriptor table
  1387. * (primary or backup) in this group. In the future there may be a
  1388. * different number of descriptor blocks in each group.
  1389. */
  1390. unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
  1391. {
  1392. return ext2_bg_has_super(sb, group) ? EXT2_SB(sb)->s_gdb_count : 0;
  1393. }