Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[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)
335 {
336         struct osd_object       *obj = osd_dt_obj(o);
337         struct osd_device       *osd = osd_obj2dev(obj);
338         uint64_t                 dnode = ZFS_NO_OBJECT;
339         int                      rc;
340         ENTRY;
341
342         /* first of all, get parent dnode from own attributes */
343         rc = osd_sa_handle_get(obj);
344         if (rc != 0)
345                 RETURN(rc);
346         rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
347         if (rc == 0)
348                 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
349
350         RETURN(rc);
351 }
352
353 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
354                                struct lu_fid *fid)
355 {
356         struct link_ea_header  *leh;
357         struct link_ea_entry   *lee;
358         struct lu_buf           buf;
359         int                     rc;
360         ENTRY;
361
362         buf.lb_buf = osd_oti_get(env)->oti_buf;
363         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
364
365         rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
366         if (rc == -ERANGE) {
367                 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
368                 if (rc < 0)
369                         RETURN(rc);
370                 LASSERT(rc > 0);
371                 OBD_ALLOC(buf.lb_buf, rc);
372                 if (buf.lb_buf == NULL)
373                         RETURN(-ENOMEM);
374                 buf.lb_len = rc;
375                 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
376         }
377         if (rc < 0)
378                 GOTO(out, rc);
379         if (rc < sizeof(*leh) + sizeof(*lee))
380                 GOTO(out, rc = -EINVAL);
381
382         leh = buf.lb_buf;
383         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
384                 leh->leh_magic = LINK_EA_MAGIC;
385                 leh->leh_reccount = __swab32(leh->leh_reccount);
386                 leh->leh_len = __swab64(leh->leh_len);
387         }
388         if (leh->leh_magic != LINK_EA_MAGIC)
389                 GOTO(out, rc = -EINVAL);
390         if (leh->leh_reccount == 0)
391                 GOTO(out, rc = -ENODATA);
392
393         lee = (struct link_ea_entry *)(leh + 1);
394         fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
395         rc = 0;
396
397 out:
398         if (buf.lb_buf != osd_oti_get(env)->oti_buf)
399                 OBD_FREE(buf.lb_buf, buf.lb_len);
400
401 #if 0
402         /* this block can be enabled for additional verification
403          * it's trying to match FID from LinkEA vs. FID from LMA */
404         if (rc == 0) {
405                 struct lu_fid fid2;
406                 int rc2;
407                 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
408                 if (rc2 == 0)
409                         if (lu_fid_eq(fid, &fid2) == 0)
410                                 CERROR("wrong parent: "DFID" != "DFID"\n",
411                                        PFID(fid), PFID(&fid2));
412         }
413 #endif
414
415         /* no LinkEA is found, let's try to find the fid in parent's LMA */
416         if (unlikely(rc != 0))
417                 rc = osd_find_parent_by_dnode(env, o, fid);
418
419         RETURN(rc);
420 }
421
422 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
423                           struct dt_rec *rec, const struct dt_key *key)
424 {
425         struct osd_thread_info *oti = osd_oti_get(env);
426         struct osd_object  *obj = osd_dt_obj(dt);
427         struct osd_device  *osd = osd_obj2dev(obj);
428         char               *name = (char *)key;
429         int                 rc;
430         ENTRY;
431
432         if (name[0] == '.') {
433                 if (name[1] == 0) {
434                         const struct lu_fid *f = lu_object_fid(&dt->do_lu);
435                         memcpy(rec, f, sizeof(*f));
436                         RETURN(1);
437                 } else if (name[1] == '.' && name[2] == 0) {
438                         rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
439                         RETURN(rc == 0 ? 1 : rc);
440                 }
441         }
442
443         memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
444         rc = osd_zap_lookup(osd, obj->oo_dn->dn_object, obj->oo_dn,
445                             (char *)key, 8, sizeof(oti->oti_zde) / 8,
446                             (void *)&oti->oti_zde);
447         if (rc != 0)
448                 RETURN(rc);
449
450         if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
451                 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
452                 RETURN(1);
453         }
454
455         rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode,
456                                 (struct lu_fid *)rec);
457
458         RETURN(rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc));
459 }
460
461 /*
462  * In DNE environment, the object and its name entry may reside on different
463  * MDTs. Under such case, we will create an agent object on the MDT where the
464  * name entry resides. The agent object is empty, and indicates that the real
465  * object for the name entry resides on another MDT. If without agent object,
466  * related name entry will be skipped when perform MDT side file level backup
467  * and restore via ZPL by userspace tool, such as 'tar'.
468  */
469 static int osd_create_agent_object(const struct lu_env *env,
470                                    struct osd_device *osd,
471                                    struct luz_direntry *zde,
472                                    uint64_t parent, dmu_tx_t *tx)
473 {
474         struct osd_thread_info *info = osd_oti_get(env);
475         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
476         struct lu_attr *la = &info->oti_la;
477         nvlist_t *nvbuf = NULL;
478         dnode_t *dn = NULL;
479         sa_handle_t *hdl;
480         int rc = 0;
481         ENTRY;
482
483         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTOBJ))
484                 RETURN(0);
485
486         rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
487         if (rc)
488                 RETURN(rc);
489
490         lustre_lma_init(lma, &zde->lzd_fid, 0, LMAI_AGENT);
491         lustre_lma_swab(lma);
492         rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA, (uchar_t *)lma,
493                                     sizeof(*lma));
494         if (rc)
495                 GOTO(out, rc);
496
497         la->la_valid = LA_TYPE | LA_MODE;
498         la->la_mode = (DTTOIF(zde->lzd_reg.zde_type) & S_IFMT) |
499                         S_IRUGO | S_IWUSR | S_IXUGO;
500
501         if (S_ISDIR(la->la_mode))
502                 rc = __osd_zap_create(env, osd, &dn, tx, la,
503                                 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS), 0);
504         else
505                 rc = __osd_object_create(env, osd, NULL, &zde->lzd_fid,
506                                          &dn, tx, la);
507         if (rc)
508                 GOTO(out, rc);
509
510         zde->lzd_reg.zde_dnode = dn->dn_object;
511         rc = -sa_handle_get(osd->od_os, dn->dn_object, NULL,
512                             SA_HDL_PRIVATE, &hdl);
513         if (!rc) {
514                 rc = __osd_attr_init(env, osd, NULL, hdl, tx,
515                                      la, parent, nvbuf);
516                 sa_handle_destroy(hdl);
517         }
518
519         GOTO(out, rc);
520
521 out:
522         if (dn) {
523                 if (rc)
524                         dmu_object_free(osd->od_os, dn->dn_object, tx);
525                 osd_dnode_rele(dn);
526         }
527
528         if (nvbuf)
529                 nvlist_free(nvbuf);
530
531         return rc;
532 }
533
534 int osd_add_to_remote_parent(const struct lu_env *env,
535                              struct osd_device *osd,
536                              struct osd_object *obj,
537                              struct osd_thandle *oh)
538 {
539         struct osd_thread_info *info = osd_oti_get(env);
540         struct luz_direntry *zde = &info->oti_zde;
541         char *name = info->oti_str;
542         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
543         struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
544         struct lu_buf buf = {
545                 .lb_buf = lma,
546                 .lb_len = sizeof(info->oti_buf),
547         };
548         int size = 0;
549         int rc;
550         ENTRY;
551
552         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
553                 RETURN(0);
554
555         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
556         if (rc) {
557                 CWARN("%s: fail to load LMA for adding "
558                       DFID" to remote parent: rc = %d\n",
559                       osd_name(osd), PFID(fid), rc);
560                 RETURN(rc);
561         }
562
563         lustre_lma_swab(lma);
564         lma->lma_incompat |= LMAI_REMOTE_PARENT;
565         lustre_lma_swab(lma);
566         buf.lb_len = size;
567         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
568                                     LU_XATTR_REPLACE, oh);
569         if (rc) {
570                 CWARN("%s: fail to update LMA for adding "
571                       DFID" to remote parent: rc = %d\n",
572                       osd_name(osd), PFID(fid), rc);
573                 RETURN(rc);
574         }
575
576         osd_fid2str(name, fid, sizeof(info->oti_str));
577         zde->lzd_reg.zde_dnode = obj->oo_dn->dn_object;
578         zde->lzd_reg.zde_type = IFTODT(S_IFDIR);
579         zde->lzd_fid = *fid;
580
581         rc = osd_zap_add(osd, osd->od_remote_parent_dir, NULL,
582                          name, 8, sizeof(*zde) / 8, zde, oh->ot_tx);
583         if (unlikely(rc == -EEXIST))
584                 rc = 0;
585         if (rc)
586                 CWARN("%s: fail to add name entry for "
587                       DFID" to remote parent: rc = %d\n",
588                       osd_name(osd), PFID(fid), rc);
589         else
590                 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
591
592         RETURN(rc);
593 }
594
595 int osd_delete_from_remote_parent(const struct lu_env *env,
596                                   struct osd_device *osd,
597                                   struct osd_object *obj,
598                                   struct osd_thandle *oh, bool destroy)
599 {
600         struct osd_thread_info *info = osd_oti_get(env);
601         char *name = info->oti_str;
602         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
603         struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
604         struct lu_buf buf = {
605                 .lb_buf = lma,
606                 .lb_len = sizeof(info->oti_buf),
607         };
608         int size = 0;
609         int rc;
610         ENTRY;
611
612         osd_fid2str(name, fid, sizeof(info->oti_str));
613         rc = osd_zap_remove(osd, osd->od_remote_parent_dir, NULL,
614                             name, oh->ot_tx);
615         if (unlikely(rc == -ENOENT))
616                 rc = 0;
617         if (rc)
618                 CERROR("%s: fail to remove entry under remote "
619                        "parent for "DFID": rc = %d\n",
620                        osd_name(osd), PFID(fid), rc);
621
622         if (destroy || rc)
623                 RETURN(rc);
624
625         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
626         if (rc) {
627                 CERROR("%s: fail to load LMA for removing "
628                        DFID" from remote parent: rc = %d\n",
629                        osd_name(osd), PFID(fid), rc);
630                 RETURN(rc);
631         }
632
633         lustre_lma_swab(lma);
634         lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
635         lustre_lma_swab(lma);
636         buf.lb_len = size;
637         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
638                                     LU_XATTR_REPLACE, oh);
639         if (rc)
640                 CERROR("%s: fail to update LMA for removing "
641                        DFID" from remote parent: rc = %d\n",
642                        osd_name(osd), PFID(fid), rc);
643         else
644                 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
645
646         RETURN(rc);
647 }
648
649 static int osd_declare_dir_insert(const struct lu_env *env,
650                                   struct dt_object *dt,
651                                   const struct dt_rec *rec,
652                                   const struct dt_key *key,
653                                   struct thandle *th)
654 {
655         struct osd_object       *obj = osd_dt_obj(dt);
656         struct osd_device       *osd = osd_obj2dev(obj);
657         const struct dt_insert_rec *rec1;
658         const struct lu_fid     *fid;
659         struct osd_thandle      *oh;
660         uint64_t                 object;
661         struct osd_idmap_cache *idc;
662         ENTRY;
663
664         rec1 = (struct dt_insert_rec *)rec;
665         fid = rec1->rec_fid;
666         LASSERT(fid != NULL);
667         LASSERT(rec1->rec_type != 0);
668
669         LASSERT(th != NULL);
670         oh = container_of0(th, struct osd_thandle, ot_super);
671
672         idc = osd_idc_find_or_init(env, osd, fid);
673         if (IS_ERR(idc))
674                 RETURN(PTR_ERR(idc));
675
676         if (idc->oic_remote) {
677                 const char *name = (const char *)key;
678
679                 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
680                         /* Prepare agent object for remote entry that will
681                          * be used for operations via ZPL, such as MDT side
682                          * file-level backup and restore. */
683                         dmu_tx_hold_sa_create(oh->ot_tx,
684                                 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
685                         if (S_ISDIR(rec1->rec_type))
686                                 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT,
687                                                 FALSE, NULL);
688                 }
689         }
690
691         /* This is for inserting dot/dotdot for new created dir. */
692         if (obj->oo_dn == NULL)
693                 object = DMU_NEW_OBJECT;
694         else
695                 object = obj->oo_dn->dn_object;
696
697         /* do not specify the key as then DMU is trying to look it up
698          * which is very expensive. usually the layers above lookup
699          * before insertion */
700         osd_tx_hold_zap(oh->ot_tx, object, obj->oo_dn, TRUE, NULL);
701
702         RETURN(0);
703 }
704
705 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
706                           u64 seq)
707 {
708         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
709         struct seq_server_site  *ss = osd_seq_site(osd);
710         int                     rc;
711         ENTRY;
712
713         LASSERT(ss != NULL);
714         LASSERT(ss->ss_server_fld != NULL);
715
716         rc = osd_fld_lookup(env, osd, seq, range);
717         if (rc != 0) {
718                 if (rc != -ENOENT)
719                         CERROR("%s: Can not lookup fld for %#llx\n",
720                                osd_name(osd), seq);
721                 RETURN(0);
722         }
723
724         RETURN(ss->ss_node_id == range->lsr_index);
725 }
726
727 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
728                    const struct lu_fid *fid)
729 {
730         struct seq_server_site  *ss = osd_seq_site(osd);
731         ENTRY;
732
733         /* FID seqs not in FLDB, must be local seq */
734         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
735                 RETURN(0);
736
737         /* If FLD is not being initialized yet, it only happens during the
738          * initialization, likely during mgs initialization, and we assume
739          * this is local FID. */
740         if (ss == NULL || ss->ss_server_fld == NULL)
741                 RETURN(0);
742
743         /* Only check the local FLDB here */
744         if (osd_seq_exists(env, osd, fid_seq(fid)))
745                 RETURN(0);
746
747         RETURN(1);
748 }
749
750 /**
751  *      Inserts (key, value) pair in \a directory object.
752  *
753  *      \param  dt      osd index object
754  *      \param  key     key for index
755  *      \param  rec     record reference
756  *      \param  th      transaction handler
757  *      \param  ignore_quota update should not affect quota
758  *
759  *      \retval  0  success
760  *      \retval -ve failure
761  */
762 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
763                           const struct dt_rec *rec, const struct dt_key *key,
764                           struct thandle *th, int ignore_quota)
765 {
766         struct osd_thread_info *oti = osd_oti_get(env);
767         struct osd_object   *parent = osd_dt_obj(dt);
768         struct osd_device   *osd = osd_obj2dev(parent);
769         struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
770         const struct lu_fid *fid = rec1->rec_fid;
771         struct osd_thandle *oh;
772         struct osd_idmap_cache *idc;
773         const char *name = (const char *)key;
774         struct luz_direntry *zde = &oti->oti_zde;
775         int num = sizeof(*zde) / 8;
776         int rc;
777         ENTRY;
778
779         LASSERT(parent->oo_dn);
780
781         LASSERT(dt_object_exists(dt));
782         LASSERT(osd_invariant(parent));
783
784         LASSERT(th != NULL);
785         oh = container_of0(th, struct osd_thandle, ot_super);
786
787         idc = osd_idc_find(env, osd, fid);
788         if (unlikely(idc == NULL)) {
789                 /* this dt_insert() wasn't declared properly, so
790                  * FID is missing in OI cache. we better do not
791                  * lookup FID in FLDB/OI and don't risk to deadlock,
792                  * but in some special cases (lfsck testing, etc)
793                  * it's much simpler than fixing a caller */
794                 CERROR("%s: "DFID" wasn't declared for insert\n",
795                        osd_name(osd), PFID(fid));
796                 idc = osd_idc_find_or_init(env, osd, fid);
797                 if (IS_ERR(idc))
798                         RETURN(PTR_ERR(idc));
799         }
800
801         CLASSERT(sizeof(zde->lzd_reg) == 8);
802         CLASSERT(sizeof(*zde) % 8 == 0);
803
804         memset(&zde->lzd_reg, 0, sizeof(zde->lzd_reg));
805         zde->lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
806         zde->lzd_fid = *fid;
807
808         if (idc->oic_remote) {
809                 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
810                         /* Create agent inode for remote object that will
811                          * be used for MDT file-level backup and restore. */
812                         rc = osd_create_agent_object(env, osd, zde,
813                                         parent->oo_dn->dn_object, oh->ot_tx);
814                         if (rc) {
815                                 CWARN("%s: Fail to create agent object for "
816                                       DFID": rc = %d\n",
817                                       osd_name(osd), PFID(fid), rc);
818                                 /* Ignore the failure since the system can go
819                                  * ahead if we do not care about the MDT side
820                                  * file-level backup and restore. */
821                                 rc = 0;
822                         }
823                 }
824         } else {
825                 if (unlikely(idc->oic_dnode == 0)) {
826                         /* for a reason OI cache wasn't filled properly */
827                         CERROR("%s: OIC for "DFID" isn't filled\n",
828                                osd_name(osd), PFID(fid));
829                         RETURN(-EINVAL);
830                 }
831                 if (name[0] == '.') {
832                         if (name[1] == 0) {
833                                 /* do not store ".", instead generate it
834                                  * during iteration */
835                                 GOTO(out, rc = 0);
836                         } else if (name[1] == '.' && name[2] == 0) {
837                                 uint64_t dnode = idc->oic_dnode;
838                                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
839                                         dnode--;
840
841                                 /* update parent dnode in the child.
842                                  * later it will be used to generate ".." */
843                                 rc = osd_object_sa_update(parent,
844                                                  SA_ZPL_PARENT(osd),
845                                                  &dnode, 8, oh);
846
847                                 GOTO(out, rc);
848                         }
849                 }
850                 zde->lzd_reg.zde_dnode = idc->oic_dnode;
851         }
852
853         if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR))
854                 zde->lzd_fid.f_ver = ~0;
855
856         /* The logic is not related with IGIF, just re-use the fail_loc value
857          * to be consistent with ldiskfs case, then share the same test logic */
858         if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
859                 num = 1;
860
861         /* Insert (key,oid) into ZAP */
862         rc = osd_zap_add(osd, parent->oo_dn->dn_object, parent->oo_dn,
863                          name, 8, num, (void *)zde, oh->ot_tx);
864         if (unlikely(rc == -EEXIST &&
865                      name[0] == '.' && name[1] == '.' && name[2] == 0))
866                 /* Update (key,oid) in ZAP */
867                 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object, name, 8,
868                                  sizeof(*zde) / 8, (void *)zde, oh->ot_tx);
869
870 out:
871
872         RETURN(rc);
873 }
874
875 static int osd_declare_dir_delete(const struct lu_env *env,
876                                   struct dt_object *dt,
877                                   const struct dt_key *key,
878                                   struct thandle *th)
879 {
880         struct osd_object *obj = osd_dt_obj(dt);
881         dnode_t *zap_dn = obj->oo_dn;
882         struct osd_thandle *oh;
883         const char *name = (const char *)key;
884         ENTRY;
885
886         LASSERT(dt_object_exists(dt));
887         LASSERT(osd_invariant(obj));
888         LASSERT(zap_dn != NULL);
889
890         LASSERT(th != NULL);
891         oh = container_of0(th, struct osd_thandle, ot_super);
892
893         /*
894          * In Orion . and .. were stored in the directory (not generated upon
895          * request as now). We preserve them for backward compatibility.
896          */
897         if (name[0] == '.') {
898                 if (name[1] == 0)
899                         RETURN(0);
900                 else if (name[1] == '.' && name[2] == 0)
901                         RETURN(0);
902         }
903
904         /* do not specify the key as then DMU is trying to look it up
905          * which is very expensive. usually the layers above lookup
906          * before deletion */
907         osd_tx_hold_zap(oh->ot_tx, zap_dn->dn_object, zap_dn, FALSE, NULL);
908
909         /* For destroying agent object if have. */
910         dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
911
912         RETURN(0);
913 }
914
915 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
916                           const struct dt_key *key, struct thandle *th)
917 {
918         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
919         struct osd_object *obj = osd_dt_obj(dt);
920         struct osd_device *osd = osd_obj2dev(obj);
921         struct osd_thandle *oh;
922         dnode_t *zap_dn = obj->oo_dn;
923         char      *name = (char *)key;
924         int rc;
925         ENTRY;
926
927         LASSERT(zap_dn);
928
929         LASSERT(th != NULL);
930         oh = container_of0(th, struct osd_thandle, ot_super);
931
932         /*
933          * In Orion . and .. were stored in the directory (not generated upon
934          * request as now). we preserve them for backward compatibility
935          */
936         if (name[0] == '.') {
937                 if (name[1] == 0) {
938                         RETURN(0);
939                 } else if (name[1] == '.' && name[2] == 0) {
940                         RETURN(0);
941                 }
942         }
943
944         /* XXX: We have to say that lookup during delete_declare will affect
945          *      performance, but we have to check whether the name entry (to
946          *      be deleted) has agent object or not to avoid orphans.
947          *
948          *      We will improve that in the future, some possible solutions,
949          *      for example:
950          *      1) Some hint from the caller via transaction handle to make
951          *         the lookup conditionally.
952          *      2) Enhance the ZFS logic to recognize the OSD lookup result
953          *         and delete the given entry directly without lookup again
954          *         internally. LU-10190 */
955         memset(&zde->lzd_fid, 0, sizeof(zde->lzd_fid));
956         rc = osd_zap_lookup(osd, zap_dn->dn_object, zap_dn, name, 8, 3, zde);
957         if (unlikely(rc)) {
958                 if (rc != -ENOENT)
959                         CERROR("%s: failed to locate entry  %s: rc = %d\n",
960                                osd->od_svname, name, rc);
961                 RETURN(rc);
962         }
963
964         if (unlikely(osd_remote_fid(env, osd, &zde->lzd_fid) > 0)) {
965                 rc = -dmu_object_free(osd->od_os, zde->lzd_reg.zde_dnode,
966                                       oh->ot_tx);
967                 if (rc)
968                         CERROR("%s: failed to destroy agent object (%llu) "
969                                "for the entry %s: rc = %d\n", osd->od_svname,
970                                (__u64)zde->lzd_reg.zde_dnode, name, rc);
971         }
972
973         /* Remove key from the ZAP */
974         rc = osd_zap_remove(osd, zap_dn->dn_object, zap_dn,
975                             (char *)key, oh->ot_tx);
976         if (unlikely(rc))
977                 CERROR("%s: zap_remove %s failed: rc = %d\n",
978                        osd->od_svname, name, rc);
979
980         RETURN(rc);
981 }
982
983 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
984                                      struct dt_object *dt,
985                                      __u32 unused)
986 {
987         struct osd_zap_it *it;
988
989         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
990         if (!IS_ERR(it))
991                 it->ozi_pos = 0;
992
993         RETURN((struct dt_it *)it);
994 }
995
996 /**
997  *  Move Iterator to record specified by \a key
998  *
999  *  \param  di      osd iterator
1000  *  \param  key     key for index
1001  *
1002  *  \retval +ve  di points to record with least key not larger than key
1003  *  \retval  0   di points to exact matched key
1004  *  \retval -ve  failure
1005  */
1006 static int osd_dir_it_get(const struct lu_env *env,
1007                           struct dt_it *di, const struct dt_key *key)
1008 {
1009         struct osd_zap_it *it = (struct osd_zap_it *)di;
1010         struct osd_object *obj = it->ozi_obj;
1011         char              *name = (char *)key;
1012         int                rc;
1013         ENTRY;
1014
1015         LASSERT(it);
1016         LASSERT(it->ozi_zc);
1017
1018         /* reset the cursor */
1019         zap_cursor_fini(it->ozi_zc);
1020         osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
1021
1022         /* XXX: implementation of the API is broken at the moment */
1023         LASSERT(((const char *)key)[0] == 0);
1024
1025         if (name[0] == 0) {
1026                 it->ozi_pos = 0;
1027                 RETURN(1);
1028         }
1029
1030         if (name[0] == '.') {
1031                 if (name[1] == 0) {
1032                         it->ozi_pos = 1;
1033                         GOTO(out, rc = 1);
1034                 } else if (name[1] == '.' && name[2] == 0) {
1035                         it->ozi_pos = 2;
1036                         GOTO(out, rc = 1);
1037                 }
1038         }
1039
1040         /* neither . nor .. - some real record */
1041         it->ozi_pos = 3;
1042         rc = +1;
1043
1044 out:
1045         RETURN(rc);
1046 }
1047
1048 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
1049 {
1050         /* PBS: do nothing : ref are incremented at retrive and decreamented
1051          *      next/finish. */
1052 }
1053
1054 /*
1055  * in Orion . and .. were stored in the directory, while ZPL
1056  * and current osd-zfs generate them up on request. so, we
1057  * need to ignore previously stored . and ..
1058  */
1059 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
1060                                         zap_attribute_t *za)
1061 {
1062         int rc, isdot;
1063
1064         do {
1065                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1066
1067                 isdot = 0;
1068                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
1069                         if (za->za_name[1] == 0) {
1070                                 isdot = 1;
1071                         } else if (za->za_name[1] == '.' &&
1072                                    za->za_name[2] == 0) {
1073                                 isdot = 1;
1074                         }
1075                         if (unlikely(isdot))
1076                                 zap_cursor_advance(it->ozi_zc);
1077                 }
1078         } while (unlikely(rc == 0 && isdot));
1079
1080         return rc;
1081 }
1082
1083 /**
1084  * to load a directory entry at a time and stored it in
1085  * iterator's in-memory data structure.
1086  *
1087  * \param di, struct osd_it_ea, iterator's in memory structure
1088  *
1089  * \retval +ve, iterator reached to end
1090  * \retval   0, iterator not reached to end
1091  * \retval -ve, on error
1092  */
1093 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
1094 {
1095         struct osd_zap_it *it = (struct osd_zap_it *)di;
1096         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1097         int                rc;
1098
1099         ENTRY;
1100
1101         /* temp. storage should be enough for any key supported by ZFS */
1102         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
1103
1104         /*
1105          * the first ->next() moves the cursor to .
1106          * the second ->next() moves the cursor to ..
1107          * then we get to the real records and have to verify any exist
1108          */
1109         if (it->ozi_pos <= 2) {
1110                 it->ozi_pos++;
1111                 if (it->ozi_pos <=2)
1112                         RETURN(0);
1113
1114         } else {
1115                 zap_cursor_advance(it->ozi_zc);
1116         }
1117
1118         /*
1119          * According to current API we need to return error if its last entry.
1120          * zap_cursor_advance() does not return any value. So we need to call
1121          * retrieve to check if there is any record.  We should make
1122          * changes to Iterator API to not return status for this API
1123          */
1124         rc = osd_index_retrieve_skip_dots(it, za);
1125
1126         if (rc == -ENOENT) /* end of dir */
1127                 RETURN(+1);
1128
1129         RETURN(rc);
1130 }
1131
1132 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
1133                                      const struct dt_it *di)
1134 {
1135         struct osd_zap_it *it = (struct osd_zap_it *)di;
1136         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1137         int                rc = 0;
1138         ENTRY;
1139
1140         if (it->ozi_pos <= 1) {
1141                 it->ozi_pos = 1;
1142                 RETURN((struct dt_key *)".");
1143         } else if (it->ozi_pos == 2) {
1144                 RETURN((struct dt_key *)"..");
1145         }
1146
1147         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
1148                 RETURN(ERR_PTR(rc));
1149
1150         strcpy(it->ozi_name, za->za_name);
1151
1152         RETURN((struct dt_key *)it->ozi_name);
1153 }
1154
1155 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
1156 {
1157         struct osd_zap_it *it = (struct osd_zap_it *)di;
1158         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1159         int                rc;
1160         ENTRY;
1161
1162         if (it->ozi_pos <= 1) {
1163                 it->ozi_pos = 1;
1164                 RETURN(2);
1165         } else if (it->ozi_pos == 2) {
1166                 RETURN(3);
1167         }
1168
1169         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
1170                 rc = strlen(za->za_name);
1171
1172         RETURN(rc);
1173 }
1174
1175 static int
1176 osd_dirent_update(const struct lu_env *env, struct osd_device *dev,
1177                   uint64_t zap, const char *key, struct luz_direntry *zde)
1178 {
1179         dmu_tx_t *tx;
1180         int rc;
1181         ENTRY;
1182
1183         tx = dmu_tx_create(dev->od_os);
1184         if (!tx)
1185                 RETURN(-ENOMEM);
1186
1187         dmu_tx_hold_zap(tx, zap, TRUE, NULL);
1188         rc = -dmu_tx_assign(tx, TXG_WAIT);
1189         if (!rc)
1190                 rc = -zap_update(dev->od_os, zap, key, 8, sizeof(*zde) / 8,
1191                                  (const void *)zde, tx);
1192         if (rc)
1193                 dmu_tx_abort(tx);
1194         else
1195                 dmu_tx_commit(tx);
1196
1197         RETURN(rc);
1198 }
1199
1200 static int osd_update_entry_for_agent(const struct lu_env *env,
1201                                       struct osd_device *osd,
1202                                       uint64_t zap, const char *name,
1203                                       struct luz_direntry *zde, __u32 attr)
1204 {
1205         dmu_tx_t *tx = NULL;
1206         int rc = 0;
1207         ENTRY;
1208
1209         if (attr & LUDA_VERIFY_DRYRUN)
1210                 GOTO(out, rc = 0);
1211
1212         tx = dmu_tx_create(osd->od_os);
1213         if (!tx)
1214                 GOTO(out, rc = -ENOMEM);
1215
1216         dmu_tx_hold_sa_create(tx, osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
1217         dmu_tx_hold_zap(tx, zap, FALSE, NULL);
1218         rc = -dmu_tx_assign(tx, TXG_WAIT);
1219         if (rc) {
1220                 dmu_tx_abort(tx);
1221                 GOTO(out, rc);
1222         }
1223
1224         rc = osd_create_agent_object(env, osd, zde, zap, tx);
1225         if (!rc)
1226                 rc = -zap_update(osd->od_os, zap, name, 8, sizeof(*zde) / 8,
1227                                  (const void *)zde, tx);
1228         dmu_tx_commit(tx);
1229
1230         GOTO(out, rc);
1231
1232 out:
1233         CDEBUG(D_LFSCK, "%s: Updated (%s) remote entry for "DFID": rc = %d\n",
1234                osd_name(osd), (attr & LUDA_VERIFY_DRYRUN) ? "(ro)" : "(rw)",
1235                PFID(&zde->lzd_fid), rc);
1236         return rc;
1237 }
1238
1239 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
1240                           struct dt_rec *dtrec, __u32 attr)
1241 {
1242         struct osd_zap_it *it = (struct osd_zap_it *)di;
1243         struct lu_dirent *lde = (struct lu_dirent *)dtrec;
1244         struct osd_thread_info *info = osd_oti_get(env);
1245         struct luz_direntry *zde = &info->oti_zde;
1246         zap_attribute_t *za = &info->oti_za;
1247         struct lu_fid *fid = &info->oti_fid;
1248         struct osd_device *osd = osd_obj2dev(it->ozi_obj);
1249         int rc, namelen;
1250         ENTRY;
1251
1252         lde->lde_attrs = 0;
1253         if (it->ozi_pos <= 1) {
1254                 lde->lde_hash = cpu_to_le64(1);
1255                 strcpy(lde->lde_name, ".");
1256                 lde->lde_namelen = cpu_to_le16(1);
1257                 fid_cpu_to_le(&lde->lde_fid,
1258                               lu_object_fid(&it->ozi_obj->oo_dt.do_lu));
1259                 lde->lde_attrs = LUDA_FID;
1260                 /* append lustre attributes */
1261                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
1262                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
1263                 it->ozi_pos = 1;
1264                 RETURN(0);
1265         } else if (it->ozi_pos == 2) {
1266                 lde->lde_hash = cpu_to_le64(2);
1267                 strcpy(lde->lde_name, "..");
1268                 lde->lde_namelen = cpu_to_le16(2);
1269                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, fid);
1270                 if (!rc) {
1271                         fid_cpu_to_le(&lde->lde_fid, fid);
1272                         lde->lde_attrs = LUDA_FID;
1273                 } else if (rc != -ENOENT) {
1274                         /* ENOENT happens at the root of filesystem, ignore */
1275                         RETURN(rc);
1276                 }
1277
1278                 /* append lustre attributes */
1279                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
1280                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
1281                 RETURN(0);
1282         }
1283
1284         LASSERT(lde);
1285
1286         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1287         if (unlikely(rc))
1288                 RETURN(rc);
1289
1290         lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
1291         namelen = strlen(za->za_name);
1292         if (namelen > NAME_MAX)
1293                 RETURN(-EOVERFLOW);
1294         strcpy(lde->lde_name, za->za_name);
1295         lde->lde_namelen = cpu_to_le16(namelen);
1296
1297         if (za->za_integer_length != 8) {
1298                 CERROR("%s: unsupported direntry format: %d %d\n",
1299                        osd->od_svname,
1300                        za->za_integer_length, (int)za->za_num_integers);
1301                 RETURN(-EIO);
1302         }
1303
1304         rc = osd_zap_lookup(osd, it->ozi_zc->zc_zapobj, it->ozi_obj->oo_dn,
1305                             za->za_name, za->za_integer_length, 3, zde);
1306         if (rc)
1307                 RETURN(rc);
1308
1309         if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid)) {
1310                 lde->lde_attrs = LUDA_FID;
1311                 fid_cpu_to_le(&lde->lde_fid, &zde->lzd_fid);
1312                 if (unlikely(zde->lzd_reg.zde_dnode == ZFS_NO_OBJECT &&
1313                              osd_remote_fid(env, osd, &zde->lzd_fid) > 0 &&
1314                              attr & LUDA_VERIFY)) {
1315                         /* It is mainly used for handling the MDT
1316                          * upgraded from old ZFS based backend. */
1317                         rc = osd_update_entry_for_agent(env, osd,
1318                                         it->ozi_obj->oo_dn->dn_object,
1319                                         za->za_name, zde, attr);
1320                         if (!rc)
1321                                 lde->lde_attrs |= LUDA_REPAIR;
1322                         else
1323                                 lde->lde_attrs |= LUDA_UNKNOWN;
1324                 }
1325
1326                 GOTO(pack_attr, rc = 0);
1327         }
1328
1329         if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
1330                 RETURN(-ENOENT);
1331
1332         rc = osd_get_fid_by_oid(env, osd, zde->lzd_reg.zde_dnode, fid);
1333         if (rc) {
1334                 lde->lde_attrs = LUDA_UNKNOWN;
1335                 GOTO(pack_attr, rc = 0);
1336         }
1337
1338         if (!(attr & LUDA_VERIFY)) {
1339                 fid_cpu_to_le(&lde->lde_fid, fid);
1340                 lde->lde_attrs = LUDA_FID;
1341                 GOTO(pack_attr, rc = 0);
1342         }
1343
1344         if (attr & LUDA_VERIFY_DRYRUN) {
1345                 fid_cpu_to_le(&lde->lde_fid, fid);
1346                 lde->lde_attrs = LUDA_FID | LUDA_REPAIR;
1347                 GOTO(pack_attr, rc = 0);
1348         }
1349
1350         fid_cpu_to_le(&lde->lde_fid, fid);
1351         lde->lde_attrs = LUDA_FID;
1352         zde->lzd_fid = *fid;
1353         rc = osd_dirent_update(env, osd, it->ozi_zc->zc_zapobj,
1354                                za->za_name, zde);
1355         if (rc) {
1356                 lde->lde_attrs |= LUDA_UNKNOWN;
1357                 GOTO(pack_attr, rc = 0);
1358         }
1359
1360         lde->lde_attrs |= LUDA_REPAIR;
1361
1362         GOTO(pack_attr, rc = 0);
1363
1364 pack_attr:
1365         osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1366         lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1367         return rc;
1368 }
1369
1370 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1371                                __u32 attr)
1372 {
1373         struct osd_zap_it   *it = (struct osd_zap_it *)di;
1374         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
1375         size_t               namelen = 0;
1376         int                  rc;
1377         ENTRY;
1378
1379         if (it->ozi_pos <= 1)
1380                 namelen = 1;
1381         else if (it->ozi_pos == 2)
1382                 namelen = 2;
1383
1384         if (namelen > 0) {
1385                 rc = lu_dirent_calc_size(namelen, attr);
1386                 RETURN(rc);
1387         }
1388
1389         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1390         if (unlikely(rc != 0))
1391                 RETURN(rc);
1392
1393         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1394                 CERROR("%s: unsupported direntry format: %d %d\n",
1395                        osd_obj2dev(it->ozi_obj)->od_svname,
1396                        za->za_integer_length, (int)za->za_num_integers);
1397                 RETURN(-EIO);
1398         }
1399
1400         namelen = strlen(za->za_name);
1401         if (namelen > NAME_MAX)
1402                 RETURN(-EOVERFLOW);
1403
1404         rc = lu_dirent_calc_size(namelen, attr);
1405
1406         RETURN(rc);
1407 }
1408
1409 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1410 {
1411         struct osd_zap_it *it = (struct osd_zap_it *)di;
1412         __u64              pos;
1413         ENTRY;
1414
1415         if (it->ozi_pos <= 2)
1416                 pos = it->ozi_pos;
1417         else
1418                 pos = osd_zap_cursor_serialize(it->ozi_zc);
1419
1420         RETURN(pos);
1421 }
1422
1423 /*
1424  * return status :
1425  *  rc == 0 -> end of directory.
1426  *  rc >  0 -> ok, proceed.
1427  *  rc <  0 -> error.  ( EOVERFLOW  can be masked.)
1428  */
1429 static int osd_dir_it_load(const struct lu_env *env,
1430                         const struct dt_it *di, __u64 hash)
1431 {
1432         struct osd_zap_it *it = (struct osd_zap_it *)di;
1433         struct osd_object *obj = it->ozi_obj;
1434         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1435         int                rc;
1436         ENTRY;
1437
1438         /* reset the cursor */
1439         zap_cursor_fini(it->ozi_zc);
1440         osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1441
1442         if (hash <= 2) {
1443                 it->ozi_pos = hash;
1444                 rc = +1;
1445         } else {
1446                 it->ozi_pos = 3;
1447                 /* to return whether the end has been reached */
1448                 rc = osd_index_retrieve_skip_dots(it, za);
1449                 if (rc == 0)
1450                         rc = +1;
1451                 else if (rc == -ENOENT)
1452                         rc = 0;
1453         }
1454
1455         RETURN(rc);
1456 }
1457
1458 struct dt_index_operations osd_dir_ops = {
1459         .dio_lookup         = osd_dir_lookup,
1460         .dio_declare_insert = osd_declare_dir_insert,
1461         .dio_insert         = osd_dir_insert,
1462         .dio_declare_delete = osd_declare_dir_delete,
1463         .dio_delete         = osd_dir_delete,
1464         .dio_it     = {
1465                 .init     = osd_dir_it_init,
1466                 .fini     = osd_index_it_fini,
1467                 .get      = osd_dir_it_get,
1468                 .put      = osd_dir_it_put,
1469                 .next     = osd_dir_it_next,
1470                 .key      = osd_dir_it_key,
1471                 .key_size = osd_dir_it_key_size,
1472                 .rec      = osd_dir_it_rec,
1473                 .rec_size = osd_dir_it_rec_size,
1474                 .store    = osd_dir_it_store,
1475                 .load     = osd_dir_it_load
1476         }
1477 };
1478
1479 /*
1480  * Primitives for index files using binary keys.
1481  */
1482
1483 /* key integer_size is 8 */
1484 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1485                                   const struct dt_key *src)
1486 {
1487         int size;
1488
1489         LASSERT(dst);
1490         LASSERT(src);
1491
1492         /* align keysize to 64bit */
1493         size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1494         size *= sizeof(__u64);
1495
1496         LASSERT(size <= MAXNAMELEN);
1497
1498         if (unlikely(size > o->oo_keysize))
1499                 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1500         memcpy(dst, (const char *)src, o->oo_keysize);
1501
1502         return (size/sizeof(__u64));
1503 }
1504
1505 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1506                         struct dt_rec *rec, const struct dt_key *key)
1507 {
1508         struct osd_object *obj = osd_dt_obj(dt);
1509         struct osd_device *osd = osd_obj2dev(obj);
1510         __u64             *k = osd_oti_get(env)->oti_key64;
1511         int                rc;
1512         ENTRY;
1513
1514         rc = osd_prepare_key_uint64(obj, k, key);
1515
1516         rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1517                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1518                                 (void *)rec);
1519         RETURN(rc == 0 ? 1 : rc);
1520 }
1521
1522 static int osd_declare_index_insert(const struct lu_env *env,
1523                                     struct dt_object *dt,
1524                                     const struct dt_rec *rec,
1525                                     const struct dt_key *key,
1526                                     struct thandle *th)
1527 {
1528         struct osd_object  *obj = osd_dt_obj(dt);
1529         struct osd_thandle *oh;
1530         ENTRY;
1531
1532         LASSERT(th != NULL);
1533         oh = container_of0(th, struct osd_thandle, ot_super);
1534
1535         LASSERT(obj->oo_dn);
1536
1537         /* do not specify the key as then DMU is trying to look it up
1538          * which is very expensive. usually the layers above lookup
1539          * before insertion */
1540         osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1541                         TRUE, NULL);
1542
1543         RETURN(0);
1544 }
1545
1546 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1547                             const struct dt_rec *rec, const struct dt_key *key,
1548                             struct thandle *th, int ignore_quota)
1549 {
1550         struct osd_object  *obj = osd_dt_obj(dt);
1551         struct osd_device  *osd = osd_obj2dev(obj);
1552         struct osd_thandle *oh;
1553         __u64              *k = osd_oti_get(env)->oti_key64;
1554         int                 rc;
1555         ENTRY;
1556
1557         LASSERT(obj->oo_dn);
1558         LASSERT(dt_object_exists(dt));
1559         LASSERT(osd_invariant(obj));
1560         LASSERT(th != NULL);
1561
1562         oh = container_of0(th, struct osd_thandle, ot_super);
1563
1564         rc = osd_prepare_key_uint64(obj, k, key);
1565
1566         /* Insert (key,oid) into ZAP */
1567         rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1568                              k, rc, obj->oo_recusize, obj->oo_recsize,
1569                              (void *)rec, oh->ot_tx);
1570         RETURN(rc);
1571 }
1572
1573 static int osd_declare_index_delete(const struct lu_env *env,
1574                                     struct dt_object *dt,
1575                                     const struct dt_key *key,
1576                                     struct thandle *th)
1577 {
1578         struct osd_object  *obj = osd_dt_obj(dt);
1579         struct osd_thandle *oh;
1580         ENTRY;
1581
1582         LASSERT(dt_object_exists(dt));
1583         LASSERT(osd_invariant(obj));
1584         LASSERT(th != NULL);
1585         LASSERT(obj->oo_dn);
1586
1587         oh = container_of0(th, struct osd_thandle, ot_super);
1588
1589         /* do not specify the key as then DMU is trying to look it up
1590          * which is very expensive. usually the layers above lookup
1591          * before deletion */
1592         osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1593                         FALSE, NULL);
1594
1595         RETURN(0);
1596 }
1597
1598 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1599                             const struct dt_key *key, struct thandle *th)
1600 {
1601         struct osd_object  *obj = osd_dt_obj(dt);
1602         struct osd_device  *osd = osd_obj2dev(obj);
1603         struct osd_thandle *oh;
1604         __u64              *k = osd_oti_get(env)->oti_key64;
1605         int                 rc;
1606         ENTRY;
1607
1608         LASSERT(obj->oo_dn);
1609         LASSERT(th != NULL);
1610         oh = container_of0(th, struct osd_thandle, ot_super);
1611
1612         rc = osd_prepare_key_uint64(obj, k, key);
1613
1614         /* Remove binary key from the ZAP */
1615         rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1616                                 k, rc, oh->ot_tx);
1617         RETURN(rc);
1618 }
1619
1620 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1621                             const struct dt_key *key)
1622 {
1623         struct osd_zap_it *it = (struct osd_zap_it *)di;
1624         struct osd_object *obj = it->ozi_obj;
1625         struct osd_device *osd = osd_obj2dev(obj);
1626         ENTRY;
1627
1628         LASSERT(it);
1629         LASSERT(it->ozi_zc);
1630
1631         /*
1632          * XXX: we need a binary version of zap_cursor_move_to_key()
1633          *      to implement this API */
1634         if (*((const __u64 *)key) != 0)
1635                 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1636                        *((__u64 *)key));
1637
1638         zap_cursor_fini(it->ozi_zc);
1639         zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1640         it->ozi_reset = 1;
1641
1642         RETURN(+1);
1643 }
1644
1645 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1646 {
1647         struct osd_zap_it *it = (struct osd_zap_it *)di;
1648         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1649         int                rc;
1650         ENTRY;
1651
1652         if (it->ozi_reset == 0)
1653                 zap_cursor_advance(it->ozi_zc);
1654         it->ozi_reset = 0;
1655
1656         /*
1657          * According to current API we need to return error if it's last entry.
1658          * zap_cursor_advance() does not return any value. So we need to call
1659          * retrieve to check if there is any record.  We should make
1660          * changes to Iterator API to not return status for this API
1661          */
1662         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1663         if (rc == -ENOENT)
1664                 RETURN(+1);
1665
1666         RETURN((rc));
1667 }
1668
1669 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1670                                        const struct dt_it *di)
1671 {
1672         struct osd_zap_it *it = (struct osd_zap_it *)di;
1673         struct osd_object *obj = it->ozi_obj;
1674         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1675         int                rc = 0;
1676         ENTRY;
1677
1678         it->ozi_reset = 0;
1679         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1680         if (rc)
1681                 RETURN(ERR_PTR(rc));
1682
1683         /* the binary key is stored in the name */
1684         memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1685
1686         RETURN((struct dt_key *)&it->ozi_key);
1687 }
1688
1689 static int osd_index_it_key_size(const struct lu_env *env,
1690                                 const struct dt_it *di)
1691 {
1692         struct osd_zap_it *it = (struct osd_zap_it *)di;
1693         struct osd_object *obj = it->ozi_obj;
1694         RETURN(obj->oo_keysize);
1695 }
1696
1697 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1698                             struct dt_rec *rec, __u32 attr)
1699 {
1700         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1701         struct osd_zap_it *it = (struct osd_zap_it *)di;
1702         struct osd_object *obj = it->ozi_obj;
1703         struct osd_device *osd = osd_obj2dev(obj);
1704         __u64             *k = osd_oti_get(env)->oti_key64;
1705         int                rc;
1706         ENTRY;
1707
1708         it->ozi_reset = 0;
1709         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1710         if (rc)
1711                 RETURN(rc);
1712
1713         rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1714
1715         rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1716                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1717                                 (void *)rec);
1718         RETURN(rc);
1719 }
1720
1721 static __u64 osd_index_it_store(const struct lu_env *env,
1722                                 const struct dt_it *di)
1723 {
1724         struct osd_zap_it *it = (struct osd_zap_it *)di;
1725
1726         it->ozi_reset = 0;
1727         RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1728 }
1729
1730 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1731                              __u64 hash)
1732 {
1733         struct osd_zap_it *it = (struct osd_zap_it *)di;
1734         struct osd_object *obj = it->ozi_obj;
1735         struct osd_device *osd = osd_obj2dev(obj);
1736         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1737         int                rc;
1738         ENTRY;
1739
1740         /* reset the cursor */
1741         zap_cursor_fini(it->ozi_zc);
1742         zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1743                                    obj->oo_dn->dn_object, hash);
1744         it->ozi_reset = 0;
1745
1746         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1747         if (rc == 0)
1748                 RETURN(+1);
1749         else if (rc == -ENOENT)
1750                 RETURN(0);
1751
1752         RETURN(rc);
1753 }
1754
1755 static struct dt_index_operations osd_index_ops = {
1756         .dio_lookup             = osd_index_lookup,
1757         .dio_declare_insert     = osd_declare_index_insert,
1758         .dio_insert             = osd_index_insert,
1759         .dio_declare_delete     = osd_declare_index_delete,
1760         .dio_delete             = osd_index_delete,
1761         .dio_it = {
1762                 .init           = osd_index_it_init,
1763                 .fini           = osd_index_it_fini,
1764                 .get            = osd_index_it_get,
1765                 .put            = osd_index_it_put,
1766                 .next           = osd_index_it_next,
1767                 .key            = osd_index_it_key,
1768                 .key_size       = osd_index_it_key_size,
1769                 .rec            = osd_index_it_rec,
1770                 .store          = osd_index_it_store,
1771                 .load           = osd_index_it_load
1772         }
1773 };
1774
1775 struct osd_metadnode_it {
1776         struct osd_device       *mit_dev;
1777         __u64                    mit_pos;
1778         struct lu_fid            mit_fid;
1779         int                      mit_prefetched;
1780         __u64                    mit_prefetched_dnode;
1781 };
1782
1783 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1784                                             struct dt_object *dt, __u32 attr)
1785 {
1786         struct osd_device       *dev   = osd_dev(dt->do_lu.lo_dev);
1787         struct osd_metadnode_it *it;
1788         ENTRY;
1789
1790         OBD_ALLOC_PTR(it);
1791         if (unlikely(it == NULL))
1792                 RETURN(ERR_PTR(-ENOMEM));
1793
1794         it->mit_dev = dev;
1795
1796         /* XXX: dmu_object_next() does NOT find dnodes allocated
1797          *      in the current non-committed txg, so we force txg
1798          *      commit to find all existing dnodes ... */
1799         if (!dev->od_dt_dev.dd_rdonly)
1800                 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1801
1802         RETURN((struct dt_it *)it);
1803 }
1804
1805 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1806 {
1807         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1808
1809         OBD_FREE_PTR(it);
1810 }
1811
1812 static int osd_zfs_otable_it_get(const struct lu_env *env,
1813                                  struct dt_it *di, const struct dt_key *key)
1814 {
1815         return 0;
1816 }
1817
1818 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1819 {
1820 }
1821
1822 #define OTABLE_PREFETCH         256
1823
1824 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1825                                     struct osd_metadnode_it *it)
1826 {
1827         struct osd_device       *dev = it->mit_dev;
1828         int                      rc;
1829
1830         /* can go negative on the very first access to the iterator
1831          * or if some non-Lustre objects were found */
1832         if (unlikely(it->mit_prefetched < 0))
1833                 it->mit_prefetched = 0;
1834
1835         if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1836                 return;
1837
1838         if (it->mit_prefetched_dnode == 0)
1839                 it->mit_prefetched_dnode = it->mit_pos;
1840
1841         while (it->mit_prefetched < OTABLE_PREFETCH) {
1842                 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1843                                       B_FALSE, 0);
1844                 if (unlikely(rc != 0))
1845                         break;
1846
1847                 osd_dmu_prefetch(dev->od_os, it->mit_prefetched_dnode,
1848                                  0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1849
1850                 it->mit_prefetched++;
1851         }
1852 }
1853
1854 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1855 {
1856         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1857         struct lustre_mdt_attrs *lma;
1858         struct osd_device       *dev = it->mit_dev;
1859         nvlist_t                *nvbuf = NULL;
1860         uchar_t                 *v;
1861         __u64                    dnode;
1862         int                      rc, s;
1863
1864         memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1865
1866         dnode = it->mit_pos;
1867         do {
1868                 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1869                 if (unlikely(rc != 0))
1870                         GOTO(out, rc = 1);
1871                 it->mit_prefetched--;
1872
1873                 /* LMA is required for this to be a Lustre object.
1874                  * If there is no xattr skip it. */
1875                 rc = __osd_xattr_load_by_oid(dev, it->mit_pos, &nvbuf);
1876                 if (unlikely(rc != 0))
1877                         continue;
1878
1879                 LASSERT(nvbuf != NULL);
1880                 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1881                 if (likely(rc == 0)) {
1882                         /* Lustre object */
1883                         lma = (struct lustre_mdt_attrs *)v;
1884                         lustre_lma_swab(lma);
1885                         if (likely(!(lma->lma_compat & LMAC_NOT_IN_OI) &&
1886                                    !(lma->lma_incompat & LMAI_AGENT))) {
1887                                 it->mit_fid = lma->lma_self_fid;
1888                                 nvlist_free(nvbuf);
1889                                 break;
1890                         }
1891                 }
1892
1893                 /* not a Lustre visible object, try next one */
1894                 nvlist_free(nvbuf);
1895         } while (1);
1896
1897
1898         /* we aren't prefetching in the above loop because the number of
1899          * non-Lustre objects is very small and we will be repeating very
1900          * rare. in case we want to use this to iterate over non-Lustre
1901          * objects (i.e. when we convert regular ZFS in Lustre) it makes
1902          * sense to initiate prefetching in the loop */
1903
1904         /* 0 - there are more items, +1 - the end */
1905         if (likely(rc == 0))
1906                 osd_zfs_otable_prefetch(env, it);
1907
1908         CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1909                it->mit_pos, PFID(&it->mit_fid), rc);
1910
1911 out:
1912         return rc;
1913 }
1914
1915 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1916                                             const struct dt_it *di)
1917 {
1918         return NULL;
1919 }
1920
1921 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1922                                       const struct dt_it *di)
1923 {
1924         return sizeof(__u64);
1925 }
1926
1927 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1928                                  const struct dt_it *di,
1929                                  struct dt_rec *rec, __u32 attr)
1930 {
1931         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1932         struct lu_fid *fid = (struct lu_fid *)rec;
1933         ENTRY;
1934
1935         *fid = it->mit_fid;
1936
1937         RETURN(0);
1938 }
1939
1940
1941 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1942                                      const struct dt_it *di)
1943 {
1944         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1945
1946         return it->mit_pos;
1947 }
1948
1949 static int osd_zfs_otable_it_load(const struct lu_env *env,
1950                                   const struct dt_it *di, __u64 hash)
1951 {
1952         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1953
1954         it->mit_pos = hash;
1955         it->mit_prefetched = 0;
1956         it->mit_prefetched_dnode = 0;
1957
1958         return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1959 }
1960
1961 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1962                                      const struct dt_it *di, void *key_rec)
1963 {
1964         return 0;
1965 }
1966
1967 const struct dt_index_operations osd_zfs_otable_ops = {
1968         .dio_it = {
1969                 .init     = osd_zfs_otable_it_init,
1970                 .fini     = osd_zfs_otable_it_fini,
1971                 .get      = osd_zfs_otable_it_get,
1972                 .put      = osd_zfs_otable_it_put,
1973                 .next     = osd_zfs_otable_it_next,
1974                 .key      = osd_zfs_otable_it_key,
1975                 .key_size = osd_zfs_otable_it_key_size,
1976                 .rec      = osd_zfs_otable_it_rec,
1977                 .store    = osd_zfs_otable_it_store,
1978                 .load     = osd_zfs_otable_it_load,
1979                 .key_rec  = osd_zfs_otable_it_key_rec,
1980         }
1981 };
1982
1983 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1984                 const struct dt_index_features *feat)
1985 {
1986         struct osd_object *obj = osd_dt_obj(dt);
1987         int rc = 0;
1988         ENTRY;
1989
1990         down_read(&obj->oo_guard);
1991
1992         /*
1993          * XXX: implement support for fixed-size keys sorted with natural
1994          *      numerical way (not using internal hash value)
1995          */
1996         if (feat->dif_flags & DT_IND_RANGE)
1997                 GOTO(out, rc = -ERANGE);
1998
1999         if (unlikely(feat == &dt_otable_features)) {
2000                 dt->do_index_ops = &osd_zfs_otable_ops;
2001                 GOTO(out, rc = 0);
2002         }
2003
2004         LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
2005         if (likely(feat == &dt_directory_features)) {
2006                 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
2007                         dt->do_index_ops = &osd_dir_ops;
2008                 else
2009                         GOTO(out, rc = -ENOTDIR);
2010         } else if (unlikely(feat == &dt_acct_features)) {
2011                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
2012                 dt->do_index_ops = &osd_acct_index_ops;
2013         } else if (dt->do_index_ops == NULL) {
2014                 /* For index file, we don't support variable key & record sizes
2015                  * and the key has to be unique */
2016                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
2017                         GOTO(out, rc = -EINVAL);
2018
2019                 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
2020                         GOTO(out, rc = -E2BIG);
2021                 if (feat->dif_keysize_max != feat->dif_keysize_min)
2022                         GOTO(out, rc = -EINVAL);
2023
2024                 /* As for the record size, it should be a multiple of 8 bytes
2025                  * and smaller than the maximum value length supported by ZAP.
2026                  */
2027                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
2028                         GOTO(out, rc = -E2BIG);
2029                 if (feat->dif_recsize_max != feat->dif_recsize_min)
2030                         GOTO(out, rc = -EINVAL);
2031
2032                 obj->oo_keysize = feat->dif_keysize_max;
2033                 obj->oo_recsize = feat->dif_recsize_max;
2034                 obj->oo_recusize = 1;
2035
2036                 /* ZFS prefers to work with array of 64bits */
2037                 if ((obj->oo_recsize & 7) == 0) {
2038                         obj->oo_recsize >>= 3;
2039                         obj->oo_recusize = 8;
2040                 }
2041                 dt->do_index_ops = &osd_index_ops;
2042         }
2043
2044 out:
2045         up_read(&obj->oo_guard);
2046
2047         RETURN(rc);
2048 }