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