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