balloc.c 53 KB

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