Commit | Line | Data |
---|---|---|
ca061973 NJ |
1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* | |
3 | * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. | |
4 | */ | |
5 | ||
6 | #include <linux/slab.h> | |
654762df | 7 | #include <linux/compat.h> |
ca061973 NJ |
8 | #include <linux/bio.h> |
9 | #include <linux/buffer_head.h> | |
10 | ||
11 | #include "exfat_raw.h" | |
12 | #include "exfat_fs.h" | |
13 | ||
14 | static int exfat_extract_uni_name(struct exfat_dentry *ep, | |
15 | unsigned short *uniname) | |
16 | { | |
17 | int i, len = 0; | |
18 | ||
19 | for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { | |
20 | *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]); | |
21 | if (*uniname == 0x0) | |
22 | return len; | |
23 | uniname++; | |
24 | len++; | |
25 | } | |
26 | ||
27 | *uniname = 0x0; | |
28 | return len; | |
29 | ||
30 | } | |
31 | ||
8258ef28 | 32 | static int exfat_get_uniname_from_ext_entry(struct super_block *sb, |
ca061973 NJ |
33 | struct exfat_chain *p_dir, int entry, unsigned short *uniname) |
34 | { | |
8258ef28 | 35 | int i, err; |
20914ff6 | 36 | struct exfat_entry_set_cache es; |
d4233457 | 37 | unsigned int uni_len = 0, len; |
ca061973 | 38 | |
8258ef28 NJ |
39 | err = exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES); |
40 | if (err) | |
41 | return err; | |
ca061973 | 42 | |
ca061973 NJ |
43 | /* |
44 | * First entry : file entry | |
45 | * Second entry : stream-extension entry | |
46 | * Third entry : first file-name entry | |
47 | * So, the index of first file-name dentry should start from 2. | |
48 | */ | |
f3fe3954 | 49 | for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) { |
20914ff6 | 50 | struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i); |
943af1fd | 51 | |
ca061973 NJ |
52 | /* end of name entry */ |
53 | if (exfat_get_entry_type(ep) != TYPE_EXTEND) | |
943af1fd | 54 | break; |
ca061973 | 55 | |
d4233457 NJ |
56 | len = exfat_extract_uni_name(ep, uniname); |
57 | uni_len += len; | |
58 | if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH) | |
59 | break; | |
ca061973 NJ |
60 | uniname += EXFAT_FILE_NAME_LEN; |
61 | } | |
62 | ||
3b9681ac | 63 | exfat_put_dentry_set(&es, false); |
8258ef28 | 64 | return 0; |
ca061973 NJ |
65 | } |
66 | ||
67 | /* read a directory entry from the opened directory */ | |
04cee52f | 68 | static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry) |
ca061973 | 69 | { |
8258ef28 | 70 | int i, dentries_per_clu, num_ext, err; |
1e5654de | 71 | unsigned int type, clu_offset, max_dentries; |
ca061973 NJ |
72 | struct exfat_chain dir, clu; |
73 | struct exfat_uni_name uni_name; | |
74 | struct exfat_dentry *ep; | |
75 | struct super_block *sb = inode->i_sb; | |
76 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
77 | struct exfat_inode_info *ei = EXFAT_I(inode); | |
04cee52f | 78 | unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF; |
ca061973 NJ |
79 | struct buffer_head *bh; |
80 | ||
81 | /* check if the given file ID is opened */ | |
82 | if (ei->type != TYPE_DIR) | |
83 | return -EPERM; | |
84 | ||
85 | if (ei->entry == -1) | |
86 | exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); | |
87 | else | |
88 | exfat_chain_set(&dir, ei->start_clu, | |
89 | EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); | |
90 | ||
91 | dentries_per_clu = sbi->dentries_per_clu; | |
1e5654de | 92 | max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES, |
088f1343 | 93 | (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi)); |
ca061973 | 94 | |
088f1343 | 95 | clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi); |
ca061973 NJ |
96 | exfat_chain_dup(&clu, &dir); |
97 | ||
98 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { | |
99 | clu.dir += clu_offset; | |
100 | clu.size -= clu_offset; | |
101 | } else { | |
102 | /* hint_information */ | |
103 | if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER && | |
104 | ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) { | |
105 | clu_offset -= ei->hint_bmap.off; | |
106 | clu.dir = ei->hint_bmap.clu; | |
107 | } | |
108 | ||
706fdcac | 109 | while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) { |
ca061973 NJ |
110 | if (exfat_get_next_cluster(sb, &(clu.dir))) |
111 | return -EIO; | |
112 | ||
113 | clu_offset--; | |
114 | } | |
115 | } | |
116 | ||
1e5654de | 117 | while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) { |
ca061973 NJ |
118 | i = dentry & (dentries_per_clu - 1); |
119 | ||
120 | for ( ; i < dentries_per_clu; i++, dentry++) { | |
c71510b3 | 121 | ep = exfat_get_dentry(sb, &clu, i, &bh); |
ca061973 NJ |
122 | if (!ep) |
123 | return -EIO; | |
124 | ||
125 | type = exfat_get_entry_type(ep); | |
126 | if (type == TYPE_UNUSED) { | |
127 | brelse(bh); | |
128 | break; | |
129 | } | |
130 | ||
131 | if (type != TYPE_FILE && type != TYPE_DIR) { | |
132 | brelse(bh); | |
133 | continue; | |
134 | } | |
135 | ||
04cee52f | 136 | num_ext = ep->dentry.file.num_ext; |
ca061973 NJ |
137 | dir_entry->attr = le16_to_cpu(ep->dentry.file.attr); |
138 | exfat_get_entry_time(sbi, &dir_entry->crtime, | |
139 | ep->dentry.file.create_tz, | |
140 | ep->dentry.file.create_time, | |
141 | ep->dentry.file.create_date, | |
ed0f84d3 | 142 | ep->dentry.file.create_time_cs); |
ca061973 NJ |
143 | exfat_get_entry_time(sbi, &dir_entry->mtime, |
144 | ep->dentry.file.modify_tz, | |
145 | ep->dentry.file.modify_time, | |
146 | ep->dentry.file.modify_date, | |
ed0f84d3 | 147 | ep->dentry.file.modify_time_cs); |
ca061973 NJ |
148 | exfat_get_entry_time(sbi, &dir_entry->atime, |
149 | ep->dentry.file.access_tz, | |
150 | ep->dentry.file.access_time, | |
151 | ep->dentry.file.access_date, | |
152 | 0); | |
153 | ||
154 | *uni_name.name = 0x0; | |
8258ef28 | 155 | err = exfat_get_uniname_from_ext_entry(sb, &clu, i, |
ca061973 | 156 | uni_name.name); |
8258ef28 NJ |
157 | if (err) { |
158 | brelse(bh); | |
159 | continue; | |
160 | } | |
ca061973 NJ |
161 | exfat_utf16_to_nls(sb, &uni_name, |
162 | dir_entry->namebuf.lfn, | |
163 | dir_entry->namebuf.lfnbuf_len); | |
164 | brelse(bh); | |
165 | ||
c71510b3 | 166 | ep = exfat_get_dentry(sb, &clu, i + 1, &bh); |
ca061973 NJ |
167 | if (!ep) |
168 | return -EIO; | |
169 | dir_entry->size = | |
170 | le64_to_cpu(ep->dentry.stream.valid_size); | |
04cee52f | 171 | dir_entry->entry = dentry; |
ca061973 NJ |
172 | brelse(bh); |
173 | ||
088f1343 | 174 | ei->hint_bmap.off = EXFAT_DEN_TO_CLU(dentry, sbi); |
ca061973 NJ |
175 | ei->hint_bmap.clu = clu.dir; |
176 | ||
04cee52f | 177 | *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext); |
ca061973 NJ |
178 | return 0; |
179 | } | |
180 | ||
181 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { | |
182 | if (--clu.size > 0) | |
183 | clu.dir++; | |
184 | else | |
185 | clu.dir = EXFAT_EOF_CLUSTER; | |
186 | } else { | |
187 | if (exfat_get_next_cluster(sb, &(clu.dir))) | |
188 | return -EIO; | |
189 | } | |
190 | } | |
191 | ||
192 | dir_entry->namebuf.lfn[0] = '\0'; | |
04cee52f | 193 | *cpos = EXFAT_DEN_TO_B(dentry); |
ca061973 NJ |
194 | return 0; |
195 | } | |
196 | ||
197 | static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb) | |
198 | { | |
199 | nb->lfn = NULL; | |
200 | nb->lfnbuf_len = 0; | |
201 | } | |
202 | ||
203 | static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb) | |
204 | { | |
205 | nb->lfn = __getname(); | |
206 | if (!nb->lfn) | |
207 | return -ENOMEM; | |
208 | nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE; | |
209 | return 0; | |
210 | } | |
211 | ||
212 | static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb) | |
213 | { | |
214 | if (!nb->lfn) | |
215 | return; | |
216 | ||
217 | __putname(nb->lfn); | |
218 | exfat_init_namebuf(nb); | |
219 | } | |
220 | ||
ff84772f SS |
221 | /* |
222 | * Before calling dir_emit*(), sbi->s_lock should be released | |
223 | * because page fault can occur in dir_emit*(). | |
224 | */ | |
ca061973 | 225 | #define ITER_POS_FILLED_DOTS (2) |
703e3e9a | 226 | static int exfat_iterate(struct file *file, struct dir_context *ctx) |
ca061973 | 227 | { |
703e3e9a | 228 | struct inode *inode = file_inode(file); |
ca061973 NJ |
229 | struct super_block *sb = inode->i_sb; |
230 | struct inode *tmp; | |
231 | struct exfat_dir_entry de; | |
232 | struct exfat_dentry_namebuf *nb = &(de.namebuf); | |
233 | struct exfat_inode_info *ei = EXFAT_I(inode); | |
234 | unsigned long inum; | |
235 | loff_t cpos, i_pos; | |
236 | int err = 0, fake_offset = 0; | |
237 | ||
238 | exfat_init_namebuf(nb); | |
ca061973 NJ |
239 | |
240 | cpos = ctx->pos; | |
703e3e9a | 241 | if (!dir_emit_dots(file, ctx)) |
ff84772f | 242 | goto out; |
ca061973 NJ |
243 | |
244 | if (ctx->pos == ITER_POS_FILLED_DOTS) { | |
245 | cpos = 0; | |
246 | fake_offset = 1; | |
247 | } | |
248 | ||
6cb5d1a1 | 249 | cpos = round_up(cpos, DENTRY_SIZE); |
ca061973 NJ |
250 | |
251 | /* name buffer should be allocated before use */ | |
252 | err = exfat_alloc_namebuf(nb); | |
253 | if (err) | |
ff84772f | 254 | goto out; |
ca061973 | 255 | get_new: |
ff84772f SS |
256 | mutex_lock(&EXFAT_SB(sb)->s_lock); |
257 | ||
1e5654de | 258 | if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode)) |
ca061973 NJ |
259 | goto end_of_dir; |
260 | ||
04cee52f | 261 | err = exfat_readdir(inode, &cpos, &de); |
ca061973 NJ |
262 | if (err) { |
263 | /* | |
ff84772f SS |
264 | * At least we tried to read a sector. |
265 | * Move cpos to next sector position (should be aligned). | |
ca061973 NJ |
266 | */ |
267 | if (err == -EIO) { | |
268 | cpos += 1 << (sb->s_blocksize_bits); | |
269 | cpos &= ~(sb->s_blocksize - 1); | |
270 | } | |
271 | ||
272 | err = -EIO; | |
273 | goto end_of_dir; | |
274 | } | |
275 | ||
ca061973 NJ |
276 | if (!nb->lfn[0]) |
277 | goto end_of_dir; | |
278 | ||
04cee52f | 279 | i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff); |
ca061973 NJ |
280 | tmp = exfat_iget(sb, i_pos); |
281 | if (tmp) { | |
282 | inum = tmp->i_ino; | |
283 | iput(tmp); | |
284 | } else { | |
285 | inum = iunique(sb, EXFAT_ROOT_INO); | |
286 | } | |
287 | ||
ca061973 NJ |
288 | mutex_unlock(&EXFAT_SB(sb)->s_lock); |
289 | if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum, | |
0ab8ba71 | 290 | (de.attr & EXFAT_ATTR_SUBDIR) ? DT_DIR : DT_REG)) |
ff84772f | 291 | goto out; |
ca061973 NJ |
292 | ctx->pos = cpos; |
293 | goto get_new; | |
294 | ||
295 | end_of_dir: | |
296 | if (!cpos && fake_offset) | |
297 | cpos = ITER_POS_FILLED_DOTS; | |
298 | ctx->pos = cpos; | |
ca061973 | 299 | mutex_unlock(&EXFAT_SB(sb)->s_lock); |
ff84772f | 300 | out: |
ca061973 NJ |
301 | /* |
302 | * To improve performance, free namebuf after unlock sb_lock. | |
303 | * If namebuf is not allocated, this function do nothing | |
304 | */ | |
305 | exfat_free_namebuf(nb); | |
306 | return err; | |
307 | } | |
308 | ||
3e327154 | 309 | WRAP_DIR_ITER(exfat_iterate) // FIXME! |
ca061973 NJ |
310 | const struct file_operations exfat_dir_operations = { |
311 | .llseek = generic_file_llseek, | |
312 | .read = generic_read_dir, | |
3e327154 | 313 | .iterate_shared = shared_exfat_iterate, |
654762df HK |
314 | .unlocked_ioctl = exfat_ioctl, |
315 | #ifdef CONFIG_COMPAT | |
316 | .compat_ioctl = exfat_compat_ioctl, | |
317 | #endif | |
5267456e | 318 | .fsync = exfat_file_fsync, |
ca061973 NJ |
319 | }; |
320 | ||
321 | int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu) | |
322 | { | |
323 | int ret; | |
324 | ||
325 | exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN); | |
326 | ||
23befe49 | 327 | ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode)); |
ca061973 NJ |
328 | if (ret) |
329 | return ret; | |
330 | ||
331 | return exfat_zeroed_cluster(inode, clu->dir); | |
332 | } | |
333 | ||
334 | int exfat_calc_num_entries(struct exfat_uni_name *p_uniname) | |
335 | { | |
336 | int len; | |
337 | ||
338 | len = p_uniname->name_len; | |
339 | if (len == 0) | |
340 | return -EINVAL; | |
341 | ||
342 | /* 1 file entry + 1 stream entry + name entries */ | |
f3fe3954 | 343 | return ES_ENTRY_NUM(len); |
ca061973 NJ |
344 | } |
345 | ||
346 | unsigned int exfat_get_entry_type(struct exfat_dentry *ep) | |
347 | { | |
348 | if (ep->type == EXFAT_UNUSED) | |
349 | return TYPE_UNUSED; | |
350 | if (IS_EXFAT_DELETED(ep->type)) | |
351 | return TYPE_DELETED; | |
352 | if (ep->type == EXFAT_INVAL) | |
353 | return TYPE_INVALID; | |
354 | if (IS_EXFAT_CRITICAL_PRI(ep->type)) { | |
355 | if (ep->type == EXFAT_BITMAP) | |
356 | return TYPE_BITMAP; | |
357 | if (ep->type == EXFAT_UPCASE) | |
358 | return TYPE_UPCASE; | |
359 | if (ep->type == EXFAT_VOLUME) | |
360 | return TYPE_VOLUME; | |
361 | if (ep->type == EXFAT_FILE) { | |
0ab8ba71 | 362 | if (le16_to_cpu(ep->dentry.file.attr) & EXFAT_ATTR_SUBDIR) |
ca061973 NJ |
363 | return TYPE_DIR; |
364 | return TYPE_FILE; | |
365 | } | |
366 | return TYPE_CRITICAL_PRI; | |
367 | } | |
368 | if (IS_EXFAT_BENIGN_PRI(ep->type)) { | |
369 | if (ep->type == EXFAT_GUID) | |
370 | return TYPE_GUID; | |
371 | if (ep->type == EXFAT_PADDING) | |
372 | return TYPE_PADDING; | |
373 | if (ep->type == EXFAT_ACLTAB) | |
374 | return TYPE_ACLTAB; | |
375 | return TYPE_BENIGN_PRI; | |
376 | } | |
377 | if (IS_EXFAT_CRITICAL_SEC(ep->type)) { | |
378 | if (ep->type == EXFAT_STREAM) | |
379 | return TYPE_STREAM; | |
380 | if (ep->type == EXFAT_NAME) | |
381 | return TYPE_EXTEND; | |
382 | if (ep->type == EXFAT_ACL) | |
383 | return TYPE_ACL; | |
384 | return TYPE_CRITICAL_SEC; | |
385 | } | |
8258ef28 NJ |
386 | |
387 | if (ep->type == EXFAT_VENDOR_EXT) | |
388 | return TYPE_VENDOR_EXT; | |
389 | if (ep->type == EXFAT_VENDOR_ALLOC) | |
390 | return TYPE_VENDOR_ALLOC; | |
391 | ||
ca061973 NJ |
392 | return TYPE_BENIGN_SEC; |
393 | } | |
394 | ||
395 | static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type) | |
396 | { | |
397 | if (type == TYPE_UNUSED) { | |
398 | ep->type = EXFAT_UNUSED; | |
399 | } else if (type == TYPE_DELETED) { | |
400 | ep->type &= EXFAT_DELETE; | |
401 | } else if (type == TYPE_STREAM) { | |
402 | ep->type = EXFAT_STREAM; | |
403 | } else if (type == TYPE_EXTEND) { | |
404 | ep->type = EXFAT_NAME; | |
405 | } else if (type == TYPE_BITMAP) { | |
406 | ep->type = EXFAT_BITMAP; | |
407 | } else if (type == TYPE_UPCASE) { | |
408 | ep->type = EXFAT_UPCASE; | |
409 | } else if (type == TYPE_VOLUME) { | |
410 | ep->type = EXFAT_VOLUME; | |
411 | } else if (type == TYPE_DIR) { | |
412 | ep->type = EXFAT_FILE; | |
0ab8ba71 | 413 | ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_SUBDIR); |
ca061973 NJ |
414 | } else if (type == TYPE_FILE) { |
415 | ep->type = EXFAT_FILE; | |
0ab8ba71 | 416 | ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE); |
ca061973 NJ |
417 | } |
418 | } | |
419 | ||
420 | static void exfat_init_stream_entry(struct exfat_dentry *ep, | |
ee785c15 | 421 | unsigned int start_clu, unsigned long long size) |
ca061973 NJ |
422 | { |
423 | exfat_set_entry_type(ep, TYPE_STREAM); | |
ee785c15 YM |
424 | if (size == 0) |
425 | ep->dentry.stream.flags = ALLOC_FAT_CHAIN; | |
426 | else | |
427 | ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN; | |
ca061973 NJ |
428 | ep->dentry.stream.start_clu = cpu_to_le32(start_clu); |
429 | ep->dentry.stream.valid_size = cpu_to_le64(size); | |
430 | ep->dentry.stream.size = cpu_to_le64(size); | |
431 | } | |
432 | ||
433 | static void exfat_init_name_entry(struct exfat_dentry *ep, | |
434 | unsigned short *uniname) | |
435 | { | |
436 | int i; | |
437 | ||
438 | exfat_set_entry_type(ep, TYPE_EXTEND); | |
439 | ep->dentry.name.flags = 0x0; | |
440 | ||
441 | for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { | |
4ba6ccd6 HK |
442 | if (*uniname != 0x0) { |
443 | ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname); | |
444 | uniname++; | |
445 | } else { | |
446 | ep->dentry.name.unicode_0_14[i] = 0x0; | |
447 | } | |
ca061973 NJ |
448 | } |
449 | } | |
450 | ||
cf8663fa YM |
451 | void exfat_init_dir_entry(struct exfat_entry_set_cache *es, |
452 | unsigned int type, unsigned int start_clu, | |
453 | unsigned long long size, struct timespec64 *ts) | |
ca061973 | 454 | { |
cf8663fa | 455 | struct super_block *sb = es->sb; |
ca061973 | 456 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
ca061973 | 457 | struct exfat_dentry *ep; |
ca061973 | 458 | |
cf8663fa | 459 | ep = exfat_get_dentry_cached(es, ES_IDX_FILE); |
ca061973 | 460 | exfat_set_entry_type(ep, type); |
cf8663fa | 461 | exfat_set_entry_time(sbi, ts, |
ca061973 NJ |
462 | &ep->dentry.file.create_tz, |
463 | &ep->dentry.file.create_time, | |
464 | &ep->dentry.file.create_date, | |
ed0f84d3 | 465 | &ep->dentry.file.create_time_cs); |
cf8663fa | 466 | exfat_set_entry_time(sbi, ts, |
ca061973 NJ |
467 | &ep->dentry.file.modify_tz, |
468 | &ep->dentry.file.modify_time, | |
469 | &ep->dentry.file.modify_date, | |
ed0f84d3 | 470 | &ep->dentry.file.modify_time_cs); |
cf8663fa | 471 | exfat_set_entry_time(sbi, ts, |
ca061973 NJ |
472 | &ep->dentry.file.access_tz, |
473 | &ep->dentry.file.access_time, | |
474 | &ep->dentry.file.access_date, | |
475 | NULL); | |
476 | ||
cf8663fa | 477 | ep = exfat_get_dentry_cached(es, ES_IDX_STREAM); |
ee785c15 | 478 | exfat_init_stream_entry(ep, start_clu, size); |
ca061973 NJ |
479 | } |
480 | ||
481 | int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir, | |
482 | int entry) | |
483 | { | |
484 | struct super_block *sb = inode->i_sb; | |
485 | int ret = 0; | |
486 | int i, num_entries; | |
5875bf28 | 487 | u16 chksum; |
ca061973 NJ |
488 | struct exfat_dentry *ep, *fep; |
489 | struct buffer_head *fbh, *bh; | |
490 | ||
c71510b3 | 491 | fep = exfat_get_dentry(sb, p_dir, entry, &fbh); |
ca061973 NJ |
492 | if (!fep) |
493 | return -EIO; | |
494 | ||
495 | num_entries = fep->dentry.file.num_ext + 1; | |
5875bf28 | 496 | chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY); |
ca061973 NJ |
497 | |
498 | for (i = 1; i < num_entries; i++) { | |
c71510b3 | 499 | ep = exfat_get_dentry(sb, p_dir, entry + i, &bh); |
ca061973 NJ |
500 | if (!ep) { |
501 | ret = -EIO; | |
502 | goto release_fbh; | |
503 | } | |
5875bf28 | 504 | chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum, |
ca061973 NJ |
505 | CS_DEFAULT); |
506 | brelse(bh); | |
507 | } | |
508 | ||
509 | fep->dentry.file.checksum = cpu_to_le16(chksum); | |
2c7f8937 | 510 | exfat_update_bh(fbh, IS_DIRSYNC(inode)); |
ca061973 NJ |
511 | release_fbh: |
512 | brelse(fbh); | |
513 | return ret; | |
514 | } | |
515 | ||
8258ef28 NJ |
516 | static void exfat_free_benign_secondary_clusters(struct inode *inode, |
517 | struct exfat_dentry *ep) | |
518 | { | |
519 | struct super_block *sb = inode->i_sb; | |
520 | struct exfat_chain dir; | |
521 | unsigned int start_clu = | |
522 | le32_to_cpu(ep->dentry.generic_secondary.start_clu); | |
523 | u64 size = le64_to_cpu(ep->dentry.generic_secondary.size); | |
524 | unsigned char flags = ep->dentry.generic_secondary.flags; | |
525 | ||
526 | if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size) | |
527 | return; | |
528 | ||
529 | exfat_chain_set(&dir, start_clu, | |
530 | EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)), | |
531 | flags); | |
532 | exfat_free_cluster(inode, &dir); | |
533 | } | |
534 | ||
ca061973 NJ |
535 | int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, |
536 | int entry, int num_entries, struct exfat_uni_name *p_uniname) | |
537 | { | |
538 | struct super_block *sb = inode->i_sb; | |
539 | int i; | |
ca061973 NJ |
540 | unsigned short *uniname = p_uniname->name; |
541 | struct exfat_dentry *ep; | |
542 | struct buffer_head *bh; | |
543 | int sync = IS_DIRSYNC(inode); | |
544 | ||
c71510b3 | 545 | ep = exfat_get_dentry(sb, p_dir, entry, &bh); |
ca061973 NJ |
546 | if (!ep) |
547 | return -EIO; | |
548 | ||
549 | ep->dentry.file.num_ext = (unsigned char)(num_entries - 1); | |
2c7f8937 | 550 | exfat_update_bh(bh, sync); |
ca061973 NJ |
551 | brelse(bh); |
552 | ||
c71510b3 | 553 | ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh); |
ca061973 NJ |
554 | if (!ep) |
555 | return -EIO; | |
556 | ||
557 | ep->dentry.stream.name_len = p_uniname->name_len; | |
558 | ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash); | |
2c7f8937 | 559 | exfat_update_bh(bh, sync); |
ca061973 NJ |
560 | brelse(bh); |
561 | ||
562 | for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) { | |
c71510b3 | 563 | ep = exfat_get_dentry(sb, p_dir, entry + i, &bh); |
ca061973 NJ |
564 | if (!ep) |
565 | return -EIO; | |
566 | ||
8258ef28 NJ |
567 | if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC) |
568 | exfat_free_benign_secondary_clusters(inode, ep); | |
569 | ||
ca061973 | 570 | exfat_init_name_entry(ep, uniname); |
2c7f8937 | 571 | exfat_update_bh(bh, sync); |
ca061973 NJ |
572 | brelse(bh); |
573 | uniname += EXFAT_FILE_NAME_LEN; | |
574 | } | |
575 | ||
576 | exfat_update_dir_chksum(inode, p_dir, entry); | |
577 | return 0; | |
578 | } | |
579 | ||
ff4343da YM |
580 | void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es, |
581 | int order) | |
ca061973 | 582 | { |
ca061973 | 583 | int i; |
ca061973 | 584 | struct exfat_dentry *ep; |
ca061973 | 585 | |
ff4343da YM |
586 | for (i = order; i < es->num_entries; i++) { |
587 | ep = exfat_get_dentry_cached(es, i); | |
ca061973 | 588 | |
8258ef28 NJ |
589 | if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC) |
590 | exfat_free_benign_secondary_clusters(inode, ep); | |
591 | ||
ca061973 | 592 | exfat_set_entry_type(ep, TYPE_DELETED); |
ca061973 NJ |
593 | } |
594 | ||
ff4343da YM |
595 | if (order < es->num_entries) |
596 | es->modified = true; | |
ca061973 NJ |
597 | } |
598 | ||
943af1fd | 599 | void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es) |
ca061973 | 600 | { |
943af1fd | 601 | int chksum_type = CS_DIR_ENTRY, i; |
ca061973 | 602 | unsigned short chksum = 0; |
943af1fd | 603 | struct exfat_dentry *ep; |
ca061973 | 604 | |
f3fe3954 | 605 | for (i = ES_IDX_FILE; i < es->num_entries; i++) { |
943af1fd | 606 | ep = exfat_get_dentry_cached(es, i); |
5875bf28 TK |
607 | chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum, |
608 | chksum_type); | |
ca061973 NJ |
609 | chksum_type = CS_DEFAULT; |
610 | } | |
f3fe3954 | 611 | ep = exfat_get_dentry_cached(es, ES_IDX_FILE); |
943af1fd TK |
612 | ep->dentry.file.checksum = cpu_to_le16(chksum); |
613 | es->modified = true; | |
614 | } | |
ca061973 | 615 | |
3b9681ac | 616 | int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync) |
943af1fd | 617 | { |
3db3c3fb | 618 | int i, err = 0; |
ca061973 | 619 | |
3db3c3fb TK |
620 | if (es->modified) |
621 | err = exfat_update_bhs(es->bh, es->num_bh, sync); | |
622 | ||
623 | for (i = 0; i < es->num_bh; i++) | |
624 | if (err) | |
625 | bforget(es->bh[i]); | |
626 | else | |
627 | brelse(es->bh[i]); | |
a3ff29a9 YM |
628 | |
629 | if (IS_DYNAMIC_ES(es)) | |
630 | kfree(es->bh); | |
631 | ||
8b0c4717 | 632 | return err; |
ca061973 NJ |
633 | } |
634 | ||
635 | static int exfat_walk_fat_chain(struct super_block *sb, | |
636 | struct exfat_chain *p_dir, unsigned int byte_offset, | |
637 | unsigned int *clu) | |
638 | { | |
639 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
640 | unsigned int clu_offset; | |
641 | unsigned int cur_clu; | |
642 | ||
643 | clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi); | |
644 | cur_clu = p_dir->dir; | |
645 | ||
646 | if (p_dir->flags == ALLOC_NO_FAT_CHAIN) { | |
647 | cur_clu += clu_offset; | |
648 | } else { | |
649 | while (clu_offset > 0) { | |
650 | if (exfat_get_next_cluster(sb, &cur_clu)) | |
651 | return -EIO; | |
652 | if (cur_clu == EXFAT_EOF_CLUSTER) { | |
653 | exfat_fs_error(sb, | |
654 | "invalid dentry access beyond EOF (clu : %u, eidx : %d)", | |
655 | p_dir->dir, | |
656 | EXFAT_B_TO_DEN(byte_offset)); | |
657 | return -EIO; | |
658 | } | |
659 | clu_offset--; | |
660 | } | |
661 | } | |
662 | ||
663 | *clu = cur_clu; | |
664 | return 0; | |
665 | } | |
666 | ||
8cf05883 CVB |
667 | static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir, |
668 | int entry, sector_t *sector, int *offset) | |
ca061973 NJ |
669 | { |
670 | int ret; | |
671 | unsigned int off, clu = 0; | |
672 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
673 | ||
674 | off = EXFAT_DEN_TO_B(entry); | |
675 | ||
676 | ret = exfat_walk_fat_chain(sb, p_dir, off, &clu); | |
677 | if (ret) | |
678 | return ret; | |
679 | ||
680 | /* byte offset in cluster */ | |
681 | off = EXFAT_CLU_OFFSET(off, sbi); | |
682 | ||
683 | /* byte offset in sector */ | |
684 | *offset = EXFAT_BLK_OFFSET(off, sb); | |
685 | ||
686 | /* sector offset in cluster */ | |
687 | *sector = EXFAT_B_TO_BLK(off, sb); | |
688 | *sector += exfat_cluster_to_sector(sbi, clu); | |
689 | return 0; | |
690 | } | |
691 | ||
692 | #define EXFAT_MAX_RA_SIZE (128*1024) | |
693 | static int exfat_dir_readahead(struct super_block *sb, sector_t sec) | |
694 | { | |
695 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
696 | struct buffer_head *bh; | |
697 | unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits; | |
698 | unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits; | |
699 | unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count); | |
700 | unsigned int ra_count = min(adj_ra_count, max_ra_count); | |
701 | ||
702 | /* Read-ahead is not required */ | |
703 | if (sbi->sect_per_clus == 1) | |
704 | return 0; | |
705 | ||
706 | if (sec < sbi->data_start_sector) { | |
d1727d55 JP |
707 | exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)", |
708 | (unsigned long long)sec, sbi->data_start_sector); | |
ca061973 NJ |
709 | return -EIO; |
710 | } | |
711 | ||
712 | /* Not sector aligned with ra_count, resize ra_count to page size */ | |
713 | if ((sec - sbi->data_start_sector) & (ra_count - 1)) | |
714 | ra_count = page_ra_count; | |
715 | ||
716 | bh = sb_find_get_block(sb, sec); | |
717 | if (!bh || !buffer_uptodate(bh)) { | |
718 | unsigned int i; | |
719 | ||
720 | for (i = 0; i < ra_count; i++) | |
721 | sb_breadahead(sb, (sector_t)(sec + i)); | |
722 | } | |
723 | brelse(bh); | |
724 | return 0; | |
725 | } | |
726 | ||
727 | struct exfat_dentry *exfat_get_dentry(struct super_block *sb, | |
c71510b3 | 728 | struct exfat_chain *p_dir, int entry, struct buffer_head **bh) |
ca061973 NJ |
729 | { |
730 | unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE); | |
731 | int off; | |
732 | sector_t sec; | |
733 | ||
734 | if (p_dir->dir == DIR_DELETED) { | |
d1727d55 | 735 | exfat_err(sb, "abnormal access to deleted dentry"); |
ca061973 NJ |
736 | return NULL; |
737 | } | |
738 | ||
739 | if (exfat_find_location(sb, p_dir, entry, &sec, &off)) | |
740 | return NULL; | |
741 | ||
742 | if (p_dir->dir != EXFAT_FREE_CLUSTER && | |
743 | !(entry & (dentries_per_page - 1))) | |
744 | exfat_dir_readahead(sb, sec); | |
745 | ||
746 | *bh = sb_bread(sb, sec); | |
747 | if (!*bh) | |
748 | return NULL; | |
749 | ||
ca061973 NJ |
750 | return (struct exfat_dentry *)((*bh)->b_data + off); |
751 | } | |
752 | ||
753 | enum exfat_validate_dentry_mode { | |
ca061973 NJ |
754 | ES_MODE_GET_FILE_ENTRY, |
755 | ES_MODE_GET_STRM_ENTRY, | |
756 | ES_MODE_GET_NAME_ENTRY, | |
757 | ES_MODE_GET_CRITICAL_SEC_ENTRY, | |
8258ef28 | 758 | ES_MODE_GET_BENIGN_SEC_ENTRY, |
ca061973 NJ |
759 | }; |
760 | ||
761 | static bool exfat_validate_entry(unsigned int type, | |
762 | enum exfat_validate_dentry_mode *mode) | |
763 | { | |
764 | if (type == TYPE_UNUSED || type == TYPE_DELETED) | |
765 | return false; | |
766 | ||
767 | switch (*mode) { | |
ca061973 NJ |
768 | case ES_MODE_GET_FILE_ENTRY: |
769 | if (type != TYPE_STREAM) | |
770 | return false; | |
771 | *mode = ES_MODE_GET_STRM_ENTRY; | |
8258ef28 | 772 | break; |
ca061973 NJ |
773 | case ES_MODE_GET_STRM_ENTRY: |
774 | if (type != TYPE_EXTEND) | |
775 | return false; | |
776 | *mode = ES_MODE_GET_NAME_ENTRY; | |
8258ef28 | 777 | break; |
ca061973 | 778 | case ES_MODE_GET_NAME_ENTRY: |
8258ef28 NJ |
779 | if (type & TYPE_BENIGN_SEC) |
780 | *mode = ES_MODE_GET_BENIGN_SEC_ENTRY; | |
781 | else if (type != TYPE_EXTEND) | |
ca061973 | 782 | return false; |
8258ef28 NJ |
783 | break; |
784 | case ES_MODE_GET_BENIGN_SEC_ENTRY: | |
785 | /* Assume unreconized benign secondary entry */ | |
786 | if (!(type & TYPE_BENIGN_SEC)) | |
ca061973 | 787 | return false; |
8258ef28 | 788 | break; |
ca061973 | 789 | default: |
ca061973 NJ |
790 | return false; |
791 | } | |
8258ef28 NJ |
792 | |
793 | return true; | |
ca061973 NJ |
794 | } |
795 | ||
943af1fd TK |
796 | struct exfat_dentry *exfat_get_dentry_cached( |
797 | struct exfat_entry_set_cache *es, int num) | |
798 | { | |
799 | int off = es->start_off + num * DENTRY_SIZE; | |
800 | struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)]; | |
801 | char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb); | |
802 | ||
803 | return (struct exfat_dentry *)p; | |
804 | } | |
805 | ||
ca061973 | 806 | /* |
7b6bab23 | 807 | * Returns a set of dentries. |
ca061973 | 808 | * |
943af1fd TK |
809 | * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached(). |
810 | * User should call exfat_get_dentry_set() after setting 'modified' to apply | |
811 | * changes made in this entry set to the real device. | |
ca061973 NJ |
812 | * |
813 | * in: | |
814 | * sb+p_dir+entry: indicates a file/dir | |
7b6bab23 YM |
815 | * num_entries: specifies how many dentries should be included. |
816 | * It will be set to es->num_entries if it is not 0. | |
817 | * If num_entries is 0, es->num_entries will be obtained | |
818 | * from the first dentry. | |
819 | * out: | |
820 | * es: pointer of entry set on success. | |
ca061973 | 821 | * return: |
7b6bab23 YM |
822 | * 0 on success |
823 | * -error code on failure | |
ca061973 | 824 | */ |
7b6bab23 | 825 | static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es, |
20914ff6 | 826 | struct super_block *sb, struct exfat_chain *p_dir, int entry, |
7b6bab23 | 827 | unsigned int num_entries) |
ca061973 | 828 | { |
943af1fd | 829 | int ret, i, num_bh; |
36955d36 | 830 | unsigned int off; |
ca061973 NJ |
831 | sector_t sec; |
832 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
ca061973 NJ |
833 | struct buffer_head *bh; |
834 | ||
835 | if (p_dir->dir == DIR_DELETED) { | |
d1727d55 | 836 | exfat_err(sb, "access to deleted dentry"); |
20914ff6 | 837 | return -EIO; |
ca061973 NJ |
838 | } |
839 | ||
36955d36 | 840 | ret = exfat_find_location(sb, p_dir, entry, &sec, &off); |
ca061973 | 841 | if (ret) |
20914ff6 | 842 | return ret; |
ca061973 | 843 | |
20914ff6 | 844 | memset(es, 0, sizeof(*es)); |
943af1fd TK |
845 | es->sb = sb; |
846 | es->modified = false; | |
943af1fd | 847 | es->start_off = off; |
a3ff29a9 | 848 | es->bh = es->__bh; |
ca061973 | 849 | |
ca061973 NJ |
850 | bh = sb_bread(sb, sec); |
851 | if (!bh) | |
20914ff6 | 852 | return -EIO; |
943af1fd | 853 | es->bh[es->num_bh++] = bh; |
ca061973 | 854 | |
7b6bab23 YM |
855 | if (num_entries == ES_ALL_ENTRIES) { |
856 | struct exfat_dentry *ep; | |
857 | ||
858 | ep = exfat_get_dentry_cached(es, ES_IDX_FILE); | |
859 | if (ep->type != EXFAT_FILE) { | |
860 | brelse(bh); | |
861 | return -EIO; | |
862 | } | |
863 | ||
864 | num_entries = ep->dentry.file.num_ext + 1; | |
865 | } | |
ca061973 | 866 | |
ca061973 | 867 | es->num_entries = num_entries; |
ca061973 | 868 | |
943af1fd | 869 | num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb); |
a3ff29a9 YM |
870 | if (num_bh > ARRAY_SIZE(es->__bh)) { |
871 | es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL); | |
872 | if (!es->bh) { | |
873 | brelse(bh); | |
20914ff6 | 874 | return -ENOMEM; |
a3ff29a9 YM |
875 | } |
876 | es->bh[0] = bh; | |
877 | } | |
878 | ||
943af1fd TK |
879 | for (i = 1; i < num_bh; i++) { |
880 | /* get the next sector */ | |
881 | if (exfat_is_last_sector_in_cluster(sbi, sec)) { | |
36955d36 YM |
882 | unsigned int clu = exfat_sector_to_cluster(sbi, sec); |
883 | ||
943af1fd TK |
884 | if (p_dir->flags == ALLOC_NO_FAT_CHAIN) |
885 | clu++; | |
886 | else if (exfat_get_next_cluster(sb, &clu)) | |
3b9681ac | 887 | goto put_es; |
943af1fd | 888 | sec = exfat_cluster_to_sector(sbi, clu); |
ca061973 | 889 | } else { |
943af1fd | 890 | sec++; |
ca061973 | 891 | } |
943af1fd TK |
892 | |
893 | bh = sb_bread(sb, sec); | |
894 | if (!bh) | |
3b9681ac | 895 | goto put_es; |
943af1fd | 896 | es->bh[es->num_bh++] = bh; |
ca061973 NJ |
897 | } |
898 | ||
7b6bab23 YM |
899 | return 0; |
900 | ||
901 | put_es: | |
902 | exfat_put_dentry_set(es, false); | |
903 | return -EIO; | |
904 | } | |
905 | ||
906 | int exfat_get_dentry_set(struct exfat_entry_set_cache *es, | |
907 | struct super_block *sb, struct exfat_chain *p_dir, | |
908 | int entry, unsigned int num_entries) | |
909 | { | |
910 | int ret, i; | |
911 | struct exfat_dentry *ep; | |
912 | enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY; | |
913 | ||
914 | ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries); | |
915 | if (ret < 0) | |
916 | return ret; | |
917 | ||
6fa96cd5 | 918 | /* validate cached dentries */ |
7b6bab23 | 919 | for (i = ES_IDX_STREAM; i < es->num_entries; i++) { |
943af1fd TK |
920 | ep = exfat_get_dentry_cached(es, i); |
921 | if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode)) | |
3b9681ac | 922 | goto put_es; |
943af1fd | 923 | } |
20914ff6 | 924 | return 0; |
ca061973 | 925 | |
3b9681ac YM |
926 | put_es: |
927 | exfat_put_dentry_set(es, false); | |
20914ff6 | 928 | return -EIO; |
ca061973 NJ |
929 | } |
930 | ||
01da3a51 YM |
931 | static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es) |
932 | { | |
933 | struct exfat_dentry *ep; | |
934 | struct buffer_head *bh; | |
935 | int i, off; | |
936 | bool unused_hit = false; | |
937 | ||
938 | /* | |
939 | * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED: | |
940 | * Although it violates the specification for a deleted entry to | |
941 | * follow an unused entry, some exFAT implementations could work | |
942 | * like this. Therefore, to improve compatibility, let's allow it. | |
943 | */ | |
944 | for (i = 0; i < es->num_entries; i++) { | |
945 | ep = exfat_get_dentry_cached(es, i); | |
946 | if (ep->type == EXFAT_UNUSED) { | |
947 | unused_hit = true; | |
948 | } else if (!IS_EXFAT_DELETED(ep->type)) { | |
949 | if (unused_hit) | |
950 | goto err_used_follow_unused; | |
951 | i++; | |
952 | goto count_skip_entries; | |
953 | } | |
954 | } | |
955 | ||
956 | return 0; | |
957 | ||
958 | err_used_follow_unused: | |
959 | off = es->start_off + (i << DENTRY_SIZE_BITS); | |
960 | bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)]; | |
961 | ||
962 | exfat_fs_error(es->sb, | |
963 | "in sector %lld, dentry %d should be unused, but 0x%x", | |
964 | bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type); | |
965 | ||
966 | return -EIO; | |
967 | ||
968 | count_skip_entries: | |
969 | es->num_entries = EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - es->start_off); | |
970 | for (; i < es->num_entries; i++) { | |
971 | ep = exfat_get_dentry_cached(es, i); | |
972 | if (IS_EXFAT_DELETED(ep->type)) | |
973 | break; | |
974 | } | |
975 | ||
976 | return i; | |
977 | } | |
978 | ||
979 | /* | |
980 | * Get an empty dentry set. | |
981 | * | |
982 | * in: | |
983 | * sb+p_dir+entry: indicates the empty dentry location | |
984 | * num_entries: specifies how many empty dentries should be included. | |
985 | * out: | |
986 | * es: pointer of empty dentry set on success. | |
987 | * return: | |
988 | * 0 : on success | |
989 | * >0 : the dentries are not empty, the return value is the number of | |
990 | * dentries to be skipped for the next lookup. | |
991 | * <0 : on failure | |
992 | */ | |
993 | int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es, | |
994 | struct super_block *sb, struct exfat_chain *p_dir, | |
995 | int entry, unsigned int num_entries) | |
996 | { | |
997 | int ret; | |
998 | ||
999 | ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries); | |
1000 | if (ret < 0) | |
1001 | return ret; | |
1002 | ||
1003 | ret = exfat_validate_empty_dentry_set(es); | |
1004 | if (ret) | |
1005 | exfat_put_dentry_set(es, false); | |
1006 | ||
1007 | return ret; | |
1008 | } | |
1009 | ||
ff39899b YM |
1010 | static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp) |
1011 | { | |
1012 | hint_femp->eidx = EXFAT_HINT_NONE; | |
1013 | hint_femp->count = 0; | |
1014 | } | |
1015 | ||
1016 | static inline void exfat_set_empty_hint(struct exfat_inode_info *ei, | |
1017 | struct exfat_hint_femp *candi_empty, struct exfat_chain *clu, | |
e298c8a8 | 1018 | int dentry, int num_entries, int entry_type) |
ff39899b YM |
1019 | { |
1020 | if (ei->hint_femp.eidx == EXFAT_HINT_NONE || | |
1021 | ei->hint_femp.eidx > dentry) { | |
e298c8a8 YM |
1022 | int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode)); |
1023 | ||
ff39899b YM |
1024 | if (candi_empty->count == 0) { |
1025 | candi_empty->cur = *clu; | |
1026 | candi_empty->eidx = dentry; | |
1027 | } | |
1028 | ||
e298c8a8 YM |
1029 | if (entry_type == TYPE_UNUSED) |
1030 | candi_empty->count += total_entries - dentry; | |
1031 | else | |
1032 | candi_empty->count++; | |
1033 | ||
1034 | if (candi_empty->count == num_entries || | |
1035 | candi_empty->count + candi_empty->eidx == total_entries) | |
ff39899b YM |
1036 | ei->hint_femp = *candi_empty; |
1037 | } | |
1038 | } | |
1039 | ||
ca061973 NJ |
1040 | enum { |
1041 | DIRENT_STEP_FILE, | |
1042 | DIRENT_STEP_STRM, | |
1043 | DIRENT_STEP_NAME, | |
1044 | DIRENT_STEP_SECD, | |
1045 | }; | |
1046 | ||
1047 | /* | |
c6e2f52e HK |
1048 | * @ei: inode info of parent directory |
1049 | * @p_dir: directory structure of parent directory | |
1050 | * @num_entries:entry size of p_uniname | |
1051 | * @hint_opt: If p_uniname is found, filled with optimized dir/entry | |
1052 | * for traversing cluster chain. | |
1053 | * @return: | |
1054 | * >= 0: file directory entry position where the name exists | |
1055 | * -ENOENT: entry with the name does not exist | |
1056 | * -EIO: I/O error | |
ca061973 NJ |
1057 | */ |
1058 | int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, | |
1059 | struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, | |
72880cb5 | 1060 | struct exfat_hint *hint_opt) |
ca061973 NJ |
1061 | { |
1062 | int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len; | |
1063 | int order, step, name_len = 0; | |
ff39899b | 1064 | int dentries_per_clu; |
ca061973 NJ |
1065 | unsigned int entry_type; |
1066 | unsigned short *uniname = NULL; | |
1067 | struct exfat_chain clu; | |
1068 | struct exfat_hint *hint_stat = &ei->hint_stat; | |
1069 | struct exfat_hint_femp candi_empty; | |
1070 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
72880cb5 YM |
1071 | int num_entries = exfat_calc_num_entries(p_uniname); |
1072 | ||
1073 | if (num_entries < 0) | |
1074 | return num_entries; | |
ca061973 NJ |
1075 | |
1076 | dentries_per_clu = sbi->dentries_per_clu; | |
1077 | ||
1078 | exfat_chain_dup(&clu, p_dir); | |
1079 | ||
1080 | if (hint_stat->eidx) { | |
1081 | clu.dir = hint_stat->clu; | |
1082 | dentry = hint_stat->eidx; | |
1083 | end_eidx = dentry; | |
1084 | } | |
1085 | ||
ff39899b YM |
1086 | exfat_reset_empty_hint(&ei->hint_femp); |
1087 | ||
ca061973 NJ |
1088 | rewind: |
1089 | order = 0; | |
1090 | step = DIRENT_STEP_FILE; | |
ff39899b YM |
1091 | exfat_reset_empty_hint(&candi_empty); |
1092 | ||
ca061973 NJ |
1093 | while (clu.dir != EXFAT_EOF_CLUSTER) { |
1094 | i = dentry & (dentries_per_clu - 1); | |
1095 | for (; i < dentries_per_clu; i++, dentry++) { | |
1096 | struct exfat_dentry *ep; | |
1097 | struct buffer_head *bh; | |
1098 | ||
1099 | if (rewind && dentry == end_eidx) | |
1100 | goto not_found; | |
1101 | ||
c71510b3 | 1102 | ep = exfat_get_dentry(sb, &clu, i, &bh); |
ca061973 NJ |
1103 | if (!ep) |
1104 | return -EIO; | |
1105 | ||
1106 | entry_type = exfat_get_entry_type(ep); | |
1107 | ||
1108 | if (entry_type == TYPE_UNUSED || | |
1109 | entry_type == TYPE_DELETED) { | |
1110 | step = DIRENT_STEP_FILE; | |
1111 | ||
ff39899b | 1112 | exfat_set_empty_hint(ei, &candi_empty, &clu, |
e298c8a8 YM |
1113 | dentry, num_entries, |
1114 | entry_type); | |
ca061973 NJ |
1115 | |
1116 | brelse(bh); | |
1117 | if (entry_type == TYPE_UNUSED) | |
1118 | goto not_found; | |
1119 | continue; | |
1120 | } | |
1121 | ||
ff39899b | 1122 | exfat_reset_empty_hint(&candi_empty); |
ca061973 NJ |
1123 | |
1124 | if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) { | |
1125 | step = DIRENT_STEP_FILE; | |
c6e2f52e HK |
1126 | hint_opt->clu = clu.dir; |
1127 | hint_opt->eidx = i; | |
72880cb5 YM |
1128 | num_ext = ep->dentry.file.num_ext; |
1129 | step = DIRENT_STEP_STRM; | |
ca061973 NJ |
1130 | brelse(bh); |
1131 | continue; | |
1132 | } | |
1133 | ||
1134 | if (entry_type == TYPE_STREAM) { | |
5875bf28 | 1135 | u16 name_hash; |
ca061973 NJ |
1136 | |
1137 | if (step != DIRENT_STEP_STRM) { | |
1138 | step = DIRENT_STEP_FILE; | |
1139 | brelse(bh); | |
1140 | continue; | |
1141 | } | |
1142 | step = DIRENT_STEP_FILE; | |
1143 | name_hash = le16_to_cpu( | |
1144 | ep->dentry.stream.name_hash); | |
1145 | if (p_uniname->name_hash == name_hash && | |
1146 | p_uniname->name_len == | |
1147 | ep->dentry.stream.name_len) { | |
1148 | step = DIRENT_STEP_NAME; | |
1149 | order = 1; | |
1150 | name_len = 0; | |
1151 | } | |
1152 | brelse(bh); | |
1153 | continue; | |
1154 | } | |
1155 | ||
1156 | brelse(bh); | |
1157 | if (entry_type == TYPE_EXTEND) { | |
1158 | unsigned short entry_uniname[16], unichar; | |
1159 | ||
d4233457 NJ |
1160 | if (step != DIRENT_STEP_NAME || |
1161 | name_len >= MAX_NAME_LENGTH) { | |
ca061973 NJ |
1162 | step = DIRENT_STEP_FILE; |
1163 | continue; | |
1164 | } | |
1165 | ||
1166 | if (++order == 2) | |
1167 | uniname = p_uniname->name; | |
1168 | else | |
1169 | uniname += EXFAT_FILE_NAME_LEN; | |
1170 | ||
1171 | len = exfat_extract_uni_name(ep, entry_uniname); | |
1172 | name_len += len; | |
1173 | ||
1174 | unichar = *(uniname+len); | |
1175 | *(uniname+len) = 0x0; | |
1176 | ||
1177 | if (exfat_uniname_ncmp(sb, uniname, | |
1178 | entry_uniname, len)) { | |
1179 | step = DIRENT_STEP_FILE; | |
1180 | } else if (p_uniname->name_len == name_len) { | |
1181 | if (order == num_ext) | |
1182 | goto found; | |
1183 | step = DIRENT_STEP_SECD; | |
1184 | } | |
1185 | ||
1186 | *(uniname+len) = unichar; | |
1187 | continue; | |
1188 | } | |
1189 | ||
1190 | if (entry_type & | |
1191 | (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) { | |
1192 | if (step == DIRENT_STEP_SECD) { | |
1193 | if (++order == num_ext) | |
1194 | goto found; | |
1195 | continue; | |
1196 | } | |
1197 | } | |
1198 | step = DIRENT_STEP_FILE; | |
1199 | } | |
1200 | ||
1201 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { | |
1202 | if (--clu.size > 0) | |
1203 | clu.dir++; | |
1204 | else | |
1205 | clu.dir = EXFAT_EOF_CLUSTER; | |
1206 | } else { | |
1207 | if (exfat_get_next_cluster(sb, &clu.dir)) | |
1208 | return -EIO; | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | not_found: | |
1213 | /* | |
1214 | * We started at not 0 index,so we should try to find target | |
1215 | * from 0 index to the index we started at. | |
1216 | */ | |
1217 | if (!rewind && end_eidx) { | |
1218 | rewind = 1; | |
1219 | dentry = 0; | |
1220 | clu.dir = p_dir->dir; | |
ca061973 NJ |
1221 | goto rewind; |
1222 | } | |
1223 | ||
e298c8a8 YM |
1224 | /* |
1225 | * set the EXFAT_EOF_CLUSTER flag to avoid search | |
1226 | * from the beginning again when allocated a new cluster | |
1227 | */ | |
1228 | if (ei->hint_femp.eidx == EXFAT_HINT_NONE) { | |
1229 | ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER; | |
1230 | ei->hint_femp.eidx = p_dir->size * dentries_per_clu; | |
1231 | ei->hint_femp.count = 0; | |
1232 | } | |
1233 | ||
ca061973 NJ |
1234 | /* initialized hint_stat */ |
1235 | hint_stat->clu = p_dir->dir; | |
1236 | hint_stat->eidx = 0; | |
1237 | return -ENOENT; | |
1238 | ||
1239 | found: | |
1240 | /* next dentry we'll find is out of this cluster */ | |
1241 | if (!((dentry + 1) & (dentries_per_clu - 1))) { | |
1242 | int ret = 0; | |
1243 | ||
1244 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { | |
1245 | if (--clu.size > 0) | |
1246 | clu.dir++; | |
1247 | else | |
1248 | clu.dir = EXFAT_EOF_CLUSTER; | |
1249 | } else { | |
1250 | ret = exfat_get_next_cluster(sb, &clu.dir); | |
1251 | } | |
1252 | ||
d2fa0c33 | 1253 | if (ret || clu.dir == EXFAT_EOF_CLUSTER) { |
ca061973 NJ |
1254 | /* just initialized hint_stat */ |
1255 | hint_stat->clu = p_dir->dir; | |
1256 | hint_stat->eidx = 0; | |
1257 | return (dentry - num_ext); | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | hint_stat->clu = clu.dir; | |
1262 | hint_stat->eidx = dentry + 1; | |
1263 | return dentry - num_ext; | |
1264 | } | |
1265 | ||
1266 | int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir, | |
1267 | int entry, struct exfat_dentry *ep) | |
1268 | { | |
1269 | int i, count = 0; | |
1270 | unsigned int type; | |
1271 | struct exfat_dentry *ext_ep; | |
1272 | struct buffer_head *bh; | |
1273 | ||
1274 | for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) { | |
c71510b3 | 1275 | ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh); |
ca061973 NJ |
1276 | if (!ext_ep) |
1277 | return -EIO; | |
1278 | ||
1279 | type = exfat_get_entry_type(ext_ep); | |
1280 | brelse(bh); | |
8258ef28 | 1281 | if (type & TYPE_CRITICAL_SEC || type & TYPE_BENIGN_SEC) |
ca061973 | 1282 | count++; |
ca061973 NJ |
1283 | } |
1284 | return count; | |
1285 | } | |
1286 | ||
1287 | int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) | |
1288 | { | |
1289 | int i, count = 0; | |
1290 | int dentries_per_clu; | |
1291 | unsigned int entry_type; | |
1292 | struct exfat_chain clu; | |
1293 | struct exfat_dentry *ep; | |
1294 | struct exfat_sb_info *sbi = EXFAT_SB(sb); | |
1295 | struct buffer_head *bh; | |
1296 | ||
1297 | dentries_per_clu = sbi->dentries_per_clu; | |
1298 | ||
1299 | exfat_chain_dup(&clu, p_dir); | |
1300 | ||
1301 | while (clu.dir != EXFAT_EOF_CLUSTER) { | |
1302 | for (i = 0; i < dentries_per_clu; i++) { | |
c71510b3 | 1303 | ep = exfat_get_dentry(sb, &clu, i, &bh); |
ca061973 NJ |
1304 | if (!ep) |
1305 | return -EIO; | |
1306 | entry_type = exfat_get_entry_type(ep); | |
1307 | brelse(bh); | |
1308 | ||
1309 | if (entry_type == TYPE_UNUSED) | |
1310 | return count; | |
1311 | if (entry_type != TYPE_DIR) | |
1312 | continue; | |
1313 | count++; | |
1314 | } | |
1315 | ||
1316 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { | |
1317 | if (--clu.size > 0) | |
1318 | clu.dir++; | |
1319 | else | |
1320 | clu.dir = EXFAT_EOF_CLUSTER; | |
1321 | } else { | |
1322 | if (exfat_get_next_cluster(sb, &(clu.dir))) | |
1323 | return -EIO; | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | return count; | |
1328 | } |