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