hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/ext4/xattr.c
....@@ -93,6 +93,7 @@
9393 #ifdef CONFIG_EXT4_FS_SECURITY
9494 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
9595 #endif
96
+ [EXT4_XATTR_INDEX_HURD] = &ext4_xattr_hurd_handler,
9697 };
9798
9899 const struct xattr_handler *ext4_xattr_handlers[] = {
....@@ -105,6 +106,7 @@
105106 #ifdef CONFIG_EXT4_FS_SECURITY
106107 &ext4_xattr_security_handler,
107108 #endif
109
+ &ext4_xattr_hurd_handler,
108110 NULL
109111 };
110112
....@@ -121,7 +123,11 @@
121123 #ifdef CONFIG_LOCKDEP
122124 void ext4_xattr_inode_set_class(struct inode *ea_inode)
123125 {
126
+ struct ext4_inode_info *ei = EXT4_I(ea_inode);
127
+
124128 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
129
+ (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
130
+ lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_EA);
125131 }
126132 #endif
127133
....@@ -245,7 +251,7 @@
245251 bh->b_data);
246252 errout:
247253 if (error)
248
- __ext4_error_inode(inode, function, line, 0,
254
+ __ext4_error_inode(inode, function, line, 0, -error,
249255 "corrupted xattr block %llu",
250256 (unsigned long long) bh->b_blocknr);
251257 else
....@@ -269,7 +275,7 @@
269275 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
270276 errout:
271277 if (error)
272
- __ext4_error_inode(inode, function, line, 0,
278
+ __ext4_error_inode(inode, function, line, 0, -error,
273279 "corrupted in-inode xattr");
274280 return error;
275281 }
....@@ -384,7 +390,18 @@
384390 struct inode *inode;
385391 int err;
386392
387
- inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_NORMAL);
393
+ /*
394
+ * We have to check for this corruption early as otherwise
395
+ * iget_locked() could wait indefinitely for the state of our
396
+ * parent inode.
397
+ */
398
+ if (parent->i_ino == ea_ino) {
399
+ ext4_error(parent->i_sb,
400
+ "Parent and EA inode have the same ino %lu", ea_ino);
401
+ return -EFSCORRUPTED;
402
+ }
403
+
404
+ inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_EA_INODE);
388405 if (IS_ERR(inode)) {
389406 err = PTR_ERR(inode);
390407 ext4_error(parent->i_sb,
....@@ -392,23 +409,6 @@
392409 err);
393410 return err;
394411 }
395
-
396
- if (is_bad_inode(inode)) {
397
- ext4_error(parent->i_sb,
398
- "error while reading EA inode %lu is_bad_inode",
399
- ea_ino);
400
- err = -EIO;
401
- goto error;
402
- }
403
-
404
- if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
405
- ext4_error(parent->i_sb,
406
- "EA inode %lu does not have EXT4_EA_INODE_FL flag",
407
- ea_ino);
408
- err = -EINVAL;
409
- goto error;
410
- }
411
-
412412 ext4_xattr_inode_set_class(inode);
413413
414414 /*
....@@ -429,9 +429,21 @@
429429
430430 *ea_inode = inode;
431431 return 0;
432
-error:
433
- iput(inode);
434
- return err;
432
+}
433
+
434
+/* Remove entry from mbcache when EA inode is getting evicted */
435
+void ext4_evict_ea_inode(struct inode *inode)
436
+{
437
+ struct mb_cache_entry *oe;
438
+
439
+ if (!EA_INODE_CACHE(inode))
440
+ return;
441
+ /* Wait for entry to get unused so that we can remove it */
442
+ while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
443
+ ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
444
+ mb_cache_entry_wait_unused(oe);
445
+ mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
446
+ }
435447 }
436448
437449 static int
....@@ -967,71 +979,18 @@
967979 return credits;
968980 }
969981
970
-static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
971
- int credits, struct buffer_head *bh,
972
- bool dirty, bool block_csum)
973
-{
974
- int error;
975
-
976
- if (!ext4_handle_valid(handle))
977
- return 0;
978
-
979
- if (handle->h_buffer_credits >= credits)
980
- return 0;
981
-
982
- error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
983
- if (!error)
984
- return 0;
985
- if (error < 0) {
986
- ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
987
- return error;
988
- }
989
-
990
- if (bh && dirty) {
991
- if (block_csum)
992
- ext4_xattr_block_csum_set(inode, bh);
993
- error = ext4_handle_dirty_metadata(handle, NULL, bh);
994
- if (error) {
995
- ext4_warning(inode->i_sb, "Handle metadata (error %d)",
996
- error);
997
- return error;
998
- }
999
- }
1000
-
1001
- error = ext4_journal_restart(handle, credits);
1002
- if (error) {
1003
- ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
1004
- return error;
1005
- }
1006
-
1007
- if (bh) {
1008
- error = ext4_journal_get_write_access(handle, bh);
1009
- if (error) {
1010
- ext4_warning(inode->i_sb,
1011
- "Get write access failed (error %d)",
1012
- error);
1013
- return error;
1014
- }
1015
- }
1016
- return 0;
1017
-}
1018
-
1019982 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
1020983 int ref_change)
1021984 {
1022
- struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
1023985 struct ext4_iloc iloc;
1024986 s64 ref_count;
1025
- u32 hash;
1026987 int ret;
1027988
1028989 inode_lock(ea_inode);
1029990
1030991 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1031
- if (ret) {
1032
- iloc.bh = NULL;
992
+ if (ret)
1033993 goto out;
1034
- }
1035994
1036995 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1037996 ref_count += ref_change;
....@@ -1047,14 +1006,6 @@
10471006
10481007 set_nlink(ea_inode, 1);
10491008 ext4_orphan_del(handle, ea_inode);
1050
-
1051
- if (ea_inode_cache) {
1052
- hash = ext4_xattr_inode_get_hash(ea_inode);
1053
- mb_cache_entry_create(ea_inode_cache,
1054
- GFP_NOFS, hash,
1055
- ea_inode->i_ino,
1056
- true /* reusable */);
1057
- }
10581009 }
10591010 } else {
10601011 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
....@@ -1067,22 +1018,14 @@
10671018
10681019 clear_nlink(ea_inode);
10691020 ext4_orphan_add(handle, ea_inode);
1070
-
1071
- if (ea_inode_cache) {
1072
- hash = ext4_xattr_inode_get_hash(ea_inode);
1073
- mb_cache_entry_delete(ea_inode_cache, hash,
1074
- ea_inode->i_ino);
1075
- }
10761021 }
10771022 }
10781023
10791024 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1080
- iloc.bh = NULL;
10811025 if (ret)
10821026 ext4_warning_inode(ea_inode,
10831027 "ext4_mark_iloc_dirty() failed ret=%d", ret);
10841028 out:
1085
- brelse(iloc.bh);
10861029 inode_unlock(ea_inode);
10871030 return ret;
10881031 }
....@@ -1153,6 +1096,24 @@
11531096 return saved_err;
11541097 }
11551098
1099
+static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1100
+ struct buffer_head *bh, bool block_csum, bool dirty)
1101
+{
1102
+ int error;
1103
+
1104
+ if (bh && dirty) {
1105
+ if (block_csum)
1106
+ ext4_xattr_block_csum_set(inode, bh);
1107
+ error = ext4_handle_dirty_metadata(handle, NULL, bh);
1108
+ if (error) {
1109
+ ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1110
+ error);
1111
+ return error;
1112
+ }
1113
+ }
1114
+ return 0;
1115
+}
1116
+
11561117 static void
11571118 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
11581119 struct buffer_head *bh,
....@@ -1189,12 +1150,23 @@
11891150 continue;
11901151 }
11911152
1192
- err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1193
- dirty, block_csum);
1194
- if (err) {
1153
+ err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1154
+ ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1155
+ ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1156
+ dirty));
1157
+ if (err < 0) {
11951158 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
11961159 err);
11971160 continue;
1161
+ }
1162
+ if (err > 0) {
1163
+ err = ext4_journal_get_write_access(handle, bh);
1164
+ if (err) {
1165
+ ext4_warning_inode(ea_inode,
1166
+ "Re-get write access err=%d",
1167
+ err);
1168
+ continue;
1169
+ }
11981170 }
11991171
12001172 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
....@@ -1253,6 +1225,7 @@
12531225 if (error)
12541226 goto out;
12551227
1228
+retry_ref:
12561229 lock_buffer(bh);
12571230 hash = le32_to_cpu(BHDR(bh)->h_hash);
12581231 ref = le32_to_cpu(BHDR(bh)->h_refcount);
....@@ -1262,9 +1235,18 @@
12621235 * This must happen under buffer lock for
12631236 * ext4_xattr_block_set() to reliably detect freed block
12641237 */
1265
- if (ea_block_cache)
1266
- mb_cache_entry_delete(ea_block_cache, hash,
1267
- bh->b_blocknr);
1238
+ if (ea_block_cache) {
1239
+ struct mb_cache_entry *oe;
1240
+
1241
+ oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1242
+ bh->b_blocknr);
1243
+ if (oe) {
1244
+ unlock_buffer(bh);
1245
+ mb_cache_entry_wait_unused(oe);
1246
+ mb_cache_entry_put(ea_block_cache, oe);
1247
+ goto retry_ref;
1248
+ }
1249
+ }
12681250 get_bh(bh);
12691251 unlock_buffer(bh);
12701252
....@@ -1288,7 +1270,7 @@
12881270 ce = mb_cache_entry_get(ea_block_cache, hash,
12891271 bh->b_blocknr);
12901272 if (ce) {
1291
- ce->e_reusable = 1;
1273
+ set_bit(MBE_REUSABLE_B, &ce->e_flags);
12921274 mb_cache_entry_put(ea_block_cache, ce);
12931275 }
12941276 }
....@@ -1351,7 +1333,7 @@
13511333 int blocksize = ea_inode->i_sb->s_blocksize;
13521334 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
13531335 int csize, wsize = 0;
1354
- int ret = 0;
1336
+ int ret = 0, ret2 = 0;
13551337 int retries = 0;
13561338
13571339 retry:
....@@ -1378,8 +1360,7 @@
13781360
13791361 block = 0;
13801362 while (wsize < bufsize) {
1381
- if (bh != NULL)
1382
- brelse(bh);
1363
+ brelse(bh);
13831364 csize = (bufsize - wsize) > blocksize ? blocksize :
13841365 bufsize - wsize;
13851366 bh = ext4_getblk(handle, ea_inode, block, 0);
....@@ -1409,7 +1390,9 @@
14091390 ext4_update_i_disksize(ea_inode, wsize);
14101391 inode_unlock(ea_inode);
14111392
1412
- ext4_mark_inode_dirty(handle, ea_inode);
1393
+ ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1394
+ if (unlikely(ret2 && !ret))
1395
+ ret = ret2;
14131396
14141397 out:
14151398 brelse(bh);
....@@ -1426,6 +1409,13 @@
14261409 struct inode *ea_inode = NULL;
14271410 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
14281411 int err;
1412
+
1413
+ if (inode->i_sb->s_root == NULL) {
1414
+ ext4_warning(inode->i_sb,
1415
+ "refuse to create EA inode when umounting");
1416
+ WARN_ON(1);
1417
+ return ERR_PTR(-EINVAL);
1418
+ }
14291419
14301420 /*
14311421 * Let the next inode be the goal, so we try and allocate the EA inode
....@@ -1446,6 +1436,9 @@
14461436 if (!err)
14471437 err = ext4_inode_attach_jinode(ea_inode);
14481438 if (err) {
1439
+ if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1440
+ ext4_warning_inode(ea_inode,
1441
+ "cleanup dec ref error %d", err);
14491442 iput(ea_inode);
14501443 return ERR_PTR(err);
14511444 }
....@@ -1483,7 +1476,7 @@
14831476 WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
14841477 !(current->flags & PF_MEMALLOC_NOFS));
14851478
1486
- ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1479
+ ea_data = kvmalloc(value_len, GFP_KERNEL);
14871480 if (!ea_data) {
14881481 mb_cache_entry_put(ea_inode_cache, ce);
14891482 return NULL;
....@@ -1491,11 +1484,11 @@
14911484
14921485 while (ce) {
14931486 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1494
- EXT4_IGET_NORMAL);
1495
- if (!IS_ERR(ea_inode) &&
1496
- !is_bad_inode(ea_inode) &&
1497
- (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1498
- i_size_read(ea_inode) == value_len &&
1487
+ EXT4_IGET_EA_INODE);
1488
+ if (IS_ERR(ea_inode))
1489
+ goto next_entry;
1490
+ ext4_xattr_inode_set_class(ea_inode);
1491
+ if (i_size_read(ea_inode) == value_len &&
14991492 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
15001493 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
15011494 value_len) &&
....@@ -1505,9 +1498,8 @@
15051498 kvfree(ea_data);
15061499 return ea_inode;
15071500 }
1508
-
1509
- if (!IS_ERR(ea_inode))
1510
- iput(ea_inode);
1501
+ iput(ea_inode);
1502
+ next_entry:
15111503 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
15121504 }
15131505 kvfree(ea_data);
....@@ -1635,7 +1627,7 @@
16351627 * If storing the value in an external inode is an option,
16361628 * reserve space for xattr entries/names in the external
16371629 * attribute block so that a long value does not occupy the
1638
- * whole space and prevent futher entries being added.
1630
+ * whole space and prevent further entries being added.
16391631 */
16401632 if (ext4_has_feature_ea_inode(inode->i_sb) &&
16411633 new_size && is_block &&
....@@ -1733,6 +1725,20 @@
17331725 memmove(here, (void *)here + size,
17341726 (void *)last - (void *)here + sizeof(__u32));
17351727 memset(last, 0, size);
1728
+
1729
+ /*
1730
+ * Update i_inline_off - moved ibody region might contain
1731
+ * system.data attribute. Handling a failure here won't
1732
+ * cause other complications for setting an xattr.
1733
+ */
1734
+ if (!is_block && ext4_has_inline_data(inode)) {
1735
+ ret = ext4_find_inline_data_nolock(inode);
1736
+ if (ret) {
1737
+ ext4_warning_inode(inode,
1738
+ "unable to update i_inline_off");
1739
+ goto out;
1740
+ }
1741
+ }
17361742 } else if (s->not_found) {
17371743 /* Insert new name. */
17381744 size_t size = EXT4_XATTR_LEN(name_len);
....@@ -1872,6 +1878,8 @@
18721878 #define header(x) ((struct ext4_xattr_header *)(x))
18731879
18741880 if (s->base) {
1881
+ int offset = (char *)s->here - bs->bh->b_data;
1882
+
18751883 BUFFER_TRACE(bs->bh, "get_write_access");
18761884 error = ext4_journal_get_write_access(handle, bs->bh);
18771885 if (error)
....@@ -1886,9 +1894,20 @@
18861894 * ext4_xattr_block_set() to reliably detect modified
18871895 * block
18881896 */
1889
- if (ea_block_cache)
1890
- mb_cache_entry_delete(ea_block_cache, hash,
1891
- bs->bh->b_blocknr);
1897
+ if (ea_block_cache) {
1898
+ struct mb_cache_entry *oe;
1899
+
1900
+ oe = mb_cache_entry_delete_or_get(ea_block_cache,
1901
+ hash, bs->bh->b_blocknr);
1902
+ if (oe) {
1903
+ /*
1904
+ * Xattr block is getting reused. Leave
1905
+ * it alone.
1906
+ */
1907
+ mb_cache_entry_put(ea_block_cache, oe);
1908
+ goto clone_block;
1909
+ }
1910
+ }
18921911 ea_bdebug(bs->bh, "modifying in-place");
18931912 error = ext4_xattr_set_entry(i, s, handle, inode,
18941913 true /* is_block */);
....@@ -1903,50 +1922,47 @@
19031922 if (error)
19041923 goto cleanup;
19051924 goto inserted;
1906
- } else {
1907
- int offset = (char *)s->here - bs->bh->b_data;
1925
+ }
1926
+clone_block:
1927
+ unlock_buffer(bs->bh);
1928
+ ea_bdebug(bs->bh, "cloning");
1929
+ s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1930
+ error = -ENOMEM;
1931
+ if (s->base == NULL)
1932
+ goto cleanup;
1933
+ s->first = ENTRY(header(s->base)+1);
1934
+ header(s->base)->h_refcount = cpu_to_le32(1);
1935
+ s->here = ENTRY(s->base + offset);
1936
+ s->end = s->base + bs->bh->b_size;
19081937
1909
- unlock_buffer(bs->bh);
1910
- ea_bdebug(bs->bh, "cloning");
1911
- s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
1912
- error = -ENOMEM;
1913
- if (s->base == NULL)
1938
+ /*
1939
+ * If existing entry points to an xattr inode, we need
1940
+ * to prevent ext4_xattr_set_entry() from decrementing
1941
+ * ref count on it because the reference belongs to the
1942
+ * original block. In this case, make the entry look
1943
+ * like it has an empty value.
1944
+ */
1945
+ if (!s->not_found && s->here->e_value_inum) {
1946
+ ea_ino = le32_to_cpu(s->here->e_value_inum);
1947
+ error = ext4_xattr_inode_iget(inode, ea_ino,
1948
+ le32_to_cpu(s->here->e_hash),
1949
+ &tmp_inode);
1950
+ if (error)
19141951 goto cleanup;
1915
- memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1916
- s->first = ENTRY(header(s->base)+1);
1917
- header(s->base)->h_refcount = cpu_to_le32(1);
1918
- s->here = ENTRY(s->base + offset);
1919
- s->end = s->base + bs->bh->b_size;
19201952
1921
- /*
1922
- * If existing entry points to an xattr inode, we need
1923
- * to prevent ext4_xattr_set_entry() from decrementing
1924
- * ref count on it because the reference belongs to the
1925
- * original block. In this case, make the entry look
1926
- * like it has an empty value.
1927
- */
1928
- if (!s->not_found && s->here->e_value_inum) {
1929
- ea_ino = le32_to_cpu(s->here->e_value_inum);
1930
- error = ext4_xattr_inode_iget(inode, ea_ino,
1931
- le32_to_cpu(s->here->e_hash),
1932
- &tmp_inode);
1933
- if (error)
1934
- goto cleanup;
1935
-
1936
- if (!ext4_test_inode_state(tmp_inode,
1937
- EXT4_STATE_LUSTRE_EA_INODE)) {
1938
- /*
1939
- * Defer quota free call for previous
1940
- * inode until success is guaranteed.
1941
- */
1942
- old_ea_inode_quota = le32_to_cpu(
1943
- s->here->e_value_size);
1944
- }
1945
- iput(tmp_inode);
1946
-
1947
- s->here->e_value_inum = 0;
1948
- s->here->e_value_size = 0;
1953
+ if (!ext4_test_inode_state(tmp_inode,
1954
+ EXT4_STATE_LUSTRE_EA_INODE)) {
1955
+ /*
1956
+ * Defer quota free call for previous
1957
+ * inode until success is guaranteed.
1958
+ */
1959
+ old_ea_inode_quota = le32_to_cpu(
1960
+ s->here->e_value_size);
19491961 }
1962
+ iput(tmp_inode);
1963
+
1964
+ s->here->e_value_inum = 0;
1965
+ s->here->e_value_size = 0;
19501966 }
19511967 } else {
19521968 /* Allocate a buffer where we construct the new block. */
....@@ -1997,8 +2013,9 @@
19972013 else {
19982014 u32 ref;
19992015
2016
+#ifdef EXT4_XATTR_DEBUG
20002017 WARN_ON_ONCE(dquot_initialize_needed(inode));
2001
-
2018
+#endif
20022019 /* The old block is released after updating
20032020 the inode. */
20042021 error = dquot_alloc_block(inode,
....@@ -2013,18 +2030,13 @@
20132030 lock_buffer(new_bh);
20142031 /*
20152032 * We have to be careful about races with
2016
- * freeing, rehashing or adding references to
2017
- * xattr block. Once we hold buffer lock xattr
2018
- * block's state is stable so we can check
2019
- * whether the block got freed / rehashed or
2020
- * not. Since we unhash mbcache entry under
2021
- * buffer lock when freeing / rehashing xattr
2022
- * block, checking whether entry is still
2023
- * hashed is reliable. Same rules hold for
2024
- * e_reusable handling.
2033
+ * adding references to xattr block. Once we
2034
+ * hold buffer lock xattr block's state is
2035
+ * stable so we can check the additional
2036
+ * reference fits.
20252037 */
2026
- if (hlist_bl_unhashed(&ce->e_hash_list) ||
2027
- !ce->e_reusable) {
2038
+ ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2039
+ if (ref > EXT4_XATTR_REFCOUNT_MAX) {
20282040 /*
20292041 * Undo everything and check mbcache
20302042 * again.
....@@ -2039,10 +2051,9 @@
20392051 new_bh = NULL;
20402052 goto inserted;
20412053 }
2042
- ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
20432054 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2044
- if (ref >= EXT4_XATTR_REFCOUNT_MAX)
2045
- ce->e_reusable = 0;
2055
+ if (ref == EXT4_XATTR_REFCOUNT_MAX)
2056
+ clear_bit(MBE_REUSABLE_B, &ce->e_flags);
20462057 ea_bdebug(new_bh, "reusing; refcount now=%d",
20472058 ref);
20482059 ext4_xattr_block_csum_set(inode, new_bh);
....@@ -2066,22 +2077,15 @@
20662077 /* We need to allocate a new block */
20672078 ext4_fsblk_t goal, block;
20682079
2080
+#ifdef EXT4_XATTR_DEBUG
20692081 WARN_ON_ONCE(dquot_initialize_needed(inode));
2070
-
2082
+#endif
20712083 goal = ext4_group_first_block_no(sb,
20722084 EXT4_I(inode)->i_block_group);
2073
-
2074
- /* non-extent files can't have physical blocks past 2^32 */
2075
- if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
2076
- goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
2077
-
20782085 block = ext4_new_meta_blocks(handle, inode, goal, 0,
20792086 NULL, &error);
20802087 if (error)
20812088 goto cleanup;
2082
-
2083
- if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
2084
- BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
20852089
20862090 ea_idebug(inode, "creating block %llu",
20872091 (unsigned long long)block);
....@@ -2188,8 +2192,9 @@
21882192 struct ext4_inode *raw_inode;
21892193 int error;
21902194
2191
- if (EXT4_I(inode)->i_extra_isize == 0)
2195
+ if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
21922196 return 0;
2197
+
21932198 raw_inode = ext4_raw_inode(&is->iloc);
21942199 header = IHDR(inode, raw_inode);
21952200 is->s.base = is->s.first = IFIRST(header);
....@@ -2209,7 +2214,7 @@
22092214 return 0;
22102215 }
22112216
2212
-int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2217
+int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
22132218 struct ext4_xattr_info *i,
22142219 struct ext4_xattr_ibody_find *is)
22152220 {
....@@ -2217,32 +2222,9 @@
22172222 struct ext4_xattr_search *s = &is->s;
22182223 int error;
22192224
2220
- if (EXT4_I(inode)->i_extra_isize == 0)
2225
+ if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
22212226 return -ENOSPC;
2222
- error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2223
- if (error)
2224
- return error;
2225
- header = IHDR(inode, ext4_raw_inode(&is->iloc));
2226
- if (!IS_LAST_ENTRY(s->first)) {
2227
- header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2228
- ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2229
- } else {
2230
- header->h_magic = cpu_to_le32(0);
2231
- ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2232
- }
2233
- return 0;
2234
-}
22352227
2236
-static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2237
- struct ext4_xattr_info *i,
2238
- struct ext4_xattr_ibody_find *is)
2239
-{
2240
- struct ext4_xattr_ibody_header *header;
2241
- struct ext4_xattr_search *s = &is->s;
2242
- int error;
2243
-
2244
- if (EXT4_I(inode)->i_extra_isize == 0)
2245
- return -ENOSPC;
22462228 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
22472229 if (error)
22482230 return error;
....@@ -2345,7 +2327,7 @@
23452327 flags & XATTR_CREATE);
23462328 brelse(bh);
23472329
2348
- if (!ext4_handle_has_enough_credits(handle, credits)) {
2330
+ if (jbd2_handle_buffer_credits(handle) < credits) {
23492331 error = -ENOSPC;
23502332 goto cleanup;
23512333 }
....@@ -2444,6 +2426,7 @@
24442426 if (IS_SYNC(inode))
24452427 ext4_handle_sync(handle);
24462428 }
2429
+ ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
24472430
24482431 cleanup:
24492432 brelse(is.iloc.bh);
....@@ -2521,6 +2504,7 @@
25212504 if (error == 0)
25222505 error = error2;
25232506 }
2507
+ ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
25242508
25252509 return error;
25262510 }
....@@ -2569,13 +2553,13 @@
25692553 .in_inode = !!entry->e_value_inum,
25702554 };
25712555 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2556
+ int needs_kvfree = 0;
25722557 int error;
25732558
25742559 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
25752560 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2576
- buffer = kmalloc(value_size, GFP_NOFS);
25772561 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2578
- if (!is || !bs || !buffer || !b_entry_name) {
2562
+ if (!is || !bs || !b_entry_name) {
25792563 error = -ENOMEM;
25802564 goto out;
25812565 }
....@@ -2587,12 +2571,18 @@
25872571
25882572 /* Save the entry name and the entry value */
25892573 if (entry->e_value_inum) {
2574
+ buffer = kvmalloc(value_size, GFP_NOFS);
2575
+ if (!buffer) {
2576
+ error = -ENOMEM;
2577
+ goto out;
2578
+ }
2579
+ needs_kvfree = 1;
25902580 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
25912581 if (error)
25922582 goto out;
25932583 } else {
25942584 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2595
- memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2585
+ buffer = (void *)IFIRST(header) + value_offs;
25962586 }
25972587
25982588 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
....@@ -2607,25 +2597,26 @@
26072597 if (error)
26082598 goto out;
26092599
2610
- /* Remove the chosen entry from the inode */
2611
- error = ext4_xattr_ibody_set(handle, inode, &i, is);
2612
- if (error)
2613
- goto out;
2614
-
26152600 i.value = buffer;
26162601 i.value_len = value_size;
26172602 error = ext4_xattr_block_find(inode, &i, bs);
26182603 if (error)
26192604 goto out;
26202605
2621
- /* Add entry which was removed from the inode into the block */
2606
+ /* Move ea entry from the inode into the block */
26222607 error = ext4_xattr_block_set(handle, inode, &i, bs);
26232608 if (error)
26242609 goto out;
2625
- error = 0;
2610
+
2611
+ /* Remove the chosen entry from the inode */
2612
+ i.value = NULL;
2613
+ i.value_len = 0;
2614
+ error = ext4_xattr_ibody_set(handle, inode, &i, is);
2615
+
26262616 out:
26272617 kfree(b_entry_name);
2628
- kfree(buffer);
2618
+ if (needs_kvfree && buffer)
2619
+ kvfree(buffer);
26292620 if (is)
26302621 brelse(is->iloc.bh);
26312622 if (bs)
....@@ -2800,6 +2791,9 @@
28002791 (void *)header, total_ino);
28012792 EXT4_I(inode)->i_extra_isize = new_extra_isize;
28022793
2794
+ if (ext4_has_inline_data(inode))
2795
+ error = ext4_find_inline_data_nolock(inode);
2796
+
28032797 cleanup:
28042798 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
28052799 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
....@@ -2873,11 +2867,9 @@
28732867 struct inode *ea_inode;
28742868 int error;
28752869
2876
- error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2877
- NULL /* bh */,
2878
- false /* dirty */,
2879
- false /* block_csum */);
2880
- if (error) {
2870
+ error = ext4_journal_ensure_credits(handle, extra_credits,
2871
+ ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2872
+ if (error < 0) {
28812873 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
28822874 goto cleanup;
28832875 }
....@@ -2912,9 +2904,11 @@
29122904 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
29132905 if (IS_ERR(bh)) {
29142906 error = PTR_ERR(bh);
2915
- if (error == -EIO)
2916
- EXT4_ERROR_INODE(inode, "block %llu read error",
2917
- EXT4_I(inode)->i_file_acl);
2907
+ if (error == -EIO) {
2908
+ EXT4_ERROR_INODE_ERR(inode, EIO,
2909
+ "block %llu read error",
2910
+ EXT4_I(inode)->i_file_acl);
2911
+ }
29182912 bh = NULL;
29192913 goto cleanup;
29202914 }
....@@ -2953,6 +2947,7 @@
29532947 error);
29542948 goto cleanup;
29552949 }
2950
+ ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
29562951 }
29572952 error = 0;
29582953 cleanup: