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