]>
Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/super.c |
ac27a0ec DK |
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 | * from | |
10 | * | |
11 | * linux/fs/minix/inode.c | |
12 | * | |
13 | * Copyright (C) 1991, 1992 Linus Torvalds | |
14 | * | |
15 | * Big-endian to little-endian byte-swapping/bitmaps by | |
16 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/string.h> | |
21 | #include <linux/fs.h> | |
22 | #include <linux/time.h> | |
c5ca7c76 | 23 | #include <linux/vmalloc.h> |
dab291af | 24 | #include <linux/jbd2.h> |
ac27a0ec DK |
25 | #include <linux/slab.h> |
26 | #include <linux/init.h> | |
27 | #include <linux/blkdev.h> | |
28 | #include <linux/parser.h> | |
29 | #include <linux/smp_lock.h> | |
30 | #include <linux/buffer_head.h> | |
a5694255 | 31 | #include <linux/exportfs.h> |
ac27a0ec DK |
32 | #include <linux/vfs.h> |
33 | #include <linux/random.h> | |
34 | #include <linux/mount.h> | |
35 | #include <linux/namei.h> | |
36 | #include <linux/quotaops.h> | |
37 | #include <linux/seq_file.h> | |
9f6200bb | 38 | #include <linux/proc_fs.h> |
3197ebdb | 39 | #include <linux/ctype.h> |
ede86cc4 | 40 | #include <linux/marker.h> |
1330593e | 41 | #include <linux/log2.h> |
717d50e4 | 42 | #include <linux/crc16.h> |
ac27a0ec DK |
43 | #include <asm/uaccess.h> |
44 | ||
3dcf5451 CH |
45 | #include "ext4.h" |
46 | #include "ext4_jbd2.h" | |
ac27a0ec DK |
47 | #include "xattr.h" |
48 | #include "acl.h" | |
ac27a0ec | 49 | |
f4033903 CW |
50 | static int default_mb_history_length = 1000; |
51 | ||
52 | module_param_named(default_mb_history_length, default_mb_history_length, | |
53 | int, 0644); | |
54 | MODULE_PARM_DESC(default_mb_history_length, | |
55 | "Default number of entries saved for mb_history"); | |
56 | ||
9f6200bb | 57 | struct proc_dir_entry *ext4_proc_root; |
3197ebdb | 58 | static struct kset *ext4_kset; |
9f6200bb | 59 | |
617ba13b | 60 | static int ext4_load_journal(struct super_block *, struct ext4_super_block *, |
ac27a0ec | 61 | unsigned long journal_devnum); |
e2d67052 | 62 | static int ext4_commit_super(struct super_block *sb, int sync); |
2b2d6d01 TT |
63 | static void ext4_mark_recovery_complete(struct super_block *sb, |
64 | struct ext4_super_block *es); | |
65 | static void ext4_clear_journal_err(struct super_block *sb, | |
66 | struct ext4_super_block *es); | |
617ba13b | 67 | static int ext4_sync_fs(struct super_block *sb, int wait); |
2b2d6d01 | 68 | static const char *ext4_decode_error(struct super_block *sb, int errno, |
ac27a0ec | 69 | char nbuf[16]); |
2b2d6d01 TT |
70 | static int ext4_remount(struct super_block *sb, int *flags, char *data); |
71 | static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf); | |
c4be0c1d | 72 | static int ext4_unfreeze(struct super_block *sb); |
2b2d6d01 | 73 | static void ext4_write_super(struct super_block *sb); |
c4be0c1d | 74 | static int ext4_freeze(struct super_block *sb); |
ac27a0ec | 75 | |
bd81d8ee | 76 | |
8fadc143 AR |
77 | ext4_fsblk_t ext4_block_bitmap(struct super_block *sb, |
78 | struct ext4_group_desc *bg) | |
bd81d8ee | 79 | { |
3a14589c | 80 | return le32_to_cpu(bg->bg_block_bitmap_lo) | |
8fadc143 | 81 | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? |
0b8e58a1 | 82 | (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0); |
bd81d8ee LV |
83 | } |
84 | ||
8fadc143 AR |
85 | ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb, |
86 | struct ext4_group_desc *bg) | |
bd81d8ee | 87 | { |
5272f837 | 88 | return le32_to_cpu(bg->bg_inode_bitmap_lo) | |
8fadc143 | 89 | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? |
0b8e58a1 | 90 | (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0); |
bd81d8ee LV |
91 | } |
92 | ||
8fadc143 AR |
93 | ext4_fsblk_t ext4_inode_table(struct super_block *sb, |
94 | struct ext4_group_desc *bg) | |
bd81d8ee | 95 | { |
5272f837 | 96 | return le32_to_cpu(bg->bg_inode_table_lo) | |
8fadc143 | 97 | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? |
0b8e58a1 | 98 | (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0); |
bd81d8ee LV |
99 | } |
100 | ||
560671a0 AK |
101 | __u32 ext4_free_blks_count(struct super_block *sb, |
102 | struct ext4_group_desc *bg) | |
103 | { | |
104 | return le16_to_cpu(bg->bg_free_blocks_count_lo) | | |
105 | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? | |
0b8e58a1 | 106 | (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0); |
560671a0 AK |
107 | } |
108 | ||
109 | __u32 ext4_free_inodes_count(struct super_block *sb, | |
110 | struct ext4_group_desc *bg) | |
111 | { | |
112 | return le16_to_cpu(bg->bg_free_inodes_count_lo) | | |
113 | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? | |
0b8e58a1 | 114 | (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0); |
560671a0 AK |
115 | } |
116 | ||
117 | __u32 ext4_used_dirs_count(struct super_block *sb, | |
118 | struct ext4_group_desc *bg) | |
119 | { | |
120 | return le16_to_cpu(bg->bg_used_dirs_count_lo) | | |
121 | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? | |
0b8e58a1 | 122 | (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0); |
560671a0 AK |
123 | } |
124 | ||
125 | __u32 ext4_itable_unused_count(struct super_block *sb, | |
126 | struct ext4_group_desc *bg) | |
127 | { | |
128 | return le16_to_cpu(bg->bg_itable_unused_lo) | | |
129 | (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? | |
0b8e58a1 | 130 | (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0); |
560671a0 AK |
131 | } |
132 | ||
8fadc143 AR |
133 | void ext4_block_bitmap_set(struct super_block *sb, |
134 | struct ext4_group_desc *bg, ext4_fsblk_t blk) | |
bd81d8ee | 135 | { |
3a14589c | 136 | bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk); |
8fadc143 AR |
137 | if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) |
138 | bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32); | |
bd81d8ee LV |
139 | } |
140 | ||
8fadc143 AR |
141 | void ext4_inode_bitmap_set(struct super_block *sb, |
142 | struct ext4_group_desc *bg, ext4_fsblk_t blk) | |
bd81d8ee | 143 | { |
5272f837 | 144 | bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk); |
8fadc143 AR |
145 | if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) |
146 | bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32); | |
bd81d8ee LV |
147 | } |
148 | ||
8fadc143 AR |
149 | void ext4_inode_table_set(struct super_block *sb, |
150 | struct ext4_group_desc *bg, ext4_fsblk_t blk) | |
bd81d8ee | 151 | { |
5272f837 | 152 | bg->bg_inode_table_lo = cpu_to_le32((u32)blk); |
8fadc143 AR |
153 | if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) |
154 | bg->bg_inode_table_hi = cpu_to_le32(blk >> 32); | |
bd81d8ee LV |
155 | } |
156 | ||
560671a0 AK |
157 | void ext4_free_blks_set(struct super_block *sb, |
158 | struct ext4_group_desc *bg, __u32 count) | |
159 | { | |
160 | bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count); | |
161 | if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) | |
162 | bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16); | |
163 | } | |
164 | ||
165 | void ext4_free_inodes_set(struct super_block *sb, | |
166 | struct ext4_group_desc *bg, __u32 count) | |
167 | { | |
168 | bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count); | |
169 | if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) | |
170 | bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16); | |
171 | } | |
172 | ||
173 | void ext4_used_dirs_set(struct super_block *sb, | |
174 | struct ext4_group_desc *bg, __u32 count) | |
175 | { | |
176 | bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count); | |
177 | if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) | |
178 | bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16); | |
179 | } | |
180 | ||
181 | void ext4_itable_unused_set(struct super_block *sb, | |
182 | struct ext4_group_desc *bg, __u32 count) | |
183 | { | |
184 | bg->bg_itable_unused_lo = cpu_to_le16((__u16)count); | |
185 | if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) | |
186 | bg->bg_itable_unused_hi = cpu_to_le16(count >> 16); | |
187 | } | |
188 | ||
ac27a0ec | 189 | /* |
dab291af | 190 | * Wrappers for jbd2_journal_start/end. |
ac27a0ec DK |
191 | * |
192 | * The only special thing we need to do here is to make sure that all | |
193 | * journal_end calls result in the superblock being marked dirty, so | |
194 | * that sync() will call the filesystem's write_super callback if | |
195 | * appropriate. | |
196 | */ | |
617ba13b | 197 | handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks) |
ac27a0ec DK |
198 | { |
199 | journal_t *journal; | |
200 | ||
201 | if (sb->s_flags & MS_RDONLY) | |
202 | return ERR_PTR(-EROFS); | |
203 | ||
204 | /* Special case here: if the journal has aborted behind our | |
205 | * backs (eg. EIO in the commit thread), then we still need to | |
206 | * take the FS itself readonly cleanly. */ | |
617ba13b | 207 | journal = EXT4_SB(sb)->s_journal; |
0390131b FM |
208 | if (journal) { |
209 | if (is_journal_aborted(journal)) { | |
0b8e58a1 | 210 | ext4_abort(sb, __func__, "Detected aborted journal"); |
0390131b FM |
211 | return ERR_PTR(-EROFS); |
212 | } | |
213 | return jbd2_journal_start(journal, nblocks); | |
ac27a0ec | 214 | } |
0390131b FM |
215 | /* |
216 | * We're not journaling, return the appropriate indication. | |
217 | */ | |
218 | current->journal_info = EXT4_NOJOURNAL_HANDLE; | |
219 | return current->journal_info; | |
ac27a0ec DK |
220 | } |
221 | ||
222 | /* | |
223 | * The only special thing we need to do here is to make sure that all | |
dab291af | 224 | * jbd2_journal_stop calls result in the superblock being marked dirty, so |
ac27a0ec DK |
225 | * that sync() will call the filesystem's write_super callback if |
226 | * appropriate. | |
227 | */ | |
617ba13b | 228 | int __ext4_journal_stop(const char *where, handle_t *handle) |
ac27a0ec DK |
229 | { |
230 | struct super_block *sb; | |
231 | int err; | |
232 | int rc; | |
233 | ||
0390131b FM |
234 | if (!ext4_handle_valid(handle)) { |
235 | /* | |
236 | * Do this here since we don't call jbd2_journal_stop() in | |
237 | * no-journal mode. | |
238 | */ | |
239 | current->journal_info = NULL; | |
240 | return 0; | |
241 | } | |
ac27a0ec DK |
242 | sb = handle->h_transaction->t_journal->j_private; |
243 | err = handle->h_err; | |
dab291af | 244 | rc = jbd2_journal_stop(handle); |
ac27a0ec DK |
245 | |
246 | if (!err) | |
247 | err = rc; | |
248 | if (err) | |
617ba13b | 249 | __ext4_std_error(sb, where, err); |
ac27a0ec DK |
250 | return err; |
251 | } | |
252 | ||
617ba13b | 253 | void ext4_journal_abort_handle(const char *caller, const char *err_fn, |
ac27a0ec DK |
254 | struct buffer_head *bh, handle_t *handle, int err) |
255 | { | |
256 | char nbuf[16]; | |
617ba13b | 257 | const char *errstr = ext4_decode_error(NULL, err, nbuf); |
ac27a0ec | 258 | |
0390131b FM |
259 | BUG_ON(!ext4_handle_valid(handle)); |
260 | ||
ac27a0ec DK |
261 | if (bh) |
262 | BUFFER_TRACE(bh, "abort"); | |
263 | ||
264 | if (!handle->h_err) | |
265 | handle->h_err = err; | |
266 | ||
267 | if (is_handle_aborted(handle)) | |
268 | return; | |
269 | ||
270 | printk(KERN_ERR "%s: aborting transaction: %s in %s\n", | |
271 | caller, errstr, err_fn); | |
272 | ||
dab291af | 273 | jbd2_journal_abort_handle(handle); |
ac27a0ec DK |
274 | } |
275 | ||
276 | /* Deal with the reporting of failure conditions on a filesystem such as | |
277 | * inconsistencies detected or read IO failures. | |
278 | * | |
279 | * On ext2, we can store the error state of the filesystem in the | |
617ba13b | 280 | * superblock. That is not possible on ext4, because we may have other |
ac27a0ec DK |
281 | * write ordering constraints on the superblock which prevent us from |
282 | * writing it out straight away; and given that the journal is about to | |
283 | * be aborted, we can't rely on the current, or future, transactions to | |
284 | * write out the superblock safely. | |
285 | * | |
dab291af | 286 | * We'll just use the jbd2_journal_abort() error code to record an error in |
ac27a0ec DK |
287 | * the journal instead. On recovery, the journal will compain about |
288 | * that error until we've noted it down and cleared it. | |
289 | */ | |
290 | ||
617ba13b | 291 | static void ext4_handle_error(struct super_block *sb) |
ac27a0ec | 292 | { |
617ba13b | 293 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
ac27a0ec | 294 | |
617ba13b MC |
295 | EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; |
296 | es->s_state |= cpu_to_le16(EXT4_ERROR_FS); | |
ac27a0ec DK |
297 | |
298 | if (sb->s_flags & MS_RDONLY) | |
299 | return; | |
300 | ||
2b2d6d01 | 301 | if (!test_opt(sb, ERRORS_CONT)) { |
617ba13b | 302 | journal_t *journal = EXT4_SB(sb)->s_journal; |
ac27a0ec | 303 | |
617ba13b | 304 | EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT; |
ac27a0ec | 305 | if (journal) |
dab291af | 306 | jbd2_journal_abort(journal, -EIO); |
ac27a0ec | 307 | } |
2b2d6d01 | 308 | if (test_opt(sb, ERRORS_RO)) { |
b31e1552 | 309 | ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); |
ac27a0ec DK |
310 | sb->s_flags |= MS_RDONLY; |
311 | } | |
e2d67052 | 312 | ext4_commit_super(sb, 1); |
ac27a0ec | 313 | if (test_opt(sb, ERRORS_PANIC)) |
617ba13b | 314 | panic("EXT4-fs (device %s): panic forced after error\n", |
ac27a0ec DK |
315 | sb->s_id); |
316 | } | |
317 | ||
2b2d6d01 TT |
318 | void ext4_error(struct super_block *sb, const char *function, |
319 | const char *fmt, ...) | |
ac27a0ec DK |
320 | { |
321 | va_list args; | |
322 | ||
323 | va_start(args, fmt); | |
2b2d6d01 | 324 | printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function); |
ac27a0ec DK |
325 | vprintk(fmt, args); |
326 | printk("\n"); | |
327 | va_end(args); | |
328 | ||
617ba13b | 329 | ext4_handle_error(sb); |
ac27a0ec DK |
330 | } |
331 | ||
2b2d6d01 | 332 | static const char *ext4_decode_error(struct super_block *sb, int errno, |
ac27a0ec DK |
333 | char nbuf[16]) |
334 | { | |
335 | char *errstr = NULL; | |
336 | ||
337 | switch (errno) { | |
338 | case -EIO: | |
339 | errstr = "IO failure"; | |
340 | break; | |
341 | case -ENOMEM: | |
342 | errstr = "Out of memory"; | |
343 | break; | |
344 | case -EROFS: | |
dab291af | 345 | if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT) |
ac27a0ec DK |
346 | errstr = "Journal has aborted"; |
347 | else | |
348 | errstr = "Readonly filesystem"; | |
349 | break; | |
350 | default: | |
351 | /* If the caller passed in an extra buffer for unknown | |
352 | * errors, textualise them now. Else we just return | |
353 | * NULL. */ | |
354 | if (nbuf) { | |
355 | /* Check for truncated error codes... */ | |
356 | if (snprintf(nbuf, 16, "error %d", -errno) >= 0) | |
357 | errstr = nbuf; | |
358 | } | |
359 | break; | |
360 | } | |
361 | ||
362 | return errstr; | |
363 | } | |
364 | ||
617ba13b | 365 | /* __ext4_std_error decodes expected errors from journaling functions |
ac27a0ec DK |
366 | * automatically and invokes the appropriate error response. */ |
367 | ||
2b2d6d01 | 368 | void __ext4_std_error(struct super_block *sb, const char *function, int errno) |
ac27a0ec DK |
369 | { |
370 | char nbuf[16]; | |
371 | const char *errstr; | |
372 | ||
373 | /* Special case: if the error is EROFS, and we're not already | |
374 | * inside a transaction, then there's really no point in logging | |
375 | * an error. */ | |
376 | if (errno == -EROFS && journal_current_handle() == NULL && | |
377 | (sb->s_flags & MS_RDONLY)) | |
378 | return; | |
379 | ||
617ba13b | 380 | errstr = ext4_decode_error(sb, errno, nbuf); |
2b2d6d01 TT |
381 | printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n", |
382 | sb->s_id, function, errstr); | |
ac27a0ec | 383 | |
617ba13b | 384 | ext4_handle_error(sb); |
ac27a0ec DK |
385 | } |
386 | ||
387 | /* | |
617ba13b | 388 | * ext4_abort is a much stronger failure handler than ext4_error. The |
ac27a0ec DK |
389 | * abort function may be used to deal with unrecoverable failures such |
390 | * as journal IO errors or ENOMEM at a critical moment in log management. | |
391 | * | |
392 | * We unconditionally force the filesystem into an ABORT|READONLY state, | |
393 | * unless the error response on the fs has been set to panic in which | |
394 | * case we take the easy way out and panic immediately. | |
395 | */ | |
396 | ||
2b2d6d01 TT |
397 | void ext4_abort(struct super_block *sb, const char *function, |
398 | const char *fmt, ...) | |
ac27a0ec DK |
399 | { |
400 | va_list args; | |
401 | ||
ac27a0ec | 402 | va_start(args, fmt); |
2b2d6d01 | 403 | printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function); |
ac27a0ec DK |
404 | vprintk(fmt, args); |
405 | printk("\n"); | |
406 | va_end(args); | |
407 | ||
408 | if (test_opt(sb, ERRORS_PANIC)) | |
617ba13b | 409 | panic("EXT4-fs panic from previous error\n"); |
ac27a0ec DK |
410 | |
411 | if (sb->s_flags & MS_RDONLY) | |
412 | return; | |
413 | ||
b31e1552 | 414 | ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); |
617ba13b | 415 | EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; |
ac27a0ec | 416 | sb->s_flags |= MS_RDONLY; |
617ba13b | 417 | EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT; |
ef2cabf7 HK |
418 | if (EXT4_SB(sb)->s_journal) |
419 | jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO); | |
ac27a0ec DK |
420 | } |
421 | ||
b31e1552 ES |
422 | void ext4_msg (struct super_block * sb, const char *prefix, |
423 | const char *fmt, ...) | |
424 | { | |
425 | va_list args; | |
426 | ||
427 | va_start(args, fmt); | |
428 | printk("%sEXT4-fs (%s): ", prefix, sb->s_id); | |
429 | vprintk(fmt, args); | |
430 | printk("\n"); | |
431 | va_end(args); | |
432 | } | |
433 | ||
2b2d6d01 TT |
434 | void ext4_warning(struct super_block *sb, const char *function, |
435 | const char *fmt, ...) | |
ac27a0ec DK |
436 | { |
437 | va_list args; | |
438 | ||
439 | va_start(args, fmt); | |
617ba13b | 440 | printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ", |
ac27a0ec DK |
441 | sb->s_id, function); |
442 | vprintk(fmt, args); | |
443 | printk("\n"); | |
444 | va_end(args); | |
445 | } | |
446 | ||
5d1b1b3f | 447 | void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp, |
0b8e58a1 | 448 | const char *function, const char *fmt, ...) |
5d1b1b3f AK |
449 | __releases(bitlock) |
450 | __acquires(bitlock) | |
451 | { | |
452 | va_list args; | |
453 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; | |
454 | ||
455 | va_start(args, fmt); | |
456 | printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function); | |
457 | vprintk(fmt, args); | |
458 | printk("\n"); | |
459 | va_end(args); | |
460 | ||
461 | if (test_opt(sb, ERRORS_CONT)) { | |
462 | EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; | |
463 | es->s_state |= cpu_to_le16(EXT4_ERROR_FS); | |
e2d67052 | 464 | ext4_commit_super(sb, 0); |
5d1b1b3f AK |
465 | return; |
466 | } | |
467 | ext4_unlock_group(sb, grp); | |
468 | ext4_handle_error(sb); | |
469 | /* | |
470 | * We only get here in the ERRORS_RO case; relocking the group | |
471 | * may be dangerous, but nothing bad will happen since the | |
472 | * filesystem will have already been marked read/only and the | |
473 | * journal has been aborted. We return 1 as a hint to callers | |
474 | * who might what to use the return value from | |
475 | * ext4_grp_locked_error() to distinguish beween the | |
476 | * ERRORS_CONT and ERRORS_RO case, and perhaps return more | |
477 | * aggressively from the ext4 function in question, with a | |
478 | * more appropriate error code. | |
479 | */ | |
480 | ext4_lock_group(sb, grp); | |
481 | return; | |
482 | } | |
483 | ||
617ba13b | 484 | void ext4_update_dynamic_rev(struct super_block *sb) |
ac27a0ec | 485 | { |
617ba13b | 486 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
ac27a0ec | 487 | |
617ba13b | 488 | if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV) |
ac27a0ec DK |
489 | return; |
490 | ||
46e665e9 | 491 | ext4_warning(sb, __func__, |
ac27a0ec DK |
492 | "updating to rev %d because of new feature flag, " |
493 | "running e2fsck is recommended", | |
617ba13b | 494 | EXT4_DYNAMIC_REV); |
ac27a0ec | 495 | |
617ba13b MC |
496 | es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO); |
497 | es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE); | |
498 | es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV); | |
ac27a0ec DK |
499 | /* leave es->s_feature_*compat flags alone */ |
500 | /* es->s_uuid will be set by e2fsck if empty */ | |
501 | ||
502 | /* | |
503 | * The rest of the superblock fields should be zero, and if not it | |
504 | * means they are likely already in use, so leave them alone. We | |
505 | * can leave it up to e2fsck to clean up any inconsistencies there. | |
506 | */ | |
507 | } | |
508 | ||
509 | /* | |
510 | * Open the external journal device | |
511 | */ | |
b31e1552 | 512 | static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb) |
ac27a0ec DK |
513 | { |
514 | struct block_device *bdev; | |
515 | char b[BDEVNAME_SIZE]; | |
516 | ||
517 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); | |
518 | if (IS_ERR(bdev)) | |
519 | goto fail; | |
520 | return bdev; | |
521 | ||
522 | fail: | |
b31e1552 | 523 | ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld", |
ac27a0ec DK |
524 | __bdevname(dev, b), PTR_ERR(bdev)); |
525 | return NULL; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Release the journal device | |
530 | */ | |
617ba13b | 531 | static int ext4_blkdev_put(struct block_device *bdev) |
ac27a0ec DK |
532 | { |
533 | bd_release(bdev); | |
9a1c3542 | 534 | return blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
ac27a0ec DK |
535 | } |
536 | ||
617ba13b | 537 | static int ext4_blkdev_remove(struct ext4_sb_info *sbi) |
ac27a0ec DK |
538 | { |
539 | struct block_device *bdev; | |
540 | int ret = -ENODEV; | |
541 | ||
542 | bdev = sbi->journal_bdev; | |
543 | if (bdev) { | |
617ba13b | 544 | ret = ext4_blkdev_put(bdev); |
ac27a0ec DK |
545 | sbi->journal_bdev = NULL; |
546 | } | |
547 | return ret; | |
548 | } | |
549 | ||
550 | static inline struct inode *orphan_list_entry(struct list_head *l) | |
551 | { | |
617ba13b | 552 | return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode; |
ac27a0ec DK |
553 | } |
554 | ||
617ba13b | 555 | static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi) |
ac27a0ec DK |
556 | { |
557 | struct list_head *l; | |
558 | ||
b31e1552 ES |
559 | ext4_msg(sb, KERN_ERR, "sb orphan head is %d", |
560 | le32_to_cpu(sbi->s_es->s_last_orphan)); | |
ac27a0ec DK |
561 | |
562 | printk(KERN_ERR "sb_info orphan list:\n"); | |
563 | list_for_each(l, &sbi->s_orphan) { | |
564 | struct inode *inode = orphan_list_entry(l); | |
565 | printk(KERN_ERR " " | |
566 | "inode %s:%lu at %p: mode %o, nlink %d, next %d\n", | |
567 | inode->i_sb->s_id, inode->i_ino, inode, | |
568 | inode->i_mode, inode->i_nlink, | |
569 | NEXT_ORPHAN(inode)); | |
570 | } | |
571 | } | |
572 | ||
2b2d6d01 | 573 | static void ext4_put_super(struct super_block *sb) |
ac27a0ec | 574 | { |
617ba13b MC |
575 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
576 | struct ext4_super_block *es = sbi->s_es; | |
ef2cabf7 | 577 | int i, err; |
ac27a0ec | 578 | |
8c85e125 CH |
579 | if (sb->s_dirt) |
580 | ext4_write_super(sb); | |
581 | ||
6fd058f7 | 582 | ext4_release_system_zone(sb); |
c9de560d | 583 | ext4_mb_release(sb); |
a86c6181 | 584 | ext4_ext_release(sb); |
617ba13b | 585 | ext4_xattr_put_super(sb); |
0390131b FM |
586 | if (sbi->s_journal) { |
587 | err = jbd2_journal_destroy(sbi->s_journal); | |
588 | sbi->s_journal = NULL; | |
589 | if (err < 0) | |
590 | ext4_abort(sb, __func__, | |
591 | "Couldn't clean up the journal"); | |
592 | } | |
ac27a0ec | 593 | if (!(sb->s_flags & MS_RDONLY)) { |
617ba13b | 594 | EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); |
ac27a0ec | 595 | es->s_state = cpu_to_le16(sbi->s_mount_state); |
e2d67052 | 596 | ext4_commit_super(sb, 1); |
ac27a0ec | 597 | } |
240799cd | 598 | if (sbi->s_proc) { |
9f6200bb | 599 | remove_proc_entry(sb->s_id, ext4_proc_root); |
240799cd | 600 | } |
3197ebdb | 601 | kobject_del(&sbi->s_kobj); |
ac27a0ec DK |
602 | |
603 | for (i = 0; i < sbi->s_gdb_count; i++) | |
604 | brelse(sbi->s_group_desc[i]); | |
605 | kfree(sbi->s_group_desc); | |
c5ca7c76 TT |
606 | if (is_vmalloc_addr(sbi->s_flex_groups)) |
607 | vfree(sbi->s_flex_groups); | |
608 | else | |
609 | kfree(sbi->s_flex_groups); | |
ac27a0ec DK |
610 | percpu_counter_destroy(&sbi->s_freeblocks_counter); |
611 | percpu_counter_destroy(&sbi->s_freeinodes_counter); | |
612 | percpu_counter_destroy(&sbi->s_dirs_counter); | |
6bc6e63f | 613 | percpu_counter_destroy(&sbi->s_dirtyblocks_counter); |
ac27a0ec DK |
614 | brelse(sbi->s_sbh); |
615 | #ifdef CONFIG_QUOTA | |
616 | for (i = 0; i < MAXQUOTAS; i++) | |
617 | kfree(sbi->s_qf_names[i]); | |
618 | #endif | |
619 | ||
620 | /* Debugging code just in case the in-memory inode orphan list | |
621 | * isn't empty. The on-disk one can be non-empty if we've | |
622 | * detected an error and taken the fs readonly, but the | |
623 | * in-memory list had better be clean by this point. */ | |
624 | if (!list_empty(&sbi->s_orphan)) | |
625 | dump_orphan_list(sb, sbi); | |
626 | J_ASSERT(list_empty(&sbi->s_orphan)); | |
627 | ||
f98393a6 | 628 | invalidate_bdev(sb->s_bdev); |
ac27a0ec DK |
629 | if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) { |
630 | /* | |
631 | * Invalidate the journal device's buffers. We don't want them | |
632 | * floating about in memory - the physical journal device may | |
633 | * hotswapped, and it breaks the `ro-after' testing code. | |
634 | */ | |
635 | sync_blockdev(sbi->journal_bdev); | |
f98393a6 | 636 | invalidate_bdev(sbi->journal_bdev); |
617ba13b | 637 | ext4_blkdev_remove(sbi); |
ac27a0ec DK |
638 | } |
639 | sb->s_fs_info = NULL; | |
3197ebdb TT |
640 | /* |
641 | * Now that we are completely done shutting down the | |
642 | * superblock, we need to actually destroy the kobject. | |
643 | */ | |
644 | unlock_kernel(); | |
645 | unlock_super(sb); | |
646 | kobject_put(&sbi->s_kobj); | |
647 | wait_for_completion(&sbi->s_kobj_unregister); | |
648 | lock_super(sb); | |
649 | lock_kernel(); | |
705895b6 | 650 | kfree(sbi->s_blockgroup_lock); |
ac27a0ec | 651 | kfree(sbi); |
ac27a0ec DK |
652 | } |
653 | ||
e18b890b | 654 | static struct kmem_cache *ext4_inode_cachep; |
ac27a0ec DK |
655 | |
656 | /* | |
657 | * Called inside transaction, so use GFP_NOFS | |
658 | */ | |
617ba13b | 659 | static struct inode *ext4_alloc_inode(struct super_block *sb) |
ac27a0ec | 660 | { |
617ba13b | 661 | struct ext4_inode_info *ei; |
ac27a0ec | 662 | |
e6b4f8da | 663 | ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS); |
ac27a0ec DK |
664 | if (!ei) |
665 | return NULL; | |
0b8e58a1 | 666 | |
03010a33 | 667 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
617ba13b MC |
668 | ei->i_acl = EXT4_ACL_NOT_CACHED; |
669 | ei->i_default_acl = EXT4_ACL_NOT_CACHED; | |
ac27a0ec | 670 | #endif |
ac27a0ec | 671 | ei->vfs_inode.i_version = 1; |
91246c00 | 672 | ei->vfs_inode.i_data.writeback_index = 0; |
a86c6181 | 673 | memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache)); |
c9de560d AT |
674 | INIT_LIST_HEAD(&ei->i_prealloc_list); |
675 | spin_lock_init(&ei->i_prealloc_lock); | |
0390131b FM |
676 | /* |
677 | * Note: We can be called before EXT4_SB(sb)->s_journal is set, | |
678 | * therefore it can be null here. Don't check it, just initialize | |
679 | * jinode. | |
680 | */ | |
678aaf48 | 681 | jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode); |
d2a17637 MC |
682 | ei->i_reserved_data_blocks = 0; |
683 | ei->i_reserved_meta_blocks = 0; | |
684 | ei->i_allocated_meta_blocks = 0; | |
685 | ei->i_delalloc_reserved_flag = 0; | |
686 | spin_lock_init(&(ei->i_block_reservation_lock)); | |
0b8e58a1 | 687 | |
ac27a0ec DK |
688 | return &ei->vfs_inode; |
689 | } | |
690 | ||
617ba13b | 691 | static void ext4_destroy_inode(struct inode *inode) |
ac27a0ec | 692 | { |
9f7dd93d | 693 | if (!list_empty(&(EXT4_I(inode)->i_orphan))) { |
b31e1552 ES |
694 | ext4_msg(inode->i_sb, KERN_ERR, |
695 | "Inode %lu (%p): orphan list check failed!", | |
696 | inode->i_ino, EXT4_I(inode)); | |
9f7dd93d VA |
697 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4, |
698 | EXT4_I(inode), sizeof(struct ext4_inode_info), | |
699 | true); | |
700 | dump_stack(); | |
701 | } | |
617ba13b | 702 | kmem_cache_free(ext4_inode_cachep, EXT4_I(inode)); |
ac27a0ec DK |
703 | } |
704 | ||
51cc5068 | 705 | static void init_once(void *foo) |
ac27a0ec | 706 | { |
617ba13b | 707 | struct ext4_inode_info *ei = (struct ext4_inode_info *) foo; |
ac27a0ec | 708 | |
a35afb83 | 709 | INIT_LIST_HEAD(&ei->i_orphan); |
03010a33 | 710 | #ifdef CONFIG_EXT4_FS_XATTR |
a35afb83 | 711 | init_rwsem(&ei->xattr_sem); |
ac27a0ec | 712 | #endif |
0e855ac8 | 713 | init_rwsem(&ei->i_data_sem); |
a35afb83 | 714 | inode_init_once(&ei->vfs_inode); |
ac27a0ec DK |
715 | } |
716 | ||
717 | static int init_inodecache(void) | |
718 | { | |
617ba13b MC |
719 | ext4_inode_cachep = kmem_cache_create("ext4_inode_cache", |
720 | sizeof(struct ext4_inode_info), | |
ac27a0ec DK |
721 | 0, (SLAB_RECLAIM_ACCOUNT| |
722 | SLAB_MEM_SPREAD), | |
20c2df83 | 723 | init_once); |
617ba13b | 724 | if (ext4_inode_cachep == NULL) |
ac27a0ec DK |
725 | return -ENOMEM; |
726 | return 0; | |
727 | } | |
728 | ||
729 | static void destroy_inodecache(void) | |
730 | { | |
617ba13b | 731 | kmem_cache_destroy(ext4_inode_cachep); |
ac27a0ec DK |
732 | } |
733 | ||
617ba13b | 734 | static void ext4_clear_inode(struct inode *inode) |
ac27a0ec | 735 | { |
03010a33 | 736 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
617ba13b MC |
737 | if (EXT4_I(inode)->i_acl && |
738 | EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) { | |
739 | posix_acl_release(EXT4_I(inode)->i_acl); | |
740 | EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED; | |
741 | } | |
742 | if (EXT4_I(inode)->i_default_acl && | |
743 | EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) { | |
744 | posix_acl_release(EXT4_I(inode)->i_default_acl); | |
745 | EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED; | |
ac27a0ec DK |
746 | } |
747 | #endif | |
c2ea3fde | 748 | ext4_discard_preallocations(inode); |
0390131b FM |
749 | if (EXT4_JOURNAL(inode)) |
750 | jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal, | |
678aaf48 | 751 | &EXT4_I(inode)->jinode); |
ac27a0ec DK |
752 | } |
753 | ||
2b2d6d01 TT |
754 | static inline void ext4_show_quota_options(struct seq_file *seq, |
755 | struct super_block *sb) | |
ac27a0ec DK |
756 | { |
757 | #if defined(CONFIG_QUOTA) | |
617ba13b | 758 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
ac27a0ec DK |
759 | |
760 | if (sbi->s_jquota_fmt) | |
761 | seq_printf(seq, ",jqfmt=%s", | |
af5bc92d | 762 | (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0"); |
ac27a0ec DK |
763 | |
764 | if (sbi->s_qf_names[USRQUOTA]) | |
765 | seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]); | |
766 | ||
767 | if (sbi->s_qf_names[GRPQUOTA]) | |
768 | seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]); | |
769 | ||
617ba13b | 770 | if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) |
ac27a0ec DK |
771 | seq_puts(seq, ",usrquota"); |
772 | ||
617ba13b | 773 | if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) |
ac27a0ec DK |
774 | seq_puts(seq, ",grpquota"); |
775 | #endif | |
776 | } | |
777 | ||
d9c9bef1 MS |
778 | /* |
779 | * Show an option if | |
780 | * - it's set to a non-default value OR | |
781 | * - if the per-sb default is different from the global default | |
782 | */ | |
617ba13b | 783 | static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs) |
ac27a0ec | 784 | { |
aa22df2c AK |
785 | int def_errors; |
786 | unsigned long def_mount_opts; | |
ac27a0ec | 787 | struct super_block *sb = vfs->mnt_sb; |
d9c9bef1 MS |
788 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
789 | struct ext4_super_block *es = sbi->s_es; | |
d9c9bef1 MS |
790 | |
791 | def_mount_opts = le32_to_cpu(es->s_default_mount_opts); | |
aa22df2c | 792 | def_errors = le16_to_cpu(es->s_errors); |
d9c9bef1 MS |
793 | |
794 | if (sbi->s_sb_block != 1) | |
795 | seq_printf(seq, ",sb=%llu", sbi->s_sb_block); | |
796 | if (test_opt(sb, MINIX_DF)) | |
797 | seq_puts(seq, ",minixdf"); | |
aa22df2c | 798 | if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS)) |
d9c9bef1 MS |
799 | seq_puts(seq, ",grpid"); |
800 | if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS)) | |
801 | seq_puts(seq, ",nogrpid"); | |
802 | if (sbi->s_resuid != EXT4_DEF_RESUID || | |
803 | le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) { | |
804 | seq_printf(seq, ",resuid=%u", sbi->s_resuid); | |
805 | } | |
806 | if (sbi->s_resgid != EXT4_DEF_RESGID || | |
807 | le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) { | |
808 | seq_printf(seq, ",resgid=%u", sbi->s_resgid); | |
809 | } | |
bb4f397a | 810 | if (test_opt(sb, ERRORS_RO)) { |
d9c9bef1 | 811 | if (def_errors == EXT4_ERRORS_PANIC || |
bb4f397a AK |
812 | def_errors == EXT4_ERRORS_CONTINUE) { |
813 | seq_puts(seq, ",errors=remount-ro"); | |
d9c9bef1 MS |
814 | } |
815 | } | |
aa22df2c | 816 | if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE) |
bb4f397a | 817 | seq_puts(seq, ",errors=continue"); |
aa22df2c | 818 | if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC) |
d9c9bef1 | 819 | seq_puts(seq, ",errors=panic"); |
aa22df2c | 820 | if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16)) |
d9c9bef1 | 821 | seq_puts(seq, ",nouid32"); |
aa22df2c | 822 | if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG)) |
d9c9bef1 MS |
823 | seq_puts(seq, ",debug"); |
824 | if (test_opt(sb, OLDALLOC)) | |
825 | seq_puts(seq, ",oldalloc"); | |
03010a33 | 826 | #ifdef CONFIG_EXT4_FS_XATTR |
aa22df2c AK |
827 | if (test_opt(sb, XATTR_USER) && |
828 | !(def_mount_opts & EXT4_DEFM_XATTR_USER)) | |
d9c9bef1 MS |
829 | seq_puts(seq, ",user_xattr"); |
830 | if (!test_opt(sb, XATTR_USER) && | |
831 | (def_mount_opts & EXT4_DEFM_XATTR_USER)) { | |
832 | seq_puts(seq, ",nouser_xattr"); | |
833 | } | |
834 | #endif | |
03010a33 | 835 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
aa22df2c | 836 | if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL)) |
d9c9bef1 MS |
837 | seq_puts(seq, ",acl"); |
838 | if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL)) | |
839 | seq_puts(seq, ",noacl"); | |
840 | #endif | |
30773840 | 841 | if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) { |
d9c9bef1 MS |
842 | seq_printf(seq, ",commit=%u", |
843 | (unsigned) (sbi->s_commit_interval / HZ)); | |
844 | } | |
30773840 TT |
845 | if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) { |
846 | seq_printf(seq, ",min_batch_time=%u", | |
847 | (unsigned) sbi->s_min_batch_time); | |
848 | } | |
849 | if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) { | |
850 | seq_printf(seq, ",max_batch_time=%u", | |
851 | (unsigned) sbi->s_min_batch_time); | |
852 | } | |
853 | ||
571640ca ES |
854 | /* |
855 | * We're changing the default of barrier mount option, so | |
856 | * let's always display its mount state so it's clear what its | |
857 | * status is. | |
858 | */ | |
859 | seq_puts(seq, ",barrier="); | |
860 | seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0"); | |
cd0b6a39 TT |
861 | if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) |
862 | seq_puts(seq, ",journal_async_commit"); | |
d9c9bef1 MS |
863 | if (test_opt(sb, NOBH)) |
864 | seq_puts(seq, ",nobh"); | |
25ec56b5 JNC |
865 | if (test_opt(sb, I_VERSION)) |
866 | seq_puts(seq, ",i_version"); | |
dd919b98 AK |
867 | if (!test_opt(sb, DELALLOC)) |
868 | seq_puts(seq, ",nodelalloc"); | |
869 | ||
ac27a0ec | 870 | |
cb45bbe4 MS |
871 | if (sbi->s_stripe) |
872 | seq_printf(seq, ",stripe=%lu", sbi->s_stripe); | |
aa22df2c AK |
873 | /* |
874 | * journal mode get enabled in different ways | |
875 | * So just print the value even if we didn't specify it | |
876 | */ | |
617ba13b | 877 | if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) |
ac27a0ec | 878 | seq_puts(seq, ",data=journal"); |
617ba13b | 879 | else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) |
ac27a0ec | 880 | seq_puts(seq, ",data=ordered"); |
617ba13b | 881 | else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA) |
ac27a0ec DK |
882 | seq_puts(seq, ",data=writeback"); |
883 | ||
240799cd TT |
884 | if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS) |
885 | seq_printf(seq, ",inode_readahead_blks=%u", | |
886 | sbi->s_inode_readahead_blks); | |
887 | ||
5bf5683a HK |
888 | if (test_opt(sb, DATA_ERR_ABORT)) |
889 | seq_puts(seq, ",data_err=abort"); | |
890 | ||
afd4672d | 891 | if (test_opt(sb, NO_AUTO_DA_ALLOC)) |
06705bff | 892 | seq_puts(seq, ",noauto_da_alloc"); |
afd4672d | 893 | |
617ba13b | 894 | ext4_show_quota_options(seq, sb); |
0b8e58a1 | 895 | |
ac27a0ec DK |
896 | return 0; |
897 | } | |
898 | ||
1b961ac0 | 899 | static struct inode *ext4_nfs_get_inode(struct super_block *sb, |
0b8e58a1 | 900 | u64 ino, u32 generation) |
ac27a0ec | 901 | { |
ac27a0ec | 902 | struct inode *inode; |
ac27a0ec | 903 | |
617ba13b | 904 | if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) |
ac27a0ec | 905 | return ERR_PTR(-ESTALE); |
617ba13b | 906 | if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) |
ac27a0ec DK |
907 | return ERR_PTR(-ESTALE); |
908 | ||
909 | /* iget isn't really right if the inode is currently unallocated!! | |
910 | * | |
617ba13b | 911 | * ext4_read_inode will return a bad_inode if the inode had been |
ac27a0ec DK |
912 | * deleted, so we should be safe. |
913 | * | |
914 | * Currently we don't know the generation for parent directory, so | |
915 | * a generation of 0 means "accept any" | |
916 | */ | |
1d1fe1ee DH |
917 | inode = ext4_iget(sb, ino); |
918 | if (IS_ERR(inode)) | |
919 | return ERR_CAST(inode); | |
920 | if (generation && inode->i_generation != generation) { | |
ac27a0ec DK |
921 | iput(inode); |
922 | return ERR_PTR(-ESTALE); | |
923 | } | |
1b961ac0 CH |
924 | |
925 | return inode; | |
926 | } | |
927 | ||
928 | static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid, | |
0b8e58a1 | 929 | int fh_len, int fh_type) |
1b961ac0 CH |
930 | { |
931 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, | |
932 | ext4_nfs_get_inode); | |
933 | } | |
934 | ||
935 | static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid, | |
0b8e58a1 | 936 | int fh_len, int fh_type) |
1b961ac0 CH |
937 | { |
938 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, | |
939 | ext4_nfs_get_inode); | |
ac27a0ec DK |
940 | } |
941 | ||
c39a7f84 TO |
942 | /* |
943 | * Try to release metadata pages (indirect blocks, directories) which are | |
944 | * mapped via the block device. Since these pages could have journal heads | |
945 | * which would prevent try_to_free_buffers() from freeing them, we must use | |
946 | * jbd2 layer's try_to_free_buffers() function to release them. | |
947 | */ | |
0b8e58a1 AD |
948 | static int bdev_try_to_free_page(struct super_block *sb, struct page *page, |
949 | gfp_t wait) | |
c39a7f84 TO |
950 | { |
951 | journal_t *journal = EXT4_SB(sb)->s_journal; | |
952 | ||
953 | WARN_ON(PageChecked(page)); | |
954 | if (!page_has_buffers(page)) | |
955 | return 0; | |
956 | if (journal) | |
957 | return jbd2_journal_try_to_free_buffers(journal, page, | |
958 | wait & ~__GFP_WAIT); | |
959 | return try_to_free_buffers(page); | |
960 | } | |
961 | ||
ac27a0ec | 962 | #ifdef CONFIG_QUOTA |
af5bc92d | 963 | #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group") |
2b2d6d01 | 964 | #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA)) |
ac27a0ec | 965 | |
617ba13b MC |
966 | static int ext4_write_dquot(struct dquot *dquot); |
967 | static int ext4_acquire_dquot(struct dquot *dquot); | |
968 | static int ext4_release_dquot(struct dquot *dquot); | |
969 | static int ext4_mark_dquot_dirty(struct dquot *dquot); | |
970 | static int ext4_write_info(struct super_block *sb, int type); | |
6f28e087 JK |
971 | static int ext4_quota_on(struct super_block *sb, int type, int format_id, |
972 | char *path, int remount); | |
617ba13b MC |
973 | static int ext4_quota_on_mount(struct super_block *sb, int type); |
974 | static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, | |
ac27a0ec | 975 | size_t len, loff_t off); |
617ba13b | 976 | static ssize_t ext4_quota_write(struct super_block *sb, int type, |
ac27a0ec DK |
977 | const char *data, size_t len, loff_t off); |
978 | ||
617ba13b | 979 | static struct dquot_operations ext4_quota_operations = { |
edf72453 JK |
980 | .initialize = dquot_initialize, |
981 | .drop = dquot_drop, | |
ac27a0ec | 982 | .alloc_space = dquot_alloc_space, |
60e58e0f MC |
983 | .reserve_space = dquot_reserve_space, |
984 | .claim_space = dquot_claim_space, | |
985 | .release_rsv = dquot_release_reserved_space, | |
986 | .get_reserved_space = ext4_get_reserved_space, | |
ac27a0ec DK |
987 | .alloc_inode = dquot_alloc_inode, |
988 | .free_space = dquot_free_space, | |
989 | .free_inode = dquot_free_inode, | |
990 | .transfer = dquot_transfer, | |
617ba13b MC |
991 | .write_dquot = ext4_write_dquot, |
992 | .acquire_dquot = ext4_acquire_dquot, | |
993 | .release_dquot = ext4_release_dquot, | |
994 | .mark_dirty = ext4_mark_dquot_dirty, | |
a5b5ee32 JK |
995 | .write_info = ext4_write_info, |
996 | .alloc_dquot = dquot_alloc, | |
997 | .destroy_dquot = dquot_destroy, | |
ac27a0ec DK |
998 | }; |
999 | ||
617ba13b MC |
1000 | static struct quotactl_ops ext4_qctl_operations = { |
1001 | .quota_on = ext4_quota_on, | |
ac27a0ec DK |
1002 | .quota_off = vfs_quota_off, |
1003 | .quota_sync = vfs_quota_sync, | |
1004 | .get_info = vfs_get_dqinfo, | |
1005 | .set_info = vfs_set_dqinfo, | |
1006 | .get_dqblk = vfs_get_dqblk, | |
1007 | .set_dqblk = vfs_set_dqblk | |
1008 | }; | |
1009 | #endif | |
1010 | ||
ee9b6d61 | 1011 | static const struct super_operations ext4_sops = { |
617ba13b MC |
1012 | .alloc_inode = ext4_alloc_inode, |
1013 | .destroy_inode = ext4_destroy_inode, | |
617ba13b MC |
1014 | .write_inode = ext4_write_inode, |
1015 | .dirty_inode = ext4_dirty_inode, | |
1016 | .delete_inode = ext4_delete_inode, | |
1017 | .put_super = ext4_put_super, | |
617ba13b | 1018 | .sync_fs = ext4_sync_fs, |
c4be0c1d TS |
1019 | .freeze_fs = ext4_freeze, |
1020 | .unfreeze_fs = ext4_unfreeze, | |
617ba13b MC |
1021 | .statfs = ext4_statfs, |
1022 | .remount_fs = ext4_remount, | |
1023 | .clear_inode = ext4_clear_inode, | |
1024 | .show_options = ext4_show_options, | |
ac27a0ec | 1025 | #ifdef CONFIG_QUOTA |
617ba13b MC |
1026 | .quota_read = ext4_quota_read, |
1027 | .quota_write = ext4_quota_write, | |
ac27a0ec | 1028 | #endif |
c39a7f84 | 1029 | .bdev_try_to_free_page = bdev_try_to_free_page, |
ac27a0ec DK |
1030 | }; |
1031 | ||
9ca92389 TT |
1032 | static const struct super_operations ext4_nojournal_sops = { |
1033 | .alloc_inode = ext4_alloc_inode, | |
1034 | .destroy_inode = ext4_destroy_inode, | |
1035 | .write_inode = ext4_write_inode, | |
1036 | .dirty_inode = ext4_dirty_inode, | |
1037 | .delete_inode = ext4_delete_inode, | |
1038 | .write_super = ext4_write_super, | |
1039 | .put_super = ext4_put_super, | |
1040 | .statfs = ext4_statfs, | |
1041 | .remount_fs = ext4_remount, | |
1042 | .clear_inode = ext4_clear_inode, | |
1043 | .show_options = ext4_show_options, | |
1044 | #ifdef CONFIG_QUOTA | |
1045 | .quota_read = ext4_quota_read, | |
1046 | .quota_write = ext4_quota_write, | |
1047 | #endif | |
1048 | .bdev_try_to_free_page = bdev_try_to_free_page, | |
1049 | }; | |
1050 | ||
39655164 | 1051 | static const struct export_operations ext4_export_ops = { |
1b961ac0 CH |
1052 | .fh_to_dentry = ext4_fh_to_dentry, |
1053 | .fh_to_parent = ext4_fh_to_parent, | |
617ba13b | 1054 | .get_parent = ext4_get_parent, |
ac27a0ec DK |
1055 | }; |
1056 | ||
1057 | enum { | |
1058 | Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid, | |
1059 | Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro, | |
01436ef2 | 1060 | Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov, |
ac27a0ec | 1061 | Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, |
06705bff | 1062 | Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh, |
30773840 | 1063 | Opt_commit, Opt_min_batch_time, Opt_max_batch_time, |
c3191067 | 1064 | Opt_journal_update, Opt_journal_dev, |
818d276c | 1065 | Opt_journal_checksum, Opt_journal_async_commit, |
ac27a0ec | 1066 | Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback, |
f4033903 | 1067 | Opt_data_err_abort, Opt_data_err_ignore, Opt_mb_history_length, |
ac27a0ec DK |
1068 | Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota, |
1069 | Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota, | |
06705bff TT |
1070 | Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize, |
1071 | Opt_usrquota, Opt_grpquota, Opt_i_version, | |
01436ef2 | 1072 | Opt_stripe, Opt_delalloc, Opt_nodelalloc, |
6fd058f7 | 1073 | Opt_block_validity, Opt_noblock_validity, |
b3881f74 | 1074 | Opt_inode_readahead_blks, Opt_journal_ioprio |
ac27a0ec DK |
1075 | }; |
1076 | ||
a447c093 | 1077 | static const match_table_t tokens = { |
ac27a0ec DK |
1078 | {Opt_bsd_df, "bsddf"}, |
1079 | {Opt_minix_df, "minixdf"}, | |
1080 | {Opt_grpid, "grpid"}, | |
1081 | {Opt_grpid, "bsdgroups"}, | |
1082 | {Opt_nogrpid, "nogrpid"}, | |
1083 | {Opt_nogrpid, "sysvgroups"}, | |
1084 | {Opt_resgid, "resgid=%u"}, | |
1085 | {Opt_resuid, "resuid=%u"}, | |
1086 | {Opt_sb, "sb=%u"}, | |
1087 | {Opt_err_cont, "errors=continue"}, | |
1088 | {Opt_err_panic, "errors=panic"}, | |
1089 | {Opt_err_ro, "errors=remount-ro"}, | |
1090 | {Opt_nouid32, "nouid32"}, | |
ac27a0ec DK |
1091 | {Opt_debug, "debug"}, |
1092 | {Opt_oldalloc, "oldalloc"}, | |
1093 | {Opt_orlov, "orlov"}, | |
1094 | {Opt_user_xattr, "user_xattr"}, | |
1095 | {Opt_nouser_xattr, "nouser_xattr"}, | |
1096 | {Opt_acl, "acl"}, | |
1097 | {Opt_noacl, "noacl"}, | |
ac27a0ec DK |
1098 | {Opt_noload, "noload"}, |
1099 | {Opt_nobh, "nobh"}, | |
1100 | {Opt_bh, "bh"}, | |
1101 | {Opt_commit, "commit=%u"}, | |
30773840 TT |
1102 | {Opt_min_batch_time, "min_batch_time=%u"}, |
1103 | {Opt_max_batch_time, "max_batch_time=%u"}, | |
ac27a0ec | 1104 | {Opt_journal_update, "journal=update"}, |
ac27a0ec | 1105 | {Opt_journal_dev, "journal_dev=%u"}, |
818d276c GS |
1106 | {Opt_journal_checksum, "journal_checksum"}, |
1107 | {Opt_journal_async_commit, "journal_async_commit"}, | |
ac27a0ec DK |
1108 | {Opt_abort, "abort"}, |
1109 | {Opt_data_journal, "data=journal"}, | |
1110 | {Opt_data_ordered, "data=ordered"}, | |
1111 | {Opt_data_writeback, "data=writeback"}, | |
5bf5683a HK |
1112 | {Opt_data_err_abort, "data_err=abort"}, |
1113 | {Opt_data_err_ignore, "data_err=ignore"}, | |
f4033903 | 1114 | {Opt_mb_history_length, "mb_history_length=%u"}, |
ac27a0ec DK |
1115 | {Opt_offusrjquota, "usrjquota="}, |
1116 | {Opt_usrjquota, "usrjquota=%s"}, | |
1117 | {Opt_offgrpjquota, "grpjquota="}, | |
1118 | {Opt_grpjquota, "grpjquota=%s"}, | |
1119 | {Opt_jqfmt_vfsold, "jqfmt=vfsold"}, | |
1120 | {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"}, | |
1121 | {Opt_grpquota, "grpquota"}, | |
1122 | {Opt_noquota, "noquota"}, | |
1123 | {Opt_quota, "quota"}, | |
1124 | {Opt_usrquota, "usrquota"}, | |
1125 | {Opt_barrier, "barrier=%u"}, | |
06705bff TT |
1126 | {Opt_barrier, "barrier"}, |
1127 | {Opt_nobarrier, "nobarrier"}, | |
25ec56b5 | 1128 | {Opt_i_version, "i_version"}, |
c9de560d | 1129 | {Opt_stripe, "stripe=%u"}, |
ac27a0ec | 1130 | {Opt_resize, "resize"}, |
64769240 | 1131 | {Opt_delalloc, "delalloc"}, |
dd919b98 | 1132 | {Opt_nodelalloc, "nodelalloc"}, |
6fd058f7 TT |
1133 | {Opt_block_validity, "block_validity"}, |
1134 | {Opt_noblock_validity, "noblock_validity"}, | |
240799cd | 1135 | {Opt_inode_readahead_blks, "inode_readahead_blks=%u"}, |
b3881f74 | 1136 | {Opt_journal_ioprio, "journal_ioprio=%u"}, |
afd4672d | 1137 | {Opt_auto_da_alloc, "auto_da_alloc=%u"}, |
06705bff TT |
1138 | {Opt_auto_da_alloc, "auto_da_alloc"}, |
1139 | {Opt_noauto_da_alloc, "noauto_da_alloc"}, | |
f3f12faa | 1140 | {Opt_err, NULL}, |
ac27a0ec DK |
1141 | }; |
1142 | ||
617ba13b | 1143 | static ext4_fsblk_t get_sb_block(void **data) |
ac27a0ec | 1144 | { |
617ba13b | 1145 | ext4_fsblk_t sb_block; |
ac27a0ec DK |
1146 | char *options = (char *) *data; |
1147 | ||
1148 | if (!options || strncmp(options, "sb=", 3) != 0) | |
1149 | return 1; /* Default location */ | |
0b8e58a1 | 1150 | |
ac27a0ec | 1151 | options += 3; |
0b8e58a1 | 1152 | /* TODO: use simple_strtoll with >32bit ext4 */ |
ac27a0ec DK |
1153 | sb_block = simple_strtoul(options, &options, 0); |
1154 | if (*options && *options != ',') { | |
4776004f | 1155 | printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n", |
ac27a0ec DK |
1156 | (char *) *data); |
1157 | return 1; | |
1158 | } | |
1159 | if (*options == ',') | |
1160 | options++; | |
1161 | *data = (void *) options; | |
0b8e58a1 | 1162 | |
ac27a0ec DK |
1163 | return sb_block; |
1164 | } | |
1165 | ||
b3881f74 TT |
1166 | #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) |
1167 | ||
2b2d6d01 | 1168 | static int parse_options(char *options, struct super_block *sb, |
c3191067 | 1169 | unsigned long *journal_devnum, |
b3881f74 | 1170 | unsigned int *journal_ioprio, |
2b2d6d01 | 1171 | ext4_fsblk_t *n_blocks_count, int is_remount) |
ac27a0ec | 1172 | { |
617ba13b | 1173 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
2b2d6d01 | 1174 | char *p; |
ac27a0ec DK |
1175 | substring_t args[MAX_OPT_ARGS]; |
1176 | int data_opt = 0; | |
1177 | int option; | |
1178 | #ifdef CONFIG_QUOTA | |
dfc5d03f | 1179 | int qtype, qfmt; |
ac27a0ec DK |
1180 | char *qname; |
1181 | #endif | |
1182 | ||
1183 | if (!options) | |
1184 | return 1; | |
1185 | ||
2b2d6d01 | 1186 | while ((p = strsep(&options, ",")) != NULL) { |
ac27a0ec DK |
1187 | int token; |
1188 | if (!*p) | |
1189 | continue; | |
1190 | ||
1191 | token = match_token(p, tokens, args); | |
1192 | switch (token) { | |
1193 | case Opt_bsd_df: | |
2b2d6d01 | 1194 | clear_opt(sbi->s_mount_opt, MINIX_DF); |
ac27a0ec DK |
1195 | break; |
1196 | case Opt_minix_df: | |
2b2d6d01 | 1197 | set_opt(sbi->s_mount_opt, MINIX_DF); |
ac27a0ec DK |
1198 | break; |
1199 | case Opt_grpid: | |
2b2d6d01 | 1200 | set_opt(sbi->s_mount_opt, GRPID); |
ac27a0ec DK |
1201 | break; |
1202 | case Opt_nogrpid: | |
2b2d6d01 | 1203 | clear_opt(sbi->s_mount_opt, GRPID); |
ac27a0ec DK |
1204 | break; |
1205 | case Opt_resuid: | |
1206 | if (match_int(&args[0], &option)) | |
1207 | return 0; | |
1208 | sbi->s_resuid = option; | |
1209 | break; | |
1210 | case Opt_resgid: | |
1211 | if (match_int(&args[0], &option)) | |
1212 | return 0; | |
1213 | sbi->s_resgid = option; | |
1214 | break; | |
1215 | case Opt_sb: | |
1216 | /* handled by get_sb_block() instead of here */ | |
1217 | /* *sb_block = match_int(&args[0]); */ | |
1218 | break; | |
1219 | case Opt_err_panic: | |
2b2d6d01 TT |
1220 | clear_opt(sbi->s_mount_opt, ERRORS_CONT); |
1221 | clear_opt(sbi->s_mount_opt, ERRORS_RO); | |
1222 | set_opt(sbi->s_mount_opt, ERRORS_PANIC); | |
ac27a0ec DK |
1223 | break; |
1224 | case Opt_err_ro: | |
2b2d6d01 TT |
1225 | clear_opt(sbi->s_mount_opt, ERRORS_CONT); |
1226 | clear_opt(sbi->s_mount_opt, ERRORS_PANIC); | |
1227 | set_opt(sbi->s_mount_opt, ERRORS_RO); | |
ac27a0ec DK |
1228 | break; |
1229 | case Opt_err_cont: | |
2b2d6d01 TT |
1230 | clear_opt(sbi->s_mount_opt, ERRORS_RO); |
1231 | clear_opt(sbi->s_mount_opt, ERRORS_PANIC); | |
1232 | set_opt(sbi->s_mount_opt, ERRORS_CONT); | |
ac27a0ec DK |
1233 | break; |
1234 | case Opt_nouid32: | |
2b2d6d01 | 1235 | set_opt(sbi->s_mount_opt, NO_UID32); |
ac27a0ec | 1236 | break; |
ac27a0ec | 1237 | case Opt_debug: |
2b2d6d01 | 1238 | set_opt(sbi->s_mount_opt, DEBUG); |
ac27a0ec DK |
1239 | break; |
1240 | case Opt_oldalloc: | |
2b2d6d01 | 1241 | set_opt(sbi->s_mount_opt, OLDALLOC); |
ac27a0ec DK |
1242 | break; |
1243 | case Opt_orlov: | |
2b2d6d01 | 1244 | clear_opt(sbi->s_mount_opt, OLDALLOC); |
ac27a0ec | 1245 | break; |
03010a33 | 1246 | #ifdef CONFIG_EXT4_FS_XATTR |
ac27a0ec | 1247 | case Opt_user_xattr: |
2b2d6d01 | 1248 | set_opt(sbi->s_mount_opt, XATTR_USER); |
ac27a0ec DK |
1249 | break; |
1250 | case Opt_nouser_xattr: | |
2b2d6d01 | 1251 | clear_opt(sbi->s_mount_opt, XATTR_USER); |
ac27a0ec DK |
1252 | break; |
1253 | #else | |
1254 | case Opt_user_xattr: | |
1255 | case Opt_nouser_xattr: | |
b31e1552 | 1256 | ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported"); |
ac27a0ec DK |
1257 | break; |
1258 | #endif | |
03010a33 | 1259 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
ac27a0ec DK |
1260 | case Opt_acl: |
1261 | set_opt(sbi->s_mount_opt, POSIX_ACL); | |
1262 | break; | |
1263 | case Opt_noacl: | |
1264 | clear_opt(sbi->s_mount_opt, POSIX_ACL); | |
1265 | break; | |
1266 | #else | |
1267 | case Opt_acl: | |
1268 | case Opt_noacl: | |
b31e1552 | 1269 | ext4_msg(sb, KERN_ERR, "(no)acl options not supported"); |
ac27a0ec DK |
1270 | break; |
1271 | #endif | |
ac27a0ec DK |
1272 | case Opt_journal_update: |
1273 | /* @@@ FIXME */ | |
1274 | /* Eventually we will want to be able to create | |
1275 | a journal file here. For now, only allow the | |
1276 | user to specify an existing inode to be the | |
1277 | journal file. */ | |
1278 | if (is_remount) { | |
b31e1552 ES |
1279 | ext4_msg(sb, KERN_ERR, |
1280 | "Cannot specify journal on remount"); | |
ac27a0ec DK |
1281 | return 0; |
1282 | } | |
2b2d6d01 | 1283 | set_opt(sbi->s_mount_opt, UPDATE_JOURNAL); |
ac27a0ec | 1284 | break; |
ac27a0ec DK |
1285 | case Opt_journal_dev: |
1286 | if (is_remount) { | |
b31e1552 ES |
1287 | ext4_msg(sb, KERN_ERR, |
1288 | "Cannot specify journal on remount"); | |
ac27a0ec DK |
1289 | return 0; |
1290 | } | |
1291 | if (match_int(&args[0], &option)) | |
1292 | return 0; | |
1293 | *journal_devnum = option; | |
1294 | break; | |
818d276c GS |
1295 | case Opt_journal_checksum: |
1296 | set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM); | |
1297 | break; | |
1298 | case Opt_journal_async_commit: | |
1299 | set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT); | |
1300 | set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM); | |
1301 | break; | |
ac27a0ec | 1302 | case Opt_noload: |
2b2d6d01 | 1303 | set_opt(sbi->s_mount_opt, NOLOAD); |
ac27a0ec DK |
1304 | break; |
1305 | case Opt_commit: | |
1306 | if (match_int(&args[0], &option)) | |
1307 | return 0; | |
1308 | if (option < 0) | |
1309 | return 0; | |
1310 | if (option == 0) | |
cd02ff0b | 1311 | option = JBD2_DEFAULT_MAX_COMMIT_AGE; |
ac27a0ec DK |
1312 | sbi->s_commit_interval = HZ * option; |
1313 | break; | |
30773840 TT |
1314 | case Opt_max_batch_time: |
1315 | if (match_int(&args[0], &option)) | |
1316 | return 0; | |
1317 | if (option < 0) | |
1318 | return 0; | |
1319 | if (option == 0) | |
1320 | option = EXT4_DEF_MAX_BATCH_TIME; | |
1321 | sbi->s_max_batch_time = option; | |
1322 | break; | |
1323 | case Opt_min_batch_time: | |
1324 | if (match_int(&args[0], &option)) | |
1325 | return 0; | |
1326 | if (option < 0) | |
1327 | return 0; | |
1328 | sbi->s_min_batch_time = option; | |
1329 | break; | |
ac27a0ec | 1330 | case Opt_data_journal: |
617ba13b | 1331 | data_opt = EXT4_MOUNT_JOURNAL_DATA; |
ac27a0ec DK |
1332 | goto datacheck; |
1333 | case Opt_data_ordered: | |
617ba13b | 1334 | data_opt = EXT4_MOUNT_ORDERED_DATA; |
ac27a0ec DK |
1335 | goto datacheck; |
1336 | case Opt_data_writeback: | |
617ba13b | 1337 | data_opt = EXT4_MOUNT_WRITEBACK_DATA; |
ac27a0ec DK |
1338 | datacheck: |
1339 | if (is_remount) { | |
617ba13b | 1340 | if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS) |
ac27a0ec | 1341 | != data_opt) { |
b31e1552 ES |
1342 | ext4_msg(sb, KERN_ERR, |
1343 | "Cannot change data mode on remount"); | |
ac27a0ec DK |
1344 | return 0; |
1345 | } | |
1346 | } else { | |
617ba13b | 1347 | sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS; |
ac27a0ec DK |
1348 | sbi->s_mount_opt |= data_opt; |
1349 | } | |
1350 | break; | |
5bf5683a HK |
1351 | case Opt_data_err_abort: |
1352 | set_opt(sbi->s_mount_opt, DATA_ERR_ABORT); | |
1353 | break; | |
1354 | case Opt_data_err_ignore: | |
1355 | clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT); | |
1356 | break; | |
f4033903 CW |
1357 | case Opt_mb_history_length: |
1358 | if (match_int(&args[0], &option)) | |
1359 | return 0; | |
1360 | if (option < 0) | |
1361 | return 0; | |
1362 | sbi->s_mb_history_max = option; | |
1363 | break; | |
ac27a0ec DK |
1364 | #ifdef CONFIG_QUOTA |
1365 | case Opt_usrjquota: | |
1366 | qtype = USRQUOTA; | |
1367 | goto set_qf_name; | |
1368 | case Opt_grpjquota: | |
1369 | qtype = GRPQUOTA; | |
1370 | set_qf_name: | |
17bd13b3 | 1371 | if (sb_any_quota_loaded(sb) && |
dfc5d03f | 1372 | !sbi->s_qf_names[qtype]) { |
b31e1552 ES |
1373 | ext4_msg(sb, KERN_ERR, |
1374 | "Cannot change journaled " | |
1375 | "quota options when quota turned on"); | |
ac27a0ec DK |
1376 | return 0; |
1377 | } | |
1378 | qname = match_strdup(&args[0]); | |
1379 | if (!qname) { | |
b31e1552 ES |
1380 | ext4_msg(sb, KERN_ERR, |
1381 | "Not enough memory for " | |
1382 | "storing quotafile name"); | |
ac27a0ec DK |
1383 | return 0; |
1384 | } | |
1385 | if (sbi->s_qf_names[qtype] && | |
1386 | strcmp(sbi->s_qf_names[qtype], qname)) { | |
b31e1552 ES |
1387 | ext4_msg(sb, KERN_ERR, |
1388 | "%s quota file already " | |
1389 | "specified", QTYPE2NAME(qtype)); | |
ac27a0ec DK |
1390 | kfree(qname); |
1391 | return 0; | |
1392 | } | |
1393 | sbi->s_qf_names[qtype] = qname; | |
1394 | if (strchr(sbi->s_qf_names[qtype], '/')) { | |
b31e1552 ES |
1395 | ext4_msg(sb, KERN_ERR, |
1396 | "quotafile must be on " | |
1397 | "filesystem root"); | |
ac27a0ec DK |
1398 | kfree(sbi->s_qf_names[qtype]); |
1399 | sbi->s_qf_names[qtype] = NULL; | |
1400 | return 0; | |
1401 | } | |
1402 | set_opt(sbi->s_mount_opt, QUOTA); | |
1403 | break; | |
1404 | case Opt_offusrjquota: | |
1405 | qtype = USRQUOTA; | |
1406 | goto clear_qf_name; | |
1407 | case Opt_offgrpjquota: | |
1408 | qtype = GRPQUOTA; | |
1409 | clear_qf_name: | |
17bd13b3 | 1410 | if (sb_any_quota_loaded(sb) && |
dfc5d03f | 1411 | sbi->s_qf_names[qtype]) { |
b31e1552 | 1412 | ext4_msg(sb, KERN_ERR, "Cannot change " |
2c8be6b2 | 1413 | "journaled quota options when " |
b31e1552 | 1414 | "quota turned on"); |
ac27a0ec DK |
1415 | return 0; |
1416 | } | |
1417 | /* | |
1418 | * The space will be released later when all options | |
1419 | * are confirmed to be correct | |
1420 | */ | |
1421 | sbi->s_qf_names[qtype] = NULL; | |
1422 | break; | |
1423 | case Opt_jqfmt_vfsold: | |
dfc5d03f JK |
1424 | qfmt = QFMT_VFS_OLD; |
1425 | goto set_qf_format; | |
ac27a0ec | 1426 | case Opt_jqfmt_vfsv0: |
dfc5d03f JK |
1427 | qfmt = QFMT_VFS_V0; |
1428 | set_qf_format: | |
17bd13b3 | 1429 | if (sb_any_quota_loaded(sb) && |
dfc5d03f | 1430 | sbi->s_jquota_fmt != qfmt) { |
b31e1552 | 1431 | ext4_msg(sb, KERN_ERR, "Cannot change " |
dfc5d03f | 1432 | "journaled quota options when " |
b31e1552 | 1433 | "quota turned on"); |
dfc5d03f JK |
1434 | return 0; |
1435 | } | |
1436 | sbi->s_jquota_fmt = qfmt; | |
ac27a0ec DK |
1437 | break; |
1438 | case Opt_quota: | |
1439 | case Opt_usrquota: | |
1440 | set_opt(sbi->s_mount_opt, QUOTA); | |
1441 | set_opt(sbi->s_mount_opt, USRQUOTA); | |
1442 | break; | |
1443 | case Opt_grpquota: | |
1444 | set_opt(sbi->s_mount_opt, QUOTA); | |
1445 | set_opt(sbi->s_mount_opt, GRPQUOTA); | |
1446 | break; | |
1447 | case Opt_noquota: | |
17bd13b3 | 1448 | if (sb_any_quota_loaded(sb)) { |
b31e1552 ES |
1449 | ext4_msg(sb, KERN_ERR, "Cannot change quota " |
1450 | "options when quota turned on"); | |
ac27a0ec DK |
1451 | return 0; |
1452 | } | |
1453 | clear_opt(sbi->s_mount_opt, QUOTA); | |
1454 | clear_opt(sbi->s_mount_opt, USRQUOTA); | |
1455 | clear_opt(sbi->s_mount_opt, GRPQUOTA); | |
1456 | break; | |
1457 | #else | |
1458 | case Opt_quota: | |
1459 | case Opt_usrquota: | |
1460 | case Opt_grpquota: | |
b31e1552 ES |
1461 | ext4_msg(sb, KERN_ERR, |
1462 | "quota options not supported"); | |
cd59e7b9 | 1463 | break; |
ac27a0ec DK |
1464 | case Opt_usrjquota: |
1465 | case Opt_grpjquota: | |
1466 | case Opt_offusrjquota: | |
1467 | case Opt_offgrpjquota: | |
1468 | case Opt_jqfmt_vfsold: | |
1469 | case Opt_jqfmt_vfsv0: | |
b31e1552 ES |
1470 | ext4_msg(sb, KERN_ERR, |
1471 | "journaled quota options not supported"); | |
ac27a0ec DK |
1472 | break; |
1473 | case Opt_noquota: | |
1474 | break; | |
1475 | #endif | |
1476 | case Opt_abort: | |
1477 | set_opt(sbi->s_mount_opt, ABORT); | |
1478 | break; | |
06705bff TT |
1479 | case Opt_nobarrier: |
1480 | clear_opt(sbi->s_mount_opt, BARRIER); | |
1481 | break; | |
ac27a0ec | 1482 | case Opt_barrier: |
06705bff TT |
1483 | if (match_int(&args[0], &option)) { |
1484 | set_opt(sbi->s_mount_opt, BARRIER); | |
1485 | break; | |
1486 | } | |
ac27a0ec DK |
1487 | if (option) |
1488 | set_opt(sbi->s_mount_opt, BARRIER); | |
1489 | else | |
1490 | clear_opt(sbi->s_mount_opt, BARRIER); | |
1491 | break; | |
1492 | case Opt_ignore: | |
1493 | break; | |
1494 | case Opt_resize: | |
1495 | if (!is_remount) { | |
b31e1552 ES |
1496 | ext4_msg(sb, KERN_ERR, |
1497 | "resize option only available " | |
1498 | "for remount"); | |
ac27a0ec DK |
1499 | return 0; |
1500 | } | |
1501 | if (match_int(&args[0], &option) != 0) | |
1502 | return 0; | |
1503 | *n_blocks_count = option; | |
1504 | break; | |
1505 | case Opt_nobh: | |
1506 | set_opt(sbi->s_mount_opt, NOBH); | |
1507 | break; | |
1508 | case Opt_bh: | |
1509 | clear_opt(sbi->s_mount_opt, NOBH); | |
1510 | break; | |
25ec56b5 JNC |
1511 | case Opt_i_version: |
1512 | set_opt(sbi->s_mount_opt, I_VERSION); | |
1513 | sb->s_flags |= MS_I_VERSION; | |
1514 | break; | |
dd919b98 AK |
1515 | case Opt_nodelalloc: |
1516 | clear_opt(sbi->s_mount_opt, DELALLOC); | |
1517 | break; | |
c9de560d AT |
1518 | case Opt_stripe: |
1519 | if (match_int(&args[0], &option)) | |
1520 | return 0; | |
1521 | if (option < 0) | |
1522 | return 0; | |
1523 | sbi->s_stripe = option; | |
1524 | break; | |
64769240 AT |
1525 | case Opt_delalloc: |
1526 | set_opt(sbi->s_mount_opt, DELALLOC); | |
1527 | break; | |
6fd058f7 TT |
1528 | case Opt_block_validity: |
1529 | set_opt(sbi->s_mount_opt, BLOCK_VALIDITY); | |
1530 | break; | |
1531 | case Opt_noblock_validity: | |
1532 | clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY); | |
1533 | break; | |
240799cd TT |
1534 | case Opt_inode_readahead_blks: |
1535 | if (match_int(&args[0], &option)) | |
1536 | return 0; | |
1537 | if (option < 0 || option > (1 << 30)) | |
1538 | return 0; | |
f7c43950 | 1539 | if (!is_power_of_2(option)) { |
b31e1552 ES |
1540 | ext4_msg(sb, KERN_ERR, |
1541 | "EXT4-fs: inode_readahead_blks" | |
1542 | " must be a power of 2"); | |
3197ebdb TT |
1543 | return 0; |
1544 | } | |
240799cd TT |
1545 | sbi->s_inode_readahead_blks = option; |
1546 | break; | |
b3881f74 TT |
1547 | case Opt_journal_ioprio: |
1548 | if (match_int(&args[0], &option)) | |
1549 | return 0; | |
1550 | if (option < 0 || option > 7) | |
1551 | break; | |
1552 | *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, | |
1553 | option); | |
1554 | break; | |
06705bff TT |
1555 | case Opt_noauto_da_alloc: |
1556 | set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC); | |
1557 | break; | |
afd4672d | 1558 | case Opt_auto_da_alloc: |
06705bff TT |
1559 | if (match_int(&args[0], &option)) { |
1560 | clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC); | |
1561 | break; | |
1562 | } | |
afd4672d TT |
1563 | if (option) |
1564 | clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC); | |
1565 | else | |
1566 | set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC); | |
1567 | break; | |
ac27a0ec | 1568 | default: |
b31e1552 ES |
1569 | ext4_msg(sb, KERN_ERR, |
1570 | "Unrecognized mount option \"%s\" " | |
1571 | "or missing value", p); | |
ac27a0ec DK |
1572 | return 0; |
1573 | } | |
1574 | } | |
1575 | #ifdef CONFIG_QUOTA | |
1576 | if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) { | |
617ba13b | 1577 | if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) && |
ac27a0ec DK |
1578 | sbi->s_qf_names[USRQUOTA]) |
1579 | clear_opt(sbi->s_mount_opt, USRQUOTA); | |
1580 | ||
617ba13b | 1581 | if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) && |
ac27a0ec DK |
1582 | sbi->s_qf_names[GRPQUOTA]) |
1583 | clear_opt(sbi->s_mount_opt, GRPQUOTA); | |
1584 | ||
1585 | if ((sbi->s_qf_names[USRQUOTA] && | |
617ba13b | 1586 | (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) || |
ac27a0ec | 1587 | (sbi->s_qf_names[GRPQUOTA] && |
617ba13b | 1588 | (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) { |
b31e1552 ES |
1589 | ext4_msg(sb, KERN_ERR, "old and new quota " |
1590 | "format mixing"); | |
ac27a0ec DK |
1591 | return 0; |
1592 | } | |
1593 | ||
1594 | if (!sbi->s_jquota_fmt) { | |
b31e1552 ES |
1595 | ext4_msg(sb, KERN_ERR, "journaled quota format " |
1596 | "not specified"); | |
ac27a0ec DK |
1597 | return 0; |
1598 | } | |
1599 | } else { | |
1600 | if (sbi->s_jquota_fmt) { | |
b31e1552 | 1601 | ext4_msg(sb, KERN_ERR, "journaled quota format " |
2c8be6b2 | 1602 | "specified with no journaling " |
b31e1552 | 1603 | "enabled"); |
ac27a0ec DK |
1604 | return 0; |
1605 | } | |
1606 | } | |
1607 | #endif | |
1608 | return 1; | |
1609 | } | |
1610 | ||
617ba13b | 1611 | static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es, |
ac27a0ec DK |
1612 | int read_only) |
1613 | { | |
617ba13b | 1614 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
ac27a0ec DK |
1615 | int res = 0; |
1616 | ||
617ba13b | 1617 | if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) { |
b31e1552 ES |
1618 | ext4_msg(sb, KERN_ERR, "revision level too high, " |
1619 | "forcing read-only mode"); | |
ac27a0ec DK |
1620 | res = MS_RDONLY; |
1621 | } | |
1622 | if (read_only) | |
1623 | return res; | |
617ba13b | 1624 | if (!(sbi->s_mount_state & EXT4_VALID_FS)) |
b31e1552 ES |
1625 | ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, " |
1626 | "running e2fsck is recommended"); | |
617ba13b | 1627 | else if ((sbi->s_mount_state & EXT4_ERROR_FS)) |
b31e1552 ES |
1628 | ext4_msg(sb, KERN_WARNING, |
1629 | "warning: mounting fs with errors, " | |
1630 | "running e2fsck is recommended"); | |
ac27a0ec DK |
1631 | else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 && |
1632 | le16_to_cpu(es->s_mnt_count) >= | |
1633 | (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count)) | |
b31e1552 ES |
1634 | ext4_msg(sb, KERN_WARNING, |
1635 | "warning: maximal mount count reached, " | |
1636 | "running e2fsck is recommended"); | |
ac27a0ec DK |
1637 | else if (le32_to_cpu(es->s_checkinterval) && |
1638 | (le32_to_cpu(es->s_lastcheck) + | |
1639 | le32_to_cpu(es->s_checkinterval) <= get_seconds())) | |
b31e1552 ES |
1640 | ext4_msg(sb, KERN_WARNING, |
1641 | "warning: checktime reached, " | |
1642 | "running e2fsck is recommended"); | |
0b8e58a1 | 1643 | if (!sbi->s_journal) |
0390131b | 1644 | es->s_state &= cpu_to_le16(~EXT4_VALID_FS); |
ac27a0ec | 1645 | if (!(__s16) le16_to_cpu(es->s_max_mnt_count)) |
617ba13b | 1646 | es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT); |
e8546d06 | 1647 | le16_add_cpu(&es->s_mnt_count, 1); |
ac27a0ec | 1648 | es->s_mtime = cpu_to_le32(get_seconds()); |
617ba13b | 1649 | ext4_update_dynamic_rev(sb); |
0390131b FM |
1650 | if (sbi->s_journal) |
1651 | EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); | |
ac27a0ec | 1652 | |
e2d67052 | 1653 | ext4_commit_super(sb, 1); |
ac27a0ec | 1654 | if (test_opt(sb, DEBUG)) |
a9df9a49 | 1655 | printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, " |
ac27a0ec DK |
1656 | "bpg=%lu, ipg=%lu, mo=%04lx]\n", |
1657 | sb->s_blocksize, | |
1658 | sbi->s_groups_count, | |
617ba13b MC |
1659 | EXT4_BLOCKS_PER_GROUP(sb), |
1660 | EXT4_INODES_PER_GROUP(sb), | |
ac27a0ec DK |
1661 | sbi->s_mount_opt); |
1662 | ||
0390131b | 1663 | if (EXT4_SB(sb)->s_journal) { |
b31e1552 ES |
1664 | ext4_msg(sb, KERN_INFO, "%s journal on %s", |
1665 | EXT4_SB(sb)->s_journal->j_inode ? "internal" : | |
0390131b FM |
1666 | "external", EXT4_SB(sb)->s_journal->j_devname); |
1667 | } else { | |
b31e1552 | 1668 | ext4_msg(sb, KERN_INFO, "no journal"); |
0390131b | 1669 | } |
ac27a0ec DK |
1670 | return res; |
1671 | } | |
1672 | ||
772cb7c8 JS |
1673 | static int ext4_fill_flex_info(struct super_block *sb) |
1674 | { | |
1675 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
1676 | struct ext4_group_desc *gdp = NULL; | |
772cb7c8 JS |
1677 | ext4_group_t flex_group_count; |
1678 | ext4_group_t flex_group; | |
1679 | int groups_per_flex = 0; | |
c5ca7c76 | 1680 | size_t size; |
772cb7c8 JS |
1681 | int i; |
1682 | ||
1683 | if (!sbi->s_es->s_log_groups_per_flex) { | |
1684 | sbi->s_log_groups_per_flex = 0; | |
1685 | return 1; | |
1686 | } | |
1687 | ||
1688 | sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex; | |
1689 | groups_per_flex = 1 << sbi->s_log_groups_per_flex; | |
1690 | ||
c62a11fd FB |
1691 | /* We allocate both existing and potentially added groups */ |
1692 | flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) + | |
d94e99a6 AK |
1693 | ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) << |
1694 | EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex; | |
c5ca7c76 TT |
1695 | size = flex_group_count * sizeof(struct flex_groups); |
1696 | sbi->s_flex_groups = kzalloc(size, GFP_KERNEL); | |
1697 | if (sbi->s_flex_groups == NULL) { | |
1698 | sbi->s_flex_groups = vmalloc(size); | |
1699 | if (sbi->s_flex_groups) | |
1700 | memset(sbi->s_flex_groups, 0, size); | |
1701 | } | |
772cb7c8 | 1702 | if (sbi->s_flex_groups == NULL) { |
b31e1552 ES |
1703 | ext4_msg(sb, KERN_ERR, "not enough memory for " |
1704 | "%u flex groups", flex_group_count); | |
772cb7c8 JS |
1705 | goto failed; |
1706 | } | |
772cb7c8 | 1707 | |
772cb7c8 | 1708 | for (i = 0; i < sbi->s_groups_count; i++) { |
88b6edd1 | 1709 | gdp = ext4_get_group_desc(sb, i, NULL); |
772cb7c8 JS |
1710 | |
1711 | flex_group = ext4_flex_group(sbi, i); | |
9f24e420 TT |
1712 | atomic_set(&sbi->s_flex_groups[flex_group].free_inodes, |
1713 | ext4_free_inodes_count(sb, gdp)); | |
1714 | atomic_set(&sbi->s_flex_groups[flex_group].free_blocks, | |
1715 | ext4_free_blks_count(sb, gdp)); | |
7d39db14 TT |
1716 | atomic_set(&sbi->s_flex_groups[flex_group].used_dirs, |
1717 | ext4_used_dirs_count(sb, gdp)); | |
772cb7c8 JS |
1718 | } |
1719 | ||
1720 | return 1; | |
1721 | failed: | |
1722 | return 0; | |
1723 | } | |
1724 | ||
717d50e4 AD |
1725 | __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group, |
1726 | struct ext4_group_desc *gdp) | |
1727 | { | |
1728 | __u16 crc = 0; | |
1729 | ||
1730 | if (sbi->s_es->s_feature_ro_compat & | |
1731 | cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { | |
1732 | int offset = offsetof(struct ext4_group_desc, bg_checksum); | |
1733 | __le32 le_group = cpu_to_le32(block_group); | |
1734 | ||
1735 | crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid)); | |
1736 | crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group)); | |
1737 | crc = crc16(crc, (__u8 *)gdp, offset); | |
1738 | offset += sizeof(gdp->bg_checksum); /* skip checksum */ | |
1739 | /* for checksum of struct ext4_group_desc do the rest...*/ | |
1740 | if ((sbi->s_es->s_feature_incompat & | |
1741 | cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) && | |
1742 | offset < le16_to_cpu(sbi->s_es->s_desc_size)) | |
1743 | crc = crc16(crc, (__u8 *)gdp + offset, | |
1744 | le16_to_cpu(sbi->s_es->s_desc_size) - | |
1745 | offset); | |
1746 | } | |
1747 | ||
1748 | return cpu_to_le16(crc); | |
1749 | } | |
1750 | ||
1751 | int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group, | |
1752 | struct ext4_group_desc *gdp) | |
1753 | { | |
1754 | if ((sbi->s_es->s_feature_ro_compat & | |
1755 | cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) && | |
1756 | (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp))) | |
1757 | return 0; | |
1758 | ||
1759 | return 1; | |
1760 | } | |
1761 | ||
ac27a0ec | 1762 | /* Called at mount-time, super-block is locked */ |
197cd65a | 1763 | static int ext4_check_descriptors(struct super_block *sb) |
ac27a0ec | 1764 | { |
617ba13b MC |
1765 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
1766 | ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block); | |
1767 | ext4_fsblk_t last_block; | |
bd81d8ee LV |
1768 | ext4_fsblk_t block_bitmap; |
1769 | ext4_fsblk_t inode_bitmap; | |
1770 | ext4_fsblk_t inode_table; | |
ce421581 | 1771 | int flexbg_flag = 0; |
fd2d4291 | 1772 | ext4_group_t i; |
ac27a0ec | 1773 | |
ce421581 JS |
1774 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) |
1775 | flexbg_flag = 1; | |
1776 | ||
af5bc92d | 1777 | ext4_debug("Checking group descriptors"); |
ac27a0ec | 1778 | |
197cd65a AM |
1779 | for (i = 0; i < sbi->s_groups_count; i++) { |
1780 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); | |
1781 | ||
ce421581 | 1782 | if (i == sbi->s_groups_count - 1 || flexbg_flag) |
bd81d8ee | 1783 | last_block = ext4_blocks_count(sbi->s_es) - 1; |
ac27a0ec DK |
1784 | else |
1785 | last_block = first_block + | |
617ba13b | 1786 | (EXT4_BLOCKS_PER_GROUP(sb) - 1); |
ac27a0ec | 1787 | |
8fadc143 | 1788 | block_bitmap = ext4_block_bitmap(sb, gdp); |
2b2d6d01 | 1789 | if (block_bitmap < first_block || block_bitmap > last_block) { |
b31e1552 | 1790 | ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " |
a9df9a49 | 1791 | "Block bitmap for group %u not in group " |
b31e1552 | 1792 | "(block %llu)!", i, block_bitmap); |
ac27a0ec DK |
1793 | return 0; |
1794 | } | |
8fadc143 | 1795 | inode_bitmap = ext4_inode_bitmap(sb, gdp); |
2b2d6d01 | 1796 | if (inode_bitmap < first_block || inode_bitmap > last_block) { |
b31e1552 | 1797 | ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " |
a9df9a49 | 1798 | "Inode bitmap for group %u not in group " |
b31e1552 | 1799 | "(block %llu)!", i, inode_bitmap); |
ac27a0ec DK |
1800 | return 0; |
1801 | } | |
8fadc143 | 1802 | inode_table = ext4_inode_table(sb, gdp); |
bd81d8ee | 1803 | if (inode_table < first_block || |
2b2d6d01 | 1804 | inode_table + sbi->s_itb_per_group - 1 > last_block) { |
b31e1552 | 1805 | ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " |
a9df9a49 | 1806 | "Inode table for group %u not in group " |
b31e1552 | 1807 | "(block %llu)!", i, inode_table); |
ac27a0ec DK |
1808 | return 0; |
1809 | } | |
955ce5f5 | 1810 | ext4_lock_group(sb, i); |
717d50e4 | 1811 | if (!ext4_group_desc_csum_verify(sbi, i, gdp)) { |
b31e1552 ES |
1812 | ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " |
1813 | "Checksum for group %u failed (%u!=%u)", | |
1814 | i, le16_to_cpu(ext4_group_desc_csum(sbi, i, | |
1815 | gdp)), le16_to_cpu(gdp->bg_checksum)); | |
7ee1ec4c | 1816 | if (!(sb->s_flags & MS_RDONLY)) { |
955ce5f5 | 1817 | ext4_unlock_group(sb, i); |
8a266467 | 1818 | return 0; |
7ee1ec4c | 1819 | } |
717d50e4 | 1820 | } |
955ce5f5 | 1821 | ext4_unlock_group(sb, i); |
ce421581 JS |
1822 | if (!flexbg_flag) |
1823 | first_block += EXT4_BLOCKS_PER_GROUP(sb); | |
ac27a0ec DK |
1824 | } |
1825 | ||
bd81d8ee | 1826 | ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb)); |
0b8e58a1 | 1827 | sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb)); |
ac27a0ec DK |
1828 | return 1; |
1829 | } | |
1830 | ||
617ba13b | 1831 | /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at |
ac27a0ec DK |
1832 | * the superblock) which were deleted from all directories, but held open by |
1833 | * a process at the time of a crash. We walk the list and try to delete these | |
1834 | * inodes at recovery time (only with a read-write filesystem). | |
1835 | * | |
1836 | * In order to keep the orphan inode chain consistent during traversal (in | |
1837 | * case of crash during recovery), we link each inode into the superblock | |
1838 | * orphan list_head and handle it the same way as an inode deletion during | |
1839 | * normal operation (which journals the operations for us). | |
1840 | * | |
1841 | * We only do an iget() and an iput() on each inode, which is very safe if we | |
1842 | * accidentally point at an in-use or already deleted inode. The worst that | |
1843 | * can happen in this case is that we get a "bit already cleared" message from | |
617ba13b | 1844 | * ext4_free_inode(). The only reason we would point at a wrong inode is if |
ac27a0ec DK |
1845 | * e2fsck was run on this filesystem, and it must have already done the orphan |
1846 | * inode cleanup for us, so we can safely abort without any further action. | |
1847 | */ | |
2b2d6d01 TT |
1848 | static void ext4_orphan_cleanup(struct super_block *sb, |
1849 | struct ext4_super_block *es) | |
ac27a0ec DK |
1850 | { |
1851 | unsigned int s_flags = sb->s_flags; | |
1852 | int nr_orphans = 0, nr_truncates = 0; | |
1853 | #ifdef CONFIG_QUOTA | |
1854 | int i; | |
1855 | #endif | |
1856 | if (!es->s_last_orphan) { | |
1857 | jbd_debug(4, "no orphan inodes to clean up\n"); | |
1858 | return; | |
1859 | } | |
1860 | ||
a8f48a95 | 1861 | if (bdev_read_only(sb->s_bdev)) { |
b31e1552 ES |
1862 | ext4_msg(sb, KERN_ERR, "write access " |
1863 | "unavailable, skipping orphan cleanup"); | |
a8f48a95 ES |
1864 | return; |
1865 | } | |
1866 | ||
617ba13b | 1867 | if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { |
ac27a0ec DK |
1868 | if (es->s_last_orphan) |
1869 | jbd_debug(1, "Errors on filesystem, " | |
1870 | "clearing orphan list.\n"); | |
1871 | es->s_last_orphan = 0; | |
1872 | jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); | |
1873 | return; | |
1874 | } | |
1875 | ||
1876 | if (s_flags & MS_RDONLY) { | |
b31e1552 | 1877 | ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs"); |
ac27a0ec DK |
1878 | sb->s_flags &= ~MS_RDONLY; |
1879 | } | |
1880 | #ifdef CONFIG_QUOTA | |
1881 | /* Needed for iput() to work correctly and not trash data */ | |
1882 | sb->s_flags |= MS_ACTIVE; | |
1883 | /* Turn on quotas so that they are updated correctly */ | |
1884 | for (i = 0; i < MAXQUOTAS; i++) { | |
617ba13b MC |
1885 | if (EXT4_SB(sb)->s_qf_names[i]) { |
1886 | int ret = ext4_quota_on_mount(sb, i); | |
ac27a0ec | 1887 | if (ret < 0) |
b31e1552 ES |
1888 | ext4_msg(sb, KERN_ERR, |
1889 | "Cannot turn on journaled " | |
1890 | "quota: error %d", ret); | |
ac27a0ec DK |
1891 | } |
1892 | } | |
1893 | #endif | |
1894 | ||
1895 | while (es->s_last_orphan) { | |
1896 | struct inode *inode; | |
1897 | ||
97bd42b9 JB |
1898 | inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); |
1899 | if (IS_ERR(inode)) { | |
ac27a0ec DK |
1900 | es->s_last_orphan = 0; |
1901 | break; | |
1902 | } | |
1903 | ||
617ba13b | 1904 | list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); |
a269eb18 | 1905 | vfs_dq_init(inode); |
ac27a0ec | 1906 | if (inode->i_nlink) { |
b31e1552 ES |
1907 | ext4_msg(sb, KERN_DEBUG, |
1908 | "%s: truncating inode %lu to %lld bytes", | |
46e665e9 | 1909 | __func__, inode->i_ino, inode->i_size); |
e5f8eab8 | 1910 | jbd_debug(2, "truncating inode %lu to %lld bytes\n", |
ac27a0ec | 1911 | inode->i_ino, inode->i_size); |
617ba13b | 1912 | ext4_truncate(inode); |
ac27a0ec DK |
1913 | nr_truncates++; |
1914 | } else { | |
b31e1552 ES |
1915 | ext4_msg(sb, KERN_DEBUG, |
1916 | "%s: deleting unreferenced inode %lu", | |
46e665e9 | 1917 | __func__, inode->i_ino); |
ac27a0ec DK |
1918 | jbd_debug(2, "deleting unreferenced inode %lu\n", |
1919 | inode->i_ino); | |
1920 | nr_orphans++; | |
1921 | } | |
1922 | iput(inode); /* The delete magic happens here! */ | |
1923 | } | |
1924 | ||
2b2d6d01 | 1925 | #define PLURAL(x) (x), ((x) == 1) ? "" : "s" |
ac27a0ec DK |
1926 | |
1927 | if (nr_orphans) | |
b31e1552 ES |
1928 | ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", |
1929 | PLURAL(nr_orphans)); | |
ac27a0ec | 1930 | if (nr_truncates) |
b31e1552 ES |
1931 | ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up", |
1932 | PLURAL(nr_truncates)); | |
ac27a0ec DK |
1933 | #ifdef CONFIG_QUOTA |
1934 | /* Turn quotas off */ | |
1935 | for (i = 0; i < MAXQUOTAS; i++) { | |
1936 | if (sb_dqopt(sb)->files[i]) | |
6f28e087 | 1937 | vfs_quota_off(sb, i, 0); |
ac27a0ec DK |
1938 | } |
1939 | #endif | |
1940 | sb->s_flags = s_flags; /* Restore MS_RDONLY status */ | |
1941 | } | |
0b8e58a1 | 1942 | |
cd2291a4 ES |
1943 | /* |
1944 | * Maximal extent format file size. | |
1945 | * Resulting logical blkno at s_maxbytes must fit in our on-disk | |
1946 | * extent format containers, within a sector_t, and within i_blocks | |
1947 | * in the vfs. ext4 inode has 48 bits of i_block in fsblock units, | |
1948 | * so that won't be a limiting factor. | |
1949 | * | |
1950 | * Note, this does *not* consider any metadata overhead for vfs i_blocks. | |
1951 | */ | |
f287a1a5 | 1952 | static loff_t ext4_max_size(int blkbits, int has_huge_files) |
cd2291a4 ES |
1953 | { |
1954 | loff_t res; | |
1955 | loff_t upper_limit = MAX_LFS_FILESIZE; | |
1956 | ||
1957 | /* small i_blocks in vfs inode? */ | |
f287a1a5 | 1958 | if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) { |
cd2291a4 | 1959 | /* |
b3a6ffe1 | 1960 | * CONFIG_LBD is not enabled implies the inode |
cd2291a4 ES |
1961 | * i_block represent total blocks in 512 bytes |
1962 | * 32 == size of vfs inode i_blocks * 8 | |
1963 | */ | |
1964 | upper_limit = (1LL << 32) - 1; | |
1965 | ||
1966 | /* total blocks in file system block size */ | |
1967 | upper_limit >>= (blkbits - 9); | |
1968 | upper_limit <<= blkbits; | |
1969 | } | |
1970 | ||
1971 | /* 32-bit extent-start container, ee_block */ | |
1972 | res = 1LL << 32; | |
1973 | res <<= blkbits; | |
1974 | res -= 1; | |
1975 | ||
1976 | /* Sanity check against vm- & vfs- imposed limits */ | |
1977 | if (res > upper_limit) | |
1978 | res = upper_limit; | |
1979 | ||
1980 | return res; | |
1981 | } | |
ac27a0ec | 1982 | |
ac27a0ec | 1983 | /* |
cd2291a4 | 1984 | * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect |
0fc1b451 AK |
1985 | * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks. |
1986 | * We need to be 1 filesystem block less than the 2^48 sector limit. | |
ac27a0ec | 1987 | */ |
f287a1a5 | 1988 | static loff_t ext4_max_bitmap_size(int bits, int has_huge_files) |
ac27a0ec | 1989 | { |
617ba13b | 1990 | loff_t res = EXT4_NDIR_BLOCKS; |
0fc1b451 AK |
1991 | int meta_blocks; |
1992 | loff_t upper_limit; | |
0b8e58a1 AD |
1993 | /* This is calculated to be the largest file size for a dense, block |
1994 | * mapped file such that the file's total number of 512-byte sectors, | |
1995 | * including data and all indirect blocks, does not exceed (2^48 - 1). | |
1996 | * | |
1997 | * __u32 i_blocks_lo and _u16 i_blocks_high represent the total | |
1998 | * number of 512-byte sectors of the file. | |
0fc1b451 AK |
1999 | */ |
2000 | ||
f287a1a5 | 2001 | if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) { |
0fc1b451 | 2002 | /* |
0b8e58a1 AD |
2003 | * !has_huge_files or CONFIG_LBD not enabled implies that |
2004 | * the inode i_block field represents total file blocks in | |
2005 | * 2^32 512-byte sectors == size of vfs inode i_blocks * 8 | |
0fc1b451 AK |
2006 | */ |
2007 | upper_limit = (1LL << 32) - 1; | |
2008 | ||
2009 | /* total blocks in file system block size */ | |
2010 | upper_limit >>= (bits - 9); | |
2011 | ||
2012 | } else { | |
8180a562 AK |
2013 | /* |
2014 | * We use 48 bit ext4_inode i_blocks | |
2015 | * With EXT4_HUGE_FILE_FL set the i_blocks | |
2016 | * represent total number of blocks in | |
2017 | * file system block size | |
2018 | */ | |
0fc1b451 AK |
2019 | upper_limit = (1LL << 48) - 1; |
2020 | ||
0fc1b451 AK |
2021 | } |
2022 | ||
2023 | /* indirect blocks */ | |
2024 | meta_blocks = 1; | |
2025 | /* double indirect blocks */ | |
2026 | meta_blocks += 1 + (1LL << (bits-2)); | |
2027 | /* tripple indirect blocks */ | |
2028 | meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2))); | |
2029 | ||
2030 | upper_limit -= meta_blocks; | |
2031 | upper_limit <<= bits; | |
ac27a0ec DK |
2032 | |
2033 | res += 1LL << (bits-2); | |
2034 | res += 1LL << (2*(bits-2)); | |
2035 | res += 1LL << (3*(bits-2)); | |
2036 | res <<= bits; | |
2037 | if (res > upper_limit) | |
2038 | res = upper_limit; | |
0fc1b451 AK |
2039 | |
2040 | if (res > MAX_LFS_FILESIZE) | |
2041 | res = MAX_LFS_FILESIZE; | |
2042 | ||
ac27a0ec DK |
2043 | return res; |
2044 | } | |
2045 | ||
617ba13b | 2046 | static ext4_fsblk_t descriptor_loc(struct super_block *sb, |
0b8e58a1 | 2047 | ext4_fsblk_t logical_sb_block, int nr) |
ac27a0ec | 2048 | { |
617ba13b | 2049 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
fd2d4291 | 2050 | ext4_group_t bg, first_meta_bg; |
ac27a0ec DK |
2051 | int has_super = 0; |
2052 | ||
2053 | first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg); | |
2054 | ||
617ba13b | 2055 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) || |
ac27a0ec | 2056 | nr < first_meta_bg) |
70bbb3e0 | 2057 | return logical_sb_block + nr + 1; |
ac27a0ec | 2058 | bg = sbi->s_desc_per_block * nr; |
617ba13b | 2059 | if (ext4_bg_has_super(sb, bg)) |
ac27a0ec | 2060 | has_super = 1; |
0b8e58a1 | 2061 | |
617ba13b | 2062 | return (has_super + ext4_group_first_block_no(sb, bg)); |
ac27a0ec DK |
2063 | } |
2064 | ||
c9de560d AT |
2065 | /** |
2066 | * ext4_get_stripe_size: Get the stripe size. | |
2067 | * @sbi: In memory super block info | |
2068 | * | |
2069 | * If we have specified it via mount option, then | |
2070 | * use the mount option value. If the value specified at mount time is | |
2071 | * greater than the blocks per group use the super block value. | |
2072 | * If the super block value is greater than blocks per group return 0. | |
2073 | * Allocator needs it be less than blocks per group. | |
2074 | * | |
2075 | */ | |
2076 | static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi) | |
2077 | { | |
2078 | unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride); | |
2079 | unsigned long stripe_width = | |
2080 | le32_to_cpu(sbi->s_es->s_raid_stripe_width); | |
2081 | ||
2082 | if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group) | |
2083 | return sbi->s_stripe; | |
2084 | ||
2085 | if (stripe_width <= sbi->s_blocks_per_group) | |
2086 | return stripe_width; | |
2087 | ||
2088 | if (stride <= sbi->s_blocks_per_group) | |
2089 | return stride; | |
2090 | ||
2091 | return 0; | |
2092 | } | |
ac27a0ec | 2093 | |
3197ebdb TT |
2094 | /* sysfs supprt */ |
2095 | ||
2096 | struct ext4_attr { | |
2097 | struct attribute attr; | |
2098 | ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *); | |
2099 | ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *, | |
2100 | const char *, size_t); | |
2101 | int offset; | |
2102 | }; | |
2103 | ||
2104 | static int parse_strtoul(const char *buf, | |
2105 | unsigned long max, unsigned long *value) | |
2106 | { | |
2107 | char *endp; | |
2108 | ||
2109 | while (*buf && isspace(*buf)) | |
2110 | buf++; | |
2111 | *value = simple_strtoul(buf, &endp, 0); | |
2112 | while (*endp && isspace(*endp)) | |
2113 | endp++; | |
2114 | if (*endp || *value > max) | |
2115 | return -EINVAL; | |
2116 | ||
2117 | return 0; | |
2118 | } | |
2119 | ||
2120 | static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a, | |
2121 | struct ext4_sb_info *sbi, | |
2122 | char *buf) | |
2123 | { | |
2124 | return snprintf(buf, PAGE_SIZE, "%llu\n", | |
2125 | (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); | |
2126 | } | |
2127 | ||
2128 | static ssize_t session_write_kbytes_show(struct ext4_attr *a, | |
2129 | struct ext4_sb_info *sbi, char *buf) | |
2130 | { | |
2131 | struct super_block *sb = sbi->s_buddy_cache->i_sb; | |
2132 | ||
2133 | return snprintf(buf, PAGE_SIZE, "%lu\n", | |
2134 | (part_stat_read(sb->s_bdev->bd_part, sectors[1]) - | |
2135 | sbi->s_sectors_written_start) >> 1); | |
2136 | } | |
2137 | ||
2138 | static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a, | |
2139 | struct ext4_sb_info *sbi, char *buf) | |
2140 | { | |
2141 | struct super_block *sb = sbi->s_buddy_cache->i_sb; | |
2142 | ||
2143 | return snprintf(buf, PAGE_SIZE, "%llu\n", | |
2144 | sbi->s_kbytes_written + | |
2145 | ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) - | |
2146 | EXT4_SB(sb)->s_sectors_written_start) >> 1)); | |
2147 | } | |
2148 | ||
2149 | static ssize_t inode_readahead_blks_store(struct ext4_attr *a, | |
2150 | struct ext4_sb_info *sbi, | |
2151 | const char *buf, size_t count) | |
2152 | { | |
2153 | unsigned long t; | |
2154 | ||
2155 | if (parse_strtoul(buf, 0x40000000, &t)) | |
2156 | return -EINVAL; | |
2157 | ||
f7c43950 | 2158 | if (!is_power_of_2(t)) |
3197ebdb TT |
2159 | return -EINVAL; |
2160 | ||
2161 | sbi->s_inode_readahead_blks = t; | |
2162 | return count; | |
2163 | } | |
2164 | ||
2165 | static ssize_t sbi_ui_show(struct ext4_attr *a, | |
0b8e58a1 | 2166 | struct ext4_sb_info *sbi, char *buf) |
3197ebdb TT |
2167 | { |
2168 | unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset); | |
2169 | ||
2170 | return snprintf(buf, PAGE_SIZE, "%u\n", *ui); | |
2171 | } | |
2172 | ||
2173 | static ssize_t sbi_ui_store(struct ext4_attr *a, | |
2174 | struct ext4_sb_info *sbi, | |
2175 | const char *buf, size_t count) | |
2176 | { | |
2177 | unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset); | |
2178 | unsigned long t; | |
2179 | ||
2180 | if (parse_strtoul(buf, 0xffffffff, &t)) | |
2181 | return -EINVAL; | |
2182 | *ui = t; | |
2183 | return count; | |
2184 | } | |
2185 | ||
2186 | #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \ | |
2187 | static struct ext4_attr ext4_attr_##_name = { \ | |
2188 | .attr = {.name = __stringify(_name), .mode = _mode }, \ | |
2189 | .show = _show, \ | |
2190 | .store = _store, \ | |
2191 | .offset = offsetof(struct ext4_sb_info, _elname), \ | |
2192 | } | |
2193 | #define EXT4_ATTR(name, mode, show, store) \ | |
2194 | static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store) | |
2195 | ||
2196 | #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL) | |
2197 | #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store) | |
2198 | #define EXT4_RW_ATTR_SBI_UI(name, elname) \ | |
2199 | EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname) | |
2200 | #define ATTR_LIST(name) &ext4_attr_##name.attr | |
2201 | ||
2202 | EXT4_RO_ATTR(delayed_allocation_blocks); | |
2203 | EXT4_RO_ATTR(session_write_kbytes); | |
2204 | EXT4_RO_ATTR(lifetime_write_kbytes); | |
2205 | EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show, | |
2206 | inode_readahead_blks_store, s_inode_readahead_blks); | |
2207 | EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats); | |
2208 | EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan); | |
2209 | EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan); | |
2210 | EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs); | |
2211 | EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request); | |
2212 | EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc); | |
2213 | ||
2214 | static struct attribute *ext4_attrs[] = { | |
2215 | ATTR_LIST(delayed_allocation_blocks), | |
2216 | ATTR_LIST(session_write_kbytes), | |
2217 | ATTR_LIST(lifetime_write_kbytes), | |
2218 | ATTR_LIST(inode_readahead_blks), | |
2219 | ATTR_LIST(mb_stats), | |
2220 | ATTR_LIST(mb_max_to_scan), | |
2221 | ATTR_LIST(mb_min_to_scan), | |
2222 | ATTR_LIST(mb_order2_req), | |
2223 | ATTR_LIST(mb_stream_req), | |
2224 | ATTR_LIST(mb_group_prealloc), | |
2225 | NULL, | |
2226 | }; | |
2227 | ||
2228 | static ssize_t ext4_attr_show(struct kobject *kobj, | |
2229 | struct attribute *attr, char *buf) | |
2230 | { | |
2231 | struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info, | |
2232 | s_kobj); | |
2233 | struct ext4_attr *a = container_of(attr, struct ext4_attr, attr); | |
2234 | ||
2235 | return a->show ? a->show(a, sbi, buf) : 0; | |
2236 | } | |
2237 | ||
2238 | static ssize_t ext4_attr_store(struct kobject *kobj, | |
2239 | struct attribute *attr, | |
2240 | const char *buf, size_t len) | |
2241 | { | |
2242 | struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info, | |
2243 | s_kobj); | |
2244 | struct ext4_attr *a = container_of(attr, struct ext4_attr, attr); | |
2245 | ||
2246 | return a->store ? a->store(a, sbi, buf, len) : 0; | |
2247 | } | |
2248 | ||
2249 | static void ext4_sb_release(struct kobject *kobj) | |
2250 | { | |
2251 | struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info, | |
2252 | s_kobj); | |
2253 | complete(&sbi->s_kobj_unregister); | |
2254 | } | |
2255 | ||
2256 | ||
2257 | static struct sysfs_ops ext4_attr_ops = { | |
2258 | .show = ext4_attr_show, | |
2259 | .store = ext4_attr_store, | |
2260 | }; | |
2261 | ||
2262 | static struct kobj_type ext4_ktype = { | |
2263 | .default_attrs = ext4_attrs, | |
2264 | .sysfs_ops = &ext4_attr_ops, | |
2265 | .release = ext4_sb_release, | |
2266 | }; | |
2267 | ||
2b2d6d01 | 2268 | static int ext4_fill_super(struct super_block *sb, void *data, int silent) |
7477827f AK |
2269 | __releases(kernel_lock) |
2270 | __acquires(kernel_lock) | |
ac27a0ec | 2271 | { |
2b2d6d01 | 2272 | struct buffer_head *bh; |
617ba13b MC |
2273 | struct ext4_super_block *es = NULL; |
2274 | struct ext4_sb_info *sbi; | |
2275 | ext4_fsblk_t block; | |
2276 | ext4_fsblk_t sb_block = get_sb_block(&data); | |
70bbb3e0 | 2277 | ext4_fsblk_t logical_sb_block; |
ac27a0ec | 2278 | unsigned long offset = 0; |
ac27a0ec DK |
2279 | unsigned long journal_devnum = 0; |
2280 | unsigned long def_mount_opts; | |
2281 | struct inode *root; | |
9f6200bb | 2282 | char *cp; |
0390131b | 2283 | const char *descr; |
1d1fe1ee | 2284 | int ret = -EINVAL; |
ac27a0ec | 2285 | int blocksize; |
4ec11028 TT |
2286 | unsigned int db_count; |
2287 | unsigned int i; | |
f287a1a5 | 2288 | int needs_recovery, has_huge_files; |
3a06d778 | 2289 | int features; |
bd81d8ee | 2290 | __u64 blocks_count; |
833f4077 | 2291 | int err; |
b3881f74 | 2292 | unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; |
ac27a0ec DK |
2293 | |
2294 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); | |
2295 | if (!sbi) | |
2296 | return -ENOMEM; | |
705895b6 PE |
2297 | |
2298 | sbi->s_blockgroup_lock = | |
2299 | kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL); | |
2300 | if (!sbi->s_blockgroup_lock) { | |
2301 | kfree(sbi); | |
2302 | return -ENOMEM; | |
2303 | } | |
ac27a0ec DK |
2304 | sb->s_fs_info = sbi; |
2305 | sbi->s_mount_opt = 0; | |
617ba13b MC |
2306 | sbi->s_resuid = EXT4_DEF_RESUID; |
2307 | sbi->s_resgid = EXT4_DEF_RESGID; | |
240799cd | 2308 | sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS; |
d9c9bef1 | 2309 | sbi->s_sb_block = sb_block; |
afc32f7e TT |
2310 | sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part, |
2311 | sectors[1]); | |
ac27a0ec DK |
2312 | |
2313 | unlock_kernel(); | |
2314 | ||
9f6200bb TT |
2315 | /* Cleanup superblock name */ |
2316 | for (cp = sb->s_id; (cp = strchr(cp, '/'));) | |
2317 | *cp = '!'; | |
2318 | ||
617ba13b | 2319 | blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE); |
ac27a0ec | 2320 | if (!blocksize) { |
b31e1552 | 2321 | ext4_msg(sb, KERN_ERR, "unable to set blocksize"); |
ac27a0ec DK |
2322 | goto out_fail; |
2323 | } | |
2324 | ||
2325 | /* | |
617ba13b | 2326 | * The ext4 superblock will not be buffer aligned for other than 1kB |
ac27a0ec DK |
2327 | * block sizes. We need to calculate the offset from buffer start. |
2328 | */ | |
617ba13b | 2329 | if (blocksize != EXT4_MIN_BLOCK_SIZE) { |
70bbb3e0 AM |
2330 | logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; |
2331 | offset = do_div(logical_sb_block, blocksize); | |
ac27a0ec | 2332 | } else { |
70bbb3e0 | 2333 | logical_sb_block = sb_block; |
ac27a0ec DK |
2334 | } |
2335 | ||
70bbb3e0 | 2336 | if (!(bh = sb_bread(sb, logical_sb_block))) { |
b31e1552 | 2337 | ext4_msg(sb, KERN_ERR, "unable to read superblock"); |
ac27a0ec DK |
2338 | goto out_fail; |
2339 | } | |
2340 | /* | |
2341 | * Note: s_es must be initialized as soon as possible because | |
617ba13b | 2342 | * some ext4 macro-instructions depend on its value |
ac27a0ec | 2343 | */ |
617ba13b | 2344 | es = (struct ext4_super_block *) (((char *)bh->b_data) + offset); |
ac27a0ec DK |
2345 | sbi->s_es = es; |
2346 | sb->s_magic = le16_to_cpu(es->s_magic); | |
617ba13b MC |
2347 | if (sb->s_magic != EXT4_SUPER_MAGIC) |
2348 | goto cantfind_ext4; | |
afc32f7e | 2349 | sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written); |
ac27a0ec DK |
2350 | |
2351 | /* Set defaults before we parse the mount options */ | |
2352 | def_mount_opts = le32_to_cpu(es->s_default_mount_opts); | |
617ba13b | 2353 | if (def_mount_opts & EXT4_DEFM_DEBUG) |
ac27a0ec | 2354 | set_opt(sbi->s_mount_opt, DEBUG); |
617ba13b | 2355 | if (def_mount_opts & EXT4_DEFM_BSDGROUPS) |
ac27a0ec | 2356 | set_opt(sbi->s_mount_opt, GRPID); |
617ba13b | 2357 | if (def_mount_opts & EXT4_DEFM_UID16) |
ac27a0ec | 2358 | set_opt(sbi->s_mount_opt, NO_UID32); |
03010a33 | 2359 | #ifdef CONFIG_EXT4_FS_XATTR |
617ba13b | 2360 | if (def_mount_opts & EXT4_DEFM_XATTR_USER) |
ac27a0ec | 2361 | set_opt(sbi->s_mount_opt, XATTR_USER); |
2e7842b8 | 2362 | #endif |
03010a33 | 2363 | #ifdef CONFIG_EXT4_FS_POSIX_ACL |
617ba13b | 2364 | if (def_mount_opts & EXT4_DEFM_ACL) |
ac27a0ec | 2365 | set_opt(sbi->s_mount_opt, POSIX_ACL); |
2e7842b8 | 2366 | #endif |
617ba13b MC |
2367 | if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA) |
2368 | sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA; | |
2369 | else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED) | |
2370 | sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA; | |
2371 | else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK) | |
2372 | sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA; | |
2373 | ||
2374 | if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC) | |
ac27a0ec | 2375 | set_opt(sbi->s_mount_opt, ERRORS_PANIC); |
bb4f397a | 2376 | else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE) |
ceea16bf | 2377 | set_opt(sbi->s_mount_opt, ERRORS_CONT); |
bb4f397a AK |
2378 | else |
2379 | set_opt(sbi->s_mount_opt, ERRORS_RO); | |
ac27a0ec DK |
2380 | |
2381 | sbi->s_resuid = le16_to_cpu(es->s_def_resuid); | |
2382 | sbi->s_resgid = le16_to_cpu(es->s_def_resgid); | |
30773840 TT |
2383 | sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ; |
2384 | sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME; | |
2385 | sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME; | |
f4033903 | 2386 | sbi->s_mb_history_max = default_mb_history_length; |
ac27a0ec | 2387 | |
571640ca | 2388 | set_opt(sbi->s_mount_opt, BARRIER); |
ac27a0ec | 2389 | |
dd919b98 AK |
2390 | /* |
2391 | * enable delayed allocation by default | |
2392 | * Use -o nodelalloc to turn it off | |
2393 | */ | |
2394 | set_opt(sbi->s_mount_opt, DELALLOC); | |
2395 | ||
b3881f74 TT |
2396 | if (!parse_options((char *) data, sb, &journal_devnum, |
2397 | &journal_ioprio, NULL, 0)) | |
ac27a0ec DK |
2398 | goto failed_mount; |
2399 | ||
2400 | sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | | |
617ba13b | 2401 | ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0); |
ac27a0ec | 2402 | |
617ba13b MC |
2403 | if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV && |
2404 | (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) || | |
2405 | EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) || | |
2406 | EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U))) | |
b31e1552 ES |
2407 | ext4_msg(sb, KERN_WARNING, |
2408 | "feature flags set on rev 0 fs, " | |
2409 | "running e2fsck is recommended"); | |
469108ff | 2410 | |
ac27a0ec DK |
2411 | /* |
2412 | * Check feature flags regardless of the revision level, since we | |
2413 | * previously didn't change the revision level when setting the flags, | |
2414 | * so there is a chance incompat flags are set on a rev 0 filesystem. | |
2415 | */ | |
617ba13b | 2416 | features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP); |
ac27a0ec | 2417 | if (features) { |
b31e1552 ES |
2418 | ext4_msg(sb, KERN_ERR, |
2419 | "Couldn't mount because of " | |
2420 | "unsupported optional features (%x)", | |
3a06d778 AK |
2421 | (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) & |
2422 | ~EXT4_FEATURE_INCOMPAT_SUPP)); | |
ac27a0ec DK |
2423 | goto failed_mount; |
2424 | } | |
617ba13b | 2425 | features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP); |
ac27a0ec | 2426 | if (!(sb->s_flags & MS_RDONLY) && features) { |
b31e1552 ES |
2427 | ext4_msg(sb, KERN_ERR, |
2428 | "Couldn't mount RDWR because of " | |
2429 | "unsupported optional features (%x)", | |
3a06d778 AK |
2430 | (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) & |
2431 | ~EXT4_FEATURE_RO_COMPAT_SUPP)); | |
ac27a0ec DK |
2432 | goto failed_mount; |
2433 | } | |
f287a1a5 TT |
2434 | has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb, |
2435 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE); | |
2436 | if (has_huge_files) { | |
0fc1b451 AK |
2437 | /* |
2438 | * Large file size enabled file system can only be | |
b3a6ffe1 | 2439 | * mount if kernel is build with CONFIG_LBD |
0fc1b451 AK |
2440 | */ |
2441 | if (sizeof(root->i_blocks) < sizeof(u64) && | |
2442 | !(sb->s_flags & MS_RDONLY)) { | |
b31e1552 | 2443 | ext4_msg(sb, KERN_ERR, "Filesystem with huge " |
0fc1b451 | 2444 | "files cannot be mounted read-write " |
b31e1552 | 2445 | "without CONFIG_LBD"); |
0fc1b451 AK |
2446 | goto failed_mount; |
2447 | } | |
2448 | } | |
ac27a0ec DK |
2449 | blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size); |
2450 | ||
617ba13b MC |
2451 | if (blocksize < EXT4_MIN_BLOCK_SIZE || |
2452 | blocksize > EXT4_MAX_BLOCK_SIZE) { | |
b31e1552 ES |
2453 | ext4_msg(sb, KERN_ERR, |
2454 | "Unsupported filesystem blocksize %d", blocksize); | |
ac27a0ec DK |
2455 | goto failed_mount; |
2456 | } | |
2457 | ||
ac27a0ec | 2458 | if (sb->s_blocksize != blocksize) { |
ce40733c AK |
2459 | /* Validate the filesystem blocksize */ |
2460 | if (!sb_set_blocksize(sb, blocksize)) { | |
b31e1552 | 2461 | ext4_msg(sb, KERN_ERR, "bad block size %d", |
ce40733c | 2462 | blocksize); |
ac27a0ec DK |
2463 | goto failed_mount; |
2464 | } | |
2465 | ||
2b2d6d01 | 2466 | brelse(bh); |
70bbb3e0 AM |
2467 | logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; |
2468 | offset = do_div(logical_sb_block, blocksize); | |
2469 | bh = sb_bread(sb, logical_sb_block); | |
ac27a0ec | 2470 | if (!bh) { |
b31e1552 ES |
2471 | ext4_msg(sb, KERN_ERR, |
2472 | "Can't read superblock on 2nd try"); | |
ac27a0ec DK |
2473 | goto failed_mount; |
2474 | } | |
617ba13b | 2475 | es = (struct ext4_super_block *)(((char *)bh->b_data) + offset); |
ac27a0ec | 2476 | sbi->s_es = es; |
617ba13b | 2477 | if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) { |
b31e1552 ES |
2478 | ext4_msg(sb, KERN_ERR, |
2479 | "Magic mismatch, very weird!"); | |
ac27a0ec DK |
2480 | goto failed_mount; |
2481 | } | |
2482 | } | |
2483 | ||
f287a1a5 TT |
2484 | sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits, |
2485 | has_huge_files); | |
2486 | sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files); | |
ac27a0ec | 2487 | |
617ba13b MC |
2488 | if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) { |
2489 | sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE; | |
2490 | sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; | |
ac27a0ec DK |
2491 | } else { |
2492 | sbi->s_inode_size = le16_to_cpu(es->s_inode_size); | |
2493 | sbi->s_first_ino = le32_to_cpu(es->s_first_ino); | |
617ba13b | 2494 | if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) || |
1330593e | 2495 | (!is_power_of_2(sbi->s_inode_size)) || |
ac27a0ec | 2496 | (sbi->s_inode_size > blocksize)) { |
b31e1552 ES |
2497 | ext4_msg(sb, KERN_ERR, |
2498 | "unsupported inode size: %d", | |
2b2d6d01 | 2499 | sbi->s_inode_size); |
ac27a0ec DK |
2500 | goto failed_mount; |
2501 | } | |
ef7f3835 KS |
2502 | if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) |
2503 | sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2); | |
ac27a0ec | 2504 | } |
0b8e58a1 | 2505 | |
0d1ee42f AR |
2506 | sbi->s_desc_size = le16_to_cpu(es->s_desc_size); |
2507 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) { | |
8fadc143 | 2508 | if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT || |
0d1ee42f | 2509 | sbi->s_desc_size > EXT4_MAX_DESC_SIZE || |
d8ea6cf8 | 2510 | !is_power_of_2(sbi->s_desc_size)) { |
b31e1552 ES |
2511 | ext4_msg(sb, KERN_ERR, |
2512 | "unsupported descriptor size %lu", | |
0d1ee42f AR |
2513 | sbi->s_desc_size); |
2514 | goto failed_mount; | |
2515 | } | |
2516 | } else | |
2517 | sbi->s_desc_size = EXT4_MIN_DESC_SIZE; | |
0b8e58a1 | 2518 | |
ac27a0ec | 2519 | sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group); |
ac27a0ec | 2520 | sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group); |
b47b6f38 | 2521 | if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0) |
617ba13b | 2522 | goto cantfind_ext4; |
0b8e58a1 | 2523 | |
617ba13b | 2524 | sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb); |
ac27a0ec | 2525 | if (sbi->s_inodes_per_block == 0) |
617ba13b | 2526 | goto cantfind_ext4; |
ac27a0ec DK |
2527 | sbi->s_itb_per_group = sbi->s_inodes_per_group / |
2528 | sbi->s_inodes_per_block; | |
0d1ee42f | 2529 | sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb); |
ac27a0ec DK |
2530 | sbi->s_sbh = bh; |
2531 | sbi->s_mount_state = le16_to_cpu(es->s_state); | |
e57aa839 FW |
2532 | sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb)); |
2533 | sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb)); | |
0b8e58a1 | 2534 | |
2b2d6d01 | 2535 | for (i = 0; i < 4; i++) |
ac27a0ec DK |
2536 | sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]); |
2537 | sbi->s_def_hash_version = es->s_def_hash_version; | |
f99b2589 TT |
2538 | i = le32_to_cpu(es->s_flags); |
2539 | if (i & EXT2_FLAGS_UNSIGNED_HASH) | |
2540 | sbi->s_hash_unsigned = 3; | |
2541 | else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) { | |
2542 | #ifdef __CHAR_UNSIGNED__ | |
2543 | es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH); | |
2544 | sbi->s_hash_unsigned = 3; | |
2545 | #else | |
2546 | es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH); | |
2547 | #endif | |
2548 | sb->s_dirt = 1; | |
2549 | } | |
ac27a0ec DK |
2550 | |
2551 | if (sbi->s_blocks_per_group > blocksize * 8) { | |
b31e1552 ES |
2552 | ext4_msg(sb, KERN_ERR, |
2553 | "#blocks per group too big: %lu", | |
2b2d6d01 | 2554 | sbi->s_blocks_per_group); |
ac27a0ec DK |
2555 | goto failed_mount; |
2556 | } | |
ac27a0ec | 2557 | if (sbi->s_inodes_per_group > blocksize * 8) { |
b31e1552 ES |
2558 | ext4_msg(sb, KERN_ERR, |
2559 | "#inodes per group too big: %lu", | |
2b2d6d01 | 2560 | sbi->s_inodes_per_group); |
ac27a0ec DK |
2561 | goto failed_mount; |
2562 | } | |
2563 | ||
bd81d8ee | 2564 | if (ext4_blocks_count(es) > |
ac27a0ec | 2565 | (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { |
b31e1552 ES |
2566 | ext4_msg(sb, KERN_ERR, "filesystem" |
2567 | " too large to mount safely"); | |
ac27a0ec | 2568 | if (sizeof(sector_t) < 8) |
b31e1552 | 2569 | ext4_msg(sb, KERN_WARNING, "CONFIG_LBD not enabled"); |
ac27a0ec DK |
2570 | goto failed_mount; |
2571 | } | |
2572 | ||
617ba13b MC |
2573 | if (EXT4_BLOCKS_PER_GROUP(sb) == 0) |
2574 | goto cantfind_ext4; | |
e7c95593 | 2575 | |
0f2ddca6 FTN |
2576 | /* check blocks count against device size */ |
2577 | blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits; | |
2578 | if (blocks_count && ext4_blocks_count(es) > blocks_count) { | |
b31e1552 ES |
2579 | ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu " |
2580 | "exceeds size of device (%llu blocks)", | |
0f2ddca6 FTN |
2581 | ext4_blocks_count(es), blocks_count); |
2582 | goto failed_mount; | |
2583 | } | |
2584 | ||
0b8e58a1 AD |
2585 | /* |
2586 | * It makes no sense for the first data block to be beyond the end | |
2587 | * of the filesystem. | |
2588 | */ | |
2589 | if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) { | |
b31e1552 ES |
2590 | ext4_msg(sb, KERN_WARNING, "bad geometry: first data" |
2591 | "block %u is beyond end of filesystem (%llu)", | |
2592 | le32_to_cpu(es->s_first_data_block), | |
2593 | ext4_blocks_count(es)); | |
e7c95593 ES |
2594 | goto failed_mount; |
2595 | } | |
bd81d8ee LV |
2596 | blocks_count = (ext4_blocks_count(es) - |
2597 | le32_to_cpu(es->s_first_data_block) + | |
2598 | EXT4_BLOCKS_PER_GROUP(sb) - 1); | |
2599 | do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb)); | |
4ec11028 | 2600 | if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) { |
b31e1552 | 2601 | ext4_msg(sb, KERN_WARNING, "groups count too large: %u " |
4ec11028 | 2602 | "(block count %llu, first data block %u, " |
b31e1552 | 2603 | "blocks per group %lu)", sbi->s_groups_count, |
4ec11028 TT |
2604 | ext4_blocks_count(es), |
2605 | le32_to_cpu(es->s_first_data_block), | |
2606 | EXT4_BLOCKS_PER_GROUP(sb)); | |
2607 | goto failed_mount; | |
2608 | } | |
bd81d8ee | 2609 | sbi->s_groups_count = blocks_count; |
617ba13b MC |
2610 | db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) / |
2611 | EXT4_DESC_PER_BLOCK(sb); | |
2b2d6d01 | 2612 | sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *), |
ac27a0ec DK |
2613 | GFP_KERNEL); |
2614 | if (sbi->s_group_desc == NULL) { | |
b31e1552 | 2615 | ext4_msg(sb, KERN_ERR, "not enough memory"); |
ac27a0ec DK |
2616 | goto failed_mount; |
2617 | } | |
2618 | ||
3244fcb1 | 2619 | #ifdef CONFIG_PROC_FS |
9f6200bb TT |
2620 | if (ext4_proc_root) |
2621 | sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root); | |
3244fcb1 | 2622 | #endif |
240799cd | 2623 | |
705895b6 | 2624 | bgl_lock_init(sbi->s_blockgroup_lock); |
ac27a0ec DK |
2625 | |
2626 | for (i = 0; i < db_count; i++) { | |
70bbb3e0 | 2627 | block = descriptor_loc(sb, logical_sb_block, i); |
ac27a0ec DK |
2628 | sbi->s_group_desc[i] = sb_bread(sb, block); |
2629 | if (!sbi->s_group_desc[i]) { | |
b31e1552 ES |
2630 | ext4_msg(sb, KERN_ERR, |
2631 | "can't read group descriptor %d", i); | |
ac27a0ec DK |
2632 | db_count = i; |
2633 | goto failed_mount2; | |
2634 | } | |
2635 | } | |
2b2d6d01 | 2636 | if (!ext4_check_descriptors(sb)) { |
b31e1552 | 2637 | ext4_msg(sb, KERN_ERR, "group descriptors corrupted!"); |
ac27a0ec DK |
2638 | goto failed_mount2; |
2639 | } | |
772cb7c8 JS |
2640 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) |
2641 | if (!ext4_fill_flex_info(sb)) { | |
b31e1552 ES |
2642 | ext4_msg(sb, KERN_ERR, |
2643 | "unable to initialize " | |
2644 | "flex_bg meta info!"); | |
772cb7c8 JS |
2645 | goto failed_mount2; |
2646 | } | |
2647 | ||
ac27a0ec DK |
2648 | sbi->s_gdb_count = db_count; |
2649 | get_random_bytes(&sbi->s_next_generation, sizeof(u32)); | |
2650 | spin_lock_init(&sbi->s_next_gen_lock); | |
2651 | ||
833f4077 PZ |
2652 | err = percpu_counter_init(&sbi->s_freeblocks_counter, |
2653 | ext4_count_free_blocks(sb)); | |
2654 | if (!err) { | |
2655 | err = percpu_counter_init(&sbi->s_freeinodes_counter, | |
2656 | ext4_count_free_inodes(sb)); | |
2657 | } | |
2658 | if (!err) { | |
2659 | err = percpu_counter_init(&sbi->s_dirs_counter, | |
2660 | ext4_count_dirs(sb)); | |
2661 | } | |
6bc6e63f AK |
2662 | if (!err) { |
2663 | err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0); | |
2664 | } | |
833f4077 | 2665 | if (err) { |
b31e1552 | 2666 | ext4_msg(sb, KERN_ERR, "insufficient memory"); |
833f4077 PZ |
2667 | goto failed_mount3; |
2668 | } | |
ac27a0ec | 2669 | |
c9de560d AT |
2670 | sbi->s_stripe = ext4_get_stripe_size(sbi); |
2671 | ||
ac27a0ec DK |
2672 | /* |
2673 | * set up enough so that it can read an inode | |
2674 | */ | |
9ca92389 TT |
2675 | if (!test_opt(sb, NOLOAD) && |
2676 | EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) | |
2677 | sb->s_op = &ext4_sops; | |
2678 | else | |
2679 | sb->s_op = &ext4_nojournal_sops; | |
617ba13b MC |
2680 | sb->s_export_op = &ext4_export_ops; |
2681 | sb->s_xattr = ext4_xattr_handlers; | |
ac27a0ec | 2682 | #ifdef CONFIG_QUOTA |
617ba13b MC |
2683 | sb->s_qcop = &ext4_qctl_operations; |
2684 | sb->dq_op = &ext4_quota_operations; | |
ac27a0ec DK |
2685 | #endif |
2686 | INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */ | |
3b9d4ed2 | 2687 | mutex_init(&sbi->s_orphan_lock); |
32ed5058 | 2688 | mutex_init(&sbi->s_resize_lock); |
ac27a0ec DK |
2689 | |
2690 | sb->s_root = NULL; | |
2691 | ||
2692 | needs_recovery = (es->s_last_orphan != 0 || | |
617ba13b MC |
2693 | EXT4_HAS_INCOMPAT_FEATURE(sb, |
2694 | EXT4_FEATURE_INCOMPAT_RECOVER)); | |
ac27a0ec DK |
2695 | |
2696 | /* | |
2697 | * The first inode we look at is the journal inode. Don't try | |
2698 | * root first: it may be modified in the journal! | |
2699 | */ | |
2700 | if (!test_opt(sb, NOLOAD) && | |
617ba13b MC |
2701 | EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) { |
2702 | if (ext4_load_journal(sb, es, journal_devnum)) | |
ac27a0ec | 2703 | goto failed_mount3; |
624080ed TT |
2704 | if (!(sb->s_flags & MS_RDONLY) && |
2705 | EXT4_SB(sb)->s_journal->j_failed_commit) { | |
b31e1552 | 2706 | ext4_msg(sb, KERN_CRIT, "error: " |
624080ed | 2707 | "ext4_fill_super: Journal transaction " |
b31e1552 | 2708 | "%u is corrupt", |
624080ed | 2709 | EXT4_SB(sb)->s_journal->j_failed_commit); |
2b2d6d01 | 2710 | if (test_opt(sb, ERRORS_RO)) { |
b31e1552 ES |
2711 | ext4_msg(sb, KERN_CRIT, |
2712 | "Mounting filesystem read-only"); | |
624080ed TT |
2713 | sb->s_flags |= MS_RDONLY; |
2714 | EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; | |
2715 | es->s_state |= cpu_to_le16(EXT4_ERROR_FS); | |
2716 | } | |
2717 | if (test_opt(sb, ERRORS_PANIC)) { | |
2718 | EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; | |
2719 | es->s_state |= cpu_to_le16(EXT4_ERROR_FS); | |
e2d67052 | 2720 | ext4_commit_super(sb, 1); |
624080ed TT |
2721 | goto failed_mount4; |
2722 | } | |
2723 | } | |
0390131b FM |
2724 | } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) && |
2725 | EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) { | |
b31e1552 ES |
2726 | ext4_msg(sb, KERN_ERR, "required journal recovery " |
2727 | "suppressed and not mounted read-only"); | |
0390131b | 2728 | goto failed_mount4; |
ac27a0ec | 2729 | } else { |
0390131b FM |
2730 | clear_opt(sbi->s_mount_opt, DATA_FLAGS); |
2731 | set_opt(sbi->s_mount_opt, WRITEBACK_DATA); | |
2732 | sbi->s_journal = NULL; | |
2733 | needs_recovery = 0; | |
2734 | goto no_journal; | |
ac27a0ec DK |
2735 | } |
2736 | ||
eb40a09c JS |
2737 | if (ext4_blocks_count(es) > 0xffffffffULL && |
2738 | !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0, | |
2739 | JBD2_FEATURE_INCOMPAT_64BIT)) { | |
b31e1552 | 2740 | ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature"); |
eb40a09c JS |
2741 | goto failed_mount4; |
2742 | } | |
2743 | ||
818d276c GS |
2744 | if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { |
2745 | jbd2_journal_set_features(sbi->s_journal, | |
2746 | JBD2_FEATURE_COMPAT_CHECKSUM, 0, | |
2747 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); | |
2748 | } else if (test_opt(sb, JOURNAL_CHECKSUM)) { | |
2749 | jbd2_journal_set_features(sbi->s_journal, | |
2750 | JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0); | |
2751 | jbd2_journal_clear_features(sbi->s_journal, 0, 0, | |
2752 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); | |
2753 | } else { | |
2754 | jbd2_journal_clear_features(sbi->s_journal, | |
2755 | JBD2_FEATURE_COMPAT_CHECKSUM, 0, | |
2756 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); | |
2757 | } | |
2758 | ||
ac27a0ec DK |
2759 | /* We have now updated the journal if required, so we can |
2760 | * validate the data journaling mode. */ | |
2761 | switch (test_opt(sb, DATA_FLAGS)) { | |
2762 | case 0: | |
2763 | /* No mode set, assume a default based on the journal | |
63f57933 AM |
2764 | * capabilities: ORDERED_DATA if the journal can |
2765 | * cope, else JOURNAL_DATA | |
2766 | */ | |
dab291af MC |
2767 | if (jbd2_journal_check_available_features |
2768 | (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) | |
ac27a0ec DK |
2769 | set_opt(sbi->s_mount_opt, ORDERED_DATA); |
2770 | else | |
2771 | set_opt(sbi->s_mount_opt, JOURNAL_DATA); | |
2772 | break; | |
2773 | ||
617ba13b MC |
2774 | case EXT4_MOUNT_ORDERED_DATA: |
2775 | case EXT4_MOUNT_WRITEBACK_DATA: | |
dab291af MC |
2776 | if (!jbd2_journal_check_available_features |
2777 | (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { | |
b31e1552 ES |
2778 | ext4_msg(sb, KERN_ERR, "Journal does not support " |
2779 | "requested data journaling mode"); | |
ac27a0ec DK |
2780 | goto failed_mount4; |
2781 | } | |
2782 | default: | |
2783 | break; | |
2784 | } | |
b3881f74 | 2785 | set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); |
ac27a0ec | 2786 | |
0390131b | 2787 | no_journal: |
ac27a0ec DK |
2788 | |
2789 | if (test_opt(sb, NOBH)) { | |
617ba13b | 2790 | if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) { |
b31e1552 ES |
2791 | ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - " |
2792 | "its supported only with writeback mode"); | |
ac27a0ec DK |
2793 | clear_opt(sbi->s_mount_opt, NOBH); |
2794 | } | |
2795 | } | |
2796 | /* | |
dab291af | 2797 | * The jbd2_journal_load will have done any necessary log recovery, |
ac27a0ec DK |
2798 | * so we can safely mount the rest of the filesystem now. |
2799 | */ | |
2800 | ||
1d1fe1ee DH |
2801 | root = ext4_iget(sb, EXT4_ROOT_INO); |
2802 | if (IS_ERR(root)) { | |
b31e1552 | 2803 | ext4_msg(sb, KERN_ERR, "get root inode failed"); |
1d1fe1ee | 2804 | ret = PTR_ERR(root); |
ac27a0ec DK |
2805 | goto failed_mount4; |
2806 | } | |
2807 | if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { | |
1d1fe1ee | 2808 | iput(root); |
b31e1552 | 2809 | ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck"); |
ac27a0ec DK |
2810 | goto failed_mount4; |
2811 | } | |
1d1fe1ee DH |
2812 | sb->s_root = d_alloc_root(root); |
2813 | if (!sb->s_root) { | |
b31e1552 | 2814 | ext4_msg(sb, KERN_ERR, "get root dentry failed"); |
1d1fe1ee DH |
2815 | iput(root); |
2816 | ret = -ENOMEM; | |
2817 | goto failed_mount4; | |
2818 | } | |
ac27a0ec | 2819 | |
2b2d6d01 | 2820 | ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY); |
ef7f3835 KS |
2821 | |
2822 | /* determine the minimum size of new large inodes, if present */ | |
2823 | if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) { | |
2824 | sbi->s_want_extra_isize = sizeof(struct ext4_inode) - | |
2825 | EXT4_GOOD_OLD_INODE_SIZE; | |
2826 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
2827 | EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) { | |
2828 | if (sbi->s_want_extra_isize < | |
2829 | le16_to_cpu(es->s_want_extra_isize)) | |
2830 | sbi->s_want_extra_isize = | |
2831 | le16_to_cpu(es->s_want_extra_isize); | |
2832 | if (sbi->s_want_extra_isize < | |
2833 | le16_to_cpu(es->s_min_extra_isize)) | |
2834 | sbi->s_want_extra_isize = | |
2835 | le16_to_cpu(es->s_min_extra_isize); | |
2836 | } | |
2837 | } | |
2838 | /* Check if enough inode space is available */ | |
2839 | if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize > | |
2840 | sbi->s_inode_size) { | |
2841 | sbi->s_want_extra_isize = sizeof(struct ext4_inode) - | |
2842 | EXT4_GOOD_OLD_INODE_SIZE; | |
b31e1552 ES |
2843 | ext4_msg(sb, KERN_INFO, "required extra inode space not" |
2844 | "available"); | |
ef7f3835 KS |
2845 | } |
2846 | ||
c2774d84 | 2847 | if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { |
b31e1552 ES |
2848 | ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - " |
2849 | "requested data journaling mode"); | |
c2774d84 AK |
2850 | clear_opt(sbi->s_mount_opt, DELALLOC); |
2851 | } else if (test_opt(sb, DELALLOC)) | |
b31e1552 | 2852 | ext4_msg(sb, KERN_INFO, "delayed allocation enabled"); |
c2774d84 | 2853 | |
6fd058f7 TT |
2854 | err = ext4_setup_system_zone(sb); |
2855 | if (err) { | |
b31e1552 ES |
2856 | ext4_msg(sb, KERN_ERR, "failed to initialize system " |
2857 | "zone (%d)\n", err); | |
6fd058f7 TT |
2858 | goto failed_mount4; |
2859 | } | |
2860 | ||
c2774d84 AK |
2861 | ext4_ext_init(sb); |
2862 | err = ext4_mb_init(sb, needs_recovery); | |
2863 | if (err) { | |
b31e1552 ES |
2864 | ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)", |
2865 | err); | |
c2774d84 AK |
2866 | goto failed_mount4; |
2867 | } | |
2868 | ||
3197ebdb TT |
2869 | sbi->s_kobj.kset = ext4_kset; |
2870 | init_completion(&sbi->s_kobj_unregister); | |
2871 | err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL, | |
2872 | "%s", sb->s_id); | |
2873 | if (err) { | |
2874 | ext4_mb_release(sb); | |
2875 | ext4_ext_release(sb); | |
2876 | goto failed_mount4; | |
2877 | }; | |
2878 | ||
617ba13b MC |
2879 | EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS; |
2880 | ext4_orphan_cleanup(sb, es); | |
2881 | EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS; | |
0390131b | 2882 | if (needs_recovery) { |
b31e1552 | 2883 | ext4_msg(sb, KERN_INFO, "recovery complete"); |
0390131b FM |
2884 | ext4_mark_recovery_complete(sb, es); |
2885 | } | |
2886 | if (EXT4_SB(sb)->s_journal) { | |
2887 | if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) | |
2888 | descr = " journalled data mode"; | |
2889 | else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) | |
2890 | descr = " ordered data mode"; | |
2891 | else | |
2892 | descr = " writeback data mode"; | |
2893 | } else | |
2894 | descr = "out journal"; | |
2895 | ||
b31e1552 | 2896 | ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr); |
ac27a0ec DK |
2897 | |
2898 | lock_kernel(); | |
2899 | return 0; | |
2900 | ||
617ba13b | 2901 | cantfind_ext4: |
ac27a0ec | 2902 | if (!silent) |
b31e1552 | 2903 | ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem"); |
ac27a0ec DK |
2904 | goto failed_mount; |
2905 | ||
2906 | failed_mount4: | |
b31e1552 | 2907 | ext4_msg(sb, KERN_ERR, "mount failed"); |
6fd058f7 | 2908 | ext4_release_system_zone(sb); |
0390131b FM |
2909 | if (sbi->s_journal) { |
2910 | jbd2_journal_destroy(sbi->s_journal); | |
2911 | sbi->s_journal = NULL; | |
2912 | } | |
ac27a0ec | 2913 | failed_mount3: |
c5ca7c76 TT |
2914 | if (sbi->s_flex_groups) { |
2915 | if (is_vmalloc_addr(sbi->s_flex_groups)) | |
2916 | vfree(sbi->s_flex_groups); | |
2917 | else | |
2918 | kfree(sbi->s_flex_groups); | |
2919 | } | |
ac27a0ec DK |
2920 | percpu_counter_destroy(&sbi->s_freeblocks_counter); |
2921 | percpu_counter_destroy(&sbi->s_freeinodes_counter); | |
2922 | percpu_counter_destroy(&sbi->s_dirs_counter); | |
6bc6e63f | 2923 | percpu_counter_destroy(&sbi->s_dirtyblocks_counter); |
ac27a0ec DK |
2924 | failed_mount2: |
2925 | for (i = 0; i < db_count; i++) | |
2926 | brelse(sbi->s_group_desc[i]); | |
2927 | kfree(sbi->s_group_desc); | |
2928 | failed_mount: | |
240799cd | 2929 | if (sbi->s_proc) { |
9f6200bb | 2930 | remove_proc_entry(sb->s_id, ext4_proc_root); |
240799cd | 2931 | } |
ac27a0ec DK |
2932 | #ifdef CONFIG_QUOTA |
2933 | for (i = 0; i < MAXQUOTAS; i++) | |
2934 | kfree(sbi->s_qf_names[i]); | |
2935 | #endif | |
617ba13b | 2936 | ext4_blkdev_remove(sbi); |
ac27a0ec DK |
2937 | brelse(bh); |
2938 | out_fail: | |
2939 | sb->s_fs_info = NULL; | |
f6830165 | 2940 | kfree(sbi->s_blockgroup_lock); |
ac27a0ec DK |
2941 | kfree(sbi); |
2942 | lock_kernel(); | |
1d1fe1ee | 2943 | return ret; |
ac27a0ec DK |
2944 | } |
2945 | ||
2946 | /* | |
2947 | * Setup any per-fs journal parameters now. We'll do this both on | |
2948 | * initial mount, once the journal has been initialised but before we've | |
2949 | * done any recovery; and again on any subsequent remount. | |
2950 | */ | |
617ba13b | 2951 | static void ext4_init_journal_params(struct super_block *sb, journal_t *journal) |
ac27a0ec | 2952 | { |
617ba13b | 2953 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
ac27a0ec | 2954 | |
30773840 TT |
2955 | journal->j_commit_interval = sbi->s_commit_interval; |
2956 | journal->j_min_batch_time = sbi->s_min_batch_time; | |
2957 | journal->j_max_batch_time = sbi->s_max_batch_time; | |
ac27a0ec DK |
2958 | |
2959 | spin_lock(&journal->j_state_lock); | |
2960 | if (test_opt(sb, BARRIER)) | |
dab291af | 2961 | journal->j_flags |= JBD2_BARRIER; |
ac27a0ec | 2962 | else |
dab291af | 2963 | journal->j_flags &= ~JBD2_BARRIER; |
5bf5683a HK |
2964 | if (test_opt(sb, DATA_ERR_ABORT)) |
2965 | journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR; | |
2966 | else | |
2967 | journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR; | |
ac27a0ec DK |
2968 | spin_unlock(&journal->j_state_lock); |
2969 | } | |
2970 | ||
617ba13b | 2971 | static journal_t *ext4_get_journal(struct super_block *sb, |
ac27a0ec DK |
2972 | unsigned int journal_inum) |
2973 | { | |
2974 | struct inode *journal_inode; | |
2975 | journal_t *journal; | |
2976 | ||
0390131b FM |
2977 | BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); |
2978 | ||
ac27a0ec DK |
2979 | /* First, test for the existence of a valid inode on disk. Bad |
2980 | * things happen if we iget() an unused inode, as the subsequent | |
2981 | * iput() will try to delete it. */ | |
2982 | ||
1d1fe1ee DH |
2983 | journal_inode = ext4_iget(sb, journal_inum); |
2984 | if (IS_ERR(journal_inode)) { | |
b31e1552 | 2985 | ext4_msg(sb, KERN_ERR, "no journal found"); |
ac27a0ec DK |
2986 | return NULL; |
2987 | } | |
2988 | if (!journal_inode->i_nlink) { | |
2989 | make_bad_inode(journal_inode); | |
2990 | iput(journal_inode); | |
b31e1552 | 2991 | ext4_msg(sb, KERN_ERR, "journal inode is deleted"); |
ac27a0ec DK |
2992 | return NULL; |
2993 | } | |
2994 | ||
e5f8eab8 | 2995 | jbd_debug(2, "Journal inode found at %p: %lld bytes\n", |
ac27a0ec | 2996 | journal_inode, journal_inode->i_size); |
1d1fe1ee | 2997 | if (!S_ISREG(journal_inode->i_mode)) { |
b31e1552 | 2998 | ext4_msg(sb, KERN_ERR, "invalid journal inode"); |
ac27a0ec DK |
2999 | iput(journal_inode); |
3000 | return NULL; | |
3001 | } | |
3002 | ||
dab291af | 3003 | journal = jbd2_journal_init_inode(journal_inode); |
ac27a0ec | 3004 | if (!journal) { |
b31e1552 | 3005 | ext4_msg(sb, KERN_ERR, "Could not load journal inode"); |
ac27a0ec DK |
3006 | iput(journal_inode); |
3007 | return NULL; | |
3008 | } | |
3009 | journal->j_private = sb; | |
617ba13b | 3010 | ext4_init_journal_params(sb, journal); |
ac27a0ec DK |
3011 | return journal; |
3012 | } | |
3013 | ||
617ba13b | 3014 | static journal_t *ext4_get_dev_journal(struct super_block *sb, |
ac27a0ec DK |
3015 | dev_t j_dev) |
3016 | { | |
2b2d6d01 | 3017 | struct buffer_head *bh; |
ac27a0ec | 3018 | journal_t *journal; |
617ba13b MC |
3019 | ext4_fsblk_t start; |
3020 | ext4_fsblk_t len; | |
ac27a0ec | 3021 | int hblock, blocksize; |
617ba13b | 3022 | ext4_fsblk_t sb_block; |
ac27a0ec | 3023 | unsigned long offset; |
2b2d6d01 | 3024 | struct ext4_super_block *es; |
ac27a0ec DK |
3025 | struct block_device *bdev; |
3026 | ||
0390131b FM |
3027 | BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); |
3028 | ||
b31e1552 | 3029 | bdev = ext4_blkdev_get(j_dev, sb); |
ac27a0ec DK |
3030 | if (bdev == NULL) |
3031 | return NULL; | |
3032 | ||
3033 | if (bd_claim(bdev, sb)) { | |
b31e1552 ES |
3034 | ext4_msg(sb, KERN_ERR, |
3035 | "failed to claim external journal device"); | |
9a1c3542 | 3036 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
ac27a0ec DK |
3037 | return NULL; |
3038 | } | |
3039 | ||
3040 | blocksize = sb->s_blocksize; | |
e1defc4f | 3041 | hblock = bdev_logical_block_size(bdev); |
ac27a0ec | 3042 | if (blocksize < hblock) { |
b31e1552 ES |
3043 | ext4_msg(sb, KERN_ERR, |
3044 | "blocksize too small for journal device"); | |
ac27a0ec DK |
3045 | goto out_bdev; |
3046 | } | |
3047 | ||
617ba13b MC |
3048 | sb_block = EXT4_MIN_BLOCK_SIZE / blocksize; |
3049 | offset = EXT4_MIN_BLOCK_SIZE % blocksize; | |
ac27a0ec DK |
3050 | set_blocksize(bdev, blocksize); |
3051 | if (!(bh = __bread(bdev, sb_block, blocksize))) { | |
b31e1552 ES |
3052 | ext4_msg(sb, KERN_ERR, "couldn't read superblock of " |
3053 | "external journal"); | |
ac27a0ec DK |
3054 | goto out_bdev; |
3055 | } | |
3056 | ||
617ba13b MC |
3057 | es = (struct ext4_super_block *) (((char *)bh->b_data) + offset); |
3058 | if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) || | |
ac27a0ec | 3059 | !(le32_to_cpu(es->s_feature_incompat) & |
617ba13b | 3060 | EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) { |
b31e1552 ES |
3061 | ext4_msg(sb, KERN_ERR, "external journal has " |
3062 | "bad superblock"); | |
ac27a0ec DK |
3063 | brelse(bh); |
3064 | goto out_bdev; | |
3065 | } | |
3066 | ||
617ba13b | 3067 | if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) { |
b31e1552 | 3068 | ext4_msg(sb, KERN_ERR, "journal UUID does not match"); |
ac27a0ec DK |
3069 | brelse(bh); |
3070 | goto out_bdev; | |
3071 | } | |
3072 | ||
bd81d8ee | 3073 | len = ext4_blocks_count(es); |
ac27a0ec DK |
3074 | start = sb_block + 1; |
3075 | brelse(bh); /* we're done with the superblock */ | |
3076 | ||
dab291af | 3077 | journal = jbd2_journal_init_dev(bdev, sb->s_bdev, |
ac27a0ec DK |
3078 | start, len, blocksize); |
3079 | if (!journal) { | |
b31e1552 | 3080 | ext4_msg(sb, KERN_ERR, "failed to create device journal"); |
ac27a0ec DK |
3081 | goto out_bdev; |
3082 | } | |
3083 | journal->j_private = sb; | |
3084 | ll_rw_block(READ, 1, &journal->j_sb_buffer); | |
3085 | wait_on_buffer(journal->j_sb_buffer); | |
3086 | if (!buffer_uptodate(journal->j_sb_buffer)) { | |
b31e1552 | 3087 | ext4_msg(sb, KERN_ERR, "I/O error on journal device"); |
ac27a0ec DK |
3088 | goto out_journal; |
3089 | } | |
3090 | if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) { | |
b31e1552 ES |
3091 | ext4_msg(sb, KERN_ERR, "External journal has more than one " |
3092 | "user (unsupported) - %d", | |
ac27a0ec DK |
3093 | be32_to_cpu(journal->j_superblock->s_nr_users)); |
3094 | goto out_journal; | |
3095 | } | |
617ba13b MC |
3096 | EXT4_SB(sb)->journal_bdev = bdev; |
3097 | ext4_init_journal_params(sb, journal); | |
ac27a0ec | 3098 | return journal; |
0b8e58a1 | 3099 | |
ac27a0ec | 3100 | out_journal: |
dab291af | 3101 | jbd2_journal_destroy(journal); |
ac27a0ec | 3102 | out_bdev: |
617ba13b | 3103 | ext4_blkdev_put(bdev); |
ac27a0ec DK |
3104 | return NULL; |
3105 | } | |
3106 | ||
617ba13b MC |
3107 | static int ext4_load_journal(struct super_block *sb, |
3108 | struct ext4_super_block *es, | |
ac27a0ec DK |
3109 | unsigned long journal_devnum) |
3110 | { | |
3111 | journal_t *journal; | |
3112 | unsigned int journal_inum = le32_to_cpu(es->s_journal_inum); | |
3113 | dev_t journal_dev; | |
3114 | int err = 0; | |
3115 | int really_read_only; | |
3116 | ||
0390131b FM |
3117 | BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); |
3118 | ||
ac27a0ec DK |
3119 | if (journal_devnum && |
3120 | journal_devnum != le32_to_cpu(es->s_journal_dev)) { | |
b31e1552 ES |
3121 | ext4_msg(sb, KERN_INFO, "external journal device major/minor " |
3122 | "numbers have changed"); | |
ac27a0ec DK |
3123 | journal_dev = new_decode_dev(journal_devnum); |
3124 | } else | |
3125 | journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev)); | |
3126 | ||
3127 | really_read_only = bdev_read_only(sb->s_bdev); | |
3128 | ||
3129 | /* | |
3130 | * Are we loading a blank journal or performing recovery after a | |
3131 | * crash? For recovery, we need to check in advance whether we | |
3132 | * can get read-write access to the device. | |
3133 | */ | |
617ba13b | 3134 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) { |
ac27a0ec | 3135 | if (sb->s_flags & MS_RDONLY) { |
b31e1552 ES |
3136 | ext4_msg(sb, KERN_INFO, "INFO: recovery " |
3137 | "required on readonly filesystem"); | |
ac27a0ec | 3138 | if (really_read_only) { |
b31e1552 ES |
3139 | ext4_msg(sb, KERN_ERR, "write access " |
3140 | "unavailable, cannot proceed"); | |
ac27a0ec DK |
3141 | return -EROFS; |
3142 | } | |
b31e1552 ES |
3143 | ext4_msg(sb, KERN_INFO, "write access will " |
3144 | "be enabled during recovery"); | |
ac27a0ec DK |
3145 | } |
3146 | } | |
3147 | ||
3148 | if (journal_inum && journal_dev) { | |
b31e1552 ES |
3149 | ext4_msg(sb, KERN_ERR, "filesystem has both journal " |
3150 | "and inode journals!"); | |
ac27a0ec DK |
3151 | return -EINVAL; |
3152 | } | |
3153 | ||
3154 | if (journal_inum) { | |
617ba13b | 3155 | if (!(journal = ext4_get_journal(sb, journal_inum))) |
ac27a0ec DK |
3156 | return -EINVAL; |
3157 | } else { | |
617ba13b | 3158 | if (!(journal = ext4_get_dev_journal(sb, journal_dev))) |
ac27a0ec DK |
3159 | return -EINVAL; |
3160 | } | |
3161 | ||
4776004f | 3162 | if (journal->j_flags & JBD2_BARRIER) |
b31e1552 | 3163 | ext4_msg(sb, KERN_INFO, "barriers enabled"); |
4776004f | 3164 | else |
b31e1552 | 3165 | ext4_msg(sb, KERN_INFO, "barriers disabled"); |
4776004f | 3166 | |
ac27a0ec | 3167 | if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) { |
dab291af | 3168 | err = jbd2_journal_update_format(journal); |
ac27a0ec | 3169 | if (err) { |
b31e1552 | 3170 | ext4_msg(sb, KERN_ERR, "error updating journal"); |
dab291af | 3171 | jbd2_journal_destroy(journal); |
ac27a0ec DK |
3172 | return err; |
3173 | } | |
3174 | } | |
3175 | ||
617ba13b | 3176 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) |
dab291af | 3177 | err = jbd2_journal_wipe(journal, !really_read_only); |
ac27a0ec | 3178 | if (!err) |
dab291af | 3179 | err = jbd2_journal_load(journal); |
ac27a0ec DK |
3180 | |
3181 | if (err) { | |
b31e1552 | 3182 | ext4_msg(sb, KERN_ERR, "error loading journal"); |
dab291af | 3183 | jbd2_journal_destroy(journal); |
ac27a0ec DK |
3184 | return err; |
3185 | } | |
3186 | ||
617ba13b MC |
3187 | EXT4_SB(sb)->s_journal = journal; |
3188 | ext4_clear_journal_err(sb, es); | |
ac27a0ec DK |
3189 | |
3190 | if (journal_devnum && | |
3191 | journal_devnum != le32_to_cpu(es->s_journal_dev)) { | |
3192 | es->s_journal_dev = cpu_to_le32(journal_devnum); | |
ac27a0ec DK |
3193 | |
3194 | /* Make sure we flush the recovery flag to disk. */ | |
e2d67052 | 3195 | ext4_commit_super(sb, 1); |
ac27a0ec DK |
3196 | } |
3197 | ||
3198 | return 0; | |
3199 | } | |
3200 | ||
e2d67052 | 3201 | static int ext4_commit_super(struct super_block *sb, int sync) |
ac27a0ec | 3202 | { |
e2d67052 | 3203 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
617ba13b | 3204 | struct buffer_head *sbh = EXT4_SB(sb)->s_sbh; |
c4be0c1d | 3205 | int error = 0; |
ac27a0ec DK |
3206 | |
3207 | if (!sbh) | |
c4be0c1d | 3208 | return error; |
914258bf TT |
3209 | if (buffer_write_io_error(sbh)) { |
3210 | /* | |
3211 | * Oh, dear. A previous attempt to write the | |
3212 | * superblock failed. This could happen because the | |
3213 | * USB device was yanked out. Or it could happen to | |
3214 | * be a transient write error and maybe the block will | |
3215 | * be remapped. Nothing we can do but to retry the | |
3216 | * write and hope for the best. | |
3217 | */ | |
b31e1552 ES |
3218 | ext4_msg(sb, KERN_ERR, "previous I/O error to " |
3219 | "superblock detected"); | |
914258bf TT |
3220 | clear_buffer_write_io_error(sbh); |
3221 | set_buffer_uptodate(sbh); | |
3222 | } | |
ac27a0ec | 3223 | es->s_wtime = cpu_to_le32(get_seconds()); |
afc32f7e TT |
3224 | es->s_kbytes_written = |
3225 | cpu_to_le64(EXT4_SB(sb)->s_kbytes_written + | |
3226 | ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) - | |
3227 | EXT4_SB(sb)->s_sectors_written_start) >> 1)); | |
5d1b1b3f AK |
3228 | ext4_free_blocks_count_set(es, percpu_counter_sum_positive( |
3229 | &EXT4_SB(sb)->s_freeblocks_counter)); | |
3230 | es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive( | |
3231 | &EXT4_SB(sb)->s_freeinodes_counter)); | |
7234ab2a | 3232 | sb->s_dirt = 0; |
ac27a0ec DK |
3233 | BUFFER_TRACE(sbh, "marking dirty"); |
3234 | mark_buffer_dirty(sbh); | |
914258bf | 3235 | if (sync) { |
c4be0c1d TS |
3236 | error = sync_dirty_buffer(sbh); |
3237 | if (error) | |
3238 | return error; | |
3239 | ||
3240 | error = buffer_write_io_error(sbh); | |
3241 | if (error) { | |
b31e1552 ES |
3242 | ext4_msg(sb, KERN_ERR, "I/O error while writing " |
3243 | "superblock"); | |
914258bf TT |
3244 | clear_buffer_write_io_error(sbh); |
3245 | set_buffer_uptodate(sbh); | |
3246 | } | |
3247 | } | |
c4be0c1d | 3248 | return error; |
ac27a0ec DK |
3249 | } |
3250 | ||
ac27a0ec DK |
3251 | /* |
3252 | * Have we just finished recovery? If so, and if we are mounting (or | |
3253 | * remounting) the filesystem readonly, then we will end up with a | |
3254 | * consistent fs on disk. Record that fact. | |
3255 | */ | |
2b2d6d01 TT |
3256 | static void ext4_mark_recovery_complete(struct super_block *sb, |
3257 | struct ext4_super_block *es) | |
ac27a0ec | 3258 | { |
617ba13b | 3259 | journal_t *journal = EXT4_SB(sb)->s_journal; |
ac27a0ec | 3260 | |
0390131b FM |
3261 | if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) { |
3262 | BUG_ON(journal != NULL); | |
3263 | return; | |
3264 | } | |
dab291af | 3265 | jbd2_journal_lock_updates(journal); |
7ffe1ea8 HK |
3266 | if (jbd2_journal_flush(journal) < 0) |
3267 | goto out; | |
3268 | ||
617ba13b | 3269 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) && |
ac27a0ec | 3270 | sb->s_flags & MS_RDONLY) { |
617ba13b | 3271 | EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); |
e2d67052 | 3272 | ext4_commit_super(sb, 1); |
ac27a0ec | 3273 | } |
7ffe1ea8 HK |
3274 | |
3275 | out: | |
dab291af | 3276 | jbd2_journal_unlock_updates(journal); |
ac27a0ec DK |
3277 | } |
3278 | ||
3279 | /* | |
3280 | * If we are mounting (or read-write remounting) a filesystem whose journal | |
3281 | * has recorded an error from a previous lifetime, move that error to the | |
3282 | * main filesystem now. | |
3283 | */ | |
2b2d6d01 TT |
3284 | static void ext4_clear_journal_err(struct super_block *sb, |
3285 | struct ext4_super_block *es) | |
ac27a0ec DK |
3286 | { |
3287 | journal_t *journal; | |
3288 | int j_errno; | |
3289 | const char *errstr; | |
3290 | ||
0390131b FM |
3291 | BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); |
3292 | ||
617ba13b | 3293 | journal = EXT4_SB(sb)->s_journal; |
ac27a0ec DK |
3294 | |
3295 | /* | |
3296 | * Now check for any error status which may have been recorded in the | |
617ba13b | 3297 | * journal by a prior ext4_error() or ext4_abort() |
ac27a0ec DK |
3298 | */ |
3299 | ||
dab291af | 3300 | j_errno = jbd2_journal_errno(journal); |
ac27a0ec DK |
3301 | if (j_errno) { |
3302 | char nbuf[16]; | |
3303 | ||
617ba13b | 3304 | errstr = ext4_decode_error(sb, j_errno, nbuf); |
46e665e9 | 3305 | ext4_warning(sb, __func__, "Filesystem error recorded " |
ac27a0ec | 3306 | "from previous mount: %s", errstr); |
46e665e9 | 3307 | ext4_warning(sb, __func__, "Marking fs in need of " |
ac27a0ec DK |
3308 | "filesystem check."); |
3309 | ||
617ba13b MC |
3310 | EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; |
3311 | es->s_state |= cpu_to_le16(EXT4_ERROR_FS); | |
e2d67052 | 3312 | ext4_commit_super(sb, 1); |
ac27a0ec | 3313 | |
dab291af | 3314 | jbd2_journal_clear_err(journal); |
ac27a0ec DK |
3315 | } |
3316 | } | |
3317 | ||
3318 | /* | |
3319 | * Force the running and committing transactions to commit, | |
3320 | * and wait on the commit. | |
3321 | */ | |
617ba13b | 3322 | int ext4_force_commit(struct super_block *sb) |
ac27a0ec DK |
3323 | { |
3324 | journal_t *journal; | |
0390131b | 3325 | int ret = 0; |
ac27a0ec DK |
3326 | |
3327 | if (sb->s_flags & MS_RDONLY) | |
3328 | return 0; | |
3329 | ||
617ba13b | 3330 | journal = EXT4_SB(sb)->s_journal; |
7234ab2a | 3331 | if (journal) |
0390131b | 3332 | ret = ext4_journal_force_commit(journal); |
0390131b | 3333 | |
ac27a0ec DK |
3334 | return ret; |
3335 | } | |
3336 | ||
2b2d6d01 | 3337 | static void ext4_write_super(struct super_block *sb) |
ac27a0ec | 3338 | { |
9ca92389 | 3339 | ext4_commit_super(sb, 1); |
ac27a0ec DK |
3340 | } |
3341 | ||
617ba13b | 3342 | static int ext4_sync_fs(struct super_block *sb, int wait) |
ac27a0ec | 3343 | { |
14ce0cb4 | 3344 | int ret = 0; |
9eddacf9 | 3345 | tid_t target; |
ac27a0ec | 3346 | |
ede86cc4 | 3347 | trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait); |
9ca92389 TT |
3348 | if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) { |
3349 | if (wait) | |
3350 | jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target); | |
0390131b | 3351 | } |
14ce0cb4 | 3352 | return ret; |
ac27a0ec DK |
3353 | } |
3354 | ||
3355 | /* | |
3356 | * LVM calls this function before a (read-only) snapshot is created. This | |
3357 | * gives us a chance to flush the journal completely and mark the fs clean. | |
3358 | */ | |
c4be0c1d | 3359 | static int ext4_freeze(struct super_block *sb) |
ac27a0ec | 3360 | { |
c4be0c1d TS |
3361 | int error = 0; |
3362 | journal_t *journal; | |
ac27a0ec | 3363 | |
9ca92389 TT |
3364 | if (sb->s_flags & MS_RDONLY) |
3365 | return 0; | |
ac27a0ec | 3366 | |
9ca92389 | 3367 | journal = EXT4_SB(sb)->s_journal; |
7ffe1ea8 | 3368 | |
9ca92389 TT |
3369 | /* Now we set up the journal barrier. */ |
3370 | jbd2_journal_lock_updates(journal); | |
ac27a0ec | 3371 | |
9ca92389 TT |
3372 | /* |
3373 | * Don't clear the needs_recovery flag if we failed to flush | |
3374 | * the journal. | |
3375 | */ | |
3376 | error = jbd2_journal_flush(journal); | |
3377 | if (error < 0) { | |
3378 | out: | |
3379 | jbd2_journal_unlock_updates(journal); | |
3380 | return error; | |
ac27a0ec | 3381 | } |
9ca92389 TT |
3382 | |
3383 | /* Journal blocked and flushed, clear needs_recovery flag. */ | |
3384 | EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); | |
3385 | error = ext4_commit_super(sb, 1); | |
3386 | if (error) | |
3387 | goto out; | |
c4be0c1d | 3388 | return 0; |
ac27a0ec DK |
3389 | } |
3390 | ||
3391 | /* | |
3392 | * Called by LVM after the snapshot is done. We need to reset the RECOVER | |
3393 | * flag here, even though the filesystem is not technically dirty yet. | |
3394 | */ | |
c4be0c1d | 3395 | static int ext4_unfreeze(struct super_block *sb) |
ac27a0ec | 3396 | { |
9ca92389 TT |
3397 | if (sb->s_flags & MS_RDONLY) |
3398 | return 0; | |
3399 | ||
3400 | lock_super(sb); | |
3401 | /* Reset the needs_recovery flag before the fs is unlocked. */ | |
3402 | EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); | |
3403 | ext4_commit_super(sb, 1); | |
3404 | unlock_super(sb); | |
3405 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
c4be0c1d | 3406 | return 0; |
ac27a0ec DK |
3407 | } |
3408 | ||
2b2d6d01 | 3409 | static int ext4_remount(struct super_block *sb, int *flags, char *data) |
ac27a0ec | 3410 | { |
2b2d6d01 | 3411 | struct ext4_super_block *es; |
617ba13b MC |
3412 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
3413 | ext4_fsblk_t n_blocks_count = 0; | |
ac27a0ec | 3414 | unsigned long old_sb_flags; |
617ba13b | 3415 | struct ext4_mount_options old_opts; |
8a266467 | 3416 | ext4_group_t g; |
b3881f74 | 3417 | unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; |
ac27a0ec DK |
3418 | int err; |
3419 | #ifdef CONFIG_QUOTA | |
3420 | int i; | |
3421 | #endif | |
3422 | ||
3423 | /* Store the original options */ | |
3424 | old_sb_flags = sb->s_flags; | |
3425 | old_opts.s_mount_opt = sbi->s_mount_opt; | |
3426 | old_opts.s_resuid = sbi->s_resuid; | |
3427 | old_opts.s_resgid = sbi->s_resgid; | |
3428 | old_opts.s_commit_interval = sbi->s_commit_interval; | |
30773840 TT |
3429 | old_opts.s_min_batch_time = sbi->s_min_batch_time; |
3430 | old_opts.s_max_batch_time = sbi->s_max_batch_time; | |
ac27a0ec DK |
3431 | #ifdef CONFIG_QUOTA |
3432 | old_opts.s_jquota_fmt = sbi->s_jquota_fmt; | |
3433 | for (i = 0; i < MAXQUOTAS; i++) | |
3434 | old_opts.s_qf_names[i] = sbi->s_qf_names[i]; | |
3435 | #endif | |
b3881f74 TT |
3436 | if (sbi->s_journal && sbi->s_journal->j_task->io_context) |
3437 | journal_ioprio = sbi->s_journal->j_task->io_context->ioprio; | |
ac27a0ec DK |
3438 | |
3439 | /* | |
3440 | * Allow the "check" option to be passed as a remount option. | |
3441 | */ | |
b3881f74 TT |
3442 | if (!parse_options(data, sb, NULL, &journal_ioprio, |
3443 | &n_blocks_count, 1)) { | |
ac27a0ec DK |
3444 | err = -EINVAL; |
3445 | goto restore_opts; | |
3446 | } | |
3447 | ||
617ba13b | 3448 | if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) |
46e665e9 | 3449 | ext4_abort(sb, __func__, "Abort forced by user"); |
ac27a0ec DK |
3450 | |
3451 | sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | | |
617ba13b | 3452 | ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0); |
ac27a0ec DK |
3453 | |
3454 | es = sbi->s_es; | |
3455 | ||
b3881f74 | 3456 | if (sbi->s_journal) { |
0390131b | 3457 | ext4_init_journal_params(sb, sbi->s_journal); |
b3881f74 TT |
3458 | set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); |
3459 | } | |
ac27a0ec DK |
3460 | |
3461 | if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) || | |
bd81d8ee | 3462 | n_blocks_count > ext4_blocks_count(es)) { |
617ba13b | 3463 | if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) { |
ac27a0ec DK |
3464 | err = -EROFS; |
3465 | goto restore_opts; | |
3466 | } | |
3467 | ||
3468 | if (*flags & MS_RDONLY) { | |
3469 | /* | |
3470 | * First of all, the unconditional stuff we have to do | |
3471 | * to disable replay of the journal when we next remount | |
3472 | */ | |
3473 | sb->s_flags |= MS_RDONLY; | |
3474 | ||
3475 | /* | |
3476 | * OK, test if we are remounting a valid rw partition | |
3477 | * readonly, and if so set the rdonly flag and then | |
3478 | * mark the partition as valid again. | |
3479 | */ | |
617ba13b MC |
3480 | if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && |
3481 | (sbi->s_mount_state & EXT4_VALID_FS)) | |
ac27a0ec DK |
3482 | es->s_state = cpu_to_le16(sbi->s_mount_state); |
3483 | ||
a63c9eb2 | 3484 | if (sbi->s_journal) |
0390131b | 3485 | ext4_mark_recovery_complete(sb, es); |
ac27a0ec | 3486 | } else { |
3a06d778 | 3487 | int ret; |
617ba13b MC |
3488 | if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb, |
3489 | ~EXT4_FEATURE_RO_COMPAT_SUPP))) { | |
b31e1552 | 3490 | ext4_msg(sb, KERN_WARNING, "couldn't " |
ac27a0ec | 3491 | "remount RDWR because of unsupported " |
b31e1552 | 3492 | "optional features (%x)", |
3a06d778 AK |
3493 | (le32_to_cpu(sbi->s_es->s_feature_ro_compat) & |
3494 | ~EXT4_FEATURE_RO_COMPAT_SUPP)); | |
ac27a0ec DK |
3495 | err = -EROFS; |
3496 | goto restore_opts; | |
3497 | } | |
ead6596b | 3498 | |
8a266467 TT |
3499 | /* |
3500 | * Make sure the group descriptor checksums | |
0b8e58a1 | 3501 | * are sane. If they aren't, refuse to remount r/w. |
8a266467 TT |
3502 | */ |
3503 | for (g = 0; g < sbi->s_groups_count; g++) { | |
3504 | struct ext4_group_desc *gdp = | |
3505 | ext4_get_group_desc(sb, g, NULL); | |
3506 | ||
3507 | if (!ext4_group_desc_csum_verify(sbi, g, gdp)) { | |
b31e1552 ES |
3508 | ext4_msg(sb, KERN_ERR, |
3509 | "ext4_remount: Checksum for group %u failed (%u!=%u)", | |
8a266467 TT |
3510 | g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)), |
3511 | le16_to_cpu(gdp->bg_checksum)); | |
3512 | err = -EINVAL; | |
3513 | goto restore_opts; | |
3514 | } | |
3515 | } | |
3516 | ||
ead6596b ES |
3517 | /* |
3518 | * If we have an unprocessed orphan list hanging | |
3519 | * around from a previously readonly bdev mount, | |
3520 | * require a full umount/remount for now. | |
3521 | */ | |
3522 | if (es->s_last_orphan) { | |
b31e1552 | 3523 | ext4_msg(sb, KERN_WARNING, "Couldn't " |
ead6596b ES |
3524 | "remount RDWR because of unprocessed " |
3525 | "orphan inode list. Please " | |
b31e1552 | 3526 | "umount/remount instead"); |
ead6596b ES |
3527 | err = -EINVAL; |
3528 | goto restore_opts; | |
3529 | } | |
3530 | ||
ac27a0ec DK |
3531 | /* |
3532 | * Mounting a RDONLY partition read-write, so reread | |
3533 | * and store the current valid flag. (It may have | |
3534 | * been changed by e2fsck since we originally mounted | |
3535 | * the partition.) | |
3536 | */ | |
0390131b FM |
3537 | if (sbi->s_journal) |
3538 | ext4_clear_journal_err(sb, es); | |
ac27a0ec | 3539 | sbi->s_mount_state = le16_to_cpu(es->s_state); |
617ba13b | 3540 | if ((err = ext4_group_extend(sb, es, n_blocks_count))) |
ac27a0ec | 3541 | goto restore_opts; |
2b2d6d01 | 3542 | if (!ext4_setup_super(sb, es, 0)) |
ac27a0ec DK |
3543 | sb->s_flags &= ~MS_RDONLY; |
3544 | } | |
3545 | } | |
6fd058f7 | 3546 | ext4_setup_system_zone(sb); |
0390131b | 3547 | if (sbi->s_journal == NULL) |
e2d67052 | 3548 | ext4_commit_super(sb, 1); |
0390131b | 3549 | |
ac27a0ec DK |
3550 | #ifdef CONFIG_QUOTA |
3551 | /* Release old quota file names */ | |
3552 | for (i = 0; i < MAXQUOTAS; i++) | |
3553 | if (old_opts.s_qf_names[i] && | |
3554 | old_opts.s_qf_names[i] != sbi->s_qf_names[i]) | |
3555 | kfree(old_opts.s_qf_names[i]); | |
3556 | #endif | |
3557 | return 0; | |
0b8e58a1 | 3558 | |
ac27a0ec DK |
3559 | restore_opts: |
3560 | sb->s_flags = old_sb_flags; | |
3561 | sbi->s_mount_opt = old_opts.s_mount_opt; | |
3562 | sbi->s_resuid = old_opts.s_resuid; | |
3563 | sbi->s_resgid = old_opts.s_resgid; | |
3564 | sbi->s_commit_interval = old_opts.s_commit_interval; | |
30773840 TT |
3565 | sbi->s_min_batch_time = old_opts.s_min_batch_time; |
3566 | sbi->s_max_batch_time = old_opts.s_max_batch_time; | |
ac27a0ec DK |
3567 | #ifdef CONFIG_QUOTA |
3568 | sbi->s_jquota_fmt = old_opts.s_jquota_fmt; | |
3569 | for (i = 0; i < MAXQUOTAS; i++) { | |
3570 | if (sbi->s_qf_names[i] && | |
3571 | old_opts.s_qf_names[i] != sbi->s_qf_names[i]) | |
3572 | kfree(sbi->s_qf_names[i]); | |
3573 | sbi->s_qf_names[i] = old_opts.s_qf_names[i]; | |
3574 | } | |
3575 | #endif | |
3576 | return err; | |
3577 | } | |
3578 | ||
2b2d6d01 | 3579 | static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) |
ac27a0ec DK |
3580 | { |
3581 | struct super_block *sb = dentry->d_sb; | |
617ba13b MC |
3582 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
3583 | struct ext4_super_block *es = sbi->s_es; | |
960cc398 | 3584 | u64 fsid; |
ac27a0ec | 3585 | |
5e70030d BP |
3586 | if (test_opt(sb, MINIX_DF)) { |
3587 | sbi->s_overhead_last = 0; | |
6bc9feff | 3588 | } else if (sbi->s_blocks_last != ext4_blocks_count(es)) { |
8df9675f | 3589 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
5e70030d | 3590 | ext4_fsblk_t overhead = 0; |
ac27a0ec DK |
3591 | |
3592 | /* | |
5e70030d BP |
3593 | * Compute the overhead (FS structures). This is constant |
3594 | * for a given filesystem unless the number of block groups | |
3595 | * changes so we cache the previous value until it does. | |
ac27a0ec DK |
3596 | */ |
3597 | ||
3598 | /* | |
3599 | * All of the blocks before first_data_block are | |
3600 | * overhead | |
3601 | */ | |
3602 | overhead = le32_to_cpu(es->s_first_data_block); | |
3603 | ||
3604 | /* | |
3605 | * Add the overhead attributed to the superblock and | |
3606 | * block group descriptors. If the sparse superblocks | |
3607 | * feature is turned on, then not all groups have this. | |
3608 | */ | |
3609 | for (i = 0; i < ngroups; i++) { | |
617ba13b MC |
3610 | overhead += ext4_bg_has_super(sb, i) + |
3611 | ext4_bg_num_gdb(sb, i); | |
ac27a0ec DK |
3612 | cond_resched(); |
3613 | } | |
3614 | ||
3615 | /* | |
3616 | * Every block group has an inode bitmap, a block | |
3617 | * bitmap, and an inode table. | |
3618 | */ | |
5e70030d BP |
3619 | overhead += ngroups * (2 + sbi->s_itb_per_group); |
3620 | sbi->s_overhead_last = overhead; | |
3621 | smp_wmb(); | |
6bc9feff | 3622 | sbi->s_blocks_last = ext4_blocks_count(es); |
ac27a0ec DK |
3623 | } |
3624 | ||
617ba13b | 3625 | buf->f_type = EXT4_SUPER_MAGIC; |
ac27a0ec | 3626 | buf->f_bsize = sb->s_blocksize; |
5e70030d | 3627 | buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last; |
6bc6e63f AK |
3628 | buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) - |
3629 | percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter); | |
308ba3ec | 3630 | ext4_free_blocks_count_set(es, buf->f_bfree); |
bd81d8ee LV |
3631 | buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es); |
3632 | if (buf->f_bfree < ext4_r_blocks_count(es)) | |
ac27a0ec DK |
3633 | buf->f_bavail = 0; |
3634 | buf->f_files = le32_to_cpu(es->s_inodes_count); | |
52d9f3b4 | 3635 | buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter); |
5e70030d | 3636 | es->s_free_inodes_count = cpu_to_le32(buf->f_ffree); |
617ba13b | 3637 | buf->f_namelen = EXT4_NAME_LEN; |
960cc398 PE |
3638 | fsid = le64_to_cpup((void *)es->s_uuid) ^ |
3639 | le64_to_cpup((void *)es->s_uuid + sizeof(u64)); | |
3640 | buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL; | |
3641 | buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL; | |
0b8e58a1 | 3642 | |
ac27a0ec DK |
3643 | return 0; |
3644 | } | |
3645 | ||
0b8e58a1 AD |
3646 | /* Helper function for writing quotas on sync - we need to start transaction |
3647 | * before quota file is locked for write. Otherwise the are possible deadlocks: | |
ac27a0ec | 3648 | * Process 1 Process 2 |
617ba13b | 3649 | * ext4_create() quota_sync() |
a269eb18 JK |
3650 | * jbd2_journal_start() write_dquot() |
3651 | * vfs_dq_init() down(dqio_mutex) | |
dab291af | 3652 | * down(dqio_mutex) jbd2_journal_start() |
ac27a0ec DK |
3653 | * |
3654 | */ | |
3655 | ||
3656 | #ifdef CONFIG_QUOTA | |
3657 | ||
3658 | static inline struct inode *dquot_to_inode(struct dquot *dquot) | |
3659 | { | |
3660 | return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type]; | |
3661 | } | |
3662 | ||
617ba13b | 3663 | static int ext4_write_dquot(struct dquot *dquot) |
ac27a0ec DK |
3664 | { |
3665 | int ret, err; | |
3666 | handle_t *handle; | |
3667 | struct inode *inode; | |
3668 | ||
3669 | inode = dquot_to_inode(dquot); | |
617ba13b | 3670 | handle = ext4_journal_start(inode, |
0b8e58a1 | 3671 | EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb)); |
ac27a0ec DK |
3672 | if (IS_ERR(handle)) |
3673 | return PTR_ERR(handle); | |
3674 | ret = dquot_commit(dquot); | |
617ba13b | 3675 | err = ext4_journal_stop(handle); |
ac27a0ec DK |
3676 | if (!ret) |
3677 | ret = err; | |
3678 | return ret; | |
3679 | } | |
3680 | ||
617ba13b | 3681 | static int ext4_acquire_dquot(struct dquot *dquot) |
ac27a0ec DK |
3682 | { |
3683 | int ret, err; | |
3684 | handle_t *handle; | |
3685 | ||
617ba13b | 3686 | handle = ext4_journal_start(dquot_to_inode(dquot), |
0b8e58a1 | 3687 | EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb)); |
ac27a0ec DK |
3688 | if (IS_ERR(handle)) |
3689 | return PTR_ERR(handle); | |
3690 | ret = dquot_acquire(dquot); | |
617ba13b | 3691 | err = ext4_journal_stop(handle); |
ac27a0ec DK |
3692 | if (!ret) |
3693 | ret = err; | |
3694 | return ret; | |
3695 | } | |
3696 | ||
617ba13b | 3697 | static int ext4_release_dquot(struct dquot *dquot) |
ac27a0ec DK |
3698 | { |
3699 | int ret, err; | |
3700 | handle_t *handle; | |
3701 | ||
617ba13b | 3702 | handle = ext4_journal_start(dquot_to_inode(dquot), |
0b8e58a1 | 3703 | EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb)); |
9c3013e9 JK |
3704 | if (IS_ERR(handle)) { |
3705 | /* Release dquot anyway to avoid endless cycle in dqput() */ | |
3706 | dquot_release(dquot); | |
ac27a0ec | 3707 | return PTR_ERR(handle); |
9c3013e9 | 3708 | } |
ac27a0ec | 3709 | ret = dquot_release(dquot); |
617ba13b | 3710 | err = ext4_journal_stop(handle); |
ac27a0ec DK |
3711 | if (!ret) |
3712 | ret = err; | |
3713 | return ret; | |
3714 | } | |
3715 | ||
617ba13b | 3716 | static int ext4_mark_dquot_dirty(struct dquot *dquot) |
ac27a0ec | 3717 | { |
2c8be6b2 | 3718 | /* Are we journaling quotas? */ |
617ba13b MC |
3719 | if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] || |
3720 | EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) { | |
ac27a0ec | 3721 | dquot_mark_dquot_dirty(dquot); |
617ba13b | 3722 | return ext4_write_dquot(dquot); |
ac27a0ec DK |
3723 | } else { |
3724 | return dquot_mark_dquot_dirty(dquot); | |
3725 | } | |
3726 | } | |
3727 | ||
617ba13b | 3728 | static int ext4_write_info(struct super_block *sb, int type) |
ac27a0ec DK |
3729 | { |
3730 | int ret, err; | |
3731 | handle_t *handle; | |
3732 | ||
3733 | /* Data block + inode block */ | |
617ba13b | 3734 | handle = ext4_journal_start(sb->s_root->d_inode, 2); |
ac27a0ec DK |
3735 | if (IS_ERR(handle)) |
3736 | return PTR_ERR(handle); | |
3737 | ret = dquot_commit_info(sb, type); | |
617ba13b | 3738 | err = ext4_journal_stop(handle); |
ac27a0ec DK |
3739 | if (!ret) |
3740 | ret = err; | |
3741 | return ret; | |
3742 | } | |
3743 | ||
3744 | /* | |
3745 | * Turn on quotas during mount time - we need to find | |
3746 | * the quota file and such... | |
3747 | */ | |
617ba13b | 3748 | static int ext4_quota_on_mount(struct super_block *sb, int type) |
ac27a0ec | 3749 | { |
617ba13b | 3750 | return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type], |
0b8e58a1 | 3751 | EXT4_SB(sb)->s_jquota_fmt, type); |
ac27a0ec DK |
3752 | } |
3753 | ||
3754 | /* | |
3755 | * Standard function to be called on quota_on | |
3756 | */ | |
617ba13b | 3757 | static int ext4_quota_on(struct super_block *sb, int type, int format_id, |
8264613d | 3758 | char *name, int remount) |
ac27a0ec DK |
3759 | { |
3760 | int err; | |
8264613d | 3761 | struct path path; |
ac27a0ec DK |
3762 | |
3763 | if (!test_opt(sb, QUOTA)) | |
3764 | return -EINVAL; | |
8264613d | 3765 | /* When remounting, no checks are needed and in fact, name is NULL */ |
0623543b | 3766 | if (remount) |
8264613d | 3767 | return vfs_quota_on(sb, type, format_id, name, remount); |
0623543b | 3768 | |
8264613d | 3769 | err = kern_path(name, LOOKUP_FOLLOW, &path); |
ac27a0ec DK |
3770 | if (err) |
3771 | return err; | |
0623543b | 3772 | |
ac27a0ec | 3773 | /* Quotafile not on the same filesystem? */ |
8264613d AV |
3774 | if (path.mnt->mnt_sb != sb) { |
3775 | path_put(&path); | |
ac27a0ec DK |
3776 | return -EXDEV; |
3777 | } | |
0623543b JK |
3778 | /* Journaling quota? */ |
3779 | if (EXT4_SB(sb)->s_qf_names[type]) { | |
2b2d6d01 | 3780 | /* Quotafile not in fs root? */ |
8264613d | 3781 | if (path.dentry->d_parent != sb->s_root) |
b31e1552 ES |
3782 | ext4_msg(sb, KERN_WARNING, |
3783 | "Quota file not on filesystem root. " | |
3784 | "Journaled quota will not work"); | |
2b2d6d01 | 3785 | } |
0623543b JK |
3786 | |
3787 | /* | |
3788 | * When we journal data on quota file, we have to flush journal to see | |
3789 | * all updates to the file when we bypass pagecache... | |
3790 | */ | |
0390131b FM |
3791 | if (EXT4_SB(sb)->s_journal && |
3792 | ext4_should_journal_data(path.dentry->d_inode)) { | |
0623543b JK |
3793 | /* |
3794 | * We don't need to lock updates but journal_flush() could | |
3795 | * otherwise be livelocked... | |
3796 | */ | |
3797 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
7ffe1ea8 | 3798 | err = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
0623543b | 3799 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
7ffe1ea8 | 3800 | if (err) { |
8264613d | 3801 | path_put(&path); |
7ffe1ea8 HK |
3802 | return err; |
3803 | } | |
0623543b JK |
3804 | } |
3805 | ||
8264613d AV |
3806 | err = vfs_quota_on_path(sb, type, format_id, &path); |
3807 | path_put(&path); | |
77e69dac | 3808 | return err; |
ac27a0ec DK |
3809 | } |
3810 | ||
3811 | /* Read data from quotafile - avoid pagecache and such because we cannot afford | |
3812 | * acquiring the locks... As quota files are never truncated and quota code | |
3813 | * itself serializes the operations (and noone else should touch the files) | |
3814 | * we don't have to be afraid of races */ | |
617ba13b | 3815 | static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, |
ac27a0ec DK |
3816 | size_t len, loff_t off) |
3817 | { | |
3818 | struct inode *inode = sb_dqopt(sb)->files[type]; | |
725d26d3 | 3819 | ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); |
ac27a0ec DK |
3820 | int err = 0; |
3821 | int offset = off & (sb->s_blocksize - 1); | |
3822 | int tocopy; | |
3823 | size_t toread; | |
3824 | struct buffer_head *bh; | |
3825 | loff_t i_size = i_size_read(inode); | |
3826 | ||
3827 | if (off > i_size) | |
3828 | return 0; | |
3829 | if (off+len > i_size) | |
3830 | len = i_size-off; | |
3831 | toread = len; | |
3832 | while (toread > 0) { | |
3833 | tocopy = sb->s_blocksize - offset < toread ? | |
3834 | sb->s_blocksize - offset : toread; | |
617ba13b | 3835 | bh = ext4_bread(NULL, inode, blk, 0, &err); |
ac27a0ec DK |
3836 | if (err) |
3837 | return err; | |
3838 | if (!bh) /* A hole? */ | |
3839 | memset(data, 0, tocopy); | |
3840 | else | |
3841 | memcpy(data, bh->b_data+offset, tocopy); | |
3842 | brelse(bh); | |
3843 | offset = 0; | |
3844 | toread -= tocopy; | |
3845 | data += tocopy; | |
3846 | blk++; | |
3847 | } | |
3848 | return len; | |
3849 | } | |
3850 | ||
3851 | /* Write to quotafile (we know the transaction is already started and has | |
3852 | * enough credits) */ | |
617ba13b | 3853 | static ssize_t ext4_quota_write(struct super_block *sb, int type, |
ac27a0ec DK |
3854 | const char *data, size_t len, loff_t off) |
3855 | { | |
3856 | struct inode *inode = sb_dqopt(sb)->files[type]; | |
725d26d3 | 3857 | ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); |
ac27a0ec DK |
3858 | int err = 0; |
3859 | int offset = off & (sb->s_blocksize - 1); | |
3860 | int tocopy; | |
617ba13b | 3861 | int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL; |
ac27a0ec DK |
3862 | size_t towrite = len; |
3863 | struct buffer_head *bh; | |
3864 | handle_t *handle = journal_current_handle(); | |
3865 | ||
0390131b | 3866 | if (EXT4_SB(sb)->s_journal && !handle) { |
b31e1552 ES |
3867 | ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" |
3868 | " cancelled because transaction is not started", | |
9c3013e9 JK |
3869 | (unsigned long long)off, (unsigned long long)len); |
3870 | return -EIO; | |
3871 | } | |
ac27a0ec DK |
3872 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA); |
3873 | while (towrite > 0) { | |
3874 | tocopy = sb->s_blocksize - offset < towrite ? | |
3875 | sb->s_blocksize - offset : towrite; | |
617ba13b | 3876 | bh = ext4_bread(handle, inode, blk, 1, &err); |
ac27a0ec DK |
3877 | if (!bh) |
3878 | goto out; | |
3879 | if (journal_quota) { | |
617ba13b | 3880 | err = ext4_journal_get_write_access(handle, bh); |
ac27a0ec DK |
3881 | if (err) { |
3882 | brelse(bh); | |
3883 | goto out; | |
3884 | } | |
3885 | } | |
3886 | lock_buffer(bh); | |
3887 | memcpy(bh->b_data+offset, data, tocopy); | |
3888 | flush_dcache_page(bh->b_page); | |
3889 | unlock_buffer(bh); | |
3890 | if (journal_quota) | |
0390131b | 3891 | err = ext4_handle_dirty_metadata(handle, NULL, bh); |
ac27a0ec DK |
3892 | else { |
3893 | /* Always do at least ordered writes for quotas */ | |
678aaf48 | 3894 | err = ext4_jbd2_file_inode(handle, inode); |
ac27a0ec DK |
3895 | mark_buffer_dirty(bh); |
3896 | } | |
3897 | brelse(bh); | |
3898 | if (err) | |
3899 | goto out; | |
3900 | offset = 0; | |
3901 | towrite -= tocopy; | |
3902 | data += tocopy; | |
3903 | blk++; | |
3904 | } | |
3905 | out: | |
4d04e4fb JK |
3906 | if (len == towrite) { |
3907 | mutex_unlock(&inode->i_mutex); | |
ac27a0ec | 3908 | return err; |
4d04e4fb | 3909 | } |
ac27a0ec DK |
3910 | if (inode->i_size < off+len-towrite) { |
3911 | i_size_write(inode, off+len-towrite); | |
617ba13b | 3912 | EXT4_I(inode)->i_disksize = inode->i_size; |
ac27a0ec | 3913 | } |
ac27a0ec | 3914 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
617ba13b | 3915 | ext4_mark_inode_dirty(handle, inode); |
ac27a0ec DK |
3916 | mutex_unlock(&inode->i_mutex); |
3917 | return len - towrite; | |
3918 | } | |
3919 | ||
3920 | #endif | |
3921 | ||
0b8e58a1 AD |
3922 | static int ext4_get_sb(struct file_system_type *fs_type, int flags, |
3923 | const char *dev_name, void *data, struct vfsmount *mnt) | |
ac27a0ec | 3924 | { |
0b8e58a1 | 3925 | return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt); |
ac27a0ec DK |
3926 | } |
3927 | ||
03010a33 TT |
3928 | static struct file_system_type ext4_fs_type = { |
3929 | .owner = THIS_MODULE, | |
3930 | .name = "ext4", | |
3931 | .get_sb = ext4_get_sb, | |
3932 | .kill_sb = kill_block_super, | |
3933 | .fs_flags = FS_REQUIRES_DEV, | |
3934 | }; | |
3935 | ||
3936 | #ifdef CONFIG_EXT4DEV_COMPAT | |
0b8e58a1 AD |
3937 | static int ext4dev_get_sb(struct file_system_type *fs_type, int flags, |
3938 | const char *dev_name, void *data,struct vfsmount *mnt) | |
03010a33 | 3939 | { |
b31e1552 ES |
3940 | printk(KERN_WARNING "EXT4-fs (%s): Update your userspace programs " |
3941 | "to mount using ext4\n", dev_name); | |
3942 | printk(KERN_WARNING "EXT4-fs (%s): ext4dev backwards compatibility " | |
3943 | "will go away by 2.6.31\n", dev_name); | |
0b8e58a1 | 3944 | return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt); |
03010a33 TT |
3945 | } |
3946 | ||
617ba13b | 3947 | static struct file_system_type ext4dev_fs_type = { |
ac27a0ec | 3948 | .owner = THIS_MODULE, |
617ba13b | 3949 | .name = "ext4dev", |
03010a33 | 3950 | .get_sb = ext4dev_get_sb, |
ac27a0ec DK |
3951 | .kill_sb = kill_block_super, |
3952 | .fs_flags = FS_REQUIRES_DEV, | |
3953 | }; | |
03010a33 TT |
3954 | MODULE_ALIAS("ext4dev"); |
3955 | #endif | |
ac27a0ec | 3956 | |
617ba13b | 3957 | static int __init init_ext4_fs(void) |
ac27a0ec | 3958 | { |
c9de560d AT |
3959 | int err; |
3960 | ||
6fd058f7 TT |
3961 | err = init_ext4_system_zone(); |
3962 | if (err) | |
3963 | return err; | |
3197ebdb TT |
3964 | ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj); |
3965 | if (!ext4_kset) | |
6fd058f7 | 3966 | goto out4; |
9f6200bb | 3967 | ext4_proc_root = proc_mkdir("fs/ext4", NULL); |
c9de560d | 3968 | err = init_ext4_mballoc(); |
ac27a0ec | 3969 | if (err) |
6fd058f7 | 3970 | goto out3; |
c9de560d AT |
3971 | |
3972 | err = init_ext4_xattr(); | |
3973 | if (err) | |
3974 | goto out2; | |
ac27a0ec DK |
3975 | err = init_inodecache(); |
3976 | if (err) | |
3977 | goto out1; | |
03010a33 | 3978 | err = register_filesystem(&ext4_fs_type); |
ac27a0ec DK |
3979 | if (err) |
3980 | goto out; | |
03010a33 TT |
3981 | #ifdef CONFIG_EXT4DEV_COMPAT |
3982 | err = register_filesystem(&ext4dev_fs_type); | |
3983 | if (err) { | |
3984 | unregister_filesystem(&ext4_fs_type); | |
3985 | goto out; | |
3986 | } | |
3987 | #endif | |
ac27a0ec DK |
3988 | return 0; |
3989 | out: | |
3990 | destroy_inodecache(); | |
3991 | out1: | |
617ba13b | 3992 | exit_ext4_xattr(); |
c9de560d AT |
3993 | out2: |
3994 | exit_ext4_mballoc(); | |
6fd058f7 TT |
3995 | out3: |
3996 | remove_proc_entry("fs/ext4", NULL); | |
3997 | kset_unregister(ext4_kset); | |
3998 | out4: | |
3999 | exit_ext4_system_zone(); | |
ac27a0ec DK |
4000 | return err; |
4001 | } | |
4002 | ||
617ba13b | 4003 | static void __exit exit_ext4_fs(void) |
ac27a0ec | 4004 | { |
03010a33 TT |
4005 | unregister_filesystem(&ext4_fs_type); |
4006 | #ifdef CONFIG_EXT4DEV_COMPAT | |
617ba13b | 4007 | unregister_filesystem(&ext4dev_fs_type); |
03010a33 | 4008 | #endif |
ac27a0ec | 4009 | destroy_inodecache(); |
617ba13b | 4010 | exit_ext4_xattr(); |
c9de560d | 4011 | exit_ext4_mballoc(); |
9f6200bb | 4012 | remove_proc_entry("fs/ext4", NULL); |
3197ebdb | 4013 | kset_unregister(ext4_kset); |
6fd058f7 | 4014 | exit_ext4_system_zone(); |
ac27a0ec DK |
4015 | } |
4016 | ||
4017 | MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others"); | |
83982b6f | 4018 | MODULE_DESCRIPTION("Fourth Extended Filesystem"); |
ac27a0ec | 4019 | MODULE_LICENSE("GPL"); |
617ba13b MC |
4020 | module_init(init_ext4_fs) |
4021 | module_exit(exit_ext4_fs) |