ad5bbb93d5255f4556a1c6269a5a96d572b8d163
[zfs.git] / patches / zap-cursor-move-to-key.patch
1 Add a ZAP API to move a ZAP cursor to a given key.
2
3 Index: zfs+chaos4/lib/libzfscommon/include/sys/zap.h
4 ===================================================================
5 --- zfs+chaos4.orig/lib/libzfscommon/include/sys/zap.h
6 +++ zfs+chaos4/lib/libzfscommon/include/sys/zap.h
7 @@ -302,6 +302,11 @@ void zap_cursor_advance(zap_cursor_t *zc
8  uint64_t zap_cursor_serialize(zap_cursor_t *zc);
9  
10  /*
11 + * Advance the cursor to the attribute having the key.
12 + */
13 +int zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt);
14 +
15 +/*
16   * Initialize a zap cursor pointing to the position recorded by
17   * zap_cursor_serialize (in the "serialized" argument).  You can also
18   * use a "serialized" argument of 0 to start at the beginning of the
19 Index: zfs+chaos4/lib/libzfscommon/include/sys/zap_impl.h
20 ===================================================================
21 --- zfs+chaos4.orig/lib/libzfscommon/include/sys/zap_impl.h
22 +++ zfs+chaos4/lib/libzfscommon/include/sys/zap_impl.h
23 @@ -210,6 +210,7 @@ int fzap_add_cd(zap_name_t *zn,
24      uint64_t integer_size, uint64_t num_integers,
25      const void *val, uint32_t cd, dmu_tx_t *tx);
26  void fzap_upgrade(zap_t *zap, dmu_tx_t *tx);
27 +int fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn);
28  
29  #ifdef __cplusplus
30  }
31 Index: zfs+chaos4/lib/libzpool/zap.c
32 ===================================================================
33 --- zfs+chaos4.orig/lib/libzpool/zap.c
34 +++ zfs+chaos4/lib/libzpool/zap.c
35 @@ -1029,6 +1029,30 @@ zap_stats_ptrtbl(zap_t *zap, uint64_t *t
36         }
37  }
38  
39 +int fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
40 +{
41 +       int err;
42 +       zap_leaf_t *l;
43 +       zap_entry_handle_t zeh;
44 +       uint64_t hash;
45 +
46 +       if (zn->zn_name_orij && strlen(zn->zn_name_orij) > ZAP_MAXNAMELEN)
47 +               return (E2BIG);
48 +
49 +       err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
50 +       if (err != 0)
51 +               return (err);
52 +
53 +       err = zap_leaf_lookup(l, zn, &zeh);
54 +       if (err != 0)
55 +               return (err);
56 +
57 +       zc->zc_leaf = l;
58 +       zc->zc_hash = zeh.zeh_hash;
59 +       zc->zc_cd = zeh.zeh_cd;
60 +       return 0;
61 +}
62 +
63  void
64  fzap_get_stats(zap_t *zap, zap_stats_t *zs)
65  {
66 Index: zfs+chaos4/lib/libzpool/zap_micro.c
67 ===================================================================
68 --- zfs+chaos4.orig/lib/libzpool/zap_micro.c
69 +++ zfs+chaos4/lib/libzpool/zap_micro.c
70 @@ -1045,6 +1045,45 @@ zap_cursor_advance(zap_cursor_t *zc)
71         }
72  }
73  
74 +int zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
75 +{
76 +       int err = 0;
77 +       mzap_ent_t *mze;
78 +       zap_name_t *zn;
79 +
80 +       if (zc->zc_zap == NULL) {
81 +               err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
82 +                               RW_READER, TRUE, FALSE, &zc->zc_zap);
83 +               if (err)
84 +                       return (err);
85 +       } else {
86 +               rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
87 +       }
88 +
89 +       zn = zap_name_alloc(zc->zc_zap, name, mt);
90 +       if (zn == NULL) {
91 +               rw_exit(&zc->zc_zap->zap_rwlock);
92 +               return (ENOTSUP);
93 +       }
94 +
95 +       if (!zc->zc_zap->zap_ismicro) {
96 +               err = fzap_cursor_move_to_key(zc, zn);
97 +       } else {
98 +               mze = mze_find(zn);
99 +               if (mze == NULL) {
100 +                       err = (ENOENT);
101 +                       goto out;
102 +               }
103 +               zc->zc_hash = mze->mze_hash;
104 +               zc->zc_cd = mze->mze_phys.mze_cd;
105 +       }
106 +
107 +out:
108 +       zap_name_free(zn);
109 +       rw_exit(&zc->zc_zap->zap_rwlock);
110 +       return (err);
111 +}
112 +
113  int
114  zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
115  {