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