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