hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/ext4/namei.c
....@@ -54,6 +54,7 @@
5454 struct inode *inode,
5555 ext4_lblk_t *block)
5656 {
57
+ struct ext4_map_blocks map;
5758 struct buffer_head *bh;
5859 int err;
5960
....@@ -63,6 +64,21 @@
6364 return ERR_PTR(-ENOSPC);
6465
6566 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
67
+ map.m_lblk = *block;
68
+ map.m_len = 1;
69
+
70
+ /*
71
+ * We're appending new directory block. Make sure the block is not
72
+ * allocated yet, otherwise we will end up corrupting the
73
+ * directory.
74
+ */
75
+ err = ext4_map_blocks(NULL, inode, &map, 0);
76
+ if (err < 0)
77
+ return ERR_PTR(err);
78
+ if (err) {
79
+ EXT4_ERROR_INODE(inode, "Logical block already allocated");
80
+ return ERR_PTR(-EFSCORRUPTED);
81
+ }
6682
6783 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
6884 if (IS_ERR(bh))
....@@ -109,7 +125,17 @@
109125 struct ext4_dir_entry *dirent;
110126 int is_dx_block = 0;
111127
112
- bh = ext4_bread(NULL, inode, block, 0);
128
+ if (block >= inode->i_size >> inode->i_blkbits) {
129
+ ext4_error_inode(inode, func, line, block,
130
+ "Attempting to read directory block (%u) that is past i_size (%llu)",
131
+ block, inode->i_size);
132
+ return ERR_PTR(-EFSCORRUPTED);
133
+ }
134
+
135
+ if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
136
+ bh = ERR_PTR(-EIO);
137
+ else
138
+ bh = ext4_bread(NULL, inode, block, 0);
113139 if (IS_ERR(bh)) {
114140 __ext4_warning(inode->i_sb, func, line,
115141 "inode #%lu: lblock %lu: comm %s: "
....@@ -153,21 +179,25 @@
153179 * caller is sure it should be an index block.
154180 */
155181 if (is_dx_block && type == INDEX) {
156
- if (ext4_dx_csum_verify(inode, dirent))
182
+ if (ext4_dx_csum_verify(inode, dirent) &&
183
+ !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
157184 set_buffer_verified(bh);
158185 else {
159
- ext4_error_inode(inode, func, line, block,
160
- "Directory index failed checksum");
186
+ ext4_error_inode_err(inode, func, line, block,
187
+ EFSBADCRC,
188
+ "Directory index failed checksum");
161189 brelse(bh);
162190 return ERR_PTR(-EFSBADCRC);
163191 }
164192 }
165193 if (!is_dx_block) {
166
- if (ext4_dirent_csum_verify(inode, dirent))
194
+ if (ext4_dirblock_csum_verify(inode, bh) &&
195
+ !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
167196 set_buffer_verified(bh);
168197 else {
169
- ext4_error_inode(inode, func, line, block,
170
- "Directory block failed checksum");
198
+ ext4_error_inode_err(inode, func, line, block,
199
+ EFSBADCRC,
200
+ "Directory block failed checksum");
171201 brelse(bh);
172202 return ERR_PTR(-EFSBADCRC);
173203 }
....@@ -226,13 +256,13 @@
226256 u8 unused_flags;
227257 }
228258 info;
229
- struct dx_entry entries[0];
259
+ struct dx_entry entries[];
230260 };
231261
232262 struct dx_node
233263 {
234264 struct fake_dirent fake;
235
- struct dx_entry entries[0];
265
+ struct dx_entry entries[];
236266 };
237267
238268
....@@ -295,9 +325,11 @@
295325 struct inode *dir, struct inode *inode);
296326
297327 /* checksumming functions */
298
-void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
299
- unsigned int blocksize)
328
+void ext4_initialize_dirent_tail(struct buffer_head *bh,
329
+ unsigned int blocksize)
300330 {
331
+ struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
332
+
301333 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
302334 t->det_rec_len = ext4_rec_len_to_disk(
303335 sizeof(struct ext4_dir_entry_tail), blocksize);
....@@ -306,31 +338,32 @@
306338
307339 /* Walk through a dirent block to find a checksum "dirent" at the tail */
308340 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
309
- struct ext4_dir_entry *de)
341
+ struct buffer_head *bh)
310342 {
311343 struct ext4_dir_entry_tail *t;
344
+ int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
312345
313346 #ifdef PARANOID
314347 struct ext4_dir_entry *d, *top;
315348
316
- d = de;
317
- top = (struct ext4_dir_entry *)(((void *)de) +
318
- (EXT4_BLOCK_SIZE(inode->i_sb) -
319
- sizeof(struct ext4_dir_entry_tail)));
320
- while (d < top && d->rec_len)
349
+ d = (struct ext4_dir_entry *)bh->b_data;
350
+ top = (struct ext4_dir_entry *)(bh->b_data +
351
+ (blocksize - sizeof(struct ext4_dir_entry_tail)));
352
+ while (d < top && ext4_rec_len_from_disk(d->rec_len, blocksize))
321353 d = (struct ext4_dir_entry *)(((void *)d) +
322
- le16_to_cpu(d->rec_len));
354
+ ext4_rec_len_from_disk(d->rec_len, blocksize));
323355
324356 if (d != top)
325357 return NULL;
326358
327359 t = (struct ext4_dir_entry_tail *)d;
328360 #else
329
- t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
361
+ t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
330362 #endif
331363
332364 if (t->det_reserved_zero1 ||
333
- le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
365
+ (ext4_rec_len_from_disk(t->det_rec_len, blocksize) !=
366
+ sizeof(struct ext4_dir_entry_tail)) ||
334367 t->det_reserved_zero2 ||
335368 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
336369 return NULL;
....@@ -338,8 +371,7 @@
338371 return t;
339372 }
340373
341
-static __le32 ext4_dirent_csum(struct inode *inode,
342
- struct ext4_dir_entry *dirent, int size)
374
+static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
343375 {
344376 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
345377 struct ext4_inode_info *ei = EXT4_I(inode);
....@@ -359,49 +391,49 @@
359391 "No space for directory leaf checksum. Please run e2fsck -D.");
360392 }
361393
362
-int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
394
+int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
363395 {
364396 struct ext4_dir_entry_tail *t;
365397
366398 if (!ext4_has_metadata_csum(inode->i_sb))
367399 return 1;
368400
369
- t = get_dirent_tail(inode, dirent);
401
+ t = get_dirent_tail(inode, bh);
370402 if (!t) {
371403 warn_no_space_for_csum(inode);
372404 return 0;
373405 }
374406
375
- if (t->det_checksum != ext4_dirent_csum(inode, dirent,
376
- (void *)t - (void *)dirent))
407
+ if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
408
+ (char *)t - bh->b_data))
377409 return 0;
378410
379411 return 1;
380412 }
381413
382
-static void ext4_dirent_csum_set(struct inode *inode,
383
- struct ext4_dir_entry *dirent)
414
+static void ext4_dirblock_csum_set(struct inode *inode,
415
+ struct buffer_head *bh)
384416 {
385417 struct ext4_dir_entry_tail *t;
386418
387419 if (!ext4_has_metadata_csum(inode->i_sb))
388420 return;
389421
390
- t = get_dirent_tail(inode, dirent);
422
+ t = get_dirent_tail(inode, bh);
391423 if (!t) {
392424 warn_no_space_for_csum(inode);
393425 return;
394426 }
395427
396
- t->det_checksum = ext4_dirent_csum(inode, dirent,
397
- (void *)t - (void *)dirent);
428
+ t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
429
+ (char *)t - bh->b_data);
398430 }
399431
400
-int ext4_handle_dirty_dirent_node(handle_t *handle,
401
- struct inode *inode,
402
- struct buffer_head *bh)
432
+int ext4_handle_dirty_dirblock(handle_t *handle,
433
+ struct inode *inode,
434
+ struct buffer_head *bh)
403435 {
404
- ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
436
+ ext4_dirblock_csum_set(inode, bh);
405437 return ext4_handle_dirty_metadata(handle, inode, bh);
406438 }
407439
....@@ -412,13 +444,14 @@
412444 struct ext4_dir_entry *dp;
413445 struct dx_root_info *root;
414446 int count_offset;
447
+ int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
448
+ unsigned int rlen = ext4_rec_len_from_disk(dirent->rec_len, blocksize);
415449
416
- if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
450
+ if (rlen == blocksize)
417451 count_offset = 8;
418
- else if (le16_to_cpu(dirent->rec_len) == 12) {
452
+ else if (rlen == 12) {
419453 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
420
- if (le16_to_cpu(dp->rec_len) !=
421
- EXT4_BLOCK_SIZE(inode->i_sb) - 12)
454
+ if (ext4_rec_len_from_disk(dp->rec_len, blocksize) != blocksize - 12)
422455 return NULL;
423456 root = (struct dx_root_info *)(((void *)dp + 12));
424457 if (root->reserved_zero ||
....@@ -639,13 +672,7 @@
639672
640673 name = de->name;
641674 len = de->name_len;
642
- if (IS_ENCRYPTED(dir))
643
- res = fscrypt_get_encryption_info(dir);
644
- if (res) {
645
- printk(KERN_WARNING "Error setting up"
646
- " fname crypto: %d\n", res);
647
- }
648
- if (!fscrypt_has_encryption_key(dir)) {
675
+ if (!IS_ENCRYPTED(dir)) {
649676 /* Directory is not encrypted */
650677 ext4fs_dirhash(dir, de->name,
651678 de->name_len, &h);
....@@ -659,8 +686,7 @@
659686
660687 /* Directory is encrypted */
661688 res = fscrypt_fname_alloc_buffer(
662
- dir, len,
663
- &fname_crypto_str);
689
+ len, &fname_crypto_str);
664690 if (res)
665691 printk(KERN_WARNING "Error "
666692 "allocating crypto "
....@@ -756,12 +782,14 @@
756782 dx_probe(struct ext4_filename *fname, struct inode *dir,
757783 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
758784 {
759
- unsigned count, indirect;
785
+ unsigned count, indirect, level, i;
760786 struct dx_entry *at, *entries, *p, *q, *m;
761787 struct dx_root *root;
762788 struct dx_frame *frame = frame_in;
763789 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
764790 u32 hash;
791
+ ext4_lblk_t block;
792
+ ext4_lblk_t blocks[EXT4_HTREE_LEVEL];
765793
766794 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
767795 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
....@@ -833,6 +861,8 @@
833861 }
834862
835863 dxtrace(printk("Look up %x", hash));
864
+ level = 0;
865
+ blocks[0] = 0;
836866 while (1) {
837867 count = dx_get_count(entries);
838868 if (!count || count > dx_get_limit(entries)) {
....@@ -874,15 +904,27 @@
874904 dx_get_block(at)));
875905 frame->entries = entries;
876906 frame->at = at;
877
- if (!indirect--)
907
+
908
+ block = dx_get_block(at);
909
+ for (i = 0; i <= level; i++) {
910
+ if (blocks[i] == block) {
911
+ ext4_warning_inode(dir,
912
+ "dx entry: tree cycle block %u points back to block %u",
913
+ blocks[level], block);
914
+ goto fail;
915
+ }
916
+ }
917
+ if (++level > indirect)
878918 return frame;
919
+ blocks[level] = block;
879920 frame++;
880
- frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
921
+ frame->bh = ext4_read_dirblock(dir, block, INDEX);
881922 if (IS_ERR(frame->bh)) {
882923 ret_err = (struct dx_frame *) frame->bh;
883924 frame->bh = NULL;
884925 goto fail;
885926 }
927
+
886928 entries = ((struct dx_node *) frame->bh->b_data)->entries;
887929
888930 if (dx_get_limit(entries) != dx_node_limit(dir)) {
....@@ -972,7 +1014,7 @@
9721014 * If the hash is 1, then continue only if the next page has a
9731015 * continuation hash of any value. This is used for readdir
9741016 * handling. Otherwise, check to see if the hash matches the
975
- * desired contiuation hash. If it doesn't, return since
1017
+ * desired continuation hash. If it doesn't, return since
9761018 * there's no point to read in the successive index pages.
9771019 */
9781020 bhash = dx_get_hash(p->at);
....@@ -1027,22 +1069,21 @@
10271069 dir->i_sb->s_blocksize -
10281070 ext4_dir_rec_len(0,
10291071 csum ? NULL : dir));
1030
-#ifdef CONFIG_FS_ENCRYPTION
10311072 /* Check if the directory is encrypted */
10321073 if (IS_ENCRYPTED(dir)) {
1033
- err = fscrypt_get_encryption_info(dir);
1074
+ err = fscrypt_prepare_readdir(dir);
10341075 if (err < 0) {
10351076 brelse(bh);
10361077 return err;
10371078 }
1038
- err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
1039
- &fname_crypto_str);
1079
+ err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
1080
+ &fname_crypto_str);
10401081 if (err < 0) {
10411082 brelse(bh);
10421083 return err;
10431084 }
10441085 }
1045
-#endif
1086
+
10461087 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
10471088 if (ext4_check_dir_entry(dir, NULL, de, bh,
10481089 bh->b_data, bh->b_size, block,
....@@ -1100,9 +1141,7 @@
11001141 }
11011142 errout:
11021143 brelse(bh);
1103
-#ifdef CONFIG_FS_ENCRYPTION
11041144 fscrypt_fname_free_buffer(&fname_crypto_str);
1105
-#endif
11061145 return count;
11071146 }
11081147
....@@ -1143,10 +1182,10 @@
11431182 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
11441183 if (ext4_has_inline_data(dir)) {
11451184 int has_inline_data = 1;
1146
- count = htree_inlinedir_to_tree(dir_file, dir, 0,
1147
- &hinfo, start_hash,
1148
- start_minor_hash,
1149
- &has_inline_data);
1185
+ count = ext4_inlinedir_to_tree(dir_file, dir, 0,
1186
+ &hinfo, start_hash,
1187
+ start_minor_hash,
1188
+ &has_inline_data);
11501189 if (has_inline_data) {
11511190 *next_hash = ~0;
11521191 return count;
....@@ -1262,11 +1301,11 @@
12621301 map_tail--;
12631302 map_tail->hash = h.hash;
12641303 map_tail->offs = ((char *) de - base)>>2;
1265
- map_tail->size = le16_to_cpu(de->rec_len);
1304
+ map_tail->size = ext4_rec_len_from_disk(de->rec_len,
1305
+ blocksize);
12661306 count++;
12671307 cond_resched();
12681308 }
1269
- /* XXX: do we need to check rec_len == 0 case? -Chris */
12701309 de = ext4_next_entry(de, blocksize);
12711310 }
12721311 return count;
....@@ -1354,7 +1393,7 @@
13541393 /* Handle invalid character sequence as either an error
13551394 * or as an opaque byte sequence.
13561395 */
1357
- if (sb_has_enc_strict_mode(sb))
1396
+ if (sb_has_strict_encoding(sb))
13581397 ret = -EINVAL;
13591398 else if (name->len != entry.len)
13601399 ret = 1;
....@@ -1373,7 +1412,8 @@
13731412 struct dx_hash_info *hinfo = &name->hinfo;
13741413 int len;
13751414
1376
- if (!needs_casefold(dir)) {
1415
+ if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding ||
1416
+ (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
13771417 cf_name->name = NULL;
13781418 return 0;
13791419 }
....@@ -1424,7 +1464,8 @@
14241464 #endif
14251465
14261466 #ifdef CONFIG_UNICODE
1427
- if (needs_casefold(parent)) {
1467
+ if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent) &&
1468
+ (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
14281469 if (fname->cf_name.name) {
14291470 struct qstr cf = {.name = fname->cf_name.name,
14301471 .len = fname->cf_name.len};
....@@ -1461,10 +1502,10 @@
14611502
14621503 de = (struct ext4_dir_entry_2 *)search_buf;
14631504 dlimit = search_buf + buf_size;
1464
- while ((char *) de < dlimit) {
1505
+ while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
14651506 /* this code is executed quadratically often */
14661507 /* do minimal checking `by hand' */
1467
- if ((char *) de + de->name_len <= dlimit &&
1508
+ if (de->name + de->name_len <= dlimit &&
14681509 ext4_match(dir, fname, de)) {
14691510 /* found a match - just to be sure, do
14701511 * a full check */
....@@ -1541,11 +1582,10 @@
15411582 &has_inline_data);
15421583 if (lblk)
15431584 *lblk = 0;
1544
- if (has_inline_data) {
1545
- if (inlined)
1546
- *inlined = 1;
1585
+ if (inlined)
1586
+ *inlined = has_inline_data;
1587
+ if (has_inline_data)
15471588 goto cleanup_and_exit;
1548
- }
15491589 }
15501590
15511591 if ((namelen <= 2) && (name[0] == '.') &&
....@@ -1606,8 +1646,9 @@
16061646 goto next;
16071647 wait_on_buffer(bh);
16081648 if (!buffer_uptodate(bh)) {
1609
- EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1610
- (unsigned long) block);
1649
+ EXT4_ERROR_INODE_ERR(dir, EIO,
1650
+ "reading directory lblock %lu",
1651
+ (unsigned long) block);
16111652 brelse(bh);
16121653 ret = ERR_PTR(-EIO);
16131654 goto cleanup_and_exit;
....@@ -1615,10 +1656,10 @@
16151656 if (!buffer_verified(bh) &&
16161657 !is_dx_internal_node(dir, block,
16171658 (struct ext4_dir_entry *)bh->b_data) &&
1618
- !ext4_dirent_csum_verify(dir,
1619
- (struct ext4_dir_entry *)bh->b_data)) {
1620
- EXT4_ERROR_INODE(dir, "checksumming directory "
1621
- "block %lu", (unsigned long)block);
1659
+ !ext4_dirblock_csum_verify(dir, bh)) {
1660
+ EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
1661
+ "checksumming directory "
1662
+ "block %lu", (unsigned long)block);
16221663 brelse(bh);
16231664 ret = ERR_PTR(-EFSBADCRC);
16241665 goto cleanup_and_exit;
....@@ -1690,7 +1731,7 @@
16901731 struct buffer_head *bh;
16911732
16921733 err = ext4_fname_prepare_lookup(dir, dentry, &fname);
1693
- generic_set_encrypted_ci_d_ops(dir, dentry);
1734
+ generic_set_encrypted_ci_d_ops(dentry);
16941735 if (err == -ENOENT)
16951736 return NULL;
16961737 if (err)
....@@ -1768,7 +1809,7 @@
17681809
17691810 bh = ext4_lookup_entry(dir, dentry, &de);
17701811 if (IS_ERR(bh))
1771
- return (struct dentry *) bh;
1812
+ return ERR_CAST(bh);
17721813 inode = NULL;
17731814 if (bh) {
17741815 __u32 ino = le32_to_cpu(de->inode);
....@@ -1823,7 +1864,7 @@
18231864
18241865 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL, NULL);
18251866 if (IS_ERR(bh))
1826
- return (struct dentry *) bh;
1867
+ return ERR_CAST(bh);
18271868 if (!bh)
18281869 return ERR_PTR(-ENOENT);
18291870 ino = le32_to_cpu(de->inode);
....@@ -1900,14 +1941,14 @@
19001941 struct dx_hash_info *hinfo, ext4_lblk_t *newblock)
19011942 {
19021943 unsigned blocksize = dir->i_sb->s_blocksize;
1903
- unsigned count, continued;
1944
+ unsigned continued;
1945
+ int count;
19041946 struct buffer_head *bh2;
19051947 u32 hash2;
19061948 struct dx_map_entry *map;
19071949 char *data1 = (*bh)->b_data, *data2;
19081950 unsigned split, move, size;
19091951 struct ext4_dir_entry_2 *de = NULL, *de2;
1910
- struct ext4_dir_entry_tail *t;
19111952 int csum_size = 0;
19121953 int err = 0, i;
19131954
....@@ -1978,11 +2019,8 @@
19782019 (char *) de2,
19792020 blocksize);
19802021 if (csum_size) {
1981
- t = EXT4_DIRENT_TAIL(data2, blocksize);
1982
- initialize_dirent_tail(t, blocksize);
1983
-
1984
- t = EXT4_DIRENT_TAIL(data1, blocksize);
1985
- initialize_dirent_tail(t, blocksize);
2022
+ ext4_initialize_dirent_tail(*bh, blocksize);
2023
+ ext4_initialize_dirent_tail(bh2, blocksize);
19862024 }
19872025
19882026 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
....@@ -1996,7 +2034,7 @@
19962034 de = de2;
19972035 }
19982036 dx_insert_block(frame, hash2 + continued, *newblock);
1999
- err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
2037
+ err = ext4_handle_dirty_dirblock(handle, dir, bh2);
20002038 if (err)
20012039 goto journal_error;
20022040 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
....@@ -2097,7 +2135,7 @@
20972135 {
20982136 unsigned int blocksize = dir->i_sb->s_blocksize;
20992137 int csum_size = 0;
2100
- int err;
2138
+ int err, err2;
21012139
21022140 if (ext4_has_metadata_csum(inode->i_sb))
21032141 csum_size = sizeof(struct ext4_dir_entry_tail);
....@@ -2132,12 +2170,12 @@
21322170 dir->i_mtime = dir->i_ctime = current_time(dir);
21332171 ext4_update_dx_flag(dir);
21342172 inode_inc_iversion(dir);
2135
- ext4_mark_inode_dirty(handle, dir);
2173
+ err2 = ext4_mark_inode_dirty(handle, dir);
21362174 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2137
- err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2175
+ err = ext4_handle_dirty_dirblock(handle, dir, bh);
21382176 if (err)
21392177 ext4_std_error(dir->i_sb, err);
2140
- return 0;
2178
+ return err ? err : err2;
21412179 }
21422180
21432181 /*
....@@ -2153,8 +2191,7 @@
21532191 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
21542192 struct dx_entry *entries;
21552193 struct ext4_dir_entry_2 *de, *de2;
2156
- struct ext4_dir_entry_tail *t;
2157
- char *data1, *top;
2194
+ char *data2, *top;
21582195 unsigned len;
21592196 int retval;
21602197 unsigned blocksize;
....@@ -2194,21 +2231,26 @@
21942231 return PTR_ERR(bh2);
21952232 }
21962233 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
2197
- data1 = bh2->b_data;
2234
+ data2 = bh2->b_data;
21982235
2199
- memcpy (data1, de, len);
2200
- de = (struct ext4_dir_entry_2 *) data1;
2201
- top = data1 + len;
2202
- while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
2236
+ memcpy(data2, de, len);
2237
+ de = (struct ext4_dir_entry_2 *) data2;
2238
+ top = data2 + len;
2239
+ while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) {
2240
+ if (ext4_check_dir_entry(dir, NULL, de, bh2, data2, len, block,
2241
+ (data2 + (blocksize - csum_size) -
2242
+ (char *) de))) {
2243
+ brelse(bh2);
2244
+ brelse(bh);
2245
+ return -EFSCORRUPTED;
2246
+ }
22032247 de = de2;
2204
- de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
2205
- (char *) de,
2206
- blocksize);
2207
-
2208
- if (csum_size) {
2209
- t = EXT4_DIRENT_TAIL(data1, blocksize);
2210
- initialize_dirent_tail(t, blocksize);
22112248 }
2249
+ de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
2250
+ (char *) de, blocksize);
2251
+
2252
+ if (csum_size)
2253
+ ext4_initialize_dirent_tail(bh2, blocksize);
22122254
22132255 /* Initialize the root; the dot dirents already exist */
22142256 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
....@@ -2247,7 +2289,7 @@
22472289 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
22482290 if (retval)
22492291 goto out_frames;
2250
- retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
2292
+ retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
22512293 if (retval)
22522294 goto out_frames;
22532295
....@@ -2287,7 +2329,6 @@
22872329 struct inode *dir = d_inode(dentry->d_parent);
22882330 struct buffer_head *bh = NULL;
22892331 struct ext4_dir_entry_2 *de;
2290
- struct ext4_dir_entry_tail *t;
22912332 struct super_block *sb;
22922333 struct ext4_filename fname;
22932334 int retval;
....@@ -2304,14 +2345,14 @@
23042345 if (!dentry->d_name.len)
23052346 return -EINVAL;
23062347
2348
+ if (fscrypt_is_nokey_name(dentry))
2349
+ return -ENOKEY;
2350
+
23072351 #ifdef CONFIG_UNICODE
2308
- if (sb_has_enc_strict_mode(sb) && IS_CASEFOLDED(dir) &&
2352
+ if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
23092353 sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
23102354 return -EINVAL;
23112355 #endif
2312
-
2313
- if (fscrypt_is_nokey_name(dentry))
2314
- return -ENOKEY;
23152356
23162357 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
23172358 if (retval)
....@@ -2340,7 +2381,9 @@
23402381 }
23412382 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
23422383 dx_fallback++;
2343
- ext4_mark_inode_dirty(handle, dir);
2384
+ retval = ext4_mark_inode_dirty(handle, dir);
2385
+ if (unlikely(retval))
2386
+ goto out;
23442387 }
23452388 blocks = dir->i_size >> sb->s_blocksize_bits;
23462389 for (block = 0; block < blocks; block++) {
....@@ -2380,10 +2423,8 @@
23802423 de->inode = 0;
23812424 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
23822425
2383
- if (csum_size) {
2384
- t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
2385
- initialize_dirent_tail(t, blocksize);
2386
- }
2426
+ if (csum_size)
2427
+ ext4_initialize_dirent_tail(bh, blocksize);
23872428
23882429 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, block, bh);
23892430 out:
....@@ -2573,8 +2614,7 @@
25732614 * ext4_generic_delete_entry deletes a directory entry by merging it
25742615 * with the previous entry
25752616 */
2576
-int ext4_generic_delete_entry(handle_t *handle,
2577
- struct inode *dir,
2617
+int ext4_generic_delete_entry(struct inode *dir,
25782618 struct ext4_dir_entry_2 *de_del,
25792619 ext4_lblk_t lblk,
25802620 struct buffer_head *bh,
....@@ -2637,14 +2677,13 @@
26372677 if (unlikely(err))
26382678 goto out;
26392679
2640
- err = ext4_generic_delete_entry(handle, dir, de_del, lblk,
2641
- bh, bh->b_data,
2680
+ err = ext4_generic_delete_entry(dir, de_del, lblk, bh, bh->b_data,
26422681 dir->i_sb->s_blocksize, csum_size);
26432682 if (err)
26442683 goto out;
26452684
26462685 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2647
- err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2686
+ err = ext4_handle_dirty_dirblock(handle, dir, bh);
26482687 if (unlikely(err))
26492688 goto out;
26502689
....@@ -2666,7 +2705,7 @@
26662705 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
26672706 * on regular files) and to avoid creating huge/slow non-HTREE directories.
26682707 */
2669
-static void ext4_inc_count(handle_t *handle, struct inode *inode)
2708
+static void ext4_inc_count(struct inode *inode)
26702709 {
26712710 inc_nlink(inode);
26722711 if (is_dx(inode) &&
....@@ -2678,25 +2717,36 @@
26782717 * If a directory had nlink == 1, then we should let it be 1. This indicates
26792718 * directory has >EXT4_LINK_MAX subdirs.
26802719 */
2681
-static void ext4_dec_count(handle_t *handle, struct inode *inode)
2720
+static void ext4_dec_count(struct inode *inode)
26822721 {
26832722 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
26842723 drop_nlink(inode);
26852724 }
26862725
26872726
2727
+/*
2728
+ * Add non-directory inode to a directory. On success, the inode reference is
2729
+ * consumed by dentry is instantiation. This is also indicated by clearing of
2730
+ * *inodep pointer. On failure, the caller is responsible for dropping the
2731
+ * inode reference in the safe context.
2732
+ */
26882733 static int ext4_add_nondir(handle_t *handle,
2689
- struct dentry *dentry, struct inode *inode)
2734
+ struct dentry *dentry, struct inode **inodep)
26902735 {
2736
+ struct inode *dir = d_inode(dentry->d_parent);
2737
+ struct inode *inode = *inodep;
26912738 int err = ext4_add_entry(handle, dentry, inode);
26922739 if (!err) {
2693
- ext4_mark_inode_dirty(handle, inode);
2740
+ err = ext4_mark_inode_dirty(handle, inode);
2741
+ if (IS_DIRSYNC(dir))
2742
+ ext4_handle_sync(handle);
26942743 d_instantiate_new(dentry, inode);
2695
- return 0;
2744
+ *inodep = NULL;
2745
+ return err;
26962746 }
26972747 drop_nlink(inode);
2748
+ ext4_orphan_add(handle, inode);
26982749 unlock_new_inode(inode);
2699
- iput(inode);
27002750 return err;
27012751 }
27022752
....@@ -2730,12 +2780,14 @@
27302780 inode->i_op = &ext4_file_inode_operations;
27312781 inode->i_fop = &ext4_file_operations;
27322782 ext4_set_aops(inode);
2733
- err = ext4_add_nondir(handle, dentry, inode);
2734
- if (!err && IS_DIRSYNC(dir))
2735
- ext4_handle_sync(handle);
2783
+ err = ext4_add_nondir(handle, dentry, &inode);
2784
+ if (!err)
2785
+ ext4_fc_track_create(handle, dentry);
27362786 }
27372787 if (handle)
27382788 ext4_journal_stop(handle);
2789
+ if (!IS_ERR_OR_NULL(inode))
2790
+ iput(inode);
27392791 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
27402792 goto retry;
27412793 return err;
....@@ -2762,12 +2814,14 @@
27622814 if (!IS_ERR(inode)) {
27632815 init_special_inode(inode, inode->i_mode, rdev);
27642816 inode->i_op = &ext4_special_inode_operations;
2765
- err = ext4_add_nondir(handle, dentry, inode);
2766
- if (!err && IS_DIRSYNC(dir))
2767
- ext4_handle_sync(handle);
2817
+ err = ext4_add_nondir(handle, dentry, &inode);
2818
+ if (!err)
2819
+ ext4_fc_track_create(handle, dentry);
27682820 }
27692821 if (handle)
27702822 ext4_journal_stop(handle);
2823
+ if (!IS_ERR_OR_NULL(inode))
2824
+ iput(inode);
27712825 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
27722826 goto retry;
27732827 return err;
....@@ -2842,12 +2896,11 @@
28422896 return ext4_next_entry(de, blocksize);
28432897 }
28442898
2845
-static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2899
+int ext4_init_new_dir(handle_t *handle, struct inode *dir,
28462900 struct inode *inode)
28472901 {
28482902 struct buffer_head *dir_block = NULL;
28492903 struct ext4_dir_entry_2 *de;
2850
- struct ext4_dir_entry_tail *t;
28512904 ext4_lblk_t block = 0;
28522905 unsigned int blocksize = dir->i_sb->s_blocksize;
28532906 int csum_size = 0;
....@@ -2871,13 +2924,11 @@
28712924 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
28722925 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
28732926 set_nlink(inode, 2);
2874
- if (csum_size) {
2875
- t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2876
- initialize_dirent_tail(t, blocksize);
2877
- }
2927
+ if (csum_size)
2928
+ ext4_initialize_dirent_tail(dir_block, blocksize);
28782929
28792930 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2880
- err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2931
+ err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
28812932 if (err)
28822933 goto out;
28832934 set_buffer_verified(dir_block);
....@@ -2890,7 +2941,7 @@
28902941 {
28912942 handle_t *handle;
28922943 struct inode *inode;
2893
- int err, credits, retries = 0;
2944
+ int err, err2 = 0, credits, retries = 0;
28942945
28952946 if (EXT4_DIR_LINK_MAX(dir))
28962947 return -EMLINK;
....@@ -2921,23 +2972,30 @@
29212972 if (err) {
29222973 out_clear_inode:
29232974 clear_nlink(inode);
2975
+ ext4_orphan_add(handle, inode);
29242976 unlock_new_inode(inode);
2925
- ext4_mark_inode_dirty(handle, inode);
2977
+ err2 = ext4_mark_inode_dirty(handle, inode);
2978
+ if (unlikely(err2))
2979
+ err = err2;
2980
+ ext4_journal_stop(handle);
29262981 iput(inode);
2927
- goto out_stop;
2982
+ goto out_retry;
29282983 }
2929
- ext4_inc_count(handle, dir);
2984
+ ext4_inc_count(dir);
2985
+
29302986 ext4_update_dx_flag(dir);
29312987 err = ext4_mark_inode_dirty(handle, dir);
29322988 if (err)
29332989 goto out_clear_inode;
29342990 d_instantiate_new(dentry, inode);
2991
+ ext4_fc_track_create(handle, dentry);
29352992 if (IS_DIRSYNC(dir))
29362993 ext4_handle_sync(handle);
29372994
29382995 out_stop:
29392996 if (handle)
29402997 ext4_journal_stop(handle);
2998
+out_retry:
29412999 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
29423000 goto retry;
29433001 return err;
....@@ -2966,14 +3024,14 @@
29663024 if (inode->i_size < ext4_dir_rec_len(1, NULL) +
29673025 ext4_dir_rec_len(2, NULL)) {
29683026 EXT4_ERROR_INODE(inode, "invalid size");
2969
- return true;
3027
+ return false;
29703028 }
29713029 /* The first directory block must not be a hole,
29723030 * so treat it as DIRENT_HTREE
29733031 */
29743032 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
29753033 if (IS_ERR(bh))
2976
- return true;
3034
+ return false;
29773035
29783036 de = (struct ext4_dir_entry_2 *) bh->b_data;
29793037 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size, 0,
....@@ -2981,7 +3039,7 @@
29813039 le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
29823040 ext4_warning_inode(inode, "directory missing '.'");
29833041 brelse(bh);
2984
- return true;
3042
+ return false;
29853043 }
29863044 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
29873045 de = ext4_next_entry(de, sb->s_blocksize);
....@@ -2990,7 +3048,7 @@
29903048 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
29913049 ext4_warning_inode(inode, "directory missing '..'");
29923050 brelse(bh);
2993
- return true;
3051
+ return false;
29943052 }
29953053 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
29963054 while (offset < inode->i_size) {
....@@ -3004,16 +3062,13 @@
30043062 continue;
30053063 }
30063064 if (IS_ERR(bh))
3007
- return true;
3065
+ return false;
30083066 }
30093067 de = (struct ext4_dir_entry_2 *) (bh->b_data +
30103068 (offset & (sb->s_blocksize - 1)));
30113069 if (ext4_check_dir_entry(inode, NULL, de, bh,
3012
- bh->b_data, bh->b_size, 0, offset)) {
3013
- offset = (offset | (sb->s_blocksize - 1)) + 1;
3014
- continue;
3015
- }
3016
- if (le32_to_cpu(de->inode)) {
3070
+ bh->b_data, bh->b_size, 0, offset) ||
3071
+ le32_to_cpu(de->inode)) {
30173072 brelse(bh);
30183073 return false;
30193074 }
....@@ -3263,10 +3318,13 @@
32633318 inode->i_size = 0;
32643319 ext4_orphan_add(handle, inode);
32653320 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
3266
- ext4_mark_inode_dirty(handle, inode);
3267
- ext4_dec_count(handle, dir);
3321
+ retval = ext4_mark_inode_dirty(handle, inode);
3322
+ if (retval)
3323
+ goto end_rmdir;
3324
+ ext4_dec_count(dir);
32683325 ext4_update_dx_flag(dir);
3269
- ext4_mark_inode_dirty(handle, dir);
3326
+ ext4_fc_track_unlink(handle, dentry);
3327
+ retval = ext4_mark_inode_dirty(handle, dir);
32703328
32713329 #ifdef CONFIG_UNICODE
32723330 /* VFS negative dentries are incompatible with Encoding and
....@@ -3286,68 +3344,100 @@
32863344 return retval;
32873345 }
32883346
3289
-static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3347
+int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
3348
+ struct inode *inode,
3349
+ struct dentry *dentry /* NULL during fast_commit recovery */)
32903350 {
3291
- int retval;
3292
- struct inode *inode;
3351
+ int retval = -ENOENT;
32933352 struct buffer_head *bh;
32943353 struct ext4_dir_entry_2 *de;
3295
- handle_t *handle = NULL;
3354
+ handle_t *handle;
3355
+ int skip_remove_dentry = 0;
32963356 ext4_lblk_t lblk;
32973357
3298
- if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3299
- return -EIO;
3300
-
3301
- trace_ext4_unlink_enter(dir, dentry);
3302
- /* Initialize quotas before so that eventual writes go
3303
- * in separate transaction */
3304
- retval = dquot_initialize(dir);
3305
- if (retval)
3306
- return retval;
3307
- retval = dquot_initialize(d_inode(dentry));
3308
- if (retval)
3309
- return retval;
3310
-
3311
- retval = -ENOENT;
3312
- bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL, &lblk);
3358
+ /*
3359
+ * Keep this outside the transaction; it may have to set up the
3360
+ * directory's encryption key, which isn't GFP_NOFS-safe.
3361
+ */
3362
+ bh = ext4_find_entry(dir, d_name, &de, NULL, &lblk);
33133363 if (IS_ERR(bh))
33143364 return PTR_ERR(bh);
3365
+
33153366 if (!bh)
3316
- goto end_unlink;
3367
+ return -ENOENT;
33173368
3318
- inode = d_inode(dentry);
3319
-
3320
- retval = -EFSCORRUPTED;
3321
- if (le32_to_cpu(de->inode) != inode->i_ino)
3322
- goto end_unlink;
3369
+ if (le32_to_cpu(de->inode) != inode->i_ino) {
3370
+ /*
3371
+ * It's okay if we find dont find dentry which matches
3372
+ * the inode. That's because it might have gotten
3373
+ * renamed to a different inode number
3374
+ */
3375
+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
3376
+ skip_remove_dentry = 1;
3377
+ else
3378
+ goto out_bh;
3379
+ }
33233380
33243381 handle = ext4_journal_start(dir, EXT4_HT_DIR,
33253382 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
33263383 if (IS_ERR(handle)) {
33273384 retval = PTR_ERR(handle);
3328
- handle = NULL;
3329
- goto end_unlink;
3385
+ goto out_bh;
33303386 }
33313387
33323388 if (IS_DIRSYNC(dir))
33333389 ext4_handle_sync(handle);
33343390
3335
- retval = ext4_delete_entry(handle, dir, de, lblk, bh);
3336
- if (retval)
3337
- goto end_unlink;
3338
- dir->i_ctime = dir->i_mtime = current_time(dir);
3339
- ext4_update_dx_flag(dir);
3340
- ext4_mark_inode_dirty(handle, dir);
3391
+ if (!skip_remove_dentry) {
3392
+ retval = ext4_delete_entry(handle, dir, de, lblk, bh);
3393
+ if (retval)
3394
+ goto out_handle;
3395
+ dir->i_ctime = dir->i_mtime = current_time(dir);
3396
+ ext4_update_dx_flag(dir);
3397
+ retval = ext4_mark_inode_dirty(handle, dir);
3398
+ if (retval)
3399
+ goto out_handle;
3400
+ } else {
3401
+ retval = 0;
3402
+ }
33413403 if (inode->i_nlink == 0)
33423404 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
3343
- dentry->d_name.len, dentry->d_name.name);
3405
+ d_name->len, d_name->name);
33443406 else
33453407 drop_nlink(inode);
33463408 if (!inode->i_nlink)
33473409 ext4_orphan_add(handle, inode);
33483410 inode->i_ctime = current_time(inode);
3349
- ext4_mark_inode_dirty(handle, inode);
3411
+ retval = ext4_mark_inode_dirty(handle, inode);
3412
+ if (dentry && !retval)
3413
+ ext4_fc_track_unlink(handle, dentry);
3414
+out_handle:
3415
+ ext4_journal_stop(handle);
3416
+out_bh:
3417
+ brelse(bh);
3418
+ return retval;
3419
+}
33503420
3421
+static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3422
+{
3423
+ int retval;
3424
+
3425
+ if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3426
+ return -EIO;
3427
+
3428
+ trace_ext4_unlink_enter(dir, dentry);
3429
+ /*
3430
+ * Initialize quotas before so that eventual writes go
3431
+ * in separate transaction
3432
+ */
3433
+ retval = dquot_initialize(dir);
3434
+ if (retval)
3435
+ goto out_trace;
3436
+ retval = dquot_initialize(d_inode(dentry));
3437
+ if (retval)
3438
+ goto out_trace;
3439
+
3440
+ retval = __ext4_unlink(dir, &dentry->d_name, d_inode(dentry), dentry);
33513441 #ifdef CONFIG_UNICODE
33523442 /* VFS negative dentries are incompatible with Encoding and
33533443 * Case-insensitiveness. Eventually we'll want avoid
....@@ -3359,10 +3449,7 @@
33593449 d_invalidate(dentry);
33603450 #endif
33613451
3362
-end_unlink:
3363
- brelse(bh);
3364
- if (handle)
3365
- ext4_journal_stop(handle);
3452
+out_trace:
33663453 trace_ext4_unlink_exit(dentry, retval);
33673454 return retval;
33683455 }
....@@ -3442,7 +3529,8 @@
34423529 */
34433530 drop_nlink(inode);
34443531 err = ext4_orphan_add(handle, inode);
3445
- ext4_journal_stop(handle);
3532
+ if (handle)
3533
+ ext4_journal_stop(handle);
34463534 handle = NULL;
34473535 if (err)
34483536 goto err_drop_inode;
....@@ -3477,12 +3565,11 @@
34773565 inode->i_size = disk_link.len - 1;
34783566 }
34793567 EXT4_I(inode)->i_disksize = inode->i_size;
3480
- err = ext4_add_nondir(handle, dentry, inode);
3481
- if (!err && IS_DIRSYNC(dir))
3482
- ext4_handle_sync(handle);
3483
-
3568
+ err = ext4_add_nondir(handle, dentry, &inode);
34843569 if (handle)
34853570 ext4_journal_stop(handle);
3571
+ if (inode)
3572
+ iput(inode);
34863573 goto out_free_encrypted_link;
34873574
34883575 err_drop_inode:
....@@ -3497,12 +3584,49 @@
34973584 return err;
34983585 }
34993586
3587
+int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
3588
+{
3589
+ handle_t *handle;
3590
+ int err, retries = 0;
3591
+retry:
3592
+ handle = ext4_journal_start(dir, EXT4_HT_DIR,
3593
+ (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3594
+ EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
3595
+ if (IS_ERR(handle))
3596
+ return PTR_ERR(handle);
3597
+
3598
+ if (IS_DIRSYNC(dir))
3599
+ ext4_handle_sync(handle);
3600
+
3601
+ inode->i_ctime = current_time(inode);
3602
+ ext4_inc_count(inode);
3603
+ ihold(inode);
3604
+
3605
+ err = ext4_add_entry(handle, dentry, inode);
3606
+ if (!err) {
3607
+ err = ext4_mark_inode_dirty(handle, inode);
3608
+ /* this can happen only for tmpfile being
3609
+ * linked the first time
3610
+ */
3611
+ if (inode->i_nlink == 1)
3612
+ ext4_orphan_del(handle, inode);
3613
+ d_instantiate(dentry, inode);
3614
+ ext4_fc_track_link(handle, dentry);
3615
+ } else {
3616
+ drop_nlink(inode);
3617
+ iput(inode);
3618
+ }
3619
+ ext4_journal_stop(handle);
3620
+ if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
3621
+ goto retry;
3622
+ return err;
3623
+}
3624
+
35003625 static int ext4_link(struct dentry *old_dentry,
35013626 struct inode *dir, struct dentry *dentry)
35023627 {
3503
- handle_t *handle;
35043628 struct inode *inode = d_inode(old_dentry);
3505
- int err, retries = 0;
3629
+ int err;
35063630
35073631 if (inode->i_nlink >= EXT4_LINK_MAX)
35083632 return -EMLINK;
....@@ -3519,40 +3643,8 @@
35193643 err = dquot_initialize(dir);
35203644 if (err)
35213645 return err;
3522
-
3523
-retry:
3524
- handle = ext4_journal_start(dir, EXT4_HT_DIR,
3525
- (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3526
- EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
3527
- if (IS_ERR(handle))
3528
- return PTR_ERR(handle);
3529
-
3530
- if (IS_DIRSYNC(dir))
3531
- ext4_handle_sync(handle);
3532
-
3533
- inode->i_ctime = current_time(inode);
3534
- ext4_inc_count(handle, inode);
3535
- ihold(inode);
3536
-
3537
- err = ext4_add_entry(handle, dentry, inode);
3538
- if (!err) {
3539
- ext4_mark_inode_dirty(handle, inode);
3540
- /* this can happen only for tmpfile being
3541
- * linked the first time
3542
- */
3543
- if (inode->i_nlink == 1)
3544
- ext4_orphan_del(handle, inode);
3545
- d_instantiate(dentry, inode);
3546
- } else {
3547
- drop_nlink(inode);
3548
- iput(inode);
3549
- }
3550
- ext4_journal_stop(handle);
3551
- if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
3552
- goto retry;
3553
- return err;
3646
+ return __ext4_link(dir, inode, dentry);
35543647 }
3555
-
35563648
35573649 /*
35583650 * Try to find buffer head where contains the parent block.
....@@ -3568,6 +3660,9 @@
35683660 struct buffer_head *bh;
35693661
35703662 if (!ext4_has_inline_data(inode)) {
3663
+ struct ext4_dir_entry_2 *de;
3664
+ unsigned int offset;
3665
+
35713666 /* The first directory block must not be a hole, so
35723667 * treat it as DIRENT_HTREE
35733668 */
....@@ -3576,9 +3671,30 @@
35763671 *retval = PTR_ERR(bh);
35773672 return NULL;
35783673 }
3579
- *parent_de = ext4_next_entry(
3580
- (struct ext4_dir_entry_2 *)bh->b_data,
3581
- inode->i_sb->s_blocksize);
3674
+
3675
+ de = (struct ext4_dir_entry_2 *) bh->b_data;
3676
+ if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3677
+ bh->b_size, 0, 0) ||
3678
+ le32_to_cpu(de->inode) != inode->i_ino ||
3679
+ strcmp(".", de->name)) {
3680
+ EXT4_ERROR_INODE(inode, "directory missing '.'");
3681
+ brelse(bh);
3682
+ *retval = -EFSCORRUPTED;
3683
+ return NULL;
3684
+ }
3685
+ offset = ext4_rec_len_from_disk(de->rec_len,
3686
+ inode->i_sb->s_blocksize);
3687
+ de = ext4_next_entry(de, inode->i_sb->s_blocksize);
3688
+ if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3689
+ bh->b_size, 0, offset) ||
3690
+ le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
3691
+ EXT4_ERROR_INODE(inode, "directory missing '..'");
3692
+ brelse(bh);
3693
+ *retval = -EFSCORRUPTED;
3694
+ return NULL;
3695
+ }
3696
+ *parent_de = de;
3697
+
35823698 return bh;
35833699 }
35843700
....@@ -3633,9 +3749,8 @@
36333749 ent->inode,
36343750 ent->dir_bh);
36353751 } else {
3636
- retval = ext4_handle_dirty_dirent_node(handle,
3637
- ent->inode,
3638
- ent->dir_bh);
3752
+ retval = ext4_handle_dirty_dirblock(handle, ent->inode,
3753
+ ent->dir_bh);
36393754 }
36403755 } else {
36413756 retval = ext4_mark_inode_dirty(handle, ent->inode);
....@@ -3650,7 +3765,7 @@
36503765 static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
36513766 unsigned ino, unsigned file_type)
36523767 {
3653
- int retval;
3768
+ int retval, retval2;
36543769
36553770 BUFFER_TRACE(ent->bh, "get write access");
36563771 retval = ext4_journal_get_write_access(handle, ent->bh);
....@@ -3662,18 +3777,16 @@
36623777 inode_inc_iversion(ent->dir);
36633778 ent->dir->i_ctime = ent->dir->i_mtime =
36643779 current_time(ent->dir);
3665
- ext4_mark_inode_dirty(handle, ent->dir);
3780
+ retval = ext4_mark_inode_dirty(handle, ent->dir);
36663781 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
36673782 if (!ent->inlined) {
3668
- retval = ext4_handle_dirty_dirent_node(handle,
3669
- ent->dir, ent->bh);
3670
- if (unlikely(retval)) {
3671
- ext4_std_error(ent->dir->i_sb, retval);
3672
- return retval;
3783
+ retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
3784
+ if (unlikely(retval2)) {
3785
+ ext4_std_error(ent->dir->i_sb, retval2);
3786
+ return retval2;
36733787 }
36743788 }
3675
-
3676
- return 0;
3789
+ return retval;
36773790 }
36783791
36793792 static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
....@@ -3687,8 +3800,8 @@
36873800 * so the old->de may no longer valid and need to find it again
36883801 * before reset old inode info.
36893802 */
3690
- old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL,
3691
- NULL);
3803
+ old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
3804
+ &old.inlined, NULL);
36923805 if (IS_ERR(old.bh))
36933806 retval = PTR_ERR(old.bh);
36943807 if (!old.bh)
....@@ -3757,9 +3870,9 @@
37573870 {
37583871 if (ent->dir_nlink_delta) {
37593872 if (ent->dir_nlink_delta == -1)
3760
- ext4_dec_count(handle, ent->dir);
3873
+ ext4_dec_count(ent->dir);
37613874 else
3762
- ext4_inc_count(handle, ent->dir);
3875
+ ext4_inc_count(ent->dir);
37633876 ext4_mark_inode_dirty(handle, ent->dir);
37643877 }
37653878 }
....@@ -3840,6 +3953,9 @@
38403953 retval = dquot_initialize(old.dir);
38413954 if (retval)
38423955 return retval;
3956
+ retval = dquot_initialize(old.inode);
3957
+ if (retval)
3958
+ return retval;
38433959 retval = dquot_initialize(new.dir);
38443960 if (retval)
38453961 return retval;
....@@ -3852,10 +3968,11 @@
38523968 return retval;
38533969 }
38543970
3855
- old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL,
3856
- &old.lblk);
3971
+ old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
3972
+ &old.inlined, &old.lblk);
38573973 if (IS_ERR(old.bh))
38583974 return PTR_ERR(old.bh);
3975
+
38593976 /*
38603977 * Check for inode number is _not_ due to possible IO errors.
38613978 * We might rmdir the source, keep it as pwd of some process
....@@ -3935,7 +4052,10 @@
39354052 EXT4_FT_CHRDEV);
39364053 if (retval)
39374054 goto end_rename;
3938
- ext4_mark_inode_dirty(handle, whiteout);
4055
+ retval = ext4_mark_inode_dirty(handle, whiteout);
4056
+ if (unlikely(retval))
4057
+ goto end_rename;
4058
+
39394059 }
39404060 if (!new.bh) {
39414061 retval = ext4_add_entry(handle, new.dentry, old.inode);
....@@ -3956,7 +4076,9 @@
39564076 * rename.
39574077 */
39584078 old.inode->i_ctime = current_time(old.inode);
3959
- ext4_mark_inode_dirty(handle, old.inode);
4079
+ retval = ext4_mark_inode_dirty(handle, old.inode);
4080
+ if (unlikely(retval))
4081
+ goto end_rename;
39604082
39614083 if (!whiteout) {
39624084 /*
....@@ -3966,7 +4088,7 @@
39664088 }
39674089
39684090 if (new.inode) {
3969
- ext4_dec_count(handle, new.inode);
4091
+ ext4_dec_count(new.inode);
39704092 new.inode->i_ctime = current_time(new.inode);
39714093 }
39724094 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
....@@ -3976,21 +4098,45 @@
39764098 if (retval)
39774099 goto end_rename;
39784100
3979
- ext4_dec_count(handle, old.dir);
4101
+ ext4_dec_count(old.dir);
39804102 if (new.inode) {
39814103 /* checked ext4_empty_dir above, can't have another
39824104 * parent, ext4_dec_count() won't work for many-linked
39834105 * dirs */
39844106 clear_nlink(new.inode);
39854107 } else {
3986
- ext4_inc_count(handle, new.dir);
4108
+ ext4_inc_count(new.dir);
39874109 ext4_update_dx_flag(new.dir);
3988
- ext4_mark_inode_dirty(handle, new.dir);
4110
+ retval = ext4_mark_inode_dirty(handle, new.dir);
4111
+ if (unlikely(retval))
4112
+ goto end_rename;
39894113 }
39904114 }
3991
- ext4_mark_inode_dirty(handle, old.dir);
4115
+ retval = ext4_mark_inode_dirty(handle, old.dir);
4116
+ if (unlikely(retval))
4117
+ goto end_rename;
4118
+
4119
+ if (S_ISDIR(old.inode->i_mode)) {
4120
+ /*
4121
+ * We disable fast commits here that's because the
4122
+ * replay code is not yet capable of changing dot dot
4123
+ * dirents in directories.
4124
+ */
4125
+ ext4_fc_mark_ineligible(old.inode->i_sb,
4126
+ EXT4_FC_REASON_RENAME_DIR);
4127
+ } else {
4128
+ if (new.inode)
4129
+ ext4_fc_track_unlink(handle, new.dentry);
4130
+ __ext4_fc_track_link(handle, old.inode, new.dentry);
4131
+ __ext4_fc_track_unlink(handle, old.inode, old.dentry);
4132
+ if (whiteout)
4133
+ __ext4_fc_track_create(handle, whiteout, old.dentry);
4134
+ }
4135
+
39924136 if (new.inode) {
3993
- ext4_mark_inode_dirty(handle, new.inode);
4137
+ retval = ext4_mark_inode_dirty(handle, new.inode);
4138
+ if (unlikely(retval))
4139
+ goto end_rename;
39944140 if (!new.inode->i_nlink)
39954141 ext4_orphan_add(handle, new.inode);
39964142 }
....@@ -4014,6 +4160,7 @@
40144160 brelse(old.dir_bh);
40154161 brelse(old.bh);
40164162 brelse(new.bh);
4163
+
40174164 return retval;
40184165 }
40194166
....@@ -4130,9 +4277,14 @@
41304277 ctime = current_time(old.inode);
41314278 old.inode->i_ctime = ctime;
41324279 new.inode->i_ctime = ctime;
4133
- ext4_mark_inode_dirty(handle, old.inode);
4134
- ext4_mark_inode_dirty(handle, new.inode);
4135
-
4280
+ retval = ext4_mark_inode_dirty(handle, old.inode);
4281
+ if (unlikely(retval))
4282
+ goto end_rename;
4283
+ retval = ext4_mark_inode_dirty(handle, new.inode);
4284
+ if (unlikely(retval))
4285
+ goto end_rename;
4286
+ ext4_fc_mark_ineligible(new.inode->i_sb,
4287
+ EXT4_FC_REASON_CROSS_RENAME);
41364288 if (old.dir_bh) {
41374289 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
41384290 if (retval)