ialloc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * linux/fs/ext4/ialloc.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. * BSD ufs-inspired inode and directory allocation by
  10. * Stephen Tweedie (sct@redhat.com), 1993
  11. * Big-endian to little-endian byte-swapping/bitmaps by
  12. * David S. Miller (davem@caip.rutgers.edu), 1995
  13. */
  14. #include <linux/time.h>
  15. #include <linux/fs.h>
  16. #include <linux/jbd2.h>
  17. #include <linux/stat.h>
  18. #include <linux/string.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/random.h>
  22. #include <linux/bitops.h>
  23. #include <linux/blkdev.h>
  24. #include <asm/byteorder.h>
  25. #include "ext4.h"
  26. #include "ext4_jbd2.h"
  27. #include "xattr.h"
  28. #include "acl.h"
  29. #include <trace/events/ext4.h>
  30. /*
  31. * ialloc.c contains the inodes allocation and deallocation routines
  32. */
  33. /*
  34. * The free inodes are managed by bitmaps. A file system contains several
  35. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  36. * block for inodes, N blocks for the inode table and data blocks.
  37. *
  38. * The file system contains group descriptors which are located after the
  39. * super block. Each descriptor contains the number of the bitmap block and
  40. * the free blocks count in the block.
  41. */
  42. /*
  43. * To avoid calling the atomic setbit hundreds or thousands of times, we only
  44. * need to use it within a single byte (to ensure we get endianness right).
  45. * We can use memset for the rest of the bitmap as there are no other users.
  46. */
  47. void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
  48. {
  49. int i;
  50. if (start_bit >= end_bit)
  51. return;
  52. ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
  53. for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
  54. ext4_set_bit(i, bitmap);
  55. if (i < end_bit)
  56. memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
  57. }
  58. /* Initializes an uninitialized inode bitmap */
  59. static unsigned ext4_init_inode_bitmap(struct super_block *sb,
  60. struct buffer_head *bh,
  61. ext4_group_t block_group,
  62. struct ext4_group_desc *gdp)
  63. {
  64. struct ext4_sb_info *sbi = EXT4_SB(sb);
  65. J_ASSERT_BH(bh, buffer_locked(bh));
  66. /* If checksum is bad mark all blocks and inodes use to prevent
  67. * allocation, essentially implementing a per-group read-only flag. */
  68. if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
  69. ext4_error(sb, "Checksum bad for group %u", block_group);
  70. ext4_free_blks_set(sb, gdp, 0);
  71. ext4_free_inodes_set(sb, gdp, 0);
  72. ext4_itable_unused_set(sb, gdp, 0);
  73. memset(bh->b_data, 0xff, sb->s_blocksize);
  74. return 0;
  75. }
  76. memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
  77. ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
  78. bh->b_data);
  79. return EXT4_INODES_PER_GROUP(sb);
  80. }
  81. /*
  82. * Read the inode allocation bitmap for a given block_group, reading
  83. * into the specified slot in the superblock's bitmap cache.
  84. *
  85. * Return buffer_head of bitmap on success or NULL.
  86. */
  87. static struct buffer_head *
  88. ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
  89. {
  90. struct ext4_group_desc *desc;
  91. struct buffer_head *bh = NULL;
  92. ext4_fsblk_t bitmap_blk;
  93. desc = ext4_get_group_desc(sb, block_group, NULL);
  94. if (!desc)
  95. return NULL;
  96. bitmap_blk = ext4_inode_bitmap(sb, desc);
  97. bh = sb_getblk(sb, bitmap_blk);
  98. if (unlikely(!bh)) {
  99. ext4_error(sb, "Cannot read inode bitmap - "
  100. "block_group = %u, inode_bitmap = %llu",
  101. block_group, bitmap_blk);
  102. return NULL;
  103. }
  104. if (bitmap_uptodate(bh))
  105. return bh;
  106. lock_buffer(bh);
  107. if (bitmap_uptodate(bh)) {
  108. unlock_buffer(bh);
  109. return bh;
  110. }
  111. ext4_lock_group(sb, block_group);
  112. if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  113. ext4_init_inode_bitmap(sb, bh, block_group, desc);
  114. set_bitmap_uptodate(bh);
  115. set_buffer_uptodate(bh);
  116. ext4_unlock_group(sb, block_group);
  117. unlock_buffer(bh);
  118. return bh;
  119. }
  120. ext4_unlock_group(sb, block_group);
  121. if (buffer_uptodate(bh)) {
  122. /*
  123. * if not uninit if bh is uptodate,
  124. * bitmap is also uptodate
  125. */
  126. set_bitmap_uptodate(bh);
  127. unlock_buffer(bh);
  128. return bh;
  129. }
  130. /*
  131. * submit the buffer_head for read. We can
  132. * safely mark the bitmap as uptodate now.
  133. * We do it here so the bitmap uptodate bit
  134. * get set with buffer lock held.
  135. */
  136. trace_ext4_load_inode_bitmap(sb, block_group);
  137. set_bitmap_uptodate(bh);
  138. if (bh_submit_read(bh) < 0) {
  139. put_bh(bh);
  140. ext4_error(sb, "Cannot read inode bitmap - "
  141. "block_group = %u, inode_bitmap = %llu",
  142. block_group, bitmap_blk);
  143. return NULL;
  144. }
  145. return bh;
  146. }
  147. /*
  148. * NOTE! When we get the inode, we're the only people
  149. * that have access to it, and as such there are no
  150. * race conditions we have to worry about. The inode
  151. * is not on the hash-lists, and it cannot be reached
  152. * through the filesystem because the directory entry
  153. * has been deleted earlier.
  154. *
  155. * HOWEVER: we must make sure that we get no aliases,
  156. * which means that we have to call "clear_inode()"
  157. * _before_ we mark the inode not in use in the inode
  158. * bitmaps. Otherwise a newly created file might use
  159. * the same inode number (not actually the same pointer
  160. * though), and then we'd have two inodes sharing the
  161. * same inode number and space on the harddisk.
  162. */
  163. void ext4_free_inode(handle_t *handle, struct inode *inode)
  164. {
  165. struct super_block *sb = inode->i_sb;
  166. int is_directory;
  167. unsigned long ino;
  168. struct buffer_head *bitmap_bh = NULL;
  169. struct buffer_head *bh2;
  170. ext4_group_t block_group;
  171. unsigned long bit;
  172. struct ext4_group_desc *gdp;
  173. struct ext4_super_block *es;
  174. struct ext4_sb_info *sbi;
  175. int fatal = 0, err, count, cleared;
  176. if (atomic_read(&inode->i_count) > 1) {
  177. printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
  178. atomic_read(&inode->i_count));
  179. return;
  180. }
  181. if (inode->i_nlink) {
  182. printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
  183. inode->i_nlink);
  184. return;
  185. }
  186. if (!sb) {
  187. printk(KERN_ERR "ext4_free_inode: inode on "
  188. "nonexistent device\n");
  189. return;
  190. }
  191. sbi = EXT4_SB(sb);
  192. ino = inode->i_ino;
  193. ext4_debug("freeing inode %lu\n", ino);
  194. trace_ext4_free_inode(inode);
  195. /*
  196. * Note: we must free any quota before locking the superblock,
  197. * as writing the quota to disk may need the lock as well.
  198. */
  199. dquot_initialize(inode);
  200. ext4_xattr_delete_inode(handle, inode);
  201. dquot_free_inode(inode);
  202. dquot_drop(inode);
  203. is_directory = S_ISDIR(inode->i_mode);
  204. /* Do this BEFORE marking the inode not in use or returning an error */
  205. ext4_clear_inode(inode);
  206. es = EXT4_SB(sb)->s_es;
  207. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  208. ext4_error(sb, "reserved or nonexistent inode %lu", ino);
  209. goto error_return;
  210. }
  211. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  212. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  213. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  214. if (!bitmap_bh)
  215. goto error_return;
  216. BUFFER_TRACE(bitmap_bh, "get_write_access");
  217. fatal = ext4_journal_get_write_access(handle, bitmap_bh);
  218. if (fatal)
  219. goto error_return;
  220. fatal = -ESRCH;
  221. gdp = ext4_get_group_desc(sb, block_group, &bh2);
  222. if (gdp) {
  223. BUFFER_TRACE(bh2, "get_write_access");
  224. fatal = ext4_journal_get_write_access(handle, bh2);
  225. }
  226. ext4_lock_group(sb, block_group);
  227. cleared = ext4_clear_bit(bit, bitmap_bh->b_data);
  228. if (fatal || !cleared) {
  229. ext4_unlock_group(sb, block_group);
  230. goto out;
  231. }
  232. count = ext4_free_inodes_count(sb, gdp) + 1;
  233. ext4_free_inodes_set(sb, gdp, count);
  234. if (is_directory) {
  235. count = ext4_used_dirs_count(sb, gdp) - 1;
  236. ext4_used_dirs_set(sb, gdp, count);
  237. percpu_counter_dec(&sbi->s_dirs_counter);
  238. }
  239. gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
  240. ext4_unlock_group(sb, block_group);
  241. percpu_counter_inc(&sbi->s_freeinodes_counter);
  242. if (sbi->s_log_groups_per_flex) {
  243. ext4_group_t f = ext4_flex_group(sbi, block_group);
  244. atomic_inc(&sbi->s_flex_groups[f].free_inodes);
  245. if (is_directory)
  246. atomic_dec(&sbi->s_flex_groups[f].used_dirs);
  247. }
  248. BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
  249. fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
  250. out:
  251. if (cleared) {
  252. BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
  253. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  254. if (!fatal)
  255. fatal = err;
  256. ext4_mark_super_dirty(sb);
  257. } else
  258. ext4_error(sb, "bit already cleared for inode %lu", ino);
  259. error_return:
  260. brelse(bitmap_bh);
  261. ext4_std_error(sb, fatal);
  262. }
  263. /*
  264. * There are two policies for allocating an inode. If the new inode is
  265. * a directory, then a forward search is made for a block group with both
  266. * free space and a low directory-to-inode ratio; if that fails, then of
  267. * the groups with above-average free space, that group with the fewest
  268. * directories already is chosen.
  269. *
  270. * For other inodes, search forward from the parent directory\'s block
  271. * group to find a free inode.
  272. */
  273. static int find_group_dir(struct super_block *sb, struct inode *parent,
  274. ext4_group_t *best_group)
  275. {
  276. ext4_group_t ngroups = ext4_get_groups_count(sb);
  277. unsigned int freei, avefreei;
  278. struct ext4_group_desc *desc, *best_desc = NULL;
  279. ext4_group_t group;
  280. int ret = -1;
  281. freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
  282. avefreei = freei / ngroups;
  283. for (group = 0; group < ngroups; group++) {
  284. desc = ext4_get_group_desc(sb, group, NULL);
  285. if (!desc || !ext4_free_inodes_count(sb, desc))
  286. continue;
  287. if (ext4_free_inodes_count(sb, desc) < avefreei)
  288. continue;
  289. if (!best_desc ||
  290. (ext4_free_blks_count(sb, desc) >
  291. ext4_free_blks_count(sb, best_desc))) {
  292. *best_group = group;
  293. best_desc = desc;
  294. ret = 0;
  295. }
  296. }
  297. return ret;
  298. }
  299. #define free_block_ratio 10
  300. static int find_group_flex(struct super_block *sb, struct inode *parent,
  301. ext4_group_t *best_group)
  302. {
  303. struct ext4_sb_info *sbi = EXT4_SB(sb);
  304. struct ext4_group_desc *desc;
  305. struct flex_groups *flex_group = sbi->s_flex_groups;
  306. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  307. ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
  308. ext4_group_t ngroups = ext4_get_groups_count(sb);
  309. int flex_size = ext4_flex_bg_size(sbi);
  310. ext4_group_t best_flex = parent_fbg_group;
  311. int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
  312. int flexbg_free_blocks;
  313. int flex_freeb_ratio;
  314. ext4_group_t n_fbg_groups;
  315. ext4_group_t i;
  316. n_fbg_groups = (ngroups + flex_size - 1) >>
  317. sbi->s_log_groups_per_flex;
  318. find_close_to_parent:
  319. flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks);
  320. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  321. if (atomic_read(&flex_group[best_flex].free_inodes) &&
  322. flex_freeb_ratio > free_block_ratio)
  323. goto found_flexbg;
  324. if (best_flex && best_flex == parent_fbg_group) {
  325. best_flex--;
  326. goto find_close_to_parent;
  327. }
  328. for (i = 0; i < n_fbg_groups; i++) {
  329. if (i == parent_fbg_group || i == parent_fbg_group - 1)
  330. continue;
  331. flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks);
  332. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  333. if (flex_freeb_ratio > free_block_ratio &&
  334. (atomic_read(&flex_group[i].free_inodes))) {
  335. best_flex = i;
  336. goto found_flexbg;
  337. }
  338. if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) ||
  339. ((atomic_read(&flex_group[i].free_blocks) >
  340. atomic_read(&flex_group[best_flex].free_blocks)) &&
  341. atomic_read(&flex_group[i].free_inodes)))
  342. best_flex = i;
  343. }
  344. if (!atomic_read(&flex_group[best_flex].free_inodes) ||
  345. !atomic_read(&flex_group[best_flex].free_blocks))
  346. return -1;
  347. found_flexbg:
  348. for (i = best_flex * flex_size; i < ngroups &&
  349. i < (best_flex + 1) * flex_size; i++) {
  350. desc = ext4_get_group_desc(sb, i, NULL);
  351. if (ext4_free_inodes_count(sb, desc)) {
  352. *best_group = i;
  353. goto out;
  354. }
  355. }
  356. return -1;
  357. out:
  358. return 0;
  359. }
  360. struct orlov_stats {
  361. __u32 free_inodes;
  362. __u32 free_blocks;
  363. __u32 used_dirs;
  364. };
  365. /*
  366. * Helper function for Orlov's allocator; returns critical information
  367. * for a particular block group or flex_bg. If flex_size is 1, then g
  368. * is a block group number; otherwise it is flex_bg number.
  369. */
  370. static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
  371. int flex_size, struct orlov_stats *stats)
  372. {
  373. struct ext4_group_desc *desc;
  374. struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
  375. if (flex_size > 1) {
  376. stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
  377. stats->free_blocks = atomic_read(&flex_group[g].free_blocks);
  378. stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
  379. return;
  380. }
  381. desc = ext4_get_group_desc(sb, g, NULL);
  382. if (desc) {
  383. stats->free_inodes = ext4_free_inodes_count(sb, desc);
  384. stats->free_blocks = ext4_free_blks_count(sb, desc);
  385. stats->used_dirs = ext4_used_dirs_count(sb, desc);
  386. } else {
  387. stats->free_inodes = 0;
  388. stats->free_blocks = 0;
  389. stats->used_dirs = 0;
  390. }
  391. }
  392. /*
  393. * Orlov's allocator for directories.
  394. *
  395. * We always try to spread first-level directories.
  396. *
  397. * If there are blockgroups with both free inodes and free blocks counts
  398. * not worse than average we return one with smallest directory count.
  399. * Otherwise we simply return a random group.
  400. *
  401. * For the rest rules look so:
  402. *
  403. * It's OK to put directory into a group unless
  404. * it has too many directories already (max_dirs) or
  405. * it has too few free inodes left (min_inodes) or
  406. * it has too few free blocks left (min_blocks) or
  407. * Parent's group is preferred, if it doesn't satisfy these
  408. * conditions we search cyclically through the rest. If none
  409. * of the groups look good we just look for a group with more
  410. * free inodes than average (starting at parent's group).
  411. */
  412. static int find_group_orlov(struct super_block *sb, struct inode *parent,
  413. ext4_group_t *group, int mode,
  414. const struct qstr *qstr)
  415. {
  416. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  417. struct ext4_sb_info *sbi = EXT4_SB(sb);
  418. ext4_group_t real_ngroups = ext4_get_groups_count(sb);
  419. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  420. unsigned int freei, avefreei;
  421. ext4_fsblk_t freeb, avefreeb;
  422. unsigned int ndirs;
  423. int max_dirs, min_inodes;
  424. ext4_grpblk_t min_blocks;
  425. ext4_group_t i, grp, g, ngroups;
  426. struct ext4_group_desc *desc;
  427. struct orlov_stats stats;
  428. int flex_size = ext4_flex_bg_size(sbi);
  429. struct dx_hash_info hinfo;
  430. ngroups = real_ngroups;
  431. if (flex_size > 1) {
  432. ngroups = (real_ngroups + flex_size - 1) >>
  433. sbi->s_log_groups_per_flex;
  434. parent_group >>= sbi->s_log_groups_per_flex;
  435. }
  436. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  437. avefreei = freei / ngroups;
  438. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  439. avefreeb = freeb;
  440. do_div(avefreeb, ngroups);
  441. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  442. if (S_ISDIR(mode) &&
  443. ((parent == sb->s_root->d_inode) ||
  444. (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
  445. int best_ndir = inodes_per_group;
  446. int ret = -1;
  447. if (qstr) {
  448. hinfo.hash_version = DX_HASH_HALF_MD4;
  449. hinfo.seed = sbi->s_hash_seed;
  450. ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
  451. grp = hinfo.hash;
  452. } else
  453. get_random_bytes(&grp, sizeof(grp));
  454. parent_group = (unsigned)grp % ngroups;
  455. for (i = 0; i < ngroups; i++) {
  456. g = (parent_group + i) % ngroups;
  457. get_orlov_stats(sb, g, flex_size, &stats);
  458. if (!stats.free_inodes)
  459. continue;
  460. if (stats.used_dirs >= best_ndir)
  461. continue;
  462. if (stats.free_inodes < avefreei)
  463. continue;
  464. if (stats.free_blocks < avefreeb)
  465. continue;
  466. grp = g;
  467. ret = 0;
  468. best_ndir = stats.used_dirs;
  469. }
  470. if (ret)
  471. goto fallback;
  472. found_flex_bg:
  473. if (flex_size == 1) {
  474. *group = grp;
  475. return 0;
  476. }
  477. /*
  478. * We pack inodes at the beginning of the flexgroup's
  479. * inode tables. Block allocation decisions will do
  480. * something similar, although regular files will
  481. * start at 2nd block group of the flexgroup. See
  482. * ext4_ext_find_goal() and ext4_find_near().
  483. */
  484. grp *= flex_size;
  485. for (i = 0; i < flex_size; i++) {
  486. if (grp+i >= real_ngroups)
  487. break;
  488. desc = ext4_get_group_desc(sb, grp+i, NULL);
  489. if (desc && ext4_free_inodes_count(sb, desc)) {
  490. *group = grp+i;
  491. return 0;
  492. }
  493. }
  494. goto fallback;
  495. }
  496. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  497. min_inodes = avefreei - inodes_per_group*flex_size / 4;
  498. if (min_inodes < 1)
  499. min_inodes = 1;
  500. min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4;
  501. /*
  502. * Start looking in the flex group where we last allocated an
  503. * inode for this parent directory
  504. */
  505. if (EXT4_I(parent)->i_last_alloc_group != ~0) {
  506. parent_group = EXT4_I(parent)->i_last_alloc_group;
  507. if (flex_size > 1)
  508. parent_group >>= sbi->s_log_groups_per_flex;
  509. }
  510. for (i = 0; i < ngroups; i++) {
  511. grp = (parent_group + i) % ngroups;
  512. get_orlov_stats(sb, grp, flex_size, &stats);
  513. if (stats.used_dirs >= max_dirs)
  514. continue;
  515. if (stats.free_inodes < min_inodes)
  516. continue;
  517. if (stats.free_blocks < min_blocks)
  518. continue;
  519. goto found_flex_bg;
  520. }
  521. fallback:
  522. ngroups = real_ngroups;
  523. avefreei = freei / ngroups;
  524. fallback_retry:
  525. parent_group = EXT4_I(parent)->i_block_group;
  526. for (i = 0; i < ngroups; i++) {
  527. grp = (parent_group + i) % ngroups;
  528. desc = ext4_get_group_desc(sb, grp, NULL);
  529. if (desc && ext4_free_inodes_count(sb, desc) &&
  530. ext4_free_inodes_count(sb, desc) >= avefreei) {
  531. *group = grp;
  532. return 0;
  533. }
  534. }
  535. if (avefreei) {
  536. /*
  537. * The free-inodes counter is approximate, and for really small
  538. * filesystems the above test can fail to find any blockgroups
  539. */
  540. avefreei = 0;
  541. goto fallback_retry;
  542. }
  543. return -1;
  544. }
  545. static int find_group_other(struct super_block *sb, struct inode *parent,
  546. ext4_group_t *group, int mode)
  547. {
  548. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  549. ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
  550. struct ext4_group_desc *desc;
  551. int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
  552. /*
  553. * Try to place the inode is the same flex group as its
  554. * parent. If we can't find space, use the Orlov algorithm to
  555. * find another flex group, and store that information in the
  556. * parent directory's inode information so that use that flex
  557. * group for future allocations.
  558. */
  559. if (flex_size > 1) {
  560. int retry = 0;
  561. try_again:
  562. parent_group &= ~(flex_size-1);
  563. last = parent_group + flex_size;
  564. if (last > ngroups)
  565. last = ngroups;
  566. for (i = parent_group; i < last; i++) {
  567. desc = ext4_get_group_desc(sb, i, NULL);
  568. if (desc && ext4_free_inodes_count(sb, desc)) {
  569. *group = i;
  570. return 0;
  571. }
  572. }
  573. if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
  574. retry = 1;
  575. parent_group = EXT4_I(parent)->i_last_alloc_group;
  576. goto try_again;
  577. }
  578. /*
  579. * If this didn't work, use the Orlov search algorithm
  580. * to find a new flex group; we pass in the mode to
  581. * avoid the topdir algorithms.
  582. */
  583. *group = parent_group + flex_size;
  584. if (*group > ngroups)
  585. *group = 0;
  586. return find_group_orlov(sb, parent, group, mode, NULL);
  587. }
  588. /*
  589. * Try to place the inode in its parent directory
  590. */
  591. *group = parent_group;
  592. desc = ext4_get_group_desc(sb, *group, NULL);
  593. if (desc && ext4_free_inodes_count(sb, desc) &&
  594. ext4_free_blks_count(sb, desc))
  595. return 0;
  596. /*
  597. * We're going to place this inode in a different blockgroup from its
  598. * parent. We want to cause files in a common directory to all land in
  599. * the same blockgroup. But we want files which are in a different
  600. * directory which shares a blockgroup with our parent to land in a
  601. * different blockgroup.
  602. *
  603. * So add our directory's i_ino into the starting point for the hash.
  604. */
  605. *group = (*group + parent->i_ino) % ngroups;
  606. /*
  607. * Use a quadratic hash to find a group with a free inode and some free
  608. * blocks.
  609. */
  610. for (i = 1; i < ngroups; i <<= 1) {
  611. *group += i;
  612. if (*group >= ngroups)
  613. *group -= ngroups;
  614. desc = ext4_get_group_desc(sb, *group, NULL);
  615. if (desc && ext4_free_inodes_count(sb, desc) &&
  616. ext4_free_blks_count(sb, desc))
  617. return 0;
  618. }
  619. /*
  620. * That failed: try linear search for a free inode, even if that group
  621. * has no free blocks.
  622. */
  623. *group = parent_group;
  624. for (i = 0; i < ngroups; i++) {
  625. if (++*group >= ngroups)
  626. *group = 0;
  627. desc = ext4_get_group_desc(sb, *group, NULL);
  628. if (desc && ext4_free_inodes_count(sb, desc))
  629. return 0;
  630. }
  631. return -1;
  632. }
  633. /*
  634. * claim the inode from the inode bitmap. If the group
  635. * is uninit we need to take the groups's ext4_group_lock
  636. * and clear the uninit flag. The inode bitmap update
  637. * and group desc uninit flag clear should be done
  638. * after holding ext4_group_lock so that ext4_read_inode_bitmap
  639. * doesn't race with the ext4_claim_inode
  640. */
  641. static int ext4_claim_inode(struct super_block *sb,
  642. struct buffer_head *inode_bitmap_bh,
  643. unsigned long ino, ext4_group_t group, int mode)
  644. {
  645. int free = 0, retval = 0, count;
  646. struct ext4_sb_info *sbi = EXT4_SB(sb);
  647. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  648. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
  649. /*
  650. * We have to be sure that new inode allocation does not race with
  651. * inode table initialization, because otherwise we may end up
  652. * allocating and writing new inode right before sb_issue_zeroout
  653. * takes place and overwriting our new inode with zeroes. So we
  654. * take alloc_sem to prevent it.
  655. */
  656. down_read(&grp->alloc_sem);
  657. ext4_lock_group(sb, group);
  658. if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
  659. /* not a free inode */
  660. retval = 1;
  661. goto err_ret;
  662. }
  663. ino++;
  664. if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
  665. ino > EXT4_INODES_PER_GROUP(sb)) {
  666. ext4_unlock_group(sb, group);
  667. up_read(&grp->alloc_sem);
  668. ext4_error(sb, "reserved inode or inode > inodes count - "
  669. "block_group = %u, inode=%lu", group,
  670. ino + group * EXT4_INODES_PER_GROUP(sb));
  671. return 1;
  672. }
  673. /* If we didn't allocate from within the initialized part of the inode
  674. * table then we need to initialize up to this inode. */
  675. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  676. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  677. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  678. /* When marking the block group with
  679. * ~EXT4_BG_INODE_UNINIT we don't want to depend
  680. * on the value of bg_itable_unused even though
  681. * mke2fs could have initialized the same for us.
  682. * Instead we calculated the value below
  683. */
  684. free = 0;
  685. } else {
  686. free = EXT4_INODES_PER_GROUP(sb) -
  687. ext4_itable_unused_count(sb, gdp);
  688. }
  689. /*
  690. * Check the relative inode number against the last used
  691. * relative inode number in this group. if it is greater
  692. * we need to update the bg_itable_unused count
  693. *
  694. */
  695. if (ino > free)
  696. ext4_itable_unused_set(sb, gdp,
  697. (EXT4_INODES_PER_GROUP(sb) - ino));
  698. }
  699. count = ext4_free_inodes_count(sb, gdp) - 1;
  700. ext4_free_inodes_set(sb, gdp, count);
  701. if (S_ISDIR(mode)) {
  702. count = ext4_used_dirs_count(sb, gdp) + 1;
  703. ext4_used_dirs_set(sb, gdp, count);
  704. if (sbi->s_log_groups_per_flex) {
  705. ext4_group_t f = ext4_flex_group(sbi, group);
  706. atomic_inc(&sbi->s_flex_groups[f].used_dirs);
  707. }
  708. }
  709. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  710. err_ret:
  711. ext4_unlock_group(sb, group);
  712. up_read(&grp->alloc_sem);
  713. return retval;
  714. }
  715. /*
  716. * There are two policies for allocating an inode. If the new inode is
  717. * a directory, then a forward search is made for a block group with both
  718. * free space and a low directory-to-inode ratio; if that fails, then of
  719. * the groups with above-average free space, that group with the fewest
  720. * directories already is chosen.
  721. *
  722. * For other inodes, search forward from the parent directory's block
  723. * group to find a free inode.
  724. */
  725. struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode,
  726. const struct qstr *qstr, __u32 goal)
  727. {
  728. struct super_block *sb;
  729. struct buffer_head *inode_bitmap_bh = NULL;
  730. struct buffer_head *group_desc_bh;
  731. ext4_group_t ngroups, group = 0;
  732. unsigned long ino = 0;
  733. struct inode *inode;
  734. struct ext4_group_desc *gdp = NULL;
  735. struct ext4_inode_info *ei;
  736. struct ext4_sb_info *sbi;
  737. int ret2, err = 0;
  738. struct inode *ret;
  739. ext4_group_t i;
  740. int free = 0;
  741. static int once = 1;
  742. ext4_group_t flex_group;
  743. /* Cannot create files in a deleted directory */
  744. if (!dir || !dir->i_nlink)
  745. return ERR_PTR(-EPERM);
  746. sb = dir->i_sb;
  747. ngroups = ext4_get_groups_count(sb);
  748. trace_ext4_request_inode(dir, mode);
  749. inode = new_inode(sb);
  750. if (!inode)
  751. return ERR_PTR(-ENOMEM);
  752. ei = EXT4_I(inode);
  753. sbi = EXT4_SB(sb);
  754. if (!goal)
  755. goal = sbi->s_inode_goal;
  756. if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
  757. group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
  758. ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
  759. ret2 = 0;
  760. goto got_group;
  761. }
  762. if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) {
  763. ret2 = find_group_flex(sb, dir, &group);
  764. if (ret2 == -1) {
  765. ret2 = find_group_other(sb, dir, &group, mode);
  766. if (ret2 == 0 && once) {
  767. once = 0;
  768. printk(KERN_NOTICE "ext4: find_group_flex "
  769. "failed, fallback succeeded dir %lu\n",
  770. dir->i_ino);
  771. }
  772. }
  773. goto got_group;
  774. }
  775. if (S_ISDIR(mode)) {
  776. if (test_opt(sb, OLDALLOC))
  777. ret2 = find_group_dir(sb, dir, &group);
  778. else
  779. ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
  780. } else
  781. ret2 = find_group_other(sb, dir, &group, mode);
  782. got_group:
  783. EXT4_I(dir)->i_last_alloc_group = group;
  784. err = -ENOSPC;
  785. if (ret2 == -1)
  786. goto out;
  787. for (i = 0; i < ngroups; i++, ino = 0) {
  788. err = -EIO;
  789. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  790. if (!gdp)
  791. goto fail;
  792. brelse(inode_bitmap_bh);
  793. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  794. if (!inode_bitmap_bh)
  795. goto fail;
  796. repeat_in_this_group:
  797. ino = ext4_find_next_zero_bit((unsigned long *)
  798. inode_bitmap_bh->b_data,
  799. EXT4_INODES_PER_GROUP(sb), ino);
  800. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  801. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  802. err = ext4_journal_get_write_access(handle,
  803. inode_bitmap_bh);
  804. if (err)
  805. goto fail;
  806. BUFFER_TRACE(group_desc_bh, "get_write_access");
  807. err = ext4_journal_get_write_access(handle,
  808. group_desc_bh);
  809. if (err)
  810. goto fail;
  811. if (!ext4_claim_inode(sb, inode_bitmap_bh,
  812. ino, group, mode)) {
  813. /* we won it */
  814. BUFFER_TRACE(inode_bitmap_bh,
  815. "call ext4_handle_dirty_metadata");
  816. err = ext4_handle_dirty_metadata(handle,
  817. NULL,
  818. inode_bitmap_bh);
  819. if (err)
  820. goto fail;
  821. /* zero bit is inode number 1*/
  822. ino++;
  823. goto got;
  824. }
  825. /* we lost it */
  826. ext4_handle_release_buffer(handle, inode_bitmap_bh);
  827. ext4_handle_release_buffer(handle, group_desc_bh);
  828. if (++ino < EXT4_INODES_PER_GROUP(sb))
  829. goto repeat_in_this_group;
  830. }
  831. /*
  832. * This case is possible in concurrent environment. It is very
  833. * rare. We cannot repeat the find_group_xxx() call because
  834. * that will simply return the same blockgroup, because the
  835. * group descriptor metadata has not yet been updated.
  836. * So we just go onto the next blockgroup.
  837. */
  838. if (++group == ngroups)
  839. group = 0;
  840. }
  841. err = -ENOSPC;
  842. goto out;
  843. got:
  844. /* We may have to initialize the block bitmap if it isn't already */
  845. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
  846. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  847. struct buffer_head *block_bitmap_bh;
  848. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  849. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  850. err = ext4_journal_get_write_access(handle, block_bitmap_bh);
  851. if (err) {
  852. brelse(block_bitmap_bh);
  853. goto fail;
  854. }
  855. free = 0;
  856. ext4_lock_group(sb, group);
  857. /* recheck and clear flag under lock if we still need to */
  858. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  859. free = ext4_free_blocks_after_init(sb, group, gdp);
  860. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  861. ext4_free_blks_set(sb, gdp, free);
  862. gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
  863. gdp);
  864. }
  865. ext4_unlock_group(sb, group);
  866. /* Don't need to dirty bitmap block if we didn't change it */
  867. if (free) {
  868. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  869. err = ext4_handle_dirty_metadata(handle,
  870. NULL, block_bitmap_bh);
  871. }
  872. brelse(block_bitmap_bh);
  873. if (err)
  874. goto fail;
  875. }
  876. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  877. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  878. if (err)
  879. goto fail;
  880. percpu_counter_dec(&sbi->s_freeinodes_counter);
  881. if (S_ISDIR(mode))
  882. percpu_counter_inc(&sbi->s_dirs_counter);
  883. ext4_mark_super_dirty(sb);
  884. if (sbi->s_log_groups_per_flex) {
  885. flex_group = ext4_flex_group(sbi, group);
  886. atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
  887. }
  888. if (test_opt(sb, GRPID)) {
  889. inode->i_mode = mode;
  890. inode->i_uid = current_fsuid();
  891. inode->i_gid = dir->i_gid;
  892. } else
  893. inode_init_owner(inode, dir, mode);
  894. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  895. /* This is the optimal IO size (for stat), not the fs block size */
  896. inode->i_blocks = 0;
  897. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  898. ext4_current_time(inode);
  899. memset(ei->i_data, 0, sizeof(ei->i_data));
  900. ei->i_dir_start_lookup = 0;
  901. ei->i_disksize = 0;
  902. /*
  903. * Don't inherit extent flag from directory, amongst others. We set
  904. * extent flag on newly created directory and file only if -o extent
  905. * mount option is specified
  906. */
  907. ei->i_flags =
  908. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  909. ei->i_file_acl = 0;
  910. ei->i_dtime = 0;
  911. ei->i_block_group = group;
  912. ei->i_last_alloc_group = ~0;
  913. ext4_set_inode_flags(inode);
  914. if (IS_DIRSYNC(inode))
  915. ext4_handle_sync(handle);
  916. if (insert_inode_locked(inode) < 0) {
  917. err = -EINVAL;
  918. goto fail_drop;
  919. }
  920. spin_lock(&sbi->s_next_gen_lock);
  921. inode->i_generation = sbi->s_next_generation++;
  922. spin_unlock(&sbi->s_next_gen_lock);
  923. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  924. ext4_set_inode_state(inode, EXT4_STATE_NEW);
  925. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  926. ret = inode;
  927. dquot_initialize(inode);
  928. err = dquot_alloc_inode(inode);
  929. if (err)
  930. goto fail_drop;
  931. err = ext4_init_acl(handle, inode, dir);
  932. if (err)
  933. goto fail_free_drop;
  934. err = ext4_init_security(handle, inode, dir, qstr);
  935. if (err)
  936. goto fail_free_drop;
  937. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  938. /* set extent flag only for directory, file and normal symlink*/
  939. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  940. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  941. ext4_ext_tree_init(handle, inode);
  942. }
  943. }
  944. if (ext4_handle_valid(handle)) {
  945. ei->i_sync_tid = handle->h_transaction->t_tid;
  946. ei->i_datasync_tid = handle->h_transaction->t_tid;
  947. }
  948. err = ext4_mark_inode_dirty(handle, inode);
  949. if (err) {
  950. ext4_std_error(sb, err);
  951. goto fail_free_drop;
  952. }
  953. ext4_debug("allocating inode %lu\n", inode->i_ino);
  954. trace_ext4_allocate_inode(inode, dir, mode);
  955. goto really_out;
  956. fail:
  957. ext4_std_error(sb, err);
  958. out:
  959. iput(inode);
  960. ret = ERR_PTR(err);
  961. really_out:
  962. brelse(inode_bitmap_bh);
  963. return ret;
  964. fail_free_drop:
  965. dquot_free_inode(inode);
  966. fail_drop:
  967. dquot_drop(inode);
  968. inode->i_flags |= S_NOQUOTA;
  969. inode->i_nlink = 0;
  970. unlock_new_inode(inode);
  971. iput(inode);
  972. brelse(inode_bitmap_bh);
  973. return ERR_PTR(err);
  974. }
  975. /* Verify that we are loading a valid orphan from disk */
  976. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  977. {
  978. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  979. ext4_group_t block_group;
  980. int bit;
  981. struct buffer_head *bitmap_bh;
  982. struct inode *inode = NULL;
  983. long err = -EIO;
  984. /* Error cases - e2fsck has already cleaned up for us */
  985. if (ino > max_ino) {
  986. ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino);
  987. goto error;
  988. }
  989. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  990. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  991. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  992. if (!bitmap_bh) {
  993. ext4_warning(sb, "inode bitmap error for orphan %lu", ino);
  994. goto error;
  995. }
  996. /* Having the inode bit set should be a 100% indicator that this
  997. * is a valid orphan (no e2fsck run on fs). Orphans also include
  998. * inodes that were being truncated, so we can't check i_nlink==0.
  999. */
  1000. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  1001. goto bad_orphan;
  1002. inode = ext4_iget(sb, ino);
  1003. if (IS_ERR(inode))
  1004. goto iget_failed;
  1005. /*
  1006. * If the orphans has i_nlinks > 0 then it should be able to be
  1007. * truncated, otherwise it won't be removed from the orphan list
  1008. * during processing and an infinite loop will result.
  1009. */
  1010. if (inode->i_nlink && !ext4_can_truncate(inode))
  1011. goto bad_orphan;
  1012. if (NEXT_ORPHAN(inode) > max_ino)
  1013. goto bad_orphan;
  1014. brelse(bitmap_bh);
  1015. return inode;
  1016. iget_failed:
  1017. err = PTR_ERR(inode);
  1018. inode = NULL;
  1019. bad_orphan:
  1020. ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino);
  1021. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  1022. bit, (unsigned long long)bitmap_bh->b_blocknr,
  1023. ext4_test_bit(bit, bitmap_bh->b_data));
  1024. printk(KERN_NOTICE "inode=%p\n", inode);
  1025. if (inode) {
  1026. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  1027. is_bad_inode(inode));
  1028. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  1029. NEXT_ORPHAN(inode));
  1030. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  1031. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  1032. /* Avoid freeing blocks if we got a bad deleted inode */
  1033. if (inode->i_nlink == 0)
  1034. inode->i_blocks = 0;
  1035. iput(inode);
  1036. }
  1037. brelse(bitmap_bh);
  1038. error:
  1039. return ERR_PTR(err);
  1040. }
  1041. unsigned long ext4_count_free_inodes(struct super_block *sb)
  1042. {
  1043. unsigned long desc_count;
  1044. struct ext4_group_desc *gdp;
  1045. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1046. #ifdef EXT4FS_DEBUG
  1047. struct ext4_super_block *es;
  1048. unsigned long bitmap_count, x;
  1049. struct buffer_head *bitmap_bh = NULL;
  1050. es = EXT4_SB(sb)->s_es;
  1051. desc_count = 0;
  1052. bitmap_count = 0;
  1053. gdp = NULL;
  1054. for (i = 0; i < ngroups; i++) {
  1055. gdp = ext4_get_group_desc(sb, i, NULL);
  1056. if (!gdp)
  1057. continue;
  1058. desc_count += ext4_free_inodes_count(sb, gdp);
  1059. brelse(bitmap_bh);
  1060. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  1061. if (!bitmap_bh)
  1062. continue;
  1063. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  1064. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1065. (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
  1066. bitmap_count += x;
  1067. }
  1068. brelse(bitmap_bh);
  1069. printk(KERN_DEBUG "ext4_count_free_inodes: "
  1070. "stored = %u, computed = %lu, %lu\n",
  1071. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  1072. return desc_count;
  1073. #else
  1074. desc_count = 0;
  1075. for (i = 0; i < ngroups; i++) {
  1076. gdp = ext4_get_group_desc(sb, i, NULL);
  1077. if (!gdp)
  1078. continue;
  1079. desc_count += ext4_free_inodes_count(sb, gdp);
  1080. cond_resched();
  1081. }
  1082. return desc_count;
  1083. #endif
  1084. }
  1085. /* Called at mount-time, super-block is locked */
  1086. unsigned long ext4_count_dirs(struct super_block * sb)
  1087. {
  1088. unsigned long count = 0;
  1089. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1090. for (i = 0; i < ngroups; i++) {
  1091. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1092. if (!gdp)
  1093. continue;
  1094. count += ext4_used_dirs_count(sb, gdp);
  1095. }
  1096. return count;
  1097. }
  1098. /*
  1099. * Zeroes not yet zeroed inode table - just write zeroes through the whole
  1100. * inode table. Must be called without any spinlock held. The only place
  1101. * where it is called from on active part of filesystem is ext4lazyinit
  1102. * thread, so we do not need any special locks, however we have to prevent
  1103. * inode allocation from the current group, so we take alloc_sem lock, to
  1104. * block ext4_claim_inode until we are finished.
  1105. */
  1106. extern int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
  1107. int barrier)
  1108. {
  1109. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  1110. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1111. struct ext4_group_desc *gdp = NULL;
  1112. struct buffer_head *group_desc_bh;
  1113. handle_t *handle;
  1114. ext4_fsblk_t blk;
  1115. int num, ret = 0, used_blks = 0;
  1116. /* This should not happen, but just to be sure check this */
  1117. if (sb->s_flags & MS_RDONLY) {
  1118. ret = 1;
  1119. goto out;
  1120. }
  1121. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  1122. if (!gdp)
  1123. goto out;
  1124. /*
  1125. * We do not need to lock this, because we are the only one
  1126. * handling this flag.
  1127. */
  1128. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
  1129. goto out;
  1130. handle = ext4_journal_start_sb(sb, 1);
  1131. if (IS_ERR(handle)) {
  1132. ret = PTR_ERR(handle);
  1133. goto out;
  1134. }
  1135. down_write(&grp->alloc_sem);
  1136. /*
  1137. * If inode bitmap was already initialized there may be some
  1138. * used inodes so we need to skip blocks with used inodes in
  1139. * inode table.
  1140. */
  1141. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
  1142. used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
  1143. ext4_itable_unused_count(sb, gdp)),
  1144. sbi->s_inodes_per_block);
  1145. if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
  1146. ext4_error(sb, "Something is wrong with group %u\n"
  1147. "Used itable blocks: %d"
  1148. "itable unused count: %u\n",
  1149. group, used_blks,
  1150. ext4_itable_unused_count(sb, gdp));
  1151. ret = 1;
  1152. goto out;
  1153. }
  1154. blk = ext4_inode_table(sb, gdp) + used_blks;
  1155. num = sbi->s_itb_per_group - used_blks;
  1156. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1157. ret = ext4_journal_get_write_access(handle,
  1158. group_desc_bh);
  1159. if (ret)
  1160. goto err_out;
  1161. /*
  1162. * Skip zeroout if the inode table is full. But we set the ZEROED
  1163. * flag anyway, because obviously, when it is full it does not need
  1164. * further zeroing.
  1165. */
  1166. if (unlikely(num == 0))
  1167. goto skip_zeroout;
  1168. ext4_debug("going to zero out inode table in group %d\n",
  1169. group);
  1170. ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
  1171. if (ret < 0)
  1172. goto err_out;
  1173. if (barrier)
  1174. blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
  1175. skip_zeroout:
  1176. ext4_lock_group(sb, group);
  1177. gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1178. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  1179. ext4_unlock_group(sb, group);
  1180. BUFFER_TRACE(group_desc_bh,
  1181. "call ext4_handle_dirty_metadata");
  1182. ret = ext4_handle_dirty_metadata(handle, NULL,
  1183. group_desc_bh);
  1184. err_out:
  1185. up_write(&grp->alloc_sem);
  1186. ext4_journal_stop(handle);
  1187. out:
  1188. return ret;
  1189. }