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