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