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