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, Intel Corporation.
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.
104 * The IAM root block is a special node, which contains the IAM descriptor.
105 * It is on disk format:
107 * +---------+-------+--------+---------+-------+------+-------+------------+
108 * |IAM desc | count | idle | | | | | |
109 * |(fix/var)| / | blocks | padding | entry | .... | entry | free space |
110 * | | limit | | | | | | |
111 * +---------+-------+--------+---------+-------+------+-------+------------+
113 * The padding length is calculated with the parameters in the IAM descriptor.
115 * The field "idle_blocks" is used to record empty leaf nodes, which have not
116 * been released but all contained entries in them have been removed. Usually,
117 * the idle blocks in the IAM should be reused when need to allocate new leaf
118 * nodes for new entries, it depends on the IAM hash functions to map the new
119 * entries to these idle blocks. Unfortunately, it is not easy to design some
120 * hash functions for such clever mapping, especially considering the insert/
121 * lookup performance.
123 * So the IAM recycles the empty leaf nodes, and put them into a per-file based
124 * idle blocks pool. If need some new leaf node, it will try to take idle block
125 * from such pool with priority, in spite of how the IAM hash functions to map
128 * The idle blocks pool is organized as a series of tables, and each table
129 * can be described as following (on-disk format):
131 * +---------+---------+---------+---------+------+---------+-------+
132 * | magic | count | next | logic | | logic | free |
133 * |(16 bits)|(16 bits)| table | blk # | .... | blk # | space |
134 * | | |(32 bits)|(32 bits)| |(32 bits)| |
135 * +---------+---------+---------+---------+------+---------+-------+
137 * The logic blk# for the first table is stored in the root node "idle_blocks".
141 #include <linux/module.h>
142 #include <linux/fs.h>
143 #include <linux/pagemap.h>
144 #include <linux/time.h>
145 #include <linux/fcntl.h>
146 #include <linux/stat.h>
147 #include <linux/string.h>
148 #include <linux/quotaops.h>
149 #include <linux/buffer_head.h>
150 #include "osd_internal.h"
156 * List of all registered formats.
158 * No locking. Callers synchronize.
160 static CFS_LIST_HEAD(iam_formats);
162 void iam_format_register(struct iam_format *fmt)
164 cfs_list_add(&fmt->if_linkage, &iam_formats);
166 EXPORT_SYMBOL(iam_format_register);
168 static struct buffer_head *
169 iam_load_idle_blocks(struct iam_container *c, iam_ptr_t blk)
171 struct inode *inode = c->ic_object;
172 struct iam_idle_head *head;
173 struct buffer_head *bh;
176 LASSERT(mutex_is_locked(&c->ic_idle_mutex));
181 bh = ldiskfs_bread(NULL, inode, blk, 0, &err);
183 CERROR("%.16s: cannot load idle blocks, blk = %u, err = %d\n",
184 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name, blk, err);
185 c->ic_idle_failed = 1;
189 head = (struct iam_idle_head *)(bh->b_data);
190 if (le16_to_cpu(head->iih_magic) != IAM_IDLE_HEADER_MAGIC) {
191 CERROR("%.16s: invalid idle block head, blk = %u, magic = %d\n",
192 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name, blk,
193 le16_to_cpu(head->iih_magic));
195 c->ic_idle_failed = 1;
196 return ERR_PTR(-EBADF);
203 * Determine format of given container. This is done by scanning list of
204 * registered formats and calling ->if_guess() method of each in turn.
206 static int iam_format_guess(struct iam_container *c)
209 struct iam_format *fmt;
212 * XXX temporary initialization hook.
215 static int initialized = 0;
218 iam_lvar_format_init();
219 iam_lfix_format_init();
225 cfs_list_for_each_entry(fmt, &iam_formats, if_linkage) {
226 result = fmt->if_guess(c);
232 struct buffer_head *bh;
235 LASSERT(c->ic_root_bh != NULL);
237 idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
238 c->ic_descr->id_root_gap +
239 sizeof(struct dx_countlimit));
240 mutex_lock(&c->ic_idle_mutex);
241 bh = iam_load_idle_blocks(c, le32_to_cpu(*idle_blocks));
242 if (bh != NULL && IS_ERR(bh))
243 result = PTR_ERR(bh);
246 mutex_unlock(&c->ic_idle_mutex);
253 * Initialize container @c.
255 int iam_container_init(struct iam_container *c,
256 struct iam_descr *descr, struct inode *inode)
258 memset(c, 0, sizeof *c);
260 c->ic_object = inode;
261 init_rwsem(&c->ic_sem);
262 dynlock_init(&c->ic_tree_lock);
263 mutex_init(&c->ic_idle_mutex);
266 EXPORT_SYMBOL(iam_container_init);
269 * Determine container format.
271 int iam_container_setup(struct iam_container *c)
273 return iam_format_guess(c);
275 EXPORT_SYMBOL(iam_container_setup);
278 * Finalize container @c, release all resources.
280 void iam_container_fini(struct iam_container *c)
282 brelse(c->ic_idle_bh);
283 c->ic_idle_bh = NULL;
284 brelse(c->ic_root_bh);
285 c->ic_root_bh = NULL;
287 EXPORT_SYMBOL(iam_container_fini);
289 void iam_path_init(struct iam_path *path, struct iam_container *c,
290 struct iam_path_descr *pd)
292 memset(path, 0, sizeof *path);
293 path->ip_container = c;
294 path->ip_frame = path->ip_frames;
296 path->ip_leaf.il_path = path;
299 static void iam_leaf_fini(struct iam_leaf *leaf);
301 void iam_path_release(struct iam_path *path)
305 for (i = 0; i < ARRAY_SIZE(path->ip_frames); i++) {
306 if (path->ip_frames[i].bh != NULL) {
307 path->ip_frames[i].at_shifted = 0;
308 brelse(path->ip_frames[i].bh);
309 path->ip_frames[i].bh = NULL;
314 void iam_path_fini(struct iam_path *path)
316 iam_leaf_fini(&path->ip_leaf);
317 iam_path_release(path);
321 void iam_path_compat_init(struct iam_path_compat *path, struct inode *inode)
325 path->ipc_hinfo = &path->ipc_hinfo_area;
326 for (i = 0; i < ARRAY_SIZE(path->ipc_scratch); ++i)
327 path->ipc_descr.ipd_key_scratch[i] =
328 (struct iam_ikey *)&path->ipc_scratch[i];
330 iam_path_init(&path->ipc_path, &path->ipc_container, &path->ipc_descr);
333 void iam_path_compat_fini(struct iam_path_compat *path)
335 iam_path_fini(&path->ipc_path);
339 * Helper function initializing iam_path_descr and its key scratch area.
341 struct iam_path_descr *iam_ipd_alloc(void *area, int keysize)
343 struct iam_path_descr *ipd;
349 for (i = 0; i < ARRAY_SIZE(ipd->ipd_key_scratch); ++i, karea += keysize)
350 ipd->ipd_key_scratch[i] = karea;
353 EXPORT_SYMBOL(iam_ipd_alloc);
355 void iam_ipd_free(struct iam_path_descr *ipd)
358 EXPORT_SYMBOL(iam_ipd_free);
360 int iam_node_read(struct iam_container *c, iam_ptr_t ptr,
361 handle_t *h, struct buffer_head **bh)
365 /* NB: it can be called by iam_lfix_guess() which is still at
366 * very early stage, c->ic_root_bh and c->ic_descr->id_ops
367 * haven't been intialized yet.
368 * Also, we don't have this for IAM dir.
370 if (c->ic_root_bh != NULL &&
371 c->ic_descr->id_ops->id_root_ptr(c) == ptr) {
372 get_bh(c->ic_root_bh);
377 *bh = ldiskfs_bread(h, c->ic_object, (int)ptr, 0, &result);
384 * Return pointer to current leaf record. Pointer is valid while corresponding
385 * leaf node is locked and pinned.
387 static struct iam_rec *iam_leaf_rec(const struct iam_leaf *leaf)
389 return iam_leaf_ops(leaf)->rec(leaf);
393 * Return pointer to the current leaf key. This function returns pointer to
394 * the key stored in node.
396 * Caller should assume that returned pointer is only valid while leaf node is
399 static struct iam_key *iam_leaf_key(const struct iam_leaf *leaf)
401 return iam_leaf_ops(leaf)->key(leaf);
404 static int iam_leaf_key_size(const struct iam_leaf *leaf)
406 return iam_leaf_ops(leaf)->key_size(leaf);
409 static struct iam_ikey *iam_leaf_ikey(const struct iam_leaf *leaf,
410 struct iam_ikey *key)
412 return iam_leaf_ops(leaf)->ikey(leaf, key);
415 static int iam_leaf_keycmp(const struct iam_leaf *leaf,
416 const struct iam_key *key)
418 return iam_leaf_ops(leaf)->key_cmp(leaf, key);
421 static int iam_leaf_keyeq(const struct iam_leaf *leaf,
422 const struct iam_key *key)
424 return iam_leaf_ops(leaf)->key_eq(leaf, key);
427 #if LDISKFS_INVARIANT_ON
428 static int iam_leaf_check(struct iam_leaf *leaf);
429 extern int dx_node_check(struct iam_path *p, struct iam_frame *f);
431 static int iam_path_check(struct iam_path *p)
436 struct iam_descr *param;
439 param = iam_path_descr(p);
440 for (i = 0; result && i < ARRAY_SIZE(p->ip_frames); ++i) {
441 f = &p->ip_frames[i];
443 result = dx_node_check(p, f);
445 result = !param->id_ops->id_node_check(p, f);
448 if (result && p->ip_leaf.il_bh != NULL)
449 result = iam_leaf_check(&p->ip_leaf);
451 ldiskfs_std_error(iam_path_obj(p)->i_sb, result);
457 static int iam_leaf_load(struct iam_path *path)
461 struct iam_container *c;
462 struct buffer_head *bh;
463 struct iam_leaf *leaf;
464 struct iam_descr *descr;
466 c = path->ip_container;
467 leaf = &path->ip_leaf;
468 descr = iam_path_descr(path);
469 block = path->ip_frame->leaf;
472 printk(KERN_EMERG "wrong leaf: %lu %d [%p %p %p]\n",
473 (long unsigned)path->ip_frame->leaf,
474 dx_get_count(dx_node_get_entries(path, path->ip_frame)),
475 path->ip_frames[0].bh, path->ip_frames[1].bh,
476 path->ip_frames[2].bh);
478 err = descr->id_ops->id_node_read(c, block, NULL, &bh);
481 leaf->il_curidx = block;
482 err = iam_leaf_ops(leaf)->init(leaf);
483 assert_inv(ergo(err == 0, iam_leaf_check(leaf)));
488 static void iam_unlock_htree(struct iam_container *ic,
489 struct dynlock_handle *lh)
492 dynlock_unlock(&ic->ic_tree_lock, lh);
496 static void iam_leaf_unlock(struct iam_leaf *leaf)
498 if (leaf->il_lock != NULL) {
499 iam_unlock_htree(iam_leaf_container(leaf),
502 leaf->il_lock = NULL;
506 static void iam_leaf_fini(struct iam_leaf *leaf)
508 if (leaf->il_path != NULL) {
509 iam_leaf_unlock(leaf);
510 assert_inv(ergo(leaf->il_bh != NULL, iam_leaf_check(leaf)));
511 iam_leaf_ops(leaf)->fini(leaf);
520 static void iam_leaf_start(struct iam_leaf *folio)
522 iam_leaf_ops(folio)->start(folio);
525 void iam_leaf_next(struct iam_leaf *folio)
527 iam_leaf_ops(folio)->next(folio);
530 static void iam_leaf_rec_add(struct iam_leaf *leaf, const struct iam_key *key,
531 const struct iam_rec *rec)
533 iam_leaf_ops(leaf)->rec_add(leaf, key, rec);
536 static void iam_rec_del(struct iam_leaf *leaf, int shift)
538 iam_leaf_ops(leaf)->rec_del(leaf, shift);
541 int iam_leaf_at_end(const struct iam_leaf *leaf)
543 return iam_leaf_ops(leaf)->at_end(leaf);
546 void iam_leaf_split(struct iam_leaf *l, struct buffer_head **bh, iam_ptr_t nr)
548 iam_leaf_ops(l)->split(l, bh, nr);
551 static inline int iam_leaf_empty(struct iam_leaf *l)
553 return iam_leaf_ops(l)->leaf_empty(l);
556 int iam_leaf_can_add(const struct iam_leaf *l,
557 const struct iam_key *k, const struct iam_rec *r)
559 return iam_leaf_ops(l)->can_add(l, k, r);
562 #if LDISKFS_INVARIANT_ON
563 static int iam_leaf_check(struct iam_leaf *leaf)
567 struct iam_lentry *orig;
568 struct iam_path *path;
569 struct iam_container *bag;
576 path = iam_leaf_path(leaf);
577 bag = iam_leaf_container(leaf);
579 result = iam_leaf_ops(leaf)->init(leaf);
584 iam_leaf_start(leaf);
585 k0 = iam_path_ikey(path, 0);
586 k1 = iam_path_ikey(path, 1);
587 while (!iam_leaf_at_end(leaf)) {
588 iam_ikeycpy(bag, k0, k1);
589 iam_ikeycpy(bag, k1, iam_leaf_ikey(leaf, k1));
590 if (!first && iam_ikeycmp(bag, k0, k1) > 0) {
602 static int iam_txn_dirty(handle_t *handle,
603 struct iam_path *path, struct buffer_head *bh)
607 result = ldiskfs_journal_dirty_metadata(handle, bh);
609 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
613 static int iam_txn_add(handle_t *handle,
614 struct iam_path *path, struct buffer_head *bh)
618 result = ldiskfs_journal_get_write_access(handle, bh);
620 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
624 /***********************************************************************/
625 /* iterator interface */
626 /***********************************************************************/
628 static enum iam_it_state it_state(const struct iam_iterator *it)
634 * Helper function returning scratch key.
636 static struct iam_container *iam_it_container(const struct iam_iterator *it)
638 return it->ii_path.ip_container;
641 static inline int it_keycmp(const struct iam_iterator *it,
642 const struct iam_key *k)
644 return iam_leaf_keycmp(&it->ii_path.ip_leaf, k);
647 static inline int it_keyeq(const struct iam_iterator *it,
648 const struct iam_key *k)
650 return iam_leaf_keyeq(&it->ii_path.ip_leaf, k);
653 static int it_ikeycmp(const struct iam_iterator *it, const struct iam_ikey *ik)
655 return iam_ikeycmp(it->ii_path.ip_container,
656 iam_leaf_ikey(&it->ii_path.ip_leaf,
657 iam_path_ikey(&it->ii_path, 0)), ik);
660 static inline int it_at_rec(const struct iam_iterator *it)
662 return !iam_leaf_at_end(&it->ii_path.ip_leaf);
665 static inline int it_before(const struct iam_iterator *it)
667 return it_state(it) == IAM_IT_SKEWED && it_at_rec(it);
671 * Helper wrapper around iam_it_get(): returns 0 (success) only when record
672 * with exactly the same key as asked is found.
674 static int iam_it_get_exact(struct iam_iterator *it, const struct iam_key *k)
678 result = iam_it_get(it, k);
681 else if (result == 0)
683 * Return -ENOENT if cursor is located above record with a key
684 * different from one specified, or in the empty leaf.
686 * XXX returning -ENOENT only works if iam_it_get() never
687 * returns -ENOENT as a legitimate error.
693 void iam_container_write_lock(struct iam_container *ic)
695 down_write(&ic->ic_sem);
698 void iam_container_write_unlock(struct iam_container *ic)
700 up_write(&ic->ic_sem);
703 void iam_container_read_lock(struct iam_container *ic)
705 down_read(&ic->ic_sem);
708 void iam_container_read_unlock(struct iam_container *ic)
710 up_read(&ic->ic_sem);
714 * Initialize iterator to IAM_IT_DETACHED state.
716 * postcondition: it_state(it) == IAM_IT_DETACHED
718 int iam_it_init(struct iam_iterator *it, struct iam_container *c, __u32 flags,
719 struct iam_path_descr *pd)
721 memset(it, 0, sizeof *it);
722 it->ii_flags = flags;
723 it->ii_state = IAM_IT_DETACHED;
724 iam_path_init(&it->ii_path, c, pd);
727 EXPORT_SYMBOL(iam_it_init);
730 * Finalize iterator and release all resources.
732 * precondition: it_state(it) == IAM_IT_DETACHED
734 void iam_it_fini(struct iam_iterator *it)
736 assert_corr(it_state(it) == IAM_IT_DETACHED);
737 iam_path_fini(&it->ii_path);
739 EXPORT_SYMBOL(iam_it_fini);
742 * this locking primitives are used to protect parts
743 * of dir's htree. protection unit is block: leaf or index
745 static struct dynlock_handle *iam_lock_htree(struct iam_container *ic,
747 enum dynlock_type lt)
749 return dynlock_lock(&ic->ic_tree_lock, value, lt, GFP_NOFS);
752 int iam_index_lock(struct iam_path *path, struct dynlock_handle **lh)
756 for (f = path->ip_frame; f >= path->ip_frames; --f, ++lh) {
758 *lh = iam_lock_htree(path->ip_container, f->curidx, DLT_READ);
766 * Fast check for frame consistency.
768 static int iam_check_fast(struct iam_path *path, struct iam_frame *frame)
770 struct iam_container *bag;
771 struct iam_entry *next;
772 struct iam_entry *last;
773 struct iam_entry *entries;
774 struct iam_entry *at;
776 bag = path->ip_container;
778 entries = frame->entries;
779 last = iam_entry_shift(path, entries, dx_get_count(entries) - 1);
781 if (unlikely(at > last))
784 if (unlikely(dx_get_block(path, at) != frame->leaf))
787 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, at),
788 path->ip_ikey_target) > 0))
791 next = iam_entry_shift(path, at, +1);
793 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, next),
794 path->ip_ikey_target) <= 0))
800 int dx_index_is_compat(struct iam_path *path)
802 return iam_path_descr(path) == NULL;
808 * search position of specified hash in index
812 struct iam_entry *iam_find_position(struct iam_path *path,
813 struct iam_frame *frame)
820 count = dx_get_count(frame->entries);
821 assert_corr(count && count <= dx_get_limit(frame->entries));
822 p = iam_entry_shift(path, frame->entries,
823 dx_index_is_compat(path) ? 1 : 2);
824 q = iam_entry_shift(path, frame->entries, count - 1);
826 m = iam_entry_shift(path, p, iam_entry_diff(path, q, p) / 2);
827 if (iam_ikeycmp(path->ip_container, iam_ikey_at(path, m),
828 path->ip_ikey_target) > 0)
829 q = iam_entry_shift(path, m, -1);
831 p = iam_entry_shift(path, m, +1);
833 return iam_entry_shift(path, p, -1);
838 static iam_ptr_t iam_find_ptr(struct iam_path *path, struct iam_frame *frame)
840 return dx_get_block(path, iam_find_position(path, frame));
843 void iam_insert_key(struct iam_path *path, struct iam_frame *frame,
844 const struct iam_ikey *key, iam_ptr_t ptr)
846 struct iam_entry *entries = frame->entries;
847 struct iam_entry *new = iam_entry_shift(path, frame->at, +1);
848 int count = dx_get_count(entries);
851 * Unfortunately we cannot assert this, as this function is sometimes
852 * called by VFS under i_sem and without pdirops lock.
854 assert_corr(1 || iam_frame_is_locked(path, frame));
855 assert_corr(count < dx_get_limit(entries));
856 assert_corr(frame->at < iam_entry_shift(path, entries, count));
857 assert_inv(dx_node_check(path, frame));
859 memmove(iam_entry_shift(path, new, 1), new,
860 (char *)iam_entry_shift(path, entries, count) - (char *)new);
861 dx_set_ikey(path, new, key);
862 dx_set_block(path, new, ptr);
863 dx_set_count(entries, count + 1);
864 assert_inv(dx_node_check(path, frame));
867 void iam_insert_key_lock(struct iam_path *path, struct iam_frame *frame,
868 const struct iam_ikey *key, iam_ptr_t ptr)
870 iam_lock_bh(frame->bh);
871 iam_insert_key(path, frame, key, ptr);
872 iam_unlock_bh(frame->bh);
875 * returns 0 if path was unchanged, -EAGAIN otherwise.
877 static int iam_check_path(struct iam_path *path, struct iam_frame *frame)
881 iam_lock_bh(frame->bh);
882 equal = iam_check_fast(path, frame) == 0 ||
883 frame->leaf == iam_find_ptr(path, frame);
884 DX_DEVAL(iam_lock_stats.dls_bh_again += !equal);
885 iam_unlock_bh(frame->bh);
887 return equal ? 0 : -EAGAIN;
890 static int iam_lookup_try(struct iam_path *path)
896 struct iam_descr *param;
897 struct iam_frame *frame;
898 struct iam_container *c;
900 param = iam_path_descr(path);
901 c = path->ip_container;
903 ptr = param->id_ops->id_root_ptr(c);
904 for (frame = path->ip_frames, i = 0; i <= path->ip_indirect;
906 err = param->id_ops->id_node_read(c, (iam_ptr_t)ptr, NULL,
910 iam_lock_bh(frame->bh);
912 * node must be initialized under bh lock because concurrent
913 * creation procedure may change it and iam_lookup_try() will
914 * see obsolete tree height. -bzzz
919 if (LDISKFS_INVARIANT_ON) {
920 err = param->id_ops->id_node_check(path, frame);
925 err = param->id_ops->id_node_load(path, frame);
929 assert_inv(dx_node_check(path, frame));
931 * splitting may change root index block and move hash we're
932 * looking for into another index block so, we have to check
933 * this situation and repeat from begining if path got changed
937 err = iam_check_path(path, frame - 1);
942 frame->at = iam_find_position(path, frame);
944 frame->leaf = ptr = dx_get_block(path, frame->at);
946 iam_unlock_bh(frame->bh);
950 iam_unlock_bh(frame->bh);
951 path->ip_frame = --frame;
955 static int __iam_path_lookup(struct iam_path *path)
960 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++ i)
961 assert(path->ip_frames[i].bh == NULL);
964 err = iam_lookup_try(path);
968 } while (err == -EAGAIN);
974 * returns 0 if path was unchanged, -EAGAIN otherwise.
976 static int iam_check_full_path(struct iam_path *path, int search)
978 struct iam_frame *bottom;
979 struct iam_frame *scan;
985 for (bottom = path->ip_frames, i = 0;
986 i < DX_MAX_TREE_HEIGHT && bottom->bh != NULL; ++bottom, ++i) {
987 ; /* find last filled in frame */
991 * Lock frames, bottom to top.
993 for (scan = bottom - 1; scan >= path->ip_frames; --scan)
994 iam_lock_bh(scan->bh);
996 * Check them top to bottom.
999 for (scan = path->ip_frames; scan < bottom; ++scan) {
1000 struct iam_entry *pos;
1003 if (iam_check_fast(path, scan) == 0)
1006 pos = iam_find_position(path, scan);
1007 if (scan->leaf != dx_get_block(path, pos)) {
1013 pos = iam_entry_shift(path, scan->entries,
1014 dx_get_count(scan->entries) - 1);
1015 if (scan->at > pos ||
1016 scan->leaf != dx_get_block(path, scan->at)) {
1024 * Unlock top to bottom.
1026 for (scan = path->ip_frames; scan < bottom; ++scan)
1027 iam_unlock_bh(scan->bh);
1028 DX_DEVAL(iam_lock_stats.dls_bh_full_again += !!result);
1029 do_corr(schedule());
1036 * Performs path lookup and returns with found leaf (if any) locked by htree
1039 int iam_lookup_lock(struct iam_path *path,
1040 struct dynlock_handle **dl, enum dynlock_type lt)
1044 while ((result = __iam_path_lookup(path)) == 0) {
1045 do_corr(schedule());
1046 *dl = iam_lock_htree(path->ip_container, path->ip_frame->leaf,
1049 iam_path_fini(path);
1053 do_corr(schedule());
1055 * while locking leaf we just found may get split so we need
1056 * to check this -bzzz
1058 if (iam_check_full_path(path, 1) == 0)
1060 iam_unlock_htree(path->ip_container, *dl);
1062 iam_path_fini(path);
1067 * Performs tree top-to-bottom traversal starting from root, and loads leaf
1070 static int iam_path_lookup(struct iam_path *path, int index)
1072 struct iam_container *c;
1073 struct iam_leaf *leaf;
1076 c = path->ip_container;
1077 leaf = &path->ip_leaf;
1078 result = iam_lookup_lock(path, &leaf->il_lock, DLT_WRITE);
1079 assert_inv(iam_path_check(path));
1080 do_corr(schedule());
1082 result = iam_leaf_load(path);
1083 assert_inv(ergo(result == 0, iam_leaf_check(leaf)));
1085 do_corr(schedule());
1087 result = iam_leaf_ops(leaf)->
1088 ilookup(leaf, path->ip_ikey_target);
1090 result = iam_leaf_ops(leaf)->
1091 lookup(leaf, path->ip_key_target);
1092 do_corr(schedule());
1095 iam_leaf_unlock(leaf);
1101 * Common part of iam_it_{i,}get().
1103 static int __iam_it_get(struct iam_iterator *it, int index)
1106 assert_corr(it_state(it) == IAM_IT_DETACHED);
1108 result = iam_path_lookup(&it->ii_path, index);
1112 collision = result & IAM_LOOKUP_LAST;
1113 switch (result & ~IAM_LOOKUP_LAST) {
1114 case IAM_LOOKUP_EXACT:
1116 it->ii_state = IAM_IT_ATTACHED;
1120 it->ii_state = IAM_IT_ATTACHED;
1122 case IAM_LOOKUP_BEFORE:
1123 case IAM_LOOKUP_EMPTY:
1125 it->ii_state = IAM_IT_SKEWED;
1130 result |= collision;
1133 * See iam_it_get_exact() for explanation.
1135 assert_corr(result != -ENOENT);
1140 * Correct hash, but not the same key was found, iterate through hash
1141 * collision chain, looking for correct record.
1143 static int iam_it_collision(struct iam_iterator *it)
1147 assert(ergo(it_at_rec(it), !it_keyeq(it, it->ii_path.ip_key_target)));
1149 while ((result = iam_it_next(it)) == 0) {
1150 do_corr(schedule());
1151 if (it_ikeycmp(it, it->ii_path.ip_ikey_target) != 0)
1153 if (it_keyeq(it, it->ii_path.ip_key_target))
1160 * Attach iterator. After successful completion, @it points to record with
1161 * least key not larger than @k.
1163 * Return value: 0: positioned on existing record,
1164 * +ve: exact position found,
1167 * precondition: it_state(it) == IAM_IT_DETACHED
1168 * postcondition: ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1169 * it_keycmp(it, k) <= 0)
1171 int iam_it_get(struct iam_iterator *it, const struct iam_key *k)
1174 assert_corr(it_state(it) == IAM_IT_DETACHED);
1176 it->ii_path.ip_ikey_target = NULL;
1177 it->ii_path.ip_key_target = k;
1179 result = __iam_it_get(it, 0);
1181 if (result == IAM_LOOKUP_LAST) {
1182 result = iam_it_collision(it);
1186 result = __iam_it_get(it, 0);
1191 result &= ~IAM_LOOKUP_LAST;
1193 assert_corr(ergo(result > 0, it_keycmp(it, k) == 0));
1194 assert_corr(ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1195 it_keycmp(it, k) <= 0));
1198 EXPORT_SYMBOL(iam_it_get);
1201 * Attach iterator by index key.
1203 static int iam_it_iget(struct iam_iterator *it, const struct iam_ikey *k)
1205 assert_corr(it_state(it) == IAM_IT_DETACHED);
1207 it->ii_path.ip_ikey_target = k;
1208 return __iam_it_get(it, 1) & ~IAM_LOOKUP_LAST;
1212 * Attach iterator, and assure it points to the record (not skewed).
1214 * Return value: 0: positioned on existing record,
1215 * +ve: exact position found,
1218 * precondition: it_state(it) == IAM_IT_DETACHED &&
1219 * !(it->ii_flags&IAM_IT_WRITE)
1220 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED)
1222 int iam_it_get_at(struct iam_iterator *it, const struct iam_key *k)
1225 assert_corr(it_state(it) == IAM_IT_DETACHED &&
1226 !(it->ii_flags&IAM_IT_WRITE));
1227 result = iam_it_get(it, k);
1229 if (it_state(it) != IAM_IT_ATTACHED) {
1230 assert_corr(it_state(it) == IAM_IT_SKEWED);
1231 result = iam_it_next(it);
1234 assert_corr(ergo(result >= 0, it_state(it) == IAM_IT_ATTACHED));
1237 EXPORT_SYMBOL(iam_it_get_at);
1240 * Duplicates iterator.
1242 * postcondition: it_state(dst) == it_state(src) &&
1243 * iam_it_container(dst) == iam_it_container(src) &&
1244 * dst->ii_flags = src->ii_flags &&
1245 * ergo(it_state(src) == IAM_IT_ATTACHED,
1246 * iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1247 * iam_it_key_get(dst) == iam_it_key_get(src))
1249 void iam_it_dup(struct iam_iterator *dst, const struct iam_iterator *src)
1251 dst->ii_flags = src->ii_flags;
1252 dst->ii_state = src->ii_state;
1253 /* XXX not yet. iam_path_dup(&dst->ii_path, &src->ii_path); */
1255 * XXX: duplicate lock.
1257 assert_corr(it_state(dst) == it_state(src));
1258 assert_corr(iam_it_container(dst) == iam_it_container(src));
1259 assert_corr(dst->ii_flags = src->ii_flags);
1260 assert_corr(ergo(it_state(src) == IAM_IT_ATTACHED,
1261 iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1262 iam_it_key_get(dst) == iam_it_key_get(src)));
1267 * Detach iterator. Does nothing it detached state.
1269 * postcondition: it_state(it) == IAM_IT_DETACHED
1271 void iam_it_put(struct iam_iterator *it)
1273 if (it->ii_state != IAM_IT_DETACHED) {
1274 it->ii_state = IAM_IT_DETACHED;
1275 iam_leaf_fini(&it->ii_path.ip_leaf);
1278 EXPORT_SYMBOL(iam_it_put);
1280 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1281 struct iam_ikey *ikey);
1285 * This function increments the frame pointer to search the next leaf
1286 * block, and reads in the necessary intervening nodes if the search
1287 * should be necessary. Whether or not the search is necessary is
1288 * controlled by the hash parameter. If the hash value is even, then
1289 * the search is only continued if the next block starts with that
1290 * hash value. This is used if we are searching for a specific file.
1292 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1294 * This function returns 1 if the caller should continue to search,
1295 * or 0 if it should not. If there is an error reading one of the
1296 * index blocks, it will a negative error code.
1298 * If start_hash is non-null, it will be filled in with the starting
1299 * hash of the next page.
1301 static int iam_htree_advance(struct inode *dir, __u32 hash,
1302 struct iam_path *path, __u32 *start_hash,
1305 struct iam_frame *p;
1306 struct buffer_head *bh;
1307 int err, num_frames = 0;
1312 * Find the next leaf page by incrementing the frame pointer.
1313 * If we run out of entries in the interior node, loop around and
1314 * increment pointer in the parent node. When we break out of
1315 * this loop, num_frames indicates the number of interior
1316 * nodes need to be read.
1319 do_corr(schedule());
1324 p->at = iam_entry_shift(path, p->at, +1);
1325 if (p->at < iam_entry_shift(path, p->entries,
1326 dx_get_count(p->entries))) {
1327 p->leaf = dx_get_block(path, p->at);
1328 iam_unlock_bh(p->bh);
1331 iam_unlock_bh(p->bh);
1332 if (p == path->ip_frames)
1343 * If the hash is 1, then continue only if the next page has a
1344 * continuation hash of any value. This is used for readdir
1345 * handling. Otherwise, check to see if the hash matches the
1346 * desired contiuation hash. If it doesn't, return since
1347 * there's no point to read in the successive index pages.
1349 dx_get_ikey(path, p->at, (struct iam_ikey *)&bhash);
1351 *start_hash = bhash;
1352 if ((hash & 1) == 0) {
1353 if ((bhash & ~1) != hash)
1358 * If the hash is HASH_NB_ALWAYS, we always go to the next
1359 * block so no check is necessary
1361 while (num_frames--) {
1364 do_corr(schedule());
1366 idx = p->leaf = dx_get_block(path, p->at);
1367 iam_unlock_bh(p->bh);
1368 err = iam_path_descr(path)->id_ops->
1369 id_node_read(path->ip_container, idx, NULL, &bh);
1371 return err; /* Failure */
1374 assert_corr(p->bh != bh);
1376 p->entries = dx_node_get_entries(path, p);
1377 p->at = iam_entry_shift(path, p->entries, !compat);
1378 assert_corr(p->curidx != idx);
1381 assert_corr(p->leaf != dx_get_block(path, p->at));
1382 p->leaf = dx_get_block(path, p->at);
1383 iam_unlock_bh(p->bh);
1384 assert_inv(dx_node_check(path, p));
1390 static inline int iam_index_advance(struct iam_path *path)
1392 return iam_htree_advance(iam_path_obj(path), 0, path, NULL, 0);
1395 static void iam_unlock_array(struct iam_container *ic,
1396 struct dynlock_handle **lh)
1400 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++i, ++lh) {
1402 iam_unlock_htree(ic, *lh);
1408 * Advance index part of @path to point to the next leaf. Returns 1 on
1409 * success, 0, when end of container was reached. Leaf node is locked.
1411 int iam_index_next(struct iam_container *c, struct iam_path *path)
1414 struct dynlock_handle *lh[DX_MAX_TREE_HEIGHT] = { 0, };
1416 struct inode *object;
1419 * Locking for iam_index_next()... is to be described.
1422 object = c->ic_object;
1423 cursor = path->ip_frame->leaf;
1426 result = iam_index_lock(path, lh);
1427 do_corr(schedule());
1431 result = iam_check_full_path(path, 0);
1432 if (result == 0 && cursor == path->ip_frame->leaf) {
1433 result = iam_index_advance(path);
1435 assert_corr(result == 0 ||
1436 cursor != path->ip_frame->leaf);
1440 iam_unlock_array(c, lh);
1442 iam_path_release(path);
1443 do_corr(schedule());
1445 result = __iam_path_lookup(path);
1449 while (path->ip_frame->leaf != cursor) {
1450 do_corr(schedule());
1452 result = iam_index_lock(path, lh);
1453 do_corr(schedule());
1457 result = iam_check_full_path(path, 0);
1461 result = iam_index_advance(path);
1463 CERROR("cannot find cursor : %u\n",
1469 result = iam_check_full_path(path, 0);
1472 iam_unlock_array(c, lh);
1474 } while (result == -EAGAIN);
1478 iam_unlock_array(c, lh);
1483 * Move iterator one record right.
1485 * Return value: 0: success,
1486 * +1: end of container reached
1489 * precondition: (it_state(it) == IAM_IT_ATTACHED ||
1490 * it_state(it) == IAM_IT_SKEWED) && it->ii_flags&IAM_IT_MOVE
1491 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED) &&
1492 * ergo(result > 0, it_state(it) == IAM_IT_DETACHED)
1494 int iam_it_next(struct iam_iterator *it)
1497 struct iam_path *path;
1498 struct iam_leaf *leaf;
1499 do_corr(struct iam_ikey *ik_orig);
1501 /* assert_corr(it->ii_flags&IAM_IT_MOVE); */
1502 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1503 it_state(it) == IAM_IT_SKEWED);
1505 path = &it->ii_path;
1506 leaf = &path->ip_leaf;
1508 assert_corr(iam_leaf_is_locked(leaf));
1511 do_corr(ik_orig = it_at_rec(it) ?
1512 iam_it_ikey_get(it, iam_path_ikey(path, 2)) : NULL);
1513 if (it_before(it)) {
1514 assert_corr(!iam_leaf_at_end(leaf));
1515 it->ii_state = IAM_IT_ATTACHED;
1517 if (!iam_leaf_at_end(leaf))
1518 /* advance within leaf node */
1519 iam_leaf_next(leaf);
1521 * multiple iterations may be necessary due to empty leaves.
1523 while (result == 0 && iam_leaf_at_end(leaf)) {
1524 do_corr(schedule());
1525 /* advance index portion of the path */
1526 result = iam_index_next(iam_it_container(it), path);
1527 assert_corr(iam_leaf_is_locked(leaf));
1529 struct dynlock_handle *lh;
1530 lh = iam_lock_htree(iam_it_container(it),
1531 path->ip_frame->leaf,
1534 iam_leaf_fini(leaf);
1536 result = iam_leaf_load(path);
1538 iam_leaf_start(leaf);
1541 } else if (result == 0)
1542 /* end of container reached */
1548 it->ii_state = IAM_IT_ATTACHED;
1550 assert_corr(ergo(result == 0, it_state(it) == IAM_IT_ATTACHED));
1551 assert_corr(ergo(result > 0, it_state(it) == IAM_IT_DETACHED));
1552 assert_corr(ergo(result == 0 && ik_orig != NULL,
1553 it_ikeycmp(it, ik_orig) >= 0));
1556 EXPORT_SYMBOL(iam_it_next);
1559 * Return pointer to the record under iterator.
1561 * precondition: it_state(it) == IAM_IT_ATTACHED && it_at_rec(it)
1562 * postcondition: it_state(it) == IAM_IT_ATTACHED
1564 struct iam_rec *iam_it_rec_get(const struct iam_iterator *it)
1566 assert_corr(it_state(it) == IAM_IT_ATTACHED);
1567 assert_corr(it_at_rec(it));
1568 return iam_leaf_rec(&it->ii_path.ip_leaf);
1570 EXPORT_SYMBOL(iam_it_rec_get);
1572 static void iam_it_reccpy(struct iam_iterator *it, const struct iam_rec *r)
1574 struct iam_leaf *folio;
1576 folio = &it->ii_path.ip_leaf;
1577 iam_leaf_ops(folio)->rec_set(folio, r);
1581 * Replace contents of record under iterator.
1583 * precondition: it_state(it) == IAM_IT_ATTACHED &&
1584 * it->ii_flags&IAM_IT_WRITE
1585 * postcondition: it_state(it) == IAM_IT_ATTACHED &&
1586 * ergo(result == 0, !memcmp(iam_it_rec_get(it), r, ...))
1588 int iam_it_rec_set(handle_t *h,
1589 struct iam_iterator *it, const struct iam_rec *r)
1592 struct iam_path *path;
1593 struct buffer_head *bh;
1595 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
1596 it->ii_flags&IAM_IT_WRITE);
1597 assert_corr(it_at_rec(it));
1599 path = &it->ii_path;
1600 bh = path->ip_leaf.il_bh;
1601 result = iam_txn_add(h, path, bh);
1603 iam_it_reccpy(it, r);
1604 result = iam_txn_dirty(h, path, bh);
1608 EXPORT_SYMBOL(iam_it_rec_set);
1611 * Return pointer to the index key under iterator.
1613 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1614 * it_state(it) == IAM_IT_SKEWED
1616 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1617 struct iam_ikey *ikey)
1619 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1620 it_state(it) == IAM_IT_SKEWED);
1621 assert_corr(it_at_rec(it));
1622 return iam_leaf_ikey(&it->ii_path.ip_leaf, ikey);
1626 * Return pointer to the key under iterator.
1628 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1629 * it_state(it) == IAM_IT_SKEWED
1631 struct iam_key *iam_it_key_get(const struct iam_iterator *it)
1633 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1634 it_state(it) == IAM_IT_SKEWED);
1635 assert_corr(it_at_rec(it));
1636 return iam_leaf_key(&it->ii_path.ip_leaf);
1638 EXPORT_SYMBOL(iam_it_key_get);
1641 * Return size of key under iterator (in bytes)
1643 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1644 * it_state(it) == IAM_IT_SKEWED
1646 int iam_it_key_size(const struct iam_iterator *it)
1648 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1649 it_state(it) == IAM_IT_SKEWED);
1650 assert_corr(it_at_rec(it));
1651 return iam_leaf_key_size(&it->ii_path.ip_leaf);
1653 EXPORT_SYMBOL(iam_it_key_size);
1655 struct buffer_head *
1656 iam_new_node(handle_t *h, struct iam_container *c, iam_ptr_t *b, int *e)
1658 struct inode *inode = c->ic_object;
1659 struct buffer_head *bh = NULL;
1660 struct iam_idle_head *head;
1661 struct buffer_head *idle;
1665 if (c->ic_idle_bh == NULL)
1668 mutex_lock(&c->ic_idle_mutex);
1669 if (unlikely(c->ic_idle_bh == NULL)) {
1670 mutex_unlock(&c->ic_idle_mutex);
1674 head = (struct iam_idle_head *)(c->ic_idle_bh->b_data);
1675 count = le16_to_cpu(head->iih_count);
1677 *e = ldiskfs_journal_get_write_access(h, c->ic_idle_bh);
1682 *b = le32_to_cpu(head->iih_blks[count]);
1683 head->iih_count = cpu_to_le16(count);
1684 *e = ldiskfs_journal_dirty_metadata(h, c->ic_idle_bh);
1688 mutex_unlock(&c->ic_idle_mutex);
1689 bh = ldiskfs_bread(NULL, inode, *b, 0, e);
1695 /* The block itself which contains the iam_idle_head is
1696 * also an idle block, and can be used as the new node. */
1697 idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
1698 c->ic_descr->id_root_gap +
1699 sizeof(struct dx_countlimit));
1700 *e = ldiskfs_journal_get_write_access(h, c->ic_root_bh);
1704 *b = le32_to_cpu(*idle_blocks);
1705 iam_lock_bh(c->ic_root_bh);
1706 *idle_blocks = head->iih_next;
1707 iam_unlock_bh(c->ic_root_bh);
1708 *e = ldiskfs_journal_dirty_metadata(h, c->ic_root_bh);
1710 iam_lock_bh(c->ic_root_bh);
1711 *idle_blocks = cpu_to_le32(*b);
1712 iam_unlock_bh(c->ic_root_bh);
1717 idle = iam_load_idle_blocks(c, le32_to_cpu(*idle_blocks));
1718 if (idle != NULL && IS_ERR(idle)) {
1720 c->ic_idle_bh = NULL;
1725 c->ic_idle_bh = idle;
1726 mutex_unlock(&c->ic_idle_mutex);
1729 /* get write access for the found buffer head */
1730 *e = ldiskfs_journal_get_write_access(h, bh);
1734 ldiskfs_std_error(inode->i_sb, *e);
1736 /* Clear the reused node as new node does. */
1737 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1738 set_buffer_uptodate(bh);
1743 bh = osd_ldiskfs_append(h, inode, b, e);
1747 mutex_unlock(&c->ic_idle_mutex);
1748 ldiskfs_std_error(inode->i_sb, *e);
1753 * Insertion of new record. Interaction with jbd during non-trivial case (when
1754 * split happens) is as following:
1756 * - new leaf node is involved into transaction by iam_new_node();
1758 * - old leaf node is involved into transaction by iam_add_rec();
1760 * - leaf where insertion point ends in, is marked dirty by iam_add_rec();
1762 * - leaf without insertion point is marked dirty (as @new_leaf) by
1765 * - split index nodes are involved into transaction and marked dirty by
1766 * split_index_node().
1768 * - "safe" index node, which is no split, but where new pointer is inserted
1769 * is involved into transaction and marked dirty by split_index_node().
1771 * - index node where pointer to new leaf is inserted is involved into
1772 * transaction by split_index_node() and marked dirty by iam_add_rec().
1774 * - inode is marked dirty by iam_add_rec().
1778 static int iam_new_leaf(handle_t *handle, struct iam_leaf *leaf)
1782 struct buffer_head *new_leaf;
1783 struct buffer_head *old_leaf;
1784 struct iam_container *c;
1786 struct iam_path *path;
1788 assert_inv(iam_leaf_check(leaf));
1790 c = iam_leaf_container(leaf);
1791 path = leaf->il_path;
1794 new_leaf = iam_new_node(handle, c, &blknr, &err);
1795 do_corr(schedule());
1796 if (new_leaf != NULL) {
1797 struct dynlock_handle *lh;
1799 lh = iam_lock_htree(c, blknr, DLT_WRITE);
1800 do_corr(schedule());
1802 iam_leaf_ops(leaf)->init_new(c, new_leaf);
1803 do_corr(schedule());
1804 old_leaf = leaf->il_bh;
1805 iam_leaf_split(leaf, &new_leaf, blknr);
1806 if (old_leaf != leaf->il_bh) {
1808 * Switched to the new leaf.
1810 iam_leaf_unlock(leaf);
1812 path->ip_frame->leaf = blknr;
1814 iam_unlock_htree(path->ip_container, lh);
1815 do_corr(schedule());
1816 err = iam_txn_dirty(handle, path, new_leaf);
1819 err = ldiskfs_mark_inode_dirty(handle, obj);
1820 do_corr(schedule());
1824 assert_inv(iam_leaf_check(leaf));
1825 assert_inv(iam_leaf_check(&iam_leaf_path(leaf)->ip_leaf));
1826 assert_inv(iam_path_check(iam_leaf_path(leaf)));
1830 static inline void dx_set_limit(struct iam_entry *entries, unsigned value)
1832 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
1835 static int iam_shift_entries(struct iam_path *path,
1836 struct iam_frame *frame, unsigned count,
1837 struct iam_entry *entries, struct iam_entry *entries2,
1844 struct iam_frame *parent = frame - 1;
1845 struct iam_ikey *pivot = iam_path_ikey(path, 3);
1847 delta = dx_index_is_compat(path) ? 0 : +1;
1849 count1 = count/2 + delta;
1850 count2 = count - count1;
1851 dx_get_ikey(path, iam_entry_shift(path, entries, count1), pivot);
1853 dxtrace(printk("Split index %d/%d\n", count1, count2));
1855 memcpy((char *) iam_entry_shift(path, entries2, delta),
1856 (char *) iam_entry_shift(path, entries, count1),
1857 count2 * iam_entry_size(path));
1859 dx_set_count(entries2, count2 + delta);
1860 dx_set_limit(entries2, dx_node_limit(path));
1863 * NOTE: very subtle piece of code competing dx_probe() may find 2nd
1864 * level index in root index, then we insert new index here and set
1865 * new count in that 2nd level index. so, dx_probe() may see 2nd level
1866 * index w/o hash it looks for. the solution is to check root index
1867 * after we locked just founded 2nd level index -bzzz
1869 iam_insert_key_lock(path, parent, pivot, newblock);
1872 * now old and new 2nd level index blocks contain all pointers, so
1873 * dx_probe() may find it in the both. it's OK -bzzz
1875 iam_lock_bh(frame->bh);
1876 dx_set_count(entries, count1);
1877 iam_unlock_bh(frame->bh);
1880 * now old 2nd level index block points to first half of leafs. it's
1881 * importand that dx_probe() must check root index block for changes
1882 * under dx_lock_bh(frame->bh) -bzzz
1889 int split_index_node(handle_t *handle, struct iam_path *path,
1890 struct dynlock_handle **lh)
1893 struct iam_entry *entries; /* old block contents */
1894 struct iam_entry *entries2; /* new block contents */
1895 struct iam_frame *frame, *safe;
1896 struct buffer_head *bh_new[DX_MAX_TREE_HEIGHT] = {0};
1897 u32 newblock[DX_MAX_TREE_HEIGHT] = {0};
1898 struct dynlock_handle *lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1899 struct dynlock_handle *new_lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1900 struct inode *dir = iam_path_obj(path);
1901 struct iam_descr *descr;
1905 descr = iam_path_descr(path);
1907 * Algorithm below depends on this.
1909 assert_corr(dx_root_limit(path) < dx_node_limit(path));
1911 frame = path->ip_frame;
1912 entries = frame->entries;
1915 * Tall-tree handling: we might have to split multiple index blocks
1916 * all the way up to tree root. Tricky point here is error handling:
1917 * to avoid complicated undo/rollback we
1919 * - first allocate all necessary blocks
1921 * - insert pointers into them atomically.
1925 * Locking: leaf is already locked. htree-locks are acquired on all
1926 * index nodes that require split bottom-to-top, on the "safe" node,
1927 * and on all new nodes
1930 dxtrace(printk("using %u of %u node entries\n",
1931 dx_get_count(entries), dx_get_limit(entries)));
1933 /* What levels need split? */
1934 for (nr_splet = 0; frame >= path->ip_frames &&
1935 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1936 --frame, ++nr_splet) {
1937 do_corr(schedule());
1938 if (nr_splet == DX_MAX_TREE_HEIGHT) {
1940 CWARN(dir->i_sb, __FUNCTION__,
1941 "Directory index full!\n");
1951 * Lock all nodes, bottom to top.
1953 for (frame = path->ip_frame, i = nr_splet; i >= 0; --i, --frame) {
1954 do_corr(schedule());
1955 lock[i] = iam_lock_htree(path->ip_container, frame->curidx,
1957 if (lock[i] == NULL) {
1964 * Check for concurrent index modification.
1966 err = iam_check_full_path(path, 1);
1970 * And check that the same number of nodes is to be split.
1972 for (i = 0, frame = path->ip_frame; frame >= path->ip_frames &&
1973 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1977 if (i != nr_splet) {
1982 /* Go back down, allocating blocks, locking them, and adding into
1984 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1985 bh_new[i] = iam_new_node(handle, path->ip_container,
1986 &newblock[i], &err);
1987 do_corr(schedule());
1989 descr->id_ops->id_node_init(path->ip_container,
1992 new_lock[i] = iam_lock_htree(path->ip_container, newblock[i],
1994 if (new_lock[i] == NULL) {
1998 do_corr(schedule());
1999 BUFFER_TRACE(frame->bh, "get_write_access");
2000 err = ldiskfs_journal_get_write_access(handle, frame->bh);
2004 /* Add "safe" node to transaction too */
2005 if (safe + 1 != path->ip_frames) {
2006 do_corr(schedule());
2007 err = ldiskfs_journal_get_write_access(handle, safe->bh);
2012 /* Go through nodes once more, inserting pointers */
2013 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
2016 struct buffer_head *bh2;
2017 struct buffer_head *bh;
2019 entries = frame->entries;
2020 count = dx_get_count(entries);
2021 idx = iam_entry_diff(path, frame->at, entries);
2024 entries2 = dx_get_entries(path, bh2->b_data, 0);
2027 if (frame == path->ip_frames) {
2028 /* splitting root node. Tricky point:
2030 * In the "normal" B-tree we'd split root *and* add
2031 * new root to the tree with pointers to the old root
2032 * and its sibling (thus introducing two new nodes).
2034 * In htree it's enough to add one node, because
2035 * capacity of the root node is smaller than that of
2038 struct iam_frame *frames;
2039 struct iam_entry *next;
2041 assert_corr(i == 0);
2043 do_corr(schedule());
2045 frames = path->ip_frames;
2046 memcpy((char *) entries2, (char *) entries,
2047 count * iam_entry_size(path));
2048 dx_set_limit(entries2, dx_node_limit(path));
2051 iam_lock_bh(frame->bh);
2052 next = descr->id_ops->id_root_inc(path->ip_container,
2054 dx_set_block(path, next, newblock[0]);
2055 iam_unlock_bh(frame->bh);
2057 do_corr(schedule());
2058 /* Shift frames in the path */
2059 memmove(frames + 2, frames + 1,
2060 (sizeof path->ip_frames) - 2 * sizeof frames[0]);
2061 /* Add new access path frame */
2062 frames[1].at = iam_entry_shift(path, entries2, idx);
2063 frames[1].entries = entries = entries2;
2065 assert_inv(dx_node_check(path, frame));
2068 assert_inv(dx_node_check(path, frame));
2069 bh_new[0] = NULL; /* buffer head is "consumed" */
2070 err = ldiskfs_journal_dirty_metadata(handle, bh2);
2073 do_corr(schedule());
2075 /* splitting non-root index node. */
2076 struct iam_frame *parent = frame - 1;
2078 do_corr(schedule());
2079 count = iam_shift_entries(path, frame, count,
2080 entries, entries2, newblock[i]);
2081 /* Which index block gets the new entry? */
2083 int d = dx_index_is_compat(path) ? 0 : +1;
2085 frame->at = iam_entry_shift(path, entries2,
2087 frame->entries = entries = entries2;
2088 frame->curidx = newblock[i];
2089 swap(frame->bh, bh2);
2090 assert_corr(lock[i + 1] != NULL);
2091 assert_corr(new_lock[i] != NULL);
2092 swap(lock[i + 1], new_lock[i]);
2094 parent->at = iam_entry_shift(path,
2097 assert_inv(dx_node_check(path, frame));
2098 assert_inv(dx_node_check(path, parent));
2099 dxtrace(dx_show_index ("node", frame->entries));
2100 dxtrace(dx_show_index ("node",
2101 ((struct dx_node *) bh2->b_data)->entries));
2102 err = ldiskfs_journal_dirty_metadata(handle, bh2);
2105 do_corr(schedule());
2106 err = ldiskfs_journal_dirty_metadata(handle, parent->bh);
2110 do_corr(schedule());
2111 err = ldiskfs_journal_dirty_metadata(handle, bh);
2116 * This function was called to make insertion of new leaf
2117 * possible. Check that it fulfilled its obligations.
2119 assert_corr(dx_get_count(path->ip_frame->entries) <
2120 dx_get_limit(path->ip_frame->entries));
2121 assert_corr(lock[nr_splet] != NULL);
2122 *lh = lock[nr_splet];
2123 lock[nr_splet] = NULL;
2126 * Log ->i_size modification.
2128 err = ldiskfs_mark_inode_dirty(handle, dir);
2134 ldiskfs_std_error(dir->i_sb, err);
2137 iam_unlock_array(path->ip_container, lock);
2138 iam_unlock_array(path->ip_container, new_lock);
2140 assert_corr(err || iam_frame_is_locked(path, path->ip_frame));
2142 do_corr(schedule());
2143 for (i = 0; i < ARRAY_SIZE(bh_new); ++i) {
2144 if (bh_new[i] != NULL)
2150 static int iam_add_rec(handle_t *handle, struct iam_iterator *it,
2151 struct iam_path *path,
2152 const struct iam_key *k, const struct iam_rec *r)
2155 struct iam_leaf *leaf;
2157 leaf = &path->ip_leaf;
2158 assert_inv(iam_leaf_check(leaf));
2159 assert_inv(iam_path_check(path));
2160 err = iam_txn_add(handle, path, leaf->il_bh);
2162 do_corr(schedule());
2163 if (!iam_leaf_can_add(leaf, k, r)) {
2164 struct dynlock_handle *lh = NULL;
2167 assert_corr(lh == NULL);
2168 do_corr(schedule());
2169 err = split_index_node(handle, path, &lh);
2170 if (err == -EAGAIN) {
2171 assert_corr(lh == NULL);
2173 iam_path_fini(path);
2174 it->ii_state = IAM_IT_DETACHED;
2176 do_corr(schedule());
2177 err = iam_it_get_exact(it, k);
2179 err = +1; /* repeat split */
2184 assert_inv(iam_path_check(path));
2186 assert_corr(lh != NULL);
2187 do_corr(schedule());
2188 err = iam_new_leaf(handle, leaf);
2190 err = iam_txn_dirty(handle, path,
2191 path->ip_frame->bh);
2193 iam_unlock_htree(path->ip_container, lh);
2194 do_corr(schedule());
2197 iam_leaf_rec_add(leaf, k, r);
2198 err = iam_txn_dirty(handle, path, leaf->il_bh);
2201 assert_inv(iam_leaf_check(leaf));
2202 assert_inv(iam_leaf_check(&path->ip_leaf));
2203 assert_inv(iam_path_check(path));
2208 * Insert new record with key @k and contents from @r, shifting records to the
2209 * right. On success, iterator is positioned on the newly inserted record.
2211 * precondition: it->ii_flags&IAM_IT_WRITE &&
2212 * (it_state(it) == IAM_IT_ATTACHED ||
2213 * it_state(it) == IAM_IT_SKEWED) &&
2214 * ergo(it_state(it) == IAM_IT_ATTACHED,
2215 * it_keycmp(it, k) <= 0) &&
2216 * ergo(it_before(it), it_keycmp(it, k) > 0));
2217 * postcondition: ergo(result == 0,
2218 * it_state(it) == IAM_IT_ATTACHED &&
2219 * it_keycmp(it, k) == 0 &&
2220 * !memcmp(iam_it_rec_get(it), r, ...))
2222 int iam_it_rec_insert(handle_t *h, struct iam_iterator *it,
2223 const struct iam_key *k, const struct iam_rec *r)
2226 struct iam_path *path;
2228 path = &it->ii_path;
2230 assert_corr(it->ii_flags&IAM_IT_WRITE);
2231 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2232 it_state(it) == IAM_IT_SKEWED);
2233 assert_corr(ergo(it_state(it) == IAM_IT_ATTACHED,
2234 it_keycmp(it, k) <= 0));
2235 assert_corr(ergo(it_before(it), it_keycmp(it, k) > 0));
2236 result = iam_add_rec(h, it, path, k, r);
2238 it->ii_state = IAM_IT_ATTACHED;
2239 assert_corr(ergo(result == 0,
2240 it_state(it) == IAM_IT_ATTACHED &&
2241 it_keycmp(it, k) == 0));
2244 EXPORT_SYMBOL(iam_it_rec_insert);
2246 static inline int iam_idle_blocks_limit(struct inode *inode)
2248 return (inode->i_sb->s_blocksize - sizeof(struct iam_idle_head)) >> 2;
2252 * If the leaf cannnot be recycled, we will lose one block for reusing.
2253 * It is not a serious issue because it almost the same of non-recycle.
2255 static iam_ptr_t iam_index_shrink(handle_t *h, struct iam_path *p,
2256 struct iam_leaf *l, struct buffer_head **bh)
2258 struct iam_container *c = p->ip_container;
2259 struct inode *inode = c->ic_object;
2260 struct iam_frame *frame = p->ip_frame;
2261 struct iam_entry *entries;
2262 struct iam_entry *pos;
2263 struct dynlock_handle *lh;
2267 if (c->ic_idle_failed)
2270 if (unlikely(frame == NULL))
2273 if (!iam_leaf_empty(l))
2276 lh = iam_lock_htree(c, frame->curidx, DLT_WRITE);
2278 CWARN("%.16s: No memory to recycle idle blocks\n",
2279 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name);
2283 rc = iam_txn_add(h, p, frame->bh);
2285 iam_unlock_htree(c, lh);
2289 iam_lock_bh(frame->bh);
2290 entries = frame->entries;
2291 count = dx_get_count(entries);
2292 /* NOT shrink the last entry in the index node, which can be reused
2293 * directly by next new node. */
2295 iam_unlock_bh(frame->bh);
2296 iam_unlock_htree(c, lh);
2300 pos = iam_find_position(p, frame);
2301 /* There may be some new leaf nodes have been added or empty leaf nodes
2302 * have been shrinked during my delete operation.
2304 * If the empty leaf is not under current index node because the index
2305 * node has been split, then just skip the empty leaf, which is rare. */
2306 if (unlikely(frame->leaf != dx_get_block(p, pos))) {
2307 iam_unlock_bh(frame->bh);
2308 iam_unlock_htree(c, lh);
2313 if (frame->at < iam_entry_shift(p, entries, count - 1)) {
2314 struct iam_entry *n = iam_entry_shift(p, frame->at, 1);
2316 memmove(frame->at, n,
2317 (char *)iam_entry_shift(p, entries, count) - (char *)n);
2318 frame->at_shifted = 1;
2320 dx_set_count(entries, count - 1);
2321 iam_unlock_bh(frame->bh);
2322 rc = iam_txn_dirty(h, p, frame->bh);
2323 iam_unlock_htree(c, lh);
2333 iam_install_idle_blocks(handle_t *h, struct iam_path *p, struct buffer_head *bh,
2334 __u32 *idle_blocks, iam_ptr_t blk)
2336 struct iam_container *c = p->ip_container;
2337 struct buffer_head *old = c->ic_idle_bh;
2338 struct iam_idle_head *head;
2341 head = (struct iam_idle_head *)(bh->b_data);
2342 head->iih_magic = cpu_to_le16(IAM_IDLE_HEADER_MAGIC);
2343 head->iih_count = 0;
2344 head->iih_next = *idle_blocks;
2345 /* The bh already get_write_accessed. */
2346 rc = iam_txn_dirty(h, p, bh);
2350 rc = iam_txn_add(h, p, c->ic_root_bh);
2354 iam_lock_bh(c->ic_root_bh);
2355 *idle_blocks = cpu_to_le32(blk);
2356 iam_unlock_bh(c->ic_root_bh);
2357 rc = iam_txn_dirty(h, p, c->ic_root_bh);
2359 /* NOT release old before new assigned. */
2364 iam_lock_bh(c->ic_root_bh);
2365 *idle_blocks = head->iih_next;
2366 iam_unlock_bh(c->ic_root_bh);
2372 * If the leaf cannnot be recycled, we will lose one block for reusing.
2373 * It is not a serious issue because it almost the same of non-recycle.
2375 static void iam_recycle_leaf(handle_t *h, struct iam_path *p,
2376 struct buffer_head *bh, iam_ptr_t blk)
2378 struct iam_container *c = p->ip_container;
2379 struct inode *inode = c->ic_object;
2380 struct iam_idle_head *head;
2385 mutex_lock(&c->ic_idle_mutex);
2386 if (unlikely(c->ic_idle_failed)) {
2391 idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
2392 c->ic_descr->id_root_gap +
2393 sizeof(struct dx_countlimit));
2394 /* It is the first idle block. */
2395 if (c->ic_idle_bh == NULL) {
2396 rc = iam_install_idle_blocks(h, p, bh, idle_blocks, blk);
2400 head = (struct iam_idle_head *)(c->ic_idle_bh->b_data);
2401 count = le16_to_cpu(head->iih_count);
2402 /* Current ic_idle_bh is full, to be replaced by the leaf. */
2403 if (count == iam_idle_blocks_limit(inode)) {
2404 rc = iam_install_idle_blocks(h, p, bh, idle_blocks, blk);
2408 /* Just add to ic_idle_bh. */
2409 rc = iam_txn_add(h, p, c->ic_idle_bh);
2413 head->iih_blks[count] = cpu_to_le32(blk);
2414 head->iih_count = cpu_to_le16(count + 1);
2415 rc = iam_txn_dirty(h, p, c->ic_idle_bh);
2418 mutex_unlock(&c->ic_idle_mutex);
2420 CWARN("%.16s: idle blocks failed, will lose the blk %u\n",
2421 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name, blk);
2425 * Delete record under iterator.
2427 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2428 * it->ii_flags&IAM_IT_WRITE &&
2430 * postcondition: it_state(it) == IAM_IT_ATTACHED ||
2431 * it_state(it) == IAM_IT_DETACHED
2433 int iam_it_rec_delete(handle_t *h, struct iam_iterator *it)
2436 struct iam_leaf *leaf;
2437 struct iam_path *path;
2439 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
2440 it->ii_flags&IAM_IT_WRITE);
2441 assert_corr(it_at_rec(it));
2443 path = &it->ii_path;
2444 leaf = &path->ip_leaf;
2446 assert_inv(iam_leaf_check(leaf));
2447 assert_inv(iam_path_check(path));
2449 result = iam_txn_add(h, path, leaf->il_bh);
2451 * no compaction for now.
2454 iam_rec_del(leaf, it->ii_flags&IAM_IT_MOVE);
2455 result = iam_txn_dirty(h, path, leaf->il_bh);
2456 if (result == 0 && iam_leaf_at_end(leaf)) {
2457 struct buffer_head *bh = NULL;
2460 blk = iam_index_shrink(h, path, leaf, &bh);
2461 if (it->ii_flags & IAM_IT_MOVE) {
2462 result = iam_it_next(it);
2468 iam_recycle_leaf(h, path, bh, blk);
2473 assert_inv(iam_leaf_check(leaf));
2474 assert_inv(iam_path_check(path));
2475 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2476 it_state(it) == IAM_IT_DETACHED);
2479 EXPORT_SYMBOL(iam_it_rec_delete);
2482 * Convert iterator to cookie.
2484 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2485 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2486 * postcondition: it_state(it) == IAM_IT_ATTACHED
2488 iam_pos_t iam_it_store(const struct iam_iterator *it)
2492 assert_corr(it_state(it) == IAM_IT_ATTACHED);
2493 assert_corr(it_at_rec(it));
2494 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <=
2498 return *(iam_pos_t *)iam_it_ikey_get(it, (void *)&result);
2500 EXPORT_SYMBOL(iam_it_store);
2503 * Restore iterator from cookie.
2505 * precondition: it_state(it) == IAM_IT_DETACHED && it->ii_flags&IAM_IT_MOVE &&
2506 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2507 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED &&
2508 * iam_it_store(it) == pos)
2510 int iam_it_load(struct iam_iterator *it, iam_pos_t pos)
2512 assert_corr(it_state(it) == IAM_IT_DETACHED &&
2513 it->ii_flags&IAM_IT_MOVE);
2514 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <= sizeof pos);
2515 return iam_it_iget(it, (struct iam_ikey *)&pos);
2517 EXPORT_SYMBOL(iam_it_load);
2519 /***********************************************************************/
2521 /***********************************************************************/
2523 static inline int ptr_inside(void *base, size_t size, void *ptr)
2525 return (base <= ptr) && (ptr < base + size);
2528 int iam_frame_invariant(struct iam_frame *f)
2532 f->bh->b_data != NULL &&
2533 ptr_inside(f->bh->b_data, f->bh->b_size, f->entries) &&
2534 ptr_inside(f->bh->b_data, f->bh->b_size, f->at) &&
2535 f->entries <= f->at);
2537 int iam_leaf_invariant(struct iam_leaf *l)
2541 l->il_bh->b_data != NULL &&
2542 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_entries) &&
2543 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_at) &&
2544 l->il_entries <= l->il_at;
2547 int iam_path_invariant(struct iam_path *p)
2551 if (p->ip_container == NULL ||
2552 p->ip_indirect < 0 || p->ip_indirect > DX_MAX_TREE_HEIGHT - 1 ||
2553 p->ip_frame != p->ip_frames + p->ip_indirect ||
2554 !iam_leaf_invariant(&p->ip_leaf))
2556 for (i = 0; i < ARRAY_SIZE(p->ip_frames); ++i) {
2557 if (i <= p->ip_indirect) {
2558 if (!iam_frame_invariant(&p->ip_frames[i]))
2565 int iam_it_invariant(struct iam_iterator *it)
2568 (it->ii_state == IAM_IT_DETACHED ||
2569 it->ii_state == IAM_IT_ATTACHED ||
2570 it->ii_state == IAM_IT_SKEWED) &&
2571 !(it->ii_flags & ~(IAM_IT_MOVE | IAM_IT_WRITE)) &&
2572 ergo(it->ii_state == IAM_IT_ATTACHED ||
2573 it->ii_state == IAM_IT_SKEWED,
2574 iam_path_invariant(&it->ii_path) &&
2575 equi(it_at_rec(it), it->ii_state == IAM_IT_SKEWED));
2579 * Search container @c for record with key @k. If record is found, its data
2580 * are moved into @r.
2582 * Return values: 0: found, -ENOENT: not-found, -ve: error
2584 int iam_lookup(struct iam_container *c, const struct iam_key *k,
2585 struct iam_rec *r, struct iam_path_descr *pd)
2587 struct iam_iterator it;
2590 iam_it_init(&it, c, 0, pd);
2592 result = iam_it_get_exact(&it, k);
2595 * record with required key found, copy it into user buffer
2597 iam_reccpy(&it.ii_path.ip_leaf, r);
2602 EXPORT_SYMBOL(iam_lookup);
2605 * Insert new record @r with key @k into container @c (within context of
2608 * Return values: 0: success, -ve: error, including -EEXIST when record with
2609 * given key is already present.
2611 * postcondition: ergo(result == 0 || result == -EEXIST,
2612 * iam_lookup(c, k, r2) > 0;
2614 int iam_insert(handle_t *h, struct iam_container *c, const struct iam_key *k,
2615 const struct iam_rec *r, struct iam_path_descr *pd)
2617 struct iam_iterator it;
2620 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2622 result = iam_it_get_exact(&it, k);
2623 if (result == -ENOENT)
2624 result = iam_it_rec_insert(h, &it, k, r);
2625 else if (result == 0)
2631 EXPORT_SYMBOL(iam_insert);
2634 * Update record with the key @k in container @c (within context of
2635 * transaction @h), new record is given by @r.
2637 * Return values: +1: skip because of the same rec value, 0: success,
2638 * -ve: error, including -ENOENT if no record with the given key found.
2640 int iam_update(handle_t *h, struct iam_container *c, const struct iam_key *k,
2641 const struct iam_rec *r, struct iam_path_descr *pd)
2643 struct iam_iterator it;
2644 struct iam_leaf *folio;
2647 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2649 result = iam_it_get_exact(&it, k);
2651 folio = &it.ii_path.ip_leaf;
2652 result = iam_leaf_ops(folio)->rec_eq(folio, r);
2654 iam_it_rec_set(h, &it, r);
2662 EXPORT_SYMBOL(iam_update);
2665 * Delete existing record with key @k.
2667 * Return values: 0: success, -ENOENT: not-found, -ve: other error.
2669 * postcondition: ergo(result == 0 || result == -ENOENT,
2670 * !iam_lookup(c, k, *));
2672 int iam_delete(handle_t *h, struct iam_container *c, const struct iam_key *k,
2673 struct iam_path_descr *pd)
2675 struct iam_iterator it;
2678 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2680 result = iam_it_get_exact(&it, k);
2682 iam_it_rec_delete(h, &it);
2687 EXPORT_SYMBOL(iam_delete);
2689 int iam_root_limit(int rootgap, int blocksize, int size)
2694 limit = (blocksize - rootgap) / size;
2695 nlimit = blocksize / size;
2696 if (limit == nlimit)