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