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