1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * NTFS kernel directory operations.
4 *
5 * Copyright (c) 2001-2007 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon
7 * Copyright (c) 2025 LG Electronics Co., Ltd.
8 */
9
10 #include <linux/blkdev.h>
11
12 #include "dir.h"
13 #include "mft.h"
14 #include "ntfs.h"
15 #include "index.h"
16 #include "reparse.h"
17
18 #include <linux/filelock.h>
19
20 /*
21 * The little endian Unicode string $I30 as a global constant.
22 */
23 __le16 I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
24 cpu_to_le16('3'), cpu_to_le16('0'), 0 };
25
ntfs_check_mref(u64 mref)26 static inline u64 ntfs_check_mref(u64 mref)
27 {
28 if (IS_ERR_MREF(mref))
29 return ERR_MREF(-EIO);
30 return mref;
31 }
32
33 /*
34 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
35 * @dir_ni: ntfs inode of the directory in which to search for the name
36 * @uname: Unicode name for which to search in the directory
37 * @uname_len: length of the name @uname in Unicode characters
38 * @res: return the found file name if necessary (see below)
39 *
40 * Look for an inode with name @uname in the directory with inode @dir_ni.
41 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
42 * the Unicode name. If the name is found in the directory, the corresponding
43 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
44 * is a 64-bit number containing the sequence number.
45 *
46 * On error, a negative value is returned corresponding to the error code. In
47 * particular if the inode is not found -ENOENT is returned. Note that you
48 * can't just check the return value for being negative, you have to check the
49 * inode number for being negative which you can extract using MREC(return
50 * value).
51 *
52 * Note, @uname_len does not include the (optional) terminating NULL character.
53 *
54 * Note, we look for a case sensitive match first but we also look for a case
55 * insensitive match at the same time. If we find a case insensitive match, we
56 * save that for the case that we don't find an exact match, where we return
57 * the case insensitive match and setup @res (which we allocate!) with the mft
58 * reference, the file name type, length and with a copy of the little endian
59 * Unicode file name itself. If we match a file name which is in the DOS name
60 * space, we only return the mft reference and file name type in @res.
61 * ntfs_lookup() then uses this to find the long file name in the inode itself.
62 * This is to avoid polluting the dcache with short file names. We want them to
63 * work but we don't care for how quickly one can access them. This also fixes
64 * the dcache aliasing issues.
65 *
66 * Locking: - Caller must hold i_mutex on the directory.
67 * - Each page cache page in the index allocation mapping must be
68 * locked whilst being accessed otherwise we may find a corrupt
69 * page due to it being under ->writepage at the moment which
70 * applies the mst protection fixups before writing out and then
71 * removes them again after the write is complete after which it
72 * unlocks the page.
73 */
ntfs_lookup_inode_by_name(struct ntfs_inode * dir_ni,const __le16 * uname,const int uname_len,struct ntfs_name ** res)74 u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
75 const int uname_len, struct ntfs_name **res)
76 {
77 struct ntfs_volume *vol = dir_ni->vol;
78 struct super_block *sb = vol->sb;
79 struct inode *ia_vi = NULL;
80 struct mft_record *m;
81 struct index_root *ir;
82 struct index_entry *ie;
83 struct index_block *ia;
84 u8 *index_end;
85 u64 mref;
86 struct ntfs_attr_search_ctx *ctx;
87 int err, rc;
88 s64 vcn, old_vcn;
89 struct address_space *ia_mapping;
90 struct folio *folio;
91 u8 *kaddr = NULL;
92 struct ntfs_name *name = NULL;
93
94 /* Get hold of the mft record for the directory. */
95 m = map_mft_record(dir_ni);
96 if (IS_ERR(m)) {
97 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
98 -PTR_ERR(m));
99 return ERR_MREF(PTR_ERR(m));
100 }
101 ctx = ntfs_attr_get_search_ctx(dir_ni, m);
102 if (unlikely(!ctx)) {
103 err = -ENOMEM;
104 goto err_out;
105 }
106 /* Find the index root attribute in the mft record. */
107 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
108 0, ctx);
109 if (unlikely(err)) {
110 if (err == -ENOENT) {
111 ntfs_error(sb,
112 "Index root attribute missing in directory inode 0x%llx.",
113 dir_ni->mft_no);
114 err = -EIO;
115 }
116 goto err_out;
117 }
118 /* Get to the index root value (it's been verified in read_inode). */
119 ir = (struct index_root *)((u8 *)ctx->attr +
120 le16_to_cpu(ctx->attr->data.resident.value_offset));
121 index_end = (u8 *)&ir->index + le32_to_cpu(ir->index.index_length);
122 /* The first index entry. */
123 ie = (struct index_entry *)((u8 *)&ir->index +
124 le32_to_cpu(ir->index.entries_offset));
125 /*
126 * Loop until we exceed valid memory (corruption case) or until we
127 * reach the last entry.
128 */
129 for (;; ie = (struct index_entry *)((u8 *)ie + le16_to_cpu(ie->length))) {
130 /* Bounds checks. */
131 if ((u8 *)ie < (u8 *)ctx->mrec ||
132 (u8 *)ie + sizeof(struct index_entry_header) > index_end ||
133 (u8 *)ie + sizeof(struct index_entry_header) + le16_to_cpu(ie->key_length) >
134 index_end || (u8 *)ie + le16_to_cpu(ie->length) > index_end)
135 goto dir_err_out;
136 /*
137 * The last entry cannot contain a name. It can however contain
138 * a pointer to a child node in the B+tree so we just break out.
139 */
140 if (ie->flags & INDEX_ENTRY_END)
141 break;
142 /* Key length should not be zero if it is not last entry. */
143 if (!ie->key_length)
144 goto dir_err_out;
145 /*
146 * We perform a case sensitive comparison and if that matches
147 * we are done and return the mft reference of the inode (i.e.
148 * the inode number together with the sequence number for
149 * consistency checking). We convert it to cpu format before
150 * returning.
151 */
152 if (ntfs_are_names_equal(uname, uname_len,
153 (__le16 *)&ie->key.file_name.file_name,
154 ie->key.file_name.file_name_length,
155 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
156 found_it:
157 /*
158 * We have a perfect match, so we don't need to care
159 * about having matched imperfectly before, so we can
160 * free name and set *res to NULL.
161 * However, if the perfect match is a short file name,
162 * we need to signal this through *res, so that
163 * ntfs_lookup() can fix dcache aliasing issues.
164 * As an optimization we just reuse an existing
165 * allocation of *res.
166 */
167 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
168 if (!name) {
169 name = kmalloc(sizeof(struct ntfs_name),
170 GFP_NOFS);
171 if (!name) {
172 err = -ENOMEM;
173 goto err_out;
174 }
175 }
176 name->mref = le64_to_cpu(
177 ie->data.dir.indexed_file);
178 name->type = FILE_NAME_DOS;
179 name->len = 0;
180 *res = name;
181 } else {
182 kfree(name);
183 *res = NULL;
184 }
185 mref = le64_to_cpu(ie->data.dir.indexed_file);
186 ntfs_attr_put_search_ctx(ctx);
187 unmap_mft_record(dir_ni);
188 return ntfs_check_mref(mref);
189 }
190 /*
191 * For a case insensitive mount, we also perform a case
192 * insensitive comparison (provided the file name is not in the
193 * POSIX namespace). If the comparison matches, and the name is
194 * in the WIN32 namespace, we cache the filename in *res so
195 * that the caller, ntfs_lookup(), can work on it. If the
196 * comparison matches, and the name is in the DOS namespace, we
197 * only cache the mft reference and the file name type (we set
198 * the name length to zero for simplicity).
199 */
200 if ((!NVolCaseSensitive(vol) ||
201 ie->key.file_name.file_name_type == FILE_NAME_DOS) &&
202 ntfs_are_names_equal(uname, uname_len,
203 (__le16 *)&ie->key.file_name.file_name,
204 ie->key.file_name.file_name_length,
205 IGNORE_CASE, vol->upcase,
206 vol->upcase_len)) {
207 int name_size = sizeof(struct ntfs_name);
208 u8 type = ie->key.file_name.file_name_type;
209 u8 len = ie->key.file_name.file_name_length;
210
211 /* Only one case insensitive matching name allowed. */
212 if (name) {
213 ntfs_error(sb,
214 "Found already allocated name in phase 1. Please run chkdsk");
215 goto dir_err_out;
216 }
217
218 if (type != FILE_NAME_DOS)
219 name_size += len * sizeof(__le16);
220 name = kmalloc(name_size, GFP_NOFS);
221 if (!name) {
222 err = -ENOMEM;
223 goto err_out;
224 }
225 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
226 name->type = type;
227 if (type != FILE_NAME_DOS) {
228 name->len = len;
229 memcpy(name->name, ie->key.file_name.file_name,
230 len * sizeof(__le16));
231 } else
232 name->len = 0;
233 *res = name;
234 }
235 /*
236 * Not a perfect match, need to do full blown collation so we
237 * know which way in the B+tree we have to go.
238 */
239 rc = ntfs_collate_names(uname, uname_len,
240 (__le16 *)&ie->key.file_name.file_name,
241 ie->key.file_name.file_name_length, 1,
242 IGNORE_CASE, vol->upcase, vol->upcase_len);
243 /*
244 * If uname collates before the name of the current entry, there
245 * is definitely no such name in this index but we might need to
246 * descend into the B+tree so we just break out of the loop.
247 */
248 if (rc == -1)
249 break;
250 /* The names are not equal, continue the search. */
251 if (rc)
252 continue;
253 /*
254 * Names match with case insensitive comparison, now try the
255 * case sensitive comparison, which is required for proper
256 * collation.
257 */
258 rc = ntfs_collate_names(uname, uname_len,
259 (__le16 *)&ie->key.file_name.file_name,
260 ie->key.file_name.file_name_length, 1,
261 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
262 if (rc == -1)
263 break;
264 if (rc)
265 continue;
266 /*
267 * Perfect match, this will never happen as the
268 * ntfs_are_names_equal() call will have gotten a match but we
269 * still treat it correctly.
270 */
271 goto found_it;
272 }
273 /*
274 * We have finished with this index without success. Check for the
275 * presence of a child node and if not present return -ENOENT, unless
276 * we have got a matching name cached in name in which case return the
277 * mft reference associated with it.
278 */
279 if (!(ie->flags & INDEX_ENTRY_NODE)) {
280 if (name) {
281 ntfs_attr_put_search_ctx(ctx);
282 unmap_mft_record(dir_ni);
283 return ntfs_check_mref(name->mref);
284 }
285 ntfs_debug("Entry not found.");
286 err = -ENOENT;
287 goto err_out;
288 } /* Child node present, descend into it. */
289
290 /* Get the starting vcn of the index_block holding the child node. */
291 vcn = le64_to_cpup((__le64 *)((u8 *)ie + le16_to_cpu(ie->length) - 8));
292
293 /*
294 * We are done with the index root and the mft record. Release them,
295 * otherwise we deadlock with read_mapping_folio().
296 */
297 ntfs_attr_put_search_ctx(ctx);
298 unmap_mft_record(dir_ni);
299 m = NULL;
300 ctx = NULL;
301
302 ia_vi = ntfs_index_iget(VFS_I(dir_ni), I30, 4);
303 if (IS_ERR(ia_vi)) {
304 err = PTR_ERR(ia_vi);
305 goto err_out;
306 }
307
308 ia_mapping = ia_vi->i_mapping;
309 descend_into_child_node:
310 /*
311 * Convert vcn to index into the index allocation attribute in units
312 * of PAGE_SIZE and map the page cache page, reading it from
313 * disk if necessary.
314 */
315 folio = read_mapping_folio(ia_mapping, vcn <<
316 dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT, NULL);
317 if (IS_ERR(folio)) {
318 ntfs_error(sb, "Failed to map directory index page, error %ld.",
319 -PTR_ERR(folio));
320 err = PTR_ERR(folio);
321 goto err_out;
322 }
323
324 folio_lock(folio);
325 kaddr = kmalloc(PAGE_SIZE, GFP_NOFS);
326 if (!kaddr) {
327 err = -ENOMEM;
328 folio_unlock(folio);
329 folio_put(folio);
330 goto unm_err_out;
331 }
332
333 memcpy_from_folio(kaddr, folio, 0, PAGE_SIZE);
334 post_read_mst_fixup((struct ntfs_record *)kaddr, PAGE_SIZE);
335 folio_unlock(folio);
336 folio_put(folio);
337 fast_descend_into_child_node:
338 /* Get to the index allocation block. */
339 ia = (struct index_block *)(kaddr + ((vcn <<
340 dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
341 /* Bounds checks. */
342 if ((u8 *)ia < kaddr || (u8 *)ia > kaddr + PAGE_SIZE) {
343 ntfs_error(sb,
344 "Out of bounds check failed. Corrupt directory inode 0x%llx or driver bug.",
345 dir_ni->mft_no);
346 goto unm_err_out;
347 }
348 index_end = (u8 *)ia + dir_ni->itype.index.block_size;
349 if (index_end > kaddr + PAGE_SIZE) {
350 ntfs_error(sb,
351 "Index buffer (VCN 0x%llx) of directory inode 0x%llx crosses page boundary. Impossible! Cannot access! This is probably a bug in the driver.",
352 vcn, dir_ni->mft_no);
353 goto unm_err_out;
354 }
355 err = ntfs_index_block_inconsistent(vol, ia,
356 dir_ni->itype.index.block_size,
357 vcn, COLLATION_FILE_NAME,
358 dir_ni->mft_no);
359 if (err)
360 goto unm_err_out;
361 index_end = (u8 *)&ia->index + le32_to_cpu(ia->index.index_length);
362 /* The first index entry. */
363 ie = (struct index_entry *)((u8 *)&ia->index +
364 le32_to_cpu(ia->index.entries_offset));
365 /*
366 * Iterate similar to above big loop but applied to index buffer, thus
367 * loop until we exceed valid memory (corruption case) or until we
368 * reach the last entry.
369 */
370 for (;; ie = (struct index_entry *)((u8 *)ie + le16_to_cpu(ie->length))) {
371 /*
372 * The last entry cannot contain a name. It can however contain
373 * a pointer to a child node in the B+tree so we just break out.
374 */
375 if (ie->flags & INDEX_ENTRY_END)
376 break;
377 /* Key length should not be zero if it is not last entry. */
378 if (!ie->key_length)
379 goto unm_err_out;
380 /*
381 * We perform a case sensitive comparison and if that matches
382 * we are done and return the mft reference of the inode (i.e.
383 * the inode number together with the sequence number for
384 * consistency checking). We convert it to cpu format before
385 * returning.
386 */
387 if (ntfs_are_names_equal(uname, uname_len,
388 (__le16 *)&ie->key.file_name.file_name,
389 ie->key.file_name.file_name_length,
390 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
391 found_it2:
392 /*
393 * We have a perfect match, so we don't need to care
394 * about having matched imperfectly before, so we can
395 * free name and set *res to NULL.
396 * However, if the perfect match is a short file name,
397 * we need to signal this through *res, so that
398 * ntfs_lookup() can fix dcache aliasing issues.
399 * As an optimization we just reuse an existing
400 * allocation of *res.
401 */
402 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
403 if (!name) {
404 name = kmalloc(sizeof(struct ntfs_name),
405 GFP_NOFS);
406 if (!name) {
407 err = -ENOMEM;
408 goto unm_err_out;
409 }
410 }
411 name->mref = le64_to_cpu(
412 ie->data.dir.indexed_file);
413 name->type = FILE_NAME_DOS;
414 name->len = 0;
415 *res = name;
416 } else {
417 kfree(name);
418 *res = NULL;
419 }
420 mref = le64_to_cpu(ie->data.dir.indexed_file);
421 kfree(kaddr);
422 iput(ia_vi);
423 return ntfs_check_mref(mref);
424 }
425 /*
426 * For a case insensitive mount, we also perform a case
427 * insensitive comparison (provided the file name is not in the
428 * POSIX namespace). If the comparison matches, and the name is
429 * in the WIN32 namespace, we cache the filename in *res so
430 * that the caller, ntfs_lookup(), can work on it. If the
431 * comparison matches, and the name is in the DOS namespace, we
432 * only cache the mft reference and the file name type (we set
433 * the name length to zero for simplicity).
434 */
435 if ((!NVolCaseSensitive(vol) ||
436 ie->key.file_name.file_name_type == FILE_NAME_DOS) &&
437 ntfs_are_names_equal(uname, uname_len,
438 (__le16 *)&ie->key.file_name.file_name,
439 ie->key.file_name.file_name_length,
440 IGNORE_CASE, vol->upcase,
441 vol->upcase_len)) {
442 int name_size = sizeof(struct ntfs_name);
443 u8 type = ie->key.file_name.file_name_type;
444 u8 len = ie->key.file_name.file_name_length;
445
446 /* Only one case insensitive matching name allowed. */
447 if (name) {
448 ntfs_error(sb,
449 "Found already allocated name in phase 2. Please run chkdsk");
450 kfree(kaddr);
451 goto dir_err_out;
452 }
453
454 if (type != FILE_NAME_DOS)
455 name_size += len * sizeof(__le16);
456 name = kmalloc(name_size, GFP_NOFS);
457 if (!name) {
458 err = -ENOMEM;
459 goto unm_err_out;
460 }
461 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
462 name->type = type;
463 if (type != FILE_NAME_DOS) {
464 name->len = len;
465 memcpy(name->name, ie->key.file_name.file_name,
466 len * sizeof(__le16));
467 } else
468 name->len = 0;
469 *res = name;
470 }
471 /*
472 * Not a perfect match, need to do full blown collation so we
473 * know which way in the B+tree we have to go.
474 */
475 rc = ntfs_collate_names(uname, uname_len,
476 (__le16 *)&ie->key.file_name.file_name,
477 ie->key.file_name.file_name_length, 1,
478 IGNORE_CASE, vol->upcase, vol->upcase_len);
479 /*
480 * If uname collates before the name of the current entry, there
481 * is definitely no such name in this index but we might need to
482 * descend into the B+tree so we just break out of the loop.
483 */
484 if (rc == -1)
485 break;
486 /* The names are not equal, continue the search. */
487 if (rc)
488 continue;
489 /*
490 * Names match with case insensitive comparison, now try the
491 * case sensitive comparison, which is required for proper
492 * collation.
493 */
494 rc = ntfs_collate_names(uname, uname_len,
495 (__le16 *)&ie->key.file_name.file_name,
496 ie->key.file_name.file_name_length, 1,
497 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
498 if (rc == -1)
499 break;
500 if (rc)
501 continue;
502 /*
503 * Perfect match, this will never happen as the
504 * ntfs_are_names_equal() call will have gotten a match but we
505 * still treat it correctly.
506 */
507 goto found_it2;
508 }
509 /*
510 * We have finished with this index buffer without success. Check for
511 * the presence of a child node.
512 */
513 if (ie->flags & INDEX_ENTRY_NODE) {
514 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
515 ntfs_error(sb,
516 "Index entry with child node found in a leaf node in directory inode 0x%llx.",
517 dir_ni->mft_no);
518 goto unm_err_out;
519 }
520 /* Child node present, descend into it. */
521 old_vcn = vcn;
522 vcn = le64_to_cpup((__le64 *)((u8 *)ie +
523 le16_to_cpu(ie->length) - 8));
524 if (vcn >= 0) {
525 /*
526 * If vcn is in the same page cache page as old_vcn we
527 * recycle the mapped page.
528 */
529 if (ntfs_cluster_to_pidx(vol, old_vcn) ==
530 ntfs_cluster_to_pidx(vol, vcn))
531 goto fast_descend_into_child_node;
532 kfree(kaddr);
533 kaddr = NULL;
534 goto descend_into_child_node;
535 }
536 ntfs_error(sb, "Negative child node vcn in directory inode 0x%llx.",
537 dir_ni->mft_no);
538 goto unm_err_out;
539 }
540 /*
541 * No child node present, return -ENOENT, unless we have got a matching
542 * name cached in name in which case return the mft reference
543 * associated with it.
544 */
545 if (name) {
546 kfree(kaddr);
547 iput(ia_vi);
548 return ntfs_check_mref(name->mref);
549 }
550 ntfs_debug("Entry not found.");
551 err = -ENOENT;
552 unm_err_out:
553 kfree(kaddr);
554 err_out:
555 if (!err)
556 err = -EIO;
557 if (ctx)
558 ntfs_attr_put_search_ctx(ctx);
559 if (m)
560 unmap_mft_record(dir_ni);
561 kfree(name);
562 *res = NULL;
563 if (!IS_ERR_OR_NULL(ia_vi))
564 iput(ia_vi);
565 return ERR_MREF(err);
566 dir_err_out:
567 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
568 goto err_out;
569 }
570
571 /*
572 * ntfs_filldir - ntfs specific filldir method
573 * @vol: current ntfs volume
574 * @ndir: ntfs inode of current directory
575 * @ia_page: page in which the index allocation buffer @ie is in resides
576 * @ie: current index entry
577 * @name: buffer to use for the converted name
578 * @actor: what to feed the entries to
579 *
580 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
581 * callback.
582 *
583 * If @ia_page is not NULL it is the locked page containing the index
584 * allocation block containing the index entry @ie.
585 *
586 * Note, we drop (and then reacquire) the page lock on @ia_page across the
587 * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
588 * since ntfs_lookup() will lock the same page. As an optimization, we do not
589 * retake the lock if we are returning a non-zero value as ntfs_readdir()
590 * would need to drop the lock immediately anyway.
591 */
ntfs_filldir(struct ntfs_volume * vol,struct ntfs_inode * ndir,struct page * ia_page,struct index_entry * ie,u8 * name,struct dir_context * actor)592 static inline int ntfs_filldir(struct ntfs_volume *vol,
593 struct ntfs_inode *ndir, struct page *ia_page, struct index_entry *ie,
594 u8 *name, struct dir_context *actor)
595 {
596 unsigned long mref;
597 int name_len;
598 unsigned int dt_type;
599 u8 name_type;
600
601 name_type = ie->key.file_name.file_name_type;
602 if (name_type == FILE_NAME_DOS) {
603 ntfs_debug("Skipping DOS name space entry.");
604 return 0;
605 }
606 if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
607 ntfs_debug("Skipping root directory self reference entry.");
608 return 0;
609 }
610 if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
611 !NVolShowSystemFiles(vol)) {
612 ntfs_debug("Skipping system file.");
613 return 0;
614 }
615 if (!NVolShowHiddenFiles(vol) &&
616 (ie->key.file_name.file_attributes & FILE_ATTR_HIDDEN)) {
617 ntfs_debug("Skipping hidden file.");
618 return 0;
619 }
620
621 name_len = ntfs_ucstonls(vol, (__le16 *)&ie->key.file_name.file_name,
622 ie->key.file_name.file_name_length, &name,
623 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
624 if (name_len <= 0) {
625 ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
626 (long long)MREF_LE(ie->data.dir.indexed_file));
627 return 0;
628 }
629
630 mref = MREF_LE(ie->data.dir.indexed_file);
631 if (ie->key.file_name.file_attributes &
632 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
633 dt_type = DT_DIR;
634 else if (ie->key.file_name.file_attributes & FILE_ATTR_REPARSE_POINT)
635 dt_type = ntfs_reparse_tag_dt_types(vol, mref);
636 else
637 dt_type = DT_REG;
638
639 /*
640 * Drop the page lock otherwise we deadlock with NFS when it calls
641 * ->lookup since ntfs_lookup() will lock the same page.
642 */
643 if (ia_page)
644 unlock_page(ia_page);
645 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode 0x%lx, DT_%s.",
646 name, name_len, actor->pos, mref, dt_type == DT_DIR ? "DIR" : "REG");
647 if (!dir_emit(actor, name, name_len, mref, dt_type))
648 return 1;
649 /* Relock the page but not if we are aborting ->readdir. */
650 if (ia_page)
651 lock_page(ia_page);
652 return 0;
653 }
654
655 struct ntfs_file_private {
656 void *key;
657 __le16 key_length;
658 bool end_in_iterate;
659 loff_t curr_pos;
660 };
661
662 struct ntfs_index_ra {
663 unsigned long start_index;
664 unsigned int count;
665 struct rb_node rb_node;
666 };
667
ntfs_insert_rb(struct ntfs_index_ra * nir,struct rb_root * root)668 static void ntfs_insert_rb(struct ntfs_index_ra *nir, struct rb_root *root)
669 {
670 struct rb_node **new = &root->rb_node, *parent = NULL;
671 struct ntfs_index_ra *cnir;
672
673 while (*new) {
674 parent = *new;
675 cnir = rb_entry(parent, struct ntfs_index_ra, rb_node);
676 if (nir->start_index < cnir->start_index)
677 new = &parent->rb_left;
678 else if (nir->start_index >= cnir->start_index + cnir->count)
679 new = &parent->rb_right;
680 else {
681 pr_err("nir start index : %ld, count : %d, cnir start_index : %ld, count : %d\n",
682 nir->start_index, nir->count, cnir->start_index, cnir->count);
683 return;
684 }
685 }
686
687 rb_link_node(&nir->rb_node, parent, new);
688 rb_insert_color(&nir->rb_node, root);
689 }
690
ntfs_ia_blocks_readahead(struct ntfs_inode * ia_ni,loff_t pos)691 static int ntfs_ia_blocks_readahead(struct ntfs_inode *ia_ni, loff_t pos)
692 {
693 unsigned long dir_start_index, dir_end_index;
694 struct inode *ia_vi = VFS_I(ia_ni);
695 struct file_ra_state *dir_ra;
696
697 dir_end_index = (i_size_read(ia_vi) + PAGE_SIZE - 1) >> PAGE_SHIFT;
698 dir_start_index = (pos + PAGE_SIZE - 1) >> PAGE_SHIFT;
699
700 if (dir_start_index >= dir_end_index)
701 return 0;
702
703 dir_ra = kzalloc(sizeof(*dir_ra), GFP_NOFS);
704 if (!dir_ra)
705 return -ENOMEM;
706
707 file_ra_state_init(dir_ra, ia_vi->i_mapping);
708 dir_end_index = (i_size_read(ia_vi) + PAGE_SIZE - 1) >> PAGE_SHIFT;
709 dir_start_index = (pos + PAGE_SIZE - 1) >> PAGE_SHIFT;
710 dir_ra->ra_pages = dir_end_index - dir_start_index;
711 page_cache_sync_readahead(ia_vi->i_mapping, dir_ra, NULL,
712 dir_start_index, dir_end_index - dir_start_index);
713 kfree(dir_ra);
714
715 return 0;
716 }
717
ntfs_readdir(struct file * file,struct dir_context * actor)718 static int ntfs_readdir(struct file *file, struct dir_context *actor)
719 {
720 struct inode *vdir = file_inode(file);
721 struct super_block *sb = vdir->i_sb;
722 struct ntfs_inode *ndir = NTFS_I(vdir);
723 struct ntfs_volume *vol = NTFS_SB(sb);
724 struct ntfs_attr_search_ctx *ctx = NULL;
725 struct ntfs_index_context *ictx = NULL;
726 u8 *name;
727 struct index_root *ir;
728 struct index_entry *next = NULL;
729 struct ntfs_file_private *private = NULL;
730 int err = 0;
731 loff_t ie_pos = 2; /* initialize it with dot and dotdot size */
732 struct ntfs_index_ra *nir = NULL;
733 unsigned long index;
734 struct rb_root ra_root = RB_ROOT;
735 struct file_ra_state *ra;
736
737 ntfs_debug("Entering for inode 0x%llx, fpos 0x%llx.",
738 ndir->mft_no, actor->pos);
739
740 if (file->private_data) {
741 private = file->private_data;
742
743 if (actor->pos != private->curr_pos) {
744 /*
745 * If actor->pos is different from the previous passed
746 * one, Discard the private->key and fill dirent buffer
747 * with linear lookup.
748 */
749 kfree(private->key);
750 private->key = NULL;
751 private->end_in_iterate = false;
752 } else if (private->end_in_iterate) {
753 kfree(private->key);
754 kfree(file->private_data);
755 file->private_data = NULL;
756 return 0;
757 }
758 }
759
760 /* Emulate . and .. for all directories. */
761 if (!dir_emit_dots(file, actor))
762 return 0;
763
764 /*
765 * Allocate a buffer to store the current name being processed
766 * converted to format determined by current NLS.
767 */
768 name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS);
769 if (unlikely(!name))
770 return -ENOMEM;
771
772 mutex_lock_nested(&ndir->mrec_lock, NTFS_INODE_MUTEX_PARENT);
773 ictx = ntfs_index_ctx_get(ndir, I30, 4);
774 if (!ictx) {
775 kfree(name);
776 mutex_unlock(&ndir->mrec_lock);
777 return -ENOMEM;
778 }
779
780 ra = kzalloc(sizeof(struct file_ra_state), GFP_NOFS);
781 if (!ra) {
782 kfree(name);
783 ntfs_index_ctx_put(ictx);
784 mutex_unlock(&ndir->mrec_lock);
785 return -ENOMEM;
786 }
787 file_ra_state_init(ra, vol->mft_ino->i_mapping);
788
789 if (private && private->key) {
790 /*
791 * Find index witk private->key using ntfs_index_lookup()
792 * instead of linear index lookup.
793 */
794 err = ntfs_index_lookup(private->key,
795 le16_to_cpu(private->key_length),
796 ictx);
797 if (!err) {
798 next = ictx->entry;
799 /*
800 * Update ie_pos with private->curr_pos
801 * to make next d_off of dirent correct.
802 */
803 ie_pos = private->curr_pos;
804
805 if (actor->pos > vol->mft_record_size && ictx->ia_ni) {
806 err = ntfs_ia_blocks_readahead(ictx->ia_ni, actor->pos);
807 if (err)
808 goto out;
809 }
810
811 goto nextdir;
812 } else {
813 goto out;
814 }
815 } else if (!private) {
816 private = kzalloc(sizeof(struct ntfs_file_private), GFP_KERNEL);
817 if (!private) {
818 err = -ENOMEM;
819 goto out;
820 }
821 file->private_data = private;
822 }
823
824 ctx = ntfs_attr_get_search_ctx(ndir, NULL);
825 if (!ctx) {
826 err = -ENOMEM;
827 goto out;
828 }
829
830 /* Find the index root attribute in the mft record. */
831 if (ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0,
832 ctx)) {
833 ntfs_error(sb, "Index root attribute missing in directory inode %llu",
834 ndir->mft_no);
835 ntfs_attr_put_search_ctx(ctx);
836 err = -ENOMEM;
837 goto out;
838 }
839
840 /* Get to the index root value. */
841 ir = (struct index_root *)((u8 *)ctx->attr +
842 le16_to_cpu(ctx->attr->data.resident.value_offset));
843
844 ictx->ir = ir;
845 ictx->actx = ctx;
846 ictx->parent_vcn[ictx->pindex] = VCN_INDEX_ROOT_PARENT;
847 ictx->is_in_root = true;
848 ictx->parent_pos[ictx->pindex] = 0;
849
850 ictx->block_size = le32_to_cpu(ir->index_block_size);
851 if (ictx->block_size < NTFS_BLOCK_SIZE) {
852 ntfs_error(sb, "Index block size (%d) is smaller than the sector size (%d)",
853 ictx->block_size, NTFS_BLOCK_SIZE);
854 err = -EIO;
855 goto out;
856 }
857
858 if (vol->cluster_size <= ictx->block_size)
859 ictx->vcn_size_bits = vol->cluster_size_bits;
860 else
861 ictx->vcn_size_bits = NTFS_BLOCK_SIZE_BITS;
862 ictx->cr = ir->collation_rule;
863
864 /* The first index entry. */
865 next = (struct index_entry *)((u8 *)&ir->index +
866 le32_to_cpu(ir->index.entries_offset));
867
868 if (next->flags & INDEX_ENTRY_NODE) {
869 ictx->ia_ni = ntfs_ia_open(ictx, ictx->idx_ni);
870 if (!ictx->ia_ni) {
871 err = -EINVAL;
872 goto out;
873 }
874
875 err = ntfs_ia_blocks_readahead(ictx->ia_ni, actor->pos);
876 if (err)
877 goto out;
878 }
879
880 if (next->flags & INDEX_ENTRY_NODE) {
881 next = ntfs_index_walk_down(next, ictx);
882 if (IS_ERR(next)) {
883 err = PTR_ERR(next);
884 goto out;
885 }
886 }
887
888 if (next && !(next->flags & INDEX_ENTRY_END))
889 goto nextdir;
890
891 while (1) {
892 next = ntfs_index_next(next, ictx);
893 if (IS_ERR(next)) {
894 err = PTR_ERR(next);
895 goto out;
896 }
897 if (!next)
898 break;
899 nextdir:
900 if (ie_pos < actor->pos) {
901 ie_pos += le16_to_cpu(next->length);
902 continue;
903 }
904
905 actor->pos = ie_pos;
906
907 index = ntfs_mft_no_to_pidx(vol,
908 MREF_LE(next->data.dir.indexed_file));
909 if (nir) {
910 struct ntfs_index_ra *cnir;
911 struct rb_node *node = ra_root.rb_node;
912
913 if (nir->start_index <= index &&
914 index < nir->start_index + nir->count) {
915 /* No behavior */
916 goto filldir;
917 }
918
919 while (node) {
920 cnir = rb_entry(node, struct ntfs_index_ra, rb_node);
921 if (cnir->start_index <= index &&
922 index < cnir->start_index + cnir->count) {
923 goto filldir;
924 } else if (cnir->start_index + cnir->count == index) {
925 cnir->count++;
926 goto filldir;
927 } else if (!cnir->start_index && cnir->start_index - 1 == index) {
928 cnir->start_index = index;
929 goto filldir;
930 }
931
932 if (index < cnir->start_index)
933 node = node->rb_left;
934 else if (index >= cnir->start_index + cnir->count)
935 node = node->rb_right;
936 }
937
938 if (nir->start_index + nir->count == index) {
939 nir->count++;
940 } else if (!nir->start_index && nir->start_index - 1 == index) {
941 nir->start_index = index;
942 } else if (nir->count > 2) {
943 ntfs_insert_rb(nir, &ra_root);
944 nir = NULL;
945 } else {
946 nir->start_index = index;
947 nir->count = 1;
948 }
949 }
950
951 if (!nir) {
952 nir = kzalloc(sizeof(struct ntfs_index_ra), GFP_KERNEL);
953 if (nir) {
954 nir->start_index = index;
955 nir->count = 1;
956 }
957 }
958
959 filldir:
960 /* Submit the name to the filldir callback. */
961 err = ntfs_filldir(vol, ndir, NULL, next, name, actor);
962 if (err) {
963 /*
964 * Store index key value to file private_data to start
965 * from current index offset on next round.
966 */
967 private = file->private_data;
968 kfree(private->key);
969 private->key = kmalloc(le16_to_cpu(next->key_length), GFP_KERNEL);
970 if (!private->key) {
971 err = -ENOMEM;
972 goto out;
973 }
974
975 memcpy(private->key, &next->key.file_name, le16_to_cpu(next->key_length));
976 private->key_length = next->key_length;
977 break;
978 }
979 ie_pos += le16_to_cpu(next->length);
980 }
981
982 if (!err)
983 private->end_in_iterate = true;
984 else
985 err = 0;
986
987 private->curr_pos = actor->pos = ie_pos;
988 out:
989 while (!RB_EMPTY_ROOT(&ra_root)) {
990 struct ntfs_index_ra *cnir;
991 struct rb_node *node;
992
993 node = rb_first(&ra_root);
994 cnir = rb_entry(node, struct ntfs_index_ra, rb_node);
995 ra->ra_pages = cnir->count;
996 page_cache_sync_readahead(vol->mft_ino->i_mapping, ra, NULL,
997 cnir->start_index, cnir->count);
998 rb_erase(node, &ra_root);
999 kfree(cnir);
1000 }
1001
1002 if (err) {
1003 if (private) {
1004 private->curr_pos = actor->pos;
1005 private->end_in_iterate = true;
1006 }
1007 err = 0;
1008 }
1009 ntfs_index_ctx_put(ictx);
1010 kfree(name);
1011 kfree(nir);
1012 kfree(ra);
1013 mutex_unlock(&ndir->mrec_lock);
1014 return err;
1015 }
1016
ntfs_check_empty_dir(struct ntfs_inode * ni,struct mft_record * ni_mrec)1017 int ntfs_check_empty_dir(struct ntfs_inode *ni, struct mft_record *ni_mrec)
1018 {
1019 struct ntfs_attr_search_ctx *ctx;
1020 int ret = 0;
1021
1022 if (!(ni_mrec->flags & MFT_RECORD_IS_DIRECTORY))
1023 return 0;
1024
1025 ctx = ntfs_attr_get_search_ctx(ni, NULL);
1026 if (!ctx) {
1027 ntfs_error(ni->vol->sb, "Failed to get search context");
1028 return -ENOMEM;
1029 }
1030
1031 /* Find the index root attribute in the mft record. */
1032 ret = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
1033 0, ctx);
1034 if (ret) {
1035 ntfs_error(ni->vol->sb, "Index root attribute missing in directory inode %llu",
1036 ni->mft_no);
1037 ntfs_attr_put_search_ctx(ctx);
1038 return ret;
1039 }
1040
1041 /* Non-empty directory? */
1042 if (le32_to_cpu(ctx->attr->data.resident.value_length) !=
1043 sizeof(struct index_root) + sizeof(struct index_entry_header)) {
1044 /* Both ENOTEMPTY and EEXIST are ok. We use the more common. */
1045 ret = -ENOTEMPTY;
1046 ntfs_debug("Directory is not empty\n");
1047 }
1048
1049 ntfs_attr_put_search_ctx(ctx);
1050
1051 return ret;
1052 }
1053
1054 /*
1055 * ntfs_dir_open - called when an inode is about to be opened
1056 * @vi: inode to be opened
1057 * @filp: file structure describing the inode
1058 *
1059 * Limit directory size to the page cache limit on architectures where unsigned
1060 * long is 32-bits. This is the most we can do for now without overflowing the
1061 * page cache page index. Doing it this way means we don't run into problems
1062 * because of existing too large directories. It would be better to allow the
1063 * user to read the accessible part of the directory but I doubt very much
1064 * anyone is going to hit this check on a 32-bit architecture, so there is no
1065 * point in adding the extra complexity required to support this.
1066 *
1067 * On 64-bit architectures, the check is hopefully optimized away by the
1068 * compiler.
1069 */
ntfs_dir_open(struct inode * vi,struct file * filp)1070 static int ntfs_dir_open(struct inode *vi, struct file *filp)
1071 {
1072 if (sizeof(unsigned long) < 8) {
1073 if (i_size_read(vi) > MAX_LFS_FILESIZE)
1074 return -EFBIG;
1075 }
1076 return 0;
1077 }
1078
ntfs_dir_release(struct inode * vi,struct file * filp)1079 static int ntfs_dir_release(struct inode *vi, struct file *filp)
1080 {
1081 if (filp->private_data) {
1082 kfree(((struct ntfs_file_private *)filp->private_data)->key);
1083 kfree(filp->private_data);
1084 filp->private_data = NULL;
1085 }
1086 return 0;
1087 }
1088
1089 /*
1090 * ntfs_dir_fsync - sync a directory to disk
1091 * @filp: file describing the directory to be synced
1092 * @start: start offset to be synced
1093 * @end: end offset to be synced
1094 * @datasync: if non-zero only flush user data and not metadata
1095 *
1096 * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and
1097 * msync system calls. This function is based on file.c::ntfs_file_fsync().
1098 *
1099 * Write the mft record and all associated extent mft records as well as the
1100 * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
1101 *
1102 * If @datasync is true, we do not wait on the inode(s) to be written out
1103 * but we always wait on the page cache pages to be written out.
1104 *
1105 * Note: In the past @filp could be NULL so we ignore it as we don't need it
1106 * anyway.
1107 *
1108 * Locking: Caller must hold i_mutex on the inode.
1109 */
ntfs_dir_fsync(struct file * filp,loff_t start,loff_t end,int datasync)1110 static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
1111 int datasync)
1112 {
1113 struct inode *bmp_vi, *vi = filp->f_mapping->host;
1114 struct ntfs_volume *vol = NTFS_I(vi)->vol;
1115 struct ntfs_inode *ni = NTFS_I(vi);
1116 struct ntfs_attr_search_ctx *ctx;
1117 struct inode *parent_vi, *ia_vi;
1118 int err, ret;
1119 struct ntfs_attr na;
1120
1121 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
1122
1123 if (NVolShutdown(vol))
1124 return -EIO;
1125
1126 ctx = ntfs_attr_get_search_ctx(ni, NULL);
1127 if (!ctx)
1128 return -ENOMEM;
1129
1130 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_CHILD);
1131 while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx))) {
1132 struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr +
1133 le16_to_cpu(ctx->attr->data.resident.value_offset));
1134
1135 if (MREF_LE(fn->parent_directory) == ni->mft_no)
1136 continue;
1137
1138 parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory));
1139 if (IS_ERR(parent_vi))
1140 continue;
1141 mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1142 ia_vi = ntfs_index_iget(parent_vi, I30, 4);
1143 mutex_unlock(&NTFS_I(parent_vi)->mrec_lock);
1144 if (IS_ERR(ia_vi)) {
1145 iput(parent_vi);
1146 continue;
1147 }
1148 write_inode_now(ia_vi, 1);
1149 iput(ia_vi);
1150 write_inode_now(parent_vi, 1);
1151 iput(parent_vi);
1152 }
1153 mutex_unlock(&ni->mrec_lock);
1154 ntfs_attr_put_search_ctx(ctx);
1155
1156 err = file_write_and_wait_range(filp, start, end);
1157 if (err)
1158 return err;
1159 inode_lock(vi);
1160
1161 /* If the bitmap attribute inode is in memory sync it, too. */
1162 na.mft_no = vi->i_ino;
1163 na.type = AT_BITMAP;
1164 na.name = I30;
1165 na.name_len = 4;
1166 bmp_vi = ilookup5(vi->i_sb, vi->i_ino, ntfs_test_inode, &na);
1167 if (bmp_vi) {
1168 write_inode_now(bmp_vi, !datasync);
1169 iput(bmp_vi);
1170 }
1171 ret = __ntfs_write_inode(vi, 1);
1172
1173 write_inode_now(vi, !datasync);
1174
1175 write_inode_now(vol->mftbmp_ino, 1);
1176 down_write(&vol->lcnbmp_lock);
1177 write_inode_now(vol->lcnbmp_ino, 1);
1178 up_write(&vol->lcnbmp_lock);
1179 write_inode_now(vol->mft_ino, 1);
1180
1181 err = sync_blockdev(vi->i_sb->s_bdev);
1182 if (unlikely(err && !ret))
1183 ret = err;
1184 if (likely(!ret))
1185 ntfs_debug("Done.");
1186 else
1187 ntfs_warning(vi->i_sb,
1188 "Failed to f%ssync inode 0x%llx. Error %u.",
1189 datasync ? "data" : "", ni->mft_no, -ret);
1190 inode_unlock(vi);
1191 return ret;
1192 }
1193
1194 const struct file_operations ntfs_dir_ops = {
1195 .llseek = generic_file_llseek, /* Seek inside directory. */
1196 .read = generic_read_dir, /* Return -EISDIR. */
1197 .iterate_shared = ntfs_readdir, /* Read directory contents. */
1198 .fsync = ntfs_dir_fsync, /* Sync a directory to disk. */
1199 .open = ntfs_dir_open, /* Open directory. */
1200 .release = ntfs_dir_release,
1201 .unlocked_ioctl = ntfs_ioctl,
1202 #ifdef CONFIG_COMPAT
1203 .compat_ioctl = ntfs_compat_ioctl,
1204 #endif
1205 .setlease = generic_setlease,
1206 };
1207