1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/quotaops.h>
16#include <linux/sched.h>
17#include <linux/backing-dev.h>
18#include <linux/buffer_head.h>
19#include <linux/random.h>
20#include "ext2.h"
21#include "xattr.h"
22#include "acl.h"
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45static struct buffer_head *
46read_inode_bitmap(struct super_block * sb, unsigned long block_group)
47{
48 struct ext2_group_desc *desc;
49 struct buffer_head *bh = NULL;
50
51 desc = ext2_get_group_desc(sb, block_group, NULL);
52 if (!desc)
53 goto error_out;
54
55 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
56 if (!bh)
57 ext2_error(sb, "read_inode_bitmap",
58 "Cannot read inode bitmap - "
59 "block_group = %lu, inode_bitmap = %u",
60 block_group, le32_to_cpu(desc->bg_inode_bitmap));
61error_out:
62 return bh;
63}
64
65static void ext2_release_inode(struct super_block *sb, int group, int dir)
66{
67 struct ext2_group_desc * desc;
68 struct buffer_head *bh;
69
70 desc = ext2_get_group_desc(sb, group, &bh);
71 if (!desc) {
72 ext2_error(sb, "ext2_release_inode",
73 "can't get descriptor for group %d", group);
74 return;
75 }
76
77 spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
78 le16_add_cpu(&desc->bg_free_inodes_count, 1);
79 if (dir)
80 le16_add_cpu(&desc->bg_used_dirs_count, -1);
81 spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
82 if (dir)
83 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
84 sb->s_dirt = 1;
85 mark_buffer_dirty(bh);
86}
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104void ext2_free_inode (struct inode * inode)
105{
106 struct super_block * sb = inode->i_sb;
107 int is_directory;
108 unsigned long ino;
109 struct buffer_head *bitmap_bh;
110 unsigned long block_group;
111 unsigned long bit;
112 struct ext2_super_block * es;
113
114 ino = inode->i_ino;
115 ext2_debug ("freeing inode %lu\n", ino);
116
117
118
119
120
121
122 ext2_xattr_delete_inode(inode);
123 dquot_free_inode(inode);
124 dquot_drop(inode);
125
126 es = EXT2_SB(sb)->s_es;
127 is_directory = S_ISDIR(inode->i_mode);
128
129 if (ino < EXT2_FIRST_INO(sb) ||
130 ino > le32_to_cpu(es->s_inodes_count)) {
131 ext2_error (sb, "ext2_free_inode",
132 "reserved or nonexistent inode %lu", ino);
133 return;
134 }
135 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
136 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
137 bitmap_bh = read_inode_bitmap(sb, block_group);
138 if (!bitmap_bh)
139 return;
140
141
142 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
143 bit, (void *) bitmap_bh->b_data))
144 ext2_error (sb, "ext2_free_inode",
145 "bit already cleared for inode %lu", ino);
146 else
147 ext2_release_inode(sb, block_group, is_directory);
148 mark_buffer_dirty(bitmap_bh);
149 if (sb->s_flags & MS_SYNCHRONOUS)
150 sync_dirty_buffer(bitmap_bh);
151
152 brelse(bitmap_bh);
153}
154
155
156
157
158
159
160
161
162
163
164
165
166
167static void ext2_preread_inode(struct inode *inode)
168{
169 unsigned long block_group;
170 unsigned long offset;
171 unsigned long block;
172 struct ext2_group_desc * gdp;
173 struct backing_dev_info *bdi;
174
175 bdi = inode->i_mapping->backing_dev_info;
176 if (bdi_read_congested(bdi))
177 return;
178 if (bdi_write_congested(bdi))
179 return;
180
181 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
182 gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL);
183 if (gdp == NULL)
184 return;
185
186
187
188
189 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
190 EXT2_INODE_SIZE(inode->i_sb);
191 block = le32_to_cpu(gdp->bg_inode_table) +
192 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
193 sb_breadahead(inode->i_sb, block);
194}
195
196
197
198
199
200
201
202
203
204
205
206static int find_group_dir(struct super_block *sb, struct inode *parent)
207{
208 int ngroups = EXT2_SB(sb)->s_groups_count;
209 int avefreei = ext2_count_free_inodes(sb) / ngroups;
210 struct ext2_group_desc *desc, *best_desc = NULL;
211 int group, best_group = -1;
212
213 for (group = 0; group < ngroups; group++) {
214 desc = ext2_get_group_desc (sb, group, NULL);
215 if (!desc || !desc->bg_free_inodes_count)
216 continue;
217 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
218 continue;
219 if (!best_desc ||
220 (le16_to_cpu(desc->bg_free_blocks_count) >
221 le16_to_cpu(best_desc->bg_free_blocks_count))) {
222 best_group = group;
223 best_desc = desc;
224 }
225 }
226 if (!best_desc)
227 return -1;
228
229 return best_group;
230}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257#define INODE_COST 64
258#define BLOCK_COST 256
259
260static int find_group_orlov(struct super_block *sb, struct inode *parent)
261{
262 int parent_group = EXT2_I(parent)->i_block_group;
263 struct ext2_sb_info *sbi = EXT2_SB(sb);
264 struct ext2_super_block *es = sbi->s_es;
265 int ngroups = sbi->s_groups_count;
266 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
267 int freei;
268 int avefreei;
269 int free_blocks;
270 int avefreeb;
271 int blocks_per_dir;
272 int ndirs;
273 int max_debt, max_dirs, min_blocks, min_inodes;
274 int group = -1, i;
275 struct ext2_group_desc *desc;
276
277 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
278 avefreei = freei / ngroups;
279 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
280 avefreeb = free_blocks / ngroups;
281 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
282
283 if ((parent == sb->s_root->d_inode) ||
284 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
285 struct ext2_group_desc *best_desc = NULL;
286 int best_ndir = inodes_per_group;
287 int best_group = -1;
288
289 get_random_bytes(&group, sizeof(group));
290 parent_group = (unsigned)group % ngroups;
291 for (i = 0; i < ngroups; i++) {
292 group = (parent_group + i) % ngroups;
293 desc = ext2_get_group_desc (sb, group, NULL);
294 if (!desc || !desc->bg_free_inodes_count)
295 continue;
296 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
297 continue;
298 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
299 continue;
300 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
301 continue;
302 best_group = group;
303 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
304 best_desc = desc;
305 }
306 if (best_group >= 0) {
307 desc = best_desc;
308 group = best_group;
309 goto found;
310 }
311 goto fallback;
312 }
313
314 if (ndirs == 0)
315 ndirs = 1;
316
317 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
318
319 max_dirs = ndirs / ngroups + inodes_per_group / 16;
320 min_inodes = avefreei - inodes_per_group / 4;
321 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
322
323 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
324 if (max_debt * INODE_COST > inodes_per_group)
325 max_debt = inodes_per_group / INODE_COST;
326 if (max_debt > 255)
327 max_debt = 255;
328 if (max_debt == 0)
329 max_debt = 1;
330
331 for (i = 0; i < ngroups; i++) {
332 group = (parent_group + i) % ngroups;
333 desc = ext2_get_group_desc (sb, group, NULL);
334 if (!desc || !desc->bg_free_inodes_count)
335 continue;
336 if (sbi->s_debts[group] >= max_debt)
337 continue;
338 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
339 continue;
340 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
341 continue;
342 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
343 continue;
344 goto found;
345 }
346
347fallback:
348 for (i = 0; i < ngroups; i++) {
349 group = (parent_group + i) % ngroups;
350 desc = ext2_get_group_desc (sb, group, NULL);
351 if (!desc || !desc->bg_free_inodes_count)
352 continue;
353 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
354 goto found;
355 }
356
357 if (avefreei) {
358
359
360
361
362 avefreei = 0;
363 goto fallback;
364 }
365
366 return -1;
367
368found:
369 return group;
370}
371
372static int find_group_other(struct super_block *sb, struct inode *parent)
373{
374 int parent_group = EXT2_I(parent)->i_block_group;
375 int ngroups = EXT2_SB(sb)->s_groups_count;
376 struct ext2_group_desc *desc;
377 int group, i;
378
379
380
381
382 group = parent_group;
383 desc = ext2_get_group_desc (sb, group, NULL);
384 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
385 le16_to_cpu(desc->bg_free_blocks_count))
386 goto found;
387
388
389
390
391
392
393
394
395
396
397 group = (group + parent->i_ino) % ngroups;
398
399
400
401
402
403 for (i = 1; i < ngroups; i <<= 1) {
404 group += i;
405 if (group >= ngroups)
406 group -= ngroups;
407 desc = ext2_get_group_desc (sb, group, NULL);
408 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
409 le16_to_cpu(desc->bg_free_blocks_count))
410 goto found;
411 }
412
413
414
415
416
417 group = parent_group;
418 for (i = 0; i < ngroups; i++) {
419 if (++group >= ngroups)
420 group = 0;
421 desc = ext2_get_group_desc (sb, group, NULL);
422 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
423 goto found;
424 }
425
426 return -1;
427
428found:
429 return group;
430}
431
432struct inode *ext2_new_inode(struct inode *dir, int mode)
433{
434 struct super_block *sb;
435 struct buffer_head *bitmap_bh = NULL;
436 struct buffer_head *bh2;
437 int group, i;
438 ino_t ino = 0;
439 struct inode * inode;
440 struct ext2_group_desc *gdp;
441 struct ext2_super_block *es;
442 struct ext2_inode_info *ei;
443 struct ext2_sb_info *sbi;
444 int err;
445
446 sb = dir->i_sb;
447 inode = new_inode(sb);
448 if (!inode)
449 return ERR_PTR(-ENOMEM);
450
451 ei = EXT2_I(inode);
452 sbi = EXT2_SB(sb);
453 es = sbi->s_es;
454 if (S_ISDIR(mode)) {
455 if (test_opt(sb, OLDALLOC))
456 group = find_group_dir(sb, dir);
457 else
458 group = find_group_orlov(sb, dir);
459 } else
460 group = find_group_other(sb, dir);
461
462 if (group == -1) {
463 err = -ENOSPC;
464 goto fail;
465 }
466
467 for (i = 0; i < sbi->s_groups_count; i++) {
468 gdp = ext2_get_group_desc(sb, group, &bh2);
469 brelse(bitmap_bh);
470 bitmap_bh = read_inode_bitmap(sb, group);
471 if (!bitmap_bh) {
472 err = -EIO;
473 goto fail;
474 }
475 ino = 0;
476
477repeat_in_this_group:
478 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
479 EXT2_INODES_PER_GROUP(sb), ino);
480 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
481
482
483
484
485
486
487
488
489 if (++group == sbi->s_groups_count)
490 group = 0;
491 continue;
492 }
493 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
494 ino, bitmap_bh->b_data)) {
495
496 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
497
498 if (++group == sbi->s_groups_count)
499 group = 0;
500 continue;
501 }
502
503 goto repeat_in_this_group;
504 }
505 goto got;
506 }
507
508
509
510
511 err = -ENOSPC;
512 goto fail;
513got:
514 mark_buffer_dirty(bitmap_bh);
515 if (sb->s_flags & MS_SYNCHRONOUS)
516 sync_dirty_buffer(bitmap_bh);
517 brelse(bitmap_bh);
518
519 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
520 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
521 ext2_error (sb, "ext2_new_inode",
522 "reserved inode or inode > inodes count - "
523 "block_group = %d,inode=%lu", group,
524 (unsigned long) ino);
525 err = -EIO;
526 goto fail;
527 }
528
529 percpu_counter_add(&sbi->s_freeinodes_counter, -1);
530 if (S_ISDIR(mode))
531 percpu_counter_inc(&sbi->s_dirs_counter);
532
533 spin_lock(sb_bgl_lock(sbi, group));
534 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
535 if (S_ISDIR(mode)) {
536 if (sbi->s_debts[group] < 255)
537 sbi->s_debts[group]++;
538 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
539 } else {
540 if (sbi->s_debts[group])
541 sbi->s_debts[group]--;
542 }
543 spin_unlock(sb_bgl_lock(sbi, group));
544
545 sb->s_dirt = 1;
546 mark_buffer_dirty(bh2);
547 if (test_opt(sb, GRPID)) {
548 inode->i_mode = mode;
549 inode->i_uid = current_fsuid();
550 inode->i_gid = dir->i_gid;
551 } else
552 inode_init_owner(inode, dir, mode);
553
554 inode->i_ino = ino;
555 inode->i_blocks = 0;
556 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
557 memset(ei->i_data, 0, sizeof(ei->i_data));
558 ei->i_flags =
559 ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
560 ei->i_faddr = 0;
561 ei->i_frag_no = 0;
562 ei->i_frag_size = 0;
563 ei->i_file_acl = 0;
564 ei->i_dir_acl = 0;
565 ei->i_dtime = 0;
566 ei->i_block_alloc_info = NULL;
567 ei->i_block_group = group;
568 ei->i_dir_start_lookup = 0;
569 ei->i_state = EXT2_STATE_NEW;
570 ext2_set_inode_flags(inode);
571 spin_lock(&sbi->s_next_gen_lock);
572 inode->i_generation = sbi->s_next_generation++;
573 spin_unlock(&sbi->s_next_gen_lock);
574 if (insert_inode_locked(inode) < 0) {
575 err = -EINVAL;
576 goto fail_drop;
577 }
578
579 dquot_initialize(inode);
580 err = dquot_alloc_inode(inode);
581 if (err)
582 goto fail_drop;
583
584 err = ext2_init_acl(inode, dir);
585 if (err)
586 goto fail_free_drop;
587
588 err = ext2_init_security(inode,dir);
589 if (err)
590 goto fail_free_drop;
591
592 mark_inode_dirty(inode);
593 ext2_debug("allocating inode %lu\n", inode->i_ino);
594 ext2_preread_inode(inode);
595 return inode;
596
597fail_free_drop:
598 dquot_free_inode(inode);
599
600fail_drop:
601 dquot_drop(inode);
602 inode->i_flags |= S_NOQUOTA;
603 inode->i_nlink = 0;
604 unlock_new_inode(inode);
605 iput(inode);
606 return ERR_PTR(err);
607
608fail:
609 make_bad_inode(inode);
610 iput(inode);
611 return ERR_PTR(err);
612}
613
614unsigned long ext2_count_free_inodes (struct super_block * sb)
615{
616 struct ext2_group_desc *desc;
617 unsigned long desc_count = 0;
618 int i;
619
620#ifdef EXT2FS_DEBUG
621 struct ext2_super_block *es;
622 unsigned long bitmap_count = 0;
623 struct buffer_head *bitmap_bh = NULL;
624
625 es = EXT2_SB(sb)->s_es;
626 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
627 unsigned x;
628
629 desc = ext2_get_group_desc (sb, i, NULL);
630 if (!desc)
631 continue;
632 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
633 brelse(bitmap_bh);
634 bitmap_bh = read_inode_bitmap(sb, i);
635 if (!bitmap_bh)
636 continue;
637
638 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
639 printk("group %d: stored = %d, counted = %u\n",
640 i, le16_to_cpu(desc->bg_free_inodes_count), x);
641 bitmap_count += x;
642 }
643 brelse(bitmap_bh);
644 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
645 percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
646 desc_count, bitmap_count);
647 return desc_count;
648#else
649 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
650 desc = ext2_get_group_desc (sb, i, NULL);
651 if (!desc)
652 continue;
653 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
654 }
655 return desc_count;
656#endif
657}
658
659
660unsigned long ext2_count_dirs (struct super_block * sb)
661{
662 unsigned long count = 0;
663 int i;
664
665 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
666 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
667 if (!gdp)
668 continue;
669 count += le16_to_cpu(gdp->bg_used_dirs_count);
670 }
671 return count;
672}
673
674