.. | .. |
---|
16 | 16 | #include "xattr.h" |
---|
17 | 17 | #include <trace/events/f2fs.h> |
---|
18 | 18 | |
---|
| 19 | +#ifdef CONFIG_UNICODE |
---|
| 20 | +extern struct kmem_cache *f2fs_cf_name_slab; |
---|
| 21 | +#endif |
---|
| 22 | + |
---|
19 | 23 | static unsigned long dir_blocks(struct inode *inode) |
---|
20 | 24 | { |
---|
21 | 25 | return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1)) |
---|
.. | .. |
---|
76 | 80 | struct f2fs_filename *fname) |
---|
77 | 81 | { |
---|
78 | 82 | #ifdef CONFIG_UNICODE |
---|
79 | | - struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
---|
| 83 | + struct super_block *sb = dir->i_sb; |
---|
80 | 84 | |
---|
81 | | - if (IS_CASEFOLDED(dir)) { |
---|
82 | | - fname->cf_name.name = f2fs_kmalloc(sbi, F2FS_NAME_LEN, |
---|
83 | | - GFP_NOFS); |
---|
| 85 | + if (IS_CASEFOLDED(dir) && |
---|
| 86 | + !is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) { |
---|
| 87 | + fname->cf_name.name = kmem_cache_alloc(f2fs_cf_name_slab, |
---|
| 88 | + GFP_NOFS); |
---|
84 | 89 | if (!fname->cf_name.name) |
---|
85 | 90 | return -ENOMEM; |
---|
86 | | - fname->cf_name.len = utf8_casefold(sbi->sb->s_encoding, |
---|
| 91 | + fname->cf_name.len = utf8_casefold(sb->s_encoding, |
---|
87 | 92 | fname->usr_fname, |
---|
88 | 93 | fname->cf_name.name, |
---|
89 | 94 | F2FS_NAME_LEN); |
---|
90 | 95 | if ((int)fname->cf_name.len <= 0) { |
---|
91 | | - kfree(fname->cf_name.name); |
---|
| 96 | + kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name); |
---|
92 | 97 | fname->cf_name.name = NULL; |
---|
93 | | - if (sb_has_enc_strict_mode(dir->i_sb)) |
---|
| 98 | + if (sb_has_strict_encoding(sb)) |
---|
94 | 99 | return -EINVAL; |
---|
95 | 100 | /* fall back to treating name as opaque byte sequence */ |
---|
96 | 101 | } |
---|
.. | .. |
---|
112 | 117 | #ifdef CONFIG_FS_ENCRYPTION |
---|
113 | 118 | fname->crypto_buf = crypt_name->crypto_buf; |
---|
114 | 119 | #endif |
---|
115 | | - if (crypt_name->is_ciphertext_name) { |
---|
| 120 | + if (crypt_name->is_nokey_name) { |
---|
116 | 121 | /* hash was decoded from the no-key name */ |
---|
117 | 122 | fname->hash = cpu_to_le32(crypt_name->hash); |
---|
118 | 123 | } else { |
---|
.. | .. |
---|
171 | 176 | fname->crypto_buf.name = NULL; |
---|
172 | 177 | #endif |
---|
173 | 178 | #ifdef CONFIG_UNICODE |
---|
174 | | - kfree(fname->cf_name.name); |
---|
175 | | - fname->cf_name.name = NULL; |
---|
| 179 | + if (fname->cf_name.name) { |
---|
| 180 | + kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name); |
---|
| 181 | + fname->cf_name.name = NULL; |
---|
| 182 | + } |
---|
176 | 183 | #endif |
---|
177 | 184 | } |
---|
178 | 185 | |
---|
.. | .. |
---|
191 | 198 | static struct f2fs_dir_entry *find_in_block(struct inode *dir, |
---|
192 | 199 | struct page *dentry_page, |
---|
193 | 200 | const struct f2fs_filename *fname, |
---|
194 | | - int *max_slots, |
---|
195 | | - struct page **res_page) |
---|
| 201 | + int *max_slots) |
---|
196 | 202 | { |
---|
197 | 203 | struct f2fs_dentry_block *dentry_blk; |
---|
198 | | - struct f2fs_dir_entry *de; |
---|
199 | 204 | struct f2fs_dentry_ptr d; |
---|
200 | 205 | |
---|
201 | 206 | dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page); |
---|
202 | 207 | |
---|
203 | 208 | make_dentry_ptr_block(dir, &d, dentry_blk); |
---|
204 | | - de = f2fs_find_target_dentry(&d, fname, max_slots); |
---|
205 | | - if (de) |
---|
206 | | - *res_page = dentry_page; |
---|
207 | | - |
---|
208 | | - return de; |
---|
| 209 | + return f2fs_find_target_dentry(&d, fname, max_slots); |
---|
209 | 210 | } |
---|
210 | 211 | |
---|
211 | 212 | #ifdef CONFIG_UNICODE |
---|
212 | 213 | /* |
---|
213 | 214 | * Test whether a case-insensitive directory entry matches the filename |
---|
214 | 215 | * being searched for. |
---|
| 216 | + * |
---|
| 217 | + * Returns 1 for a match, 0 for no match, and -errno on an error. |
---|
215 | 218 | */ |
---|
216 | | -static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name, |
---|
| 219 | +static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name, |
---|
217 | 220 | const u8 *de_name, u32 de_name_len) |
---|
218 | 221 | { |
---|
219 | 222 | const struct super_block *sb = dir->i_sb; |
---|
.. | .. |
---|
227 | 230 | FSTR_INIT((u8 *)de_name, de_name_len); |
---|
228 | 231 | |
---|
229 | 232 | if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir))) |
---|
230 | | - return false; |
---|
| 233 | + return -EINVAL; |
---|
231 | 234 | |
---|
232 | 235 | decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL); |
---|
233 | 236 | if (!decrypted_name.name) |
---|
234 | | - return false; |
---|
| 237 | + return -ENOMEM; |
---|
235 | 238 | res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name, |
---|
236 | 239 | &decrypted_name); |
---|
237 | 240 | if (res < 0) |
---|
.. | .. |
---|
241 | 244 | } |
---|
242 | 245 | |
---|
243 | 246 | res = utf8_strncasecmp_folded(um, name, &entry); |
---|
244 | | - if (res < 0) { |
---|
245 | | - /* |
---|
246 | | - * In strict mode, ignore invalid names. In non-strict mode, |
---|
247 | | - * fall back to treating them as opaque byte sequences. |
---|
248 | | - */ |
---|
249 | | - if (sb_has_enc_strict_mode(sb) || name->len != entry.len) |
---|
250 | | - res = 1; |
---|
251 | | - else |
---|
252 | | - res = memcmp(name->name, entry.name, name->len); |
---|
| 247 | + /* |
---|
| 248 | + * In strict mode, ignore invalid names. In non-strict mode, |
---|
| 249 | + * fall back to treating them as opaque byte sequences. |
---|
| 250 | + */ |
---|
| 251 | + if (res < 0 && !sb_has_strict_encoding(sb)) { |
---|
| 252 | + res = name->len == entry.len && |
---|
| 253 | + memcmp(name->name, entry.name, name->len) == 0; |
---|
| 254 | + } else { |
---|
| 255 | + /* utf8_strncasecmp_folded returns 0 on match */ |
---|
| 256 | + res = (res == 0); |
---|
253 | 257 | } |
---|
254 | 258 | out: |
---|
255 | 259 | kfree(decrypted_name.name); |
---|
256 | | - return res == 0; |
---|
| 260 | + return res; |
---|
257 | 261 | } |
---|
258 | 262 | #endif /* CONFIG_UNICODE */ |
---|
259 | 263 | |
---|
260 | | -static inline bool f2fs_match_name(const struct inode *dir, |
---|
| 264 | +static inline int f2fs_match_name(const struct inode *dir, |
---|
261 | 265 | const struct f2fs_filename *fname, |
---|
262 | 266 | const u8 *de_name, u32 de_name_len) |
---|
263 | 267 | { |
---|
.. | .. |
---|
284 | 288 | struct f2fs_dir_entry *de; |
---|
285 | 289 | unsigned long bit_pos = 0; |
---|
286 | 290 | int max_len = 0; |
---|
| 291 | + int res = 0; |
---|
287 | 292 | |
---|
288 | 293 | if (max_slots) |
---|
289 | 294 | *max_slots = 0; |
---|
.. | .. |
---|
301 | 306 | continue; |
---|
302 | 307 | } |
---|
303 | 308 | |
---|
304 | | - if (de->hash_code == fname->hash && |
---|
305 | | - f2fs_match_name(d->inode, fname, d->filename[bit_pos], |
---|
306 | | - le16_to_cpu(de->name_len))) |
---|
307 | | - goto found; |
---|
| 309 | + if (de->hash_code == fname->hash) { |
---|
| 310 | + res = f2fs_match_name(d->inode, fname, |
---|
| 311 | + d->filename[bit_pos], |
---|
| 312 | + le16_to_cpu(de->name_len)); |
---|
| 313 | + if (res < 0) |
---|
| 314 | + return ERR_PTR(res); |
---|
| 315 | + if (res) |
---|
| 316 | + goto found; |
---|
| 317 | + } |
---|
308 | 318 | |
---|
309 | 319 | if (max_slots && max_len > *max_slots) |
---|
310 | 320 | *max_slots = max_len; |
---|
.. | .. |
---|
353 | 363 | } |
---|
354 | 364 | } |
---|
355 | 365 | |
---|
356 | | - de = find_in_block(dir, dentry_page, fname, &max_slots, |
---|
357 | | - res_page); |
---|
358 | | - if (de) |
---|
| 366 | + de = find_in_block(dir, dentry_page, fname, &max_slots); |
---|
| 367 | + if (IS_ERR(de)) { |
---|
| 368 | + *res_page = ERR_CAST(de); |
---|
| 369 | + de = NULL; |
---|
359 | 370 | break; |
---|
| 371 | + } else if (de) { |
---|
| 372 | + *res_page = dentry_page; |
---|
| 373 | + break; |
---|
| 374 | + } |
---|
360 | 375 | |
---|
361 | 376 | if (max_slots >= s) |
---|
362 | 377 | room = true; |
---|
.. | .. |
---|
464 | 479 | struct page *page, struct inode *inode) |
---|
465 | 480 | { |
---|
466 | 481 | enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA; |
---|
| 482 | + |
---|
467 | 483 | lock_page(page); |
---|
468 | 484 | f2fs_wait_on_page_writeback(page, type, true, true); |
---|
469 | 485 | de->ino = cpu_to_le32(inode->i_ino); |
---|
.. | .. |
---|
580 | 596 | goto put_error; |
---|
581 | 597 | |
---|
582 | 598 | if (IS_ENCRYPTED(inode)) { |
---|
583 | | - err = fscrypt_inherit_context(dir, inode, page, false); |
---|
| 599 | + err = fscrypt_set_context(inode, page); |
---|
584 | 600 | if (err) |
---|
585 | 601 | goto put_error; |
---|
586 | 602 | } |
---|
.. | .. |
---|
753 | 769 | f2fs_wait_on_page_writeback(dentry_page, DATA, true, true); |
---|
754 | 770 | |
---|
755 | 771 | if (inode) { |
---|
756 | | - down_write(&F2FS_I(inode)->i_sem); |
---|
| 772 | + f2fs_down_write(&F2FS_I(inode)->i_sem); |
---|
757 | 773 | page = f2fs_init_inode_metadata(inode, dir, fname, NULL); |
---|
758 | 774 | if (IS_ERR(page)) { |
---|
759 | 775 | err = PTR_ERR(page); |
---|
.. | .. |
---|
780 | 796 | f2fs_update_parent_metadata(dir, inode, current_depth); |
---|
781 | 797 | fail: |
---|
782 | 798 | if (inode) |
---|
783 | | - up_write(&F2FS_I(inode)->i_sem); |
---|
| 799 | + f2fs_up_write(&F2FS_I(inode)->i_sem); |
---|
784 | 800 | |
---|
785 | 801 | f2fs_put_page(dentry_page, 1); |
---|
786 | 802 | |
---|
.. | .. |
---|
818 | 834 | return err; |
---|
819 | 835 | |
---|
820 | 836 | /* |
---|
821 | | - * An immature stakable filesystem shows a race condition between lookup |
---|
| 837 | + * An immature stackable filesystem shows a race condition between lookup |
---|
822 | 838 | * and create. If we have same task when doing lookup and create, it's |
---|
823 | 839 | * definitely fine as expected by VFS normally. Otherwise, let's just |
---|
824 | 840 | * verify on-disk dentry one more time, which guarantees filesystem |
---|
.. | .. |
---|
845 | 861 | struct page *page; |
---|
846 | 862 | int err = 0; |
---|
847 | 863 | |
---|
848 | | - down_write(&F2FS_I(inode)->i_sem); |
---|
| 864 | + f2fs_down_write(&F2FS_I(inode)->i_sem); |
---|
849 | 865 | page = f2fs_init_inode_metadata(inode, dir, NULL, NULL); |
---|
850 | 866 | if (IS_ERR(page)) { |
---|
851 | 867 | err = PTR_ERR(page); |
---|
.. | .. |
---|
856 | 872 | clear_inode_flag(inode, FI_NEW_INODE); |
---|
857 | 873 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
---|
858 | 874 | fail: |
---|
859 | | - up_write(&F2FS_I(inode)->i_sem); |
---|
| 875 | + f2fs_up_write(&F2FS_I(inode)->i_sem); |
---|
860 | 876 | return err; |
---|
861 | 877 | } |
---|
862 | 878 | |
---|
.. | .. |
---|
864 | 880 | { |
---|
865 | 881 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
---|
866 | 882 | |
---|
867 | | - down_write(&F2FS_I(inode)->i_sem); |
---|
| 883 | + f2fs_down_write(&F2FS_I(inode)->i_sem); |
---|
868 | 884 | |
---|
869 | 885 | if (S_ISDIR(inode->i_mode)) |
---|
870 | 886 | f2fs_i_links_write(dir, false); |
---|
.. | .. |
---|
875 | 891 | f2fs_i_links_write(inode, false); |
---|
876 | 892 | f2fs_i_size_write(inode, 0); |
---|
877 | 893 | } |
---|
878 | | - up_write(&F2FS_I(inode)->i_sem); |
---|
| 894 | + f2fs_up_write(&F2FS_I(inode)->i_sem); |
---|
879 | 895 | |
---|
880 | 896 | if (inode->i_nlink == 0) |
---|
881 | 897 | f2fs_add_orphan_inode(inode); |
---|
.. | .. |
---|
919 | 935 | |
---|
920 | 936 | if (bit_pos == NR_DENTRY_IN_BLOCK && |
---|
921 | 937 | !f2fs_truncate_hole(dir, page->index, page->index + 1)) { |
---|
922 | | - f2fs_clear_radix_tree_dirty_tag(page); |
---|
| 938 | + f2fs_clear_page_cache_dirty_tag(page); |
---|
923 | 939 | clear_page_dirty_for_io(page); |
---|
924 | | - f2fs_clear_page_private(page); |
---|
925 | 940 | ClearPageUptodate(page); |
---|
926 | | - clear_cold_data(page); |
---|
| 941 | + |
---|
| 942 | + clear_page_private_gcing(page); |
---|
| 943 | + |
---|
927 | 944 | inode_dec_dirty_pages(dir); |
---|
928 | 945 | f2fs_remove_dirty_inode(dir); |
---|
| 946 | + |
---|
| 947 | + detach_page_private(page); |
---|
| 948 | + set_page_private(page, 0); |
---|
929 | 949 | } |
---|
930 | 950 | f2fs_put_page(page, 1); |
---|
931 | 951 | |
---|
.. | .. |
---|
983 | 1003 | struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode); |
---|
984 | 1004 | struct blk_plug plug; |
---|
985 | 1005 | bool readdir_ra = sbi->readdir_ra == 1; |
---|
| 1006 | + bool found_valid_dirent = false; |
---|
986 | 1007 | int err = 0; |
---|
987 | 1008 | |
---|
988 | 1009 | bit_pos = ((unsigned long)ctx->pos % d->max); |
---|
.. | .. |
---|
997 | 1018 | |
---|
998 | 1019 | de = &d->dentry[bit_pos]; |
---|
999 | 1020 | if (de->name_len == 0) { |
---|
| 1021 | + if (found_valid_dirent || !bit_pos) { |
---|
| 1022 | + printk_ratelimited( |
---|
| 1023 | + "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.", |
---|
| 1024 | + KERN_WARNING, sbi->sb->s_id, |
---|
| 1025 | + le32_to_cpu(de->ino)); |
---|
| 1026 | + set_sbi_flag(sbi, SBI_NEED_FSCK); |
---|
| 1027 | + } |
---|
1000 | 1028 | bit_pos++; |
---|
1001 | 1029 | ctx->pos = start_pos + bit_pos; |
---|
1002 | | - printk_ratelimited( |
---|
1003 | | - "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.", |
---|
1004 | | - KERN_WARNING, sbi->sb->s_id, |
---|
1005 | | - le32_to_cpu(de->ino)); |
---|
1006 | | - set_sbi_flag(sbi, SBI_NEED_FSCK); |
---|
1007 | 1030 | continue; |
---|
1008 | 1031 | } |
---|
1009 | 1032 | |
---|
.. | .. |
---|
1046 | 1069 | f2fs_ra_node_page(sbi, le32_to_cpu(de->ino)); |
---|
1047 | 1070 | |
---|
1048 | 1071 | ctx->pos = start_pos + bit_pos; |
---|
| 1072 | + found_valid_dirent = true; |
---|
1049 | 1073 | } |
---|
1050 | 1074 | out: |
---|
1051 | 1075 | if (readdir_ra) |
---|
.. | .. |
---|
1067 | 1091 | int err = 0; |
---|
1068 | 1092 | |
---|
1069 | 1093 | if (IS_ENCRYPTED(inode)) { |
---|
1070 | | - err = fscrypt_get_encryption_info(inode); |
---|
| 1094 | + err = fscrypt_prepare_readdir(inode); |
---|
1071 | 1095 | if (err) |
---|
1072 | 1096 | goto out; |
---|
1073 | 1097 | |
---|
1074 | | - err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr); |
---|
| 1098 | + err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr); |
---|
1075 | 1099 | if (err < 0) |
---|
1076 | 1100 | goto out; |
---|
1077 | 1101 | } |
---|
.. | .. |
---|
1126 | 1150 | return err < 0 ? err : 0; |
---|
1127 | 1151 | } |
---|
1128 | 1152 | |
---|
1129 | | -static int f2fs_dir_open(struct inode *inode, struct file *filp) |
---|
1130 | | -{ |
---|
1131 | | - if (IS_ENCRYPTED(inode)) |
---|
1132 | | - return fscrypt_get_encryption_info(inode) ? -EACCES : 0; |
---|
1133 | | - return 0; |
---|
1134 | | -} |
---|
1135 | | - |
---|
1136 | 1153 | const struct file_operations f2fs_dir_operations = { |
---|
1137 | 1154 | .llseek = generic_file_llseek, |
---|
1138 | 1155 | .read = generic_read_dir, |
---|
1139 | 1156 | .iterate_shared = f2fs_readdir, |
---|
1140 | 1157 | .fsync = f2fs_sync_file, |
---|
1141 | | - .open = f2fs_dir_open, |
---|
1142 | 1158 | .unlocked_ioctl = f2fs_ioctl, |
---|
1143 | 1159 | #ifdef CONFIG_COMPAT |
---|
1144 | 1160 | .compat_ioctl = f2fs_compat_ioctl, |
---|