balloc.c 44 KB

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