Whamcloud - gitweb
LU-7117 osp: set ptlrpc_request::rq_allow_replay properly
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_iam_lvar.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see [sun.com URL with a
18  * copy of GPLv2].
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * iam_lvar.c
37  *
38  * implementation of iam format for fixed size records, variable sized keys.
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 #include <linux/types.h>
44 #include "osd_internal.h"
45
46 /*
47  * Leaf operations.
48  */
49
50 enum {
51         IAM_LVAR_LEAF_MAGIC = 0x1973 /* This is duplicated in
52                                       * lustre/utils/create_iam.c */
53 };
54
55 /* This is duplicated in lustre/utils/create_iam.c */
56 struct lvar_leaf_header {
57         __le16 vlh_magic; /* magic number IAM_LVAR_LEAF_MAGIC */
58         __le16 vlh_used;  /* used bytes, including header */
59 };
60
61 /*
62  * Format of leaf entry:
63  *
64  * __le16 keysize
65  *     u8 key[keysize]
66  *     u8 record[rec_size]
67  *
68  * Entries are ordered in key order.
69  */
70
71 /* This is duplicated in lustre/utils/create_iam.c */
72 typedef __u32 lvar_hash_t;
73
74 /* This is duplicated in lustre/utils/create_iam.c */
75 struct lvar_leaf_entry {
76         __le32 vle_hash;
77         __le16 vle_keysize;
78         u8     vle_key[0];
79 };
80
81 #define PDIFF(ptr0, ptr1) (((char *)(ptr0)) - ((char *)(ptr1)))
82
83
84 static inline int blocksize(const struct iam_leaf *leaf)
85 {
86         return iam_leaf_container(leaf)->ic_object->i_sb->s_blocksize;
87 }
88
89 static inline const char *kchar(const struct iam_key *key)
90 {
91         return (void *)key;
92 }
93
94 static inline struct iam_lentry *lvar_lentry(const struct lvar_leaf_entry *ent)
95 {
96         return (struct iam_lentry *)ent;
97 }
98
99 static inline struct lvar_leaf_entry *lentry_lvar(const struct iam_lentry *lent)
100 {
101         return (struct lvar_leaf_entry *)lent;
102 }
103
104
105 static inline int e_keysize(const struct lvar_leaf_entry *ent)
106 {
107         return le16_to_cpu(ent->vle_keysize);
108 }
109
110 /* This is duplicated in lustre/utils/create_iam.c */
111 enum {
112         LVAR_PAD   = 4,
113         LVAR_ROUND = LVAR_PAD - 1
114 };
115
116 static inline int getsize(const struct iam_leaf *leaf, int namelen, int recsize)
117 {
118         CLASSERT(!(LVAR_PAD & (LVAR_PAD - 1)));
119
120         return (offsetof(struct lvar_leaf_entry, vle_key) +
121                 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
122 }
123
124 static inline int rec_size(const struct iam_rec *rec)
125 {
126         return *(const char *)rec;
127 }
128
129 static inline struct iam_rec *e_rec(const struct lvar_leaf_entry *ent)
130 {
131         return ((void *)ent) +
132                 offsetof(struct lvar_leaf_entry, vle_key) + e_keysize(ent);
133 }
134
135 static inline int e_size(const struct iam_leaf *leaf,
136                          const struct lvar_leaf_entry *ent)
137 {
138         return getsize(leaf, e_keysize(ent), rec_size(e_rec(ent)));
139 }
140
141 static inline char *e_char(const struct lvar_leaf_entry *ent)
142 {
143         return (char *)&ent->vle_key;
144 }
145
146 static inline struct iam_key *e_key(const struct lvar_leaf_entry *ent)
147 {
148         return (struct iam_key *)e_char(ent);
149 }
150
151 static inline lvar_hash_t e_hash(const struct lvar_leaf_entry *ent)
152 {
153         return le32_to_cpu(ent->vle_hash);
154 }
155
156 static void e_print(const struct lvar_leaf_entry *ent)
157 {
158         printk("        %p %8.8x \"%*.*s\"\n", ent, e_hash(ent),
159                e_keysize(ent), e_keysize(ent), e_char(ent));
160 }
161
162 static inline struct lvar_leaf_entry *e_next(const struct iam_leaf *leaf,
163                                              const struct lvar_leaf_entry *ent)
164 {
165         return ((void *)ent) + e_size(leaf, ent);
166 }
167
168 #define LVAR_HASH_SANDWICH  (0)
169 #define LVAR_HASH_TEA       (1)
170 #define LVAR_HASH_R5        (0)
171 #define LVAR_HASH_PREFIX    (0)
172
173 static __u32 hash_build0(const char *name, int namelen)
174 {
175         __u32 result;
176
177         if (namelen == 0)
178                 return 0;
179         if (strncmp(name, ".", 1) == 0 && namelen == 1)
180                 return 1;
181         if (strncmp(name, "..", 2) == 0 && namelen == 2)
182                 return 2;
183
184         if (LVAR_HASH_PREFIX) {
185                 result = 0;
186                 strncpy((void *)&result,
187                         name, min(namelen, (int)sizeof result));
188         } else {
189                 struct ldiskfs_dx_hash_info hinfo;
190
191                 hinfo.hash_version = LDISKFS_DX_HASH_TEA;
192                 hinfo.seed = NULL;
193                 ldiskfsfs_dirhash(name, namelen, &hinfo);
194                 result = hinfo.hash;
195                 if (LVAR_HASH_SANDWICH) {
196                         __u32 result2;
197
198                         hinfo.hash_version = LDISKFS_DX_HASH_TEA;
199                         hinfo.seed = NULL;
200                         ldiskfsfs_dirhash(name, namelen, &hinfo);
201                         result2 = hinfo.hash;
202                         result = (0xfc000000 & result2) | (0x03ffffff & result);
203                 }
204         }
205         return result;
206 }
207
208 enum {
209         HASH_GRAY_AREA = 1024,
210         HASH_MAX_SIZE  = 0x7fffffffUL
211 };
212
213 static __u32 hash_build(const char *name, int namelen)
214 {
215         __u32 hash;
216
217         hash = (hash_build0(name, namelen) << 1) & HASH_MAX_SIZE;
218         if (hash > HASH_MAX_SIZE - HASH_GRAY_AREA)
219                 hash &= HASH_GRAY_AREA - 1;
220         return hash;
221 }
222
223 static inline lvar_hash_t get_hash(const struct iam_container *bag,
224                                    const char *name, int namelen)
225 {
226         return hash_build(name, namelen);
227 }
228
229 static inline int e_eq(const struct lvar_leaf_entry *ent,
230                        const char *name, int namelen)
231 {
232         return namelen == e_keysize(ent) && !memcmp(e_char(ent), name, namelen);
233 }
234
235 static inline int e_cmp(const struct iam_leaf *leaf,
236                         const struct lvar_leaf_entry *ent, lvar_hash_t hash)
237 {
238         lvar_hash_t ehash;
239
240         ehash = e_hash(ent);
241         return ehash == hash ? 0 : (ehash < hash ? -1 : +1);
242 }
243
244 static struct lvar_leaf_header *n_head(const struct iam_leaf *l)
245 {
246         return (struct lvar_leaf_header *)l->il_bh->b_data;
247 }
248
249 static int h_used(const struct lvar_leaf_header *hdr)
250 {
251         return le16_to_cpu(hdr->vlh_used);
252 }
253
254 static void h_used_adj(const struct iam_leaf *leaf,
255                        struct lvar_leaf_header *hdr, int adj)
256 {
257         int used;
258
259         used = h_used(hdr) + adj;
260         assert_corr(sizeof *hdr <= used && used <= blocksize(leaf));
261         hdr->vlh_used = cpu_to_le16(used);
262 }
263
264 static struct lvar_leaf_entry *n_start(const struct iam_leaf *leaf)
265 {
266         return (void *)leaf->il_bh->b_data + sizeof(struct lvar_leaf_header);
267 }
268
269 static struct lvar_leaf_entry *n_end(const struct iam_leaf *l)
270 {
271         return (void *)l->il_bh->b_data + h_used(n_head(l));
272 }
273
274 static struct lvar_leaf_entry *n_cur(const struct iam_leaf *l)
275 {
276         return lentry_lvar(l->il_at);
277 }
278
279 void n_print(const struct iam_leaf *l)
280 {
281         struct lvar_leaf_entry *scan;
282
283         printk(KERN_EMERG "used: %d\n", h_used(n_head(l)));
284         for (scan = n_start(l); scan < n_end(l); scan = e_next(l, scan))
285                 e_print(scan);
286 }
287
288 #if LDISKFS_CORRECTNESS_ON
289 static int n_at_rec(const struct iam_leaf *folio)
290 {
291         return
292                 n_start(folio) <= lentry_lvar(folio->il_at) &&
293                 lentry_lvar(folio->il_at) < n_end(folio);
294 }
295
296 #if LDISKFS_INVARIANT_ON
297 static int n_invariant(const struct iam_leaf *leaf)
298 {
299         struct iam_path        *path;
300         struct lvar_leaf_entry *scan;
301         struct lvar_leaf_entry *end;
302         lvar_hash_t             hash;
303         lvar_hash_t             nexthash;
304         lvar_hash_t             starthash;
305
306         end  = n_end(leaf);
307         hash = 0;
308         path = leaf->il_path;
309
310         if (h_used(n_head(leaf)) > blocksize(leaf))
311                 return 0;
312
313         /*
314          * Delimiting key in the parent index node. Clear least bit to account
315          * for hash collision marker.
316          */
317         starthash = *(lvar_hash_t *)iam_ikey_at(path, path->ip_frame->at) & ~1;
318         for (scan = n_start(leaf); scan < end; scan = e_next(leaf, scan)) {
319                 nexthash = e_hash(scan);
320                 if (nexthash != get_hash(iam_leaf_container(leaf),
321                                          e_char(scan), e_keysize(scan))) {
322                         BREAKPOINT();
323                         return 0;
324                 }
325                 if (0 && nexthash < starthash) {
326                         /*
327                          * Unfortunately this useful invariant cannot be
328                          * reliably checked as parent node is nor necessarily
329                          * locked.
330                          */
331                         n_print(leaf);
332                         printk("%#x < %#x\n", nexthash, starthash);
333                         dump_stack();
334                         return 0;
335                 }
336                 if (nexthash < hash) {
337                         BREAKPOINT();
338                         return 0;
339                 }
340                 hash = nexthash;
341         }
342         if (scan != end) {
343                 BREAKPOINT();
344                 return 0;
345         }
346         return 1;
347 }
348 /* LDISKFS_INVARIANT_ON */
349 #endif
350
351 /* LDISKFS_CORRECTNESS_ON */
352 #endif
353
354 static struct iam_ikey *lvar_ikey(const struct iam_leaf *l,
355                                   struct iam_ikey *key)
356 {
357         lvar_hash_t *hash;
358
359         assert_corr(n_at_rec(l));
360
361         hash = (void *)key;
362         *hash = e_hash(n_cur(l));
363         return key;
364 }
365
366 static struct iam_key *lvar_key(const struct iam_leaf *l)
367 {
368         return e_key(n_cur(l));
369 }
370
371 static int lvar_key_size(const struct iam_leaf *l)
372 {
373         return e_keysize(n_cur(l));
374 }
375
376 static void lvar_start(struct iam_leaf *l)
377 {
378         l->il_at = lvar_lentry(n_start(l));
379 }
380
381 static int lvar_init(struct iam_leaf *l)
382 {
383         int result;
384         int used;
385         struct lvar_leaf_header *head;
386
387         assert_corr(l->il_bh != NULL);
388
389         head = n_head(l);
390         used = h_used(head);
391         if (le16_to_cpu(head->vlh_magic) == IAM_LVAR_LEAF_MAGIC &&
392             used <= blocksize(l)) {
393                 l->il_at = l->il_entries = lvar_lentry(n_start(l));
394                 result = 0;
395         } else {
396                 struct inode *obj;
397
398                 obj = iam_leaf_container(l)->ic_object;
399                 CERROR("Wrong magic in node %llu (#%lu): %#x != %#x or "
400                        "wrong used: %d\n",
401                        (unsigned long long)l->il_bh->b_blocknr, obj->i_ino,
402                        le16_to_cpu(head->vlh_magic), IAM_LVAR_LEAF_MAGIC,
403                        used);
404                 result = -EIO;
405         }
406         return result;
407 }
408
409 static void lvar_fini(struct iam_leaf *l)
410 {
411         l->il_entries = l->il_at = NULL;
412 }
413
414 static struct iam_rec *lvar_rec(const struct iam_leaf *l)
415 {
416         assert_corr(n_at_rec(l));
417         return e_rec(n_cur(l));
418 }
419
420 static void lvar_next(struct iam_leaf *l)
421 {
422         assert_corr(n_at_rec(l));
423         assert_corr(iam_leaf_is_locked(l));
424         l->il_at = lvar_lentry(e_next(l, n_cur(l)));
425 }
426
427 static int lvar_lookup(struct iam_leaf *leaf, const struct iam_key *k)
428 {
429         struct lvar_leaf_entry *found;
430         struct lvar_leaf_entry *scan;
431         struct lvar_leaf_entry *end;
432         int                     result;
433         const char             *name;
434         int                     namelen;
435         int                     found_equal;
436         lvar_hash_t             hash;
437         int                     last;
438
439         assert_inv(n_invariant(leaf));
440         end = n_end(leaf);
441
442         name = kchar(k);
443         namelen = strlen(name);
444         hash = get_hash(iam_leaf_container(leaf), name, namelen);
445         found = NULL;
446         found_equal = 0;
447         last = 1;
448
449         for (scan = n_start(leaf); scan < end; scan = e_next(leaf, scan)) {
450                 lvar_hash_t scan_hash;
451
452                 scan_hash = e_hash(scan);
453                 if (scan_hash < hash)
454                         found = scan;
455                 else if (scan_hash == hash) {
456                         if (e_eq(scan, name, namelen)) {
457                                 /*
458                                  * perfect match
459                                  */
460                                 leaf->il_at = lvar_lentry(scan);
461                                 return IAM_LOOKUP_EXACT;
462                         } else if (!found_equal) {
463                                         found = scan;
464                                         found_equal = 1;
465                         }
466                 } else {
467                         last = 0;
468                         break;
469                 }
470         }
471         if (found == NULL) {
472                 /*
473                  * @k is less than all hashes in the leaf.
474                  */
475                 lvar_start(leaf);
476                 result = IAM_LOOKUP_BEFORE;
477         } else {
478                 leaf->il_at = lvar_lentry(found);
479                 result = IAM_LOOKUP_OK;
480                 assert_corr(n_at_rec(leaf));
481         }
482         if (last)
483                 result |= IAM_LOOKUP_LAST;
484         assert_inv(n_invariant(leaf));
485
486         return result;
487 }
488
489 static int lvar_ilookup(struct iam_leaf *leaf, const struct iam_ikey *ik)
490 {
491         struct lvar_leaf_entry *scan;
492         struct lvar_leaf_entry *end;
493         lvar_hash_t             hash;
494
495         assert_inv(n_invariant(leaf));
496         end  = n_end(leaf);
497         hash = *(const lvar_hash_t *)ik;
498
499         lvar_start(leaf);
500         for (scan = n_start(leaf); scan < end; scan = e_next(leaf, scan)) {
501                 lvar_hash_t scan_hash;
502
503                 scan_hash = e_hash(scan);
504                 if (scan_hash > hash)
505                         return scan == n_start(leaf) ?
506                                 IAM_LOOKUP_BEFORE : IAM_LOOKUP_OK;
507                 leaf->il_at = lvar_lentry(scan);
508                 if (scan_hash == hash)
509                         return IAM_LOOKUP_EXACT;
510         }
511         assert_inv(n_invariant(leaf));
512         /*
513          * @ik is greater than any key in the node. Return last record in the
514          * node.
515          */
516         return IAM_LOOKUP_OK;
517 }
518
519 static void __lvar_key_set(struct iam_leaf *l, const struct iam_key *k)
520 {
521         memcpy(e_key(n_cur(l)), k, e_keysize(n_cur(l)));
522 }
523
524 static void lvar_key_set(struct iam_leaf *l, const struct iam_key *k)
525 {
526         assert_corr(n_at_rec(l));
527         assert_corr(strlen(kchar(k)) == e_keysize(n_cur(l)));
528         assert_corr(iam_leaf_is_locked(l));
529         __lvar_key_set(l, k);
530         assert_inv(n_invariant(l));
531 }
532
533 static int lvar_key_cmp(const struct iam_leaf *l, const struct iam_key *k)
534 {
535         lvar_hash_t hash;
536         const char *name;
537
538         name = kchar(k);
539
540         hash = get_hash(iam_leaf_container(l), name, strlen(name));
541         return e_cmp(l, n_cur(l), hash);
542 }
543
544 static int lvar_key_eq(const struct iam_leaf *l, const struct iam_key *k)
545 {
546         const char *name;
547
548         name = kchar(k);
549         return e_eq(n_cur(l), name, strlen(name));
550 }
551
552 static void __lvar_rec_set(struct iam_leaf *l, const struct iam_rec *r)
553 {
554         memcpy(e_rec(n_cur(l)), r, rec_size(r));
555 }
556
557 static void lvar_rec_set(struct iam_leaf *l, const struct iam_rec *r)
558 {
559         assert_corr(n_at_rec(l));
560         assert_corr(iam_leaf_is_locked(l));
561         __lvar_rec_set(l, r);
562         assert_inv(n_invariant(l));
563 }
564
565 static int lvar_rec_eq(const struct iam_leaf *l, const struct iam_rec *r)
566 {
567         struct iam_rec *rec = e_rec(n_cur(l));
568
569         if (rec_size(rec) != rec_size(r))
570                 return 0;
571         return !memcmp(rec, r, rec_size(r));
572 }
573
574 static void lvar_rec_get(const struct iam_leaf *l, struct iam_rec *r)
575 {
576         struct iam_rec *rec;
577
578         rec = e_rec(n_cur(l));
579         assert_corr(n_at_rec(l));
580         assert_corr(iam_leaf_is_locked(l));
581         memcpy(r, rec, rec_size(rec));
582         assert_inv(n_invariant(l));
583 }
584
585 static int lvar_can_add(const struct iam_leaf *l,
586                         const struct iam_key *k, const struct iam_rec *r)
587 {
588         assert_corr(iam_leaf_is_locked(l));
589         return
590                 h_used(n_head(l)) +
591                 getsize(l, strlen(kchar(k)), rec_size(r)) <= blocksize(l);
592 }
593
594 static int lvar_at_end(const struct iam_leaf *folio)
595 {
596         assert_corr(iam_leaf_is_locked(folio));
597         return n_cur(folio) == n_end(folio);
598 }
599
600 static void lvar_rec_add(struct iam_leaf *leaf,
601                          const struct iam_key *k, const struct iam_rec *r)
602 {
603         const char *key;
604         int   ksize;
605         int   shift;
606         void *end;
607         void *start;
608         ptrdiff_t diff;
609
610         assert_corr(lvar_can_add(leaf, k, r));
611         assert_inv(n_invariant(leaf));
612         assert_corr(iam_leaf_is_locked(leaf));
613
614         key   = kchar(k);
615         ksize = strlen(key);
616         shift = getsize(leaf, ksize, rec_size(r));
617
618         if (!lvar_at_end(leaf)) {
619                 assert_corr(n_cur(leaf) < n_end(leaf));
620                 end = n_end(leaf);
621                 if (lvar_key_cmp(leaf, k) <= 0)
622                         lvar_next(leaf);
623                 else
624                         /*
625                          * Another exceptional case: insertion with the key
626                          * less than least key in the leaf.
627                          */
628                         assert_corr(leaf->il_at == leaf->il_entries);
629
630                 start = leaf->il_at;
631                 diff  = PDIFF(end, start);
632                 assert_corr(diff >= 0);
633                 memmove(start + shift, start, diff);
634         }
635         h_used_adj(leaf, n_head(leaf), shift);
636         n_cur(leaf)->vle_keysize = cpu_to_le16(ksize);
637         n_cur(leaf)->vle_hash = cpu_to_le32(get_hash(iam_leaf_container(leaf),
638                                                      key, ksize));
639         __lvar_key_set(leaf, k);
640         __lvar_rec_set(leaf, r);
641         assert_corr(n_at_rec(leaf));
642         assert_inv(n_invariant(leaf));
643 }
644
645 static void lvar_rec_del(struct iam_leaf *leaf, int shift)
646 {
647         void *next;
648         void *end;
649         int nob;
650
651         assert_corr(n_at_rec(leaf));
652         assert_inv(n_invariant(leaf));
653         assert_corr(iam_leaf_is_locked(leaf));
654
655         end  = n_end(leaf);
656         next = e_next(leaf, n_cur(leaf));
657         nob  = e_size(leaf, n_cur(leaf));
658         memmove(leaf->il_at, next, end - next);
659         h_used_adj(leaf, n_head(leaf), -nob);
660         assert_inv(n_invariant(leaf));
661 }
662
663 static void lvar_init_new(struct iam_container *c, struct buffer_head *bh)
664 {
665         struct lvar_leaf_header *hdr;
666
667         hdr = (struct lvar_leaf_header *)bh->b_data;
668         hdr->vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC);
669         hdr->vlh_used  = sizeof *hdr;
670 }
671
672 static struct lvar_leaf_entry *find_pivot(const struct iam_leaf *leaf,
673                                           struct lvar_leaf_entry **prev)
674 {
675         void *scan;
676         void *start;
677         int threshold;
678
679         *prev = NULL;
680         threshold = blocksize(leaf) / 2;
681         for (scan = start = n_start(leaf); scan - start <= threshold;
682              *prev = scan, scan = e_next(leaf, scan)) {
683                 ;
684         }
685         return scan;
686 }
687
688 static void lvar_split(struct iam_leaf *leaf, struct buffer_head **bh,
689                        iam_ptr_t new_blknr)
690 {
691         struct lvar_leaf_entry  *first_to_move;
692         struct lvar_leaf_entry  *last_to_stay;
693         struct iam_path         *path;
694         struct lvar_leaf_header *hdr;
695         struct buffer_head      *new_leaf;
696
697         ptrdiff_t   tomove;
698         lvar_hash_t hash;
699
700         assert_inv(n_invariant(leaf));
701         assert_corr(iam_leaf_is_locked(leaf));
702
703         new_leaf = *bh;
704         path = iam_leaf_path(leaf);
705
706         hdr = (void *)new_leaf->b_data;
707
708         first_to_move = find_pivot(leaf, &last_to_stay);
709         assert_corr(last_to_stay != NULL);
710         assert_corr(e_next(leaf, last_to_stay) == first_to_move);
711
712         hash = e_hash(first_to_move);
713         if (hash == e_hash(last_to_stay))
714                 /*
715                  * Duplicate hash.
716                  */
717                 hash |= 1;
718
719         tomove = PDIFF(n_end(leaf), first_to_move);
720         memmove(hdr + 1, first_to_move, tomove);
721
722         h_used_adj(leaf, hdr, tomove);
723         h_used_adj(leaf, n_head(leaf), -tomove);
724
725         assert_corr(n_end(leaf) == first_to_move);
726
727         if (n_cur(leaf) >= first_to_move) {
728                 /*
729                  * insertion point moves into new leaf.
730                  */
731                 ptrdiff_t shift;
732                 int result;
733
734                 shift = PDIFF(leaf->il_at, first_to_move);
735                 *bh = leaf->il_bh;
736                 leaf->il_bh = new_leaf;
737                 leaf->il_curidx = new_blknr;
738
739                 assert_corr(iam_leaf_is_locked(leaf));
740                 result = lvar_init(leaf);
741                 /*
742                  * init cannot fail, as node was just initialized.
743                  */
744                 assert_corr(result == 0);
745                 leaf->il_at = ((void *)leaf->il_at) + shift;
746         }
747         /*
748          * Insert pointer to the new node (together with the least key in
749          * the node) into index node.
750          */
751         iam_insert_key_lock(path, path->ip_frame, (struct iam_ikey *)&hash,
752                             new_blknr);
753         assert_corr(n_cur(leaf) < n_end(leaf));
754         assert_inv(n_invariant(leaf));
755 }
756
757 static int lvar_leaf_empty(struct iam_leaf *leaf)
758 {
759         return h_used(n_head(leaf)) == sizeof(struct lvar_leaf_header);
760 }
761
762 static struct iam_leaf_operations lvar_leaf_ops = {
763         .init           = lvar_init,
764         .init_new       = lvar_init_new,
765         .fini           = lvar_fini,
766         .start          = lvar_start,
767         .next           = lvar_next,
768         .key            = lvar_key,
769         .ikey           = lvar_ikey,
770         .rec            = lvar_rec,
771         .key_set        = lvar_key_set,
772         .key_cmp        = lvar_key_cmp,
773         .key_eq         = lvar_key_eq,
774         .key_size       = lvar_key_size,
775         .rec_set        = lvar_rec_set,
776         .rec_eq         = lvar_rec_eq,
777         .rec_get        = lvar_rec_get,
778         .lookup         = lvar_lookup,
779         .ilookup        = lvar_ilookup,
780         .at_end         = lvar_at_end,
781         .rec_add        = lvar_rec_add,
782         .rec_del        = lvar_rec_del,
783         .can_add        = lvar_can_add,
784         .split          = lvar_split,
785         .leaf_empty     = lvar_leaf_empty,
786 };
787
788 /*
789  * Index operations.
790  */
791
792 enum {
793         /* This is duplicated in lustre/utils/create_iam.c */
794         /* egrep -i '^o?x?[olabcdef]*$' /usr/share/dict/words */
795         IAM_LVAR_ROOT_MAGIC = 0xb01dface
796 };
797
798 /* This is duplicated in lustre/utils/create_iam.c */
799 struct lvar_root {
800         __le32 vr_magic;
801         __le16 vr_recsize;
802         __le16 vr_ptrsize;
803         u8     vr_indirect_levels;
804         u8     vr_padding0;
805         __le16 vr_padding1;
806 };
807
808 static __u32 lvar_root_ptr(struct iam_container *c)
809 {
810         return 0;
811 }
812
813 static int lvar_node_init(struct iam_container *c, struct buffer_head *bh,
814                           int root)
815 {
816         return 0;
817 }
818
819 static struct iam_entry *lvar_root_inc(struct iam_container *c,
820                                        struct iam_path *path,
821                                        struct iam_frame *frame)
822 {
823         struct lvar_root *root;
824         struct iam_entry *entries;
825
826         assert_corr(iam_frame_is_locked(path, frame));
827         entries = frame->entries;
828
829         dx_set_count(entries, 2);
830         assert_corr(dx_get_limit(entries) == dx_root_limit(path));
831
832         root = (void *)frame->bh->b_data;
833         assert_corr(le64_to_cpu(root->vr_magic) == IAM_LVAR_ROOT_MAGIC);
834         root->vr_indirect_levels ++;
835         frame->at = entries = iam_entry_shift(path, entries, 1);
836         memset(iam_ikey_at(path, entries), 0,
837                iam_path_descr(path)->id_ikey_size);
838         return entries;
839 }
840
841 static int lvar_node_check(struct iam_path *path, struct iam_frame *frame)
842 {
843         unsigned count;
844         unsigned limit;
845         unsigned limit_correct;
846         struct iam_entry *entries;
847
848         entries = dx_node_get_entries(path, frame);
849
850         if (frame == path->ip_frames) {
851                 struct lvar_root *root;
852
853                 root = (void *)frame->bh->b_data;
854                 if (le32_to_cpu(root->vr_magic) != IAM_LVAR_ROOT_MAGIC)
855                         return -EIO;
856                 limit_correct = dx_root_limit(path);
857         } else
858                 limit_correct = dx_node_limit(path);
859         count = dx_get_count(entries);
860         limit = dx_get_limit(entries);
861         if (count > limit)
862                 return -EIO;
863         if (limit != limit_correct)
864                 return -EIO;
865         return 0;
866 }
867
868 static int lvar_node_load(struct iam_path *path, struct iam_frame *frame)
869 {
870         struct iam_entry *entries;
871         void *data;
872         entries = dx_node_get_entries(path, frame);
873
874         data = frame->bh->b_data;
875
876         if (frame == path->ip_frames) {
877                 struct lvar_root *root;
878                 const char *name;
879
880                 root = data;
881                 name = kchar(path->ip_key_target);
882                 path->ip_indirect = root->vr_indirect_levels;
883                 if (path->ip_ikey_target == NULL) {
884                         path->ip_ikey_target = iam_path_ikey(path, 4);
885                         *(lvar_hash_t *)path->ip_ikey_target =
886                                 get_hash(path->ip_container, name,
887                                          strlen(name));
888                 }
889         }
890         frame->entries = frame->at = entries;
891         return 0;
892 }
893
894 static int lvar_ikeycmp(const struct iam_container *c,
895                         const struct iam_ikey *k1, const struct iam_ikey *k2)
896 {
897         lvar_hash_t p1 = le32_to_cpu(*(lvar_hash_t *)k1);
898         lvar_hash_t p2 = le32_to_cpu(*(lvar_hash_t *)k2);
899
900         return p1 > p2 ? +1 : (p1 < p2 ? -1 : 0);
901 }
902
903 static struct iam_path_descr *lvar_ipd_alloc(const struct iam_container *c,
904                                              void *area)
905 {
906         return iam_ipd_alloc(area, c->ic_descr->id_ikey_size);
907 }
908
909 static void lvar_root(void *buf,
910                       int blocksize, int keysize, int ptrsize, int recsize)
911 {
912         struct lvar_root *root;
913         struct dx_countlimit *limit;
914         void                 *entry;
915         int isize;
916
917         isize = sizeof(lvar_hash_t) + ptrsize;
918         root = buf;
919         *root = (typeof(*root)) {
920                 .vr_magic            = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
921                 .vr_recsize          = cpu_to_le16(recsize),
922                 .vr_ptrsize          = cpu_to_le16(ptrsize),
923                 .vr_indirect_levels  = 0
924         };
925
926         limit = (void *)(root + 1);
927         *limit = (typeof(*limit)){
928                 /*
929                  * limit itself + one pointer to the leaf.
930                  */
931                 .count = cpu_to_le16(2),
932                 .limit = iam_root_limit(sizeof(struct lvar_root), blocksize,
933                                         sizeof (lvar_hash_t) + ptrsize)
934         };
935
936         /* To guarantee that the padding "keysize + ptrsize"
937          * covers the "dx_countlimit" and the "idle_blocks". */
938         LASSERT((keysize + ptrsize) >=
939                 (sizeof(struct dx_countlimit) + sizeof(__u32)));
940
941         entry = (void *)(limit + 1);
942         /* Put "idle_blocks" just after the limit. There was padding after
943          * the limit, the "idle_blocks" re-uses part of the padding, so no
944          * compatibility issues with old layout.
945          */
946         *(__u32 *)entry = 0;
947
948         /*
949          * Skip over @limit.
950          */
951         entry = (void *)(root + 1) + isize;
952
953         /*
954          * Entry format is <key> followed by <ptr>. In the minimal tree
955          * consisting of a root and single node, <key> is a minimal possible
956          * key.
957          */
958         *(lvar_hash_t *)entry = 0;
959         entry += sizeof(lvar_hash_t);
960         /* now @entry points to <ptr> */
961         if (ptrsize == 4)
962                 *(u_int32_t *)entry = cpu_to_le32(1);
963         else
964                 *(u_int64_t *)entry = cpu_to_le64(1);
965 }
966
967 static int lvar_esize(int namelen, int recsize)
968 {
969         return (offsetof(struct lvar_leaf_entry, vle_key) +
970                 namelen + recsize + LVAR_ROUND) & ~LVAR_ROUND;
971 }
972
973 static void lvar_leaf(void *buf,
974                       int blocksize, int keysize, int ptrsize, int recsize)
975 {
976         struct lvar_leaf_header *head;
977         struct lvar_leaf_entry  *entry;
978
979         /* form leaf */
980         head = buf;
981         *head = (typeof(*head)) {
982                 .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
983                 .vlh_used  = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
984         };
985         entry = (void *)(head + 1);
986         *entry = (typeof(*entry)) {
987                 .vle_hash    = 0,
988                 .vle_keysize = 0
989         };
990         memset(e_rec(entry), 0, recsize);
991         *(char *)e_rec(entry) = recsize;
992 }
993
994 int iam_lvar_create(struct inode *obj,
995                     int keysize, int ptrsize, int recsize, handle_t *handle)
996 {
997         struct buffer_head *root_node;
998         struct buffer_head *leaf_node;
999         struct super_block *sb;
1000
1001         u32 blknr;
1002         int result = 0;
1003         unsigned long bsize;
1004
1005         assert_corr(obj->i_size == 0);
1006
1007         sb = obj->i_sb;
1008         bsize = sb->s_blocksize;
1009         root_node = osd_ldiskfs_append(handle, obj, &blknr);
1010         if (IS_ERR(root_node))
1011                 GOTO(out, result = PTR_ERR(root_node));
1012
1013         leaf_node = osd_ldiskfs_append(handle, obj, &blknr);
1014         if (IS_ERR(leaf_node))
1015                 GOTO(out_root, result = PTR_ERR(leaf_node));
1016
1017         lvar_root(root_node->b_data, bsize, keysize, ptrsize, recsize);
1018         lvar_leaf(leaf_node->b_data, bsize, keysize, ptrsize, recsize);
1019         ldiskfs_mark_inode_dirty(handle, obj);
1020         result = ldiskfs_handle_dirty_metadata(handle, NULL, root_node);
1021         if (result == 0)
1022                 result = ldiskfs_handle_dirty_metadata(handle, NULL, leaf_node);
1023         if (result != 0)
1024                 ldiskfs_std_error(sb, result);
1025
1026         brelse(leaf_node);
1027
1028         GOTO(out_root, result);
1029
1030 out_root:
1031         brelse(root_node);
1032 out:
1033         return result;
1034 }
1035
1036 static struct iam_operations lvar_ops = {
1037         .id_root_ptr    = lvar_root_ptr,
1038         .id_node_read   = iam_node_read,
1039         .id_node_init   = lvar_node_init,
1040         .id_node_check  = lvar_node_check,
1041         .id_node_load   = lvar_node_load,
1042         .id_ikeycmp     = lvar_ikeycmp,
1043         .id_root_inc    = lvar_root_inc,
1044         .id_ipd_alloc   = lvar_ipd_alloc,
1045         .id_ipd_free    = iam_ipd_free,
1046         .id_name        = "lvar"
1047 };
1048
1049 static int lvar_guess(struct iam_container *c)
1050 {
1051         int result;
1052         struct buffer_head *bh;
1053         const struct lvar_root *root;
1054
1055         assert_corr(c->ic_object != NULL);
1056
1057         result = iam_node_read(c, lvar_root_ptr(c), NULL, &bh);
1058         if (result == 0) {
1059                 root = (void *)bh->b_data;
1060                 if (le32_to_cpu(root->vr_magic) == IAM_LVAR_ROOT_MAGIC) {
1061                         struct iam_descr *descr;
1062
1063                         descr = c->ic_descr;
1064                         descr->id_key_size  = LDISKFS_NAME_LEN;
1065                         descr->id_ikey_size = sizeof (lvar_hash_t);
1066                         descr->id_rec_size  = le16_to_cpu(root->vr_recsize);
1067                         descr->id_ptr_size  = le16_to_cpu(root->vr_ptrsize);
1068                         descr->id_root_gap  = sizeof *root;
1069                         descr->id_node_gap  = 0;
1070                         descr->id_ops       = &lvar_ops;
1071                         descr->id_leaf_ops  = &lvar_leaf_ops;
1072
1073                         c->ic_root_bh = bh;
1074                 } else {
1075                         result = -EBADF;
1076                         brelse(bh);
1077                 }
1078         }
1079         return result;
1080 }
1081
1082 static struct iam_format lvar_format = {
1083         .if_guess = lvar_guess
1084 };
1085
1086 void iam_lvar_format_init(void)
1087 {
1088         iam_format_register(&lvar_format);
1089 }
1090