This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / ntfs / index.c
1 /*
2  * index.c - NTFS kernel index handling.  Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2004 Anton Altaparmakov
5  *
6  * This program/include file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program/include file is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the Linux-NTFS
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "ntfs.h"
23 #include "collate.h"
24 #include "index.h"
25
26 /**
27  * ntfs_index_ctx_get - allocate and initialize a new index context
28  * @idx_ni:     ntfs index inode with which to initialize the context
29  *
30  * Allocate a new index context, initialize it with @idx_ni and return it.
31  * Return NULL if allocation failed.
32  *
33  * Locking:  Caller must hold i_sem on the index inode.
34  */
35 ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni)
36 {
37         ntfs_index_context *ictx;
38
39         ictx = kmem_cache_alloc(ntfs_index_ctx_cache, SLAB_NOFS);
40         if (ictx) {
41                 ictx->idx_ni = idx_ni;
42                 ictx->entry = NULL;
43                 ictx->data = NULL;
44                 ictx->data_len = 0;
45                 ictx->is_in_root = 0;
46                 ictx->ir = NULL;
47                 ictx->actx = NULL;
48                 ictx->base_ni = NULL;
49                 ictx->ia = NULL;
50                 ictx->page = NULL;
51         }
52         return ictx;
53 }
54
55 /**
56  * ntfs_index_ctx_put - release an index context
57  * @ictx:       index context to free
58  *
59  * Release the index context @ictx, releasing all associated resources.
60  *
61  * Locking:  Caller must hold i_sem on the index inode.
62  */
63 void ntfs_index_ctx_put(ntfs_index_context *ictx)
64 {
65         if (ictx->entry) {
66                 if (ictx->is_in_root) {
67                         if (ictx->actx)
68                                 put_attr_search_ctx(ictx->actx);
69                         if (ictx->base_ni)
70                                 unmap_mft_record(ictx->base_ni);
71                 } else {
72                         struct page *page = ictx->page;
73                         if (page) {
74                                 BUG_ON(!PageLocked(page));
75                                 unlock_page(page);
76                                 ntfs_unmap_page(page);
77                         }
78                 }
79         }
80         kmem_cache_free(ntfs_index_ctx_cache, ictx);
81         return;
82 }
83
84 /**
85  * ntfs_index_lookup - find a key in an index and return its index entry
86  * @key:        [IN] key for which to search in the index
87  * @key_len:    [IN] length of @key in bytes
88  * @ictx:       [IN/OUT] context describing the index and the returned entry
89  *
90  * Before calling ntfs_index_lookup(), @ictx must have been obtained from a
91  * call to ntfs_index_ctx_get().
92  *
93  * Look for the @key in the index specified by the index lookup context @ictx.
94  * ntfs_index_lookup() walks the contents of the index looking for the @key.
95  *
96  * If the @key is found in the index, 0 is returned and @ictx is setup to
97  * describe the index entry containing the matching @key.  @ictx->entry is the
98  * index entry and @ictx->data and @ictx->data_len are the index entry data and
99  * its length in bytes, respectively.
100  *
101  * If the @key is not found in the index, -ENOENT is returned and @ictx is
102  * setup to describe the index entry whose key collates immediately after the
103  * search @key, i.e. this is the position in the index at which an index entry
104  * with a key of @key would need to be inserted.
105  *
106  * If an error occurs return the negative error code and @ictx is left
107  * untouched.
108  *
109  * When finished with the entry and its data, call ntfs_index_ctx_put() to free
110  * the context and other associated resources.
111  *
112  * If the index entry was modified, call flush_dcache_index_entry_page()
113  * immediately after the modification and either ntfs_index_entry_mark_dirty()
114  * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to
115  * ensure that the changes are written to disk.
116  *
117  * Locking:  - Caller must hold i_sem on the index inode.
118  *           - Each page cache page in the index allocation mapping must be
119  *             locked whilst being accessed otherwise we may find a corrupt
120  *             page due to it being under ->writepage at the moment which
121  *             applies the mst protection fixups before writing out and then
122  *             removes them again after the write is complete after which it 
123  *             unlocks the page.
124  */
125 int ntfs_index_lookup(const void *key, const int key_len,
126                 ntfs_index_context *ictx)
127 {
128         ntfs_inode *idx_ni = ictx->idx_ni;
129         ntfs_volume *vol = idx_ni->vol;
130         struct super_block *sb = vol->sb;
131         ntfs_inode *base_ni = idx_ni->ext.base_ntfs_ino;
132         MFT_RECORD *m;
133         INDEX_ROOT *ir;
134         INDEX_ENTRY *ie;
135         INDEX_ALLOCATION *ia;
136         u8 *index_end;
137         attr_search_context *actx;
138         int rc, err = 0;
139         VCN vcn, old_vcn;
140         struct address_space *ia_mapping;
141         struct page *page;
142         u8 *kaddr;
143
144         ntfs_debug("Entering.");
145         BUG_ON(!NInoAttr(idx_ni));
146         BUG_ON(idx_ni->type != AT_INDEX_ALLOCATION);
147         BUG_ON(idx_ni->nr_extents != -1);
148         BUG_ON(!base_ni);
149         BUG_ON(!key);
150         BUG_ON(key_len <= 0);
151         if (!ntfs_is_collation_rule_supported(
152                         idx_ni->itype.index.collation_rule)) {
153                 ntfs_error(sb, "Index uses unsupported collation rule 0x%x.  "
154                                 "Aborting lookup.", le32_to_cpu(
155                                 idx_ni->itype.index.collation_rule));
156                 return -EOPNOTSUPP;
157         }
158         /* Get hold of the mft record for the index inode. */
159         m = map_mft_record(base_ni);
160         if (unlikely(IS_ERR(m))) {
161                 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
162                                 -PTR_ERR(m));
163                 return PTR_ERR(m);
164         }
165         actx = get_attr_search_ctx(base_ni, m);
166         if (unlikely(!actx)) {
167                 err = -ENOMEM;
168                 goto err_out;
169         }
170         /* Find the index root attribute in the mft record. */
171         if (!lookup_attr(AT_INDEX_ROOT, idx_ni->name, idx_ni->name_len,
172                         CASE_SENSITIVE, 0, NULL, 0, actx)) {
173                 ntfs_error(sb, "Index root attribute missing in inode 0x%lx.",
174                                 idx_ni->mft_no);
175                 err = -EIO;
176                 goto err_out;
177         }
178         /* Get to the index root value (it has been verified in read_inode). */
179         ir = (INDEX_ROOT*)((u8*)actx->attr +
180                         le16_to_cpu(actx->attr->data.resident.value_offset));
181         index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
182         /* The first index entry. */
183         ie = (INDEX_ENTRY*)((u8*)&ir->index +
184                         le32_to_cpu(ir->index.entries_offset));
185         /*
186          * Loop until we exceed valid memory (corruption case) or until we
187          * reach the last entry.
188          */
189         for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
190                 /* Bounds checks. */
191                 if ((u8*)ie < (u8*)actx->mrec || (u8*)ie +
192                                 sizeof(INDEX_ENTRY_HEADER) > index_end ||
193                                 (u8*)ie + le16_to_cpu(ie->length) > index_end)
194                         goto idx_err_out;
195                 /*
196                  * The last entry cannot contain a key.  It can however contain
197                  * a pointer to a child node in the B+tree so we just break out.
198                  */
199                 if (ie->flags & INDEX_ENTRY_END)
200                         break;
201                 /* Further bounds checks. */
202                 if ((u32)sizeof(INDEX_ENTRY_HEADER) +
203                                 le16_to_cpu(ie->key_length) >
204                                 le16_to_cpu(ie->data.vi.data_offset) ||
205                                 (u32)le16_to_cpu(ie->data.vi.data_offset) +
206                                 le16_to_cpu(ie->data.vi.data_length) >
207                                 le16_to_cpu(ie->length))
208                         goto idx_err_out;
209                 /* If the keys match perfectly, we setup @ictx and return 0. */
210                 if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
211                                 &ie->key, key_len)) {
212 ir_done:
213                         ictx->is_in_root = TRUE;
214                         ictx->actx = actx;
215                         ictx->base_ni = base_ni;
216                         ictx->ia = NULL;
217                         ictx->page = NULL;
218 done:
219                         ictx->entry = ie;
220                         ictx->data = (u8*)ie +
221                                         le16_to_cpu(ie->data.vi.data_offset);
222                         ictx->data_len = le16_to_cpu(ie->data.vi.data_length);
223                         ntfs_debug("Done.");
224                         return err;
225                 }
226                 /*
227                  * Not a perfect match, need to do full blown collation so we
228                  * know which way in the B+tree we have to go.
229                  */
230                 rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
231                                 key_len, &ie->key, le16_to_cpu(ie->key_length));
232                 /*
233                  * If @key collates before the key of the current entry, there
234                  * is definitely no such key in this index but we might need to
235                  * descend into the B+tree so we just break out of the loop.
236                  */
237                 if (rc == -1)
238                         break;
239                 /*
240                  * A match should never happen as the memcmp() call should have
241                  * cought it, but we still treat it correctly.
242                  */
243                 if (!rc)
244                         goto ir_done;
245                 /* The keys are not equal, continue the search. */
246         }
247         /*
248          * We have finished with this index without success.  Check for the
249          * presence of a child node and if not present setup @ictx and return
250          * -ENOENT.
251          */
252         if (!(ie->flags & INDEX_ENTRY_NODE)) {
253                 ntfs_debug("Entry not found.");
254                 err = -ENOENT;
255                 goto ir_done;
256         } /* Child node present, descend into it. */
257         /* Consistency check: Verify that an index allocation exists. */
258         if (!NInoIndexAllocPresent(idx_ni)) {
259                 ntfs_error(sb, "No index allocation attribute but index entry "
260                                 "requires one.  Inode 0x%lx is corrupt or "
261                                 "driver bug.", idx_ni->mft_no);
262                 err = -EIO;
263                 goto err_out;
264         }
265         /* Get the starting vcn of the index_block holding the child node. */
266         vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
267         ia_mapping = VFS_I(idx_ni)->i_mapping;
268         /*
269          * We are done with the index root and the mft record.  Release them,
270          * otherwise we deadlock with ntfs_map_page().
271          */
272         put_attr_search_ctx(actx);
273         unmap_mft_record(base_ni);
274         m = NULL;
275         actx = NULL;
276 descend_into_child_node:
277         /*
278          * Convert vcn to index into the index allocation attribute in units
279          * of PAGE_CACHE_SIZE and map the page cache page, reading it from
280          * disk if necessary.
281          */
282         page = ntfs_map_page(ia_mapping, vcn <<
283                         idx_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
284         if (IS_ERR(page)) {
285                 ntfs_error(sb, "Failed to map index page, error %ld.",
286                                 -PTR_ERR(page));
287                 err = PTR_ERR(page);
288                 goto err_out;
289         }
290         lock_page(page);
291         kaddr = (u8*)page_address(page);
292 fast_descend_into_child_node:
293         /* Get to the index allocation block. */
294         ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
295                         idx_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
296         /* Bounds checks. */
297         if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
298                 ntfs_error(sb, "Out of bounds check failed.  Corrupt inode "
299                                 "0x%lx or driver bug.", idx_ni->mft_no);
300                 err = -EIO;
301                 goto unm_err_out;
302         }
303         if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
304                 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
305                                 "different from expected VCN (0x%llx).  Inode "
306                                 "0x%lx is corrupt or driver bug.",
307                                 (unsigned long long)
308                                 sle64_to_cpu(ia->index_block_vcn),
309                                 (unsigned long long)vcn, idx_ni->mft_no);
310                 err = -EIO;
311                 goto unm_err_out;
312         }
313         if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
314                         idx_ni->itype.index.block_size) {
315                 ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx has "
316                                 "a size (%u) differing from the index "
317                                 "specified size (%u).  Inode is corrupt or "
318                                 "driver bug.", (unsigned long long)vcn,
319                                 idx_ni->mft_no,
320                                 le32_to_cpu(ia->index.allocated_size) + 0x18,
321                                 idx_ni->itype.index.block_size);
322                 err = -EIO;
323                 goto unm_err_out;
324         }
325         index_end = (u8*)ia + idx_ni->itype.index.block_size;
326         if (index_end > kaddr + PAGE_CACHE_SIZE) {
327                 ntfs_error(sb, "Index buffer (VCN 0x%llx) of inode 0x%lx "
328                                 "crosses page boundary.  Impossible!  Cannot "
329                                 "access!  This is probably a bug in the "
330                                 "driver.", (unsigned long long)vcn,
331                                 idx_ni->mft_no);
332                 err = -EIO;
333                 goto unm_err_out;
334         }
335         index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
336         if (index_end > (u8*)ia + idx_ni->itype.index.block_size) {
337                 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of inode "
338                                 "0x%lx exceeds maximum size.",
339                                 (unsigned long long)vcn, idx_ni->mft_no);
340                 err = -EIO;
341                 goto unm_err_out;
342         }
343         /* The first index entry. */
344         ie = (INDEX_ENTRY*)((u8*)&ia->index +
345                         le32_to_cpu(ia->index.entries_offset));
346         /*
347          * Iterate similar to above big loop but applied to index buffer, thus
348          * loop until we exceed valid memory (corruption case) or until we
349          * reach the last entry.
350          */
351         for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
352                 /* Bounds checks. */
353                 if ((u8*)ie < (u8*)ia || (u8*)ie +
354                                 sizeof(INDEX_ENTRY_HEADER) > index_end ||
355                                 (u8*)ie + le16_to_cpu(ie->length) > index_end) {
356                         ntfs_error(sb, "Index entry out of bounds in inode "
357                                         "0x%lx.", idx_ni->mft_no);
358                         err = -EIO;
359                         goto unm_err_out;
360                 }
361                 /*
362                  * The last entry cannot contain a ket.  It can however contain
363                  * a pointer to a child node in the B+tree so we just break out.
364                  */
365                 if (ie->flags & INDEX_ENTRY_END)
366                         break;
367                 /* Further bounds checks. */
368                 if ((u32)sizeof(INDEX_ENTRY_HEADER) +
369                                 le16_to_cpu(ie->key_length) >
370                                 le16_to_cpu(ie->data.vi.data_offset) ||
371                                 (u32)le16_to_cpu(ie->data.vi.data_offset) +
372                                 le16_to_cpu(ie->data.vi.data_length) >
373                                 le16_to_cpu(ie->length)) {
374                         ntfs_error(sb, "Index entry out of bounds in inode "
375                                         "0x%lx.", idx_ni->mft_no);
376                         err = -EIO;
377                         goto unm_err_out;
378                 }
379                 /* If the keys match perfectly, we setup @ictx and return 0. */
380                 if ((key_len == le16_to_cpu(ie->key_length)) && !memcmp(key,
381                                 &ie->key, key_len)) {
382 ia_done:
383                         ictx->is_in_root = FALSE;
384                         ictx->actx = NULL;
385                         ictx->base_ni = NULL;
386                         ictx->ia = ia;
387                         ictx->page = page;
388                         goto done;
389                 }
390                 /*
391                  * Not a perfect match, need to do full blown collation so we
392                  * know which way in the B+tree we have to go.
393                  */
394                 rc = ntfs_collate(vol, idx_ni->itype.index.collation_rule, key,
395                                 key_len, &ie->key, le16_to_cpu(ie->key_length));
396                 /*
397                  * If @key collates before the key of the current entry, there
398                  * is definitely no such key in this index but we might need to
399                  * descend into the B+tree so we just break out of the loop.
400                  */
401                 if (rc == -1)
402                         break;
403                 /*
404                  * A match should never happen as the memcmp() call should have
405                  * cought it, but we still treat it correctly.
406                  */
407                 if (!rc)
408                         goto ia_done;
409                 /* The keys are not equal, continue the search. */
410         }
411         /*
412          * We have finished with this index buffer without success.  Check for
413          * the presence of a child node and if not present return -ENOENT.
414          */
415         if (!(ie->flags & INDEX_ENTRY_NODE)) {
416                 ntfs_debug("Entry not found.");
417                 err = -ENOENT;
418                 goto ia_done;
419         }
420         if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
421                 ntfs_error(sb, "Index entry with child node found in a leaf "
422                                 "node in inode 0x%lx.", idx_ni->mft_no);
423                 err = -EIO;
424                 goto unm_err_out;
425         }
426         /* Child node present, descend into it. */
427         old_vcn = vcn;
428         vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
429         if (vcn >= 0) {
430                 /*
431                  * If vcn is in the same page cache page as old_vcn we recycle
432                  * the mapped page.
433                  */
434                 if (old_vcn << vol->cluster_size_bits >>
435                                 PAGE_CACHE_SHIFT == vcn <<
436                                 vol->cluster_size_bits >>
437                                 PAGE_CACHE_SHIFT)
438                         goto fast_descend_into_child_node;
439                 unlock_page(page);
440                 ntfs_unmap_page(page);
441                 goto descend_into_child_node;
442         }
443         ntfs_error(sb, "Negative child node vcn in inode 0x%lx.",
444                         idx_ni->mft_no);
445         err = -EIO;
446 unm_err_out:
447         unlock_page(page);
448         ntfs_unmap_page(page);
449 err_out:
450         if (actx)
451                 put_attr_search_ctx(actx);
452         if (m)
453                 unmap_mft_record(base_ni);
454         return err;
455 idx_err_out:
456         ntfs_error(sb, "Corrupt index.  Aborting lookup.");
457         err = -EIO;
458         goto err_out;
459 }
460
461 #ifdef NTFS_RW
462
463 /**
464  * __ntfs_index_entry_mark_dirty - mark an index allocation entry dirty
465  * @ictx:       ntfs index context describing the index entry
466  *
467  * NOTE: You want to use fs/ntfs/index.h::ntfs_index_entry_mark_dirty() instead!
468  * 
469  * Mark the index allocation entry described by the index entry context @ictx
470  * dirty.
471  *
472  * The index entry must be in an index block belonging to the index allocation
473  * attribute.  Mark the buffers belonging to the index record as well as the
474  * page cache page the index block is in dirty.  This automatically marks the
475  * VFS inode of the ntfs index inode to which the index entry belongs dirty,
476  * too (I_DIRTY_PAGES) and this in turn ensures the page buffers, and hence the
477  * dirty index block, will be written out to disk later.
478  */
479 void __ntfs_index_entry_mark_dirty(ntfs_index_context *ictx)
480 {
481         ntfs_inode *ni;
482         struct page *page;
483         struct buffer_head *bh, *head;
484         unsigned int rec_start, rec_end, bh_size, bh_start, bh_end;
485
486         BUG_ON(ictx->is_in_root);
487         ni = ictx->idx_ni;
488         page = ictx->page;
489         BUG_ON(!page_has_buffers(page));
490         /*
491          * If the index block is the same size as the page cache page, set all
492          * the buffers in the page, as well as the page itself, dirty.
493          */
494         if (ni->itype.index.block_size == PAGE_CACHE_SIZE) {
495                 __set_page_dirty_buffers(page);
496                 return;
497         }
498         /* Set only the buffers in which the index block is located dirty. */
499         rec_start = (unsigned int)((u8*)ictx->ia - (u8*)page_address(page));
500         rec_end = rec_start + ni->itype.index.block_size;
501         bh_size = ni->vol->sb->s_blocksize;
502         bh_start = 0;
503         bh = head = page_buffers(page);
504         do {
505                 bh_end = bh_start + bh_size;
506                 if ((bh_start >= rec_start) && (bh_end <= rec_end))
507                         set_buffer_dirty(bh);
508                 bh_start = bh_end;
509         } while ((bh = bh->b_this_page) != head);
510         /* Finally, set the page itself dirty, too. */
511         __set_page_dirty_nobuffers(page);
512 }
513
514 #endif /* NTFS_RW */