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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd-zfs/osd_index.c
34 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35 * Author: Mike Pershin <tappro@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_OSD
40 #include <libcfs/libcfs.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
44 #include <obd_class.h>
45 #include <lustre_disk.h>
46 #include <lustre_fid.h>
48 #include "osd_internal.h"
50 #include <sys/dnode.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>
61 #include <lustre_scrub.h>
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
66 #define zap_hashbits(zc) 48
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.
78 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
80 uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
82 return (zfs_hash >> zap_hashbits(zc)) |
83 (zfs_hash << (63 - zap_hashbits(zc)));
86 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
87 uint64_t id, uint64_t dirhash)
89 uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
90 (dirhash >> (63 - zap_hashbits(zc)));
92 zap_cursor_init_serialized(zc, os, id, zfs_hash);
95 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
96 uint64_t id, uint64_t dirhash)
101 if (unlikely(t == NULL))
104 osd_zap_cursor_init_serialized(t, os, id, dirhash);
110 void osd_zap_cursor_fini(zap_cursor_t *zc)
116 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
117 struct osd_object *o,
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);
125 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
128 struct osd_device *d = osd_obj2dev(o);
129 return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
132 static struct dt_it *osd_index_it_init(const struct lu_env *env,
133 struct dt_object *dt,
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;
143 if (obj->oo_destroyed)
144 RETURN(ERR_PTR(-ENOENT));
146 LASSERT(lu_object_exists(lo));
150 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
152 RETURN(ERR_PTR(-ENOMEM));
154 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
156 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
164 RETURN((struct dt_it *)it);
167 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
169 struct osd_zap_it *it = (struct osd_zap_it *)di;
170 struct osd_object *obj;
174 LASSERT(it->ozi_obj);
178 osd_zap_cursor_fini(it->ozi_zc);
179 osd_object_put(env, obj);
180 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
186 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
188 /* PBS: do nothing : ref are incremented at retrive and decreamented
192 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
195 const unsigned align = sizeof(struct luda_type) - 1;
196 struct luda_type *lt;
198 /* check if file type is required */
199 if (attr & LUDA_TYPE) {
200 len = (len + align) & ~align;
202 lt = (void *)ent->lde_name + len;
203 lt->lt_type = cpu_to_le16(DTTOIF(type));
204 ent->lde_attrs |= LUDA_TYPE;
207 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
210 int __osd_xattr_load_by_oid(struct osd_device *osd, uint64_t oid, nvlist_t **sa)
216 rc = -dmu_bonus_hold(osd->od_os, oid, osd_obj_tag, &db);
218 CERROR("%s: can't get bonus, rc = %d\n", osd->od_svname, rc);
222 rc = -sa_handle_get_from_db(osd->od_os, db, NULL, SA_HDL_PRIVATE, &hdl);
224 dmu_buf_rele(db, osd_obj_tag);
228 rc = __osd_xattr_load(osd, hdl, sa);
230 sa_handle_destroy(hdl);
235 * Get the object's FID from its LMA EA.
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
242 * \retval 0 for success
243 * \retval negative error number on failure
245 int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
246 uint64_t oid, struct lu_fid *fid)
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;
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;
261 rc = __osd_xattr_load_by_oid(osd, oid, &sa_xattr);
268 rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
276 if (unlikely(size > sizeof(oti->oti_buf)))
277 GOTO(out, rc = -ERANGE);
279 memcpy(lma, nv_value, size);
284 rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
288 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
289 sa_handle_destroy(sa_hdl);
294 buf.lb_len = sizeof(oti->oti_buf);
295 rc = __osd_xattr_get_large(env, osd, xattr, &buf,
296 XATTR_NAME_LMA, &size);
301 if (size < sizeof(*lma))
302 GOTO(out, rc = -EIO);
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);
312 *fid = lma->lma_self_fid;
317 if (sa_xattr != NULL)
318 nvlist_free(sa_xattr);
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
327 static int osd_find_parent_by_dnode(const struct lu_env *env,
329 struct lu_fid *fid, uint64_t *oid)
331 struct osd_object *obj = osd_dt_obj(o);
332 struct osd_device *osd = osd_obj2dev(obj);
333 uint64_t dnode = ZFS_NO_OBJECT;
337 /* first of all, get parent dnode from own attributes */
338 rc = osd_sa_handle_get(obj);
341 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
345 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
351 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
352 struct lu_fid *fid, uint64_t *oid)
354 struct link_ea_header *leh;
355 struct link_ea_entry *lee;
360 buf.lb_buf = osd_oti_get(env)->oti_buf;
361 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
363 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
365 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
369 OBD_ALLOC(buf.lb_buf, rc);
370 if (buf.lb_buf == NULL)
373 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
377 if (rc < sizeof(*leh) + sizeof(*lee))
378 GOTO(out, rc = -EINVAL);
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);
386 if (leh->leh_magic != LINK_EA_MAGIC)
387 GOTO(out, rc = -EINVAL);
388 if (leh->leh_reccount == 0)
389 GOTO(out, rc = -ENODATA);
391 lee = (struct link_ea_entry *)(leh + 1);
392 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
396 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
397 OBD_FREE(buf.lb_buf, buf.lb_len);
400 /* this block can be enabled for additional verification
401 * it's trying to match FID from LinkEA vs. FID from LMA */
405 rc2 = osd_find_parent_by_dnode(env, o, &fid2, oid);
407 if (lu_fid_eq(fid, &fid2) == 0)
408 CERROR("wrong parent: "DFID" != "DFID"\n",
409 PFID(fid), PFID(&fid2));
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);
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.
440 * To resolve above trouble, we will handle it as the following two cases:
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().
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.
456 static int osd_check_lmv(const struct lu_env *env, struct osd_device *osd,
457 uint64_t oid, const struct lu_fid *fid)
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;
470 rc = __osd_xattr_load_by_oid(osd, oid, &nvbuf);
471 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
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);
485 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
486 GOTO(out_nvbuf, rc = -EINVAL);
488 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
489 rc = -zap_cursor_retrieve(zc, za);
491 zap_cursor_advance(zc);
493 CERROR("%s: fail to init for check LMV "DFID"(%llu): rc = %d\n",
494 osd_name(osd), PFID(fid), oid, rc);
499 rc = -zap_cursor_retrieve(zc, za);
501 GOTO(out_zc, rc = 0);
504 CERROR("%s: fail to locate next for check LMV "
505 DFID"(%llu): rc = %d\n",
506 osd_name(osd), PFID(fid), oid, rc);
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,
518 CERROR("%s: fail to lookup for check LMV "
519 DFID"(%llu): rc = %d\n",
520 osd_name(osd), PFID(fid), oid, rc);
524 rc = osd_oii_insert(env, osd, tfid,
525 zde->lzd_reg.zde_dnode, false);
529 zap_cursor_advance(zc);
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)
545 struct lustre_scrub *scrub = &osd->od_scrub;
553 if (!fid_is_norm(fid) && !fid_is_igif(fid))
556 /* oid == ZFS_NO_OBJECT must be for lookup ".." case */
557 if (oid == ZFS_NO_OBJECT) {
558 rc = osd_sa_handle_get(obj);
562 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &oid, 8);
567 if (scrub->os_running) {
568 if (scrub->os_pos_current > oid)
570 } else if (osd->od_auto_scrub_interval == AS_NEVER) {
573 if (ktime_get_real_seconds() <
574 scrub->os_file.sf_time_last_complete +
575 osd->od_auto_scrub_interval)
580 rc = osd_fid_lookup(env, osd, fid, &oid2);
586 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
587 /* The object has been removed (by race maybe). */
589 RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
592 } else if (rc || oid == oid2) {
599 if (scrub->os_running) {
601 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
602 /* The object has been removed (by race maybe). */
604 RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
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))
616 rc = osd_check_lmv(env, osd, oid, fid);
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(D_LFSCK | D_CONSOLE | D_WARNING,
627 "%s: trigger partial OI scrub for RPC inconsistency "
628 "checking FID "DFID": rc = %d\n",
629 osd_name(osd), PFID(fid), rc);
643 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
644 struct dt_rec *rec, const struct dt_key *key)
646 struct osd_thread_info *oti = osd_oti_get(env);
647 struct osd_object *obj = osd_dt_obj(dt);
648 struct osd_device *osd = osd_obj2dev(obj);
649 struct lu_fid *fid = (struct lu_fid *)rec;
650 char *name = (char *)key;
651 uint64_t oid = ZFS_NO_OBJECT;
655 if (name[0] == '.') {
657 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
658 memcpy(rec, f, sizeof(*f));
660 } else if (name[1] == '.' && name[2] == 0) {
661 rc = osd_find_parent_fid(env, dt, fid, &oid);
666 memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
667 rc = osd_zap_lookup(osd, obj->oo_dn->dn_object, obj->oo_dn,
668 (char *)key, 8, sizeof(oti->oti_zde) / 8,
669 (void *)&oti->oti_zde);
673 oid = oti->oti_zde.lzd_reg.zde_dnode;
674 if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
675 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
679 rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode, fid);
684 if (!rc && !osd_remote_fid(env, osd, fid)) {
685 rc = osd_consistency_check(env, osd, obj, fid, oid,
686 S_ISDIR(DTTOIF(oti->oti_zde.lzd_reg.zde_type)));
687 /* Only -ENOENT error will affect the lookup result. */
692 return rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc);
696 * In DNE environment, the object and its name entry may reside on different
697 * MDTs. Under such case, we will create an agent object on the MDT where the
698 * name entry resides. The agent object is empty, and indicates that the real
699 * object for the name entry resides on another MDT. If without agent object,
700 * related name entry will be skipped when perform MDT side file level backup
701 * and restore via ZPL by userspace tool, such as 'tar'.
703 static int osd_create_agent_object(const struct lu_env *env,
704 struct osd_device *osd,
705 struct luz_direntry *zde,
706 uint64_t parent, dmu_tx_t *tx)
708 struct osd_thread_info *info = osd_oti_get(env);
709 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
710 struct lu_attr *la = &info->oti_la;
711 nvlist_t *nvbuf = NULL;
717 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTOBJ))
720 rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
724 lustre_lma_init(lma, &zde->lzd_fid, 0, LMAI_AGENT);
725 lustre_lma_swab(lma);
726 rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA, (uchar_t *)lma,
731 la->la_valid = LA_TYPE | LA_MODE;
732 la->la_mode = (DTTOIF(zde->lzd_reg.zde_type) & S_IFMT) |
733 S_IRUGO | S_IWUSR | S_IXUGO;
735 if (S_ISDIR(la->la_mode))
736 rc = __osd_zap_create(env, osd, &dn, tx, la,
737 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS), 0);
739 rc = __osd_object_create(env, osd, NULL, &zde->lzd_fid,
744 zde->lzd_reg.zde_dnode = dn->dn_object;
745 rc = -sa_handle_get(osd->od_os, dn->dn_object, NULL,
746 SA_HDL_PRIVATE, &hdl);
748 rc = __osd_attr_init(env, osd, NULL, hdl, tx,
750 sa_handle_destroy(hdl);
758 dmu_object_free(osd->od_os, dn->dn_object, tx);
768 int osd_add_to_remote_parent(const struct lu_env *env,
769 struct osd_device *osd,
770 struct osd_object *obj,
771 struct osd_thandle *oh)
773 struct osd_thread_info *info = osd_oti_get(env);
774 struct luz_direntry *zde = &info->oti_zde;
775 char *name = info->oti_str;
776 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
777 struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
778 struct lu_buf buf = {
780 .lb_len = sizeof(info->oti_buf),
786 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
789 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
791 CWARN("%s: fail to load LMA for adding "
792 DFID" to remote parent: rc = %d\n",
793 osd_name(osd), PFID(fid), rc);
797 lustre_lma_swab(lma);
798 lma->lma_incompat |= LMAI_REMOTE_PARENT;
799 lustre_lma_swab(lma);
801 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
802 LU_XATTR_REPLACE, oh);
804 CWARN("%s: fail to update LMA for adding "
805 DFID" to remote parent: rc = %d\n",
806 osd_name(osd), PFID(fid), rc);
810 osd_fid2str(name, fid, sizeof(info->oti_str));
811 zde->lzd_reg.zde_dnode = obj->oo_dn->dn_object;
812 zde->lzd_reg.zde_type = IFTODT(S_IFDIR);
815 rc = osd_zap_add(osd, osd->od_remote_parent_dir, NULL,
816 name, 8, sizeof(*zde) / 8, zde, oh->ot_tx);
817 if (unlikely(rc == -EEXIST))
820 CWARN("%s: fail to add name entry for "
821 DFID" to remote parent: rc = %d\n",
822 osd_name(osd), PFID(fid), rc);
824 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
829 int osd_delete_from_remote_parent(const struct lu_env *env,
830 struct osd_device *osd,
831 struct osd_object *obj,
832 struct osd_thandle *oh, bool destroy)
834 struct osd_thread_info *info = osd_oti_get(env);
835 char *name = info->oti_str;
836 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
837 struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
838 struct lu_buf buf = {
840 .lb_len = sizeof(info->oti_buf),
846 osd_fid2str(name, fid, sizeof(info->oti_str));
847 rc = osd_zap_remove(osd, osd->od_remote_parent_dir, NULL,
849 if (unlikely(rc == -ENOENT))
852 CERROR("%s: fail to remove entry under remote "
853 "parent for "DFID": rc = %d\n",
854 osd_name(osd), PFID(fid), rc);
859 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
861 CERROR("%s: fail to load LMA for removing "
862 DFID" from remote parent: rc = %d\n",
863 osd_name(osd), PFID(fid), rc);
867 lustre_lma_swab(lma);
868 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
869 lustre_lma_swab(lma);
871 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
872 LU_XATTR_REPLACE, oh);
874 CERROR("%s: fail to update LMA for removing "
875 DFID" from remote parent: rc = %d\n",
876 osd_name(osd), PFID(fid), rc);
878 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
883 static int osd_declare_dir_insert(const struct lu_env *env,
884 struct dt_object *dt,
885 const struct dt_rec *rec,
886 const struct dt_key *key,
889 struct osd_object *obj = osd_dt_obj(dt);
890 struct osd_device *osd = osd_obj2dev(obj);
891 const struct dt_insert_rec *rec1;
892 const struct lu_fid *fid;
893 struct osd_thandle *oh;
895 struct osd_idmap_cache *idc;
898 rec1 = (struct dt_insert_rec *)rec;
900 LASSERT(fid != NULL);
901 LASSERT(rec1->rec_type != 0);
904 oh = container_of(th, struct osd_thandle, ot_super);
906 idc = osd_idc_find_or_init(env, osd, fid);
908 RETURN(PTR_ERR(idc));
910 if (idc->oic_remote) {
911 const char *name = (const char *)key;
913 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
914 /* Prepare agent object for remote entry that will
915 * be used for operations via ZPL, such as MDT side
916 * file-level backup and restore. */
917 dmu_tx_hold_sa_create(oh->ot_tx,
918 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
919 if (S_ISDIR(rec1->rec_type))
920 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT,
925 /* This is for inserting dot/dotdot for new created dir. */
926 if (obj->oo_dn == NULL)
927 object = DMU_NEW_OBJECT;
929 object = obj->oo_dn->dn_object;
931 /* do not specify the key as then DMU is trying to look it up
932 * which is very expensive. usually the layers above lookup
933 * before insertion */
934 osd_tx_hold_zap(oh->ot_tx, object, obj->oo_dn, TRUE, NULL);
939 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
942 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
943 struct seq_server_site *ss = osd_seq_site(osd);
948 LASSERT(ss->ss_server_fld != NULL);
950 rc = osd_fld_lookup(env, osd, seq, range);
953 CERROR("%s: Can not lookup fld for %#llx\n",
958 RETURN(ss->ss_node_id == range->lsr_index);
961 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
962 const struct lu_fid *fid)
964 struct seq_server_site *ss = osd_seq_site(osd);
967 /* FID seqs not in FLDB, must be local seq */
968 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
971 /* If FLD is not being initialized yet, it only happens during the
972 * initialization, likely during mgs initialization, and we assume
973 * this is local FID. */
974 if (ss == NULL || ss->ss_server_fld == NULL)
977 /* Only check the local FLDB here */
978 if (osd_seq_exists(env, osd, fid_seq(fid)))
985 * Inserts (key, value) pair in \a directory object.
987 * \param dt osd index object
988 * \param key key for index
989 * \param rec record reference
990 * \param th transaction handler
993 * \retval -ve failure
995 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
996 const struct dt_rec *rec, const struct dt_key *key,
999 struct osd_thread_info *oti = osd_oti_get(env);
1000 struct osd_object *parent = osd_dt_obj(dt);
1001 struct osd_device *osd = osd_obj2dev(parent);
1002 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
1003 const struct lu_fid *fid = rec1->rec_fid;
1004 struct osd_thandle *oh;
1005 struct osd_idmap_cache *idc;
1006 const char *name = (const char *)key;
1007 struct luz_direntry *zde = &oti->oti_zde;
1008 int num = sizeof(*zde) / 8;
1012 LASSERT(parent->oo_dn);
1014 LASSERT(dt_object_exists(dt));
1015 LASSERT(osd_invariant(parent));
1017 LASSERT(th != NULL);
1018 oh = container_of(th, struct osd_thandle, ot_super);
1020 idc = osd_idc_find(env, osd, fid);
1021 if (unlikely(idc == NULL)) {
1022 /* this dt_insert() wasn't declared properly, so
1023 * FID is missing in OI cache. we better do not
1024 * lookup FID in FLDB/OI and don't risk to deadlock,
1025 * but in some special cases (lfsck testing, etc)
1026 * it's much simpler than fixing a caller */
1027 idc = osd_idc_find_or_init(env, osd, fid);
1029 CERROR("%s: "DFID" wasn't declared for insert\n",
1030 osd_name(osd), PFID(fid));
1031 RETURN(PTR_ERR(idc));
1035 BUILD_BUG_ON(sizeof(zde->lzd_reg) != 8);
1036 BUILD_BUG_ON(sizeof(*zde) % 8 != 0);
1038 memset(&zde->lzd_reg, 0, sizeof(zde->lzd_reg));
1039 zde->lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
1040 zde->lzd_fid = *fid;
1042 if (idc->oic_remote) {
1043 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
1044 /* Create agent inode for remote object that will
1045 * be used for MDT file-level backup and restore. */
1046 rc = osd_create_agent_object(env, osd, zde,
1047 parent->oo_dn->dn_object, oh->ot_tx);
1049 CWARN("%s: Fail to create agent object for "
1051 osd_name(osd), PFID(fid), rc);
1052 /* Ignore the failure since the system can go
1053 * ahead if we do not care about the MDT side
1054 * file-level backup and restore. */
1059 if (unlikely(idc->oic_dnode == 0)) {
1060 /* for a reason OI cache wasn't filled properly */
1061 CERROR("%s: OIC for "DFID" isn't filled\n",
1062 osd_name(osd), PFID(fid));
1065 if (name[0] == '.') {
1067 /* do not store ".", instead generate it
1068 * during iteration */
1070 } else if (name[1] == '.' && name[2] == 0) {
1071 uint64_t dnode = idc->oic_dnode;
1072 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
1075 /* update parent dnode in the child.
1076 * later it will be used to generate ".." */
1077 rc = osd_object_sa_update(parent,
1084 zde->lzd_reg.zde_dnode = idc->oic_dnode;
1087 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR))
1088 zde->lzd_fid.f_ver = ~0;
1090 /* The logic is not related with IGIF, just re-use the fail_loc value
1091 * to be consistent with ldiskfs case, then share the same test logic */
1092 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1095 /* Insert (key,oid) into ZAP */
1096 rc = osd_zap_add(osd, parent->oo_dn->dn_object, parent->oo_dn,
1097 name, 8, num, (void *)zde, oh->ot_tx);
1098 if (unlikely(rc == -EEXIST &&
1099 name[0] == '.' && name[1] == '.' && name[2] == 0))
1100 /* Update (key,oid) in ZAP */
1101 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object, name, 8,
1102 sizeof(*zde) / 8, (void *)zde, oh->ot_tx);
1109 static int osd_declare_dir_delete(const struct lu_env *env,
1110 struct dt_object *dt,
1111 const struct dt_key *key,
1114 struct osd_object *obj = osd_dt_obj(dt);
1115 dnode_t *zap_dn = obj->oo_dn;
1116 struct osd_thandle *oh;
1117 const char *name = (const char *)key;
1120 LASSERT(dt_object_exists(dt));
1121 LASSERT(osd_invariant(obj));
1122 LASSERT(zap_dn != NULL);
1124 LASSERT(th != NULL);
1125 oh = container_of(th, struct osd_thandle, ot_super);
1128 * In Orion . and .. were stored in the directory (not generated upon
1129 * request as now). We preserve them for backward compatibility.
1131 if (name[0] == '.') {
1134 else if (name[1] == '.' && name[2] == 0)
1138 /* do not specify the key as then DMU is trying to look it up
1139 * which is very expensive. usually the layers above lookup
1140 * before deletion */
1141 osd_tx_hold_zap(oh->ot_tx, zap_dn->dn_object, zap_dn, FALSE, NULL);
1143 /* For destroying agent object if have. */
1144 dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1149 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
1150 const struct dt_key *key, struct thandle *th)
1152 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1153 struct osd_object *obj = osd_dt_obj(dt);
1154 struct osd_device *osd = osd_obj2dev(obj);
1155 struct osd_thandle *oh;
1156 dnode_t *zap_dn = obj->oo_dn;
1157 char *name = (char *)key;
1163 LASSERT(th != NULL);
1164 oh = container_of(th, struct osd_thandle, ot_super);
1167 * In Orion . and .. were stored in the directory (not generated upon
1168 * request as now). we preserve them for backward compatibility
1170 if (name[0] == '.') {
1173 } else if (name[1] == '.' && name[2] == 0) {
1178 /* XXX: We have to say that lookup during delete_declare will affect
1179 * performance, but we have to check whether the name entry (to
1180 * be deleted) has agent object or not to avoid orphans.
1182 * We will improve that in the future, some possible solutions,
1184 * 1) Some hint from the caller via transaction handle to make
1185 * the lookup conditionally.
1186 * 2) Enhance the ZFS logic to recognize the OSD lookup result
1187 * and delete the given entry directly without lookup again
1188 * internally. LU-10190 */
1189 memset(&zde->lzd_fid, 0, sizeof(zde->lzd_fid));
1190 rc = osd_zap_lookup(osd, zap_dn->dn_object, zap_dn, name, 8, 3, zde);
1193 CERROR("%s: failed to locate entry %s: rc = %d\n",
1194 osd->od_svname, name, rc);
1198 if (unlikely(osd_remote_fid(env, osd, &zde->lzd_fid) > 0)) {
1199 rc = -dmu_object_free(osd->od_os, zde->lzd_reg.zde_dnode,
1202 CERROR("%s: failed to destroy agent object (%llu) "
1203 "for the entry %s: rc = %d\n", osd->od_svname,
1204 (__u64)zde->lzd_reg.zde_dnode, name, rc);
1207 /* Remove key from the ZAP */
1208 rc = osd_zap_remove(osd, zap_dn->dn_object, zap_dn,
1209 (char *)key, oh->ot_tx);
1211 CERROR("%s: zap_remove %s failed: rc = %d\n",
1212 osd->od_svname, name, rc);
1217 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
1218 struct dt_object *dt,
1221 struct osd_zap_it *it;
1223 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
1225 it->ozi_pos = OZI_POS_INIT;
1227 RETURN((struct dt_it *)it);
1231 * Move Iterator to record specified by \a key
1233 * \param di osd iterator
1234 * \param key key for index
1236 * \retval +ve di points to record with least key not larger than key
1237 * \retval 0 di points to exact matched key
1238 * \retval -ve failure
1240 static int osd_dir_it_get(const struct lu_env *env,
1241 struct dt_it *di, const struct dt_key *key)
1243 struct osd_zap_it *it = (struct osd_zap_it *)di;
1244 struct osd_object *obj = it->ozi_obj;
1245 char *name = (char *)key;
1250 LASSERT(it->ozi_zc);
1252 /* reset the cursor */
1253 zap_cursor_fini(it->ozi_zc);
1254 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
1256 /* XXX: implementation of the API is broken at the moment */
1257 LASSERT(((const char *)key)[0] == 0);
1260 it->ozi_pos = OZI_POS_INIT;
1264 if (name[0] == '.') {
1266 it->ozi_pos = OZI_POS_DOT;
1268 } else if (name[1] == '.' && name[2] == 0) {
1269 it->ozi_pos = OZI_POS_DOTDOT;
1274 /* neither . nor .. - some real record */
1275 it->ozi_pos = OZI_POS_REAL;
1282 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
1284 /* PBS: do nothing : ref are incremented at retrive and decreamented
1289 * in Orion . and .. were stored in the directory, while ZPL
1290 * and current osd-zfs generate them up on request. so, we
1291 * need to ignore previously stored . and ..
1293 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
1294 zap_attribute_t *za)
1299 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1302 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
1303 if (za->za_name[1] == 0) {
1305 } else if (za->za_name[1] == '.' &&
1306 za->za_name[2] == 0) {
1309 if (unlikely(isdot))
1310 zap_cursor_advance(it->ozi_zc);
1312 } while (unlikely(rc == 0 && isdot));
1318 * to load a directory entry at a time and stored it in
1319 * iterator's in-memory data structure.
1321 * \param di, struct osd_it_ea, iterator's in memory structure
1323 * \retval +ve, iterator reached to end
1324 * \retval 0, iterator not reached to end
1325 * \retval -ve, on error
1327 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
1329 struct osd_zap_it *it = (struct osd_zap_it *)di;
1330 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1335 /* temp. storage should be enough for any key supported by ZFS */
1336 BUILD_BUG_ON(sizeof(za->za_name) > sizeof(it->ozi_name));
1339 * the first ->next() moves the cursor to .
1340 * the second ->next() moves the cursor to ..
1341 * then we get to the real records and have to verify any exist
1343 if (it->ozi_pos <= OZI_POS_DOTDOT) {
1345 if (it->ozi_pos <= OZI_POS_DOTDOT)
1349 zap_cursor_advance(it->ozi_zc);
1353 * According to current API we need to return error if its last entry.
1354 * zap_cursor_advance() does not return any value. So we need to call
1355 * retrieve to check if there is any record. We should make
1356 * changes to Iterator API to not return status for this API
1358 rc = osd_index_retrieve_skip_dots(it, za);
1360 if (rc == -ENOENT) /* end of dir */
1366 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
1367 const struct dt_it *di)
1369 struct osd_zap_it *it = (struct osd_zap_it *)di;
1370 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1374 if (it->ozi_pos <= OZI_POS_DOT) {
1375 it->ozi_pos = OZI_POS_DOT;
1376 RETURN((struct dt_key *)".");
1377 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1378 RETURN((struct dt_key *)"..");
1381 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
1382 RETURN(ERR_PTR(rc));
1384 strcpy(it->ozi_name, za->za_name);
1386 RETURN((struct dt_key *)it->ozi_name);
1389 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
1391 struct osd_zap_it *it = (struct osd_zap_it *)di;
1392 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1396 if (it->ozi_pos <= OZI_POS_DOT) {
1397 it->ozi_pos = OZI_POS_DOT;
1399 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1403 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
1404 rc = strlen(za->za_name);
1410 osd_dirent_update(const struct lu_env *env, struct osd_device *dev,
1411 uint64_t zap, const char *key, struct luz_direntry *zde)
1417 tx = dmu_tx_create(dev->od_os);
1421 dmu_tx_hold_zap(tx, zap, TRUE, NULL);
1422 rc = -dmu_tx_assign(tx, TXG_WAIT);
1424 rc = -zap_update(dev->od_os, zap, key, 8, sizeof(*zde) / 8,
1425 (const void *)zde, tx);
1434 static int osd_update_entry_for_agent(const struct lu_env *env,
1435 struct osd_device *osd,
1436 uint64_t zap, const char *name,
1437 struct luz_direntry *zde, __u32 attr)
1439 dmu_tx_t *tx = NULL;
1443 if (attr & LUDA_VERIFY_DRYRUN)
1446 tx = dmu_tx_create(osd->od_os);
1448 GOTO(out, rc = -ENOMEM);
1450 dmu_tx_hold_sa_create(tx, osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
1451 dmu_tx_hold_zap(tx, zap, FALSE, NULL);
1452 rc = -dmu_tx_assign(tx, TXG_WAIT);
1458 rc = osd_create_agent_object(env, osd, zde, zap, tx);
1460 rc = -zap_update(osd->od_os, zap, name, 8, sizeof(*zde) / 8,
1461 (const void *)zde, tx);
1467 CDEBUG(D_LFSCK, "%s: Updated (%s) remote entry for "DFID": rc = %d\n",
1468 osd_name(osd), (attr & LUDA_VERIFY_DRYRUN) ? "(ro)" : "(rw)",
1469 PFID(&zde->lzd_fid), rc);
1473 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
1474 struct dt_rec *dtrec, __u32 attr)
1476 struct osd_zap_it *it = (struct osd_zap_it *)di;
1477 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
1478 struct osd_thread_info *info = osd_oti_get(env);
1479 struct luz_direntry *zde = &info->oti_zde;
1480 zap_attribute_t *za = &info->oti_za;
1481 struct lu_fid *fid = &info->oti_fid;
1482 struct osd_device *osd = osd_obj2dev(it->ozi_obj);
1487 if (it->ozi_pos <= OZI_POS_DOT) {
1488 /* notice hash=0 here, this is needed to avoid
1489 * case when some real entry (after ./..) may
1490 * have hash=0. in this case the client would
1491 * be confused having records out of hash order. */
1492 lde->lde_hash = cpu_to_le64(0);
1493 strcpy(lde->lde_name, ".");
1494 lde->lde_namelen = cpu_to_le16(1);
1495 fid_cpu_to_le(&lde->lde_fid,
1496 lu_object_fid(&it->ozi_obj->oo_dt.do_lu));
1497 lde->lde_attrs = LUDA_FID;
1498 /* append lustre attributes */
1499 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
1500 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
1501 it->ozi_pos = OZI_POS_DOT;
1503 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1504 /* same as for . above */
1505 lde->lde_hash = cpu_to_le64(0);
1506 strcpy(lde->lde_name, "..");
1507 lde->lde_namelen = cpu_to_le16(2);
1508 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, fid, NULL);
1510 fid_cpu_to_le(&lde->lde_fid, fid);
1511 lde->lde_attrs = LUDA_FID;
1512 } else if (rc != -ENOENT) {
1513 /* ENOENT happens at the root of filesystem, ignore */
1517 /* append lustre attributes */
1518 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
1519 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
1525 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1529 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
1530 namelen = strlen(za->za_name);
1531 if (namelen > NAME_MAX)
1533 strcpy(lde->lde_name, za->za_name);
1534 lde->lde_namelen = cpu_to_le16(namelen);
1536 if (za->za_integer_length != 8) {
1537 CERROR("%s: unsupported direntry format: %d %d\n",
1539 za->za_integer_length, (int)za->za_num_integers);
1543 rc = osd_zap_lookup(osd, it->ozi_zc->zc_zapobj, it->ozi_obj->oo_dn,
1544 za->za_name, za->za_integer_length, 3, zde);
1548 if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid)) {
1549 lde->lde_attrs = LUDA_FID;
1550 fid_cpu_to_le(&lde->lde_fid, &zde->lzd_fid);
1551 if (unlikely(zde->lzd_reg.zde_dnode == ZFS_NO_OBJECT &&
1552 osd_remote_fid(env, osd, &zde->lzd_fid) > 0 &&
1553 attr & LUDA_VERIFY)) {
1554 /* It is mainly used for handling the MDT
1555 * upgraded from old ZFS based backend. */
1556 rc = osd_update_entry_for_agent(env, osd,
1557 it->ozi_obj->oo_dn->dn_object,
1558 za->za_name, zde, attr);
1560 lde->lde_attrs |= LUDA_REPAIR;
1562 lde->lde_attrs |= LUDA_UNKNOWN;
1565 GOTO(pack_attr, rc = 0);
1568 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
1571 rc = osd_get_fid_by_oid(env, osd, zde->lzd_reg.zde_dnode, fid);
1573 lde->lde_attrs = LUDA_UNKNOWN;
1574 GOTO(pack_attr, rc = 0);
1577 if (!(attr & LUDA_VERIFY)) {
1578 fid_cpu_to_le(&lde->lde_fid, fid);
1579 lde->lde_attrs = LUDA_FID;
1580 GOTO(pack_attr, rc = 0);
1583 if (attr & LUDA_VERIFY_DRYRUN) {
1584 fid_cpu_to_le(&lde->lde_fid, fid);
1585 lde->lde_attrs = LUDA_FID | LUDA_REPAIR;
1586 GOTO(pack_attr, rc = 0);
1589 fid_cpu_to_le(&lde->lde_fid, fid);
1590 lde->lde_attrs = LUDA_FID;
1591 zde->lzd_fid = *fid;
1592 rc = osd_dirent_update(env, osd, it->ozi_zc->zc_zapobj,
1595 lde->lde_attrs |= LUDA_UNKNOWN;
1596 GOTO(pack_attr, rc = 0);
1599 lde->lde_attrs |= LUDA_REPAIR;
1601 GOTO(pack_attr, rc = 0);
1604 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1605 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1609 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1612 struct osd_zap_it *it = (struct osd_zap_it *)di;
1613 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1618 if (it->ozi_pos <= OZI_POS_DOT)
1620 else if (it->ozi_pos == OZI_POS_DOTDOT)
1624 rc = lu_dirent_calc_size(namelen, attr);
1628 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1629 if (unlikely(rc != 0))
1632 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1633 CERROR("%s: unsupported direntry format: %d %d\n",
1634 osd_obj2dev(it->ozi_obj)->od_svname,
1635 za->za_integer_length, (int)za->za_num_integers);
1639 namelen = strlen(za->za_name);
1640 if (namelen > NAME_MAX)
1643 rc = lu_dirent_calc_size(namelen, attr);
1648 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1650 struct osd_zap_it *it = (struct osd_zap_it *)di;
1654 if (it->ozi_pos <= OZI_POS_DOTDOT)
1657 pos = osd_zap_cursor_serialize(it->ozi_zc);
1664 * rc == 0 -> end of directory.
1665 * rc > 0 -> ok, proceed.
1666 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1668 static int osd_dir_it_load(const struct lu_env *env,
1669 const struct dt_it *di, __u64 hash)
1671 struct osd_zap_it *it = (struct osd_zap_it *)di;
1672 struct osd_object *obj = it->ozi_obj;
1673 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1677 /* reset the cursor */
1678 zap_cursor_fini(it->ozi_zc);
1679 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1682 it->ozi_pos = OZI_POS_INIT;
1683 rc = +1; /* there will be ./.. at least */
1685 it->ozi_pos = OZI_POS_REAL;
1686 /* to return whether the end has been reached */
1687 rc = osd_index_retrieve_skip_dots(it, za);
1690 else if (rc == -ENOENT)
1697 struct dt_index_operations osd_dir_ops = {
1698 .dio_lookup = osd_dir_lookup,
1699 .dio_declare_insert = osd_declare_dir_insert,
1700 .dio_insert = osd_dir_insert,
1701 .dio_declare_delete = osd_declare_dir_delete,
1702 .dio_delete = osd_dir_delete,
1704 .init = osd_dir_it_init,
1705 .fini = osd_index_it_fini,
1706 .get = osd_dir_it_get,
1707 .put = osd_dir_it_put,
1708 .next = osd_dir_it_next,
1709 .key = osd_dir_it_key,
1710 .key_size = osd_dir_it_key_size,
1711 .rec = osd_dir_it_rec,
1712 .rec_size = osd_dir_it_rec_size,
1713 .store = osd_dir_it_store,
1714 .load = osd_dir_it_load
1719 * Primitives for index files using binary keys.
1722 /* key integer_size is 8 */
1723 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1724 const struct dt_key *src)
1731 /* align keysize to 64bit */
1732 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1733 size *= sizeof(__u64);
1735 LASSERT(size <= MAXNAMELEN);
1737 if (unlikely(size > o->oo_keysize))
1738 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1739 memcpy(dst, (const char *)src, o->oo_keysize);
1741 return (size/sizeof(__u64));
1744 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1745 struct dt_rec *rec, const struct dt_key *key)
1747 struct osd_object *obj = osd_dt_obj(dt);
1748 struct osd_device *osd = osd_obj2dev(obj);
1749 __u64 *k = osd_oti_get(env)->oti_key64;
1753 rc = osd_prepare_key_uint64(obj, k, key);
1755 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1756 k, rc, obj->oo_recusize, obj->oo_recsize,
1758 RETURN(rc == 0 ? 1 : rc);
1761 static int osd_declare_index_insert(const struct lu_env *env,
1762 struct dt_object *dt,
1763 const struct dt_rec *rec,
1764 const struct dt_key *key,
1767 struct osd_object *obj = osd_dt_obj(dt);
1768 struct osd_thandle *oh;
1771 LASSERT(th != NULL);
1772 oh = container_of(th, struct osd_thandle, ot_super);
1774 LASSERT(obj->oo_dn);
1776 /* do not specify the key as then DMU is trying to look it up
1777 * which is very expensive. usually the layers above lookup
1778 * before insertion */
1779 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1785 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1786 const struct dt_rec *rec, const struct dt_key *key,
1789 struct osd_object *obj = osd_dt_obj(dt);
1790 struct osd_device *osd = osd_obj2dev(obj);
1791 struct osd_thandle *oh;
1792 __u64 *k = osd_oti_get(env)->oti_key64;
1796 LASSERT(obj->oo_dn);
1797 LASSERT(dt_object_exists(dt));
1798 LASSERT(osd_invariant(obj));
1799 LASSERT(th != NULL);
1801 oh = container_of(th, struct osd_thandle, ot_super);
1803 rc = osd_prepare_key_uint64(obj, k, key);
1805 /* Insert (key,oid) into ZAP */
1806 rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1807 k, rc, obj->oo_recusize, obj->oo_recsize,
1808 (void *)rec, oh->ot_tx);
1812 static int osd_declare_index_delete(const struct lu_env *env,
1813 struct dt_object *dt,
1814 const struct dt_key *key,
1817 struct osd_object *obj = osd_dt_obj(dt);
1818 struct osd_thandle *oh;
1821 LASSERT(dt_object_exists(dt));
1822 LASSERT(osd_invariant(obj));
1823 LASSERT(th != NULL);
1824 LASSERT(obj->oo_dn);
1826 oh = container_of(th, struct osd_thandle, ot_super);
1828 /* do not specify the key as then DMU is trying to look it up
1829 * which is very expensive. usually the layers above lookup
1830 * before deletion */
1831 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1837 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1838 const struct dt_key *key, struct thandle *th)
1840 struct osd_object *obj = osd_dt_obj(dt);
1841 struct osd_device *osd = osd_obj2dev(obj);
1842 struct osd_thandle *oh;
1843 __u64 *k = osd_oti_get(env)->oti_key64;
1847 LASSERT(obj->oo_dn);
1848 LASSERT(th != NULL);
1849 oh = container_of(th, struct osd_thandle, ot_super);
1851 rc = osd_prepare_key_uint64(obj, k, key);
1853 /* Remove binary key from the ZAP */
1854 rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1859 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1860 const struct dt_key *key)
1862 struct osd_zap_it *it = (struct osd_zap_it *)di;
1863 struct osd_object *obj = it->ozi_obj;
1864 struct osd_device *osd = osd_obj2dev(obj);
1868 LASSERT(it->ozi_zc);
1871 * XXX: we need a binary version of zap_cursor_move_to_key()
1872 * to implement this API */
1873 if (*((const __u64 *)key) != 0)
1874 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1877 zap_cursor_fini(it->ozi_zc);
1878 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1884 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1886 struct osd_zap_it *it = (struct osd_zap_it *)di;
1887 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1891 if (it->ozi_reset == 0)
1892 zap_cursor_advance(it->ozi_zc);
1896 * According to current API we need to return error if it's last entry.
1897 * zap_cursor_advance() does not return any value. So we need to call
1898 * retrieve to check if there is any record. We should make
1899 * changes to Iterator API to not return status for this API
1901 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1908 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1909 const struct dt_it *di)
1911 struct osd_zap_it *it = (struct osd_zap_it *)di;
1912 struct osd_object *obj = it->ozi_obj;
1913 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1918 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1920 RETURN(ERR_PTR(rc));
1922 /* the binary key is stored in the name */
1923 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1925 RETURN((struct dt_key *)&it->ozi_key);
1928 static int osd_index_it_key_size(const struct lu_env *env,
1929 const struct dt_it *di)
1931 struct osd_zap_it *it = (struct osd_zap_it *)di;
1932 struct osd_object *obj = it->ozi_obj;
1933 RETURN(obj->oo_keysize);
1936 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1937 struct dt_rec *rec, __u32 attr)
1939 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1940 struct osd_zap_it *it = (struct osd_zap_it *)di;
1941 struct osd_object *obj = it->ozi_obj;
1942 struct osd_device *osd = osd_obj2dev(obj);
1943 __u64 *k = osd_oti_get(env)->oti_key64;
1948 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1952 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1954 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1955 k, rc, obj->oo_recusize, obj->oo_recsize,
1960 static __u64 osd_index_it_store(const struct lu_env *env,
1961 const struct dt_it *di)
1963 struct osd_zap_it *it = (struct osd_zap_it *)di;
1966 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1969 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1972 struct osd_zap_it *it = (struct osd_zap_it *)di;
1973 struct osd_object *obj = it->ozi_obj;
1974 struct osd_device *osd = osd_obj2dev(obj);
1975 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1979 /* reset the cursor */
1980 zap_cursor_fini(it->ozi_zc);
1981 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1982 obj->oo_dn->dn_object, hash);
1985 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1988 else if (rc == -ENOENT)
1994 static struct dt_index_operations osd_index_ops = {
1995 .dio_lookup = osd_index_lookup,
1996 .dio_declare_insert = osd_declare_index_insert,
1997 .dio_insert = osd_index_insert,
1998 .dio_declare_delete = osd_declare_index_delete,
1999 .dio_delete = osd_index_delete,
2001 .init = osd_index_it_init,
2002 .fini = osd_index_it_fini,
2003 .get = osd_index_it_get,
2004 .put = osd_index_it_put,
2005 .next = osd_index_it_next,
2006 .key = osd_index_it_key,
2007 .key_size = osd_index_it_key_size,
2008 .rec = osd_index_it_rec,
2009 .store = osd_index_it_store,
2010 .load = osd_index_it_load
2014 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2015 const struct dt_index_features *feat)
2017 struct osd_object *obj = osd_dt_obj(dt);
2018 struct osd_device *osd = osd_obj2dev(obj);
2019 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2023 down_read(&obj->oo_guard);
2026 * XXX: implement support for fixed-size keys sorted with natural
2027 * numerical way (not using internal hash value)
2029 if (feat->dif_flags & DT_IND_RANGE)
2030 GOTO(out, rc = -ERANGE);
2032 if (unlikely(feat == &dt_otable_features)) {
2033 dt->do_index_ops = &osd_otable_ops;
2037 LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
2038 if (likely(feat == &dt_directory_features)) {
2039 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
2040 dt->do_index_ops = &osd_dir_ops;
2042 GOTO(out, rc = -ENOTDIR);
2043 } else if (unlikely(feat == &dt_acct_features)) {
2044 LASSERT(fid_is_acct(fid));
2045 dt->do_index_ops = &osd_acct_index_ops;
2046 } else if (dt->do_index_ops == NULL) {
2047 /* For index file, we don't support variable key & record sizes
2048 * and the key has to be unique */
2049 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
2050 GOTO(out, rc = -EINVAL);
2052 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
2053 GOTO(out, rc = -E2BIG);
2054 if (feat->dif_keysize_max != feat->dif_keysize_min)
2055 GOTO(out, rc = -EINVAL);
2057 /* As for the record size, it should be a multiple of 8 bytes
2058 * and smaller than the maximum value length supported by ZAP.
2060 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
2061 GOTO(out, rc = -E2BIG);
2062 if (feat->dif_recsize_max != feat->dif_recsize_min)
2063 GOTO(out, rc = -EINVAL);
2065 obj->oo_keysize = feat->dif_keysize_max;
2066 obj->oo_recsize = feat->dif_recsize_max;
2067 obj->oo_recusize = 1;
2069 /* ZFS prefers to work with array of 64bits */
2070 if ((obj->oo_recsize & 7) == 0) {
2071 obj->oo_recsize >>= 3;
2072 obj->oo_recusize = 8;
2074 dt->do_index_ops = &osd_index_ops;
2076 if (feat == &dt_lfsck_layout_orphan_features ||
2077 feat == &dt_lfsck_layout_dangling_features ||
2078 feat == &dt_lfsck_namespace_features)
2081 rc = osd_index_register(osd, fid, obj->oo_keysize,
2082 obj->oo_recusize * obj->oo_recsize);
2084 CWARN("%s: failed to register index "DFID": rc = %d\n",
2085 osd_name(osd), PFID(fid), rc);
2089 CDEBUG(D_LFSCK, "%s: index object "DFID
2090 " (%u/%u/%u) registered\n",
2091 osd_name(osd), PFID(fid), obj->oo_keysize,
2092 obj->oo_recusize, obj->oo_recsize);
2096 up_read(&obj->oo_guard);