Whamcloud - gitweb
LU-3157 llite: A not locked mutex can be unlocked.
[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, 2013, 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 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
480                           struct lu_fid *fid)
481 {
482         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
483         struct seq_server_site  *ss = osd_seq_site(osd);
484         int                     rc;
485         ENTRY;
486
487         if (!fid_is_norm(fid) && !fid_is_root(fid))
488                 RETURN(0);
489
490         rc = osd_fld_lookup(env, osd, fid, range);
491         if (rc != 0) {
492                 CERROR("%s: Can not lookup fld for "DFID"\n",
493                        osd_name(osd), PFID(fid));
494                 RETURN(rc);
495         }
496
497         RETURN(ss->ss_node_id != range->lsr_index);
498 }
499
500 /**
501  *      Inserts (key, value) pair in \a directory object.
502  *
503  *      \param  dt      osd index object
504  *      \param  key     key for index
505  *      \param  rec     record reference
506  *      \param  th      transaction handler
507  *      \param  capa    capability descriptor
508  *      \param  ignore_quota update should not affect quota
509  *
510  *      \retval  0  success
511  *      \retval -ve failure
512  */
513 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
514                           const struct dt_rec *rec, const struct dt_key *key,
515                           struct thandle *th, struct lustre_capa *capa,
516                           int ignore_quota)
517 {
518         struct osd_thread_info *oti = osd_oti_get(env);
519         struct osd_object   *parent = osd_dt_obj(dt);
520         struct osd_device   *osd = osd_obj2dev(parent);
521         struct lu_fid       *fid = (struct lu_fid *)rec;
522         struct osd_thandle  *oh;
523         struct osd_object   *child = NULL;
524         __u32                attr;
525         char                *name = (char *)key;
526         int                  rc;
527         ENTRY;
528
529         LASSERT(parent->oo_db);
530         LASSERT(udmu_object_is_zap(parent->oo_db));
531
532         LASSERT(dt_object_exists(dt));
533         LASSERT(osd_invariant(parent));
534
535         LASSERT(th != NULL);
536         oh = container_of0(th, struct osd_thandle, ot_super);
537
538         rc = osd_remote_fid(env, osd, fid);
539         if (rc < 0) {
540                 CERROR("%s: Can not find object "DFID": rc = %d\n",
541                        osd->od_svname, PFID(fid), rc);
542                 RETURN(rc);
543         }
544
545         if (unlikely(rc == 1)) {
546                 /* Insert remote entry */
547                 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
548                 oti->oti_zde.lzd_reg.zde_type = IFTODT(S_IFDIR & S_IFMT);
549         } else {
550                 /*
551                  * To simulate old Orion setups with ./..  stored in the
552                  * directories
553                  */
554                 /* Insert local entry */
555                 child = osd_object_find(env, dt, fid);
556                 if (IS_ERR(child))
557                         RETURN(PTR_ERR(child));
558
559                 LASSERT(child->oo_db);
560                 if (name[0] == '.') {
561                         if (name[1] == 0) {
562                                 /* do not store ".", instead generate it
563                                  * during iteration */
564                                 GOTO(out, rc = 0);
565                         } else if (name[1] == '.' && name[2] == 0) {
566                                 /* update parent dnode in the child.
567                                  * later it will be used to generate ".." */
568                                 udmu_objset_t *uos = &osd->od_objset;
569                                 rc = osd_object_sa_update(parent,
570                                                  SA_ZPL_PARENT(uos),
571                                                  &child->oo_db->db_object,
572                                                  8, oh);
573                                 GOTO(out, rc);
574                         }
575                 }
576                 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
577                 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
578                 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
579                 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
580                 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
581         }
582
583         oti->oti_zde.lzd_fid = *fid;
584         /* Insert (key,oid) into ZAP */
585         rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
586                       (char *)key, 8, sizeof(oti->oti_zde) / 8,
587                       (void *)&oti->oti_zde, oh->ot_tx);
588
589 out:
590         if (child != NULL)
591                 osd_object_put(env, child);
592
593         RETURN(rc);
594 }
595
596 static int osd_declare_dir_delete(const struct lu_env *env,
597                                   struct dt_object *dt,
598                                   const struct dt_key *key,
599                                   struct thandle *th)
600 {
601         struct osd_object *obj = osd_dt_obj(dt);
602         struct osd_thandle *oh;
603         ENTRY;
604
605         LASSERT(dt_object_exists(dt));
606         LASSERT(osd_invariant(obj));
607
608         LASSERT(th != NULL);
609         oh = container_of0(th, struct osd_thandle, ot_super);
610
611         LASSERT(obj->oo_db);
612         LASSERT(udmu_object_is_zap(obj->oo_db));
613
614         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
615
616         RETURN(0);
617 }
618
619 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
620                           const struct dt_key *key, struct thandle *th,
621                           struct lustre_capa *capa)
622 {
623         struct osd_object *obj = osd_dt_obj(dt);
624         struct osd_device *osd = osd_obj2dev(obj);
625         struct osd_thandle *oh;
626         dmu_buf_t *zap_db = obj->oo_db;
627         char      *name = (char *)key;
628         int rc;
629         ENTRY;
630
631         LASSERT(obj->oo_db);
632         LASSERT(udmu_object_is_zap(obj->oo_db));
633
634         LASSERT(th != NULL);
635         oh = container_of0(th, struct osd_thandle, ot_super);
636
637         /*
638          * In Orion . and .. were stored in the directory (not generated upon
639          * request as now). we preserve them for backward compatibility
640          */
641         if (name[0] == '.') {
642                 if (name[1] == 0) {
643                         RETURN(0);
644                 } else if (name[1] == '.' && name[2] == 0) {
645                         RETURN(0);
646                 }
647         }
648
649         /* Remove key from the ZAP */
650         rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
651                          (char *) key, oh->ot_tx);
652
653 #if LUSTRE_VERSION_CODE <= OBD_OCD_VERSION(2, 4, 53, 0)
654         if (unlikely(rc == -ENOENT && name[0] == '.' &&
655             (name[1] == 0 || (name[1] == '.' && name[2] == 0))))
656                 rc = 0;
657 #endif
658         if (unlikely(rc && rc != -ENOENT))
659                 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
660
661         RETURN(rc);
662 }
663
664 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
665                                      struct dt_object *dt,
666                                      __u32 unused,
667                                      struct lustre_capa *capa)
668 {
669         struct osd_zap_it *it;
670
671         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
672         if (!IS_ERR(it))
673                 it->ozi_pos = 0;
674
675         RETURN((struct dt_it *)it);
676 }
677
678 /**
679  *  Move Iterator to record specified by \a key
680  *
681  *  \param  di      osd iterator
682  *  \param  key     key for index
683  *
684  *  \retval +ve  di points to record with least key not larger than key
685  *  \retval  0   di points to exact matched key
686  *  \retval -ve  failure
687  */
688 static int osd_dir_it_get(const struct lu_env *env,
689                           struct dt_it *di, const struct dt_key *key)
690 {
691         struct osd_zap_it *it = (struct osd_zap_it *)di;
692         struct osd_object *obj = it->ozi_obj;
693         struct osd_device *osd = osd_obj2dev(obj);
694         char              *name = (char *)key;
695         int                rc;
696         ENTRY;
697
698         LASSERT(it);
699         LASSERT(it->ozi_zc);
700
701         udmu_zap_cursor_fini(it->ozi_zc);
702
703         if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
704                                  obj->oo_db->db_object, 0))
705                 RETURN(-ENOMEM);
706
707         /* XXX: implementation of the API is broken at the moment */
708         LASSERT(((const char *)key)[0] == 0);
709
710         if (name[0] == 0) {
711                 it->ozi_pos = 0;
712                 RETURN(1);
713         }
714
715         if (name[0] == '.') {
716                 if (name[1] == 0) {
717                         it->ozi_pos = 1;
718                         GOTO(out, rc = 1);
719                 } else if (name[1] == '.' && name[2] == 0) {
720                         it->ozi_pos = 2;
721                         GOTO(out, rc = 1);
722                 }
723         }
724
725         /* neither . nor .. - some real record */
726         it->ozi_pos = 3;
727         rc = +1;
728
729 out:
730         RETURN(rc);
731 }
732
733 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
734 {
735         /* PBS: do nothing : ref are incremented at retrive and decreamented
736          *      next/finish. */
737 }
738
739 /*
740  * in Orion . and .. were stored in the directory, while ZPL
741  * and current osd-zfs generate them up on request. so, we
742  * need to ignore previously stored . and ..
743  */
744 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
745                                         zap_attribute_t *za)
746 {
747         int rc, isdot;
748
749         do {
750                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
751
752                 isdot = 0;
753                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
754                         if (za->za_name[1] == 0) {
755                                 isdot = 1;
756                         } else if (za->za_name[1] == '.' &&
757                                    za->za_name[2] == 0) {
758                                 isdot = 1;
759                         }
760                         if (unlikely(isdot))
761                                 zap_cursor_advance(it->ozi_zc);
762                 }
763         } while (unlikely(rc == 0 && isdot));
764
765         return rc;
766 }
767
768 /**
769  * to load a directory entry at a time and stored it in
770  * iterator's in-memory data structure.
771  *
772  * \param di, struct osd_it_ea, iterator's in memory structure
773  *
774  * \retval +ve, iterator reached to end
775  * \retval   0, iterator not reached to end
776  * \retval -ve, on error
777  */
778 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
779 {
780         struct osd_zap_it *it = (struct osd_zap_it *)di;
781         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
782         int                rc;
783
784         /* temp. storage should be enough for any key supported by ZFS */
785         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
786
787         /*
788          * the first ->next() moves the cursor to .
789          * the second ->next() moves the cursor to ..
790          * then we get to the real records and have to verify any exist
791          */
792         if (it->ozi_pos <= 2) {
793                 it->ozi_pos++;
794                 if (it->ozi_pos <=2)
795                         RETURN(0);
796         }
797
798         zap_cursor_advance(it->ozi_zc);
799
800         /*
801          * According to current API we need to return error if its last entry.
802          * zap_cursor_advance() does not return any value. So we need to call
803          * retrieve to check if there is any record.  We should make
804          * changes to Iterator API to not return status for this API
805          */
806         rc = osd_index_retrieve_skip_dots(it, za);
807
808         if (rc == -ENOENT) /* end of dir */
809                 RETURN(+1);
810
811         RETURN(rc);
812 }
813
814 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
815                                      const struct dt_it *di)
816 {
817         struct osd_zap_it *it = (struct osd_zap_it *)di;
818         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
819         int                rc = 0;
820         ENTRY;
821
822         if (it->ozi_pos <= 1) {
823                 it->ozi_pos = 1;
824                 RETURN((struct dt_key *)".");
825         } else if (it->ozi_pos == 2) {
826                 RETURN((struct dt_key *)"..");
827         }
828
829         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
830                 RETURN(ERR_PTR(rc));
831
832         strcpy(it->ozi_name, za->za_name);
833
834 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
835         if (za->za_name[0] == '.') {
836                 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
837                     za->za_name[2] == 0)) {
838                         /* we should not get onto . and ..
839                          * stored in the directory. ->next() and
840                          * other methods should prevent this
841                          */
842                         LBUG();
843                 }
844         }
845 #endif
846
847         RETURN((struct dt_key *)it->ozi_name);
848 }
849
850 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
851 {
852         struct osd_zap_it *it = (struct osd_zap_it *)di;
853         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
854         int                rc;
855         ENTRY;
856
857         if (it->ozi_pos <= 1) {
858                 it->ozi_pos = 1;
859                 RETURN(2);
860         } else if (it->ozi_pos == 2) {
861                 RETURN(3);
862         }
863
864         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
865                 rc = strlen(za->za_name);
866
867 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
868         if (rc == 0 && za->za_name[0] == '.') {
869                 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
870                     za->za_name[2] == 0)) {
871                         /* we should not get onto . and ..
872                          * stored in the directory. ->next() and
873                          * other methods should prevent this
874                          */
875                         LBUG();
876                 }
877         }
878 #endif
879         RETURN(rc);
880 }
881
882 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
883                           struct dt_rec *dtrec, __u32 attr)
884 {
885         struct osd_zap_it   *it = (struct osd_zap_it *)di;
886         struct lu_dirent    *lde = (struct lu_dirent *)dtrec;
887         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
888         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
889         int                  rc, namelen;
890         ENTRY;
891
892         if (it->ozi_pos <= 1) {
893                 lde->lde_hash = cpu_to_le64(1);
894                 strcpy(lde->lde_name, ".");
895                 lde->lde_namelen = cpu_to_le16(1);
896                 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
897                 lde->lde_attrs = LUDA_FID;
898                 /* append lustre attributes */
899                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
900                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
901                 it->ozi_pos = 1;
902                 GOTO(out, rc = 0);
903
904         } else if (it->ozi_pos == 2) {
905                 lde->lde_hash = cpu_to_le64(2);
906                 strcpy(lde->lde_name, "..");
907                 lde->lde_namelen = cpu_to_le16(2);
908                 lde->lde_attrs = LUDA_FID;
909                 /* append lustre attributes */
910                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
911                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
912                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
913                 /*
914                  * early Orion code was not setting LinkEA, so it's possible
915                  * some setups still have objects with no LinkEA set.
916                  * but at that time .. was a real record in the directory
917                  * so we should try to lookup .. in ZAP
918                  */
919                 if (rc != -ENOENT)
920                         GOTO(out, rc);
921         }
922
923         LASSERT(lde);
924
925         lde->lde_hash = cpu_to_le64(udmu_zap_cursor_serialize(it->ozi_zc));
926
927         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
928                 GOTO(out, rc);
929
930         namelen = strlen(za->za_name);
931         if (namelen > NAME_MAX)
932                 GOTO(out, rc = -EOVERFLOW);
933         strcpy(lde->lde_name, za->za_name);
934         lde->lde_namelen = cpu_to_le16(namelen);
935
936         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
937                 CERROR("%s: unsupported direntry format: %d %d\n",
938                        osd_obj2dev(it->ozi_obj)->od_svname,
939                        za->za_integer_length, (int)za->za_num_integers);
940
941                 GOTO(out, rc = -EIO);
942         }
943
944         rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
945                          za->za_name, za->za_integer_length, 3, zde);
946         if (rc)
947                 GOTO(out, rc);
948
949         lde->lde_fid = zde->lzd_fid;
950         lde->lde_attrs = LUDA_FID;
951
952         /* append lustre attributes */
953         osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
954
955         lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
956
957 out:
958         RETURN(rc);
959 }
960
961 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
962 {
963         struct osd_zap_it *it = (struct osd_zap_it *)di;
964         __u64              pos;
965         ENTRY;
966
967         if (it->ozi_pos <= 2)
968                 pos = it->ozi_pos;
969         else
970                 pos = udmu_zap_cursor_serialize(it->ozi_zc);
971
972         RETURN(pos);
973 }
974
975 /*
976  * return status :
977  *  rc == 0 -> end of directory.
978  *  rc >  0 -> ok, proceed.
979  *  rc <  0 -> error.  ( EOVERFLOW  can be masked.)
980  */
981 static int osd_dir_it_load(const struct lu_env *env,
982                         const struct dt_it *di, __u64 hash)
983 {
984         struct osd_zap_it *it = (struct osd_zap_it *)di;
985         struct osd_object *obj = it->ozi_obj;
986         struct osd_device *osd = osd_obj2dev(obj);
987         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
988         int                rc;
989         ENTRY;
990
991         udmu_zap_cursor_fini(it->ozi_zc);
992         if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
993                                  obj->oo_db->db_object, hash))
994                 RETURN(-ENOMEM);
995
996         if (hash <= 2) {
997                 it->ozi_pos = hash;
998                 rc = +1;
999         } else {
1000                 it->ozi_pos = 3;
1001                 /* to return whether the end has been reached */
1002                 rc = osd_index_retrieve_skip_dots(it, za);
1003                 if (rc == 0)
1004                         rc = +1;
1005                 else if (rc == -ENOENT)
1006                         rc = 0;
1007         }
1008
1009         RETURN(rc);
1010 }
1011
1012 static struct dt_index_operations osd_dir_ops = {
1013         .dio_lookup         = osd_dir_lookup,
1014         .dio_declare_insert = osd_declare_dir_insert,
1015         .dio_insert         = osd_dir_insert,
1016         .dio_declare_delete = osd_declare_dir_delete,
1017         .dio_delete         = osd_dir_delete,
1018         .dio_it     = {
1019                 .init     = osd_dir_it_init,
1020                 .fini     = osd_index_it_fini,
1021                 .get      = osd_dir_it_get,
1022                 .put      = osd_dir_it_put,
1023                 .next     = osd_dir_it_next,
1024                 .key      = osd_dir_it_key,
1025                 .key_size = osd_dir_it_key_size,
1026                 .rec      = osd_dir_it_rec,
1027                 .store    = osd_dir_it_store,
1028                 .load     = osd_dir_it_load
1029         }
1030 };
1031
1032 /*
1033  * Primitives for index files using binary keys.
1034  * XXX: only 64-bit keys are supported for now.
1035  */
1036
1037 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1038                         struct dt_rec *rec, const struct dt_key *key,
1039                         struct lustre_capa *capa)
1040 {
1041         struct osd_object *obj = osd_dt_obj(dt);
1042         struct osd_device *osd = osd_obj2dev(obj);
1043         int                rc;
1044         ENTRY;
1045
1046         rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1047                                 (const __u64 *)key, 1, 8, obj->oo_recsize,
1048                                 (void *)rec);
1049         RETURN(rc == 0 ? 1 : rc);
1050 }
1051
1052 static int osd_declare_index_insert(const struct lu_env *env,
1053                                     struct dt_object *dt,
1054                                     const struct dt_rec *rec,
1055                                     const struct dt_key *key,
1056                                     struct thandle *th)
1057 {
1058         struct osd_object  *obj = osd_dt_obj(dt);
1059         struct osd_thandle *oh;
1060         ENTRY;
1061
1062         LASSERT(th != NULL);
1063         oh = container_of0(th, struct osd_thandle, ot_super);
1064
1065         LASSERT(obj->oo_db);
1066
1067         dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1068
1069         /* It is not clear what API should be used for binary keys, so we pass
1070          * a null name which has the side effect of over-reserving space,
1071          * accounting for the worst case. See zap_count_write() */
1072         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1073
1074         RETURN(0);
1075 }
1076
1077 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1078                             const struct dt_rec *rec, const struct dt_key *key,
1079                             struct thandle *th, struct lustre_capa *capa,
1080                             int ignore_quota)
1081 {
1082         struct osd_object  *obj = osd_dt_obj(dt);
1083         struct osd_device  *osd = osd_obj2dev(obj);
1084         struct osd_thandle *oh;
1085         int                 rc;
1086         ENTRY;
1087
1088         LASSERT(obj->oo_db);
1089         LASSERT(dt_object_exists(dt));
1090         LASSERT(osd_invariant(obj));
1091         LASSERT(th != NULL);
1092
1093         oh = container_of0(th, struct osd_thandle, ot_super);
1094
1095         /* Insert (key,oid) into ZAP */
1096         rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
1097                              (const __u64 *)key, 1, 8, obj->oo_recsize,
1098                              (void *)rec, oh->ot_tx);
1099         RETURN(rc);
1100 }
1101
1102 static int osd_declare_index_delete(const struct lu_env *env,
1103                                     struct dt_object *dt,
1104                                     const struct dt_key *key,
1105                                     struct thandle *th)
1106 {
1107         struct osd_object  *obj = osd_dt_obj(dt);
1108         struct osd_thandle *oh;
1109         ENTRY;
1110
1111         LASSERT(dt_object_exists(dt));
1112         LASSERT(osd_invariant(obj));
1113         LASSERT(th != NULL);
1114         LASSERT(obj->oo_db);
1115
1116         oh = container_of0(th, struct osd_thandle, ot_super);
1117         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1118
1119         RETURN(0);
1120 }
1121
1122 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1123                             const struct dt_key *key, struct thandle *th,
1124                             struct lustre_capa *capa)
1125 {
1126         struct osd_object  *obj = osd_dt_obj(dt);
1127         struct osd_device  *osd = osd_obj2dev(obj);
1128         struct osd_thandle *oh;
1129         int                 rc;
1130         ENTRY;
1131
1132         LASSERT(obj->oo_db);
1133         LASSERT(th != NULL);
1134         oh = container_of0(th, struct osd_thandle, ot_super);
1135
1136         /* Remove binary key from the ZAP */
1137         rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
1138                                 (const __u64 *)key, 1, oh->ot_tx);
1139         RETURN(rc);
1140 }
1141
1142 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1143                             const struct dt_key *key)
1144 {
1145         struct osd_zap_it *it = (struct osd_zap_it *)di;
1146         struct osd_object *obj = it->ozi_obj;
1147         struct osd_device *osd = osd_obj2dev(obj);
1148         ENTRY;
1149
1150         LASSERT(it);
1151         LASSERT(it->ozi_zc);
1152
1153         /* XXX: API is broken at the moment */
1154         LASSERT(*((const __u64 *)key) == 0);
1155
1156         zap_cursor_fini(it->ozi_zc);
1157         memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1158         zap_cursor_init(it->ozi_zc, osd->od_objset.os, obj->oo_db->db_object);
1159         it->ozi_reset = 1;
1160
1161         RETURN(+1);
1162 }
1163
1164 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1165 {
1166         struct osd_zap_it *it = (struct osd_zap_it *)di;
1167         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1168         int                rc;
1169         ENTRY;
1170
1171         if (it->ozi_reset == 0)
1172                 zap_cursor_advance(it->ozi_zc);
1173         it->ozi_reset = 0;
1174
1175         /*
1176          * According to current API we need to return error if it's last entry.
1177          * zap_cursor_advance() does not return any value. So we need to call
1178          * retrieve to check if there is any record.  We should make
1179          * changes to Iterator API to not return status for this API
1180          */
1181         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1182         if (rc == -ENOENT)
1183                 RETURN(+1);
1184
1185         RETURN((rc));
1186 }
1187
1188 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1189                                        const struct dt_it *di)
1190 {
1191         struct osd_zap_it *it = (struct osd_zap_it *)di;
1192         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1193         int                rc = 0;
1194         ENTRY;
1195
1196         it->ozi_reset = 0;
1197         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1198         if (rc)
1199                 RETURN(ERR_PTR(rc));
1200
1201         /* the binary key is stored in the name */
1202         it->ozi_key = *((__u64 *)za->za_name);
1203
1204         RETURN((struct dt_key *)&it->ozi_key);
1205 }
1206
1207 static int osd_index_it_key_size(const struct lu_env *env,
1208                                 const struct dt_it *di)
1209 {
1210         /* we only support 64-bit binary keys for the time being */
1211         RETURN(sizeof(__u64));
1212 }
1213
1214 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1215                             struct dt_rec *rec, __u32 attr)
1216 {
1217         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1218         struct osd_zap_it *it = (struct osd_zap_it *)di;
1219         struct osd_object *obj = it->ozi_obj;
1220         struct osd_device *osd = osd_obj2dev(obj);
1221         int                rc;
1222         ENTRY;
1223
1224         it->ozi_reset = 0;
1225         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1226         if (rc)
1227                 RETURN(rc);
1228
1229         rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1230                                 (const __u64 *)za->za_name, 1, 8,
1231                                 obj->oo_recsize, (void *)rec);
1232         RETURN(rc);
1233 }
1234
1235 static __u64 osd_index_it_store(const struct lu_env *env,
1236                                 const struct dt_it *di)
1237 {
1238         struct osd_zap_it *it = (struct osd_zap_it *)di;
1239
1240         it->ozi_reset = 0;
1241         RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1242 }
1243
1244 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1245                              __u64 hash)
1246 {
1247         struct osd_zap_it *it = (struct osd_zap_it *)di;
1248         struct osd_object *obj = it->ozi_obj;
1249         struct osd_device *osd = osd_obj2dev(obj);
1250         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1251         int                rc;
1252         ENTRY;
1253
1254         /* close the current cursor */
1255         zap_cursor_fini(it->ozi_zc);
1256
1257         /* create a new one starting at hash */
1258         memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1259         zap_cursor_init_serialized(it->ozi_zc, osd->od_objset.os,
1260                                    obj->oo_db->db_object, hash);
1261         it->ozi_reset = 0;
1262
1263         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1264         if (rc == 0)
1265                 RETURN(+1);
1266         else if (rc == -ENOENT)
1267                 RETURN(0);
1268
1269         RETURN(rc);
1270 }
1271
1272 static struct dt_index_operations osd_index_ops = {
1273         .dio_lookup             = osd_index_lookup,
1274         .dio_declare_insert     = osd_declare_index_insert,
1275         .dio_insert             = osd_index_insert,
1276         .dio_declare_delete     = osd_declare_index_delete,
1277         .dio_delete             = osd_index_delete,
1278         .dio_it = {
1279                 .init           = osd_index_it_init,
1280                 .fini           = osd_index_it_fini,
1281                 .get            = osd_index_it_get,
1282                 .put            = osd_index_it_put,
1283                 .next           = osd_index_it_next,
1284                 .key            = osd_index_it_key,
1285                 .key_size       = osd_index_it_key_size,
1286                 .rec            = osd_index_it_rec,
1287                 .store          = osd_index_it_store,
1288                 .load           = osd_index_it_load
1289         }
1290 };
1291
1292 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1293                 const struct dt_index_features *feat)
1294 {
1295         struct osd_object *obj = osd_dt_obj(dt);
1296         ENTRY;
1297
1298         LASSERT(dt_object_exists(dt));
1299
1300         /*
1301          * XXX: implement support for fixed-size keys sorted with natural
1302          *      numerical way (not using internal hash value)
1303          */
1304         if (feat->dif_flags & DT_IND_RANGE)
1305                 RETURN(-ERANGE);
1306
1307         if (unlikely(feat == &dt_otable_features))
1308                 /* do not support oi scrub yet. */
1309                 RETURN(-ENOTSUPP);
1310
1311         LASSERT(obj->oo_db != NULL);
1312         if (likely(feat == &dt_directory_features)) {
1313                 if (udmu_object_is_zap(obj->oo_db))
1314                         dt->do_index_ops = &osd_dir_ops;
1315                 else
1316                         RETURN(-ENOTDIR);
1317         } else if (unlikely(feat == &dt_acct_features)) {
1318                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1319                 dt->do_index_ops = &osd_acct_index_ops;
1320         } else if (udmu_object_is_zap(obj->oo_db) &&
1321                    dt->do_index_ops == NULL) {
1322                 /* For index file, we don't support variable key & record sizes
1323                  * and the key has to be unique */
1324                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1325                         RETURN(-EINVAL);
1326
1327                 /* Although the zap_*_uint64() primitives support large keys, we
1328                  * limit ourselves to 64-bit keys for now */
1329                 if (feat->dif_keysize_max != sizeof(__u64) ||
1330                     feat->dif_keysize_min != sizeof(__u64))
1331                         RETURN(-EINVAL);
1332
1333                 /* As for the record size, it should be a multiple of 8 bytes
1334                  * and smaller than the maximum value length supported by ZAP.
1335                  */
1336                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1337                         RETURN(-E2BIG);
1338                 if (feat->dif_recsize_max != feat->dif_recsize_min ||
1339                     (feat->dif_recsize_max & (sizeof(__u64) - 1)))
1340                         RETURN(-EINVAL);
1341
1342                 obj->oo_recsize = feat->dif_recsize_max / sizeof(__u64);
1343                 dt->do_index_ops = &osd_index_ops;
1344         }
1345
1346         RETURN(0);
1347 }
1348