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