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