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