balloc.c 43 KB

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