[PATCH] DVB: Update documentation and credits
[linux-2.6/history.git] / fs / ntfs / dir.c
blob30dee6372e464e5ecf7fa75b8d92be18c2f83726
1 /**
2 * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2003 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/smp_lock.h>
24 #include "ntfs.h"
25 #include "dir.h"
27 /**
28 * The little endian Unicode string $I30 as a global constant.
30 uchar_t I30[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('I'),
31 const_cpu_to_le16('3'), const_cpu_to_le16('0'),
32 const_cpu_to_le16(0) };
34 /**
35 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
36 * @dir_ni: ntfs inode of the directory in which to search for the name
37 * @uname: Unicode name for which to search in the directory
38 * @uname_len: length of the name @uname in Unicode characters
39 * @res: return the found file name if necessary (see below)
41 * Look for an inode with name @uname in the directory with inode @dir_ni.
42 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
43 * the Unicode name. If the name is found in the directory, the corresponding
44 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
45 * is a 64-bit number containing the sequence number.
47 * On error, a negative value is returned corresponding to the error code. In
48 * particular if the inode is not found -ENOENT is returned. Note that you
49 * can't just check the return value for being negative, you have to check the
50 * inode number for being negative which you can extract using MREC(return
51 * value).
53 * Note, @uname_len does not include the (optional) terminating NULL character.
55 * Note, we look for a case sensitive match first but we also look for a case
56 * insensitive match at the same time. If we find a case insensitive match, we
57 * save that for the case that we don't find an exact match, where we return
58 * the case insensitive match and setup @res (which we allocate!) with the mft
59 * reference, the file name type, length and with a copy of the little endian
60 * Unicode file name itself. If we match a file name which is in the DOS name
61 * space, we only return the mft reference and file name type in @res.
62 * ntfs_lookup() then uses this to find the long file name in the inode itself.
63 * This is to avoid polluting the dcache with short file names. We want them to
64 * work but we don't care for how quickly one can access them. This also fixes
65 * the dcache aliasing issues.
67 MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const uchar_t *uname,
68 const int uname_len, ntfs_name **res)
70 ntfs_volume *vol = dir_ni->vol;
71 struct super_block *sb = vol->sb;
72 MFT_RECORD *m;
73 INDEX_ROOT *ir;
74 INDEX_ENTRY *ie;
75 INDEX_ALLOCATION *ia;
76 u8 *index_end;
77 u64 mref;
78 attr_search_context *ctx;
79 int err, rc;
80 VCN vcn, old_vcn;
81 struct address_space *ia_mapping;
82 struct page *page;
83 u8 *kaddr;
84 ntfs_name *name = NULL;
86 /* Get hold of the mft record for the directory. */
87 m = map_mft_record(dir_ni);
88 if (unlikely(IS_ERR(m))) {
89 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
90 -PTR_ERR(m));
91 return ERR_MREF(PTR_ERR(m));
93 ctx = get_attr_search_ctx(dir_ni, m);
94 if (unlikely(!ctx)) {
95 err = -ENOMEM;
96 goto err_out;
98 /* Find the index root attribute in the mft record. */
99 if (!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0,
100 ctx)) {
101 ntfs_error(sb, "Index root attribute missing in directory "
102 "inode 0x%lx.", dir_ni->mft_no);
103 err = -EIO;
104 goto err_out;
106 /* Get to the index root value (it's been verified in read_inode). */
107 ir = (INDEX_ROOT*)((u8*)ctx->attr +
108 le16_to_cpu(ctx->attr->data.resident.value_offset));
109 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
110 /* The first index entry. */
111 ie = (INDEX_ENTRY*)((u8*)&ir->index +
112 le32_to_cpu(ir->index.entries_offset));
114 * Loop until we exceed valid memory (corruption case) or until we
115 * reach the last entry.
117 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
118 /* Bounds checks. */
119 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
120 sizeof(INDEX_ENTRY_HEADER) > index_end ||
121 (u8*)ie + le16_to_cpu(ie->key_length) >
122 index_end)
123 goto dir_err_out;
125 * The last entry cannot contain a name. It can however contain
126 * a pointer to a child node in the B+tree so we just break out.
128 if (ie->flags & INDEX_ENTRY_END)
129 break;
131 * We perform a case sensitive comparison and if that matches
132 * we are done and return the mft reference of the inode (i.e.
133 * the inode number together with the sequence number for
134 * consistency checking). We convert it to cpu format before
135 * returning.
137 if (ntfs_are_names_equal(uname, uname_len,
138 (uchar_t*)&ie->key.file_name.file_name,
139 ie->key.file_name.file_name_length,
140 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
141 found_it:
143 * We have a perfect match, so we don't need to care
144 * about having matched imperfectly before, so we can
145 * free name and set *res to NULL.
146 * However, if the perfect match is a short file name,
147 * we need to signal this through *res, so that
148 * ntfs_lookup() can fix dcache aliasing issues.
149 * As an optimization we just reuse an existing
150 * allocation of *res.
152 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
153 if (!name) {
154 name = kmalloc(sizeof(ntfs_name),
155 GFP_NOFS);
156 if (!name) {
157 err = -ENOMEM;
158 goto err_out;
161 name->mref = le64_to_cpu(
162 ie->data.dir.indexed_file);
163 name->type = FILE_NAME_DOS;
164 name->len = 0;
165 *res = name;
166 } else {
167 if (name)
168 kfree(name);
169 *res = NULL;
171 mref = le64_to_cpu(ie->data.dir.indexed_file);
172 put_attr_search_ctx(ctx);
173 unmap_mft_record(dir_ni);
174 return mref;
177 * For a case insensitive mount, we also perform a case
178 * insensitive comparison (provided the file name is not in the
179 * POSIX namespace). If the comparison matches, and the name is
180 * in the WIN32 namespace, we cache the filename in *res so
181 * that the caller, ntfs_lookup(), can work on it. If the
182 * comparison matches, and the name is in the DOS namespace, we
183 * only cache the mft reference and the file name type (we set
184 * the name length to zero for simplicity).
186 if (!NVolCaseSensitive(vol) &&
187 ie->key.file_name.file_name_type &&
188 ntfs_are_names_equal(uname, uname_len,
189 (uchar_t*)&ie->key.file_name.file_name,
190 ie->key.file_name.file_name_length,
191 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
192 int name_size = sizeof(ntfs_name);
193 u8 type = ie->key.file_name.file_name_type;
194 u8 len = ie->key.file_name.file_name_length;
196 /* Only one case insensitive matching name allowed. */
197 if (name) {
198 ntfs_error(sb, "Found already allocated name "
199 "in phase 1. Please run chkdsk "
200 "and if that doesn't find any "
201 "errors please report you saw "
202 "this message to "
203 "linux-ntfs-dev@lists.sf.net.");
204 goto dir_err_out;
207 if (type != FILE_NAME_DOS)
208 name_size += len * sizeof(uchar_t);
209 name = kmalloc(name_size, GFP_NOFS);
210 if (!name) {
211 err = -ENOMEM;
212 goto err_out;
214 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
215 name->type = type;
216 if (type != FILE_NAME_DOS) {
217 name->len = len;
218 memcpy(name->name, ie->key.file_name.file_name,
219 len * sizeof(uchar_t));
220 } else
221 name->len = 0;
222 *res = name;
225 * Not a perfect match, need to do full blown collation so we
226 * know which way in the B+tree we have to go.
228 rc = ntfs_collate_names(uname, uname_len,
229 (uchar_t*)&ie->key.file_name.file_name,
230 ie->key.file_name.file_name_length, 1,
231 IGNORE_CASE, vol->upcase, vol->upcase_len);
233 * If uname collates before the name of the current entry, there
234 * is definitely no such name in this index but we might need to
235 * descend into the B+tree so we just break out of the loop.
237 if (rc == -1)
238 break;
239 /* The names are not equal, continue the search. */
240 if (rc)
241 continue;
243 * Names match with case insensitive comparison, now try the
244 * case sensitive comparison, which is required for proper
245 * collation.
247 rc = ntfs_collate_names(uname, uname_len,
248 (uchar_t*)&ie->key.file_name.file_name,
249 ie->key.file_name.file_name_length, 1,
250 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
251 if (rc == -1)
252 break;
253 if (rc)
254 continue;
256 * Perfect match, this will never happen as the
257 * ntfs_are_names_equal() call will have gotten a match but we
258 * still treat it correctly.
260 goto found_it;
263 * We have finished with this index without success. Check for the
264 * presence of a child node and if not present return -ENOENT, unless
265 * we have got a matching name cached in name in which case return the
266 * mft reference associated with it.
268 if (!(ie->flags & INDEX_ENTRY_NODE)) {
269 if (name) {
270 put_attr_search_ctx(ctx);
271 unmap_mft_record(dir_ni);
272 return name->mref;
274 ntfs_debug("Entry not found.");
275 err = -ENOENT;
276 goto err_out;
277 } /* Child node present, descend into it. */
278 /* Consistency check: Verify that an index allocation exists. */
279 if (!NInoIndexAllocPresent(dir_ni)) {
280 ntfs_error(sb, "No index allocation attribute but index entry "
281 "requires one. Directory inode 0x%lx is "
282 "corrupt or driver bug.", dir_ni->mft_no);
283 err = -EIO;
284 goto err_out;
286 /* Get the starting vcn of the index_block holding the child node. */
287 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
288 ia_mapping = VFS_I(dir_ni)->i_mapping;
290 * We are done with the index root and the mft record. Release them,
291 * otherwise we deadlock with ntfs_map_page().
293 put_attr_search_ctx(ctx);
294 unmap_mft_record(dir_ni);
295 m = NULL;
296 ctx = NULL;
297 descend_into_child_node:
299 * Convert vcn to index into the index allocation attribute in units
300 * of PAGE_CACHE_SIZE and map the page cache page, reading it from
301 * disk if necessary.
303 page = ntfs_map_page(ia_mapping, vcn <<
304 dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
305 if (IS_ERR(page)) {
306 ntfs_error(sb, "Failed to map directory index page, error %ld.",
307 -PTR_ERR(page));
308 err = PTR_ERR(page);
309 goto err_out;
311 kaddr = (u8*)page_address(page);
312 fast_descend_into_child_node:
313 /* Get to the index allocation block. */
314 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
315 dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
316 /* Bounds checks. */
317 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
318 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
319 "inode 0x%lx or driver bug.", dir_ni->mft_no);
320 err = -EIO;
321 goto unm_err_out;
323 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
324 ntfs_error(sb, "Actual VCN (0x%Lx) of index buffer is "
325 "different from expected VCN (0x%Lx). "
326 "Directory inode 0x%lx is corrupt or driver "
327 "bug.",
328 (long long)sle64_to_cpu(ia->index_block_vcn),
329 (long long)vcn, dir_ni->mft_no);
330 err = -EIO;
331 goto unm_err_out;
333 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
334 dir_ni->itype.index.block_size) {
335 ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
336 "0x%lx has a size (%u) differing from the "
337 "directory specified size (%u). Directory "
338 "inode is corrupt or driver bug.",
339 (long long)vcn, dir_ni->mft_no,
340 le32_to_cpu(ia->index.allocated_size) + 0x18,
341 dir_ni->itype.index.block_size);
342 err = -EIO;
343 goto unm_err_out;
345 index_end = (u8*)ia + dir_ni->itype.index.block_size;
346 if (index_end > kaddr + PAGE_CACHE_SIZE) {
347 ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
348 "0x%lx crosses page boundary. Impossible! "
349 "Cannot access! This is probably a bug in the "
350 "driver.", (long long)vcn, dir_ni->mft_no);
351 err = -EIO;
352 goto unm_err_out;
354 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
355 if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
356 ntfs_error(sb, "Size of index buffer (VCN 0x%Lx) of directory "
357 "inode 0x%lx exceeds maximum size.",
358 (long long)vcn, dir_ni->mft_no);
359 err = -EIO;
360 goto unm_err_out;
362 /* The first index entry. */
363 ie = (INDEX_ENTRY*)((u8*)&ia->index +
364 le32_to_cpu(ia->index.entries_offset));
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.
370 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
371 /* Bounds check. */
372 if ((u8*)ie < (u8*)ia || (u8*)ie +
373 sizeof(INDEX_ENTRY_HEADER) > index_end ||
374 (u8*)ie + le16_to_cpu(ie->key_length) >
375 index_end) {
376 ntfs_error(sb, "Index entry out of bounds in "
377 "directory inode 0x%lx.",
378 dir_ni->mft_no);
379 err = -EIO;
380 goto unm_err_out;
383 * The last entry cannot contain a name. It can however contain
384 * a pointer to a child node in the B+tree so we just break out.
386 if (ie->flags & INDEX_ENTRY_END)
387 break;
389 * We perform a case sensitive comparison and if that matches
390 * we are done and return the mft reference of the inode (i.e.
391 * the inode number together with the sequence number for
392 * consistency checking). We convert it to cpu format before
393 * returning.
395 if (ntfs_are_names_equal(uname, uname_len,
396 (uchar_t*)&ie->key.file_name.file_name,
397 ie->key.file_name.file_name_length,
398 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
399 found_it2:
401 * We have a perfect match, so we don't need to care
402 * about having matched imperfectly before, so we can
403 * free name and set *res to NULL.
404 * However, if the perfect match is a short file name,
405 * we need to signal this through *res, so that
406 * ntfs_lookup() can fix dcache aliasing issues.
407 * As an optimization we just reuse an existing
408 * allocation of *res.
410 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
411 if (!name) {
412 name = kmalloc(sizeof(ntfs_name),
413 GFP_NOFS);
414 if (!name) {
415 err = -ENOMEM;
416 goto unm_err_out;
419 name->mref = le64_to_cpu(
420 ie->data.dir.indexed_file);
421 name->type = FILE_NAME_DOS;
422 name->len = 0;
423 *res = name;
424 } else {
425 if (name)
426 kfree(name);
427 *res = NULL;
429 mref = le64_to_cpu(ie->data.dir.indexed_file);
430 ntfs_unmap_page(page);
431 return mref;
434 * For a case insensitive mount, we also perform a case
435 * insensitive comparison (provided the file name is not in the
436 * POSIX namespace). If the comparison matches, and the name is
437 * in the WIN32 namespace, we cache the filename in *res so
438 * that the caller, ntfs_lookup(), can work on it. If the
439 * comparison matches, and the name is in the DOS namespace, we
440 * only cache the mft reference and the file name type (we set
441 * the name length to zero for simplicity).
443 if (!NVolCaseSensitive(vol) &&
444 ie->key.file_name.file_name_type &&
445 ntfs_are_names_equal(uname, uname_len,
446 (uchar_t*)&ie->key.file_name.file_name,
447 ie->key.file_name.file_name_length,
448 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
449 int name_size = sizeof(ntfs_name);
450 u8 type = ie->key.file_name.file_name_type;
451 u8 len = ie->key.file_name.file_name_length;
453 /* Only one case insensitive matching name allowed. */
454 if (name) {
455 ntfs_error(sb, "Found already allocated name "
456 "in phase 2. Please run chkdsk "
457 "and if that doesn't find any "
458 "errors please report you saw "
459 "this message to "
460 "linux-ntfs-dev@lists.sf.net.");
461 ntfs_unmap_page(page);
462 goto dir_err_out;
465 if (type != FILE_NAME_DOS)
466 name_size += len * sizeof(uchar_t);
467 name = kmalloc(name_size, GFP_NOFS);
468 if (!name) {
469 err = -ENOMEM;
470 goto unm_err_out;
472 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
473 name->type = type;
474 if (type != FILE_NAME_DOS) {
475 name->len = len;
476 memcpy(name->name, ie->key.file_name.file_name,
477 len * sizeof(uchar_t));
478 } else
479 name->len = 0;
480 *res = name;
483 * Not a perfect match, need to do full blown collation so we
484 * know which way in the B+tree we have to go.
486 rc = ntfs_collate_names(uname, uname_len,
487 (uchar_t*)&ie->key.file_name.file_name,
488 ie->key.file_name.file_name_length, 1,
489 IGNORE_CASE, vol->upcase, vol->upcase_len);
491 * If uname collates before the name of the current entry, there
492 * is definitely no such name in this index but we might need to
493 * descend into the B+tree so we just break out of the loop.
495 if (rc == -1)
496 break;
497 /* The names are not equal, continue the search. */
498 if (rc)
499 continue;
501 * Names match with case insensitive comparison, now try the
502 * case sensitive comparison, which is required for proper
503 * collation.
505 rc = ntfs_collate_names(uname, uname_len,
506 (uchar_t*)&ie->key.file_name.file_name,
507 ie->key.file_name.file_name_length, 1,
508 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
509 if (rc == -1)
510 break;
511 if (rc)
512 continue;
514 * Perfect match, this will never happen as the
515 * ntfs_are_names_equal() call will have gotten a match but we
516 * still treat it correctly.
518 goto found_it2;
521 * We have finished with this index buffer without success. Check for
522 * the presence of a child node.
524 if (ie->flags & INDEX_ENTRY_NODE) {
525 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
526 ntfs_error(sb, "Index entry with child node found in "
527 "a leaf node in directory inode 0x%lx.",
528 dir_ni->mft_no);
529 err = -EIO;
530 goto unm_err_out;
532 /* Child node present, descend into it. */
533 old_vcn = vcn;
534 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
535 if (vcn >= 0) {
536 /* If vcn is in the same page cache page as old_vcn we
537 * recycle the mapped page. */
538 if (old_vcn << vol->cluster_size_bits >>
539 PAGE_CACHE_SHIFT == vcn <<
540 vol->cluster_size_bits >>
541 PAGE_CACHE_SHIFT)
542 goto fast_descend_into_child_node;
543 ntfs_unmap_page(page);
544 goto descend_into_child_node;
546 ntfs_error(sb, "Negative child node vcn in directory inode "
547 "0x%lx.", dir_ni->mft_no);
548 err = -EIO;
549 goto unm_err_out;
552 * No child node present, return -ENOENT, unless we have got a matching
553 * name cached in name in which case return the mft reference
554 * associated with it.
556 if (name) {
557 ntfs_unmap_page(page);
558 return name->mref;
560 ntfs_debug("Entry not found.");
561 err = -ENOENT;
562 unm_err_out:
563 ntfs_unmap_page(page);
564 err_out:
565 if (ctx)
566 put_attr_search_ctx(ctx);
567 if (m)
568 unmap_mft_record(dir_ni);
569 if (name) {
570 kfree(name);
571 *res = NULL;
573 return ERR_MREF(err);
574 dir_err_out:
575 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
576 err = -EIO;
577 goto err_out;
580 #if 0
582 // TODO: (AIA)
583 // The algorithm embedded in this code will be required for the time when we
584 // want to support adding of entries to directories, where we require correct
585 // collation of file names in order not to cause corruption of the file system.
588 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
589 * @dir_ni: ntfs inode of the directory in which to search for the name
590 * @uname: Unicode name for which to search in the directory
591 * @uname_len: length of the name @uname in Unicode characters
593 * Look for an inode with name @uname in the directory with inode @dir_ni.
594 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
595 * the Unicode name. If the name is found in the directory, the corresponding
596 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
597 * is a 64-bit number containing the sequence number.
599 * On error, a negative value is returned corresponding to the error code. In
600 * particular if the inode is not found -ENOENT is returned. Note that you
601 * can't just check the return value for being negative, you have to check the
602 * inode number for being negative which you can extract using MREC(return
603 * value).
605 * Note, @uname_len does not include the (optional) terminating NULL character.
607 u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const uchar_t *uname,
608 const int uname_len)
610 ntfs_volume *vol = dir_ni->vol;
611 struct super_block *sb = vol->sb;
612 MFT_RECORD *m;
613 INDEX_ROOT *ir;
614 INDEX_ENTRY *ie;
615 INDEX_ALLOCATION *ia;
616 u8 *index_end;
617 u64 mref;
618 attr_search_context *ctx;
619 int err, rc;
620 IGNORE_CASE_BOOL ic;
621 VCN vcn, old_vcn;
622 struct address_space *ia_mapping;
623 struct page *page;
624 u8 *kaddr;
626 /* Get hold of the mft record for the directory. */
627 m = map_mft_record(dir_ni);
628 if (IS_ERR(m)) {
629 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
630 -PTR_ERR(m));
631 return ERR_MREF(PTR_ERR(m));
633 ctx = get_attr_search_ctx(dir_ni, m);
634 if (!ctx) {
635 err = -ENOMEM;
636 goto err_out;
638 /* Find the index root attribute in the mft record. */
639 if (!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0,
640 ctx)) {
641 ntfs_error(sb, "Index root attribute missing in directory "
642 "inode 0x%lx.", dir_ni->mft_no);
643 err = -EIO;
644 goto err_out;
646 /* Get to the index root value (it's been verified in read_inode). */
647 ir = (INDEX_ROOT*)((u8*)ctx->attr +
648 le16_to_cpu(ctx->attr->data.resident.value_offset));
649 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
650 /* The first index entry. */
651 ie = (INDEX_ENTRY*)((u8*)&ir->index +
652 le32_to_cpu(ir->index.entries_offset));
654 * Loop until we exceed valid memory (corruption case) or until we
655 * reach the last entry.
657 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
658 /* Bounds checks. */
659 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
660 sizeof(INDEX_ENTRY_HEADER) > index_end ||
661 (u8*)ie + le16_to_cpu(ie->key_length) >
662 index_end)
663 goto dir_err_out;
665 * The last entry cannot contain a name. It can however contain
666 * a pointer to a child node in the B+tree so we just break out.
668 if (ie->flags & INDEX_ENTRY_END)
669 break;
671 * If the current entry has a name type of POSIX, the name is
672 * case sensitive and not otherwise. This has the effect of us
673 * not being able to access any POSIX file names which collate
674 * after the non-POSIX one when they only differ in case, but
675 * anyone doing screwy stuff like that deserves to burn in
676 * hell... Doing that kind of stuff on NT4 actually causes
677 * corruption on the partition even when using SP6a and Linux
678 * is not involved at all.
680 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
681 CASE_SENSITIVE;
683 * If the names match perfectly, we are done and return the
684 * mft reference of the inode (i.e. the inode number together
685 * with the sequence number for consistency checking. We
686 * convert it to cpu format before returning.
688 if (ntfs_are_names_equal(uname, uname_len,
689 (uchar_t*)&ie->key.file_name.file_name,
690 ie->key.file_name.file_name_length, ic,
691 vol->upcase, vol->upcase_len)) {
692 found_it:
693 mref = le64_to_cpu(ie->data.dir.indexed_file);
694 put_attr_search_ctx(ctx);
695 unmap_mft_record(dir_ni);
696 return mref;
699 * Not a perfect match, need to do full blown collation so we
700 * know which way in the B+tree we have to go.
702 rc = ntfs_collate_names(uname, uname_len,
703 (uchar_t*)&ie->key.file_name.file_name,
704 ie->key.file_name.file_name_length, 1,
705 IGNORE_CASE, vol->upcase, vol->upcase_len);
707 * If uname collates before the name of the current entry, there
708 * is definitely no such name in this index but we might need to
709 * descend into the B+tree so we just break out of the loop.
711 if (rc == -1)
712 break;
713 /* The names are not equal, continue the search. */
714 if (rc)
715 continue;
717 * Names match with case insensitive comparison, now try the
718 * case sensitive comparison, which is required for proper
719 * collation.
721 rc = ntfs_collate_names(uname, uname_len,
722 (uchar_t*)&ie->key.file_name.file_name,
723 ie->key.file_name.file_name_length, 1,
724 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
725 if (rc == -1)
726 break;
727 if (rc)
728 continue;
730 * Perfect match, this will never happen as the
731 * ntfs_are_names_equal() call will have gotten a match but we
732 * still treat it correctly.
734 goto found_it;
737 * We have finished with this index without success. Check for the
738 * presence of a child node.
740 if (!(ie->flags & INDEX_ENTRY_NODE)) {
741 /* No child node, return -ENOENT. */
742 err = -ENOENT;
743 goto err_out;
744 } /* Child node present, descend into it. */
745 /* Consistency check: Verify that an index allocation exists. */
746 if (!NInoIndexAllocPresent(dir_ni)) {
747 ntfs_error(sb, "No index allocation attribute but index entry "
748 "requires one. Directory inode 0x%lx is "
749 "corrupt or driver bug.", dir_ni->mft_no);
750 err = -EIO;
751 goto err_out;
753 /* Get the starting vcn of the index_block holding the child node. */
754 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
755 ia_mapping = VFS_I(dir_ni)->i_mapping;
757 * We are done with the index root and the mft record. Release them,
758 * otherwise we deadlock with ntfs_map_page().
760 put_attr_search_ctx(ctx);
761 unmap_mft_record(dir_ni);
762 m = NULL;
763 ctx = NULL;
764 descend_into_child_node:
766 * Convert vcn to index into the index allocation attribute in units
767 * of PAGE_CACHE_SIZE and map the page cache page, reading it from
768 * disk if necessary.
770 page = ntfs_map_page(ia_mapping, vcn <<
771 dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
772 if (IS_ERR(page)) {
773 ntfs_error(sb, "Failed to map directory index page, error %ld.",
774 -PTR_ERR(page));
775 err = PTR_ERR(page);
776 goto err_out;
778 kaddr = (u8*)page_address(page);
779 fast_descend_into_child_node:
780 /* Get to the index allocation block. */
781 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
782 dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
783 /* Bounds checks. */
784 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
785 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
786 "inode 0x%lx or driver bug.", dir_ni->mft_no);
787 err = -EIO;
788 goto unm_err_out;
790 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
791 ntfs_error(sb, "Actual VCN (0x%Lx) of index buffer is "
792 "different from expected VCN (0x%Lx). "
793 "Directory inode 0x%lx is corrupt or driver "
794 "bug.",
795 (long long)sle64_to_cpu(ia->index_block_vcn),
796 (long long)vcn, dir_ni->mft_no);
797 err = -EIO;
798 goto unm_err_out;
800 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
801 dir_ni->itype.index.block_size) {
802 ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
803 "0x%lx has a size (%u) differing from the "
804 "directory specified size (%u). Directory "
805 "inode is corrupt or driver bug.",
806 (long long)vcn, dir_ni->mft_no,
807 le32_to_cpu(ia->index.allocated_size) + 0x18,
808 dir_ni->itype.index.block_size);
809 err = -EIO;
810 goto unm_err_out;
812 index_end = (u8*)ia + dir_ni->itype.index.block_size;
813 if (index_end > kaddr + PAGE_CACHE_SIZE) {
814 ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
815 "0x%lx crosses page boundary. Impossible! "
816 "Cannot access! This is probably a bug in the "
817 "driver.", (long long)vcn, dir_ni->mft_no);
818 err = -EIO;
819 goto unm_err_out;
821 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
822 if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
823 ntfs_error(sb, "Size of index buffer (VCN 0x%Lx) of directory "
824 "inode 0x%lx exceeds maximum size.",
825 (long long)vcn, dir_ni->mft_no);
826 err = -EIO;
827 goto unm_err_out;
829 /* The first index entry. */
830 ie = (INDEX_ENTRY*)((u8*)&ia->index +
831 le32_to_cpu(ia->index.entries_offset));
833 * Iterate similar to above big loop but applied to index buffer, thus
834 * loop until we exceed valid memory (corruption case) or until we
835 * reach the last entry.
837 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
838 /* Bounds check. */
839 if ((u8*)ie < (u8*)ia || (u8*)ie +
840 sizeof(INDEX_ENTRY_HEADER) > index_end ||
841 (u8*)ie + le16_to_cpu(ie->key_length) >
842 index_end) {
843 ntfs_error(sb, "Index entry out of bounds in "
844 "directory inode 0x%lx.",
845 dir_ni->mft_no);
846 err = -EIO;
847 goto unm_err_out;
850 * The last entry cannot contain a name. It can however contain
851 * a pointer to a child node in the B+tree so we just break out.
853 if (ie->flags & INDEX_ENTRY_END)
854 break;
856 * If the current entry has a name type of POSIX, the name is
857 * case sensitive and not otherwise. This has the effect of us
858 * not being able to access any POSIX file names which collate
859 * after the non-POSIX one when they only differ in case, but
860 * anyone doing screwy stuff like that deserves to burn in
861 * hell... Doing that kind of stuff on NT4 actually causes
862 * corruption on the partition even when using SP6a and Linux
863 * is not involved at all.
865 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
866 CASE_SENSITIVE;
868 * If the names match perfectly, we are done and return the
869 * mft reference of the inode (i.e. the inode number together
870 * with the sequence number for consistency checking. We
871 * convert it to cpu format before returning.
873 if (ntfs_are_names_equal(uname, uname_len,
874 (uchar_t*)&ie->key.file_name.file_name,
875 ie->key.file_name.file_name_length, ic,
876 vol->upcase, vol->upcase_len)) {
877 found_it2:
878 mref = le64_to_cpu(ie->data.dir.indexed_file);
879 ntfs_unmap_page(page);
880 return mref;
883 * Not a perfect match, need to do full blown collation so we
884 * know which way in the B+tree we have to go.
886 rc = ntfs_collate_names(uname, uname_len,
887 (uchar_t*)&ie->key.file_name.file_name,
888 ie->key.file_name.file_name_length, 1,
889 IGNORE_CASE, vol->upcase, vol->upcase_len);
891 * If uname collates before the name of the current entry, there
892 * is definitely no such name in this index but we might need to
893 * descend into the B+tree so we just break out of the loop.
895 if (rc == -1)
896 break;
897 /* The names are not equal, continue the search. */
898 if (rc)
899 continue;
901 * Names match with case insensitive comparison, now try the
902 * case sensitive comparison, which is required for proper
903 * collation.
905 rc = ntfs_collate_names(uname, uname_len,
906 (uchar_t*)&ie->key.file_name.file_name,
907 ie->key.file_name.file_name_length, 1,
908 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
909 if (rc == -1)
910 break;
911 if (rc)
912 continue;
914 * Perfect match, this will never happen as the
915 * ntfs_are_names_equal() call will have gotten a match but we
916 * still treat it correctly.
918 goto found_it2;
921 * We have finished with this index buffer without success. Check for
922 * the presence of a child node.
924 if (ie->flags & INDEX_ENTRY_NODE) {
925 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
926 ntfs_error(sb, "Index entry with child node found in "
927 "a leaf node in directory inode 0x%lx.",
928 dir_ni->mft_no);
929 err = -EIO;
930 goto unm_err_out;
932 /* Child node present, descend into it. */
933 old_vcn = vcn;
934 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
935 if (vcn >= 0) {
936 /* If vcn is in the same page cache page as old_vcn we
937 * recycle the mapped page. */
938 if (old_vcn << vol->cluster_size_bits >>
939 PAGE_CACHE_SHIFT == vcn <<
940 vol->cluster_size_bits >>
941 PAGE_CACHE_SHIFT)
942 goto fast_descend_into_child_node;
943 ntfs_unmap_page(page);
944 goto descend_into_child_node;
946 ntfs_error(sb, "Negative child node vcn in directory inode "
947 "0x%lx.", dir_ni->mft_no);
948 err = -EIO;
949 goto unm_err_out;
951 /* No child node, return -ENOENT. */
952 ntfs_debug("Entry not found.");
953 err = -ENOENT;
954 unm_err_out:
955 ntfs_unmap_page(page);
956 err_out:
957 if (ctx)
958 put_attr_search_ctx(ctx);
959 if (m)
960 unmap_mft_record(dir_ni);
961 return ERR_MREF(err);
962 dir_err_out:
963 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
964 err = -EIO;
965 goto err_out;
968 #endif
970 typedef union {
971 INDEX_ROOT *ir;
972 INDEX_ALLOCATION *ia;
973 } index_union __attribute__ ((__transparent_union__));
975 typedef enum {
976 INDEX_TYPE_ROOT, /* index root */
977 INDEX_TYPE_ALLOCATION, /* index allocation */
978 } INDEX_TYPE;
981 * ntfs_filldir - ntfs specific filldir method
982 * @vol: current ntfs volume
983 * @fpos: position in the directory
984 * @ndir: ntfs inode of current directory
985 * @index_type: specifies whether @iu is an index root or an index allocation
986 * @iu: index root or index allocation attribute to which @ie belongs
987 * @ie: current index entry
988 * @name: buffer to use for the converted name
989 * @dirent: vfs filldir callback context
990 * @filldir: vfs filldir callback
992 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
993 * callback.
995 static inline int ntfs_filldir(ntfs_volume *vol, loff_t *fpos,
996 ntfs_inode *ndir, const INDEX_TYPE index_type,
997 index_union iu, INDEX_ENTRY *ie, u8 *name,
998 void *dirent, filldir_t filldir)
1000 int name_len;
1001 unsigned dt_type;
1002 FILE_NAME_TYPE_FLAGS name_type;
1004 /* Advance the position even if going to skip the entry. */
1005 if (index_type == INDEX_TYPE_ALLOCATION)
1006 *fpos = (u8*)ie - (u8*)iu.ia +
1007 (sle64_to_cpu(iu.ia->index_block_vcn) <<
1008 ndir->itype.index.vcn_size_bits) +
1009 vol->mft_record_size;
1010 else /* if (index_type == INDEX_TYPE_ROOT) */
1011 *fpos = (u8*)ie - (u8*)iu.ir;
1012 name_type = ie->key.file_name.file_name_type;
1013 if (name_type == FILE_NAME_DOS) {
1014 ntfs_debug("Skipping DOS name space entry.");
1015 return 0;
1017 if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
1018 ntfs_debug("Skipping root directory self reference entry.");
1019 return 0;
1021 if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
1022 !NVolShowSystemFiles(vol)) {
1023 ntfs_debug("Skipping system file.");
1024 return 0;
1026 name_len = ntfs_ucstonls(vol, (uchar_t*)&ie->key.file_name.file_name,
1027 ie->key.file_name.file_name_length, &name,
1028 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
1029 if (name_len <= 0) {
1030 ntfs_debug("Skipping unrepresentable file.");
1031 return 0;
1033 if (ie->key.file_name.file_attributes &
1034 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
1035 dt_type = DT_DIR;
1036 else
1037 dt_type = DT_REG;
1038 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%Lx, inode "
1039 "0x%lx, DT_%s.", name, name_len, *fpos,
1040 MREF_LE(ie->data.dir.indexed_file),
1041 dt_type == DT_DIR ? "DIR" : "REG");
1042 return filldir(dirent, name, name_len, *fpos,
1043 MREF_LE(ie->data.dir.indexed_file), dt_type);
1047 * VFS calls readdir without BKL but with i_sem held. This protects the VFS
1048 * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
1049 * modifications).
1051 * We use the same basic approach as the old NTFS driver, i.e. we parse the
1052 * index root entries and then the index allocation entries that are marked
1053 * as in use in the index bitmap.
1054 * While this will return the names in random order this doesn't matter for
1055 * readdir but OTOH results in a faster readdir.
1057 static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1059 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
1060 loff_t fpos;
1061 struct inode *bmp_vi, *vdir = filp->f_dentry->d_inode;
1062 struct super_block *sb = vdir->i_sb;
1063 ntfs_inode *ndir = NTFS_I(vdir);
1064 ntfs_volume *vol = NTFS_SB(sb);
1065 MFT_RECORD *m;
1066 INDEX_ROOT *ir;
1067 INDEX_ENTRY *ie;
1068 INDEX_ALLOCATION *ia;
1069 u8 *name = NULL;
1070 int rc, err, ir_pos, cur_bmp_pos;
1071 struct address_space *ia_mapping, *bmp_mapping;
1072 struct page *bmp_page = NULL, *ia_page = NULL;
1073 u8 *kaddr, *bmp, *index_end;
1074 attr_search_context *ctx;
1076 fpos = filp->f_pos;
1077 ntfs_debug("Entering for inode 0x%lx, fpos 0x%Lx.",
1078 vdir->i_ino, fpos);
1079 rc = err = 0;
1080 /* Are we at end of dir yet? */
1081 if (fpos >= vdir->i_size + vol->mft_record_size)
1082 goto done;
1083 /* Emulate . and .. for all directories. */
1084 if (!fpos) {
1085 ntfs_debug("Calling filldir for . with len 1, fpos 0x0, "
1086 "inode 0x%lx, DT_DIR.", vdir->i_ino);
1087 rc = filldir(dirent, ".", 1, fpos, vdir->i_ino, DT_DIR);
1088 if (rc)
1089 goto done;
1090 fpos++;
1092 if (fpos == 1) {
1093 ntfs_debug("Calling filldir for .. with len 2, fpos 0x1, "
1094 "inode 0x%lx, DT_DIR.",
1095 parent_ino(filp->f_dentry));
1096 rc = filldir(dirent, "..", 2, fpos,
1097 parent_ino(filp->f_dentry), DT_DIR);
1098 if (rc)
1099 goto done;
1100 fpos++;
1102 m = NULL;
1103 ctx = NULL;
1105 * Allocate a buffer to store the current name being processed
1106 * converted to format determined by current NLS.
1108 name = (u8*)kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1,
1109 GFP_NOFS);
1110 if (unlikely(!name)) {
1111 err = -ENOMEM;
1112 goto err_out;
1114 /* Are we jumping straight into the index allocation attribute? */
1115 if (fpos >= vol->mft_record_size)
1116 goto skip_index_root;
1117 /* Get hold of the mft record for the directory. */
1118 m = map_mft_record(ndir);
1119 if (unlikely(IS_ERR(m))) {
1120 err = PTR_ERR(m);
1121 m = NULL;
1122 goto err_out;
1124 ctx = get_attr_search_ctx(ndir, m);
1125 if (unlikely(!ctx)) {
1126 err = -ENOMEM;
1127 goto err_out;
1129 /* Get the offset into the index root attribute. */
1130 ir_pos = (s64)fpos;
1131 /* Find the index root attribute in the mft record. */
1132 if (unlikely(!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0,
1133 NULL, 0, ctx))) {
1134 ntfs_error(sb, "Index root attribute missing in directory "
1135 "inode 0x%lx.", vdir->i_ino);
1136 goto err_out;
1138 /* Get to the index root value (it's been verified in read_inode). */
1139 ir = (INDEX_ROOT*)((u8*)ctx->attr +
1140 le16_to_cpu(ctx->attr->data.resident.value_offset));
1141 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
1142 /* The first index entry. */
1143 ie = (INDEX_ENTRY*)((u8*)&ir->index +
1144 le32_to_cpu(ir->index.entries_offset));
1146 * Loop until we exceed valid memory (corruption case) or until we
1147 * reach the last entry or until filldir tells us it has had enough
1148 * or signals an error (both covered by the rc test).
1150 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1151 ntfs_debug("In index root, offset 0x%x.", (u8*)ie - (u8*)ir);
1152 /* Bounds checks. */
1153 if (unlikely((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
1154 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1155 (u8*)ie + le16_to_cpu(ie->key_length) >
1156 index_end))
1157 goto err_out;
1158 /* The last entry cannot contain a name. */
1159 if (ie->flags & INDEX_ENTRY_END)
1160 break;
1161 /* Skip index root entry if continuing previous readdir. */
1162 if (ir_pos > (u8*)ie - (u8*)ir)
1163 continue;
1164 /* Submit the name to the filldir callback. */
1165 rc = ntfs_filldir(vol, &fpos, ndir, INDEX_TYPE_ROOT, ir, ie,
1166 name, dirent, filldir);
1167 if (rc) {
1168 put_attr_search_ctx(ctx);
1169 unmap_mft_record(ndir);
1170 goto abort;
1174 * We are done with the index root and the mft record for that matter.
1175 * We need to release it, otherwise we deadlock on ntfs_attr_iget()
1176 * and/or ntfs_read_page().
1178 put_attr_search_ctx(ctx);
1179 unmap_mft_record(ndir);
1180 m = NULL;
1181 ctx = NULL;
1182 /* If there is no index allocation attribute we are finished. */
1183 if (!NInoIndexAllocPresent(ndir))
1184 goto EOD;
1185 /* Advance fpos to the beginning of the index allocation. */
1186 fpos = vol->mft_record_size;
1187 skip_index_root:
1188 kaddr = NULL;
1189 prev_ia_pos = -1LL;
1190 /* Get the offset into the index allocation attribute. */
1191 ia_pos = (s64)fpos - vol->mft_record_size;
1192 ia_mapping = vdir->i_mapping;
1193 bmp_vi = ndir->itype.index.bmp_ino;
1194 if (unlikely(!bmp_vi)) {
1195 ntfs_debug("Inode %lu, regetting index bitmap.", vdir->i_ino);
1196 bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
1197 if (unlikely(IS_ERR(bmp_vi))) {
1198 ntfs_error(sb, "Failed to get bitmap attribute.");
1199 err = PTR_ERR(bmp_vi);
1200 goto err_out;
1202 ndir->itype.index.bmp_ino = bmp_vi;
1204 bmp_mapping = bmp_vi->i_mapping;
1205 /* Get the starting bitmap bit position and sanity check it. */
1206 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
1207 if (unlikely(bmp_pos >> 3 >= bmp_vi->i_size)) {
1208 ntfs_error(sb, "Current index allocation position exceeds "
1209 "index bitmap size.");
1210 goto err_out;
1212 /* Get the starting bit position in the current bitmap page. */
1213 cur_bmp_pos = bmp_pos & ((PAGE_CACHE_SIZE * 8) - 1);
1214 bmp_pos &= ~(u64)((PAGE_CACHE_SIZE * 8) - 1);
1215 get_next_bmp_page:
1216 ntfs_debug("Reading bitmap with page index 0x%Lx, bit ofs 0x%Lx",
1217 (long long)bmp_pos >> (3 + PAGE_CACHE_SHIFT),
1218 (long long)bmp_pos & ((PAGE_CACHE_SIZE * 8) - 1));
1219 bmp_page = ntfs_map_page(bmp_mapping,
1220 bmp_pos >> (3 + PAGE_CACHE_SHIFT));
1221 if (unlikely(IS_ERR(bmp_page))) {
1222 ntfs_error(sb, "Reading index bitmap failed.");
1223 err = PTR_ERR(bmp_page);
1224 bmp_page = NULL;
1225 goto err_out;
1227 bmp = (u8*)page_address(bmp_page);
1228 /* Find next index block in use. */
1229 while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
1230 find_next_index_buffer:
1231 cur_bmp_pos++;
1233 * If we have reached the end of the bitmap page, get the next
1234 * page, and put away the old one.
1236 if (unlikely((cur_bmp_pos >> 3) >= PAGE_CACHE_SIZE)) {
1237 ntfs_unmap_page(bmp_page);
1238 bmp_pos += PAGE_CACHE_SIZE * 8;
1239 cur_bmp_pos = 0;
1240 goto get_next_bmp_page;
1242 /* If we have reached the end of the bitmap, we are done. */
1243 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= vdir->i_size))
1244 goto unm_EOD;
1245 ia_pos = (bmp_pos + cur_bmp_pos) <<
1246 ndir->itype.index.block_size_bits;
1248 ntfs_debug("Handling index buffer 0x%Lx.",
1249 (long long)bmp_pos + cur_bmp_pos);
1250 /* If the current index buffer is in the same page we reuse the page. */
1251 if ((prev_ia_pos & PAGE_CACHE_MASK) != (ia_pos & PAGE_CACHE_MASK)) {
1252 prev_ia_pos = ia_pos;
1253 if (likely(ia_page != NULL))
1254 ntfs_unmap_page(ia_page);
1256 * Map the page cache page containing the current ia_pos,
1257 * reading it from disk if necessary.
1259 ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_CACHE_SHIFT);
1260 if (unlikely(IS_ERR(ia_page))) {
1261 ntfs_error(sb, "Reading index allocation data failed.");
1262 err = PTR_ERR(ia_page);
1263 ia_page = NULL;
1264 goto err_out;
1266 kaddr = (u8*)page_address(ia_page);
1268 /* Get the current index buffer. */
1269 ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_CACHE_MASK &
1270 ~(s64)(ndir->itype.index.block_size - 1)));
1271 /* Bounds checks. */
1272 if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE)) {
1273 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
1274 "inode 0x%lx or driver bug.", vdir->i_ino);
1275 goto err_out;
1277 if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
1278 ~(s64)(ndir->itype.index.block_size - 1)) >>
1279 ndir->itype.index.vcn_size_bits)) {
1280 ntfs_error(sb, "Actual VCN (0x%Lx) of index buffer is "
1281 "different from expected VCN (0x%Lx). "
1282 "Directory inode 0x%lx is corrupt or driver "
1283 "bug. ",
1284 (long long)sle64_to_cpu(ia->index_block_vcn),
1285 (long long)ia_pos >>
1286 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1287 goto err_out;
1289 if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
1290 ndir->itype.index.block_size)) {
1291 ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
1292 "0x%lx has a size (%u) differing from the "
1293 "directory specified size (%u). Directory "
1294 "inode is corrupt or driver bug.",
1295 (long long)ia_pos >>
1296 ndir->itype.index.vcn_size_bits, vdir->i_ino,
1297 le32_to_cpu(ia->index.allocated_size) + 0x18,
1298 ndir->itype.index.block_size);
1299 goto err_out;
1301 index_end = (u8*)ia + ndir->itype.index.block_size;
1302 if (unlikely(index_end > kaddr + PAGE_CACHE_SIZE)) {
1303 ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
1304 "0x%lx crosses page boundary. Impossible! "
1305 "Cannot access! This is probably a bug in the "
1306 "driver.", (long long)ia_pos >>
1307 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1308 goto err_out;
1310 ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
1311 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
1312 if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
1313 ntfs_error(sb, "Size of index buffer (VCN 0x%Lx) of directory "
1314 "inode 0x%lx exceeds maximum size.",
1315 (long long)ia_pos >>
1316 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1317 goto err_out;
1319 /* The first index entry in this index buffer. */
1320 ie = (INDEX_ENTRY*)((u8*)&ia->index +
1321 le32_to_cpu(ia->index.entries_offset));
1323 * Loop until we exceed valid memory (corruption case) or until we
1324 * reach the last entry or until filldir tells us it has had enough
1325 * or signals an error (both covered by the rc test).
1327 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1328 ntfs_debug("In index allocation, offset 0x%Lx.",
1329 (long long)ia_start + ((u8*)ie - (u8*)ia));
1330 /* Bounds checks. */
1331 if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
1332 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1333 (u8*)ie + le16_to_cpu(ie->key_length) >
1334 index_end))
1335 goto err_out;
1336 /* The last entry cannot contain a name. */
1337 if (ie->flags & INDEX_ENTRY_END)
1338 break;
1339 /* Skip index block entry if continuing previous readdir. */
1340 if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
1341 continue;
1342 /* Submit the name to the filldir callback. */
1343 rc = ntfs_filldir(vol, &fpos, ndir, INDEX_TYPE_ALLOCATION, ia,
1344 ie, name, dirent, filldir);
1345 if (rc) {
1346 ntfs_unmap_page(ia_page);
1347 ntfs_unmap_page(bmp_page);
1348 goto abort;
1351 goto find_next_index_buffer;
1352 unm_EOD:
1353 if (ia_page)
1354 ntfs_unmap_page(ia_page);
1355 ntfs_unmap_page(bmp_page);
1356 EOD:
1357 /* We are finished, set fpos to EOD. */
1358 fpos = vdir->i_size + vol->mft_record_size;
1359 abort:
1360 kfree(name);
1361 done:
1362 #ifdef DEBUG
1363 if (!rc)
1364 ntfs_debug("EOD, fpos 0x%Lx, returning 0.", fpos);
1365 else
1366 ntfs_debug("filldir returned %i, fpos 0x%Lx, returning 0.",
1367 rc, fpos);
1368 #endif
1369 filp->f_pos = fpos;
1370 return 0;
1371 err_out:
1372 if (bmp_page)
1373 ntfs_unmap_page(bmp_page);
1374 if (ia_page)
1375 ntfs_unmap_page(ia_page);
1376 if (name)
1377 kfree(name);
1378 if (ctx)
1379 put_attr_search_ctx(ctx);
1380 if (m)
1381 unmap_mft_record(ndir);
1382 if (!err)
1383 err = -EIO;
1384 ntfs_debug("Failed. Returning error code %i.", -err);
1385 filp->f_pos = fpos;
1386 return err;
1390 * ntfs_dir_open - called when an inode is about to be opened
1391 * @vi: inode to be opened
1392 * @filp: file structure describing the inode
1394 * Limit directory size to the page cache limit on architectures where unsigned
1395 * long is 32-bits. This is the most we can do for now without overflowing the
1396 * page cache page index. Doing it this way means we don't run into problems
1397 * because of existing too large directories. It would be better to allow the
1398 * user to read the accessible part of the directory but I doubt very much
1399 * anyone is going to hit this check on a 32-bit architecture, so there is no
1400 * point in adding the extra complexity required to support this.
1402 * On 64-bit architectures, the check is hopefully optimized away by the
1403 * compiler.
1405 static int ntfs_dir_open(struct inode *vi, struct file *filp)
1407 if (sizeof(unsigned long) < 8) {
1408 if (vi->i_size > MAX_LFS_FILESIZE)
1409 return -EFBIG;
1411 return 0;
1414 struct file_operations ntfs_dir_ops = {
1415 .llseek = generic_file_llseek, /* Seek inside directory. */
1416 .read = generic_read_dir, /* Return -EISDIR. */
1417 .readdir = ntfs_readdir, /* Read directory contents. */
1418 .open = ntfs_dir_open, /* Open directory. */