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