balloc.c 54 KB

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