4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/osd-zfs/osd_index.c
33 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
34 * Author: Mike Pershin <tappro@whamcloud.com>
37 #define DEBUG_SUBSYSTEM S_OSD
39 #include <libcfs/libcfs.h>
40 #include <obd_support.h>
41 #include <lustre_net.h>
43 #include <obd_class.h>
44 #include <lustre_disk.h>
45 #include <lustre_fid.h>
47 #include "osd_internal.h"
49 #include <sys/dnode.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>
60 #include <lustre_scrub.h>
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
65 #define zap_hashbits(zc) 48
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.
77 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
79 uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
81 return (zfs_hash >> zap_hashbits(zc)) |
82 (zfs_hash << (63 - zap_hashbits(zc)));
85 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
86 uint64_t id, uint64_t dirhash)
88 uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
89 (dirhash >> (63 - zap_hashbits(zc)));
91 zap_cursor_init_serialized(zc, os, id, zfs_hash);
94 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
95 uint64_t id, uint64_t dirhash)
100 if (unlikely(t == NULL))
103 osd_zap_cursor_init_serialized(t, os, id, dirhash);
109 void osd_zap_cursor_fini(zap_cursor_t *zc)
115 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
116 struct osd_object *o,
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);
124 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
127 struct osd_device *d = osd_obj2dev(o);
128 return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
131 static struct dt_it *osd_index_it_init(const struct lu_env *env,
132 struct dt_object *dt,
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;
142 if (obj->oo_destroyed)
143 RETURN(ERR_PTR(-ENOENT));
145 LASSERT(lu_object_exists(lo));
149 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
151 RETURN(ERR_PTR(-ENOMEM));
153 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
155 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
163 RETURN((struct dt_it *)it);
166 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
168 struct osd_zap_it *it = (struct osd_zap_it *)di;
169 struct osd_object *obj;
173 LASSERT(it->ozi_obj);
177 osd_zap_cursor_fini(it->ozi_zc);
178 osd_object_put(env, obj);
179 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
185 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
187 /* PBS: do nothing : ref are incremented at retrive and decreamented
191 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
194 const unsigned align = sizeof(struct luda_type) - 1;
195 struct luda_type *lt;
197 /* check if file type is required */
198 if (attr & LUDA_TYPE) {
199 len = (len + align) & ~align;
201 lt = (void *)ent->lde_name + len;
202 lt->lt_type = cpu_to_le16(DTTOIF(type));
203 ent->lde_attrs |= LUDA_TYPE;
206 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
209 int __osd_xattr_load_by_oid(struct osd_device *osd, uint64_t oid, nvlist_t **sa)
215 rc = -dmu_bonus_hold(osd->od_os, oid, osd_obj_tag, &db);
217 CERROR("%s: can't get bonus, rc = %d\n", osd->od_svname, rc);
221 rc = -sa_handle_get_from_db(osd->od_os, db, NULL, SA_HDL_PRIVATE, &hdl);
223 dmu_buf_rele(db, osd_obj_tag);
227 rc = __osd_xattr_load(osd, hdl, sa);
229 sa_handle_destroy(hdl);
234 * Get the object's FID from its LMA EA.
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
241 * \retval 0 for success
242 * \retval negative error number on failure
244 int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
245 uint64_t oid, struct lu_fid *fid)
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;
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;
260 rc = __osd_xattr_load_by_oid(osd, oid, &sa_xattr);
267 rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
275 if (unlikely(size > sizeof(oti->oti_buf)))
276 GOTO(out, rc = -ERANGE);
278 memcpy(lma, nv_value, size);
283 rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
287 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
288 sa_handle_destroy(sa_hdl);
293 buf.lb_len = sizeof(oti->oti_buf);
294 rc = __osd_xattr_get_large(env, osd, xattr, &buf,
295 XATTR_NAME_LMA, &size);
300 if (size < sizeof(*lma))
301 GOTO(out, rc = -EIO);
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);
311 *fid = lma->lma_self_fid;
316 if (sa_xattr != NULL)
317 nvlist_free(sa_xattr);
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
326 static int osd_find_parent_by_dnode(const struct lu_env *env,
328 struct lu_fid *fid, uint64_t *oid)
330 struct osd_object *obj = osd_dt_obj(o);
331 struct osd_device *osd = osd_obj2dev(obj);
332 uint64_t dnode = ZFS_NO_OBJECT;
336 /* first of all, get parent dnode from own attributes */
337 rc = osd_sa_handle_get(obj);
340 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
344 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
350 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
351 struct lu_fid *fid, uint64_t *oid)
353 struct link_ea_header *leh;
354 struct link_ea_entry *lee;
359 buf.lb_buf = osd_oti_get(env)->oti_buf;
360 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
362 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
364 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
368 OBD_ALLOC(buf.lb_buf, rc);
369 if (buf.lb_buf == NULL)
372 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
376 if (rc < sizeof(*leh) + sizeof(*lee))
377 GOTO(out, rc = -EINVAL);
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);
385 if (leh->leh_magic != LINK_EA_MAGIC)
386 GOTO(out, rc = -EINVAL);
387 if (leh->leh_reccount == 0)
388 GOTO(out, rc = -ENODATA);
390 lee = (struct link_ea_entry *)(leh + 1);
391 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
395 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
396 OBD_FREE(buf.lb_buf, buf.lb_len);
399 /* this block can be enabled for additional verification
400 * it's trying to match FID from LinkEA vs. FID from LMA */
404 rc2 = osd_find_parent_by_dnode(env, o, &fid2, oid);
406 if (lu_fid_eq(fid, &fid2) == 0)
407 CERROR("wrong parent: "DFID" != "DFID"\n",
408 PFID(fid), PFID(&fid2));
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);
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.
439 * To resolve above trouble, we will handle it as the following two cases:
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().
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.
455 static int osd_check_lmv(const struct lu_env *env, struct osd_device *osd,
456 uint64_t oid, const struct lu_fid *fid)
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;
469 rc = __osd_xattr_load_by_oid(osd, oid, &nvbuf);
470 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
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);
484 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
485 GOTO(out_nvbuf, rc = -EINVAL);
487 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
488 rc = -zap_cursor_retrieve(zc, za);
490 zap_cursor_advance(zc);
492 CERROR("%s: fail to init for check LMV "DFID"(%llu): rc = %d\n",
493 osd_name(osd), PFID(fid), oid, rc);
498 rc = -zap_cursor_retrieve(zc, za);
500 GOTO(out_zc, rc = 0);
503 CERROR("%s: fail to locate next for check LMV "
504 DFID"(%llu): rc = %d\n",
505 osd_name(osd), PFID(fid), oid, rc);
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,
517 CERROR("%s: fail to lookup for check LMV "
518 DFID"(%llu): rc = %d\n",
519 osd_name(osd), PFID(fid), oid, rc);
523 rc = osd_oii_insert(env, osd, tfid,
524 zde->lzd_reg.zde_dnode, false);
528 zap_cursor_advance(zc);
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)
544 struct lustre_scrub *scrub = &osd->od_scrub;
552 if (!fid_is_norm(fid) && !fid_is_igif(fid))
555 /* oid == ZFS_NO_OBJECT must be for lookup ".." case */
556 if (oid == ZFS_NO_OBJECT) {
557 rc = osd_sa_handle_get(obj);
561 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &oid, 8);
566 if (scrub->os_running) {
567 if (scrub->os_pos_current > oid)
569 } else if (osd->od_auto_scrub_interval == AS_NEVER) {
572 if (ktime_get_real_seconds() <
573 scrub->os_file.sf_time_last_complete +
574 osd->od_auto_scrub_interval)
579 rc = osd_fid_lookup(env, osd, fid, &oid2);
585 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
586 /* The object has been removed (by race maybe). */
588 RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
591 } else if (rc || oid == oid2) {
598 if (scrub->os_running) {
600 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
601 /* The object has been removed (by race maybe). */
603 RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
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))
615 rc = osd_check_lmv(env, osd, oid, fid);
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);
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)
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;
653 if (name[0] == '.') {
655 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
656 memcpy(rec, f, sizeof(*f));
658 } else if (name[1] == '.' && name[2] == 0) {
659 rc = osd_find_parent_fid(env, dt, fid, &oid);
664 memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
665 rc = osd_zap_lookup(osd, obj->oo_dn->dn_object, obj->oo_dn,
666 (char *)key, 8, sizeof(oti->oti_zde) / 8,
667 (void *)&oti->oti_zde);
671 oid = oti->oti_zde.lzd_reg.zde_dnode;
672 if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
673 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
677 rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode, fid);
682 if (!rc && !osd_remote_fid(env, osd, fid)) {
684 * this should ask the scrubber to check OI given
685 * the mapping we just found in the dir entry.
686 * but result of that check should not affect
687 * result of the lookup in the directory.
688 * otherwise such a direntry becomes hidden
689 * from the layers above, including LFSCK which
690 * is supposed to fix dangling entries.
692 osd_consistency_check(env, osd, obj, fid, oid,
693 S_ISDIR(DTTOIF(oti->oti_zde.lzd_reg.zde_type)));
696 return rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc);
700 * In DNE environment, the object and its name entry may reside on different
701 * MDTs. Under such case, we will create an agent object on the MDT where the
702 * name entry resides. The agent object is empty, and indicates that the real
703 * object for the name entry resides on another MDT. If without agent object,
704 * related name entry will be skipped when perform MDT side file level backup
705 * and restore via ZPL by userspace tool, such as 'tar'.
707 static int osd_create_agent_object(const struct lu_env *env,
708 struct osd_device *osd,
709 struct luz_direntry *zde,
710 uint64_t parent, dmu_tx_t *tx)
712 struct osd_thread_info *info = osd_oti_get(env);
713 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
714 struct lu_attr *la = &info->oti_la;
715 nvlist_t *nvbuf = NULL;
721 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTOBJ))
724 rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
728 lustre_lma_init(lma, &zde->lzd_fid, 0, LMAI_AGENT);
729 lustre_lma_swab(lma);
730 rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA, (uchar_t *)lma,
735 la->la_valid = LA_TYPE | LA_MODE;
736 la->la_mode = (DTTOIF(zde->lzd_reg.zde_type) & S_IFMT) |
737 S_IRUGO | S_IWUSR | S_IXUGO;
739 if (S_ISDIR(la->la_mode))
740 rc = __osd_zap_create(env, osd, &dn, tx, la,
741 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS), 0);
743 rc = __osd_object_create(env, osd, NULL, &zde->lzd_fid,
748 zde->lzd_reg.zde_dnode = dn->dn_object;
749 rc = -sa_handle_get(osd->od_os, dn->dn_object, NULL,
750 SA_HDL_PRIVATE, &hdl);
752 rc = __osd_attr_init(env, osd, NULL, hdl, tx,
754 sa_handle_destroy(hdl);
762 dmu_object_free(osd->od_os, dn->dn_object, tx);
772 int osd_add_to_remote_parent(const struct lu_env *env,
773 struct osd_device *osd,
774 struct osd_object *obj,
775 struct osd_thandle *oh)
777 struct osd_thread_info *info = osd_oti_get(env);
778 struct luz_direntry *zde = &info->oti_zde;
779 char *name = info->oti_str;
780 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
781 struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
782 struct lu_buf buf = {
784 .lb_len = sizeof(info->oti_buf),
790 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
793 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
795 CWARN("%s: fail to load LMA for adding "
796 DFID" to remote parent: rc = %d\n",
797 osd_name(osd), PFID(fid), rc);
801 lustre_lma_swab(lma);
802 lma->lma_incompat |= LMAI_REMOTE_PARENT;
803 lustre_lma_swab(lma);
805 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
806 LU_XATTR_REPLACE, oh);
808 CWARN("%s: fail to update LMA for adding "
809 DFID" to remote parent: rc = %d\n",
810 osd_name(osd), PFID(fid), rc);
814 osd_fid2str(name, fid, sizeof(info->oti_str));
815 zde->lzd_reg.zde_dnode = obj->oo_dn->dn_object;
816 zde->lzd_reg.zde_type = S_DT(S_IFDIR);
819 rc = osd_zap_add(osd, osd->od_remote_parent_dir, NULL,
820 name, 8, sizeof(*zde) / 8, zde, oh->ot_tx);
821 if (unlikely(rc == -EEXIST))
824 CWARN("%s: fail to add name entry for "
825 DFID" to remote parent: rc = %d\n",
826 osd_name(osd), PFID(fid), rc);
828 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
833 int osd_delete_from_remote_parent(const struct lu_env *env,
834 struct osd_device *osd,
835 struct osd_object *obj,
836 struct osd_thandle *oh, bool destroy)
838 struct osd_thread_info *info = osd_oti_get(env);
839 char *name = info->oti_str;
840 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
841 struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
842 struct lu_buf buf = {
844 .lb_len = sizeof(info->oti_buf),
850 osd_fid2str(name, fid, sizeof(info->oti_str));
851 rc = osd_zap_remove(osd, osd->od_remote_parent_dir, NULL,
853 if (unlikely(rc == -ENOENT))
856 CERROR("%s: fail to remove entry under remote "
857 "parent for "DFID": rc = %d\n",
858 osd_name(osd), PFID(fid), rc);
863 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
865 CERROR("%s: fail to load LMA for removing "
866 DFID" from remote parent: rc = %d\n",
867 osd_name(osd), PFID(fid), rc);
871 lustre_lma_swab(lma);
872 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
873 lustre_lma_swab(lma);
875 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
876 LU_XATTR_REPLACE, oh);
878 CERROR("%s: fail to update LMA for removing "
879 DFID" from remote parent: rc = %d\n",
880 osd_name(osd), PFID(fid), rc);
882 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
887 static int osd_declare_dir_insert(const struct lu_env *env,
888 struct dt_object *dt,
889 const struct dt_rec *rec,
890 const struct dt_key *key,
893 struct osd_object *obj = osd_dt_obj(dt);
894 struct osd_device *osd = osd_obj2dev(obj);
895 const struct dt_insert_rec *rec1;
896 const struct lu_fid *fid;
897 struct osd_thandle *oh;
899 struct osd_idmap_cache *idc;
902 rec1 = (struct dt_insert_rec *)rec;
904 LASSERT(fid != NULL);
905 LASSERT(rec1->rec_type != 0);
908 oh = container_of(th, struct osd_thandle, ot_super);
910 idc = osd_idc_find_or_init(env, osd, fid);
912 RETURN(PTR_ERR(idc));
914 if (idc->oic_remote) {
915 const char *name = (const char *)key;
917 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
918 /* Prepare agent object for remote entry that will
919 * be used for operations via ZPL, such as MDT side
920 * file-level backup and restore. */
921 dmu_tx_hold_sa_create(oh->ot_tx,
922 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
923 if (S_ISDIR(rec1->rec_type))
924 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT,
929 /* This is for inserting dot/dotdot for new created dir. */
930 if (obj->oo_dn == NULL)
931 object = DMU_NEW_OBJECT;
933 object = obj->oo_dn->dn_object;
935 /* do not specify the key as then DMU is trying to look it up
936 * which is very expensive. usually the layers above lookup
937 * before insertion */
938 osd_tx_hold_zap(oh->ot_tx, object, obj->oo_dn, TRUE, NULL);
943 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
946 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
947 struct seq_server_site *ss = osd_seq_site(osd);
952 LASSERT(ss->ss_server_fld != NULL);
954 rc = osd_fld_lookup(env, osd, seq, range);
957 CERROR("%s: Can not lookup fld for %#llx\n",
962 RETURN(ss->ss_node_id == range->lsr_index);
965 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
966 const struct lu_fid *fid)
968 struct seq_server_site *ss = osd_seq_site(osd);
971 /* FID seqs not in FLDB, must be local seq */
972 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
975 /* If FLD is not being initialized yet, it only happens during the
976 * initialization, likely during mgs initialization, and we assume
977 * this is local FID. */
978 if (ss == NULL || ss->ss_server_fld == NULL)
981 /* Only check the local FLDB here */
982 if (osd_seq_exists(env, osd, fid_seq(fid)))
989 * Inserts (key, value) pair in \a directory object.
991 * \param dt osd index object
992 * \param key key for index
993 * \param rec record reference
994 * \param th transaction handler
997 * \retval -ve failure
999 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
1000 const struct dt_rec *rec, const struct dt_key *key,
1003 struct osd_thread_info *oti = osd_oti_get(env);
1004 struct osd_object *parent = osd_dt_obj(dt);
1005 struct osd_device *osd = osd_obj2dev(parent);
1006 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
1007 const struct lu_fid *fid = rec1->rec_fid;
1008 struct osd_thandle *oh;
1009 struct osd_idmap_cache *idc;
1010 const char *name = (const char *)key;
1011 struct luz_direntry *zde = &oti->oti_zde;
1012 int num = sizeof(*zde) / 8;
1016 LASSERT(parent->oo_dn);
1018 LASSERT(dt_object_exists(dt));
1019 LASSERT(osd_invariant(parent));
1021 LASSERT(th != NULL);
1022 oh = container_of(th, struct osd_thandle, ot_super);
1024 idc = osd_idc_find(env, osd, fid);
1025 if (unlikely(idc == NULL)) {
1026 /* this dt_insert() wasn't declared properly, so
1027 * FID is missing in OI cache. we better do not
1028 * lookup FID in FLDB/OI and don't risk to deadlock,
1029 * but in some special cases (lfsck testing, etc)
1030 * it's much simpler than fixing a caller */
1031 idc = osd_idc_find_or_init(env, osd, fid);
1033 CERROR("%s: "DFID" wasn't declared for insert\n",
1034 osd_name(osd), PFID(fid));
1035 RETURN(PTR_ERR(idc));
1039 BUILD_BUG_ON(sizeof(zde->lzd_reg) != 8);
1040 BUILD_BUG_ON(sizeof(*zde) % 8 != 0);
1042 memset(&zde->lzd_reg, 0, sizeof(zde->lzd_reg));
1043 zde->lzd_reg.zde_type = S_DT(rec1->rec_type & S_IFMT);
1044 zde->lzd_fid = *fid;
1046 if (idc->oic_remote) {
1047 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
1048 /* Create agent inode for remote object that will
1049 * be used for MDT file-level backup and restore. */
1050 rc = osd_create_agent_object(env, osd, zde,
1051 parent->oo_dn->dn_object, oh->ot_tx);
1053 CWARN("%s: Fail to create agent object for "
1055 osd_name(osd), PFID(fid), rc);
1056 /* Ignore the failure since the system can go
1057 * ahead if we do not care about the MDT side
1058 * file-level backup and restore. */
1063 if (unlikely(idc->oic_dnode == 0)) {
1064 /* for a reason OI cache wasn't filled properly */
1065 CERROR("%s: OIC for "DFID" isn't filled\n",
1066 osd_name(osd), PFID(fid));
1069 if (name[0] == '.') {
1071 /* do not store ".", instead generate it
1072 * during iteration */
1074 } else if (name[1] == '.' && name[2] == 0) {
1075 uint64_t dnode = idc->oic_dnode;
1076 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
1079 /* update parent dnode in the child.
1080 * later it will be used to generate ".." */
1081 rc = osd_object_sa_update(parent,
1088 zde->lzd_reg.zde_dnode = idc->oic_dnode;
1091 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR))
1092 zde->lzd_fid.f_ver = ~0;
1094 /* The logic is not related with IGIF, just re-use the fail_loc value
1095 * to be consistent with ldiskfs case, then share the same test logic */
1096 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1099 /* Insert (key,oid) into ZAP */
1100 rc = osd_zap_add(osd, parent->oo_dn->dn_object, parent->oo_dn,
1101 name, 8, num, (void *)zde, oh->ot_tx);
1102 if (unlikely(rc == -EEXIST &&
1103 name[0] == '.' && name[1] == '.' && name[2] == 0))
1104 /* Update (key,oid) in ZAP */
1105 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object, name, 8,
1106 sizeof(*zde) / 8, (void *)zde, oh->ot_tx);
1113 static int osd_declare_dir_delete(const struct lu_env *env,
1114 struct dt_object *dt,
1115 const struct dt_key *key,
1118 struct osd_object *obj = osd_dt_obj(dt);
1119 dnode_t *zap_dn = obj->oo_dn;
1120 struct osd_thandle *oh;
1121 const char *name = (const char *)key;
1124 LASSERT(dt_object_exists(dt));
1125 LASSERT(osd_invariant(obj));
1126 LASSERT(zap_dn != NULL);
1128 LASSERT(th != NULL);
1129 oh = container_of(th, struct osd_thandle, ot_super);
1132 * In Orion . and .. were stored in the directory (not generated upon
1133 * request as now). We preserve them for backward compatibility.
1135 if (name[0] == '.') {
1138 else if (name[1] == '.' && name[2] == 0)
1142 /* do not specify the key as then DMU is trying to look it up
1143 * which is very expensive. usually the layers above lookup
1144 * before deletion */
1145 osd_tx_hold_zap(oh->ot_tx, zap_dn->dn_object, zap_dn, FALSE, NULL);
1147 /* For destroying agent object if have. */
1148 dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1153 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
1154 const struct dt_key *key, struct thandle *th)
1156 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1157 struct osd_object *obj = osd_dt_obj(dt);
1158 struct osd_device *osd = osd_obj2dev(obj);
1159 struct osd_thandle *oh;
1160 dnode_t *zap_dn = obj->oo_dn;
1161 char *name = (char *)key;
1167 LASSERT(th != NULL);
1168 oh = container_of(th, struct osd_thandle, ot_super);
1171 * In Orion . and .. were stored in the directory (not generated upon
1172 * request as now). we preserve them for backward compatibility
1174 if (name[0] == '.') {
1177 } else if (name[1] == '.' && name[2] == 0) {
1182 /* XXX: We have to say that lookup during delete_declare will affect
1183 * performance, but we have to check whether the name entry (to
1184 * be deleted) has agent object or not to avoid orphans.
1186 * We will improve that in the future, some possible solutions,
1188 * 1) Some hint from the caller via transaction handle to make
1189 * the lookup conditionally.
1190 * 2) Enhance the ZFS logic to recognize the OSD lookup result
1191 * and delete the given entry directly without lookup again
1192 * internally. LU-10190 */
1193 memset(&zde->lzd_fid, 0, sizeof(zde->lzd_fid));
1194 rc = osd_zap_lookup(osd, zap_dn->dn_object, zap_dn, name, 8, 3, zde);
1197 CERROR("%s: failed to locate entry %s: rc = %d\n",
1198 osd->od_svname, name, rc);
1202 if (unlikely(osd_remote_fid(env, osd, &zde->lzd_fid) > 0)) {
1203 rc = -dmu_object_free(osd->od_os, zde->lzd_reg.zde_dnode,
1206 CERROR("%s: failed to destroy agent object (%llu) "
1207 "for the entry %s: rc = %d\n", osd->od_svname,
1208 (__u64)zde->lzd_reg.zde_dnode, name, rc);
1211 /* Remove key from the ZAP */
1212 rc = osd_zap_remove(osd, zap_dn->dn_object, zap_dn,
1213 (char *)key, oh->ot_tx);
1215 CERROR("%s: zap_remove %s failed: rc = %d\n",
1216 osd->od_svname, name, rc);
1221 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
1222 struct dt_object *dt,
1225 struct osd_zap_it *it;
1227 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
1229 it->ozi_pos = OZI_POS_INIT;
1231 RETURN((struct dt_it *)it);
1235 * Move Iterator to record specified by \a key
1237 * \param di osd iterator
1238 * \param key key for index
1240 * \retval +ve di points to record with least key not larger than key
1241 * \retval 0 di points to exact matched key
1242 * \retval -ve failure
1244 static int osd_dir_it_get(const struct lu_env *env,
1245 struct dt_it *di, const struct dt_key *key)
1247 struct osd_zap_it *it = (struct osd_zap_it *)di;
1248 struct osd_object *obj = it->ozi_obj;
1249 char *name = (char *)key;
1254 LASSERT(it->ozi_zc);
1256 /* reset the cursor */
1257 zap_cursor_fini(it->ozi_zc);
1258 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
1260 /* XXX: implementation of the API is broken at the moment */
1261 LASSERT(((const char *)key)[0] == 0);
1264 it->ozi_pos = OZI_POS_INIT;
1268 if (name[0] == '.') {
1270 it->ozi_pos = OZI_POS_DOT;
1272 } else if (name[1] == '.' && name[2] == 0) {
1273 it->ozi_pos = OZI_POS_DOTDOT;
1278 /* neither . nor .. - some real record */
1279 it->ozi_pos = OZI_POS_REAL;
1286 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
1288 /* PBS: do nothing : ref are incremented at retrive and decreamented
1293 * in Orion . and .. were stored in the directory, while ZPL
1294 * and current osd-zfs generate them up on request. so, we
1295 * need to ignore previously stored . and ..
1297 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
1298 zap_attribute_t *za)
1303 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1306 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
1307 if (za->za_name[1] == 0) {
1309 } else if (za->za_name[1] == '.' &&
1310 za->za_name[2] == 0) {
1313 if (unlikely(isdot))
1314 zap_cursor_advance(it->ozi_zc);
1316 } while (unlikely(rc == 0 && isdot));
1322 * to load a directory entry at a time and stored it in
1323 * iterator's in-memory data structure.
1325 * \param di, struct osd_it_ea, iterator's in memory structure
1327 * \retval +ve, iterator reached to end
1328 * \retval 0, iterator not reached to end
1329 * \retval -ve, on error
1331 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
1333 struct osd_zap_it *it = (struct osd_zap_it *)di;
1334 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1339 /* temp. storage should be enough for any key supported by ZFS */
1340 BUILD_BUG_ON(sizeof(za->za_name) > sizeof(it->ozi_name));
1343 * the first ->next() moves the cursor to .
1344 * the second ->next() moves the cursor to ..
1345 * then we get to the real records and have to verify any exist
1347 if (it->ozi_pos <= OZI_POS_DOTDOT) {
1349 if (it->ozi_pos <= OZI_POS_DOTDOT)
1353 zap_cursor_advance(it->ozi_zc);
1357 * According to current API we need to return error if its last entry.
1358 * zap_cursor_advance() does not return any value. So we need to call
1359 * retrieve to check if there is any record. We should make
1360 * changes to Iterator API to not return status for this API
1362 rc = osd_index_retrieve_skip_dots(it, za);
1364 if (rc == -ENOENT) /* end of dir */
1370 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
1371 const struct dt_it *di)
1373 struct osd_zap_it *it = (struct osd_zap_it *)di;
1374 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1378 if (it->ozi_pos <= OZI_POS_DOT) {
1379 it->ozi_pos = OZI_POS_DOT;
1380 RETURN((struct dt_key *)".");
1381 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1382 RETURN((struct dt_key *)"..");
1385 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
1386 RETURN(ERR_PTR(rc));
1388 strcpy(it->ozi_name, za->za_name);
1390 RETURN((struct dt_key *)it->ozi_name);
1393 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
1395 struct osd_zap_it *it = (struct osd_zap_it *)di;
1396 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1400 if (it->ozi_pos <= OZI_POS_DOT) {
1401 it->ozi_pos = OZI_POS_DOT;
1403 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1407 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
1408 rc = strlen(za->za_name);
1414 osd_dirent_update(const struct lu_env *env, struct osd_device *dev,
1415 uint64_t zap, const char *key, struct luz_direntry *zde)
1421 tx = dmu_tx_create(dev->od_os);
1425 dmu_tx_hold_zap(tx, zap, TRUE, NULL);
1426 rc = -dmu_tx_assign(tx, TXG_WAIT);
1428 rc = -zap_update(dev->od_os, zap, key, 8, sizeof(*zde) / 8,
1429 (const void *)zde, tx);
1438 static int osd_update_entry_for_agent(const struct lu_env *env,
1439 struct osd_device *osd,
1440 uint64_t zap, const char *name,
1441 struct luz_direntry *zde, __u32 attr)
1443 dmu_tx_t *tx = NULL;
1447 if (attr & LUDA_VERIFY_DRYRUN)
1450 tx = dmu_tx_create(osd->od_os);
1452 GOTO(out, rc = -ENOMEM);
1454 dmu_tx_hold_sa_create(tx, osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
1455 dmu_tx_hold_zap(tx, zap, FALSE, NULL);
1456 rc = -dmu_tx_assign(tx, TXG_WAIT);
1462 rc = osd_create_agent_object(env, osd, zde, zap, tx);
1464 rc = -zap_update(osd->od_os, zap, name, 8, sizeof(*zde) / 8,
1465 (const void *)zde, tx);
1471 CDEBUG(D_LFSCK, "%s: Updated (%s) remote entry for "DFID": rc = %d\n",
1472 osd_name(osd), (attr & LUDA_VERIFY_DRYRUN) ? "(ro)" : "(rw)",
1473 PFID(&zde->lzd_fid), rc);
1477 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
1478 struct dt_rec *dtrec, __u32 attr)
1480 struct osd_zap_it *it = (struct osd_zap_it *)di;
1481 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
1482 struct osd_thread_info *info = osd_oti_get(env);
1483 struct luz_direntry *zde = &info->oti_zde;
1484 zap_attribute_t *za = &info->oti_za;
1485 struct lu_fid *fid = &info->oti_fid;
1486 struct osd_device *osd = osd_obj2dev(it->ozi_obj);
1491 if (it->ozi_pos <= OZI_POS_DOT) {
1492 /* notice hash=0 here, this is needed to avoid
1493 * case when some real entry (after ./..) may
1494 * have hash=0. in this case the client would
1495 * be confused having records out of hash order. */
1496 lde->lde_hash = cpu_to_le64(0);
1497 strcpy(lde->lde_name, ".");
1498 lde->lde_namelen = cpu_to_le16(1);
1499 fid_cpu_to_le(&lde->lde_fid,
1500 lu_object_fid(&it->ozi_obj->oo_dt.do_lu));
1501 lde->lde_attrs = LUDA_FID;
1502 /* append lustre attributes */
1503 osd_it_append_attrs(lde, attr, 1, S_DT(S_IFDIR));
1504 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
1505 it->ozi_pos = OZI_POS_DOT;
1507 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1508 /* same as for . above */
1509 lde->lde_hash = cpu_to_le64(0);
1510 strcpy(lde->lde_name, "..");
1511 lde->lde_namelen = cpu_to_le16(2);
1512 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, fid, NULL);
1514 fid_cpu_to_le(&lde->lde_fid, fid);
1515 lde->lde_attrs = LUDA_FID;
1516 } else if (rc != -ENOENT) {
1517 /* ENOENT happens at the root of filesystem, ignore */
1521 /* append lustre attributes */
1522 osd_it_append_attrs(lde, attr, 2, S_DT(S_IFDIR));
1523 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
1529 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1533 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
1534 namelen = strlen(za->za_name);
1535 if (namelen > NAME_MAX)
1537 strcpy(lde->lde_name, za->za_name);
1538 lde->lde_namelen = cpu_to_le16(namelen);
1540 if (za->za_integer_length != 8) {
1541 CERROR("%s: unsupported direntry format: %d %d\n",
1543 za->za_integer_length, (int)za->za_num_integers);
1547 rc = osd_zap_lookup(osd, it->ozi_zc->zc_zapobj, it->ozi_obj->oo_dn,
1548 za->za_name, za->za_integer_length, 3, zde);
1552 if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid)) {
1553 lde->lde_attrs = LUDA_FID;
1554 fid_cpu_to_le(&lde->lde_fid, &zde->lzd_fid);
1555 if (unlikely(zde->lzd_reg.zde_dnode == ZFS_NO_OBJECT &&
1556 osd_remote_fid(env, osd, &zde->lzd_fid) > 0 &&
1557 attr & LUDA_VERIFY)) {
1558 /* It is mainly used for handling the MDT
1559 * upgraded from old ZFS based backend. */
1560 rc = osd_update_entry_for_agent(env, osd,
1561 it->ozi_obj->oo_dn->dn_object,
1562 za->za_name, zde, attr);
1564 lde->lde_attrs |= LUDA_REPAIR;
1566 lde->lde_attrs |= LUDA_UNKNOWN;
1569 if (!(attr & (LUDA_VERIFY | LUDA_VERIFY_DRYRUN)))
1570 GOTO(pack_attr, rc = 0);
1573 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
1576 rc = osd_get_fid_by_oid(env, osd, zde->lzd_reg.zde_dnode, fid);
1578 lde->lde_attrs = LUDA_UNKNOWN;
1579 GOTO(pack_attr, rc = 0);
1582 if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid) &&
1583 lu_fid_eq(&zde->lzd_fid, fid))
1584 GOTO(pack_attr, rc = 0);
1586 if (!(attr & LUDA_VERIFY)) {
1587 fid_cpu_to_le(&lde->lde_fid, fid);
1588 lde->lde_attrs = LUDA_FID;
1589 GOTO(pack_attr, rc = 0);
1592 if (attr & LUDA_VERIFY_DRYRUN) {
1593 fid_cpu_to_le(&lde->lde_fid, fid);
1594 lde->lde_attrs = LUDA_FID | LUDA_REPAIR;
1595 GOTO(pack_attr, rc = 0);
1598 fid_cpu_to_le(&lde->lde_fid, fid);
1599 lde->lde_attrs = LUDA_FID;
1600 zde->lzd_fid = *fid;
1601 rc = osd_dirent_update(env, osd, it->ozi_zc->zc_zapobj,
1604 lde->lde_attrs |= LUDA_UNKNOWN;
1605 GOTO(pack_attr, rc = 0);
1608 lde->lde_attrs |= LUDA_REPAIR;
1610 GOTO(pack_attr, rc = 0);
1613 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1614 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1618 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1621 struct osd_zap_it *it = (struct osd_zap_it *)di;
1622 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1627 if (it->ozi_pos <= OZI_POS_DOT)
1629 else if (it->ozi_pos == OZI_POS_DOTDOT)
1633 rc = lu_dirent_calc_size(namelen, attr);
1637 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1638 if (unlikely(rc != 0))
1641 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1642 CERROR("%s: unsupported direntry format: %d %d\n",
1643 osd_obj2dev(it->ozi_obj)->od_svname,
1644 za->za_integer_length, (int)za->za_num_integers);
1648 namelen = strlen(za->za_name);
1649 if (namelen > NAME_MAX)
1652 rc = lu_dirent_calc_size(namelen, attr);
1657 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1659 struct osd_zap_it *it = (struct osd_zap_it *)di;
1663 if (it->ozi_pos <= OZI_POS_DOTDOT)
1666 pos = osd_zap_cursor_serialize(it->ozi_zc);
1673 * rc == 0 -> end of directory.
1674 * rc > 0 -> ok, proceed.
1675 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1677 static int osd_dir_it_load(const struct lu_env *env,
1678 const struct dt_it *di, __u64 hash)
1680 struct osd_zap_it *it = (struct osd_zap_it *)di;
1681 struct osd_object *obj = it->ozi_obj;
1682 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1686 /* reset the cursor */
1687 zap_cursor_fini(it->ozi_zc);
1688 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1691 it->ozi_pos = OZI_POS_INIT;
1692 rc = +1; /* there will be ./.. at least */
1694 it->ozi_pos = OZI_POS_REAL;
1695 /* to return whether the end has been reached */
1696 rc = osd_index_retrieve_skip_dots(it, za);
1699 else if (rc == -ENOENT)
1706 const struct dt_index_operations osd_dir_ops = {
1707 .dio_lookup = osd_dir_lookup,
1708 .dio_declare_insert = osd_declare_dir_insert,
1709 .dio_insert = osd_dir_insert,
1710 .dio_declare_delete = osd_declare_dir_delete,
1711 .dio_delete = osd_dir_delete,
1713 .init = osd_dir_it_init,
1714 .fini = osd_index_it_fini,
1715 .get = osd_dir_it_get,
1716 .put = osd_dir_it_put,
1717 .next = osd_dir_it_next,
1718 .key = osd_dir_it_key,
1719 .key_size = osd_dir_it_key_size,
1720 .rec = osd_dir_it_rec,
1721 .rec_size = osd_dir_it_rec_size,
1722 .store = osd_dir_it_store,
1723 .load = osd_dir_it_load
1728 * Primitives for index files using binary keys.
1731 /* key integer_size is 8 */
1732 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1733 const struct dt_key *src)
1740 /* align keysize to 64bit */
1741 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1742 size *= sizeof(__u64);
1744 LASSERT(size <= MAXNAMELEN);
1746 if (unlikely(size > o->oo_keysize))
1747 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1748 memcpy(dst, (const char *)src, o->oo_keysize);
1750 return (size/sizeof(__u64));
1753 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1754 struct dt_rec *rec, const struct dt_key *key)
1756 struct osd_object *obj = osd_dt_obj(dt);
1757 struct osd_device *osd = osd_obj2dev(obj);
1758 __u64 *k = osd_oti_get(env)->oti_key64;
1762 rc = osd_prepare_key_uint64(obj, k, key);
1764 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1765 k, rc, obj->oo_recusize, obj->oo_recsize,
1767 RETURN(rc == 0 ? 1 : rc);
1770 static int osd_declare_index_insert(const struct lu_env *env,
1771 struct dt_object *dt,
1772 const struct dt_rec *rec,
1773 const struct dt_key *key,
1776 struct osd_object *obj = osd_dt_obj(dt);
1777 struct osd_thandle *oh;
1780 LASSERT(th != NULL);
1781 oh = container_of(th, struct osd_thandle, ot_super);
1783 LASSERT(obj->oo_dn);
1785 /* do not specify the key as then DMU is trying to look it up
1786 * which is very expensive. usually the layers above lookup
1787 * before insertion */
1788 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1794 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1795 const struct dt_rec *rec, const struct dt_key *key,
1798 struct osd_object *obj = osd_dt_obj(dt);
1799 struct osd_device *osd = osd_obj2dev(obj);
1800 struct osd_thandle *oh;
1801 __u64 *k = osd_oti_get(env)->oti_key64;
1805 LASSERT(obj->oo_dn);
1806 LASSERT(dt_object_exists(dt));
1807 LASSERT(osd_invariant(obj));
1808 LASSERT(th != NULL);
1810 oh = container_of(th, struct osd_thandle, ot_super);
1812 rc = osd_prepare_key_uint64(obj, k, key);
1814 /* Insert (key,oid) into ZAP */
1815 rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1816 k, rc, obj->oo_recusize, obj->oo_recsize,
1817 (void *)rec, oh->ot_tx);
1821 static int osd_declare_index_delete(const struct lu_env *env,
1822 struct dt_object *dt,
1823 const struct dt_key *key,
1826 struct osd_object *obj = osd_dt_obj(dt);
1827 struct osd_thandle *oh;
1830 LASSERT(dt_object_exists(dt));
1831 LASSERT(osd_invariant(obj));
1832 LASSERT(th != NULL);
1833 LASSERT(obj->oo_dn);
1835 oh = container_of(th, struct osd_thandle, ot_super);
1837 /* do not specify the key as then DMU is trying to look it up
1838 * which is very expensive. usually the layers above lookup
1839 * before deletion */
1840 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1846 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1847 const struct dt_key *key, struct thandle *th)
1849 struct osd_object *obj = osd_dt_obj(dt);
1850 struct osd_device *osd = osd_obj2dev(obj);
1851 struct osd_thandle *oh;
1852 __u64 *k = osd_oti_get(env)->oti_key64;
1856 LASSERT(obj->oo_dn);
1857 LASSERT(th != NULL);
1858 oh = container_of(th, struct osd_thandle, ot_super);
1860 rc = osd_prepare_key_uint64(obj, k, key);
1862 /* Remove binary key from the ZAP */
1863 rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1868 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1869 const struct dt_key *key)
1871 struct osd_zap_it *it = (struct osd_zap_it *)di;
1872 struct osd_object *obj = it->ozi_obj;
1873 struct osd_device *osd = osd_obj2dev(obj);
1877 LASSERT(it->ozi_zc);
1880 * XXX: we need a binary version of zap_cursor_move_to_key()
1881 * to implement this API */
1882 if (*((const __u64 *)key) != 0)
1883 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1886 zap_cursor_fini(it->ozi_zc);
1887 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1893 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1895 struct osd_zap_it *it = (struct osd_zap_it *)di;
1896 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1900 if (it->ozi_reset == 0)
1901 zap_cursor_advance(it->ozi_zc);
1905 * According to current API we need to return error if it's last entry.
1906 * zap_cursor_advance() does not return any value. So we need to call
1907 * retrieve to check if there is any record. We should make
1908 * changes to Iterator API to not return status for this API
1910 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1917 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1918 const struct dt_it *di)
1920 struct osd_zap_it *it = (struct osd_zap_it *)di;
1921 struct osd_object *obj = it->ozi_obj;
1922 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1927 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1929 RETURN(ERR_PTR(rc));
1931 /* the binary key is stored in the name */
1932 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1934 RETURN((struct dt_key *)&it->ozi_key);
1937 static int osd_index_it_key_size(const struct lu_env *env,
1938 const struct dt_it *di)
1940 struct osd_zap_it *it = (struct osd_zap_it *)di;
1941 struct osd_object *obj = it->ozi_obj;
1942 RETURN(obj->oo_keysize);
1945 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1946 struct dt_rec *rec, __u32 attr)
1948 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1949 struct osd_zap_it *it = (struct osd_zap_it *)di;
1950 struct osd_object *obj = it->ozi_obj;
1951 struct osd_device *osd = osd_obj2dev(obj);
1952 __u64 *k = osd_oti_get(env)->oti_key64;
1957 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1961 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1963 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1964 k, rc, obj->oo_recusize, obj->oo_recsize,
1969 static __u64 osd_index_it_store(const struct lu_env *env,
1970 const struct dt_it *di)
1972 struct osd_zap_it *it = (struct osd_zap_it *)di;
1975 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1978 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1981 struct osd_zap_it *it = (struct osd_zap_it *)di;
1982 struct osd_object *obj = it->ozi_obj;
1983 struct osd_device *osd = osd_obj2dev(obj);
1984 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1988 /* reset the cursor */
1989 zap_cursor_fini(it->ozi_zc);
1990 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1991 obj->oo_dn->dn_object, hash);
1994 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1997 else if (rc == -ENOENT)
2003 static const struct dt_index_operations osd_index_ops = {
2004 .dio_lookup = osd_index_lookup,
2005 .dio_declare_insert = osd_declare_index_insert,
2006 .dio_insert = osd_index_insert,
2007 .dio_declare_delete = osd_declare_index_delete,
2008 .dio_delete = osd_index_delete,
2010 .init = osd_index_it_init,
2011 .fini = osd_index_it_fini,
2012 .get = osd_index_it_get,
2013 .put = osd_index_it_put,
2014 .next = osd_index_it_next,
2015 .key = osd_index_it_key,
2016 .key_size = osd_index_it_key_size,
2017 .rec = osd_index_it_rec,
2018 .store = osd_index_it_store,
2019 .load = osd_index_it_load
2023 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2024 const struct dt_index_features *feat)
2026 struct osd_object *obj = osd_dt_obj(dt);
2027 struct osd_device *osd = osd_obj2dev(obj);
2028 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2032 down_read(&obj->oo_guard);
2035 * XXX: implement support for fixed-size keys sorted with natural
2036 * numerical way (not using internal hash value)
2038 if (feat->dif_flags & DT_IND_RANGE)
2039 GOTO(out, rc = -ERANGE);
2041 if (unlikely(feat == &dt_otable_features)) {
2042 dt->do_index_ops = &osd_otable_ops;
2046 LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
2047 if (likely(feat == &dt_directory_features)) {
2048 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
2049 dt->do_index_ops = &osd_dir_ops;
2051 GOTO(out, rc = -ENOTDIR);
2052 } else if (unlikely(feat == &dt_acct_features)) {
2053 LASSERT(fid_is_acct(fid));
2054 dt->do_index_ops = &osd_acct_index_ops;
2055 } else if (dt->do_index_ops == NULL) {
2056 /* For index file, we don't support variable key & record sizes
2057 * and the key has to be unique */
2058 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
2059 GOTO(out, rc = -EINVAL);
2061 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
2062 GOTO(out, rc = -E2BIG);
2063 if (feat->dif_keysize_max != feat->dif_keysize_min)
2064 GOTO(out, rc = -EINVAL);
2066 /* As for the record size, it should be a multiple of 8 bytes
2067 * and smaller than the maximum value length supported by ZAP.
2069 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
2070 GOTO(out, rc = -E2BIG);
2071 if (feat->dif_recsize_max != feat->dif_recsize_min)
2072 GOTO(out, rc = -EINVAL);
2074 obj->oo_keysize = feat->dif_keysize_max;
2075 obj->oo_recsize = feat->dif_recsize_max;
2076 obj->oo_recusize = 1;
2078 /* ZFS prefers to work with array of 64bits */
2079 if ((obj->oo_recsize & 7) == 0) {
2080 obj->oo_recsize >>= 3;
2081 obj->oo_recusize = 8;
2083 dt->do_index_ops = &osd_index_ops;
2085 if (feat == &dt_lfsck_layout_orphan_features ||
2086 feat == &dt_lfsck_layout_dangling_features ||
2087 feat == &dt_lfsck_namespace_features)
2090 rc = osd_index_register(osd, fid, obj->oo_keysize,
2091 obj->oo_recusize * obj->oo_recsize);
2093 CWARN("%s: failed to register index "DFID": rc = %d\n",
2094 osd_name(osd), PFID(fid), rc);
2098 CDEBUG(D_LFSCK, "%s: index object "DFID
2099 " (%u/%u/%u) registered\n",
2100 osd_name(osd), PFID(fid), obj->oo_keysize,
2101 obj->oo_recusize, obj->oo_recsize);
2105 up_read(&obj->oo_guard);