1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/buffer_head.h>
24#include <linux/slab.h>
25
26#include "dir.h"
27#include "aops.h"
28#include "attrib.h"
29#include "mft.h"
30#include "debug.h"
31#include "ntfs.h"
32
33
34
35
36ntfschar I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
37 cpu_to_le16('3'), cpu_to_le16('0'), 0 };
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
81 const int uname_len, ntfs_name **res)
82{
83 ntfs_volume *vol = dir_ni->vol;
84 struct super_block *sb = vol->sb;
85 MFT_RECORD *m;
86 INDEX_ROOT *ir;
87 INDEX_ENTRY *ie;
88 INDEX_ALLOCATION *ia;
89 u8 *index_end;
90 u64 mref;
91 ntfs_attr_search_ctx *ctx;
92 int err, rc;
93 VCN vcn, old_vcn;
94 struct address_space *ia_mapping;
95 struct page *page;
96 u8 *kaddr;
97 ntfs_name *name = NULL;
98
99 BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode));
100 BUG_ON(NInoAttr(dir_ni));
101
102 m = map_mft_record(dir_ni);
103 if (IS_ERR(m)) {
104 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
105 -PTR_ERR(m));
106 return ERR_MREF(PTR_ERR(m));
107 }
108 ctx = ntfs_attr_get_search_ctx(dir_ni, m);
109 if (unlikely(!ctx)) {
110 err = -ENOMEM;
111 goto err_out;
112 }
113
114 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
115 0, ctx);
116 if (unlikely(err)) {
117 if (err == -ENOENT) {
118 ntfs_error(sb, "Index root attribute missing in "
119 "directory inode 0x%lx.",
120 dir_ni->mft_no);
121 err = -EIO;
122 }
123 goto err_out;
124 }
125
126 ir = (INDEX_ROOT*)((u8*)ctx->attr +
127 le16_to_cpu(ctx->attr->data.resident.value_offset));
128 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
129
130 ie = (INDEX_ENTRY*)((u8*)&ir->index +
131 le32_to_cpu(ir->index.entries_offset));
132
133
134
135
136 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
137
138 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
139 sizeof(INDEX_ENTRY_HEADER) > index_end ||
140 (u8*)ie + le16_to_cpu(ie->key_length) >
141 index_end)
142 goto dir_err_out;
143
144
145
146
147 if (ie->flags & INDEX_ENTRY_END)
148 break;
149
150
151
152
153
154
155
156 if (ntfs_are_names_equal(uname, uname_len,
157 (ntfschar*)&ie->key.file_name.file_name,
158 ie->key.file_name.file_name_length,
159 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
160found_it:
161
162
163
164
165
166
167
168
169
170
171 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
172 if (!name) {
173 name = kmalloc(sizeof(ntfs_name),
174 GFP_NOFS);
175 if (!name) {
176 err = -ENOMEM;
177 goto err_out;
178 }
179 }
180 name->mref = le64_to_cpu(
181 ie->data.dir.indexed_file);
182 name->type = FILE_NAME_DOS;
183 name->len = 0;
184 *res = name;
185 } else {
186 kfree(name);
187 *res = NULL;
188 }
189 mref = le64_to_cpu(ie->data.dir.indexed_file);
190 ntfs_attr_put_search_ctx(ctx);
191 unmap_mft_record(dir_ni);
192 return mref;
193 }
194
195
196
197
198
199
200
201
202
203
204 if (!NVolCaseSensitive(vol) &&
205 ie->key.file_name.file_name_type &&
206 ntfs_are_names_equal(uname, uname_len,
207 (ntfschar*)&ie->key.file_name.file_name,
208 ie->key.file_name.file_name_length,
209 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
210 int name_size = sizeof(ntfs_name);
211 u8 type = ie->key.file_name.file_name_type;
212 u8 len = ie->key.file_name.file_name_length;
213
214
215 if (name) {
216 ntfs_error(sb, "Found already allocated name "
217 "in phase 1. Please run chkdsk "
218 "and if that doesn't find any "
219 "errors please report you saw "
220 "this message to "
221 "linux-ntfs-dev@lists."
222 "sourceforge.net.");
223 goto dir_err_out;
224 }
225
226 if (type != FILE_NAME_DOS)
227 name_size += len * sizeof(ntfschar);
228 name = kmalloc(name_size, GFP_NOFS);
229 if (!name) {
230 err = -ENOMEM;
231 goto err_out;
232 }
233 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
234 name->type = type;
235 if (type != FILE_NAME_DOS) {
236 name->len = len;
237 memcpy(name->name, ie->key.file_name.file_name,
238 len * sizeof(ntfschar));
239 } else
240 name->len = 0;
241 *res = name;
242 }
243
244
245
246
247 rc = ntfs_collate_names(uname, uname_len,
248 (ntfschar*)&ie->key.file_name.file_name,
249 ie->key.file_name.file_name_length, 1,
250 IGNORE_CASE, vol->upcase, vol->upcase_len);
251
252
253
254
255
256 if (rc == -1)
257 break;
258
259 if (rc)
260 continue;
261
262
263
264
265
266 rc = ntfs_collate_names(uname, uname_len,
267 (ntfschar*)&ie->key.file_name.file_name,
268 ie->key.file_name.file_name_length, 1,
269 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
270 if (rc == -1)
271 break;
272 if (rc)
273 continue;
274
275
276
277
278
279 goto found_it;
280 }
281
282
283
284
285
286
287 if (!(ie->flags & INDEX_ENTRY_NODE)) {
288 if (name) {
289 ntfs_attr_put_search_ctx(ctx);
290 unmap_mft_record(dir_ni);
291 return name->mref;
292 }
293 ntfs_debug("Entry not found.");
294 err = -ENOENT;
295 goto err_out;
296 }
297
298 if (!NInoIndexAllocPresent(dir_ni)) {
299 ntfs_error(sb, "No index allocation attribute but index entry "
300 "requires one. Directory inode 0x%lx is "
301 "corrupt or driver bug.", dir_ni->mft_no);
302 goto err_out;
303 }
304
305 vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
306 ia_mapping = VFS_I(dir_ni)->i_mapping;
307
308
309
310
311 ntfs_attr_put_search_ctx(ctx);
312 unmap_mft_record(dir_ni);
313 m = NULL;
314 ctx = NULL;
315descend_into_child_node:
316
317
318
319
320
321 page = ntfs_map_page(ia_mapping, vcn <<
322 dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
323 if (IS_ERR(page)) {
324 ntfs_error(sb, "Failed to map directory index page, error %ld.",
325 -PTR_ERR(page));
326 err = PTR_ERR(page);
327 goto err_out;
328 }
329 lock_page(page);
330 kaddr = (u8*)page_address(page);
331fast_descend_into_child_node:
332
333 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
334 dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
335
336 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
337 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
338 "inode 0x%lx or driver bug.", dir_ni->mft_no);
339 goto unm_err_out;
340 }
341
342 if (unlikely(!ntfs_is_indx_record(ia->magic))) {
343 ntfs_error(sb, "Directory index record with vcn 0x%llx is "
344 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
345 (unsigned long long)vcn, dir_ni->mft_no);
346 goto unm_err_out;
347 }
348 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
349 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
350 "different from expected VCN (0x%llx). "
351 "Directory inode 0x%lx is corrupt or driver "
352 "bug.", (unsigned long long)
353 sle64_to_cpu(ia->index_block_vcn),
354 (unsigned long long)vcn, dir_ni->mft_no);
355 goto unm_err_out;
356 }
357 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
358 dir_ni->itype.index.block_size) {
359 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
360 "0x%lx has a size (%u) differing from the "
361 "directory specified size (%u). Directory "
362 "inode is corrupt or driver bug.",
363 (unsigned long long)vcn, dir_ni->mft_no,
364 le32_to_cpu(ia->index.allocated_size) + 0x18,
365 dir_ni->itype.index.block_size);
366 goto unm_err_out;
367 }
368 index_end = (u8*)ia + dir_ni->itype.index.block_size;
369 if (index_end > kaddr + PAGE_CACHE_SIZE) {
370 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
371 "0x%lx crosses page boundary. Impossible! "
372 "Cannot access! This is probably a bug in the "
373 "driver.", (unsigned long long)vcn,
374 dir_ni->mft_no);
375 goto unm_err_out;
376 }
377 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
378 if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
379 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
380 "inode 0x%lx exceeds maximum size.",
381 (unsigned long long)vcn, dir_ni->mft_no);
382 goto unm_err_out;
383 }
384
385 ie = (INDEX_ENTRY*)((u8*)&ia->index +
386 le32_to_cpu(ia->index.entries_offset));
387
388
389
390
391
392 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
393
394 if ((u8*)ie < (u8*)ia || (u8*)ie +
395 sizeof(INDEX_ENTRY_HEADER) > index_end ||
396 (u8*)ie + le16_to_cpu(ie->key_length) >
397 index_end) {
398 ntfs_error(sb, "Index entry out of bounds in "
399 "directory inode 0x%lx.",
400 dir_ni->mft_no);
401 goto unm_err_out;
402 }
403
404
405
406
407 if (ie->flags & INDEX_ENTRY_END)
408 break;
409
410
411
412
413
414
415
416 if (ntfs_are_names_equal(uname, uname_len,
417 (ntfschar*)&ie->key.file_name.file_name,
418 ie->key.file_name.file_name_length,
419 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
420found_it2:
421
422
423
424
425
426
427
428
429
430
431 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
432 if (!name) {
433 name = kmalloc(sizeof(ntfs_name),
434 GFP_NOFS);
435 if (!name) {
436 err = -ENOMEM;
437 goto unm_err_out;
438 }
439 }
440 name->mref = le64_to_cpu(
441 ie->data.dir.indexed_file);
442 name->type = FILE_NAME_DOS;
443 name->len = 0;
444 *res = name;
445 } else {
446 kfree(name);
447 *res = NULL;
448 }
449 mref = le64_to_cpu(ie->data.dir.indexed_file);
450 unlock_page(page);
451 ntfs_unmap_page(page);
452 return mref;
453 }
454
455
456
457
458
459
460
461
462
463
464 if (!NVolCaseSensitive(vol) &&
465 ie->key.file_name.file_name_type &&
466 ntfs_are_names_equal(uname, uname_len,
467 (ntfschar*)&ie->key.file_name.file_name,
468 ie->key.file_name.file_name_length,
469 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
470 int name_size = sizeof(ntfs_name);
471 u8 type = ie->key.file_name.file_name_type;
472 u8 len = ie->key.file_name.file_name_length;
473
474
475 if (name) {
476 ntfs_error(sb, "Found already allocated name "
477 "in phase 2. Please run chkdsk "
478 "and if that doesn't find any "
479 "errors please report you saw "
480 "this message to "
481 "linux-ntfs-dev@lists."
482 "sourceforge.net.");
483 unlock_page(page);
484 ntfs_unmap_page(page);
485 goto dir_err_out;
486 }
487
488 if (type != FILE_NAME_DOS)
489 name_size += len * sizeof(ntfschar);
490 name = kmalloc(name_size, GFP_NOFS);
491 if (!name) {
492 err = -ENOMEM;
493 goto unm_err_out;
494 }
495 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
496 name->type = type;
497 if (type != FILE_NAME_DOS) {
498 name->len = len;
499 memcpy(name->name, ie->key.file_name.file_name,
500 len * sizeof(ntfschar));
501 } else
502 name->len = 0;
503 *res = name;
504 }
505
506
507
508
509 rc = ntfs_collate_names(uname, uname_len,
510 (ntfschar*)&ie->key.file_name.file_name,
511 ie->key.file_name.file_name_length, 1,
512 IGNORE_CASE, vol->upcase, vol->upcase_len);
513
514
515
516
517
518 if (rc == -1)
519 break;
520
521 if (rc)
522 continue;
523
524
525
526
527
528 rc = ntfs_collate_names(uname, uname_len,
529 (ntfschar*)&ie->key.file_name.file_name,
530 ie->key.file_name.file_name_length, 1,
531 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
532 if (rc == -1)
533 break;
534 if (rc)
535 continue;
536
537
538
539
540
541 goto found_it2;
542 }
543
544
545
546
547 if (ie->flags & INDEX_ENTRY_NODE) {
548 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
549 ntfs_error(sb, "Index entry with child node found in "
550 "a leaf node in directory inode 0x%lx.",
551 dir_ni->mft_no);
552 goto unm_err_out;
553 }
554
555 old_vcn = vcn;
556 vcn = sle64_to_cpup((sle64*)((u8*)ie +
557 le16_to_cpu(ie->length) - 8));
558 if (vcn >= 0) {
559
560
561 if (old_vcn << vol->cluster_size_bits >>
562 PAGE_CACHE_SHIFT == vcn <<
563 vol->cluster_size_bits >>
564 PAGE_CACHE_SHIFT)
565 goto fast_descend_into_child_node;
566 unlock_page(page);
567 ntfs_unmap_page(page);
568 goto descend_into_child_node;
569 }
570 ntfs_error(sb, "Negative child node vcn in directory inode "
571 "0x%lx.", dir_ni->mft_no);
572 goto unm_err_out;
573 }
574
575
576
577
578
579 if (name) {
580 unlock_page(page);
581 ntfs_unmap_page(page);
582 return name->mref;
583 }
584 ntfs_debug("Entry not found.");
585 err = -ENOENT;
586unm_err_out:
587 unlock_page(page);
588 ntfs_unmap_page(page);
589err_out:
590 if (!err)
591 err = -EIO;
592 if (ctx)
593 ntfs_attr_put_search_ctx(ctx);
594 if (m)
595 unmap_mft_record(dir_ni);
596 if (name) {
597 kfree(name);
598 *res = NULL;
599 }
600 return ERR_MREF(err);
601dir_err_out:
602 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
603 goto err_out;
604}
605
606#if 0
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
634 const int uname_len)
635{
636 ntfs_volume *vol = dir_ni->vol;
637 struct super_block *sb = vol->sb;
638 MFT_RECORD *m;
639 INDEX_ROOT *ir;
640 INDEX_ENTRY *ie;
641 INDEX_ALLOCATION *ia;
642 u8 *index_end;
643 u64 mref;
644 ntfs_attr_search_ctx *ctx;
645 int err, rc;
646 IGNORE_CASE_BOOL ic;
647 VCN vcn, old_vcn;
648 struct address_space *ia_mapping;
649 struct page *page;
650 u8 *kaddr;
651
652
653 m = map_mft_record(dir_ni);
654 if (IS_ERR(m)) {
655 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
656 -PTR_ERR(m));
657 return ERR_MREF(PTR_ERR(m));
658 }
659 ctx = ntfs_attr_get_search_ctx(dir_ni, m);
660 if (!ctx) {
661 err = -ENOMEM;
662 goto err_out;
663 }
664
665 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
666 0, ctx);
667 if (unlikely(err)) {
668 if (err == -ENOENT) {
669 ntfs_error(sb, "Index root attribute missing in "
670 "directory inode 0x%lx.",
671 dir_ni->mft_no);
672 err = -EIO;
673 }
674 goto err_out;
675 }
676
677 ir = (INDEX_ROOT*)((u8*)ctx->attr +
678 le16_to_cpu(ctx->attr->data.resident.value_offset));
679 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
680
681 ie = (INDEX_ENTRY*)((u8*)&ir->index +
682 le32_to_cpu(ir->index.entries_offset));
683
684
685
686
687 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
688
689 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
690 sizeof(INDEX_ENTRY_HEADER) > index_end ||
691 (u8*)ie + le16_to_cpu(ie->key_length) >
692 index_end)
693 goto dir_err_out;
694
695
696
697
698 if (ie->flags & INDEX_ENTRY_END)
699 break;
700
701
702
703
704
705
706
707
708
709
710 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
711 CASE_SENSITIVE;
712
713
714
715
716
717
718 if (ntfs_are_names_equal(uname, uname_len,
719 (ntfschar*)&ie->key.file_name.file_name,
720 ie->key.file_name.file_name_length, ic,
721 vol->upcase, vol->upcase_len)) {
722found_it:
723 mref = le64_to_cpu(ie->data.dir.indexed_file);
724 ntfs_attr_put_search_ctx(ctx);
725 unmap_mft_record(dir_ni);
726 return mref;
727 }
728
729
730
731
732 rc = ntfs_collate_names(uname, uname_len,
733 (ntfschar*)&ie->key.file_name.file_name,
734 ie->key.file_name.file_name_length, 1,
735 IGNORE_CASE, vol->upcase, vol->upcase_len);
736
737
738
739
740
741 if (rc == -1)
742 break;
743
744 if (rc)
745 continue;
746
747
748
749
750
751 rc = ntfs_collate_names(uname, uname_len,
752 (ntfschar*)&ie->key.file_name.file_name,
753 ie->key.file_name.file_name_length, 1,
754 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
755 if (rc == -1)
756 break;
757 if (rc)
758 continue;
759
760
761
762
763
764 goto found_it;
765 }
766
767
768
769
770 if (!(ie->flags & INDEX_ENTRY_NODE)) {
771
772 err = -ENOENT;
773 goto err_out;
774 }
775
776 if (!NInoIndexAllocPresent(dir_ni)) {
777 ntfs_error(sb, "No index allocation attribute but index entry "
778 "requires one. Directory inode 0x%lx is "
779 "corrupt or driver bug.", dir_ni->mft_no);
780 goto err_out;
781 }
782
783 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
784 ia_mapping = VFS_I(dir_ni)->i_mapping;
785
786
787
788
789 ntfs_attr_put_search_ctx(ctx);
790 unmap_mft_record(dir_ni);
791 m = NULL;
792 ctx = NULL;
793descend_into_child_node:
794
795
796
797
798
799 page = ntfs_map_page(ia_mapping, vcn <<
800 dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
801 if (IS_ERR(page)) {
802 ntfs_error(sb, "Failed to map directory index page, error %ld.",
803 -PTR_ERR(page));
804 err = PTR_ERR(page);
805 goto err_out;
806 }
807 lock_page(page);
808 kaddr = (u8*)page_address(page);
809fast_descend_into_child_node:
810
811 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
812 dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
813
814 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
815 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
816 "inode 0x%lx or driver bug.", dir_ni->mft_no);
817 goto unm_err_out;
818 }
819
820 if (unlikely(!ntfs_is_indx_record(ia->magic))) {
821 ntfs_error(sb, "Directory index record with vcn 0x%llx is "
822 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
823 (unsigned long long)vcn, dir_ni->mft_no);
824 goto unm_err_out;
825 }
826 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
827 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
828 "different from expected VCN (0x%llx). "
829 "Directory inode 0x%lx is corrupt or driver "
830 "bug.", (unsigned long long)
831 sle64_to_cpu(ia->index_block_vcn),
832 (unsigned long long)vcn, dir_ni->mft_no);
833 goto unm_err_out;
834 }
835 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
836 dir_ni->itype.index.block_size) {
837 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
838 "0x%lx has a size (%u) differing from the "
839 "directory specified size (%u). Directory "
840 "inode is corrupt or driver bug.",
841 (unsigned long long)vcn, dir_ni->mft_no,
842 le32_to_cpu(ia->index.allocated_size) + 0x18,
843 dir_ni->itype.index.block_size);
844 goto unm_err_out;
845 }
846 index_end = (u8*)ia + dir_ni->itype.index.block_size;
847 if (index_end > kaddr + PAGE_CACHE_SIZE) {
848 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
849 "0x%lx crosses page boundary. Impossible! "
850 "Cannot access! This is probably a bug in the "
851 "driver.", (unsigned long long)vcn,
852 dir_ni->mft_no);
853 goto unm_err_out;
854 }
855 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
856 if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
857 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
858 "inode 0x%lx exceeds maximum size.",
859 (unsigned long long)vcn, dir_ni->mft_no);
860 goto unm_err_out;
861 }
862
863 ie = (INDEX_ENTRY*)((u8*)&ia->index +
864 le32_to_cpu(ia->index.entries_offset));
865
866
867
868
869
870 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
871
872 if ((u8*)ie < (u8*)ia || (u8*)ie +
873 sizeof(INDEX_ENTRY_HEADER) > index_end ||
874 (u8*)ie + le16_to_cpu(ie->key_length) >
875 index_end) {
876 ntfs_error(sb, "Index entry out of bounds in "
877 "directory inode 0x%lx.",
878 dir_ni->mft_no);
879 goto unm_err_out;
880 }
881
882
883
884
885 if (ie->flags & INDEX_ENTRY_END)
886 break;
887
888
889
890
891
892
893
894
895
896
897 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
898 CASE_SENSITIVE;
899
900
901
902
903
904
905 if (ntfs_are_names_equal(uname, uname_len,
906 (ntfschar*)&ie->key.file_name.file_name,
907 ie->key.file_name.file_name_length, ic,
908 vol->upcase, vol->upcase_len)) {
909found_it2:
910 mref = le64_to_cpu(ie->data.dir.indexed_file);
911 unlock_page(page);
912 ntfs_unmap_page(page);
913 return mref;
914 }
915
916
917
918
919 rc = ntfs_collate_names(uname, uname_len,
920 (ntfschar*)&ie->key.file_name.file_name,
921 ie->key.file_name.file_name_length, 1,
922 IGNORE_CASE, vol->upcase, vol->upcase_len);
923
924
925
926
927
928 if (rc == -1)
929 break;
930
931 if (rc)
932 continue;
933
934
935
936
937
938 rc = ntfs_collate_names(uname, uname_len,
939 (ntfschar*)&ie->key.file_name.file_name,
940 ie->key.file_name.file_name_length, 1,
941 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
942 if (rc == -1)
943 break;
944 if (rc)
945 continue;
946
947
948
949
950
951 goto found_it2;
952 }
953
954
955
956
957 if (ie->flags & INDEX_ENTRY_NODE) {
958 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
959 ntfs_error(sb, "Index entry with child node found in "
960 "a leaf node in directory inode 0x%lx.",
961 dir_ni->mft_no);
962 goto unm_err_out;
963 }
964
965 old_vcn = vcn;
966 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
967 if (vcn >= 0) {
968
969
970 if (old_vcn << vol->cluster_size_bits >>
971 PAGE_CACHE_SHIFT == vcn <<
972 vol->cluster_size_bits >>
973 PAGE_CACHE_SHIFT)
974 goto fast_descend_into_child_node;
975 unlock_page(page);
976 ntfs_unmap_page(page);
977 goto descend_into_child_node;
978 }
979 ntfs_error(sb, "Negative child node vcn in directory inode "
980 "0x%lx.", dir_ni->mft_no);
981 goto unm_err_out;
982 }
983
984 ntfs_debug("Entry not found.");
985 err = -ENOENT;
986unm_err_out:
987 unlock_page(page);
988 ntfs_unmap_page(page);
989err_out:
990 if (!err)
991 err = -EIO;
992 if (ctx)
993 ntfs_attr_put_search_ctx(ctx);
994 if (m)
995 unmap_mft_record(dir_ni);
996 return ERR_MREF(err);
997dir_err_out:
998 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
999 goto err_out;
1000}
1001
1002#endif
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027static inline int ntfs_filldir(ntfs_volume *vol, loff_t fpos,
1028 ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie,
1029 u8 *name, void *dirent, filldir_t filldir)
1030{
1031 unsigned long mref;
1032 int name_len, rc;
1033 unsigned dt_type;
1034 FILE_NAME_TYPE_FLAGS name_type;
1035
1036 name_type = ie->key.file_name.file_name_type;
1037 if (name_type == FILE_NAME_DOS) {
1038 ntfs_debug("Skipping DOS name space entry.");
1039 return 0;
1040 }
1041 if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
1042 ntfs_debug("Skipping root directory self reference entry.");
1043 return 0;
1044 }
1045 if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
1046 !NVolShowSystemFiles(vol)) {
1047 ntfs_debug("Skipping system file.");
1048 return 0;
1049 }
1050 name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name,
1051 ie->key.file_name.file_name_length, &name,
1052 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
1053 if (name_len <= 0) {
1054 ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
1055 (long long)MREF_LE(ie->data.dir.indexed_file));
1056 return 0;
1057 }
1058 if (ie->key.file_name.file_attributes &
1059 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
1060 dt_type = DT_DIR;
1061 else
1062 dt_type = DT_REG;
1063 mref = MREF_LE(ie->data.dir.indexed_file);
1064
1065
1066
1067
1068 if (ia_page)
1069 unlock_page(ia_page);
1070 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
1071 "0x%lx, DT_%s.", name, name_len, fpos, mref,
1072 dt_type == DT_DIR ? "DIR" : "REG");
1073 rc = filldir(dirent, name, name_len, fpos, mref, dt_type);
1074
1075 if (!rc && ia_page)
1076 lock_page(ia_page);
1077 return rc;
1078}
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1101{
1102 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
1103 loff_t fpos, i_size;
1104 struct inode *bmp_vi, *vdir = filp->f_path.dentry->d_inode;
1105 struct super_block *sb = vdir->i_sb;
1106 ntfs_inode *ndir = NTFS_I(vdir);
1107 ntfs_volume *vol = NTFS_SB(sb);
1108 MFT_RECORD *m;
1109 INDEX_ROOT *ir = NULL;
1110 INDEX_ENTRY *ie;
1111 INDEX_ALLOCATION *ia;
1112 u8 *name = NULL;
1113 int rc, err, ir_pos, cur_bmp_pos;
1114 struct address_space *ia_mapping, *bmp_mapping;
1115 struct page *bmp_page = NULL, *ia_page = NULL;
1116 u8 *kaddr, *bmp, *index_end;
1117 ntfs_attr_search_ctx *ctx;
1118
1119 fpos = filp->f_pos;
1120 ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
1121 vdir->i_ino, fpos);
1122 rc = err = 0;
1123
1124 i_size = i_size_read(vdir);
1125 if (fpos >= i_size + vol->mft_record_size)
1126 goto done;
1127
1128 if (!fpos) {
1129 ntfs_debug("Calling filldir for . with len 1, fpos 0x0, "
1130 "inode 0x%lx, DT_DIR.", vdir->i_ino);
1131 rc = filldir(dirent, ".", 1, fpos, vdir->i_ino, DT_DIR);
1132 if (rc)
1133 goto done;
1134 fpos++;
1135 }
1136 if (fpos == 1) {
1137 ntfs_debug("Calling filldir for .. with len 2, fpos 0x1, "
1138 "inode 0x%lx, DT_DIR.",
1139 (unsigned long)parent_ino(filp->f_path.dentry));
1140 rc = filldir(dirent, "..", 2, fpos,
1141 parent_ino(filp->f_path.dentry), DT_DIR);
1142 if (rc)
1143 goto done;
1144 fpos++;
1145 }
1146 m = NULL;
1147 ctx = NULL;
1148
1149
1150
1151
1152 name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS);
1153 if (unlikely(!name)) {
1154 err = -ENOMEM;
1155 goto err_out;
1156 }
1157
1158 if (fpos >= vol->mft_record_size)
1159 goto skip_index_root;
1160
1161 m = map_mft_record(ndir);
1162 if (IS_ERR(m)) {
1163 err = PTR_ERR(m);
1164 m = NULL;
1165 goto err_out;
1166 }
1167 ctx = ntfs_attr_get_search_ctx(ndir, m);
1168 if (unlikely(!ctx)) {
1169 err = -ENOMEM;
1170 goto err_out;
1171 }
1172
1173 ir_pos = (s64)fpos;
1174
1175 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
1176 0, ctx);
1177 if (unlikely(err)) {
1178 ntfs_error(sb, "Index root attribute missing in directory "
1179 "inode 0x%lx.", vdir->i_ino);
1180 goto err_out;
1181 }
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192 rc = le32_to_cpu(ctx->attr->data.resident.value_length);
1193 ir = kmalloc(rc, GFP_NOFS);
1194 if (unlikely(!ir)) {
1195 err = -ENOMEM;
1196 goto err_out;
1197 }
1198
1199 memcpy(ir, (u8*)ctx->attr +
1200 le16_to_cpu(ctx->attr->data.resident.value_offset), rc);
1201 ntfs_attr_put_search_ctx(ctx);
1202 unmap_mft_record(ndir);
1203 ctx = NULL;
1204 m = NULL;
1205 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
1206
1207 ie = (INDEX_ENTRY*)((u8*)&ir->index +
1208 le32_to_cpu(ir->index.entries_offset));
1209
1210
1211
1212
1213
1214 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1215 ntfs_debug("In index root, offset 0x%zx.", (u8*)ie - (u8*)ir);
1216
1217 if (unlikely((u8*)ie < (u8*)ir || (u8*)ie +
1218 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1219 (u8*)ie + le16_to_cpu(ie->key_length) >
1220 index_end))
1221 goto err_out;
1222
1223 if (ie->flags & INDEX_ENTRY_END)
1224 break;
1225
1226 if (ir_pos > (u8*)ie - (u8*)ir)
1227 continue;
1228
1229 fpos = (u8*)ie - (u8*)ir;
1230
1231 rc = ntfs_filldir(vol, fpos, ndir, NULL, ie, name, dirent,
1232 filldir);
1233 if (rc) {
1234 kfree(ir);
1235 goto abort;
1236 }
1237 }
1238
1239 kfree(ir);
1240 ir = NULL;
1241
1242 if (!NInoIndexAllocPresent(ndir))
1243 goto EOD;
1244
1245 fpos = vol->mft_record_size;
1246skip_index_root:
1247 kaddr = NULL;
1248 prev_ia_pos = -1LL;
1249
1250 ia_pos = (s64)fpos - vol->mft_record_size;
1251 ia_mapping = vdir->i_mapping;
1252 ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir->i_ino);
1253 bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
1254 if (IS_ERR(bmp_vi)) {
1255 ntfs_error(sb, "Failed to get bitmap attribute.");
1256 err = PTR_ERR(bmp_vi);
1257 goto err_out;
1258 }
1259 bmp_mapping = bmp_vi->i_mapping;
1260
1261 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
1262 if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
1263 ntfs_error(sb, "Current index allocation position exceeds "
1264 "index bitmap size.");
1265 goto iput_err_out;
1266 }
1267
1268 cur_bmp_pos = bmp_pos & ((PAGE_CACHE_SIZE * 8) - 1);
1269 bmp_pos &= ~(u64)((PAGE_CACHE_SIZE * 8) - 1);
1270get_next_bmp_page:
1271 ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
1272 (unsigned long long)bmp_pos >> (3 + PAGE_CACHE_SHIFT),
1273 (unsigned long long)bmp_pos &
1274 (unsigned long long)((PAGE_CACHE_SIZE * 8) - 1));
1275 bmp_page = ntfs_map_page(bmp_mapping,
1276 bmp_pos >> (3 + PAGE_CACHE_SHIFT));
1277 if (IS_ERR(bmp_page)) {
1278 ntfs_error(sb, "Reading index bitmap failed.");
1279 err = PTR_ERR(bmp_page);
1280 bmp_page = NULL;
1281 goto iput_err_out;
1282 }
1283 bmp = (u8*)page_address(bmp_page);
1284
1285 while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
1286find_next_index_buffer:
1287 cur_bmp_pos++;
1288
1289
1290
1291
1292 if (unlikely((cur_bmp_pos >> 3) >= PAGE_CACHE_SIZE)) {
1293 ntfs_unmap_page(bmp_page);
1294 bmp_pos += PAGE_CACHE_SIZE * 8;
1295 cur_bmp_pos = 0;
1296 goto get_next_bmp_page;
1297 }
1298
1299 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
1300 goto unm_EOD;
1301 ia_pos = (bmp_pos + cur_bmp_pos) <<
1302 ndir->itype.index.block_size_bits;
1303 }
1304 ntfs_debug("Handling index buffer 0x%llx.",
1305 (unsigned long long)bmp_pos + cur_bmp_pos);
1306
1307 if ((prev_ia_pos & (s64)PAGE_CACHE_MASK) !=
1308 (ia_pos & (s64)PAGE_CACHE_MASK)) {
1309 prev_ia_pos = ia_pos;
1310 if (likely(ia_page != NULL)) {
1311 unlock_page(ia_page);
1312 ntfs_unmap_page(ia_page);
1313 }
1314
1315
1316
1317
1318 ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_CACHE_SHIFT);
1319 if (IS_ERR(ia_page)) {
1320 ntfs_error(sb, "Reading index allocation data failed.");
1321 err = PTR_ERR(ia_page);
1322 ia_page = NULL;
1323 goto err_out;
1324 }
1325 lock_page(ia_page);
1326 kaddr = (u8*)page_address(ia_page);
1327 }
1328
1329 ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_CACHE_MASK &
1330 ~(s64)(ndir->itype.index.block_size - 1)));
1331
1332 if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE)) {
1333 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
1334 "inode 0x%lx or driver bug.", vdir->i_ino);
1335 goto err_out;
1336 }
1337
1338 if (unlikely(!ntfs_is_indx_record(ia->magic))) {
1339 ntfs_error(sb, "Directory index record with vcn 0x%llx is "
1340 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
1341 (unsigned long long)ia_pos >>
1342 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1343 goto err_out;
1344 }
1345 if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
1346 ~(s64)(ndir->itype.index.block_size - 1)) >>
1347 ndir->itype.index.vcn_size_bits)) {
1348 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
1349 "different from expected VCN (0x%llx). "
1350 "Directory inode 0x%lx is corrupt or driver "
1351 "bug. ", (unsigned long long)
1352 sle64_to_cpu(ia->index_block_vcn),
1353 (unsigned long long)ia_pos >>
1354 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1355 goto err_out;
1356 }
1357 if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
1358 ndir->itype.index.block_size)) {
1359 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1360 "0x%lx has a size (%u) differing from the "
1361 "directory specified size (%u). Directory "
1362 "inode is corrupt or driver bug.",
1363 (unsigned long long)ia_pos >>
1364 ndir->itype.index.vcn_size_bits, vdir->i_ino,
1365 le32_to_cpu(ia->index.allocated_size) + 0x18,
1366 ndir->itype.index.block_size);
1367 goto err_out;
1368 }
1369 index_end = (u8*)ia + ndir->itype.index.block_size;
1370 if (unlikely(index_end > kaddr + PAGE_CACHE_SIZE)) {
1371 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1372 "0x%lx crosses page boundary. Impossible! "
1373 "Cannot access! This is probably a bug in the "
1374 "driver.", (unsigned long long)ia_pos >>
1375 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1376 goto err_out;
1377 }
1378 ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
1379 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
1380 if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
1381 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
1382 "inode 0x%lx exceeds maximum size.",
1383 (unsigned long long)ia_pos >>
1384 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1385 goto err_out;
1386 }
1387
1388 ie = (INDEX_ENTRY*)((u8*)&ia->index +
1389 le32_to_cpu(ia->index.entries_offset));
1390
1391
1392
1393
1394
1395 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1396 ntfs_debug("In index allocation, offset 0x%llx.",
1397 (unsigned long long)ia_start +
1398 (unsigned long long)((u8*)ie - (u8*)ia));
1399
1400 if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
1401 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1402 (u8*)ie + le16_to_cpu(ie->key_length) >
1403 index_end))
1404 goto err_out;
1405
1406 if (ie->flags & INDEX_ENTRY_END)
1407 break;
1408
1409 if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
1410 continue;
1411
1412 fpos = (u8*)ie - (u8*)ia +
1413 (sle64_to_cpu(ia->index_block_vcn) <<
1414 ndir->itype.index.vcn_size_bits) +
1415 vol->mft_record_size;
1416
1417
1418
1419
1420
1421
1422 rc = ntfs_filldir(vol, fpos, ndir, ia_page, ie, name, dirent,
1423 filldir);
1424 if (rc) {
1425
1426 ntfs_unmap_page(ia_page);
1427 ntfs_unmap_page(bmp_page);
1428 iput(bmp_vi);
1429 goto abort;
1430 }
1431 }
1432 goto find_next_index_buffer;
1433unm_EOD:
1434 if (ia_page) {
1435 unlock_page(ia_page);
1436 ntfs_unmap_page(ia_page);
1437 }
1438 ntfs_unmap_page(bmp_page);
1439 iput(bmp_vi);
1440EOD:
1441
1442 fpos = i_size + vol->mft_record_size;
1443abort:
1444 kfree(name);
1445done:
1446#ifdef DEBUG
1447 if (!rc)
1448 ntfs_debug("EOD, fpos 0x%llx, returning 0.", fpos);
1449 else
1450 ntfs_debug("filldir returned %i, fpos 0x%llx, returning 0.",
1451 rc, fpos);
1452#endif
1453 filp->f_pos = fpos;
1454 return 0;
1455err_out:
1456 if (bmp_page) {
1457 ntfs_unmap_page(bmp_page);
1458iput_err_out:
1459 iput(bmp_vi);
1460 }
1461 if (ia_page) {
1462 unlock_page(ia_page);
1463 ntfs_unmap_page(ia_page);
1464 }
1465 kfree(ir);
1466 kfree(name);
1467 if (ctx)
1468 ntfs_attr_put_search_ctx(ctx);
1469 if (m)
1470 unmap_mft_record(ndir);
1471 if (!err)
1472 err = -EIO;
1473 ntfs_debug("Failed. Returning error code %i.", -err);
1474 filp->f_pos = fpos;
1475 return err;
1476}
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494static int ntfs_dir_open(struct inode *vi, struct file *filp)
1495{
1496 if (sizeof(unsigned long) < 8) {
1497 if (i_size_read(vi) > MAX_LFS_FILESIZE)
1498 return -EFBIG;
1499 }
1500 return 0;
1501}
1502
1503#ifdef NTFS_RW
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
1531 int datasync)
1532{
1533 struct inode *bmp_vi, *vi = filp->f_mapping->host;
1534 int err, ret;
1535 ntfs_attr na;
1536
1537 ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
1538
1539 err = filemap_write_and_wait_range(vi->i_mapping, start, end);
1540 if (err)
1541 return err;
1542 mutex_lock(&vi->i_mutex);
1543
1544 BUG_ON(!S_ISDIR(vi->i_mode));
1545
1546 na.mft_no = vi->i_ino;
1547 na.type = AT_BITMAP;
1548 na.name = I30;
1549 na.name_len = 4;
1550 bmp_vi = ilookup5(vi->i_sb, vi->i_ino, (test_t)ntfs_test_inode, &na);
1551 if (bmp_vi) {
1552 write_inode_now(bmp_vi, !datasync);
1553 iput(bmp_vi);
1554 }
1555 ret = __ntfs_write_inode(vi, 1);
1556 write_inode_now(vi, !datasync);
1557 err = sync_blockdev(vi->i_sb->s_bdev);
1558 if (unlikely(err && !ret))
1559 ret = err;
1560 if (likely(!ret))
1561 ntfs_debug("Done.");
1562 else
1563 ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error "
1564 "%u.", datasync ? "data" : "", vi->i_ino, -ret);
1565 mutex_unlock(&vi->i_mutex);
1566 return ret;
1567}
1568
1569#endif
1570
1571const struct file_operations ntfs_dir_ops = {
1572 .llseek = generic_file_llseek,
1573 .read = generic_read_dir,
1574 .readdir = ntfs_readdir,
1575#ifdef NTFS_RW
1576 .fsync = ntfs_dir_fsync,
1577
1578
1579#endif
1580
1581
1582 .open = ntfs_dir_open,
1583};
1584