balloc.c 53 KB

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