Whamcloud - gitweb
20f86f26a387e2564657915a72b32caefc364d34
[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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd-zfs/osd_index.c
33  *
34  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35  * Author: Mike Pershin <tappro@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_OSD
39
40 #include <libcfs/libcfs.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <lustre_disk.h>
46 #include <lustre_fid.h>
47
48 #include "osd_internal.h"
49
50 #include <sys/dnode.h>
51 #include <sys/spa.h>
52 #include <sys/stat.h>
53 #include <sys/zap.h>
54 #include <sys/spa_impl.h>
55 #include <sys/zfs_znode.h>
56 #include <sys/dmu_tx.h>
57 #include <sys/dmu_objset.h>
58 #include <sys/dsl_prop.h>
59 #include <sys/sa_impl.h>
60 #include <sys/txg.h>
61
62 static inline int osd_object_is_zap(dnode_t *dn)
63 {
64         return (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
65                         dn->dn_type == DMU_OT_USERGROUP_USED);
66 }
67
68 /* We don't actually have direct access to the zap_hashbits() function
69  * so just pretend like we do for now.  If this ever breaks we can look at
70  * it at that time. */
71 #define zap_hashbits(zc) 48
72 /*
73  * ZFS hash format:
74  * | cd (16 bits) | hash (48 bits) |
75  * we need it in other form:
76  * |0| hash (48 bit) | cd (15 bit) |
77  * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
78  * the readdir hashes from multiple directory stripes uniformly on the client.
79  * Another point is sign bit, the hash range should be in [0, 2^63-1] because
80  * loff_t (for llseek) needs to be a positive value.  This means the "cd" field
81  * should only be the low 15 bits.
82  */
83 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
84 {
85         uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
86
87         return (zfs_hash >> zap_hashbits(zc)) |
88                 (zfs_hash << (63 - zap_hashbits(zc)));
89 }
90
91 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
92                                     uint64_t id, uint64_t dirhash)
93 {
94         uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
95                 (dirhash >> (63 - zap_hashbits(zc)));
96
97         zap_cursor_init_serialized(zc, os, id, zfs_hash);
98 }
99
100 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
101                         uint64_t id, uint64_t dirhash)
102 {
103         zap_cursor_t *t;
104
105         OBD_ALLOC_PTR(t);
106         if (unlikely(t == NULL))
107                 return -ENOMEM;
108
109         osd_zap_cursor_init_serialized(t, os, id, dirhash);
110         *zc = t;
111
112         return 0;
113 }
114
115 void osd_zap_cursor_fini(zap_cursor_t *zc)
116 {
117         zap_cursor_fini(zc);
118         OBD_FREE_PTR(zc);
119 }
120
121 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
122                                                  struct osd_object *o,
123                                                  uint64_t dirhash)
124 {
125         struct osd_device *d = osd_obj2dev(o);
126         osd_zap_cursor_init_serialized(zc, d->od_os,
127                                        o->oo_dn->dn_object, dirhash);
128 }
129
130 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
131                         uint64_t dirhash)
132 {
133         struct osd_device *d = osd_obj2dev(o);
134         return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
135 }
136
137 static struct dt_it *osd_index_it_init(const struct lu_env *env,
138                                        struct dt_object *dt,
139                                        __u32 unused)
140 {
141         struct osd_thread_info  *info = osd_oti_get(env);
142         struct osd_zap_it       *it;
143         struct osd_object       *obj = osd_dt_obj(dt);
144         struct lu_object        *lo  = &dt->do_lu;
145         int                      rc;
146         ENTRY;
147
148         if (obj->oo_destroyed)
149                 RETURN(ERR_PTR(-ENOENT));
150
151         LASSERT(lu_object_exists(lo));
152         LASSERT(obj->oo_dn);
153         LASSERT(info);
154
155         OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
156         if (it == NULL)
157                 RETURN(ERR_PTR(-ENOMEM));
158
159         rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
160         if (rc != 0) {
161                 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
162                 RETURN(ERR_PTR(rc));
163         }
164
165         it->ozi_obj   = obj;
166         it->ozi_reset = 1;
167         lu_object_get(lo);
168
169         RETURN((struct dt_it *)it);
170 }
171
172 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
173 {
174         struct osd_zap_it       *it     = (struct osd_zap_it *)di;
175         struct osd_object       *obj;
176         ENTRY;
177
178         LASSERT(it);
179         LASSERT(it->ozi_obj);
180
181         obj = it->ozi_obj;
182
183         osd_zap_cursor_fini(it->ozi_zc);
184         osd_object_put(env, obj);
185         OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
186
187         EXIT;
188 }
189
190
191 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
192 {
193         /* PBS: do nothing : ref are incremented at retrive and decreamented
194          *      next/finish. */
195 }
196
197 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
198                                        int len, __u16 type)
199 {
200         const unsigned    align = sizeof(struct luda_type) - 1;
201         struct luda_type *lt;
202
203         /* check if file type is required */
204         if (attr & LUDA_TYPE) {
205                 len = (len + align) & ~align;
206
207                 lt = (void *)ent->lde_name + len;
208                 lt->lt_type = cpu_to_le16(DTTOIF(type));
209                 ent->lde_attrs |= LUDA_TYPE;
210         }
211
212         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
213 }
214
215 int __osd_xattr_load_by_oid(struct osd_device *osd, uint64_t oid, nvlist_t **sa)
216 {
217         sa_handle_t *hdl;
218         dmu_buf_t *db;
219         int rc;
220
221         rc = -dmu_bonus_hold(osd->od_os, oid, osd_obj_tag, &db);
222         if (rc < 0) {
223                 CERROR("%s: can't get bonus, rc = %d\n", osd->od_svname, rc);
224                 return rc;
225         }
226
227         rc = -sa_handle_get_from_db(osd->od_os, db, NULL, SA_HDL_PRIVATE, &hdl);
228         if (rc) {
229                 dmu_buf_rele(db, osd_obj_tag);
230                 return rc;
231         }
232
233         rc = __osd_xattr_load(osd, hdl, sa);
234
235         sa_handle_destroy(hdl);
236
237         return rc;
238 }
239 /**
240  * Get the object's FID from its LMA EA.
241  *
242  * \param[in] env       pointer to the thread context
243  * \param[in] osd       pointer to the OSD device
244  * \param[in] oid       the object's local identifier
245  * \param[out] fid      the buffer to hold the object's FID
246  *
247  * \retval              0 for success
248  * \retval              negative error number on failure
249  */
250 static int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
251                               uint64_t oid, struct lu_fid *fid)
252 {
253         struct objset           *os       = osd->od_os;
254         struct osd_thread_info  *oti      = osd_oti_get(env);
255         struct lustre_mdt_attrs *lma      =
256                         (struct lustre_mdt_attrs *)oti->oti_buf;
257         struct lu_buf            buf;
258         nvlist_t                *sa_xattr = NULL;
259         sa_handle_t             *sa_hdl   = NULL;
260         uchar_t                 *nv_value = NULL;
261         uint64_t                 xattr    = ZFS_NO_OBJECT;
262         int                      size     = 0;
263         int                      rc;
264         ENTRY;
265
266         rc = __osd_xattr_load_by_oid(osd, oid, &sa_xattr);
267         if (rc == -ENOENT)
268                 goto regular;
269
270         if (rc != 0)
271                 GOTO(out, rc);
272
273         rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
274                                        &size);
275         if (rc == -ENOENT)
276                 goto regular;
277
278         if (rc != 0)
279                 GOTO(out, rc);
280
281         if (unlikely(size > sizeof(oti->oti_buf)))
282                 GOTO(out, rc = -ERANGE);
283
284         memcpy(lma, nv_value, size);
285
286         goto found;
287
288 regular:
289         rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
290         if (rc != 0)
291                 GOTO(out, rc);
292
293         rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
294         sa_handle_destroy(sa_hdl);
295         if (rc != 0)
296                 GOTO(out, rc);
297
298         buf.lb_buf = lma;
299         buf.lb_len = sizeof(oti->oti_buf);
300         rc = __osd_xattr_get_large(env, osd, xattr, &buf,
301                                    XATTR_NAME_LMA, &size);
302         if (rc != 0)
303                 GOTO(out, rc);
304
305 found:
306         if (size < sizeof(*lma))
307                 GOTO(out, rc = -EIO);
308
309         lustre_lma_swab(lma);
310         if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
311                      CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
312                 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
313                       "oid = %#llx\n", osd->od_svname,
314                       lma->lma_incompat & ~LMA_INCOMPAT_SUPP, oid);
315                 GOTO(out, rc = -EOPNOTSUPP);
316         } else {
317                 *fid = lma->lma_self_fid;
318                 GOTO(out, rc = 0);
319         }
320
321 out:
322         if (sa_xattr != NULL)
323                 nvlist_free(sa_xattr);
324         return rc;
325 }
326
327 /*
328  * As we don't know FID, we can't use LU object, so this function
329  * partially duplicate osd_xattr_get_internal() which is built around
330  * LU-object and uses it to cache data like regular EA dnode, etc
331  */
332 static int osd_find_parent_by_dnode(const struct lu_env *env,
333                                     struct dt_object *o,
334                                     struct lu_fid *fid, uint64_t *oid)
335 {
336         struct osd_object       *obj = osd_dt_obj(o);
337         struct osd_device       *osd = osd_obj2dev(obj);
338         uint64_t                 dnode = ZFS_NO_OBJECT;
339         int                      rc;
340         ENTRY;
341
342         /* first of all, get parent dnode from own attributes */
343         rc = osd_sa_handle_get(obj);
344         if (rc != 0)
345                 RETURN(rc);
346         rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
347         if (!rc) {
348                 if (oid)
349                         *oid = dnode;
350                 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
351         }
352
353         RETURN(rc);
354 }
355
356 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
357                                struct lu_fid *fid, uint64_t *oid)
358 {
359         struct link_ea_header  *leh;
360         struct link_ea_entry   *lee;
361         struct lu_buf           buf;
362         int                     rc;
363         ENTRY;
364
365         buf.lb_buf = osd_oti_get(env)->oti_buf;
366         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
367
368         rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
369         if (rc == -ERANGE) {
370                 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
371                 if (rc < 0)
372                         RETURN(rc);
373                 LASSERT(rc > 0);
374                 OBD_ALLOC(buf.lb_buf, rc);
375                 if (buf.lb_buf == NULL)
376                         RETURN(-ENOMEM);
377                 buf.lb_len = rc;
378                 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
379         }
380         if (rc < 0)
381                 GOTO(out, rc);
382         if (rc < sizeof(*leh) + sizeof(*lee))
383                 GOTO(out, rc = -EINVAL);
384
385         leh = buf.lb_buf;
386         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
387                 leh->leh_magic = LINK_EA_MAGIC;
388                 leh->leh_reccount = __swab32(leh->leh_reccount);
389                 leh->leh_len = __swab64(leh->leh_len);
390         }
391         if (leh->leh_magic != LINK_EA_MAGIC)
392                 GOTO(out, rc = -EINVAL);
393         if (leh->leh_reccount == 0)
394                 GOTO(out, rc = -ENODATA);
395
396         lee = (struct link_ea_entry *)(leh + 1);
397         fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
398         rc = 0;
399
400 out:
401         if (buf.lb_buf != osd_oti_get(env)->oti_buf)
402                 OBD_FREE(buf.lb_buf, buf.lb_len);
403
404 #if 0
405         /* this block can be enabled for additional verification
406          * it's trying to match FID from LinkEA vs. FID from LMA */
407         if (rc == 0) {
408                 struct lu_fid fid2;
409                 int rc2;
410                 rc2 = osd_find_parent_by_dnode(env, o, &fid2, oid);
411                 if (rc2 == 0)
412                         if (lu_fid_eq(fid, &fid2) == 0)
413                                 CERROR("wrong parent: "DFID" != "DFID"\n",
414                                        PFID(fid), PFID(&fid2));
415         }
416 #endif
417
418         /* no LinkEA is found, let's try to find the fid in parent's LMA */
419         if (unlikely(rc != 0))
420                 rc = osd_find_parent_by_dnode(env, o, fid, oid);
421
422         RETURN(rc);
423 }
424
425 /*
426  * When lookup item under striped directory, we need to locate the master
427  * MDT-object of the striped directory firstly, then the client will send
428  * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
429  * and the item's name. If the system is restored from MDT file level backup,
430  * then before the OI scrub completely built the OI files, the OI mappings of
431  * the master MDT-object and slave MDT-object may be invalid. Usually, it is
432  * not a problem for the master MDT-object. Because when locate the master
433  * MDT-object, we will do name based lookup (for the striped directory itself)
434  * firstly, during such process we can setup the correct OI mapping for the
435  * master MDT-object. But it will be trouble for the slave MDT-object. Because
436  * the client will not trigger name based lookup on the MDT to locate the slave
437  * MDT-object before locating item under the striped directory, then when
438  * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
439  * is invalid and does not know what the right OI mapping is, then the MDT has
440  * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
441  * the OI file, related OI mapping is unknown yet, please try again later. And
442  * then client will re-try the RPC again and again until related OI mapping has
443  * been updated. That is quite inefficient.
444  *
445  * To resolve above trouble, we will handle it as the following two cases:
446  *
447  * 1) The slave MDT-object and the master MDT-object are on different MDTs.
448  *    It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
449  *    is linked under /REMOTE_PARENT_DIR with the name of its FID string.
450  *    We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
451  *    directly. Please check osd_fid_lookup().
452  *
453  * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
454  *    Under such case, during lookup the master MDT-object, we will lookup the
455  *    slave MDT-object via readdir against the master MDT-object, because the
456  *    slave MDT-objects information are stored as sub-directories with the name
457  *    "${FID}:${index}". Then when find the local slave MDT-object, its OI
458  *    mapping will be recorded. Then subsequent osd_fid_lookup() will know
459  *    the correct OI mapping for the slave MDT-object.
460  */
461 static int osd_check_lmv(const struct lu_env *env, struct osd_device *osd,
462                          uint64_t oid, const struct lu_fid *fid)
463 {
464         struct osd_thread_info *info = osd_oti_get(env);
465         struct luz_direntry *zde = &info->oti_zde;
466         zap_attribute_t *za = &info->oti_za;
467         zap_cursor_t *zc = &info->oti_zc;
468         struct lu_fid *tfid = &info->oti_fid;
469         nvlist_t *nvbuf = NULL;
470         struct lmv_mds_md_v1 *lmv = NULL;
471         int size;
472         int rc;
473         ENTRY;
474
475         rc = __osd_xattr_load_by_oid(osd, oid, &nvbuf);
476         if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
477                 RETURN(0);
478
479         if (rc)
480                 RETURN(rc);
481
482         rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMV,
483                                        (uchar_t **)&lmv, &size);
484         if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
485                 GOTO(out_nvbuf, rc = 0);
486
487         if (rc || le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
488                 GOTO(out_nvbuf, rc);
489
490         zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
491         rc = -zap_cursor_retrieve(zc, za);
492         if (rc == -ENOENT) {
493                 zap_cursor_advance(zc);
494         } else if (rc) {
495                 CERROR("%s: fail to init for check LMV "DFID"(%llu): rc = %d\n",
496                        osd_name(osd), PFID(fid), oid, rc);
497                 GOTO(out_zc, rc);
498         }
499
500         while (1) {
501                 rc = -zap_cursor_retrieve(zc, za);
502                 if (rc == -ENOENT)
503                         GOTO(out_zc, rc = 0);
504
505                 if (rc) {
506                         CERROR("%s: fail to locate next for check LMV "
507                                DFID"(%llu): rc = %d\n",
508                                osd_name(osd), PFID(fid), oid, rc);
509                         GOTO(out_zc, rc);
510                 }
511
512                 fid_zero(tfid);
513                 sscanf(za->za_name + 1, SFID, RFID(tfid));
514                 if (fid_is_sane(tfid) && !osd_remote_fid(env, osd, tfid)) {
515                         rc = osd_zap_lookup(osd, oid, NULL, za->za_name,
516                                         za->za_integer_length,
517                                         sizeof(*zde) / za->za_integer_length,
518                                         (void *)zde);
519                         if (rc) {
520                                 CERROR("%s: fail to lookup for check LMV "
521                                        DFID"(%llu): rc = %d\n",
522                                        osd_name(osd), PFID(fid), oid, rc);
523                                 GOTO(out_zc, rc);
524                         }
525
526                         rc = osd_oii_insert(env, osd, tfid,
527                                             zde->lzd_reg.zde_dnode, false);
528                         GOTO(out_zc, rc);
529                 }
530
531                 zap_cursor_advance(zc);
532         }
533
534 out_zc:
535         zap_cursor_fini(zc);
536 out_nvbuf:
537         nvlist_free(nvbuf);
538
539         return rc;
540 }
541
542 static int
543 osd_consistency_check(const struct lu_env *env, struct osd_device *osd,
544                       struct osd_object *obj, const struct lu_fid *fid,
545                       uint64_t oid, bool is_dir)
546 {
547         struct lustre_scrub *scrub = &osd->od_scrub;
548         dnode_t *dn = NULL;
549         uint64_t oid2;
550         int once = 0;
551         bool insert;
552         int rc;
553         ENTRY;
554
555         if (!fid_is_norm(fid) && !fid_is_igif(fid))
556                 RETURN(0);
557
558         /* oid == ZFS_NO_OBJECT must be for lookup ".." case */
559         if (oid == ZFS_NO_OBJECT) {
560                 rc = osd_sa_handle_get(obj);
561                 if (rc)
562                         RETURN(rc);
563
564                 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &oid, 8);
565                 if (rc)
566                         RETURN(rc);
567         }
568
569         if (thread_is_running(&scrub->os_thread)) {
570                 if (scrub->os_pos_current > oid)
571                         RETURN(0);
572         } else if (osd->od_auto_scrub_interval == AS_NEVER) {
573                 RETURN(0);
574         } else {
575                 if (cfs_time_before(cfs_time_current_sec(),
576                                     scrub->os_file.sf_time_last_complete +
577                                     osd->od_auto_scrub_interval))
578                         RETURN(0);
579         }
580
581 again:
582         rc = osd_fid_lookup(env, osd, fid, &oid2);
583         if (rc == -ENOENT) {
584                 insert = true;
585                 if (dn)
586                         goto trigger;
587
588                 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
589                 /* The object has been removed (by race maybe). */
590                 if (rc)
591                         RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
592
593                 goto trigger;
594         } else if (rc || oid == oid2) {
595                 GOTO(out, rc);
596         }
597
598         insert = false;
599
600 trigger:
601         if (thread_is_running(&scrub->os_thread)) {
602                 if (!dn) {
603                         rc = __osd_obj2dnode(osd->od_os, oid, &dn);
604                         /* The object has been removed (by race maybe). */
605                         if (rc)
606                                 RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
607                 }
608
609                 rc = osd_oii_insert(env, osd, fid, oid, insert);
610                 /* There is race condition between osd_oi_lookup and OI scrub.
611                  * The OI scrub finished just after osd_oi_lookup() failure.
612                  * Under such case, it is unnecessary to trigger OI scrub again,
613                  * but try to call osd_oi_lookup() again. */
614                 if (unlikely(rc == -EAGAIN))
615                         goto again;
616
617                 if (is_dir)
618                         rc = osd_check_lmv(env, osd, oid, fid);
619                 else
620                         rc = 0;
621
622                 GOTO(out, rc);
623         }
624
625         if (osd->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
626                 rc = osd_scrub_start(env, osd, SS_AUTO_FULL |
627                                      SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
628                 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
629                        "%s: trigger partial OI scrub for RPC inconsistency "
630                        "checking FID "DFID": rc = %d\n",
631                        osd_name(osd), PFID(fid), rc);
632                 if (!rc)
633                         goto again;
634         }
635
636         GOTO(out, rc);
637
638 out:
639         if (dn)
640                 osd_dnode_rele(dn);
641
642         return rc;
643 }
644
645 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
646                           struct dt_rec *rec, const struct dt_key *key)
647 {
648         struct osd_thread_info *oti = osd_oti_get(env);
649         struct osd_object *obj = osd_dt_obj(dt);
650         struct osd_device *osd = osd_obj2dev(obj);
651         struct lu_fid *fid = (struct lu_fid *)rec;
652         char *name = (char *)key;
653         uint64_t oid = ZFS_NO_OBJECT;
654         int rc;
655         ENTRY;
656
657         if (name[0] == '.') {
658                 if (name[1] == 0) {
659                         const struct lu_fid *f = lu_object_fid(&dt->do_lu);
660                         memcpy(rec, f, sizeof(*f));
661                         RETURN(1);
662                 } else if (name[1] == '.' && name[2] == 0) {
663                         rc = osd_find_parent_fid(env, dt, fid, &oid);
664                         GOTO(out, rc);
665                 }
666         }
667
668         memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
669         rc = osd_zap_lookup(osd, obj->oo_dn->dn_object, obj->oo_dn,
670                             (char *)key, 8, sizeof(oti->oti_zde) / 8,
671                             (void *)&oti->oti_zde);
672         if (rc != 0)
673                 RETURN(rc);
674
675         oid = oti->oti_zde.lzd_reg.zde_dnode;
676         if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
677                 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
678                 GOTO(out, rc = 0);
679         }
680
681         rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode, fid);
682
683         GOTO(out, rc);
684
685 out:
686         if (!rc && !osd_remote_fid(env, osd, fid)) {
687                 rc = osd_consistency_check(env, osd, obj, fid, oid,
688                                 S_ISDIR(DTTOIF(oti->oti_zde.lzd_reg.zde_type)));
689                 /* Only -ENOENT error will affect the lookup result. */
690                 if (rc != -ENOENT)
691                         rc = 0;
692         }
693
694         return rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc);
695 }
696
697 /*
698  * In DNE environment, the object and its name entry may reside on different
699  * MDTs. Under such case, we will create an agent object on the MDT where the
700  * name entry resides. The agent object is empty, and indicates that the real
701  * object for the name entry resides on another MDT. If without agent object,
702  * related name entry will be skipped when perform MDT side file level backup
703  * and restore via ZPL by userspace tool, such as 'tar'.
704  */
705 static int osd_create_agent_object(const struct lu_env *env,
706                                    struct osd_device *osd,
707                                    struct luz_direntry *zde,
708                                    uint64_t parent, dmu_tx_t *tx)
709 {
710         struct osd_thread_info *info = osd_oti_get(env);
711         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
712         struct lu_attr *la = &info->oti_la;
713         nvlist_t *nvbuf = NULL;
714         dnode_t *dn = NULL;
715         sa_handle_t *hdl;
716         int rc = 0;
717         ENTRY;
718
719         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTOBJ))
720                 RETURN(0);
721
722         rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
723         if (rc)
724                 RETURN(rc);
725
726         lustre_lma_init(lma, &zde->lzd_fid, 0, LMAI_AGENT);
727         lustre_lma_swab(lma);
728         rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA, (uchar_t *)lma,
729                                     sizeof(*lma));
730         if (rc)
731                 GOTO(out, rc);
732
733         la->la_valid = LA_TYPE | LA_MODE;
734         la->la_mode = (DTTOIF(zde->lzd_reg.zde_type) & S_IFMT) |
735                         S_IRUGO | S_IWUSR | S_IXUGO;
736
737         if (S_ISDIR(la->la_mode))
738                 rc = __osd_zap_create(env, osd, &dn, tx, la,
739                                 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS), 0);
740         else
741                 rc = __osd_object_create(env, osd, NULL, &zde->lzd_fid,
742                                          &dn, tx, la);
743         if (rc)
744                 GOTO(out, rc);
745
746         zde->lzd_reg.zde_dnode = dn->dn_object;
747         rc = -sa_handle_get(osd->od_os, dn->dn_object, NULL,
748                             SA_HDL_PRIVATE, &hdl);
749         if (!rc) {
750                 rc = __osd_attr_init(env, osd, NULL, hdl, tx,
751                                      la, parent, nvbuf);
752                 sa_handle_destroy(hdl);
753         }
754
755         GOTO(out, rc);
756
757 out:
758         if (dn) {
759                 if (rc)
760                         dmu_object_free(osd->od_os, dn->dn_object, tx);
761                 osd_dnode_rele(dn);
762         }
763
764         if (nvbuf)
765                 nvlist_free(nvbuf);
766
767         return rc;
768 }
769
770 int osd_add_to_remote_parent(const struct lu_env *env,
771                              struct osd_device *osd,
772                              struct osd_object *obj,
773                              struct osd_thandle *oh)
774 {
775         struct osd_thread_info *info = osd_oti_get(env);
776         struct luz_direntry *zde = &info->oti_zde;
777         char *name = info->oti_str;
778         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
779         struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
780         struct lu_buf buf = {
781                 .lb_buf = lma,
782                 .lb_len = sizeof(info->oti_buf),
783         };
784         int size = 0;
785         int rc;
786         ENTRY;
787
788         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
789                 RETURN(0);
790
791         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
792         if (rc) {
793                 CWARN("%s: fail to load LMA for adding "
794                       DFID" to remote parent: rc = %d\n",
795                       osd_name(osd), PFID(fid), rc);
796                 RETURN(rc);
797         }
798
799         lustre_lma_swab(lma);
800         lma->lma_incompat |= LMAI_REMOTE_PARENT;
801         lustre_lma_swab(lma);
802         buf.lb_len = size;
803         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
804                                     LU_XATTR_REPLACE, oh);
805         if (rc) {
806                 CWARN("%s: fail to update LMA for adding "
807                       DFID" to remote parent: rc = %d\n",
808                       osd_name(osd), PFID(fid), rc);
809                 RETURN(rc);
810         }
811
812         osd_fid2str(name, fid, sizeof(info->oti_str));
813         zde->lzd_reg.zde_dnode = obj->oo_dn->dn_object;
814         zde->lzd_reg.zde_type = IFTODT(S_IFDIR);
815         zde->lzd_fid = *fid;
816
817         rc = osd_zap_add(osd, osd->od_remote_parent_dir, NULL,
818                          name, 8, sizeof(*zde) / 8, zde, oh->ot_tx);
819         if (unlikely(rc == -EEXIST))
820                 rc = 0;
821         if (rc)
822                 CWARN("%s: fail to add name entry for "
823                       DFID" to remote parent: rc = %d\n",
824                       osd_name(osd), PFID(fid), rc);
825         else
826                 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
827
828         RETURN(rc);
829 }
830
831 int osd_delete_from_remote_parent(const struct lu_env *env,
832                                   struct osd_device *osd,
833                                   struct osd_object *obj,
834                                   struct osd_thandle *oh, bool destroy)
835 {
836         struct osd_thread_info *info = osd_oti_get(env);
837         char *name = info->oti_str;
838         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
839         struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
840         struct lu_buf buf = {
841                 .lb_buf = lma,
842                 .lb_len = sizeof(info->oti_buf),
843         };
844         int size = 0;
845         int rc;
846         ENTRY;
847
848         osd_fid2str(name, fid, sizeof(info->oti_str));
849         rc = osd_zap_remove(osd, osd->od_remote_parent_dir, NULL,
850                             name, oh->ot_tx);
851         if (unlikely(rc == -ENOENT))
852                 rc = 0;
853         if (rc)
854                 CERROR("%s: fail to remove entry under remote "
855                        "parent for "DFID": rc = %d\n",
856                        osd_name(osd), PFID(fid), rc);
857
858         if (destroy || rc)
859                 RETURN(rc);
860
861         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
862         if (rc) {
863                 CERROR("%s: fail to load LMA for removing "
864                        DFID" from remote parent: rc = %d\n",
865                        osd_name(osd), PFID(fid), rc);
866                 RETURN(rc);
867         }
868
869         lustre_lma_swab(lma);
870         lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
871         lustre_lma_swab(lma);
872         buf.lb_len = size;
873         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
874                                     LU_XATTR_REPLACE, oh);
875         if (rc)
876                 CERROR("%s: fail to update LMA for removing "
877                        DFID" from remote parent: rc = %d\n",
878                        osd_name(osd), PFID(fid), rc);
879         else
880                 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
881
882         RETURN(rc);
883 }
884
885 static int osd_declare_dir_insert(const struct lu_env *env,
886                                   struct dt_object *dt,
887                                   const struct dt_rec *rec,
888                                   const struct dt_key *key,
889                                   struct thandle *th)
890 {
891         struct osd_object       *obj = osd_dt_obj(dt);
892         struct osd_device       *osd = osd_obj2dev(obj);
893         const struct dt_insert_rec *rec1;
894         const struct lu_fid     *fid;
895         struct osd_thandle      *oh;
896         uint64_t                 object;
897         struct osd_idmap_cache *idc;
898         ENTRY;
899
900         rec1 = (struct dt_insert_rec *)rec;
901         fid = rec1->rec_fid;
902         LASSERT(fid != NULL);
903         LASSERT(rec1->rec_type != 0);
904
905         LASSERT(th != NULL);
906         oh = container_of0(th, struct osd_thandle, ot_super);
907
908         idc = osd_idc_find_or_init(env, osd, fid);
909         if (IS_ERR(idc))
910                 RETURN(PTR_ERR(idc));
911
912         if (idc->oic_remote) {
913                 const char *name = (const char *)key;
914
915                 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
916                         /* Prepare agent object for remote entry that will
917                          * be used for operations via ZPL, such as MDT side
918                          * file-level backup and restore. */
919                         dmu_tx_hold_sa_create(oh->ot_tx,
920                                 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
921                         if (S_ISDIR(rec1->rec_type))
922                                 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT,
923                                                 FALSE, NULL);
924                 }
925         }
926
927         /* This is for inserting dot/dotdot for new created dir. */
928         if (obj->oo_dn == NULL)
929                 object = DMU_NEW_OBJECT;
930         else
931                 object = obj->oo_dn->dn_object;
932
933         /* do not specify the key as then DMU is trying to look it up
934          * which is very expensive. usually the layers above lookup
935          * before insertion */
936         osd_tx_hold_zap(oh->ot_tx, object, obj->oo_dn, TRUE, NULL);
937
938         RETURN(0);
939 }
940
941 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
942                           u64 seq)
943 {
944         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
945         struct seq_server_site  *ss = osd_seq_site(osd);
946         int                     rc;
947         ENTRY;
948
949         LASSERT(ss != NULL);
950         LASSERT(ss->ss_server_fld != NULL);
951
952         rc = osd_fld_lookup(env, osd, seq, range);
953         if (rc != 0) {
954                 if (rc != -ENOENT)
955                         CERROR("%s: Can not lookup fld for %#llx\n",
956                                osd_name(osd), seq);
957                 RETURN(0);
958         }
959
960         RETURN(ss->ss_node_id == range->lsr_index);
961 }
962
963 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
964                    const struct lu_fid *fid)
965 {
966         struct seq_server_site  *ss = osd_seq_site(osd);
967         ENTRY;
968
969         /* FID seqs not in FLDB, must be local seq */
970         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
971                 RETURN(0);
972
973         /* If FLD is not being initialized yet, it only happens during the
974          * initialization, likely during mgs initialization, and we assume
975          * this is local FID. */
976         if (ss == NULL || ss->ss_server_fld == NULL)
977                 RETURN(0);
978
979         /* Only check the local FLDB here */
980         if (osd_seq_exists(env, osd, fid_seq(fid)))
981                 RETURN(0);
982
983         RETURN(1);
984 }
985
986 /**
987  *      Inserts (key, value) pair in \a directory object.
988  *
989  *      \param  dt      osd index object
990  *      \param  key     key for index
991  *      \param  rec     record reference
992  *      \param  th      transaction handler
993  *      \param  ignore_quota update should not affect quota
994  *
995  *      \retval  0  success
996  *      \retval -ve failure
997  */
998 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
999                           const struct dt_rec *rec, const struct dt_key *key,
1000                           struct thandle *th, int ignore_quota)
1001 {
1002         struct osd_thread_info *oti = osd_oti_get(env);
1003         struct osd_object   *parent = osd_dt_obj(dt);
1004         struct osd_device   *osd = osd_obj2dev(parent);
1005         struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
1006         const struct lu_fid *fid = rec1->rec_fid;
1007         struct osd_thandle *oh;
1008         struct osd_idmap_cache *idc;
1009         const char *name = (const char *)key;
1010         struct luz_direntry *zde = &oti->oti_zde;
1011         int num = sizeof(*zde) / 8;
1012         int rc;
1013         ENTRY;
1014
1015         LASSERT(parent->oo_dn);
1016
1017         LASSERT(dt_object_exists(dt));
1018         LASSERT(osd_invariant(parent));
1019
1020         LASSERT(th != NULL);
1021         oh = container_of0(th, struct osd_thandle, ot_super);
1022
1023         idc = osd_idc_find(env, osd, fid);
1024         if (unlikely(idc == NULL)) {
1025                 /* this dt_insert() wasn't declared properly, so
1026                  * FID is missing in OI cache. we better do not
1027                  * lookup FID in FLDB/OI and don't risk to deadlock,
1028                  * but in some special cases (lfsck testing, etc)
1029                  * it's much simpler than fixing a caller */
1030                 CERROR("%s: "DFID" wasn't declared for insert\n",
1031                        osd_name(osd), PFID(fid));
1032                 idc = osd_idc_find_or_init(env, osd, fid);
1033                 if (IS_ERR(idc))
1034                         RETURN(PTR_ERR(idc));
1035         }
1036
1037         CLASSERT(sizeof(zde->lzd_reg) == 8);
1038         CLASSERT(sizeof(*zde) % 8 == 0);
1039
1040         memset(&zde->lzd_reg, 0, sizeof(zde->lzd_reg));
1041         zde->lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
1042         zde->lzd_fid = *fid;
1043
1044         if (idc->oic_remote) {
1045                 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
1046                         /* Create agent inode for remote object that will
1047                          * be used for MDT file-level backup and restore. */
1048                         rc = osd_create_agent_object(env, osd, zde,
1049                                         parent->oo_dn->dn_object, oh->ot_tx);
1050                         if (rc) {
1051                                 CWARN("%s: Fail to create agent object for "
1052                                       DFID": rc = %d\n",
1053                                       osd_name(osd), PFID(fid), rc);
1054                                 /* Ignore the failure since the system can go
1055                                  * ahead if we do not care about the MDT side
1056                                  * file-level backup and restore. */
1057                                 rc = 0;
1058                         }
1059                 }
1060         } else {
1061                 if (unlikely(idc->oic_dnode == 0)) {
1062                         /* for a reason OI cache wasn't filled properly */
1063                         CERROR("%s: OIC for "DFID" isn't filled\n",
1064                                osd_name(osd), PFID(fid));
1065                         RETURN(-EINVAL);
1066                 }
1067                 if (name[0] == '.') {
1068                         if (name[1] == 0) {
1069                                 /* do not store ".", instead generate it
1070                                  * during iteration */
1071                                 GOTO(out, rc = 0);
1072                         } else if (name[1] == '.' && name[2] == 0) {
1073                                 uint64_t dnode = idc->oic_dnode;
1074                                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
1075                                         dnode--;
1076
1077                                 /* update parent dnode in the child.
1078                                  * later it will be used to generate ".." */
1079                                 rc = osd_object_sa_update(parent,
1080                                                  SA_ZPL_PARENT(osd),
1081                                                  &dnode, 8, oh);
1082
1083                                 GOTO(out, rc);
1084                         }
1085                 }
1086                 zde->lzd_reg.zde_dnode = idc->oic_dnode;
1087         }
1088
1089         if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR))
1090                 zde->lzd_fid.f_ver = ~0;
1091
1092         /* The logic is not related with IGIF, just re-use the fail_loc value
1093          * to be consistent with ldiskfs case, then share the same test logic */
1094         if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1095                 num = 1;
1096
1097         /* Insert (key,oid) into ZAP */
1098         rc = osd_zap_add(osd, parent->oo_dn->dn_object, parent->oo_dn,
1099                          name, 8, num, (void *)zde, oh->ot_tx);
1100         if (unlikely(rc == -EEXIST &&
1101                      name[0] == '.' && name[1] == '.' && name[2] == 0))
1102                 /* Update (key,oid) in ZAP */
1103                 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object, name, 8,
1104                                  sizeof(*zde) / 8, (void *)zde, oh->ot_tx);
1105
1106 out:
1107
1108         RETURN(rc);
1109 }
1110
1111 static int osd_declare_dir_delete(const struct lu_env *env,
1112                                   struct dt_object *dt,
1113                                   const struct dt_key *key,
1114                                   struct thandle *th)
1115 {
1116         struct osd_object *obj = osd_dt_obj(dt);
1117         dnode_t *zap_dn = obj->oo_dn;
1118         struct osd_thandle *oh;
1119         const char *name = (const char *)key;
1120         ENTRY;
1121
1122         LASSERT(dt_object_exists(dt));
1123         LASSERT(osd_invariant(obj));
1124         LASSERT(zap_dn != NULL);
1125
1126         LASSERT(th != NULL);
1127         oh = container_of0(th, struct osd_thandle, ot_super);
1128
1129         /*
1130          * In Orion . and .. were stored in the directory (not generated upon
1131          * request as now). We preserve them for backward compatibility.
1132          */
1133         if (name[0] == '.') {
1134                 if (name[1] == 0)
1135                         RETURN(0);
1136                 else if (name[1] == '.' && name[2] == 0)
1137                         RETURN(0);
1138         }
1139
1140         /* do not specify the key as then DMU is trying to look it up
1141          * which is very expensive. usually the layers above lookup
1142          * before deletion */
1143         osd_tx_hold_zap(oh->ot_tx, zap_dn->dn_object, zap_dn, FALSE, NULL);
1144
1145         /* For destroying agent object if have. */
1146         dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1147
1148         RETURN(0);
1149 }
1150
1151 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
1152                           const struct dt_key *key, struct thandle *th)
1153 {
1154         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1155         struct osd_object *obj = osd_dt_obj(dt);
1156         struct osd_device *osd = osd_obj2dev(obj);
1157         struct osd_thandle *oh;
1158         dnode_t *zap_dn = obj->oo_dn;
1159         char      *name = (char *)key;
1160         int rc;
1161         ENTRY;
1162
1163         LASSERT(zap_dn);
1164
1165         LASSERT(th != NULL);
1166         oh = container_of0(th, struct osd_thandle, ot_super);
1167
1168         /*
1169          * In Orion . and .. were stored in the directory (not generated upon
1170          * request as now). we preserve them for backward compatibility
1171          */
1172         if (name[0] == '.') {
1173                 if (name[1] == 0) {
1174                         RETURN(0);
1175                 } else if (name[1] == '.' && name[2] == 0) {
1176                         RETURN(0);
1177                 }
1178         }
1179
1180         /* XXX: We have to say that lookup during delete_declare will affect
1181          *      performance, but we have to check whether the name entry (to
1182          *      be deleted) has agent object or not to avoid orphans.
1183          *
1184          *      We will improve that in the future, some possible solutions,
1185          *      for example:
1186          *      1) Some hint from the caller via transaction handle to make
1187          *         the lookup conditionally.
1188          *      2) Enhance the ZFS logic to recognize the OSD lookup result
1189          *         and delete the given entry directly without lookup again
1190          *         internally. LU-10190 */
1191         memset(&zde->lzd_fid, 0, sizeof(zde->lzd_fid));
1192         rc = osd_zap_lookup(osd, zap_dn->dn_object, zap_dn, name, 8, 3, zde);
1193         if (unlikely(rc)) {
1194                 if (rc != -ENOENT)
1195                         CERROR("%s: failed to locate entry  %s: rc = %d\n",
1196                                osd->od_svname, name, rc);
1197                 RETURN(rc);
1198         }
1199
1200         if (unlikely(osd_remote_fid(env, osd, &zde->lzd_fid) > 0)) {
1201                 rc = -dmu_object_free(osd->od_os, zde->lzd_reg.zde_dnode,
1202                                       oh->ot_tx);
1203                 if (rc)
1204                         CERROR("%s: failed to destroy agent object (%llu) "
1205                                "for the entry %s: rc = %d\n", osd->od_svname,
1206                                (__u64)zde->lzd_reg.zde_dnode, name, rc);
1207         }
1208
1209         /* Remove key from the ZAP */
1210         rc = osd_zap_remove(osd, zap_dn->dn_object, zap_dn,
1211                             (char *)key, oh->ot_tx);
1212         if (unlikely(rc))
1213                 CERROR("%s: zap_remove %s failed: rc = %d\n",
1214                        osd->od_svname, name, rc);
1215
1216         RETURN(rc);
1217 }
1218
1219 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
1220                                      struct dt_object *dt,
1221                                      __u32 unused)
1222 {
1223         struct osd_zap_it *it;
1224
1225         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
1226         if (!IS_ERR(it))
1227                 it->ozi_pos = 0;
1228
1229         RETURN((struct dt_it *)it);
1230 }
1231
1232 /**
1233  *  Move Iterator to record specified by \a key
1234  *
1235  *  \param  di      osd iterator
1236  *  \param  key     key for index
1237  *
1238  *  \retval +ve  di points to record with least key not larger than key
1239  *  \retval  0   di points to exact matched key
1240  *  \retval -ve  failure
1241  */
1242 static int osd_dir_it_get(const struct lu_env *env,
1243                           struct dt_it *di, const struct dt_key *key)
1244 {
1245         struct osd_zap_it *it = (struct osd_zap_it *)di;
1246         struct osd_object *obj = it->ozi_obj;
1247         char              *name = (char *)key;
1248         int                rc;
1249         ENTRY;
1250
1251         LASSERT(it);
1252         LASSERT(it->ozi_zc);
1253
1254         /* reset the cursor */
1255         zap_cursor_fini(it->ozi_zc);
1256         osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
1257
1258         /* XXX: implementation of the API is broken at the moment */
1259         LASSERT(((const char *)key)[0] == 0);
1260
1261         if (name[0] == 0) {
1262                 it->ozi_pos = 0;
1263                 RETURN(1);
1264         }
1265
1266         if (name[0] == '.') {
1267                 if (name[1] == 0) {
1268                         it->ozi_pos = 1;
1269                         GOTO(out, rc = 1);
1270                 } else if (name[1] == '.' && name[2] == 0) {
1271                         it->ozi_pos = 2;
1272                         GOTO(out, rc = 1);
1273                 }
1274         }
1275
1276         /* neither . nor .. - some real record */
1277         it->ozi_pos = 3;
1278         rc = +1;
1279
1280 out:
1281         RETURN(rc);
1282 }
1283
1284 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
1285 {
1286         /* PBS: do nothing : ref are incremented at retrive and decreamented
1287          *      next/finish. */
1288 }
1289
1290 /*
1291  * in Orion . and .. were stored in the directory, while ZPL
1292  * and current osd-zfs generate them up on request. so, we
1293  * need to ignore previously stored . and ..
1294  */
1295 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
1296                                         zap_attribute_t *za)
1297 {
1298         int rc, isdot;
1299
1300         do {
1301                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1302
1303                 isdot = 0;
1304                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
1305                         if (za->za_name[1] == 0) {
1306                                 isdot = 1;
1307                         } else if (za->za_name[1] == '.' &&
1308                                    za->za_name[2] == 0) {
1309                                 isdot = 1;
1310                         }
1311                         if (unlikely(isdot))
1312                                 zap_cursor_advance(it->ozi_zc);
1313                 }
1314         } while (unlikely(rc == 0 && isdot));
1315
1316         return rc;
1317 }
1318
1319 /**
1320  * to load a directory entry at a time and stored it in
1321  * iterator's in-memory data structure.
1322  *
1323  * \param di, struct osd_it_ea, iterator's in memory structure
1324  *
1325  * \retval +ve, iterator reached to end
1326  * \retval   0, iterator not reached to end
1327  * \retval -ve, on error
1328  */
1329 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
1330 {
1331         struct osd_zap_it *it = (struct osd_zap_it *)di;
1332         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1333         int                rc;
1334
1335         ENTRY;
1336
1337         /* temp. storage should be enough for any key supported by ZFS */
1338         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
1339
1340         /*
1341          * the first ->next() moves the cursor to .
1342          * the second ->next() moves the cursor to ..
1343          * then we get to the real records and have to verify any exist
1344          */
1345         if (it->ozi_pos <= 2) {
1346                 it->ozi_pos++;
1347                 if (it->ozi_pos <=2)
1348                         RETURN(0);
1349
1350         } else {
1351                 zap_cursor_advance(it->ozi_zc);
1352         }
1353
1354         /*
1355          * According to current API we need to return error if its last entry.
1356          * zap_cursor_advance() does not return any value. So we need to call
1357          * retrieve to check if there is any record.  We should make
1358          * changes to Iterator API to not return status for this API
1359          */
1360         rc = osd_index_retrieve_skip_dots(it, za);
1361
1362         if (rc == -ENOENT) /* end of dir */
1363                 RETURN(+1);
1364
1365         RETURN(rc);
1366 }
1367
1368 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
1369                                      const struct dt_it *di)
1370 {
1371         struct osd_zap_it *it = (struct osd_zap_it *)di;
1372         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1373         int                rc = 0;
1374         ENTRY;
1375
1376         if (it->ozi_pos <= 1) {
1377                 it->ozi_pos = 1;
1378                 RETURN((struct dt_key *)".");
1379         } else if (it->ozi_pos == 2) {
1380                 RETURN((struct dt_key *)"..");
1381         }
1382
1383         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
1384                 RETURN(ERR_PTR(rc));
1385
1386         strcpy(it->ozi_name, za->za_name);
1387
1388         RETURN((struct dt_key *)it->ozi_name);
1389 }
1390
1391 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
1392 {
1393         struct osd_zap_it *it = (struct osd_zap_it *)di;
1394         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1395         int                rc;
1396         ENTRY;
1397
1398         if (it->ozi_pos <= 1) {
1399                 it->ozi_pos = 1;
1400                 RETURN(2);
1401         } else if (it->ozi_pos == 2) {
1402                 RETURN(3);
1403         }
1404
1405         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
1406                 rc = strlen(za->za_name);
1407
1408         RETURN(rc);
1409 }
1410
1411 static int
1412 osd_dirent_update(const struct lu_env *env, struct osd_device *dev,
1413                   uint64_t zap, const char *key, struct luz_direntry *zde)
1414 {
1415         dmu_tx_t *tx;
1416         int rc;
1417         ENTRY;
1418
1419         tx = dmu_tx_create(dev->od_os);
1420         if (!tx)
1421                 RETURN(-ENOMEM);
1422
1423         dmu_tx_hold_zap(tx, zap, TRUE, NULL);
1424         rc = -dmu_tx_assign(tx, TXG_WAIT);
1425         if (!rc)
1426                 rc = -zap_update(dev->od_os, zap, key, 8, sizeof(*zde) / 8,
1427                                  (const void *)zde, tx);
1428         if (rc)
1429                 dmu_tx_abort(tx);
1430         else
1431                 dmu_tx_commit(tx);
1432
1433         RETURN(rc);
1434 }
1435
1436 static int osd_update_entry_for_agent(const struct lu_env *env,
1437                                       struct osd_device *osd,
1438                                       uint64_t zap, const char *name,
1439                                       struct luz_direntry *zde, __u32 attr)
1440 {
1441         dmu_tx_t *tx = NULL;
1442         int rc = 0;
1443         ENTRY;
1444
1445         if (attr & LUDA_VERIFY_DRYRUN)
1446                 GOTO(out, rc = 0);
1447
1448         tx = dmu_tx_create(osd->od_os);
1449         if (!tx)
1450                 GOTO(out, rc = -ENOMEM);
1451
1452         dmu_tx_hold_sa_create(tx, osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
1453         dmu_tx_hold_zap(tx, zap, FALSE, NULL);
1454         rc = -dmu_tx_assign(tx, TXG_WAIT);
1455         if (rc) {
1456                 dmu_tx_abort(tx);
1457                 GOTO(out, rc);
1458         }
1459
1460         rc = osd_create_agent_object(env, osd, zde, zap, tx);
1461         if (!rc)
1462                 rc = -zap_update(osd->od_os, zap, name, 8, sizeof(*zde) / 8,
1463                                  (const void *)zde, tx);
1464         dmu_tx_commit(tx);
1465
1466         GOTO(out, rc);
1467
1468 out:
1469         CDEBUG(D_LFSCK, "%s: Updated (%s) remote entry for "DFID": rc = %d\n",
1470                osd_name(osd), (attr & LUDA_VERIFY_DRYRUN) ? "(ro)" : "(rw)",
1471                PFID(&zde->lzd_fid), rc);
1472         return rc;
1473 }
1474
1475 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
1476                           struct dt_rec *dtrec, __u32 attr)
1477 {
1478         struct osd_zap_it *it = (struct osd_zap_it *)di;
1479         struct lu_dirent *lde = (struct lu_dirent *)dtrec;
1480         struct osd_thread_info *info = osd_oti_get(env);
1481         struct luz_direntry *zde = &info->oti_zde;
1482         zap_attribute_t *za = &info->oti_za;
1483         struct lu_fid *fid = &info->oti_fid;
1484         struct osd_device *osd = osd_obj2dev(it->ozi_obj);
1485         int rc, namelen;
1486         ENTRY;
1487
1488         lde->lde_attrs = 0;
1489         if (it->ozi_pos <= 1) {
1490                 lde->lde_hash = cpu_to_le64(1);
1491                 strcpy(lde->lde_name, ".");
1492                 lde->lde_namelen = cpu_to_le16(1);
1493                 fid_cpu_to_le(&lde->lde_fid,
1494                               lu_object_fid(&it->ozi_obj->oo_dt.do_lu));
1495                 lde->lde_attrs = LUDA_FID;
1496                 /* append lustre attributes */
1497                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
1498                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
1499                 it->ozi_pos = 1;
1500                 RETURN(0);
1501         } else if (it->ozi_pos == 2) {
1502                 lde->lde_hash = cpu_to_le64(2);
1503                 strcpy(lde->lde_name, "..");
1504                 lde->lde_namelen = cpu_to_le16(2);
1505                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, fid, NULL);
1506                 if (!rc) {
1507                         fid_cpu_to_le(&lde->lde_fid, fid);
1508                         lde->lde_attrs = LUDA_FID;
1509                 } else if (rc != -ENOENT) {
1510                         /* ENOENT happens at the root of filesystem, ignore */
1511                         RETURN(rc);
1512                 }
1513
1514                 /* append lustre attributes */
1515                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
1516                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
1517                 RETURN(0);
1518         }
1519
1520         LASSERT(lde);
1521
1522         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1523         if (unlikely(rc))
1524                 RETURN(rc);
1525
1526         lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
1527         namelen = strlen(za->za_name);
1528         if (namelen > NAME_MAX)
1529                 RETURN(-EOVERFLOW);
1530         strcpy(lde->lde_name, za->za_name);
1531         lde->lde_namelen = cpu_to_le16(namelen);
1532
1533         if (za->za_integer_length != 8) {
1534                 CERROR("%s: unsupported direntry format: %d %d\n",
1535                        osd->od_svname,
1536                        za->za_integer_length, (int)za->za_num_integers);
1537                 RETURN(-EIO);
1538         }
1539
1540         rc = osd_zap_lookup(osd, it->ozi_zc->zc_zapobj, it->ozi_obj->oo_dn,
1541                             za->za_name, za->za_integer_length, 3, zde);
1542         if (rc)
1543                 RETURN(rc);
1544
1545         if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid)) {
1546                 lde->lde_attrs = LUDA_FID;
1547                 fid_cpu_to_le(&lde->lde_fid, &zde->lzd_fid);
1548                 if (unlikely(zde->lzd_reg.zde_dnode == ZFS_NO_OBJECT &&
1549                              osd_remote_fid(env, osd, &zde->lzd_fid) > 0 &&
1550                              attr & LUDA_VERIFY)) {
1551                         /* It is mainly used for handling the MDT
1552                          * upgraded from old ZFS based backend. */
1553                         rc = osd_update_entry_for_agent(env, osd,
1554                                         it->ozi_obj->oo_dn->dn_object,
1555                                         za->za_name, zde, attr);
1556                         if (!rc)
1557                                 lde->lde_attrs |= LUDA_REPAIR;
1558                         else
1559                                 lde->lde_attrs |= LUDA_UNKNOWN;
1560                 }
1561
1562                 GOTO(pack_attr, rc = 0);
1563         }
1564
1565         if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
1566                 RETURN(-ENOENT);
1567
1568         rc = osd_get_fid_by_oid(env, osd, zde->lzd_reg.zde_dnode, fid);
1569         if (rc) {
1570                 lde->lde_attrs = LUDA_UNKNOWN;
1571                 GOTO(pack_attr, rc = 0);
1572         }
1573
1574         if (!(attr & LUDA_VERIFY)) {
1575                 fid_cpu_to_le(&lde->lde_fid, fid);
1576                 lde->lde_attrs = LUDA_FID;
1577                 GOTO(pack_attr, rc = 0);
1578         }
1579
1580         if (attr & LUDA_VERIFY_DRYRUN) {
1581                 fid_cpu_to_le(&lde->lde_fid, fid);
1582                 lde->lde_attrs = LUDA_FID | LUDA_REPAIR;
1583                 GOTO(pack_attr, rc = 0);
1584         }
1585
1586         fid_cpu_to_le(&lde->lde_fid, fid);
1587         lde->lde_attrs = LUDA_FID;
1588         zde->lzd_fid = *fid;
1589         rc = osd_dirent_update(env, osd, it->ozi_zc->zc_zapobj,
1590                                za->za_name, zde);
1591         if (rc) {
1592                 lde->lde_attrs |= LUDA_UNKNOWN;
1593                 GOTO(pack_attr, rc = 0);
1594         }
1595
1596         lde->lde_attrs |= LUDA_REPAIR;
1597
1598         GOTO(pack_attr, rc = 0);
1599
1600 pack_attr:
1601         osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1602         lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1603         return rc;
1604 }
1605
1606 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1607                                __u32 attr)
1608 {
1609         struct osd_zap_it   *it = (struct osd_zap_it *)di;
1610         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
1611         size_t               namelen = 0;
1612         int                  rc;
1613         ENTRY;
1614
1615         if (it->ozi_pos <= 1)
1616                 namelen = 1;
1617         else if (it->ozi_pos == 2)
1618                 namelen = 2;
1619
1620         if (namelen > 0) {
1621                 rc = lu_dirent_calc_size(namelen, attr);
1622                 RETURN(rc);
1623         }
1624
1625         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1626         if (unlikely(rc != 0))
1627                 RETURN(rc);
1628
1629         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1630                 CERROR("%s: unsupported direntry format: %d %d\n",
1631                        osd_obj2dev(it->ozi_obj)->od_svname,
1632                        za->za_integer_length, (int)za->za_num_integers);
1633                 RETURN(-EIO);
1634         }
1635
1636         namelen = strlen(za->za_name);
1637         if (namelen > NAME_MAX)
1638                 RETURN(-EOVERFLOW);
1639
1640         rc = lu_dirent_calc_size(namelen, attr);
1641
1642         RETURN(rc);
1643 }
1644
1645 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1646 {
1647         struct osd_zap_it *it = (struct osd_zap_it *)di;
1648         __u64              pos;
1649         ENTRY;
1650
1651         if (it->ozi_pos <= 2)
1652                 pos = it->ozi_pos;
1653         else
1654                 pos = osd_zap_cursor_serialize(it->ozi_zc);
1655
1656         RETURN(pos);
1657 }
1658
1659 /*
1660  * return status :
1661  *  rc == 0 -> end of directory.
1662  *  rc >  0 -> ok, proceed.
1663  *  rc <  0 -> error.  ( EOVERFLOW  can be masked.)
1664  */
1665 static int osd_dir_it_load(const struct lu_env *env,
1666                         const struct dt_it *di, __u64 hash)
1667 {
1668         struct osd_zap_it *it = (struct osd_zap_it *)di;
1669         struct osd_object *obj = it->ozi_obj;
1670         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1671         int                rc;
1672         ENTRY;
1673
1674         /* reset the cursor */
1675         zap_cursor_fini(it->ozi_zc);
1676         osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1677
1678         if (hash <= 2) {
1679                 it->ozi_pos = hash;
1680                 rc = +1;
1681         } else {
1682                 it->ozi_pos = 3;
1683                 /* to return whether the end has been reached */
1684                 rc = osd_index_retrieve_skip_dots(it, za);
1685                 if (rc == 0)
1686                         rc = +1;
1687                 else if (rc == -ENOENT)
1688                         rc = 0;
1689         }
1690
1691         RETURN(rc);
1692 }
1693
1694 struct dt_index_operations osd_dir_ops = {
1695         .dio_lookup         = osd_dir_lookup,
1696         .dio_declare_insert = osd_declare_dir_insert,
1697         .dio_insert         = osd_dir_insert,
1698         .dio_declare_delete = osd_declare_dir_delete,
1699         .dio_delete         = osd_dir_delete,
1700         .dio_it     = {
1701                 .init     = osd_dir_it_init,
1702                 .fini     = osd_index_it_fini,
1703                 .get      = osd_dir_it_get,
1704                 .put      = osd_dir_it_put,
1705                 .next     = osd_dir_it_next,
1706                 .key      = osd_dir_it_key,
1707                 .key_size = osd_dir_it_key_size,
1708                 .rec      = osd_dir_it_rec,
1709                 .rec_size = osd_dir_it_rec_size,
1710                 .store    = osd_dir_it_store,
1711                 .load     = osd_dir_it_load
1712         }
1713 };
1714
1715 /*
1716  * Primitives for index files using binary keys.
1717  */
1718
1719 /* key integer_size is 8 */
1720 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1721                                   const struct dt_key *src)
1722 {
1723         int size;
1724
1725         LASSERT(dst);
1726         LASSERT(src);
1727
1728         /* align keysize to 64bit */
1729         size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1730         size *= sizeof(__u64);
1731
1732         LASSERT(size <= MAXNAMELEN);
1733
1734         if (unlikely(size > o->oo_keysize))
1735                 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1736         memcpy(dst, (const char *)src, o->oo_keysize);
1737
1738         return (size/sizeof(__u64));
1739 }
1740
1741 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1742                         struct dt_rec *rec, const struct dt_key *key)
1743 {
1744         struct osd_object *obj = osd_dt_obj(dt);
1745         struct osd_device *osd = osd_obj2dev(obj);
1746         __u64             *k = osd_oti_get(env)->oti_key64;
1747         int                rc;
1748         ENTRY;
1749
1750         rc = osd_prepare_key_uint64(obj, k, key);
1751
1752         rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1753                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1754                                 (void *)rec);
1755         RETURN(rc == 0 ? 1 : rc);
1756 }
1757
1758 static int osd_declare_index_insert(const struct lu_env *env,
1759                                     struct dt_object *dt,
1760                                     const struct dt_rec *rec,
1761                                     const struct dt_key *key,
1762                                     struct thandle *th)
1763 {
1764         struct osd_object  *obj = osd_dt_obj(dt);
1765         struct osd_thandle *oh;
1766         ENTRY;
1767
1768         LASSERT(th != NULL);
1769         oh = container_of0(th, struct osd_thandle, ot_super);
1770
1771         LASSERT(obj->oo_dn);
1772
1773         /* do not specify the key as then DMU is trying to look it up
1774          * which is very expensive. usually the layers above lookup
1775          * before insertion */
1776         osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1777                         TRUE, NULL);
1778
1779         RETURN(0);
1780 }
1781
1782 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1783                             const struct dt_rec *rec, const struct dt_key *key,
1784                             struct thandle *th, int ignore_quota)
1785 {
1786         struct osd_object  *obj = osd_dt_obj(dt);
1787         struct osd_device  *osd = osd_obj2dev(obj);
1788         struct osd_thandle *oh;
1789         __u64              *k = osd_oti_get(env)->oti_key64;
1790         int                 rc;
1791         ENTRY;
1792
1793         LASSERT(obj->oo_dn);
1794         LASSERT(dt_object_exists(dt));
1795         LASSERT(osd_invariant(obj));
1796         LASSERT(th != NULL);
1797
1798         oh = container_of0(th, struct osd_thandle, ot_super);
1799
1800         rc = osd_prepare_key_uint64(obj, k, key);
1801
1802         /* Insert (key,oid) into ZAP */
1803         rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1804                              k, rc, obj->oo_recusize, obj->oo_recsize,
1805                              (void *)rec, oh->ot_tx);
1806         RETURN(rc);
1807 }
1808
1809 static int osd_declare_index_delete(const struct lu_env *env,
1810                                     struct dt_object *dt,
1811                                     const struct dt_key *key,
1812                                     struct thandle *th)
1813 {
1814         struct osd_object  *obj = osd_dt_obj(dt);
1815         struct osd_thandle *oh;
1816         ENTRY;
1817
1818         LASSERT(dt_object_exists(dt));
1819         LASSERT(osd_invariant(obj));
1820         LASSERT(th != NULL);
1821         LASSERT(obj->oo_dn);
1822
1823         oh = container_of0(th, struct osd_thandle, ot_super);
1824
1825         /* do not specify the key as then DMU is trying to look it up
1826          * which is very expensive. usually the layers above lookup
1827          * before deletion */
1828         osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1829                         FALSE, NULL);
1830
1831         RETURN(0);
1832 }
1833
1834 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1835                             const struct dt_key *key, struct thandle *th)
1836 {
1837         struct osd_object  *obj = osd_dt_obj(dt);
1838         struct osd_device  *osd = osd_obj2dev(obj);
1839         struct osd_thandle *oh;
1840         __u64              *k = osd_oti_get(env)->oti_key64;
1841         int                 rc;
1842         ENTRY;
1843
1844         LASSERT(obj->oo_dn);
1845         LASSERT(th != NULL);
1846         oh = container_of0(th, struct osd_thandle, ot_super);
1847
1848         rc = osd_prepare_key_uint64(obj, k, key);
1849
1850         /* Remove binary key from the ZAP */
1851         rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1852                                 k, rc, oh->ot_tx);
1853         RETURN(rc);
1854 }
1855
1856 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1857                             const struct dt_key *key)
1858 {
1859         struct osd_zap_it *it = (struct osd_zap_it *)di;
1860         struct osd_object *obj = it->ozi_obj;
1861         struct osd_device *osd = osd_obj2dev(obj);
1862         ENTRY;
1863
1864         LASSERT(it);
1865         LASSERT(it->ozi_zc);
1866
1867         /*
1868          * XXX: we need a binary version of zap_cursor_move_to_key()
1869          *      to implement this API */
1870         if (*((const __u64 *)key) != 0)
1871                 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1872                        *((__u64 *)key));
1873
1874         zap_cursor_fini(it->ozi_zc);
1875         zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1876         it->ozi_reset = 1;
1877
1878         RETURN(+1);
1879 }
1880
1881 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1882 {
1883         struct osd_zap_it *it = (struct osd_zap_it *)di;
1884         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1885         int                rc;
1886         ENTRY;
1887
1888         if (it->ozi_reset == 0)
1889                 zap_cursor_advance(it->ozi_zc);
1890         it->ozi_reset = 0;
1891
1892         /*
1893          * According to current API we need to return error if it's last entry.
1894          * zap_cursor_advance() does not return any value. So we need to call
1895          * retrieve to check if there is any record.  We should make
1896          * changes to Iterator API to not return status for this API
1897          */
1898         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1899         if (rc == -ENOENT)
1900                 RETURN(+1);
1901
1902         RETURN((rc));
1903 }
1904
1905 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1906                                        const struct dt_it *di)
1907 {
1908         struct osd_zap_it *it = (struct osd_zap_it *)di;
1909         struct osd_object *obj = it->ozi_obj;
1910         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1911         int                rc = 0;
1912         ENTRY;
1913
1914         it->ozi_reset = 0;
1915         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1916         if (rc)
1917                 RETURN(ERR_PTR(rc));
1918
1919         /* the binary key is stored in the name */
1920         memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1921
1922         RETURN((struct dt_key *)&it->ozi_key);
1923 }
1924
1925 static int osd_index_it_key_size(const struct lu_env *env,
1926                                 const struct dt_it *di)
1927 {
1928         struct osd_zap_it *it = (struct osd_zap_it *)di;
1929         struct osd_object *obj = it->ozi_obj;
1930         RETURN(obj->oo_keysize);
1931 }
1932
1933 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1934                             struct dt_rec *rec, __u32 attr)
1935 {
1936         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1937         struct osd_zap_it *it = (struct osd_zap_it *)di;
1938         struct osd_object *obj = it->ozi_obj;
1939         struct osd_device *osd = osd_obj2dev(obj);
1940         __u64             *k = osd_oti_get(env)->oti_key64;
1941         int                rc;
1942         ENTRY;
1943
1944         it->ozi_reset = 0;
1945         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1946         if (rc)
1947                 RETURN(rc);
1948
1949         rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1950
1951         rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1952                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1953                                 (void *)rec);
1954         RETURN(rc);
1955 }
1956
1957 static __u64 osd_index_it_store(const struct lu_env *env,
1958                                 const struct dt_it *di)
1959 {
1960         struct osd_zap_it *it = (struct osd_zap_it *)di;
1961
1962         it->ozi_reset = 0;
1963         RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1964 }
1965
1966 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1967                              __u64 hash)
1968 {
1969         struct osd_zap_it *it = (struct osd_zap_it *)di;
1970         struct osd_object *obj = it->ozi_obj;
1971         struct osd_device *osd = osd_obj2dev(obj);
1972         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1973         int                rc;
1974         ENTRY;
1975
1976         /* reset the cursor */
1977         zap_cursor_fini(it->ozi_zc);
1978         zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1979                                    obj->oo_dn->dn_object, hash);
1980         it->ozi_reset = 0;
1981
1982         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1983         if (rc == 0)
1984                 RETURN(+1);
1985         else if (rc == -ENOENT)
1986                 RETURN(0);
1987
1988         RETURN(rc);
1989 }
1990
1991 static struct dt_index_operations osd_index_ops = {
1992         .dio_lookup             = osd_index_lookup,
1993         .dio_declare_insert     = osd_declare_index_insert,
1994         .dio_insert             = osd_index_insert,
1995         .dio_declare_delete     = osd_declare_index_delete,
1996         .dio_delete             = osd_index_delete,
1997         .dio_it = {
1998                 .init           = osd_index_it_init,
1999                 .fini           = osd_index_it_fini,
2000                 .get            = osd_index_it_get,
2001                 .put            = osd_index_it_put,
2002                 .next           = osd_index_it_next,
2003                 .key            = osd_index_it_key,
2004                 .key_size       = osd_index_it_key_size,
2005                 .rec            = osd_index_it_rec,
2006                 .store          = osd_index_it_store,
2007                 .load           = osd_index_it_load
2008         }
2009 };
2010
2011 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2012                 const struct dt_index_features *feat)
2013 {
2014         struct osd_object *obj = osd_dt_obj(dt);
2015         int rc = 0;
2016         ENTRY;
2017
2018         down_read(&obj->oo_guard);
2019
2020         /*
2021          * XXX: implement support for fixed-size keys sorted with natural
2022          *      numerical way (not using internal hash value)
2023          */
2024         if (feat->dif_flags & DT_IND_RANGE)
2025                 GOTO(out, rc = -ERANGE);
2026
2027         if (unlikely(feat == &dt_otable_features)) {
2028                 dt->do_index_ops = &osd_otable_ops;
2029                 GOTO(out, rc = 0);
2030         }
2031
2032         LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
2033         if (likely(feat == &dt_directory_features)) {
2034                 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
2035                         dt->do_index_ops = &osd_dir_ops;
2036                 else
2037                         GOTO(out, rc = -ENOTDIR);
2038         } else if (unlikely(feat == &dt_acct_features)) {
2039                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
2040                 dt->do_index_ops = &osd_acct_index_ops;
2041         } else if (dt->do_index_ops == NULL) {
2042                 /* For index file, we don't support variable key & record sizes
2043                  * and the key has to be unique */
2044                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
2045                         GOTO(out, rc = -EINVAL);
2046
2047                 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
2048                         GOTO(out, rc = -E2BIG);
2049                 if (feat->dif_keysize_max != feat->dif_keysize_min)
2050                         GOTO(out, rc = -EINVAL);
2051
2052                 /* As for the record size, it should be a multiple of 8 bytes
2053                  * and smaller than the maximum value length supported by ZAP.
2054                  */
2055                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
2056                         GOTO(out, rc = -E2BIG);
2057                 if (feat->dif_recsize_max != feat->dif_recsize_min)
2058                         GOTO(out, rc = -EINVAL);
2059
2060                 obj->oo_keysize = feat->dif_keysize_max;
2061                 obj->oo_recsize = feat->dif_recsize_max;
2062                 obj->oo_recusize = 1;
2063
2064                 /* ZFS prefers to work with array of 64bits */
2065                 if ((obj->oo_recsize & 7) == 0) {
2066                         obj->oo_recsize >>= 3;
2067                         obj->oo_recusize = 8;
2068                 }
2069                 dt->do_index_ops = &osd_index_ops;
2070         }
2071
2072 out:
2073         up_read(&obj->oo_guard);
2074
2075         RETURN(rc);
2076 }