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