Whamcloud - gitweb
LU-8882 osd: use bydnode methods to access DMU
[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/spa.h>
53 #include <sys/stat.h>
54 #include <sys/zap.h>
55 #include <sys/spa_impl.h>
56 #include <sys/zfs_znode.h>
57 #include <sys/dmu_tx.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/sa_impl.h>
61 #include <sys/txg.h>
62
63 static inline int osd_object_is_zap(dnode_t *dn)
64 {
65         return (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
66                         dn->dn_type == DMU_OT_USERGROUP_USED);
67 }
68
69 /* We don't actually have direct access to the zap_hashbits() function
70  * so just pretend like we do for now.  If this ever breaks we can look at
71  * it at that time. */
72 #define zap_hashbits(zc) 48
73 /*
74  * ZFS hash format:
75  * | cd (16 bits) | hash (48 bits) |
76  * we need it in other form:
77  * |0| hash (48 bit) | cd (15 bit) |
78  * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
79  * the readdir hashes from multiple directory stripes uniformly on the client.
80  * Another point is sign bit, the hash range should be in [0, 2^63-1] because
81  * loff_t (for llseek) needs to be a positive value.  This means the "cd" field
82  * should only be the low 15 bits.
83  */
84 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
85 {
86         uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
87
88         return (zfs_hash >> zap_hashbits(zc)) |
89                 (zfs_hash << (63 - zap_hashbits(zc)));
90 }
91
92 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
93                                     uint64_t id, uint64_t dirhash)
94 {
95         uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
96                 (dirhash >> (63 - zap_hashbits(zc)));
97
98         zap_cursor_init_serialized(zc, os, id, zfs_hash);
99 }
100
101 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
102                         uint64_t id, uint64_t dirhash)
103 {
104         zap_cursor_t *t;
105
106         OBD_ALLOC_PTR(t);
107         if (unlikely(t == NULL))
108                 return -ENOMEM;
109
110         osd_zap_cursor_init_serialized(t, os, id, dirhash);
111         *zc = t;
112
113         return 0;
114 }
115
116 void osd_zap_cursor_fini(zap_cursor_t *zc)
117 {
118         zap_cursor_fini(zc);
119         OBD_FREE_PTR(zc);
120 }
121
122 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
123                                                  struct osd_object *o,
124                                                  uint64_t dirhash)
125 {
126         struct osd_device *d = osd_obj2dev(o);
127         osd_zap_cursor_init_serialized(zc, d->od_os,
128                                        o->oo_dn->dn_object, dirhash);
129 }
130
131 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
132                         uint64_t dirhash)
133 {
134         struct osd_device *d = osd_obj2dev(o);
135         return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
136 }
137
138 static struct dt_it *osd_index_it_init(const struct lu_env *env,
139                                        struct dt_object *dt,
140                                        __u32 unused)
141 {
142         struct osd_thread_info  *info = osd_oti_get(env);
143         struct osd_zap_it       *it;
144         struct osd_object       *obj = osd_dt_obj(dt);
145         struct lu_object        *lo  = &dt->do_lu;
146         int                      rc;
147         ENTRY;
148
149         if (obj->oo_destroyed)
150                 RETURN(ERR_PTR(-ENOENT));
151
152         LASSERT(lu_object_exists(lo));
153         LASSERT(obj->oo_dn);
154         LASSERT(info);
155
156         OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
157         if (it == NULL)
158                 RETURN(ERR_PTR(-ENOMEM));
159
160         rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
161         if (rc != 0) {
162                 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
163                 RETURN(ERR_PTR(rc));
164         }
165
166         it->ozi_obj   = obj;
167         it->ozi_reset = 1;
168         lu_object_get(lo);
169
170         RETURN((struct dt_it *)it);
171 }
172
173 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
174 {
175         struct osd_zap_it       *it     = (struct osd_zap_it *)di;
176         struct osd_object       *obj;
177         ENTRY;
178
179         LASSERT(it);
180         LASSERT(it->ozi_obj);
181
182         obj = it->ozi_obj;
183
184         osd_zap_cursor_fini(it->ozi_zc);
185         osd_object_put(env, obj);
186         OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
187
188         EXIT;
189 }
190
191
192 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
193 {
194         /* PBS: do nothing : ref are incremented at retrive and decreamented
195          *      next/finish. */
196 }
197
198 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
199                                        int len, __u16 type)
200 {
201         const unsigned    align = sizeof(struct luda_type) - 1;
202         struct luda_type *lt;
203
204         /* check if file type is required */
205         if (attr & LUDA_TYPE) {
206                 len = (len + align) & ~align;
207
208                 lt = (void *)ent->lde_name + len;
209                 lt->lt_type = cpu_to_le16(DTTOIF(type));
210                 ent->lde_attrs |= LUDA_TYPE;
211         }
212
213         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
214 }
215
216 int __osd_xattr_load_by_oid(struct osd_device *osd, uint64_t oid, nvlist_t **sa)
217 {
218         sa_handle_t *hdl;
219         dmu_buf_t *db;
220         int rc;
221
222         rc = -dmu_bonus_hold(osd->od_os, oid, osd_obj_tag, &db);
223         if (rc < 0) {
224                 CERROR("%s: can't get bonus, rc = %d\n", osd->od_svname, rc);
225                 return rc;
226         }
227
228         rc = -sa_handle_get_from_db(osd->od_os, db, NULL, SA_HDL_PRIVATE, &hdl);
229         if (rc) {
230                 dmu_buf_rele(db, osd_obj_tag);
231                 return rc;
232         }
233
234         rc = __osd_xattr_load(osd, hdl, sa);
235
236         sa_handle_destroy(hdl);
237
238         return rc;
239 }
240 /**
241  * Get the object's FID from its LMA EA.
242  *
243  * \param[in] env       pointer to the thread context
244  * \param[in] osd       pointer to the OSD device
245  * \param[in] oid       the object's local identifier
246  * \param[out] fid      the buffer to hold the object's FID
247  *
248  * \retval              0 for success
249  * \retval              negative error number on failure
250  */
251 static int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
252                               uint64_t oid, struct lu_fid *fid)
253 {
254         struct objset           *os       = osd->od_os;
255         struct osd_thread_info  *oti      = osd_oti_get(env);
256         struct lustre_mdt_attrs *lma      =
257                         (struct lustre_mdt_attrs *)oti->oti_buf;
258         struct lu_buf            buf;
259         nvlist_t                *sa_xattr = NULL;
260         sa_handle_t             *sa_hdl   = NULL;
261         uchar_t                 *nv_value = NULL;
262         uint64_t                 xattr    = ZFS_NO_OBJECT;
263         int                      size     = 0;
264         int                      rc;
265         ENTRY;
266
267         rc = __osd_xattr_load_by_oid(osd, oid, &sa_xattr);
268         if (rc == -ENOENT)
269                 goto regular;
270
271         if (rc != 0)
272                 GOTO(out, rc);
273
274         rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
275                                        &size);
276         if (rc == -ENOENT)
277                 goto regular;
278
279         if (rc != 0)
280                 GOTO(out, rc);
281
282         if (unlikely(size > sizeof(oti->oti_buf)))
283                 GOTO(out, rc = -ERANGE);
284
285         memcpy(lma, nv_value, size);
286
287         goto found;
288
289 regular:
290         rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
291         if (rc != 0)
292                 GOTO(out, rc);
293
294         rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
295         sa_handle_destroy(sa_hdl);
296         if (rc != 0)
297                 GOTO(out, rc);
298
299         buf.lb_buf = lma;
300         buf.lb_len = sizeof(oti->oti_buf);
301         rc = __osd_xattr_get_large(env, osd, xattr, &buf,
302                                    XATTR_NAME_LMA, &size);
303         if (rc != 0)
304                 GOTO(out, rc);
305
306 found:
307         if (size < sizeof(*lma))
308                 GOTO(out, rc = -EIO);
309
310         lustre_lma_swab(lma);
311         if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
312                      CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
313                 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
314                       "oid = %#llx\n", osd->od_svname,
315                       lma->lma_incompat & ~LMA_INCOMPAT_SUPP, oid);
316                 GOTO(out, rc = -EOPNOTSUPP);
317         } else {
318                 *fid = lma->lma_self_fid;
319                 GOTO(out, rc = 0);
320         }
321
322 out:
323         if (sa_xattr != NULL)
324                 nvlist_free(sa_xattr);
325         return rc;
326 }
327
328 /*
329  * As we don't know FID, we can't use LU object, so this function
330  * partially duplicate __osd_xattr_get() which is built around
331  * LU-object and uses it to cache data like regular EA dnode, etc
332  */
333 static int osd_find_parent_by_dnode(const struct lu_env *env,
334                                     struct dt_object *o,
335                                     struct lu_fid *fid)
336 {
337         struct osd_object       *obj = osd_dt_obj(o);
338         struct osd_device       *osd = osd_obj2dev(obj);
339         uint64_t                 dnode = ZFS_NO_OBJECT;
340         int                      rc;
341         ENTRY;
342
343         /* first of all, get parent dnode from own attributes */
344         rc = osd_sa_handle_get(obj);
345         if (rc != 0)
346                 RETURN(rc);
347         rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
348         if (rc == 0)
349                 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
350
351         RETURN(rc);
352 }
353
354 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
355                                struct lu_fid *fid)
356 {
357         struct link_ea_header  *leh;
358         struct link_ea_entry   *lee;
359         struct lu_buf           buf;
360         int                     rc;
361         ENTRY;
362
363         buf.lb_buf = osd_oti_get(env)->oti_buf;
364         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
365
366         rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
367         if (rc == -ERANGE) {
368                 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
369                 if (rc < 0)
370                         RETURN(rc);
371                 LASSERT(rc > 0);
372                 OBD_ALLOC(buf.lb_buf, rc);
373                 if (buf.lb_buf == NULL)
374                         RETURN(-ENOMEM);
375                 buf.lb_len = rc;
376                 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
377         }
378         if (rc < 0)
379                 GOTO(out, rc);
380         if (rc < sizeof(*leh) + sizeof(*lee))
381                 GOTO(out, rc = -EINVAL);
382
383         leh = buf.lb_buf;
384         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
385                 leh->leh_magic = LINK_EA_MAGIC;
386                 leh->leh_reccount = __swab32(leh->leh_reccount);
387                 leh->leh_len = __swab64(leh->leh_len);
388         }
389         if (leh->leh_magic != LINK_EA_MAGIC)
390                 GOTO(out, rc = -EINVAL);
391         if (leh->leh_reccount == 0)
392                 GOTO(out, rc = -ENODATA);
393
394         lee = (struct link_ea_entry *)(leh + 1);
395         fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
396         rc = 0;
397
398 out:
399         if (buf.lb_buf != osd_oti_get(env)->oti_buf)
400                 OBD_FREE(buf.lb_buf, buf.lb_len);
401
402 #if 0
403         /* this block can be enabled for additional verification
404          * it's trying to match FID from LinkEA vs. FID from LMA */
405         if (rc == 0) {
406                 struct lu_fid fid2;
407                 int rc2;
408                 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
409                 if (rc2 == 0)
410                         if (lu_fid_eq(fid, &fid2) == 0)
411                                 CERROR("wrong parent: "DFID" != "DFID"\n",
412                                        PFID(fid), PFID(&fid2));
413         }
414 #endif
415
416         /* no LinkEA is found, let's try to find the fid in parent's LMA */
417         if (unlikely(rc != 0))
418                 rc = osd_find_parent_by_dnode(env, o, fid);
419
420         RETURN(rc);
421 }
422
423 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
424                           struct dt_rec *rec, const struct dt_key *key)
425 {
426         struct osd_thread_info *oti = osd_oti_get(env);
427         struct osd_object  *obj = osd_dt_obj(dt);
428         struct osd_device  *osd = osd_obj2dev(obj);
429         char               *name = (char *)key;
430         int                 rc;
431         ENTRY;
432
433         if (name[0] == '.') {
434                 if (name[1] == 0) {
435                         const struct lu_fid *f = lu_object_fid(&dt->do_lu);
436                         memcpy(rec, f, sizeof(*f));
437                         RETURN(1);
438                 } else if (name[1] == '.' && name[2] == 0) {
439                         rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
440                         RETURN(rc == 0 ? 1 : rc);
441                 }
442         }
443
444         memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
445         rc = osd_zap_lookup(osd, obj->oo_dn->dn_object, obj->oo_dn,
446                             (char *)key, 8, sizeof(oti->oti_zde) / 8,
447                             (void *)&oti->oti_zde);
448         if (rc != 0)
449                 RETURN(rc);
450
451         if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
452                 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
453                 RETURN(1);
454         }
455
456         rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode,
457                                 (struct lu_fid *)rec);
458
459         RETURN(rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc));
460 }
461
462 static int osd_declare_dir_insert(const struct lu_env *env,
463                                   struct dt_object *dt,
464                                   const struct dt_rec *rec,
465                                   const struct dt_key *key,
466                                   struct thandle *th)
467 {
468         struct osd_object       *obj = osd_dt_obj(dt);
469         struct osd_device       *osd = osd_obj2dev(obj);
470         const struct dt_insert_rec *rec1;
471         const struct lu_fid     *fid;
472         struct osd_thandle      *oh;
473         uint64_t                 object;
474         ENTRY;
475
476         rec1 = (struct dt_insert_rec *)rec;
477         fid = rec1->rec_fid;
478         LASSERT(fid != NULL);
479         LASSERT(rec1->rec_type != 0);
480
481         LASSERT(th != NULL);
482         oh = container_of0(th, struct osd_thandle, ot_super);
483
484         /* This is for inserting dot/dotdot for new created dir. */
485         if (obj->oo_dn == NULL)
486                 object = DMU_NEW_OBJECT;
487         else
488                 object = obj->oo_dn->dn_object;
489
490         /* do not specify the key as then DMU is trying to look it up
491          * which is very expensive. usually the layers above lookup
492          * before insertion */
493         osd_tx_hold_zap(oh->ot_tx, object, obj->oo_dn, TRUE, NULL);
494
495         osd_idc_find_or_init(env, osd, fid);
496
497         RETURN(0);
498 }
499
500 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
501                           u64 seq)
502 {
503         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
504         struct seq_server_site  *ss = osd_seq_site(osd);
505         int                     rc;
506         ENTRY;
507
508         LASSERT(ss != NULL);
509         LASSERT(ss->ss_server_fld != NULL);
510
511         rc = osd_fld_lookup(env, osd, seq, range);
512         if (rc != 0) {
513                 if (rc != -ENOENT)
514                         CERROR("%s: Can not lookup fld for %#llx\n",
515                                osd_name(osd), seq);
516                 RETURN(0);
517         }
518
519         RETURN(ss->ss_node_id == range->lsr_index);
520 }
521
522 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
523                    const struct lu_fid *fid)
524 {
525         struct seq_server_site  *ss = osd_seq_site(osd);
526         ENTRY;
527
528         /* FID seqs not in FLDB, must be local seq */
529         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
530                 RETURN(0);
531
532         /* If FLD is not being initialized yet, it only happens during the
533          * initialization, likely during mgs initialization, and we assume
534          * this is local FID. */
535         if (ss == NULL || ss->ss_server_fld == NULL)
536                 RETURN(0);
537
538         /* Only check the local FLDB here */
539         if (osd_seq_exists(env, osd, fid_seq(fid)))
540                 RETURN(0);
541
542         RETURN(1);
543 }
544
545 /**
546  *      Inserts (key, value) pair in \a directory object.
547  *
548  *      \param  dt      osd index object
549  *      \param  key     key for index
550  *      \param  rec     record reference
551  *      \param  th      transaction handler
552  *      \param  ignore_quota update should not affect quota
553  *
554  *      \retval  0  success
555  *      \retval -ve failure
556  */
557 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
558                           const struct dt_rec *rec, const struct dt_key *key,
559                           struct thandle *th, int ignore_quota)
560 {
561         struct osd_thread_info *oti = osd_oti_get(env);
562         struct osd_object   *parent = osd_dt_obj(dt);
563         struct osd_device   *osd = osd_obj2dev(parent);
564         struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
565         const struct lu_fid *fid = rec1->rec_fid;
566         struct osd_thandle *oh;
567         struct osd_idmap_cache *idc;
568         char                *name = (char *)key;
569         int                  rc;
570         ENTRY;
571
572         LASSERT(parent->oo_dn);
573
574         LASSERT(dt_object_exists(dt));
575         LASSERT(osd_invariant(parent));
576
577         LASSERT(th != NULL);
578         oh = container_of0(th, struct osd_thandle, ot_super);
579
580         idc = osd_idc_find(env, osd, fid);
581         if (unlikely(idc == NULL)) {
582                 /* this dt_insert() wasn't declared properly, so
583                  * FID is missing in OI cache. we better do not
584                  * lookup FID in FLDB/OI and don't risk to deadlock,
585                  * but in some special cases (lfsck testing, etc)
586                  * it's much simpler than fixing a caller */
587                 CERROR("%s: "DFID" wasn't declared for insert\n",
588                        osd_name(osd), PFID(fid));
589                 idc = osd_idc_find_or_init(env, osd, fid);
590                 if (IS_ERR(idc))
591                         RETURN(PTR_ERR(idc));
592         }
593
594         if (idc->oic_remote) {
595                 /* Insert remote entry */
596                 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
597                 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
598         } else {
599                 if (unlikely(idc->oic_dnode == 0)) {
600                         /* for a reason OI cache wasn't filled properly */
601                         CERROR("%s: OIC for "DFID" isn't filled\n",
602                                osd_name(osd), PFID(fid));
603                         RETURN(-EINVAL);
604                 }
605                 if (name[0] == '.') {
606                         if (name[1] == 0) {
607                                 /* do not store ".", instead generate it
608                                  * during iteration */
609                                 GOTO(out, rc = 0);
610                         } else if (name[1] == '.' && name[2] == 0) {
611                                 uint64_t dnode = idc->oic_dnode;
612                                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
613                                         dnode--;
614
615                                 /* update parent dnode in the child.
616                                  * later it will be used to generate ".." */
617                                 rc = osd_object_sa_update(parent,
618                                                  SA_ZPL_PARENT(osd),
619                                                  &dnode, 8, oh);
620
621                                 GOTO(out, rc);
622                         }
623                 }
624                 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
625                 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
626                 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
627                 oti->oti_zde.lzd_reg.zde_dnode = idc->oic_dnode;
628         }
629
630         oti->oti_zde.lzd_fid = *fid;
631         /* Insert (key,oid) into ZAP */
632         rc = osd_zap_add(osd, parent->oo_dn->dn_object, parent->oo_dn,
633                          (char *)key, 8, sizeof(oti->oti_zde) / 8,
634                          (void *)&oti->oti_zde, oh->ot_tx);
635         if (unlikely(rc == -EEXIST &&
636                      name[0] == '.' && name[1] == '.' && name[2] == 0))
637                 /* Update (key,oid) in ZAP */
638                 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object,
639                                 (char *)key, 8, sizeof(oti->oti_zde) / 8,
640                                 (void *)&oti->oti_zde, oh->ot_tx);
641
642 out:
643
644         RETURN(rc);
645 }
646
647 static int osd_declare_dir_delete(const struct lu_env *env,
648                                   struct dt_object *dt,
649                                   const struct dt_key *key,
650                                   struct thandle *th)
651 {
652         struct osd_object  *obj = osd_dt_obj(dt);
653         struct osd_thandle *oh;
654         uint64_t            dnode;
655         ENTRY;
656
657         LASSERT(dt_object_exists(dt));
658         LASSERT(osd_invariant(obj));
659
660         LASSERT(th != NULL);
661         oh = container_of0(th, struct osd_thandle, ot_super);
662
663         if (dt_object_exists(dt)) {
664                 LASSERT(obj->oo_dn);
665                 dnode = obj->oo_dn->dn_object;
666         } else {
667                 dnode = DMU_NEW_OBJECT;
668         }
669
670         /* do not specify the key as then DMU is trying to look it up
671          * which is very expensive. usually the layers above lookup
672          * before deletion */
673         osd_tx_hold_zap(oh->ot_tx, dnode, obj->oo_dn, FALSE, NULL);
674
675         RETURN(0);
676 }
677
678 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
679                           const struct dt_key *key, struct thandle *th)
680 {
681         struct osd_object *obj = osd_dt_obj(dt);
682         struct osd_device *osd = osd_obj2dev(obj);
683         struct osd_thandle *oh;
684         dnode_t *zap_dn = obj->oo_dn;
685         char      *name = (char *)key;
686         int rc;
687         ENTRY;
688
689         LASSERT(zap_dn);
690
691         LASSERT(th != NULL);
692         oh = container_of0(th, struct osd_thandle, ot_super);
693
694         /*
695          * In Orion . and .. were stored in the directory (not generated upon
696          * request as now). we preserve them for backward compatibility
697          */
698         if (name[0] == '.') {
699                 if (name[1] == 0) {
700                         RETURN(0);
701                 } else if (name[1] == '.' && name[2] == 0) {
702                         RETURN(0);
703                 }
704         }
705
706         /* Remove key from the ZAP */
707         rc = osd_zap_remove(osd, zap_dn->dn_object, zap_dn,
708                             (char *)key, oh->ot_tx);
709
710         if (unlikely(rc && rc != -ENOENT))
711                 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
712
713         RETURN(rc);
714 }
715
716 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
717                                      struct dt_object *dt,
718                                      __u32 unused)
719 {
720         struct osd_zap_it *it;
721
722         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
723         if (!IS_ERR(it))
724                 it->ozi_pos = 0;
725
726         RETURN((struct dt_it *)it);
727 }
728
729 /**
730  *  Move Iterator to record specified by \a key
731  *
732  *  \param  di      osd iterator
733  *  \param  key     key for index
734  *
735  *  \retval +ve  di points to record with least key not larger than key
736  *  \retval  0   di points to exact matched key
737  *  \retval -ve  failure
738  */
739 static int osd_dir_it_get(const struct lu_env *env,
740                           struct dt_it *di, const struct dt_key *key)
741 {
742         struct osd_zap_it *it = (struct osd_zap_it *)di;
743         struct osd_object *obj = it->ozi_obj;
744         char              *name = (char *)key;
745         int                rc;
746         ENTRY;
747
748         LASSERT(it);
749         LASSERT(it->ozi_zc);
750
751         /* reset the cursor */
752         zap_cursor_fini(it->ozi_zc);
753         osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
754
755         /* XXX: implementation of the API is broken at the moment */
756         LASSERT(((const char *)key)[0] == 0);
757
758         if (name[0] == 0) {
759                 it->ozi_pos = 0;
760                 RETURN(1);
761         }
762
763         if (name[0] == '.') {
764                 if (name[1] == 0) {
765                         it->ozi_pos = 1;
766                         GOTO(out, rc = 1);
767                 } else if (name[1] == '.' && name[2] == 0) {
768                         it->ozi_pos = 2;
769                         GOTO(out, rc = 1);
770                 }
771         }
772
773         /* neither . nor .. - some real record */
774         it->ozi_pos = 3;
775         rc = +1;
776
777 out:
778         RETURN(rc);
779 }
780
781 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
782 {
783         /* PBS: do nothing : ref are incremented at retrive and decreamented
784          *      next/finish. */
785 }
786
787 /*
788  * in Orion . and .. were stored in the directory, while ZPL
789  * and current osd-zfs generate them up on request. so, we
790  * need to ignore previously stored . and ..
791  */
792 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
793                                         zap_attribute_t *za)
794 {
795         int rc, isdot;
796
797         do {
798                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
799
800                 isdot = 0;
801                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
802                         if (za->za_name[1] == 0) {
803                                 isdot = 1;
804                         } else if (za->za_name[1] == '.' &&
805                                    za->za_name[2] == 0) {
806                                 isdot = 1;
807                         }
808                         if (unlikely(isdot))
809                                 zap_cursor_advance(it->ozi_zc);
810                 }
811         } while (unlikely(rc == 0 && isdot));
812
813         return rc;
814 }
815
816 /**
817  * to load a directory entry at a time and stored it in
818  * iterator's in-memory data structure.
819  *
820  * \param di, struct osd_it_ea, iterator's in memory structure
821  *
822  * \retval +ve, iterator reached to end
823  * \retval   0, iterator not reached to end
824  * \retval -ve, on error
825  */
826 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
827 {
828         struct osd_zap_it *it = (struct osd_zap_it *)di;
829         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
830         int                rc;
831
832         ENTRY;
833
834         /* temp. storage should be enough for any key supported by ZFS */
835         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
836
837         /*
838          * the first ->next() moves the cursor to .
839          * the second ->next() moves the cursor to ..
840          * then we get to the real records and have to verify any exist
841          */
842         if (it->ozi_pos <= 2) {
843                 it->ozi_pos++;
844                 if (it->ozi_pos <=2)
845                         RETURN(0);
846
847         } else {
848                 zap_cursor_advance(it->ozi_zc);
849         }
850
851         /*
852          * According to current API we need to return error if its last entry.
853          * zap_cursor_advance() does not return any value. So we need to call
854          * retrieve to check if there is any record.  We should make
855          * changes to Iterator API to not return status for this API
856          */
857         rc = osd_index_retrieve_skip_dots(it, za);
858
859         if (rc == -ENOENT) /* end of dir */
860                 RETURN(+1);
861
862         RETURN(rc);
863 }
864
865 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
866                                      const struct dt_it *di)
867 {
868         struct osd_zap_it *it = (struct osd_zap_it *)di;
869         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
870         int                rc = 0;
871         ENTRY;
872
873         if (it->ozi_pos <= 1) {
874                 it->ozi_pos = 1;
875                 RETURN((struct dt_key *)".");
876         } else if (it->ozi_pos == 2) {
877                 RETURN((struct dt_key *)"..");
878         }
879
880         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
881                 RETURN(ERR_PTR(rc));
882
883         strcpy(it->ozi_name, za->za_name);
884
885         RETURN((struct dt_key *)it->ozi_name);
886 }
887
888 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
889 {
890         struct osd_zap_it *it = (struct osd_zap_it *)di;
891         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
892         int                rc;
893         ENTRY;
894
895         if (it->ozi_pos <= 1) {
896                 it->ozi_pos = 1;
897                 RETURN(2);
898         } else if (it->ozi_pos == 2) {
899                 RETURN(3);
900         }
901
902         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
903                 rc = strlen(za->za_name);
904
905         RETURN(rc);
906 }
907
908 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
909                           struct dt_rec *dtrec, __u32 attr)
910 {
911         struct osd_zap_it   *it = (struct osd_zap_it *)di;
912         struct lu_dirent    *lde = (struct lu_dirent *)dtrec;
913         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
914         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
915         int                  rc, namelen;
916         ENTRY;
917
918         if (it->ozi_pos <= 1) {
919                 lde->lde_hash = cpu_to_le64(1);
920                 strcpy(lde->lde_name, ".");
921                 lde->lde_namelen = cpu_to_le16(1);
922                 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
923                 lde->lde_attrs = LUDA_FID;
924                 /* append lustre attributes */
925                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
926                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
927                 it->ozi_pos = 1;
928                 GOTO(out, rc = 0);
929
930         } else if (it->ozi_pos == 2) {
931                 lde->lde_hash = cpu_to_le64(2);
932                 strcpy(lde->lde_name, "..");
933                 lde->lde_namelen = cpu_to_le16(2);
934                 lde->lde_attrs = LUDA_FID;
935                 /* append lustre attributes */
936                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
937                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
938                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
939
940                 /* ENOENT happens at the root of filesystem so ignore it */
941                 if (rc == -ENOENT)
942                         rc = 0;
943                 GOTO(out, rc);
944         }
945
946         LASSERT(lde);
947
948         rc = -zap_cursor_retrieve(it->ozi_zc, za);
949         if (unlikely(rc != 0))
950                 GOTO(out, rc);
951
952         lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
953         namelen = strlen(za->za_name);
954         if (namelen > NAME_MAX)
955                 GOTO(out, rc = -EOVERFLOW);
956         strcpy(lde->lde_name, za->za_name);
957         lde->lde_namelen = cpu_to_le16(namelen);
958
959         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
960                 CERROR("%s: unsupported direntry format: %d %d\n",
961                        osd_obj2dev(it->ozi_obj)->od_svname,
962                        za->za_integer_length, (int)za->za_num_integers);
963
964                 GOTO(out, rc = -EIO);
965         }
966
967         rc = osd_zap_lookup(osd_obj2dev(it->ozi_obj), it->ozi_zc->zc_zapobj,
968                             it->ozi_obj->oo_dn, za->za_name,
969                             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_dn->dn_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_dn);
1151
1152         /* do not specify the key as then DMU is trying to look it up
1153          * which is very expensive. usually the layers above lookup
1154          * before insertion */
1155         osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1156                         TRUE, NULL);
1157
1158         RETURN(0);
1159 }
1160
1161 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1162                             const struct dt_rec *rec, const struct dt_key *key,
1163                             struct thandle *th, int ignore_quota)
1164 {
1165         struct osd_object  *obj = osd_dt_obj(dt);
1166         struct osd_device  *osd = osd_obj2dev(obj);
1167         struct osd_thandle *oh;
1168         __u64              *k = osd_oti_get(env)->oti_key64;
1169         int                 rc;
1170         ENTRY;
1171
1172         LASSERT(obj->oo_dn);
1173         LASSERT(dt_object_exists(dt));
1174         LASSERT(osd_invariant(obj));
1175         LASSERT(th != NULL);
1176
1177         oh = container_of0(th, struct osd_thandle, ot_super);
1178
1179         rc = osd_prepare_key_uint64(obj, k, key);
1180
1181         /* Insert (key,oid) into ZAP */
1182         rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1183                              k, rc, obj->oo_recusize, obj->oo_recsize,
1184                              (void *)rec, oh->ot_tx);
1185         RETURN(rc);
1186 }
1187
1188 static int osd_declare_index_delete(const struct lu_env *env,
1189                                     struct dt_object *dt,
1190                                     const struct dt_key *key,
1191                                     struct thandle *th)
1192 {
1193         struct osd_object  *obj = osd_dt_obj(dt);
1194         struct osd_thandle *oh;
1195         ENTRY;
1196
1197         LASSERT(dt_object_exists(dt));
1198         LASSERT(osd_invariant(obj));
1199         LASSERT(th != NULL);
1200         LASSERT(obj->oo_dn);
1201
1202         oh = container_of0(th, struct osd_thandle, ot_super);
1203
1204         /* do not specify the key as then DMU is trying to look it up
1205          * which is very expensive. usually the layers above lookup
1206          * before deletion */
1207         osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1208                         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_dn);
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_dn->dn_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_dn->dn_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_dn->dn_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_dn->dn_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         if (!dev->od_dt_dev.dd_rdonly)
1415                 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1416
1417         RETURN((struct dt_it *)it);
1418 }
1419
1420 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1421 {
1422         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1423
1424         OBD_FREE_PTR(it);
1425 }
1426
1427 static int osd_zfs_otable_it_get(const struct lu_env *env,
1428                                  struct dt_it *di, const struct dt_key *key)
1429 {
1430         return 0;
1431 }
1432
1433 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1434 {
1435 }
1436
1437 #define OTABLE_PREFETCH         256
1438
1439 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1440                                     struct osd_metadnode_it *it)
1441 {
1442         struct osd_device       *dev = it->mit_dev;
1443         int                      rc;
1444
1445         /* can go negative on the very first access to the iterator
1446          * or if some non-Lustre objects were found */
1447         if (unlikely(it->mit_prefetched < 0))
1448                 it->mit_prefetched = 0;
1449
1450         if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1451                 return;
1452
1453         if (it->mit_prefetched_dnode == 0)
1454                 it->mit_prefetched_dnode = it->mit_pos;
1455
1456         while (it->mit_prefetched < OTABLE_PREFETCH) {
1457                 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1458                                       B_FALSE, 0);
1459                 if (unlikely(rc != 0))
1460                         break;
1461
1462                 osd_dmu_prefetch(dev->od_os, it->mit_prefetched_dnode,
1463                                  0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1464
1465                 it->mit_prefetched++;
1466         }
1467 }
1468
1469 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1470 {
1471         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1472         struct lustre_mdt_attrs *lma;
1473         struct osd_device       *dev = it->mit_dev;
1474         nvlist_t                *nvbuf = NULL;
1475         uchar_t                 *v;
1476         __u64                    dnode;
1477         int                      rc, s;
1478
1479         memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1480
1481         dnode = it->mit_pos;
1482         do {
1483                 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1484                 if (unlikely(rc != 0))
1485                         GOTO(out, rc = 1);
1486                 it->mit_prefetched--;
1487
1488                 /* LMA is required for this to be a Lustre object.
1489                  * If there is no xattr skip it. */
1490                 rc = __osd_xattr_load_by_oid(dev, it->mit_pos, &nvbuf);
1491                 if (unlikely(rc != 0))
1492                         continue;
1493
1494                 LASSERT(nvbuf != NULL);
1495                 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1496                 if (likely(rc == 0)) {
1497                         /* Lustre object */
1498                         lma = (struct lustre_mdt_attrs *)v;
1499                         lustre_lma_swab(lma);
1500                         it->mit_fid = lma->lma_self_fid;
1501                         nvlist_free(nvbuf);
1502                         break;
1503                 } else {
1504                         /* not a Lustre object, try next one */
1505                         nvlist_free(nvbuf);
1506                 }
1507
1508         } while (1);
1509
1510
1511         /* we aren't prefetching in the above loop because the number of
1512          * non-Lustre objects is very small and we will be repeating very
1513          * rare. in case we want to use this to iterate over non-Lustre
1514          * objects (i.e. when we convert regular ZFS in Lustre) it makes
1515          * sense to initiate prefetching in the loop */
1516
1517         /* 0 - there are more items, +1 - the end */
1518         if (likely(rc == 0))
1519                 osd_zfs_otable_prefetch(env, it);
1520
1521         CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1522                it->mit_pos, PFID(&it->mit_fid), rc);
1523
1524 out:
1525         return rc;
1526 }
1527
1528 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1529                                             const struct dt_it *di)
1530 {
1531         return NULL;
1532 }
1533
1534 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1535                                       const struct dt_it *di)
1536 {
1537         return sizeof(__u64);
1538 }
1539
1540 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1541                                  const struct dt_it *di,
1542                                  struct dt_rec *rec, __u32 attr)
1543 {
1544         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1545         struct lu_fid *fid = (struct lu_fid *)rec;
1546         ENTRY;
1547
1548         *fid = it->mit_fid;
1549
1550         RETURN(0);
1551 }
1552
1553
1554 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1555                                      const struct dt_it *di)
1556 {
1557         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1558
1559         return it->mit_pos;
1560 }
1561
1562 static int osd_zfs_otable_it_load(const struct lu_env *env,
1563                                   const struct dt_it *di, __u64 hash)
1564 {
1565         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1566
1567         it->mit_pos = hash;
1568         it->mit_prefetched = 0;
1569         it->mit_prefetched_dnode = 0;
1570
1571         return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1572 }
1573
1574 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1575                                      const struct dt_it *di, void *key_rec)
1576 {
1577         return 0;
1578 }
1579
1580 const struct dt_index_operations osd_zfs_otable_ops = {
1581         .dio_it = {
1582                 .init     = osd_zfs_otable_it_init,
1583                 .fini     = osd_zfs_otable_it_fini,
1584                 .get      = osd_zfs_otable_it_get,
1585                 .put      = osd_zfs_otable_it_put,
1586                 .next     = osd_zfs_otable_it_next,
1587                 .key      = osd_zfs_otable_it_key,
1588                 .key_size = osd_zfs_otable_it_key_size,
1589                 .rec      = osd_zfs_otable_it_rec,
1590                 .store    = osd_zfs_otable_it_store,
1591                 .load     = osd_zfs_otable_it_load,
1592                 .key_rec  = osd_zfs_otable_it_key_rec,
1593         }
1594 };
1595
1596 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1597                 const struct dt_index_features *feat)
1598 {
1599         struct osd_object *obj = osd_dt_obj(dt);
1600         int rc = 0;
1601         ENTRY;
1602
1603         down_read(&obj->oo_guard);
1604
1605         /*
1606          * XXX: implement support for fixed-size keys sorted with natural
1607          *      numerical way (not using internal hash value)
1608          */
1609         if (feat->dif_flags & DT_IND_RANGE)
1610                 GOTO(out, rc = -ERANGE);
1611
1612         if (unlikely(feat == &dt_otable_features)) {
1613                 dt->do_index_ops = &osd_zfs_otable_ops;
1614                 GOTO(out, rc = 0);
1615         }
1616
1617         LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
1618         if (likely(feat == &dt_directory_features)) {
1619                 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
1620                         dt->do_index_ops = &osd_dir_ops;
1621                 else
1622                         GOTO(out, rc = -ENOTDIR);
1623         } else if (unlikely(feat == &dt_acct_features)) {
1624                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1625                 dt->do_index_ops = &osd_acct_index_ops;
1626         } else if (dt->do_index_ops == NULL) {
1627                 /* For index file, we don't support variable key & record sizes
1628                  * and the key has to be unique */
1629                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1630                         GOTO(out, rc = -EINVAL);
1631
1632                 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1633                         GOTO(out, rc = -E2BIG);
1634                 if (feat->dif_keysize_max != feat->dif_keysize_min)
1635                         GOTO(out, rc = -EINVAL);
1636
1637                 /* As for the record size, it should be a multiple of 8 bytes
1638                  * and smaller than the maximum value length supported by ZAP.
1639                  */
1640                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1641                         GOTO(out, rc = -E2BIG);
1642                 if (feat->dif_recsize_max != feat->dif_recsize_min)
1643                         GOTO(out, rc = -EINVAL);
1644
1645                 obj->oo_keysize = feat->dif_keysize_max;
1646                 obj->oo_recsize = feat->dif_recsize_max;
1647                 obj->oo_recusize = 1;
1648
1649                 /* ZFS prefers to work with array of 64bits */
1650                 if ((obj->oo_recsize & 7) == 0) {
1651                         obj->oo_recsize >>= 3;
1652                         obj->oo_recusize = 8;
1653                 }
1654                 dt->do_index_ops = &osd_index_ops;
1655         }
1656
1657 out:
1658         up_read(&obj->oo_guard);
1659
1660         RETURN(rc);
1661 }