Whamcloud - gitweb
f14faf73478a7021b8ab0cad286402432794fada
[fs/lustre-release.git] / lustre / osd-zfs / osd_index.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
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 /*
31  * Copyright (c) 2012, Intel Corporation.
32  * Use is subject to license terms.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/osd-zfs/osd_index.c
39  *
40  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41  * Author: Mike Pershin <tappro@whamcloud.com>
42  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_OSD
48
49 #include <lustre_ver.h>
50 #include <libcfs/libcfs.h>
51 #include <lustre_fsfilt.h>
52 #include <obd_support.h>
53 #include <lustre_net.h>
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
58
59 #include "osd_internal.h"
60
61 #include <sys/dnode.h>
62 #include <sys/dbuf.h>
63 #include <sys/spa.h>
64 #include <sys/stat.h>
65 #include <sys/zap.h>
66 #include <sys/spa_impl.h>
67 #include <sys/zfs_znode.h>
68 #include <sys/dmu_tx.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/dsl_prop.h>
71 #include <sys/sa_impl.h>
72 #include <sys/txg.h>
73
74 static struct dt_it *osd_index_it_init(const struct lu_env *env,
75                                        struct dt_object *dt,
76                                        __u32 unused,
77                                        struct lustre_capa *capa)
78 {
79         struct osd_thread_info  *info = osd_oti_get(env);
80         struct osd_zap_it       *it;
81         struct osd_object       *obj = osd_dt_obj(dt);
82         struct osd_device       *osd = osd_obj2dev(obj);
83         struct lu_object        *lo  = &dt->do_lu;
84         ENTRY;
85
86         /* XXX: check capa ? */
87
88         LASSERT(lu_object_exists(lo));
89         LASSERT(obj->oo_db);
90         LASSERT(udmu_object_is_zap(obj->oo_db));
91         LASSERT(info);
92
93         it = &info->oti_it_zap;
94
95         if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
96                                  obj->oo_db->db_object, 0))
97                 RETURN(ERR_PTR(-ENOMEM));
98
99         it->ozi_obj   = obj;
100         it->ozi_capa  = capa;
101         it->ozi_reset = 1;
102         lu_object_get(lo);
103
104         RETURN((struct dt_it *)it);
105 }
106
107 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
108 {
109         struct osd_zap_it *it = (struct osd_zap_it *)di;
110         struct osd_object *obj;
111         ENTRY;
112
113         LASSERT(it);
114         LASSERT(it->ozi_obj);
115
116         obj = it->ozi_obj;
117
118         udmu_zap_cursor_fini(it->ozi_zc);
119         lu_object_put(env, &obj->oo_dt.do_lu);
120
121         EXIT;
122 }
123
124
125 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
126 {
127         /* PBS: do nothing : ref are incremented at retrive and decreamented
128          *      next/finish. */
129 }
130
131 int udmu_zap_cursor_retrieve_key(const struct lu_env *env,
132                                  zap_cursor_t *zc, char *key, int max)
133 {
134         zap_attribute_t *za = &osd_oti_get(env)->oti_za;
135         int              err;
136
137         if ((err = zap_cursor_retrieve(zc, za)))
138                 return err;
139
140         if (key)
141                 strcpy(key, za->za_name);
142
143         return 0;
144 }
145
146 /*
147  * zap_cursor_retrieve read from current record.
148  * to read bytes we need to call zap_lookup explicitly.
149  */
150 int udmu_zap_cursor_retrieve_value(const struct lu_env *env,
151                 zap_cursor_t *zc,  char *buf,
152                 int buf_size, int *bytes_read)
153 {
154         zap_attribute_t *za = &osd_oti_get(env)->oti_za;
155         int err, actual_size;
156
157         if ((err = zap_cursor_retrieve(zc, za)))
158                 return err;
159
160         if (za->za_integer_length <= 0)
161                 return (ERANGE);
162
163         actual_size = za->za_integer_length * za->za_num_integers;
164
165         if (actual_size > buf_size) {
166                 actual_size = buf_size;
167                 buf_size = actual_size / za->za_integer_length;
168         } else {
169                 buf_size = za->za_num_integers;
170         }
171
172         err = -zap_lookup(zc->zc_objset, zc->zc_zapobj,
173                         za->za_name, za->za_integer_length,
174                         buf_size, buf);
175
176         if (!err)
177                 *bytes_read = actual_size;
178
179         return err;
180 }
181
182 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
183                                        int len, __u16 type)
184 {
185         const unsigned    align = sizeof(struct luda_type) - 1;
186         struct luda_type *lt;
187
188         /* check if file type is required */
189         if (attr & LUDA_TYPE) {
190                 len = (len + align) & ~align;
191
192                 lt = (void *)ent->lde_name + len;
193                 lt->lt_type = cpu_to_le16(DTTOIF(type));
194                 ent->lde_attrs |= LUDA_TYPE;
195         }
196
197         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
198 }
199
200 /*
201  * as we don't know FID, we can't use LU object, so this function
202  * partially duplicate __osd_xattr_get() which is built around
203  * LU-object and uses it to cache data like regular EA dnode, etc
204  */
205 static int osd_find_parent_by_dnode(const struct lu_env *env,
206                                     struct dt_object *o,
207                                     struct lu_fid *fid)
208 {
209         struct lustre_mdt_attrs *lma;
210         udmu_objset_t           *uos = &osd_obj2dev(osd_dt_obj(o))->od_objset;
211         struct lu_buf            buf;
212         sa_handle_t             *sa_hdl;
213         nvlist_t                *nvbuf = NULL;
214         uchar_t                 *value;
215         uint64_t                 dnode;
216         int                      rc, size;
217         ENTRY;
218
219         /* first of all, get parent dnode from own attributes */
220         LASSERT(osd_dt_obj(o)->oo_db);
221         rc = -sa_handle_get(uos->os, osd_dt_obj(o)->oo_db->db_object,
222                             NULL, SA_HDL_PRIVATE, &sa_hdl);
223         if (rc)
224                 RETURN(rc);
225
226         dnode = ZFS_NO_OBJECT;
227         rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(uos), &dnode, 8);
228         sa_handle_destroy(sa_hdl);
229         if (rc)
230                 RETURN(rc);
231
232         /* now get EA buffer */
233         rc = __osd_xattr_load(uos, dnode, &nvbuf);
234         if (rc)
235                 GOTO(regular, rc);
236
237         /* XXX: if we get that far.. should we cache the result? */
238
239         /* try to find LMA attribute */
240         LASSERT(nvbuf != NULL);
241         rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &value, &size);
242         if (rc == 0 && size >= sizeof(*lma)) {
243                 lma = (struct lustre_mdt_attrs *)value;
244                 lustre_lma_swab(lma);
245                 *fid = lma->lma_self_fid;
246                 GOTO(out, rc = 0);
247         }
248
249 regular:
250         /* no LMA attribute in SA, let's try regular EA */
251
252         /* first of all, get parent dnode storing regular EA */
253         rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
254         if (rc)
255                 GOTO(out, rc);
256
257         dnode = ZFS_NO_OBJECT;
258         rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(uos), &dnode, 8);
259         sa_handle_destroy(sa_hdl);
260         if (rc)
261                 GOTO(out, rc);
262
263         CLASSERT(sizeof(*lma) <= sizeof(osd_oti_get(env)->oti_buf));
264         buf.lb_buf = osd_oti_get(env)->oti_buf;
265         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
266
267         /* now try to find LMA */
268         rc = __osd_xattr_get_large(env, uos, dnode, &buf,
269                                    XATTR_NAME_LMA, &size);
270         if (rc == 0 && size >= sizeof(*lma)) {
271                 lma = buf.lb_buf;
272                 lustre_lma_swab(lma);
273                 *fid = lma->lma_self_fid;
274                 GOTO(out, rc = 0);
275         } else if (rc < 0) {
276                 GOTO(out, rc);
277         } else {
278                 GOTO(out, rc = -EIO);
279         }
280
281 out:
282         if (nvbuf != NULL)
283                 nvlist_free(nvbuf);
284         RETURN(rc);
285 }
286
287 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
288                                struct lu_fid *fid)
289 {
290         struct link_ea_header  *leh;
291         struct link_ea_entry   *lee;
292         struct lu_buf           buf;
293         int                     rc;
294         ENTRY;
295
296         buf.lb_buf = osd_oti_get(env)->oti_buf;
297         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
298
299         rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
300         if (rc == -ERANGE) {
301                 rc = osd_xattr_get(env, o, &LU_BUF_NULL,
302                                    XATTR_NAME_LINK, BYPASS_CAPA);
303                 if (rc < 0)
304                         RETURN(rc);
305                 LASSERT(rc > 0);
306                 OBD_ALLOC(buf.lb_buf, rc);
307                 if (buf.lb_buf == NULL)
308                         RETURN(-ENOMEM);
309                 buf.lb_len = rc;
310                 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
311         }
312         if (rc < 0)
313                 GOTO(out, rc);
314         if (rc < sizeof(*leh) + sizeof(*lee))
315                 GOTO(out, rc = -EINVAL);
316
317         leh = buf.lb_buf;
318         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
319                 leh->leh_magic = LINK_EA_MAGIC;
320                 leh->leh_reccount = __swab32(leh->leh_reccount);
321                 leh->leh_len = __swab64(leh->leh_len);
322         }
323         if (leh->leh_magic != LINK_EA_MAGIC)
324                 GOTO(out, rc = -EINVAL);
325         if (leh->leh_reccount == 0)
326                 GOTO(out, rc = -ENODATA);
327
328         lee = (struct link_ea_entry *)(leh + 1);
329         fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
330         rc = 0;
331
332 out:
333         if (buf.lb_buf != osd_oti_get(env)->oti_buf)
334                 OBD_FREE(buf.lb_buf, buf.lb_len);
335
336 #if 0
337         /* this block can be enabled for additional verification
338          * it's trying to match FID from LinkEA vs. FID from LMA */
339         if (rc == 0) {
340                 struct lu_fid fid2;
341                 int rc2;
342                 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
343                 if (rc2 == 0)
344                         if (lu_fid_eq(fid, &fid2) == 0)
345                                 CERROR("wrong parent: "DFID" != "DFID"\n",
346                                        PFID(fid), PFID(&fid2));
347         }
348 #endif
349
350         /* no LinkEA is found, let's try to find the fid in parent's LMA */
351         if (unlikely(rc != 0))
352                 rc = osd_find_parent_by_dnode(env, o, fid);
353
354         RETURN(rc);
355 }
356
357 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
358                           struct dt_rec *rec, const struct dt_key *key,
359                           struct lustre_capa *capa)
360 {
361         struct osd_thread_info *oti = osd_oti_get(env);
362         struct osd_object  *obj = osd_dt_obj(dt);
363         struct osd_device  *osd = osd_obj2dev(obj);
364         char               *name = (char *)key;
365         int                 rc;
366         ENTRY;
367
368         LASSERT(udmu_object_is_zap(obj->oo_db));
369
370         if (name[0] == '.') {
371                 if (name[1] == 0) {
372                         const struct lu_fid *f = lu_object_fid(&dt->do_lu);
373                         memcpy(rec, f, sizeof(*f));
374                         RETURN(1);
375                 } else if (name[1] == '.' && name[2] == 0) {
376                         rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
377                         RETURN(rc == 0 ? 1 : rc);
378                 }
379         }
380
381         rc = -zap_lookup(osd->od_objset.os, obj->oo_db->db_object,
382                          (char *)key, 8, sizeof(oti->oti_zde) / 8,
383                          (void *)&oti->oti_zde);
384         memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
385
386         RETURN(rc == 0 ? 1 : rc);
387 }
388
389 static int osd_declare_dir_insert(const struct lu_env *env,
390                                   struct dt_object *dt,
391                                   const struct dt_rec *rec,
392                                   const struct dt_key *key,
393                                   struct thandle *th)
394 {
395         struct osd_object  *obj = osd_dt_obj(dt);
396         struct osd_thandle *oh;
397         ENTRY;
398
399         LASSERT(th != NULL);
400         oh = container_of0(th, struct osd_thandle, ot_super);
401
402         LASSERT(obj->oo_db);
403         LASSERT(udmu_object_is_zap(obj->oo_db));
404
405         dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
406         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
407
408         RETURN(0);
409 }
410
411 /**
412  * Find the osd object for given fid.
413  *
414  * \param fid need to find the osd object having this fid
415  *
416  * \retval osd_object on success
417  * \retval        -ve on error
418  */
419 struct osd_object *osd_object_find(const struct lu_env *env,
420                                    struct dt_object *dt,
421                                    const struct lu_fid *fid)
422 {
423         struct lu_device         *ludev = dt->do_lu.lo_dev;
424         struct osd_object        *child = NULL;
425         struct lu_object         *luch;
426         struct lu_object         *lo;
427
428         /*
429          * at this point topdev might not exist yet
430          * (i.e. MGS is preparing profiles). so we can
431          * not rely on topdev and instead lookup with
432          * our device passed as topdev. this can't work
433          * if the object isn't cached yet (as osd doesn't
434          * allocate lu_header). IOW, the object must be
435          * in the cache, otherwise lu_object_alloc() crashes
436          * -bzzz
437          */
438         luch = lu_object_find_at(env, ludev, fid, NULL);
439         if (IS_ERR(luch))
440                 return (void *)luch;
441
442         if (lu_object_exists(luch)) {
443                 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
444                 if (lo != NULL)
445                         child = osd_obj(lo);
446                 else
447                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
448                                         "%s: object can't be located "DFID"\n",
449                                         osd_dev(ludev)->od_svname, PFID(fid));
450
451                 if (child == NULL) {
452                         lu_object_put(env, luch);
453                         CERROR("%s: Unable to get osd_object "DFID"\n",
454                                osd_dev(ludev)->od_svname, PFID(fid));
455                         child = ERR_PTR(-ENOENT);
456                 }
457         } else {
458                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
459                                 "%s: lu_object does not exists "DFID"\n",
460                                 osd_dev(ludev)->od_svname, PFID(fid));
461                 lu_object_put(env, luch);
462                 child = ERR_PTR(-ENOENT);
463         }
464
465         return child;
466 }
467
468 /**
469  * Put the osd object once done with it.
470  *
471  * \param obj osd object that needs to be put
472  */
473 static inline void osd_object_put(const struct lu_env *env,
474                                   struct osd_object *obj)
475 {
476         lu_object_put(env, &obj->oo_dt.do_lu);
477 }
478
479 /**
480  *      Inserts (key, value) pair in \a directory object.
481  *
482  *      \param  dt      osd index object
483  *      \param  key     key for index
484  *      \param  rec     record reference
485  *      \param  th      transaction handler
486  *      \param  capa    capability descriptor
487  *      \param  ignore_quota update should not affect quota
488  *
489  *      \retval  0  success
490  *      \retval -ve failure
491  */
492 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
493                           const struct dt_rec *rec, const struct dt_key *key,
494                           struct thandle *th, struct lustre_capa *capa,
495                           int ignore_quota)
496 {
497         struct osd_thread_info *oti = osd_oti_get(env);
498         struct osd_object   *parent = osd_dt_obj(dt);
499         struct osd_device   *osd = osd_obj2dev(parent);
500         struct lu_fid       *fid = (struct lu_fid *)rec;
501         struct osd_thandle  *oh;
502         struct osd_object   *child;
503         __u32                attr;
504         char                *name = (char *)key;
505         int                  rc;
506         ENTRY;
507
508         LASSERT(parent->oo_db);
509         LASSERT(udmu_object_is_zap(parent->oo_db));
510
511         LASSERT(dt_object_exists(dt));
512         LASSERT(osd_invariant(parent));
513
514         LASSERT(th != NULL);
515         oh = container_of0(th, struct osd_thandle, ot_super);
516
517         child = osd_object_find(env, dt, fid);
518         if (IS_ERR(child))
519                 RETURN(PTR_ERR(child));
520
521 /*
522  * to simulate old Orion setups with ./..  stored in the directories
523  */
524 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 91, 0)
525 #define OSD_ZFS_INSERT_DOTS_FOR_TESTING__
526 #endif
527
528         LASSERT(child->oo_db);
529         if (name[0] == '.') {
530                 if (name[1] == 0) {
531                         /* do not store ".", instead generate it
532                          * during iteration */
533 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
534                         GOTO(out, rc = 0);
535 #endif
536                 } else if (name[1] == '.' && name[2] == 0) {
537                         /* update parent dnode in the child.
538                          * later it will be used to generate ".." */
539                         udmu_objset_t *uos = &osd->od_objset;
540                         rc = osd_object_sa_update(parent, SA_ZPL_PARENT(uos),
541                                                   &child->oo_db->db_object,
542                                                   8, oh);
543
544 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
545                         GOTO(out, rc);
546 #endif
547                 }
548         }
549
550         CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
551         CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
552         attr = child->oo_dt.do_lu.lo_header ->loh_attr;
553         oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
554         oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
555         oti->oti_zde.lzd_fid = *fid;
556
557         /* Insert (key,oid) into ZAP */
558         rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
559                       (char *)key, 8, sizeof(oti->oti_zde) / 8,
560                       (void *)&oti->oti_zde, oh->ot_tx);
561
562 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
563 out:
564 #endif
565         osd_object_put(env, child);
566
567         RETURN(rc);
568 }
569
570 static int osd_declare_dir_delete(const struct lu_env *env,
571                                   struct dt_object *dt,
572                                   const struct dt_key *key,
573                                   struct thandle *th)
574 {
575         struct osd_object *obj = osd_dt_obj(dt);
576         struct osd_thandle *oh;
577         ENTRY;
578
579         LASSERT(dt_object_exists(dt));
580         LASSERT(osd_invariant(obj));
581
582         LASSERT(th != NULL);
583         oh = container_of0(th, struct osd_thandle, ot_super);
584
585         LASSERT(obj->oo_db);
586         LASSERT(udmu_object_is_zap(obj->oo_db));
587
588         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
589
590         RETURN(0);
591 }
592
593 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
594                           const struct dt_key *key, struct thandle *th,
595                           struct lustre_capa *capa)
596 {
597         struct osd_object *obj = osd_dt_obj(dt);
598         struct osd_device *osd = osd_obj2dev(obj);
599         struct osd_thandle *oh;
600         dmu_buf_t *zap_db = obj->oo_db;
601         char      *name = (char *)key;
602         int rc;
603         ENTRY;
604
605         LASSERT(obj->oo_db);
606         LASSERT(udmu_object_is_zap(obj->oo_db));
607
608         LASSERT(th != NULL);
609         oh = container_of0(th, struct osd_thandle, ot_super);
610
611 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
612         /*
613          * in Orion . and .. were stored in the directory (not generated up on
614          * request as now. we preserve them for backward compatibility
615          */
616         if (name[0] == '.') {
617                 if (name[1] == 0) {
618                         RETURN(0);
619                 } else if (name[1] == '.' && name[2] == 0) {
620                         RETURN(0);
621                 }
622         }
623 #endif
624
625         /* Remove key from the ZAP */
626         rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
627                          (char *) key, oh->ot_tx);
628
629 #if LUSTRE_VERSION_CODE <= OBD_OCD_VERSION(2, 4, 53, 0)
630         if (unlikely(rc == -ENOENT && name[0] == '.' &&
631             (name[1] == 0 || (name[1] == '.' && name[2] == 0))))
632                 rc = 0;
633 #endif
634         if (unlikely(rc && rc != -ENOENT))
635                 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
636
637         RETURN(rc);
638 }
639
640 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
641                                      struct dt_object *dt,
642                                      __u32 unused,
643                                      struct lustre_capa *capa)
644 {
645         struct osd_zap_it *it;
646
647         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
648         if (!IS_ERR(it))
649                 it->ozi_pos = 0;
650
651         RETURN((struct dt_it *)it);
652 }
653
654 /**
655  *  Move Iterator to record specified by \a key
656  *
657  *  \param  di      osd iterator
658  *  \param  key     key for index
659  *
660  *  \retval +ve  di points to record with least key not larger than key
661  *  \retval  0   di points to exact matched key
662  *  \retval -ve  failure
663  */
664 static int osd_dir_it_get(const struct lu_env *env,
665                           struct dt_it *di, const struct dt_key *key)
666 {
667         struct osd_zap_it *it = (struct osd_zap_it *)di;
668         struct osd_object *obj = it->ozi_obj;
669         struct osd_device *osd = osd_obj2dev(obj);
670         char              *name = (char *)key;
671         int                rc;
672         ENTRY;
673
674         LASSERT(it);
675         LASSERT(it->ozi_zc);
676
677         udmu_zap_cursor_fini(it->ozi_zc);
678
679         if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
680                                  obj->oo_db->db_object, 0))
681                 RETURN(-ENOMEM);
682
683         /* XXX: implementation of the API is broken at the moment */
684         LASSERT(((const char *)key)[0] == 0);
685
686         if (name[0] == 0) {
687                 it->ozi_pos = 0;
688                 RETURN(1);
689         }
690
691         if (name[0] == '.') {
692                 if (name[1] == 0) {
693                         it->ozi_pos = 1;
694                         GOTO(out, rc = 1);
695                 } else if (name[1] == '.' && name[2] == 0) {
696                         it->ozi_pos = 2;
697                         GOTO(out, rc = 1);
698                 }
699         }
700
701         /* neither . nor .. - some real record */
702         it->ozi_pos = 3;
703         rc = +1;
704
705 out:
706         RETURN(rc);
707 }
708
709 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
710 {
711         /* PBS: do nothing : ref are incremented at retrive and decreamented
712          *      next/finish. */
713 }
714
715 /*
716  * in Orion . and .. were stored in the directory, while ZPL
717  * and current osd-zfs generate them up on request. so, we
718  * need to ignore previously stored . and ..
719  */
720 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
721                                         zap_attribute_t *za)
722 {
723         int rc, isdot;
724
725         do {
726                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
727
728                 isdot = 0;
729                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
730                         if (za->za_name[1] == 0) {
731                                 isdot = 1;
732                         } else if (za->za_name[1] == '.' &&
733                                    za->za_name[2] == 0) {
734                                 isdot = 1;
735                         }
736                         if (unlikely(isdot))
737                                 zap_cursor_advance(it->ozi_zc);
738                 }
739         } while (unlikely(rc == 0 && isdot));
740
741         return rc;
742 }
743
744 /**
745  * to load a directory entry at a time and stored it in
746  * iterator's in-memory data structure.
747  *
748  * \param di, struct osd_it_ea, iterator's in memory structure
749  *
750  * \retval +ve, iterator reached to end
751  * \retval   0, iterator not reached to end
752  * \retval -ve, on error
753  */
754 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
755 {
756         struct osd_zap_it *it = (struct osd_zap_it *)di;
757         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
758         int                rc;
759
760         /* temp. storage should be enough for any key supported by ZFS */
761         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
762
763         /*
764          * the first ->next() moves the cursor to .
765          * the second ->next() moves the cursor to ..
766          * then we get to the real records and have to verify any exist
767          */
768         if (it->ozi_pos <= 2) {
769                 it->ozi_pos++;
770                 if (it->ozi_pos <=2)
771                         RETURN(0);
772         }
773
774         zap_cursor_advance(it->ozi_zc);
775
776         /*
777          * According to current API we need to return error if its last entry.
778          * zap_cursor_advance() does not return any value. So we need to call
779          * retrieve to check if there is any record.  We should make
780          * changes to Iterator API to not return status for this API
781          */
782         rc = osd_index_retrieve_skip_dots(it, za);
783
784         if (rc == -ENOENT) /* end of dir */
785                 RETURN(+1);
786
787         RETURN(rc);
788 }
789
790 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
791                                      const struct dt_it *di)
792 {
793         struct osd_zap_it *it = (struct osd_zap_it *)di;
794         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
795         int                rc = 0;
796         ENTRY;
797
798         if (it->ozi_pos <= 1) {
799                 it->ozi_pos = 1;
800                 RETURN((struct dt_key *)".");
801         } else if (it->ozi_pos == 2) {
802                 RETURN((struct dt_key *)"..");
803         }
804
805         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
806                 RETURN(ERR_PTR(rc));
807
808         strcpy(it->ozi_name, za->za_name);
809
810 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 91, 0)
811         if (za->za_name[0] == '.') {
812                 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
813                     za->za_name[2] == 0)) {
814                         /* we should not get onto . and ..
815                          * stored in the directory. ->next() and
816                          * other methods should prevent this
817                          */
818                         LBUG();
819                 }
820         }
821 #endif
822
823         RETURN((struct dt_key *)it->ozi_name);
824 }
825
826 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
827 {
828         struct osd_zap_it *it = (struct osd_zap_it *)di;
829         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
830         int                rc;
831         ENTRY;
832
833         if (it->ozi_pos <= 1) {
834                 it->ozi_pos = 1;
835                 RETURN(2);
836         } else if (it->ozi_pos == 2) {
837                 RETURN(3);
838         }
839
840         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
841                 rc = strlen(za->za_name);
842
843 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 99, 0)
844         if (rc == 0 && za->za_name[0] == '.') {
845                 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
846                     za->za_name[2] == 0)) {
847                         /* we should not get onto . and ..
848                          * stored in the directory. ->next() and
849                          * other methods should prevent this
850                          */
851                         LBUG();
852                 }
853         }
854 #endif
855         RETURN(rc);
856 }
857
858 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
859                           struct dt_rec *dtrec, __u32 attr)
860 {
861         struct osd_zap_it   *it = (struct osd_zap_it *)di;
862         struct lu_dirent    *lde = (struct lu_dirent *)dtrec;
863         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
864         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
865         int                  rc, namelen;
866         ENTRY;
867
868         if (it->ozi_pos <= 1) {
869                 lde->lde_hash = cpu_to_le64(1);
870                 strcpy(lde->lde_name, ".");
871                 lde->lde_namelen = cpu_to_le16(1);
872                 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
873                 lde->lde_attrs = LUDA_FID;
874                 /* append lustre attributes */
875                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
876                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
877                 it->ozi_pos = 1;
878                 GOTO(out, rc = 0);
879
880         } else if (it->ozi_pos == 2) {
881                 lde->lde_hash = cpu_to_le64(2);
882                 strcpy(lde->lde_name, "..");
883                 lde->lde_namelen = cpu_to_le16(2);
884                 lde->lde_attrs = LUDA_FID;
885                 /* append lustre attributes */
886                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
887                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
888                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
889                 /*
890                  * early Orion code was not setting LinkEA, so it's possible
891                  * some setups still have objects with no LinkEA set.
892                  * but at that time .. was a real record in the directory
893                  * so we should try to lookup .. in ZAP
894                  */
895                 if (rc != -ENOENT)
896                         GOTO(out, rc);
897         }
898
899         LASSERT(lde);
900
901         lde->lde_hash = cpu_to_le64(udmu_zap_cursor_serialize(it->ozi_zc));
902
903         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
904                 GOTO(out, rc);
905
906         namelen = strlen(za->za_name);
907         if (namelen > NAME_MAX)
908                 GOTO(out, rc = -EOVERFLOW);
909         strcpy(lde->lde_name, za->za_name);
910         lde->lde_namelen = cpu_to_le16(namelen);
911
912         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
913                 CERROR("%s: unsupported direntry format: %d %d\n",
914                        osd_obj2dev(it->ozi_obj)->od_svname,
915                        za->za_integer_length, (int)za->za_num_integers);
916
917                 GOTO(out, rc = -EIO);
918         }
919
920         rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
921                          za->za_name, za->za_integer_length, 3, zde);
922         if (rc)
923                 GOTO(out, rc);
924
925         lde->lde_fid = zde->lzd_fid;
926         lde->lde_attrs = LUDA_FID;
927
928         /* append lustre attributes */
929         osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
930
931         lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
932
933 out:
934         RETURN(rc);
935 }
936
937 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
938 {
939         struct osd_zap_it *it = (struct osd_zap_it *)di;
940         __u64              pos;
941         ENTRY;
942
943         if (it->ozi_pos <= 2)
944                 pos = it->ozi_pos;
945         else
946                 pos = udmu_zap_cursor_serialize(it->ozi_zc);
947
948         RETURN(pos);
949 }
950
951 /*
952  * return status :
953  *  rc == 0 -> end of directory.
954  *  rc >  0 -> ok, proceed.
955  *  rc <  0 -> error.  ( EOVERFLOW  can be masked.)
956  */
957 static int osd_dir_it_load(const struct lu_env *env,
958                         const struct dt_it *di, __u64 hash)
959 {
960         struct osd_zap_it *it = (struct osd_zap_it *)di;
961         struct osd_object *obj = it->ozi_obj;
962         struct osd_device *osd = osd_obj2dev(obj);
963         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
964         int                rc;
965         ENTRY;
966
967         if (it->ozi_pos != 0) {
968                 /* the cursor wasn't at the beginning
969                  * so we should reset ZAP cursor as well */
970                 udmu_zap_cursor_fini(it->ozi_zc);
971                 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
972                                          obj->oo_db->db_object, hash))
973                         RETURN(-ENOMEM);
974         }
975
976         if (hash <= 2) {
977                 it->ozi_pos = hash;
978                 rc = +1;
979         } else {
980                 it->ozi_pos = 3;
981                 /* to return whether the end has been reached */
982                 rc = osd_index_retrieve_skip_dots(it, za);
983                 if (rc == 0)
984                         rc = +1;
985                 else if (rc == -ENOENT)
986                         rc = 0;
987         }
988
989         RETURN(rc);
990 }
991
992 static struct dt_index_operations osd_dir_ops = {
993         .dio_lookup         = osd_dir_lookup,
994         .dio_declare_insert = osd_declare_dir_insert,
995         .dio_insert         = osd_dir_insert,
996         .dio_declare_delete = osd_declare_dir_delete,
997         .dio_delete         = osd_dir_delete,
998         .dio_it     = {
999                 .init     = osd_dir_it_init,
1000                 .fini     = osd_index_it_fini,
1001                 .get      = osd_dir_it_get,
1002                 .put      = osd_dir_it_put,
1003                 .next     = osd_dir_it_next,
1004                 .key      = osd_dir_it_key,
1005                 .key_size = osd_dir_it_key_size,
1006                 .rec      = osd_dir_it_rec,
1007                 .store    = osd_dir_it_store,
1008                 .load     = osd_dir_it_load
1009         }
1010 };
1011
1012 /*
1013  * Primitives for index files using binary keys.
1014  * XXX: only 64-bit keys are supported for now.
1015  */
1016
1017 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1018                         struct dt_rec *rec, const struct dt_key *key,
1019                         struct lustre_capa *capa)
1020 {
1021         struct osd_object *obj = osd_dt_obj(dt);
1022         struct osd_device *osd = osd_obj2dev(obj);
1023         int                rc;
1024         ENTRY;
1025
1026         rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1027                                 (const __u64 *)key, 1, 8, obj->oo_recsize,
1028                                 (void *)rec);
1029         RETURN(rc == 0 ? 1 : rc);
1030 }
1031
1032 static int osd_declare_index_insert(const struct lu_env *env,
1033                                     struct dt_object *dt,
1034                                     const struct dt_rec *rec,
1035                                     const struct dt_key *key,
1036                                     struct thandle *th)
1037 {
1038         struct osd_object  *obj = osd_dt_obj(dt);
1039         struct osd_thandle *oh;
1040         ENTRY;
1041
1042         LASSERT(th != NULL);
1043         oh = container_of0(th, struct osd_thandle, ot_super);
1044
1045         LASSERT(obj->oo_db);
1046
1047         dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1048
1049         /* It is not clear what API should be used for binary keys, so we pass
1050          * a null name which has the side effect of over-reserving space,
1051          * accounting for the worst case. See zap_count_write() */
1052         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1053
1054         RETURN(0);
1055 }
1056
1057 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1058                             const struct dt_rec *rec, const struct dt_key *key,
1059                             struct thandle *th, struct lustre_capa *capa,
1060                             int ignore_quota)
1061 {
1062         struct osd_object  *obj = osd_dt_obj(dt);
1063         struct osd_device  *osd = osd_obj2dev(obj);
1064         struct osd_thandle *oh;
1065         int                 rc;
1066         ENTRY;
1067
1068         LASSERT(obj->oo_db);
1069         LASSERT(dt_object_exists(dt));
1070         LASSERT(osd_invariant(obj));
1071         LASSERT(th != NULL);
1072
1073         oh = container_of0(th, struct osd_thandle, ot_super);
1074
1075         /* Insert (key,oid) into ZAP */
1076         rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
1077                              (const __u64 *)key, 1, 8, obj->oo_recsize,
1078                              (void *)rec, oh->ot_tx);
1079         RETURN(rc);
1080 }
1081
1082 static int osd_declare_index_delete(const struct lu_env *env,
1083                                     struct dt_object *dt,
1084                                     const struct dt_key *key,
1085                                     struct thandle *th)
1086 {
1087         struct osd_object  *obj = osd_dt_obj(dt);
1088         struct osd_thandle *oh;
1089         ENTRY;
1090
1091         LASSERT(dt_object_exists(dt));
1092         LASSERT(osd_invariant(obj));
1093         LASSERT(th != NULL);
1094         LASSERT(obj->oo_db);
1095
1096         oh = container_of0(th, struct osd_thandle, ot_super);
1097         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1098
1099         RETURN(0);
1100 }
1101
1102 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1103                             const struct dt_key *key, struct thandle *th,
1104                             struct lustre_capa *capa)
1105 {
1106         struct osd_object  *obj = osd_dt_obj(dt);
1107         struct osd_device  *osd = osd_obj2dev(obj);
1108         struct osd_thandle *oh;
1109         int                 rc;
1110         ENTRY;
1111
1112         LASSERT(obj->oo_db);
1113         LASSERT(th != NULL);
1114         oh = container_of0(th, struct osd_thandle, ot_super);
1115
1116         /* Remove binary key from the ZAP */
1117         rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
1118                                 (const __u64 *)key, 1, oh->ot_tx);
1119         RETURN(rc);
1120 }
1121
1122 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1123                             const struct dt_key *key)
1124 {
1125         struct osd_zap_it *it = (struct osd_zap_it *)di;
1126         struct osd_object *obj = it->ozi_obj;
1127         struct osd_device *osd = osd_obj2dev(obj);
1128         ENTRY;
1129
1130         LASSERT(it);
1131         LASSERT(it->ozi_zc);
1132
1133         /* XXX: API is broken at the moment */
1134         LASSERT(*((const __u64 *)key) == 0);
1135
1136         zap_cursor_fini(it->ozi_zc);
1137         memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1138         zap_cursor_init(it->ozi_zc, osd->od_objset.os, obj->oo_db->db_object);
1139         it->ozi_reset = 1;
1140
1141         RETURN(+1);
1142 }
1143
1144 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1145 {
1146         struct osd_zap_it *it = (struct osd_zap_it *)di;
1147         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1148         int                rc;
1149         ENTRY;
1150
1151         if (it->ozi_reset == 0)
1152                 zap_cursor_advance(it->ozi_zc);
1153         it->ozi_reset = 0;
1154
1155         /*
1156          * According to current API we need to return error if it's last entry.
1157          * zap_cursor_advance() does not return any value. So we need to call
1158          * retrieve to check if there is any record.  We should make
1159          * changes to Iterator API to not return status for this API
1160          */
1161         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1162         if (rc == -ENOENT)
1163                 RETURN(+1);
1164
1165         RETURN((rc));
1166 }
1167
1168 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1169                                        const struct dt_it *di)
1170 {
1171         struct osd_zap_it *it = (struct osd_zap_it *)di;
1172         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1173         int                rc = 0;
1174         ENTRY;
1175
1176         it->ozi_reset = 0;
1177         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1178         if (rc)
1179                 RETURN(ERR_PTR(rc));
1180
1181         /* the binary key is stored in the name */
1182         it->ozi_key = *((__u64 *)za->za_name);
1183
1184         RETURN((struct dt_key *)&it->ozi_key);
1185 }
1186
1187 static int osd_index_it_key_size(const struct lu_env *env,
1188                                 const struct dt_it *di)
1189 {
1190         /* we only support 64-bit binary keys for the time being */
1191         RETURN(sizeof(__u64));
1192 }
1193
1194 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1195                             struct dt_rec *rec, __u32 attr)
1196 {
1197         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1198         struct osd_zap_it *it = (struct osd_zap_it *)di;
1199         struct osd_object *obj = it->ozi_obj;
1200         struct osd_device *osd = osd_obj2dev(obj);
1201         int                rc;
1202         ENTRY;
1203
1204         it->ozi_reset = 0;
1205         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1206         if (rc)
1207                 RETURN(rc);
1208
1209         rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1210                                 (const __u64 *)za->za_name, 1, 8,
1211                                 obj->oo_recsize, (void *)rec);
1212         RETURN(rc);
1213 }
1214
1215 static __u64 osd_index_it_store(const struct lu_env *env,
1216                                 const struct dt_it *di)
1217 {
1218         struct osd_zap_it *it = (struct osd_zap_it *)di;
1219
1220         it->ozi_reset = 0;
1221         RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1222 }
1223
1224 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1225                              __u64 hash)
1226 {
1227         struct osd_zap_it *it = (struct osd_zap_it *)di;
1228         struct osd_object *obj = it->ozi_obj;
1229         struct osd_device *osd = osd_obj2dev(obj);
1230         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1231         int                rc;
1232         ENTRY;
1233
1234         /* close the current cursor */
1235         zap_cursor_fini(it->ozi_zc);
1236
1237         /* create a new one starting at hash */
1238         memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1239         zap_cursor_init_serialized(it->ozi_zc, osd->od_objset.os,
1240                                    obj->oo_db->db_object, hash);
1241         it->ozi_reset = 0;
1242
1243         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1244         if (rc == 0)
1245                 RETURN(+1);
1246         else if (rc == -ENOENT)
1247                 RETURN(0);
1248
1249         RETURN(rc);
1250 }
1251
1252 static struct dt_index_operations osd_index_ops = {
1253         .dio_lookup             = osd_index_lookup,
1254         .dio_declare_insert     = osd_declare_index_insert,
1255         .dio_insert             = osd_index_insert,
1256         .dio_declare_delete     = osd_declare_index_delete,
1257         .dio_delete             = osd_index_delete,
1258         .dio_it = {
1259                 .init           = osd_index_it_init,
1260                 .fini           = osd_index_it_fini,
1261                 .get            = osd_index_it_get,
1262                 .put            = osd_index_it_put,
1263                 .next           = osd_index_it_next,
1264                 .key            = osd_index_it_key,
1265                 .key_size       = osd_index_it_key_size,
1266                 .rec            = osd_index_it_rec,
1267                 .store          = osd_index_it_store,
1268                 .load           = osd_index_it_load
1269         }
1270 };
1271
1272 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1273                 const struct dt_index_features *feat)
1274 {
1275         struct osd_object *obj = osd_dt_obj(dt);
1276         ENTRY;
1277
1278         LASSERT(dt_object_exists(dt));
1279
1280         /*
1281          * XXX: implement support for fixed-size keys sorted with natural
1282          *      numerical way (not using internal hash value)
1283          */
1284         if (feat->dif_flags & DT_IND_RANGE)
1285                 RETURN(-ERANGE);
1286
1287         if (unlikely(feat == &dt_otable_features))
1288                 /* do not support oi scrub yet. */
1289                 RETURN(-ENOTSUPP);
1290
1291         LASSERT(obj->oo_db != NULL);
1292         if (likely(feat == &dt_directory_features)) {
1293                 if (udmu_object_is_zap(obj->oo_db))
1294                         dt->do_index_ops = &osd_dir_ops;
1295                 else
1296                         RETURN(-ENOTDIR);
1297         } else if (unlikely(feat == &dt_acct_features)) {
1298                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1299                 dt->do_index_ops = &osd_acct_index_ops;
1300         } else if (udmu_object_is_zap(obj->oo_db) &&
1301                    dt->do_index_ops == NULL) {
1302                 /* For index file, we don't support variable key & record sizes
1303                  * and the key has to be unique */
1304                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1305                         RETURN(-EINVAL);
1306
1307                 /* Although the zap_*_uint64() primitives support large keys, we
1308                  * limit ourselves to 64-bit keys for now */
1309                 if (feat->dif_keysize_max != sizeof(__u64) ||
1310                     feat->dif_keysize_min != sizeof(__u64))
1311                         RETURN(-EINVAL);
1312
1313                 /* As for the record size, it should be a multiple of 8 bytes
1314                  * and smaller than the maximum value length supported by ZAP.
1315                  */
1316                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1317                         RETURN(-E2BIG);
1318                 if (feat->dif_recsize_max != feat->dif_recsize_min ||
1319                     (feat->dif_recsize_max & (sizeof(__u64) - 1)))
1320                         RETURN(-EINVAL);
1321
1322                 obj->oo_recsize = feat->dif_recsize_max / sizeof(__u64);
1323                 dt->do_index_ops = &osd_index_ops;
1324         }
1325
1326         RETURN(0);
1327 }
1328