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