4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see [sun.com URL with a
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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)
193 brelse(c->ic_root_bh);
194 c->ic_root_bh = NULL;
196 EXPORT_SYMBOL(iam_container_fini);
198 void iam_path_init(struct iam_path *path, struct iam_container *c,
199 struct iam_path_descr *pd)
201 memset(path, 0, sizeof *path);
202 path->ip_container = c;
203 path->ip_frame = path->ip_frames;
205 path->ip_leaf.il_path = path;
208 static void iam_leaf_fini(struct iam_leaf *leaf);
210 void iam_path_release(struct iam_path *path)
214 for (i = 0; i < ARRAY_SIZE(path->ip_frames); i++) {
215 if (path->ip_frames[i].bh != NULL) {
216 brelse(path->ip_frames[i].bh);
217 path->ip_frames[i].bh = NULL;
222 void iam_path_fini(struct iam_path *path)
224 iam_leaf_fini(&path->ip_leaf);
225 iam_path_release(path);
229 void iam_path_compat_init(struct iam_path_compat *path, struct inode *inode)
233 path->ipc_hinfo = &path->ipc_hinfo_area;
234 for (i = 0; i < ARRAY_SIZE(path->ipc_scratch); ++i)
235 path->ipc_descr.ipd_key_scratch[i] =
236 (struct iam_ikey *)&path->ipc_scratch[i];
238 iam_path_init(&path->ipc_path, &path->ipc_container, &path->ipc_descr);
241 void iam_path_compat_fini(struct iam_path_compat *path)
243 iam_path_fini(&path->ipc_path);
247 * Helper function initializing iam_path_descr and its key scratch area.
249 struct iam_path_descr *iam_ipd_alloc(void *area, int keysize)
251 struct iam_path_descr *ipd;
257 for (i = 0; i < ARRAY_SIZE(ipd->ipd_key_scratch); ++i, karea += keysize)
258 ipd->ipd_key_scratch[i] = karea;
261 EXPORT_SYMBOL(iam_ipd_alloc);
263 void iam_ipd_free(struct iam_path_descr *ipd)
266 EXPORT_SYMBOL(iam_ipd_free);
268 int iam_node_read(struct iam_container *c, iam_ptr_t ptr,
269 handle_t *h, struct buffer_head **bh)
273 /* NB: it can be called by iam_lfix_guess() which is still at
274 * very early stage, c->ic_root_bh and c->ic_descr->id_ops
275 * haven't been intialized yet.
276 * Also, we don't have this for IAM dir.
278 if (c->ic_root_bh != NULL &&
279 c->ic_descr->id_ops->id_root_ptr(c) == ptr) {
280 get_bh(c->ic_root_bh);
285 *bh = ldiskfs_bread(h, c->ic_object, (int)ptr, 0, &result);
292 * Return pointer to current leaf record. Pointer is valid while corresponding
293 * leaf node is locked and pinned.
295 static struct iam_rec *iam_leaf_rec(const struct iam_leaf *leaf)
297 return iam_leaf_ops(leaf)->rec(leaf);
301 * Return pointer to the current leaf key. This function returns pointer to
302 * the key stored in node.
304 * Caller should assume that returned pointer is only valid while leaf node is
307 static struct iam_key *iam_leaf_key(const struct iam_leaf *leaf)
309 return iam_leaf_ops(leaf)->key(leaf);
312 static int iam_leaf_key_size(const struct iam_leaf *leaf)
314 return iam_leaf_ops(leaf)->key_size(leaf);
317 static struct iam_ikey *iam_leaf_ikey(const struct iam_leaf *leaf,
318 struct iam_ikey *key)
320 return iam_leaf_ops(leaf)->ikey(leaf, key);
323 static int iam_leaf_keycmp(const struct iam_leaf *leaf,
324 const struct iam_key *key)
326 return iam_leaf_ops(leaf)->key_cmp(leaf, key);
329 static int iam_leaf_keyeq(const struct iam_leaf *leaf,
330 const struct iam_key *key)
332 return iam_leaf_ops(leaf)->key_eq(leaf, key);
335 #if LDISKFS_INVARIANT_ON
336 static int iam_leaf_check(struct iam_leaf *leaf);
337 extern int dx_node_check(struct iam_path *p, struct iam_frame *f);
339 static int iam_path_check(struct iam_path *p)
344 struct iam_descr *param;
347 param = iam_path_descr(p);
348 for (i = 0; result && i < ARRAY_SIZE(p->ip_frames); ++i) {
349 f = &p->ip_frames[i];
351 result = dx_node_check(p, f);
353 result = !param->id_ops->id_node_check(p, f);
356 if (result && p->ip_leaf.il_bh != NULL)
357 result = iam_leaf_check(&p->ip_leaf);
359 ldiskfs_std_error(iam_path_obj(p)->i_sb, result);
365 static int iam_leaf_load(struct iam_path *path)
369 struct iam_container *c;
370 struct buffer_head *bh;
371 struct iam_leaf *leaf;
372 struct iam_descr *descr;
374 c = path->ip_container;
375 leaf = &path->ip_leaf;
376 descr = iam_path_descr(path);
377 block = path->ip_frame->leaf;
380 printk(CFS_KERN_EMERG "wrong leaf: %lu %d [%p %p %p]\n",
381 (long unsigned)path->ip_frame->leaf,
382 dx_get_count(dx_node_get_entries(path, path->ip_frame)),
383 path->ip_frames[0].bh, path->ip_frames[1].bh,
384 path->ip_frames[2].bh);
386 err = descr->id_ops->id_node_read(c, block, NULL, &bh);
389 leaf->il_curidx = block;
390 err = iam_leaf_ops(leaf)->init(leaf);
391 assert_inv(ergo(err == 0, iam_leaf_check(leaf)));
396 static void iam_unlock_htree(struct inode *dir, struct dynlock_handle *lh)
399 dynlock_unlock(&LDISKFS_I(dir)->i_htree_lock, lh);
403 static void iam_leaf_unlock(struct iam_leaf *leaf)
405 if (leaf->il_lock != NULL) {
406 iam_unlock_htree(iam_leaf_container(leaf)->ic_object,
409 leaf->il_lock = NULL;
413 static void iam_leaf_fini(struct iam_leaf *leaf)
415 if (leaf->il_path != NULL) {
416 iam_leaf_unlock(leaf);
417 assert_inv(ergo(leaf->il_bh != NULL, iam_leaf_check(leaf)));
418 iam_leaf_ops(leaf)->fini(leaf);
427 static void iam_leaf_start(struct iam_leaf *folio)
429 iam_leaf_ops(folio)->start(folio);
432 void iam_leaf_next(struct iam_leaf *folio)
434 iam_leaf_ops(folio)->next(folio);
437 static void iam_leaf_rec_add(struct iam_leaf *leaf, const struct iam_key *key,
438 const struct iam_rec *rec)
440 iam_leaf_ops(leaf)->rec_add(leaf, key, rec);
443 static void iam_rec_del(struct iam_leaf *leaf, int shift)
445 iam_leaf_ops(leaf)->rec_del(leaf, shift);
448 int iam_leaf_at_end(const struct iam_leaf *leaf)
450 return iam_leaf_ops(leaf)->at_end(leaf);
453 void iam_leaf_split(struct iam_leaf *l, struct buffer_head **bh, iam_ptr_t nr)
455 iam_leaf_ops(l)->split(l, bh, nr);
458 int iam_leaf_can_add(const struct iam_leaf *l,
459 const struct iam_key *k, const struct iam_rec *r)
461 return iam_leaf_ops(l)->can_add(l, k, r);
464 #if LDISKFS_INVARIANT_ON
465 static int iam_leaf_check(struct iam_leaf *leaf)
469 struct iam_lentry *orig;
470 struct iam_path *path;
471 struct iam_container *bag;
478 path = iam_leaf_path(leaf);
479 bag = iam_leaf_container(leaf);
481 result = iam_leaf_ops(leaf)->init(leaf);
486 iam_leaf_start(leaf);
487 k0 = iam_path_ikey(path, 0);
488 k1 = iam_path_ikey(path, 1);
489 while (!iam_leaf_at_end(leaf)) {
490 iam_ikeycpy(bag, k0, k1);
491 iam_ikeycpy(bag, k1, iam_leaf_ikey(leaf, k1));
492 if (!first && iam_ikeycmp(bag, k0, k1) > 0) {
504 static int iam_txn_dirty(handle_t *handle,
505 struct iam_path *path, struct buffer_head *bh)
509 result = ldiskfs_journal_dirty_metadata(handle, bh);
511 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
515 static int iam_txn_add(handle_t *handle,
516 struct iam_path *path, struct buffer_head *bh)
520 result = ldiskfs_journal_get_write_access(handle, bh);
522 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
526 /***********************************************************************/
527 /* iterator interface */
528 /***********************************************************************/
530 static enum iam_it_state it_state(const struct iam_iterator *it)
536 * Helper function returning scratch key.
538 static struct iam_container *iam_it_container(const struct iam_iterator *it)
540 return it->ii_path.ip_container;
543 static inline int it_keycmp(const struct iam_iterator *it,
544 const struct iam_key *k)
546 return iam_leaf_keycmp(&it->ii_path.ip_leaf, k);
549 static inline int it_keyeq(const struct iam_iterator *it,
550 const struct iam_key *k)
552 return iam_leaf_keyeq(&it->ii_path.ip_leaf, k);
555 static int it_ikeycmp(const struct iam_iterator *it, const struct iam_ikey *ik)
557 return iam_ikeycmp(it->ii_path.ip_container,
558 iam_leaf_ikey(&it->ii_path.ip_leaf,
559 iam_path_ikey(&it->ii_path, 0)), ik);
562 static inline int it_at_rec(const struct iam_iterator *it)
564 return !iam_leaf_at_end(&it->ii_path.ip_leaf);
567 static inline int it_before(const struct iam_iterator *it)
569 return it_state(it) == IAM_IT_SKEWED && it_at_rec(it);
573 * Helper wrapper around iam_it_get(): returns 0 (success) only when record
574 * with exactly the same key as asked is found.
576 static int iam_it_get_exact(struct iam_iterator *it, const struct iam_key *k)
580 result = iam_it_get(it, k);
583 else if (result == 0)
585 * Return -ENOENT if cursor is located above record with a key
586 * different from one specified, or in the empty leaf.
588 * XXX returning -ENOENT only works if iam_it_get() never
589 * returns -ENOENT as a legitimate error.
595 void iam_container_write_lock(struct iam_container *ic)
597 cfs_down_write(&ic->ic_sem);
600 void iam_container_write_unlock(struct iam_container *ic)
602 cfs_up_write(&ic->ic_sem);
605 void iam_container_read_lock(struct iam_container *ic)
607 cfs_down_read(&ic->ic_sem);
610 void iam_container_read_unlock(struct iam_container *ic)
612 cfs_up_read(&ic->ic_sem);
616 * Initialize iterator to IAM_IT_DETACHED state.
618 * postcondition: it_state(it) == IAM_IT_DETACHED
620 int iam_it_init(struct iam_iterator *it, struct iam_container *c, __u32 flags,
621 struct iam_path_descr *pd)
623 memset(it, 0, sizeof *it);
624 it->ii_flags = flags;
625 it->ii_state = IAM_IT_DETACHED;
626 iam_path_init(&it->ii_path, c, pd);
629 EXPORT_SYMBOL(iam_it_init);
632 * Finalize iterator and release all resources.
634 * precondition: it_state(it) == IAM_IT_DETACHED
636 void iam_it_fini(struct iam_iterator *it)
638 assert_corr(it_state(it) == IAM_IT_DETACHED);
639 iam_path_fini(&it->ii_path);
641 EXPORT_SYMBOL(iam_it_fini);
644 * this locking primitives are used to protect parts
645 * of dir's htree. protection unit is block: leaf or index
647 struct dynlock_handle *iam_lock_htree(struct inode *dir, unsigned long value,
648 enum dynlock_type lt)
650 return dynlock_lock(&LDISKFS_I(dir)->i_htree_lock, value, lt, GFP_NOFS);
655 int iam_index_lock(struct iam_path *path, struct dynlock_handle **lh)
659 for (f = path->ip_frame; f >= path->ip_frames; --f, ++lh) {
661 *lh = iam_lock_htree(iam_path_obj(path), f->curidx, DLT_READ);
669 * Fast check for frame consistency.
671 static int iam_check_fast(struct iam_path *path, struct iam_frame *frame)
673 struct iam_container *bag;
674 struct iam_entry *next;
675 struct iam_entry *last;
676 struct iam_entry *entries;
677 struct iam_entry *at;
679 bag = path->ip_container;
681 entries = frame->entries;
682 last = iam_entry_shift(path, entries, dx_get_count(entries) - 1);
684 if (unlikely(at > last))
687 if (unlikely(dx_get_block(path, at) != frame->leaf))
690 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, at),
691 path->ip_ikey_target) > 0))
694 next = iam_entry_shift(path, at, +1);
696 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, next),
697 path->ip_ikey_target) <= 0))
703 int dx_index_is_compat(struct iam_path *path)
705 return iam_path_descr(path) == NULL;
711 * search position of specified hash in index
715 struct iam_entry *iam_find_position(struct iam_path *path,
716 struct iam_frame *frame)
723 count = dx_get_count(frame->entries);
724 assert_corr(count && count <= dx_get_limit(frame->entries));
725 p = iam_entry_shift(path, frame->entries,
726 dx_index_is_compat(path) ? 1 : 2);
727 q = iam_entry_shift(path, frame->entries, count - 1);
729 m = iam_entry_shift(path, p, iam_entry_diff(path, q, p) / 2);
730 if (iam_ikeycmp(path->ip_container, iam_ikey_at(path, m),
731 path->ip_ikey_target) > 0)
732 q = iam_entry_shift(path, m, -1);
734 p = iam_entry_shift(path, m, +1);
736 return iam_entry_shift(path, p, -1);
741 static iam_ptr_t iam_find_ptr(struct iam_path *path, struct iam_frame *frame)
743 return dx_get_block(path, iam_find_position(path, frame));
746 void iam_insert_key(struct iam_path *path, struct iam_frame *frame,
747 const struct iam_ikey *key, iam_ptr_t ptr)
749 struct iam_entry *entries = frame->entries;
750 struct iam_entry *new = iam_entry_shift(path, frame->at, +1);
751 int count = dx_get_count(entries);
754 * Unfortunately we cannot assert this, as this function is sometimes
755 * called by VFS under i_sem and without pdirops lock.
757 assert_corr(1 || iam_frame_is_locked(path, frame));
758 assert_corr(count < dx_get_limit(entries));
759 assert_corr(frame->at < iam_entry_shift(path, entries, count));
760 assert_inv(dx_node_check(path, frame));
762 memmove(iam_entry_shift(path, new, 1), new,
763 (char *)iam_entry_shift(path, entries, count) - (char *)new);
764 dx_set_ikey(path, new, key);
765 dx_set_block(path, new, ptr);
766 dx_set_count(entries, count + 1);
767 assert_inv(dx_node_check(path, frame));
770 void iam_insert_key_lock(struct iam_path *path, struct iam_frame *frame,
771 const struct iam_ikey *key, iam_ptr_t ptr)
773 iam_lock_bh(frame->bh);
774 iam_insert_key(path, frame, key, ptr);
775 iam_unlock_bh(frame->bh);
778 * returns 0 if path was unchanged, -EAGAIN otherwise.
780 static int iam_check_path(struct iam_path *path, struct iam_frame *frame)
784 iam_lock_bh(frame->bh);
785 equal = iam_check_fast(path, frame) == 0 ||
786 frame->leaf == iam_find_ptr(path, frame);
787 DX_DEVAL(iam_lock_stats.dls_bh_again += !equal);
788 iam_unlock_bh(frame->bh);
790 return equal ? 0 : -EAGAIN;
793 static int iam_lookup_try(struct iam_path *path)
799 struct iam_descr *param;
800 struct iam_frame *frame;
801 struct iam_container *c;
803 param = iam_path_descr(path);
804 c = path->ip_container;
806 ptr = param->id_ops->id_root_ptr(c);
807 for (frame = path->ip_frames, i = 0; i <= path->ip_indirect;
809 err = param->id_ops->id_node_read(c, (iam_ptr_t)ptr, NULL,
813 iam_lock_bh(frame->bh);
815 * node must be initialized under bh lock because concurrent
816 * creation procedure may change it and iam_lookup_try() will
817 * see obsolete tree height. -bzzz
822 if (LDISKFS_INVARIANT_ON) {
823 err = param->id_ops->id_node_check(path, frame);
828 err = param->id_ops->id_node_load(path, frame);
832 assert_inv(dx_node_check(path, frame));
834 * splitting may change root index block and move hash we're
835 * looking for into another index block so, we have to check
836 * this situation and repeat from begining if path got changed
840 err = iam_check_path(path, frame - 1);
845 frame->at = iam_find_position(path, frame);
847 frame->leaf = ptr = dx_get_block(path, frame->at);
849 iam_unlock_bh(frame->bh);
853 iam_unlock_bh(frame->bh);
854 path->ip_frame = --frame;
858 static int __iam_path_lookup(struct iam_path *path)
863 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++ i)
864 assert(path->ip_frames[i].bh == NULL);
867 err = iam_lookup_try(path);
871 } while (err == -EAGAIN);
877 * returns 0 if path was unchanged, -EAGAIN otherwise.
879 static int iam_check_full_path(struct iam_path *path, int search)
881 struct iam_frame *bottom;
882 struct iam_frame *scan;
888 for (bottom = path->ip_frames, i = 0;
889 i < DX_MAX_TREE_HEIGHT && bottom->bh != NULL; ++bottom, ++i) {
890 ; /* find last filled in frame */
894 * Lock frames, bottom to top.
896 for (scan = bottom - 1; scan >= path->ip_frames; --scan)
897 iam_lock_bh(scan->bh);
899 * Check them top to bottom.
902 for (scan = path->ip_frames; scan < bottom; ++scan) {
903 struct iam_entry *pos;
906 if (iam_check_fast(path, scan) == 0)
909 pos = iam_find_position(path, scan);
910 if (scan->leaf != dx_get_block(path, pos)) {
916 pos = iam_entry_shift(path, scan->entries,
917 dx_get_count(scan->entries) - 1);
918 if (scan->at > pos ||
919 scan->leaf != dx_get_block(path, scan->at)) {
927 * Unlock top to bottom.
929 for (scan = path->ip_frames; scan < bottom; ++scan)
930 iam_unlock_bh(scan->bh);
931 DX_DEVAL(iam_lock_stats.dls_bh_full_again += !!result);
939 * Performs path lookup and returns with found leaf (if any) locked by htree
942 int iam_lookup_lock(struct iam_path *path,
943 struct dynlock_handle **dl, enum dynlock_type lt)
948 dir = iam_path_obj(path);
949 while ((result = __iam_path_lookup(path)) == 0) {
951 *dl = iam_lock_htree(dir, path->ip_frame->leaf, lt);
959 * while locking leaf we just found may get split so we need
960 * to check this -bzzz
962 if (iam_check_full_path(path, 1) == 0)
964 iam_unlock_htree(dir, *dl);
971 * Performs tree top-to-bottom traversal starting from root, and loads leaf
974 static int iam_path_lookup(struct iam_path *path, int index)
976 struct iam_container *c;
977 struct iam_descr *descr;
978 struct iam_leaf *leaf;
981 c = path->ip_container;
982 leaf = &path->ip_leaf;
983 descr = iam_path_descr(path);
984 result = iam_lookup_lock(path, &leaf->il_lock, DLT_WRITE);
985 assert_inv(iam_path_check(path));
988 result = iam_leaf_load(path);
989 assert_inv(ergo(result == 0, iam_leaf_check(leaf)));
993 result = iam_leaf_ops(leaf)->
994 ilookup(leaf, path->ip_ikey_target);
996 result = iam_leaf_ops(leaf)->
997 lookup(leaf, path->ip_key_target);
1001 iam_leaf_unlock(leaf);
1007 * Common part of iam_it_{i,}get().
1009 static int __iam_it_get(struct iam_iterator *it, int index)
1012 assert_corr(it_state(it) == IAM_IT_DETACHED);
1014 result = iam_path_lookup(&it->ii_path, index);
1018 collision = result & IAM_LOOKUP_LAST;
1019 switch (result & ~IAM_LOOKUP_LAST) {
1020 case IAM_LOOKUP_EXACT:
1022 it->ii_state = IAM_IT_ATTACHED;
1026 it->ii_state = IAM_IT_ATTACHED;
1028 case IAM_LOOKUP_BEFORE:
1029 case IAM_LOOKUP_EMPTY:
1031 it->ii_state = IAM_IT_SKEWED;
1036 result |= collision;
1039 * See iam_it_get_exact() for explanation.
1041 assert_corr(result != -ENOENT);
1046 * Correct hash, but not the same key was found, iterate through hash
1047 * collision chain, looking for correct record.
1049 static int iam_it_collision(struct iam_iterator *it)
1053 assert(ergo(it_at_rec(it), !it_keyeq(it, it->ii_path.ip_key_target)));
1055 while ((result = iam_it_next(it)) == 0) {
1056 do_corr(schedule());
1057 if (it_ikeycmp(it, it->ii_path.ip_ikey_target) != 0)
1059 if (it_keyeq(it, it->ii_path.ip_key_target))
1066 * Attach iterator. After successful completion, @it points to record with
1067 * least key not larger than @k.
1069 * Return value: 0: positioned on existing record,
1070 * +ve: exact position found,
1073 * precondition: it_state(it) == IAM_IT_DETACHED
1074 * postcondition: ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1075 * it_keycmp(it, k) <= 0)
1077 int iam_it_get(struct iam_iterator *it, const struct iam_key *k)
1080 assert_corr(it_state(it) == IAM_IT_DETACHED);
1082 it->ii_path.ip_ikey_target = NULL;
1083 it->ii_path.ip_key_target = k;
1085 result = __iam_it_get(it, 0);
1087 if (result == IAM_LOOKUP_LAST) {
1088 result = iam_it_collision(it);
1092 result = __iam_it_get(it, 0);
1097 result &= ~IAM_LOOKUP_LAST;
1099 assert_corr(ergo(result > 0, it_keycmp(it, k) == 0));
1100 assert_corr(ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1101 it_keycmp(it, k) <= 0));
1104 EXPORT_SYMBOL(iam_it_get);
1107 * Attach iterator by index key.
1109 static int iam_it_iget(struct iam_iterator *it, const struct iam_ikey *k)
1111 assert_corr(it_state(it) == IAM_IT_DETACHED);
1113 it->ii_path.ip_ikey_target = k;
1114 return __iam_it_get(it, 1) & ~IAM_LOOKUP_LAST;
1118 * Attach iterator, and assure it points to the record (not skewed).
1120 * Return value: 0: positioned on existing record,
1121 * +ve: exact position found,
1124 * precondition: it_state(it) == IAM_IT_DETACHED &&
1125 * !(it->ii_flags&IAM_IT_WRITE)
1126 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED)
1128 int iam_it_get_at(struct iam_iterator *it, const struct iam_key *k)
1131 assert_corr(it_state(it) == IAM_IT_DETACHED &&
1132 !(it->ii_flags&IAM_IT_WRITE));
1133 result = iam_it_get(it, k);
1135 if (it_state(it) != IAM_IT_ATTACHED) {
1136 assert_corr(it_state(it) == IAM_IT_SKEWED);
1137 result = iam_it_next(it);
1140 assert_corr(ergo(result >= 0, it_state(it) == IAM_IT_ATTACHED));
1143 EXPORT_SYMBOL(iam_it_get_at);
1146 * Duplicates iterator.
1148 * postcondition: it_state(dst) == it_state(src) &&
1149 * iam_it_container(dst) == iam_it_container(src) &&
1150 * dst->ii_flags = src->ii_flags &&
1151 * ergo(it_state(src) == IAM_IT_ATTACHED,
1152 * iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1153 * iam_it_key_get(dst) == iam_it_key_get(src))
1155 void iam_it_dup(struct iam_iterator *dst, const struct iam_iterator *src)
1157 dst->ii_flags = src->ii_flags;
1158 dst->ii_state = src->ii_state;
1159 /* XXX not yet. iam_path_dup(&dst->ii_path, &src->ii_path); */
1161 * XXX: duplicate lock.
1163 assert_corr(it_state(dst) == it_state(src));
1164 assert_corr(iam_it_container(dst) == iam_it_container(src));
1165 assert_corr(dst->ii_flags = src->ii_flags);
1166 assert_corr(ergo(it_state(src) == IAM_IT_ATTACHED,
1167 iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1168 iam_it_key_get(dst) == iam_it_key_get(src)));
1173 * Detach iterator. Does nothing it detached state.
1175 * postcondition: it_state(it) == IAM_IT_DETACHED
1177 void iam_it_put(struct iam_iterator *it)
1179 if (it->ii_state != IAM_IT_DETACHED) {
1180 it->ii_state = IAM_IT_DETACHED;
1181 iam_leaf_fini(&it->ii_path.ip_leaf);
1184 EXPORT_SYMBOL(iam_it_put);
1186 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1187 struct iam_ikey *ikey);
1191 * This function increments the frame pointer to search the next leaf
1192 * block, and reads in the necessary intervening nodes if the search
1193 * should be necessary. Whether or not the search is necessary is
1194 * controlled by the hash parameter. If the hash value is even, then
1195 * the search is only continued if the next block starts with that
1196 * hash value. This is used if we are searching for a specific file.
1198 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1200 * This function returns 1 if the caller should continue to search,
1201 * or 0 if it should not. If there is an error reading one of the
1202 * index blocks, it will a negative error code.
1204 * If start_hash is non-null, it will be filled in with the starting
1205 * hash of the next page.
1207 static int iam_htree_advance(struct inode *dir, __u32 hash,
1208 struct iam_path *path, __u32 *start_hash,
1211 struct iam_frame *p;
1212 struct buffer_head *bh;
1213 int err, num_frames = 0;
1218 * Find the next leaf page by incrementing the frame pointer.
1219 * If we run out of entries in the interior node, loop around and
1220 * increment pointer in the parent node. When we break out of
1221 * this loop, num_frames indicates the number of interior
1222 * nodes need to be read.
1225 do_corr(schedule());
1227 p->at = iam_entry_shift(path, p->at, +1);
1228 if (p->at < iam_entry_shift(path, p->entries,
1229 dx_get_count(p->entries))) {
1230 p->leaf = dx_get_block(path, p->at);
1231 iam_unlock_bh(p->bh);
1234 iam_unlock_bh(p->bh);
1235 if (p == path->ip_frames)
1246 * If the hash is 1, then continue only if the next page has a
1247 * continuation hash of any value. This is used for readdir
1248 * handling. Otherwise, check to see if the hash matches the
1249 * desired contiuation hash. If it doesn't, return since
1250 * there's no point to read in the successive index pages.
1252 dx_get_ikey(path, p->at, (struct iam_ikey *)&bhash);
1254 *start_hash = bhash;
1255 if ((hash & 1) == 0) {
1256 if ((bhash & ~1) != hash)
1261 * If the hash is HASH_NB_ALWAYS, we always go to the next
1262 * block so no check is necessary
1264 while (num_frames--) {
1267 do_corr(schedule());
1269 idx = p->leaf = dx_get_block(path, p->at);
1270 iam_unlock_bh(p->bh);
1271 err = iam_path_descr(path)->id_ops->
1272 id_node_read(path->ip_container, idx, NULL, &bh);
1274 return err; /* Failure */
1277 assert_corr(p->bh != bh);
1279 p->entries = dx_node_get_entries(path, p);
1280 p->at = iam_entry_shift(path, p->entries, !compat);
1281 assert_corr(p->curidx != idx);
1284 assert_corr(p->leaf != dx_get_block(path, p->at));
1285 p->leaf = dx_get_block(path, p->at);
1286 iam_unlock_bh(p->bh);
1287 assert_inv(dx_node_check(path, p));
1293 static inline int iam_index_advance(struct iam_path *path)
1295 return iam_htree_advance(iam_path_obj(path), 0, path, NULL, 0);
1298 static void iam_unlock_array(struct inode *dir, struct dynlock_handle **lh)
1302 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++i, ++lh) {
1304 iam_unlock_htree(dir, *lh);
1310 * Advance index part of @path to point to the next leaf. Returns 1 on
1311 * success, 0, when end of container was reached. Leaf node is locked.
1313 int iam_index_next(struct iam_container *c, struct iam_path *path)
1316 struct dynlock_handle *lh[DX_MAX_TREE_HEIGHT] = { 0, };
1318 struct inode *object;
1321 * Locking for iam_index_next()... is to be described.
1324 object = c->ic_object;
1325 cursor = path->ip_frame->leaf;
1328 result = iam_index_lock(path, lh);
1329 do_corr(schedule());
1333 result = iam_check_full_path(path, 0);
1334 if (result == 0 && cursor == path->ip_frame->leaf) {
1335 result = iam_index_advance(path);
1337 assert_corr(result == 0 ||
1338 cursor != path->ip_frame->leaf);
1342 iam_unlock_array(object, lh);
1344 iam_path_release(path);
1345 do_corr(schedule());
1347 result = __iam_path_lookup(path);
1351 while (path->ip_frame->leaf != cursor) {
1352 do_corr(schedule());
1354 result = iam_index_lock(path, lh);
1355 do_corr(schedule());
1359 result = iam_check_full_path(path, 0);
1363 result = iam_index_advance(path);
1365 CERROR("cannot find cursor : %u\n",
1371 result = iam_check_full_path(path, 0);
1374 iam_unlock_array(object, lh);
1376 } while (result == -EAGAIN);
1380 iam_unlock_array(object, lh);
1385 * Move iterator one record right.
1387 * Return value: 0: success,
1388 * +1: end of container reached
1391 * precondition: (it_state(it) == IAM_IT_ATTACHED ||
1392 * it_state(it) == IAM_IT_SKEWED) && it->ii_flags&IAM_IT_MOVE
1393 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED) &&
1394 * ergo(result > 0, it_state(it) == IAM_IT_DETACHED)
1396 int iam_it_next(struct iam_iterator *it)
1399 struct iam_path *path;
1400 struct iam_leaf *leaf;
1402 do_corr(struct iam_ikey *ik_orig);
1404 /* assert_corr(it->ii_flags&IAM_IT_MOVE); */
1405 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1406 it_state(it) == IAM_IT_SKEWED);
1408 path = &it->ii_path;
1409 leaf = &path->ip_leaf;
1410 obj = iam_path_obj(path);
1412 assert_corr(iam_leaf_is_locked(leaf));
1415 do_corr(ik_orig = it_at_rec(it) ?
1416 iam_it_ikey_get(it, iam_path_ikey(path, 2)) : NULL);
1417 if (it_before(it)) {
1418 assert_corr(!iam_leaf_at_end(leaf));
1419 it->ii_state = IAM_IT_ATTACHED;
1421 if (!iam_leaf_at_end(leaf))
1422 /* advance within leaf node */
1423 iam_leaf_next(leaf);
1425 * multiple iterations may be necessary due to empty leaves.
1427 while (result == 0 && iam_leaf_at_end(leaf)) {
1428 do_corr(schedule());
1429 /* advance index portion of the path */
1430 result = iam_index_next(iam_it_container(it), path);
1431 assert_corr(iam_leaf_is_locked(leaf));
1433 struct dynlock_handle *lh;
1434 lh = iam_lock_htree(obj, path->ip_frame->leaf,
1437 iam_leaf_fini(leaf);
1439 result = iam_leaf_load(path);
1441 iam_leaf_start(leaf);
1444 } else if (result == 0)
1445 /* end of container reached */
1451 it->ii_state = IAM_IT_ATTACHED;
1453 assert_corr(ergo(result == 0, it_state(it) == IAM_IT_ATTACHED));
1454 assert_corr(ergo(result > 0, it_state(it) == IAM_IT_DETACHED));
1455 assert_corr(ergo(result == 0 && ik_orig != NULL,
1456 it_ikeycmp(it, ik_orig) >= 0));
1459 EXPORT_SYMBOL(iam_it_next);
1462 * Return pointer to the record under iterator.
1464 * precondition: it_state(it) == IAM_IT_ATTACHED && it_at_rec(it)
1465 * postcondition: it_state(it) == IAM_IT_ATTACHED
1467 struct iam_rec *iam_it_rec_get(const struct iam_iterator *it)
1469 assert_corr(it_state(it) == IAM_IT_ATTACHED);
1470 assert_corr(it_at_rec(it));
1471 return iam_leaf_rec(&it->ii_path.ip_leaf);
1473 EXPORT_SYMBOL(iam_it_rec_get);
1475 static void iam_it_reccpy(struct iam_iterator *it, const struct iam_rec *r)
1477 struct iam_leaf *folio;
1479 folio = &it->ii_path.ip_leaf;
1480 iam_leaf_ops(folio)->rec_set(folio, r);
1484 * Replace contents of record under iterator.
1486 * precondition: it_state(it) == IAM_IT_ATTACHED &&
1487 * it->ii_flags&IAM_IT_WRITE
1488 * postcondition: it_state(it) == IAM_IT_ATTACHED &&
1489 * ergo(result == 0, !memcmp(iam_it_rec_get(it), r, ...))
1491 int iam_it_rec_set(handle_t *h,
1492 struct iam_iterator *it, const struct iam_rec *r)
1495 struct iam_path *path;
1496 struct buffer_head *bh;
1498 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
1499 it->ii_flags&IAM_IT_WRITE);
1500 assert_corr(it_at_rec(it));
1502 path = &it->ii_path;
1503 bh = path->ip_leaf.il_bh;
1504 result = iam_txn_add(h, path, bh);
1506 iam_it_reccpy(it, r);
1507 result = iam_txn_dirty(h, path, bh);
1511 EXPORT_SYMBOL(iam_it_rec_set);
1514 * Return pointer to the index key under iterator.
1516 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1517 * it_state(it) == IAM_IT_SKEWED
1519 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1520 struct iam_ikey *ikey)
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_ikey(&it->ii_path.ip_leaf, ikey);
1529 * Return pointer to the key under iterator.
1531 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1532 * it_state(it) == IAM_IT_SKEWED
1534 struct iam_key *iam_it_key_get(const struct iam_iterator *it)
1536 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1537 it_state(it) == IAM_IT_SKEWED);
1538 assert_corr(it_at_rec(it));
1539 return iam_leaf_key(&it->ii_path.ip_leaf);
1541 EXPORT_SYMBOL(iam_it_key_get);
1544 * Return size of key under iterator (in bytes)
1546 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1547 * it_state(it) == IAM_IT_SKEWED
1549 int iam_it_key_size(const struct iam_iterator *it)
1551 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1552 it_state(it) == IAM_IT_SKEWED);
1553 assert_corr(it_at_rec(it));
1554 return iam_leaf_key_size(&it->ii_path.ip_leaf);
1556 EXPORT_SYMBOL(iam_it_key_size);
1559 * Insertion of new record. Interaction with jbd during non-trivial case (when
1560 * split happens) is as following:
1562 * - new leaf node is involved into transaction by ldiskfs_append();
1564 * - old leaf node is involved into transaction by iam_add_rec();
1566 * - leaf where insertion point ends in, is marked dirty by iam_add_rec();
1568 * - leaf without insertion point is marked dirty (as @new_leaf) by
1571 * - split index nodes are involved into transaction and marked dirty by
1572 * split_index_node().
1574 * - "safe" index node, which is no split, but where new pointer is inserted
1575 * is involved into transaction and marked dirty by split_index_node().
1577 * - index node where pointer to new leaf is inserted is involved into
1578 * transaction by split_index_node() and marked dirty by iam_add_rec().
1580 * - inode is marked dirty by iam_add_rec().
1584 static int iam_new_leaf(handle_t *handle, struct iam_leaf *leaf)
1588 struct buffer_head *new_leaf;
1589 struct buffer_head *old_leaf;
1590 struct iam_container *c;
1592 struct iam_path *path;
1594 assert_inv(iam_leaf_check(leaf));
1596 c = iam_leaf_container(leaf);
1597 path = leaf->il_path;
1600 new_leaf = ldiskfs_append(handle, obj, (__u32 *)&blknr, &err);
1601 do_corr(schedule());
1602 if (new_leaf != NULL) {
1603 struct dynlock_handle *lh;
1605 lh = iam_lock_htree(obj, blknr, DLT_WRITE);
1606 do_corr(schedule());
1608 iam_leaf_ops(leaf)->init_new(c, new_leaf);
1609 do_corr(schedule());
1610 old_leaf = leaf->il_bh;
1611 iam_leaf_split(leaf, &new_leaf, blknr);
1612 if (old_leaf != leaf->il_bh) {
1614 * Switched to the new leaf.
1616 iam_leaf_unlock(leaf);
1618 path->ip_frame->leaf = blknr;
1620 iam_unlock_htree(obj, lh);
1621 do_corr(schedule());
1622 err = iam_txn_dirty(handle, path, new_leaf);
1625 err = ldiskfs_mark_inode_dirty(handle, obj);
1626 do_corr(schedule());
1630 assert_inv(iam_leaf_check(leaf));
1631 assert_inv(iam_leaf_check(&iam_leaf_path(leaf)->ip_leaf));
1632 assert_inv(iam_path_check(iam_leaf_path(leaf)));
1636 static inline void dx_set_limit(struct iam_entry *entries, unsigned value)
1638 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
1641 static int iam_shift_entries(struct iam_path *path,
1642 struct iam_frame *frame, unsigned count,
1643 struct iam_entry *entries, struct iam_entry *entries2,
1650 struct iam_frame *parent = frame - 1;
1651 struct iam_ikey *pivot = iam_path_ikey(path, 3);
1653 delta = dx_index_is_compat(path) ? 0 : +1;
1655 count1 = count/2 + delta;
1656 count2 = count - count1;
1657 dx_get_ikey(path, iam_entry_shift(path, entries, count1), pivot);
1659 dxtrace(printk("Split index %d/%d\n", count1, count2));
1661 memcpy((char *) iam_entry_shift(path, entries2, delta),
1662 (char *) iam_entry_shift(path, entries, count1),
1663 count2 * iam_entry_size(path));
1665 dx_set_count(entries2, count2 + delta);
1666 dx_set_limit(entries2, dx_node_limit(path));
1669 * NOTE: very subtle piece of code competing dx_probe() may find 2nd
1670 * level index in root index, then we insert new index here and set
1671 * new count in that 2nd level index. so, dx_probe() may see 2nd level
1672 * index w/o hash it looks for. the solution is to check root index
1673 * after we locked just founded 2nd level index -bzzz
1675 iam_insert_key_lock(path, parent, pivot, newblock);
1678 * now old and new 2nd level index blocks contain all pointers, so
1679 * dx_probe() may find it in the both. it's OK -bzzz
1681 iam_lock_bh(frame->bh);
1682 dx_set_count(entries, count1);
1683 iam_unlock_bh(frame->bh);
1686 * now old 2nd level index block points to first half of leafs. it's
1687 * importand that dx_probe() must check root index block for changes
1688 * under dx_lock_bh(frame->bh) -bzzz
1695 int split_index_node(handle_t *handle, struct iam_path *path,
1696 struct dynlock_handle **lh)
1699 struct iam_entry *entries; /* old block contents */
1700 struct iam_entry *entries2; /* new block contents */
1701 struct iam_frame *frame, *safe;
1702 struct buffer_head *bh_new[DX_MAX_TREE_HEIGHT] = {0};
1703 u32 newblock[DX_MAX_TREE_HEIGHT] = {0};
1704 struct dynlock_handle *lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1705 struct dynlock_handle *new_lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1706 struct inode *dir = iam_path_obj(path);
1707 struct iam_descr *descr;
1711 descr = iam_path_descr(path);
1713 * Algorithm below depends on this.
1715 assert_corr(dx_root_limit(path) < dx_node_limit(path));
1717 frame = path->ip_frame;
1718 entries = frame->entries;
1721 * Tall-tree handling: we might have to split multiple index blocks
1722 * all the way up to tree root. Tricky point here is error handling:
1723 * to avoid complicated undo/rollback we
1725 * - first allocate all necessary blocks
1727 * - insert pointers into them atomically.
1731 * Locking: leaf is already locked. htree-locks are acquired on all
1732 * index nodes that require split bottom-to-top, on the "safe" node,
1733 * and on all new nodes
1736 dxtrace(printk("using %u of %u node entries\n",
1737 dx_get_count(entries), dx_get_limit(entries)));
1739 /* What levels need split? */
1740 for (nr_splet = 0; frame >= path->ip_frames &&
1741 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1742 --frame, ++nr_splet) {
1743 do_corr(schedule());
1744 if (nr_splet == DX_MAX_TREE_HEIGHT) {
1746 CWARN(dir->i_sb, __FUNCTION__,
1747 "Directory index full!\n");
1757 * Lock all nodes, bottom to top.
1759 for (frame = path->ip_frame, i = nr_splet; i >= 0; --i, --frame) {
1760 do_corr(schedule());
1761 lock[i] = iam_lock_htree(dir, frame->curidx, DLT_WRITE);
1762 if (lock[i] == NULL) {
1769 * Check for concurrent index modification.
1771 err = iam_check_full_path(path, 1);
1775 * And check that the same number of nodes is to be split.
1777 for (i = 0, frame = path->ip_frame; frame >= path->ip_frames &&
1778 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1782 if (i != nr_splet) {
1787 /* Go back down, allocating blocks, locking them, and adding into
1789 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1790 bh_new[i] = ldiskfs_append (handle, dir, &newblock[i], &err);
1791 do_corr(schedule());
1793 descr->id_ops->id_node_init(path->ip_container,
1796 new_lock[i] = iam_lock_htree(dir, newblock[i], DLT_WRITE);
1797 if (new_lock[i] == NULL) {
1801 do_corr(schedule());
1802 BUFFER_TRACE(frame->bh, "get_write_access");
1803 err = ldiskfs_journal_get_write_access(handle, frame->bh);
1807 /* Add "safe" node to transaction too */
1808 if (safe + 1 != path->ip_frames) {
1809 do_corr(schedule());
1810 err = ldiskfs_journal_get_write_access(handle, safe->bh);
1815 /* Go through nodes once more, inserting pointers */
1816 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1819 struct buffer_head *bh2;
1820 struct buffer_head *bh;
1822 entries = frame->entries;
1823 count = dx_get_count(entries);
1824 idx = iam_entry_diff(path, frame->at, entries);
1827 entries2 = dx_get_entries(path, bh2->b_data, 0);
1830 if (frame == path->ip_frames) {
1831 /* splitting root node. Tricky point:
1833 * In the "normal" B-tree we'd split root *and* add
1834 * new root to the tree with pointers to the old root
1835 * and its sibling (thus introducing two new nodes).
1837 * In htree it's enough to add one node, because
1838 * capacity of the root node is smaller than that of
1841 struct iam_frame *frames;
1842 struct iam_entry *next;
1844 assert_corr(i == 0);
1846 do_corr(schedule());
1848 frames = path->ip_frames;
1849 memcpy((char *) entries2, (char *) entries,
1850 count * iam_entry_size(path));
1851 dx_set_limit(entries2, dx_node_limit(path));
1854 iam_lock_bh(frame->bh);
1855 next = descr->id_ops->id_root_inc(path->ip_container,
1857 dx_set_block(path, next, newblock[0]);
1858 iam_unlock_bh(frame->bh);
1860 do_corr(schedule());
1861 /* Shift frames in the path */
1862 memmove(frames + 2, frames + 1,
1863 (sizeof path->ip_frames) - 2 * sizeof frames[0]);
1864 /* Add new access path frame */
1865 frames[1].at = iam_entry_shift(path, entries2, idx);
1866 frames[1].entries = entries = entries2;
1868 assert_inv(dx_node_check(path, frame));
1871 assert_inv(dx_node_check(path, frame));
1872 bh_new[0] = NULL; /* buffer head is "consumed" */
1873 err = ldiskfs_journal_get_write_access(handle, bh2);
1876 do_corr(schedule());
1878 /* splitting non-root index node. */
1879 struct iam_frame *parent = frame - 1;
1881 do_corr(schedule());
1882 count = iam_shift_entries(path, frame, count,
1883 entries, entries2, newblock[i]);
1884 /* Which index block gets the new entry? */
1886 int d = dx_index_is_compat(path) ? 0 : +1;
1888 frame->at = iam_entry_shift(path, entries2,
1890 frame->entries = entries = entries2;
1891 frame->curidx = newblock[i];
1892 swap(frame->bh, bh2);
1893 assert_corr(lock[i + 1] != NULL);
1894 assert_corr(new_lock[i] != NULL);
1895 swap(lock[i + 1], new_lock[i]);
1897 parent->at = iam_entry_shift(path,
1900 assert_inv(dx_node_check(path, frame));
1901 assert_inv(dx_node_check(path, parent));
1902 dxtrace(dx_show_index ("node", frame->entries));
1903 dxtrace(dx_show_index ("node",
1904 ((struct dx_node *) bh2->b_data)->entries));
1905 err = ldiskfs_journal_dirty_metadata(handle, bh2);
1908 do_corr(schedule());
1909 err = ldiskfs_journal_dirty_metadata(handle, parent->bh);
1913 do_corr(schedule());
1914 err = ldiskfs_journal_dirty_metadata(handle, bh);
1919 * This function was called to make insertion of new leaf
1920 * possible. Check that it fulfilled its obligations.
1922 assert_corr(dx_get_count(path->ip_frame->entries) <
1923 dx_get_limit(path->ip_frame->entries));
1924 assert_corr(lock[nr_splet] != NULL);
1925 *lh = lock[nr_splet];
1926 lock[nr_splet] = NULL;
1929 * Log ->i_size modification.
1931 err = ldiskfs_mark_inode_dirty(handle, dir);
1937 ldiskfs_std_error(dir->i_sb, err);
1940 iam_unlock_array(dir, lock);
1941 iam_unlock_array(dir, new_lock);
1943 assert_corr(err || iam_frame_is_locked(path, path->ip_frame));
1945 do_corr(schedule());
1946 for (i = 0; i < ARRAY_SIZE(bh_new); ++i) {
1947 if (bh_new[i] != NULL)
1953 static int iam_add_rec(handle_t *handle, struct iam_iterator *it,
1954 struct iam_path *path,
1955 const struct iam_key *k, const struct iam_rec *r)
1958 struct iam_leaf *leaf;
1960 leaf = &path->ip_leaf;
1961 assert_inv(iam_leaf_check(leaf));
1962 assert_inv(iam_path_check(path));
1963 err = iam_txn_add(handle, path, leaf->il_bh);
1965 do_corr(schedule());
1966 if (!iam_leaf_can_add(leaf, k, r)) {
1967 struct dynlock_handle *lh = NULL;
1970 assert_corr(lh == NULL);
1971 do_corr(schedule());
1972 err = split_index_node(handle, path, &lh);
1973 if (err == -EAGAIN) {
1974 assert_corr(lh == NULL);
1976 iam_path_fini(path);
1977 it->ii_state = IAM_IT_DETACHED;
1979 do_corr(schedule());
1980 err = iam_it_get_exact(it, k);
1982 err = +1; /* repeat split */
1987 assert_inv(iam_path_check(path));
1989 assert_corr(lh != NULL);
1990 do_corr(schedule());
1991 err = iam_new_leaf(handle, leaf);
1993 err = iam_txn_dirty(handle, path,
1994 path->ip_frame->bh);
1996 iam_unlock_htree(iam_path_obj(path), lh);
1997 do_corr(schedule());
2000 iam_leaf_rec_add(leaf, k, r);
2001 err = iam_txn_dirty(handle, path, leaf->il_bh);
2004 assert_inv(iam_leaf_check(leaf));
2005 assert_inv(iam_leaf_check(&path->ip_leaf));
2006 assert_inv(iam_path_check(path));
2011 * Insert new record with key @k and contents from @r, shifting records to the
2012 * right. On success, iterator is positioned on the newly inserted record.
2014 * precondition: it->ii_flags&IAM_IT_WRITE &&
2015 * (it_state(it) == IAM_IT_ATTACHED ||
2016 * it_state(it) == IAM_IT_SKEWED) &&
2017 * ergo(it_state(it) == IAM_IT_ATTACHED,
2018 * it_keycmp(it, k) <= 0) &&
2019 * ergo(it_before(it), it_keycmp(it, k) > 0));
2020 * postcondition: ergo(result == 0,
2021 * it_state(it) == IAM_IT_ATTACHED &&
2022 * it_keycmp(it, k) == 0 &&
2023 * !memcmp(iam_it_rec_get(it), r, ...))
2025 int iam_it_rec_insert(handle_t *h, struct iam_iterator *it,
2026 const struct iam_key *k, const struct iam_rec *r)
2029 struct iam_path *path;
2031 path = &it->ii_path;
2033 assert_corr(it->ii_flags&IAM_IT_WRITE);
2034 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2035 it_state(it) == IAM_IT_SKEWED);
2036 assert_corr(ergo(it_state(it) == IAM_IT_ATTACHED,
2037 it_keycmp(it, k) <= 0));
2038 assert_corr(ergo(it_before(it), it_keycmp(it, k) > 0));
2039 result = iam_add_rec(h, it, path, k, r);
2041 it->ii_state = IAM_IT_ATTACHED;
2042 assert_corr(ergo(result == 0,
2043 it_state(it) == IAM_IT_ATTACHED &&
2044 it_keycmp(it, k) == 0));
2047 EXPORT_SYMBOL(iam_it_rec_insert);
2050 * Delete record under iterator.
2052 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2053 * it->ii_flags&IAM_IT_WRITE &&
2055 * postcondition: it_state(it) == IAM_IT_ATTACHED ||
2056 * it_state(it) == IAM_IT_DETACHED
2058 int iam_it_rec_delete(handle_t *h, struct iam_iterator *it)
2061 struct iam_leaf *leaf;
2062 struct iam_path *path;
2064 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
2065 it->ii_flags&IAM_IT_WRITE);
2066 assert_corr(it_at_rec(it));
2068 path = &it->ii_path;
2069 leaf = &path->ip_leaf;
2071 assert_inv(iam_leaf_check(leaf));
2072 assert_inv(iam_path_check(path));
2074 result = iam_txn_add(h, path, leaf->il_bh);
2076 * no compaction for now.
2079 iam_rec_del(leaf, it->ii_flags&IAM_IT_MOVE);
2080 result = iam_txn_dirty(h, path, leaf->il_bh);
2081 if (result == 0 && iam_leaf_at_end(leaf) &&
2082 it->ii_flags&IAM_IT_MOVE) {
2083 result = iam_it_next(it);
2088 assert_inv(iam_leaf_check(leaf));
2089 assert_inv(iam_path_check(path));
2090 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2091 it_state(it) == IAM_IT_DETACHED);
2094 EXPORT_SYMBOL(iam_it_rec_delete);
2097 * Convert iterator to cookie.
2099 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2100 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2101 * postcondition: it_state(it) == IAM_IT_ATTACHED
2103 iam_pos_t iam_it_store(const struct iam_iterator *it)
2107 assert_corr(it_state(it) == IAM_IT_ATTACHED);
2108 assert_corr(it_at_rec(it));
2109 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <=
2113 return *(iam_pos_t *)iam_it_ikey_get(it, (void *)&result);
2115 EXPORT_SYMBOL(iam_it_store);
2118 * Restore iterator from cookie.
2120 * precondition: it_state(it) == IAM_IT_DETACHED && it->ii_flags&IAM_IT_MOVE &&
2121 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2122 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED &&
2123 * iam_it_store(it) == pos)
2125 int iam_it_load(struct iam_iterator *it, iam_pos_t pos)
2127 assert_corr(it_state(it) == IAM_IT_DETACHED &&
2128 it->ii_flags&IAM_IT_MOVE);
2129 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <= sizeof pos);
2130 return iam_it_iget(it, (struct iam_ikey *)&pos);
2132 EXPORT_SYMBOL(iam_it_load);
2134 /***********************************************************************/
2136 /***********************************************************************/
2138 static inline int ptr_inside(void *base, size_t size, void *ptr)
2140 return (base <= ptr) && (ptr < base + size);
2143 int iam_frame_invariant(struct iam_frame *f)
2147 f->bh->b_data != NULL &&
2148 ptr_inside(f->bh->b_data, f->bh->b_size, f->entries) &&
2149 ptr_inside(f->bh->b_data, f->bh->b_size, f->at) &&
2150 f->entries <= f->at);
2152 int iam_leaf_invariant(struct iam_leaf *l)
2156 l->il_bh->b_data != NULL &&
2157 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_entries) &&
2158 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_at) &&
2159 l->il_entries <= l->il_at;
2162 int iam_path_invariant(struct iam_path *p)
2166 if (p->ip_container == NULL ||
2167 p->ip_indirect < 0 || p->ip_indirect > DX_MAX_TREE_HEIGHT - 1 ||
2168 p->ip_frame != p->ip_frames + p->ip_indirect ||
2169 !iam_leaf_invariant(&p->ip_leaf))
2171 for (i = 0; i < ARRAY_SIZE(p->ip_frames); ++i) {
2172 if (i <= p->ip_indirect) {
2173 if (!iam_frame_invariant(&p->ip_frames[i]))
2180 int iam_it_invariant(struct iam_iterator *it)
2183 (it->ii_state == IAM_IT_DETACHED ||
2184 it->ii_state == IAM_IT_ATTACHED ||
2185 it->ii_state == IAM_IT_SKEWED) &&
2186 !(it->ii_flags & ~(IAM_IT_MOVE | IAM_IT_WRITE)) &&
2187 ergo(it->ii_state == IAM_IT_ATTACHED ||
2188 it->ii_state == IAM_IT_SKEWED,
2189 iam_path_invariant(&it->ii_path) &&
2190 equi(it_at_rec(it), it->ii_state == IAM_IT_SKEWED));
2194 * Search container @c for record with key @k. If record is found, its data
2195 * are moved into @r.
2197 * Return values: 0: found, -ENOENT: not-found, -ve: error
2199 int iam_lookup(struct iam_container *c, const struct iam_key *k,
2200 struct iam_rec *r, struct iam_path_descr *pd)
2202 struct iam_iterator it;
2205 iam_it_init(&it, c, 0, pd);
2207 result = iam_it_get_exact(&it, k);
2210 * record with required key found, copy it into user buffer
2212 iam_reccpy(&it.ii_path.ip_leaf, r);
2217 EXPORT_SYMBOL(iam_lookup);
2220 * Insert new record @r with key @k into container @c (within context of
2223 * Return values: 0: success, -ve: error, including -EEXIST when record with
2224 * given key is already present.
2226 * postcondition: ergo(result == 0 || result == -EEXIST,
2227 * iam_lookup(c, k, r2) > 0;
2229 int iam_insert(handle_t *h, struct iam_container *c, const struct iam_key *k,
2230 const struct iam_rec *r, struct iam_path_descr *pd)
2232 struct iam_iterator it;
2235 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2237 result = iam_it_get_exact(&it, k);
2238 if (result == -ENOENT)
2239 result = iam_it_rec_insert(h, &it, k, r);
2240 else if (result == 0)
2246 EXPORT_SYMBOL(iam_insert);
2249 * Update record with the key @k in container @c (within context of
2250 * transaction @h), new record is given by @r.
2252 * Return values: 0: success, -ve: error, including -ENOENT if no record with
2253 * the given key found.
2255 int iam_update(handle_t *h, struct iam_container *c, const struct iam_key *k,
2256 const struct iam_rec *r, struct iam_path_descr *pd)
2258 struct iam_iterator it;
2261 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2263 result = iam_it_get_exact(&it, k);
2265 iam_it_rec_set(h, &it, r);
2270 EXPORT_SYMBOL(iam_update);
2273 * Delete existing record with key @k.
2275 * Return values: 0: success, -ENOENT: not-found, -ve: other error.
2277 * postcondition: ergo(result == 0 || result == -ENOENT,
2278 * !iam_lookup(c, k, *));
2280 int iam_delete(handle_t *h, struct iam_container *c, const struct iam_key *k,
2281 struct iam_path_descr *pd)
2283 struct iam_iterator it;
2286 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2288 result = iam_it_get_exact(&it, k);
2290 iam_it_rec_delete(h, &it);
2295 EXPORT_SYMBOL(iam_delete);
2297 int iam_root_limit(int rootgap, int blocksize, int size)
2302 limit = (blocksize - rootgap) / size;
2303 nlimit = blocksize / size;
2304 if (limit == nlimit)