balloc.c 54 KB

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