1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 * implementation of iam format for fixed size records.
39 * Author: Wang Di <wangdi@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
43 #include <linux/types.h>
44 #include "osd_internal.h"
51 IAM_LEAF_HEADER_MAGIC = 0x1976 /* This is duplicated in
52 * lustre/utils/create_iam.c */
55 /* This is duplicated in lustre/utils/create_iam.c */
56 struct iam_leaf_head {
61 static inline int iam_lfix_entry_size(const struct iam_leaf *l)
63 return iam_leaf_descr(l)->id_key_size + iam_leaf_descr(l)->id_rec_size;
66 static inline struct iam_lentry *
67 iam_lfix_shift(const struct iam_leaf *l, struct iam_lentry *entry, int shift)
69 return (void *)entry + shift * iam_lfix_entry_size(l);
72 static inline struct iam_key *iam_leaf_key_at(struct iam_lentry *entry)
74 return (struct iam_key *)entry;
77 static inline int lfix_keycmp(const struct iam_container *c,
78 const struct iam_key *k1,
79 const struct iam_key *k2)
81 return memcmp(k1, k2, c->ic_descr->id_key_size);
84 static struct iam_leaf_head *iam_get_head(const struct iam_leaf *l)
86 return (struct iam_leaf_head *)l->il_bh->b_data;
89 static struct iam_lentry *iam_entries(const struct buffer_head *bh)
91 return (void *)bh->b_data + sizeof(struct iam_leaf_head);
94 static struct iam_lentry *iam_get_lentries(const struct iam_leaf *l)
96 return iam_entries(l->il_bh);
99 static int leaf_count_limit(const struct iam_leaf *leaf)
103 free_space = iam_leaf_container(leaf)->ic_object->i_sb->s_blocksize;
104 free_space -= sizeof(struct iam_leaf_head);
105 return free_space / iam_lfix_entry_size(leaf);
108 static int lentry_count_get(const struct iam_leaf *leaf)
110 return le16_to_cpu(iam_get_head(leaf)->ill_count);
113 static void lentry_count_set(struct iam_leaf *leaf, unsigned count)
115 assert_corr(0 <= count && count <= leaf_count_limit(leaf));
116 iam_get_head(leaf)->ill_count = cpu_to_le16(count);
119 static struct iam_lentry *iam_lfix_get_end(const struct iam_leaf *l);
121 #if LDISKFS_CORRECTNESS_ON || LDISKFS_INVARIANT_ON
122 static int iam_leaf_at_rec(const struct iam_leaf *folio)
125 iam_get_lentries(folio) <= folio->il_at &&
126 folio->il_at < iam_lfix_get_end(folio);
130 static struct iam_ikey *iam_lfix_ikey(const struct iam_leaf *l,
131 struct iam_ikey *key)
134 assert_corr(iam_leaf_at_rec(l));
135 return (struct iam_ikey*)ie;
138 static struct iam_key *iam_lfix_key(const struct iam_leaf *l)
141 assert_corr(iam_leaf_at_rec(l));
142 return (struct iam_key*)ie;
145 static int iam_lfix_key_size(const struct iam_leaf *l)
147 return iam_leaf_descr(l)->id_key_size;
150 static void iam_lfix_start(struct iam_leaf *l)
152 l->il_at = iam_get_lentries(l);
155 static inline ptrdiff_t iam_lfix_diff(const struct iam_leaf *l,
156 const struct iam_lentry *e1,
157 const struct iam_lentry *e2)
162 esize = iam_lfix_entry_size(l);
163 diff = (void *)e1 - (void *)e2;
164 assert_corr(diff / esize * esize == diff);
168 static int iam_lfix_init(struct iam_leaf *l)
171 struct iam_leaf_head *ill;
174 assert_corr(l->il_bh != NULL);
176 ill = iam_get_head(l);
177 count = le16_to_cpu(ill->ill_count);
178 if (ill->ill_magic == le16_to_cpu(IAM_LEAF_HEADER_MAGIC) &&
179 0 <= count && count <= leaf_count_limit(l)) {
180 l->il_at = l->il_entries = iam_get_lentries(l);
185 obj = iam_leaf_container(l)->ic_object;
186 CERROR("Wrong magic in node %llu (#%lu): %#x != %#x or "
187 "wrong count: %d (%d)\n",
188 (unsigned long long)l->il_bh->b_blocknr, obj->i_ino,
189 ill->ill_magic, le16_to_cpu(IAM_LEAF_HEADER_MAGIC),
190 count, leaf_count_limit(l));
196 static void iam_lfix_fini(struct iam_leaf *l)
198 l->il_entries = l->il_at = NULL;
201 static struct iam_lentry *iam_lfix_get_end(const struct iam_leaf *l)
203 int count = lentry_count_get(l);
204 struct iam_lentry *ile = iam_lfix_shift(l, l->il_entries, count);
209 struct iam_rec *iam_lfix_rec(const struct iam_leaf *l)
212 assert_corr(iam_leaf_at_rec(l));
213 return e + iam_leaf_descr(l)->id_key_size;
216 static void iam_lfix_next(struct iam_leaf *l)
218 assert_corr(iam_leaf_at_rec(l));
219 l->il_at = iam_lfix_shift(l, l->il_at, 1);
226 EXPORT_SYMBOL(lfix_dump);
228 static char hdigit(char ch)
230 static char d[] = "0123456789abcdef";
234 static char *hex(char ch, char *area)
236 area[0] = hdigit(ch >> 4);
237 area[1] = hdigit(ch);
242 static void l_print(struct iam_leaf *leaf, struct iam_lentry *entry)
248 area = (char *)entry;
249 printk(CFS_KERN_EMERG "[");
250 for (i = iam_lfix_key_size(leaf); i > 0; --i, ++area)
251 printk("%s", hex(*area, h));
253 for (i = iam_leaf_descr(leaf)->id_rec_size; i > 0; --i, ++area)
254 printk("%s", hex(*area, h));
258 static void lfix_print(struct iam_leaf *leaf)
260 struct iam_lentry *entry;
264 entry = leaf->il_entries;
265 count = lentry_count_get(leaf);
266 printk(CFS_KERN_EMERG "lfix: %p %p %d\n", leaf, leaf->il_at, count);
267 for (i = 0; i < count; ++i, entry = iam_lfix_shift(leaf, entry, 1))
268 l_print(leaf, entry);
271 static int iam_lfix_lookup(struct iam_leaf *l, const struct iam_key *k)
273 struct iam_lentry *p, *q, *m, *t;
274 struct iam_container *c;
278 count = lentry_count_get(l);
280 return IAM_LOOKUP_EMPTY;
282 result = IAM_LOOKUP_OK;
283 c = iam_leaf_container(l);
286 q = iam_lfix_shift(l, p, count - 1);
287 if (lfix_keycmp(c, k, iam_leaf_key_at(p)) < 0) {
289 * @k is less than the least key in the leaf
292 result = IAM_LOOKUP_BEFORE;
293 } else if (lfix_keycmp(c, iam_leaf_key_at(q), k) <= 0) {
299 while (iam_lfix_shift(l, p, 1) != q) {
300 m = iam_lfix_shift(l, p, iam_lfix_diff(l, q, p) / 2);
301 assert_corr(p < m && m < q);
302 if (lfix_keycmp(c, iam_leaf_key_at(m), k) <= 0)
307 assert_corr(lfix_keycmp(c, iam_leaf_key_at(p), k) <= 0 &&
308 lfix_keycmp(c, k, iam_leaf_key_at(q)) < 0);
310 * skip over records with duplicate keys.
312 while (p > l->il_entries) {
313 t = iam_lfix_shift(l, p, -1);
314 if (lfix_keycmp(c, iam_leaf_key_at(t), k) == 0)
321 assert_corr(iam_leaf_at_rec(l));
323 if (lfix_keycmp(c, iam_leaf_key_at(l->il_at), k) == 0)
324 result = IAM_LOOKUP_EXACT;
332 static int iam_lfix_ilookup(struct iam_leaf *l, const struct iam_ikey *ik)
335 return IAM_LOOKUP_OK;
338 static void iam_lfix_key_set(struct iam_leaf *l, const struct iam_key *k)
340 assert_corr(iam_leaf_at_rec(l));
341 memcpy(iam_leaf_key_at(l->il_at), k, iam_leaf_descr(l)->id_key_size);
344 static int iam_lfix_key_cmp(const struct iam_leaf *l, const struct iam_key *k)
346 return lfix_keycmp(iam_leaf_container(l), iam_leaf_key_at(l->il_at), k);
349 static int iam_lfix_key_eq(const struct iam_leaf *l, const struct iam_key *k)
351 return !lfix_keycmp(iam_leaf_container(l),
352 iam_leaf_key_at(l->il_at), k);
355 static void iam_lfix_rec_set(struct iam_leaf *l, const struct iam_rec *r)
357 assert_corr(iam_leaf_at_rec(l));
358 memcpy(iam_lfix_rec(l), r, iam_leaf_descr(l)->id_rec_size);
361 static void iam_lfix_rec_get(const struct iam_leaf *l, struct iam_rec *r)
363 assert_corr(iam_leaf_at_rec(l));
364 memcpy(r, iam_lfix_rec(l), iam_leaf_descr(l)->id_rec_size);
367 static void iam_lfix_rec_add(struct iam_leaf *leaf,
368 const struct iam_key *k, const struct iam_rec *r)
370 struct iam_lentry *end;
371 struct iam_lentry *cur;
372 struct iam_lentry *start;
376 assert_corr(iam_leaf_can_add(leaf, k, r));
378 count = lentry_count_get(leaf);
380 * This branch handles two exceptional cases:
382 * - leaf positioned beyond last record, and
386 if (!iam_leaf_at_end(leaf)) {
387 end = iam_lfix_get_end(leaf);
389 if (lfix_keycmp(iam_leaf_container(leaf),
390 k, iam_leaf_key_at(cur)) >= 0)
394 * Another exceptional case: insertion with the key
395 * less than least key in the leaf.
397 assert_corr(cur == leaf->il_entries);
400 diff = (void *)end - (void *)start;
401 assert_corr(diff >= 0);
402 memmove(iam_lfix_shift(leaf, start, 1), start, diff);
404 lentry_count_set(leaf, count + 1);
405 iam_lfix_key_set(leaf, k);
406 iam_lfix_rec_set(leaf, r);
407 assert_corr(iam_leaf_at_rec(leaf));
410 static void iam_lfix_rec_del(struct iam_leaf *leaf, int shift)
412 struct iam_lentry *next, *end;
416 assert_corr(iam_leaf_at_rec(leaf));
418 count = lentry_count_get(leaf);
419 end = iam_lfix_get_end(leaf);
420 next = iam_lfix_shift(leaf, leaf->il_at, 1);
421 diff = (void *)end - (void *)next;
422 memmove(leaf->il_at, next, diff);
424 lentry_count_set(leaf, count - 1);
427 static int iam_lfix_can_add(const struct iam_leaf *l,
428 const struct iam_key *k, const struct iam_rec *r)
430 return lentry_count_get(l) < leaf_count_limit(l);
433 static int iam_lfix_at_end(const struct iam_leaf *folio)
435 return folio->il_at == iam_lfix_get_end(folio);
438 static void iam_lfix_init_new(struct iam_container *c, struct buffer_head *bh)
440 struct iam_leaf_head *hdr;
442 hdr = (struct iam_leaf_head*)bh->b_data;
443 hdr->ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC);
444 hdr->ill_count = cpu_to_le16(0);
447 static void iam_lfix_split(struct iam_leaf *l, struct buffer_head **bh,
450 struct iam_path *path;
451 struct iam_leaf_head *hdr;
452 const struct iam_ikey *pivot;
453 struct buffer_head *new_leaf;
462 path = iam_leaf_path(l);
464 hdr = (void *)new_leaf->b_data;
466 count = lentry_count_get(l);
469 start = iam_lfix_shift(l, iam_get_lentries(l), split);
470 finis = iam_lfix_shift(l, iam_get_lentries(l), count);
472 pivot = (const struct iam_ikey *)iam_leaf_key_at(start);
474 memmove(iam_entries(new_leaf), start, finis - start);
475 hdr->ill_count = count - split;
476 lentry_count_set(l, split);
477 if ((void *)l->il_at >= start) {
479 * insertion point moves into new leaf.
484 shift = iam_lfix_diff(l, l->il_at, start);
487 l->il_curidx = new_blknr;
488 result = iam_lfix_init(l);
490 * init cannot fail, as node was just initialized.
492 assert_corr(result == 0);
493 l->il_at = iam_lfix_shift(l, iam_get_lentries(l), shift);
496 * Insert pointer to the new node (together with the least key in
497 * the node) into index node.
499 iam_insert_key_lock(path, path->ip_frame, pivot, new_blknr);
502 static struct iam_leaf_operations iam_lfix_leaf_ops = {
503 .init = iam_lfix_init,
504 .init_new = iam_lfix_init_new,
505 .fini = iam_lfix_fini,
506 .start = iam_lfix_start,
507 .next = iam_lfix_next,
509 .ikey = iam_lfix_ikey,
511 .key_set = iam_lfix_key_set,
512 .key_cmp = iam_lfix_key_cmp,
513 .key_eq = iam_lfix_key_eq,
514 .key_size = iam_lfix_key_size,
515 .rec_set = iam_lfix_rec_set,
516 .rec_get = iam_lfix_rec_get,
517 .lookup = iam_lfix_lookup,
518 .ilookup = iam_lfix_ilookup,
519 .at_end = iam_lfix_at_end,
520 .rec_add = iam_lfix_rec_add,
521 .rec_del = iam_lfix_rec_del,
522 .can_add = iam_lfix_can_add,
523 .split = iam_lfix_split
531 /* This is duplicated in lustre/utils/create_iam.c */
533 * Then shalt thou see the dew-BEDABBLED wretch
534 * Turn, and return, indenting with the way;
535 * Each envious brier his weary legs doth scratch,
536 * Each shadow makes him stop, each murmur stay:
537 * For misery is trodden on by many,
538 * And being low never relieved by any.
540 IAM_LFIX_ROOT_MAGIC = 0xbedabb1edULL // d01efull
543 /* This is duplicated in lustre/utils/create_iam.c */
544 struct iam_lfix_root {
549 u8 ilr_indirect_levels;
553 static __u32 iam_lfix_root_ptr(struct iam_container *c)
558 static int iam_lfix_node_init(struct iam_container *c, struct buffer_head *bh,
564 static struct iam_entry *iam_lfix_root_inc(struct iam_container *c,
565 struct iam_path *path,
566 struct iam_frame *frame)
568 struct iam_lfix_root *root;
569 struct iam_entry *entries;
571 entries = frame->entries;
573 dx_set_count(entries, 2);
574 assert_corr(dx_get_limit(entries) == dx_root_limit(path));
576 root = (void *)frame->bh->b_data;
577 assert_corr(le64_to_cpu(root->ilr_magic) == IAM_LFIX_ROOT_MAGIC);
578 root->ilr_indirect_levels ++;
579 frame->at = entries = iam_entry_shift(path, entries, 1);
580 memset(iam_ikey_at(path, entries), 0,
581 iam_path_descr(path)->id_ikey_size);
585 static int iam_lfix_node_check(struct iam_path *path, struct iam_frame *frame)
589 unsigned limit_correct;
590 struct iam_entry *entries;
592 entries = dx_node_get_entries(path, frame);
594 if (frame == path->ip_frames) {
595 struct iam_lfix_root *root;
597 root = (void *)frame->bh->b_data;
598 if (le64_to_cpu(root->ilr_magic) != IAM_LFIX_ROOT_MAGIC) {
601 limit_correct = dx_root_limit(path);
603 limit_correct = dx_node_limit(path);
604 count = dx_get_count(entries);
605 limit = dx_get_limit(entries);
609 if (limit != limit_correct) {
615 static int iam_lfix_node_load(struct iam_path *path, struct iam_frame *frame)
617 struct iam_entry *entries;
619 entries = dx_node_get_entries(path, frame);
621 data = frame->bh->b_data;
623 if (frame == path->ip_frames) {
624 struct iam_lfix_root *root;
627 path->ip_indirect = root->ilr_indirect_levels;
628 if (path->ip_ikey_target == NULL)
629 path->ip_ikey_target =
630 (struct iam_ikey *)path->ip_key_target;
632 frame->entries = frame->at = entries;
636 static int iam_lfix_ikeycmp(const struct iam_container *c,
637 const struct iam_ikey *k1,
638 const struct iam_ikey *k2)
640 return memcmp(k1, k2, c->ic_descr->id_ikey_size);
643 static struct iam_path_descr *iam_lfix_ipd_alloc(const struct iam_container *c,
646 return iam_ipd_alloc(area, c->ic_descr->id_ikey_size);
649 static struct iam_operations iam_lfix_ops = {
650 .id_root_ptr = iam_lfix_root_ptr,
651 .id_node_read = iam_node_read,
652 .id_node_init = iam_lfix_node_init,
653 .id_node_check = iam_lfix_node_check,
654 .id_node_load = iam_lfix_node_load,
655 .id_ikeycmp = iam_lfix_ikeycmp,
656 .id_root_inc = iam_lfix_root_inc,
657 .id_ipd_alloc = iam_lfix_ipd_alloc,
658 .id_ipd_free = iam_ipd_free,
662 static int iam_lfix_guess(struct iam_container *c)
665 struct buffer_head *bh;
666 const struct iam_lfix_root *root;
668 assert_corr(c->ic_object != NULL);
670 result = iam_node_read(c, iam_lfix_root_ptr(c), NULL, &bh);
672 root = (void *)bh->b_data;
673 if (le64_to_cpu(root->ilr_magic) == IAM_LFIX_ROOT_MAGIC) {
674 struct iam_descr *descr;
677 descr->id_key_size = le16_to_cpu(root->ilr_keysize);
678 descr->id_ikey_size = le16_to_cpu(root->ilr_keysize);
679 descr->id_rec_size = le16_to_cpu(root->ilr_recsize);
680 descr->id_ptr_size = le16_to_cpu(root->ilr_ptrsize);
681 descr->id_root_gap = sizeof(struct iam_lfix_root);
682 descr->id_node_gap = 0;
683 descr->id_ops = &iam_lfix_ops;
684 descr->id_leaf_ops = &iam_lfix_leaf_ops;
692 static struct iam_format iam_lfix_format = {
693 .if_guess = iam_lfix_guess
696 void iam_lfix_format_init(void)
698 iam_format_register(&iam_lfix_format);
709 #define LFIX_ROOT_RECNO \
710 ((4096 - sizeof(struct iam_lfix_root)) / (KEYSIZE + PTRSIZE))
712 #define LFIX_INDEX_RECNO (4096 / (KEYSIZE + PTRSIZE))
714 #define LFIX_LEAF_RECNO \
715 ((4096 - sizeof(struct iam_leaf_head)) / (KEYSIZE + RECSIZE))
718 struct iam_lfix_root lr_root;
722 } lr_entry[LFIX_ROOT_RECNO];
726 struct dx_countlimit li_cl;
727 char li_padding[KEYSIZE + PTRSIZE - sizeof(struct dx_countlimit)];
731 } li_entry[LFIX_INDEX_RECNO - 1];
735 struct iam_leaf_head ll_head;
739 } ll_entry[LFIX_LEAF_RECNO];
742 #define STORE_UNALIGNED(val, dst) \
744 typeof(val) __val = (val); \
745 CLASSERT(sizeof(val) == sizeof(*(dst))); \
746 memcpy(dst, &__val, sizeof(*(dst))); \
749 static void lfix_root(void *buf,
750 int blocksize, int keysize, int ptrsize, int recsize)
752 struct iam_lfix_root *root;
753 struct dx_countlimit *limit;
757 *root = (typeof(*root)) {
758 .ilr_magic = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
759 .ilr_keysize = cpu_to_le16(keysize),
760 .ilr_recsize = cpu_to_le16(recsize),
761 .ilr_ptrsize = cpu_to_le16(ptrsize),
762 .ilr_indirect_levels = 0
765 limit = (void *)(root + 1);
766 *limit = (typeof(*limit)){
768 * limit itself + one pointer to the leaf.
770 .count = cpu_to_le16(2),
771 .limit = iam_root_limit(sizeof(struct iam_lfix_root),
772 blocksize, keysize + ptrsize)
779 entry += keysize + ptrsize;
782 * Entry format is <key> followed by <ptr>. In the minimal tree
783 * consisting of a root and single node, <key> is a minimal possible
786 * XXX: this key is hard-coded to be a sequence of 0's.
790 /* now @entry points to <ptr> */
792 STORE_UNALIGNED(cpu_to_le32(1), (u_int32_t *)entry);
794 STORE_UNALIGNED(cpu_to_le64(1), (u_int64_t *)entry);
797 static void lfix_leaf(void *buf,
798 int blocksize, int keysize, int ptrsize, int recsize)
800 struct iam_leaf_head *head;
804 *head = (struct iam_leaf_head) {
805 .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
807 * Leaf contains an entry with the smallest possible key
808 * (created by zeroing).
810 .ill_count = cpu_to_le16(1),
814 int iam_lfix_create(struct inode *obj,
815 int keysize, int ptrsize, int recsize, handle_t *handle)
817 struct buffer_head *root_node;
818 struct buffer_head *leaf_node;
819 struct super_block *sb;
825 assert_corr(obj->i_size == 0);
828 bsize = sb->s_blocksize;
829 root_node = ldiskfs_append(handle, obj, &blknr, &result);
830 leaf_node = ldiskfs_append(handle, obj, &blknr, &result);
831 if (root_node != NULL && leaf_node != NULL) {
832 lfix_root(root_node->b_data, bsize, keysize, ptrsize, recsize);
833 lfix_leaf(leaf_node->b_data, bsize, keysize, ptrsize, recsize);
834 ldiskfs_mark_inode_dirty(handle, obj);
835 result = ldiskfs_journal_dirty_metadata(handle, root_node);
837 result = ldiskfs_journal_dirty_metadata(handle, leaf_node);
839 ldiskfs_std_error(sb, result);
845 EXPORT_SYMBOL(iam_lfix_create);