Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4

Pull ext4 updates from Ted Ts'o:
"In addition to some ext4 bug fixes and cleanups, this cycle we add the
orphan_file feature, which eliminates bottlenecks when doing a large
number of parallel truncates and file deletions, and move the discard
operation out of the jbd2 commit thread when using the discard mount
option, to better support devices with slow discard operations"

* tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: (23 commits)
ext4: make the updating inode data procedure atomic
ext4: remove an unnecessary if statement in __ext4_get_inode_loc()
ext4: move inode eio simulation behind io completeion
ext4: Improve scalability of ext4 orphan file handling
ext4: Orphan file documentation
ext4: Speedup ext4 orphan inode handling
ext4: Move orphan inode handling into a separate file
ext4: Support for checksumming from journal triggers
ext4: fix race writing to an inline_data file while its xattrs are changing
jbd2: add sparse annotations for add_transaction_credits()
ext4: fix sparse warnings
ext4: Make sure quota files are not grabbed accidentally
ext4: fix e2fsprogs checksum failure for mounted filesystem
ext4: if zeroout fails fall back to splitting the extent node
ext4: reduce arguments of ext4_fc_add_dentry_tlv
ext4: flush background discard kwork when retry allocation
ext4: get discard out of jbd2 commit kthread contex
ext4: remove the repeated comment of ext4_trim_all_free
ext4: add new helper interface ext4_try_to_trim_range()
ext4: remove the 'group' parameter of ext4_trim_extent
...

+1445 -733
+1
Documentation/filesystems/ext4/globals.rst
··· 11 11 .. include:: bitmaps.rst 12 12 .. include:: mmp.rst 13 13 .. include:: journal.rst 14 + .. include:: orphan.rst
+5 -5
Documentation/filesystems/ext4/inodes.rst
··· 498 498 modification time (mtime), and deletion time (dtime). The four fields 499 499 are 32-bit signed integers that represent seconds since the Unix epoch 500 500 (1970-01-01 00:00:00 GMT), which means that the fields will overflow in 501 - January 2038. For inodes that are not linked from any directory but are 502 - still open (orphan inodes), the dtime field is overloaded for use with 503 - the orphan list. The superblock field ``s_last_orphan`` points to the 504 - first inode in the orphan list; dtime is then the number of the next 505 - orphaned inode, or zero if there are no more orphans. 501 + January 2038. If the filesystem does not have orphan_file feature, inodes 502 + that are not linked from any directory but are still open (orphan inodes) have 503 + the dtime field overloaded for use with the orphan list. The superblock field 504 + ``s_last_orphan`` points to the first inode in the orphan list; dtime is then 505 + the number of the next orphaned inode, or zero if there are no more orphans. 506 506 507 507 If the inode structure size ``sb->s_inode_size`` is larger than 128 508 508 bytes and the ``i_inode_extra`` field is large enough to encompass the
+52
Documentation/filesystems/ext4/orphan.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0 2 + 3 + Orphan file 4 + ----------- 5 + 6 + In unix there can inodes that are unlinked from directory hierarchy but that 7 + are still alive because they are open. In case of crash the filesystem has to 8 + clean up these inodes as otherwise they (and the blocks referenced from them) 9 + would leak. Similarly if we truncate or extend the file, we need not be able 10 + to perform the operation in a single journalling transaction. In such case we 11 + track the inode as orphan so that in case of crash extra blocks allocated to 12 + the file get truncated. 13 + 14 + Traditionally ext4 tracks orphan inodes in a form of single linked list where 15 + superblock contains the inode number of the last orphan inode (s\_last\_orphan 16 + field) and then each inode contains inode number of the previously orphaned 17 + inode (we overload i\_dtime inode field for this). However this filesystem 18 + global single linked list is a scalability bottleneck for workloads that result 19 + in heavy creation of orphan inodes. When orphan file feature 20 + (COMPAT\_ORPHAN\_FILE) is enabled, the filesystem has a special inode 21 + (referenced from the superblock through s\_orphan_file_inum) with several 22 + blocks. Each of these blocks has a structure: 23 + 24 + .. list-table:: 25 + :widths: 8 8 24 40 26 + :header-rows: 1 27 + 28 + * - Offset 29 + - Type 30 + - Name 31 + - Description 32 + * - 0x0 33 + - Array of \_\_le32 entries 34 + - Orphan inode entries 35 + - Each \_\_le32 entry is either empty (0) or it contains inode number of 36 + an orphan inode. 37 + * - blocksize - 8 38 + - \_\_le32 39 + - ob\_magic 40 + - Magic value stored in orphan block tail (0x0b10ca04) 41 + * - blocksize - 4 42 + - \_\_le32 43 + - ob\_checksum 44 + - Checksum of the orphan block. 45 + 46 + When a filesystem with orphan file feature is writeably mounted, we set 47 + RO\_COMPAT\_ORPHAN\_PRESENT feature in the superblock to indicate there may 48 + be valid orphan entries. In case we see this feature when mounting the 49 + filesystem, we read the whole orphan file and process all orphan inodes found 50 + there as usual. When cleanly unmounting the filesystem we remove the 51 + RO\_COMPAT\_ORPHAN\_PRESENT feature to avoid unnecessary scanning of the orphan 52 + file and also make the filesystem fully compatible with older kernels.
+17
Documentation/filesystems/ext4/special_inodes.rst
··· 36 36 * - 11 37 37 - Traditional first non-reserved inode. Usually this is the lost+found directory. See s\_first\_ino in the superblock. 38 38 39 + Note that there are also some inodes allocated from non-reserved inode numbers 40 + for other filesystem features which are not referenced from standard directory 41 + hierarchy. These are generally reference from the superblock. They are: 42 + 43 + .. list-table:: 44 + :widths: 20 50 45 + :header-rows: 1 46 + 47 + * - Superblock field 48 + - Description 49 + 50 + * - s\_lpf\_ino 51 + - Inode number of lost+found directory. 52 + * - s\_prj\_quota\_inum 53 + - Inode number of quota file tracking project quotas 54 + * - s\_orphan\_file\_inum 55 + - Inode number of file tracking orphan inodes.
+14 -1
Documentation/filesystems/ext4/super.rst
··· 479 479 - Filename charset encoding flags. 480 480 * - 0x280 481 481 - \_\_le32 482 - - s\_reserved[95] 482 + - s\_orphan\_file\_inum 483 + - Orphan file inode number. 484 + * - 0x284 485 + - \_\_le32 486 + - s\_reserved[94] 483 487 - Padding to the end of the block. 484 488 * - 0x3FC 485 489 - \_\_le32 ··· 607 603 the journal, JBD2 incompat feature 608 604 (JBD2\_FEATURE\_INCOMPAT\_FAST\_COMMIT) gets 609 605 set (COMPAT\_FAST\_COMMIT). 606 + * - 0x1000 607 + - Orphan file allocated. This is the special file for more efficient 608 + tracking of unlinked but still open inodes. When there may be any 609 + entries in the file, we additionally set proper rocompat feature 610 + (RO\_COMPAT\_ORPHAN\_PRESENT). 610 611 611 612 .. _super_incompat: 612 613 ··· 722 713 - Filesystem tracks project quotas. (RO\_COMPAT\_PROJECT) 723 714 * - 0x8000 724 715 - Verity inodes may be present on the filesystem. (RO\_COMPAT\_VERITY) 716 + * - 0x10000 717 + - Indicates orphan file may have valid orphan entries and thus we need 718 + to clean them up when mounting the filesystem 719 + (RO\_COMPAT\_ORPHAN\_PRESENT). 725 720 726 721 .. _super_def_hash: 727 722
+1 -1
fs/ext4/Makefile
··· 10 10 indirect.o inline.o inode.o ioctl.o mballoc.o migrate.o \ 11 11 mmp.o move_extent.o namei.o page-io.o readpage.o resize.o \ 12 12 super.o symlink.o sysfs.o xattr.o xattr_hurd.o xattr_trusted.o \ 13 - xattr_user.o fast_commit.o 13 + xattr_user.o fast_commit.o orphan.o 14 14 15 15 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o 16 16 ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
+7 -1
fs/ext4/balloc.c
··· 652 652 * possible we just missed a transaction commit that did so 653 653 */ 654 654 smp_mb(); 655 - if (sbi->s_mb_free_pending == 0) 655 + if (sbi->s_mb_free_pending == 0) { 656 + if (test_opt(sb, DISCARD)) { 657 + atomic_inc(&sbi->s_retry_alloc_pending); 658 + flush_work(&sbi->s_discard_work); 659 + atomic_dec(&sbi->s_retry_alloc_pending); 660 + } 656 661 return ext4_has_free_clusters(sbi, 1, 0); 662 + } 657 663 658 664 /* 659 665 * it's possible we've just missed a transaction commit here,
+97 -11
fs/ext4/ext4.h
··· 1034 1034 */ 1035 1035 struct rw_semaphore xattr_sem; 1036 1036 1037 - struct list_head i_orphan; /* unlinked but open inodes */ 1037 + /* 1038 + * Inodes with EXT4_STATE_ORPHAN_FILE use i_orphan_idx. Otherwise 1039 + * i_orphan is used. 1040 + */ 1041 + union { 1042 + struct list_head i_orphan; /* unlinked but open inodes */ 1043 + unsigned int i_orphan_idx; /* Index in orphan file */ 1044 + }; 1038 1045 1039 1046 /* Fast commit related info */ 1040 1047 ··· 1426 1419 __u8 s_last_error_errcode; 1427 1420 __le16 s_encoding; /* Filename charset encoding */ 1428 1421 __le16 s_encoding_flags; /* Filename charset encoding flags */ 1429 - __le32 s_reserved[95]; /* Padding to the end of the block */ 1422 + __le32 s_orphan_file_inum; /* Inode for tracking orphan inodes */ 1423 + __le32 s_reserved[94]; /* Padding to the end of the block */ 1430 1424 __le32 s_checksum; /* crc32c(superblock) */ 1431 1425 }; 1432 1426 ··· 1445 1437 #define EXT4_MAXQUOTAS 3 1446 1438 1447 1439 #define EXT4_ENC_UTF8_12_1 1 1440 + 1441 + /* Types of ext4 journal triggers */ 1442 + enum ext4_journal_trigger_type { 1443 + EXT4_JTR_ORPHAN_FILE, 1444 + EXT4_JTR_NONE /* This must be the last entry for indexing to work! */ 1445 + }; 1446 + 1447 + #define EXT4_JOURNAL_TRIGGER_COUNT EXT4_JTR_NONE 1448 + 1449 + struct ext4_journal_trigger { 1450 + struct jbd2_buffer_trigger_type tr_triggers; 1451 + struct super_block *sb; 1452 + }; 1453 + 1454 + static inline struct ext4_journal_trigger *EXT4_TRIGGER( 1455 + struct jbd2_buffer_trigger_type *trigger) 1456 + { 1457 + return container_of(trigger, struct ext4_journal_trigger, tr_triggers); 1458 + } 1459 + 1460 + #define EXT4_ORPHAN_BLOCK_MAGIC 0x0b10ca04 1461 + 1462 + /* Structure at the tail of orphan block */ 1463 + struct ext4_orphan_block_tail { 1464 + __le32 ob_magic; 1465 + __le32 ob_checksum; 1466 + }; 1467 + 1468 + static inline int ext4_inodes_per_orphan_block(struct super_block *sb) 1469 + { 1470 + return (sb->s_blocksize - sizeof(struct ext4_orphan_block_tail)) / 1471 + sizeof(u32); 1472 + } 1473 + 1474 + struct ext4_orphan_block { 1475 + atomic_t ob_free_entries; /* Number of free orphan entries in block */ 1476 + struct buffer_head *ob_bh; /* Buffer for orphan block */ 1477 + }; 1478 + 1479 + /* 1480 + * Info about orphan file. 1481 + */ 1482 + struct ext4_orphan_info { 1483 + int of_blocks; /* Number of orphan blocks in a file */ 1484 + __u32 of_csum_seed; /* Checksum seed for orphan file */ 1485 + struct ext4_orphan_block *of_binfo; /* Array with info about orphan 1486 + * file blocks */ 1487 + }; 1448 1488 1449 1489 /* 1450 1490 * fourth extended-fs super-block data in memory ··· 1548 1492 1549 1493 /* Journaling */ 1550 1494 struct journal_s *s_journal; 1551 - struct list_head s_orphan; 1552 - struct mutex s_orphan_lock; 1553 1495 unsigned long s_ext4_flags; /* Ext4 superblock flags */ 1496 + struct mutex s_orphan_lock; /* Protects on disk list changes */ 1497 + struct list_head s_orphan; /* List of orphaned inodes in on disk 1498 + list */ 1499 + struct ext4_orphan_info s_orphan_info; 1554 1500 unsigned long s_commit_interval; 1555 1501 u32 s_max_batch_time; 1556 1502 u32 s_min_batch_time; ··· 1585 1527 unsigned int s_mb_free_pending; 1586 1528 struct list_head s_freed_data_list; /* List of blocks to be freed 1587 1529 after commit completed */ 1530 + struct list_head s_discard_list; 1531 + struct work_struct s_discard_work; 1532 + atomic_t s_retry_alloc_pending; 1588 1533 struct rb_root s_mb_avg_fragment_size_root; 1589 1534 rwlock_t s_mb_rb_lock; 1590 1535 struct list_head *s_mb_largest_free_orders; ··· 1676 1615 struct mb_cache *s_ea_block_cache; 1677 1616 struct mb_cache *s_ea_inode_cache; 1678 1617 spinlock_t s_es_lock ____cacheline_aligned_in_smp; 1618 + 1619 + /* Journal triggers for checksum computation */ 1620 + struct ext4_journal_trigger s_journal_triggers[EXT4_JOURNAL_TRIGGER_COUNT]; 1679 1621 1680 1622 /* Ratelimit ext4 messages. */ 1681 1623 struct ratelimit_state s_err_ratelimit_state; ··· 1890 1826 EXT4_STATE_LUSTRE_EA_INODE, /* Lustre-style ea_inode */ 1891 1827 EXT4_STATE_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */ 1892 1828 EXT4_STATE_FC_COMMITTING, /* Fast commit ongoing */ 1829 + EXT4_STATE_ORPHAN_FILE, /* Inode orphaned in orphan file */ 1893 1830 }; 1894 1831 1895 1832 #define EXT4_INODE_BIT_FNS(name, field, offset) \ ··· 1992 1927 */ 1993 1928 #define EXT4_FEATURE_COMPAT_FAST_COMMIT 0x0400 1994 1929 #define EXT4_FEATURE_COMPAT_STABLE_INODES 0x0800 1930 + #define EXT4_FEATURE_COMPAT_ORPHAN_FILE 0x1000 /* Orphan file exists */ 1995 1931 1996 1932 #define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001 1997 1933 #define EXT4_FEATURE_RO_COMPAT_LARGE_FILE 0x0002 ··· 2013 1947 #define EXT4_FEATURE_RO_COMPAT_READONLY 0x1000 2014 1948 #define EXT4_FEATURE_RO_COMPAT_PROJECT 0x2000 2015 1949 #define EXT4_FEATURE_RO_COMPAT_VERITY 0x8000 1950 + #define EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT 0x10000 /* Orphan file may be 1951 + non-empty */ 2016 1952 2017 1953 #define EXT4_FEATURE_INCOMPAT_COMPRESSION 0x0001 2018 1954 #define EXT4_FEATURE_INCOMPAT_FILETYPE 0x0002 ··· 2098 2030 EXT4_FEATURE_COMPAT_FUNCS(sparse_super2, SPARSE_SUPER2) 2099 2031 EXT4_FEATURE_COMPAT_FUNCS(fast_commit, FAST_COMMIT) 2100 2032 EXT4_FEATURE_COMPAT_FUNCS(stable_inodes, STABLE_INODES) 2033 + EXT4_FEATURE_COMPAT_FUNCS(orphan_file, ORPHAN_FILE) 2101 2034 2102 2035 EXT4_FEATURE_RO_COMPAT_FUNCS(sparse_super, SPARSE_SUPER) 2103 2036 EXT4_FEATURE_RO_COMPAT_FUNCS(large_file, LARGE_FILE) ··· 2113 2044 EXT4_FEATURE_RO_COMPAT_FUNCS(readonly, READONLY) 2114 2045 EXT4_FEATURE_RO_COMPAT_FUNCS(project, PROJECT) 2115 2046 EXT4_FEATURE_RO_COMPAT_FUNCS(verity, VERITY) 2047 + EXT4_FEATURE_RO_COMPAT_FUNCS(orphan_present, ORPHAN_PRESENT) 2116 2048 2117 2049 EXT4_FEATURE_INCOMPAT_FUNCS(compression, COMPRESSION) 2118 2050 EXT4_FEATURE_INCOMPAT_FUNCS(filetype, FILETYPE) ··· 2147 2077 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \ 2148 2078 EXT4_FEATURE_RO_COMPAT_BTREE_DIR) 2149 2079 2150 - #define EXT4_FEATURE_COMPAT_SUPP EXT4_FEATURE_COMPAT_EXT_ATTR 2080 + #define EXT4_FEATURE_COMPAT_SUPP (EXT4_FEATURE_COMPAT_EXT_ATTR| \ 2081 + EXT4_FEATURE_COMPAT_ORPHAN_FILE) 2151 2082 #define EXT4_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE| \ 2152 2083 EXT4_FEATURE_INCOMPAT_RECOVER| \ 2153 2084 EXT4_FEATURE_INCOMPAT_META_BG| \ ··· 2173 2102 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\ 2174 2103 EXT4_FEATURE_RO_COMPAT_QUOTA |\ 2175 2104 EXT4_FEATURE_RO_COMPAT_PROJECT |\ 2176 - EXT4_FEATURE_RO_COMPAT_VERITY) 2105 + EXT4_FEATURE_RO_COMPAT_VERITY |\ 2106 + EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT) 2177 2107 2178 2108 #define EXTN_FEATURE_FUNCS(ver) \ 2179 2109 static inline bool ext4_has_unknown_ext##ver##_compat_features(struct super_block *sb) \ ··· 2210 2138 return (EXT4_SB(sb)->s_es->s_feature_incompat != 0); 2211 2139 } 2212 2140 2141 + extern int ext4_feature_set_ok(struct super_block *sb, int readonly); 2142 + 2213 2143 /* 2214 2144 * Superblock flags 2215 2145 */ ··· 2223 2149 { 2224 2150 return test_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); 2225 2151 } 2226 - 2227 2152 2228 2153 /* 2229 2154 * Default values for user and/or group using reserved blocks ··· 2984 2911 int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 2985 2912 struct buffer_head *bh, int create); 2986 2913 int ext4_walk_page_buffers(handle_t *handle, 2914 + struct inode *inode, 2987 2915 struct buffer_head *head, 2988 2916 unsigned from, 2989 2917 unsigned to, 2990 2918 int *partial, 2991 - int (*fn)(handle_t *handle, 2919 + int (*fn)(handle_t *handle, struct inode *inode, 2992 2920 struct buffer_head *bh)); 2993 - int do_journal_get_write_access(handle_t *handle, 2921 + int do_journal_get_write_access(handle_t *handle, struct inode *inode, 2994 2922 struct buffer_head *bh); 2995 2923 #define FALL_BACK_TO_NONDELALLOC 1 2996 2924 #define CONVERT_INLINE_DATA 2 ··· 3070 2996 struct inode *inode); 3071 2997 extern int ext4_dirblock_csum_verify(struct inode *inode, 3072 2998 struct buffer_head *bh); 3073 - extern int ext4_orphan_add(handle_t *, struct inode *); 3074 - extern int ext4_orphan_del(handle_t *, struct inode *); 3075 2999 extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, 3076 3000 __u32 start_minor_hash, __u32 *next_hash); 3077 3001 extern int ext4_search_dir(struct buffer_head *bh, ··· 3538 3466 return (ext4_has_feature_quota(sb) || 3539 3467 sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]); 3540 3468 } 3469 + int ext4_enable_quotas(struct super_block *sb); 3541 3470 #endif 3542 3471 3543 3472 /* ··· 3799 3726 3800 3727 /* verity.c */ 3801 3728 extern const struct fsverity_operations ext4_verityops; 3729 + 3730 + /* orphan.c */ 3731 + extern int ext4_orphan_add(handle_t *, struct inode *); 3732 + extern int ext4_orphan_del(handle_t *, struct inode *); 3733 + extern void ext4_orphan_cleanup(struct super_block *sb, 3734 + struct ext4_super_block *es); 3735 + extern void ext4_release_orphan_info(struct super_block *sb); 3736 + extern int ext4_init_orphan_info(struct super_block *sb); 3737 + extern int ext4_orphan_file_empty(struct super_block *sb); 3738 + extern void ext4_orphan_file_block_trigger( 3739 + struct jbd2_buffer_trigger_type *triggers, 3740 + struct buffer_head *bh, 3741 + void *data, size_t size); 3802 3742 3803 3743 /* 3804 3744 * Add new method to test whether block and inode bitmaps are properly
+3 -2
fs/ext4/ext4_extents.h
··· 173 173 #define EXT_MAX_EXTENT(__hdr__) \ 174 174 ((le16_to_cpu((__hdr__)->eh_max)) ? \ 175 175 ((EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)) \ 176 - : 0) 176 + : NULL) 177 177 #define EXT_MAX_INDEX(__hdr__) \ 178 178 ((le16_to_cpu((__hdr__)->eh_max)) ? \ 179 - ((EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)) : 0) 179 + ((EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)) \ 180 + : NULL) 180 181 181 182 static inline struct ext4_extent_header *ext_inode_hdr(struct inode *inode) 182 183 {
+31 -12
fs/ext4/ext4_jbd2.c
··· 218 218 } 219 219 220 220 int __ext4_journal_get_write_access(const char *where, unsigned int line, 221 - handle_t *handle, struct buffer_head *bh) 221 + handle_t *handle, struct super_block *sb, 222 + struct buffer_head *bh, 223 + enum ext4_journal_trigger_type trigger_type) 222 224 { 223 - int err = 0; 225 + int err; 224 226 225 227 might_sleep(); 226 228 ··· 231 229 232 230 if (ext4_handle_valid(handle)) { 233 231 err = jbd2_journal_get_write_access(handle, bh); 234 - if (err) 232 + if (err) { 235 233 ext4_journal_abort_handle(where, line, __func__, bh, 236 234 handle, err); 235 + return err; 236 + } 237 237 } 238 - return err; 238 + if (trigger_type == EXT4_JTR_NONE || !ext4_has_metadata_csum(sb)) 239 + return 0; 240 + BUG_ON(trigger_type >= EXT4_JOURNAL_TRIGGER_COUNT); 241 + jbd2_journal_set_triggers(bh, 242 + &EXT4_SB(sb)->s_journal_triggers[trigger_type].tr_triggers); 243 + return 0; 239 244 } 240 245 241 246 /* ··· 310 301 } 311 302 312 303 int __ext4_journal_get_create_access(const char *where, unsigned int line, 313 - handle_t *handle, struct buffer_head *bh) 304 + handle_t *handle, struct super_block *sb, 305 + struct buffer_head *bh, 306 + enum ext4_journal_trigger_type trigger_type) 314 307 { 315 - int err = 0; 308 + int err; 316 309 317 - if (ext4_handle_valid(handle)) { 318 - err = jbd2_journal_get_create_access(handle, bh); 319 - if (err) 320 - ext4_journal_abort_handle(where, line, __func__, 321 - bh, handle, err); 310 + if (!ext4_handle_valid(handle)) 311 + return 0; 312 + 313 + err = jbd2_journal_get_create_access(handle, bh); 314 + if (err) { 315 + ext4_journal_abort_handle(where, line, __func__, bh, handle, 316 + err); 317 + return err; 322 318 } 323 - return err; 319 + if (trigger_type == EXT4_JTR_NONE || !ext4_has_metadata_csum(sb)) 320 + return 0; 321 + BUG_ON(trigger_type >= EXT4_JOURNAL_TRIGGER_COUNT); 322 + jbd2_journal_set_triggers(bh, 323 + &EXT4_SB(sb)->s_journal_triggers[trigger_type].tr_triggers); 324 + return 0; 324 325 } 325 326 326 327 int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
+12 -6
fs/ext4/ext4_jbd2.h
··· 231 231 * Wrapper functions with which ext4 calls into JBD. 232 232 */ 233 233 int __ext4_journal_get_write_access(const char *where, unsigned int line, 234 - handle_t *handle, struct buffer_head *bh); 234 + handle_t *handle, struct super_block *sb, 235 + struct buffer_head *bh, 236 + enum ext4_journal_trigger_type trigger_type); 235 237 236 238 int __ext4_forget(const char *where, unsigned int line, handle_t *handle, 237 239 int is_metadata, struct inode *inode, 238 240 struct buffer_head *bh, ext4_fsblk_t blocknr); 239 241 240 242 int __ext4_journal_get_create_access(const char *where, unsigned int line, 241 - handle_t *handle, struct buffer_head *bh); 243 + handle_t *handle, struct super_block *sb, 244 + struct buffer_head *bh, 245 + enum ext4_journal_trigger_type trigger_type); 242 246 243 247 int __ext4_handle_dirty_metadata(const char *where, unsigned int line, 244 248 handle_t *handle, struct inode *inode, 245 249 struct buffer_head *bh); 246 250 247 - #define ext4_journal_get_write_access(handle, bh) \ 248 - __ext4_journal_get_write_access(__func__, __LINE__, (handle), (bh)) 251 + #define ext4_journal_get_write_access(handle, sb, bh, trigger_type) \ 252 + __ext4_journal_get_write_access(__func__, __LINE__, (handle), (sb), \ 253 + (bh), (trigger_type)) 249 254 #define ext4_forget(handle, is_metadata, inode, bh, block_nr) \ 250 255 __ext4_forget(__func__, __LINE__, (handle), (is_metadata), (inode), \ 251 256 (bh), (block_nr)) 252 - #define ext4_journal_get_create_access(handle, bh) \ 253 - __ext4_journal_get_create_access(__func__, __LINE__, (handle), (bh)) 257 + #define ext4_journal_get_create_access(handle, sb, bh, trigger_type) \ 258 + __ext4_journal_get_create_access(__func__, __LINE__, (handle), (sb), \ 259 + (bh), (trigger_type)) 254 260 #define ext4_handle_dirty_metadata(handle, inode, bh) \ 255 261 __ext4_handle_dirty_metadata(__func__, __LINE__, (handle), (inode), \ 256 262 (bh))
+11 -6
fs/ext4/extents.c
··· 139 139 if (path->p_bh) { 140 140 /* path points to block */ 141 141 BUFFER_TRACE(path->p_bh, "get_write_access"); 142 - return ext4_journal_get_write_access(handle, path->p_bh); 142 + return ext4_journal_get_write_access(handle, inode->i_sb, 143 + path->p_bh, EXT4_JTR_NONE); 143 144 } 144 145 /* path points to leaf/index in inode body */ 145 146 /* we use in-core data, no need to protect them */ ··· 1083 1082 } 1084 1083 lock_buffer(bh); 1085 1084 1086 - err = ext4_journal_get_create_access(handle, bh); 1085 + err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 1086 + EXT4_JTR_NONE); 1087 1087 if (err) 1088 1088 goto cleanup; 1089 1089 ··· 1162 1160 } 1163 1161 lock_buffer(bh); 1164 1162 1165 - err = ext4_journal_get_create_access(handle, bh); 1163 + err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 1164 + EXT4_JTR_NONE); 1166 1165 if (err) 1167 1166 goto cleanup; 1168 1167 ··· 1289 1286 return -ENOMEM; 1290 1287 lock_buffer(bh); 1291 1288 1292 - err = ext4_journal_get_create_access(handle, bh); 1289 + err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 1290 + EXT4_JTR_NONE); 1293 1291 if (err) { 1294 1292 unlock_buffer(bh); 1295 1293 goto out; ··· 3573 3569 split_map.m_len - ee_block); 3574 3570 err = ext4_ext_zeroout(inode, &zero_ex1); 3575 3571 if (err) 3576 - goto out; 3572 + goto fallback; 3577 3573 split_map.m_len = allocated; 3578 3574 } 3579 3575 if (split_map.m_lblk - ee_block + split_map.m_len < ··· 3587 3583 ext4_ext_pblock(ex)); 3588 3584 err = ext4_ext_zeroout(inode, &zero_ex2); 3589 3585 if (err) 3590 - goto out; 3586 + goto fallback; 3591 3587 } 3592 3588 3593 3589 split_map.m_len += split_map.m_lblk - ee_block; ··· 3596 3592 } 3597 3593 } 3598 3594 3595 + fallback: 3599 3596 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag, 3600 3597 flags); 3601 3598 if (err > 0)
+9 -18
fs/ext4/fast_commit.c
··· 775 775 } 776 776 777 777 /* Same as above, but adds dentry tlv. */ 778 - static bool ext4_fc_add_dentry_tlv(struct super_block *sb, u16 tag, 779 - int parent_ino, int ino, int dlen, 780 - const unsigned char *dname, 781 - u32 *crc) 778 + static bool ext4_fc_add_dentry_tlv(struct super_block *sb, u32 *crc, 779 + struct ext4_fc_dentry_update *fc_dentry) 782 780 { 783 781 struct ext4_fc_dentry_info fcd; 784 782 struct ext4_fc_tl tl; 783 + int dlen = fc_dentry->fcd_name.len; 785 784 u8 *dst = ext4_fc_reserve_space(sb, sizeof(tl) + sizeof(fcd) + dlen, 786 785 crc); 787 786 788 787 if (!dst) 789 788 return false; 790 789 791 - fcd.fc_parent_ino = cpu_to_le32(parent_ino); 792 - fcd.fc_ino = cpu_to_le32(ino); 793 - tl.fc_tag = cpu_to_le16(tag); 790 + fcd.fc_parent_ino = cpu_to_le32(fc_dentry->fcd_parent); 791 + fcd.fc_ino = cpu_to_le32(fc_dentry->fcd_ino); 792 + tl.fc_tag = cpu_to_le16(fc_dentry->fcd_op); 794 793 tl.fc_len = cpu_to_le16(sizeof(fcd) + dlen); 795 794 ext4_fc_memcpy(sb, dst, &tl, sizeof(tl), crc); 796 795 dst += sizeof(tl); 797 796 ext4_fc_memcpy(sb, dst, &fcd, sizeof(fcd), crc); 798 797 dst += sizeof(fcd); 799 - ext4_fc_memcpy(sb, dst, dname, dlen, crc); 798 + ext4_fc_memcpy(sb, dst, fc_dentry->fcd_name.name, dlen, crc); 800 799 dst += dlen; 801 800 802 801 return true; ··· 991 992 &sbi->s_fc_dentry_q[FC_Q_MAIN], fcd_list) { 992 993 if (fc_dentry->fcd_op != EXT4_FC_TAG_CREAT) { 993 994 spin_unlock(&sbi->s_fc_lock); 994 - if (!ext4_fc_add_dentry_tlv( 995 - sb, fc_dentry->fcd_op, 996 - fc_dentry->fcd_parent, fc_dentry->fcd_ino, 997 - fc_dentry->fcd_name.len, 998 - fc_dentry->fcd_name.name, crc)) { 995 + if (!ext4_fc_add_dentry_tlv(sb, crc, fc_dentry)) { 999 996 ret = -ENOSPC; 1000 997 goto lock_and_exit; 1001 998 } ··· 1030 1035 if (ret) 1031 1036 goto lock_and_exit; 1032 1037 1033 - if (!ext4_fc_add_dentry_tlv( 1034 - sb, fc_dentry->fcd_op, 1035 - fc_dentry->fcd_parent, fc_dentry->fcd_ino, 1036 - fc_dentry->fcd_name.len, 1037 - fc_dentry->fcd_name.name, crc)) { 1038 + if (!ext4_fc_add_dentry_tlv(sb, crc, fc_dentry)) { 1038 1039 ret = -ENOSPC; 1039 1040 goto lock_and_exit; 1040 1041 }
+2 -1
fs/ext4/file.c
··· 823 823 if (IS_ERR(handle)) 824 824 goto out; 825 825 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 826 - err = ext4_journal_get_write_access(handle, sbi->s_sbh); 826 + err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 827 + EXT4_JTR_NONE); 827 828 if (err) 828 829 goto out_journal; 829 830 lock_buffer(sbi->s_sbh);
+12 -7
fs/ext4/ialloc.c
··· 300 300 } 301 301 302 302 BUFFER_TRACE(bitmap_bh, "get_write_access"); 303 - fatal = ext4_journal_get_write_access(handle, bitmap_bh); 303 + fatal = ext4_journal_get_write_access(handle, sb, bitmap_bh, 304 + EXT4_JTR_NONE); 304 305 if (fatal) 305 306 goto error_return; 306 307 ··· 309 308 gdp = ext4_get_group_desc(sb, block_group, &bh2); 310 309 if (gdp) { 311 310 BUFFER_TRACE(bh2, "get_write_access"); 312 - fatal = ext4_journal_get_write_access(handle, bh2); 311 + fatal = ext4_journal_get_write_access(handle, sb, bh2, 312 + EXT4_JTR_NONE); 313 313 } 314 314 ext4_lock_group(sb, block_group); 315 315 cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); ··· 1087 1085 } 1088 1086 } 1089 1087 BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 1090 - err = ext4_journal_get_write_access(handle, inode_bitmap_bh); 1088 + err = ext4_journal_get_write_access(handle, sb, inode_bitmap_bh, 1089 + EXT4_JTR_NONE); 1091 1090 if (err) { 1092 1091 ext4_std_error(sb, err); 1093 1092 goto out; ··· 1130 1127 } 1131 1128 1132 1129 BUFFER_TRACE(group_desc_bh, "get_write_access"); 1133 - err = ext4_journal_get_write_access(handle, group_desc_bh); 1130 + err = ext4_journal_get_write_access(handle, sb, group_desc_bh, 1131 + EXT4_JTR_NONE); 1134 1132 if (err) { 1135 1133 ext4_std_error(sb, err); 1136 1134 goto out; ··· 1148 1144 goto out; 1149 1145 } 1150 1146 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 1151 - err = ext4_journal_get_write_access(handle, block_bitmap_bh); 1147 + err = ext4_journal_get_write_access(handle, sb, block_bitmap_bh, 1148 + EXT4_JTR_NONE); 1152 1149 if (err) { 1153 1150 brelse(block_bitmap_bh); 1154 1151 ext4_std_error(sb, err); ··· 1588 1583 num = sbi->s_itb_per_group - used_blks; 1589 1584 1590 1585 BUFFER_TRACE(group_desc_bh, "get_write_access"); 1591 - ret = ext4_journal_get_write_access(handle, 1592 - group_desc_bh); 1586 + ret = ext4_journal_get_write_access(handle, sb, group_desc_bh, 1587 + EXT4_JTR_NONE); 1593 1588 if (ret) 1594 1589 goto err_out; 1595 1590
+10 -5
fs/ext4/indirect.c
··· 354 354 } 355 355 lock_buffer(bh); 356 356 BUFFER_TRACE(bh, "call get_create_access"); 357 - err = ext4_journal_get_create_access(handle, bh); 357 + err = ext4_journal_get_create_access(handle, ar->inode->i_sb, 358 + bh, EXT4_JTR_NONE); 358 359 if (err) { 359 360 unlock_buffer(bh); 360 361 goto failed; ··· 430 429 */ 431 430 if (where->bh) { 432 431 BUFFER_TRACE(where->bh, "get_write_access"); 433 - err = ext4_journal_get_write_access(handle, where->bh); 432 + err = ext4_journal_get_write_access(handle, ar->inode->i_sb, 433 + where->bh, EXT4_JTR_NONE); 434 434 if (err) 435 435 goto err_out; 436 436 } ··· 730 728 return ret; 731 729 if (bh) { 732 730 BUFFER_TRACE(bh, "retaking write access"); 733 - ret = ext4_journal_get_write_access(handle, bh); 731 + ret = ext4_journal_get_write_access(handle, inode->i_sb, bh, 732 + EXT4_JTR_NONE); 734 733 if (unlikely(ret)) 735 734 return ret; 736 735 } ··· 919 916 920 917 if (this_bh) { /* For indirect block */ 921 918 BUFFER_TRACE(this_bh, "get_write_access"); 922 - err = ext4_journal_get_write_access(handle, this_bh); 919 + err = ext4_journal_get_write_access(handle, inode->i_sb, 920 + this_bh, EXT4_JTR_NONE); 923 921 /* Important: if we can't update the indirect pointers 924 922 * to the blocks, we can't free them. */ 925 923 if (err) ··· 1083 1079 */ 1084 1080 BUFFER_TRACE(parent_bh, "get_write_access"); 1085 1081 if (!ext4_journal_get_write_access(handle, 1086 - parent_bh)){ 1082 + inode->i_sb, parent_bh, 1083 + EXT4_JTR_NONE)) { 1087 1084 *p = 0; 1088 1085 BUFFER_TRACE(parent_bh, 1089 1086 "call ext4_handle_dirty_metadata");
+23 -9
fs/ext4/inline.c
··· 264 264 return error; 265 265 266 266 BUFFER_TRACE(is.iloc.bh, "get_write_access"); 267 - error = ext4_journal_get_write_access(handle, is.iloc.bh); 267 + error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh, 268 + EXT4_JTR_NONE); 268 269 if (error) 269 270 goto out; 270 271 ··· 351 350 goto out; 352 351 353 352 BUFFER_TRACE(is.iloc.bh, "get_write_access"); 354 - error = ext4_journal_get_write_access(handle, is.iloc.bh); 353 + error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh, 354 + EXT4_JTR_NONE); 355 355 if (error) 356 356 goto out; 357 357 ··· 429 427 goto out; 430 428 431 429 BUFFER_TRACE(is.iloc.bh, "get_write_access"); 432 - error = ext4_journal_get_write_access(handle, is.iloc.bh); 430 + error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh, 431 + EXT4_JTR_NONE); 433 432 if (error) 434 433 goto out; 435 434 ··· 596 593 ret = __block_write_begin(page, from, to, ext4_get_block); 597 594 598 595 if (!ret && ext4_should_journal_data(inode)) { 599 - ret = ext4_walk_page_buffers(handle, page_buffers(page), 596 + ret = ext4_walk_page_buffers(handle, inode, page_buffers(page), 600 597 from, to, NULL, 601 598 do_journal_get_write_access); 602 599 } ··· 685 682 goto convert; 686 683 } 687 684 688 - ret = ext4_journal_get_write_access(handle, iloc.bh); 685 + ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh, 686 + EXT4_JTR_NONE); 689 687 if (ret) 690 688 goto out; 691 689 ··· 753 749 754 750 ext4_write_lock_xattr(inode, &no_expand); 755 751 BUG_ON(!ext4_has_inline_data(inode)); 752 + 753 + /* 754 + * ei->i_inline_off may have changed since ext4_write_begin() 755 + * called ext4_try_to_write_inline_data() 756 + */ 757 + (void) ext4_find_inline_data_nolock(inode); 756 758 757 759 kaddr = kmap_atomic(page); 758 760 ext4_write_inline_data(inode, &iloc, kaddr, pos, len); ··· 933 923 if (ret < 0) 934 924 goto out_release_page; 935 925 } 936 - ret = ext4_journal_get_write_access(handle, iloc.bh); 926 + ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh, 927 + EXT4_JTR_NONE); 937 928 if (ret) 938 929 goto out_release_page; 939 930 ··· 1039 1028 return err; 1040 1029 1041 1030 BUFFER_TRACE(iloc->bh, "get_write_access"); 1042 - err = ext4_journal_get_write_access(handle, iloc->bh); 1031 + err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh, 1032 + EXT4_JTR_NONE); 1043 1033 if (err) 1044 1034 return err; 1045 1035 ext4_insert_dentry(dir, inode, de, inline_size, fname); ··· 1235 1223 } 1236 1224 1237 1225 lock_buffer(data_bh); 1238 - error = ext4_journal_get_create_access(handle, data_bh); 1226 + error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh, 1227 + EXT4_JTR_NONE); 1239 1228 if (error) { 1240 1229 unlock_buffer(data_bh); 1241 1230 error = -EIO; ··· 1720 1707 } 1721 1708 1722 1709 BUFFER_TRACE(bh, "get_write_access"); 1723 - err = ext4_journal_get_write_access(handle, bh); 1710 + err = ext4_journal_get_write_access(handle, dir->i_sb, bh, 1711 + EXT4_JTR_NONE); 1724 1712 if (err) 1725 1713 goto out; 1726 1714
+163 -140
fs/ext4/inode.c
··· 139 139 static void ext4_invalidatepage(struct page *page, unsigned int offset, 140 140 unsigned int length); 141 141 static int __ext4_journalled_writepage(struct page *page, unsigned int len); 142 - static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 143 142 static int ext4_meta_trans_blocks(struct inode *inode, int lblocks, 144 143 int pextents); 145 144 ··· 868 869 */ 869 870 lock_buffer(bh); 870 871 BUFFER_TRACE(bh, "call get_create_access"); 871 - err = ext4_journal_get_create_access(handle, bh); 872 + err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 873 + EXT4_JTR_NONE); 872 874 if (unlikely(err)) { 873 875 unlock_buffer(bh); 874 876 goto errout; ··· 954 954 return err; 955 955 } 956 956 957 - int ext4_walk_page_buffers(handle_t *handle, 957 + int ext4_walk_page_buffers(handle_t *handle, struct inode *inode, 958 958 struct buffer_head *head, 959 959 unsigned from, 960 960 unsigned to, 961 961 int *partial, 962 - int (*fn)(handle_t *handle, 962 + int (*fn)(handle_t *handle, struct inode *inode, 963 963 struct buffer_head *bh)) 964 964 { 965 965 struct buffer_head *bh; ··· 978 978 *partial = 1; 979 979 continue; 980 980 } 981 - err = (*fn)(handle, bh); 981 + err = (*fn)(handle, inode, bh); 982 982 if (!ret) 983 983 ret = err; 984 984 } ··· 1009 1009 * is elevated. We'll still have enough credits for the tiny quotafile 1010 1010 * write. 1011 1011 */ 1012 - int do_journal_get_write_access(handle_t *handle, 1012 + int do_journal_get_write_access(handle_t *handle, struct inode *inode, 1013 1013 struct buffer_head *bh) 1014 1014 { 1015 1015 int dirty = buffer_dirty(bh); ··· 1028 1028 if (dirty) 1029 1029 clear_buffer_dirty(bh); 1030 1030 BUFFER_TRACE(bh, "get write access"); 1031 - ret = ext4_journal_get_write_access(handle, bh); 1031 + ret = ext4_journal_get_write_access(handle, inode->i_sb, bh, 1032 + EXT4_JTR_NONE); 1032 1033 if (!ret && dirty) 1033 1034 ret = ext4_handle_dirty_metadata(handle, NULL, bh); 1034 1035 return ret; ··· 1209 1208 ret = __block_write_begin(page, pos, len, ext4_get_block); 1210 1209 #endif 1211 1210 if (!ret && ext4_should_journal_data(inode)) { 1212 - ret = ext4_walk_page_buffers(handle, page_buffers(page), 1213 - from, to, NULL, 1211 + ret = ext4_walk_page_buffers(handle, inode, 1212 + page_buffers(page), from, to, NULL, 1214 1213 do_journal_get_write_access); 1215 1214 } 1216 1215 ··· 1254 1253 } 1255 1254 1256 1255 /* For write_end() in data=journal mode */ 1257 - static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1256 + static int write_end_fn(handle_t *handle, struct inode *inode, 1257 + struct buffer_head *bh) 1258 1258 { 1259 1259 int ret; 1260 1260 if (!buffer_mapped(bh) || buffer_freed(bh)) ··· 1354 1352 * to call ext4_handle_dirty_metadata() instead. 1355 1353 */ 1356 1354 static void ext4_journalled_zero_new_buffers(handle_t *handle, 1355 + struct inode *inode, 1357 1356 struct page *page, 1358 1357 unsigned from, unsigned to) 1359 1358 { ··· 1373 1370 size = min(to, block_end) - start; 1374 1371 1375 1372 zero_user(page, start, size); 1376 - write_end_fn(handle, bh); 1373 + write_end_fn(handle, inode, bh); 1377 1374 } 1378 1375 clear_buffer_new(bh); 1379 1376 } ··· 1415 1412 copied = ret; 1416 1413 } else if (unlikely(copied < len) && !PageUptodate(page)) { 1417 1414 copied = 0; 1418 - ext4_journalled_zero_new_buffers(handle, page, from, to); 1415 + ext4_journalled_zero_new_buffers(handle, inode, page, from, to); 1419 1416 } else { 1420 1417 if (unlikely(copied < len)) 1421 - ext4_journalled_zero_new_buffers(handle, page, 1418 + ext4_journalled_zero_new_buffers(handle, inode, page, 1422 1419 from + copied, to); 1423 - ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1424 - from + copied, &partial, 1420 + ret = ext4_walk_page_buffers(handle, inode, page_buffers(page), 1421 + from, from + copied, &partial, 1425 1422 write_end_fn); 1426 1423 if (!partial) 1427 1424 SetPageUptodate(page); ··· 1622 1619 return; 1623 1620 } 1624 1621 1625 - static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 1622 + static int ext4_bh_delay_or_unwritten(handle_t *handle, struct inode *inode, 1623 + struct buffer_head *bh) 1626 1624 { 1627 1625 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 1628 1626 } ··· 1855 1851 return 0; 1856 1852 } 1857 1853 1858 - static int bget_one(handle_t *handle, struct buffer_head *bh) 1854 + static int bget_one(handle_t *handle, struct inode *inode, 1855 + struct buffer_head *bh) 1859 1856 { 1860 1857 get_bh(bh); 1861 1858 return 0; 1862 1859 } 1863 1860 1864 - static int bput_one(handle_t *handle, struct buffer_head *bh) 1861 + static int bput_one(handle_t *handle, struct inode *inode, 1862 + struct buffer_head *bh) 1865 1863 { 1866 1864 put_bh(bh); 1867 1865 return 0; ··· 1894 1888 BUG(); 1895 1889 goto out; 1896 1890 } 1897 - ext4_walk_page_buffers(handle, page_bufs, 0, len, 1891 + ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1898 1892 NULL, bget_one); 1899 1893 } 1900 1894 /* ··· 1926 1920 if (inline_data) { 1927 1921 ret = ext4_mark_inode_dirty(handle, inode); 1928 1922 } else { 1929 - ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 1930 - do_journal_get_write_access); 1923 + ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1924 + NULL, do_journal_get_write_access); 1931 1925 1932 - err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 1933 - write_end_fn); 1926 + err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len, 1927 + NULL, write_end_fn); 1934 1928 } 1935 1929 if (ret == 0) 1936 1930 ret = err; ··· 1947 1941 unlock_page(page); 1948 1942 out_no_pagelock: 1949 1943 if (!inline_data && page_bufs) 1950 - ext4_walk_page_buffers(NULL, page_bufs, 0, len, 1944 + ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len, 1951 1945 NULL, bput_one); 1952 1946 brelse(inode_bh); 1953 1947 return ret; ··· 2037 2031 * for the extremely common case, this is an optimization that 2038 2032 * skips a useless round trip through ext4_bio_write_page(). 2039 2033 */ 2040 - if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2034 + if (ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len, NULL, 2041 2035 ext4_bh_delay_or_unwritten)) { 2042 2036 redirty_page_for_writepage(wbc, page); 2043 2037 if ((current->flags & PF_MEMALLOC) || ··· 3800 3794 } 3801 3795 if (ext4_should_journal_data(inode)) { 3802 3796 BUFFER_TRACE(bh, "get write access"); 3803 - err = ext4_journal_get_write_access(handle, bh); 3797 + err = ext4_journal_get_write_access(handle, inode->i_sb, bh, 3798 + EXT4_JTR_NONE); 3804 3799 if (err) 3805 3800 goto unlock; 3806 3801 } ··· 4336 4329 bh = sb_getblk(sb, block); 4337 4330 if (unlikely(!bh)) 4338 4331 return -ENOMEM; 4339 - if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO)) 4340 - goto simulate_eio; 4341 - if (!buffer_uptodate(bh)) { 4342 - lock_buffer(bh); 4332 + if (ext4_buffer_uptodate(bh)) 4333 + goto has_buffer; 4343 4334 4344 - if (ext4_buffer_uptodate(bh)) { 4345 - /* someone brought it uptodate while we waited */ 4335 + lock_buffer(bh); 4336 + /* 4337 + * If we have all information of the inode in memory and this 4338 + * is the only valid inode in the block, we need not read the 4339 + * block. 4340 + */ 4341 + if (in_mem) { 4342 + struct buffer_head *bitmap_bh; 4343 + int i, start; 4344 + 4345 + start = inode_offset & ~(inodes_per_block - 1); 4346 + 4347 + /* Is the inode bitmap in cache? */ 4348 + bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4349 + if (unlikely(!bitmap_bh)) 4350 + goto make_io; 4351 + 4352 + /* 4353 + * If the inode bitmap isn't in cache then the 4354 + * optimisation may end up performing two reads instead 4355 + * of one, so skip it. 4356 + */ 4357 + if (!buffer_uptodate(bitmap_bh)) { 4358 + brelse(bitmap_bh); 4359 + goto make_io; 4360 + } 4361 + for (i = start; i < start + inodes_per_block; i++) { 4362 + if (i == inode_offset) 4363 + continue; 4364 + if (ext4_test_bit(i, bitmap_bh->b_data)) 4365 + break; 4366 + } 4367 + brelse(bitmap_bh); 4368 + if (i == start + inodes_per_block) { 4369 + /* all other inodes are free, so skip I/O */ 4370 + memset(bh->b_data, 0, bh->b_size); 4371 + set_buffer_uptodate(bh); 4346 4372 unlock_buffer(bh); 4347 4373 goto has_buffer; 4348 4374 } 4349 - 4350 - /* 4351 - * If we have all information of the inode in memory and this 4352 - * is the only valid inode in the block, we need not read the 4353 - * block. 4354 - */ 4355 - if (in_mem) { 4356 - struct buffer_head *bitmap_bh; 4357 - int i, start; 4358 - 4359 - start = inode_offset & ~(inodes_per_block - 1); 4360 - 4361 - /* Is the inode bitmap in cache? */ 4362 - bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4363 - if (unlikely(!bitmap_bh)) 4364 - goto make_io; 4365 - 4366 - /* 4367 - * If the inode bitmap isn't in cache then the 4368 - * optimisation may end up performing two reads instead 4369 - * of one, so skip it. 4370 - */ 4371 - if (!buffer_uptodate(bitmap_bh)) { 4372 - brelse(bitmap_bh); 4373 - goto make_io; 4374 - } 4375 - for (i = start; i < start + inodes_per_block; i++) { 4376 - if (i == inode_offset) 4377 - continue; 4378 - if (ext4_test_bit(i, bitmap_bh->b_data)) 4379 - break; 4380 - } 4381 - brelse(bitmap_bh); 4382 - if (i == start + inodes_per_block) { 4383 - /* all other inodes are free, so skip I/O */ 4384 - memset(bh->b_data, 0, bh->b_size); 4385 - set_buffer_uptodate(bh); 4386 - unlock_buffer(bh); 4387 - goto has_buffer; 4388 - } 4389 - } 4375 + } 4390 4376 4391 4377 make_io: 4392 - /* 4393 - * If we need to do any I/O, try to pre-readahead extra 4394 - * blocks from the inode table. 4395 - */ 4396 - blk_start_plug(&plug); 4397 - if (EXT4_SB(sb)->s_inode_readahead_blks) { 4398 - ext4_fsblk_t b, end, table; 4399 - unsigned num; 4400 - __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4378 + /* 4379 + * If we need to do any I/O, try to pre-readahead extra 4380 + * blocks from the inode table. 4381 + */ 4382 + blk_start_plug(&plug); 4383 + if (EXT4_SB(sb)->s_inode_readahead_blks) { 4384 + ext4_fsblk_t b, end, table; 4385 + unsigned num; 4386 + __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 4401 4387 4402 - table = ext4_inode_table(sb, gdp); 4403 - /* s_inode_readahead_blks is always a power of 2 */ 4404 - b = block & ~((ext4_fsblk_t) ra_blks - 1); 4405 - if (table > b) 4406 - b = table; 4407 - end = b + ra_blks; 4408 - num = EXT4_INODES_PER_GROUP(sb); 4409 - if (ext4_has_group_desc_csum(sb)) 4410 - num -= ext4_itable_unused_count(sb, gdp); 4411 - table += num / inodes_per_block; 4412 - if (end > table) 4413 - end = table; 4414 - while (b <= end) 4415 - ext4_sb_breadahead_unmovable(sb, b++); 4416 - } 4388 + table = ext4_inode_table(sb, gdp); 4389 + /* s_inode_readahead_blks is always a power of 2 */ 4390 + b = block & ~((ext4_fsblk_t) ra_blks - 1); 4391 + if (table > b) 4392 + b = table; 4393 + end = b + ra_blks; 4394 + num = EXT4_INODES_PER_GROUP(sb); 4395 + if (ext4_has_group_desc_csum(sb)) 4396 + num -= ext4_itable_unused_count(sb, gdp); 4397 + table += num / inodes_per_block; 4398 + if (end > table) 4399 + end = table; 4400 + while (b <= end) 4401 + ext4_sb_breadahead_unmovable(sb, b++); 4402 + } 4417 4403 4418 - /* 4419 - * There are other valid inodes in the buffer, this inode 4420 - * has in-inode xattrs, or we don't have this inode in memory. 4421 - * Read the block from disk. 4422 - */ 4423 - trace_ext4_load_inode(sb, ino); 4424 - ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 4425 - blk_finish_plug(&plug); 4426 - wait_on_buffer(bh); 4427 - if (!buffer_uptodate(bh)) { 4428 - simulate_eio: 4429 - if (ret_block) 4430 - *ret_block = block; 4431 - brelse(bh); 4432 - return -EIO; 4433 - } 4404 + /* 4405 + * There are other valid inodes in the buffer, this inode 4406 + * has in-inode xattrs, or we don't have this inode in memory. 4407 + * Read the block from disk. 4408 + */ 4409 + trace_ext4_load_inode(sb, ino); 4410 + ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL); 4411 + blk_finish_plug(&plug); 4412 + wait_on_buffer(bh); 4413 + ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO); 4414 + if (!buffer_uptodate(bh)) { 4415 + if (ret_block) 4416 + *ret_block = block; 4417 + brelse(bh); 4418 + return -EIO; 4434 4419 } 4435 4420 has_buffer: 4436 4421 iloc->bh = bh; ··· 4601 4602 struct ext4_iloc iloc; 4602 4603 struct ext4_inode *raw_inode; 4603 4604 struct ext4_inode_info *ei; 4605 + struct ext4_super_block *es = EXT4_SB(sb)->s_es; 4604 4606 struct inode *inode; 4605 4607 journal_t *journal = EXT4_SB(sb)->s_journal; 4606 4608 long ret; ··· 4612 4612 projid_t i_projid; 4613 4613 4614 4614 if ((!(flags & EXT4_IGET_SPECIAL) && 4615 - (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) || 4615 + ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) || 4616 + ino == le32_to_cpu(es->s_usr_quota_inum) || 4617 + ino == le32_to_cpu(es->s_grp_quota_inum) || 4618 + ino == le32_to_cpu(es->s_prj_quota_inum) || 4619 + ino == le32_to_cpu(es->s_orphan_file_inum))) || 4616 4620 (ino < EXT4_ROOT_INO) || 4617 - (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) { 4621 + (ino > le32_to_cpu(es->s_inodes_count))) { 4618 4622 if (flags & EXT4_IGET_HANDLE) 4619 4623 return ERR_PTR(-ESTALE); 4620 4624 __ext4_error(sb, function, line, false, EFSCORRUPTED, 0, ··· 4931 4927 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4932 4928 return 0; 4933 4929 } 4930 + 4931 + /* 4932 + * This should never happen since sb->s_maxbytes should not have 4933 + * allowed this, sb->s_maxbytes was set according to the huge_file 4934 + * feature in ext4_fill_super(). 4935 + */ 4934 4936 if (!ext4_has_feature_huge_file(sb)) 4935 - return -EFBIG; 4937 + return -EFSCORRUPTED; 4936 4938 4937 4939 if (i_blocks <= 0xffffffffffffULL) { 4938 4940 /* ··· 5041 5031 5042 5032 spin_lock(&ei->i_raw_lock); 5043 5033 5044 - /* For fields not tracked in the in-memory inode, 5045 - * initialise them to zero for new inodes. */ 5034 + /* 5035 + * For fields not tracked in the in-memory inode, initialise them 5036 + * to zero for new inodes. 5037 + */ 5046 5038 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5047 5039 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5048 5040 5049 5041 err = ext4_inode_blocks_set(handle, raw_inode, ei); 5050 - if (err) { 5051 - spin_unlock(&ei->i_raw_lock); 5052 - goto out_brelse; 5053 - } 5054 5042 5055 5043 raw_inode->i_mode = cpu_to_le16(inode->i_mode); 5056 5044 i_uid = i_uid_read(inode); ··· 5057 5049 if (!(test_opt(inode->i_sb, NO_UID32))) { 5058 5050 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 5059 5051 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 5060 - /* 5061 - * Fix up interoperability with old kernels. Otherwise, old inodes get 5062 - * re-used with the upper 16 bits of the uid/gid intact 5063 - */ 5052 + /* 5053 + * Fix up interoperability with old kernels. Otherwise, 5054 + * old inodes get re-used with the upper 16 bits of the 5055 + * uid/gid intact. 5056 + */ 5064 5057 if (ei->i_dtime && list_empty(&ei->i_orphan)) { 5065 5058 raw_inode->i_uid_high = 0; 5066 5059 raw_inode->i_gid_high = 0; ··· 5130 5121 } 5131 5122 } 5132 5123 5133 - BUG_ON(!ext4_has_feature_project(inode->i_sb) && 5134 - i_projid != EXT4_DEF_PROJID); 5124 + if (i_projid != EXT4_DEF_PROJID && 5125 + !ext4_has_feature_project(inode->i_sb)) 5126 + err = err ?: -EFSCORRUPTED; 5135 5127 5136 5128 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 5137 5129 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) ··· 5140 5130 5141 5131 ext4_inode_csum_set(inode, raw_inode, ei); 5142 5132 spin_unlock(&ei->i_raw_lock); 5133 + if (err) { 5134 + EXT4_ERROR_INODE(inode, "corrupted inode contents"); 5135 + goto out_brelse; 5136 + } 5137 + 5143 5138 if (inode->i_sb->s_flags & SB_LAZYTIME) 5144 5139 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino, 5145 5140 bh->b_data); ··· 5152 5137 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 5153 5138 err = ext4_handle_dirty_metadata(handle, NULL, bh); 5154 5139 if (err) 5155 - goto out_brelse; 5140 + goto out_error; 5156 5141 ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5157 5142 if (set_large_file) { 5158 5143 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access"); 5159 - err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 5144 + err = ext4_journal_get_write_access(handle, sb, 5145 + EXT4_SB(sb)->s_sbh, 5146 + EXT4_JTR_NONE); 5160 5147 if (err) 5161 - goto out_brelse; 5148 + goto out_error; 5162 5149 lock_buffer(EXT4_SB(sb)->s_sbh); 5163 5150 ext4_set_feature_large_file(sb); 5164 5151 ext4_superblock_csum_set(sb); ··· 5170 5153 EXT4_SB(sb)->s_sbh); 5171 5154 } 5172 5155 ext4_update_inode_fsync_trans(handle, inode, need_datasync); 5156 + out_error: 5157 + ext4_std_error(inode->i_sb, err); 5173 5158 out_brelse: 5174 5159 brelse(bh); 5175 - ext4_std_error(inode->i_sb, err); 5176 5160 return err; 5177 5161 } 5178 5162 ··· 5760 5742 err = ext4_get_inode_loc(inode, iloc); 5761 5743 if (!err) { 5762 5744 BUFFER_TRACE(iloc->bh, "get_write_access"); 5763 - err = ext4_journal_get_write_access(handle, iloc->bh); 5745 + err = ext4_journal_get_write_access(handle, inode->i_sb, 5746 + iloc->bh, EXT4_JTR_NONE); 5764 5747 if (err) { 5765 5748 brelse(iloc->bh); 5766 5749 iloc->bh = NULL; ··· 5884 5865 ext4_write_lock_xattr(inode, &no_expand); 5885 5866 5886 5867 BUFFER_TRACE(iloc->bh, "get_write_access"); 5887 - error = ext4_journal_get_write_access(handle, iloc->bh); 5868 + error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh, 5869 + EXT4_JTR_NONE); 5888 5870 if (error) { 5889 5871 brelse(iloc->bh); 5890 5872 goto out_unlock; ··· 6056 6036 return err; 6057 6037 } 6058 6038 6059 - static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 6039 + static int ext4_bh_unmapped(handle_t *handle, struct inode *inode, 6040 + struct buffer_head *bh) 6060 6041 { 6061 6042 return !buffer_mapped(bh); 6062 6043 } ··· 6130 6109 * inode to the transaction's list to writeprotect pages on commit. 6131 6110 */ 6132 6111 if (page_has_buffers(page)) { 6133 - if (!ext4_walk_page_buffers(NULL, page_buffers(page), 6112 + if (!ext4_walk_page_buffers(NULL, inode, page_buffers(page), 6134 6113 0, len, NULL, 6135 6114 ext4_bh_unmapped)) { 6136 6115 /* Wait so that we don't change page under IO */ ··· 6176 6155 err = __block_write_begin(page, 0, len, ext4_get_block); 6177 6156 if (!err) { 6178 6157 ret = VM_FAULT_SIGBUS; 6179 - if (ext4_walk_page_buffers(handle, page_buffers(page), 6180 - 0, len, NULL, do_journal_get_write_access)) 6158 + if (ext4_walk_page_buffers(handle, inode, 6159 + page_buffers(page), 0, len, NULL, 6160 + do_journal_get_write_access)) 6181 6161 goto out_error; 6182 - if (ext4_walk_page_buffers(handle, page_buffers(page), 6183 - 0, len, NULL, write_end_fn)) 6162 + if (ext4_walk_page_buffers(handle, inode, 6163 + page_buffers(page), 0, len, NULL, 6164 + write_end_fn)) 6184 6165 goto out_error; 6185 6166 if (ext4_jbd2_inode_add_write(handle, inode, 6186 6167 page_offset(page), len))
+3 -1
fs/ext4/ioctl.c
··· 1154 1154 err = PTR_ERR(handle); 1155 1155 goto pwsalt_err_exit; 1156 1156 } 1157 - err = ext4_journal_get_write_access(handle, sbi->s_sbh); 1157 + err = ext4_journal_get_write_access(handle, sb, 1158 + sbi->s_sbh, 1159 + EXT4_JTR_NONE); 1158 1160 if (err) 1159 1161 goto pwsalt_err_journal; 1160 1162 lock_buffer(sbi->s_sbh);
+171 -90
fs/ext4/mballoc.c
··· 408 408 static bool ext4_mb_good_group(struct ext4_allocation_context *ac, 409 409 ext4_group_t group, int cr); 410 410 411 + static int ext4_try_to_trim_range(struct super_block *sb, 412 + struct ext4_buddy *e4b, ext4_grpblk_t start, 413 + ext4_grpblk_t max, ext4_grpblk_t minblocks); 414 + 411 415 /* 412 416 * The algorithm using this percpu seq counter goes below: 413 417 * 1. We sample the percpu discard_pa_seq counter before trying for block ··· 2478 2474 * This could return negative error code if something goes wrong 2479 2475 * during ext4_mb_init_group(). This should not be called with 2480 2476 * ext4_lock_group() held. 2477 + * 2478 + * Note: because we are conditionally operating with the group lock in 2479 + * the EXT4_MB_STRICT_CHECK case, we need to fake out sparse in this 2480 + * function using __acquire and __release. This means we need to be 2481 + * super careful before messing with the error path handling via "goto 2482 + * out"! 2481 2483 */ 2482 2484 static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac, 2483 2485 ext4_group_t group, int cr) ··· 2497 2487 2498 2488 if (sbi->s_mb_stats) 2499 2489 atomic64_inc(&sbi->s_bal_cX_groups_considered[ac->ac_criteria]); 2500 - if (should_lock) 2490 + if (should_lock) { 2501 2491 ext4_lock_group(sb, group); 2492 + __release(ext4_group_lock_ptr(sb, group)); 2493 + } 2502 2494 free = grp->bb_free; 2503 2495 if (free == 0) 2504 2496 goto out; ··· 2508 2496 goto out; 2509 2497 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) 2510 2498 goto out; 2511 - if (should_lock) 2499 + if (should_lock) { 2500 + __acquire(ext4_group_lock_ptr(sb, group)); 2512 2501 ext4_unlock_group(sb, group); 2502 + } 2513 2503 2514 2504 /* We only do this if the grp has never been initialized */ 2515 2505 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { ··· 2538 2524 return ret; 2539 2525 } 2540 2526 2541 - if (should_lock) 2527 + if (should_lock) { 2542 2528 ext4_lock_group(sb, group); 2529 + __release(ext4_group_lock_ptr(sb, group)); 2530 + } 2543 2531 ret = ext4_mb_good_group(ac, group, cr); 2544 2532 out: 2545 - if (should_lock) 2533 + if (should_lock) { 2534 + __acquire(ext4_group_lock_ptr(sb, group)); 2546 2535 ext4_unlock_group(sb, group); 2536 + } 2547 2537 return ret; 2548 2538 } 2549 2539 ··· 2983 2965 } 2984 2966 2985 2967 static void *ext4_mb_seq_structs_summary_start(struct seq_file *seq, loff_t *pos) 2968 + __acquires(&EXT4_SB(sb)->s_mb_rb_lock) 2986 2969 { 2987 2970 struct super_block *sb = PDE_DATA(file_inode(seq->file)); 2988 2971 unsigned long position; ··· 3056 3037 } 3057 3038 3058 3039 static void ext4_mb_seq_structs_summary_stop(struct seq_file *seq, void *v) 3040 + __releases(&EXT4_SB(sb)->s_mb_rb_lock) 3059 3041 { 3060 3042 struct super_block *sb = PDE_DATA(file_inode(seq->file)); 3061 3043 ··· 3328 3308 return 0; 3329 3309 } 3330 3310 3311 + static void ext4_discard_work(struct work_struct *work) 3312 + { 3313 + struct ext4_sb_info *sbi = container_of(work, 3314 + struct ext4_sb_info, s_discard_work); 3315 + struct super_block *sb = sbi->s_sb; 3316 + struct ext4_free_data *fd, *nfd; 3317 + struct ext4_buddy e4b; 3318 + struct list_head discard_list; 3319 + ext4_group_t grp, load_grp; 3320 + int err = 0; 3321 + 3322 + INIT_LIST_HEAD(&discard_list); 3323 + spin_lock(&sbi->s_md_lock); 3324 + list_splice_init(&sbi->s_discard_list, &discard_list); 3325 + spin_unlock(&sbi->s_md_lock); 3326 + 3327 + load_grp = UINT_MAX; 3328 + list_for_each_entry_safe(fd, nfd, &discard_list, efd_list) { 3329 + /* 3330 + * If filesystem is umounting or no memory or suffering 3331 + * from no space, give up the discard 3332 + */ 3333 + if ((sb->s_flags & SB_ACTIVE) && !err && 3334 + !atomic_read(&sbi->s_retry_alloc_pending)) { 3335 + grp = fd->efd_group; 3336 + if (grp != load_grp) { 3337 + if (load_grp != UINT_MAX) 3338 + ext4_mb_unload_buddy(&e4b); 3339 + 3340 + err = ext4_mb_load_buddy(sb, grp, &e4b); 3341 + if (err) { 3342 + kmem_cache_free(ext4_free_data_cachep, fd); 3343 + load_grp = UINT_MAX; 3344 + continue; 3345 + } else { 3346 + load_grp = grp; 3347 + } 3348 + } 3349 + 3350 + ext4_lock_group(sb, grp); 3351 + ext4_try_to_trim_range(sb, &e4b, fd->efd_start_cluster, 3352 + fd->efd_start_cluster + fd->efd_count - 1, 1); 3353 + ext4_unlock_group(sb, grp); 3354 + } 3355 + kmem_cache_free(ext4_free_data_cachep, fd); 3356 + } 3357 + 3358 + if (load_grp != UINT_MAX) 3359 + ext4_mb_unload_buddy(&e4b); 3360 + } 3361 + 3331 3362 int ext4_mb_init(struct super_block *sb) 3332 3363 { 3333 3364 struct ext4_sb_info *sbi = EXT4_SB(sb); ··· 3447 3376 spin_lock_init(&sbi->s_md_lock); 3448 3377 sbi->s_mb_free_pending = 0; 3449 3378 INIT_LIST_HEAD(&sbi->s_freed_data_list); 3379 + INIT_LIST_HEAD(&sbi->s_discard_list); 3380 + INIT_WORK(&sbi->s_discard_work, ext4_discard_work); 3381 + atomic_set(&sbi->s_retry_alloc_pending, 0); 3450 3382 3451 3383 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN; 3452 3384 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN; ··· 3547 3473 struct ext4_sb_info *sbi = EXT4_SB(sb); 3548 3474 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); 3549 3475 int count; 3476 + 3477 + if (test_opt(sb, DISCARD)) { 3478 + /* 3479 + * wait the discard work to drain all of ext4_free_data 3480 + */ 3481 + flush_work(&sbi->s_discard_work); 3482 + WARN_ON_ONCE(!list_empty(&sbi->s_discard_list)); 3483 + } 3550 3484 3551 3485 if (sbi->s_group_info) { 3552 3486 for (i = 0; i < ngroups; i++) { ··· 3678 3596 put_page(e4b.bd_bitmap_page); 3679 3597 } 3680 3598 ext4_unlock_group(sb, entry->efd_group); 3681 - kmem_cache_free(ext4_free_data_cachep, entry); 3682 3599 ext4_mb_unload_buddy(&e4b); 3683 3600 3684 3601 mb_debug(sb, "freed %d blocks in %d structures\n", count, ··· 3692 3611 { 3693 3612 struct ext4_sb_info *sbi = EXT4_SB(sb); 3694 3613 struct ext4_free_data *entry, *tmp; 3695 - struct bio *discard_bio = NULL; 3696 3614 struct list_head freed_data_list; 3697 3615 struct list_head *cut_pos = NULL; 3698 - int err; 3616 + bool wake; 3699 3617 3700 3618 INIT_LIST_HEAD(&freed_data_list); 3701 3619 ··· 3709 3629 cut_pos); 3710 3630 spin_unlock(&sbi->s_md_lock); 3711 3631 3712 - if (test_opt(sb, DISCARD)) { 3713 - list_for_each_entry(entry, &freed_data_list, efd_list) { 3714 - err = ext4_issue_discard(sb, entry->efd_group, 3715 - entry->efd_start_cluster, 3716 - entry->efd_count, 3717 - &discard_bio); 3718 - if (err && err != -EOPNOTSUPP) { 3719 - ext4_msg(sb, KERN_WARNING, "discard request in" 3720 - " group:%d block:%d count:%d failed" 3721 - " with %d", entry->efd_group, 3722 - entry->efd_start_cluster, 3723 - entry->efd_count, err); 3724 - } else if (err == -EOPNOTSUPP) 3725 - break; 3726 - } 3727 - 3728 - if (discard_bio) { 3729 - submit_bio_wait(discard_bio); 3730 - bio_put(discard_bio); 3731 - } 3732 - } 3733 - 3734 - list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list) 3632 + list_for_each_entry(entry, &freed_data_list, efd_list) 3735 3633 ext4_free_data_in_buddy(sb, entry); 3634 + 3635 + if (test_opt(sb, DISCARD)) { 3636 + spin_lock(&sbi->s_md_lock); 3637 + wake = list_empty(&sbi->s_discard_list); 3638 + list_splice_tail(&freed_data_list, &sbi->s_discard_list); 3639 + spin_unlock(&sbi->s_md_lock); 3640 + if (wake) 3641 + queue_work(system_unbound_wq, &sbi->s_discard_work); 3642 + } else { 3643 + list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list) 3644 + kmem_cache_free(ext4_free_data_cachep, entry); 3645 + } 3736 3646 } 3737 3647 3738 3648 int __init ext4_init_mballoc(void) ··· 3796 3726 } 3797 3727 3798 3728 BUFFER_TRACE(bitmap_bh, "getting write access"); 3799 - err = ext4_journal_get_write_access(handle, bitmap_bh); 3729 + err = ext4_journal_get_write_access(handle, sb, bitmap_bh, 3730 + EXT4_JTR_NONE); 3800 3731 if (err) 3801 3732 goto out_err; 3802 3733 ··· 3810 3739 ext4_free_group_clusters(sb, gdp)); 3811 3740 3812 3741 BUFFER_TRACE(gdp_bh, "get_write_access"); 3813 - err = ext4_journal_get_write_access(handle, gdp_bh); 3742 + err = ext4_journal_get_write_access(handle, sb, gdp_bh, EXT4_JTR_NONE); 3814 3743 if (err) 3815 3744 goto out_err; 3816 3745 ··· 5987 5916 } 5988 5917 5989 5918 BUFFER_TRACE(bitmap_bh, "getting write access"); 5990 - err = ext4_journal_get_write_access(handle, bitmap_bh); 5919 + err = ext4_journal_get_write_access(handle, sb, bitmap_bh, 5920 + EXT4_JTR_NONE); 5991 5921 if (err) 5992 5922 goto error_return; 5993 5923 ··· 5998 5926 * using it 5999 5927 */ 6000 5928 BUFFER_TRACE(gd_bh, "get_write_access"); 6001 - err = ext4_journal_get_write_access(handle, gd_bh); 5929 + err = ext4_journal_get_write_access(handle, sb, gd_bh, EXT4_JTR_NONE); 6002 5930 if (err) 6003 5931 goto error_return; 6004 5932 #ifdef AGGRESSIVE_CHECK ··· 6179 6107 } 6180 6108 6181 6109 BUFFER_TRACE(bitmap_bh, "getting write access"); 6182 - err = ext4_journal_get_write_access(handle, bitmap_bh); 6110 + err = ext4_journal_get_write_access(handle, sb, bitmap_bh, 6111 + EXT4_JTR_NONE); 6183 6112 if (err) 6184 6113 goto error_return; 6185 6114 ··· 6190 6117 * using it 6191 6118 */ 6192 6119 BUFFER_TRACE(gd_bh, "get_write_access"); 6193 - err = ext4_journal_get_write_access(handle, gd_bh); 6120 + err = ext4_journal_get_write_access(handle, sb, gd_bh, EXT4_JTR_NONE); 6194 6121 if (err) 6195 6122 goto error_return; 6196 6123 ··· 6256 6183 * @sb: super block for the file system 6257 6184 * @start: starting block of the free extent in the alloc. group 6258 6185 * @count: number of blocks to TRIM 6259 - * @group: alloc. group we are working with 6260 6186 * @e4b: ext4 buddy for the group 6261 6187 * 6262 6188 * Trim "count" blocks starting at "start" in the "group". To assure that no 6263 6189 * one will allocate those blocks, mark it as used in buddy bitmap. This must 6264 6190 * be called with under the group lock. 6265 6191 */ 6266 - static int ext4_trim_extent(struct super_block *sb, int start, int count, 6267 - ext4_group_t group, struct ext4_buddy *e4b) 6192 + static int ext4_trim_extent(struct super_block *sb, 6193 + int start, int count, struct ext4_buddy *e4b) 6268 6194 __releases(bitlock) 6269 6195 __acquires(bitlock) 6270 6196 { 6271 6197 struct ext4_free_extent ex; 6198 + ext4_group_t group = e4b->bd_group; 6272 6199 int ret = 0; 6273 6200 6274 6201 trace_ext4_trim_extent(sb, group, start, count); ··· 6291 6218 return ret; 6292 6219 } 6293 6220 6294 - /** 6295 - * ext4_trim_all_free -- function to trim all free space in alloc. group 6296 - * @sb: super block for file system 6297 - * @group: group to be trimmed 6298 - * @start: first group block to examine 6299 - * @max: last group block to examine 6300 - * @minblocks: minimum extent block count 6301 - * 6302 - * ext4_trim_all_free walks through group's buddy bitmap searching for free 6303 - * extents. When the free block is found, ext4_trim_extent is called to TRIM 6304 - * the extent. 6305 - * 6306 - * 6307 - * ext4_trim_all_free walks through group's block bitmap searching for free 6308 - * extents. When the free extent is found, mark it as used in group buddy 6309 - * bitmap. Then issue a TRIM command on this extent and free the extent in 6310 - * the group buddy bitmap. This is done until whole group is scanned. 6311 - */ 6312 - static ext4_grpblk_t 6313 - ext4_trim_all_free(struct super_block *sb, ext4_group_t group, 6314 - ext4_grpblk_t start, ext4_grpblk_t max, 6315 - ext4_grpblk_t minblocks) 6221 + static int ext4_try_to_trim_range(struct super_block *sb, 6222 + struct ext4_buddy *e4b, ext4_grpblk_t start, 6223 + ext4_grpblk_t max, ext4_grpblk_t minblocks) 6224 + __acquires(ext4_group_lock_ptr(sb, e4b->bd_group)) 6225 + __releases(ext4_group_lock_ptr(sb, e4b->bd_group)) 6316 6226 { 6227 + ext4_grpblk_t next, count, free_count; 6317 6228 void *bitmap; 6318 - ext4_grpblk_t next, count = 0, free_count = 0; 6319 - struct ext4_buddy e4b; 6320 6229 int ret = 0; 6321 6230 6322 - trace_ext4_trim_all_free(sb, group, start, max); 6323 - 6324 - ret = ext4_mb_load_buddy(sb, group, &e4b); 6325 - if (ret) { 6326 - ext4_warning(sb, "Error %d loading buddy information for %u", 6327 - ret, group); 6328 - return ret; 6329 - } 6330 - bitmap = e4b.bd_bitmap; 6331 - 6332 - ext4_lock_group(sb, group); 6333 - if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) && 6334 - minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks)) 6335 - goto out; 6336 - 6337 - start = (e4b.bd_info->bb_first_free > start) ? 6338 - e4b.bd_info->bb_first_free : start; 6231 + bitmap = e4b->bd_bitmap; 6232 + start = (e4b->bd_info->bb_first_free > start) ? 6233 + e4b->bd_info->bb_first_free : start; 6234 + count = 0; 6235 + free_count = 0; 6339 6236 6340 6237 while (start <= max) { 6341 6238 start = mb_find_next_zero_bit(bitmap, max + 1, start); ··· 6314 6271 next = mb_find_next_bit(bitmap, max + 1, start); 6315 6272 6316 6273 if ((next - start) >= minblocks) { 6317 - ret = ext4_trim_extent(sb, start, 6318 - next - start, group, &e4b); 6274 + ret = ext4_trim_extent(sb, start, next - start, e4b); 6319 6275 if (ret && ret != -EOPNOTSUPP) 6320 6276 break; 6321 6277 ret = 0; ··· 6329 6287 } 6330 6288 6331 6289 if (need_resched()) { 6332 - ext4_unlock_group(sb, group); 6290 + ext4_unlock_group(sb, e4b->bd_group); 6333 6291 cond_resched(); 6334 - ext4_lock_group(sb, group); 6292 + ext4_lock_group(sb, e4b->bd_group); 6335 6293 } 6336 6294 6337 - if ((e4b.bd_info->bb_free - free_count) < minblocks) 6295 + if ((e4b->bd_info->bb_free - free_count) < minblocks) 6338 6296 break; 6339 6297 } 6340 6298 6341 - if (!ret) { 6342 - ret = count; 6343 - EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info); 6299 + return count; 6300 + } 6301 + 6302 + /** 6303 + * ext4_trim_all_free -- function to trim all free space in alloc. group 6304 + * @sb: super block for file system 6305 + * @group: group to be trimmed 6306 + * @start: first group block to examine 6307 + * @max: last group block to examine 6308 + * @minblocks: minimum extent block count 6309 + * 6310 + * ext4_trim_all_free walks through group's block bitmap searching for free 6311 + * extents. When the free extent is found, mark it as used in group buddy 6312 + * bitmap. Then issue a TRIM command on this extent and free the extent in 6313 + * the group buddy bitmap. 6314 + */ 6315 + static ext4_grpblk_t 6316 + ext4_trim_all_free(struct super_block *sb, ext4_group_t group, 6317 + ext4_grpblk_t start, ext4_grpblk_t max, 6318 + ext4_grpblk_t minblocks) 6319 + { 6320 + struct ext4_buddy e4b; 6321 + int ret; 6322 + 6323 + trace_ext4_trim_all_free(sb, group, start, max); 6324 + 6325 + ret = ext4_mb_load_buddy(sb, group, &e4b); 6326 + if (ret) { 6327 + ext4_warning(sb, "Error %d loading buddy information for %u", 6328 + ret, group); 6329 + return ret; 6344 6330 } 6345 - out: 6331 + 6332 + ext4_lock_group(sb, group); 6333 + 6334 + if (!EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) || 6335 + minblocks < atomic_read(&EXT4_SB(sb)->s_last_trim_minblks)) { 6336 + ret = ext4_try_to_trim_range(sb, &e4b, start, max, minblocks); 6337 + if (ret >= 0) 6338 + EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info); 6339 + } else { 6340 + ret = 0; 6341 + } 6342 + 6346 6343 ext4_unlock_group(sb, group); 6347 6344 ext4_mb_unload_buddy(&e4b); 6348 6345 6349 6346 ext4_debug("trimmed %d blocks in the group %d\n", 6350 - count, group); 6347 + ret, group); 6351 6348 6352 6349 return ret; 6353 6350 }
+22 -192
fs/ext4/namei.c
··· 70 70 inode->i_size += inode->i_sb->s_blocksize; 71 71 EXT4_I(inode)->i_disksize = inode->i_size; 72 72 BUFFER_TRACE(bh, "get_write_access"); 73 - err = ext4_journal_get_write_access(handle, bh); 73 + err = ext4_journal_get_write_access(handle, inode->i_sb, bh, 74 + EXT4_JTR_NONE); 74 75 if (err) { 75 76 brelse(bh); 76 77 ext4_std_error(inode->i_sb, err); ··· 1928 1927 } 1929 1928 1930 1929 BUFFER_TRACE(*bh, "get_write_access"); 1931 - err = ext4_journal_get_write_access(handle, *bh); 1930 + err = ext4_journal_get_write_access(handle, dir->i_sb, *bh, 1931 + EXT4_JTR_NONE); 1932 1932 if (err) 1933 1933 goto journal_error; 1934 1934 1935 1935 BUFFER_TRACE(frame->bh, "get_write_access"); 1936 - err = ext4_journal_get_write_access(handle, frame->bh); 1936 + err = ext4_journal_get_write_access(handle, dir->i_sb, frame->bh, 1937 + EXT4_JTR_NONE); 1937 1938 if (err) 1938 1939 goto journal_error; 1939 1940 ··· 2112 2109 return err; 2113 2110 } 2114 2111 BUFFER_TRACE(bh, "get_write_access"); 2115 - err = ext4_journal_get_write_access(handle, bh); 2112 + err = ext4_journal_get_write_access(handle, dir->i_sb, bh, 2113 + EXT4_JTR_NONE); 2116 2114 if (err) { 2117 2115 ext4_std_error(dir->i_sb, err); 2118 2116 return err; ··· 2171 2167 blocksize = dir->i_sb->s_blocksize; 2172 2168 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino)); 2173 2169 BUFFER_TRACE(bh, "get_write_access"); 2174 - retval = ext4_journal_get_write_access(handle, bh); 2170 + retval = ext4_journal_get_write_access(handle, dir->i_sb, bh, 2171 + EXT4_JTR_NONE); 2175 2172 if (retval) { 2176 2173 ext4_std_error(dir->i_sb, retval); 2177 2174 brelse(bh); ··· 2424 2419 } 2425 2420 2426 2421 BUFFER_TRACE(bh, "get_write_access"); 2427 - err = ext4_journal_get_write_access(handle, bh); 2422 + err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE); 2428 2423 if (err) 2429 2424 goto journal_error; 2430 2425 ··· 2481 2476 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize, 2482 2477 sb->s_blocksize); 2483 2478 BUFFER_TRACE(frame->bh, "get_write_access"); 2484 - err = ext4_journal_get_write_access(handle, frame->bh); 2479 + err = ext4_journal_get_write_access(handle, sb, frame->bh, 2480 + EXT4_JTR_NONE); 2485 2481 if (err) 2486 2482 goto journal_error; 2487 2483 if (!add_level) { ··· 2492 2486 icount1, icount2)); 2493 2487 2494 2488 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */ 2495 - err = ext4_journal_get_write_access(handle, 2496 - (frame - 1)->bh); 2489 + err = ext4_journal_get_write_access(handle, sb, 2490 + (frame - 1)->bh, 2491 + EXT4_JTR_NONE); 2497 2492 if (err) 2498 2493 goto journal_error; 2499 2494 ··· 2643 2636 csum_size = sizeof(struct ext4_dir_entry_tail); 2644 2637 2645 2638 BUFFER_TRACE(bh, "get_write_access"); 2646 - err = ext4_journal_get_write_access(handle, bh); 2639 + err = ext4_journal_get_write_access(handle, dir->i_sb, bh, 2640 + EXT4_JTR_NONE); 2647 2641 if (unlikely(err)) 2648 2642 goto out; 2649 2643 ··· 3052 3044 } 3053 3045 brelse(bh); 3054 3046 return true; 3055 - } 3056 - 3057 - /* 3058 - * ext4_orphan_add() links an unlinked or truncated inode into a list of 3059 - * such inodes, starting at the superblock, in case we crash before the 3060 - * file is closed/deleted, or in case the inode truncate spans multiple 3061 - * transactions and the last transaction is not recovered after a crash. 3062 - * 3063 - * At filesystem recovery time, we walk this list deleting unlinked 3064 - * inodes and truncating linked inodes in ext4_orphan_cleanup(). 3065 - * 3066 - * Orphan list manipulation functions must be called under i_mutex unless 3067 - * we are just creating the inode or deleting it. 3068 - */ 3069 - int ext4_orphan_add(handle_t *handle, struct inode *inode) 3070 - { 3071 - struct super_block *sb = inode->i_sb; 3072 - struct ext4_sb_info *sbi = EXT4_SB(sb); 3073 - struct ext4_iloc iloc; 3074 - int err = 0, rc; 3075 - bool dirty = false; 3076 - 3077 - if (!sbi->s_journal || is_bad_inode(inode)) 3078 - return 0; 3079 - 3080 - WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 3081 - !inode_is_locked(inode)); 3082 - /* 3083 - * Exit early if inode already is on orphan list. This is a big speedup 3084 - * since we don't have to contend on the global s_orphan_lock. 3085 - */ 3086 - if (!list_empty(&EXT4_I(inode)->i_orphan)) 3087 - return 0; 3088 - 3089 - /* 3090 - * Orphan handling is only valid for files with data blocks 3091 - * being truncated, or files being unlinked. Note that we either 3092 - * hold i_mutex, or the inode can not be referenced from outside, 3093 - * so i_nlink should not be bumped due to race 3094 - */ 3095 - ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 3096 - S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); 3097 - 3098 - BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 3099 - err = ext4_journal_get_write_access(handle, sbi->s_sbh); 3100 - if (err) 3101 - goto out; 3102 - 3103 - err = ext4_reserve_inode_write(handle, inode, &iloc); 3104 - if (err) 3105 - goto out; 3106 - 3107 - mutex_lock(&sbi->s_orphan_lock); 3108 - /* 3109 - * Due to previous errors inode may be already a part of on-disk 3110 - * orphan list. If so skip on-disk list modification. 3111 - */ 3112 - if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) > 3113 - (le32_to_cpu(sbi->s_es->s_inodes_count))) { 3114 - /* Insert this inode at the head of the on-disk orphan list */ 3115 - NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan); 3116 - lock_buffer(sbi->s_sbh); 3117 - sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); 3118 - ext4_superblock_csum_set(sb); 3119 - unlock_buffer(sbi->s_sbh); 3120 - dirty = true; 3121 - } 3122 - list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan); 3123 - mutex_unlock(&sbi->s_orphan_lock); 3124 - 3125 - if (dirty) { 3126 - err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 3127 - rc = ext4_mark_iloc_dirty(handle, inode, &iloc); 3128 - if (!err) 3129 - err = rc; 3130 - if (err) { 3131 - /* 3132 - * We have to remove inode from in-memory list if 3133 - * addition to on disk orphan list failed. Stray orphan 3134 - * list entries can cause panics at unmount time. 3135 - */ 3136 - mutex_lock(&sbi->s_orphan_lock); 3137 - list_del_init(&EXT4_I(inode)->i_orphan); 3138 - mutex_unlock(&sbi->s_orphan_lock); 3139 - } 3140 - } else 3141 - brelse(iloc.bh); 3142 - 3143 - jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); 3144 - jbd_debug(4, "orphan inode %lu will point to %d\n", 3145 - inode->i_ino, NEXT_ORPHAN(inode)); 3146 - out: 3147 - ext4_std_error(sb, err); 3148 - return err; 3149 - } 3150 - 3151 - /* 3152 - * ext4_orphan_del() removes an unlinked or truncated inode from the list 3153 - * of such inodes stored on disk, because it is finally being cleaned up. 3154 - */ 3155 - int ext4_orphan_del(handle_t *handle, struct inode *inode) 3156 - { 3157 - struct list_head *prev; 3158 - struct ext4_inode_info *ei = EXT4_I(inode); 3159 - struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3160 - __u32 ino_next; 3161 - struct ext4_iloc iloc; 3162 - int err = 0; 3163 - 3164 - if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS)) 3165 - return 0; 3166 - 3167 - WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 3168 - !inode_is_locked(inode)); 3169 - /* Do this quick check before taking global s_orphan_lock. */ 3170 - if (list_empty(&ei->i_orphan)) 3171 - return 0; 3172 - 3173 - if (handle) { 3174 - /* Grab inode buffer early before taking global s_orphan_lock */ 3175 - err = ext4_reserve_inode_write(handle, inode, &iloc); 3176 - } 3177 - 3178 - mutex_lock(&sbi->s_orphan_lock); 3179 - jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); 3180 - 3181 - prev = ei->i_orphan.prev; 3182 - list_del_init(&ei->i_orphan); 3183 - 3184 - /* If we're on an error path, we may not have a valid 3185 - * transaction handle with which to update the orphan list on 3186 - * disk, but we still need to remove the inode from the linked 3187 - * list in memory. */ 3188 - if (!handle || err) { 3189 - mutex_unlock(&sbi->s_orphan_lock); 3190 - goto out_err; 3191 - } 3192 - 3193 - ino_next = NEXT_ORPHAN(inode); 3194 - if (prev == &sbi->s_orphan) { 3195 - jbd_debug(4, "superblock will point to %u\n", ino_next); 3196 - BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 3197 - err = ext4_journal_get_write_access(handle, sbi->s_sbh); 3198 - if (err) { 3199 - mutex_unlock(&sbi->s_orphan_lock); 3200 - goto out_brelse; 3201 - } 3202 - lock_buffer(sbi->s_sbh); 3203 - sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); 3204 - ext4_superblock_csum_set(inode->i_sb); 3205 - unlock_buffer(sbi->s_sbh); 3206 - mutex_unlock(&sbi->s_orphan_lock); 3207 - err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 3208 - } else { 3209 - struct ext4_iloc iloc2; 3210 - struct inode *i_prev = 3211 - &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode; 3212 - 3213 - jbd_debug(4, "orphan inode %lu will point to %u\n", 3214 - i_prev->i_ino, ino_next); 3215 - err = ext4_reserve_inode_write(handle, i_prev, &iloc2); 3216 - if (err) { 3217 - mutex_unlock(&sbi->s_orphan_lock); 3218 - goto out_brelse; 3219 - } 3220 - NEXT_ORPHAN(i_prev) = ino_next; 3221 - err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2); 3222 - mutex_unlock(&sbi->s_orphan_lock); 3223 - } 3224 - if (err) 3225 - goto out_brelse; 3226 - NEXT_ORPHAN(inode) = 0; 3227 - err = ext4_mark_iloc_dirty(handle, inode, &iloc); 3228 - out_err: 3229 - ext4_std_error(inode->i_sb, err); 3230 - return err; 3231 - 3232 - out_brelse: 3233 - brelse(iloc.bh); 3234 - goto out_err; 3235 3047 } 3236 3048 3237 3049 static int ext4_rmdir(struct inode *dir, struct dentry *dentry) ··· 3503 3675 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino) 3504 3676 return -EFSCORRUPTED; 3505 3677 BUFFER_TRACE(ent->dir_bh, "get_write_access"); 3506 - return ext4_journal_get_write_access(handle, ent->dir_bh); 3678 + return ext4_journal_get_write_access(handle, ent->dir->i_sb, 3679 + ent->dir_bh, EXT4_JTR_NONE); 3507 3680 } 3508 3681 3509 3682 static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent, ··· 3539 3710 int retval, retval2; 3540 3711 3541 3712 BUFFER_TRACE(ent->bh, "get write access"); 3542 - retval = ext4_journal_get_write_access(handle, ent->bh); 3713 + retval = ext4_journal_get_write_access(handle, ent->dir->i_sb, ent->bh, 3714 + EXT4_JTR_NONE); 3543 3715 if (retval) 3544 3716 return retval; 3545 3717 ent->de->inode = cpu_to_le32(ino);
+652
fs/ext4/orphan.c
··· 1 + /* 2 + * Ext4 orphan inode handling 3 + */ 4 + #include <linux/fs.h> 5 + #include <linux/quotaops.h> 6 + #include <linux/buffer_head.h> 7 + 8 + #include "ext4.h" 9 + #include "ext4_jbd2.h" 10 + 11 + static int ext4_orphan_file_add(handle_t *handle, struct inode *inode) 12 + { 13 + int i, j, start; 14 + struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info; 15 + int ret = 0; 16 + bool found = false; 17 + __le32 *bdata; 18 + int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb); 19 + int looped = 0; 20 + 21 + /* 22 + * Find block with free orphan entry. Use CPU number for a naive hash 23 + * for a search start in the orphan file 24 + */ 25 + start = raw_smp_processor_id()*13 % oi->of_blocks; 26 + i = start; 27 + do { 28 + if (atomic_dec_if_positive(&oi->of_binfo[i].ob_free_entries) 29 + >= 0) { 30 + found = true; 31 + break; 32 + } 33 + if (++i >= oi->of_blocks) 34 + i = 0; 35 + } while (i != start); 36 + 37 + if (!found) { 38 + /* 39 + * For now we don't grow or shrink orphan file. We just use 40 + * whatever was allocated at mke2fs time. The additional 41 + * credits we would have to reserve for each orphan inode 42 + * operation just don't seem worth it. 43 + */ 44 + return -ENOSPC; 45 + } 46 + 47 + ret = ext4_journal_get_write_access(handle, inode->i_sb, 48 + oi->of_binfo[i].ob_bh, EXT4_JTR_ORPHAN_FILE); 49 + if (ret) { 50 + atomic_inc(&oi->of_binfo[i].ob_free_entries); 51 + return ret; 52 + } 53 + 54 + bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data); 55 + /* Find empty slot in a block */ 56 + j = 0; 57 + do { 58 + if (looped) { 59 + /* 60 + * Did we walk through the block several times without 61 + * finding free entry? It is theoretically possible 62 + * if entries get constantly allocated and freed or 63 + * if the block is corrupted. Avoid indefinite looping 64 + * and bail. We'll use orphan list instead. 65 + */ 66 + if (looped > 3) { 67 + atomic_inc(&oi->of_binfo[i].ob_free_entries); 68 + return -ENOSPC; 69 + } 70 + cond_resched(); 71 + } 72 + while (bdata[j]) { 73 + if (++j >= inodes_per_ob) { 74 + j = 0; 75 + looped++; 76 + } 77 + } 78 + } while (cmpxchg(&bdata[j], (__le32)0, cpu_to_le32(inode->i_ino)) != 79 + (__le32)0); 80 + 81 + EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j; 82 + ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE); 83 + 84 + return ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[i].ob_bh); 85 + } 86 + 87 + /* 88 + * ext4_orphan_add() links an unlinked or truncated inode into a list of 89 + * such inodes, starting at the superblock, in case we crash before the 90 + * file is closed/deleted, or in case the inode truncate spans multiple 91 + * transactions and the last transaction is not recovered after a crash. 92 + * 93 + * At filesystem recovery time, we walk this list deleting unlinked 94 + * inodes and truncating linked inodes in ext4_orphan_cleanup(). 95 + * 96 + * Orphan list manipulation functions must be called under i_mutex unless 97 + * we are just creating the inode or deleting it. 98 + */ 99 + int ext4_orphan_add(handle_t *handle, struct inode *inode) 100 + { 101 + struct super_block *sb = inode->i_sb; 102 + struct ext4_sb_info *sbi = EXT4_SB(sb); 103 + struct ext4_iloc iloc; 104 + int err = 0, rc; 105 + bool dirty = false; 106 + 107 + if (!sbi->s_journal || is_bad_inode(inode)) 108 + return 0; 109 + 110 + WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 111 + !inode_is_locked(inode)); 112 + /* 113 + * Inode orphaned in orphan file or in orphan list? 114 + */ 115 + if (ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE) || 116 + !list_empty(&EXT4_I(inode)->i_orphan)) 117 + return 0; 118 + 119 + /* 120 + * Orphan handling is only valid for files with data blocks 121 + * being truncated, or files being unlinked. Note that we either 122 + * hold i_mutex, or the inode can not be referenced from outside, 123 + * so i_nlink should not be bumped due to race 124 + */ 125 + ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 126 + S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); 127 + 128 + if (sbi->s_orphan_info.of_blocks) { 129 + err = ext4_orphan_file_add(handle, inode); 130 + /* 131 + * Fallback to normal orphan list of orphan file is 132 + * out of space 133 + */ 134 + if (err != -ENOSPC) 135 + return err; 136 + } 137 + 138 + BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 139 + err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 140 + EXT4_JTR_NONE); 141 + if (err) 142 + goto out; 143 + 144 + err = ext4_reserve_inode_write(handle, inode, &iloc); 145 + if (err) 146 + goto out; 147 + 148 + mutex_lock(&sbi->s_orphan_lock); 149 + /* 150 + * Due to previous errors inode may be already a part of on-disk 151 + * orphan list. If so skip on-disk list modification. 152 + */ 153 + if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) > 154 + (le32_to_cpu(sbi->s_es->s_inodes_count))) { 155 + /* Insert this inode at the head of the on-disk orphan list */ 156 + NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan); 157 + lock_buffer(sbi->s_sbh); 158 + sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); 159 + ext4_superblock_csum_set(sb); 160 + unlock_buffer(sbi->s_sbh); 161 + dirty = true; 162 + } 163 + list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan); 164 + mutex_unlock(&sbi->s_orphan_lock); 165 + 166 + if (dirty) { 167 + err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 168 + rc = ext4_mark_iloc_dirty(handle, inode, &iloc); 169 + if (!err) 170 + err = rc; 171 + if (err) { 172 + /* 173 + * We have to remove inode from in-memory list if 174 + * addition to on disk orphan list failed. Stray orphan 175 + * list entries can cause panics at unmount time. 176 + */ 177 + mutex_lock(&sbi->s_orphan_lock); 178 + list_del_init(&EXT4_I(inode)->i_orphan); 179 + mutex_unlock(&sbi->s_orphan_lock); 180 + } 181 + } else 182 + brelse(iloc.bh); 183 + 184 + jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); 185 + jbd_debug(4, "orphan inode %lu will point to %d\n", 186 + inode->i_ino, NEXT_ORPHAN(inode)); 187 + out: 188 + ext4_std_error(sb, err); 189 + return err; 190 + } 191 + 192 + static int ext4_orphan_file_del(handle_t *handle, struct inode *inode) 193 + { 194 + struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info; 195 + __le32 *bdata; 196 + int blk, off; 197 + int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb); 198 + int ret = 0; 199 + 200 + if (!handle) 201 + goto out; 202 + blk = EXT4_I(inode)->i_orphan_idx / inodes_per_ob; 203 + off = EXT4_I(inode)->i_orphan_idx % inodes_per_ob; 204 + if (WARN_ON_ONCE(blk >= oi->of_blocks)) 205 + goto out; 206 + 207 + ret = ext4_journal_get_write_access(handle, inode->i_sb, 208 + oi->of_binfo[blk].ob_bh, EXT4_JTR_ORPHAN_FILE); 209 + if (ret) 210 + goto out; 211 + 212 + bdata = (__le32 *)(oi->of_binfo[blk].ob_bh->b_data); 213 + bdata[off] = 0; 214 + atomic_inc(&oi->of_binfo[blk].ob_free_entries); 215 + ret = ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[blk].ob_bh); 216 + out: 217 + ext4_clear_inode_state(inode, EXT4_STATE_ORPHAN_FILE); 218 + INIT_LIST_HEAD(&EXT4_I(inode)->i_orphan); 219 + 220 + return ret; 221 + } 222 + 223 + /* 224 + * ext4_orphan_del() removes an unlinked or truncated inode from the list 225 + * of such inodes stored on disk, because it is finally being cleaned up. 226 + */ 227 + int ext4_orphan_del(handle_t *handle, struct inode *inode) 228 + { 229 + struct list_head *prev; 230 + struct ext4_inode_info *ei = EXT4_I(inode); 231 + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 232 + __u32 ino_next; 233 + struct ext4_iloc iloc; 234 + int err = 0; 235 + 236 + if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS)) 237 + return 0; 238 + 239 + WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 240 + !inode_is_locked(inode)); 241 + if (ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE)) 242 + return ext4_orphan_file_del(handle, inode); 243 + 244 + /* Do this quick check before taking global s_orphan_lock. */ 245 + if (list_empty(&ei->i_orphan)) 246 + return 0; 247 + 248 + if (handle) { 249 + /* Grab inode buffer early before taking global s_orphan_lock */ 250 + err = ext4_reserve_inode_write(handle, inode, &iloc); 251 + } 252 + 253 + mutex_lock(&sbi->s_orphan_lock); 254 + jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); 255 + 256 + prev = ei->i_orphan.prev; 257 + list_del_init(&ei->i_orphan); 258 + 259 + /* If we're on an error path, we may not have a valid 260 + * transaction handle with which to update the orphan list on 261 + * disk, but we still need to remove the inode from the linked 262 + * list in memory. */ 263 + if (!handle || err) { 264 + mutex_unlock(&sbi->s_orphan_lock); 265 + goto out_err; 266 + } 267 + 268 + ino_next = NEXT_ORPHAN(inode); 269 + if (prev == &sbi->s_orphan) { 270 + jbd_debug(4, "superblock will point to %u\n", ino_next); 271 + BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 272 + err = ext4_journal_get_write_access(handle, inode->i_sb, 273 + sbi->s_sbh, EXT4_JTR_NONE); 274 + if (err) { 275 + mutex_unlock(&sbi->s_orphan_lock); 276 + goto out_brelse; 277 + } 278 + lock_buffer(sbi->s_sbh); 279 + sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); 280 + ext4_superblock_csum_set(inode->i_sb); 281 + unlock_buffer(sbi->s_sbh); 282 + mutex_unlock(&sbi->s_orphan_lock); 283 + err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 284 + } else { 285 + struct ext4_iloc iloc2; 286 + struct inode *i_prev = 287 + &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode; 288 + 289 + jbd_debug(4, "orphan inode %lu will point to %u\n", 290 + i_prev->i_ino, ino_next); 291 + err = ext4_reserve_inode_write(handle, i_prev, &iloc2); 292 + if (err) { 293 + mutex_unlock(&sbi->s_orphan_lock); 294 + goto out_brelse; 295 + } 296 + NEXT_ORPHAN(i_prev) = ino_next; 297 + err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2); 298 + mutex_unlock(&sbi->s_orphan_lock); 299 + } 300 + if (err) 301 + goto out_brelse; 302 + NEXT_ORPHAN(inode) = 0; 303 + err = ext4_mark_iloc_dirty(handle, inode, &iloc); 304 + out_err: 305 + ext4_std_error(inode->i_sb, err); 306 + return err; 307 + 308 + out_brelse: 309 + brelse(iloc.bh); 310 + goto out_err; 311 + } 312 + 313 + #ifdef CONFIG_QUOTA 314 + static int ext4_quota_on_mount(struct super_block *sb, int type) 315 + { 316 + return dquot_quota_on_mount(sb, 317 + rcu_dereference_protected(EXT4_SB(sb)->s_qf_names[type], 318 + lockdep_is_held(&sb->s_umount)), 319 + EXT4_SB(sb)->s_jquota_fmt, type); 320 + } 321 + #endif 322 + 323 + static void ext4_process_orphan(struct inode *inode, 324 + int *nr_truncates, int *nr_orphans) 325 + { 326 + struct super_block *sb = inode->i_sb; 327 + int ret; 328 + 329 + dquot_initialize(inode); 330 + if (inode->i_nlink) { 331 + if (test_opt(sb, DEBUG)) 332 + ext4_msg(sb, KERN_DEBUG, 333 + "%s: truncating inode %lu to %lld bytes", 334 + __func__, inode->i_ino, inode->i_size); 335 + jbd_debug(2, "truncating inode %lu to %lld bytes\n", 336 + inode->i_ino, inode->i_size); 337 + inode_lock(inode); 338 + truncate_inode_pages(inode->i_mapping, inode->i_size); 339 + ret = ext4_truncate(inode); 340 + if (ret) { 341 + /* 342 + * We need to clean up the in-core orphan list 343 + * manually if ext4_truncate() failed to get a 344 + * transaction handle. 345 + */ 346 + ext4_orphan_del(NULL, inode); 347 + ext4_std_error(inode->i_sb, ret); 348 + } 349 + inode_unlock(inode); 350 + (*nr_truncates)++; 351 + } else { 352 + if (test_opt(sb, DEBUG)) 353 + ext4_msg(sb, KERN_DEBUG, 354 + "%s: deleting unreferenced inode %lu", 355 + __func__, inode->i_ino); 356 + jbd_debug(2, "deleting unreferenced inode %lu\n", 357 + inode->i_ino); 358 + (*nr_orphans)++; 359 + } 360 + iput(inode); /* The delete magic happens here! */ 361 + } 362 + 363 + /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at 364 + * the superblock) which were deleted from all directories, but held open by 365 + * a process at the time of a crash. We walk the list and try to delete these 366 + * inodes at recovery time (only with a read-write filesystem). 367 + * 368 + * In order to keep the orphan inode chain consistent during traversal (in 369 + * case of crash during recovery), we link each inode into the superblock 370 + * orphan list_head and handle it the same way as an inode deletion during 371 + * normal operation (which journals the operations for us). 372 + * 373 + * We only do an iget() and an iput() on each inode, which is very safe if we 374 + * accidentally point at an in-use or already deleted inode. The worst that 375 + * can happen in this case is that we get a "bit already cleared" message from 376 + * ext4_free_inode(). The only reason we would point at a wrong inode is if 377 + * e2fsck was run on this filesystem, and it must have already done the orphan 378 + * inode cleanup for us, so we can safely abort without any further action. 379 + */ 380 + void ext4_orphan_cleanup(struct super_block *sb, struct ext4_super_block *es) 381 + { 382 + unsigned int s_flags = sb->s_flags; 383 + int nr_orphans = 0, nr_truncates = 0; 384 + struct inode *inode; 385 + int i, j; 386 + #ifdef CONFIG_QUOTA 387 + int quota_update = 0; 388 + #endif 389 + __le32 *bdata; 390 + struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 391 + int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 392 + 393 + if (!es->s_last_orphan && !oi->of_blocks) { 394 + jbd_debug(4, "no orphan inodes to clean up\n"); 395 + return; 396 + } 397 + 398 + if (bdev_read_only(sb->s_bdev)) { 399 + ext4_msg(sb, KERN_ERR, "write access " 400 + "unavailable, skipping orphan cleanup"); 401 + return; 402 + } 403 + 404 + /* Check if feature set would not allow a r/w mount */ 405 + if (!ext4_feature_set_ok(sb, 0)) { 406 + ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to " 407 + "unknown ROCOMPAT features"); 408 + return; 409 + } 410 + 411 + if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 412 + /* don't clear list on RO mount w/ errors */ 413 + if (es->s_last_orphan && !(s_flags & SB_RDONLY)) { 414 + ext4_msg(sb, KERN_INFO, "Errors on filesystem, " 415 + "clearing orphan list.\n"); 416 + es->s_last_orphan = 0; 417 + } 418 + jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 419 + return; 420 + } 421 + 422 + if (s_flags & SB_RDONLY) { 423 + ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs"); 424 + sb->s_flags &= ~SB_RDONLY; 425 + } 426 + #ifdef CONFIG_QUOTA 427 + /* 428 + * Turn on quotas which were not enabled for read-only mounts if 429 + * filesystem has quota feature, so that they are updated correctly. 430 + */ 431 + if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) { 432 + int ret = ext4_enable_quotas(sb); 433 + 434 + if (!ret) 435 + quota_update = 1; 436 + else 437 + ext4_msg(sb, KERN_ERR, 438 + "Cannot turn on quotas: error %d", ret); 439 + } 440 + 441 + /* Turn on journaled quotas used for old sytle */ 442 + for (i = 0; i < EXT4_MAXQUOTAS; i++) { 443 + if (EXT4_SB(sb)->s_qf_names[i]) { 444 + int ret = ext4_quota_on_mount(sb, i); 445 + 446 + if (!ret) 447 + quota_update = 1; 448 + else 449 + ext4_msg(sb, KERN_ERR, 450 + "Cannot turn on journaled " 451 + "quota: type %d: error %d", i, ret); 452 + } 453 + } 454 + #endif 455 + 456 + while (es->s_last_orphan) { 457 + /* 458 + * We may have encountered an error during cleanup; if 459 + * so, skip the rest. 460 + */ 461 + if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 462 + jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 463 + es->s_last_orphan = 0; 464 + break; 465 + } 466 + 467 + inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); 468 + if (IS_ERR(inode)) { 469 + es->s_last_orphan = 0; 470 + break; 471 + } 472 + 473 + list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); 474 + ext4_process_orphan(inode, &nr_truncates, &nr_orphans); 475 + } 476 + 477 + for (i = 0; i < oi->of_blocks; i++) { 478 + bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data); 479 + for (j = 0; j < inodes_per_ob; j++) { 480 + if (!bdata[j]) 481 + continue; 482 + inode = ext4_orphan_get(sb, le32_to_cpu(bdata[j])); 483 + if (IS_ERR(inode)) 484 + continue; 485 + ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE); 486 + EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j; 487 + ext4_process_orphan(inode, &nr_truncates, &nr_orphans); 488 + } 489 + } 490 + 491 + #define PLURAL(x) (x), ((x) == 1) ? "" : "s" 492 + 493 + if (nr_orphans) 494 + ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", 495 + PLURAL(nr_orphans)); 496 + if (nr_truncates) 497 + ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up", 498 + PLURAL(nr_truncates)); 499 + #ifdef CONFIG_QUOTA 500 + /* Turn off quotas if they were enabled for orphan cleanup */ 501 + if (quota_update) { 502 + for (i = 0; i < EXT4_MAXQUOTAS; i++) { 503 + if (sb_dqopt(sb)->files[i]) 504 + dquot_quota_off(sb, i); 505 + } 506 + } 507 + #endif 508 + sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 509 + } 510 + 511 + void ext4_release_orphan_info(struct super_block *sb) 512 + { 513 + int i; 514 + struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 515 + 516 + if (!oi->of_blocks) 517 + return; 518 + for (i = 0; i < oi->of_blocks; i++) 519 + brelse(oi->of_binfo[i].ob_bh); 520 + kfree(oi->of_binfo); 521 + } 522 + 523 + static struct ext4_orphan_block_tail *ext4_orphan_block_tail( 524 + struct super_block *sb, 525 + struct buffer_head *bh) 526 + { 527 + return (struct ext4_orphan_block_tail *)(bh->b_data + sb->s_blocksize - 528 + sizeof(struct ext4_orphan_block_tail)); 529 + } 530 + 531 + static int ext4_orphan_file_block_csum_verify(struct super_block *sb, 532 + struct buffer_head *bh) 533 + { 534 + __u32 calculated; 535 + int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 536 + struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 537 + struct ext4_orphan_block_tail *ot; 538 + __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr); 539 + 540 + if (!ext4_has_metadata_csum(sb)) 541 + return 1; 542 + 543 + ot = ext4_orphan_block_tail(sb, bh); 544 + calculated = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed, 545 + (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr)); 546 + calculated = ext4_chksum(EXT4_SB(sb), calculated, (__u8 *)bh->b_data, 547 + inodes_per_ob * sizeof(__u32)); 548 + return le32_to_cpu(ot->ob_checksum) == calculated; 549 + } 550 + 551 + /* This gets called only when checksumming is enabled */ 552 + void ext4_orphan_file_block_trigger(struct jbd2_buffer_trigger_type *triggers, 553 + struct buffer_head *bh, 554 + void *data, size_t size) 555 + { 556 + struct super_block *sb = EXT4_TRIGGER(triggers)->sb; 557 + __u32 csum; 558 + int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 559 + struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 560 + struct ext4_orphan_block_tail *ot; 561 + __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr); 562 + 563 + csum = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed, 564 + (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr)); 565 + csum = ext4_chksum(EXT4_SB(sb), csum, (__u8 *)data, 566 + inodes_per_ob * sizeof(__u32)); 567 + ot = ext4_orphan_block_tail(sb, bh); 568 + ot->ob_checksum = cpu_to_le32(csum); 569 + } 570 + 571 + int ext4_init_orphan_info(struct super_block *sb) 572 + { 573 + struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 574 + struct inode *inode; 575 + int i, j; 576 + int ret; 577 + int free; 578 + __le32 *bdata; 579 + int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 580 + struct ext4_orphan_block_tail *ot; 581 + ino_t orphan_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_orphan_file_inum); 582 + 583 + if (!ext4_has_feature_orphan_file(sb)) 584 + return 0; 585 + 586 + inode = ext4_iget(sb, orphan_ino, EXT4_IGET_SPECIAL); 587 + if (IS_ERR(inode)) { 588 + ext4_msg(sb, KERN_ERR, "get orphan inode failed"); 589 + return PTR_ERR(inode); 590 + } 591 + oi->of_blocks = inode->i_size >> sb->s_blocksize_bits; 592 + oi->of_csum_seed = EXT4_I(inode)->i_csum_seed; 593 + oi->of_binfo = kmalloc(oi->of_blocks*sizeof(struct ext4_orphan_block), 594 + GFP_KERNEL); 595 + if (!oi->of_binfo) { 596 + ret = -ENOMEM; 597 + goto out_put; 598 + } 599 + for (i = 0; i < oi->of_blocks; i++) { 600 + oi->of_binfo[i].ob_bh = ext4_bread(NULL, inode, i, 0); 601 + if (IS_ERR(oi->of_binfo[i].ob_bh)) { 602 + ret = PTR_ERR(oi->of_binfo[i].ob_bh); 603 + goto out_free; 604 + } 605 + if (!oi->of_binfo[i].ob_bh) { 606 + ret = -EIO; 607 + goto out_free; 608 + } 609 + ot = ext4_orphan_block_tail(sb, oi->of_binfo[i].ob_bh); 610 + if (le32_to_cpu(ot->ob_magic) != EXT4_ORPHAN_BLOCK_MAGIC) { 611 + ext4_error(sb, "orphan file block %d: bad magic", i); 612 + ret = -EIO; 613 + goto out_free; 614 + } 615 + if (!ext4_orphan_file_block_csum_verify(sb, 616 + oi->of_binfo[i].ob_bh)) { 617 + ext4_error(sb, "orphan file block %d: bad checksum", i); 618 + ret = -EIO; 619 + goto out_free; 620 + } 621 + bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data); 622 + free = 0; 623 + for (j = 0; j < inodes_per_ob; j++) 624 + if (bdata[j] == 0) 625 + free++; 626 + atomic_set(&oi->of_binfo[i].ob_free_entries, free); 627 + } 628 + iput(inode); 629 + return 0; 630 + out_free: 631 + for (i--; i >= 0; i--) 632 + brelse(oi->of_binfo[i].ob_bh); 633 + kfree(oi->of_binfo); 634 + out_put: 635 + iput(inode); 636 + return ret; 637 + } 638 + 639 + int ext4_orphan_file_empty(struct super_block *sb) 640 + { 641 + struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 642 + int i; 643 + int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 644 + 645 + if (!ext4_has_feature_orphan_file(sb)) 646 + return 1; 647 + for (i = 0; i < oi->of_blocks; i++) 648 + if (atomic_read(&oi->of_binfo[i].ob_free_entries) != 649 + inodes_per_ob) 650 + return 0; 651 + return 1; 652 + }
+23 -15
fs/ext4/resize.c
··· 409 409 if (unlikely(!bh)) 410 410 return ERR_PTR(-ENOMEM); 411 411 BUFFER_TRACE(bh, "get_write_access"); 412 - if ((err = ext4_journal_get_write_access(handle, bh))) { 412 + err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE); 413 + if (err) { 413 414 brelse(bh); 414 415 bh = ERR_PTR(err); 415 416 } else { ··· 475 474 return -ENOMEM; 476 475 477 476 BUFFER_TRACE(bh, "get_write_access"); 478 - err = ext4_journal_get_write_access(handle, bh); 477 + err = ext4_journal_get_write_access(handle, sb, bh, 478 + EXT4_JTR_NONE); 479 479 if (err) { 480 480 brelse(bh); 481 481 return err; ··· 571 569 } 572 570 573 571 BUFFER_TRACE(gdb, "get_write_access"); 574 - err = ext4_journal_get_write_access(handle, gdb); 572 + err = ext4_journal_get_write_access(handle, sb, gdb, 573 + EXT4_JTR_NONE); 575 574 if (err) { 576 575 brelse(gdb); 577 576 goto out; ··· 840 837 } 841 838 842 839 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 843 - err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 840 + err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, 841 + EXT4_JTR_NONE); 844 842 if (unlikely(err)) 845 843 goto errout; 846 844 847 845 BUFFER_TRACE(gdb_bh, "get_write_access"); 848 - err = ext4_journal_get_write_access(handle, gdb_bh); 846 + err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE); 849 847 if (unlikely(err)) 850 848 goto errout; 851 849 852 850 BUFFER_TRACE(dind, "get_write_access"); 853 - err = ext4_journal_get_write_access(handle, dind); 851 + err = ext4_journal_get_write_access(handle, sb, dind, EXT4_JTR_NONE); 854 852 if (unlikely(err)) { 855 853 ext4_std_error(sb, err); 856 854 goto errout; ··· 960 956 n_group_desc[gdb_num] = gdb_bh; 961 957 962 958 BUFFER_TRACE(gdb_bh, "get_write_access"); 963 - err = ext4_journal_get_write_access(handle, gdb_bh); 959 + err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE); 964 960 if (err) { 965 961 kvfree(n_group_desc); 966 962 brelse(gdb_bh); ··· 1046 1042 1047 1043 for (i = 0; i < reserved_gdb; i++) { 1048 1044 BUFFER_TRACE(primary[i], "get_write_access"); 1049 - if ((err = ext4_journal_get_write_access(handle, primary[i]))) 1045 + if ((err = ext4_journal_get_write_access(handle, sb, primary[i], 1046 + EXT4_JTR_NONE))) 1050 1047 goto exit_bh; 1051 1048 } 1052 1049 ··· 1154 1149 backup_block, backup_block - 1155 1150 ext4_group_first_block_no(sb, group)); 1156 1151 BUFFER_TRACE(bh, "get_write_access"); 1157 - if ((err = ext4_journal_get_write_access(handle, bh))) { 1158 - brelse(bh); 1152 + if ((err = ext4_journal_get_write_access(handle, sb, bh, 1153 + EXT4_JTR_NONE))) 1159 1154 break; 1160 - } 1161 1155 lock_buffer(bh); 1162 1156 memcpy(bh->b_data, data, size); 1163 1157 if (rest) ··· 1236 1232 gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, 1237 1233 gdb_num); 1238 1234 BUFFER_TRACE(gdb_bh, "get_write_access"); 1239 - err = ext4_journal_get_write_access(handle, gdb_bh); 1235 + err = ext4_journal_get_write_access(handle, sb, gdb_bh, 1236 + EXT4_JTR_NONE); 1240 1237 1241 1238 if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group)) 1242 1239 err = reserve_backup_gdb(handle, resize_inode, group); ··· 1514 1509 } 1515 1510 1516 1511 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 1517 - err = ext4_journal_get_write_access(handle, sbi->s_sbh); 1512 + err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 1513 + EXT4_JTR_NONE); 1518 1514 if (err) 1519 1515 goto exit_journal; 1520 1516 ··· 1728 1722 } 1729 1723 1730 1724 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 1731 - err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 1725 + err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, 1726 + EXT4_JTR_NONE); 1732 1727 if (err) { 1733 1728 ext4_warning(sb, "error %d on journal write access", err); 1734 1729 goto errout; ··· 1891 1884 return PTR_ERR(handle); 1892 1885 1893 1886 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 1894 - err = ext4_journal_get_write_access(handle, sbi->s_sbh); 1887 + err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 1888 + EXT4_JTR_NONE); 1895 1889 if (err) 1896 1890 goto errout; 1897 1891
+54 -184
fs/ext4/super.c
··· 80 80 const char *dev_name, void *data); 81 81 static inline int ext2_feature_set_ok(struct super_block *sb); 82 82 static inline int ext3_feature_set_ok(struct super_block *sb); 83 - static int ext4_feature_set_ok(struct super_block *sb, int readonly); 84 83 static void ext4_destroy_lazyinit_thread(void); 85 84 static void ext4_unregister_li_request(struct super_block *sb); 86 85 static void ext4_clear_request_list(void); ··· 1172 1173 1173 1174 flush_work(&sbi->s_error_work); 1174 1175 destroy_workqueue(sbi->rsv_conversion_wq); 1176 + ext4_release_orphan_info(sb); 1175 1177 1176 1178 /* 1177 1179 * Unregister sysfs before destroying jbd2 journal. ··· 1198 1198 1199 1199 if (!sb_rdonly(sb) && !aborted) { 1200 1200 ext4_clear_feature_journal_needs_recovery(sb); 1201 + ext4_clear_feature_orphan_present(sb); 1201 1202 es->s_state = cpu_to_le16(sbi->s_mount_state); 1202 1203 } 1203 1204 if (!sb_rdonly(sb)) ··· 1583 1582 static int ext4_write_info(struct super_block *sb, int type); 1584 1583 static int ext4_quota_on(struct super_block *sb, int type, int format_id, 1585 1584 const struct path *path); 1586 - static int ext4_quota_on_mount(struct super_block *sb, int type); 1587 1585 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 1588 1586 size_t len, loff_t off); 1589 1587 static ssize_t ext4_quota_write(struct super_block *sb, int type, 1590 1588 const char *data, size_t len, loff_t off); 1591 1589 static int ext4_quota_enable(struct super_block *sb, int type, int format_id, 1592 1590 unsigned int flags); 1593 - static int ext4_enable_quotas(struct super_block *sb); 1594 1591 1595 1592 static struct dquot **ext4_get_dquots(struct inode *inode) 1596 1593 { ··· 2683 2684 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT); 2684 2685 le16_add_cpu(&es->s_mnt_count, 1); 2685 2686 ext4_update_tstamp(es, s_mtime); 2686 - if (sbi->s_journal) 2687 + if (sbi->s_journal) { 2687 2688 ext4_set_feature_journal_needs_recovery(sb); 2689 + if (ext4_has_feature_orphan_file(sb)) 2690 + ext4_set_feature_orphan_present(sb); 2691 + } 2688 2692 2689 2693 err = ext4_commit_super(sb); 2690 2694 done: ··· 2969 2967 return 1; 2970 2968 } 2971 2969 2972 - /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at 2973 - * the superblock) which were deleted from all directories, but held open by 2974 - * a process at the time of a crash. We walk the list and try to delete these 2975 - * inodes at recovery time (only with a read-write filesystem). 2976 - * 2977 - * In order to keep the orphan inode chain consistent during traversal (in 2978 - * case of crash during recovery), we link each inode into the superblock 2979 - * orphan list_head and handle it the same way as an inode deletion during 2980 - * normal operation (which journals the operations for us). 2981 - * 2982 - * We only do an iget() and an iput() on each inode, which is very safe if we 2983 - * accidentally point at an in-use or already deleted inode. The worst that 2984 - * can happen in this case is that we get a "bit already cleared" message from 2985 - * ext4_free_inode(). The only reason we would point at a wrong inode is if 2986 - * e2fsck was run on this filesystem, and it must have already done the orphan 2987 - * inode cleanup for us, so we can safely abort without any further action. 2988 - */ 2989 - static void ext4_orphan_cleanup(struct super_block *sb, 2990 - struct ext4_super_block *es) 2991 - { 2992 - unsigned int s_flags = sb->s_flags; 2993 - int ret, nr_orphans = 0, nr_truncates = 0; 2994 - #ifdef CONFIG_QUOTA 2995 - int quota_update = 0; 2996 - int i; 2997 - #endif 2998 - if (!es->s_last_orphan) { 2999 - jbd_debug(4, "no orphan inodes to clean up\n"); 3000 - return; 3001 - } 3002 - 3003 - if (bdev_read_only(sb->s_bdev)) { 3004 - ext4_msg(sb, KERN_ERR, "write access " 3005 - "unavailable, skipping orphan cleanup"); 3006 - return; 3007 - } 3008 - 3009 - /* Check if feature set would not allow a r/w mount */ 3010 - if (!ext4_feature_set_ok(sb, 0)) { 3011 - ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to " 3012 - "unknown ROCOMPAT features"); 3013 - return; 3014 - } 3015 - 3016 - if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 3017 - /* don't clear list on RO mount w/ errors */ 3018 - if (es->s_last_orphan && !(s_flags & SB_RDONLY)) { 3019 - ext4_msg(sb, KERN_INFO, "Errors on filesystem, " 3020 - "clearing orphan list.\n"); 3021 - es->s_last_orphan = 0; 3022 - } 3023 - jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 3024 - return; 3025 - } 3026 - 3027 - if (s_flags & SB_RDONLY) { 3028 - ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs"); 3029 - sb->s_flags &= ~SB_RDONLY; 3030 - } 3031 - #ifdef CONFIG_QUOTA 3032 - /* 3033 - * Turn on quotas which were not enabled for read-only mounts if 3034 - * filesystem has quota feature, so that they are updated correctly. 3035 - */ 3036 - if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) { 3037 - int ret = ext4_enable_quotas(sb); 3038 - 3039 - if (!ret) 3040 - quota_update = 1; 3041 - else 3042 - ext4_msg(sb, KERN_ERR, 3043 - "Cannot turn on quotas: error %d", ret); 3044 - } 3045 - 3046 - /* Turn on journaled quotas used for old sytle */ 3047 - for (i = 0; i < EXT4_MAXQUOTAS; i++) { 3048 - if (EXT4_SB(sb)->s_qf_names[i]) { 3049 - int ret = ext4_quota_on_mount(sb, i); 3050 - 3051 - if (!ret) 3052 - quota_update = 1; 3053 - else 3054 - ext4_msg(sb, KERN_ERR, 3055 - "Cannot turn on journaled " 3056 - "quota: type %d: error %d", i, ret); 3057 - } 3058 - } 3059 - #endif 3060 - 3061 - while (es->s_last_orphan) { 3062 - struct inode *inode; 3063 - 3064 - /* 3065 - * We may have encountered an error during cleanup; if 3066 - * so, skip the rest. 3067 - */ 3068 - if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 3069 - jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 3070 - es->s_last_orphan = 0; 3071 - break; 3072 - } 3073 - 3074 - inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); 3075 - if (IS_ERR(inode)) { 3076 - es->s_last_orphan = 0; 3077 - break; 3078 - } 3079 - 3080 - list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); 3081 - dquot_initialize(inode); 3082 - if (inode->i_nlink) { 3083 - if (test_opt(sb, DEBUG)) 3084 - ext4_msg(sb, KERN_DEBUG, 3085 - "%s: truncating inode %lu to %lld bytes", 3086 - __func__, inode->i_ino, inode->i_size); 3087 - jbd_debug(2, "truncating inode %lu to %lld bytes\n", 3088 - inode->i_ino, inode->i_size); 3089 - inode_lock(inode); 3090 - truncate_inode_pages(inode->i_mapping, inode->i_size); 3091 - ret = ext4_truncate(inode); 3092 - if (ret) { 3093 - /* 3094 - * We need to clean up the in-core orphan list 3095 - * manually if ext4_truncate() failed to get a 3096 - * transaction handle. 3097 - */ 3098 - ext4_orphan_del(NULL, inode); 3099 - ext4_std_error(inode->i_sb, ret); 3100 - } 3101 - inode_unlock(inode); 3102 - nr_truncates++; 3103 - } else { 3104 - if (test_opt(sb, DEBUG)) 3105 - ext4_msg(sb, KERN_DEBUG, 3106 - "%s: deleting unreferenced inode %lu", 3107 - __func__, inode->i_ino); 3108 - jbd_debug(2, "deleting unreferenced inode %lu\n", 3109 - inode->i_ino); 3110 - nr_orphans++; 3111 - } 3112 - iput(inode); /* The delete magic happens here! */ 3113 - } 3114 - 3115 - #define PLURAL(x) (x), ((x) == 1) ? "" : "s" 3116 - 3117 - if (nr_orphans) 3118 - ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", 3119 - PLURAL(nr_orphans)); 3120 - if (nr_truncates) 3121 - ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up", 3122 - PLURAL(nr_truncates)); 3123 - #ifdef CONFIG_QUOTA 3124 - /* Turn off quotas if they were enabled for orphan cleanup */ 3125 - if (quota_update) { 3126 - for (i = 0; i < EXT4_MAXQUOTAS; i++) { 3127 - if (sb_dqopt(sb)->files[i]) 3128 - dquot_quota_off(sb, i); 3129 - } 3130 - } 3131 - #endif 3132 - sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 3133 - } 3134 - 3135 2970 /* 3136 2971 * Maximal extent format file size. 3137 2972 * Resulting logical blkno at s_maxbytes must fit in our on-disk ··· 3148 3309 * Returns 1 if this filesystem can be mounted as requested, 3149 3310 * 0 if it cannot be. 3150 3311 */ 3151 - static int ext4_feature_set_ok(struct super_block *sb, int readonly) 3312 + int ext4_feature_set_ok(struct super_block *sb, int readonly) 3152 3313 { 3153 3314 if (ext4_has_unknown_ext4_incompat_features(sb)) { 3154 3315 ext4_msg(sb, KERN_ERR, ··· 3850 4011 #endif 3851 4012 } 3852 4013 4014 + static void ext4_setup_csum_trigger(struct super_block *sb, 4015 + enum ext4_journal_trigger_type type, 4016 + void (*trigger)( 4017 + struct jbd2_buffer_trigger_type *type, 4018 + struct buffer_head *bh, 4019 + void *mapped_data, 4020 + size_t size)) 4021 + { 4022 + struct ext4_sb_info *sbi = EXT4_SB(sb); 4023 + 4024 + sbi->s_journal_triggers[type].sb = sb; 4025 + sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger; 4026 + } 4027 + 3853 4028 static int ext4_fill_super(struct super_block *sb, void *data, int silent) 3854 4029 { 3855 4030 struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev); ··· 3962 4109 silent = 1; 3963 4110 goto cantfind_ext4; 3964 4111 } 4112 + ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE, 4113 + ext4_orphan_file_block_trigger); 3965 4114 3966 4115 /* Load the checksum driver */ 3967 4116 sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); ··· 4628 4773 sb->s_root = NULL; 4629 4774 4630 4775 needs_recovery = (es->s_last_orphan != 0 || 4776 + ext4_has_feature_orphan_present(sb) || 4631 4777 ext4_has_feature_journal_needs_recovery(sb)); 4632 4778 4633 4779 if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) ··· 4885 5029 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei, 4886 5030 GFP_KERNEL); 4887 5031 } 5032 + /* 5033 + * Update the checksum after updating free space/inode 5034 + * counters. Otherwise the superblock can have an incorrect 5035 + * checksum in the buffer cache until it is written out and 5036 + * e2fsprogs programs trying to open a file system immediately 5037 + * after it is mounted can fail. 5038 + */ 5039 + ext4_superblock_csum_set(sb); 4888 5040 if (!err) 4889 5041 err = percpu_counter_init(&sbi->s_dirs_counter, 4890 5042 ext4_count_dirs(sb), GFP_KERNEL); ··· 4927 5063 if (err) 4928 5064 goto failed_mount7; 4929 5065 5066 + err = ext4_init_orphan_info(sb); 5067 + if (err) 5068 + goto failed_mount8; 4930 5069 #ifdef CONFIG_QUOTA 4931 5070 /* Enable quota usage during mount. */ 4932 5071 if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) { 4933 5072 err = ext4_enable_quotas(sb); 4934 5073 if (err) 4935 - goto failed_mount8; 5074 + goto failed_mount9; 4936 5075 } 4937 5076 #endif /* CONFIG_QUOTA */ 4938 5077 ··· 4954 5087 ext4_msg(sb, KERN_INFO, "recovery complete"); 4955 5088 err = ext4_mark_recovery_complete(sb, es); 4956 5089 if (err) 4957 - goto failed_mount8; 5090 + goto failed_mount9; 4958 5091 } 4959 5092 if (EXT4_SB(sb)->s_journal) { 4960 5093 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) ··· 5000 5133 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem"); 5001 5134 goto failed_mount; 5002 5135 5136 + failed_mount9: 5137 + ext4_release_orphan_info(sb); 5003 5138 failed_mount8: 5004 5139 ext4_unregister_sysfs(sb); 5005 5140 kobject_put(&sbi->s_kobj); ··· 5512 5643 if (err < 0) 5513 5644 goto out; 5514 5645 5515 - if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) { 5646 + if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) || 5647 + ext4_has_feature_orphan_present(sb))) { 5648 + if (!ext4_orphan_file_empty(sb)) { 5649 + ext4_error(sb, "Orphan file not empty on read-only fs."); 5650 + err = -EFSCORRUPTED; 5651 + goto out; 5652 + } 5516 5653 ext4_clear_feature_journal_needs_recovery(sb); 5654 + ext4_clear_feature_orphan_present(sb); 5517 5655 ext4_commit_super(sb); 5518 5656 } 5519 5657 out: ··· 5663 5787 5664 5788 /* Journal blocked and flushed, clear needs_recovery flag. */ 5665 5789 ext4_clear_feature_journal_needs_recovery(sb); 5790 + if (ext4_orphan_file_empty(sb)) 5791 + ext4_clear_feature_orphan_present(sb); 5666 5792 } 5667 5793 5668 5794 error = ext4_commit_super(sb); ··· 5687 5809 if (EXT4_SB(sb)->s_journal) { 5688 5810 /* Reset the needs_recovery flag before the fs is unlocked. */ 5689 5811 ext4_set_feature_journal_needs_recovery(sb); 5812 + if (ext4_has_feature_orphan_file(sb)) 5813 + ext4_set_feature_orphan_present(sb); 5690 5814 } 5691 5815 5692 5816 ext4_commit_super(sb); ··· 5892 6012 * around from a previously readonly bdev mount, 5893 6013 * require a full umount/remount for now. 5894 6014 */ 5895 - if (es->s_last_orphan) { 6015 + if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) { 5896 6016 ext4_msg(sb, KERN_WARNING, "Couldn't " 5897 6017 "remount RDWR because of unprocessed " 5898 6018 "orphan inode list. Please " ··· 6189 6309 return ret; 6190 6310 } 6191 6311 6192 - /* 6193 - * Turn on quotas during mount time - we need to find 6194 - * the quota file and such... 6195 - */ 6196 - static int ext4_quota_on_mount(struct super_block *sb, int type) 6197 - { 6198 - return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type), 6199 - EXT4_SB(sb)->s_jquota_fmt, type); 6200 - } 6201 - 6202 6312 static void lockdep_set_quota_inode(struct inode *inode, int subclass) 6203 6313 { 6204 6314 struct ext4_inode_info *ei = EXT4_I(inode); ··· 6318 6448 } 6319 6449 6320 6450 /* Enable usage tracking for all quota types. */ 6321 - static int ext4_enable_quotas(struct super_block *sb) 6451 + int ext4_enable_quotas(struct super_block *sb) 6322 6452 { 6323 6453 int type, err = 0; 6324 6454 unsigned long qf_inums[EXT4_MAXQUOTAS] = { ··· 6476 6606 if (!bh) 6477 6607 goto out; 6478 6608 BUFFER_TRACE(bh, "get write access"); 6479 - err = ext4_journal_get_write_access(handle, bh); 6609 + err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE); 6480 6610 if (err) { 6481 6611 brelse(bh); 6482 6612 return err;
+17 -9
fs/ext4/xattr.c
··· 791 791 return; 792 792 793 793 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 794 - if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) { 794 + if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, 795 + EXT4_JTR_NONE) == 0) { 795 796 lock_buffer(EXT4_SB(sb)->s_sbh); 796 797 ext4_set_feature_xattr(sb); 797 798 ext4_superblock_csum_set(sb); ··· 1170 1169 continue; 1171 1170 } 1172 1171 if (err > 0) { 1173 - err = ext4_journal_get_write_access(handle, bh); 1172 + err = ext4_journal_get_write_access(handle, 1173 + parent->i_sb, bh, EXT4_JTR_NONE); 1174 1174 if (err) { 1175 1175 ext4_warning_inode(ea_inode, 1176 1176 "Re-get write access err=%d", ··· 1232 1230 int error = 0; 1233 1231 1234 1232 BUFFER_TRACE(bh, "get_write_access"); 1235 - error = ext4_journal_get_write_access(handle, bh); 1233 + error = ext4_journal_get_write_access(handle, inode->i_sb, bh, 1234 + EXT4_JTR_NONE); 1236 1235 if (error) 1237 1236 goto out; 1238 1237 ··· 1374 1371 "ext4_getblk() return bh = NULL"); 1375 1372 return -EFSCORRUPTED; 1376 1373 } 1377 - ret = ext4_journal_get_write_access(handle, bh); 1374 + ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh, 1375 + EXT4_JTR_NONE); 1378 1376 if (ret) 1379 1377 goto out; 1380 1378 ··· 1859 1855 1860 1856 if (s->base) { 1861 1857 BUFFER_TRACE(bs->bh, "get_write_access"); 1862 - error = ext4_journal_get_write_access(handle, bs->bh); 1858 + error = ext4_journal_get_write_access(handle, sb, bs->bh, 1859 + EXT4_JTR_NONE); 1863 1860 if (error) 1864 1861 goto cleanup; 1865 1862 lock_buffer(bs->bh); ··· 1992 1987 if (error) 1993 1988 goto cleanup; 1994 1989 BUFFER_TRACE(new_bh, "get_write_access"); 1995 - error = ext4_journal_get_write_access(handle, 1996 - new_bh); 1990 + error = ext4_journal_get_write_access( 1991 + handle, sb, new_bh, 1992 + EXT4_JTR_NONE); 1997 1993 if (error) 1998 1994 goto cleanup_dquot; 1999 1995 lock_buffer(new_bh); ··· 2098 2092 } 2099 2093 2100 2094 lock_buffer(new_bh); 2101 - error = ext4_journal_get_create_access(handle, new_bh); 2095 + error = ext4_journal_get_create_access(handle, sb, 2096 + new_bh, EXT4_JTR_NONE); 2102 2097 if (error) { 2103 2098 unlock_buffer(new_bh); 2104 2099 error = -EIO; ··· 2855 2848 goto cleanup; 2856 2849 } 2857 2850 2858 - error = ext4_journal_get_write_access(handle, iloc.bh); 2851 + error = ext4_journal_get_write_access(handle, inode->i_sb, 2852 + iloc.bh, EXT4_JTR_NONE); 2859 2853 if (error) { 2860 2854 EXT4_ERROR_INODE(inode, "write access (error %d)", 2861 2855 error);
+14 -15
fs/jbd2/recovery.c
··· 179 179 if (!jbd2_journal_has_csum_v2or3(j)) 180 180 return 1; 181 181 182 - tail = (struct jbd2_journal_block_tail *)(buf + j->j_blocksize - 183 - sizeof(struct jbd2_journal_block_tail)); 182 + tail = (struct jbd2_journal_block_tail *)((char *)buf + 183 + j->j_blocksize - sizeof(struct jbd2_journal_block_tail)); 184 184 provided = tail->t_checksum; 185 185 tail->t_checksum = 0; 186 186 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize); ··· 196 196 static int count_tags(journal_t *journal, struct buffer_head *bh) 197 197 { 198 198 char * tagp; 199 - journal_block_tag_t * tag; 199 + journal_block_tag_t tag; 200 200 int nr = 0, size = journal->j_blocksize; 201 201 int tag_bytes = journal_tag_bytes(journal); 202 202 ··· 206 206 tagp = &bh->b_data[sizeof(journal_header_t)]; 207 207 208 208 while ((tagp - bh->b_data + tag_bytes) <= size) { 209 - tag = (journal_block_tag_t *) tagp; 209 + memcpy(&tag, tagp, sizeof(tag)); 210 210 211 211 nr++; 212 212 tagp += tag_bytes; 213 - if (!(tag->t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID))) 213 + if (!(tag.t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID))) 214 214 tagp += 16; 215 215 216 - if (tag->t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG)) 216 + if (tag.t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG)) 217 217 break; 218 218 } 219 219 ··· 433 433 } 434 434 435 435 static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag, 436 + journal_block_tag3_t *tag3, 436 437 void *buf, __u32 sequence) 437 438 { 438 - journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag; 439 439 __u32 csum32; 440 440 __be32 seq; 441 441 ··· 496 496 while (1) { 497 497 int flags; 498 498 char * tagp; 499 - journal_block_tag_t * tag; 499 + journal_block_tag_t tag; 500 500 struct buffer_head * obh; 501 501 struct buffer_head * nbh; 502 502 ··· 613 613 <= journal->j_blocksize - descr_csum_size) { 614 614 unsigned long io_block; 615 615 616 - tag = (journal_block_tag_t *) tagp; 617 - flags = be16_to_cpu(tag->t_flags); 616 + memcpy(&tag, tagp, sizeof(tag)); 617 + flags = be16_to_cpu(tag.t_flags); 618 618 619 619 io_block = next_log_block++; 620 620 wrap(journal, next_log_block); ··· 632 632 633 633 J_ASSERT(obh != NULL); 634 634 blocknr = read_tag_block(journal, 635 - tag); 635 + &tag); 636 636 637 637 /* If the block has been 638 638 * revoked, then we're all done ··· 647 647 648 648 /* Look for block corruption */ 649 649 if (!jbd2_block_tag_csum_verify( 650 - journal, tag, obh->b_data, 651 - be32_to_cpu(tmp->h_sequence))) { 650 + journal, &tag, (journal_block_tag3_t *)tagp, 651 + obh->b_data, be32_to_cpu(tmp->h_sequence))) { 652 652 brelse(obh); 653 653 success = -EFSBADCRC; 654 654 printk(KERN_ERR "JBD2: Invalid " ··· 760 760 */ 761 761 jbd_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n", 762 762 next_commit_ID); 763 - err = 0; 764 763 brelse(bh); 765 764 goto done; 766 765 } ··· 896 897 { 897 898 jbd2_journal_revoke_header_t *header; 898 899 int offset, max; 899 - int csum_size = 0; 900 + unsigned csum_size = 0; 900 901 __u32 rcount; 901 902 int record_len = 4; 902 903
+19 -2
fs/jbd2/transaction.c
··· 223 223 * with j_state_lock held for reading. Returns 0 if handle joined the running 224 224 * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and 225 225 * caller must retry. 226 + * 227 + * Note: because j_state_lock may be dropped depending on the return 228 + * value, we need to fake out sparse so ti doesn't complain about a 229 + * locking imbalance. Callers of add_transaction_credits will need to 230 + * make a similar accomodation. 226 231 */ 227 232 static int add_transaction_credits(journal_t *journal, int blocks, 228 233 int rsv_blocks) 234 + __must_hold(&journal->j_state_lock) 229 235 { 230 236 transaction_t *t = journal->j_running_transaction; 231 237 int needed; ··· 244 238 if (t->t_state != T_RUNNING) { 245 239 WARN_ON_ONCE(t->t_state >= T_FLUSH); 246 240 wait_transaction_locked(journal); 241 + __acquire(&journal->j_state_lock); /* fake out sparse */ 247 242 return 1; 248 243 } 249 244 ··· 273 266 wait_event(journal->j_wait_reserved, 274 267 atomic_read(&journal->j_reserved_credits) + total <= 275 268 journal->j_max_transaction_buffers); 269 + __acquire(&journal->j_state_lock); /* fake out sparse */ 276 270 return 1; 277 271 } 278 272 279 273 wait_transaction_locked(journal); 274 + __acquire(&journal->j_state_lock); /* fake out sparse */ 280 275 return 1; 281 276 } 282 277 ··· 302 293 journal->j_max_transaction_buffers) 303 294 __jbd2_log_wait_for_space(journal); 304 295 write_unlock(&journal->j_state_lock); 296 + __acquire(&journal->j_state_lock); /* fake out sparse */ 305 297 return 1; 306 298 } 307 299 ··· 320 310 wait_event(journal->j_wait_reserved, 321 311 atomic_read(&journal->j_reserved_credits) + rsv_blocks 322 312 <= journal->j_max_transaction_buffers / 2); 313 + __acquire(&journal->j_state_lock); /* fake out sparse */ 323 314 return 1; 324 315 } 325 316 return 0; ··· 424 413 425 414 if (!handle->h_reserved) { 426 415 /* We may have dropped j_state_lock - restart in that case */ 427 - if (add_transaction_credits(journal, blocks, rsv_blocks)) 416 + if (add_transaction_credits(journal, blocks, rsv_blocks)) { 417 + /* 418 + * add_transaction_credits releases 419 + * j_state_lock on a non-zero return 420 + */ 421 + __release(&journal->j_state_lock); 428 422 goto repeat; 423 + } 429 424 } else { 430 425 /* 431 426 * We have handle reserved so we are allowed to join T_LOCKED ··· 1421 1404 { 1422 1405 struct journal_head *jh = jbd2_journal_grab_journal_head(bh); 1423 1406 1424 - if (WARN_ON(!jh)) 1407 + if (WARN_ON_ONCE(!jh)) 1425 1408 return; 1426 1409 jh->b_triggers = type; 1427 1410 jbd2_journal_put_journal_head(jh);