balloc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. /*
  2. * linux/fs/ext3/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 <linux/time.h>
  14. #include <linux/capability.h>
  15. #include <linux/fs.h>
  16. #include <linux/slab.h>
  17. #include <linux/jbd.h>
  18. #include <linux/ext3_fs.h>
  19. #include <linux/ext3_jbd.h>
  20. #include <linux/quotaops.h>
  21. #include <linux/buffer_head.h>
  22. /*
  23. * balloc.c contains the blocks allocation and deallocation routines
  24. */
  25. /*
  26. * The free blocks are managed by bitmaps. A file system contains several
  27. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  28. * block for inodes, N blocks for the inode table and data blocks.
  29. *
  30. * The file system contains group descriptors which are located after the
  31. * super block. Each descriptor contains the number of the bitmap block and
  32. * the free blocks count in the block. The descriptors are loaded in memory
  33. * when a file system is mounted (see ext3_fill_super).
  34. */
  35. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  36. /**
  37. * ext3_get_group_desc() -- load group descriptor from disk
  38. * @sb: super block
  39. * @block_group: given block group
  40. * @bh: pointer to the buffer head to store the block
  41. * group descriptor
  42. */
  43. struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
  44. unsigned int block_group,
  45. struct buffer_head ** bh)
  46. {
  47. unsigned long group_desc;
  48. unsigned long offset;
  49. struct ext3_group_desc * desc;
  50. struct ext3_sb_info *sbi = EXT3_SB(sb);
  51. if (block_group >= sbi->s_groups_count) {
  52. ext3_error (sb, "ext3_get_group_desc",
  53. "block_group >= groups_count - "
  54. "block_group = %d, groups_count = %lu",
  55. block_group, sbi->s_groups_count);
  56. return NULL;
  57. }
  58. smp_rmb();
  59. group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb);
  60. offset = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1);
  61. if (!sbi->s_group_desc[group_desc]) {
  62. ext3_error (sb, "ext3_get_group_desc",
  63. "Group descriptor not loaded - "
  64. "block_group = %d, group_desc = %lu, desc = %lu",
  65. block_group, group_desc, offset);
  66. return NULL;
  67. }
  68. desc = (struct ext3_group_desc *) sbi->s_group_desc[group_desc]->b_data;
  69. if (bh)
  70. *bh = sbi->s_group_desc[group_desc];
  71. return desc + offset;
  72. }
  73. static int ext3_valid_block_bitmap(struct super_block *sb,
  74. struct ext3_group_desc *desc,
  75. unsigned int block_group,
  76. struct buffer_head *bh)
  77. {
  78. ext3_grpblk_t offset;
  79. ext3_grpblk_t next_zero_bit;
  80. ext3_fsblk_t bitmap_blk;
  81. ext3_fsblk_t group_first_block;
  82. group_first_block = ext3_group_first_block_no(sb, block_group);
  83. /* check whether block bitmap block number is set */
  84. bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
  85. offset = bitmap_blk - group_first_block;
  86. if (!ext3_test_bit(offset, bh->b_data))
  87. /* bad block bitmap */
  88. goto err_out;
  89. /* check whether the inode bitmap block number is set */
  90. bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
  91. offset = bitmap_blk - group_first_block;
  92. if (!ext3_test_bit(offset, bh->b_data))
  93. /* bad block bitmap */
  94. goto err_out;
  95. /* check whether the inode table block number is set */
  96. bitmap_blk = le32_to_cpu(desc->bg_inode_table);
  97. offset = bitmap_blk - group_first_block;
  98. next_zero_bit = ext3_find_next_zero_bit(bh->b_data,
  99. offset + EXT3_SB(sb)->s_itb_per_group,
  100. offset);
  101. if (next_zero_bit >= offset + EXT3_SB(sb)->s_itb_per_group)
  102. /* good bitmap for inode tables */
  103. return 1;
  104. err_out:
  105. ext3_error(sb, __func__,
  106. "Invalid block bitmap - "
  107. "block_group = %d, block = %lu",
  108. block_group, bitmap_blk);
  109. return 0;
  110. }
  111. /**
  112. * read_block_bitmap()
  113. * @sb: super block
  114. * @block_group: given block group
  115. *
  116. * Read the bitmap for a given block_group,and validate the
  117. * bits for block/inode/inode tables are set in the bitmaps
  118. *
  119. * Return buffer_head on success or NULL in case of failure.
  120. */
  121. static struct buffer_head *
  122. read_block_bitmap(struct super_block *sb, unsigned int block_group)
  123. {
  124. struct ext3_group_desc * desc;
  125. struct buffer_head * bh = NULL;
  126. ext3_fsblk_t bitmap_blk;
  127. desc = ext3_get_group_desc(sb, block_group, NULL);
  128. if (!desc)
  129. return NULL;
  130. bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
  131. bh = sb_getblk(sb, bitmap_blk);
  132. if (unlikely(!bh)) {
  133. ext3_error(sb, __func__,
  134. "Cannot read block bitmap - "
  135. "block_group = %d, block_bitmap = %u",
  136. block_group, le32_to_cpu(desc->bg_block_bitmap));
  137. return NULL;
  138. }
  139. if (likely(bh_uptodate_or_lock(bh)))
  140. return bh;
  141. if (bh_submit_read(bh) < 0) {
  142. brelse(bh);
  143. ext3_error(sb, __func__,
  144. "Cannot read block bitmap - "
  145. "block_group = %d, block_bitmap = %u",
  146. block_group, le32_to_cpu(desc->bg_block_bitmap));
  147. return NULL;
  148. }
  149. ext3_valid_block_bitmap(sb, desc, block_group, bh);
  150. /*
  151. * file system mounted not to panic on error, continue with corrupt
  152. * bitmap
  153. */
  154. return bh;
  155. }
  156. /*
  157. * The reservation window structure operations
  158. * --------------------------------------------
  159. * Operations include:
  160. * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
  161. *
  162. * We use a red-black tree to represent per-filesystem reservation
  163. * windows.
  164. *
  165. */
  166. /**
  167. * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
  168. * @rb_root: root of per-filesystem reservation rb tree
  169. * @verbose: verbose mode
  170. * @fn: function which wishes to dump the reservation map
  171. *
  172. * If verbose is turned on, it will print the whole block reservation
  173. * windows(start, end). Otherwise, it will only print out the "bad" windows,
  174. * those windows that overlap with their immediate neighbors.
  175. */
  176. #if 1
  177. static void __rsv_window_dump(struct rb_root *root, int verbose,
  178. const char *fn)
  179. {
  180. struct rb_node *n;
  181. struct ext3_reserve_window_node *rsv, *prev;
  182. int bad;
  183. restart:
  184. n = rb_first(root);
  185. bad = 0;
  186. prev = NULL;
  187. printk("Block Allocation Reservation Windows Map (%s):\n", fn);
  188. while (n) {
  189. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  190. if (verbose)
  191. printk("reservation window 0x%p "
  192. "start: %lu, end: %lu\n",
  193. rsv, rsv->rsv_start, rsv->rsv_end);
  194. if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
  195. printk("Bad reservation %p (start >= end)\n",
  196. rsv);
  197. bad = 1;
  198. }
  199. if (prev && prev->rsv_end >= rsv->rsv_start) {
  200. printk("Bad reservation %p (prev->end >= start)\n",
  201. rsv);
  202. bad = 1;
  203. }
  204. if (bad) {
  205. if (!verbose) {
  206. printk("Restarting reservation walk in verbose mode\n");
  207. verbose = 1;
  208. goto restart;
  209. }
  210. }
  211. n = rb_next(n);
  212. prev = rsv;
  213. }
  214. printk("Window map complete.\n");
  215. BUG_ON(bad);
  216. }
  217. #define rsv_window_dump(root, verbose) \
  218. __rsv_window_dump((root), (verbose), __func__)
  219. #else
  220. #define rsv_window_dump(root, verbose) do {} while (0)
  221. #endif
  222. /**
  223. * goal_in_my_reservation()
  224. * @rsv: inode's reservation window
  225. * @grp_goal: given goal block relative to the allocation block group
  226. * @group: the current allocation block group
  227. * @sb: filesystem super block
  228. *
  229. * Test if the given goal block (group relative) is within the file's
  230. * own block reservation window range.
  231. *
  232. * If the reservation window is outside the goal allocation group, return 0;
  233. * grp_goal (given goal block) could be -1, which means no specific
  234. * goal block. In this case, always return 1.
  235. * If the goal block is within the reservation window, return 1;
  236. * otherwise, return 0;
  237. */
  238. static int
  239. goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal,
  240. unsigned int group, struct super_block * sb)
  241. {
  242. ext3_fsblk_t group_first_block, group_last_block;
  243. group_first_block = ext3_group_first_block_no(sb, group);
  244. group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  245. if ((rsv->_rsv_start > group_last_block) ||
  246. (rsv->_rsv_end < group_first_block))
  247. return 0;
  248. if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
  249. || (grp_goal + group_first_block > rsv->_rsv_end)))
  250. return 0;
  251. return 1;
  252. }
  253. /**
  254. * search_reserve_window()
  255. * @rb_root: root of reservation tree
  256. * @goal: target allocation block
  257. *
  258. * Find the reserved window which includes the goal, or the previous one
  259. * if the goal is not in any window.
  260. * Returns NULL if there are no windows or if all windows start after the goal.
  261. */
  262. static struct ext3_reserve_window_node *
  263. search_reserve_window(struct rb_root *root, ext3_fsblk_t goal)
  264. {
  265. struct rb_node *n = root->rb_node;
  266. struct ext3_reserve_window_node *rsv;
  267. if (!n)
  268. return NULL;
  269. do {
  270. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  271. if (goal < rsv->rsv_start)
  272. n = n->rb_left;
  273. else if (goal > rsv->rsv_end)
  274. n = n->rb_right;
  275. else
  276. return rsv;
  277. } while (n);
  278. /*
  279. * We've fallen off the end of the tree: the goal wasn't inside
  280. * any particular node. OK, the previous node must be to one
  281. * side of the interval containing the goal. If it's the RHS,
  282. * we need to back up one.
  283. */
  284. if (rsv->rsv_start > goal) {
  285. n = rb_prev(&rsv->rsv_node);
  286. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  287. }
  288. return rsv;
  289. }
  290. /**
  291. * ext3_rsv_window_add() -- Insert a window to the block reservation rb tree.
  292. * @sb: super block
  293. * @rsv: reservation window to add
  294. *
  295. * Must be called with rsv_lock hold.
  296. */
  297. void ext3_rsv_window_add(struct super_block *sb,
  298. struct ext3_reserve_window_node *rsv)
  299. {
  300. struct rb_root *root = &EXT3_SB(sb)->s_rsv_window_root;
  301. struct rb_node *node = &rsv->rsv_node;
  302. ext3_fsblk_t start = rsv->rsv_start;
  303. struct rb_node ** p = &root->rb_node;
  304. struct rb_node * parent = NULL;
  305. struct ext3_reserve_window_node *this;
  306. while (*p)
  307. {
  308. parent = *p;
  309. this = rb_entry(parent, struct ext3_reserve_window_node, rsv_node);
  310. if (start < this->rsv_start)
  311. p = &(*p)->rb_left;
  312. else if (start > this->rsv_end)
  313. p = &(*p)->rb_right;
  314. else {
  315. rsv_window_dump(root, 1);
  316. BUG();
  317. }
  318. }
  319. rb_link_node(node, parent, p);
  320. rb_insert_color(node, root);
  321. }
  322. /**
  323. * ext3_rsv_window_remove() -- unlink a window from the reservation rb tree
  324. * @sb: super block
  325. * @rsv: reservation window to remove
  326. *
  327. * Mark the block reservation window as not allocated, and unlink it
  328. * from the filesystem reservation window rb tree. Must be called with
  329. * rsv_lock hold.
  330. */
  331. static void rsv_window_remove(struct super_block *sb,
  332. struct ext3_reserve_window_node *rsv)
  333. {
  334. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  335. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  336. rsv->rsv_alloc_hit = 0;
  337. rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root);
  338. }
  339. /*
  340. * rsv_is_empty() -- Check if the reservation window is allocated.
  341. * @rsv: given reservation window to check
  342. *
  343. * returns 1 if the end block is EXT3_RESERVE_WINDOW_NOT_ALLOCATED.
  344. */
  345. static inline int rsv_is_empty(struct ext3_reserve_window *rsv)
  346. {
  347. /* a valid reservation end block could not be 0 */
  348. return rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  349. }
  350. /**
  351. * ext3_init_block_alloc_info()
  352. * @inode: file inode structure
  353. *
  354. * Allocate and initialize the reservation window structure, and
  355. * link the window to the ext3 inode structure at last
  356. *
  357. * The reservation window structure is only dynamically allocated
  358. * and linked to ext3 inode the first time the open file
  359. * needs a new block. So, before every ext3_new_block(s) call, for
  360. * regular files, we should check whether the reservation window
  361. * structure exists or not. In the latter case, this function is called.
  362. * Fail to do so will result in block reservation being turned off for that
  363. * open file.
  364. *
  365. * This function is called from ext3_get_blocks_handle(), also called
  366. * when setting the reservation window size through ioctl before the file
  367. * is open for write (needs block allocation).
  368. *
  369. * Needs truncate_mutex protection prior to call this function.
  370. */
  371. void ext3_init_block_alloc_info(struct inode *inode)
  372. {
  373. struct ext3_inode_info *ei = EXT3_I(inode);
  374. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  375. struct super_block *sb = inode->i_sb;
  376. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  377. if (block_i) {
  378. struct ext3_reserve_window_node *rsv = &block_i->rsv_window_node;
  379. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  380. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  381. /*
  382. * if filesystem is mounted with NORESERVATION, the goal
  383. * reservation window size is set to zero to indicate
  384. * block reservation is off
  385. */
  386. if (!test_opt(sb, RESERVATION))
  387. rsv->rsv_goal_size = 0;
  388. else
  389. rsv->rsv_goal_size = EXT3_DEFAULT_RESERVE_BLOCKS;
  390. rsv->rsv_alloc_hit = 0;
  391. block_i->last_alloc_logical_block = 0;
  392. block_i->last_alloc_physical_block = 0;
  393. }
  394. ei->i_block_alloc_info = block_i;
  395. }
  396. /**
  397. * ext3_discard_reservation()
  398. * @inode: inode
  399. *
  400. * Discard(free) block reservation window on last file close, or truncate
  401. * or at last iput().
  402. *
  403. * It is being called in three cases:
  404. * ext3_release_file(): last writer close the file
  405. * ext3_clear_inode(): last iput(), when nobody link to this file.
  406. * ext3_truncate(): when the block indirect map is about to change.
  407. *
  408. */
  409. void ext3_discard_reservation(struct inode *inode)
  410. {
  411. struct ext3_inode_info *ei = EXT3_I(inode);
  412. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  413. struct ext3_reserve_window_node *rsv;
  414. spinlock_t *rsv_lock = &EXT3_SB(inode->i_sb)->s_rsv_window_lock;
  415. if (!block_i)
  416. return;
  417. rsv = &block_i->rsv_window_node;
  418. if (!rsv_is_empty(&rsv->rsv_window)) {
  419. spin_lock(rsv_lock);
  420. if (!rsv_is_empty(&rsv->rsv_window))
  421. rsv_window_remove(inode->i_sb, rsv);
  422. spin_unlock(rsv_lock);
  423. }
  424. }
  425. /**
  426. * ext3_free_blocks_sb() -- Free given blocks and update quota
  427. * @handle: handle to this transaction
  428. * @sb: super block
  429. * @block: start physcial block to free
  430. * @count: number of blocks to free
  431. * @pdquot_freed_blocks: pointer to quota
  432. */
  433. void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb,
  434. ext3_fsblk_t block, unsigned long count,
  435. unsigned long *pdquot_freed_blocks)
  436. {
  437. struct buffer_head *bitmap_bh = NULL;
  438. struct buffer_head *gd_bh;
  439. unsigned long block_group;
  440. ext3_grpblk_t bit;
  441. unsigned long i;
  442. unsigned long overflow;
  443. struct ext3_group_desc * desc;
  444. struct ext3_super_block * es;
  445. struct ext3_sb_info *sbi;
  446. int err = 0, ret;
  447. ext3_grpblk_t group_freed;
  448. *pdquot_freed_blocks = 0;
  449. sbi = EXT3_SB(sb);
  450. es = sbi->s_es;
  451. if (block < le32_to_cpu(es->s_first_data_block) ||
  452. block + count < block ||
  453. block + count > le32_to_cpu(es->s_blocks_count)) {
  454. ext3_error (sb, "ext3_free_blocks",
  455. "Freeing blocks not in datazone - "
  456. "block = "E3FSBLK", count = %lu", block, count);
  457. goto error_return;
  458. }
  459. ext3_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
  460. do_more:
  461. overflow = 0;
  462. block_group = (block - le32_to_cpu(es->s_first_data_block)) /
  463. EXT3_BLOCKS_PER_GROUP(sb);
  464. bit = (block - le32_to_cpu(es->s_first_data_block)) %
  465. EXT3_BLOCKS_PER_GROUP(sb);
  466. /*
  467. * Check to see if we are freeing blocks across a group
  468. * boundary.
  469. */
  470. if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
  471. overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
  472. count -= overflow;
  473. }
  474. brelse(bitmap_bh);
  475. bitmap_bh = read_block_bitmap(sb, block_group);
  476. if (!bitmap_bh)
  477. goto error_return;
  478. desc = ext3_get_group_desc (sb, block_group, &gd_bh);
  479. if (!desc)
  480. goto error_return;
  481. if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
  482. in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
  483. in_range (block, le32_to_cpu(desc->bg_inode_table),
  484. sbi->s_itb_per_group) ||
  485. in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
  486. sbi->s_itb_per_group)) {
  487. ext3_error (sb, "ext3_free_blocks",
  488. "Freeing blocks in system zones - "
  489. "Block = "E3FSBLK", count = %lu",
  490. block, count);
  491. goto error_return;
  492. }
  493. /*
  494. * We are about to start releasing blocks in the bitmap,
  495. * so we need undo access.
  496. */
  497. /* @@@ check errors */
  498. BUFFER_TRACE(bitmap_bh, "getting undo access");
  499. err = ext3_journal_get_undo_access(handle, bitmap_bh);
  500. if (err)
  501. goto error_return;
  502. /*
  503. * We are about to modify some metadata. Call the journal APIs
  504. * to unshare ->b_data if a currently-committing transaction is
  505. * using it
  506. */
  507. BUFFER_TRACE(gd_bh, "get_write_access");
  508. err = ext3_journal_get_write_access(handle, gd_bh);
  509. if (err)
  510. goto error_return;
  511. jbd_lock_bh_state(bitmap_bh);
  512. for (i = 0, group_freed = 0; i < count; i++) {
  513. /*
  514. * An HJ special. This is expensive...
  515. */
  516. #ifdef CONFIG_JBD_DEBUG
  517. jbd_unlock_bh_state(bitmap_bh);
  518. {
  519. struct buffer_head *debug_bh;
  520. debug_bh = sb_find_get_block(sb, block + i);
  521. if (debug_bh) {
  522. BUFFER_TRACE(debug_bh, "Deleted!");
  523. if (!bh2jh(bitmap_bh)->b_committed_data)
  524. BUFFER_TRACE(debug_bh,
  525. "No commited data in bitmap");
  526. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
  527. __brelse(debug_bh);
  528. }
  529. }
  530. jbd_lock_bh_state(bitmap_bh);
  531. #endif
  532. if (need_resched()) {
  533. jbd_unlock_bh_state(bitmap_bh);
  534. cond_resched();
  535. jbd_lock_bh_state(bitmap_bh);
  536. }
  537. /* @@@ This prevents newly-allocated data from being
  538. * freed and then reallocated within the same
  539. * transaction.
  540. *
  541. * Ideally we would want to allow that to happen, but to
  542. * do so requires making journal_forget() capable of
  543. * revoking the queued write of a data block, which
  544. * implies blocking on the journal lock. *forget()
  545. * cannot block due to truncate races.
  546. *
  547. * Eventually we can fix this by making journal_forget()
  548. * return a status indicating whether or not it was able
  549. * to revoke the buffer. On successful revoke, it is
  550. * safe not to set the allocation bit in the committed
  551. * bitmap, because we know that there is no outstanding
  552. * activity on the buffer any more and so it is safe to
  553. * reallocate it.
  554. */
  555. BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
  556. J_ASSERT_BH(bitmap_bh,
  557. bh2jh(bitmap_bh)->b_committed_data != NULL);
  558. ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
  559. bh2jh(bitmap_bh)->b_committed_data);
  560. /*
  561. * We clear the bit in the bitmap after setting the committed
  562. * data bit, because this is the reverse order to that which
  563. * the allocator uses.
  564. */
  565. BUFFER_TRACE(bitmap_bh, "clear bit");
  566. if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  567. bit + i, bitmap_bh->b_data)) {
  568. jbd_unlock_bh_state(bitmap_bh);
  569. ext3_error(sb, __func__,
  570. "bit already cleared for block "E3FSBLK,
  571. block + i);
  572. jbd_lock_bh_state(bitmap_bh);
  573. BUFFER_TRACE(bitmap_bh, "bit already cleared");
  574. } else {
  575. group_freed++;
  576. }
  577. }
  578. jbd_unlock_bh_state(bitmap_bh);
  579. spin_lock(sb_bgl_lock(sbi, block_group));
  580. le16_add_cpu(&desc->bg_free_blocks_count, group_freed);
  581. spin_unlock(sb_bgl_lock(sbi, block_group));
  582. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  583. /* We dirtied the bitmap block */
  584. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  585. err = ext3_journal_dirty_metadata(handle, bitmap_bh);
  586. /* And the group descriptor block */
  587. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  588. ret = ext3_journal_dirty_metadata(handle, gd_bh);
  589. if (!err) err = ret;
  590. *pdquot_freed_blocks += group_freed;
  591. if (overflow && !err) {
  592. block += count;
  593. count = overflow;
  594. goto do_more;
  595. }
  596. error_return:
  597. brelse(bitmap_bh);
  598. ext3_std_error(sb, err);
  599. return;
  600. }
  601. /**
  602. * ext3_free_blocks() -- Free given blocks and update quota
  603. * @handle: handle for this transaction
  604. * @inode: inode
  605. * @block: start physical block to free
  606. * @count: number of blocks to count
  607. */
  608. void ext3_free_blocks(handle_t *handle, struct inode *inode,
  609. ext3_fsblk_t block, unsigned long count)
  610. {
  611. struct super_block * sb;
  612. unsigned long dquot_freed_blocks;
  613. sb = inode->i_sb;
  614. if (!sb) {
  615. printk ("ext3_free_blocks: nonexistent device");
  616. return;
  617. }
  618. ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
  619. if (dquot_freed_blocks)
  620. dquot_free_block(inode, dquot_freed_blocks);
  621. return;
  622. }
  623. /**
  624. * ext3_test_allocatable()
  625. * @nr: given allocation block group
  626. * @bh: bufferhead contains the bitmap of the given block group
  627. *
  628. * For ext3 allocations, we must not reuse any blocks which are
  629. * allocated in the bitmap buffer's "last committed data" copy. This
  630. * prevents deletes from freeing up the page for reuse until we have
  631. * committed the delete transaction.
  632. *
  633. * If we didn't do this, then deleting something and reallocating it as
  634. * data would allow the old block to be overwritten before the
  635. * transaction committed (because we force data to disk before commit).
  636. * This would lead to corruption if we crashed between overwriting the
  637. * data and committing the delete.
  638. *
  639. * @@@ We may want to make this allocation behaviour conditional on
  640. * data-writes at some point, and disable it for metadata allocations or
  641. * sync-data inodes.
  642. */
  643. static int ext3_test_allocatable(ext3_grpblk_t nr, struct buffer_head *bh)
  644. {
  645. int ret;
  646. struct journal_head *jh = bh2jh(bh);
  647. if (ext3_test_bit(nr, bh->b_data))
  648. return 0;
  649. jbd_lock_bh_state(bh);
  650. if (!jh->b_committed_data)
  651. ret = 1;
  652. else
  653. ret = !ext3_test_bit(nr, jh->b_committed_data);
  654. jbd_unlock_bh_state(bh);
  655. return ret;
  656. }
  657. /**
  658. * bitmap_search_next_usable_block()
  659. * @start: the starting block (group relative) of the search
  660. * @bh: bufferhead contains the block group bitmap
  661. * @maxblocks: the ending block (group relative) of the reservation
  662. *
  663. * The bitmap search --- search forward alternately through the actual
  664. * bitmap on disk and the last-committed copy in journal, until we find a
  665. * bit free in both bitmaps.
  666. */
  667. static ext3_grpblk_t
  668. bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
  669. ext3_grpblk_t maxblocks)
  670. {
  671. ext3_grpblk_t next;
  672. struct journal_head *jh = bh2jh(bh);
  673. while (start < maxblocks) {
  674. next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start);
  675. if (next >= maxblocks)
  676. return -1;
  677. if (ext3_test_allocatable(next, bh))
  678. return next;
  679. jbd_lock_bh_state(bh);
  680. if (jh->b_committed_data)
  681. start = ext3_find_next_zero_bit(jh->b_committed_data,
  682. maxblocks, next);
  683. jbd_unlock_bh_state(bh);
  684. }
  685. return -1;
  686. }
  687. /**
  688. * find_next_usable_block()
  689. * @start: the starting block (group relative) to find next
  690. * allocatable block in bitmap.
  691. * @bh: bufferhead contains the block group bitmap
  692. * @maxblocks: the ending block (group relative) for the search
  693. *
  694. * Find an allocatable block in a bitmap. We honor both the bitmap and
  695. * its last-committed copy (if that exists), and perform the "most
  696. * appropriate allocation" algorithm of looking for a free block near
  697. * the initial goal; then for a free byte somewhere in the bitmap; then
  698. * for any free bit in the bitmap.
  699. */
  700. static ext3_grpblk_t
  701. find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
  702. ext3_grpblk_t maxblocks)
  703. {
  704. ext3_grpblk_t here, next;
  705. char *p, *r;
  706. if (start > 0) {
  707. /*
  708. * The goal was occupied; search forward for a free
  709. * block within the next XX blocks.
  710. *
  711. * end_goal is more or less random, but it has to be
  712. * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
  713. * next 64-bit boundary is simple..
  714. */
  715. ext3_grpblk_t end_goal = (start + 63) & ~63;
  716. if (end_goal > maxblocks)
  717. end_goal = maxblocks;
  718. here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
  719. if (here < end_goal && ext3_test_allocatable(here, bh))
  720. return here;
  721. ext3_debug("Bit not found near goal\n");
  722. }
  723. here = start;
  724. if (here < 0)
  725. here = 0;
  726. p = ((char *)bh->b_data) + (here >> 3);
  727. r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
  728. next = (r - ((char *)bh->b_data)) << 3;
  729. if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh))
  730. return next;
  731. /*
  732. * The bitmap search --- search forward alternately through the actual
  733. * bitmap and the last-committed copy until we find a bit free in
  734. * both
  735. */
  736. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  737. return here;
  738. }
  739. /**
  740. * claim_block()
  741. * @block: the free block (group relative) to allocate
  742. * @bh: the bufferhead containts the block group bitmap
  743. *
  744. * We think we can allocate this block in this bitmap. Try to set the bit.
  745. * If that succeeds then check that nobody has allocated and then freed the
  746. * block since we saw that is was not marked in b_committed_data. If it _was_
  747. * allocated and freed then clear the bit in the bitmap again and return
  748. * zero (failure).
  749. */
  750. static inline int
  751. claim_block(spinlock_t *lock, ext3_grpblk_t block, struct buffer_head *bh)
  752. {
  753. struct journal_head *jh = bh2jh(bh);
  754. int ret;
  755. if (ext3_set_bit_atomic(lock, block, bh->b_data))
  756. return 0;
  757. jbd_lock_bh_state(bh);
  758. if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) {
  759. ext3_clear_bit_atomic(lock, block, bh->b_data);
  760. ret = 0;
  761. } else {
  762. ret = 1;
  763. }
  764. jbd_unlock_bh_state(bh);
  765. return ret;
  766. }
  767. /**
  768. * ext3_try_to_allocate()
  769. * @sb: superblock
  770. * @handle: handle to this transaction
  771. * @group: given allocation block group
  772. * @bitmap_bh: bufferhead holds the block bitmap
  773. * @grp_goal: given target block within the group
  774. * @count: target number of blocks to allocate
  775. * @my_rsv: reservation window
  776. *
  777. * Attempt to allocate blocks within a give range. Set the range of allocation
  778. * first, then find the first free bit(s) from the bitmap (within the range),
  779. * and at last, allocate the blocks by claiming the found free bit as allocated.
  780. *
  781. * To set the range of this allocation:
  782. * if there is a reservation window, only try to allocate block(s) from the
  783. * file's own reservation window;
  784. * Otherwise, the allocation range starts from the give goal block, ends at
  785. * the block group's last block.
  786. *
  787. * If we failed to allocate the desired block then we may end up crossing to a
  788. * new bitmap. In that case we must release write access to the old one via
  789. * ext3_journal_release_buffer(), else we'll run out of credits.
  790. */
  791. static ext3_grpblk_t
  792. ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
  793. struct buffer_head *bitmap_bh, ext3_grpblk_t grp_goal,
  794. unsigned long *count, struct ext3_reserve_window *my_rsv)
  795. {
  796. ext3_fsblk_t group_first_block;
  797. ext3_grpblk_t start, end;
  798. unsigned long num = 0;
  799. /* we do allocation within the reservation window if we have a window */
  800. if (my_rsv) {
  801. group_first_block = ext3_group_first_block_no(sb, group);
  802. if (my_rsv->_rsv_start >= group_first_block)
  803. start = my_rsv->_rsv_start - group_first_block;
  804. else
  805. /* reservation window cross group boundary */
  806. start = 0;
  807. end = my_rsv->_rsv_end - group_first_block + 1;
  808. if (end > EXT3_BLOCKS_PER_GROUP(sb))
  809. /* reservation window crosses group boundary */
  810. end = EXT3_BLOCKS_PER_GROUP(sb);
  811. if ((start <= grp_goal) && (grp_goal < end))
  812. start = grp_goal;
  813. else
  814. grp_goal = -1;
  815. } else {
  816. if (grp_goal > 0)
  817. start = grp_goal;
  818. else
  819. start = 0;
  820. end = EXT3_BLOCKS_PER_GROUP(sb);
  821. }
  822. BUG_ON(start > EXT3_BLOCKS_PER_GROUP(sb));
  823. repeat:
  824. if (grp_goal < 0 || !ext3_test_allocatable(grp_goal, bitmap_bh)) {
  825. grp_goal = find_next_usable_block(start, bitmap_bh, end);
  826. if (grp_goal < 0)
  827. goto fail_access;
  828. if (!my_rsv) {
  829. int i;
  830. for (i = 0; i < 7 && grp_goal > start &&
  831. ext3_test_allocatable(grp_goal - 1,
  832. bitmap_bh);
  833. i++, grp_goal--)
  834. ;
  835. }
  836. }
  837. start = grp_goal;
  838. if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group),
  839. grp_goal, bitmap_bh)) {
  840. /*
  841. * The block was allocated by another thread, or it was
  842. * allocated and then freed by another thread
  843. */
  844. start++;
  845. grp_goal++;
  846. if (start >= end)
  847. goto fail_access;
  848. goto repeat;
  849. }
  850. num++;
  851. grp_goal++;
  852. while (num < *count && grp_goal < end
  853. && ext3_test_allocatable(grp_goal, bitmap_bh)
  854. && claim_block(sb_bgl_lock(EXT3_SB(sb), group),
  855. grp_goal, bitmap_bh)) {
  856. num++;
  857. grp_goal++;
  858. }
  859. *count = num;
  860. return grp_goal - num;
  861. fail_access:
  862. *count = num;
  863. return -1;
  864. }
  865. /**
  866. * find_next_reservable_window():
  867. * find a reservable space within the given range.
  868. * It does not allocate the reservation window for now:
  869. * alloc_new_reservation() will do the work later.
  870. *
  871. * @search_head: the head of the searching list;
  872. * This is not necessarily the list head of the whole filesystem
  873. *
  874. * We have both head and start_block to assist the search
  875. * for the reservable space. The list starts from head,
  876. * but we will shift to the place where start_block is,
  877. * then start from there, when looking for a reservable space.
  878. *
  879. * @size: the target new reservation window size
  880. *
  881. * @group_first_block: the first block we consider to start
  882. * the real search from
  883. *
  884. * @last_block:
  885. * the maximum block number that our goal reservable space
  886. * could start from. This is normally the last block in this
  887. * group. The search will end when we found the start of next
  888. * possible reservable space is out of this boundary.
  889. * This could handle the cross boundary reservation window
  890. * request.
  891. *
  892. * basically we search from the given range, rather than the whole
  893. * reservation double linked list, (start_block, last_block)
  894. * to find a free region that is of my size and has not
  895. * been reserved.
  896. *
  897. */
  898. static int find_next_reservable_window(
  899. struct ext3_reserve_window_node *search_head,
  900. struct ext3_reserve_window_node *my_rsv,
  901. struct super_block * sb,
  902. ext3_fsblk_t start_block,
  903. ext3_fsblk_t last_block)
  904. {
  905. struct rb_node *next;
  906. struct ext3_reserve_window_node *rsv, *prev;
  907. ext3_fsblk_t cur;
  908. int size = my_rsv->rsv_goal_size;
  909. /* TODO: make the start of the reservation window byte-aligned */
  910. /* cur = *start_block & ~7;*/
  911. cur = start_block;
  912. rsv = search_head;
  913. if (!rsv)
  914. return -1;
  915. while (1) {
  916. if (cur <= rsv->rsv_end)
  917. cur = rsv->rsv_end + 1;
  918. /* TODO?
  919. * in the case we could not find a reservable space
  920. * that is what is expected, during the re-search, we could
  921. * remember what's the largest reservable space we could have
  922. * and return that one.
  923. *
  924. * For now it will fail if we could not find the reservable
  925. * space with expected-size (or more)...
  926. */
  927. if (cur > last_block)
  928. return -1; /* fail */
  929. prev = rsv;
  930. next = rb_next(&rsv->rsv_node);
  931. rsv = rb_entry(next,struct ext3_reserve_window_node,rsv_node);
  932. /*
  933. * Reached the last reservation, we can just append to the
  934. * previous one.
  935. */
  936. if (!next)
  937. break;
  938. if (cur + size <= rsv->rsv_start) {
  939. /*
  940. * Found a reserveable space big enough. We could
  941. * have a reservation across the group boundary here
  942. */
  943. break;
  944. }
  945. }
  946. /*
  947. * we come here either :
  948. * when we reach the end of the whole list,
  949. * and there is empty reservable space after last entry in the list.
  950. * append it to the end of the list.
  951. *
  952. * or we found one reservable space in the middle of the list,
  953. * return the reservation window that we could append to.
  954. * succeed.
  955. */
  956. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  957. rsv_window_remove(sb, my_rsv);
  958. /*
  959. * Let's book the whole avaliable window for now. We will check the
  960. * disk bitmap later and then, if there are free blocks then we adjust
  961. * the window size if it's larger than requested.
  962. * Otherwise, we will remove this node from the tree next time
  963. * call find_next_reservable_window.
  964. */
  965. my_rsv->rsv_start = cur;
  966. my_rsv->rsv_end = cur + size - 1;
  967. my_rsv->rsv_alloc_hit = 0;
  968. if (prev != my_rsv)
  969. ext3_rsv_window_add(sb, my_rsv);
  970. return 0;
  971. }
  972. /**
  973. * alloc_new_reservation()--allocate a new reservation window
  974. *
  975. * To make a new reservation, we search part of the filesystem
  976. * reservation list (the list that inside the group). We try to
  977. * allocate a new reservation window near the allocation goal,
  978. * or the beginning of the group, if there is no goal.
  979. *
  980. * We first find a reservable space after the goal, then from
  981. * there, we check the bitmap for the first free block after
  982. * it. If there is no free block until the end of group, then the
  983. * whole group is full, we failed. Otherwise, check if the free
  984. * block is inside the expected reservable space, if so, we
  985. * succeed.
  986. * If the first free block is outside the reservable space, then
  987. * start from the first free block, we search for next available
  988. * space, and go on.
  989. *
  990. * on succeed, a new reservation will be found and inserted into the list
  991. * It contains at least one free block, and it does not overlap with other
  992. * reservation windows.
  993. *
  994. * failed: we failed to find a reservation window in this group
  995. *
  996. * @rsv: the reservation
  997. *
  998. * @grp_goal: The goal (group-relative). It is where the search for a
  999. * free reservable space should start from.
  1000. * if we have a grp_goal(grp_goal >0 ), then start from there,
  1001. * no grp_goal(grp_goal = -1), we start from the first block
  1002. * of the group.
  1003. *
  1004. * @sb: the super block
  1005. * @group: the group we are trying to allocate in
  1006. * @bitmap_bh: the block group block bitmap
  1007. *
  1008. */
  1009. static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
  1010. ext3_grpblk_t grp_goal, struct super_block *sb,
  1011. unsigned int group, struct buffer_head *bitmap_bh)
  1012. {
  1013. struct ext3_reserve_window_node *search_head;
  1014. ext3_fsblk_t group_first_block, group_end_block, start_block;
  1015. ext3_grpblk_t first_free_block;
  1016. struct rb_root *fs_rsv_root = &EXT3_SB(sb)->s_rsv_window_root;
  1017. unsigned long size;
  1018. int ret;
  1019. spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
  1020. group_first_block = ext3_group_first_block_no(sb, group);
  1021. group_end_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  1022. if (grp_goal < 0)
  1023. start_block = group_first_block;
  1024. else
  1025. start_block = grp_goal + group_first_block;
  1026. size = my_rsv->rsv_goal_size;
  1027. if (!rsv_is_empty(&my_rsv->rsv_window)) {
  1028. /*
  1029. * if the old reservation is cross group boundary
  1030. * and if the goal is inside the old reservation window,
  1031. * we will come here when we just failed to allocate from
  1032. * the first part of the window. We still have another part
  1033. * that belongs to the next group. In this case, there is no
  1034. * point to discard our window and try to allocate a new one
  1035. * in this group(which will fail). we should
  1036. * keep the reservation window, just simply move on.
  1037. *
  1038. * Maybe we could shift the start block of the reservation
  1039. * window to the first block of next group.
  1040. */
  1041. if ((my_rsv->rsv_start <= group_end_block) &&
  1042. (my_rsv->rsv_end > group_end_block) &&
  1043. (start_block >= my_rsv->rsv_start))
  1044. return -1;
  1045. if ((my_rsv->rsv_alloc_hit >
  1046. (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
  1047. /*
  1048. * if the previously allocation hit ratio is
  1049. * greater than 1/2, then we double the size of
  1050. * the reservation window the next time,
  1051. * otherwise we keep the same size window
  1052. */
  1053. size = size * 2;
  1054. if (size > EXT3_MAX_RESERVE_BLOCKS)
  1055. size = EXT3_MAX_RESERVE_BLOCKS;
  1056. my_rsv->rsv_goal_size= size;
  1057. }
  1058. }
  1059. spin_lock(rsv_lock);
  1060. /*
  1061. * shift the search start to the window near the goal block
  1062. */
  1063. search_head = search_reserve_window(fs_rsv_root, start_block);
  1064. /*
  1065. * find_next_reservable_window() simply finds a reservable window
  1066. * inside the given range(start_block, group_end_block).
  1067. *
  1068. * To make sure the reservation window has a free bit inside it, we
  1069. * need to check the bitmap after we found a reservable window.
  1070. */
  1071. retry:
  1072. ret = find_next_reservable_window(search_head, my_rsv, sb,
  1073. start_block, group_end_block);
  1074. if (ret == -1) {
  1075. if (!rsv_is_empty(&my_rsv->rsv_window))
  1076. rsv_window_remove(sb, my_rsv);
  1077. spin_unlock(rsv_lock);
  1078. return -1;
  1079. }
  1080. /*
  1081. * On success, find_next_reservable_window() returns the
  1082. * reservation window where there is a reservable space after it.
  1083. * Before we reserve this reservable space, we need
  1084. * to make sure there is at least a free block inside this region.
  1085. *
  1086. * searching the first free bit on the block bitmap and copy of
  1087. * last committed bitmap alternatively, until we found a allocatable
  1088. * block. Search start from the start block of the reservable space
  1089. * we just found.
  1090. */
  1091. spin_unlock(rsv_lock);
  1092. first_free_block = bitmap_search_next_usable_block(
  1093. my_rsv->rsv_start - group_first_block,
  1094. bitmap_bh, group_end_block - group_first_block + 1);
  1095. if (first_free_block < 0) {
  1096. /*
  1097. * no free block left on the bitmap, no point
  1098. * to reserve the space. return failed.
  1099. */
  1100. spin_lock(rsv_lock);
  1101. if (!rsv_is_empty(&my_rsv->rsv_window))
  1102. rsv_window_remove(sb, my_rsv);
  1103. spin_unlock(rsv_lock);
  1104. return -1; /* failed */
  1105. }
  1106. start_block = first_free_block + group_first_block;
  1107. /*
  1108. * check if the first free block is within the
  1109. * free space we just reserved
  1110. */
  1111. if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
  1112. return 0; /* success */
  1113. /*
  1114. * if the first free bit we found is out of the reservable space
  1115. * continue search for next reservable space,
  1116. * start from where the free block is,
  1117. * we also shift the list head to where we stopped last time
  1118. */
  1119. search_head = my_rsv;
  1120. spin_lock(rsv_lock);
  1121. goto retry;
  1122. }
  1123. /**
  1124. * try_to_extend_reservation()
  1125. * @my_rsv: given reservation window
  1126. * @sb: super block
  1127. * @size: the delta to extend
  1128. *
  1129. * Attempt to expand the reservation window large enough to have
  1130. * required number of free blocks
  1131. *
  1132. * Since ext3_try_to_allocate() will always allocate blocks within
  1133. * the reservation window range, if the window size is too small,
  1134. * multiple blocks allocation has to stop at the end of the reservation
  1135. * window. To make this more efficient, given the total number of
  1136. * blocks needed and the current size of the window, we try to
  1137. * expand the reservation window size if necessary on a best-effort
  1138. * basis before ext3_new_blocks() tries to allocate blocks,
  1139. */
  1140. static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv,
  1141. struct super_block *sb, int size)
  1142. {
  1143. struct ext3_reserve_window_node *next_rsv;
  1144. struct rb_node *next;
  1145. spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
  1146. if (!spin_trylock(rsv_lock))
  1147. return;
  1148. next = rb_next(&my_rsv->rsv_node);
  1149. if (!next)
  1150. my_rsv->rsv_end += size;
  1151. else {
  1152. next_rsv = rb_entry(next, struct ext3_reserve_window_node, rsv_node);
  1153. if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
  1154. my_rsv->rsv_end += size;
  1155. else
  1156. my_rsv->rsv_end = next_rsv->rsv_start - 1;
  1157. }
  1158. spin_unlock(rsv_lock);
  1159. }
  1160. /**
  1161. * ext3_try_to_allocate_with_rsv()
  1162. * @sb: superblock
  1163. * @handle: handle to this transaction
  1164. * @group: given allocation block group
  1165. * @bitmap_bh: bufferhead holds the block bitmap
  1166. * @grp_goal: given target block within the group
  1167. * @count: target number of blocks to allocate
  1168. * @my_rsv: reservation window
  1169. * @errp: pointer to store the error code
  1170. *
  1171. * This is the main function used to allocate a new block and its reservation
  1172. * window.
  1173. *
  1174. * Each time when a new block allocation is need, first try to allocate from
  1175. * its own reservation. If it does not have a reservation window, instead of
  1176. * looking for a free bit on bitmap first, then look up the reservation list to
  1177. * see if it is inside somebody else's reservation window, we try to allocate a
  1178. * reservation window for it starting from the goal first. Then do the block
  1179. * allocation within the reservation window.
  1180. *
  1181. * This will avoid keeping on searching the reservation list again and
  1182. * again when somebody is looking for a free block (without
  1183. * reservation), and there are lots of free blocks, but they are all
  1184. * being reserved.
  1185. *
  1186. * We use a red-black tree for the per-filesystem reservation list.
  1187. *
  1188. */
  1189. static ext3_grpblk_t
  1190. ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
  1191. unsigned int group, struct buffer_head *bitmap_bh,
  1192. ext3_grpblk_t grp_goal,
  1193. struct ext3_reserve_window_node * my_rsv,
  1194. unsigned long *count, int *errp)
  1195. {
  1196. ext3_fsblk_t group_first_block, group_last_block;
  1197. ext3_grpblk_t ret = 0;
  1198. int fatal;
  1199. unsigned long num = *count;
  1200. *errp = 0;
  1201. /*
  1202. * Make sure we use undo access for the bitmap, because it is critical
  1203. * that we do the frozen_data COW on bitmap buffers in all cases even
  1204. * if the buffer is in BJ_Forget state in the committing transaction.
  1205. */
  1206. BUFFER_TRACE(bitmap_bh, "get undo access for new block");
  1207. fatal = ext3_journal_get_undo_access(handle, bitmap_bh);
  1208. if (fatal) {
  1209. *errp = fatal;
  1210. return -1;
  1211. }
  1212. /*
  1213. * we don't deal with reservation when
  1214. * filesystem is mounted without reservation
  1215. * or the file is not a regular file
  1216. * or last attempt to allocate a block with reservation turned on failed
  1217. */
  1218. if (my_rsv == NULL ) {
  1219. ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh,
  1220. grp_goal, count, NULL);
  1221. goto out;
  1222. }
  1223. /*
  1224. * grp_goal is a group relative block number (if there is a goal)
  1225. * 0 <= grp_goal < EXT3_BLOCKS_PER_GROUP(sb)
  1226. * first block is a filesystem wide block number
  1227. * first block is the block number of the first block in this group
  1228. */
  1229. group_first_block = ext3_group_first_block_no(sb, group);
  1230. group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  1231. /*
  1232. * Basically we will allocate a new block from inode's reservation
  1233. * window.
  1234. *
  1235. * We need to allocate a new reservation window, if:
  1236. * a) inode does not have a reservation window; or
  1237. * b) last attempt to allocate a block from existing reservation
  1238. * failed; or
  1239. * c) we come here with a goal and with a reservation window
  1240. *
  1241. * We do not need to allocate a new reservation window if we come here
  1242. * at the beginning with a goal and the goal is inside the window, or
  1243. * we don't have a goal but already have a reservation window.
  1244. * then we could go to allocate from the reservation window directly.
  1245. */
  1246. while (1) {
  1247. if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
  1248. !goal_in_my_reservation(&my_rsv->rsv_window,
  1249. grp_goal, group, sb)) {
  1250. if (my_rsv->rsv_goal_size < *count)
  1251. my_rsv->rsv_goal_size = *count;
  1252. ret = alloc_new_reservation(my_rsv, grp_goal, sb,
  1253. group, bitmap_bh);
  1254. if (ret < 0)
  1255. break; /* failed */
  1256. if (!goal_in_my_reservation(&my_rsv->rsv_window,
  1257. grp_goal, group, sb))
  1258. grp_goal = -1;
  1259. } else if (grp_goal >= 0) {
  1260. int curr = my_rsv->rsv_end -
  1261. (grp_goal + group_first_block) + 1;
  1262. if (curr < *count)
  1263. try_to_extend_reservation(my_rsv, sb,
  1264. *count - curr);
  1265. }
  1266. if ((my_rsv->rsv_start > group_last_block) ||
  1267. (my_rsv->rsv_end < group_first_block)) {
  1268. rsv_window_dump(&EXT3_SB(sb)->s_rsv_window_root, 1);
  1269. BUG();
  1270. }
  1271. ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh,
  1272. grp_goal, &num, &my_rsv->rsv_window);
  1273. if (ret >= 0) {
  1274. my_rsv->rsv_alloc_hit += num;
  1275. *count = num;
  1276. break; /* succeed */
  1277. }
  1278. num = *count;
  1279. }
  1280. out:
  1281. if (ret >= 0) {
  1282. BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for "
  1283. "bitmap block");
  1284. fatal = ext3_journal_dirty_metadata(handle, bitmap_bh);
  1285. if (fatal) {
  1286. *errp = fatal;
  1287. return -1;
  1288. }
  1289. return ret;
  1290. }
  1291. BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
  1292. ext3_journal_release_buffer(handle, bitmap_bh);
  1293. return ret;
  1294. }
  1295. /**
  1296. * ext3_has_free_blocks()
  1297. * @sbi: in-core super block structure.
  1298. *
  1299. * Check if filesystem has at least 1 free block available for allocation.
  1300. */
  1301. static int ext3_has_free_blocks(struct ext3_sb_info *sbi)
  1302. {
  1303. ext3_fsblk_t free_blocks, root_blocks;
  1304. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  1305. root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
  1306. if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
  1307. sbi->s_resuid != current_fsuid() &&
  1308. (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
  1309. return 0;
  1310. }
  1311. return 1;
  1312. }
  1313. /**
  1314. * ext3_should_retry_alloc()
  1315. * @sb: super block
  1316. * @retries number of attemps has been made
  1317. *
  1318. * ext3_should_retry_alloc() is called when ENOSPC is returned, and if
  1319. * it is profitable to retry the operation, this function will wait
  1320. * for the current or commiting transaction to complete, and then
  1321. * return TRUE.
  1322. *
  1323. * if the total number of retries exceed three times, return FALSE.
  1324. */
  1325. int ext3_should_retry_alloc(struct super_block *sb, int *retries)
  1326. {
  1327. if (!ext3_has_free_blocks(EXT3_SB(sb)) || (*retries)++ > 3)
  1328. return 0;
  1329. jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
  1330. return journal_force_commit_nested(EXT3_SB(sb)->s_journal);
  1331. }
  1332. /**
  1333. * ext3_new_blocks() -- core block(s) allocation function
  1334. * @handle: handle to this transaction
  1335. * @inode: file inode
  1336. * @goal: given target block(filesystem wide)
  1337. * @count: target number of blocks to allocate
  1338. * @errp: error code
  1339. *
  1340. * ext3_new_blocks uses a goal block to assist allocation. It tries to
  1341. * allocate block(s) from the block group contains the goal block first. If that
  1342. * fails, it will try to allocate block(s) from other block groups without
  1343. * any specific goal block.
  1344. *
  1345. */
  1346. ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode,
  1347. ext3_fsblk_t goal, unsigned long *count, int *errp)
  1348. {
  1349. struct buffer_head *bitmap_bh = NULL;
  1350. struct buffer_head *gdp_bh;
  1351. int group_no;
  1352. int goal_group;
  1353. ext3_grpblk_t grp_target_blk; /* blockgroup relative goal block */
  1354. ext3_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
  1355. ext3_fsblk_t ret_block; /* filesyetem-wide allocated block */
  1356. int bgi; /* blockgroup iteration index */
  1357. int fatal = 0, err;
  1358. int performed_allocation = 0;
  1359. ext3_grpblk_t free_blocks; /* number of free blocks in a group */
  1360. struct super_block *sb;
  1361. struct ext3_group_desc *gdp;
  1362. struct ext3_super_block *es;
  1363. struct ext3_sb_info *sbi;
  1364. struct ext3_reserve_window_node *my_rsv = NULL;
  1365. struct ext3_block_alloc_info *block_i;
  1366. unsigned short windowsz = 0;
  1367. #ifdef EXT3FS_DEBUG
  1368. static int goal_hits, goal_attempts;
  1369. #endif
  1370. unsigned long ngroups;
  1371. unsigned long num = *count;
  1372. *errp = -ENOSPC;
  1373. sb = inode->i_sb;
  1374. if (!sb) {
  1375. printk("ext3_new_block: nonexistent device");
  1376. return 0;
  1377. }
  1378. /*
  1379. * Check quota for allocation of this block.
  1380. */
  1381. err = dquot_alloc_block(inode, num);
  1382. if (err) {
  1383. *errp = err;
  1384. return 0;
  1385. }
  1386. sbi = EXT3_SB(sb);
  1387. es = EXT3_SB(sb)->s_es;
  1388. ext3_debug("goal=%lu.\n", goal);
  1389. /*
  1390. * Allocate a block from reservation only when
  1391. * filesystem is mounted with reservation(default,-o reservation), and
  1392. * it's a regular file, and
  1393. * the desired window size is greater than 0 (One could use ioctl
  1394. * command EXT3_IOC_SETRSVSZ to set the window size to 0 to turn off
  1395. * reservation on that particular file)
  1396. */
  1397. block_i = EXT3_I(inode)->i_block_alloc_info;
  1398. if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0))
  1399. my_rsv = &block_i->rsv_window_node;
  1400. if (!ext3_has_free_blocks(sbi)) {
  1401. *errp = -ENOSPC;
  1402. goto out;
  1403. }
  1404. /*
  1405. * First, test whether the goal block is free.
  1406. */
  1407. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1408. goal >= le32_to_cpu(es->s_blocks_count))
  1409. goal = le32_to_cpu(es->s_first_data_block);
  1410. group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
  1411. EXT3_BLOCKS_PER_GROUP(sb);
  1412. goal_group = group_no;
  1413. retry_alloc:
  1414. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1415. if (!gdp)
  1416. goto io_error;
  1417. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1418. /*
  1419. * if there is not enough free blocks to make a new resevation
  1420. * turn off reservation for this allocation
  1421. */
  1422. if (my_rsv && (free_blocks < windowsz)
  1423. && (free_blocks > 0)
  1424. && (rsv_is_empty(&my_rsv->rsv_window)))
  1425. my_rsv = NULL;
  1426. if (free_blocks > 0) {
  1427. grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
  1428. EXT3_BLOCKS_PER_GROUP(sb));
  1429. bitmap_bh = read_block_bitmap(sb, group_no);
  1430. if (!bitmap_bh)
  1431. goto io_error;
  1432. grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle,
  1433. group_no, bitmap_bh, grp_target_blk,
  1434. my_rsv, &num, &fatal);
  1435. if (fatal)
  1436. goto out;
  1437. if (grp_alloc_blk >= 0)
  1438. goto allocated;
  1439. }
  1440. ngroups = EXT3_SB(sb)->s_groups_count;
  1441. smp_rmb();
  1442. /*
  1443. * Now search the rest of the groups. We assume that
  1444. * group_no and gdp correctly point to the last group visited.
  1445. */
  1446. for (bgi = 0; bgi < ngroups; bgi++) {
  1447. group_no++;
  1448. if (group_no >= ngroups)
  1449. group_no = 0;
  1450. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1451. if (!gdp)
  1452. goto io_error;
  1453. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1454. /*
  1455. * skip this group (and avoid loading bitmap) if there
  1456. * are no free blocks
  1457. */
  1458. if (!free_blocks)
  1459. continue;
  1460. /*
  1461. * skip this group if the number of
  1462. * free blocks is less than half of the reservation
  1463. * window size.
  1464. */
  1465. if (my_rsv && (free_blocks <= (windowsz/2)))
  1466. continue;
  1467. brelse(bitmap_bh);
  1468. bitmap_bh = read_block_bitmap(sb, group_no);
  1469. if (!bitmap_bh)
  1470. goto io_error;
  1471. /*
  1472. * try to allocate block(s) from this group, without a goal(-1).
  1473. */
  1474. grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle,
  1475. group_no, bitmap_bh, -1, my_rsv,
  1476. &num, &fatal);
  1477. if (fatal)
  1478. goto out;
  1479. if (grp_alloc_blk >= 0)
  1480. goto allocated;
  1481. }
  1482. /*
  1483. * We may end up a bogus ealier ENOSPC error due to
  1484. * filesystem is "full" of reservations, but
  1485. * there maybe indeed free blocks avaliable on disk
  1486. * In this case, we just forget about the reservations
  1487. * just do block allocation as without reservations.
  1488. */
  1489. if (my_rsv) {
  1490. my_rsv = NULL;
  1491. windowsz = 0;
  1492. group_no = goal_group;
  1493. goto retry_alloc;
  1494. }
  1495. /* No space left on the device */
  1496. *errp = -ENOSPC;
  1497. goto out;
  1498. allocated:
  1499. ext3_debug("using block group %d(%d)\n",
  1500. group_no, gdp->bg_free_blocks_count);
  1501. BUFFER_TRACE(gdp_bh, "get_write_access");
  1502. fatal = ext3_journal_get_write_access(handle, gdp_bh);
  1503. if (fatal)
  1504. goto out;
  1505. ret_block = grp_alloc_blk + ext3_group_first_block_no(sb, group_no);
  1506. if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
  1507. in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
  1508. in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
  1509. EXT3_SB(sb)->s_itb_per_group) ||
  1510. in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
  1511. EXT3_SB(sb)->s_itb_per_group)) {
  1512. ext3_error(sb, "ext3_new_block",
  1513. "Allocating block in system zone - "
  1514. "blocks from "E3FSBLK", length %lu",
  1515. ret_block, num);
  1516. /*
  1517. * claim_block() marked the blocks we allocated as in use. So we
  1518. * may want to selectively mark some of the blocks as free.
  1519. */
  1520. goto retry_alloc;
  1521. }
  1522. performed_allocation = 1;
  1523. #ifdef CONFIG_JBD_DEBUG
  1524. {
  1525. struct buffer_head *debug_bh;
  1526. /* Record bitmap buffer state in the newly allocated block */
  1527. debug_bh = sb_find_get_block(sb, ret_block);
  1528. if (debug_bh) {
  1529. BUFFER_TRACE(debug_bh, "state when allocated");
  1530. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
  1531. brelse(debug_bh);
  1532. }
  1533. }
  1534. jbd_lock_bh_state(bitmap_bh);
  1535. spin_lock(sb_bgl_lock(sbi, group_no));
  1536. if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
  1537. int i;
  1538. for (i = 0; i < num; i++) {
  1539. if (ext3_test_bit(grp_alloc_blk+i,
  1540. bh2jh(bitmap_bh)->b_committed_data)) {
  1541. printk("%s: block was unexpectedly set in "
  1542. "b_committed_data\n", __func__);
  1543. }
  1544. }
  1545. }
  1546. ext3_debug("found bit %d\n", grp_alloc_blk);
  1547. spin_unlock(sb_bgl_lock(sbi, group_no));
  1548. jbd_unlock_bh_state(bitmap_bh);
  1549. #endif
  1550. if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
  1551. ext3_error(sb, "ext3_new_block",
  1552. "block("E3FSBLK") >= blocks count(%d) - "
  1553. "block_group = %d, es == %p ", ret_block,
  1554. le32_to_cpu(es->s_blocks_count), group_no, es);
  1555. goto out;
  1556. }
  1557. /*
  1558. * It is up to the caller to add the new buffer to a journal
  1559. * list of some description. We don't know in advance whether
  1560. * the caller wants to use it as metadata or data.
  1561. */
  1562. ext3_debug("allocating block %lu. Goal hits %d of %d.\n",
  1563. ret_block, goal_hits, goal_attempts);
  1564. spin_lock(sb_bgl_lock(sbi, group_no));
  1565. le16_add_cpu(&gdp->bg_free_blocks_count, -num);
  1566. spin_unlock(sb_bgl_lock(sbi, group_no));
  1567. percpu_counter_sub(&sbi->s_freeblocks_counter, num);
  1568. BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
  1569. err = ext3_journal_dirty_metadata(handle, gdp_bh);
  1570. if (!fatal)
  1571. fatal = err;
  1572. if (fatal)
  1573. goto out;
  1574. *errp = 0;
  1575. brelse(bitmap_bh);
  1576. dquot_free_block(inode, *count-num);
  1577. *count = num;
  1578. return ret_block;
  1579. io_error:
  1580. *errp = -EIO;
  1581. out:
  1582. if (fatal) {
  1583. *errp = fatal;
  1584. ext3_std_error(sb, fatal);
  1585. }
  1586. /*
  1587. * Undo the block allocation
  1588. */
  1589. if (!performed_allocation)
  1590. dquot_free_block(inode, *count);
  1591. brelse(bitmap_bh);
  1592. return 0;
  1593. }
  1594. ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
  1595. ext3_fsblk_t goal, int *errp)
  1596. {
  1597. unsigned long count = 1;
  1598. return ext3_new_blocks(handle, inode, goal, &count, errp);
  1599. }
  1600. /**
  1601. * ext3_count_free_blocks() -- count filesystem free blocks
  1602. * @sb: superblock
  1603. *
  1604. * Adds up the number of free blocks from each block group.
  1605. */
  1606. ext3_fsblk_t ext3_count_free_blocks(struct super_block *sb)
  1607. {
  1608. ext3_fsblk_t desc_count;
  1609. struct ext3_group_desc *gdp;
  1610. int i;
  1611. unsigned long ngroups = EXT3_SB(sb)->s_groups_count;
  1612. #ifdef EXT3FS_DEBUG
  1613. struct ext3_super_block *es;
  1614. ext3_fsblk_t bitmap_count;
  1615. unsigned long x;
  1616. struct buffer_head *bitmap_bh = NULL;
  1617. es = EXT3_SB(sb)->s_es;
  1618. desc_count = 0;
  1619. bitmap_count = 0;
  1620. gdp = NULL;
  1621. smp_rmb();
  1622. for (i = 0; i < ngroups; i++) {
  1623. gdp = ext3_get_group_desc(sb, i, NULL);
  1624. if (!gdp)
  1625. continue;
  1626. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1627. brelse(bitmap_bh);
  1628. bitmap_bh = read_block_bitmap(sb, i);
  1629. if (bitmap_bh == NULL)
  1630. continue;
  1631. x = ext3_count_free(bitmap_bh, sb->s_blocksize);
  1632. printk("group %d: stored = %d, counted = %lu\n",
  1633. i, le16_to_cpu(gdp->bg_free_blocks_count), x);
  1634. bitmap_count += x;
  1635. }
  1636. brelse(bitmap_bh);
  1637. printk("ext3_count_free_blocks: stored = "E3FSBLK
  1638. ", computed = "E3FSBLK", "E3FSBLK"\n",
  1639. le32_to_cpu(es->s_free_blocks_count),
  1640. desc_count, bitmap_count);
  1641. return bitmap_count;
  1642. #else
  1643. desc_count = 0;
  1644. smp_rmb();
  1645. for (i = 0; i < ngroups; i++) {
  1646. gdp = ext3_get_group_desc(sb, i, NULL);
  1647. if (!gdp)
  1648. continue;
  1649. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1650. }
  1651. return desc_count;
  1652. #endif
  1653. }
  1654. static inline int test_root(int a, int b)
  1655. {
  1656. int num = b;
  1657. while (a > num)
  1658. num *= b;
  1659. return num == a;
  1660. }
  1661. static int ext3_group_sparse(int group)
  1662. {
  1663. if (group <= 1)
  1664. return 1;
  1665. if (!(group & 1))
  1666. return 0;
  1667. return (test_root(group, 7) || test_root(group, 5) ||
  1668. test_root(group, 3));
  1669. }
  1670. /**
  1671. * ext3_bg_has_super - number of blocks used by the superblock in group
  1672. * @sb: superblock for filesystem
  1673. * @group: group number to check
  1674. *
  1675. * Return the number of blocks used by the superblock (primary or backup)
  1676. * in this group. Currently this will be only 0 or 1.
  1677. */
  1678. int ext3_bg_has_super(struct super_block *sb, int group)
  1679. {
  1680. if (EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1681. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1682. !ext3_group_sparse(group))
  1683. return 0;
  1684. return 1;
  1685. }
  1686. static unsigned long ext3_bg_num_gdb_meta(struct super_block *sb, int group)
  1687. {
  1688. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1689. unsigned long first = metagroup * EXT3_DESC_PER_BLOCK(sb);
  1690. unsigned long last = first + EXT3_DESC_PER_BLOCK(sb) - 1;
  1691. if (group == first || group == first + 1 || group == last)
  1692. return 1;
  1693. return 0;
  1694. }
  1695. static unsigned long ext3_bg_num_gdb_nometa(struct super_block *sb, int group)
  1696. {
  1697. return ext3_bg_has_super(sb, group) ? EXT3_SB(sb)->s_gdb_count : 0;
  1698. }
  1699. /**
  1700. * ext3_bg_num_gdb - number of blocks used by the group table in group
  1701. * @sb: superblock for filesystem
  1702. * @group: group number to check
  1703. *
  1704. * Return the number of blocks used by the group descriptor table
  1705. * (primary or backup) in this group. In the future there may be a
  1706. * different number of descriptor blocks in each group.
  1707. */
  1708. unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
  1709. {
  1710. unsigned long first_meta_bg =
  1711. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_meta_bg);
  1712. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1713. if (!EXT3_HAS_INCOMPAT_FEATURE(sb,EXT3_FEATURE_INCOMPAT_META_BG) ||
  1714. metagroup < first_meta_bg)
  1715. return ext3_bg_num_gdb_nometa(sb,group);
  1716. return ext3_bg_num_gdb_meta(sb,group);
  1717. }