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