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