balloc.c 46 KB

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