Whamcloud - gitweb
LU-1548 osd: move i_htree_lock to iam container
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_iam.h
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, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * osd_iam.c
37  * Top-level entry points into osd module
38  *
39  * Author: Wang Di <wangdi@clusterfs.com>
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 #ifndef __LINUX_LUSTRE_IAM_H__
44 #define __LINUX_LUSTRE_IAM_H__
45
46 #include <linux/module.h>
47 #include <asm/unaligned.h>
48 #include <linux/dynlocks.h>
49 /*
50  *  linux/include/linux/osd_iam.h
51  */
52 #ifndef CLASSERT
53 #define CLASSERT(cond) do {switch(42) {case (cond): case 0: break;}} while (0)
54 #endif
55 /* implication */
56 #define ergo(a, b) (!(a) || (b))
57 /* logical equivalence */
58 #define equi(a, b) (!!(a) == !!(b))
59
60 enum {
61         /*
62          * Maximal number of non-leaf levels in htree. In the stock ldiskfs this
63          * is 2.
64          */
65         /*
66          * XXX reduced back to 2 to make per-node locking work.
67          */
68         DX_MAX_TREE_HEIGHT = 5,
69         /*
70          * Scratch keys used by generic code for temporaries.
71          *
72          * Allocation:
73          *
74          *         [0] reserved for assertions and as a staging area for
75          *         record keys immediately used for key comparisons.
76          *
77          *         [1] reserved for record key, stored during iteration over
78          *         node records (see dx_node_check()).
79          *
80          *         [2] reserved for leaf node operations.
81          *
82          *         [3] reserved for index operations.
83          *
84          *         [4] reserved for path->ip_ikey_target
85          *
86          */
87         DX_SCRATCH_KEYS    = 5,
88         /*
89          * Maximal format name length.
90          */
91         DX_FMT_NAME_LEN    = 16,
92 };
93
94 #ifdef __KERNEL__
95
96 /*
97  * Debugging.
98  *
99  * Various debugging levels.
100  */
101
102 #if 0
103 /*
104  * Following macros are defined in config.h and are tunable through
105  * appropriate configure switches (indicated below).
106  */
107
108 /*
109  * Compile basic assertions in. You want this most of the time.
110  *
111  * --{enable,disable}-ldiskfs-assert (on by default).
112  */
113 #define LDISKFS_ASSERT (1)
114
115 /*
116  * Compile heavier correctness checks in. You want this during development
117  * cycle.
118  *
119  * --{enable,disable}-ldiskfs-correctness (off by default).
120  */
121 #define LDISKFS_CORRECTNESS (1)
122
123 /*
124  * Compile heavy invariant checking in. You want this early during development
125  * or when chasing a bug.
126  *
127  * --{enable,disable}-ldiskfs-invariant (off by default).
128  */
129 #define LDISKFS_INVARIANT (1)
130 #endif
131
132 #if defined(LDISKFS_ASSERT)
133 #define LDISKFS_ASSERT_ON (1)
134 #else
135 #define LDISKFS_ASSERT_ON (0)
136 #endif
137
138 #if defined(LDISKFS_CORRECTNESS)
139 #define LDISKFS_CORRECTNESS_ON (1)
140 #else
141 #define LDISKFS_CORRECTNESS_ON (0)
142 #endif
143
144 #if defined(LDISKFS_INVARIANT)
145 #define LDISKFS_INVARIANT_ON (1)
146 #else
147 #define LDISKFS_INVARIANT_ON (0)
148 #endif
149
150 #ifndef assert
151 #if LDISKFS_ASSERT_ON
152 #define assert(test) J_ASSERT(test)
153 #else
154 #define assert(test) ((void)(test))
155 #endif
156 #endif
157
158 #if LDISKFS_CORRECTNESS_ON
159 #define assert_corr(test) J_ASSERT(test)
160 #define do_corr(exp) exp
161 #else
162 #define assert_corr(test) do {;} while (0)
163 #define do_corr(exp) do {;} while (0)
164 #endif
165
166 #if LDISKFS_INVARIANT_ON
167 #define assert_inv(test) J_ASSERT(test)
168 #else
169 #define assert_inv(test) do {;} while (0)
170 #endif
171
172 /*
173  * Entry within index tree node. Consists of a key immediately followed
174  * (without padding) by a pointer to the child node.
175  *
176  * Both key and pointer are of variable size, hence incomplete type.
177  */
178 struct iam_entry;
179
180 struct iam_entry_compat {
181         __le32 hash;
182         __le32 block;
183 };
184
185 /*
186  * Incomplete type used to refer to keys in iam container.
187  *
188  * As key size can be different from container to container, iam has to use
189  * incomplete type. Clients cast pointer to iam_key to real key type and back.
190  */
191 struct iam_key;
192
193 /*
194  * Incomplete type use to refer to the records stored in iam containers.
195  */
196 struct iam_rec;
197
198 /*
199  * Key in index node. Possibly compressed. Fixed size.
200  */
201 struct iam_ikey;
202
203 /*
204  * Scalar type into which certain iam_key's can be uniquely mapped. Used to
205  * support interfaces like readdir(), where iteration over index has to be
206  * re-startable.
207  */
208 typedef __u32 iam_ptr_t;
209
210 /*
211  * Index node traversed during tree lookup.
212  */
213 struct iam_frame {
214         struct buffer_head *bh;    /* buffer holding node data */
215         struct iam_entry *entries; /* array of entries */
216         struct iam_entry *at;      /* target entry, found by binary search */
217         iam_ptr_t         leaf;    /* (logical) offset of child node found by
218                                     * binary search. */
219         iam_ptr_t         curidx;  /* (logical) offset of this node. Used to
220                                     * per-node locking to detect concurrent
221                                     * splits. */
222 };
223
224 /*
225  * Opaque entry in the leaf node.
226  */
227 struct iam_lentry;
228
229 struct iam_path;
230 struct iam_container;
231
232
233 /* leaf node reached by tree lookup */
234 struct iam_leaf {
235         struct iam_path    *il_path;
236         struct buffer_head *il_bh;
237         struct iam_lentry  *il_entries;
238         struct iam_lentry  *il_at;
239         /*
240          * Lock on a leaf node.
241          */
242         struct dynlock_handle *il_lock;
243         iam_ptr_t              il_curidx; /* logical offset of leaf node. */
244         void               *il_descr_data;
245 };
246
247 /*
248  * Return values of ->lookup() operation from struct iam_leaf_operations.
249  */
250 enum iam_lookup_t {
251         /*
252          * lookup found a record with the key requested
253          */
254         IAM_LOOKUP_EXACT  = 0,
255         /*
256          * lookup positioned leaf on some record
257          */
258         IAM_LOOKUP_OK     = 1,
259         /*
260          * leaf was empty
261          */
262         IAM_LOOKUP_EMPTY  = 2,
263         /*
264          * lookup positioned leaf before first record
265          */
266         IAM_LOOKUP_BEFORE = 3,
267         /*
268          * Found hash may have a continuation in the next leaf.
269          */
270         IAM_LOOKUP_LAST   = 0x100
271 };
272
273 /*
274  * Format-specific container operations. These are called by generic iam code.
275  */
276 struct iam_operations {
277         /*
278          * Returns pointer (in the same sense as pointer in index entry) to
279          * the root node.
280          */
281         __u32 (*id_root_ptr)(struct iam_container *c);
282
283         /*
284          * Check validity and consistency of index node.
285          */
286         int (*id_node_check)(struct iam_path *path, struct iam_frame *frame);
287         /*
288          * Copy some data from node header into frame. This is called when
289          * new node is loaded into frame.
290          */
291         int (*id_node_load)(struct iam_path *path, struct iam_frame *frame);
292         /*
293          * Initialize new node (stored in @bh) that is going to be added into
294          * tree.
295          */
296         int (*id_node_init)(struct iam_container *c,
297                             struct buffer_head *bh, int root);
298         int (*id_node_read)(struct iam_container *c, iam_ptr_t ptr,
299                             handle_t *h, struct buffer_head **bh);
300         /*
301          * Key comparison functions. Returns -1, 0, +1.
302          */
303         int (*id_ikeycmp)(const struct iam_container *c,
304                           const struct iam_ikey *k1,
305                           const struct iam_ikey *k2);
306         /*
307          * Modify root node when tree height increases.
308          */
309         struct iam_entry *(*id_root_inc)(struct iam_container *c,
310                                          struct iam_path *path,
311                                          struct iam_frame *frame);
312
313         struct iam_path_descr *(*id_ipd_alloc)(const struct iam_container *c,
314                                                void *area);
315         void (*id_ipd_free)(struct iam_path_descr *ipd);
316         /*
317          * Format name.
318          */
319         char id_name[DX_FMT_NAME_LEN];
320 };
321
322 /*
323  * Another format-specific operation vector, consisting of methods to access
324  * leaf nodes. This is separated from struct iam_operations, because it is
325  * assumed that there will be many formats with different format of leaf
326  * nodes, yes the same struct iam_operations.
327  */
328 struct iam_leaf_operations {
329                 /*
330                  * leaf operations.
331                  */
332
333         /*
334          * initialize just loaded leaf node.
335          */
336         int (*init)(struct iam_leaf *p);
337         /*
338          * Format new node.
339          */
340         void (*init_new)(struct iam_container *c, struct buffer_head *bh);
341         /*
342          * Release resources.
343          */
344         void (*fini)(struct iam_leaf *l);
345                 /*
346                  * returns true iff leaf is positioned at the last entry.
347                  */
348         int (*at_end)(const struct iam_leaf *l);
349                 /* position leaf at the first entry */
350         void (*start)(struct iam_leaf *l);
351                 /* more leaf to the next entry. */
352         void (*next)(struct iam_leaf *l);
353         /*
354          * return key of current leaf record. This method may return
355          * either pointer to the key stored in node, or copy key into
356          * @k buffer supplied by caller and return pointer to this
357          * buffer. The latter approach is used when keys in nodes are
358          * not stored in plain form (e.g., htree doesn't store keys at
359          * all).
360          *
361          * Caller should assume that returned pointer is only valid
362          * while leaf node is pinned and locked.
363          */
364         struct iam_ikey *(*ikey)(const struct iam_leaf *l, struct iam_ikey *k);
365         struct iam_key *(*key)(const struct iam_leaf *l);
366         /* return pointer to entry body. Pointer is valid while
367            corresponding leaf node is locked and pinned. */
368         struct iam_rec *(*rec)(const struct iam_leaf *l);
369
370         void (*key_set)(struct iam_leaf *l, const struct iam_key *k);
371         void (*rec_set)(struct iam_leaf *l, const struct iam_rec *r);
372         void (*rec_get)(const struct iam_leaf *l, struct iam_rec *r);
373
374         int (*key_cmp)(const struct iam_leaf *l, const struct iam_key *k);
375         int (*key_eq)(const struct iam_leaf *l, const struct iam_key *k);
376
377         int (*rec_eq)(const struct iam_leaf *l, const struct iam_rec *r);
378
379         int (*key_size)(const struct iam_leaf *l);
380         /*
381          * Search leaf @l for a record with key @k or for a place
382          * where such record is to be inserted.
383          *
384          * Scratch keys from @path can be used.
385          */
386         int (*lookup)(struct iam_leaf *l, const struct iam_key *k);
387         int (*ilookup)(struct iam_leaf *l, const struct iam_ikey *ik);
388
389         int (*can_add)(const struct iam_leaf *l,
390                        const struct iam_key *k, const struct iam_rec *r);
391         /*
392          * add rec for a leaf
393          */
394         void (*rec_add)(struct iam_leaf *l,
395                         const struct iam_key *k, const struct iam_rec *r);
396         /*
397          * remove rec for a leaf
398          */
399         void (*rec_del)(struct iam_leaf *l, int shift);
400         /*
401          * split leaf node, moving some entries into @bh (the latter currently
402          * is assumed to be empty).
403          */
404         void (*split)(struct iam_leaf *l, struct buffer_head **bh,
405                       iam_ptr_t newblknr);
406 };
407
408 /*
409  * Parameters, describing a flavor of iam container.
410  */
411 struct iam_descr {
412         /*
413          * Size of a key in this container, in bytes.
414          */
415          size_t       id_key_size;
416         /*
417          * Size of a key in index nodes, in bytes.
418          */
419          size_t       id_ikey_size;
420         /*
421          * Size of a pointer to the next level (stored in index nodes), in
422          * bytes.
423          */
424         size_t       id_ptr_size;
425         /*
426          * Size of a record (stored in leaf nodes), in bytes.
427          */
428         size_t       id_rec_size;
429         /*
430          * Size of unused (by iam) space at the beginning of every non-root
431          * node, in bytes. Used for compatibility with ldiskfs.
432          */
433         size_t       id_node_gap;
434         /*
435          * Size of unused (by iam) space at the beginning of root node, in
436          * bytes. Used for compatibility with ldiskfs.
437          */
438         size_t       id_root_gap;
439
440         struct iam_operations           *id_ops;
441         struct iam_leaf_operations      *id_leaf_ops;
442 };
443
444 /*
445  * An instance of iam container.
446  */
447 struct iam_container {
448         /*
449          * Underlying flat file. IO against this object is issued to
450          * read/write nodes.
451          */
452         struct inode        *ic_object;
453         /*
454          * BH of root block
455          */
456         struct buffer_head  *ic_root_bh;
457         /*
458          * container flavor.
459          */
460         struct iam_descr    *ic_descr;
461         /*
462          * read-write lock protecting index consistency.
463          */
464         cfs_rw_semaphore_t   ic_sem;
465         struct dynlock       ic_tree_lock;
466 };
467
468 /*
469  * description-specific part of iam_path. This is usually embedded into larger
470  * structure.
471  */
472 struct iam_path_descr {
473         /*
474          * Scratch-pad area for temporary keys.
475          */
476         struct iam_ikey *ipd_key_scratch[DX_SCRATCH_KEYS];
477 };
478
479 /*
480  * Structure to keep track of a path drilled through htree.
481  */
482 struct iam_path {
483         /*
484          * Parent container.
485          */
486         struct iam_container  *ip_container;
487         /*
488          * Number of index levels minus one.
489          */
490         int                    ip_indirect;
491         /*
492          * Nodes that top-to-bottom traversal passed through.
493          */
494         struct iam_frame       ip_frames[DX_MAX_TREE_HEIGHT];
495         /*
496          * Last filled frame in ->ip_frames. Refers to the 'twig' node (one
497          * immediately above leaf).
498          */
499         struct iam_frame      *ip_frame;
500         /*
501          * Leaf node: a child of ->ip_frame.
502          */
503         struct iam_leaf        ip_leaf;
504         /*
505          * Key searched for.
506          */
507         const struct iam_key  *ip_key_target;
508         const struct iam_ikey *ip_ikey_target;
509         /*
510          * Description-specific data.
511          */
512         struct iam_path_descr *ip_data;
513 };
514
515 struct ldiskfs_dx_hash_info;
516
517 /*
518  * Helper structure for legacy htrees.
519  */
520 struct iam_path_compat {
521         struct iam_path      ipc_path;
522         struct iam_container ipc_container;
523         __u32                 ipc_scratch[DX_SCRATCH_KEYS];
524         struct ldiskfs_dx_hash_info  *ipc_hinfo;
525         struct qstr          *ipc_qstr;
526         struct iam_path_descr ipc_descr;
527         struct ldiskfs_dx_hash_info   ipc_hinfo_area;
528 };
529
530 #define const_max(p, q) ((p > q) ? p : q)
531
532 enum {
533         DX_MAX_IKEY_SIZE   = 32, /* be generous */
534         /*
535          * Hack to avoid dynamic allocation and freeing of ipd.
536          */
537         DX_IPD_MAX_SIZE    = const_max(sizeof(struct iam_path_compat),
538                                        DX_MAX_IKEY_SIZE * DX_SCRATCH_KEYS +
539                                        sizeof(struct iam_path_descr))
540 };
541
542 /*
543  * iam cursor (iterator) api.
544  */
545
546 /*
547  * States of iterator state machine.
548  */
549 enum iam_it_state {
550         /* initial state */
551         IAM_IT_DETACHED,
552         /* iterator is above particular record in the container */
553         IAM_IT_ATTACHED,
554         /* iterator is positioned before record  */
555         IAM_IT_SKEWED
556 };
557
558 /*
559  * Flags controlling iterator functionality.
560  */
561 enum iam_it_flags {
562         /*
563          * this iterator will move (iam_it_next() will be called on it)
564          */
565         IAM_IT_MOVE  = (1 << 0),
566         /*
567          * tree can be updated through this iterator.
568          */
569         IAM_IT_WRITE = (1 << 1)
570 };
571
572 /*
573  * Iterator.
574  *
575  * Immediately after call to iam_it_init() iterator is in "detached"
576  * (IAM_IT_DETACHED) state: it is associated with given parent container, but
577  * doesn't point to any particular record in this container.
578  *
579  * After successful call to iam_it_get() and until corresponding call to
580  * iam_it_put() iterator is in one of "active" states: IAM_IT_ATTACHED or
581  * IAM_IT_SKEWED.
582  *
583  * Active iterator can move through records in a container (provided
584  * IAM_IT_MOVE permission) in a key order, can get record and key values as it
585  * passes over them, and can modify container (provided IAM_IT_WRITE
586  * permission).
587  *
588  * Iteration may reach the end of container, at which point iterator switches
589  * into IAM_IT_DETACHED state.
590  *
591  * Concurrency: iterators are supposed to be local to thread. Interfaces below
592  * do no internal serialization of access to the iterator fields.
593  *
594  * When in non-detached state, iterator keeps some container nodes pinned in
595  * memory and locked (that locking may be implemented at the container
596  * granularity though). In particular, clients may assume that pointers to
597  * records and keys obtained through iterator interface as valid until
598  * iterator is detached (except that they may be invalidated by sub-sequent
599  * operations done through the same iterator).
600  *
601  */
602 struct iam_iterator {
603         /*
604          * iterator flags, taken from enum iam_it_flags.
605          */
606         __u32                 ii_flags;
607         enum iam_it_state     ii_state;
608         /*
609          * path to the record. Valid in IAM_IT_ATTACHED, and IAM_IT_SKEWED
610          * states.
611          */
612         struct iam_path       ii_path;
613 };
614
615 void iam_path_init(struct iam_path *path, struct iam_container *c,
616                    struct iam_path_descr *pd);
617 void iam_path_fini(struct iam_path *path);
618 void iam_path_release(struct iam_path *path);
619
620 void iam_path_compat_init(struct iam_path_compat *path, struct inode *inode);
621 void iam_path_compat_fini(struct iam_path_compat *path);
622
623 struct iam_path_descr *iam_ipd_alloc(void *area, int keysize);
624 void iam_ipd_free(struct iam_path_descr *ipd);
625
626 int  iam_it_init(struct iam_iterator *it, struct iam_container *c, __u32 flags,
627                  struct iam_path_descr *pd);
628 void iam_it_fini(struct iam_iterator *it);
629 int iam_it_get(struct iam_iterator *it, const struct iam_key *k);
630 int iam_it_get_at(struct iam_iterator *it, const struct iam_key *k);
631 void iam_it_dup(struct iam_iterator *dst, const struct iam_iterator *src);
632 void iam_it_put(struct iam_iterator *it);
633 int iam_it_next(struct iam_iterator *it);
634 struct iam_rec *iam_it_rec_get(const struct iam_iterator *it);
635 int iam_it_rec_set(handle_t *h,
636                    struct iam_iterator *it, const struct iam_rec *r);
637 struct iam_key *iam_it_key_get(const struct iam_iterator *it);
638 int iam_it_key_size(const struct iam_iterator *it);
639 int iam_it_rec_insert(handle_t *h, struct iam_iterator *it,
640                       const struct iam_key *k, const struct iam_rec *r);
641 int iam_it_rec_delete(handle_t *h, struct iam_iterator *it);
642
643 typedef __u64 iam_pos_t;
644
645 iam_pos_t iam_it_store(const struct iam_iterator *it);
646 int iam_it_load(struct iam_iterator *it, iam_pos_t pos);
647
648 int iam_lookup(struct iam_container *c, const struct iam_key *k,
649                struct iam_rec *r, struct iam_path_descr *pd);
650 int iam_delete(handle_t *h, struct iam_container *c, const struct iam_key *k,
651                struct iam_path_descr *pd);
652 int iam_update(handle_t *h, struct iam_container *c, const struct iam_key *k,
653                const struct iam_rec *r, struct iam_path_descr *pd);
654 int iam_insert(handle_t *handle, struct iam_container *c,
655                const struct iam_key *k,
656                const struct iam_rec *r, struct iam_path_descr *pd);
657 /*
658  * Initialize container @c.
659  */
660 int iam_container_init(struct iam_container *c,
661                        struct iam_descr *descr, struct inode *inode);
662 /*
663  * Finalize container @c, release all resources.
664  */
665 void iam_container_fini(struct iam_container *c);
666
667 /*
668  * Determine container format.
669  */
670 int iam_container_setup(struct iam_container *c);
671
672 static inline struct iam_descr *iam_container_descr(struct iam_container *c)
673 {
674         return c->ic_descr;
675 }
676
677 static inline struct iam_descr *iam_path_descr(const struct iam_path *p)
678 {
679         return p->ip_container->ic_descr;
680 }
681
682 static inline struct inode *iam_path_obj(struct iam_path *p)
683 {
684         return p->ip_container->ic_object;
685 }
686
687 static inline void iam_ikeycpy(const struct iam_container *c,
688                                struct iam_ikey *k1, const struct iam_ikey *k2)
689 {
690         memcpy(k1, k2, c->ic_descr->id_ikey_size);
691 }
692
693 static inline size_t iam_entry_size(struct iam_path *p)
694 {
695         return iam_path_descr(p)->id_ikey_size + iam_path_descr(p)->id_ptr_size;
696 }
697
698 static inline struct iam_entry *iam_entry_shift(struct iam_path *p,
699                                                 struct iam_entry *entry,
700                                                 int shift)
701 {
702         void *e = entry;
703         return e + shift * iam_entry_size(p);
704 }
705
706 static inline struct iam_ikey *dx_get_ikey(struct iam_path *p,
707                                             struct iam_entry *entry,
708                                             struct iam_ikey *key)
709 {
710         return memcpy(key, entry, iam_path_descr(p)->id_ikey_size);
711 }
712
713 static inline struct iam_ikey *iam_ikey_at(struct iam_path *p,
714                                            struct iam_entry *entry)
715 {
716         return (struct iam_ikey *)entry;
717 }
718
719 static inline ptrdiff_t iam_entry_diff(struct iam_path *p,
720                                        struct iam_entry *e1,
721                                        struct iam_entry *e2)
722 {
723         ptrdiff_t diff;
724
725         diff = (void *)e1 - (void *)e2;
726         assert_corr(diff / iam_entry_size(p) * iam_entry_size(p) == diff);
727         return diff / iam_entry_size(p);
728 }
729
730 /*
731  * Helper for the frequent case, where key was already placed into @k1 by
732  * callback.
733  */
734 static inline void iam_ikeycpy0(const struct iam_container *c,
735                                 struct iam_ikey *k1, const struct iam_ikey *k2)
736 {
737         if (k1 != k2)
738                 iam_ikeycpy(c, k1, k2);
739 }
740
741 static inline int iam_ikeycmp(const struct iam_container *c,
742                               const struct iam_ikey *k1,
743                               const struct iam_ikey *k2)
744 {
745         return c->ic_descr->id_ops->id_ikeycmp(c, k1, k2);
746 }
747
748 static inline void *iam_entry_off(struct iam_entry *entry, size_t off)
749 {
750         return (void *)((char *)entry + off);
751 }
752
753 /*
754  * Leaf helpers.
755  */
756
757 static inline struct iam_path *iam_leaf_path(const struct iam_leaf *leaf)
758 {
759         return leaf->il_path;
760 }
761
762 static inline struct iam_container *
763 iam_leaf_container(const struct iam_leaf *leaf)
764 {
765         return iam_leaf_path(leaf)->ip_container;
766 }
767
768 static inline struct iam_descr *iam_leaf_descr(const struct iam_leaf *leaf)
769 {
770         return iam_leaf_container(leaf)->ic_descr;
771 }
772
773 static inline struct iam_leaf_operations *
774 iam_leaf_ops(const struct iam_leaf *leaf)
775 {
776         return iam_leaf_descr(leaf)->id_leaf_ops;
777 }
778
779 static inline void iam_reccpy(const struct iam_leaf *leaf,
780                               struct iam_rec *rec_dst)
781 {
782         iam_leaf_ops(leaf)->rec_get(leaf, rec_dst);
783 }
784
785 /*XXX These stuff put here, just because they are used by iam.c */
786 static inline unsigned dx_get_block(struct iam_path *p, struct iam_entry *entry)
787 {
788         u32 *addr;
789
790         addr = iam_entry_off(entry, iam_path_descr(p)->id_ikey_size);
791         return le32_to_cpu(get_unaligned(addr));
792 }
793
794 static inline void dx_set_block(struct iam_path *p,
795                                 struct iam_entry *entry, unsigned value)
796 {
797         u32 *addr;
798
799         addr = iam_entry_off(entry, iam_path_descr(p)->id_ikey_size);
800         put_unaligned(cpu_to_le32(value), addr);
801 }
802
803 static inline void dx_set_ikey(struct iam_path *p, struct iam_entry *entry,
804                                const struct iam_ikey *key)
805 {
806         iam_ikeycpy(p->ip_container, iam_entry_off(entry, 0), key);
807 }
808
809 struct dx_map_entry
810 {
811         u32 hash;
812         u32 offs;
813 };
814
815 struct fake_dirent {
816         __le32 inode;
817         __le16 rec_len;
818         u8 name_len;
819         u8 file_type;
820 };
821
822 struct dx_countlimit {
823         __le16 limit;
824         __le16 count;
825 };
826
827 /*
828  * dx_root_info is laid out so that if it should somehow get overlaid by a
829  * dirent the two low bits of the hash version will be zero.  Therefore, the
830  * hash version mod 4 should never be 0.  Sincerely, the paranoia department.
831  */
832
833 struct dx_root {
834         struct fake_dirent dot;
835         char dot_name[4];
836         struct fake_dirent dotdot;
837         char dotdot_name[4];
838         struct dx_root_info
839         {
840                 __le32 reserved_zero;
841                 u8 hash_version;
842                 u8 info_length; /* 8 */
843                 u8 indirect_levels;
844                 u8 unused_flags;
845         }
846         info;
847         struct {} entries[0];
848 };
849
850 struct dx_node
851 {
852         struct fake_dirent fake;
853         struct {} entries[0];
854 };
855
856
857 static inline unsigned dx_get_count(struct iam_entry *entries)
858 {
859         return le16_to_cpu(((struct dx_countlimit *) entries)->count);
860 }
861
862 static inline unsigned dx_get_limit(struct iam_entry *entries)
863 {
864         return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
865 }
866
867 static inline void dx_set_count(struct iam_entry *entries, unsigned value)
868 {
869         ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
870 }
871
872 static inline unsigned dx_node_limit(struct iam_path *p)
873 {
874         struct iam_descr *param = iam_path_descr(p);
875         unsigned entry_space   = iam_path_obj(p)->i_sb->s_blocksize -
876                 param->id_node_gap;
877         return entry_space / (param->id_ikey_size + param->id_ptr_size);
878 }
879
880 static inline unsigned dx_root_limit(struct iam_path *p)
881 {
882         struct iam_descr *param = iam_path_descr(p);
883         unsigned limit = iam_path_obj(p)->i_sb->s_blocksize -
884                 param->id_root_gap;
885         limit /= (param->id_ikey_size + param->id_ptr_size);
886         if (limit == dx_node_limit(p))
887                 limit--;
888         return limit;
889 }
890
891
892 static inline struct iam_entry *dx_get_entries(struct iam_path *path,
893                                                void *data, int root)
894 {
895         struct iam_descr *param = iam_path_descr(path);
896         return data + (root ? param->id_root_gap : param->id_node_gap);
897 }
898
899
900 static inline struct iam_entry *dx_node_get_entries(struct iam_path *path,
901                                                     struct iam_frame *frame)
902 {
903         return dx_get_entries(path,
904                               frame->bh->b_data, frame == path->ip_frames);
905 }
906
907 static inline struct iam_ikey *iam_path_ikey(const struct iam_path *path,
908                                              int nr)
909 {
910         assert(0 <= nr && nr < ARRAY_SIZE(path->ip_data->ipd_key_scratch));
911         return path->ip_data->ipd_key_scratch[nr];
912 }
913
914 static inline int iam_leaf_is_locked(const struct iam_leaf *leaf)
915 {
916         int result;
917
918         result = dynlock_is_locked(&iam_leaf_container(leaf)->ic_tree_lock,
919                                    leaf->il_curidx);
920         if (!result)
921                 dump_stack();
922         return result;
923 }
924
925 static inline int iam_frame_is_locked(struct iam_path *path,
926                                       const struct iam_frame *frame)
927 {
928         int result;
929
930         result = dynlock_is_locked(&path->ip_container->ic_tree_lock,
931                                    frame->curidx);
932         if (!result)
933                 dump_stack();
934         return result;
935 }
936
937 int dx_lookup_lock(struct iam_path *path,
938                    struct dynlock_handle **dl, enum dynlock_type lt);
939
940 void dx_insert_block(struct iam_path *path, struct iam_frame *frame,
941                      u32 hash, u32 block);
942 int dx_index_is_compat(struct iam_path *path);
943
944 int ldiskfs_htree_next_block(struct inode *dir, __u32 hash,
945                           struct iam_path *path, __u32 *start_hash);
946
947 struct buffer_head *ldiskfs_append(handle_t *handle, struct inode *inode,
948                                 u32 *block, int *err);
949 int split_index_node(handle_t *handle, struct iam_path *path,
950                      struct dynlock_handle **lh);
951 struct ldiskfs_dir_entry_2 *split_entry(struct inode *dir,
952                                      struct ldiskfs_dir_entry_2 *de,
953                                      unsigned long ino, mode_t mode,
954                                      const char *name, int namelen);
955 struct ldiskfs_dir_entry_2 *find_insertion_point(struct inode *dir,
956                                               struct buffer_head *bh,
957                                               const char *name, int namelen);
958 struct ldiskfs_dir_entry_2 *move_entries(struct inode *dir,
959                                       struct ldiskfs_dx_hash_info *hinfo,
960                                       struct buffer_head **bh1,
961                                       struct buffer_head **bh2,
962                                       __u32 *delim_hash);
963
964 extern struct iam_descr iam_htree_compat_param;
965
966 struct dynlock_handle *dx_lock_htree(struct inode *dir, unsigned long value,
967                                      enum dynlock_type lt);
968 void dx_unlock_htree(struct inode *dir, struct dynlock_handle *lh);
969
970 /*
971  * external
972  */
973 void iam_container_write_lock(struct iam_container *c);
974 void iam_container_write_unlock(struct iam_container *c);
975
976 void iam_container_read_lock(struct iam_container *c);
977 void iam_container_read_unlock(struct iam_container *c);
978
979 int iam_index_next(struct iam_container *c, struct iam_path *p);
980 int iam_read_leaf(struct iam_path *p);
981
982 int iam_node_read(struct iam_container *c, iam_ptr_t ptr,
983                   handle_t *handle, struct buffer_head **bh);
984 int iam_lvar_create(struct inode *obj,
985                     int keysize, int ptrsize, int recsize, handle_t *handle);
986
987 #ifndef swap
988 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
989 #endif
990
991 #ifdef DX_DEBUG
992 #define dxtrace(command) command
993 #else
994 #define dxtrace(command) 
995 #endif
996
997 #define BH_DXLock        25
998 #define DX_DEBUG (0)
999 #if DX_DEBUG
1000 static struct iam_lock_stats {
1001         unsigned dls_bh_lock;
1002         unsigned dls_bh_busy;
1003         unsigned dls_bh_again;
1004         unsigned dls_bh_full_again;
1005 } iam_lock_stats = { 0, };
1006 #define DX_DEVAL(x) x
1007 #else
1008 #define DX_DEVAL(x)
1009 #endif
1010
1011 static inline void iam_lock_bh(struct buffer_head volatile *bh)
1012 {
1013         DX_DEVAL(iam_lock_stats.dls_bh_lock++);
1014 #ifdef CONFIG_SMP
1015         while (cfs_test_and_set_bit(BH_DXLock, &bh->b_state)) {
1016                 DX_DEVAL(iam_lock_stats.dls_bh_busy++);
1017                 while (cfs_test_bit(BH_DXLock, &bh->b_state))
1018                         cpu_relax();
1019         }
1020 #endif
1021 }
1022
1023 static inline void iam_unlock_bh(struct buffer_head *bh)
1024 {
1025 #ifdef CONFIG_SMP
1026         smp_mb__before_clear_bit();
1027         clear_bit(BH_DXLock, &bh->b_state);
1028 #endif
1029 }
1030
1031
1032 void iam_insert_key(struct iam_path *path, struct iam_frame *frame,
1033                     const struct iam_ikey *key, iam_ptr_t ptr);
1034
1035 void iam_insert_key_lock(struct iam_path *path, struct iam_frame *frame,
1036                          const struct iam_ikey *key, iam_ptr_t ptr);
1037
1038
1039 int  iam_leaf_at_end(const struct iam_leaf *l);
1040 void iam_leaf_next(struct iam_leaf *folio);
1041 int iam_leaf_can_add(const struct iam_leaf *l,
1042                      const struct iam_key *k, const struct iam_rec *r);
1043
1044 struct iam_path *iam_leaf_path(const struct iam_leaf *leaf);
1045 struct iam_container *iam_leaf_container(const struct iam_leaf *leaf);
1046 struct iam_descr *iam_leaf_descr(const struct iam_leaf *leaf);
1047 struct iam_leaf_operations *iam_leaf_ops(const struct iam_leaf *leaf);
1048
1049
1050 int iam_node_read(struct iam_container *c, iam_ptr_t ptr,
1051                   handle_t *h, struct buffer_head **bh);
1052
1053 /*
1054  * Container format.
1055  */
1056 struct iam_format {
1057         /*
1058          * Method called to recognize container format. Should return true iff
1059          * container @c conforms to this format. This method may do IO to read
1060          * container pages.
1061          *
1062          * If container is recognized, this method sets operation vectors
1063          * ->id_ops and ->id_leaf_ops in container description (c->ic_descr),
1064          * and fills other description fields.
1065          */
1066         int (*if_guess)(struct iam_container *c);
1067         /*
1068          * Linkage into global list of container formats.
1069          */
1070         cfs_list_t if_linkage;
1071 };
1072
1073 void iam_format_register(struct iam_format *fmt);
1074 int iam_root_limit(int rootgap, int blocksize, int size);
1075
1076 void iam_lfix_format_init(void);
1077 void iam_lvar_format_init(void);
1078 void iam_htree_format_init(void);
1079
1080 int iam_lfix_create(struct inode *obj,
1081                     int keysize, int ptrsize, int recsize, handle_t *handle);
1082 struct iam_private_info;
1083
1084 void ldiskfs_iam_release(struct file *filp, struct inode *inode);
1085
1086 int iam_uapi_ioctl(struct inode * inode, struct file * filp, unsigned int cmd,
1087                    unsigned long arg);
1088
1089 /* dir.c 
1090 #if LDISKFS_INVARIANT_ON
1091 extern int ldiskfs_check_dir_entry(const char *, struct inode *,
1092                                 struct ldiskfs_dir_entry_2 *,
1093                                 struct buffer_head *, unsigned long);
1094 #else
1095 static inline int ldiskfs_check_dir_entry(const char * function,
1096                                        struct inode * dir,
1097                                        struct ldiskfs_dir_entry_2 * de,
1098                                        struct buffer_head * bh,
1099                                        unsigned long offset)
1100 {
1101         return 1;
1102 }
1103 #endif
1104 */
1105
1106 /* __KERNEL__ */
1107 #endif
1108
1109 /*
1110  * User level API. Copy exists in lustre/lustre/tests/iam_ut.c
1111  */
1112
1113 struct iam_uapi_info {
1114         __u16 iui_keysize;
1115         __u16 iui_recsize;
1116         __u16 iui_ptrsize;
1117         __u16 iui_height;
1118         char  iui_fmt_name[DX_FMT_NAME_LEN];
1119 };
1120
1121 struct iam_uapi_op {
1122         void *iul_key;
1123         void *iul_rec;
1124 };
1125
1126 struct iam_uapi_it {
1127         struct iam_uapi_op iui_op;
1128         __u16              iui_state;
1129 };
1130
1131 enum iam_ioctl_cmd {
1132         IAM_IOC_INIT     = _IOW('i', 1, struct iam_uapi_info),
1133         IAM_IOC_GETINFO  = _IOR('i', 2, struct iam_uapi_info),
1134         IAM_IOC_INSERT   = _IOR('i', 3, struct iam_uapi_op),
1135         IAM_IOC_LOOKUP   = _IOWR('i', 4, struct iam_uapi_op),
1136         IAM_IOC_DELETE   = _IOR('i', 5, struct iam_uapi_op),
1137         IAM_IOC_IT_START = _IOR('i', 6, struct iam_uapi_it),
1138         IAM_IOC_IT_NEXT  = _IOW('i', 7, struct iam_uapi_it),
1139         IAM_IOC_IT_STOP  = _IOR('i', 8, struct iam_uapi_it),
1140
1141         IAM_IOC_POLYMORPH = _IOR('i', 9, unsigned long)
1142 };
1143
1144 /* __LINUX_LUSTRE_IAM_H__ */
1145 #endif