Whamcloud - gitweb
ee95d01c163ecd1be909cfba4aa2e88c28947057
[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_seq_exists(const struct lu_env *env, struct osd_device *osd,
476                           obd_seq seq)
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 (ss == NULL)
484                 RETURN(1);
485
486         rc = osd_fld_lookup(env, osd, seq, range);
487         if (rc != 0) {
488                 CERROR("%s: Can not lookup fld for "LPX64"\n",
489                        osd_name(osd), seq);
490                 RETURN(0);
491         }
492
493         RETURN(ss->ss_node_id == range->lsr_index);
494 }
495
496 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
497                           struct lu_fid *fid)
498 {
499         ENTRY;
500
501         /* FID seqs not in FLDB, must be local seq */
502         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
503                 RETURN(0);
504
505         if (osd_seq_exists(env, osd, fid_seq(fid)))
506                 RETURN(0);
507
508         RETURN(1);
509 }
510
511 /**
512  *      Inserts (key, value) pair in \a directory object.
513  *
514  *      \param  dt      osd index object
515  *      \param  key     key for index
516  *      \param  rec     record reference
517  *      \param  th      transaction handler
518  *      \param  capa    capability descriptor
519  *      \param  ignore_quota update should not affect quota
520  *
521  *      \retval  0  success
522  *      \retval -ve failure
523  */
524 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
525                           const struct dt_rec *rec, const struct dt_key *key,
526                           struct thandle *th, struct lustre_capa *capa,
527                           int ignore_quota)
528 {
529         struct osd_thread_info *oti = osd_oti_get(env);
530         struct osd_object   *parent = osd_dt_obj(dt);
531         struct osd_device   *osd = osd_obj2dev(parent);
532         struct lu_fid       *fid = (struct lu_fid *)rec;
533         struct osd_thandle  *oh;
534         struct osd_object   *child = NULL;
535         __u32                attr;
536         char                *name = (char *)key;
537         int                  rc;
538         ENTRY;
539
540         LASSERT(parent->oo_db);
541         LASSERT(udmu_object_is_zap(parent->oo_db));
542
543         LASSERT(dt_object_exists(dt));
544         LASSERT(osd_invariant(parent));
545
546         LASSERT(th != NULL);
547         oh = container_of0(th, struct osd_thandle, ot_super);
548
549         rc = osd_remote_fid(env, osd, fid);
550         if (rc < 0) {
551                 CERROR("%s: Can not find object "DFID": rc = %d\n",
552                        osd->od_svname, PFID(fid), rc);
553                 RETURN(rc);
554         }
555
556         if (unlikely(rc == 1)) {
557                 /* Insert remote entry */
558                 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
559                 oti->oti_zde.lzd_reg.zde_type = IFTODT(S_IFDIR & S_IFMT);
560         } else {
561                 /*
562                  * To simulate old Orion setups with ./..  stored in the
563                  * directories
564                  */
565                 /* Insert local entry */
566                 child = osd_object_find(env, dt, fid);
567                 if (IS_ERR(child))
568                         RETURN(PTR_ERR(child));
569
570                 LASSERT(child->oo_db);
571                 if (name[0] == '.') {
572                         if (name[1] == 0) {
573                                 /* do not store ".", instead generate it
574                                  * during iteration */
575                                 GOTO(out, rc = 0);
576                         } else if (name[1] == '.' && name[2] == 0) {
577                                 /* update parent dnode in the child.
578                                  * later it will be used to generate ".." */
579                                 udmu_objset_t *uos = &osd->od_objset;
580                                 rc = osd_object_sa_update(parent,
581                                                  SA_ZPL_PARENT(uos),
582                                                  &child->oo_db->db_object,
583                                                  8, oh);
584                                 GOTO(out, rc);
585                         }
586                 }
587                 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
588                 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
589                 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
590                 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
591                 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
592         }
593
594         oti->oti_zde.lzd_fid = *fid;
595         /* Insert (key,oid) into ZAP */
596         rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
597                       (char *)key, 8, sizeof(oti->oti_zde) / 8,
598                       (void *)&oti->oti_zde, oh->ot_tx);
599
600 out:
601         if (child != NULL)
602                 osd_object_put(env, child);
603
604         RETURN(rc);
605 }
606
607 static int osd_declare_dir_delete(const struct lu_env *env,
608                                   struct dt_object *dt,
609                                   const struct dt_key *key,
610                                   struct thandle *th)
611 {
612         struct osd_object *obj = osd_dt_obj(dt);
613         struct osd_thandle *oh;
614         ENTRY;
615
616         LASSERT(dt_object_exists(dt));
617         LASSERT(osd_invariant(obj));
618
619         LASSERT(th != NULL);
620         oh = container_of0(th, struct osd_thandle, ot_super);
621
622         LASSERT(obj->oo_db);
623         LASSERT(udmu_object_is_zap(obj->oo_db));
624
625         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
626
627         RETURN(0);
628 }
629
630 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
631                           const struct dt_key *key, struct thandle *th,
632                           struct lustre_capa *capa)
633 {
634         struct osd_object *obj = osd_dt_obj(dt);
635         struct osd_device *osd = osd_obj2dev(obj);
636         struct osd_thandle *oh;
637         dmu_buf_t *zap_db = obj->oo_db;
638         char      *name = (char *)key;
639         int rc;
640         ENTRY;
641
642         LASSERT(obj->oo_db);
643         LASSERT(udmu_object_is_zap(obj->oo_db));
644
645         LASSERT(th != NULL);
646         oh = container_of0(th, struct osd_thandle, ot_super);
647
648         /*
649          * In Orion . and .. were stored in the directory (not generated upon
650          * request as now). we preserve them for backward compatibility
651          */
652         if (name[0] == '.') {
653                 if (name[1] == 0) {
654                         RETURN(0);
655                 } else if (name[1] == '.' && name[2] == 0) {
656                         RETURN(0);
657                 }
658         }
659
660         /* Remove key from the ZAP */
661         rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
662                          (char *) key, oh->ot_tx);
663
664 #if LUSTRE_VERSION_CODE <= OBD_OCD_VERSION(2, 4, 53, 0)
665         if (unlikely(rc == -ENOENT && name[0] == '.' &&
666             (name[1] == 0 || (name[1] == '.' && name[2] == 0))))
667                 rc = 0;
668 #endif
669         if (unlikely(rc && rc != -ENOENT))
670                 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
671
672         RETURN(rc);
673 }
674
675 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
676                                      struct dt_object *dt,
677                                      __u32 unused,
678                                      struct lustre_capa *capa)
679 {
680         struct osd_zap_it *it;
681
682         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
683         if (!IS_ERR(it))
684                 it->ozi_pos = 0;
685
686         RETURN((struct dt_it *)it);
687 }
688
689 /**
690  *  Move Iterator to record specified by \a key
691  *
692  *  \param  di      osd iterator
693  *  \param  key     key for index
694  *
695  *  \retval +ve  di points to record with least key not larger than key
696  *  \retval  0   di points to exact matched key
697  *  \retval -ve  failure
698  */
699 static int osd_dir_it_get(const struct lu_env *env,
700                           struct dt_it *di, const struct dt_key *key)
701 {
702         struct osd_zap_it *it = (struct osd_zap_it *)di;
703         struct osd_object *obj = it->ozi_obj;
704         struct osd_device *osd = osd_obj2dev(obj);
705         char              *name = (char *)key;
706         int                rc;
707         ENTRY;
708
709         LASSERT(it);
710         LASSERT(it->ozi_zc);
711
712         udmu_zap_cursor_fini(it->ozi_zc);
713
714         if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
715                                  obj->oo_db->db_object, 0))
716                 RETURN(-ENOMEM);
717
718         /* XXX: implementation of the API is broken at the moment */
719         LASSERT(((const char *)key)[0] == 0);
720
721         if (name[0] == 0) {
722                 it->ozi_pos = 0;
723                 RETURN(1);
724         }
725
726         if (name[0] == '.') {
727                 if (name[1] == 0) {
728                         it->ozi_pos = 1;
729                         GOTO(out, rc = 1);
730                 } else if (name[1] == '.' && name[2] == 0) {
731                         it->ozi_pos = 2;
732                         GOTO(out, rc = 1);
733                 }
734         }
735
736         /* neither . nor .. - some real record */
737         it->ozi_pos = 3;
738         rc = +1;
739
740 out:
741         RETURN(rc);
742 }
743
744 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
745 {
746         /* PBS: do nothing : ref are incremented at retrive and decreamented
747          *      next/finish. */
748 }
749
750 /*
751  * in Orion . and .. were stored in the directory, while ZPL
752  * and current osd-zfs generate them up on request. so, we
753  * need to ignore previously stored . and ..
754  */
755 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
756                                         zap_attribute_t *za)
757 {
758         int rc, isdot;
759
760         do {
761                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
762
763                 isdot = 0;
764                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
765                         if (za->za_name[1] == 0) {
766                                 isdot = 1;
767                         } else if (za->za_name[1] == '.' &&
768                                    za->za_name[2] == 0) {
769                                 isdot = 1;
770                         }
771                         if (unlikely(isdot))
772                                 zap_cursor_advance(it->ozi_zc);
773                 }
774         } while (unlikely(rc == 0 && isdot));
775
776         return rc;
777 }
778
779 /**
780  * to load a directory entry at a time and stored it in
781  * iterator's in-memory data structure.
782  *
783  * \param di, struct osd_it_ea, iterator's in memory structure
784  *
785  * \retval +ve, iterator reached to end
786  * \retval   0, iterator not reached to end
787  * \retval -ve, on error
788  */
789 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
790 {
791         struct osd_zap_it *it = (struct osd_zap_it *)di;
792         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
793         int                rc;
794
795         /* temp. storage should be enough for any key supported by ZFS */
796         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
797
798         /*
799          * the first ->next() moves the cursor to .
800          * the second ->next() moves the cursor to ..
801          * then we get to the real records and have to verify any exist
802          */
803         if (it->ozi_pos <= 2) {
804                 it->ozi_pos++;
805                 if (it->ozi_pos <=2)
806                         RETURN(0);
807         }
808
809         zap_cursor_advance(it->ozi_zc);
810
811         /*
812          * According to current API we need to return error if its last entry.
813          * zap_cursor_advance() does not return any value. So we need to call
814          * retrieve to check if there is any record.  We should make
815          * changes to Iterator API to not return status for this API
816          */
817         rc = osd_index_retrieve_skip_dots(it, za);
818
819         if (rc == -ENOENT) /* end of dir */
820                 RETURN(+1);
821
822         RETURN(rc);
823 }
824
825 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
826                                      const struct dt_it *di)
827 {
828         struct osd_zap_it *it = (struct osd_zap_it *)di;
829         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
830         int                rc = 0;
831         ENTRY;
832
833         if (it->ozi_pos <= 1) {
834                 it->ozi_pos = 1;
835                 RETURN((struct dt_key *)".");
836         } else if (it->ozi_pos == 2) {
837                 RETURN((struct dt_key *)"..");
838         }
839
840         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
841                 RETURN(ERR_PTR(rc));
842
843         strcpy(it->ozi_name, za->za_name);
844
845 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
846         if (za->za_name[0] == '.') {
847                 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
848                     za->za_name[2] == 0)) {
849                         /* we should not get onto . and ..
850                          * stored in the directory. ->next() and
851                          * other methods should prevent this
852                          */
853                         LBUG();
854                 }
855         }
856 #endif
857
858         RETURN((struct dt_key *)it->ozi_name);
859 }
860
861 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
862 {
863         struct osd_zap_it *it = (struct osd_zap_it *)di;
864         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
865         int                rc;
866         ENTRY;
867
868         if (it->ozi_pos <= 1) {
869                 it->ozi_pos = 1;
870                 RETURN(2);
871         } else if (it->ozi_pos == 2) {
872                 RETURN(3);
873         }
874
875         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
876                 rc = strlen(za->za_name);
877
878 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
879         if (rc == 0 && za->za_name[0] == '.') {
880                 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
881                     za->za_name[2] == 0)) {
882                         /* we should not get onto . and ..
883                          * stored in the directory. ->next() and
884                          * other methods should prevent this
885                          */
886                         LBUG();
887                 }
888         }
889 #endif
890         RETURN(rc);
891 }
892
893 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
894                           struct dt_rec *dtrec, __u32 attr)
895 {
896         struct osd_zap_it   *it = (struct osd_zap_it *)di;
897         struct lu_dirent    *lde = (struct lu_dirent *)dtrec;
898         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
899         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
900         int                  rc, namelen;
901         ENTRY;
902
903         if (it->ozi_pos <= 1) {
904                 lde->lde_hash = cpu_to_le64(1);
905                 strcpy(lde->lde_name, ".");
906                 lde->lde_namelen = cpu_to_le16(1);
907                 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
908                 lde->lde_attrs = LUDA_FID;
909                 /* append lustre attributes */
910                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
911                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
912                 it->ozi_pos = 1;
913                 GOTO(out, rc = 0);
914
915         } else if (it->ozi_pos == 2) {
916                 lde->lde_hash = cpu_to_le64(2);
917                 strcpy(lde->lde_name, "..");
918                 lde->lde_namelen = cpu_to_le16(2);
919                 lde->lde_attrs = LUDA_FID;
920                 /* append lustre attributes */
921                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
922                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
923                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
924                 /*
925                  * early Orion code was not setting LinkEA, so it's possible
926                  * some setups still have objects with no LinkEA set.
927                  * but at that time .. was a real record in the directory
928                  * so we should try to lookup .. in ZAP
929                  */
930                 if (rc != -ENOENT)
931                         GOTO(out, rc);
932         }
933
934         LASSERT(lde);
935
936         rc = -zap_cursor_retrieve(it->ozi_zc, za);
937         if (unlikely(rc != 0))
938                 GOTO(out, rc);
939
940         lde->lde_hash = cpu_to_le64(udmu_zap_cursor_serialize(it->ozi_zc));
941         namelen = strlen(za->za_name);
942         if (namelen > NAME_MAX)
943                 GOTO(out, rc = -EOVERFLOW);
944         strcpy(lde->lde_name, za->za_name);
945         lde->lde_namelen = cpu_to_le16(namelen);
946
947         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
948                 CERROR("%s: unsupported direntry format: %d %d\n",
949                        osd_obj2dev(it->ozi_obj)->od_svname,
950                        za->za_integer_length, (int)za->za_num_integers);
951
952                 GOTO(out, rc = -EIO);
953         }
954
955         rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
956                          za->za_name, za->za_integer_length, 3, zde);
957         if (rc)
958                 GOTO(out, rc);
959
960         lde->lde_fid = zde->lzd_fid;
961         lde->lde_attrs = LUDA_FID;
962
963         /* append lustre attributes */
964         osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
965
966         lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
967
968 out:
969         RETURN(rc);
970 }
971
972 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
973 {
974         struct osd_zap_it *it = (struct osd_zap_it *)di;
975         __u64              pos;
976         ENTRY;
977
978         if (it->ozi_pos <= 2)
979                 pos = it->ozi_pos;
980         else
981                 pos = udmu_zap_cursor_serialize(it->ozi_zc);
982
983         RETURN(pos);
984 }
985
986 /*
987  * return status :
988  *  rc == 0 -> end of directory.
989  *  rc >  0 -> ok, proceed.
990  *  rc <  0 -> error.  ( EOVERFLOW  can be masked.)
991  */
992 static int osd_dir_it_load(const struct lu_env *env,
993                         const struct dt_it *di, __u64 hash)
994 {
995         struct osd_zap_it *it = (struct osd_zap_it *)di;
996         struct osd_object *obj = it->ozi_obj;
997         struct osd_device *osd = osd_obj2dev(obj);
998         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
999         int                rc;
1000         ENTRY;
1001
1002         udmu_zap_cursor_fini(it->ozi_zc);
1003         if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
1004                                  obj->oo_db->db_object, hash))
1005                 RETURN(-ENOMEM);
1006
1007         if (hash <= 2) {
1008                 it->ozi_pos = hash;
1009                 rc = +1;
1010         } else {
1011                 it->ozi_pos = 3;
1012                 /* to return whether the end has been reached */
1013                 rc = osd_index_retrieve_skip_dots(it, za);
1014                 if (rc == 0)
1015                         rc = +1;
1016                 else if (rc == -ENOENT)
1017                         rc = 0;
1018         }
1019
1020         RETURN(rc);
1021 }
1022
1023 static struct dt_index_operations osd_dir_ops = {
1024         .dio_lookup         = osd_dir_lookup,
1025         .dio_declare_insert = osd_declare_dir_insert,
1026         .dio_insert         = osd_dir_insert,
1027         .dio_declare_delete = osd_declare_dir_delete,
1028         .dio_delete         = osd_dir_delete,
1029         .dio_it     = {
1030                 .init     = osd_dir_it_init,
1031                 .fini     = osd_index_it_fini,
1032                 .get      = osd_dir_it_get,
1033                 .put      = osd_dir_it_put,
1034                 .next     = osd_dir_it_next,
1035                 .key      = osd_dir_it_key,
1036                 .key_size = osd_dir_it_key_size,
1037                 .rec      = osd_dir_it_rec,
1038                 .store    = osd_dir_it_store,
1039                 .load     = osd_dir_it_load
1040         }
1041 };
1042
1043 /*
1044  * Primitives for index files using binary keys.
1045  */
1046
1047 /* key integer_size is 8 */
1048 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1049                                   const struct dt_key *src)
1050 {
1051         int size;
1052
1053         LASSERT(dst);
1054         LASSERT(src);
1055
1056         /* align keysize to 64bit */
1057         size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1058         size *= sizeof(__u64);
1059
1060         LASSERT(size <= MAXNAMELEN);
1061
1062         if (unlikely(size > o->oo_keysize))
1063                 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1064         memcpy(dst, (const char *)src, o->oo_keysize);
1065
1066         return (size/sizeof(__u64));
1067 }
1068
1069 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1070                         struct dt_rec *rec, const struct dt_key *key,
1071                         struct lustre_capa *capa)
1072 {
1073         struct osd_object *obj = osd_dt_obj(dt);
1074         struct osd_device *osd = osd_obj2dev(obj);
1075         __u64             *k = osd_oti_get(env)->oti_key64;
1076         int                rc;
1077         ENTRY;
1078
1079         rc = osd_prepare_key_uint64(obj, k, key);
1080
1081         rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1082                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1083                                 (void *)rec);
1084         RETURN(rc == 0 ? 1 : rc);
1085 }
1086
1087 static int osd_declare_index_insert(const struct lu_env *env,
1088                                     struct dt_object *dt,
1089                                     const struct dt_rec *rec,
1090                                     const struct dt_key *key,
1091                                     struct thandle *th)
1092 {
1093         struct osd_object  *obj = osd_dt_obj(dt);
1094         struct osd_thandle *oh;
1095         ENTRY;
1096
1097         LASSERT(th != NULL);
1098         oh = container_of0(th, struct osd_thandle, ot_super);
1099
1100         LASSERT(obj->oo_db);
1101
1102         dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1103
1104         /* It is not clear what API should be used for binary keys, so we pass
1105          * a null name which has the side effect of over-reserving space,
1106          * accounting for the worst case. See zap_count_write() */
1107         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1108
1109         RETURN(0);
1110 }
1111
1112 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1113                             const struct dt_rec *rec, const struct dt_key *key,
1114                             struct thandle *th, struct lustre_capa *capa,
1115                             int ignore_quota)
1116 {
1117         struct osd_object  *obj = osd_dt_obj(dt);
1118         struct osd_device  *osd = osd_obj2dev(obj);
1119         struct osd_thandle *oh;
1120         __u64              *k = osd_oti_get(env)->oti_key64;
1121         int                 rc;
1122         ENTRY;
1123
1124         LASSERT(obj->oo_db);
1125         LASSERT(dt_object_exists(dt));
1126         LASSERT(osd_invariant(obj));
1127         LASSERT(th != NULL);
1128
1129         oh = container_of0(th, struct osd_thandle, ot_super);
1130
1131         rc = osd_prepare_key_uint64(obj, k, key);
1132
1133         /* Insert (key,oid) into ZAP */
1134         rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
1135                              k, rc, obj->oo_recusize, obj->oo_recsize,
1136                              (void *)rec, oh->ot_tx);
1137         RETURN(rc);
1138 }
1139
1140 static int osd_declare_index_delete(const struct lu_env *env,
1141                                     struct dt_object *dt,
1142                                     const struct dt_key *key,
1143                                     struct thandle *th)
1144 {
1145         struct osd_object  *obj = osd_dt_obj(dt);
1146         struct osd_thandle *oh;
1147         ENTRY;
1148
1149         LASSERT(dt_object_exists(dt));
1150         LASSERT(osd_invariant(obj));
1151         LASSERT(th != NULL);
1152         LASSERT(obj->oo_db);
1153
1154         oh = container_of0(th, struct osd_thandle, ot_super);
1155         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1156
1157         RETURN(0);
1158 }
1159
1160 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1161                             const struct dt_key *key, struct thandle *th,
1162                             struct lustre_capa *capa)
1163 {
1164         struct osd_object  *obj = osd_dt_obj(dt);
1165         struct osd_device  *osd = osd_obj2dev(obj);
1166         struct osd_thandle *oh;
1167         __u64              *k = osd_oti_get(env)->oti_key64;
1168         int                 rc;
1169         ENTRY;
1170
1171         LASSERT(obj->oo_db);
1172         LASSERT(th != NULL);
1173         oh = container_of0(th, struct osd_thandle, ot_super);
1174
1175         rc = osd_prepare_key_uint64(obj, k, key);
1176
1177         /* Remove binary key from the ZAP */
1178         rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
1179                                 k, rc, oh->ot_tx);
1180         RETURN(rc);
1181 }
1182
1183 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1184                             const struct dt_key *key)
1185 {
1186         struct osd_zap_it *it = (struct osd_zap_it *)di;
1187         struct osd_object *obj = it->ozi_obj;
1188         struct osd_device *osd = osd_obj2dev(obj);
1189         ENTRY;
1190
1191         LASSERT(it);
1192         LASSERT(it->ozi_zc);
1193
1194         /*
1195          * XXX: we need a binary version of zap_cursor_move_to_key()
1196          *      to implement this API */
1197         if (*((const __u64 *)key) != 0)
1198                 CERROR("NOT IMPLEMETED YET (move to "LPX64")\n",
1199                        *((__u64 *)key));
1200
1201         zap_cursor_fini(it->ozi_zc);
1202         memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1203         zap_cursor_init(it->ozi_zc, osd->od_objset.os, obj->oo_db->db_object);
1204         it->ozi_reset = 1;
1205
1206         RETURN(+1);
1207 }
1208
1209 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1210 {
1211         struct osd_zap_it *it = (struct osd_zap_it *)di;
1212         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1213         int                rc;
1214         ENTRY;
1215
1216         if (it->ozi_reset == 0)
1217                 zap_cursor_advance(it->ozi_zc);
1218         it->ozi_reset = 0;
1219
1220         /*
1221          * According to current API we need to return error if it's last entry.
1222          * zap_cursor_advance() does not return any value. So we need to call
1223          * retrieve to check if there is any record.  We should make
1224          * changes to Iterator API to not return status for this API
1225          */
1226         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1227         if (rc == -ENOENT)
1228                 RETURN(+1);
1229
1230         RETURN((rc));
1231 }
1232
1233 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1234                                        const struct dt_it *di)
1235 {
1236         struct osd_zap_it *it = (struct osd_zap_it *)di;
1237         struct osd_object *obj = it->ozi_obj;
1238         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1239         int                rc = 0;
1240         ENTRY;
1241
1242         it->ozi_reset = 0;
1243         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1244         if (rc)
1245                 RETURN(ERR_PTR(rc));
1246
1247         /* the binary key is stored in the name */
1248         memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1249
1250         RETURN((struct dt_key *)&it->ozi_key);
1251 }
1252
1253 static int osd_index_it_key_size(const struct lu_env *env,
1254                                 const struct dt_it *di)
1255 {
1256         struct osd_zap_it *it = (struct osd_zap_it *)di;
1257         struct osd_object *obj = it->ozi_obj;
1258         RETURN(obj->oo_keysize);
1259 }
1260
1261 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1262                             struct dt_rec *rec, __u32 attr)
1263 {
1264         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1265         struct osd_zap_it *it = (struct osd_zap_it *)di;
1266         struct osd_object *obj = it->ozi_obj;
1267         struct osd_device *osd = osd_obj2dev(obj);
1268         __u64             *k = osd_oti_get(env)->oti_key64;
1269         int                rc;
1270         ENTRY;
1271
1272         it->ozi_reset = 0;
1273         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1274         if (rc)
1275                 RETURN(rc);
1276
1277         rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1278
1279         rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1280                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1281                                 (void *)rec);
1282         RETURN(rc);
1283 }
1284
1285 static __u64 osd_index_it_store(const struct lu_env *env,
1286                                 const struct dt_it *di)
1287 {
1288         struct osd_zap_it *it = (struct osd_zap_it *)di;
1289
1290         it->ozi_reset = 0;
1291         RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1292 }
1293
1294 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1295                              __u64 hash)
1296 {
1297         struct osd_zap_it *it = (struct osd_zap_it *)di;
1298         struct osd_object *obj = it->ozi_obj;
1299         struct osd_device *osd = osd_obj2dev(obj);
1300         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1301         int                rc;
1302         ENTRY;
1303
1304         /* close the current cursor */
1305         zap_cursor_fini(it->ozi_zc);
1306
1307         /* create a new one starting at hash */
1308         memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1309         zap_cursor_init_serialized(it->ozi_zc, osd->od_objset.os,
1310                                    obj->oo_db->db_object, hash);
1311         it->ozi_reset = 0;
1312
1313         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1314         if (rc == 0)
1315                 RETURN(+1);
1316         else if (rc == -ENOENT)
1317                 RETURN(0);
1318
1319         RETURN(rc);
1320 }
1321
1322 static struct dt_index_operations osd_index_ops = {
1323         .dio_lookup             = osd_index_lookup,
1324         .dio_declare_insert     = osd_declare_index_insert,
1325         .dio_insert             = osd_index_insert,
1326         .dio_declare_delete     = osd_declare_index_delete,
1327         .dio_delete             = osd_index_delete,
1328         .dio_it = {
1329                 .init           = osd_index_it_init,
1330                 .fini           = osd_index_it_fini,
1331                 .get            = osd_index_it_get,
1332                 .put            = osd_index_it_put,
1333                 .next           = osd_index_it_next,
1334                 .key            = osd_index_it_key,
1335                 .key_size       = osd_index_it_key_size,
1336                 .rec            = osd_index_it_rec,
1337                 .store          = osd_index_it_store,
1338                 .load           = osd_index_it_load
1339         }
1340 };
1341
1342 struct osd_metadnode_it {
1343         struct osd_device       *mit_dev;
1344         __u64                    mit_pos;
1345         struct lu_fid            mit_fid;
1346         int                      mit_prefetched;
1347         __u64                    mit_prefetched_dnode;
1348 };
1349
1350 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1351                                             struct dt_object *dt, __u32 attr,
1352                                             struct lustre_capa *capa)
1353 {
1354         struct osd_device       *dev   = osd_dev(dt->do_lu.lo_dev);
1355         struct osd_metadnode_it *it;
1356         ENTRY;
1357
1358         OBD_ALLOC_PTR(it);
1359         if (unlikely(it == NULL))
1360                 RETURN(ERR_PTR(-ENOMEM));
1361
1362         it->mit_dev = dev;
1363
1364         /* XXX: dmu_object_next() does NOT find dnodes allocated
1365          *      in the current non-committed txg, so we force txg
1366          *      commit to find all existing dnodes ... */
1367         txg_wait_synced(dmu_objset_pool(dev->od_objset.os), 0ULL);
1368
1369         RETURN((struct dt_it *)it);
1370 }
1371
1372 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1373 {
1374         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1375
1376         OBD_FREE_PTR(it);
1377 }
1378
1379 static int osd_zfs_otable_it_get(const struct lu_env *env,
1380                                  struct dt_it *di, const struct dt_key *key)
1381 {
1382         return 0;
1383 }
1384
1385 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1386 {
1387 }
1388
1389 #define OTABLE_PREFETCH         256
1390
1391 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1392                                     struct osd_metadnode_it *it)
1393 {
1394         struct osd_device       *dev = it->mit_dev;
1395         udmu_objset_t           *uos = &dev->od_objset;
1396         int                      rc;
1397
1398         /* can go negative on the very first access to the iterator
1399          * or if some non-Lustre objects were found */
1400         if (unlikely(it->mit_prefetched < 0))
1401                 it->mit_prefetched = 0;
1402
1403         if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1404                 return;
1405
1406         if (it->mit_prefetched_dnode == 0)
1407                 it->mit_prefetched_dnode = it->mit_pos;
1408
1409         while (it->mit_prefetched < OTABLE_PREFETCH) {
1410                 rc = -dmu_object_next(uos->os, &it->mit_prefetched_dnode,
1411                                       B_FALSE, 0);
1412                 if (unlikely(rc != 0))
1413                         break;
1414
1415                 /* dmu_prefetch() was exported in 0.6.2, if you use with
1416                  * an older release, just comment it out - this is an
1417                  * optimization */
1418                 dmu_prefetch(uos->os, it->mit_prefetched_dnode, 0, 0);
1419
1420                 it->mit_prefetched++;
1421         }
1422 }
1423
1424 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1425 {
1426         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1427         struct lustre_mdt_attrs *lma;
1428         struct osd_device       *dev = it->mit_dev;
1429         udmu_objset_t           *uos = &dev->od_objset;
1430         nvlist_t                *nvbuf = NULL;
1431         uchar_t                 *v;
1432         __u64                    dnode;
1433         int                      rc, s;
1434
1435         memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1436
1437         dnode = it->mit_pos;
1438         do {
1439                 rc = -dmu_object_next(uos->os, &it->mit_pos, B_FALSE, 0);
1440                 if (unlikely(rc != 0))
1441                         GOTO(out, rc = 1);
1442                 it->mit_prefetched--;
1443
1444                 /* LMA is required for this to be a Lustre object.
1445                  * If there is no xattr skip it. */
1446                 rc = __osd_xattr_load(uos, it->mit_pos, &nvbuf);
1447                 if (unlikely(rc != 0))
1448                         continue;
1449
1450                 LASSERT(nvbuf != NULL);
1451                 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1452                 if (likely(rc == 0)) {
1453                         /* Lustre object */
1454                         lma = (struct lustre_mdt_attrs *)v;
1455                         lustre_lma_swab(lma);
1456                         it->mit_fid = lma->lma_self_fid;
1457                         nvlist_free(nvbuf);
1458                         break;
1459                 } else {
1460                         /* not a Lustre object, try next one */
1461                         nvlist_free(nvbuf);
1462                 }
1463
1464         } while (1);
1465
1466
1467         /* we aren't prefetching in the above loop because the number of
1468          * non-Lustre objects is very small and we will be repeating very
1469          * rare. in case we want to use this to iterate over non-Lustre
1470          * objects (i.e. when we convert regular ZFS in Lustre) it makes
1471          * sense to initiate prefetching in the loop */
1472
1473         /* 0 - there are more items, +1 - the end */
1474         if (likely(rc == 0))
1475                 osd_zfs_otable_prefetch(env, it);
1476
1477         CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1478                it->mit_pos, PFID(&it->mit_fid), rc);
1479
1480 out:
1481         return rc;
1482 }
1483
1484 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1485                                             const struct dt_it *di)
1486 {
1487         return NULL;
1488 }
1489
1490 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1491                                       const struct dt_it *di)
1492 {
1493         return sizeof(__u64);
1494 }
1495
1496 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1497                                  const struct dt_it *di,
1498                                  struct dt_rec *rec, __u32 attr)
1499 {
1500         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1501         struct lu_fid *fid = (struct lu_fid *)rec;
1502         ENTRY;
1503
1504         *fid = it->mit_fid;
1505
1506         RETURN(0);
1507 }
1508
1509
1510 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1511                                      const struct dt_it *di)
1512 {
1513         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1514
1515         return it->mit_pos;
1516 }
1517
1518 static int osd_zfs_otable_it_load(const struct lu_env *env,
1519                                   const struct dt_it *di, __u64 hash)
1520 {
1521         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1522
1523         it->mit_pos = hash;
1524         it->mit_prefetched = 0;
1525         it->mit_prefetched_dnode = 0;
1526
1527         return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1528 }
1529
1530 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1531                                      const struct dt_it *di, void *key_rec)
1532 {
1533         return 0;
1534 }
1535
1536 const struct dt_index_operations osd_zfs_otable_ops = {
1537         .dio_it = {
1538                 .init     = osd_zfs_otable_it_init,
1539                 .fini     = osd_zfs_otable_it_fini,
1540                 .get      = osd_zfs_otable_it_get,
1541                 .put      = osd_zfs_otable_it_put,
1542                 .next     = osd_zfs_otable_it_next,
1543                 .key      = osd_zfs_otable_it_key,
1544                 .key_size = osd_zfs_otable_it_key_size,
1545                 .rec      = osd_zfs_otable_it_rec,
1546                 .store    = osd_zfs_otable_it_store,
1547                 .load     = osd_zfs_otable_it_load,
1548                 .key_rec  = osd_zfs_otable_it_key_rec,
1549         }
1550 };
1551
1552 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1553                 const struct dt_index_features *feat)
1554 {
1555         struct osd_object *obj = osd_dt_obj(dt);
1556         ENTRY;
1557
1558         LASSERT(dt_object_exists(dt));
1559
1560         /*
1561          * XXX: implement support for fixed-size keys sorted with natural
1562          *      numerical way (not using internal hash value)
1563          */
1564         if (feat->dif_flags & DT_IND_RANGE)
1565                 RETURN(-ERANGE);
1566
1567         if (unlikely(feat == &dt_otable_features)) {
1568                 dt->do_index_ops = &osd_zfs_otable_ops;
1569                 RETURN(0);
1570         }
1571
1572         LASSERT(obj->oo_db != NULL);
1573         if (likely(feat == &dt_directory_features)) {
1574                 if (udmu_object_is_zap(obj->oo_db))
1575                         dt->do_index_ops = &osd_dir_ops;
1576                 else
1577                         RETURN(-ENOTDIR);
1578         } else if (unlikely(feat == &dt_acct_features)) {
1579                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1580                 dt->do_index_ops = &osd_acct_index_ops;
1581         } else if (udmu_object_is_zap(obj->oo_db) &&
1582                    dt->do_index_ops == NULL) {
1583                 /* For index file, we don't support variable key & record sizes
1584                  * and the key has to be unique */
1585                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1586                         RETURN(-EINVAL);
1587
1588                 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1589                         RETURN(-E2BIG);
1590                 if (feat->dif_keysize_max != feat->dif_keysize_min)
1591                         RETURN(-EINVAL);
1592
1593                 /* As for the record size, it should be a multiple of 8 bytes
1594                  * and smaller than the maximum value length supported by ZAP.
1595                  */
1596                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1597                         RETURN(-E2BIG);
1598                 if (feat->dif_recsize_max != feat->dif_recsize_min)
1599                         RETURN(-EINVAL);
1600
1601                 obj->oo_keysize = feat->dif_keysize_max;
1602                 obj->oo_recsize = feat->dif_recsize_max;
1603                 obj->oo_recusize = 1;
1604
1605                 /* ZFS prefers to work with array of 64bits */
1606                 if ((obj->oo_recsize & 7) == 0) {
1607                         obj->oo_recsize >>= 3;
1608                         obj->oo_recusize = 8;
1609                 }
1610                 dt->do_index_ops = &osd_index_ops;
1611         }
1612
1613         RETURN(0);
1614 }