Whamcloud - gitweb
8765df1395f17e2d2c9bc5a450d0c3914312d292
[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 inline int osd_object_is_zap(dmu_buf_t *db)
71 {
72         dmu_buf_impl_t *dbi = (dmu_buf_impl_t *) db;
73         dnode_t *dn;
74         int rc;
75
76         DB_DNODE_ENTER(dbi);
77         dn = DB_DNODE(dbi);
78         rc = (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
79                         dn->dn_type == DMU_OT_USERGROUP_USED);
80         DB_DNODE_EXIT(dbi);
81
82         return rc;
83 }
84
85 /* We don't actually have direct access to the zap_hashbits() function
86  * so just pretend like we do for now.  If this ever breaks we can look at
87  * it at that time. */
88 #define zap_hashbits(zc) 48
89 /*
90  * ZFS hash format:
91  * | cd (16 bits) | hash (48 bits) |
92  * we need it in other form:
93  * |0| hash (48 bit) | cd (15 bit) |
94  * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
95  * the readdir hashes from multiple directory stripes uniformly on the client.
96  * Another point is sign bit, the hash range should be in [0, 2^63-1] because
97  * loff_t (for llseek) needs to be a positive value.  This means the "cd" field
98  * should only be the low 15 bits.
99  */
100 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
101 {
102         uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
103
104         return (zfs_hash >> zap_hashbits(zc)) |
105                 (zfs_hash << (63 - zap_hashbits(zc)));
106 }
107
108 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
109                                     uint64_t id, uint64_t dirhash)
110 {
111         uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
112                 (dirhash >> (63 - zap_hashbits(zc)));
113
114         zap_cursor_init_serialized(zc, os, id, zfs_hash);
115 }
116
117 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
118                         uint64_t id, uint64_t dirhash)
119 {
120         zap_cursor_t *t;
121
122         OBD_ALLOC_PTR(t);
123         if (unlikely(t == NULL))
124                 return -ENOMEM;
125
126         osd_zap_cursor_init_serialized(t, os, id, dirhash);
127         *zc = t;
128
129         return 0;
130 }
131
132 void osd_zap_cursor_fini(zap_cursor_t *zc)
133 {
134         zap_cursor_fini(zc);
135         OBD_FREE_PTR(zc);
136 }
137
138 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
139                                                  struct osd_object *o,
140                                                  uint64_t dirhash)
141 {
142         struct osd_device *d = osd_obj2dev(o);
143         zap_cursor_init_serialized(zc, d->od_os, o->oo_db->db_object, dirhash);
144 }
145
146 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
147                         uint64_t dirhash)
148 {
149         struct osd_device *d = osd_obj2dev(o);
150         return osd_zap_cursor_init(zc, d->od_os, o->oo_db->db_object, dirhash);
151 }
152
153 static struct dt_it *osd_index_it_init(const struct lu_env *env,
154                                        struct dt_object *dt,
155                                        __u32 unused,
156                                        struct lustre_capa *capa)
157 {
158         struct osd_thread_info  *info = osd_oti_get(env);
159         struct osd_zap_it       *it;
160         struct osd_object       *obj = osd_dt_obj(dt);
161         struct lu_object        *lo  = &dt->do_lu;
162         int                      rc;
163         ENTRY;
164
165         /* XXX: check capa ? */
166
167         LASSERT(lu_object_exists(lo));
168         LASSERT(obj->oo_db);
169         LASSERT(osd_object_is_zap(obj->oo_db));
170         LASSERT(info);
171
172         if (info->oti_it_inline) {
173                 OBD_ALLOC_PTR(it);
174                 if (it == NULL)
175                         RETURN(ERR_PTR(-ENOMEM));
176         } else {
177                 it = &info->oti_it_zap;
178                 info->oti_it_inline = 1;
179         }
180
181         rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
182         if (rc != 0) {
183                 if (it != &info->oti_it_zap)
184                         OBD_FREE_PTR(it);
185                 else
186                         info->oti_it_inline = 0;
187
188                 RETURN(ERR_PTR(rc));
189         }
190
191         it->ozi_obj   = obj;
192         it->ozi_capa  = capa;
193         it->ozi_reset = 1;
194         lu_object_get(lo);
195
196         RETURN((struct dt_it *)it);
197 }
198
199 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
200 {
201         struct osd_thread_info  *info   = osd_oti_get(env);
202         struct osd_zap_it       *it     = (struct osd_zap_it *)di;
203         struct osd_object       *obj;
204         ENTRY;
205
206         LASSERT(it);
207         LASSERT(it->ozi_obj);
208
209         obj = it->ozi_obj;
210
211         osd_zap_cursor_fini(it->ozi_zc);
212         lu_object_put(env, &obj->oo_dt.do_lu);
213         if (it != &info->oti_it_zap)
214                 OBD_FREE_PTR(it);
215         else
216                 info->oti_it_inline = 0;
217
218         EXIT;
219 }
220
221
222 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
223 {
224         /* PBS: do nothing : ref are incremented at retrive and decreamented
225          *      next/finish. */
226 }
227
228 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
229                                        int len, __u16 type)
230 {
231         const unsigned    align = sizeof(struct luda_type) - 1;
232         struct luda_type *lt;
233
234         /* check if file type is required */
235         if (attr & LUDA_TYPE) {
236                 len = (len + align) & ~align;
237
238                 lt = (void *)ent->lde_name + len;
239                 lt->lt_type = cpu_to_le16(DTTOIF(type));
240                 ent->lde_attrs |= LUDA_TYPE;
241         }
242
243         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
244 }
245
246 /*
247  * as we don't know FID, we can't use LU object, so this function
248  * partially duplicate __osd_xattr_get() which is built around
249  * LU-object and uses it to cache data like regular EA dnode, etc
250  */
251 static int osd_find_parent_by_dnode(const struct lu_env *env,
252                                     struct dt_object *o,
253                                     struct lu_fid *fid)
254 {
255         struct osd_device       *osd = osd_obj2dev(osd_dt_obj(o));
256         struct lustre_mdt_attrs *lma;
257         struct lu_buf            buf;
258         sa_handle_t             *sa_hdl;
259         nvlist_t                *nvbuf = NULL;
260         uchar_t                 *value;
261         uint64_t                 dnode;
262         int                      rc, size;
263         ENTRY;
264
265         /* first of all, get parent dnode from own attributes */
266         LASSERT(osd_dt_obj(o)->oo_db);
267         rc = -sa_handle_get(osd->od_os, osd_dt_obj(o)->oo_db->db_object,
268                             NULL, SA_HDL_PRIVATE, &sa_hdl);
269         if (rc)
270                 RETURN(rc);
271
272         dnode = ZFS_NO_OBJECT;
273         rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
274         sa_handle_destroy(sa_hdl);
275         if (rc)
276                 RETURN(rc);
277
278         /* now get EA buffer */
279         rc = __osd_xattr_load(osd, dnode, &nvbuf);
280         if (rc)
281                 GOTO(regular, rc);
282
283         /* XXX: if we get that far.. should we cache the result? */
284
285         /* try to find LMA attribute */
286         LASSERT(nvbuf != NULL);
287         rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &value, &size);
288         if (rc == 0 && size >= sizeof(*lma)) {
289                 lma = (struct lustre_mdt_attrs *)value;
290                 lustre_lma_swab(lma);
291                 *fid = lma->lma_self_fid;
292                 GOTO(out, rc = 0);
293         }
294
295 regular:
296         /* no LMA attribute in SA, let's try regular EA */
297
298         /* first of all, get parent dnode storing regular EA */
299         rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
300         if (rc)
301                 GOTO(out, rc);
302
303         dnode = ZFS_NO_OBJECT;
304         rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &dnode, 8);
305         sa_handle_destroy(sa_hdl);
306         if (rc)
307                 GOTO(out, rc);
308
309         CLASSERT(sizeof(*lma) <= sizeof(osd_oti_get(env)->oti_buf));
310         buf.lb_buf = osd_oti_get(env)->oti_buf;
311         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
312
313         /* now try to find LMA */
314         rc = __osd_xattr_get_large(env, osd, dnode, &buf,
315                                    XATTR_NAME_LMA, &size);
316         if (rc == 0 && size >= sizeof(*lma)) {
317                 lma = buf.lb_buf;
318                 lustre_lma_swab(lma);
319                 *fid = lma->lma_self_fid;
320                 GOTO(out, rc = 0);
321         } else if (rc < 0) {
322                 GOTO(out, rc);
323         } else {
324                 GOTO(out, rc = -EIO);
325         }
326
327 out:
328         if (nvbuf != NULL)
329                 nvlist_free(nvbuf);
330         RETURN(rc);
331 }
332
333 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
334                                struct lu_fid *fid)
335 {
336         struct link_ea_header  *leh;
337         struct link_ea_entry   *lee;
338         struct lu_buf           buf;
339         int                     rc;
340         ENTRY;
341
342         buf.lb_buf = osd_oti_get(env)->oti_buf;
343         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
344
345         rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
346         if (rc == -ERANGE) {
347                 rc = osd_xattr_get(env, o, &LU_BUF_NULL,
348                                    XATTR_NAME_LINK, BYPASS_CAPA);
349                 if (rc < 0)
350                         RETURN(rc);
351                 LASSERT(rc > 0);
352                 OBD_ALLOC(buf.lb_buf, rc);
353                 if (buf.lb_buf == NULL)
354                         RETURN(-ENOMEM);
355                 buf.lb_len = rc;
356                 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
357         }
358         if (rc < 0)
359                 GOTO(out, rc);
360         if (rc < sizeof(*leh) + sizeof(*lee))
361                 GOTO(out, rc = -EINVAL);
362
363         leh = buf.lb_buf;
364         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
365                 leh->leh_magic = LINK_EA_MAGIC;
366                 leh->leh_reccount = __swab32(leh->leh_reccount);
367                 leh->leh_len = __swab64(leh->leh_len);
368         }
369         if (leh->leh_magic != LINK_EA_MAGIC)
370                 GOTO(out, rc = -EINVAL);
371         if (leh->leh_reccount == 0)
372                 GOTO(out, rc = -ENODATA);
373
374         lee = (struct link_ea_entry *)(leh + 1);
375         fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
376         rc = 0;
377
378 out:
379         if (buf.lb_buf != osd_oti_get(env)->oti_buf)
380                 OBD_FREE(buf.lb_buf, buf.lb_len);
381
382 #if 0
383         /* this block can be enabled for additional verification
384          * it's trying to match FID from LinkEA vs. FID from LMA */
385         if (rc == 0) {
386                 struct lu_fid fid2;
387                 int rc2;
388                 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
389                 if (rc2 == 0)
390                         if (lu_fid_eq(fid, &fid2) == 0)
391                                 CERROR("wrong parent: "DFID" != "DFID"\n",
392                                        PFID(fid), PFID(&fid2));
393         }
394 #endif
395
396         /* no LinkEA is found, let's try to find the fid in parent's LMA */
397         if (unlikely(rc != 0))
398                 rc = osd_find_parent_by_dnode(env, o, fid);
399
400         RETURN(rc);
401 }
402
403 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
404                           struct dt_rec *rec, const struct dt_key *key,
405                           struct lustre_capa *capa)
406 {
407         struct osd_thread_info *oti = osd_oti_get(env);
408         struct osd_object  *obj = osd_dt_obj(dt);
409         struct osd_device  *osd = osd_obj2dev(obj);
410         char               *name = (char *)key;
411         int                 rc;
412         ENTRY;
413
414         LASSERT(osd_object_is_zap(obj->oo_db));
415
416         if (name[0] == '.') {
417                 if (name[1] == 0) {
418                         const struct lu_fid *f = lu_object_fid(&dt->do_lu);
419                         memcpy(rec, f, sizeof(*f));
420                         RETURN(1);
421                 } else if (name[1] == '.' && name[2] == 0) {
422                         rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
423                         RETURN(rc == 0 ? 1 : rc);
424                 }
425         }
426
427         rc = -zap_lookup(osd->od_os, obj->oo_db->db_object,
428                          (char *)key, 8, sizeof(oti->oti_zde) / 8,
429                          (void *)&oti->oti_zde);
430         memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
431
432         RETURN(rc == 0 ? 1 : rc);
433 }
434
435 static int osd_declare_dir_insert(const struct lu_env *env,
436                                   struct dt_object *dt,
437                                   const struct dt_rec *rec,
438                                   const struct dt_key *key,
439                                   struct thandle *th)
440 {
441         struct osd_object  *obj = osd_dt_obj(dt);
442         struct osd_thandle *oh;
443         ENTRY;
444
445         LASSERT(th != NULL);
446         oh = container_of0(th, struct osd_thandle, ot_super);
447
448         LASSERT(obj->oo_db);
449         LASSERT(osd_object_is_zap(obj->oo_db));
450
451         dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
452         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
453
454         RETURN(0);
455 }
456
457 /**
458  * Find the osd object for given fid.
459  *
460  * \param fid need to find the osd object having this fid
461  *
462  * \retval osd_object on success
463  * \retval        -ve on error
464  */
465 struct osd_object *osd_object_find(const struct lu_env *env,
466                                    struct dt_object *dt,
467                                    const struct lu_fid *fid)
468 {
469         struct lu_device         *ludev = dt->do_lu.lo_dev;
470         struct osd_object        *child = NULL;
471         struct lu_object         *luch;
472         struct lu_object         *lo;
473
474         /*
475          * at this point topdev might not exist yet
476          * (i.e. MGS is preparing profiles). so we can
477          * not rely on topdev and instead lookup with
478          * our device passed as topdev. this can't work
479          * if the object isn't cached yet (as osd doesn't
480          * allocate lu_header). IOW, the object must be
481          * in the cache, otherwise lu_object_alloc() crashes
482          * -bzzz
483          */
484         luch = lu_object_find_at(env, ludev, fid, NULL);
485         if (IS_ERR(luch))
486                 return (void *)luch;
487
488         if (lu_object_exists(luch)) {
489                 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
490                 if (lo != NULL)
491                         child = osd_obj(lo);
492                 else
493                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
494                                         "%s: object can't be located "DFID"\n",
495                                         osd_dev(ludev)->od_svname, PFID(fid));
496
497                 if (child == NULL) {
498                         lu_object_put(env, luch);
499                         CERROR("%s: Unable to get osd_object "DFID"\n",
500                                osd_dev(ludev)->od_svname, PFID(fid));
501                         child = ERR_PTR(-ENOENT);
502                 }
503         } else {
504                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
505                                 "%s: lu_object does not exists "DFID"\n",
506                                 osd_dev(ludev)->od_svname, PFID(fid));
507                 lu_object_put(env, luch);
508                 child = ERR_PTR(-ENOENT);
509         }
510
511         return child;
512 }
513
514 /**
515  * Put the osd object once done with it.
516  *
517  * \param obj osd object that needs to be put
518  */
519 static inline void osd_object_put(const struct lu_env *env,
520                                   struct osd_object *obj)
521 {
522         lu_object_put(env, &obj->oo_dt.do_lu);
523 }
524
525 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
526                           obd_seq seq)
527 {
528         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
529         struct seq_server_site  *ss = osd_seq_site(osd);
530         int                     rc;
531         ENTRY;
532
533         LASSERT(ss != NULL);
534         LASSERT(ss->ss_server_fld != NULL);
535
536         rc = osd_fld_lookup(env, osd, seq, range);
537         if (rc != 0) {
538                 CERROR("%s: Can not lookup fld for "LPX64"\n",
539                        osd_name(osd), seq);
540                 RETURN(0);
541         }
542
543         RETURN(ss->ss_node_id == range->lsr_index);
544 }
545
546 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
547                           const struct lu_fid *fid)
548 {
549         struct seq_server_site  *ss = osd_seq_site(osd);
550         ENTRY;
551
552         /* FID seqs not in FLDB, must be local seq */
553         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
554                 RETURN(0);
555
556         /* If FLD is not being initialized yet, it only happens during the
557          * initialization, likely during mgs initialization, and we assume
558          * this is local FID. */
559         if (ss == NULL || ss->ss_server_fld == NULL)
560                 RETURN(0);
561
562         /* Only check the local FLDB here */
563         if (osd_seq_exists(env, osd, fid_seq(fid)))
564                 RETURN(0);
565
566         RETURN(1);
567 }
568
569 /**
570  *      Inserts (key, value) pair in \a directory object.
571  *
572  *      \param  dt      osd index object
573  *      \param  key     key for index
574  *      \param  rec     record reference
575  *      \param  th      transaction handler
576  *      \param  capa    capability descriptor
577  *      \param  ignore_quota update should not affect quota
578  *
579  *      \retval  0  success
580  *      \retval -ve failure
581  */
582 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
583                           const struct dt_rec *rec, const struct dt_key *key,
584                           struct thandle *th, struct lustre_capa *capa,
585                           int ignore_quota)
586 {
587         struct osd_thread_info *oti = osd_oti_get(env);
588         struct osd_object   *parent = osd_dt_obj(dt);
589         struct osd_device   *osd = osd_obj2dev(parent);
590         struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
591         const struct lu_fid *fid = rec1->rec_fid;
592         struct osd_thandle  *oh;
593         struct osd_object   *child = NULL;
594         __u32                attr;
595         char                *name = (char *)key;
596         int                  rc;
597         ENTRY;
598
599         LASSERT(parent->oo_db);
600         LASSERT(osd_object_is_zap(parent->oo_db));
601
602         LASSERT(dt_object_exists(dt));
603         LASSERT(osd_invariant(parent));
604
605         LASSERT(th != NULL);
606         oh = container_of0(th, struct osd_thandle, ot_super);
607
608         rc = osd_remote_fid(env, osd, fid);
609         if (rc < 0) {
610                 CERROR("%s: Can not find object "DFID": rc = %d\n",
611                        osd->od_svname, PFID(fid), rc);
612                 RETURN(rc);
613         }
614
615         if (unlikely(rc == 1)) {
616                 /* Insert remote entry */
617                 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
618                 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
619         } else {
620                 /*
621                  * To simulate old Orion setups with ./..  stored in the
622                  * directories
623                  */
624                 /* Insert local entry */
625                 child = osd_object_find(env, dt, fid);
626                 if (IS_ERR(child))
627                         RETURN(PTR_ERR(child));
628
629                 LASSERT(child->oo_db);
630                 if (name[0] == '.') {
631                         if (name[1] == 0) {
632                                 /* do not store ".", instead generate it
633                                  * during iteration */
634                                 GOTO(out, rc = 0);
635                         } else if (name[1] == '.' && name[2] == 0) {
636                                 /* update parent dnode in the child.
637                                  * later it will be used to generate ".." */
638                                 rc = osd_object_sa_update(parent,
639                                                  SA_ZPL_PARENT(osd),
640                                                  &child->oo_db->db_object,
641                                                  8, oh);
642                                 GOTO(out, rc);
643                         }
644                 }
645                 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
646                 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
647                 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
648                 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
649                 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
650         }
651
652         oti->oti_zde.lzd_fid = *fid;
653         /* Insert (key,oid) into ZAP */
654         rc = -zap_add(osd->od_os, parent->oo_db->db_object,
655                       (char *)key, 8, sizeof(oti->oti_zde) / 8,
656                       (void *)&oti->oti_zde, oh->ot_tx);
657
658 out:
659         if (child != NULL)
660                 osd_object_put(env, child);
661
662         RETURN(rc);
663 }
664
665 static int osd_declare_dir_delete(const struct lu_env *env,
666                                   struct dt_object *dt,
667                                   const struct dt_key *key,
668                                   struct thandle *th)
669 {
670         struct osd_object *obj = osd_dt_obj(dt);
671         struct osd_thandle *oh;
672         ENTRY;
673
674         LASSERT(dt_object_exists(dt));
675         LASSERT(osd_invariant(obj));
676
677         LASSERT(th != NULL);
678         oh = container_of0(th, struct osd_thandle, ot_super);
679
680         LASSERT(obj->oo_db);
681         LASSERT(osd_object_is_zap(obj->oo_db));
682
683         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
684
685         RETURN(0);
686 }
687
688 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
689                           const struct dt_key *key, struct thandle *th,
690                           struct lustre_capa *capa)
691 {
692         struct osd_object *obj = osd_dt_obj(dt);
693         struct osd_device *osd = osd_obj2dev(obj);
694         struct osd_thandle *oh;
695         dmu_buf_t *zap_db = obj->oo_db;
696         char      *name = (char *)key;
697         int rc;
698         ENTRY;
699
700         LASSERT(obj->oo_db);
701         LASSERT(osd_object_is_zap(obj->oo_db));
702
703         LASSERT(th != NULL);
704         oh = container_of0(th, struct osd_thandle, ot_super);
705
706         /*
707          * In Orion . and .. were stored in the directory (not generated upon
708          * request as now). we preserve them for backward compatibility
709          */
710         if (name[0] == '.') {
711                 if (name[1] == 0) {
712                         RETURN(0);
713                 } else if (name[1] == '.' && name[2] == 0) {
714                         RETURN(0);
715                 }
716         }
717
718         /* Remove key from the ZAP */
719         rc = -zap_remove(osd->od_os, zap_db->db_object,
720                          (char *) key, oh->ot_tx);
721
722         if (unlikely(rc && rc != -ENOENT))
723                 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
724
725         RETURN(rc);
726 }
727
728 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
729                                      struct dt_object *dt,
730                                      __u32 unused,
731                                      struct lustre_capa *capa)
732 {
733         struct osd_zap_it *it;
734
735         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
736         if (!IS_ERR(it))
737                 it->ozi_pos = 0;
738
739         RETURN((struct dt_it *)it);
740 }
741
742 /**
743  *  Move Iterator to record specified by \a key
744  *
745  *  \param  di      osd iterator
746  *  \param  key     key for index
747  *
748  *  \retval +ve  di points to record with least key not larger than key
749  *  \retval  0   di points to exact matched key
750  *  \retval -ve  failure
751  */
752 static int osd_dir_it_get(const struct lu_env *env,
753                           struct dt_it *di, const struct dt_key *key)
754 {
755         struct osd_zap_it *it = (struct osd_zap_it *)di;
756         struct osd_object *obj = it->ozi_obj;
757         char              *name = (char *)key;
758         int                rc;
759         ENTRY;
760
761         LASSERT(it);
762         LASSERT(it->ozi_zc);
763
764         /* reset the cursor */
765         zap_cursor_fini(it->ozi_zc);
766         osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
767
768         /* XXX: implementation of the API is broken at the moment */
769         LASSERT(((const char *)key)[0] == 0);
770
771         if (name[0] == 0) {
772                 it->ozi_pos = 0;
773                 RETURN(1);
774         }
775
776         if (name[0] == '.') {
777                 if (name[1] == 0) {
778                         it->ozi_pos = 1;
779                         GOTO(out, rc = 1);
780                 } else if (name[1] == '.' && name[2] == 0) {
781                         it->ozi_pos = 2;
782                         GOTO(out, rc = 1);
783                 }
784         }
785
786         /* neither . nor .. - some real record */
787         it->ozi_pos = 3;
788         rc = +1;
789
790 out:
791         RETURN(rc);
792 }
793
794 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
795 {
796         /* PBS: do nothing : ref are incremented at retrive and decreamented
797          *      next/finish. */
798 }
799
800 /*
801  * in Orion . and .. were stored in the directory, while ZPL
802  * and current osd-zfs generate them up on request. so, we
803  * need to ignore previously stored . and ..
804  */
805 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
806                                         zap_attribute_t *za)
807 {
808         int rc, isdot;
809
810         do {
811                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
812
813                 isdot = 0;
814                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
815                         if (za->za_name[1] == 0) {
816                                 isdot = 1;
817                         } else if (za->za_name[1] == '.' &&
818                                    za->za_name[2] == 0) {
819                                 isdot = 1;
820                         }
821                         if (unlikely(isdot))
822                                 zap_cursor_advance(it->ozi_zc);
823                 }
824         } while (unlikely(rc == 0 && isdot));
825
826         return rc;
827 }
828
829 /**
830  * to load a directory entry at a time and stored it in
831  * iterator's in-memory data structure.
832  *
833  * \param di, struct osd_it_ea, iterator's in memory structure
834  *
835  * \retval +ve, iterator reached to end
836  * \retval   0, iterator not reached to end
837  * \retval -ve, on error
838  */
839 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
840 {
841         struct osd_zap_it *it = (struct osd_zap_it *)di;
842         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
843         int                rc;
844
845         ENTRY;
846
847         /* temp. storage should be enough for any key supported by ZFS */
848         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
849
850         /*
851          * the first ->next() moves the cursor to .
852          * the second ->next() moves the cursor to ..
853          * then we get to the real records and have to verify any exist
854          */
855         if (it->ozi_pos <= 2) {
856                 it->ozi_pos++;
857                 if (it->ozi_pos <=2)
858                         RETURN(0);
859
860         } else {
861                 zap_cursor_advance(it->ozi_zc);
862         }
863
864         /*
865          * According to current API we need to return error if its last entry.
866          * zap_cursor_advance() does not return any value. So we need to call
867          * retrieve to check if there is any record.  We should make
868          * changes to Iterator API to not return status for this API
869          */
870         rc = osd_index_retrieve_skip_dots(it, za);
871
872         if (rc == -ENOENT) /* end of dir */
873                 RETURN(+1);
874
875         RETURN(rc);
876 }
877
878 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
879                                      const struct dt_it *di)
880 {
881         struct osd_zap_it *it = (struct osd_zap_it *)di;
882         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
883         int                rc = 0;
884         ENTRY;
885
886         if (it->ozi_pos <= 1) {
887                 it->ozi_pos = 1;
888                 RETURN((struct dt_key *)".");
889         } else if (it->ozi_pos == 2) {
890                 RETURN((struct dt_key *)"..");
891         }
892
893         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
894                 RETURN(ERR_PTR(rc));
895
896         strcpy(it->ozi_name, za->za_name);
897
898         RETURN((struct dt_key *)it->ozi_name);
899 }
900
901 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
902 {
903         struct osd_zap_it *it = (struct osd_zap_it *)di;
904         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
905         int                rc;
906         ENTRY;
907
908         if (it->ozi_pos <= 1) {
909                 it->ozi_pos = 1;
910                 RETURN(2);
911         } else if (it->ozi_pos == 2) {
912                 RETURN(3);
913         }
914
915         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
916                 rc = strlen(za->za_name);
917
918         RETURN(rc);
919 }
920
921 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
922                           struct dt_rec *dtrec, __u32 attr)
923 {
924         struct osd_zap_it   *it = (struct osd_zap_it *)di;
925         struct lu_dirent    *lde = (struct lu_dirent *)dtrec;
926         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
927         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
928         int                  rc, namelen;
929         ENTRY;
930
931         if (it->ozi_pos <= 1) {
932                 lde->lde_hash = cpu_to_le64(1);
933                 strcpy(lde->lde_name, ".");
934                 lde->lde_namelen = cpu_to_le16(1);
935                 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
936                 lde->lde_attrs = LUDA_FID;
937                 /* append lustre attributes */
938                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
939                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
940                 it->ozi_pos = 1;
941                 GOTO(out, rc = 0);
942
943         } else if (it->ozi_pos == 2) {
944                 lde->lde_hash = cpu_to_le64(2);
945                 strcpy(lde->lde_name, "..");
946                 lde->lde_namelen = cpu_to_le16(2);
947                 lde->lde_attrs = LUDA_FID;
948                 /* append lustre attributes */
949                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
950                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
951                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
952                 /*
953                  * early Orion code was not setting LinkEA, so it's possible
954                  * some setups still have objects with no LinkEA set.
955                  * but at that time .. was a real record in the directory
956                  * so we should try to lookup .. in ZAP
957                  */
958                 if (rc != -ENOENT)
959                         GOTO(out, rc);
960         }
961
962         LASSERT(lde);
963
964         rc = -zap_cursor_retrieve(it->ozi_zc, za);
965         if (unlikely(rc != 0))
966                 GOTO(out, rc);
967
968         lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
969         namelen = strlen(za->za_name);
970         if (namelen > NAME_MAX)
971                 GOTO(out, rc = -EOVERFLOW);
972         strcpy(lde->lde_name, za->za_name);
973         lde->lde_namelen = cpu_to_le16(namelen);
974
975         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
976                 CERROR("%s: unsupported direntry format: %d %d\n",
977                        osd_obj2dev(it->ozi_obj)->od_svname,
978                        za->za_integer_length, (int)za->za_num_integers);
979
980                 GOTO(out, rc = -EIO);
981         }
982
983         rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
984                          za->za_name, za->za_integer_length, 3, zde);
985         if (rc)
986                 GOTO(out, rc);
987
988         lde->lde_fid = zde->lzd_fid;
989         lde->lde_attrs = LUDA_FID;
990
991         /* append lustre attributes */
992         osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
993
994         lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
995
996 out:
997         RETURN(rc);
998 }
999
1000 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1001                                __u32 attr)
1002 {
1003         struct osd_zap_it   *it = (struct osd_zap_it *)di;
1004         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
1005         size_t               namelen = 0;
1006         int                  rc;
1007         ENTRY;
1008
1009         if (it->ozi_pos <= 1)
1010                 namelen = 1;
1011         else if (it->ozi_pos == 2)
1012                 namelen = 2;
1013
1014         if (namelen > 0) {
1015                 rc = lu_dirent_calc_size(namelen, attr);
1016                 RETURN(rc);
1017         }
1018
1019         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1020         if (unlikely(rc != 0))
1021                 RETURN(rc);
1022
1023         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1024                 CERROR("%s: unsupported direntry format: %d %d\n",
1025                        osd_obj2dev(it->ozi_obj)->od_svname,
1026                        za->za_integer_length, (int)za->za_num_integers);
1027                 RETURN(-EIO);
1028         }
1029
1030         namelen = strlen(za->za_name);
1031         if (namelen > NAME_MAX)
1032                 RETURN(-EOVERFLOW);
1033
1034         rc = lu_dirent_calc_size(namelen, attr);
1035
1036         RETURN(rc);
1037 }
1038
1039 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1040 {
1041         struct osd_zap_it *it = (struct osd_zap_it *)di;
1042         __u64              pos;
1043         ENTRY;
1044
1045         if (it->ozi_pos <= 2)
1046                 pos = it->ozi_pos;
1047         else
1048                 pos = osd_zap_cursor_serialize(it->ozi_zc);
1049
1050         RETURN(pos);
1051 }
1052
1053 /*
1054  * return status :
1055  *  rc == 0 -> end of directory.
1056  *  rc >  0 -> ok, proceed.
1057  *  rc <  0 -> error.  ( EOVERFLOW  can be masked.)
1058  */
1059 static int osd_dir_it_load(const struct lu_env *env,
1060                         const struct dt_it *di, __u64 hash)
1061 {
1062         struct osd_zap_it *it = (struct osd_zap_it *)di;
1063         struct osd_object *obj = it->ozi_obj;
1064         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1065         int                rc;
1066         ENTRY;
1067
1068         /* reset the cursor */
1069         zap_cursor_fini(it->ozi_zc);
1070         osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1071
1072         if (hash <= 2) {
1073                 it->ozi_pos = hash;
1074                 rc = +1;
1075         } else {
1076                 it->ozi_pos = 3;
1077                 /* to return whether the end has been reached */
1078                 rc = osd_index_retrieve_skip_dots(it, za);
1079                 if (rc == 0)
1080                         rc = +1;
1081                 else if (rc == -ENOENT)
1082                         rc = 0;
1083         }
1084
1085         RETURN(rc);
1086 }
1087
1088 static struct dt_index_operations osd_dir_ops = {
1089         .dio_lookup         = osd_dir_lookup,
1090         .dio_declare_insert = osd_declare_dir_insert,
1091         .dio_insert         = osd_dir_insert,
1092         .dio_declare_delete = osd_declare_dir_delete,
1093         .dio_delete         = osd_dir_delete,
1094         .dio_it     = {
1095                 .init     = osd_dir_it_init,
1096                 .fini     = osd_index_it_fini,
1097                 .get      = osd_dir_it_get,
1098                 .put      = osd_dir_it_put,
1099                 .next     = osd_dir_it_next,
1100                 .key      = osd_dir_it_key,
1101                 .key_size = osd_dir_it_key_size,
1102                 .rec      = osd_dir_it_rec,
1103                 .rec_size = osd_dir_it_rec_size,
1104                 .store    = osd_dir_it_store,
1105                 .load     = osd_dir_it_load
1106         }
1107 };
1108
1109 /*
1110  * Primitives for index files using binary keys.
1111  */
1112
1113 /* key integer_size is 8 */
1114 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1115                                   const struct dt_key *src)
1116 {
1117         int size;
1118
1119         LASSERT(dst);
1120         LASSERT(src);
1121
1122         /* align keysize to 64bit */
1123         size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1124         size *= sizeof(__u64);
1125
1126         LASSERT(size <= MAXNAMELEN);
1127
1128         if (unlikely(size > o->oo_keysize))
1129                 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1130         memcpy(dst, (const char *)src, o->oo_keysize);
1131
1132         return (size/sizeof(__u64));
1133 }
1134
1135 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1136                         struct dt_rec *rec, const struct dt_key *key,
1137                         struct lustre_capa *capa)
1138 {
1139         struct osd_object *obj = osd_dt_obj(dt);
1140         struct osd_device *osd = osd_obj2dev(obj);
1141         __u64             *k = osd_oti_get(env)->oti_key64;
1142         int                rc;
1143         ENTRY;
1144
1145         rc = osd_prepare_key_uint64(obj, k, key);
1146
1147         rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1148                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1149                                 (void *)rec);
1150         RETURN(rc == 0 ? 1 : rc);
1151 }
1152
1153 static int osd_declare_index_insert(const struct lu_env *env,
1154                                     struct dt_object *dt,
1155                                     const struct dt_rec *rec,
1156                                     const struct dt_key *key,
1157                                     struct thandle *th)
1158 {
1159         struct osd_object  *obj = osd_dt_obj(dt);
1160         struct osd_thandle *oh;
1161         ENTRY;
1162
1163         LASSERT(th != NULL);
1164         oh = container_of0(th, struct osd_thandle, ot_super);
1165
1166         LASSERT(obj->oo_db);
1167
1168         dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1169
1170         /* It is not clear what API should be used for binary keys, so we pass
1171          * a null name which has the side effect of over-reserving space,
1172          * accounting for the worst case. See zap_count_write() */
1173         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1174
1175         RETURN(0);
1176 }
1177
1178 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1179                             const struct dt_rec *rec, const struct dt_key *key,
1180                             struct thandle *th, struct lustre_capa *capa,
1181                             int ignore_quota)
1182 {
1183         struct osd_object  *obj = osd_dt_obj(dt);
1184         struct osd_device  *osd = osd_obj2dev(obj);
1185         struct osd_thandle *oh;
1186         __u64              *k = osd_oti_get(env)->oti_key64;
1187         int                 rc;
1188         ENTRY;
1189
1190         LASSERT(obj->oo_db);
1191         LASSERT(dt_object_exists(dt));
1192         LASSERT(osd_invariant(obj));
1193         LASSERT(th != NULL);
1194
1195         oh = container_of0(th, struct osd_thandle, ot_super);
1196
1197         rc = osd_prepare_key_uint64(obj, k, key);
1198
1199         /* Insert (key,oid) into ZAP */
1200         rc = -zap_add_uint64(osd->od_os, obj->oo_db->db_object,
1201                              k, rc, obj->oo_recusize, obj->oo_recsize,
1202                              (void *)rec, oh->ot_tx);
1203         RETURN(rc);
1204 }
1205
1206 static int osd_declare_index_delete(const struct lu_env *env,
1207                                     struct dt_object *dt,
1208                                     const struct dt_key *key,
1209                                     struct thandle *th)
1210 {
1211         struct osd_object  *obj = osd_dt_obj(dt);
1212         struct osd_thandle *oh;
1213         ENTRY;
1214
1215         LASSERT(dt_object_exists(dt));
1216         LASSERT(osd_invariant(obj));
1217         LASSERT(th != NULL);
1218         LASSERT(obj->oo_db);
1219
1220         oh = container_of0(th, struct osd_thandle, ot_super);
1221         dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1222
1223         RETURN(0);
1224 }
1225
1226 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1227                             const struct dt_key *key, struct thandle *th,
1228                             struct lustre_capa *capa)
1229 {
1230         struct osd_object  *obj = osd_dt_obj(dt);
1231         struct osd_device  *osd = osd_obj2dev(obj);
1232         struct osd_thandle *oh;
1233         __u64              *k = osd_oti_get(env)->oti_key64;
1234         int                 rc;
1235         ENTRY;
1236
1237         LASSERT(obj->oo_db);
1238         LASSERT(th != NULL);
1239         oh = container_of0(th, struct osd_thandle, ot_super);
1240
1241         rc = osd_prepare_key_uint64(obj, k, key);
1242
1243         /* Remove binary key from the ZAP */
1244         rc = -zap_remove_uint64(osd->od_os, obj->oo_db->db_object,
1245                                 k, rc, oh->ot_tx);
1246         RETURN(rc);
1247 }
1248
1249 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1250                             const struct dt_key *key)
1251 {
1252         struct osd_zap_it *it = (struct osd_zap_it *)di;
1253         struct osd_object *obj = it->ozi_obj;
1254         struct osd_device *osd = osd_obj2dev(obj);
1255         ENTRY;
1256
1257         LASSERT(it);
1258         LASSERT(it->ozi_zc);
1259
1260         /*
1261          * XXX: we need a binary version of zap_cursor_move_to_key()
1262          *      to implement this API */
1263         if (*((const __u64 *)key) != 0)
1264                 CERROR("NOT IMPLEMETED YET (move to "LPX64")\n",
1265                        *((__u64 *)key));
1266
1267         zap_cursor_fini(it->ozi_zc);
1268         zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_db->db_object);
1269         it->ozi_reset = 1;
1270
1271         RETURN(+1);
1272 }
1273
1274 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1275 {
1276         struct osd_zap_it *it = (struct osd_zap_it *)di;
1277         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1278         int                rc;
1279         ENTRY;
1280
1281         if (it->ozi_reset == 0)
1282                 zap_cursor_advance(it->ozi_zc);
1283         it->ozi_reset = 0;
1284
1285         /*
1286          * According to current API we need to return error if it's last entry.
1287          * zap_cursor_advance() does not return any value. So we need to call
1288          * retrieve to check if there is any record.  We should make
1289          * changes to Iterator API to not return status for this API
1290          */
1291         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1292         if (rc == -ENOENT)
1293                 RETURN(+1);
1294
1295         RETURN((rc));
1296 }
1297
1298 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1299                                        const struct dt_it *di)
1300 {
1301         struct osd_zap_it *it = (struct osd_zap_it *)di;
1302         struct osd_object *obj = it->ozi_obj;
1303         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1304         int                rc = 0;
1305         ENTRY;
1306
1307         it->ozi_reset = 0;
1308         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1309         if (rc)
1310                 RETURN(ERR_PTR(rc));
1311
1312         /* the binary key is stored in the name */
1313         memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1314
1315         RETURN((struct dt_key *)&it->ozi_key);
1316 }
1317
1318 static int osd_index_it_key_size(const struct lu_env *env,
1319                                 const struct dt_it *di)
1320 {
1321         struct osd_zap_it *it = (struct osd_zap_it *)di;
1322         struct osd_object *obj = it->ozi_obj;
1323         RETURN(obj->oo_keysize);
1324 }
1325
1326 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1327                             struct dt_rec *rec, __u32 attr)
1328 {
1329         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1330         struct osd_zap_it *it = (struct osd_zap_it *)di;
1331         struct osd_object *obj = it->ozi_obj;
1332         struct osd_device *osd = osd_obj2dev(obj);
1333         __u64             *k = osd_oti_get(env)->oti_key64;
1334         int                rc;
1335         ENTRY;
1336
1337         it->ozi_reset = 0;
1338         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1339         if (rc)
1340                 RETURN(rc);
1341
1342         rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1343
1344         rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1345                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1346                                 (void *)rec);
1347         RETURN(rc);
1348 }
1349
1350 static __u64 osd_index_it_store(const struct lu_env *env,
1351                                 const struct dt_it *di)
1352 {
1353         struct osd_zap_it *it = (struct osd_zap_it *)di;
1354
1355         it->ozi_reset = 0;
1356         RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1357 }
1358
1359 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1360                              __u64 hash)
1361 {
1362         struct osd_zap_it *it = (struct osd_zap_it *)di;
1363         struct osd_object *obj = it->ozi_obj;
1364         struct osd_device *osd = osd_obj2dev(obj);
1365         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1366         int                rc;
1367         ENTRY;
1368
1369         /* reset the cursor */
1370         zap_cursor_fini(it->ozi_zc);
1371         zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1372                                    obj->oo_db->db_object, hash);
1373         it->ozi_reset = 0;
1374
1375         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1376         if (rc == 0)
1377                 RETURN(+1);
1378         else if (rc == -ENOENT)
1379                 RETURN(0);
1380
1381         RETURN(rc);
1382 }
1383
1384 static struct dt_index_operations osd_index_ops = {
1385         .dio_lookup             = osd_index_lookup,
1386         .dio_declare_insert     = osd_declare_index_insert,
1387         .dio_insert             = osd_index_insert,
1388         .dio_declare_delete     = osd_declare_index_delete,
1389         .dio_delete             = osd_index_delete,
1390         .dio_it = {
1391                 .init           = osd_index_it_init,
1392                 .fini           = osd_index_it_fini,
1393                 .get            = osd_index_it_get,
1394                 .put            = osd_index_it_put,
1395                 .next           = osd_index_it_next,
1396                 .key            = osd_index_it_key,
1397                 .key_size       = osd_index_it_key_size,
1398                 .rec            = osd_index_it_rec,
1399                 .store          = osd_index_it_store,
1400                 .load           = osd_index_it_load
1401         }
1402 };
1403
1404 struct osd_metadnode_it {
1405         struct osd_device       *mit_dev;
1406         __u64                    mit_pos;
1407         struct lu_fid            mit_fid;
1408         int                      mit_prefetched;
1409         __u64                    mit_prefetched_dnode;
1410 };
1411
1412 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1413                                             struct dt_object *dt, __u32 attr,
1414                                             struct lustre_capa *capa)
1415 {
1416         struct osd_device       *dev   = osd_dev(dt->do_lu.lo_dev);
1417         struct osd_metadnode_it *it;
1418         ENTRY;
1419
1420         OBD_ALLOC_PTR(it);
1421         if (unlikely(it == NULL))
1422                 RETURN(ERR_PTR(-ENOMEM));
1423
1424         it->mit_dev = dev;
1425
1426         /* XXX: dmu_object_next() does NOT find dnodes allocated
1427          *      in the current non-committed txg, so we force txg
1428          *      commit to find all existing dnodes ... */
1429         txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1430
1431         RETURN((struct dt_it *)it);
1432 }
1433
1434 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1435 {
1436         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1437
1438         OBD_FREE_PTR(it);
1439 }
1440
1441 static int osd_zfs_otable_it_get(const struct lu_env *env,
1442                                  struct dt_it *di, const struct dt_key *key)
1443 {
1444         return 0;
1445 }
1446
1447 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1448 {
1449 }
1450
1451 #define OTABLE_PREFETCH         256
1452
1453 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1454                                     struct osd_metadnode_it *it)
1455 {
1456         struct osd_device       *dev = it->mit_dev;
1457         int                      rc;
1458
1459         /* can go negative on the very first access to the iterator
1460          * or if some non-Lustre objects were found */
1461         if (unlikely(it->mit_prefetched < 0))
1462                 it->mit_prefetched = 0;
1463
1464         if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1465                 return;
1466
1467         if (it->mit_prefetched_dnode == 0)
1468                 it->mit_prefetched_dnode = it->mit_pos;
1469
1470         while (it->mit_prefetched < OTABLE_PREFETCH) {
1471                 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1472                                       B_FALSE, 0);
1473                 if (unlikely(rc != 0))
1474                         break;
1475
1476                 /* dmu_prefetch() was exported in 0.6.2, if you use with
1477                  * an older release, just comment it out - this is an
1478                  * optimization */
1479                 dmu_prefetch(dev->od_os, it->mit_prefetched_dnode, 0, 0);
1480
1481                 it->mit_prefetched++;
1482         }
1483 }
1484
1485 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1486 {
1487         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1488         struct lustre_mdt_attrs *lma;
1489         struct osd_device       *dev = it->mit_dev;
1490         nvlist_t                *nvbuf = NULL;
1491         uchar_t                 *v;
1492         __u64                    dnode;
1493         int                      rc, s;
1494
1495         memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1496
1497         dnode = it->mit_pos;
1498         do {
1499                 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1500                 if (unlikely(rc != 0))
1501                         GOTO(out, rc = 1);
1502                 it->mit_prefetched--;
1503
1504                 /* LMA is required for this to be a Lustre object.
1505                  * If there is no xattr skip it. */
1506                 rc = __osd_xattr_load(dev, it->mit_pos, &nvbuf);
1507                 if (unlikely(rc != 0))
1508                         continue;
1509
1510                 LASSERT(nvbuf != NULL);
1511                 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1512                 if (likely(rc == 0)) {
1513                         /* Lustre object */
1514                         lma = (struct lustre_mdt_attrs *)v;
1515                         lustre_lma_swab(lma);
1516                         it->mit_fid = lma->lma_self_fid;
1517                         nvlist_free(nvbuf);
1518                         break;
1519                 } else {
1520                         /* not a Lustre object, try next one */
1521                         nvlist_free(nvbuf);
1522                 }
1523
1524         } while (1);
1525
1526
1527         /* we aren't prefetching in the above loop because the number of
1528          * non-Lustre objects is very small and we will be repeating very
1529          * rare. in case we want to use this to iterate over non-Lustre
1530          * objects (i.e. when we convert regular ZFS in Lustre) it makes
1531          * sense to initiate prefetching in the loop */
1532
1533         /* 0 - there are more items, +1 - the end */
1534         if (likely(rc == 0))
1535                 osd_zfs_otable_prefetch(env, it);
1536
1537         CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1538                it->mit_pos, PFID(&it->mit_fid), rc);
1539
1540 out:
1541         return rc;
1542 }
1543
1544 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1545                                             const struct dt_it *di)
1546 {
1547         return NULL;
1548 }
1549
1550 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1551                                       const struct dt_it *di)
1552 {
1553         return sizeof(__u64);
1554 }
1555
1556 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1557                                  const struct dt_it *di,
1558                                  struct dt_rec *rec, __u32 attr)
1559 {
1560         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1561         struct lu_fid *fid = (struct lu_fid *)rec;
1562         ENTRY;
1563
1564         *fid = it->mit_fid;
1565
1566         RETURN(0);
1567 }
1568
1569
1570 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1571                                      const struct dt_it *di)
1572 {
1573         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1574
1575         return it->mit_pos;
1576 }
1577
1578 static int osd_zfs_otable_it_load(const struct lu_env *env,
1579                                   const struct dt_it *di, __u64 hash)
1580 {
1581         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1582
1583         it->mit_pos = hash;
1584         it->mit_prefetched = 0;
1585         it->mit_prefetched_dnode = 0;
1586
1587         return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1588 }
1589
1590 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1591                                      const struct dt_it *di, void *key_rec)
1592 {
1593         return 0;
1594 }
1595
1596 const struct dt_index_operations osd_zfs_otable_ops = {
1597         .dio_it = {
1598                 .init     = osd_zfs_otable_it_init,
1599                 .fini     = osd_zfs_otable_it_fini,
1600                 .get      = osd_zfs_otable_it_get,
1601                 .put      = osd_zfs_otable_it_put,
1602                 .next     = osd_zfs_otable_it_next,
1603                 .key      = osd_zfs_otable_it_key,
1604                 .key_size = osd_zfs_otable_it_key_size,
1605                 .rec      = osd_zfs_otable_it_rec,
1606                 .store    = osd_zfs_otable_it_store,
1607                 .load     = osd_zfs_otable_it_load,
1608                 .key_rec  = osd_zfs_otable_it_key_rec,
1609         }
1610 };
1611
1612 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1613                 const struct dt_index_features *feat)
1614 {
1615         struct osd_object *obj = osd_dt_obj(dt);
1616         ENTRY;
1617
1618         LASSERT(dt_object_exists(dt));
1619
1620         /*
1621          * XXX: implement support for fixed-size keys sorted with natural
1622          *      numerical way (not using internal hash value)
1623          */
1624         if (feat->dif_flags & DT_IND_RANGE)
1625                 RETURN(-ERANGE);
1626
1627         if (unlikely(feat == &dt_otable_features)) {
1628                 dt->do_index_ops = &osd_zfs_otable_ops;
1629                 RETURN(0);
1630         }
1631
1632         LASSERT(obj->oo_db != NULL);
1633         if (likely(feat == &dt_directory_features)) {
1634                 if (osd_object_is_zap(obj->oo_db))
1635                         dt->do_index_ops = &osd_dir_ops;
1636                 else
1637                         RETURN(-ENOTDIR);
1638         } else if (unlikely(feat == &dt_acct_features)) {
1639                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1640                 dt->do_index_ops = &osd_acct_index_ops;
1641         } else if (osd_object_is_zap(obj->oo_db) &&
1642                    dt->do_index_ops == NULL) {
1643                 /* For index file, we don't support variable key & record sizes
1644                  * and the key has to be unique */
1645                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1646                         RETURN(-EINVAL);
1647
1648                 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1649                         RETURN(-E2BIG);
1650                 if (feat->dif_keysize_max != feat->dif_keysize_min)
1651                         RETURN(-EINVAL);
1652
1653                 /* As for the record size, it should be a multiple of 8 bytes
1654                  * and smaller than the maximum value length supported by ZAP.
1655                  */
1656                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1657                         RETURN(-E2BIG);
1658                 if (feat->dif_recsize_max != feat->dif_recsize_min)
1659                         RETURN(-EINVAL);
1660
1661                 obj->oo_keysize = feat->dif_keysize_max;
1662                 obj->oo_recsize = feat->dif_recsize_max;
1663                 obj->oo_recusize = 1;
1664
1665                 /* ZFS prefers to work with array of 64bits */
1666                 if ((obj->oo_recsize & 7) == 0) {
1667                         obj->oo_recsize >>= 3;
1668                         obj->oo_recusize = 8;
1669                 }
1670                 dt->do_index_ops = &osd_index_ops;
1671         }
1672
1673         RETURN(0);
1674 }