1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/time.h>
17#include <linux/fs.h>
18#include <linux/stat.h>
19#include <linux/string.h>
20#include <linux/quotaops.h>
21#include <linux/buffer_head.h>
22#include <linux/random.h>
23#include <linux/bitops.h>
24#include <linux/blkdev.h>
25#include <linux/cred.h>
26
27#include <asm/byteorder.h>
28
29#include "ext4.h"
30#include "ext4_jbd2.h"
31#include "xattr.h"
32#include "acl.h"
33
34#include <trace/events/ext4.h>
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
56{
57 int i;
58
59 if (start_bit >= end_bit)
60 return;
61
62 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
63 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
64 ext4_set_bit(i, bitmap);
65 if (i < end_bit)
66 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
67}
68
69void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
70{
71 if (uptodate) {
72 set_buffer_uptodate(bh);
73 set_bitmap_uptodate(bh);
74 }
75 unlock_buffer(bh);
76 put_bh(bh);
77}
78
79static int ext4_validate_inode_bitmap(struct super_block *sb,
80 struct ext4_group_desc *desc,
81 ext4_group_t block_group,
82 struct buffer_head *bh)
83{
84 ext4_fsblk_t blk;
85 struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
86
87 if (buffer_verified(bh))
88 return 0;
89 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
90 return -EFSCORRUPTED;
91
92 ext4_lock_group(sb, block_group);
93 if (buffer_verified(bh))
94 goto verified;
95 blk = ext4_inode_bitmap(sb, desc);
96 if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
97 EXT4_INODES_PER_GROUP(sb) / 8)) {
98 ext4_unlock_group(sb, block_group);
99 ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
100 "inode_bitmap = %llu", block_group, blk);
101 ext4_mark_group_bitmap_corrupted(sb, block_group,
102 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
103 return -EFSBADCRC;
104 }
105 set_buffer_verified(bh);
106verified:
107 ext4_unlock_group(sb, block_group);
108 return 0;
109}
110
111
112
113
114
115
116
117static struct buffer_head *
118ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
119{
120 struct ext4_group_desc *desc;
121 struct ext4_sb_info *sbi = EXT4_SB(sb);
122 struct buffer_head *bh = NULL;
123 ext4_fsblk_t bitmap_blk;
124 int err;
125
126 desc = ext4_get_group_desc(sb, block_group, NULL);
127 if (!desc)
128 return ERR_PTR(-EFSCORRUPTED);
129
130 bitmap_blk = ext4_inode_bitmap(sb, desc);
131 if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
132 (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
133 ext4_error(sb, "Invalid inode bitmap blk %llu in "
134 "block_group %u", bitmap_blk, block_group);
135 ext4_mark_group_bitmap_corrupted(sb, block_group,
136 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
137 return ERR_PTR(-EFSCORRUPTED);
138 }
139 bh = sb_getblk(sb, bitmap_blk);
140 if (unlikely(!bh)) {
141 ext4_warning(sb, "Cannot read inode bitmap - "
142 "block_group = %u, inode_bitmap = %llu",
143 block_group, bitmap_blk);
144 return ERR_PTR(-ENOMEM);
145 }
146 if (bitmap_uptodate(bh))
147 goto verify;
148
149 lock_buffer(bh);
150 if (bitmap_uptodate(bh)) {
151 unlock_buffer(bh);
152 goto verify;
153 }
154
155 ext4_lock_group(sb, block_group);
156 if (ext4_has_group_desc_csum(sb) &&
157 (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
158 if (block_group == 0) {
159 ext4_unlock_group(sb, block_group);
160 unlock_buffer(bh);
161 ext4_error(sb, "Inode bitmap for bg 0 marked "
162 "uninitialized");
163 err = -EFSCORRUPTED;
164 goto out;
165 }
166 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
167 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
168 sb->s_blocksize * 8, bh->b_data);
169 set_bitmap_uptodate(bh);
170 set_buffer_uptodate(bh);
171 set_buffer_verified(bh);
172 ext4_unlock_group(sb, block_group);
173 unlock_buffer(bh);
174 return bh;
175 }
176 ext4_unlock_group(sb, block_group);
177
178 if (buffer_uptodate(bh)) {
179
180
181
182
183 set_bitmap_uptodate(bh);
184 unlock_buffer(bh);
185 goto verify;
186 }
187
188
189
190 trace_ext4_load_inode_bitmap(sb, block_group);
191 bh->b_end_io = ext4_end_bitmap_read;
192 get_bh(bh);
193 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
194 wait_on_buffer(bh);
195 if (!buffer_uptodate(bh)) {
196 put_bh(bh);
197 ext4_error_err(sb, EIO, "Cannot read inode bitmap - "
198 "block_group = %u, inode_bitmap = %llu",
199 block_group, bitmap_blk);
200 ext4_mark_group_bitmap_corrupted(sb, block_group,
201 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
202 return ERR_PTR(-EIO);
203 }
204
205verify:
206 err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
207 if (err)
208 goto out;
209 return bh;
210out:
211 put_bh(bh);
212 return ERR_PTR(err);
213}
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231void ext4_free_inode(handle_t *handle, struct inode *inode)
232{
233 struct super_block *sb = inode->i_sb;
234 int is_directory;
235 unsigned long ino;
236 struct buffer_head *bitmap_bh = NULL;
237 struct buffer_head *bh2;
238 ext4_group_t block_group;
239 unsigned long bit;
240 struct ext4_group_desc *gdp;
241 struct ext4_super_block *es;
242 struct ext4_sb_info *sbi;
243 int fatal = 0, err, count, cleared;
244 struct ext4_group_info *grp;
245
246 if (!sb) {
247 printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
248 "nonexistent device\n", __func__, __LINE__);
249 return;
250 }
251 if (atomic_read(&inode->i_count) > 1) {
252 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
253 __func__, __LINE__, inode->i_ino,
254 atomic_read(&inode->i_count));
255 return;
256 }
257 if (inode->i_nlink) {
258 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
259 __func__, __LINE__, inode->i_ino, inode->i_nlink);
260 return;
261 }
262 sbi = EXT4_SB(sb);
263
264 ino = inode->i_ino;
265 ext4_debug("freeing inode %lu\n", ino);
266 trace_ext4_free_inode(inode);
267
268
269
270
271
272 dquot_initialize(inode);
273 dquot_free_inode(inode);
274 dquot_drop(inode);
275
276 is_directory = S_ISDIR(inode->i_mode);
277
278
279 ext4_clear_inode(inode);
280
281 es = sbi->s_es;
282 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
283 ext4_error(sb, "reserved or nonexistent inode %lu", ino);
284 goto error_return;
285 }
286 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
287 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
288 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
289
290 grp = ext4_get_group_info(sb, block_group);
291 if (IS_ERR(bitmap_bh)) {
292 fatal = PTR_ERR(bitmap_bh);
293 bitmap_bh = NULL;
294 goto error_return;
295 }
296 if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
297 fatal = -EFSCORRUPTED;
298 goto error_return;
299 }
300
301 BUFFER_TRACE(bitmap_bh, "get_write_access");
302 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
303 if (fatal)
304 goto error_return;
305
306 fatal = -ESRCH;
307 gdp = ext4_get_group_desc(sb, block_group, &bh2);
308 if (gdp) {
309 BUFFER_TRACE(bh2, "get_write_access");
310 fatal = ext4_journal_get_write_access(handle, bh2);
311 }
312 ext4_lock_group(sb, block_group);
313 cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
314 if (fatal || !cleared) {
315 ext4_unlock_group(sb, block_group);
316 goto out;
317 }
318
319 count = ext4_free_inodes_count(sb, gdp) + 1;
320 ext4_free_inodes_set(sb, gdp, count);
321 if (is_directory) {
322 count = ext4_used_dirs_count(sb, gdp) - 1;
323 ext4_used_dirs_set(sb, gdp, count);
324 percpu_counter_dec(&sbi->s_dirs_counter);
325 }
326 ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
327 EXT4_INODES_PER_GROUP(sb) / 8);
328 ext4_group_desc_csum_set(sb, block_group, gdp);
329 ext4_unlock_group(sb, block_group);
330
331 percpu_counter_inc(&sbi->s_freeinodes_counter);
332 if (sbi->s_log_groups_per_flex) {
333 struct flex_groups *fg;
334
335 fg = sbi_array_rcu_deref(sbi, s_flex_groups,
336 ext4_flex_group(sbi, block_group));
337 atomic_inc(&fg->free_inodes);
338 if (is_directory)
339 atomic_dec(&fg->used_dirs);
340 }
341 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
342 fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
343out:
344 if (cleared) {
345 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
346 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
347 if (!fatal)
348 fatal = err;
349 } else {
350 ext4_error(sb, "bit already cleared for inode %lu", ino);
351 ext4_mark_group_bitmap_corrupted(sb, block_group,
352 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
353 }
354
355error_return:
356 brelse(bitmap_bh);
357 ext4_std_error(sb, fatal);
358}
359
360struct orlov_stats {
361 __u64 free_clusters;
362 __u32 free_inodes;
363 __u32 used_dirs;
364};
365
366
367
368
369
370
371static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
372 int flex_size, struct orlov_stats *stats)
373{
374 struct ext4_group_desc *desc;
375
376 if (flex_size > 1) {
377 struct flex_groups *fg = sbi_array_rcu_deref(EXT4_SB(sb),
378 s_flex_groups, g);
379 stats->free_inodes = atomic_read(&fg->free_inodes);
380 stats->free_clusters = atomic64_read(&fg->free_clusters);
381 stats->used_dirs = atomic_read(&fg->used_dirs);
382 return;
383 }
384
385 desc = ext4_get_group_desc(sb, g, NULL);
386 if (desc) {
387 stats->free_inodes = ext4_free_inodes_count(sb, desc);
388 stats->free_clusters = ext4_free_group_clusters(sb, desc);
389 stats->used_dirs = ext4_used_dirs_count(sb, desc);
390 } else {
391 stats->free_inodes = 0;
392 stats->free_clusters = 0;
393 stats->used_dirs = 0;
394 }
395}
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418static int find_group_orlov(struct super_block *sb, struct inode *parent,
419 ext4_group_t *group, umode_t mode,
420 const struct qstr *qstr)
421{
422 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
423 struct ext4_sb_info *sbi = EXT4_SB(sb);
424 ext4_group_t real_ngroups = ext4_get_groups_count(sb);
425 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
426 unsigned int freei, avefreei, grp_free;
427 ext4_fsblk_t freeb, avefreec;
428 unsigned int ndirs;
429 int max_dirs, min_inodes;
430 ext4_grpblk_t min_clusters;
431 ext4_group_t i, grp, g, ngroups;
432 struct ext4_group_desc *desc;
433 struct orlov_stats stats;
434 int flex_size = ext4_flex_bg_size(sbi);
435 struct dx_hash_info hinfo;
436
437 ngroups = real_ngroups;
438 if (flex_size > 1) {
439 ngroups = (real_ngroups + flex_size - 1) >>
440 sbi->s_log_groups_per_flex;
441 parent_group >>= sbi->s_log_groups_per_flex;
442 }
443
444 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
445 avefreei = freei / ngroups;
446 freeb = EXT4_C2B(sbi,
447 percpu_counter_read_positive(&sbi->s_freeclusters_counter));
448 avefreec = freeb;
449 do_div(avefreec, ngroups);
450 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
451
452 if (S_ISDIR(mode) &&
453 ((parent == d_inode(sb->s_root)) ||
454 (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
455 int best_ndir = inodes_per_group;
456 int ret = -1;
457
458 if (qstr) {
459 hinfo.hash_version = DX_HASH_HALF_MD4;
460 hinfo.seed = sbi->s_hash_seed;
461 ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
462 grp = hinfo.hash;
463 } else
464 grp = prandom_u32();
465 parent_group = (unsigned)grp % ngroups;
466 for (i = 0; i < ngroups; i++) {
467 g = (parent_group + i) % ngroups;
468 get_orlov_stats(sb, g, flex_size, &stats);
469 if (!stats.free_inodes)
470 continue;
471 if (stats.used_dirs >= best_ndir)
472 continue;
473 if (stats.free_inodes < avefreei)
474 continue;
475 if (stats.free_clusters < avefreec)
476 continue;
477 grp = g;
478 ret = 0;
479 best_ndir = stats.used_dirs;
480 }
481 if (ret)
482 goto fallback;
483 found_flex_bg:
484 if (flex_size == 1) {
485 *group = grp;
486 return 0;
487 }
488
489
490
491
492
493
494
495
496 grp *= flex_size;
497 for (i = 0; i < flex_size; i++) {
498 if (grp+i >= real_ngroups)
499 break;
500 desc = ext4_get_group_desc(sb, grp+i, NULL);
501 if (desc && ext4_free_inodes_count(sb, desc)) {
502 *group = grp+i;
503 return 0;
504 }
505 }
506 goto fallback;
507 }
508
509 max_dirs = ndirs / ngroups + inodes_per_group / 16;
510 min_inodes = avefreei - inodes_per_group*flex_size / 4;
511 if (min_inodes < 1)
512 min_inodes = 1;
513 min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
514
515
516
517
518
519 if (EXT4_I(parent)->i_last_alloc_group != ~0) {
520 parent_group = EXT4_I(parent)->i_last_alloc_group;
521 if (flex_size > 1)
522 parent_group >>= sbi->s_log_groups_per_flex;
523 }
524
525 for (i = 0; i < ngroups; i++) {
526 grp = (parent_group + i) % ngroups;
527 get_orlov_stats(sb, grp, flex_size, &stats);
528 if (stats.used_dirs >= max_dirs)
529 continue;
530 if (stats.free_inodes < min_inodes)
531 continue;
532 if (stats.free_clusters < min_clusters)
533 continue;
534 goto found_flex_bg;
535 }
536
537fallback:
538 ngroups = real_ngroups;
539 avefreei = freei / ngroups;
540fallback_retry:
541 parent_group = EXT4_I(parent)->i_block_group;
542 for (i = 0; i < ngroups; i++) {
543 grp = (parent_group + i) % ngroups;
544 desc = ext4_get_group_desc(sb, grp, NULL);
545 if (desc) {
546 grp_free = ext4_free_inodes_count(sb, desc);
547 if (grp_free && grp_free >= avefreei) {
548 *group = grp;
549 return 0;
550 }
551 }
552 }
553
554 if (avefreei) {
555
556
557
558
559 avefreei = 0;
560 goto fallback_retry;
561 }
562
563 return -1;
564}
565
566static int find_group_other(struct super_block *sb, struct inode *parent,
567 ext4_group_t *group, umode_t mode)
568{
569 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
570 ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
571 struct ext4_group_desc *desc;
572 int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
573
574
575
576
577
578
579
580
581 if (flex_size > 1) {
582 int retry = 0;
583
584 try_again:
585 parent_group &= ~(flex_size-1);
586 last = parent_group + flex_size;
587 if (last > ngroups)
588 last = ngroups;
589 for (i = parent_group; i < last; i++) {
590 desc = ext4_get_group_desc(sb, i, NULL);
591 if (desc && ext4_free_inodes_count(sb, desc)) {
592 *group = i;
593 return 0;
594 }
595 }
596 if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
597 retry = 1;
598 parent_group = EXT4_I(parent)->i_last_alloc_group;
599 goto try_again;
600 }
601
602
603
604
605
606 *group = parent_group + flex_size;
607 if (*group > ngroups)
608 *group = 0;
609 return find_group_orlov(sb, parent, group, mode, NULL);
610 }
611
612
613
614
615 *group = parent_group;
616 desc = ext4_get_group_desc(sb, *group, NULL);
617 if (desc && ext4_free_inodes_count(sb, desc) &&
618 ext4_free_group_clusters(sb, desc))
619 return 0;
620
621
622
623
624
625
626
627
628
629
630 *group = (*group + parent->i_ino) % ngroups;
631
632
633
634
635
636 for (i = 1; i < ngroups; i <<= 1) {
637 *group += i;
638 if (*group >= ngroups)
639 *group -= ngroups;
640 desc = ext4_get_group_desc(sb, *group, NULL);
641 if (desc && ext4_free_inodes_count(sb, desc) &&
642 ext4_free_group_clusters(sb, desc))
643 return 0;
644 }
645
646
647
648
649
650 *group = parent_group;
651 for (i = 0; i < ngroups; i++) {
652 if (++*group >= ngroups)
653 *group = 0;
654 desc = ext4_get_group_desc(sb, *group, NULL);
655 if (desc && ext4_free_inodes_count(sb, desc))
656 return 0;
657 }
658
659 return -1;
660}
661
662
663
664
665
666
667
668#define RECENTCY_MIN 5
669#define RECENTCY_DIRTY 300
670
671static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
672{
673 struct ext4_group_desc *gdp;
674 struct ext4_inode *raw_inode;
675 struct buffer_head *bh;
676 int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
677 int offset, ret = 0;
678 int recentcy = RECENTCY_MIN;
679 u32 dtime, now;
680
681 gdp = ext4_get_group_desc(sb, group, NULL);
682 if (unlikely(!gdp))
683 return 0;
684
685 bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
686 (ino / inodes_per_block));
687 if (!bh || !buffer_uptodate(bh))
688
689
690
691
692 goto out;
693
694 offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
695 raw_inode = (struct ext4_inode *) (bh->b_data + offset);
696
697
698
699
700
701 dtime = le32_to_cpu(raw_inode->i_dtime);
702 now = ktime_get_real_seconds();
703 if (buffer_dirty(bh))
704 recentcy += RECENTCY_DIRTY;
705
706 if (dtime && time_before32(dtime, now) &&
707 time_before32(now, dtime + recentcy))
708 ret = 1;
709out:
710 brelse(bh);
711 return ret;
712}
713
714static int find_inode_bit(struct super_block *sb, ext4_group_t group,
715 struct buffer_head *bitmap, unsigned long *ino)
716{
717 bool check_recently_deleted = EXT4_SB(sb)->s_journal == NULL;
718 unsigned long recently_deleted_ino = EXT4_INODES_PER_GROUP(sb);
719
720next:
721 *ino = ext4_find_next_zero_bit((unsigned long *)
722 bitmap->b_data,
723 EXT4_INODES_PER_GROUP(sb), *ino);
724 if (*ino >= EXT4_INODES_PER_GROUP(sb))
725 goto not_found;
726
727 if (check_recently_deleted && recently_deleted(sb, group, *ino)) {
728 recently_deleted_ino = *ino;
729 *ino = *ino + 1;
730 if (*ino < EXT4_INODES_PER_GROUP(sb))
731 goto next;
732 goto not_found;
733 }
734 return 1;
735not_found:
736 if (recently_deleted_ino >= EXT4_INODES_PER_GROUP(sb))
737 return 0;
738
739
740
741
742
743
744 *ino = recently_deleted_ino;
745 return 1;
746}
747
748
749
750
751
752
753
754
755
756
757
758struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
759 umode_t mode, const struct qstr *qstr,
760 __u32 goal, uid_t *owner, __u32 i_flags,
761 int handle_type, unsigned int line_no,
762 int nblocks)
763{
764 struct super_block *sb;
765 struct buffer_head *inode_bitmap_bh = NULL;
766 struct buffer_head *group_desc_bh;
767 ext4_group_t ngroups, group = 0;
768 unsigned long ino = 0;
769 struct inode *inode;
770 struct ext4_group_desc *gdp = NULL;
771 struct ext4_inode_info *ei;
772 struct ext4_sb_info *sbi;
773 int ret2, err;
774 struct inode *ret;
775 ext4_group_t i;
776 ext4_group_t flex_group;
777 struct ext4_group_info *grp;
778 int encrypt = 0;
779
780
781 if (!dir || !dir->i_nlink)
782 return ERR_PTR(-EPERM);
783
784 sb = dir->i_sb;
785 sbi = EXT4_SB(sb);
786
787 if (unlikely(ext4_forced_shutdown(sbi)))
788 return ERR_PTR(-EIO);
789
790 if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
791 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
792 !(i_flags & EXT4_EA_INODE_FL)) {
793 err = fscrypt_get_encryption_info(dir);
794 if (err)
795 return ERR_PTR(err);
796 if (!fscrypt_has_encryption_key(dir))
797 return ERR_PTR(-ENOKEY);
798 encrypt = 1;
799 }
800
801 if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
802#ifdef CONFIG_EXT4_FS_POSIX_ACL
803 struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
804
805 if (IS_ERR(p))
806 return ERR_CAST(p);
807 if (p) {
808 int acl_size = p->a_count * sizeof(ext4_acl_entry);
809
810 nblocks += (S_ISDIR(mode) ? 2 : 1) *
811 __ext4_xattr_set_credits(sb, NULL ,
812 NULL , acl_size,
813 true );
814 posix_acl_release(p);
815 }
816#endif
817
818#ifdef CONFIG_SECURITY
819 {
820 int num_security_xattrs = 1;
821
822#ifdef CONFIG_INTEGRITY
823 num_security_xattrs++;
824#endif
825
826
827
828
829
830 nblocks += num_security_xattrs *
831 __ext4_xattr_set_credits(sb, NULL ,
832 NULL , 1024,
833 true );
834 }
835#endif
836 if (encrypt)
837 nblocks += __ext4_xattr_set_credits(sb,
838 NULL , NULL ,
839 FSCRYPT_SET_CONTEXT_MAX_SIZE,
840 true );
841 }
842
843 ngroups = ext4_get_groups_count(sb);
844 trace_ext4_request_inode(dir, mode);
845 inode = new_inode(sb);
846 if (!inode)
847 return ERR_PTR(-ENOMEM);
848 ei = EXT4_I(inode);
849
850
851
852
853
854
855 if (owner) {
856 inode->i_mode = mode;
857 i_uid_write(inode, owner[0]);
858 i_gid_write(inode, owner[1]);
859 } else if (test_opt(sb, GRPID)) {
860 inode->i_mode = mode;
861 inode->i_uid = current_fsuid();
862 inode->i_gid = dir->i_gid;
863 } else
864 inode_init_owner(inode, dir, mode);
865
866 if (ext4_has_feature_project(sb) &&
867 ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
868 ei->i_projid = EXT4_I(dir)->i_projid;
869 else
870 ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
871
872 err = dquot_initialize(inode);
873 if (err)
874 goto out;
875
876 if (!goal)
877 goal = sbi->s_inode_goal;
878
879 if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
880 group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
881 ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
882 ret2 = 0;
883 goto got_group;
884 }
885
886 if (S_ISDIR(mode))
887 ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
888 else
889 ret2 = find_group_other(sb, dir, &group, mode);
890
891got_group:
892 EXT4_I(dir)->i_last_alloc_group = group;
893 err = -ENOSPC;
894 if (ret2 == -1)
895 goto out;
896
897
898
899
900
901
902 for (i = 0; i < ngroups; i++, ino = 0) {
903 err = -EIO;
904
905 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
906 if (!gdp)
907 goto out;
908
909
910
911
912 if (ext4_free_inodes_count(sb, gdp) == 0)
913 goto next_group;
914
915 grp = ext4_get_group_info(sb, group);
916
917 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
918 goto next_group;
919
920 brelse(inode_bitmap_bh);
921 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
922
923 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
924 IS_ERR(inode_bitmap_bh)) {
925 inode_bitmap_bh = NULL;
926 goto next_group;
927 }
928
929repeat_in_this_group:
930 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
931 if (!ret2)
932 goto next_group;
933
934 if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
935 ext4_error(sb, "reserved inode found cleared - "
936 "inode=%lu", ino + 1);
937 ext4_mark_group_bitmap_corrupted(sb, group,
938 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
939 goto next_group;
940 }
941
942 if (!handle) {
943 BUG_ON(nblocks <= 0);
944 handle = __ext4_journal_start_sb(dir->i_sb, line_no,
945 handle_type, nblocks, 0,
946 ext4_trans_default_revoke_credits(sb));
947 if (IS_ERR(handle)) {
948 err = PTR_ERR(handle);
949 ext4_std_error(sb, err);
950 goto out;
951 }
952 }
953 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
954 err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
955 if (err) {
956 ext4_std_error(sb, err);
957 goto out;
958 }
959 ext4_lock_group(sb, group);
960 ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
961 if (ret2) {
962
963
964
965 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
966 if (ret2) {
967 ext4_set_bit(ino, inode_bitmap_bh->b_data);
968 ret2 = 0;
969 } else {
970 ret2 = 1;
971 }
972 }
973 ext4_unlock_group(sb, group);
974 ino++;
975 if (!ret2)
976 goto got;
977
978 if (ino < EXT4_INODES_PER_GROUP(sb))
979 goto repeat_in_this_group;
980next_group:
981 if (++group == ngroups)
982 group = 0;
983 }
984 err = -ENOSPC;
985 goto out;
986
987got:
988 BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
989 err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
990 if (err) {
991 ext4_std_error(sb, err);
992 goto out;
993 }
994
995 BUFFER_TRACE(group_desc_bh, "get_write_access");
996 err = ext4_journal_get_write_access(handle, group_desc_bh);
997 if (err) {
998 ext4_std_error(sb, err);
999 goto out;
1000 }
1001
1002
1003 if (ext4_has_group_desc_csum(sb) &&
1004 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
1005 struct buffer_head *block_bitmap_bh;
1006
1007 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
1008 if (IS_ERR(block_bitmap_bh)) {
1009 err = PTR_ERR(block_bitmap_bh);
1010 goto out;
1011 }
1012 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
1013 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
1014 if (err) {
1015 brelse(block_bitmap_bh);
1016 ext4_std_error(sb, err);
1017 goto out;
1018 }
1019
1020 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
1021 err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
1022
1023
1024 ext4_lock_group(sb, group);
1025 if (ext4_has_group_desc_csum(sb) &&
1026 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
1027 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
1028 ext4_free_group_clusters_set(sb, gdp,
1029 ext4_free_clusters_after_init(sb, group, gdp));
1030 ext4_block_bitmap_csum_set(sb, group, gdp,
1031 block_bitmap_bh);
1032 ext4_group_desc_csum_set(sb, group, gdp);
1033 }
1034 ext4_unlock_group(sb, group);
1035 brelse(block_bitmap_bh);
1036
1037 if (err) {
1038 ext4_std_error(sb, err);
1039 goto out;
1040 }
1041 }
1042
1043
1044 if (ext4_has_group_desc_csum(sb)) {
1045 int free;
1046 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1047
1048 down_read(&grp->alloc_sem);
1049 ext4_lock_group(sb, group);
1050 free = EXT4_INODES_PER_GROUP(sb) -
1051 ext4_itable_unused_count(sb, gdp);
1052 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1053 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1054 free = 0;
1055 }
1056
1057
1058
1059
1060
1061 if (ino > free)
1062 ext4_itable_unused_set(sb, gdp,
1063 (EXT4_INODES_PER_GROUP(sb) - ino));
1064 up_read(&grp->alloc_sem);
1065 } else {
1066 ext4_lock_group(sb, group);
1067 }
1068
1069 ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1070 if (S_ISDIR(mode)) {
1071 ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1072 if (sbi->s_log_groups_per_flex) {
1073 ext4_group_t f = ext4_flex_group(sbi, group);
1074
1075 atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
1076 f)->used_dirs);
1077 }
1078 }
1079 if (ext4_has_group_desc_csum(sb)) {
1080 ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
1081 EXT4_INODES_PER_GROUP(sb) / 8);
1082 ext4_group_desc_csum_set(sb, group, gdp);
1083 }
1084 ext4_unlock_group(sb, group);
1085
1086 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
1087 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1088 if (err) {
1089 ext4_std_error(sb, err);
1090 goto out;
1091 }
1092
1093 percpu_counter_dec(&sbi->s_freeinodes_counter);
1094 if (S_ISDIR(mode))
1095 percpu_counter_inc(&sbi->s_dirs_counter);
1096
1097 if (sbi->s_log_groups_per_flex) {
1098 flex_group = ext4_flex_group(sbi, group);
1099 atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups,
1100 flex_group)->free_inodes);
1101 }
1102
1103 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1104
1105 inode->i_blocks = 0;
1106 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1107 ei->i_crtime = inode->i_mtime;
1108
1109 memset(ei->i_data, 0, sizeof(ei->i_data));
1110 ei->i_dir_start_lookup = 0;
1111 ei->i_disksize = 0;
1112
1113
1114 ei->i_flags =
1115 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
1116 ei->i_flags |= i_flags;
1117 ei->i_file_acl = 0;
1118 ei->i_dtime = 0;
1119 ei->i_block_group = group;
1120 ei->i_last_alloc_group = ~0;
1121
1122 ext4_set_inode_flags(inode);
1123 if (IS_DIRSYNC(inode))
1124 ext4_handle_sync(handle);
1125 if (insert_inode_locked(inode) < 0) {
1126
1127
1128
1129
1130 err = -EIO;
1131 ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1132 inode->i_ino);
1133 ext4_mark_group_bitmap_corrupted(sb, group,
1134 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
1135 goto out;
1136 }
1137 inode->i_generation = prandom_u32();
1138
1139
1140 if (ext4_has_metadata_csum(sb)) {
1141 __u32 csum;
1142 __le32 inum = cpu_to_le32(inode->i_ino);
1143 __le32 gen = cpu_to_le32(inode->i_generation);
1144 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1145 sizeof(inum));
1146 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1147 sizeof(gen));
1148 }
1149
1150 ext4_clear_state_flags(ei);
1151 ext4_set_inode_state(inode, EXT4_STATE_NEW);
1152
1153 ei->i_extra_isize = sbi->s_want_extra_isize;
1154 ei->i_inline_off = 0;
1155 if (ext4_has_feature_inline_data(sb))
1156 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1157 ret = inode;
1158 err = dquot_alloc_inode(inode);
1159 if (err)
1160 goto fail_drop;
1161
1162
1163
1164
1165
1166
1167 if (encrypt) {
1168 err = fscrypt_inherit_context(dir, inode, handle, true);
1169 if (err)
1170 goto fail_free_drop;
1171 }
1172
1173 if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1174 err = ext4_init_acl(handle, inode, dir);
1175 if (err)
1176 goto fail_free_drop;
1177
1178 err = ext4_init_security(handle, inode, dir, qstr);
1179 if (err)
1180 goto fail_free_drop;
1181 }
1182
1183 if (ext4_has_feature_extents(sb)) {
1184
1185 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
1186 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1187 ext4_ext_tree_init(handle, inode);
1188 }
1189 }
1190
1191 if (ext4_handle_valid(handle)) {
1192 ei->i_sync_tid = handle->h_transaction->t_tid;
1193 ei->i_datasync_tid = handle->h_transaction->t_tid;
1194 }
1195
1196 err = ext4_mark_inode_dirty(handle, inode);
1197 if (err) {
1198 ext4_std_error(sb, err);
1199 goto fail_free_drop;
1200 }
1201
1202 ext4_debug("allocating inode %lu\n", inode->i_ino);
1203 trace_ext4_allocate_inode(inode, dir, mode);
1204 brelse(inode_bitmap_bh);
1205 return ret;
1206
1207fail_free_drop:
1208 dquot_free_inode(inode);
1209fail_drop:
1210 clear_nlink(inode);
1211 unlock_new_inode(inode);
1212out:
1213 dquot_drop(inode);
1214 inode->i_flags |= S_NOQUOTA;
1215 iput(inode);
1216 brelse(inode_bitmap_bh);
1217 return ERR_PTR(err);
1218}
1219
1220
1221struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1222{
1223 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1224 ext4_group_t block_group;
1225 int bit;
1226 struct buffer_head *bitmap_bh = NULL;
1227 struct inode *inode = NULL;
1228 int err = -EFSCORRUPTED;
1229
1230 if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
1231 goto bad_orphan;
1232
1233 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1234 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1235 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
1236 if (IS_ERR(bitmap_bh))
1237 return ERR_CAST(bitmap_bh);
1238
1239
1240
1241
1242
1243 if (!ext4_test_bit(bit, bitmap_bh->b_data))
1244 goto bad_orphan;
1245
1246 inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
1247 if (IS_ERR(inode)) {
1248 err = PTR_ERR(inode);
1249 ext4_error_err(sb, -err,
1250 "couldn't read orphan inode %lu (err %d)",
1251 ino, err);
1252 brelse(bitmap_bh);
1253 return inode;
1254 }
1255
1256
1257
1258
1259
1260
1261
1262 if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1263 is_bad_inode(inode))
1264 goto bad_orphan;
1265
1266 if (NEXT_ORPHAN(inode) > max_ino)
1267 goto bad_orphan;
1268 brelse(bitmap_bh);
1269 return inode;
1270
1271bad_orphan:
1272 ext4_error(sb, "bad orphan inode %lu", ino);
1273 if (bitmap_bh)
1274 printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1275 bit, (unsigned long long)bitmap_bh->b_blocknr,
1276 ext4_test_bit(bit, bitmap_bh->b_data));
1277 if (inode) {
1278 printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1279 is_bad_inode(inode));
1280 printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1281 NEXT_ORPHAN(inode));
1282 printk(KERN_ERR "max_ino=%lu\n", max_ino);
1283 printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1284
1285 if (inode->i_nlink == 0)
1286 inode->i_blocks = 0;
1287 iput(inode);
1288 }
1289 brelse(bitmap_bh);
1290 return ERR_PTR(err);
1291}
1292
1293unsigned long ext4_count_free_inodes(struct super_block *sb)
1294{
1295 unsigned long desc_count;
1296 struct ext4_group_desc *gdp;
1297 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1298#ifdef EXT4FS_DEBUG
1299 struct ext4_super_block *es;
1300 unsigned long bitmap_count, x;
1301 struct buffer_head *bitmap_bh = NULL;
1302
1303 es = EXT4_SB(sb)->s_es;
1304 desc_count = 0;
1305 bitmap_count = 0;
1306 gdp = NULL;
1307 for (i = 0; i < ngroups; i++) {
1308 gdp = ext4_get_group_desc(sb, i, NULL);
1309 if (!gdp)
1310 continue;
1311 desc_count += ext4_free_inodes_count(sb, gdp);
1312 brelse(bitmap_bh);
1313 bitmap_bh = ext4_read_inode_bitmap(sb, i);
1314 if (IS_ERR(bitmap_bh)) {
1315 bitmap_bh = NULL;
1316 continue;
1317 }
1318
1319 x = ext4_count_free(bitmap_bh->b_data,
1320 EXT4_INODES_PER_GROUP(sb) / 8);
1321 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1322 (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1323 bitmap_count += x;
1324 }
1325 brelse(bitmap_bh);
1326 printk(KERN_DEBUG "ext4_count_free_inodes: "
1327 "stored = %u, computed = %lu, %lu\n",
1328 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1329 return desc_count;
1330#else
1331 desc_count = 0;
1332 for (i = 0; i < ngroups; i++) {
1333 gdp = ext4_get_group_desc(sb, i, NULL);
1334 if (!gdp)
1335 continue;
1336 desc_count += ext4_free_inodes_count(sb, gdp);
1337 cond_resched();
1338 }
1339 return desc_count;
1340#endif
1341}
1342
1343
1344unsigned long ext4_count_dirs(struct super_block * sb)
1345{
1346 unsigned long count = 0;
1347 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1348
1349 for (i = 0; i < ngroups; i++) {
1350 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1351 if (!gdp)
1352 continue;
1353 count += ext4_used_dirs_count(sb, gdp);
1354 }
1355 return count;
1356}
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1367 int barrier)
1368{
1369 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1370 struct ext4_sb_info *sbi = EXT4_SB(sb);
1371 struct ext4_group_desc *gdp = NULL;
1372 struct buffer_head *group_desc_bh;
1373 handle_t *handle;
1374 ext4_fsblk_t blk;
1375 int num, ret = 0, used_blks = 0;
1376
1377
1378 if (sb_rdonly(sb)) {
1379 ret = 1;
1380 goto out;
1381 }
1382
1383 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1384 if (!gdp)
1385 goto out;
1386
1387
1388
1389
1390
1391 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1392 goto out;
1393
1394 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1395 if (IS_ERR(handle)) {
1396 ret = PTR_ERR(handle);
1397 goto out;
1398 }
1399
1400 down_write(&grp->alloc_sem);
1401
1402
1403
1404
1405
1406 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1407 used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1408 ext4_itable_unused_count(sb, gdp)),
1409 sbi->s_inodes_per_block);
1410
1411 if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group) ||
1412 ((group == 0) && ((EXT4_INODES_PER_GROUP(sb) -
1413 ext4_itable_unused_count(sb, gdp)) <
1414 EXT4_FIRST_INO(sb)))) {
1415 ext4_error(sb, "Something is wrong with group %u: "
1416 "used itable blocks: %d; "
1417 "itable unused count: %u",
1418 group, used_blks,
1419 ext4_itable_unused_count(sb, gdp));
1420 ret = 1;
1421 goto err_out;
1422 }
1423
1424 blk = ext4_inode_table(sb, gdp) + used_blks;
1425 num = sbi->s_itb_per_group - used_blks;
1426
1427 BUFFER_TRACE(group_desc_bh, "get_write_access");
1428 ret = ext4_journal_get_write_access(handle,
1429 group_desc_bh);
1430 if (ret)
1431 goto err_out;
1432
1433
1434
1435
1436
1437
1438 if (unlikely(num == 0))
1439 goto skip_zeroout;
1440
1441 ext4_debug("going to zero out inode table in group %d\n",
1442 group);
1443 ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1444 if (ret < 0)
1445 goto err_out;
1446 if (barrier)
1447 blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1448
1449skip_zeroout:
1450 ext4_lock_group(sb, group);
1451 gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1452 ext4_group_desc_csum_set(sb, group, gdp);
1453 ext4_unlock_group(sb, group);
1454
1455 BUFFER_TRACE(group_desc_bh,
1456 "call ext4_handle_dirty_metadata");
1457 ret = ext4_handle_dirty_metadata(handle, NULL,
1458 group_desc_bh);
1459
1460err_out:
1461 up_write(&grp->alloc_sem);
1462 ext4_journal_stop(handle);
1463out:
1464 return ret;
1465}
1466