Whamcloud - gitweb
LU-5396 all: use NULL instead of 0
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_iam.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
18  * copy of GPLv2].
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * iam.c
37  * Top-level entry points into iam module
38  *
39  * Author: Wang Di <wangdi@clusterfs.com>
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 /*
44  * iam: big theory statement.
45  *
46  * iam (Index Access Module) is a module providing abstraction of persistent
47  * transactional container on top of generalized ldiskfs htree.
48  *
49  * iam supports:
50  *
51  *     - key, pointer, and record size specifiable per container.
52  *
53  *     - trees taller than 2 index levels.
54  *
55  *     - read/write to existing ldiskfs htree directories as iam containers.
56  *
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.
60  *
61  * iam does not work with keys directly, instead it calls user-supplied key
62  * comparison function (->dpo_keycmp()).
63  *
64  * Pointers are (currently) interpreted as logical offsets (measured in
65  * blocksful) within underlying flat file on top of which iam tree lives.
66  *
67  * On-disk format:
68  *
69  * iam mostly tries to reuse existing htree formats.
70  *
71  * Format of index node:
72  *
73  * +-----+-------+-------+-------+------+-------+------------+
74  * |     | count |       |       |      |       |            |
75  * | gap |   /   | entry | entry | .... | entry | free space |
76  * |     | limit |       |       |      |       |            |
77  * +-----+-------+-------+-------+------+-------+------------+
78  *
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).
86  *
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
90  *                     count.
91  *
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.
96  *
97  *       free space    portion of node new entries are added to
98  *
99  * Entries in index node are sorted by their key value.
100  *
101  * Format of a leaf node is not specified. Generic iam code accesses leaf
102  * nodes through ->id_leaf methods in struct iam_descr.
103  *
104  * The IAM root block is a special node, which contains the IAM descriptor.
105  * It is on disk format:
106  *
107  * +---------+-------+--------+---------+-------+------+-------+------------+
108  * |IAM desc | count |  idle  |         |       |      |       |            |
109  * |(fix/var)|   /   | blocks | padding | entry | .... | entry | free space |
110  * |         | limit |        |         |       |      |       |            |
111  * +---------+-------+--------+---------+-------+------+-------+------------+
112  *
113  * The padding length is calculated with the parameters in the IAM descriptor.
114  *
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.
122  *
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
126  * the entry.
127  *
128  * The idle blocks pool is organized as a series of tables, and each table
129  * can be described as following (on-disk format):
130  *
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  * +---------+---------+---------+---------+------+---------+-------+
136  *
137  * The logic blk# for the first table is stored in the root node "idle_blocks".
138  *
139  */
140
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"
151
152 #include "xattr.h"
153 #include "acl.h"
154
155 /*
156  * List of all registered formats.
157  *
158  * No locking. Callers synchronize.
159  */
160 static struct list_head iam_formats = LIST_HEAD_INIT(iam_formats);
161
162 void iam_format_register(struct iam_format *fmt)
163 {
164         list_add(&fmt->if_linkage, &iam_formats);
165 }
166 EXPORT_SYMBOL(iam_format_register);
167
168 static struct buffer_head *
169 iam_load_idle_blocks(struct iam_container *c, iam_ptr_t blk)
170 {
171         struct inode *inode = c->ic_object;
172         struct iam_idle_head *head;
173         struct buffer_head *bh;
174         int err;
175
176         LASSERT(mutex_is_locked(&c->ic_idle_mutex));
177
178         if (blk == 0)
179                 return NULL;
180
181         bh = ldiskfs_bread(NULL, inode, blk, 0, &err);
182         if (bh == NULL) {
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;
186                 return ERR_PTR(err);
187         }
188
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));
194                 brelse(bh);
195                 c->ic_idle_failed = 1;
196                 return ERR_PTR(-EBADF);
197         }
198
199         return bh;
200 }
201
202 /*
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.
205  */
206 static int iam_format_guess(struct iam_container *c)
207 {
208         int result;
209         struct iam_format *fmt;
210
211         /*
212          * XXX temporary initialization hook.
213          */
214         {
215                 static int initialized = 0;
216
217                 if (!initialized) {
218                         iam_lvar_format_init();
219                         iam_lfix_format_init();
220                         initialized = 1;
221                 }
222         }
223
224         result = -ENOENT;
225         list_for_each_entry(fmt, &iam_formats, if_linkage) {
226                 result = fmt->if_guess(c);
227                 if (result == 0)
228                         break;
229         }
230
231         if (result == 0) {
232                 struct buffer_head *bh;
233                 __u32 *idle_blocks;
234
235                 LASSERT(c->ic_root_bh != NULL);
236
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);
244                 else
245                         c->ic_idle_bh = bh;
246                 mutex_unlock(&c->ic_idle_mutex);
247         }
248
249         return result;
250 }
251
252 /*
253  * Initialize container @c.
254  */
255 int iam_container_init(struct iam_container *c,
256                        struct iam_descr *descr, struct inode *inode)
257 {
258         memset(c, 0, sizeof *c);
259         c->ic_descr  = descr;
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);
264         return 0;
265 }
266 EXPORT_SYMBOL(iam_container_init);
267
268 /*
269  * Determine container format.
270  */
271 int iam_container_setup(struct iam_container *c)
272 {
273         return iam_format_guess(c);
274 }
275 EXPORT_SYMBOL(iam_container_setup);
276
277 /*
278  * Finalize container @c, release all resources.
279  */
280 void iam_container_fini(struct iam_container *c)
281 {
282         brelse(c->ic_idle_bh);
283         c->ic_idle_bh = NULL;
284         brelse(c->ic_root_bh);
285         c->ic_root_bh = NULL;
286 }
287 EXPORT_SYMBOL(iam_container_fini);
288
289 void iam_path_init(struct iam_path *path, struct iam_container *c,
290                    struct iam_path_descr *pd)
291 {
292         memset(path, 0, sizeof *path);
293         path->ip_container = c;
294         path->ip_frame = path->ip_frames;
295         path->ip_data = pd;
296         path->ip_leaf.il_path = path;
297 }
298
299 static void iam_leaf_fini(struct iam_leaf *leaf);
300
301 void iam_path_release(struct iam_path *path)
302 {
303         int i;
304
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;
310                 }
311         }
312 }
313
314 void iam_path_fini(struct iam_path *path)
315 {
316         iam_leaf_fini(&path->ip_leaf);
317         iam_path_release(path);
318 }
319
320
321 void iam_path_compat_init(struct iam_path_compat *path, struct inode *inode)
322 {
323         int i;
324
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];
329
330         iam_path_init(&path->ipc_path, &path->ipc_container, &path->ipc_descr);
331 }
332
333 void iam_path_compat_fini(struct iam_path_compat *path)
334 {
335         iam_path_fini(&path->ipc_path);
336 }
337
338 /*
339  * Helper function initializing iam_path_descr and its key scratch area.
340  */
341 struct iam_path_descr *iam_ipd_alloc(void *area, int keysize)
342 {
343         struct iam_path_descr *ipd;
344         void *karea;
345         int i;
346
347         ipd = area;
348         karea = ipd + 1;
349         for (i = 0; i < ARRAY_SIZE(ipd->ipd_key_scratch); ++i, karea += keysize)
350                 ipd->ipd_key_scratch[i] = karea;
351         return ipd;
352 }
353 EXPORT_SYMBOL(iam_ipd_alloc);
354
355 void iam_ipd_free(struct iam_path_descr *ipd)
356 {
357 }
358 EXPORT_SYMBOL(iam_ipd_free);
359
360 int iam_node_read(struct iam_container *c, iam_ptr_t ptr,
361                   handle_t *h, struct buffer_head **bh)
362 {
363         int result = 0;
364
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.
369          */
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);
373                 *bh = c->ic_root_bh;
374                 return 0;
375         }
376
377         *bh = ldiskfs_bread(h, c->ic_object, (int)ptr, 0, &result);
378         if (*bh == NULL)
379                 result = -EIO;
380         return result;
381 }
382
383 /*
384  * Return pointer to current leaf record. Pointer is valid while corresponding
385  * leaf node is locked and pinned.
386  */
387 static struct iam_rec *iam_leaf_rec(const struct iam_leaf *leaf)
388 {
389         return iam_leaf_ops(leaf)->rec(leaf);
390 }
391
392 /*
393  * Return pointer to the current leaf key. This function returns pointer to
394  * the key stored in node.
395  *
396  * Caller should assume that returned pointer is only valid while leaf node is
397  * pinned and locked.
398  */
399 static struct iam_key *iam_leaf_key(const struct iam_leaf *leaf)
400 {
401         return iam_leaf_ops(leaf)->key(leaf);
402 }
403
404 static int iam_leaf_key_size(const struct iam_leaf *leaf)
405 {
406         return iam_leaf_ops(leaf)->key_size(leaf);
407 }
408
409 static struct iam_ikey *iam_leaf_ikey(const struct iam_leaf *leaf,
410                                       struct iam_ikey *key)
411 {
412         return iam_leaf_ops(leaf)->ikey(leaf, key);
413 }
414
415 static int iam_leaf_keycmp(const struct iam_leaf *leaf,
416                            const struct iam_key *key)
417 {
418         return iam_leaf_ops(leaf)->key_cmp(leaf, key);
419 }
420
421 static int iam_leaf_keyeq(const struct iam_leaf *leaf,
422                           const struct iam_key *key)
423 {
424         return iam_leaf_ops(leaf)->key_eq(leaf, key);
425 }
426
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);
430
431 static int iam_path_check(struct iam_path *p)
432 {
433         int i;
434         int result;
435         struct iam_frame *f;
436         struct iam_descr *param;
437
438         result = 1;
439         param = iam_path_descr(p);
440         for (i = 0; result && i < ARRAY_SIZE(p->ip_frames); ++i) {
441                 f = &p->ip_frames[i];
442                 if (f->bh != NULL) {
443                         result = dx_node_check(p, f);
444                         if (result)
445                                 result = !param->id_ops->id_node_check(p, f);
446                 }
447         }
448         if (result && p->ip_leaf.il_bh != NULL)
449                 result = iam_leaf_check(&p->ip_leaf);
450         if (result == 0) {
451                 ldiskfs_std_error(iam_path_obj(p)->i_sb, result);
452         }
453         return result;
454 }
455 #endif
456
457 static int iam_leaf_load(struct iam_path *path)
458 {
459         iam_ptr_t block;
460         int err;
461         struct iam_container *c;
462         struct buffer_head   *bh;
463         struct iam_leaf      *leaf;
464         struct iam_descr     *descr;
465
466         c     = path->ip_container;
467         leaf  = &path->ip_leaf;
468         descr = iam_path_descr(path);
469         block = path->ip_frame->leaf;
470         if (block == 0) {
471                 /* XXX bug 11027 */
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);
477         }
478         err = descr->id_ops->id_node_read(c, block, NULL, &bh);
479         if (err == 0) {
480                 leaf->il_bh = bh;
481                 leaf->il_curidx = block;
482                 err = iam_leaf_ops(leaf)->init(leaf);
483                 assert_inv(ergo(err == 0, iam_leaf_check(leaf)));
484         }
485         return err;
486 }
487
488 static void iam_unlock_htree(struct iam_container *ic,
489                              struct dynlock_handle *lh)
490 {
491         if (lh != NULL)
492                 dynlock_unlock(&ic->ic_tree_lock, lh);
493 }
494
495
496 static void iam_leaf_unlock(struct iam_leaf *leaf)
497 {
498         if (leaf->il_lock != NULL) {
499                 iam_unlock_htree(iam_leaf_container(leaf),
500                                  leaf->il_lock);
501                 do_corr(schedule());
502                 leaf->il_lock = NULL;
503         }
504 }
505
506 static void iam_leaf_fini(struct iam_leaf *leaf)
507 {
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);
512                 if (leaf->il_bh) {
513                         brelse(leaf->il_bh);
514                         leaf->il_bh = NULL;
515                         leaf->il_curidx = 0;
516                 }
517         }
518 }
519
520 static void iam_leaf_start(struct iam_leaf *folio)
521 {
522         iam_leaf_ops(folio)->start(folio);
523 }
524
525 void iam_leaf_next(struct iam_leaf *folio)
526 {
527         iam_leaf_ops(folio)->next(folio);
528 }
529
530 static void iam_leaf_rec_add(struct iam_leaf *leaf, const struct iam_key *key,
531                              const struct iam_rec *rec)
532 {
533         iam_leaf_ops(leaf)->rec_add(leaf, key, rec);
534 }
535
536 static void iam_rec_del(struct iam_leaf *leaf, int shift)
537 {
538         iam_leaf_ops(leaf)->rec_del(leaf, shift);
539 }
540
541 int iam_leaf_at_end(const struct iam_leaf *leaf)
542 {
543         return iam_leaf_ops(leaf)->at_end(leaf);
544 }
545
546 static void iam_leaf_split(struct iam_leaf *l, struct buffer_head **bh,
547                            iam_ptr_t nr)
548 {
549         iam_leaf_ops(l)->split(l, bh, nr);
550 }
551
552 static inline int iam_leaf_empty(struct iam_leaf *l)
553 {
554         return iam_leaf_ops(l)->leaf_empty(l);
555 }
556
557 int iam_leaf_can_add(const struct iam_leaf *l,
558                      const struct iam_key *k, const struct iam_rec *r)
559 {
560         return iam_leaf_ops(l)->can_add(l, k, r);
561 }
562
563 #if LDISKFS_INVARIANT_ON
564 static int iam_leaf_check(struct iam_leaf *leaf)
565 {
566         return 1;
567 #if 0
568         struct iam_lentry    *orig;
569         struct iam_path      *path;
570         struct iam_container *bag;
571         struct iam_ikey       *k0;
572         struct iam_ikey       *k1;
573         int result;
574         int first;
575
576         orig = leaf->il_at;
577         path = iam_leaf_path(leaf);
578         bag  = iam_leaf_container(leaf);
579
580         result = iam_leaf_ops(leaf)->init(leaf);
581         if (result != 0)
582                 return result;
583
584         first = 1;
585         iam_leaf_start(leaf);
586         k0 = iam_path_ikey(path, 0);
587         k1 = iam_path_ikey(path, 1);
588         while (!iam_leaf_at_end(leaf)) {
589                 iam_ikeycpy(bag, k0, k1);
590                 iam_ikeycpy(bag, k1, iam_leaf_ikey(leaf, k1));
591                 if (!first && iam_ikeycmp(bag, k0, k1) > 0) {
592                         return 0;
593                 }
594                 first = 0;
595                 iam_leaf_next(leaf);
596         }
597         leaf->il_at = orig;
598         return 1;
599 #endif
600 }
601 #endif
602
603 static int iam_txn_dirty(handle_t *handle,
604                          struct iam_path *path, struct buffer_head *bh)
605 {
606         int result;
607
608         result = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
609         if (result != 0)
610                 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
611         return result;
612 }
613
614 static int iam_txn_add(handle_t *handle,
615                        struct iam_path *path, struct buffer_head *bh)
616 {
617         int result;
618
619         result = ldiskfs_journal_get_write_access(handle, bh);
620         if (result != 0)
621                 ldiskfs_std_error(iam_path_obj(path)->i_sb, result);
622         return result;
623 }
624
625 /***********************************************************************/
626 /* iterator interface                                                  */
627 /***********************************************************************/
628
629 static enum iam_it_state it_state(const struct iam_iterator *it)
630 {
631         return it->ii_state;
632 }
633
634 /*
635  * Helper function returning scratch key.
636  */
637 static struct iam_container *iam_it_container(const struct iam_iterator *it)
638 {
639         return it->ii_path.ip_container;
640 }
641
642 static inline int it_keycmp(const struct iam_iterator *it,
643                             const struct iam_key *k)
644 {
645         return iam_leaf_keycmp(&it->ii_path.ip_leaf, k);
646 }
647
648 static inline int it_keyeq(const struct iam_iterator *it,
649                            const struct iam_key *k)
650 {
651         return iam_leaf_keyeq(&it->ii_path.ip_leaf, k);
652 }
653
654 static int it_ikeycmp(const struct iam_iterator *it, const struct iam_ikey *ik)
655 {
656         return iam_ikeycmp(it->ii_path.ip_container,
657                            iam_leaf_ikey(&it->ii_path.ip_leaf,
658                                          iam_path_ikey(&it->ii_path, 0)), ik);
659 }
660
661 static inline int it_at_rec(const struct iam_iterator *it)
662 {
663         return !iam_leaf_at_end(&it->ii_path.ip_leaf);
664 }
665
666 static inline int it_before(const struct iam_iterator *it)
667 {
668         return it_state(it) == IAM_IT_SKEWED && it_at_rec(it);
669 }
670
671 /*
672  * Helper wrapper around iam_it_get(): returns 0 (success) only when record
673  * with exactly the same key as asked is found.
674  */
675 static int iam_it_get_exact(struct iam_iterator *it, const struct iam_key *k)
676 {
677         int result;
678
679         result = iam_it_get(it, k);
680         if (result > 0)
681                 result = 0;
682         else if (result == 0)
683                 /*
684                  * Return -ENOENT if cursor is located above record with a key
685                  * different from one specified, or in the empty leaf.
686                  *
687                  * XXX returning -ENOENT only works if iam_it_get() never
688                  * returns -ENOENT as a legitimate error.
689                  */
690                 result = -ENOENT;
691         return result;
692 }
693
694 void iam_container_write_lock(struct iam_container *ic)
695 {
696         down_write(&ic->ic_sem);
697 }
698
699 void iam_container_write_unlock(struct iam_container *ic)
700 {
701         up_write(&ic->ic_sem);
702 }
703
704 void iam_container_read_lock(struct iam_container *ic)
705 {
706         down_read(&ic->ic_sem);
707 }
708
709 void iam_container_read_unlock(struct iam_container *ic)
710 {
711         up_read(&ic->ic_sem);
712 }
713
714 /*
715  * Initialize iterator to IAM_IT_DETACHED state.
716  *
717  * postcondition: it_state(it) == IAM_IT_DETACHED
718  */
719 int  iam_it_init(struct iam_iterator *it, struct iam_container *c, __u32 flags,
720                  struct iam_path_descr *pd)
721 {
722         memset(it, 0, sizeof *it);
723         it->ii_flags  = flags;
724         it->ii_state  = IAM_IT_DETACHED;
725         iam_path_init(&it->ii_path, c, pd);
726         return 0;
727 }
728 EXPORT_SYMBOL(iam_it_init);
729
730 /*
731  * Finalize iterator and release all resources.
732  *
733  * precondition: it_state(it) == IAM_IT_DETACHED
734  */
735 void iam_it_fini(struct iam_iterator *it)
736 {
737         assert_corr(it_state(it) == IAM_IT_DETACHED);
738         iam_path_fini(&it->ii_path);
739 }
740 EXPORT_SYMBOL(iam_it_fini);
741
742 /*
743  * this locking primitives are used to protect parts
744  * of dir's htree. protection unit is block: leaf or index
745  */
746 static struct dynlock_handle *iam_lock_htree(struct iam_container *ic,
747                                              unsigned long value,
748                                              enum dynlock_type lt)
749 {
750         return dynlock_lock(&ic->ic_tree_lock, value, lt, GFP_NOFS);
751 }
752
753 static int iam_index_lock(struct iam_path *path, struct dynlock_handle **lh)
754 {
755         struct iam_frame *f;
756
757         for (f = path->ip_frame; f >= path->ip_frames; --f, ++lh) {
758                 do_corr(schedule());
759                 *lh = iam_lock_htree(path->ip_container, f->curidx, DLT_READ);
760                 if (*lh == NULL)
761                         return -ENOMEM;
762         }
763         return 0;
764 }
765
766 /*
767  * Fast check for frame consistency.
768  */
769 static int iam_check_fast(struct iam_path *path, struct iam_frame *frame)
770 {
771         struct iam_container *bag;
772         struct iam_entry *next;
773         struct iam_entry *last;
774         struct iam_entry *entries;
775         struct iam_entry *at;
776
777         bag     = path->ip_container;
778         at      = frame->at;
779         entries = frame->entries;
780         last    = iam_entry_shift(path, entries, dx_get_count(entries) - 1);
781
782         if (unlikely(at > last))
783                 return -EAGAIN;
784
785         if (unlikely(dx_get_block(path, at) != frame->leaf))
786                 return -EAGAIN;
787
788         if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, at),
789                                  path->ip_ikey_target) > 0))
790                 return -EAGAIN;
791
792         next = iam_entry_shift(path, at, +1);
793         if (next <= last) {
794                 if (unlikely(iam_ikeycmp(bag, iam_ikey_at(path, next),
795                                          path->ip_ikey_target) <= 0))
796                         return -EAGAIN;
797         }
798         return 0;
799 }
800
801 int dx_index_is_compat(struct iam_path *path)
802 {
803         return iam_path_descr(path) == NULL;
804 }
805
806 /*
807  * dx_find_position
808  *
809  * search position of specified hash in index
810  *
811  */
812
813 static struct iam_entry *iam_find_position(struct iam_path *path,
814                                            struct iam_frame *frame)
815 {
816         int count;
817         struct iam_entry *p;
818         struct iam_entry *q;
819         struct iam_entry *m;
820
821         count = dx_get_count(frame->entries);
822         assert_corr(count && count <= dx_get_limit(frame->entries));
823         p = iam_entry_shift(path, frame->entries,
824                             dx_index_is_compat(path) ? 1 : 2);
825         q = iam_entry_shift(path, frame->entries, count - 1);
826         while (p <= q) {
827                 m = iam_entry_shift(path, p, iam_entry_diff(path, q, p) / 2);
828                 if (iam_ikeycmp(path->ip_container, iam_ikey_at(path, m),
829                                 path->ip_ikey_target) > 0)
830                         q = iam_entry_shift(path, m, -1);
831                 else
832                         p = iam_entry_shift(path, m, +1);
833         }
834         return iam_entry_shift(path, p, -1);
835 }
836
837
838
839 static iam_ptr_t iam_find_ptr(struct iam_path *path, struct iam_frame *frame)
840 {
841         return dx_get_block(path, iam_find_position(path, frame));
842 }
843
844 void iam_insert_key(struct iam_path *path, struct iam_frame *frame,
845                     const struct iam_ikey *key, iam_ptr_t ptr)
846 {
847         struct iam_entry *entries = frame->entries;
848         struct iam_entry *new = iam_entry_shift(path, frame->at, +1);
849         int count = dx_get_count(entries);
850
851         /*
852          * Unfortunately we cannot assert this, as this function is sometimes
853          * called by VFS under i_sem and without pdirops lock.
854          */
855         assert_corr(1 || iam_frame_is_locked(path, frame));
856         assert_corr(count < dx_get_limit(entries));
857         assert_corr(frame->at < iam_entry_shift(path, entries, count));
858         assert_inv(dx_node_check(path, frame));
859
860         memmove(iam_entry_shift(path, new, 1), new,
861                 (char *)iam_entry_shift(path, entries, count) - (char *)new);
862         dx_set_ikey(path, new, key);
863         dx_set_block(path, new, ptr);
864         dx_set_count(entries, count + 1);
865         assert_inv(dx_node_check(path, frame));
866 }
867
868 void iam_insert_key_lock(struct iam_path *path, struct iam_frame *frame,
869                          const struct iam_ikey *key, iam_ptr_t ptr)
870 {
871         iam_lock_bh(frame->bh);
872         iam_insert_key(path, frame, key, ptr);
873         iam_unlock_bh(frame->bh);
874 }
875 /*
876  * returns 0 if path was unchanged, -EAGAIN otherwise.
877  */
878 static int iam_check_path(struct iam_path *path, struct iam_frame *frame)
879 {
880         int equal;
881
882         iam_lock_bh(frame->bh);
883         equal = iam_check_fast(path, frame) == 0 ||
884                 frame->leaf == iam_find_ptr(path, frame);
885         DX_DEVAL(iam_lock_stats.dls_bh_again += !equal);
886         iam_unlock_bh(frame->bh);
887
888         return equal ? 0 : -EAGAIN;
889 }
890
891 static int iam_lookup_try(struct iam_path *path)
892 {
893         u32 ptr;
894         int err = 0;
895         int i;
896
897         struct iam_descr *param;
898         struct iam_frame *frame;
899         struct iam_container *c;
900
901         param = iam_path_descr(path);
902         c = path->ip_container;
903
904         ptr = param->id_ops->id_root_ptr(c);
905         for (frame = path->ip_frames, i = 0; i <= path->ip_indirect;
906              ++frame, ++i) {
907                 err = param->id_ops->id_node_read(c, (iam_ptr_t)ptr, NULL,
908                                                   &frame->bh);
909                 do_corr(schedule());
910
911                 iam_lock_bh(frame->bh);
912                 /*
913                  * node must be initialized under bh lock because concurrent
914                  * creation procedure may change it and iam_lookup_try() will
915                  * see obsolete tree height. -bzzz
916                  */
917                 if (err != 0)
918                         break;
919
920                 if (LDISKFS_INVARIANT_ON) {
921                         err = param->id_ops->id_node_check(path, frame);
922                         if (err != 0)
923                                 break;
924                 }
925
926                 err = param->id_ops->id_node_load(path, frame);
927                 if (err != 0)
928                         break;
929
930                 assert_inv(dx_node_check(path, frame));
931                 /*
932                  * splitting may change root index block and move hash we're
933                  * looking for into another index block so, we have to check
934                  * this situation and repeat from begining if path got changed
935                  * -bzzz
936                  */
937                 if (i > 0) {
938                         err = iam_check_path(path, frame - 1);
939                         if (err != 0)
940                                 break;
941                 }
942
943                 frame->at = iam_find_position(path, frame);
944                 frame->curidx = ptr;
945                 frame->leaf = ptr = dx_get_block(path, frame->at);
946
947                 iam_unlock_bh(frame->bh);
948                 do_corr(schedule());
949         }
950         if (err != 0)
951                 iam_unlock_bh(frame->bh);
952         path->ip_frame = --frame;
953         return err;
954 }
955
956 static int __iam_path_lookup(struct iam_path *path)
957 {
958         int err;
959         int i;
960
961         for (i = 0; i < DX_MAX_TREE_HEIGHT; ++ i)
962                 assert(path->ip_frames[i].bh == NULL);
963
964         do {
965                 err = iam_lookup_try(path);
966                 do_corr(schedule());
967                 if (err != 0)
968                         iam_path_fini(path);
969         } while (err == -EAGAIN);
970
971         return err;
972 }
973
974 /*
975  * returns 0 if path was unchanged, -EAGAIN otherwise.
976  */
977 static int iam_check_full_path(struct iam_path *path, int search)
978 {
979         struct iam_frame *bottom;
980         struct iam_frame *scan;
981         int i;
982         int result;
983
984         do_corr(schedule());
985
986         for (bottom = path->ip_frames, i = 0;
987              i < DX_MAX_TREE_HEIGHT && bottom->bh != NULL; ++bottom, ++i) {
988                 ; /* find last filled in frame */
989         }
990
991         /*
992          * Lock frames, bottom to top.
993          */
994         for (scan = bottom - 1; scan >= path->ip_frames; --scan)
995                 iam_lock_bh(scan->bh);
996         /*
997          * Check them top to bottom.
998          */
999         result = 0;
1000         for (scan = path->ip_frames; scan < bottom; ++scan) {
1001                 struct iam_entry *pos;
1002
1003                 if (search) {
1004                         if (iam_check_fast(path, scan) == 0)
1005                                 continue;
1006
1007                         pos = iam_find_position(path, scan);
1008                         if (scan->leaf != dx_get_block(path, pos)) {
1009                                 result = -EAGAIN;
1010                                 break;
1011                         }
1012                         scan->at = pos;
1013                 } else {
1014                         pos = iam_entry_shift(path, scan->entries,
1015                                               dx_get_count(scan->entries) - 1);
1016                         if (scan->at > pos ||
1017                             scan->leaf != dx_get_block(path, scan->at)) {
1018                                 result = -EAGAIN;
1019                                 break;
1020                         }
1021                 }
1022         }
1023
1024         /*
1025          * Unlock top to bottom.
1026          */
1027         for (scan = path->ip_frames; scan < bottom; ++scan)
1028                 iam_unlock_bh(scan->bh);
1029         DX_DEVAL(iam_lock_stats.dls_bh_full_again += !!result);
1030         do_corr(schedule());
1031
1032         return result;
1033 }
1034
1035
1036 /*
1037  * Performs path lookup and returns with found leaf (if any) locked by htree
1038  * lock.
1039  */
1040 static int iam_lookup_lock(struct iam_path *path,
1041                            struct dynlock_handle **dl, enum dynlock_type lt)
1042 {
1043         int result;
1044
1045         while ((result = __iam_path_lookup(path)) == 0) {
1046                 do_corr(schedule());
1047                 *dl = iam_lock_htree(path->ip_container, path->ip_frame->leaf,
1048                                      lt);
1049                 if (*dl == NULL) {
1050                         iam_path_fini(path);
1051                         result = -ENOMEM;
1052                         break;
1053                 }
1054                 do_corr(schedule());
1055                 /*
1056                  * while locking leaf we just found may get split so we need
1057                  * to check this -bzzz
1058                  */
1059                 if (iam_check_full_path(path, 1) == 0)
1060                         break;
1061                 iam_unlock_htree(path->ip_container, *dl);
1062                 *dl = NULL;
1063                 iam_path_fini(path);
1064         }
1065         return result;
1066 }
1067 /*
1068  * Performs tree top-to-bottom traversal starting from root, and loads leaf
1069  * node.
1070  */
1071 static int iam_path_lookup(struct iam_path *path, int index)
1072 {
1073         struct iam_container *c;
1074         struct iam_leaf  *leaf;
1075         int result;
1076
1077         c = path->ip_container;
1078         leaf = &path->ip_leaf;
1079         result = iam_lookup_lock(path, &leaf->il_lock, DLT_WRITE);
1080         assert_inv(iam_path_check(path));
1081         do_corr(schedule());
1082         if (result == 0) {
1083                 result = iam_leaf_load(path);
1084                 assert_inv(ergo(result == 0, iam_leaf_check(leaf)));
1085                 if (result == 0) {
1086                         do_corr(schedule());
1087                         if (index)
1088                                 result = iam_leaf_ops(leaf)->
1089                                         ilookup(leaf, path->ip_ikey_target);
1090                         else
1091                                 result = iam_leaf_ops(leaf)->
1092                                         lookup(leaf, path->ip_key_target);
1093                         do_corr(schedule());
1094                 }
1095                 if (result < 0)
1096                         iam_leaf_unlock(leaf);
1097         }
1098         return result;
1099 }
1100
1101 /*
1102  * Common part of iam_it_{i,}get().
1103  */
1104 static int __iam_it_get(struct iam_iterator *it, int index)
1105 {
1106         int result;
1107         assert_corr(it_state(it) == IAM_IT_DETACHED);
1108
1109         result = iam_path_lookup(&it->ii_path, index);
1110         if (result >= 0) {
1111                 int collision;
1112
1113                 collision = result & IAM_LOOKUP_LAST;
1114                 switch (result & ~IAM_LOOKUP_LAST) {
1115                 case IAM_LOOKUP_EXACT:
1116                         result = +1;
1117                         it->ii_state = IAM_IT_ATTACHED;
1118                         break;
1119                 case IAM_LOOKUP_OK:
1120                         result = 0;
1121                         it->ii_state = IAM_IT_ATTACHED;
1122                         break;
1123                 case IAM_LOOKUP_BEFORE:
1124                 case IAM_LOOKUP_EMPTY:
1125                         result = 0;
1126                         it->ii_state = IAM_IT_SKEWED;
1127                         break;
1128                 default:
1129                         assert(0);
1130                 }
1131                 result |= collision;
1132         }
1133         /*
1134          * See iam_it_get_exact() for explanation.
1135          */
1136         assert_corr(result != -ENOENT);
1137         return result;
1138 }
1139
1140 /*
1141  * Correct hash, but not the same key was found, iterate through hash
1142  * collision chain, looking for correct record.
1143  */
1144 static int iam_it_collision(struct iam_iterator *it)
1145 {
1146         int result;
1147
1148         assert(ergo(it_at_rec(it), !it_keyeq(it, it->ii_path.ip_key_target)));
1149
1150         while ((result = iam_it_next(it)) == 0) {
1151                 do_corr(schedule());
1152                 if (it_ikeycmp(it, it->ii_path.ip_ikey_target) != 0)
1153                         return -ENOENT;
1154                 if (it_keyeq(it, it->ii_path.ip_key_target))
1155                         return 0;
1156         }
1157         return result;
1158 }
1159
1160 /*
1161  * Attach iterator. After successful completion, @it points to record with
1162  * least key not larger than @k.
1163  *
1164  * Return value: 0: positioned on existing record,
1165  *             +ve: exact position found,
1166  *             -ve: error.
1167  *
1168  * precondition:  it_state(it) == IAM_IT_DETACHED
1169  * postcondition: ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1170  *                     it_keycmp(it, k) <= 0)
1171  */
1172 int iam_it_get(struct iam_iterator *it, const struct iam_key *k)
1173 {
1174         int result;
1175         assert_corr(it_state(it) == IAM_IT_DETACHED);
1176
1177         it->ii_path.ip_ikey_target = NULL;
1178         it->ii_path.ip_key_target  = k;
1179
1180         result = __iam_it_get(it, 0);
1181
1182         if (result == IAM_LOOKUP_LAST) {
1183                 result = iam_it_collision(it);
1184                 if (result != 0) {
1185                         iam_it_put(it);
1186                         iam_it_fini(it);
1187                         result = __iam_it_get(it, 0);
1188                 } else
1189                         result = +1;
1190         }
1191         if (result > 0)
1192                 result &= ~IAM_LOOKUP_LAST;
1193
1194         assert_corr(ergo(result > 0, it_keycmp(it, k) == 0));
1195         assert_corr(ergo(result == 0 && it_state(it) == IAM_IT_ATTACHED,
1196                          it_keycmp(it, k) <= 0));
1197         return result;
1198 }
1199 EXPORT_SYMBOL(iam_it_get);
1200
1201 /*
1202  * Attach iterator by index key.
1203  */
1204 static int iam_it_iget(struct iam_iterator *it, const struct iam_ikey *k)
1205 {
1206         assert_corr(it_state(it) == IAM_IT_DETACHED);
1207
1208         it->ii_path.ip_ikey_target = k;
1209         return __iam_it_get(it, 1) & ~IAM_LOOKUP_LAST;
1210 }
1211
1212 /*
1213  * Attach iterator, and assure it points to the record (not skewed).
1214  *
1215  * Return value: 0: positioned on existing record,
1216  *             +ve: exact position found,
1217  *             -ve: error.
1218  *
1219  * precondition:  it_state(it) == IAM_IT_DETACHED &&
1220  *                !(it->ii_flags&IAM_IT_WRITE)
1221  * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED)
1222  */
1223 int iam_it_get_at(struct iam_iterator *it, const struct iam_key *k)
1224 {
1225         int result;
1226         assert_corr(it_state(it) == IAM_IT_DETACHED &&
1227                     !(it->ii_flags&IAM_IT_WRITE));
1228         result = iam_it_get(it, k);
1229         if (result == 0) {
1230                 if (it_state(it) != IAM_IT_ATTACHED) {
1231                         assert_corr(it_state(it) == IAM_IT_SKEWED);
1232                         result = iam_it_next(it);
1233                 }
1234         }
1235         assert_corr(ergo(result >= 0, it_state(it) == IAM_IT_ATTACHED));
1236         return result;
1237 }
1238 EXPORT_SYMBOL(iam_it_get_at);
1239
1240 /*
1241  * Duplicates iterator.
1242  *
1243  * postcondition: it_state(dst) == it_state(src) &&
1244  *                iam_it_container(dst) == iam_it_container(src) &&
1245  *                dst->ii_flags = src->ii_flags &&
1246  *                ergo(it_state(src) == IAM_IT_ATTACHED,
1247  *                     iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1248  *                     iam_it_key_get(dst) == iam_it_key_get(src))
1249  */
1250 void iam_it_dup(struct iam_iterator *dst, const struct iam_iterator *src)
1251 {
1252         dst->ii_flags     = src->ii_flags;
1253         dst->ii_state     = src->ii_state;
1254         /* XXX not yet. iam_path_dup(&dst->ii_path, &src->ii_path); */
1255         /*
1256          * XXX: duplicate lock.
1257          */
1258         assert_corr(it_state(dst) == it_state(src));
1259         assert_corr(iam_it_container(dst) == iam_it_container(src));
1260         assert_corr(dst->ii_flags = src->ii_flags);
1261         assert_corr(ergo(it_state(src) == IAM_IT_ATTACHED,
1262                     iam_it_rec_get(dst) == iam_it_rec_get(src) &&
1263                     iam_it_key_get(dst) == iam_it_key_get(src)));
1264
1265 }
1266
1267 /*
1268  * Detach iterator. Does nothing it detached state.
1269  *
1270  * postcondition: it_state(it) == IAM_IT_DETACHED
1271  */
1272 void iam_it_put(struct iam_iterator *it)
1273 {
1274         if (it->ii_state != IAM_IT_DETACHED) {
1275                 it->ii_state = IAM_IT_DETACHED;
1276                 iam_leaf_fini(&it->ii_path.ip_leaf);
1277         }
1278 }
1279 EXPORT_SYMBOL(iam_it_put);
1280
1281 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1282                                         struct iam_ikey *ikey);
1283
1284
1285 /*
1286  * This function increments the frame pointer to search the next leaf
1287  * block, and reads in the necessary intervening nodes if the search
1288  * should be necessary.  Whether or not the search is necessary is
1289  * controlled by the hash parameter.  If the hash value is even, then
1290  * the search is only continued if the next block starts with that
1291  * hash value.  This is used if we are searching for a specific file.
1292  *
1293  * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1294  *
1295  * This function returns 1 if the caller should continue to search,
1296  * or 0 if it should not.  If there is an error reading one of the
1297  * index blocks, it will a negative error code.
1298  *
1299  * If start_hash is non-null, it will be filled in with the starting
1300  * hash of the next page.
1301  */
1302 static int iam_htree_advance(struct inode *dir, __u32 hash,
1303                               struct iam_path *path, __u32 *start_hash,
1304                               int compat)
1305 {
1306         struct iam_frame *p;
1307         struct buffer_head *bh;
1308         int err, num_frames = 0;
1309         __u32 bhash;
1310
1311         p = path->ip_frame;
1312         /*
1313          * Find the next leaf page by incrementing the frame pointer.
1314          * If we run out of entries in the interior node, loop around and
1315          * increment pointer in the parent node.  When we break out of
1316          * this loop, num_frames indicates the number of interior
1317          * nodes need to be read.
1318          */
1319         while (1) {
1320                 do_corr(schedule());
1321                 iam_lock_bh(p->bh);
1322                 if (p->at_shifted)
1323                         p->at_shifted = 0;
1324                 else
1325                         p->at = iam_entry_shift(path, p->at, +1);
1326                 if (p->at < iam_entry_shift(path, p->entries,
1327                                             dx_get_count(p->entries))) {
1328                         p->leaf = dx_get_block(path, p->at);
1329                         iam_unlock_bh(p->bh);
1330                         break;
1331                 }
1332                 iam_unlock_bh(p->bh);
1333                 if (p == path->ip_frames)
1334                         return 0;
1335                 num_frames++;
1336                 --p;
1337         }
1338
1339         if (compat) {
1340                 /*
1341                  * Htree hash magic.
1342                  */
1343         /*
1344          * If the hash is 1, then continue only if the next page has a
1345          * continuation hash of any value.  This is used for readdir
1346          * handling.  Otherwise, check to see if the hash matches the
1347          * desired contiuation hash.  If it doesn't, return since
1348          * there's no point to read in the successive index pages.
1349          */
1350                 dx_get_ikey(path, p->at, (struct iam_ikey *)&bhash);
1351         if (start_hash)
1352                 *start_hash = bhash;
1353         if ((hash & 1) == 0) {
1354                 if ((bhash & ~1) != hash)
1355                         return 0;
1356         }
1357         }
1358         /*
1359          * If the hash is HASH_NB_ALWAYS, we always go to the next
1360          * block so no check is necessary
1361          */
1362         while (num_frames--) {
1363                 iam_ptr_t idx;
1364
1365                 do_corr(schedule());
1366                 iam_lock_bh(p->bh);
1367                 idx = p->leaf = dx_get_block(path, p->at);
1368                 iam_unlock_bh(p->bh);
1369                 err = iam_path_descr(path)->id_ops->
1370                         id_node_read(path->ip_container, idx, NULL, &bh);
1371                 if (err != 0)
1372                         return err; /* Failure */
1373                 ++p;
1374                 brelse(p->bh);
1375                 assert_corr(p->bh != bh);
1376                 p->bh = bh;
1377                 p->entries = dx_node_get_entries(path, p);
1378                 p->at = iam_entry_shift(path, p->entries, !compat);
1379                 assert_corr(p->curidx != idx);
1380                 p->curidx = idx;
1381                 iam_lock_bh(p->bh);
1382                 assert_corr(p->leaf != dx_get_block(path, p->at));
1383                 p->leaf = dx_get_block(path, p->at);
1384                 iam_unlock_bh(p->bh);
1385                 assert_inv(dx_node_check(path, p));
1386         }
1387         return 1;
1388 }
1389
1390
1391 static inline int iam_index_advance(struct iam_path *path)
1392 {
1393         return iam_htree_advance(iam_path_obj(path), 0, path, NULL, 0);
1394 }
1395
1396 static void iam_unlock_array(struct iam_container *ic,
1397                              struct dynlock_handle **lh)
1398 {
1399         int i;
1400
1401         for (i = 0; i < DX_MAX_TREE_HEIGHT; ++i, ++lh) {
1402                 if (*lh != NULL) {
1403                         iam_unlock_htree(ic, *lh);
1404                         *lh = NULL;
1405                 }
1406         }
1407 }
1408 /*
1409  * Advance index part of @path to point to the next leaf. Returns 1 on
1410  * success, 0, when end of container was reached. Leaf node is locked.
1411  */
1412 int iam_index_next(struct iam_container *c, struct iam_path *path)
1413 {
1414         iam_ptr_t cursor;
1415         struct dynlock_handle *lh[DX_MAX_TREE_HEIGHT] = { NULL, };
1416         int result;
1417         struct inode *object;
1418
1419         /*
1420          * Locking for iam_index_next()... is to be described.
1421          */
1422
1423         object = c->ic_object;
1424         cursor = path->ip_frame->leaf;
1425
1426         while (1) {
1427                 result = iam_index_lock(path, lh);
1428                 do_corr(schedule());
1429                 if (result < 0)
1430                         break;
1431
1432                 result = iam_check_full_path(path, 0);
1433                 if (result == 0 && cursor == path->ip_frame->leaf) {
1434                         result = iam_index_advance(path);
1435
1436                         assert_corr(result == 0 ||
1437                                     cursor != path->ip_frame->leaf);
1438                         break;
1439                 }
1440                 do {
1441                         iam_unlock_array(c, lh);
1442
1443                         iam_path_release(path);
1444                         do_corr(schedule());
1445
1446                         result = __iam_path_lookup(path);
1447                         if (result < 0)
1448                                 break;
1449
1450                         while (path->ip_frame->leaf != cursor) {
1451                                 do_corr(schedule());
1452
1453                                 result = iam_index_lock(path, lh);
1454                                 do_corr(schedule());
1455                                 if (result < 0)
1456                                         break;
1457
1458                                 result = iam_check_full_path(path, 0);
1459                                 if (result != 0)
1460                                         break;
1461
1462                                 result = iam_index_advance(path);
1463                                 if (result == 0) {
1464                                         CERROR("cannot find cursor : %u\n",
1465                                                 cursor);
1466                                         result = -EIO;
1467                                 }
1468                                 if (result < 0)
1469                                         break;
1470                                 result = iam_check_full_path(path, 0);
1471                                 if (result != 0)
1472                                         break;
1473                                 iam_unlock_array(c, lh);
1474                         }
1475                 } while (result == -EAGAIN);
1476                 if (result < 0)
1477                         break;
1478         }
1479         iam_unlock_array(c, lh);
1480         return result;
1481 }
1482
1483 /*
1484  * Move iterator one record right.
1485  *
1486  * Return value: 0: success,
1487  *              +1: end of container reached
1488  *             -ve: error
1489  *
1490  * precondition:  (it_state(it) == IAM_IT_ATTACHED ||
1491  *                 it_state(it) == IAM_IT_SKEWED) && it->ii_flags&IAM_IT_MOVE
1492  * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED) &&
1493  *                ergo(result >  0, it_state(it) == IAM_IT_DETACHED)
1494  */
1495 int iam_it_next(struct iam_iterator *it)
1496 {
1497         int result;
1498         struct iam_path      *path;
1499         struct iam_leaf      *leaf;
1500         do_corr(struct iam_ikey *ik_orig);
1501
1502         /* assert_corr(it->ii_flags&IAM_IT_MOVE); */
1503         assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1504                     it_state(it) == IAM_IT_SKEWED);
1505
1506         path = &it->ii_path;
1507         leaf = &path->ip_leaf;
1508
1509         assert_corr(iam_leaf_is_locked(leaf));
1510
1511         result = 0;
1512         do_corr(ik_orig = it_at_rec(it) ?
1513                 iam_it_ikey_get(it, iam_path_ikey(path, 2)) : NULL);
1514         if (it_before(it)) {
1515                 assert_corr(!iam_leaf_at_end(leaf));
1516                 it->ii_state = IAM_IT_ATTACHED;
1517         } else {
1518                 if (!iam_leaf_at_end(leaf))
1519                         /* advance within leaf node */
1520                         iam_leaf_next(leaf);
1521                 /*
1522                  * multiple iterations may be necessary due to empty leaves.
1523                  */
1524                 while (result == 0 && iam_leaf_at_end(leaf)) {
1525                         do_corr(schedule());
1526                         /* advance index portion of the path */
1527                         result = iam_index_next(iam_it_container(it), path);
1528                         assert_corr(iam_leaf_is_locked(leaf));
1529                         if (result == 1) {
1530                                 struct dynlock_handle *lh;
1531                                 lh = iam_lock_htree(iam_it_container(it),
1532                                                     path->ip_frame->leaf,
1533                                                     DLT_WRITE);
1534                                 if (lh != NULL) {
1535                                         iam_leaf_fini(leaf);
1536                                         leaf->il_lock = lh;
1537                                         result = iam_leaf_load(path);
1538                                         if (result == 0)
1539                                                 iam_leaf_start(leaf);
1540                                 } else
1541                                         result = -ENOMEM;
1542                         } else if (result == 0)
1543                                 /* end of container reached */
1544                                 result = +1;
1545                         if (result != 0)
1546                                 iam_it_put(it);
1547                 }
1548                 if (result == 0)
1549                         it->ii_state = IAM_IT_ATTACHED;
1550         }
1551         assert_corr(ergo(result == 0, it_state(it) == IAM_IT_ATTACHED));
1552         assert_corr(ergo(result >  0, it_state(it) == IAM_IT_DETACHED));
1553         assert_corr(ergo(result == 0 && ik_orig != NULL,
1554                          it_ikeycmp(it, ik_orig) >= 0));
1555         return result;
1556 }
1557 EXPORT_SYMBOL(iam_it_next);
1558
1559 /*
1560  * Return pointer to the record under iterator.
1561  *
1562  * precondition:  it_state(it) == IAM_IT_ATTACHED && it_at_rec(it)
1563  * postcondition: it_state(it) == IAM_IT_ATTACHED
1564  */
1565 struct iam_rec *iam_it_rec_get(const struct iam_iterator *it)
1566 {
1567         assert_corr(it_state(it) == IAM_IT_ATTACHED);
1568         assert_corr(it_at_rec(it));
1569         return iam_leaf_rec(&it->ii_path.ip_leaf);
1570 }
1571 EXPORT_SYMBOL(iam_it_rec_get);
1572
1573 static void iam_it_reccpy(struct iam_iterator *it, const struct iam_rec *r)
1574 {
1575         struct iam_leaf *folio;
1576
1577         folio = &it->ii_path.ip_leaf;
1578         iam_leaf_ops(folio)->rec_set(folio, r);
1579 }
1580
1581 /*
1582  * Replace contents of record under iterator.
1583  *
1584  * precondition:  it_state(it) == IAM_IT_ATTACHED &&
1585  *                it->ii_flags&IAM_IT_WRITE
1586  * postcondition: it_state(it) == IAM_IT_ATTACHED &&
1587  *                ergo(result == 0, !memcmp(iam_it_rec_get(it), r, ...))
1588  */
1589 int iam_it_rec_set(handle_t *h,
1590                    struct iam_iterator *it, const struct iam_rec *r)
1591 {
1592         int result;
1593         struct iam_path *path;
1594         struct buffer_head *bh;
1595
1596         assert_corr(it_state(it) == IAM_IT_ATTACHED &&
1597                     it->ii_flags&IAM_IT_WRITE);
1598         assert_corr(it_at_rec(it));
1599
1600         path = &it->ii_path;
1601         bh   = path->ip_leaf.il_bh;
1602         result = iam_txn_add(h, path, bh);
1603         if (result == 0) {
1604                 iam_it_reccpy(it, r);
1605                 result = iam_txn_dirty(h, path, bh);
1606         }
1607         return result;
1608 }
1609 EXPORT_SYMBOL(iam_it_rec_set);
1610
1611 /*
1612  * Return pointer to the index key under iterator.
1613  *
1614  * precondition:  it_state(it) == IAM_IT_ATTACHED ||
1615  *                it_state(it) == IAM_IT_SKEWED
1616  */
1617 static struct iam_ikey *iam_it_ikey_get(const struct iam_iterator *it,
1618                                         struct iam_ikey *ikey)
1619 {
1620         assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1621                     it_state(it) == IAM_IT_SKEWED);
1622         assert_corr(it_at_rec(it));
1623         return iam_leaf_ikey(&it->ii_path.ip_leaf, ikey);
1624 }
1625
1626 /*
1627  * Return pointer to the key under iterator.
1628  *
1629  * precondition:  it_state(it) == IAM_IT_ATTACHED ||
1630  *                it_state(it) == IAM_IT_SKEWED
1631  */
1632 struct iam_key *iam_it_key_get(const struct iam_iterator *it)
1633 {
1634         assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1635                     it_state(it) == IAM_IT_SKEWED);
1636         assert_corr(it_at_rec(it));
1637         return iam_leaf_key(&it->ii_path.ip_leaf);
1638 }
1639 EXPORT_SYMBOL(iam_it_key_get);
1640
1641 /*
1642  * Return size of key under iterator (in bytes)
1643  *
1644  * precondition:  it_state(it) == IAM_IT_ATTACHED ||
1645  *                it_state(it) == IAM_IT_SKEWED
1646  */
1647 int iam_it_key_size(const struct iam_iterator *it)
1648 {
1649         assert_corr(it_state(it) == IAM_IT_ATTACHED ||
1650                     it_state(it) == IAM_IT_SKEWED);
1651         assert_corr(it_at_rec(it));
1652         return iam_leaf_key_size(&it->ii_path.ip_leaf);
1653 }
1654 EXPORT_SYMBOL(iam_it_key_size);
1655
1656 static struct buffer_head *
1657 iam_new_node(handle_t *h, struct iam_container *c, iam_ptr_t *b, int *e)
1658 {
1659         struct inode *inode = c->ic_object;
1660         struct buffer_head *bh = NULL;
1661         struct iam_idle_head *head;
1662         struct buffer_head *idle;
1663         __u32 *idle_blocks;
1664         __u16 count;
1665
1666         if (c->ic_idle_bh == NULL)
1667                 goto newblock;
1668
1669         mutex_lock(&c->ic_idle_mutex);
1670         if (unlikely(c->ic_idle_bh == NULL)) {
1671                 mutex_unlock(&c->ic_idle_mutex);
1672                 goto newblock;
1673         }
1674
1675         head = (struct iam_idle_head *)(c->ic_idle_bh->b_data);
1676         count = le16_to_cpu(head->iih_count);
1677         if (count > 0) {
1678                 *e = ldiskfs_journal_get_write_access(h, c->ic_idle_bh);
1679                 if (*e != 0)
1680                         goto fail;
1681
1682                 --count;
1683                 *b = le32_to_cpu(head->iih_blks[count]);
1684                 head->iih_count = cpu_to_le16(count);
1685                 *e = ldiskfs_handle_dirty_metadata(h, inode, c->ic_idle_bh);
1686                 if (*e != 0)
1687                         goto fail;
1688
1689                 mutex_unlock(&c->ic_idle_mutex);
1690                 bh = ldiskfs_bread(NULL, inode, *b, 0, e);
1691                 if (bh == NULL)
1692                         return NULL;
1693                 goto got;
1694         }
1695
1696         /* The block itself which contains the iam_idle_head is
1697          * also an idle block, and can be used as the new node. */
1698         idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
1699                                 c->ic_descr->id_root_gap +
1700                                 sizeof(struct dx_countlimit));
1701         *e = ldiskfs_journal_get_write_access(h, c->ic_root_bh);
1702         if (*e != 0)
1703                 goto fail;
1704
1705         *b = le32_to_cpu(*idle_blocks);
1706         iam_lock_bh(c->ic_root_bh);
1707         *idle_blocks = head->iih_next;
1708         iam_unlock_bh(c->ic_root_bh);
1709         *e = ldiskfs_handle_dirty_metadata(h, inode, c->ic_root_bh);
1710         if (*e != 0) {
1711                 iam_lock_bh(c->ic_root_bh);
1712                 *idle_blocks = cpu_to_le32(*b);
1713                 iam_unlock_bh(c->ic_root_bh);
1714                 goto fail;
1715         }
1716
1717         bh = c->ic_idle_bh;
1718         idle = iam_load_idle_blocks(c, le32_to_cpu(*idle_blocks));
1719         if (idle != NULL && IS_ERR(idle)) {
1720                 *e = PTR_ERR(idle);
1721                 c->ic_idle_bh = NULL;
1722                 brelse(bh);
1723                 goto fail;
1724         }
1725
1726         c->ic_idle_bh = idle;
1727         mutex_unlock(&c->ic_idle_mutex);
1728
1729 got:
1730         /* get write access for the found buffer head */
1731         *e = ldiskfs_journal_get_write_access(h, bh);
1732         if (*e != 0) {
1733                 brelse(bh);
1734                 bh = NULL;
1735                 ldiskfs_std_error(inode->i_sb, *e);
1736         } else {
1737                 /* Clear the reused node as new node does. */
1738                 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1739                 set_buffer_uptodate(bh);
1740         }
1741         return bh;
1742
1743 newblock:
1744         bh = osd_ldiskfs_append(h, inode, b, e);
1745         return bh;
1746
1747 fail:
1748         mutex_unlock(&c->ic_idle_mutex);
1749         ldiskfs_std_error(inode->i_sb, *e);
1750         return NULL;
1751 }
1752
1753 /*
1754  * Insertion of new record. Interaction with jbd during non-trivial case (when
1755  * split happens) is as following:
1756  *
1757  *  - new leaf node is involved into transaction by iam_new_node();
1758  *
1759  *  - old leaf node is involved into transaction by iam_add_rec();
1760  *
1761  *  - leaf where insertion point ends in, is marked dirty by iam_add_rec();
1762  *
1763  *  - leaf without insertion point is marked dirty (as @new_leaf) by
1764  *  iam_new_leaf();
1765  *
1766  *  - split index nodes are involved into transaction and marked dirty by
1767  *  split_index_node().
1768  *
1769  *  - "safe" index node, which is no split, but where new pointer is inserted
1770  *  is involved into transaction and marked dirty by split_index_node().
1771  *
1772  *  - index node where pointer to new leaf is inserted is involved into
1773  *  transaction by split_index_node() and marked dirty by iam_add_rec().
1774  *
1775  *  - inode is marked dirty by iam_add_rec().
1776  *
1777  */
1778
1779 static int iam_new_leaf(handle_t *handle, struct iam_leaf *leaf)
1780 {
1781         int err;
1782         iam_ptr_t blknr;
1783         struct buffer_head   *new_leaf;
1784         struct buffer_head   *old_leaf;
1785         struct iam_container *c;
1786         struct inode         *obj;
1787         struct iam_path      *path;
1788
1789         assert_inv(iam_leaf_check(leaf));
1790
1791         c = iam_leaf_container(leaf);
1792         path = leaf->il_path;
1793
1794         obj = c->ic_object;
1795         new_leaf = iam_new_node(handle, c, &blknr, &err);
1796         do_corr(schedule());
1797         if (new_leaf != NULL) {
1798                 struct dynlock_handle *lh;
1799
1800                 lh = iam_lock_htree(c, blknr, DLT_WRITE);
1801                 do_corr(schedule());
1802                 if (lh != NULL) {
1803                         iam_leaf_ops(leaf)->init_new(c, new_leaf);
1804                         do_corr(schedule());
1805                         old_leaf = leaf->il_bh;
1806                         iam_leaf_split(leaf, &new_leaf, blknr);
1807                         if (old_leaf != leaf->il_bh) {
1808                                 /*
1809                                  * Switched to the new leaf.
1810                                  */
1811                                 iam_leaf_unlock(leaf);
1812                                 leaf->il_lock = lh;
1813                                 path->ip_frame->leaf = blknr;
1814                         } else
1815                                 iam_unlock_htree(path->ip_container, lh);
1816                         do_corr(schedule());
1817                         err = iam_txn_dirty(handle, path, new_leaf);
1818                         brelse(new_leaf);
1819                         if (err == 0)
1820                                 err = ldiskfs_mark_inode_dirty(handle, obj);
1821                         do_corr(schedule());
1822                 } else
1823                         err = -ENOMEM;
1824         }
1825         assert_inv(iam_leaf_check(leaf));
1826         assert_inv(iam_leaf_check(&iam_leaf_path(leaf)->ip_leaf));
1827         assert_inv(iam_path_check(iam_leaf_path(leaf)));
1828         return err;
1829 }
1830
1831 static inline void dx_set_limit(struct iam_entry *entries, unsigned value)
1832 {
1833         ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
1834 }
1835
1836 static int iam_shift_entries(struct iam_path *path,
1837                          struct iam_frame *frame, unsigned count,
1838                          struct iam_entry *entries, struct iam_entry *entries2,
1839                          u32 newblock)
1840 {
1841         unsigned count1;
1842         unsigned count2;
1843         int delta;
1844
1845         struct iam_frame *parent = frame - 1;
1846         struct iam_ikey *pivot = iam_path_ikey(path, 3);
1847
1848         delta = dx_index_is_compat(path) ? 0 : +1;
1849
1850         count1 = count/2 + delta;
1851         count2 = count - count1;
1852         dx_get_ikey(path, iam_entry_shift(path, entries, count1), pivot);
1853
1854         dxtrace(printk("Split index %d/%d\n", count1, count2));
1855
1856         memcpy((char *) iam_entry_shift(path, entries2, delta),
1857                (char *) iam_entry_shift(path, entries, count1),
1858                count2 * iam_entry_size(path));
1859
1860         dx_set_count(entries2, count2 + delta);
1861         dx_set_limit(entries2, dx_node_limit(path));
1862
1863         /*
1864          * NOTE: very subtle piece of code competing dx_probe() may find 2nd
1865          * level index in root index, then we insert new index here and set
1866          * new count in that 2nd level index. so, dx_probe() may see 2nd level
1867          * index w/o hash it looks for. the solution is to check root index
1868          * after we locked just founded 2nd level index -bzzz
1869          */
1870         iam_insert_key_lock(path, parent, pivot, newblock);
1871
1872         /*
1873          * now old and new 2nd level index blocks contain all pointers, so
1874          * dx_probe() may find it in the both.  it's OK -bzzz
1875          */
1876         iam_lock_bh(frame->bh);
1877         dx_set_count(entries, count1);
1878         iam_unlock_bh(frame->bh);
1879
1880         /*
1881          * now old 2nd level index block points to first half of leafs. it's
1882          * importand that dx_probe() must check root index block for changes
1883          * under dx_lock_bh(frame->bh) -bzzz
1884          */
1885
1886         return count1;
1887 }
1888
1889
1890 int split_index_node(handle_t *handle, struct iam_path *path,
1891                      struct dynlock_handle **lh)
1892 {
1893
1894         struct iam_entry *entries;   /* old block contents */
1895         struct iam_entry *entries2;  /* new block contents */
1896         struct iam_frame *frame, *safe;
1897         struct buffer_head *bh_new[DX_MAX_TREE_HEIGHT] = {NULL};
1898         u32 newblock[DX_MAX_TREE_HEIGHT] = {0};
1899         struct dynlock_handle *lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1900         struct dynlock_handle *new_lock[DX_MAX_TREE_HEIGHT] = {NULL,};
1901         struct inode *dir = iam_path_obj(path);
1902         struct iam_descr *descr;
1903         int nr_splet;
1904         int i, err;
1905
1906         descr = iam_path_descr(path);
1907         /*
1908          * Algorithm below depends on this.
1909          */
1910         assert_corr(dx_root_limit(path) < dx_node_limit(path));
1911
1912         frame = path->ip_frame;
1913         entries = frame->entries;
1914
1915         /*
1916          * Tall-tree handling: we might have to split multiple index blocks
1917          * all the way up to tree root. Tricky point here is error handling:
1918          * to avoid complicated undo/rollback we
1919          *
1920          *   - first allocate all necessary blocks
1921          *
1922          *   - insert pointers into them atomically.
1923          */
1924
1925         /*
1926          * Locking: leaf is already locked. htree-locks are acquired on all
1927          * index nodes that require split bottom-to-top, on the "safe" node,
1928          * and on all new nodes
1929          */
1930
1931         dxtrace(printk("using %u of %u node entries\n",
1932                        dx_get_count(entries), dx_get_limit(entries)));
1933
1934         /* What levels need split? */
1935         for (nr_splet = 0; frame >= path->ip_frames &&
1936              dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1937              --frame, ++nr_splet) {
1938                 do_corr(schedule());
1939                 if (nr_splet == DX_MAX_TREE_HEIGHT) {
1940                         /*
1941                         CWARN(dir->i_sb, __FUNCTION__,
1942                                      "Directory index full!\n");
1943                                      */
1944                         err = -ENOSPC;
1945                         goto cleanup;
1946                 }
1947         }
1948
1949         safe = frame;
1950
1951         /*
1952          * Lock all nodes, bottom to top.
1953          */
1954         for (frame = path->ip_frame, i = nr_splet; i >= 0; --i, --frame) {
1955                 do_corr(schedule());
1956                 lock[i] = iam_lock_htree(path->ip_container, frame->curidx,
1957                                          DLT_WRITE);
1958                 if (lock[i] == NULL) {
1959                         err = -ENOMEM;
1960                         goto cleanup;
1961                 }
1962         }
1963
1964         /*
1965          * Check for concurrent index modification.
1966          */
1967         err = iam_check_full_path(path, 1);
1968         if (err)
1969                 goto cleanup;
1970         /*
1971          * And check that the same number of nodes is to be split.
1972          */
1973         for (i = 0, frame = path->ip_frame; frame >= path->ip_frames &&
1974              dx_get_count(frame->entries) == dx_get_limit(frame->entries);
1975              --frame, ++i) {
1976                 ;
1977         }
1978         if (i != nr_splet) {
1979                 err = -EAGAIN;
1980                 goto cleanup;
1981         }
1982
1983         /* Go back down, allocating blocks, locking them, and adding into
1984          * transaction... */
1985         for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
1986                 bh_new[i] = iam_new_node(handle, path->ip_container,
1987                                          &newblock[i], &err);
1988                 do_corr(schedule());
1989                 if (!bh_new[i] ||
1990                     descr->id_ops->id_node_init(path->ip_container,
1991                                                 bh_new[i], 0) != 0)
1992                         goto cleanup;
1993                 new_lock[i] = iam_lock_htree(path->ip_container, newblock[i],
1994                                              DLT_WRITE);
1995                 if (new_lock[i] == NULL) {
1996                         err = -ENOMEM;
1997                         goto cleanup;
1998                 }
1999                 do_corr(schedule());
2000                 BUFFER_TRACE(frame->bh, "get_write_access");
2001                 err = ldiskfs_journal_get_write_access(handle, frame->bh);
2002                 if (err)
2003                         goto journal_error;
2004         }
2005         /* Add "safe" node to transaction too */
2006         if (safe + 1 != path->ip_frames) {
2007                 do_corr(schedule());
2008                 err = ldiskfs_journal_get_write_access(handle, safe->bh);
2009                 if (err)
2010                         goto journal_error;
2011         }
2012
2013         /* Go through nodes once more, inserting pointers */
2014         for (frame = safe + 1, i = 0; i < nr_splet; ++i, ++frame) {
2015                 unsigned count;
2016                 int idx;
2017                 struct buffer_head *bh2;
2018                 struct buffer_head *bh;
2019
2020                 entries = frame->entries;
2021                 count = dx_get_count(entries);
2022                 idx = iam_entry_diff(path, frame->at, entries);
2023
2024                 bh2 = bh_new[i];
2025                 entries2 = dx_get_entries(path, bh2->b_data, 0);
2026
2027                 bh = frame->bh;
2028                 if (frame == path->ip_frames) {
2029                         /* splitting root node. Tricky point:
2030                          *
2031                          * In the "normal" B-tree we'd split root *and* add
2032                          * new root to the tree with pointers to the old root
2033                          * and its sibling (thus introducing two new nodes).
2034                          *
2035                          * In htree it's enough to add one node, because
2036                          * capacity of the root node is smaller than that of
2037                          * non-root one.
2038                          */
2039                         struct iam_frame *frames;
2040                         struct iam_entry *next;
2041
2042                         assert_corr(i == 0);
2043
2044                         do_corr(schedule());
2045
2046                         frames = path->ip_frames;
2047                         memcpy((char *) entries2, (char *) entries,
2048                                count * iam_entry_size(path));
2049                         dx_set_limit(entries2, dx_node_limit(path));
2050
2051                         /* Set up root */
2052                           iam_lock_bh(frame->bh);
2053                         next = descr->id_ops->id_root_inc(path->ip_container,
2054                                                           path, frame);
2055                         dx_set_block(path, next, newblock[0]);
2056                           iam_unlock_bh(frame->bh);
2057
2058                         do_corr(schedule());
2059                         /* Shift frames in the path */
2060                         memmove(frames + 2, frames + 1,
2061                                 (sizeof path->ip_frames) - 2 * sizeof frames[0]);
2062                         /* Add new access path frame */
2063                         frames[1].at = iam_entry_shift(path, entries2, idx);
2064                         frames[1].entries = entries = entries2;
2065                         frames[1].bh = bh2;
2066                         assert_inv(dx_node_check(path, frame));
2067                         ++ path->ip_frame;
2068                         ++ frame;
2069                         assert_inv(dx_node_check(path, frame));
2070                         bh_new[0] = NULL; /* buffer head is "consumed" */
2071                         err = ldiskfs_handle_dirty_metadata(handle, NULL, bh2);
2072                         if (err)
2073                                 goto journal_error;
2074                         do_corr(schedule());
2075                 } else {
2076                         /* splitting non-root index node. */
2077                         struct iam_frame *parent = frame - 1;
2078
2079                         do_corr(schedule());
2080                         count = iam_shift_entries(path, frame, count,
2081                                               entries, entries2, newblock[i]);
2082                         /* Which index block gets the new entry? */
2083                         if (idx >= count) {
2084                                 int d = dx_index_is_compat(path) ? 0 : +1;
2085
2086                                 frame->at = iam_entry_shift(path, entries2,
2087                                                             idx - count + d);
2088                                 frame->entries = entries = entries2;
2089                                 frame->curidx = newblock[i];
2090                                 swap(frame->bh, bh2);
2091                                 assert_corr(lock[i + 1] != NULL);
2092                                 assert_corr(new_lock[i] != NULL);
2093                                 swap(lock[i + 1], new_lock[i]);
2094                                 bh_new[i] = bh2;
2095                                 parent->at = iam_entry_shift(path,
2096                                                              parent->at, +1);
2097                         }
2098                         assert_inv(dx_node_check(path, frame));
2099                         assert_inv(dx_node_check(path, parent));
2100                         dxtrace(dx_show_index ("node", frame->entries));
2101                         dxtrace(dx_show_index ("node",
2102                                ((struct dx_node *) bh2->b_data)->entries));
2103                         err = ldiskfs_handle_dirty_metadata(handle, NULL, bh2);
2104                         if (err)
2105                                 goto journal_error;
2106                         do_corr(schedule());
2107                         err = ldiskfs_handle_dirty_metadata(handle, NULL,
2108                                                             parent->bh);
2109                         if (err)
2110                                 goto journal_error;
2111                 }
2112                 do_corr(schedule());
2113                 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
2114                 if (err)
2115                         goto journal_error;
2116         }
2117                 /*
2118                  * This function was called to make insertion of new leaf
2119                  * possible. Check that it fulfilled its obligations.
2120                  */
2121                 assert_corr(dx_get_count(path->ip_frame->entries) <
2122                             dx_get_limit(path->ip_frame->entries));
2123         assert_corr(lock[nr_splet] != NULL);
2124         *lh = lock[nr_splet];
2125         lock[nr_splet] = NULL;
2126         if (nr_splet > 0) {
2127                 /*
2128                  * Log ->i_size modification.
2129                  */
2130                 err = ldiskfs_mark_inode_dirty(handle, dir);
2131                 if (err)
2132                         goto journal_error;
2133         }
2134         goto cleanup;
2135 journal_error:
2136         ldiskfs_std_error(dir->i_sb, err);
2137
2138 cleanup:
2139         iam_unlock_array(path->ip_container, lock);
2140         iam_unlock_array(path->ip_container, new_lock);
2141
2142         assert_corr(err || iam_frame_is_locked(path, path->ip_frame));
2143
2144         do_corr(schedule());
2145         for (i = 0; i < ARRAY_SIZE(bh_new); ++i) {
2146                 if (bh_new[i] != NULL)
2147                         brelse(bh_new[i]);
2148         }
2149         return err;
2150 }
2151
2152 static int iam_add_rec(handle_t *handle, struct iam_iterator *it,
2153                        struct iam_path *path,
2154                        const struct iam_key *k, const struct iam_rec *r)
2155 {
2156         int err;
2157         struct iam_leaf *leaf;
2158
2159         leaf = &path->ip_leaf;
2160         assert_inv(iam_leaf_check(leaf));
2161         assert_inv(iam_path_check(path));
2162         err = iam_txn_add(handle, path, leaf->il_bh);
2163         if (err == 0) {
2164                 do_corr(schedule());
2165                 if (!iam_leaf_can_add(leaf, k, r)) {
2166                         struct dynlock_handle *lh = NULL;
2167
2168                         do {
2169                                 assert_corr(lh == NULL);
2170                                 do_corr(schedule());
2171                                 err = split_index_node(handle, path, &lh);
2172                                 if (err == -EAGAIN) {
2173                                         assert_corr(lh == NULL);
2174
2175                                         iam_path_fini(path);
2176                                         it->ii_state = IAM_IT_DETACHED;
2177
2178                                         do_corr(schedule());
2179                                         err = iam_it_get_exact(it, k);
2180                                         if (err == -ENOENT)
2181                                                 err = +1; /* repeat split */
2182                                         else if (err == 0)
2183                                                 err = -EEXIST;
2184                                 }
2185                         } while (err > 0);
2186                         assert_inv(iam_path_check(path));
2187                         if (err == 0) {
2188                                 assert_corr(lh != NULL);
2189                                 do_corr(schedule());
2190                                 err = iam_new_leaf(handle, leaf);
2191                                 if (err == 0)
2192                                         err = iam_txn_dirty(handle, path,
2193                                                             path->ip_frame->bh);
2194                         }
2195                         iam_unlock_htree(path->ip_container, lh);
2196                         do_corr(schedule());
2197                 }
2198                 if (err == 0) {
2199                         iam_leaf_rec_add(leaf, k, r);
2200                         err = iam_txn_dirty(handle, path, leaf->il_bh);
2201                 }
2202         }
2203         assert_inv(iam_leaf_check(leaf));
2204         assert_inv(iam_leaf_check(&path->ip_leaf));
2205         assert_inv(iam_path_check(path));
2206         return err;
2207 }
2208
2209 /*
2210  * Insert new record with key @k and contents from @r, shifting records to the
2211  * right. On success, iterator is positioned on the newly inserted record.
2212  *
2213  * precondition: it->ii_flags&IAM_IT_WRITE &&
2214  *               (it_state(it) == IAM_IT_ATTACHED ||
2215  *                it_state(it) == IAM_IT_SKEWED) &&
2216  *               ergo(it_state(it) == IAM_IT_ATTACHED,
2217  *                    it_keycmp(it, k) <= 0) &&
2218  *               ergo(it_before(it), it_keycmp(it, k) > 0));
2219  * postcondition: ergo(result == 0,
2220  *                     it_state(it) == IAM_IT_ATTACHED &&
2221  *                     it_keycmp(it, k) == 0 &&
2222  *                     !memcmp(iam_it_rec_get(it), r, ...))
2223  */
2224 int iam_it_rec_insert(handle_t *h, struct iam_iterator *it,
2225                       const struct iam_key *k, const struct iam_rec *r)
2226 {
2227         int result;
2228         struct iam_path *path;
2229
2230         path = &it->ii_path;
2231
2232         assert_corr(it->ii_flags&IAM_IT_WRITE);
2233         assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2234                     it_state(it) == IAM_IT_SKEWED);
2235         assert_corr(ergo(it_state(it) == IAM_IT_ATTACHED,
2236                          it_keycmp(it, k) <= 0));
2237         assert_corr(ergo(it_before(it), it_keycmp(it, k) > 0));
2238         result = iam_add_rec(h, it, path, k, r);
2239         if (result == 0)
2240                 it->ii_state = IAM_IT_ATTACHED;
2241         assert_corr(ergo(result == 0,
2242                          it_state(it) == IAM_IT_ATTACHED &&
2243                          it_keycmp(it, k) == 0));
2244         return result;
2245 }
2246 EXPORT_SYMBOL(iam_it_rec_insert);
2247
2248 static inline int iam_idle_blocks_limit(struct inode *inode)
2249 {
2250         return (inode->i_sb->s_blocksize - sizeof(struct iam_idle_head)) >> 2;
2251 }
2252
2253 /*
2254  * If the leaf cannnot be recycled, we will lose one block for reusing.
2255  * It is not a serious issue because it almost the same of non-recycle.
2256  */
2257 static iam_ptr_t iam_index_shrink(handle_t *h, struct iam_path *p,
2258                                   struct iam_leaf *l, struct buffer_head **bh)
2259 {
2260         struct iam_container *c = p->ip_container;
2261         struct inode *inode = c->ic_object;
2262         struct iam_frame *frame = p->ip_frame;
2263         struct iam_entry *entries;
2264         struct iam_entry *pos;
2265         struct dynlock_handle *lh;
2266         int count;
2267         int rc;
2268
2269         if (c->ic_idle_failed)
2270                 return 0;
2271
2272         if (unlikely(frame == NULL))
2273                 return 0;
2274
2275         if (!iam_leaf_empty(l))
2276                 return 0;
2277
2278         lh = iam_lock_htree(c, frame->curidx, DLT_WRITE);
2279         if (lh == NULL) {
2280                 CWARN("%.16s: No memory to recycle idle blocks\n",
2281                       LDISKFS_SB(inode->i_sb)->s_es->s_volume_name);
2282                 return 0;
2283         }
2284
2285         rc = iam_txn_add(h, p, frame->bh);
2286         if (rc != 0) {
2287                 iam_unlock_htree(c, lh);
2288                 return 0;
2289         }
2290
2291         iam_lock_bh(frame->bh);
2292         entries = frame->entries;
2293         count = dx_get_count(entries);
2294         /* NOT shrink the last entry in the index node, which can be reused
2295          * directly by next new node. */
2296         if (count == 2) {
2297                 iam_unlock_bh(frame->bh);
2298                 iam_unlock_htree(c, lh);
2299                 return 0;
2300         }
2301
2302         pos = iam_find_position(p, frame);
2303         /* There may be some new leaf nodes have been added or empty leaf nodes
2304          * have been shrinked during my delete operation.
2305          *
2306          * If the empty leaf is not under current index node because the index
2307          * node has been split, then just skip the empty leaf, which is rare. */
2308         if (unlikely(frame->leaf != dx_get_block(p, pos))) {
2309                 iam_unlock_bh(frame->bh);
2310                 iam_unlock_htree(c, lh);
2311                 return 0;
2312         }
2313
2314         frame->at = pos;
2315         if (frame->at < iam_entry_shift(p, entries, count - 1)) {
2316                 struct iam_entry *n = iam_entry_shift(p, frame->at, 1);
2317
2318                 memmove(frame->at, n,
2319                         (char *)iam_entry_shift(p, entries, count) - (char *)n);
2320                 frame->at_shifted = 1;
2321         }
2322         dx_set_count(entries, count - 1);
2323         iam_unlock_bh(frame->bh);
2324         rc = iam_txn_dirty(h, p, frame->bh);
2325         iam_unlock_htree(c, lh);
2326         if (rc != 0)
2327                 return 0;
2328
2329         get_bh(l->il_bh);
2330         *bh = l->il_bh;
2331         return frame->leaf;
2332 }
2333
2334 static int
2335 iam_install_idle_blocks(handle_t *h, struct iam_path *p, struct buffer_head *bh,
2336                         __u32 *idle_blocks, iam_ptr_t blk)
2337 {
2338         struct iam_container *c = p->ip_container;
2339         struct buffer_head *old = c->ic_idle_bh;
2340         struct iam_idle_head *head;
2341         int rc;
2342
2343         head = (struct iam_idle_head *)(bh->b_data);
2344         head->iih_magic = cpu_to_le16(IAM_IDLE_HEADER_MAGIC);
2345         head->iih_count = 0;
2346         head->iih_next = *idle_blocks;
2347         /* The bh already get_write_accessed. */
2348         rc = iam_txn_dirty(h, p, bh);
2349         if (rc != 0)
2350                 return rc;
2351
2352         rc = iam_txn_add(h, p, c->ic_root_bh);
2353         if (rc != 0)
2354                 return rc;
2355
2356         iam_lock_bh(c->ic_root_bh);
2357         *idle_blocks = cpu_to_le32(blk);
2358         iam_unlock_bh(c->ic_root_bh);
2359         rc = iam_txn_dirty(h, p, c->ic_root_bh);
2360         if (rc == 0) {
2361                 /* NOT release old before new assigned. */
2362                 get_bh(bh);
2363                 c->ic_idle_bh = bh;
2364                 brelse(old);
2365         } else {
2366                 iam_lock_bh(c->ic_root_bh);
2367                 *idle_blocks = head->iih_next;
2368                 iam_unlock_bh(c->ic_root_bh);
2369         }
2370         return rc;
2371 }
2372
2373 /*
2374  * If the leaf cannnot be recycled, we will lose one block for reusing.
2375  * It is not a serious issue because it almost the same of non-recycle.
2376  */
2377 static void iam_recycle_leaf(handle_t *h, struct iam_path *p,
2378                              struct buffer_head *bh, iam_ptr_t blk)
2379 {
2380         struct iam_container *c = p->ip_container;
2381         struct inode *inode = c->ic_object;
2382         struct iam_idle_head *head;
2383         __u32 *idle_blocks;
2384         int count;
2385         int rc;
2386
2387         mutex_lock(&c->ic_idle_mutex);
2388         if (unlikely(c->ic_idle_failed)) {
2389                 rc = -EFAULT;
2390                 goto unlock;
2391         }
2392
2393         idle_blocks = (__u32 *)(c->ic_root_bh->b_data +
2394                                 c->ic_descr->id_root_gap +
2395                                 sizeof(struct dx_countlimit));
2396         /* It is the first idle block. */
2397         if (c->ic_idle_bh == NULL) {
2398                 rc = iam_install_idle_blocks(h, p, bh, idle_blocks, blk);
2399                 goto unlock;
2400         }
2401
2402         head = (struct iam_idle_head *)(c->ic_idle_bh->b_data);
2403         count = le16_to_cpu(head->iih_count);
2404         /* Current ic_idle_bh is full, to be replaced by the leaf. */
2405         if (count == iam_idle_blocks_limit(inode)) {
2406                 rc = iam_install_idle_blocks(h, p, bh, idle_blocks, blk);
2407                 goto unlock;
2408         }
2409
2410         /* Just add to ic_idle_bh. */
2411         rc = iam_txn_add(h, p, c->ic_idle_bh);
2412         if (rc != 0)
2413                 goto unlock;
2414
2415         head->iih_blks[count] = cpu_to_le32(blk);
2416         head->iih_count = cpu_to_le16(count + 1);
2417         rc = iam_txn_dirty(h, p, c->ic_idle_bh);
2418
2419 unlock:
2420         mutex_unlock(&c->ic_idle_mutex);
2421         if (rc != 0)
2422                 CWARN("%.16s: idle blocks failed, will lose the blk %u\n",
2423                       LDISKFS_SB(inode->i_sb)->s_es->s_volume_name, blk);
2424 }
2425
2426 /*
2427  * Delete record under iterator.
2428  *
2429  * precondition:  it_state(it) == IAM_IT_ATTACHED &&
2430  *                it->ii_flags&IAM_IT_WRITE &&
2431  *                it_at_rec(it)
2432  * postcondition: it_state(it) == IAM_IT_ATTACHED ||
2433  *                it_state(it) == IAM_IT_DETACHED
2434  */
2435 int iam_it_rec_delete(handle_t *h, struct iam_iterator *it)
2436 {
2437         int result;
2438         struct iam_leaf *leaf;
2439         struct iam_path *path;
2440
2441         assert_corr(it_state(it) == IAM_IT_ATTACHED &&
2442                     it->ii_flags&IAM_IT_WRITE);
2443         assert_corr(it_at_rec(it));
2444
2445         path = &it->ii_path;
2446         leaf = &path->ip_leaf;
2447
2448         assert_inv(iam_leaf_check(leaf));
2449         assert_inv(iam_path_check(path));
2450
2451         result = iam_txn_add(h, path, leaf->il_bh);
2452         /*
2453          * no compaction for now.
2454          */
2455         if (result == 0) {
2456                 iam_rec_del(leaf, it->ii_flags&IAM_IT_MOVE);
2457                 result = iam_txn_dirty(h, path, leaf->il_bh);
2458                 if (result == 0 && iam_leaf_at_end(leaf)) {
2459                         struct buffer_head *bh = NULL;
2460                         iam_ptr_t blk;
2461
2462                         blk = iam_index_shrink(h, path, leaf, &bh);
2463                         if (it->ii_flags & IAM_IT_MOVE) {
2464                                 result = iam_it_next(it);
2465                                 if (result > 0)
2466                                         result = 0;
2467                         }
2468
2469                         if (bh != NULL) {
2470                                 iam_recycle_leaf(h, path, bh, blk);
2471                                 brelse(bh);
2472                         }
2473                 }
2474         }
2475         assert_inv(iam_leaf_check(leaf));
2476         assert_inv(iam_path_check(path));
2477         assert_corr(it_state(it) == IAM_IT_ATTACHED ||
2478                     it_state(it) == IAM_IT_DETACHED);
2479         return result;
2480 }
2481 EXPORT_SYMBOL(iam_it_rec_delete);
2482
2483 /*
2484  * Convert iterator to cookie.
2485  *
2486  * precondition:  it_state(it) == IAM_IT_ATTACHED &&
2487  *                iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2488  * postcondition: it_state(it) == IAM_IT_ATTACHED
2489  */
2490 iam_pos_t iam_it_store(const struct iam_iterator *it)
2491 {
2492         iam_pos_t result;
2493
2494         assert_corr(it_state(it) == IAM_IT_ATTACHED);
2495         assert_corr(it_at_rec(it));
2496         assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <=
2497                     sizeof result);
2498
2499         result = 0;
2500         return *(iam_pos_t *)iam_it_ikey_get(it, (void *)&result);
2501 }
2502 EXPORT_SYMBOL(iam_it_store);
2503
2504 /*
2505  * Restore iterator from cookie.
2506  *
2507  * precondition:  it_state(it) == IAM_IT_DETACHED && it->ii_flags&IAM_IT_MOVE &&
2508  *                iam_path_descr(it->ii_path)->id_key_size <= sizeof(iam_pos_t)
2509  * postcondition: ergo(result == 0, it_state(it) == IAM_IT_ATTACHED &&
2510  *                                  iam_it_store(it) == pos)
2511  */
2512 int iam_it_load(struct iam_iterator *it, iam_pos_t pos)
2513 {
2514         assert_corr(it_state(it) == IAM_IT_DETACHED &&
2515                     it->ii_flags&IAM_IT_MOVE);
2516         assert_corr(iam_it_container(it)->ic_descr->id_ikey_size <= sizeof pos);
2517         return iam_it_iget(it, (struct iam_ikey *)&pos);
2518 }
2519 EXPORT_SYMBOL(iam_it_load);
2520
2521 /***********************************************************************/
2522 /* invariants                                                          */
2523 /***********************************************************************/
2524
2525 static inline int ptr_inside(void *base, size_t size, void *ptr)
2526 {
2527         return (base <= ptr) && (ptr < base + size);
2528 }
2529
2530 static int iam_frame_invariant(struct iam_frame *f)
2531 {
2532         return
2533                 (f->bh != NULL &&
2534                 f->bh->b_data != NULL &&
2535                 ptr_inside(f->bh->b_data, f->bh->b_size, f->entries) &&
2536                 ptr_inside(f->bh->b_data, f->bh->b_size, f->at) &&
2537                 f->entries <= f->at);
2538 }
2539
2540 static int iam_leaf_invariant(struct iam_leaf *l)
2541 {
2542         return
2543                 l->il_bh != NULL &&
2544                 l->il_bh->b_data != NULL &&
2545                 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_entries) &&
2546                 ptr_inside(l->il_bh->b_data, l->il_bh->b_size, l->il_at) &&
2547                 l->il_entries <= l->il_at;
2548 }
2549
2550 static int iam_path_invariant(struct iam_path *p)
2551 {
2552         int i;
2553
2554         if (p->ip_container == NULL ||
2555             p->ip_indirect < 0 || p->ip_indirect > DX_MAX_TREE_HEIGHT - 1 ||
2556             p->ip_frame != p->ip_frames + p->ip_indirect ||
2557             !iam_leaf_invariant(&p->ip_leaf))
2558                 return 0;
2559         for (i = 0; i < ARRAY_SIZE(p->ip_frames); ++i) {
2560                 if (i <= p->ip_indirect) {
2561                         if (!iam_frame_invariant(&p->ip_frames[i]))
2562                                 return 0;
2563                 }
2564         }
2565         return 1;
2566 }
2567
2568 int iam_it_invariant(struct iam_iterator *it)
2569 {
2570         return
2571                 (it->ii_state == IAM_IT_DETACHED ||
2572                  it->ii_state == IAM_IT_ATTACHED ||
2573                  it->ii_state == IAM_IT_SKEWED) &&
2574                 !(it->ii_flags & ~(IAM_IT_MOVE | IAM_IT_WRITE)) &&
2575                 ergo(it->ii_state == IAM_IT_ATTACHED ||
2576                      it->ii_state == IAM_IT_SKEWED,
2577                      iam_path_invariant(&it->ii_path) &&
2578                      equi(it_at_rec(it), it->ii_state == IAM_IT_SKEWED));
2579 }
2580
2581 /*
2582  * Search container @c for record with key @k. If record is found, its data
2583  * are moved into @r.
2584  *
2585  * Return values: 0: found, -ENOENT: not-found, -ve: error
2586  */
2587 int iam_lookup(struct iam_container *c, const struct iam_key *k,
2588                struct iam_rec *r, struct iam_path_descr *pd)
2589 {
2590         struct iam_iterator it;
2591         int result;
2592
2593         iam_it_init(&it, c, 0, pd);
2594
2595         result = iam_it_get_exact(&it, k);
2596         if (result == 0)
2597                 /*
2598                  * record with required key found, copy it into user buffer
2599                  */
2600                 iam_reccpy(&it.ii_path.ip_leaf, r);
2601         iam_it_put(&it);
2602         iam_it_fini(&it);
2603         return result;
2604 }
2605 EXPORT_SYMBOL(iam_lookup);
2606
2607 /*
2608  * Insert new record @r with key @k into container @c (within context of
2609  * transaction @h).
2610  *
2611  * Return values: 0: success, -ve: error, including -EEXIST when record with
2612  * given key is already present.
2613  *
2614  * postcondition: ergo(result == 0 || result == -EEXIST,
2615  *                                  iam_lookup(c, k, r2) > 0;
2616  */
2617 int iam_insert(handle_t *h, struct iam_container *c, const struct iam_key *k,
2618                const struct iam_rec *r, struct iam_path_descr *pd)
2619 {
2620         struct iam_iterator it;
2621         int result;
2622
2623         iam_it_init(&it, c, IAM_IT_WRITE, pd);
2624
2625         result = iam_it_get_exact(&it, k);
2626         if (result == -ENOENT)
2627                 result = iam_it_rec_insert(h, &it, k, r);
2628         else if (result == 0)
2629                 result = -EEXIST;
2630         iam_it_put(&it);
2631         iam_it_fini(&it);
2632         return result;
2633 }
2634 EXPORT_SYMBOL(iam_insert);
2635
2636 /*
2637  * Update record with the key @k in container @c (within context of
2638  * transaction @h), new record is given by @r.
2639  *
2640  * Return values: +1: skip because of the same rec value, 0: success,
2641  * -ve: error, including -ENOENT if no record with the given key found.
2642  */
2643 int iam_update(handle_t *h, struct iam_container *c, const struct iam_key *k,
2644                const struct iam_rec *r, struct iam_path_descr *pd)
2645 {
2646         struct iam_iterator it;
2647         struct iam_leaf *folio;
2648         int result;
2649
2650         iam_it_init(&it, c, IAM_IT_WRITE, pd);
2651
2652         result = iam_it_get_exact(&it, k);
2653         if (result == 0) {
2654                 folio = &it.ii_path.ip_leaf;
2655                 result = iam_leaf_ops(folio)->rec_eq(folio, r);
2656                 if (result == 0)
2657                         iam_it_rec_set(h, &it, r);
2658                 else
2659                         result = 1;
2660         }
2661         iam_it_put(&it);
2662         iam_it_fini(&it);
2663         return result;
2664 }
2665 EXPORT_SYMBOL(iam_update);
2666
2667 /*
2668  * Delete existing record with key @k.
2669  *
2670  * Return values: 0: success, -ENOENT: not-found, -ve: other error.
2671  *
2672  * postcondition: ergo(result == 0 || result == -ENOENT,
2673  *                                 !iam_lookup(c, k, *));
2674  */
2675 int iam_delete(handle_t *h, struct iam_container *c, const struct iam_key *k,
2676                struct iam_path_descr *pd)
2677 {
2678         struct iam_iterator it;
2679         int result;
2680
2681         iam_it_init(&it, c, IAM_IT_WRITE, pd);
2682
2683         result = iam_it_get_exact(&it, k);
2684         if (result == 0)
2685                 iam_it_rec_delete(h, &it);
2686         iam_it_put(&it);
2687         iam_it_fini(&it);
2688         return result;
2689 }
2690 EXPORT_SYMBOL(iam_delete);
2691
2692 int iam_root_limit(int rootgap, int blocksize, int size)
2693 {
2694         int limit;
2695         int nlimit;
2696
2697         limit = (blocksize - rootgap) / size;
2698         nlimit = blocksize / size;
2699         if (limit == nlimit)
2700                 limit--;
2701         return limit;
2702 }