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