1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 * Top-level entry points into iam module
39 * Author: Wang Di <wangdi@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * iam: big theory statement.
46 * iam (Index Access Module) is a module providing abstraction of persistent
47 * transactional container on top of generalized ldiskfs htree.
51 * - key, pointer, and record size specifiable per container.
53 * - trees taller than 2 index levels.
55 * - read/write to existing ldiskfs htree directories as iam containers.
57 * iam container is a tree, consisting of leaf nodes containing keys and
58 * records stored in this container, and index nodes, containing keys and
59 * pointers to leaf or index nodes.
61 * iam does not work with keys directly, instead it calls user-supplied key
62 * comparison function (->dpo_keycmp()).
64 * Pointers are (currently) interpreted as logical offsets (measured in
65 * blocksful) within underlying flat file on top of which iam tree lives.
69 * iam mostly tries to reuse existing htree formats.
71 * Format of index node:
73 * +-----+-------+-------+-------+------+-------+------------+
74 * | | count | | | | | |
75 * | gap | / | entry | entry | .... | entry | free space |
76 * | | limit | | | | | |
77 * +-----+-------+-------+-------+------+-------+------------+
79 * gap this part of node is never accessed by iam code. It
80 * exists for binary compatibility with ldiskfs htree (that,
81 * in turn, stores fake struct ext2_dirent for ext2
82 * compatibility), and to keep some unspecified per-node
83 * data. Gap can be different for root and non-root index
84 * nodes. Gap size can be specified for each container
85 * (gap of 0 is allowed).
87 * count/limit current number of entries in this node, and the maximal
88 * number of entries that can fit into node. count/limit
89 * has the same size as entry, and is itself counted in
92 * entry index entry: consists of a key immediately followed by
93 * a pointer to a child node. Size of a key and size of a
94 * pointer depends on container. Entry has neither
95 * alignment nor padding.
97 * free space portion of node new entries are added to
99 * Entries in index node are sorted by their key value.
101 * Format of a leaf node is not specified. Generic iam code accesses leaf
102 * nodes through ->id_leaf methods in struct iam_descr.
106 #include <linux/module.h>
107 #include <linux/fs.h>
108 #include <linux/pagemap.h>
109 #include <linux/time.h>
110 #include <linux/fcntl.h>
111 #include <linux/stat.h>
112 #include <linux/string.h>
113 #include <linux/quotaops.h>
114 #include <linux/buffer_head.h>
115 #include <linux/smp_lock.h>
116 #include "osd_internal.h"
122 * List of all registered formats.
124 * No locking. Callers synchronize.
126 static CFS_LIST_HEAD(iam_formats);
128 void iam_format_register(struct iam_format *fmt)
130 cfs_list_add(&fmt->if_linkage, &iam_formats);
132 EXPORT_SYMBOL(iam_format_register);
135 * Determine format of given container. This is done by scanning list of
136 * registered formats and calling ->if_guess() method of each in turn.
138 static int iam_format_guess(struct iam_container *c)
141 struct iam_format *fmt;
144 * XXX temporary initialization hook.
147 static int initialized = 0;
150 iam_lvar_format_init();
151 iam_lfix_format_init();
157 cfs_list_for_each_entry(fmt, &iam_formats, if_linkage) {
158 result = fmt->if_guess(c);
166 * Initialize container @c.
168 int iam_container_init(struct iam_container *c,
169 struct iam_descr *descr, struct inode *inode)
171 memset(c, 0, sizeof *c);
173 c->ic_object = inode;
174 cfs_init_rwsem(&c->ic_sem);
177 EXPORT_SYMBOL(iam_container_init);
180 * Determine container format.
182 int iam_container_setup(struct iam_container *c)
184 return iam_format_guess(c);
186 EXPORT_SYMBOL(iam_container_setup);
189 * Finalize container @c, release all resources.
191 void iam_container_fini(struct iam_container *c)
194 EXPORT_SYMBOL(iam_container_fini);
196 void iam_path_init(struct iam_path *path, struct iam_container *c,
197 struct iam_path_descr *pd)
199 memset(path, 0, sizeof *path);
200 path->ip_container = c;
201 path->ip_frame = path->ip_frames;
203 path->ip_leaf.il_path = path;
206 static void iam_leaf_fini(struct iam_leaf *leaf);
208 void iam_path_release(struct iam_path *path)
212 for (i = 0; i < ARRAY_SIZE(path->ip_frames); i++) {
213 if (path->ip_frames[i].bh != NULL) {
214 brelse(path->ip_frames[i].bh);
215 path->ip_frames[i].bh = NULL;
220 void iam_path_fini(struct iam_path *path)
222 iam_leaf_fini(&path->ip_leaf);
223 iam_path_release(path);
227 void iam_path_compat_init(struct iam_path_compat *path, struct inode *inode)
231 path->ipc_hinfo = &path->ipc_hinfo_area;
232 for (i = 0; i < ARRAY_SIZE(path->ipc_scratch); ++i)
233 path->ipc_descr.ipd_key_scratch[i] =
234 (struct iam_ikey *)&path->ipc_scratch[i];
236 iam_path_init(&path->ipc_path, &path->ipc_container, &path->ipc_descr);
239 void iam_path_compat_fini(struct iam_path_compat *path)
241 iam_path_fini(&path->ipc_path);
245 * Helper function initializing iam_path_descr and its key scratch area.
247 struct iam_path_descr *iam_ipd_alloc(void *area, int keysize)
249 struct iam_path_descr *ipd;
255 for (i = 0; i < ARRAY_SIZE(ipd->ipd_key_scratch); ++i, karea += keysize)
256 ipd->ipd_key_scratch[i] = karea;
259 EXPORT_SYMBOL(iam_ipd_alloc);
261 void iam_ipd_free(struct iam_path_descr *ipd)
264 EXPORT_SYMBOL(iam_ipd_free);
266 int iam_node_read(struct iam_container *c, iam_ptr_t ptr,
267 handle_t *h, struct buffer_head **bh)
271 *bh = ldiskfs_bread(h, c->ic_object, (int)ptr, 0, &result);
278 * Return pointer to current leaf record. Pointer is valid while corresponding
279 * leaf node is locked and pinned.
281 static struct iam_rec *iam_leaf_rec(const struct iam_leaf *leaf)
283 return iam_leaf_ops(leaf)->rec(leaf);
287 * Return pointer to the current leaf key. This function returns pointer to
288 * the key stored in node.
290 * Caller should assume that returned pointer is only valid while leaf node is
293 static struct iam_key *iam_leaf_key(const struct iam_leaf *leaf)
295 return iam_leaf_ops(leaf)->key(leaf);
298 static int iam_leaf_key_size(const struct iam_leaf *leaf)
300 return iam_leaf_ops(leaf)->key_size(leaf);
303 static struct iam_ikey *iam_leaf_ikey(const struct iam_leaf *leaf,
304 struct iam_ikey *key)
306 return iam_leaf_ops(leaf)->ikey(leaf, key);
309 static int iam_leaf_keycmp(const struct iam_leaf *leaf,
310 const struct iam_key *key)
312 return iam_leaf_ops(leaf)->key_cmp(leaf, key);
315 static int iam_leaf_keyeq(const struct iam_leaf *leaf,
316 const struct iam_key *key)
318 return iam_leaf_ops(leaf)->key_eq(leaf, key);
321 #if LDISKFS_INVARIANT_ON
322 static int iam_leaf_check(struct iam_leaf *leaf);
323 extern int dx_node_check(struct iam_path *p, struct iam_frame *f);
325 static int iam_path_check(struct iam_path *p)
330 struct iam_descr *param;
333 param = iam_path_descr(p);
334 for (i = 0; result && i < ARRAY_SIZE(p->ip_frames); ++i) {
335 f = &p->ip_frames[i];
337 result = dx_node_check(p, f);
339 result = !param->id_ops->id_node_check(p, f);
342 if (result && p->ip_leaf.il_bh != NULL)
343 result = iam_leaf_check(&p->ip_leaf);
345 ldiskfs_std_error(iam_path_obj(p)->i_sb, result);
351 static int iam_leaf_load(struct iam_path *path)
355 struct iam_container *c;
356 struct buffer_head *bh;
357 struct iam_leaf *leaf;
358 struct iam_descr *descr;
360 c = path->ip_container;
361 leaf = &path->ip_leaf;
362 descr = iam_path_descr(path);
363 block = path->ip_frame->leaf;
366 printk(CFS_KERN_EMERG "wrong leaf: %lu %d [%p %p %p]\n",
367 (long unsigned)path->ip_frame->leaf,
368 dx_get_count(dx_node_get_entries(path, path->ip_frame)),
369 path->ip_frames[0].bh, path->ip_frames[1].bh,
370 path->ip_frames[2].bh);
372 err = descr->id_ops->id_node_read(c, block, NULL, &bh);
375 leaf->il_curidx = block;
376 err = iam_leaf_ops(leaf)->init(leaf);
377 assert_inv(ergo(err == 0, iam_leaf_check(leaf)));
382 static void iam_unlock_htree(struct inode *dir, struct dynlock_handle *lh)
385 dynlock_unlock(&LDISKFS_I(dir)->i_htree_lock, lh);
389 static void iam_leaf_unlock(struct iam_leaf *leaf)
391 if (leaf->il_lock != NULL) {
392 iam_unlock_htree(iam_leaf_container(leaf)->ic_object,
395 leaf->il_lock = NULL;
399 static void iam_leaf_fini(struct iam_leaf *leaf)
401 if (leaf->il_path != NULL) {
402 iam_leaf_unlock(leaf);
403 assert_inv(ergo(leaf->il_bh != NULL, iam_leaf_check(leaf)));
404 iam_leaf_ops(leaf)->fini(leaf);
413 static void iam_leaf_start(struct iam_leaf *folio)
415 iam_leaf_ops(folio)->start(folio);
418 void iam_leaf_next(struct iam_leaf *folio)
420 iam_leaf_ops(folio)->next(folio);
423 static void iam_leaf_rec_add(struct iam_leaf *leaf, const struct iam_key *key,
424 const struct iam_rec *rec)
426 iam_leaf_ops(leaf)->rec_add(leaf, key, rec);
429 static void iam_rec_del(struct iam_leaf *leaf, int shift)
431 iam_leaf_ops(leaf)->rec_del(leaf, shift);
434 int iam_leaf_at_end(const struct iam_leaf *leaf)
436 return iam_leaf_ops(leaf)->at_end(leaf);
439 void iam_leaf_split(struct iam_leaf *l, struct buffer_head **bh, iam_ptr_t nr)
441 iam_leaf_ops(l)->split(l, bh, nr);
444 int iam_leaf_can_add(const struct iam_leaf *l,
445 const struct iam_key *k, const struct iam_rec *r)
447 return iam_leaf_ops(l)->can_add(l, k, r);
450 #if LDISKFS_INVARIANT_ON
451 static int iam_leaf_check(struct iam_leaf *leaf)
455 struct iam_lentry *orig;
456 struct iam_path *path;
457 struct iam_container *bag;
464 path = iam_leaf_path(leaf);
465 bag = iam_leaf_container(leaf);
467 result = iam_leaf_ops(leaf)->init(leaf);
472 iam_leaf_start(leaf);
473 k0 = iam_path_ikey(path, 0);
474 k1 = iam_path_ikey(path, 1);
475 while (!iam_leaf_at_end(leaf)) {
476 iam_ikeycpy(bag, k0, k1);
477 iam_ikeycpy(bag, k1, iam_leaf_ikey(leaf, k1));
478 if (!first && iam_ikeycmp(bag, k0, k1) > 0) {
490 static int iam_txn_dirty(handle_t *handle,
491 struct iam_path *path, struct buffer_head *bh)
495 result = ldiskfs_journal_dirty_metadata(handle, bh);
497 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
501 static int iam_txn_add(handle_t *handle,
502 struct iam_path *path, struct buffer_head *bh)
506 result = ldiskfs_journal_get_write_access(handle, bh);
508 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
512 /***********************************************************************/
513 /* iterator interface */
514 /***********************************************************************/
516 static enum iam_it_state it_state(const struct iam_iterator *it)
522 * Helper function returning scratch key.
524 static struct iam_container *iam_it_container(const struct iam_iterator *it)
526 return it->ii_path.ip_container;
529 static inline int it_keycmp(const struct iam_iterator *it,
530 const struct iam_key *k)
532 return iam_leaf_keycmp(&it->ii_path.ip_leaf, k);
535 static inline int it_keyeq(const struct iam_iterator *it,
536 const struct iam_key *k)
538 return iam_leaf_keyeq(&it->ii_path.ip_leaf, k);
541 static int it_ikeycmp(const struct iam_iterator *it, const struct iam_ikey *ik)
543 return iam_ikeycmp(it->ii_path.ip_container,
544 iam_leaf_ikey(&it->ii_path.ip_leaf,
545 iam_path_ikey(&it->ii_path, 0)), ik);
548 static inline int it_at_rec(const struct iam_iterator *it)
550 return !iam_leaf_at_end(&it->ii_path.ip_leaf);
553 static inline int it_before(const struct iam_iterator *it)
555 return it_state(it) == IAM_IT_SKEWED && it_at_rec(it);
559 * Helper wrapper around iam_it_get(): returns 0 (success) only when record
560 * with exactly the same key as asked is found.
562 static int iam_it_get_exact(struct iam_iterator *it, const struct iam_key *k)
566 result = iam_it_get(it, k);
569 else if (result == 0)
571 * Return -ENOENT if cursor is located above record with a key
572 * different from one specified, or in the empty leaf.
574 * XXX returning -ENOENT only works if iam_it_get() never
575 * returns -ENOENT as a legitimate error.
581 void iam_container_write_lock(struct iam_container *ic)
583 cfs_down_write(&ic->ic_sem);
586 void iam_container_write_unlock(struct iam_container *ic)
588 cfs_up_write(&ic->ic_sem);
591 void iam_container_read_lock(struct iam_container *ic)
593 cfs_down_read(&ic->ic_sem);
596 void iam_container_read_unlock(struct iam_container *ic)
598 cfs_up_read(&ic->ic_sem);
602 * Initialize iterator to IAM_IT_DETACHED state.
604 * postcondition: it_state(it) == IAM_IT_DETACHED
606 int iam_it_init(struct iam_iterator *it, struct iam_container *c, __u32 flags,
607 struct iam_path_descr *pd)
609 memset(it, 0, sizeof *it);
610 it->ii_flags = flags;
611 it->ii_state = IAM_IT_DETACHED;
612 iam_path_init(&it->ii_path, c, pd);
615 EXPORT_SYMBOL(iam_it_init);
618 * Finalize iterator and release all resources.
620 * precondition: it_state(it) == IAM_IT_DETACHED
622 void iam_it_fini(struct iam_iterator *it)
624 assert_corr(it_state(it) == IAM_IT_DETACHED);
625 iam_path_fini(&it->ii_path);
627 EXPORT_SYMBOL(iam_it_fini);
630 * this locking primitives are used to protect parts
631 * of dir's htree. protection unit is block: leaf or index
633 struct dynlock_handle *iam_lock_htree(struct inode *dir, unsigned long value,
634 enum dynlock_type lt)
636 return dynlock_lock(&LDISKFS_I(dir)->i_htree_lock, value, lt, GFP_NOFS);
641 int iam_index_lock(struct iam_path *path, struct dynlock_handle **lh)
645 for (f = path->ip_frame; f >= path->ip_frames; --f, ++lh) {
647 *lh = iam_lock_htree(iam_path_obj(path), f->curidx, DLT_READ);
655 * Fast check for frame consistency.
657 static int iam_check_fast(struct iam_path *path, struct iam_frame *frame)
659 struct iam_container *bag;
660 struct iam_entry *next;
661 struct iam_entry *last;
662 struct iam_entry *entries;
663 struct iam_entry *at;
665 bag = path->ip_container;
667 entries = frame->entries;
668 last = iam_entry_shift(path, entries, dx_get_count(entries) - 1);
670 if (unlikely(at > last))
673 if (unlikely(dx_get_block(path, at) != frame->leaf))
676 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, at),
677 path->ip_ikey_target) > 0))
680 next = iam_entry_shift(path, at, +1);
682 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, next),
683 path->ip_ikey_target) <= 0))
689 int dx_index_is_compat(struct iam_path *path)
691 return iam_path_descr(path) == NULL;
697 * search position of specified hash in index
701 struct iam_entry *iam_find_position(struct iam_path *path,
702 struct iam_frame *frame)
709 count = dx_get_count(frame->entries);
710 assert_corr(count && count <= dx_get_limit(frame->entries));
711 p = iam_entry_shift(path, frame->entries,
712 dx_index_is_compat(path) ? 1 : 2);
713 q = iam_entry_shift(path, frame->entries, count - 1);
715 m = iam_entry_shift(path, p, iam_entry_diff(path, q, p) / 2);
716 if (iam_ikeycmp(path->ip_container, iam_ikey_at(path, m),
717 path->ip_ikey_target) > 0)
718 q = iam_entry_shift(path, m, -1);
720 p = iam_entry_shift(path, m, +1);
722 return iam_entry_shift(path, p, -1);
727 static iam_ptr_t iam_find_ptr(struct iam_path *path, struct iam_frame *frame)
729 return dx_get_block(path, iam_find_position(path, frame));
732 void iam_insert_key(struct iam_path *path, struct iam_frame *frame,
733 const struct iam_ikey *key, iam_ptr_t ptr)
735 struct iam_entry *entries = frame->entries;
736 struct iam_entry *new = iam_entry_shift(path, frame->at, +1);
737 int count = dx_get_count(entries);
740 * Unfortunately we cannot assert this, as this function is sometimes
741 * called by VFS under i_sem and without pdirops lock.
743 assert_corr(1 || iam_frame_is_locked(path, frame));
744 assert_corr(count < dx_get_limit(entries));
745 assert_corr(frame->at < iam_entry_shift(path, entries, count));
746 assert_inv(dx_node_check(path, frame));
748 memmove(iam_entry_shift(path, new, 1), new,
749 (char *)iam_entry_shift(path, entries, count) - (char *)new);
750 dx_set_ikey(path, new, key);
751 dx_set_block(path, new, ptr);
752 dx_set_count(entries, count + 1);
753 assert_inv(dx_node_check(path, frame));
756 void iam_insert_key_lock(struct iam_path *path, struct iam_frame *frame,
757 const struct iam_ikey *key, iam_ptr_t ptr)
759 iam_lock_bh(frame->bh);
760 iam_insert_key(path, frame, key, ptr);
761 iam_unlock_bh(frame->bh);
764 * returns 0 if path was unchanged, -EAGAIN otherwise.
766 static int iam_check_path(struct iam_path *path, struct iam_frame *frame)
770 iam_lock_bh(frame->bh);
771 equal = iam_check_fast(path, frame) == 0 ||
772 frame->leaf == iam_find_ptr(path, frame);
773 DX_DEVAL(iam_lock_stats.dls_bh_again += !equal);
774 iam_unlock_bh(frame->bh);
776 return equal ? 0 : -EAGAIN;
779 static int iam_lookup_try(struct iam_path *path)
785 struct iam_descr *param;
786 struct iam_frame *frame;
787 struct iam_container *c;
789 param = iam_path_descr(path);
790 c = path->ip_container;
792 ptr = param->id_ops->id_root_ptr(c);
793 for (frame = path->ip_frames, i = 0; i <= path->ip_indirect;
795 err = param->id_ops->id_node_read(c, (iam_ptr_t)ptr, NULL,
799 iam_lock_bh(frame->bh);
801 * node must be initialized under bh lock because concurrent
802 * creation procedure may change it and iam_lookup_try() will
803 * see obsolete tree height. -bzzz
808 if (LDISKFS_INVARIANT_ON) {
809 err = param->id_ops->id_node_check(path, frame);
814 err = param->id_ops->id_node_load(path, frame);
818 assert_inv(dx_node_check(path, frame));
820 * splitting may change root index block and move hash we're
821 * looking for into another index block so, we have to check
822 * this situation and repeat from begining if path got changed
826 err = iam_check_path(path, frame - 1);
831 frame->at = iam_find_position(path, frame);
833 frame->leaf = ptr = dx_get_block(path, frame->at);
835 iam_unlock_bh(frame->bh);
839 iam_unlock_bh(frame->bh);
840 path->ip_frame = --frame;
844 static int __iam_path_lookup(struct iam_path *path)
849 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++ i)
850 assert(path->ip_frames[i].bh == NULL);
853 err = iam_lookup_try(path);
857 } while (err == -EAGAIN);
863 * returns 0 if path was unchanged, -EAGAIN otherwise.
865 static int iam_check_full_path(struct iam_path *path, int search)
867 struct iam_frame *bottom;
868 struct iam_frame *scan;
874 for (bottom = path->ip_frames, i = 0;
875 i < DX_MAX_TREE_HEIGHT && bottom->bh != NULL; ++bottom, ++i) {
876 ; /* find last filled in frame */
880 * Lock frames, bottom to top.
882 for (scan = bottom - 1; scan >= path->ip_frames; --scan)
883 iam_lock_bh(scan->bh);
885 * Check them top to bottom.
888 for (scan = path->ip_frames; scan < bottom; ++scan) {
889 struct iam_entry *pos;
892 if (iam_check_fast(path, scan) == 0)
895 pos = iam_find_position(path, scan);
896 if (scan->leaf != dx_get_block(path, pos)) {
902 pos = iam_entry_shift(path, scan->entries,
903 dx_get_count(scan->entries) - 1);
904 if (scan->at > pos ||
905 scan->leaf != dx_get_block(path, scan->at)) {
913 * Unlock top to bottom.
915 for (scan = path->ip_frames; scan < bottom; ++scan)
916 iam_unlock_bh(scan->bh);
917 DX_DEVAL(iam_lock_stats.dls_bh_full_again += !!result);
925 * Performs path lookup and returns with found leaf (if any) locked by htree
928 int iam_lookup_lock(struct iam_path *path,
929 struct dynlock_handle **dl, enum dynlock_type lt)
934 dir = iam_path_obj(path);
935 while ((result = __iam_path_lookup(path)) == 0) {
937 *dl = iam_lock_htree(dir, path->ip_frame->leaf, lt);
945 * while locking leaf we just found may get split so we need
946 * to check this -bzzz
948 if (iam_check_full_path(path, 1) == 0)
950 iam_unlock_htree(dir, *dl);
957 * Performs tree top-to-bottom traversal starting from root, and loads leaf
960 static int iam_path_lookup(struct iam_path *path, int index)
962 struct iam_container *c;
963 struct iam_descr *descr;
964 struct iam_leaf *leaf;
967 c = path->ip_container;
968 leaf = &path->ip_leaf;
969 descr = iam_path_descr(path);
970 result = iam_lookup_lock(path, &leaf->il_lock, DLT_WRITE);
971 assert_inv(iam_path_check(path));
974 result = iam_leaf_load(path);
975 assert_inv(ergo(result == 0, iam_leaf_check(leaf)));
979 result = iam_leaf_ops(leaf)->
980 ilookup(leaf, path->ip_ikey_target);
982 result = iam_leaf_ops(leaf)->
983 lookup(leaf, path->ip_key_target);
987 iam_leaf_unlock(leaf);
993 * Common part of iam_it_{i,}get().
995 static int __iam_it_get(struct iam_iterator *it, int index)
998 assert_corr(it_state(it) == IAM_IT_DETACHED);
1000 result = iam_path_lookup(&it->ii_path, index);
1004 collision = result & IAM_LOOKUP_LAST;
1005 switch (result & ~IAM_LOOKUP_LAST) {
1006 case IAM_LOOKUP_EXACT:
1008 it->ii_state = IAM_IT_ATTACHED;
1012 it->ii_state = IAM_IT_ATTACHED;
1014 case IAM_LOOKUP_BEFORE:
1015 case IAM_LOOKUP_EMPTY:
1017 it->ii_state = IAM_IT_SKEWED;
1022 result |= collision;
1025 * See iam_it_get_exact() for explanation.
1027 assert_corr(result != -ENOENT);
1032 * Correct hash, but not the same key was found, iterate through hash
1033 * collision chain, looking for correct record.
1035 static int iam_it_collision(struct iam_iterator *it)
1039 assert(ergo(it_at_rec(it), !it_keyeq(it, it->ii_path.ip_key_target)));
1041 while ((result = iam_it_next(it)) == 0) {
1042 do_corr(schedule());
1043 if (it_ikeycmp(it, it->ii_path.ip_ikey_target) != 0)
1045 if (it_keyeq(it, it->ii_path.ip_key_target))
1052 * Attach iterator. After successful completion, @it points to record with
1053 * least key not larger than @k.
1055 * Return value: 0: positioned on existing record,
1056 * +ve: exact position found,
1059 * precondition: it_state(it) == IAM_IT_DETACHED
1060 * postcondition: ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1061 * it_keycmp(it, k) <= 0)
1063 int iam_it_get(struct iam_iterator *it, const struct iam_key *k)
1066 assert_corr(it_state(it) == IAM_IT_DETACHED);
1068 it->ii_path.ip_ikey_target = NULL;
1069 it->ii_path.ip_key_target = k;
1071 result = __iam_it_get(it, 0);
1073 if (result == IAM_LOOKUP_LAST) {
1074 result = iam_it_collision(it);
1078 result = __iam_it_get(it, 0);
1083 result &= ~IAM_LOOKUP_LAST;
1085 assert_corr(ergo(result > 0, it_keycmp(it, k) == 0));
1086 assert_corr(ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1087 it_keycmp(it, k) <= 0));
1090 EXPORT_SYMBOL(iam_it_get);
1093 * Attach iterator by index key.
1095 static int iam_it_iget(struct iam_iterator *it, const struct iam_ikey *k)
1097 assert_corr(it_state(it) == IAM_IT_DETACHED);
1099 it->ii_path.ip_ikey_target = k;
1100 return __iam_it_get(it, 1) & ~IAM_LOOKUP_LAST;
1104 * Attach iterator, and assure it points to the record (not skewed).
1106 * Return value: 0: positioned on existing record,
1107 * +ve: exact position found,
1110 * precondition: it_state(it) == IAM_IT_DETACHED &&
1111 * !(it->ii_flags&IAM_IT_WRITE)
1112 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED)
1114 int iam_it_get_at(struct iam_iterator *it, const struct iam_key *k)
1117 assert_corr(it_state(it) == IAM_IT_DETACHED &&
1118 !(it->ii_flags&IAM_IT_WRITE));
1119 result = iam_it_get(it, k);
1121 if (it_state(it) != IAM_IT_ATTACHED) {
1122 assert_corr(it_state(it) == IAM_IT_SKEWED);
1123 result = iam_it_next(it);
1126 assert_corr(ergo(result >= 0, it_state(it) == IAM_IT_ATTACHED));
1129 EXPORT_SYMBOL(iam_it_get_at);
1132 * Duplicates iterator.
1134 * postcondition: it_state(dst) == it_state(src) &&
1135 * iam_it_container(dst) == iam_it_container(src) &&
1136 * dst->ii_flags = src->ii_flags &&
1137 * ergo(it_state(src) == IAM_IT_ATTACHED,
1138 * iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1139 * iam_it_key_get(dst) == iam_it_key_get(src))
1141 void iam_it_dup(struct iam_iterator *dst, const struct iam_iterator *src)
1143 dst->ii_flags = src->ii_flags;
1144 dst->ii_state = src->ii_state;
1145 /* XXX not yet. iam_path_dup(&dst->ii_path, &src->ii_path); */
1147 * XXX: duplicate lock.
1149 assert_corr(it_state(dst) == it_state(src));
1150 assert_corr(iam_it_container(dst) == iam_it_container(src));
1151 assert_corr(dst->ii_flags = src->ii_flags);
1152 assert_corr(ergo(it_state(src) == IAM_IT_ATTACHED,
1153 iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1154 iam_it_key_get(dst) == iam_it_key_get(src)));
1159 * Detach iterator. Does nothing it detached state.
1161 * postcondition: it_state(it) == IAM_IT_DETACHED
1163 void iam_it_put(struct iam_iterator *it)
1165 if (it->ii_state != IAM_IT_DETACHED) {
1166 it->ii_state = IAM_IT_DETACHED;
1167 iam_leaf_fini(&it->ii_path.ip_leaf);
1170 EXPORT_SYMBOL(iam_it_put);
1172 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1173 struct iam_ikey *ikey);
1177 * This function increments the frame pointer to search the next leaf
1178 * block, and reads in the necessary intervening nodes if the search
1179 * should be necessary. Whether or not the search is necessary is
1180 * controlled by the hash parameter. If the hash value is even, then
1181 * the search is only continued if the next block starts with that
1182 * hash value. This is used if we are searching for a specific file.
1184 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1186 * This function returns 1 if the caller should continue to search,
1187 * or 0 if it should not. If there is an error reading one of the
1188 * index blocks, it will a negative error code.
1190 * If start_hash is non-null, it will be filled in with the starting
1191 * hash of the next page.
1193 static int iam_htree_advance(struct inode *dir, __u32 hash,
1194 struct iam_path *path, __u32 *start_hash,
1197 struct iam_frame *p;
1198 struct buffer_head *bh;
1199 int err, num_frames = 0;
1204 * Find the next leaf page by incrementing the frame pointer.
1205 * If we run out of entries in the interior node, loop around and
1206 * increment pointer in the parent node. When we break out of
1207 * this loop, num_frames indicates the number of interior
1208 * nodes need to be read.
1211 do_corr(schedule());
1213 p->at = iam_entry_shift(path, p->at, +1);
1214 if (p->at < iam_entry_shift(path, p->entries,
1215 dx_get_count(p->entries))) {
1216 p->leaf = dx_get_block(path, p->at);
1217 iam_unlock_bh(p->bh);
1220 iam_unlock_bh(p->bh);
1221 if (p == path->ip_frames)
1232 * If the hash is 1, then continue only if the next page has a
1233 * continuation hash of any value. This is used for readdir
1234 * handling. Otherwise, check to see if the hash matches the
1235 * desired contiuation hash. If it doesn't, return since
1236 * there's no point to read in the successive index pages.
1238 dx_get_ikey(path, p->at, (struct iam_ikey *)&bhash);
1240 *start_hash = bhash;
1241 if ((hash & 1) == 0) {
1242 if ((bhash & ~1) != hash)
1247 * If the hash is HASH_NB_ALWAYS, we always go to the next
1248 * block so no check is necessary
1250 while (num_frames--) {
1253 do_corr(schedule());
1255 idx = p->leaf = dx_get_block(path, p->at);
1256 iam_unlock_bh(p->bh);
1257 err = iam_path_descr(path)->id_ops->
1258 id_node_read(path->ip_container, idx, NULL, &bh);
1260 return err; /* Failure */
1263 assert_corr(p->bh != bh);
1265 p->entries = dx_node_get_entries(path, p);
1266 p->at = iam_entry_shift(path, p->entries, !compat);
1267 assert_corr(p->curidx != idx);
1270 assert_corr(p->leaf != dx_get_block(path, p->at));
1271 p->leaf = dx_get_block(path, p->at);
1272 iam_unlock_bh(p->bh);
1273 assert_inv(dx_node_check(path, p));
1279 static inline int iam_index_advance(struct iam_path *path)
1281 return iam_htree_advance(iam_path_obj(path), 0, path, NULL, 0);
1284 static void iam_unlock_array(struct inode *dir, struct dynlock_handle **lh)
1288 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++i, ++lh) {
1290 iam_unlock_htree(dir, *lh);
1296 * Advance index part of @path to point to the next leaf. Returns 1 on
1297 * success, 0, when end of container was reached. Leaf node is locked.
1299 int iam_index_next(struct iam_container *c, struct iam_path *path)
1302 struct dynlock_handle *lh[DX_MAX_TREE_HEIGHT] = { 0, };
1304 struct inode *object;
1307 * Locking for iam_index_next()... is to be described.
1310 object = c->ic_object;
1311 cursor = path->ip_frame->leaf;
1314 result = iam_index_lock(path, lh);
1315 do_corr(schedule());
1319 result = iam_check_full_path(path, 0);
1320 if (result == 0 && cursor == path->ip_frame->leaf) {
1321 result = iam_index_advance(path);
1323 assert_corr(result == 0 ||
1324 cursor != path->ip_frame->leaf);
1328 iam_unlock_array(object, lh);
1330 iam_path_release(path);
1331 do_corr(schedule());
1333 result = __iam_path_lookup(path);
1337 while (path->ip_frame->leaf != cursor) {
1338 do_corr(schedule());
1340 result = iam_index_lock(path, lh);
1341 do_corr(schedule());
1345 result = iam_check_full_path(path, 0);
1349 result = iam_index_advance(path);
1351 CERROR("cannot find cursor : %u\n",
1357 result = iam_check_full_path(path, 0);
1360 iam_unlock_array(object, lh);
1362 } while (result == -EAGAIN);
1366 iam_unlock_array(object, lh);
1371 * Move iterator one record right.
1373 * Return value: 0: success,
1374 * +1: end of container reached
1377 * precondition: (it_state(it) == IAM_IT_ATTACHED ||
1378 * it_state(it) == IAM_IT_SKEWED) && it->ii_flags&IAM_IT_MOVE
1379 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED) &&
1380 * ergo(result > 0, it_state(it) == IAM_IT_DETACHED)
1382 int iam_it_next(struct iam_iterator *it)
1385 struct iam_path *path;
1386 struct iam_leaf *leaf;
1388 do_corr(struct iam_ikey *ik_orig);
1390 /* assert_corr(it->ii_flags&IAM_IT_MOVE); */
1391 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1392 it_state(it) == IAM_IT_SKEWED);
1394 path = &it->ii_path;
1395 leaf = &path->ip_leaf;
1396 obj = iam_path_obj(path);
1398 assert_corr(iam_leaf_is_locked(leaf));
1401 do_corr(ik_orig = it_at_rec(it) ?
1402 iam_it_ikey_get(it, iam_path_ikey(path, 2)) : NULL);
1403 if (it_before(it)) {
1404 assert_corr(!iam_leaf_at_end(leaf));
1405 it->ii_state = IAM_IT_ATTACHED;
1407 if (!iam_leaf_at_end(leaf))
1408 /* advance within leaf node */
1409 iam_leaf_next(leaf);
1411 * multiple iterations may be necessary due to empty leaves.
1413 while (result == 0 && iam_leaf_at_end(leaf)) {
1414 do_corr(schedule());
1415 /* advance index portion of the path */
1416 result = iam_index_next(iam_it_container(it), path);
1417 assert_corr(iam_leaf_is_locked(leaf));
1419 struct dynlock_handle *lh;
1420 lh = iam_lock_htree(obj, path->ip_frame->leaf,
1423 iam_leaf_fini(leaf);
1425 result = iam_leaf_load(path);
1427 iam_leaf_start(leaf);
1430 } else if (result == 0)
1431 /* end of container reached */
1437 it->ii_state = IAM_IT_ATTACHED;
1439 assert_corr(ergo(result == 0, it_state(it) == IAM_IT_ATTACHED));
1440 assert_corr(ergo(result > 0, it_state(it) == IAM_IT_DETACHED));
1441 assert_corr(ergo(result == 0 && ik_orig != NULL,
1442 it_ikeycmp(it, ik_orig) >= 0));
1445 EXPORT_SYMBOL(iam_it_next);
1448 * Return pointer to the record under iterator.
1450 * precondition: it_state(it) == IAM_IT_ATTACHED && it_at_rec(it)
1451 * postcondition: it_state(it) == IAM_IT_ATTACHED
1453 struct iam_rec *iam_it_rec_get(const struct iam_iterator *it)
1455 assert_corr(it_state(it) == IAM_IT_ATTACHED);
1456 assert_corr(it_at_rec(it));
1457 return iam_leaf_rec(&it->ii_path.ip_leaf);
1459 EXPORT_SYMBOL(iam_it_rec_get);
1461 static void iam_it_reccpy(struct iam_iterator *it, const struct iam_rec *r)
1463 struct iam_leaf *folio;
1465 folio = &it->ii_path.ip_leaf;
1466 iam_leaf_ops(folio)->rec_set(folio, r);
1470 * Replace contents of record under iterator.
1472 * precondition: it_state(it) == IAM_IT_ATTACHED &&
1473 * it->ii_flags&IAM_IT_WRITE
1474 * postcondition: it_state(it) == IAM_IT_ATTACHED &&
1475 * ergo(result == 0, !memcmp(iam_it_rec_get(it), r, ...))
1477 int iam_it_rec_set(handle_t *h,
1478 struct iam_iterator *it, const struct iam_rec *r)
1481 struct iam_path *path;
1482 struct buffer_head *bh;
1484 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
1485 it->ii_flags&IAM_IT_WRITE);
1486 assert_corr(it_at_rec(it));
1488 path = &it->ii_path;
1489 bh = path->ip_leaf.il_bh;
1490 result = iam_txn_add(h, path, bh);
1492 iam_it_reccpy(it, r);
1493 result = iam_txn_dirty(h, path, bh);
1497 EXPORT_SYMBOL(iam_it_rec_set);
1500 * Return pointer to the index key under iterator.
1502 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1503 * it_state(it) == IAM_IT_SKEWED
1505 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1506 struct iam_ikey *ikey)
1508 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1509 it_state(it) == IAM_IT_SKEWED);
1510 assert_corr(it_at_rec(it));
1511 return iam_leaf_ikey(&it->ii_path.ip_leaf, ikey);
1515 * Return pointer to the key under iterator.
1517 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1518 * it_state(it) == IAM_IT_SKEWED
1520 struct iam_key *iam_it_key_get(const struct iam_iterator *it)
1522 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1523 it_state(it) == IAM_IT_SKEWED);
1524 assert_corr(it_at_rec(it));
1525 return iam_leaf_key(&it->ii_path.ip_leaf);
1527 EXPORT_SYMBOL(iam_it_key_get);
1530 * Return size of key under iterator (in bytes)
1532 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1533 * it_state(it) == IAM_IT_SKEWED
1535 int iam_it_key_size(const struct iam_iterator *it)
1537 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1538 it_state(it) == IAM_IT_SKEWED);
1539 assert_corr(it_at_rec(it));
1540 return iam_leaf_key_size(&it->ii_path.ip_leaf);
1542 EXPORT_SYMBOL(iam_it_key_size);
1545 * Insertion of new record. Interaction with jbd during non-trivial case (when
1546 * split happens) is as following:
1548 * - new leaf node is involved into transaction by ldiskfs_append();
1550 * - old leaf node is involved into transaction by iam_add_rec();
1552 * - leaf where insertion point ends in, is marked dirty by iam_add_rec();
1554 * - leaf without insertion point is marked dirty (as @new_leaf) by
1557 * - split index nodes are involved into transaction and marked dirty by
1558 * split_index_node().
1560 * - "safe" index node, which is no split, but where new pointer is inserted
1561 * is involved into transaction and marked dirty by split_index_node().
1563 * - index node where pointer to new leaf is inserted is involved into
1564 * transaction by split_index_node() and marked dirty by iam_add_rec().
1566 * - inode is marked dirty by iam_add_rec().
1570 static int iam_new_leaf(handle_t *handle, struct iam_leaf *leaf)
1574 struct buffer_head *new_leaf;
1575 struct buffer_head *old_leaf;
1576 struct iam_container *c;
1578 struct iam_path *path;
1580 assert_inv(iam_leaf_check(leaf));
1582 c = iam_leaf_container(leaf);
1583 path = leaf->il_path;
1586 new_leaf = ldiskfs_append(handle, obj, (__u32 *)&blknr, &err);
1587 do_corr(schedule());
1588 if (new_leaf != NULL) {
1589 struct dynlock_handle *lh;
1591 lh = iam_lock_htree(obj, blknr, DLT_WRITE);
1592 do_corr(schedule());
1594 iam_leaf_ops(leaf)->init_new(c, new_leaf);
1595 do_corr(schedule());
1596 old_leaf = leaf->il_bh;
1597 iam_leaf_split(leaf, &new_leaf, blknr);
1598 if (old_leaf != leaf->il_bh) {
1600 * Switched to the new leaf.
1602 iam_leaf_unlock(leaf);
1604 path->ip_frame->leaf = blknr;
1606 iam_unlock_htree(obj, lh);
1607 do_corr(schedule());
1608 err = iam_txn_dirty(handle, path, new_leaf);
1611 err = ldiskfs_mark_inode_dirty(handle, obj);
1612 do_corr(schedule());
1616 assert_inv(iam_leaf_check(leaf));
1617 assert_inv(iam_leaf_check(&iam_leaf_path(leaf)->ip_leaf));
1618 assert_inv(iam_path_check(iam_leaf_path(leaf)));
1622 static inline void dx_set_limit(struct iam_entry *entries, unsigned value)
1624 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
1627 static int iam_shift_entries(struct iam_path *path,
1628 struct iam_frame *frame, unsigned count,
1629 struct iam_entry *entries, struct iam_entry *entries2,
1636 struct iam_frame *parent = frame - 1;
1637 struct iam_ikey *pivot = iam_path_ikey(path, 3);
1639 delta = dx_index_is_compat(path) ? 0 : +1;
1641 count1 = count/2 + delta;
1642 count2 = count - count1;
1643 dx_get_ikey(path, iam_entry_shift(path, entries, count1), pivot);
1645 dxtrace(printk("Split index %d/%d\n", count1, count2));
1647 memcpy((char *) iam_entry_shift(path, entries2, delta),
1648 (char *) iam_entry_shift(path, entries, count1),
1649 count2 * iam_entry_size(path));
1651 dx_set_count(entries2, count2 + delta);
1652 dx_set_limit(entries2, dx_node_limit(path));
1655 * NOTE: very subtle piece of code competing dx_probe() may find 2nd
1656 * level index in root index, then we insert new index here and set
1657 * new count in that 2nd level index. so, dx_probe() may see 2nd level
1658 * index w/o hash it looks for. the solution is to check root index
1659 * after we locked just founded 2nd level index -bzzz
1661 iam_insert_key_lock(path, parent, pivot, newblock);
1664 * now old and new 2nd level index blocks contain all pointers, so
1665 * dx_probe() may find it in the both. it's OK -bzzz
1667 iam_lock_bh(frame->bh);
1668 dx_set_count(entries, count1);
1669 iam_unlock_bh(frame->bh);
1672 * now old 2nd level index block points to first half of leafs. it's
1673 * importand that dx_probe() must check root index block for changes
1674 * under dx_lock_bh(frame->bh) -bzzz
1681 int split_index_node(handle_t *handle, struct iam_path *path,
1682 struct dynlock_handle **lh)
1685 struct iam_entry *entries; /* old block contents */
1686 struct iam_entry *entries2; /* new block contents */
1687 struct iam_frame *frame, *safe;
1688 struct buffer_head *bh_new[DX_MAX_TREE_HEIGHT] = {0};
1689 u32 newblock[DX_MAX_TREE_HEIGHT] = {0};
1690 struct dynlock_handle *lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1691 struct dynlock_handle *new_lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1692 struct inode *dir = iam_path_obj(path);
1693 struct iam_descr *descr;
1697 descr = iam_path_descr(path);
1699 * Algorithm below depends on this.
1701 assert_corr(dx_root_limit(path) < dx_node_limit(path));
1703 frame = path->ip_frame;
1704 entries = frame->entries;
1707 * Tall-tree handling: we might have to split multiple index blocks
1708 * all the way up to tree root. Tricky point here is error handling:
1709 * to avoid complicated undo/rollback we
1711 * - first allocate all necessary blocks
1713 * - insert pointers into them atomically.
1717 * Locking: leaf is already locked. htree-locks are acquired on all
1718 * index nodes that require split bottom-to-top, on the "safe" node,
1719 * and on all new nodes
1722 dxtrace(printk("using %u of %u node entries\n",
1723 dx_get_count(entries), dx_get_limit(entries)));
1725 /* What levels need split? */
1726 for (nr_splet = 0; frame >= path->ip_frames &&
1727 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1728 --frame, ++nr_splet) {
1729 do_corr(schedule());
1730 if (nr_splet == DX_MAX_TREE_HEIGHT) {
1732 CWARN(dir->i_sb, __FUNCTION__,
1733 "Directory index full!\n");
1743 * Lock all nodes, bottom to top.
1745 for (frame = path->ip_frame, i = nr_splet; i >= 0; --i, --frame) {
1746 do_corr(schedule());
1747 lock[i] = iam_lock_htree(dir, frame->curidx, DLT_WRITE);
1748 if (lock[i] == NULL) {
1755 * Check for concurrent index modification.
1757 err = iam_check_full_path(path, 1);
1761 * And check that the same number of nodes is to be split.
1763 for (i = 0, frame = path->ip_frame; frame >= path->ip_frames &&
1764 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1768 if (i != nr_splet) {
1773 /* Go back down, allocating blocks, locking them, and adding into
1775 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1776 bh_new[i] = ldiskfs_append (handle, dir, &newblock[i], &err);
1777 do_corr(schedule());
1779 descr->id_ops->id_node_init(path->ip_container,
1782 new_lock[i] = iam_lock_htree(dir, newblock[i], DLT_WRITE);
1783 if (new_lock[i] == NULL) {
1787 do_corr(schedule());
1788 BUFFER_TRACE(frame->bh, "get_write_access");
1789 err = ldiskfs_journal_get_write_access(handle, frame->bh);
1793 /* Add "safe" node to transaction too */
1794 if (safe + 1 != path->ip_frames) {
1795 do_corr(schedule());
1796 err = ldiskfs_journal_get_write_access(handle, safe->bh);
1801 /* Go through nodes once more, inserting pointers */
1802 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1805 struct buffer_head *bh2;
1806 struct buffer_head *bh;
1808 entries = frame->entries;
1809 count = dx_get_count(entries);
1810 idx = iam_entry_diff(path, frame->at, entries);
1813 entries2 = dx_get_entries(path, bh2->b_data, 0);
1816 if (frame == path->ip_frames) {
1817 /* splitting root node. Tricky point:
1819 * In the "normal" B-tree we'd split root *and* add
1820 * new root to the tree with pointers to the old root
1821 * and its sibling (thus introducing two new nodes).
1823 * In htree it's enough to add one node, because
1824 * capacity of the root node is smaller than that of
1827 struct iam_frame *frames;
1828 struct iam_entry *next;
1830 assert_corr(i == 0);
1832 do_corr(schedule());
1834 frames = path->ip_frames;
1835 memcpy((char *) entries2, (char *) entries,
1836 count * iam_entry_size(path));
1837 dx_set_limit(entries2, dx_node_limit(path));
1840 iam_lock_bh(frame->bh);
1841 next = descr->id_ops->id_root_inc(path->ip_container,
1843 dx_set_block(path, next, newblock[0]);
1844 iam_unlock_bh(frame->bh);
1846 do_corr(schedule());
1847 /* Shift frames in the path */
1848 memmove(frames + 2, frames + 1,
1849 (sizeof path->ip_frames) - 2 * sizeof frames[0]);
1850 /* Add new access path frame */
1851 frames[1].at = iam_entry_shift(path, entries2, idx);
1852 frames[1].entries = entries = entries2;
1854 assert_inv(dx_node_check(path, frame));
1857 assert_inv(dx_node_check(path, frame));
1858 bh_new[0] = NULL; /* buffer head is "consumed" */
1859 err = ldiskfs_journal_get_write_access(handle, bh2);
1862 do_corr(schedule());
1864 /* splitting non-root index node. */
1865 struct iam_frame *parent = frame - 1;
1867 do_corr(schedule());
1868 count = iam_shift_entries(path, frame, count,
1869 entries, entries2, newblock[i]);
1870 /* Which index block gets the new entry? */
1872 int d = dx_index_is_compat(path) ? 0 : +1;
1874 frame->at = iam_entry_shift(path, entries2,
1876 frame->entries = entries = entries2;
1877 frame->curidx = newblock[i];
1878 swap(frame->bh, bh2);
1879 assert_corr(lock[i + 1] != NULL);
1880 assert_corr(new_lock[i] != NULL);
1881 swap(lock[i + 1], new_lock[i]);
1883 parent->at = iam_entry_shift(path,
1886 assert_inv(dx_node_check(path, frame));
1887 assert_inv(dx_node_check(path, parent));
1888 dxtrace(dx_show_index ("node", frame->entries));
1889 dxtrace(dx_show_index ("node",
1890 ((struct dx_node *) bh2->b_data)->entries));
1891 err = ldiskfs_journal_dirty_metadata(handle, bh2);
1894 do_corr(schedule());
1895 err = ldiskfs_journal_dirty_metadata(handle, parent->bh);
1899 do_corr(schedule());
1900 err = ldiskfs_journal_dirty_metadata(handle, bh);
1905 * This function was called to make insertion of new leaf
1906 * possible. Check that it fulfilled its obligations.
1908 assert_corr(dx_get_count(path->ip_frame->entries) <
1909 dx_get_limit(path->ip_frame->entries));
1910 assert_corr(lock[nr_splet] != NULL);
1911 *lh = lock[nr_splet];
1912 lock[nr_splet] = NULL;
1915 * Log ->i_size modification.
1917 err = ldiskfs_mark_inode_dirty(handle, dir);
1923 ldiskfs_std_error(dir->i_sb, err);
1926 iam_unlock_array(dir, lock);
1927 iam_unlock_array(dir, new_lock);
1929 assert_corr(err || iam_frame_is_locked(path, path->ip_frame));
1931 do_corr(schedule());
1932 for (i = 0; i < ARRAY_SIZE(bh_new); ++i) {
1933 if (bh_new[i] != NULL)
1939 static int iam_add_rec(handle_t *handle, struct iam_iterator *it,
1940 struct iam_path *path,
1941 const struct iam_key *k, const struct iam_rec *r)
1944 struct iam_leaf *leaf;
1946 leaf = &path->ip_leaf;
1947 assert_inv(iam_leaf_check(leaf));
1948 assert_inv(iam_path_check(path));
1949 err = iam_txn_add(handle, path, leaf->il_bh);
1951 do_corr(schedule());
1952 if (!iam_leaf_can_add(leaf, k, r)) {
1953 struct dynlock_handle *lh = NULL;
1956 assert_corr(lh == NULL);
1957 do_corr(schedule());
1958 err = split_index_node(handle, path, &lh);
1959 if (err == -EAGAIN) {
1960 assert_corr(lh == NULL);
1962 iam_path_fini(path);
1963 it->ii_state = IAM_IT_DETACHED;
1965 do_corr(schedule());
1966 err = iam_it_get_exact(it, k);
1968 err = +1; /* repeat split */
1973 assert_inv(iam_path_check(path));
1975 assert_corr(lh != NULL);
1976 do_corr(schedule());
1977 err = iam_new_leaf(handle, leaf);
1979 err = iam_txn_dirty(handle, path,
1980 path->ip_frame->bh);
1982 iam_unlock_htree(iam_path_obj(path), lh);
1983 do_corr(schedule());
1986 iam_leaf_rec_add(leaf, k, r);
1987 err = iam_txn_dirty(handle, path, leaf->il_bh);
1990 assert_inv(iam_leaf_check(leaf));
1991 assert_inv(iam_leaf_check(&path->ip_leaf));
1992 assert_inv(iam_path_check(path));
1997 * Insert new record with key @k and contents from @r, shifting records to the
1998 * right. On success, iterator is positioned on the newly inserted record.
2000 * precondition: it->ii_flags&IAM_IT_WRITE &&
2001 * (it_state(it) == IAM_IT_ATTACHED ||
2002 * it_state(it) == IAM_IT_SKEWED) &&
2003 * ergo(it_state(it) == IAM_IT_ATTACHED,
2004 * it_keycmp(it, k) <= 0) &&
2005 * ergo(it_before(it), it_keycmp(it, k) > 0));
2006 * postcondition: ergo(result == 0,
2007 * it_state(it) == IAM_IT_ATTACHED &&
2008 * it_keycmp(it, k) == 0 &&
2009 * !memcmp(iam_it_rec_get(it), r, ...))
2011 int iam_it_rec_insert(handle_t *h, struct iam_iterator *it,
2012 const struct iam_key *k, const struct iam_rec *r)
2015 struct iam_path *path;
2017 path = &it->ii_path;
2019 assert_corr(it->ii_flags&IAM_IT_WRITE);
2020 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2021 it_state(it) == IAM_IT_SKEWED);
2022 assert_corr(ergo(it_state(it) == IAM_IT_ATTACHED,
2023 it_keycmp(it, k) <= 0));
2024 assert_corr(ergo(it_before(it), it_keycmp(it, k) > 0));
2025 result = iam_add_rec(h, it, path, k, r);
2027 it->ii_state = IAM_IT_ATTACHED;
2028 assert_corr(ergo(result == 0,
2029 it_state(it) == IAM_IT_ATTACHED &&
2030 it_keycmp(it, k) == 0));
2033 EXPORT_SYMBOL(iam_it_rec_insert);
2036 * Delete record under iterator.
2038 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2039 * it->ii_flags&IAM_IT_WRITE &&
2041 * postcondition: it_state(it) == IAM_IT_ATTACHED ||
2042 * it_state(it) == IAM_IT_DETACHED
2044 int iam_it_rec_delete(handle_t *h, struct iam_iterator *it)
2047 struct iam_leaf *leaf;
2048 struct iam_path *path;
2050 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
2051 it->ii_flags&IAM_IT_WRITE);
2052 assert_corr(it_at_rec(it));
2054 path = &it->ii_path;
2055 leaf = &path->ip_leaf;
2057 assert_inv(iam_leaf_check(leaf));
2058 assert_inv(iam_path_check(path));
2060 result = iam_txn_add(h, path, leaf->il_bh);
2062 * no compaction for now.
2065 iam_rec_del(leaf, it->ii_flags&IAM_IT_MOVE);
2066 result = iam_txn_dirty(h, path, leaf->il_bh);
2067 if (result == 0 && iam_leaf_at_end(leaf) &&
2068 it->ii_flags&IAM_IT_MOVE) {
2069 result = iam_it_next(it);
2074 assert_inv(iam_leaf_check(leaf));
2075 assert_inv(iam_path_check(path));
2076 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2077 it_state(it) == IAM_IT_DETACHED);
2080 EXPORT_SYMBOL(iam_it_rec_delete);
2083 * Convert iterator to cookie.
2085 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2086 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2087 * postcondition: it_state(it) == IAM_IT_ATTACHED
2089 iam_pos_t iam_it_store(const struct iam_iterator *it)
2093 assert_corr(it_state(it) == IAM_IT_ATTACHED);
2094 assert_corr(it_at_rec(it));
2095 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <=
2099 return *(iam_pos_t *)iam_it_ikey_get(it, (void *)&result);
2101 EXPORT_SYMBOL(iam_it_store);
2104 * Restore iterator from cookie.
2106 * precondition: it_state(it) == IAM_IT_DETACHED && it->ii_flags&IAM_IT_MOVE &&
2107 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2108 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED &&
2109 * iam_it_store(it) == pos)
2111 int iam_it_load(struct iam_iterator *it, iam_pos_t pos)
2113 assert_corr(it_state(it) == IAM_IT_DETACHED &&
2114 it->ii_flags&IAM_IT_MOVE);
2115 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <= sizeof pos);
2116 return iam_it_iget(it, (struct iam_ikey *)&pos);
2118 EXPORT_SYMBOL(iam_it_load);
2120 /***********************************************************************/
2122 /***********************************************************************/
2124 static inline int ptr_inside(void *base, size_t size, void *ptr)
2126 return (base <= ptr) && (ptr < base + size);
2129 int iam_frame_invariant(struct iam_frame *f)
2133 f->bh->b_data != NULL &&
2134 ptr_inside(f->bh->b_data, f->bh->b_size, f->entries) &&
2135 ptr_inside(f->bh->b_data, f->bh->b_size, f->at) &&
2136 f->entries <= f->at);
2138 int iam_leaf_invariant(struct iam_leaf *l)
2142 l->il_bh->b_data != NULL &&
2143 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_entries) &&
2144 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_at) &&
2145 l->il_entries <= l->il_at;
2148 int iam_path_invariant(struct iam_path *p)
2152 if (p->ip_container == NULL ||
2153 p->ip_indirect < 0 || p->ip_indirect > DX_MAX_TREE_HEIGHT - 1 ||
2154 p->ip_frame != p->ip_frames + p->ip_indirect ||
2155 !iam_leaf_invariant(&p->ip_leaf))
2157 for (i = 0; i < ARRAY_SIZE(p->ip_frames); ++i) {
2158 if (i <= p->ip_indirect) {
2159 if (!iam_frame_invariant(&p->ip_frames[i]))
2166 int iam_it_invariant(struct iam_iterator *it)
2169 (it->ii_state == IAM_IT_DETACHED ||
2170 it->ii_state == IAM_IT_ATTACHED ||
2171 it->ii_state == IAM_IT_SKEWED) &&
2172 !(it->ii_flags & ~(IAM_IT_MOVE | IAM_IT_WRITE)) &&
2173 ergo(it->ii_state == IAM_IT_ATTACHED ||
2174 it->ii_state == IAM_IT_SKEWED,
2175 iam_path_invariant(&it->ii_path) &&
2176 equi(it_at_rec(it), it->ii_state == IAM_IT_SKEWED));
2180 * Search container @c for record with key @k. If record is found, its data
2181 * are moved into @r.
2183 * Return values: 0: found, -ENOENT: not-found, -ve: error
2185 int iam_lookup(struct iam_container *c, const struct iam_key *k,
2186 struct iam_rec *r, struct iam_path_descr *pd)
2188 struct iam_iterator it;
2191 iam_it_init(&it, c, 0, pd);
2193 result = iam_it_get_exact(&it, k);
2196 * record with required key found, copy it into user buffer
2198 iam_reccpy(&it.ii_path.ip_leaf, r);
2203 EXPORT_SYMBOL(iam_lookup);
2206 * Insert new record @r with key @k into container @c (within context of
2209 * Return values: 0: success, -ve: error, including -EEXIST when record with
2210 * given key is already present.
2212 * postcondition: ergo(result == 0 || result == -EEXIST,
2213 * iam_lookup(c, k, r2) > 0;
2215 int iam_insert(handle_t *h, struct iam_container *c, const struct iam_key *k,
2216 const struct iam_rec *r, struct iam_path_descr *pd)
2218 struct iam_iterator it;
2221 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2223 result = iam_it_get_exact(&it, k);
2224 if (result == -ENOENT)
2225 result = iam_it_rec_insert(h, &it, k, r);
2226 else if (result == 0)
2232 EXPORT_SYMBOL(iam_insert);
2235 * Update record with the key @k in container @c (within context of
2236 * transaction @h), new record is given by @r.
2238 * Return values: 0: success, -ve: error, including -ENOENT if no record with
2239 * the given key found.
2241 int iam_update(handle_t *h, struct iam_container *c, const struct iam_key *k,
2242 const struct iam_rec *r, struct iam_path_descr *pd)
2244 struct iam_iterator it;
2247 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2249 result = iam_it_get_exact(&it, k);
2251 iam_it_rec_set(h, &it, r);
2256 EXPORT_SYMBOL(iam_update);
2259 * Delete existing record with key @k.
2261 * Return values: 0: success, -ENOENT: not-found, -ve: other error.
2263 * postcondition: ergo(result == 0 || result == -ENOENT,
2264 * !iam_lookup(c, k, *));
2266 int iam_delete(handle_t *h, struct iam_container *c, const struct iam_key *k,
2267 struct iam_path_descr *pd)
2269 struct iam_iterator it;
2272 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2274 result = iam_it_get_exact(&it, k);
2276 iam_it_rec_delete(h, &it);
2281 EXPORT_SYMBOL(iam_delete);
2283 int iam_root_limit(int rootgap, int blocksize, int size)
2288 limit = (blocksize - rootgap) / size;
2289 nlimit = blocksize / size;
2290 if (limit == nlimit)