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
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * Top-level entry points into iam module
35 * Author: Wang Di <wangdi@clusterfs.com>
36 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * iam: big theory statement.
42 * iam (Index Access Module) is a module providing abstraction of persistent
43 * transactional container on top of generalized ldiskfs htree.
47 * - key, pointer, and record size specifiable per container.
49 * - trees taller than 2 index levels.
51 * - read/write to existing ldiskfs htree directories as iam containers.
53 * iam container is a tree, consisting of leaf nodes containing keys and
54 * records stored in this container, and index nodes, containing keys and
55 * pointers to leaf or index nodes.
57 * iam does not work with keys directly, instead it calls user-supplied key
58 * comparison function (->dpo_keycmp()).
60 * Pointers are (currently) interpreted as logical offsets (measured in
61 * blocksful) within underlying flat file on top of which iam tree lives.
65 * iam mostly tries to reuse existing htree formats.
67 * Format of index node:
69 * +-----+-------+-------+-------+------+-------+------------+
70 * | | count | | | | | |
71 * | gap | / | entry | entry | .... | entry | free space |
72 * | | limit | | | | | |
73 * +-----+-------+-------+-------+------+-------+------------+
75 * gap this part of node is never accessed by iam code. It
76 * exists for binary compatibility with ldiskfs htree (that,
77 * in turn, stores fake struct ext2_dirent for ext2
78 * compatibility), and to keep some unspecified per-node
79 * data. Gap can be different for root and non-root index
80 * nodes. Gap size can be specified for each container
81 * (gap of 0 is allowed).
83 * count/limit current number of entries in this node, and the maximal
84 * number of entries that can fit into node. count/limit
85 * has the same size as entry, and is itself counted in
88 * entry index entry: consists of a key immediately followed by
89 * a pointer to a child node. Size of a key and size of a
90 * pointer depends on container. Entry has neither
91 * alignment nor padding.
93 * free space portion of node new entries are added to
95 * Entries in index node are sorted by their key value.
97 * Format of a leaf node is not specified. Generic iam code accesses leaf
98 * nodes through ->id_leaf methods in struct iam_descr.
100 * The IAM root block is a special node, which contains the IAM descriptor.
101 * It is on disk format:
103 * +---------+-------+--------+---------+-------+------+-------+------------+
104 * |IAM desc | count | idle | | | | | |
105 * |(fix/var)| / | blocks | padding | entry | .... | entry | free space |
106 * | | limit | | | | | | |
107 * +---------+-------+--------+---------+-------+------+-------+------------+
109 * The padding length is calculated with the parameters in the IAM descriptor.
111 * The field "idle_blocks" is used to record empty leaf nodes, which have not
112 * been released but all contained entries in them have been removed. Usually,
113 * the idle blocks in the IAM should be reused when need to allocate new leaf
114 * nodes for new entries, it depends on the IAM hash functions to map the new
115 * entries to these idle blocks. Unfortunately, it is not easy to design some
116 * hash functions for such clever mapping, especially considering the insert/
117 * lookup performance.
119 * So the IAM recycles the empty leaf nodes, and put them into a per-file based
120 * idle blocks pool. If need some new leaf node, it will try to take idle block
121 * from such pool with priority, in spite of how the IAM hash functions to map
124 * The idle blocks pool is organized as a series of tables, and each table
125 * can be described as following (on-disk format):
127 * +---------+---------+---------+---------+------+---------+-------+
128 * | magic | count | next | logic | | logic | free |
129 * |(16 bits)|(16 bits)| table | blk # | .... | blk # | space |
130 * | | |(32 bits)|(32 bits)| |(32 bits)| |
131 * +---------+---------+---------+---------+------+---------+-------+
133 * The logic blk# for the first table is stored in the root node "idle_blocks".
137 #include <linux/module.h>
138 #include <linux/fs.h>
139 #include <linux/pagemap.h>
140 #include <linux/time.h>
141 #include <linux/fcntl.h>
142 #include <linux/stat.h>
143 #include <linux/string.h>
144 #include <linux/quotaops.h>
145 #include <linux/buffer_head.h>
147 #include <ldiskfs/ldiskfs.h>
148 #include <ldiskfs/xattr.h>
151 #include "osd_internal.h"
153 #include <ldiskfs/acl.h>
156 * List of all registered formats.
158 * No locking. Callers synchronize.
160 static struct list_head iam_formats = LIST_HEAD_INIT(iam_formats);
162 void iam_format_register(struct iam_format *fmt)
164 list_add(&fmt->if_linkage, &iam_formats);
167 static struct buffer_head *
168 iam_load_idle_blocks(struct iam_container *c, iam_ptr_t blk)
170 struct inode *inode = c->ic_object;
171 struct iam_idle_head *head;
172 struct buffer_head *bh;
174 LASSERT(mutex_is_locked(&c->ic_idle_mutex));
179 bh = __ldiskfs_bread(NULL, inode, blk, 0);
180 if (IS_ERR_OR_NULL(bh)) {
181 CERROR("%.16s: cannot load idle blocks, blk = %u, err = %ld\n",
182 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name, blk,
183 bh ? PTR_ERR(bh) : -EIO);
184 c->ic_idle_failed = 1;
190 head = (struct iam_idle_head *)(bh->b_data);
191 if (le16_to_cpu(head->iih_magic) != IAM_IDLE_HEADER_MAGIC) {
192 CERROR("%.16s: invalid idle block head, blk = %u, magic = %d\n",
193 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name, blk,
194 le16_to_cpu(head->iih_magic));
196 c->ic_idle_failed = 1;
197 return ERR_PTR(-EBADF);
204 * Determine format of given container. This is done by scanning list of
205 * registered formats and calling ->if_guess() method of each in turn.
207 static int iam_format_guess(struct iam_container *c)
210 struct iam_format *fmt;
213 * XXX temporary initialization hook.
216 static int initialized = 0;
219 iam_lvar_format_init();
220 iam_lfix_format_init();
226 list_for_each_entry(fmt, &iam_formats, if_linkage) {
227 result = fmt->if_guess(c);
233 struct buffer_head *bh;
236 LASSERT(c->ic_root_bh != NULL);
238 idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
239 c->ic_descr->id_root_gap +
240 sizeof(struct dx_countlimit));
241 mutex_lock(&c->ic_idle_mutex);
242 bh = iam_load_idle_blocks(c, le32_to_cpu(*idle_blocks));
243 if (bh != NULL && IS_ERR(bh))
244 result = PTR_ERR(bh);
247 mutex_unlock(&c->ic_idle_mutex);
254 * Initialize container @c.
256 int iam_container_init(struct iam_container *c,
257 struct iam_descr *descr, struct inode *inode)
259 memset(c, 0, sizeof *c);
261 c->ic_object = inode;
262 init_rwsem(&c->ic_sem);
263 dynlock_init(&c->ic_tree_lock);
264 mutex_init(&c->ic_idle_mutex);
269 * Determine container format.
271 int iam_container_setup(struct iam_container *c)
273 return iam_format_guess(c);
277 * Finalize container @c, release all resources.
279 void iam_container_fini(struct iam_container *c)
281 brelse(c->ic_idle_bh);
282 c->ic_idle_bh = NULL;
283 brelse(c->ic_root_bh);
284 c->ic_root_bh = NULL;
287 void iam_path_init(struct iam_path *path, struct iam_container *c,
288 struct iam_path_descr *pd)
290 memset(path, 0, sizeof *path);
291 path->ip_container = c;
292 path->ip_frame = path->ip_frames;
294 path->ip_leaf.il_path = path;
297 static void iam_leaf_fini(struct iam_leaf *leaf);
299 void iam_path_release(struct iam_path *path)
303 for (i = 0; i < ARRAY_SIZE(path->ip_frames); i++) {
304 if (path->ip_frames[i].bh != NULL) {
305 path->ip_frames[i].at_shifted = 0;
306 brelse(path->ip_frames[i].bh);
307 path->ip_frames[i].bh = NULL;
312 void iam_path_fini(struct iam_path *path)
314 iam_leaf_fini(&path->ip_leaf);
315 iam_path_release(path);
319 void iam_path_compat_init(struct iam_path_compat *path, struct inode *inode)
323 path->ipc_hinfo = &path->ipc_hinfo_area;
324 for (i = 0; i < ARRAY_SIZE(path->ipc_scratch); ++i)
325 path->ipc_descr.ipd_key_scratch[i] =
326 (struct iam_ikey *)&path->ipc_scratch[i];
328 iam_path_init(&path->ipc_path, &path->ipc_container, &path->ipc_descr);
331 void iam_path_compat_fini(struct iam_path_compat *path)
333 iam_path_fini(&path->ipc_path);
337 * Helper function initializing iam_path_descr and its key scratch area.
339 struct iam_path_descr *iam_ipd_alloc(void *area, int keysize)
341 struct iam_path_descr *ipd;
347 for (i = 0; i < ARRAY_SIZE(ipd->ipd_key_scratch); ++i, karea += keysize)
348 ipd->ipd_key_scratch[i] = karea;
352 void iam_ipd_free(struct iam_path_descr *ipd)
356 int iam_node_read(struct iam_container *c, iam_ptr_t ptr,
357 handle_t *h, struct buffer_head **bh)
359 /* NB: it can be called by iam_lfix_guess() which is still at
360 * very early stage, c->ic_root_bh and c->ic_descr->id_ops
361 * haven't been intialized yet.
362 * Also, we don't have this for IAM dir.
364 if (c->ic_root_bh != NULL &&
365 c->ic_descr->id_ops->id_root_ptr(c) == ptr) {
366 get_bh(c->ic_root_bh);
371 *bh = __ldiskfs_bread(h, c->ic_object, (int)ptr, 0);
382 * Return pointer to current leaf record. Pointer is valid while corresponding
383 * leaf node is locked and pinned.
385 static struct iam_rec *iam_leaf_rec(const struct iam_leaf *leaf)
387 return iam_leaf_ops(leaf)->rec(leaf);
391 * Return pointer to the current leaf key. This function returns pointer to
392 * the key stored in node.
394 * Caller should assume that returned pointer is only valid while leaf node is
397 static struct iam_key *iam_leaf_key(const struct iam_leaf *leaf)
399 return iam_leaf_ops(leaf)->key(leaf);
402 static int iam_leaf_key_size(const struct iam_leaf *leaf)
404 return iam_leaf_ops(leaf)->key_size(leaf);
407 static struct iam_ikey *iam_leaf_ikey(const struct iam_leaf *leaf,
408 struct iam_ikey *key)
410 return iam_leaf_ops(leaf)->ikey(leaf, key);
413 static int iam_leaf_keycmp(const struct iam_leaf *leaf,
414 const struct iam_key *key)
416 return iam_leaf_ops(leaf)->key_cmp(leaf, key);
419 static int iam_leaf_keyeq(const struct iam_leaf *leaf,
420 const struct iam_key *key)
422 return iam_leaf_ops(leaf)->key_eq(leaf, key);
425 #if LDISKFS_INVARIANT_ON
426 extern int dx_node_check(struct iam_path *p, struct iam_frame *f);
428 static int iam_path_check(struct iam_path *p)
433 struct iam_descr *param;
436 param = iam_path_descr(p);
437 for (i = 0; result && i < ARRAY_SIZE(p->ip_frames); ++i) {
438 f = &p->ip_frames[i];
440 result = dx_node_check(p, f);
442 result = !param->id_ops->id_node_check(p, f);
445 if (result && p->ip_leaf.il_bh != NULL)
448 ldiskfs_std_error(iam_path_obj(p)->i_sb, result);
454 static int iam_leaf_load(struct iam_path *path)
458 struct iam_container *c;
459 struct buffer_head *bh;
460 struct iam_leaf *leaf;
461 struct iam_descr *descr;
463 c = path->ip_container;
464 leaf = &path->ip_leaf;
465 descr = iam_path_descr(path);
466 block = path->ip_frame->leaf;
469 printk(KERN_EMERG "wrong leaf: %lu %d [%p %p %p]\n",
470 (long unsigned)path->ip_frame->leaf,
471 dx_get_count(dx_node_get_entries(path, path->ip_frame)),
472 path->ip_frames[0].bh, path->ip_frames[1].bh,
473 path->ip_frames[2].bh);
475 err = descr->id_ops->id_node_read(c, block, NULL, &bh);
478 leaf->il_curidx = block;
479 err = iam_leaf_ops(leaf)->init(leaf);
484 static void iam_unlock_htree(struct iam_container *ic,
485 struct dynlock_handle *lh)
488 dynlock_unlock(&ic->ic_tree_lock, lh);
492 static void iam_leaf_unlock(struct iam_leaf *leaf)
494 if (leaf->il_lock != NULL) {
495 iam_unlock_htree(iam_leaf_container(leaf),
498 leaf->il_lock = NULL;
502 static void iam_leaf_fini(struct iam_leaf *leaf)
504 if (leaf->il_path != NULL) {
505 iam_leaf_unlock(leaf);
506 iam_leaf_ops(leaf)->fini(leaf);
515 static void iam_leaf_start(struct iam_leaf *folio)
517 iam_leaf_ops(folio)->start(folio);
520 void iam_leaf_next(struct iam_leaf *folio)
522 iam_leaf_ops(folio)->next(folio);
525 static void iam_leaf_rec_add(struct iam_leaf *leaf, const struct iam_key *key,
526 const struct iam_rec *rec)
528 iam_leaf_ops(leaf)->rec_add(leaf, key, rec);
531 static void iam_rec_del(struct iam_leaf *leaf, int shift)
533 iam_leaf_ops(leaf)->rec_del(leaf, shift);
536 int iam_leaf_at_end(const struct iam_leaf *leaf)
538 return iam_leaf_ops(leaf)->at_end(leaf);
541 static void iam_leaf_split(struct iam_leaf *l, struct buffer_head **bh,
544 iam_leaf_ops(l)->split(l, bh, nr);
547 static inline int iam_leaf_empty(struct iam_leaf *l)
549 return iam_leaf_ops(l)->leaf_empty(l);
552 int iam_leaf_can_add(const struct iam_leaf *l,
553 const struct iam_key *k, const struct iam_rec *r)
555 return iam_leaf_ops(l)->can_add(l, k, r);
558 static int iam_txn_dirty(handle_t *handle,
559 struct iam_path *path, struct buffer_head *bh)
563 result = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
565 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
569 static int iam_txn_add(handle_t *handle,
570 struct iam_path *path, struct buffer_head *bh)
574 result = ldiskfs_journal_get_write_access(handle, bh);
576 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
580 /***********************************************************************/
581 /* iterator interface */
582 /***********************************************************************/
584 static enum iam_it_state it_state(const struct iam_iterator *it)
590 * Helper function returning scratch key.
592 static struct iam_container *iam_it_container(const struct iam_iterator *it)
594 return it->ii_path.ip_container;
597 static inline int it_keycmp(const struct iam_iterator *it,
598 const struct iam_key *k)
600 return iam_leaf_keycmp(&it->ii_path.ip_leaf, k);
603 static inline int it_keyeq(const struct iam_iterator *it,
604 const struct iam_key *k)
606 return iam_leaf_keyeq(&it->ii_path.ip_leaf, k);
609 static int it_ikeycmp(const struct iam_iterator *it, const struct iam_ikey *ik)
611 return iam_ikeycmp(it->ii_path.ip_container,
612 iam_leaf_ikey(&it->ii_path.ip_leaf,
613 iam_path_ikey(&it->ii_path, 0)), ik);
616 static inline int it_at_rec(const struct iam_iterator *it)
618 return !iam_leaf_at_end(&it->ii_path.ip_leaf);
621 static inline int it_before(const struct iam_iterator *it)
623 return it_state(it) == IAM_IT_SKEWED && it_at_rec(it);
627 * Helper wrapper around iam_it_get(): returns 0 (success) only when record
628 * with exactly the same key as asked is found.
630 static int iam_it_get_exact(struct iam_iterator *it, const struct iam_key *k)
634 result = iam_it_get(it, k);
637 else if (result == 0)
639 * Return -ENOENT if cursor is located above record with a key
640 * different from one specified, or in the empty leaf.
642 * XXX returning -ENOENT only works if iam_it_get() never
643 * returns -ENOENT as a legitimate error.
649 void iam_container_write_lock(struct iam_container *ic)
651 down_write(&ic->ic_sem);
654 void iam_container_write_unlock(struct iam_container *ic)
656 up_write(&ic->ic_sem);
659 void iam_container_read_lock(struct iam_container *ic)
661 down_read(&ic->ic_sem);
664 void iam_container_read_unlock(struct iam_container *ic)
666 up_read(&ic->ic_sem);
670 * Initialize iterator to IAM_IT_DETACHED state.
672 * postcondition: it_state(it) == IAM_IT_DETACHED
674 int iam_it_init(struct iam_iterator *it, struct iam_container *c, __u32 flags,
675 struct iam_path_descr *pd)
677 memset(it, 0, sizeof *it);
678 it->ii_flags = flags;
679 it->ii_state = IAM_IT_DETACHED;
680 iam_path_init(&it->ii_path, c, pd);
685 * Finalize iterator and release all resources.
687 * precondition: it_state(it) == IAM_IT_DETACHED
689 void iam_it_fini(struct iam_iterator *it)
691 assert_corr(it_state(it) == IAM_IT_DETACHED);
692 iam_path_fini(&it->ii_path);
696 * this locking primitives are used to protect parts
697 * of dir's htree. protection unit is block: leaf or index
699 static struct dynlock_handle *iam_lock_htree(struct iam_container *ic,
701 enum dynlock_type lt)
703 return dynlock_lock(&ic->ic_tree_lock, value, lt, GFP_NOFS);
706 static int iam_index_lock(struct iam_path *path, struct dynlock_handle **lh)
710 for (f = path->ip_frame; f >= path->ip_frames; --f, ++lh) {
712 *lh = iam_lock_htree(path->ip_container, f->curidx, DLT_READ);
720 * Fast check for frame consistency.
722 static int iam_check_fast(struct iam_path *path, struct iam_frame *frame)
724 struct iam_container *bag;
725 struct iam_entry *next;
726 struct iam_entry *last;
727 struct iam_entry *entries;
728 struct iam_entry *at;
730 bag = path->ip_container;
732 entries = frame->entries;
733 last = iam_entry_shift(path, entries, dx_get_count(entries) - 1);
735 if (unlikely(at > last))
738 if (unlikely(dx_get_block(path, at) != frame->leaf))
741 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, at),
742 path->ip_ikey_target) > 0))
745 next = iam_entry_shift(path, at, +1);
747 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, next),
748 path->ip_ikey_target) <= 0))
754 int dx_index_is_compat(struct iam_path *path)
756 return iam_path_descr(path) == NULL;
762 * search position of specified hash in index
766 static struct iam_entry *iam_find_position(struct iam_path *path,
767 struct iam_frame *frame)
774 count = dx_get_count(frame->entries);
775 assert_corr(count && count <= dx_get_limit(frame->entries));
776 p = iam_entry_shift(path, frame->entries,
777 dx_index_is_compat(path) ? 1 : 2);
778 q = iam_entry_shift(path, frame->entries, count - 1);
780 m = iam_entry_shift(path, p, iam_entry_diff(path, q, p) / 2);
781 if (iam_ikeycmp(path->ip_container, iam_ikey_at(path, m),
782 path->ip_ikey_target) > 0)
783 q = iam_entry_shift(path, m, -1);
785 p = iam_entry_shift(path, m, +1);
787 return iam_entry_shift(path, p, -1);
792 static iam_ptr_t iam_find_ptr(struct iam_path *path, struct iam_frame *frame)
794 return dx_get_block(path, iam_find_position(path, frame));
797 void iam_insert_key(struct iam_path *path, struct iam_frame *frame,
798 const struct iam_ikey *key, iam_ptr_t ptr)
800 struct iam_entry *entries = frame->entries;
801 struct iam_entry *new = iam_entry_shift(path, frame->at, +1);
802 int count = dx_get_count(entries);
805 * Unfortunately we cannot assert this, as this function is sometimes
806 * called by VFS under i_sem and without pdirops lock.
808 assert_corr(1 || iam_frame_is_locked(path, frame));
809 assert_corr(count < dx_get_limit(entries));
810 assert_corr(frame->at < iam_entry_shift(path, entries, count));
811 assert_inv(dx_node_check(path, frame));
813 memmove(iam_entry_shift(path, new, 1), new,
814 (char *)iam_entry_shift(path, entries, count) - (char *)new);
815 dx_set_ikey(path, new, key);
816 dx_set_block(path, new, ptr);
817 dx_set_count(entries, count + 1);
818 assert_inv(dx_node_check(path, frame));
821 void iam_insert_key_lock(struct iam_path *path, struct iam_frame *frame,
822 const struct iam_ikey *key, iam_ptr_t ptr)
824 iam_lock_bh(frame->bh);
825 iam_insert_key(path, frame, key, ptr);
826 iam_unlock_bh(frame->bh);
829 * returns 0 if path was unchanged, -EAGAIN otherwise.
831 static int iam_check_path(struct iam_path *path, struct iam_frame *frame)
835 iam_lock_bh(frame->bh);
836 equal = iam_check_fast(path, frame) == 0 ||
837 frame->leaf == iam_find_ptr(path, frame);
838 DX_DEVAL(iam_lock_stats.dls_bh_again += !equal);
839 iam_unlock_bh(frame->bh);
841 return equal ? 0 : -EAGAIN;
844 static int iam_lookup_try(struct iam_path *path)
850 struct iam_descr *param;
851 struct iam_frame *frame;
852 struct iam_container *c;
854 param = iam_path_descr(path);
855 c = path->ip_container;
857 ptr = param->id_ops->id_root_ptr(c);
858 for (frame = path->ip_frames, i = 0; i <= path->ip_indirect;
860 err = param->id_ops->id_node_read(c, (iam_ptr_t)ptr, NULL,
864 iam_lock_bh(frame->bh);
866 * node must be initialized under bh lock because concurrent
867 * creation procedure may change it and iam_lookup_try() will
868 * see obsolete tree height. -bzzz
873 if (LDISKFS_INVARIANT_ON) {
874 err = param->id_ops->id_node_check(path, frame);
879 err = param->id_ops->id_node_load(path, frame);
883 assert_inv(dx_node_check(path, frame));
885 * splitting may change root index block and move hash we're
886 * looking for into another index block so, we have to check
887 * this situation and repeat from begining if path got changed
891 err = iam_check_path(path, frame - 1);
896 frame->at = iam_find_position(path, frame);
898 frame->leaf = ptr = dx_get_block(path, frame->at);
900 iam_unlock_bh(frame->bh);
904 iam_unlock_bh(frame->bh);
905 path->ip_frame = --frame;
909 static int __iam_path_lookup(struct iam_path *path)
914 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++ i)
915 assert(path->ip_frames[i].bh == NULL);
918 err = iam_lookup_try(path);
922 } while (err == -EAGAIN);
928 * returns 0 if path was unchanged, -EAGAIN otherwise.
930 static int iam_check_full_path(struct iam_path *path, int search)
932 struct iam_frame *bottom;
933 struct iam_frame *scan;
939 for (bottom = path->ip_frames, i = 0;
940 i < DX_MAX_TREE_HEIGHT && bottom->bh != NULL; ++bottom, ++i) {
941 ; /* find last filled in frame */
945 * Lock frames, bottom to top.
947 for (scan = bottom - 1; scan >= path->ip_frames; --scan)
948 iam_lock_bh(scan->bh);
950 * Check them top to bottom.
953 for (scan = path->ip_frames; scan < bottom; ++scan) {
954 struct iam_entry *pos;
957 if (iam_check_fast(path, scan) == 0)
960 pos = iam_find_position(path, scan);
961 if (scan->leaf != dx_get_block(path, pos)) {
967 pos = iam_entry_shift(path, scan->entries,
968 dx_get_count(scan->entries) - 1);
969 if (scan->at > pos ||
970 scan->leaf != dx_get_block(path, scan->at)) {
978 * Unlock top to bottom.
980 for (scan = path->ip_frames; scan < bottom; ++scan)
981 iam_unlock_bh(scan->bh);
982 DX_DEVAL(iam_lock_stats.dls_bh_full_again += !!result);
990 * Performs path lookup and returns with found leaf (if any) locked by htree
993 static int iam_lookup_lock(struct iam_path *path,
994 struct dynlock_handle **dl, enum dynlock_type lt)
998 while ((result = __iam_path_lookup(path)) == 0) {
1000 *dl = iam_lock_htree(path->ip_container, path->ip_frame->leaf,
1003 iam_path_fini(path);
1007 do_corr(schedule());
1009 * while locking leaf we just found may get split so we need
1010 * to check this -bzzz
1012 if (iam_check_full_path(path, 1) == 0)
1014 iam_unlock_htree(path->ip_container, *dl);
1016 iam_path_fini(path);
1021 * Performs tree top-to-bottom traversal starting from root, and loads leaf
1024 static int iam_path_lookup(struct iam_path *path, int index)
1026 struct iam_leaf *leaf;
1029 leaf = &path->ip_leaf;
1030 result = iam_lookup_lock(path, &leaf->il_lock, DLT_WRITE);
1031 assert_inv(iam_path_check(path));
1032 do_corr(schedule());
1034 result = iam_leaf_load(path);
1036 do_corr(schedule());
1038 result = iam_leaf_ops(leaf)->
1039 ilookup(leaf, path->ip_ikey_target);
1041 result = iam_leaf_ops(leaf)->
1042 lookup(leaf, path->ip_key_target);
1043 do_corr(schedule());
1046 iam_leaf_unlock(leaf);
1052 * Common part of iam_it_{i,}get().
1054 static int __iam_it_get(struct iam_iterator *it, int index)
1057 assert_corr(it_state(it) == IAM_IT_DETACHED);
1059 result = iam_path_lookup(&it->ii_path, index);
1063 collision = result & IAM_LOOKUP_LAST;
1064 switch (result & ~IAM_LOOKUP_LAST) {
1065 case IAM_LOOKUP_EXACT:
1067 it->ii_state = IAM_IT_ATTACHED;
1071 it->ii_state = IAM_IT_ATTACHED;
1073 case IAM_LOOKUP_BEFORE:
1074 case IAM_LOOKUP_EMPTY:
1076 it->ii_state = IAM_IT_SKEWED;
1081 result |= collision;
1084 * See iam_it_get_exact() for explanation.
1086 assert_corr(result != -ENOENT);
1091 * Correct hash, but not the same key was found, iterate through hash
1092 * collision chain, looking for correct record.
1094 static int iam_it_collision(struct iam_iterator *it)
1098 assert(ergo(it_at_rec(it), !it_keyeq(it, it->ii_path.ip_key_target)));
1100 while ((result = iam_it_next(it)) == 0) {
1101 do_corr(schedule());
1102 if (it_ikeycmp(it, it->ii_path.ip_ikey_target) != 0)
1104 if (it_keyeq(it, it->ii_path.ip_key_target))
1111 * Attach iterator. After successful completion, @it points to record with
1112 * least key not larger than @k.
1114 * Return value: 0: positioned on existing record,
1115 * +ve: exact position found,
1118 * precondition: it_state(it) == IAM_IT_DETACHED
1119 * postcondition: ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1120 * it_keycmp(it, k) <= 0)
1122 int iam_it_get(struct iam_iterator *it, const struct iam_key *k)
1125 assert_corr(it_state(it) == IAM_IT_DETACHED);
1127 it->ii_path.ip_ikey_target = NULL;
1128 it->ii_path.ip_key_target = k;
1130 result = __iam_it_get(it, 0);
1132 if (result == IAM_LOOKUP_LAST) {
1133 result = iam_it_collision(it);
1137 result = __iam_it_get(it, 0);
1142 result &= ~IAM_LOOKUP_LAST;
1144 assert_corr(ergo(result > 0, it_keycmp(it, k) == 0));
1145 assert_corr(ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1146 it_keycmp(it, k) <= 0));
1151 * Attach iterator by index key.
1153 static int iam_it_iget(struct iam_iterator *it, const struct iam_ikey *k)
1155 assert_corr(it_state(it) == IAM_IT_DETACHED);
1157 it->ii_path.ip_ikey_target = k;
1158 return __iam_it_get(it, 1) & ~IAM_LOOKUP_LAST;
1162 * Attach iterator, and assure it points to the record (not skewed).
1164 * Return value: 0: positioned on existing record,
1165 * +ve: exact position found,
1168 * precondition: it_state(it) == IAM_IT_DETACHED &&
1169 * !(it->ii_flags&IAM_IT_WRITE)
1170 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED)
1172 int iam_it_get_at(struct iam_iterator *it, const struct iam_key *k)
1175 assert_corr(it_state(it) == IAM_IT_DETACHED &&
1176 !(it->ii_flags&IAM_IT_WRITE));
1177 result = iam_it_get(it, k);
1179 if (it_state(it) != IAM_IT_ATTACHED) {
1180 assert_corr(it_state(it) == IAM_IT_SKEWED);
1181 result = iam_it_next(it);
1184 assert_corr(ergo(result >= 0, it_state(it) == IAM_IT_ATTACHED));
1189 * Duplicates iterator.
1191 * postcondition: it_state(dst) == it_state(src) &&
1192 * iam_it_container(dst) == iam_it_container(src) &&
1193 * dst->ii_flags = src->ii_flags &&
1194 * ergo(it_state(src) == IAM_IT_ATTACHED,
1195 * iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1196 * iam_it_key_get(dst) == iam_it_key_get(src))
1198 void iam_it_dup(struct iam_iterator *dst, const struct iam_iterator *src)
1200 dst->ii_flags = src->ii_flags;
1201 dst->ii_state = src->ii_state;
1202 /* XXX not yet. iam_path_dup(&dst->ii_path, &src->ii_path); */
1204 * XXX: duplicate lock.
1206 assert_corr(it_state(dst) == it_state(src));
1207 assert_corr(iam_it_container(dst) == iam_it_container(src));
1208 assert_corr(dst->ii_flags = src->ii_flags);
1209 assert_corr(ergo(it_state(src) == IAM_IT_ATTACHED,
1210 iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1211 iam_it_key_get(dst) == iam_it_key_get(src)));
1216 * Detach iterator. Does nothing it detached state.
1218 * postcondition: it_state(it) == IAM_IT_DETACHED
1220 void iam_it_put(struct iam_iterator *it)
1222 if (it->ii_state != IAM_IT_DETACHED) {
1223 it->ii_state = IAM_IT_DETACHED;
1224 iam_leaf_fini(&it->ii_path.ip_leaf);
1228 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1229 struct iam_ikey *ikey);
1233 * This function increments the frame pointer to search the next leaf
1234 * block, and reads in the necessary intervening nodes if the search
1235 * should be necessary. Whether or not the search is necessary is
1236 * controlled by the hash parameter. If the hash value is even, then
1237 * the search is only continued if the next block starts with that
1238 * hash value. This is used if we are searching for a specific file.
1240 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1242 * This function returns 1 if the caller should continue to search,
1243 * or 0 if it should not. If there is an error reading one of the
1244 * index blocks, it will a negative error code.
1246 * If start_hash is non-null, it will be filled in with the starting
1247 * hash of the next page.
1249 static int iam_htree_advance(struct inode *dir, __u32 hash,
1250 struct iam_path *path, __u32 *start_hash,
1253 struct iam_frame *p;
1254 struct buffer_head *bh;
1255 int err, num_frames = 0;
1260 * Find the next leaf page by incrementing the frame pointer.
1261 * If we run out of entries in the interior node, loop around and
1262 * increment pointer in the parent node. When we break out of
1263 * this loop, num_frames indicates the number of interior
1264 * nodes need to be read.
1267 do_corr(schedule());
1272 p->at = iam_entry_shift(path, p->at, +1);
1273 if (p->at < iam_entry_shift(path, p->entries,
1274 dx_get_count(p->entries))) {
1275 p->leaf = dx_get_block(path, p->at);
1276 iam_unlock_bh(p->bh);
1279 iam_unlock_bh(p->bh);
1280 if (p == path->ip_frames)
1291 * If the hash is 1, then continue only if the next page has a
1292 * continuation hash of any value. This is used for readdir
1293 * handling. Otherwise, check to see if the hash matches the
1294 * desired contiuation hash. If it doesn't, return since
1295 * there's no point to read in the successive index pages.
1297 dx_get_ikey(path, p->at, (struct iam_ikey *)&bhash);
1299 *start_hash = bhash;
1300 if ((hash & 1) == 0) {
1301 if ((bhash & ~1) != hash)
1306 * If the hash is HASH_NB_ALWAYS, we always go to the next
1307 * block so no check is necessary
1309 while (num_frames--) {
1312 do_corr(schedule());
1314 idx = p->leaf = dx_get_block(path, p->at);
1315 iam_unlock_bh(p->bh);
1316 err = iam_path_descr(path)->id_ops->
1317 id_node_read(path->ip_container, idx, NULL, &bh);
1319 return err; /* Failure */
1322 assert_corr(p->bh != bh);
1324 p->entries = dx_node_get_entries(path, p);
1325 p->at = iam_entry_shift(path, p->entries, !compat);
1326 assert_corr(p->curidx != idx);
1329 assert_corr(p->leaf != dx_get_block(path, p->at));
1330 p->leaf = dx_get_block(path, p->at);
1331 iam_unlock_bh(p->bh);
1332 assert_inv(dx_node_check(path, p));
1338 static inline int iam_index_advance(struct iam_path *path)
1340 return iam_htree_advance(iam_path_obj(path), 0, path, NULL, 0);
1343 static void iam_unlock_array(struct iam_container *ic,
1344 struct dynlock_handle **lh)
1348 for (i = 0; i < DX_MAX_TREE_HEIGHT; ++i, ++lh) {
1350 iam_unlock_htree(ic, *lh);
1356 * Advance index part of @path to point to the next leaf. Returns 1 on
1357 * success, 0, when end of container was reached. Leaf node is locked.
1359 int iam_index_next(struct iam_container *c, struct iam_path *path)
1362 struct dynlock_handle *lh[DX_MAX_TREE_HEIGHT] = { NULL, };
1366 * Locking for iam_index_next()... is to be described.
1369 cursor = path->ip_frame->leaf;
1372 result = iam_index_lock(path, lh);
1373 do_corr(schedule());
1377 result = iam_check_full_path(path, 0);
1378 if (result == 0 && cursor == path->ip_frame->leaf) {
1379 result = iam_index_advance(path);
1381 assert_corr(result == 0 ||
1382 cursor != path->ip_frame->leaf);
1386 iam_unlock_array(c, lh);
1388 iam_path_release(path);
1389 do_corr(schedule());
1391 result = __iam_path_lookup(path);
1395 while (path->ip_frame->leaf != cursor) {
1396 do_corr(schedule());
1398 result = iam_index_lock(path, lh);
1399 do_corr(schedule());
1403 result = iam_check_full_path(path, 0);
1407 result = iam_index_advance(path);
1409 CERROR("cannot find cursor : %u\n",
1415 result = iam_check_full_path(path, 0);
1418 iam_unlock_array(c, lh);
1420 } while (result == -EAGAIN);
1424 iam_unlock_array(c, lh);
1429 * Move iterator one record right.
1431 * Return value: 0: success,
1432 * +1: end of container reached
1435 * precondition: (it_state(it) == IAM_IT_ATTACHED ||
1436 * it_state(it) == IAM_IT_SKEWED) && it->ii_flags&IAM_IT_MOVE
1437 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED) &&
1438 * ergo(result > 0, it_state(it) == IAM_IT_DETACHED)
1440 int iam_it_next(struct iam_iterator *it)
1443 struct iam_path *path;
1444 struct iam_leaf *leaf;
1445 do_corr(struct iam_ikey *ik_orig);
1447 /* assert_corr(it->ii_flags&IAM_IT_MOVE); */
1448 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1449 it_state(it) == IAM_IT_SKEWED);
1451 path = &it->ii_path;
1452 leaf = &path->ip_leaf;
1454 assert_corr(iam_leaf_is_locked(leaf));
1457 do_corr(ik_orig = it_at_rec(it) ?
1458 iam_it_ikey_get(it, iam_path_ikey(path, 2)) : NULL);
1459 if (it_before(it)) {
1460 assert_corr(!iam_leaf_at_end(leaf));
1461 it->ii_state = IAM_IT_ATTACHED;
1463 if (!iam_leaf_at_end(leaf))
1464 /* advance within leaf node */
1465 iam_leaf_next(leaf);
1467 * multiple iterations may be necessary due to empty leaves.
1469 while (result == 0 && iam_leaf_at_end(leaf)) {
1470 do_corr(schedule());
1471 /* advance index portion of the path */
1472 result = iam_index_next(iam_it_container(it), path);
1473 assert_corr(iam_leaf_is_locked(leaf));
1475 struct dynlock_handle *lh;
1476 lh = iam_lock_htree(iam_it_container(it),
1477 path->ip_frame->leaf,
1480 iam_leaf_fini(leaf);
1482 result = iam_leaf_load(path);
1484 iam_leaf_start(leaf);
1487 } else if (result == 0)
1488 /* end of container reached */
1494 it->ii_state = IAM_IT_ATTACHED;
1496 assert_corr(ergo(result == 0, it_state(it) == IAM_IT_ATTACHED));
1497 assert_corr(ergo(result > 0, it_state(it) == IAM_IT_DETACHED));
1498 assert_corr(ergo(result == 0 && ik_orig != NULL,
1499 it_ikeycmp(it, ik_orig) >= 0));
1504 * Return pointer to the record under iterator.
1506 * precondition: it_state(it) == IAM_IT_ATTACHED && it_at_rec(it)
1507 * postcondition: it_state(it) == IAM_IT_ATTACHED
1509 struct iam_rec *iam_it_rec_get(const struct iam_iterator *it)
1511 assert_corr(it_state(it) == IAM_IT_ATTACHED);
1512 assert_corr(it_at_rec(it));
1513 return iam_leaf_rec(&it->ii_path.ip_leaf);
1516 static void iam_it_reccpy(struct iam_iterator *it, const struct iam_rec *r)
1518 struct iam_leaf *folio;
1520 folio = &it->ii_path.ip_leaf;
1521 iam_leaf_ops(folio)->rec_set(folio, r);
1525 * Replace contents of record under iterator.
1527 * precondition: it_state(it) == IAM_IT_ATTACHED &&
1528 * it->ii_flags&IAM_IT_WRITE
1529 * postcondition: it_state(it) == IAM_IT_ATTACHED &&
1530 * ergo(result == 0, !memcmp(iam_it_rec_get(it), r, ...))
1532 int iam_it_rec_set(handle_t *h,
1533 struct iam_iterator *it, const struct iam_rec *r)
1536 struct iam_path *path;
1537 struct buffer_head *bh;
1539 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
1540 it->ii_flags&IAM_IT_WRITE);
1541 assert_corr(it_at_rec(it));
1543 path = &it->ii_path;
1544 bh = path->ip_leaf.il_bh;
1545 result = iam_txn_add(h, path, bh);
1547 iam_it_reccpy(it, r);
1548 result = iam_txn_dirty(h, path, bh);
1554 * Return pointer to the index key under iterator.
1556 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1557 * it_state(it) == IAM_IT_SKEWED
1559 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1560 struct iam_ikey *ikey)
1562 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1563 it_state(it) == IAM_IT_SKEWED);
1564 assert_corr(it_at_rec(it));
1565 return iam_leaf_ikey(&it->ii_path.ip_leaf, ikey);
1569 * Return pointer to the key under iterator.
1571 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1572 * it_state(it) == IAM_IT_SKEWED
1574 struct iam_key *iam_it_key_get(const struct iam_iterator *it)
1576 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1577 it_state(it) == IAM_IT_SKEWED);
1578 assert_corr(it_at_rec(it));
1579 return iam_leaf_key(&it->ii_path.ip_leaf);
1583 * Return size of key under iterator (in bytes)
1585 * precondition: it_state(it) == IAM_IT_ATTACHED ||
1586 * it_state(it) == IAM_IT_SKEWED
1588 int iam_it_key_size(const struct iam_iterator *it)
1590 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1591 it_state(it) == IAM_IT_SKEWED);
1592 assert_corr(it_at_rec(it));
1593 return iam_leaf_key_size(&it->ii_path.ip_leaf);
1596 static struct buffer_head *
1597 iam_new_node(handle_t *h, struct iam_container *c, iam_ptr_t *b, int *e)
1599 struct inode *inode = c->ic_object;
1600 struct buffer_head *bh = NULL;
1601 struct iam_idle_head *head;
1602 struct buffer_head *idle;
1606 if (c->ic_idle_bh == NULL)
1609 mutex_lock(&c->ic_idle_mutex);
1610 if (unlikely(c->ic_idle_bh == NULL)) {
1611 mutex_unlock(&c->ic_idle_mutex);
1615 head = (struct iam_idle_head *)(c->ic_idle_bh->b_data);
1616 count = le16_to_cpu(head->iih_count);
1618 *e = ldiskfs_journal_get_write_access(h, c->ic_idle_bh);
1623 *b = le32_to_cpu(head->iih_blks[count]);
1624 head->iih_count = cpu_to_le16(count);
1625 *e = ldiskfs_handle_dirty_metadata(h, inode, c->ic_idle_bh);
1629 mutex_unlock(&c->ic_idle_mutex);
1630 bh = __ldiskfs_bread(NULL, inode, *b, 0);
1631 if (IS_ERR_OR_NULL(bh)) {
1641 /* The block itself which contains the iam_idle_head is
1642 * also an idle block, and can be used as the new node. */
1643 idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
1644 c->ic_descr->id_root_gap +
1645 sizeof(struct dx_countlimit));
1646 *e = ldiskfs_journal_get_write_access(h, c->ic_root_bh);
1650 *b = le32_to_cpu(*idle_blocks);
1651 iam_lock_bh(c->ic_root_bh);
1652 *idle_blocks = head->iih_next;
1653 iam_unlock_bh(c->ic_root_bh);
1654 *e = ldiskfs_handle_dirty_metadata(h, inode, c->ic_root_bh);
1656 iam_lock_bh(c->ic_root_bh);
1657 *idle_blocks = cpu_to_le32(*b);
1658 iam_unlock_bh(c->ic_root_bh);
1663 idle = iam_load_idle_blocks(c, le32_to_cpu(*idle_blocks));
1664 if (idle != NULL && IS_ERR(idle)) {
1666 c->ic_idle_bh = NULL;
1671 c->ic_idle_bh = idle;
1672 mutex_unlock(&c->ic_idle_mutex);
1675 /* get write access for the found buffer head */
1676 *e = ldiskfs_journal_get_write_access(h, bh);
1680 ldiskfs_std_error(inode->i_sb, *e);
1682 /* Clear the reused node as new node does. */
1683 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1684 set_buffer_uptodate(bh);
1689 bh = osd_ldiskfs_append(h, inode, b);
1698 mutex_unlock(&c->ic_idle_mutex);
1699 ldiskfs_std_error(inode->i_sb, *e);
1704 * Insertion of new record. Interaction with jbd during non-trivial case (when
1705 * split happens) is as following:
1707 * - new leaf node is involved into transaction by iam_new_node();
1709 * - old leaf node is involved into transaction by iam_add_rec();
1711 * - leaf where insertion point ends in, is marked dirty by iam_add_rec();
1713 * - leaf without insertion point is marked dirty (as @new_leaf) by
1716 * - split index nodes are involved into transaction and marked dirty by
1717 * split_index_node().
1719 * - "safe" index node, which is no split, but where new pointer is inserted
1720 * is involved into transaction and marked dirty by split_index_node().
1722 * - index node where pointer to new leaf is inserted is involved into
1723 * transaction by split_index_node() and marked dirty by iam_add_rec().
1725 * - inode is marked dirty by iam_add_rec().
1729 static int iam_new_leaf(handle_t *handle, struct iam_leaf *leaf)
1733 struct buffer_head *new_leaf;
1734 struct buffer_head *old_leaf;
1735 struct iam_container *c;
1737 struct iam_path *path;
1739 c = iam_leaf_container(leaf);
1740 path = leaf->il_path;
1743 new_leaf = iam_new_node(handle, c, &blknr, &err);
1744 do_corr(schedule());
1745 if (new_leaf != NULL) {
1746 struct dynlock_handle *lh;
1748 lh = iam_lock_htree(c, blknr, DLT_WRITE);
1749 do_corr(schedule());
1751 iam_leaf_ops(leaf)->init_new(c, new_leaf);
1752 do_corr(schedule());
1753 old_leaf = leaf->il_bh;
1754 iam_leaf_split(leaf, &new_leaf, blknr);
1755 if (old_leaf != leaf->il_bh) {
1757 * Switched to the new leaf.
1759 iam_leaf_unlock(leaf);
1761 path->ip_frame->leaf = blknr;
1763 iam_unlock_htree(path->ip_container, lh);
1764 do_corr(schedule());
1765 err = iam_txn_dirty(handle, path, new_leaf);
1767 err = ldiskfs_mark_inode_dirty(handle, obj);
1768 do_corr(schedule());
1773 assert_inv(iam_path_check(iam_leaf_path(leaf)));
1777 static inline void dx_set_limit(struct iam_entry *entries, unsigned value)
1779 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
1782 static int iam_shift_entries(struct iam_path *path,
1783 struct iam_frame *frame, unsigned count,
1784 struct iam_entry *entries, struct iam_entry *entries2,
1791 struct iam_frame *parent = frame - 1;
1792 struct iam_ikey *pivot = iam_path_ikey(path, 3);
1794 delta = dx_index_is_compat(path) ? 0 : +1;
1796 count1 = count/2 + delta;
1797 count2 = count - count1;
1798 dx_get_ikey(path, iam_entry_shift(path, entries, count1), pivot);
1800 dxtrace(printk("Split index %d/%d\n", count1, count2));
1802 memcpy((char *) iam_entry_shift(path, entries2, delta),
1803 (char *) iam_entry_shift(path, entries, count1),
1804 count2 * iam_entry_size(path));
1806 dx_set_count(entries2, count2 + delta);
1807 dx_set_limit(entries2, dx_node_limit(path));
1810 * NOTE: very subtle piece of code competing dx_probe() may find 2nd
1811 * level index in root index, then we insert new index here and set
1812 * new count in that 2nd level index. so, dx_probe() may see 2nd level
1813 * index w/o hash it looks for. the solution is to check root index
1814 * after we locked just founded 2nd level index -bzzz
1816 iam_insert_key_lock(path, parent, pivot, newblock);
1819 * now old and new 2nd level index blocks contain all pointers, so
1820 * dx_probe() may find it in the both. it's OK -bzzz
1822 iam_lock_bh(frame->bh);
1823 dx_set_count(entries, count1);
1824 iam_unlock_bh(frame->bh);
1827 * now old 2nd level index block points to first half of leafs. it's
1828 * importand that dx_probe() must check root index block for changes
1829 * under dx_lock_bh(frame->bh) -bzzz
1836 int split_index_node(handle_t *handle, struct iam_path *path,
1837 struct dynlock_handle **lh)
1840 struct iam_entry *entries; /* old block contents */
1841 struct iam_entry *entries2; /* new block contents */
1842 struct iam_frame *frame, *safe;
1843 struct buffer_head *bh_new[DX_MAX_TREE_HEIGHT] = {NULL};
1844 u32 newblock[DX_MAX_TREE_HEIGHT] = {0};
1845 struct dynlock_handle *lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1846 struct dynlock_handle *new_lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1847 struct inode *dir = iam_path_obj(path);
1848 struct iam_descr *descr;
1852 descr = iam_path_descr(path);
1854 * Algorithm below depends on this.
1856 assert_corr(dx_root_limit(path) < dx_node_limit(path));
1858 frame = path->ip_frame;
1859 entries = frame->entries;
1862 * Tall-tree handling: we might have to split multiple index blocks
1863 * all the way up to tree root. Tricky point here is error handling:
1864 * to avoid complicated undo/rollback we
1866 * - first allocate all necessary blocks
1868 * - insert pointers into them atomically.
1872 * Locking: leaf is already locked. htree-locks are acquired on all
1873 * index nodes that require split bottom-to-top, on the "safe" node,
1874 * and on all new nodes
1877 dxtrace(printk("using %u of %u node entries\n",
1878 dx_get_count(entries), dx_get_limit(entries)));
1880 /* What levels need split? */
1881 for (nr_splet = 0; frame >= path->ip_frames &&
1882 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1883 --frame, ++nr_splet) {
1884 do_corr(schedule());
1885 if (nr_splet == DX_MAX_TREE_HEIGHT) {
1887 CWARN(dir->i_sb, __FUNCTION__,
1888 "Directory index full!\n");
1898 * Lock all nodes, bottom to top.
1900 for (frame = path->ip_frame, i = nr_splet; i >= 0; --i, --frame) {
1901 do_corr(schedule());
1902 lock[i] = iam_lock_htree(path->ip_container, frame->curidx,
1904 if (lock[i] == NULL) {
1911 * Check for concurrent index modification.
1913 err = iam_check_full_path(path, 1);
1917 * And check that the same number of nodes is to be split.
1919 for (i = 0, frame = path->ip_frame; frame >= path->ip_frames &&
1920 dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1924 if (i != nr_splet) {
1929 /* Go back down, allocating blocks, locking them, and adding into
1931 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1932 bh_new[i] = iam_new_node(handle, path->ip_container,
1933 &newblock[i], &err);
1934 do_corr(schedule());
1936 descr->id_ops->id_node_init(path->ip_container,
1939 new_lock[i] = iam_lock_htree(path->ip_container, newblock[i],
1941 if (new_lock[i] == NULL) {
1945 do_corr(schedule());
1946 BUFFER_TRACE(frame->bh, "get_write_access");
1947 err = ldiskfs_journal_get_write_access(handle, frame->bh);
1951 /* Add "safe" node to transaction too */
1952 if (safe + 1 != path->ip_frames) {
1953 do_corr(schedule());
1954 err = ldiskfs_journal_get_write_access(handle, safe->bh);
1959 /* Go through nodes once more, inserting pointers */
1960 for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1963 struct buffer_head *bh2;
1964 struct buffer_head *bh;
1966 entries = frame->entries;
1967 count = dx_get_count(entries);
1968 idx = iam_entry_diff(path, frame->at, entries);
1971 entries2 = dx_get_entries(path, bh2->b_data, 0);
1974 if (frame == path->ip_frames) {
1975 /* splitting root node. Tricky point:
1977 * In the "normal" B-tree we'd split root *and* add
1978 * new root to the tree with pointers to the old root
1979 * and its sibling (thus introducing two new nodes).
1981 * In htree it's enough to add one node, because
1982 * capacity of the root node is smaller than that of
1985 struct iam_frame *frames;
1986 struct iam_entry *next;
1988 assert_corr(i == 0);
1990 do_corr(schedule());
1992 frames = path->ip_frames;
1993 memcpy((char *) entries2, (char *) entries,
1994 count * iam_entry_size(path));
1995 dx_set_limit(entries2, dx_node_limit(path));
1998 iam_lock_bh(frame->bh);
1999 next = descr->id_ops->id_root_inc(path->ip_container,
2001 dx_set_block(path, next, newblock[0]);
2002 iam_unlock_bh(frame->bh);
2004 do_corr(schedule());
2005 /* Shift frames in the path */
2006 memmove(frames + 2, frames + 1,
2007 (sizeof path->ip_frames) - 2 * sizeof frames[0]);
2008 /* Add new access path frame */
2009 frames[1].at = iam_entry_shift(path, entries2, idx);
2010 frames[1].entries = entries = entries2;
2012 assert_inv(dx_node_check(path, frame));
2015 assert_inv(dx_node_check(path, frame));
2016 bh_new[0] = NULL; /* buffer head is "consumed" */
2017 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh2);
2020 do_corr(schedule());
2022 /* splitting non-root index node. */
2023 struct iam_frame *parent = frame - 1;
2025 do_corr(schedule());
2026 count = iam_shift_entries(path, frame, count,
2027 entries, entries2, newblock[i]);
2028 /* Which index block gets the new entry? */
2030 int d = dx_index_is_compat(path) ? 0 : +1;
2032 frame->at = iam_entry_shift(path, entries2,
2034 frame->entries = entries = entries2;
2035 frame->curidx = newblock[i];
2036 swap(frame->bh, bh2);
2037 assert_corr(lock[i + 1] != NULL);
2038 assert_corr(new_lock[i] != NULL);
2039 swap(lock[i + 1], new_lock[i]);
2041 parent->at = iam_entry_shift(path,
2044 assert_inv(dx_node_check(path, frame));
2045 assert_inv(dx_node_check(path, parent));
2046 dxtrace(dx_show_index ("node", frame->entries));
2047 dxtrace(dx_show_index ("node",
2048 ((struct dx_node *) bh2->b_data)->entries));
2049 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh2);
2052 do_corr(schedule());
2053 err = ldiskfs_handle_dirty_metadata(handle, NULL,
2058 do_corr(schedule());
2059 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
2064 * This function was called to make insertion of new leaf
2065 * possible. Check that it fulfilled its obligations.
2067 assert_corr(dx_get_count(path->ip_frame->entries) <
2068 dx_get_limit(path->ip_frame->entries));
2069 assert_corr(lock[nr_splet] != NULL);
2070 *lh = lock[nr_splet];
2071 lock[nr_splet] = NULL;
2074 * Log ->i_size modification.
2076 err = ldiskfs_mark_inode_dirty(handle, dir);
2082 ldiskfs_std_error(dir->i_sb, err);
2085 iam_unlock_array(path->ip_container, lock);
2086 iam_unlock_array(path->ip_container, new_lock);
2088 assert_corr(err || iam_frame_is_locked(path, path->ip_frame));
2090 do_corr(schedule());
2091 for (i = 0; i < ARRAY_SIZE(bh_new); ++i) {
2092 if (bh_new[i] != NULL)
2098 static int iam_add_rec(handle_t *handle, struct iam_iterator *it,
2099 struct iam_path *path,
2100 const struct iam_key *k, const struct iam_rec *r)
2103 struct iam_leaf *leaf;
2105 leaf = &path->ip_leaf;
2106 assert_inv(iam_path_check(path));
2107 err = iam_txn_add(handle, path, leaf->il_bh);
2109 do_corr(schedule());
2110 if (!iam_leaf_can_add(leaf, k, r)) {
2111 struct dynlock_handle *lh = NULL;
2114 assert_corr(lh == NULL);
2115 do_corr(schedule());
2116 err = split_index_node(handle, path, &lh);
2117 if (err == -EAGAIN) {
2118 assert_corr(lh == NULL);
2120 iam_path_fini(path);
2121 it->ii_state = IAM_IT_DETACHED;
2123 do_corr(schedule());
2124 err = iam_it_get_exact(it, k);
2126 err = +1; /* repeat split */
2131 assert_inv(iam_path_check(path));
2133 assert_corr(lh != NULL);
2134 do_corr(schedule());
2135 err = iam_new_leaf(handle, leaf);
2137 err = iam_txn_dirty(handle, path,
2138 path->ip_frame->bh);
2140 iam_unlock_htree(path->ip_container, lh);
2141 do_corr(schedule());
2144 iam_leaf_rec_add(leaf, k, r);
2145 err = iam_txn_dirty(handle, path, leaf->il_bh);
2148 assert_inv(iam_path_check(path));
2153 * Insert new record with key @k and contents from @r, shifting records to the
2154 * right. On success, iterator is positioned on the newly inserted record.
2156 * precondition: it->ii_flags&IAM_IT_WRITE &&
2157 * (it_state(it) == IAM_IT_ATTACHED ||
2158 * it_state(it) == IAM_IT_SKEWED) &&
2159 * ergo(it_state(it) == IAM_IT_ATTACHED,
2160 * it_keycmp(it, k) <= 0) &&
2161 * ergo(it_before(it), it_keycmp(it, k) > 0));
2162 * postcondition: ergo(result == 0,
2163 * it_state(it) == IAM_IT_ATTACHED &&
2164 * it_keycmp(it, k) == 0 &&
2165 * !memcmp(iam_it_rec_get(it), r, ...))
2167 int iam_it_rec_insert(handle_t *h, struct iam_iterator *it,
2168 const struct iam_key *k, const struct iam_rec *r)
2171 struct iam_path *path;
2173 path = &it->ii_path;
2175 assert_corr(it->ii_flags&IAM_IT_WRITE);
2176 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2177 it_state(it) == IAM_IT_SKEWED);
2178 assert_corr(ergo(it_state(it) == IAM_IT_ATTACHED,
2179 it_keycmp(it, k) <= 0));
2180 assert_corr(ergo(it_before(it), it_keycmp(it, k) > 0));
2181 result = iam_add_rec(h, it, path, k, r);
2183 it->ii_state = IAM_IT_ATTACHED;
2184 assert_corr(ergo(result == 0,
2185 it_state(it) == IAM_IT_ATTACHED &&
2186 it_keycmp(it, k) == 0));
2190 static inline int iam_idle_blocks_limit(struct inode *inode)
2192 return (inode->i_sb->s_blocksize - sizeof(struct iam_idle_head)) >> 2;
2196 * If the leaf cannnot be recycled, we will lose one block for reusing.
2197 * It is not a serious issue because it almost the same of non-recycle.
2199 static iam_ptr_t iam_index_shrink(handle_t *h, struct iam_path *p,
2200 struct iam_leaf *l, struct buffer_head **bh)
2202 struct iam_container *c = p->ip_container;
2203 struct inode *inode = c->ic_object;
2204 struct iam_frame *frame = p->ip_frame;
2205 struct iam_entry *entries;
2206 struct iam_entry *pos;
2207 struct dynlock_handle *lh;
2211 if (c->ic_idle_failed)
2214 if (unlikely(frame == NULL))
2217 if (!iam_leaf_empty(l))
2220 lh = iam_lock_htree(c, frame->curidx, DLT_WRITE);
2222 CWARN("%.16s: No memory to recycle idle blocks\n",
2223 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name);
2227 rc = iam_txn_add(h, p, frame->bh);
2229 iam_unlock_htree(c, lh);
2233 iam_lock_bh(frame->bh);
2234 entries = frame->entries;
2235 count = dx_get_count(entries);
2236 /* NOT shrink the last entry in the index node, which can be reused
2237 * directly by next new node. */
2239 iam_unlock_bh(frame->bh);
2240 iam_unlock_htree(c, lh);
2244 pos = iam_find_position(p, frame);
2245 /* There may be some new leaf nodes have been added or empty leaf nodes
2246 * have been shrinked during my delete operation.
2248 * If the empty leaf is not under current index node because the index
2249 * node has been split, then just skip the empty leaf, which is rare. */
2250 if (unlikely(frame->leaf != dx_get_block(p, pos))) {
2251 iam_unlock_bh(frame->bh);
2252 iam_unlock_htree(c, lh);
2257 if (frame->at < iam_entry_shift(p, entries, count - 1)) {
2258 struct iam_entry *n = iam_entry_shift(p, frame->at, 1);
2260 memmove(frame->at, n,
2261 (char *)iam_entry_shift(p, entries, count) - (char *)n);
2262 frame->at_shifted = 1;
2264 dx_set_count(entries, count - 1);
2265 iam_unlock_bh(frame->bh);
2266 rc = iam_txn_dirty(h, p, frame->bh);
2267 iam_unlock_htree(c, lh);
2277 iam_install_idle_blocks(handle_t *h, struct iam_path *p, struct buffer_head *bh,
2278 __u32 *idle_blocks, iam_ptr_t blk)
2280 struct iam_container *c = p->ip_container;
2281 struct buffer_head *old = c->ic_idle_bh;
2282 struct iam_idle_head *head;
2285 head = (struct iam_idle_head *)(bh->b_data);
2286 head->iih_magic = cpu_to_le16(IAM_IDLE_HEADER_MAGIC);
2287 head->iih_count = 0;
2288 head->iih_next = *idle_blocks;
2289 /* The bh already get_write_accessed. */
2290 rc = iam_txn_dirty(h, p, bh);
2294 rc = iam_txn_add(h, p, c->ic_root_bh);
2298 iam_lock_bh(c->ic_root_bh);
2299 *idle_blocks = cpu_to_le32(blk);
2300 iam_unlock_bh(c->ic_root_bh);
2301 rc = iam_txn_dirty(h, p, c->ic_root_bh);
2303 /* NOT release old before new assigned. */
2308 iam_lock_bh(c->ic_root_bh);
2309 *idle_blocks = head->iih_next;
2310 iam_unlock_bh(c->ic_root_bh);
2316 * If the leaf cannnot be recycled, we will lose one block for reusing.
2317 * It is not a serious issue because it almost the same of non-recycle.
2319 static void iam_recycle_leaf(handle_t *h, struct iam_path *p,
2320 struct buffer_head *bh, iam_ptr_t blk)
2322 struct iam_container *c = p->ip_container;
2323 struct inode *inode = c->ic_object;
2324 struct iam_idle_head *head;
2329 mutex_lock(&c->ic_idle_mutex);
2330 if (unlikely(c->ic_idle_failed)) {
2335 idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
2336 c->ic_descr->id_root_gap +
2337 sizeof(struct dx_countlimit));
2338 /* It is the first idle block. */
2339 if (c->ic_idle_bh == NULL) {
2340 rc = iam_install_idle_blocks(h, p, bh, idle_blocks, blk);
2344 head = (struct iam_idle_head *)(c->ic_idle_bh->b_data);
2345 count = le16_to_cpu(head->iih_count);
2346 /* Current ic_idle_bh is full, to be replaced by the leaf. */
2347 if (count == iam_idle_blocks_limit(inode)) {
2348 rc = iam_install_idle_blocks(h, p, bh, idle_blocks, blk);
2352 /* Just add to ic_idle_bh. */
2353 rc = iam_txn_add(h, p, c->ic_idle_bh);
2357 head->iih_blks[count] = cpu_to_le32(blk);
2358 head->iih_count = cpu_to_le16(count + 1);
2359 rc = iam_txn_dirty(h, p, c->ic_idle_bh);
2362 mutex_unlock(&c->ic_idle_mutex);
2364 CWARN("%.16s: idle blocks failed, will lose the blk %u\n",
2365 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name, blk);
2369 * Delete record under iterator.
2371 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2372 * it->ii_flags&IAM_IT_WRITE &&
2374 * postcondition: it_state(it) == IAM_IT_ATTACHED ||
2375 * it_state(it) == IAM_IT_DETACHED
2377 int iam_it_rec_delete(handle_t *h, struct iam_iterator *it)
2380 struct iam_leaf *leaf;
2381 struct iam_path *path;
2383 assert_corr(it_state(it) == IAM_IT_ATTACHED &&
2384 it->ii_flags&IAM_IT_WRITE);
2385 assert_corr(it_at_rec(it));
2387 path = &it->ii_path;
2388 leaf = &path->ip_leaf;
2390 assert_inv(iam_path_check(path));
2392 result = iam_txn_add(h, path, leaf->il_bh);
2394 * no compaction for now.
2397 iam_rec_del(leaf, it->ii_flags&IAM_IT_MOVE);
2398 result = iam_txn_dirty(h, path, leaf->il_bh);
2399 if (result == 0 && iam_leaf_at_end(leaf)) {
2400 struct buffer_head *bh = NULL;
2403 blk = iam_index_shrink(h, path, leaf, &bh);
2404 if (it->ii_flags & IAM_IT_MOVE) {
2405 result = iam_it_next(it);
2411 iam_recycle_leaf(h, path, bh, blk);
2416 assert_inv(iam_path_check(path));
2417 assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2418 it_state(it) == IAM_IT_DETACHED);
2423 * Convert iterator to cookie.
2425 * precondition: it_state(it) == IAM_IT_ATTACHED &&
2426 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2427 * postcondition: it_state(it) == IAM_IT_ATTACHED
2429 iam_pos_t iam_it_store(const struct iam_iterator *it)
2433 assert_corr(it_state(it) == IAM_IT_ATTACHED);
2434 assert_corr(it_at_rec(it));
2435 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <=
2439 return *(iam_pos_t *)iam_it_ikey_get(it, (void *)&result);
2443 * Restore iterator from cookie.
2445 * precondition: it_state(it) == IAM_IT_DETACHED && it->ii_flags&IAM_IT_MOVE &&
2446 * iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2447 * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED &&
2448 * iam_it_store(it) == pos)
2450 int iam_it_load(struct iam_iterator *it, iam_pos_t pos)
2452 assert_corr(it_state(it) == IAM_IT_DETACHED &&
2453 it->ii_flags&IAM_IT_MOVE);
2454 assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <= sizeof pos);
2455 return iam_it_iget(it, (struct iam_ikey *)&pos);
2458 /***********************************************************************/
2460 /***********************************************************************/
2462 static inline int ptr_inside(void *base, size_t size, void *ptr)
2464 return (base <= ptr) && (ptr < base + size);
2467 static int iam_frame_invariant(struct iam_frame *f)
2471 f->bh->b_data != NULL &&
2472 ptr_inside(f->bh->b_data, f->bh->b_size, f->entries) &&
2473 ptr_inside(f->bh->b_data, f->bh->b_size, f->at) &&
2474 f->entries <= f->at);
2477 static int iam_leaf_invariant(struct iam_leaf *l)
2481 l->il_bh->b_data != NULL &&
2482 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_entries) &&
2483 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_at) &&
2484 l->il_entries <= l->il_at;
2487 static int iam_path_invariant(struct iam_path *p)
2491 if (p->ip_container == NULL ||
2492 p->ip_indirect < 0 || p->ip_indirect > DX_MAX_TREE_HEIGHT - 1 ||
2493 p->ip_frame != p->ip_frames + p->ip_indirect ||
2494 !iam_leaf_invariant(&p->ip_leaf))
2496 for (i = 0; i < ARRAY_SIZE(p->ip_frames); ++i) {
2497 if (i <= p->ip_indirect) {
2498 if (!iam_frame_invariant(&p->ip_frames[i]))
2505 int iam_it_invariant(struct iam_iterator *it)
2508 (it->ii_state == IAM_IT_DETACHED ||
2509 it->ii_state == IAM_IT_ATTACHED ||
2510 it->ii_state == IAM_IT_SKEWED) &&
2511 !(it->ii_flags & ~(IAM_IT_MOVE | IAM_IT_WRITE)) &&
2512 ergo(it->ii_state == IAM_IT_ATTACHED ||
2513 it->ii_state == IAM_IT_SKEWED,
2514 iam_path_invariant(&it->ii_path) &&
2515 equi(it_at_rec(it), it->ii_state == IAM_IT_SKEWED));
2519 * Search container @c for record with key @k. If record is found, its data
2520 * are moved into @r.
2522 * Return values: 0: found, -ENOENT: not-found, -ve: error
2524 int iam_lookup(struct iam_container *c, const struct iam_key *k,
2525 struct iam_rec *r, struct iam_path_descr *pd)
2527 struct iam_iterator it;
2530 iam_it_init(&it, c, 0, pd);
2532 result = iam_it_get_exact(&it, k);
2535 * record with required key found, copy it into user buffer
2537 iam_reccpy(&it.ii_path.ip_leaf, r);
2544 * Insert new record @r with key @k into container @c (within context of
2547 * Return values: 0: success, -ve: error, including -EEXIST when record with
2548 * given key is already present.
2550 * postcondition: ergo(result == 0 || result == -EEXIST,
2551 * iam_lookup(c, k, r2) > 0;
2553 int iam_insert(handle_t *h, struct iam_container *c, const struct iam_key *k,
2554 const struct iam_rec *r, struct iam_path_descr *pd)
2556 struct iam_iterator it;
2559 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2561 result = iam_it_get_exact(&it, k);
2562 if (result == -ENOENT)
2563 result = iam_it_rec_insert(h, &it, k, r);
2564 else if (result == 0)
2572 * Update record with the key @k in container @c (within context of
2573 * transaction @h), new record is given by @r.
2575 * Return values: +1: skip because of the same rec value, 0: success,
2576 * -ve: error, including -ENOENT if no record with the given key found.
2578 int iam_update(handle_t *h, struct iam_container *c, const struct iam_key *k,
2579 const struct iam_rec *r, struct iam_path_descr *pd)
2581 struct iam_iterator it;
2582 struct iam_leaf *folio;
2585 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2587 result = iam_it_get_exact(&it, k);
2589 folio = &it.ii_path.ip_leaf;
2590 result = iam_leaf_ops(folio)->rec_eq(folio, r);
2592 iam_it_rec_set(h, &it, r);
2602 * Delete existing record with key @k.
2604 * Return values: 0: success, -ENOENT: not-found, -ve: other error.
2606 * postcondition: ergo(result == 0 || result == -ENOENT,
2607 * !iam_lookup(c, k, *));
2609 int iam_delete(handle_t *h, struct iam_container *c, const struct iam_key *k,
2610 struct iam_path_descr *pd)
2612 struct iam_iterator it;
2615 iam_it_init(&it, c, IAM_IT_WRITE, pd);
2617 result = iam_it_get_exact(&it, k);
2619 iam_it_rec_delete(h, &it);
2625 int iam_root_limit(int rootgap, int blocksize, int size)
2630 limit = (blocksize - rootgap) / size;
2631 nlimit = blocksize / size;
2632 if (limit == nlimit)