24 #include <linux/slab.h>
81 const int uname_len, ntfs_name **
res)
104 ntfs_error(sb,
"map_mft_record() failed with error code %ld.",
118 ntfs_error(sb,
"Index root attribute missing in "
119 "directory inode 0x%lx.",
126 ir = (INDEX_ROOT*)((
u8*)ctx->
attr +
128 index_end = (
u8*)&ir->index +
le32_to_cpu(ir->index.index_length);
130 ie = (INDEX_ENTRY*)((
u8*)&ir->index +
136 for (;; ie = (INDEX_ENTRY*)((
u8*)ie +
le16_to_cpu(ie->length))) {
139 sizeof(INDEX_ENTRY_HEADER) > index_end ||
157 (
ntfschar*)&ie->key.file_name.file_name,
158 ie->key.file_name.file_name_length,
173 name =
kmalloc(
sizeof(ntfs_name),
181 ie->data.dir.indexed_file);
204 if (!NVolCaseSensitive(vol) &&
205 ie->key.file_name.file_name_type &&
207 (
ntfschar*)&ie->key.file_name.file_name,
208 ie->key.file_name.file_name_length,
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;
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 "
221 "linux-ntfs-dev@lists."
227 name_size += len *
sizeof(
ntfschar);
233 name->mref =
le64_to_cpu(ie->data.dir.indexed_file);
237 memcpy(name->name, ie->key.file_name.file_name,
248 (
ntfschar*)&ie->key.file_name.file_name,
249 ie->key.file_name.file_name_length, 1,
267 (
ntfschar*)&ie->key.file_name.file_name,
268 ie->key.file_name.file_name_length, 1,
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);
306 ia_mapping = VFS_I(dir_ni)->i_mapping;
315 descend_into_child_node:
321 page = ntfs_map_page(ia_mapping, vcn <<
324 ntfs_error(sb,
"Failed to map directory index page, error %ld.",
331 fast_descend_into_child_node:
337 ntfs_error(sb,
"Out of bounds check failed. Corrupt directory "
338 "inode 0x%lx or driver bug.", dir_ni->
mft_no);
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);
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);
357 if (
le32_to_cpu(ia->index.allocated_size) + 0x18 !=
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,
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,
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);
385 ie = (INDEX_ENTRY*)((
u8*)&ia->index +
392 for (;; ie = (INDEX_ENTRY*)((
u8*)ie +
le16_to_cpu(ie->length))) {
394 if ((
u8*)ie < (
u8*)ia || (
u8*)ie +
395 sizeof(INDEX_ENTRY_HEADER) > index_end ||
398 ntfs_error(sb,
"Index entry out of bounds in "
399 "directory inode 0x%lx.",
417 (
ntfschar*)&ie->key.file_name.file_name,
418 ie->key.file_name.file_name_length,
433 name =
kmalloc(
sizeof(ntfs_name),
441 ie->data.dir.indexed_file);
451 ntfs_unmap_page(page);
464 if (!NVolCaseSensitive(vol) &&
465 ie->key.file_name.file_name_type &&
467 (
ntfschar*)&ie->key.file_name.file_name,
468 ie->key.file_name.file_name_length,
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;
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 "
481 "linux-ntfs-dev@lists."
484 ntfs_unmap_page(page);
489 name_size += len *
sizeof(
ntfschar);
495 name->mref =
le64_to_cpu(ie->data.dir.indexed_file);
499 memcpy(name->name, ie->key.file_name.file_name,
510 (
ntfschar*)&ie->key.file_name.file_name,
511 ie->key.file_name.file_name_length, 1,
529 (
ntfschar*)&ie->key.file_name.file_name,
530 ie->key.file_name.file_name_length, 1,
549 ntfs_error(sb,
"Index entry with child node found in "
550 "a leaf node in directory inode 0x%lx.",
556 vcn = sle64_to_cpup((
sle64*)((
u8*)ie +
565 goto fast_descend_into_child_node;
567 ntfs_unmap_page(page);
568 goto descend_into_child_node;
570 ntfs_error(sb,
"Negative child node vcn in directory inode "
571 "0x%lx.", dir_ni->
mft_no);
581 ntfs_unmap_page(page);
588 ntfs_unmap_page(page);
602 ntfs_error(sb,
"Corrupt directory. Aborting lookup.");
655 ntfs_error(sb,
"map_mft_record() failed with error code %ld.",
669 ntfs_error(sb,
"Index root attribute missing in "
670 "directory inode 0x%lx.",
677 ir = (INDEX_ROOT*)((
u8*)ctx->
attr +
679 index_end = (
u8*)&ir->index +
le32_to_cpu(ir->index.index_length);
681 ie = (INDEX_ENTRY*)((
u8*)&ir->index +
687 for (;; ie = (INDEX_ENTRY*)((
u8*)ie +
le16_to_cpu(ie->length))) {
690 sizeof(INDEX_ENTRY_HEADER) > index_end ||
720 ie->
key.file_name.file_name_length, ic,
721 vol->upcase, vol->upcase_len)) {
733 (
ntfschar*)&ie->key.file_name.file_name,
734 ie->key.file_name.file_name_length, 1,
752 (
ntfschar*)&ie->key.file_name.file_name,
753 ie->key.file_name.file_name_length, 1,
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);
784 ia_mapping = VFS_I(dir_ni)->i_mapping;
793 descend_into_child_node:
799 page = ntfs_map_page(ia_mapping, vcn <<
802 ntfs_error(sb,
"Failed to map directory index page, error %ld.",
809 fast_descend_into_child_node:
815 ntfs_error(sb,
"Out of bounds check failed. Corrupt directory "
816 "inode 0x%lx or driver bug.", dir_ni->
mft_no);
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);
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);
835 if (
le32_to_cpu(ia->index.allocated_size) + 0x18 !=
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,
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,
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);
863 ie = (INDEX_ENTRY*)((
u8*)&ia->index +
870 for (;; ie = (INDEX_ENTRY*)((
u8*)ie +
le16_to_cpu(ie->length))) {
872 if ((
u8*)ie < (
u8*)ia || (
u8*)ie +
873 sizeof(INDEX_ENTRY_HEADER) > index_end ||
876 ntfs_error(sb,
"Index entry out of bounds in "
877 "directory inode 0x%lx.",
897 ic = ie->key.file_name.file_name_type ?
IGNORE_CASE :
906 (
ntfschar*)&ie->key.file_name.file_name,
907 ie->key.file_name.file_name_length, ic,
912 ntfs_unmap_page(page);
920 (
ntfschar*)&ie->key.file_name.file_name,
921 ie->key.file_name.file_name_length, 1,
939 (
ntfschar*)&ie->key.file_name.file_name,
940 ie->key.file_name.file_name_length, 1,
959 ntfs_error(sb,
"Index entry with child node found in "
960 "a leaf node in directory inode 0x%lx.",
974 goto fast_descend_into_child_node;
976 ntfs_unmap_page(page);
977 goto descend_into_child_node;
979 ntfs_error(sb,
"Negative child node vcn in directory inode "
980 "0x%lx.", dir_ni->
mft_no);
988 ntfs_unmap_page(page);
998 ntfs_error(sb,
"Corrupt directory. Aborting lookup.");
1027 static inline int ntfs_filldir(
ntfs_volume *vol, loff_t fpos,
1028 ntfs_inode *ndir,
struct page *ia_page, INDEX_ENTRY *ie,
1036 name_type = ie->key.file_name.file_name_type;
1038 ntfs_debug(
"Skipping DOS name space entry.");
1042 ntfs_debug(
"Skipping root directory self reference entry.");
1046 !NVolShowSystemFiles(vol)) {
1051 ie->key.file_name.file_name_length, &name,
1053 if (name_len <= 0) {
1055 (
long long)
MREF_LE(ie->data.dir.indexed_file));
1058 if (ie->key.file_name.file_attributes &
1063 mref =
MREF_LE(ie->data.dir.indexed_file);
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);
1100 static int ntfs_readdir(
struct file *filp,
void *dirent,
filldir_t filldir)
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;
1109 INDEX_ROOT *ir =
NULL;
1113 int rc,
err, ir_pos, cur_bmp_pos;
1115 struct page *bmp_page =
NULL, *ia_page =
NULL;
1116 u8 *kaddr, *bmp, *index_end;
1120 ntfs_debug(
"Entering for inode 0x%lx, fpos 0x%llx.",
1124 i_size = i_size_read(vdir);
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);
1137 ntfs_debug(
"Calling filldir for .. with len 2, fpos 0x1, "
1138 "inode 0x%lx, DT_DIR.",
1140 rc = filldir(dirent,
"..", 2, fpos,
1159 goto skip_index_root;
1178 ntfs_error(sb,
"Index root attribute missing in directory "
1179 "inode 0x%lx.", vdir->
i_ino);
1205 index_end = (
u8*)&ir->index +
le32_to_cpu(ir->index.index_length);
1207 ie = (INDEX_ENTRY*)((
u8*)&ir->index +
1214 for (;; ie = (INDEX_ENTRY*)((
u8*)ie +
le16_to_cpu(ie->length))) {
1218 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1226 if (ir_pos > (
u8*)ie - (
u8*)ir)
1229 fpos = (
u8*)ie - (
u8*)
ir;
1231 rc = ntfs_filldir(vol, fpos, ndir,
NULL, ie, name, dirent,
1242 if (!NInoIndexAllocPresent(ndir))
1254 if (IS_ERR(bmp_vi)) {
1255 ntfs_error(sb,
"Failed to get bitmap attribute.");
1256 err = PTR_ERR(bmp_vi);
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.");
1271 ntfs_debug(
"Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
1273 (
unsigned long long)bmp_pos &
1275 bmp_page = ntfs_map_page(bmp_mapping,
1277 if (IS_ERR(bmp_page)) {
1278 ntfs_error(sb,
"Reading index bitmap failed.");
1279 err = PTR_ERR(bmp_page);
1285 while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
1286 find_next_index_buffer:
1293 ntfs_unmap_page(bmp_page);
1296 goto get_next_bmp_page;
1299 if (
unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
1301 ia_pos = (bmp_pos + cur_bmp_pos) <<
1305 (
unsigned long long)bmp_pos + cur_bmp_pos);
1308 (ia_pos & (
s64)PAGE_CACHE_MASK)) {
1309 prev_ia_pos = ia_pos;
1312 ntfs_unmap_page(ia_page);
1319 if (IS_ERR(ia_page)) {
1320 ntfs_error(sb,
"Reading index allocation data failed.");
1321 err = PTR_ERR(ia_page);
1333 ntfs_error(sb,
"Out of bounds check failed. Corrupt directory "
1334 "inode 0x%lx or driver bug.", vdir->
i_ino);
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 >>
1345 if (
unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
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 >>
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 >>
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 >>
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);
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 >>
1388 ie = (INDEX_ENTRY*)((
u8*)&ia->index +
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));
1401 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1409 if (ia_pos - ia_start > (
u8*)ie - (
u8*)ia)
1412 fpos = (
u8*)ie - (
u8*)ia +
1413 (sle64_to_cpu(ia->index_block_vcn) <<
1422 rc = ntfs_filldir(vol, fpos, ndir, ia_page, ie, name, dirent,
1426 ntfs_unmap_page(ia_page);
1427 ntfs_unmap_page(bmp_page);
1432 goto find_next_index_buffer;
1436 ntfs_unmap_page(ia_page);
1438 ntfs_unmap_page(bmp_page);
1448 ntfs_debug(
"EOD, fpos 0x%llx, returning 0.", fpos);
1450 ntfs_debug(
"filldir returned %i, fpos 0x%llx, returning 0.",
1457 ntfs_unmap_page(bmp_page);
1463 ntfs_unmap_page(ia_page);
1473 ntfs_debug(
"Failed. Returning error code %i.", -err);
1494 static int ntfs_dir_open(
struct inode *vi,
struct file *filp)
1496 if (
sizeof(
unsigned long) < 8) {
1497 if (i_size_read(vi) > MAX_LFS_FILESIZE)
1530 static int ntfs_dir_fsync(
struct file *filp, loff_t
start, loff_t
end,
1555 ret = __ntfs_write_inode(vi, 1);
1564 "%u.", datasync ?
"data" :
"", vi->
i_ino, -ret);
1574 .readdir = ntfs_readdir,
1576 .fsync = ntfs_dir_fsync,
1582 .open = ntfs_dir_open,