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
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);
122 osd_zap_cursor_init_serialized(zc, d->od_os,
123 o->oo_dn->dn_object, dirhash);
126 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
129 struct osd_device *d = osd_obj2dev(o);
131 return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
134 static struct dt_it *osd_index_it_init(const struct lu_env *env,
135 struct dt_object *dt,
138 struct osd_thread_info *info = osd_oti_get(env);
139 struct osd_zap_it *it;
140 struct osd_object *obj = osd_dt_obj(dt);
141 struct lu_object *lo = &dt->do_lu;
145 if (obj->oo_destroyed)
146 RETURN(ERR_PTR(-ENOENT));
148 LASSERT(lu_object_exists(lo));
152 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
154 RETURN(ERR_PTR(-ENOMEM));
156 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
158 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
166 RETURN((struct dt_it *)it);
169 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
171 struct osd_zap_it *it = (struct osd_zap_it *)di;
172 struct osd_object *obj;
176 LASSERT(it->ozi_obj);
180 osd_zap_cursor_fini(it->ozi_zc);
181 osd_object_put(env, obj);
182 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
188 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
190 /* PBS: do nothing : ref are incremented at retrive and decreamented
195 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
198 const unsigned int align = sizeof(struct luda_type) - 1;
199 struct luda_type *lt;
201 /* check if file type is required */
202 if (attr & LUDA_TYPE) {
203 len = (len + align) & ~align;
205 lt = (void *)ent->lde_name + len;
206 lt->lt_type = cpu_to_le16(DTTOIF(type));
207 ent->lde_attrs |= LUDA_TYPE;
210 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
213 int __osd_xattr_load_by_oid(struct osd_device *osd, uint64_t oid, nvlist_t **sa)
219 rc = -dmu_bonus_hold(osd->od_os, oid, osd_obj_tag, &db);
221 CERROR("%s: can't get bonus, rc = %d\n", osd->od_svname, rc);
225 rc = -sa_handle_get_from_db(osd->od_os, db, NULL, SA_HDL_PRIVATE, &hdl);
227 dmu_buf_rele(db, osd_obj_tag);
231 rc = __osd_xattr_load(osd, hdl, sa);
233 sa_handle_destroy(hdl);
238 * Get the object's FID from its LMA EA.
240 * \param[in] env pointer to the thread context
241 * \param[in] osd pointer to the OSD device
242 * \param[in] oid the object's local identifier
243 * \param[out] fid the buffer to hold the object's FID
245 * \retval 0 for success
246 * \retval negative error number on failure
248 int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
249 uint64_t oid, struct lu_fid *fid)
251 struct objset *os = osd->od_os;
252 struct osd_thread_info *oti = osd_oti_get(env);
253 struct lustre_mdt_attrs *lma =
254 (struct lustre_mdt_attrs *)oti->oti_buf;
256 nvlist_t *sa_xattr = NULL;
257 sa_handle_t *sa_hdl = NULL;
258 uchar_t *nv_value = NULL;
259 uint64_t xattr = ZFS_NO_OBJECT;
264 rc = __osd_xattr_load_by_oid(osd, oid, &sa_xattr);
271 rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
279 if (unlikely(size > sizeof(oti->oti_buf)))
280 GOTO(out, rc = -ERANGE);
282 memcpy(lma, nv_value, size);
287 rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
291 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
292 sa_handle_destroy(sa_hdl);
297 buf.lb_len = sizeof(oti->oti_buf);
298 rc = __osd_xattr_get_large(env, osd, xattr, &buf,
299 XATTR_NAME_LMA, &size);
304 if (size < sizeof(*lma))
305 GOTO(out, rc = -EIO);
307 lustre_lma_swab(lma);
308 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
309 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
311 CWARN("%s: unsupported incompat LMA feature(s) %#x for oid = %#llx: rc = %d\n",
312 osd->od_svname, lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
316 *fid = lma->lma_self_fid;
321 if (sa_xattr != NULL)
322 nvlist_free(sa_xattr);
327 * As we don't know FID, we can't use LU object, so this function
328 * partially duplicate osd_xattr_get_internal() which is built around
329 * LU-object and uses it to cache data like regular EA dnode, etc
331 static int osd_find_parent_by_dnode(const struct lu_env *env,
333 struct lu_fid *fid, uint64_t *oid)
335 struct osd_object *obj = osd_dt_obj(o);
336 struct osd_device *osd = osd_obj2dev(obj);
337 uint64_t dnode = ZFS_NO_OBJECT;
341 /* first of all, get parent dnode from own attributes */
342 rc = osd_sa_handle_get(obj);
345 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
349 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
355 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
356 struct lu_fid *fid, uint64_t *oid)
358 struct link_ea_header *leh;
359 struct link_ea_entry *lee;
364 buf.lb_buf = osd_oti_get(env)->oti_buf;
365 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
367 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
369 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
373 OBD_ALLOC(buf.lb_buf, rc);
374 if (buf.lb_buf == NULL)
377 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
381 if (rc < sizeof(*leh) + sizeof(*lee))
382 GOTO(out, rc = -EINVAL);
385 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
386 leh->leh_magic = LINK_EA_MAGIC;
387 leh->leh_reccount = __swab32(leh->leh_reccount);
388 leh->leh_len = __swab64(leh->leh_len);
390 if (leh->leh_magic != LINK_EA_MAGIC)
391 GOTO(out, rc = -EINVAL);
392 if (leh->leh_reccount == 0)
393 GOTO(out, rc = -ENODATA);
395 lee = (struct link_ea_entry *)(leh + 1);
396 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
400 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
401 OBD_FREE(buf.lb_buf, buf.lb_len);
404 /* this block can be enabled for additional verification
405 * it's trying to match FID from LinkEA vs. FID from LMA
412 rc2 = osd_find_parent_by_dnode(env, o, &fid2, oid);
414 if (lu_fid_eq(fid, &fid2) == 0)
415 CERROR("wrong parent: "DFID" != "DFID"\n",
416 PFID(fid), PFID(&fid2));
420 /* no LinkEA is found, let's try to find the fid in parent's LMA */
421 if (unlikely(rc != 0))
422 rc = osd_find_parent_by_dnode(env, o, fid, oid);
428 * When lookup item under striped directory, we need to locate the master
429 * MDT-object of the striped directory firstly, then the client will send
430 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
431 * and the item's name. If the system is restored from MDT file level backup,
432 * then before the OI scrub completely built the OI files, the OI mappings of
433 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
434 * not a problem for the master MDT-object. Because when locate the master
435 * MDT-object, we will do name based lookup (for the striped directory itself)
436 * firstly, during such process we can setup the correct OI mapping for the
437 * master MDT-object. But it will be trouble for the slave MDT-object. Because
438 * the client will not trigger name based lookup on the MDT to locate the slave
439 * MDT-object before locating item under the striped directory, then when
440 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
441 * is invalid and does not know what the right OI mapping is, then the MDT has
442 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
443 * the OI file, related OI mapping is unknown yet, please try again later. And
444 * then client will re-try the RPC again and again until related OI mapping has
445 * been updated. That is quite inefficient.
447 * To resolve above trouble, we will handle it as the following two cases:
449 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
450 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
451 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
452 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
453 * directly. Please check osd_fid_lookup().
455 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
456 * Under such case, during lookup the master MDT-object, we will lookup the
457 * slave MDT-object via readdir against the master MDT-object, because the
458 * slave MDT-objects information are stored as sub-directories with the name
459 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
460 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
461 * the correct OI mapping for the slave MDT-object.
463 static int osd_check_lmv(const struct lu_env *env, struct osd_device *osd,
464 uint64_t oid, const struct lu_fid *fid)
466 struct osd_thread_info *info = osd_oti_get(env);
467 struct luz_direntry *zde = &info->oti_zde;
468 zap_attribute_t *za = &info->oti_za;
469 zap_cursor_t *zc = &info->oti_zc;
470 struct lu_fid *tfid = &info->oti_fid;
471 nvlist_t *nvbuf = NULL;
472 struct lmv_mds_md_v1 *lmv = NULL;
477 rc = __osd_xattr_load_by_oid(osd, oid, &nvbuf);
478 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
484 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMV,
485 (uchar_t **)&lmv, &size);
486 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
487 GOTO(out_nvbuf, rc = 0);
492 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
493 GOTO(out_nvbuf, rc = -EINVAL);
495 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
496 rc = -zap_cursor_retrieve(zc, za);
498 zap_cursor_advance(zc);
500 CERROR("%s: fail to init for check LMV "DFID"(%llu): rc = %d\n",
501 osd_name(osd), PFID(fid), oid, rc);
506 rc = -zap_cursor_retrieve(zc, za);
508 GOTO(out_zc, rc = 0);
511 CERROR("%s: fail to locate next for check LMV "DFID"(%llu): rc = %d\n",
512 osd_name(osd), PFID(fid), oid, rc);
517 sscanf(za->za_name + 1, SFID, RFID(tfid));
518 if (fid_is_sane(tfid) && !osd_remote_fid(env, osd, tfid)) {
519 rc = osd_zap_lookup(osd, oid, NULL, za->za_name,
520 za->za_integer_length,
521 sizeof(*zde) / za->za_integer_length,
524 CERROR("%s: fail to lookup for check LMV "DFID"(%llu): rc = %d\n",
525 osd_name(osd), PFID(fid), oid, rc);
529 rc = osd_oii_insert(env, osd, tfid,
530 zde->lzd_reg.zde_dnode, false);
534 zap_cursor_advance(zc);
546 osd_zfs_consistency_check(const struct lu_env *env, struct osd_device *osd,
547 struct osd_object *obj, const struct lu_fid *fid,
548 u64 oid, bool is_dir)
550 struct lustre_scrub *scrub = &osd->od_scrub;
558 /* oid == ZFS_NO_OBJECT must be for lookup ".." case */
559 if (oid == ZFS_NO_OBJECT) {
560 rc = osd_sa_handle_get(obj);
564 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &oid, 8);
569 if (!scrub_needs_check(&osd->od_scrub, fid, oid))
572 rc = osd_fid_lookup(env, osd, fid, &oid2);
578 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
579 /* The object has been removed (by race maybe). */
584 } else if (rc || oid == oid2) {
591 if (scrub->os_running) {
593 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
594 /* The object has been removed (by race maybe). */
599 rc = osd_oii_insert(env, osd, fid, oid, insert);
600 /* There is race condition between osd_oi_lookup and OI scrub.
601 * The OI scrub finished just after osd_oi_lookup() failure.
602 * Under such case, it is unnecessary to trigger OI scrub again,
603 * but try to call osd_oi_lookup() again.
605 if (unlikely(rc == -EAGAIN))
609 rc = osd_check_lmv(env, osd, oid, fid);
616 if (osd->od_scrub.os_auto_scrub_interval != AS_NEVER && ++once == 1) {
617 rc = osd_scrub_start(env, osd, SS_AUTO_FULL |
618 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
619 CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
620 "%s: trigger partial OI scrub for RPC inconsistency, checking FID "DFID"/%#llx): rc = %d\n",
621 osd_name(osd), PFID(fid), oid, rc);
633 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
634 struct dt_rec *rec, const struct dt_key *key)
636 struct osd_thread_info *oti = osd_oti_get(env);
637 struct osd_object *obj = osd_dt_obj(dt);
638 struct osd_device *osd = osd_obj2dev(obj);
639 struct lu_fid *fid = (struct lu_fid *)rec;
640 char *name = (char *)key;
641 uint64_t oid = ZFS_NO_OBJECT;
645 if (name[0] == '.') {
647 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
649 memcpy(rec, f, sizeof(*f));
651 } else if (name[1] == '.' && name[2] == 0) {
652 rc = osd_find_parent_fid(env, dt, fid, &oid);
657 memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
659 down_read(&obj->oo_guard);
660 if (obj->oo_destroyed)
661 GOTO(out_unlock, rc = -ENOENT);
663 rc = osd_zap_lookup(osd, obj->oo_dn->dn_object, obj->oo_dn,
664 (char *)key, 8, sizeof(oti->oti_zde) / 8,
665 (void *)&oti->oti_zde);
667 up_read(&obj->oo_guard);
671 oid = oti->oti_zde.lzd_reg.zde_dnode;
672 if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
673 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
674 GOTO(out_unlock, rc = 0);
677 rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode, fid);
680 up_read(&obj->oo_guard);
682 if (!rc && !osd_remote_fid(env, osd, fid)) {
683 bool is_dir = S_ISDIR(DTTOIF(oti->oti_zde.lzd_reg.zde_type));
686 * this should ask the scrubber to check OI given
687 * the mapping we just found in the dir entry.
688 * but result of that check should not affect
689 * result of the lookup in the directory.
690 * otherwise such a direntry becomes hidden
691 * from the layers above, including LFSCK which
692 * is supposed to fix dangling entries.
694 osd_zfs_consistency_check(env, osd, obj, fid, oid, is_dir);
697 return rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc);
701 * In DNE environment, the object and its name entry may reside on different
702 * MDTs. Under such case, we will create an agent object on the MDT where the
703 * name entry resides. The agent object is empty, and indicates that the real
704 * object for the name entry resides on another MDT. If without agent object,
705 * related name entry will be skipped when perform MDT side file level backup
706 * and restore via ZPL by userspace tool, such as 'tar'.
708 static int osd_create_agent_object(const struct lu_env *env,
709 struct osd_device *osd,
710 struct luz_direntry *zde,
711 uint64_t parent, dmu_tx_t *tx)
713 struct osd_thread_info *info = osd_oti_get(env);
714 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
715 struct lu_attr *la = &info->oti_la;
716 nvlist_t *nvbuf = NULL;
722 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTOBJ))
725 rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
729 lustre_lma_init(lma, &zde->lzd_fid, 0, LMAI_AGENT);
730 lustre_lma_swab(lma);
731 rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA, (uchar_t *)lma,
736 la->la_valid = LA_TYPE | LA_MODE;
737 la->la_mode = (DTTOIF(zde->lzd_reg.zde_type) & S_IFMT) | 0755;
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 (CFS_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 "DFID" to remote parent: rc = %d\n",
796 osd_name(osd), PFID(fid), rc);
800 lustre_lma_swab(lma);
801 lma->lma_incompat |= LMAI_REMOTE_PARENT;
802 lustre_lma_swab(lma);
804 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
805 LU_XATTR_REPLACE, oh);
807 CWARN("%s: fail to update LMA for adding "DFID" to remote parent: rc = %d\n",
808 osd_name(osd), PFID(fid), rc);
812 osd_fid2str(name, fid, sizeof(info->oti_str));
813 zde->lzd_reg.zde_dnode = obj->oo_dn->dn_object;
814 zde->lzd_reg.zde_type = S_DT(S_IFDIR);
817 rc = osd_zap_add(osd, osd->od_remote_parent_dir, NULL,
818 name, 8, sizeof(*zde) / 8, zde, oh->ot_tx);
819 if (unlikely(rc == -EEXIST))
822 CWARN("%s: fail to add name entry for "
823 DFID" to remote parent: rc = %d\n",
824 osd_name(osd), PFID(fid), rc);
826 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
831 int osd_delete_from_remote_parent(const struct lu_env *env,
832 struct osd_device *osd,
833 struct osd_object *obj,
834 struct osd_thandle *oh, bool destroy)
836 struct osd_thread_info *info = osd_oti_get(env);
837 char *name = info->oti_str;
838 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
839 struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
840 struct lu_buf buf = {
842 .lb_len = sizeof(info->oti_buf),
848 osd_fid2str(name, fid, sizeof(info->oti_str));
849 rc = osd_zap_remove(osd, osd->od_remote_parent_dir, NULL,
851 if (unlikely(rc == -ENOENT))
854 CERROR("%s: fail to remove entry under remote parent for "DFID": rc = %d\n",
855 osd_name(osd), PFID(fid), rc);
860 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
862 CERROR("%s: fail to load LMA for removing "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 "DFID" from remote parent: rc = %d\n",
875 osd_name(osd), PFID(fid), rc);
877 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
882 static int osd_declare_dir_insert(const struct lu_env *env,
883 struct dt_object *dt,
884 const struct dt_rec *rec,
885 const struct dt_key *key,
888 struct osd_object *obj = osd_dt_obj(dt);
889 struct osd_device *osd = osd_obj2dev(obj);
890 const struct dt_insert_rec *rec1;
891 const struct lu_fid *fid;
892 struct osd_thandle *oh;
894 struct osd_idmap_cache *idc;
897 rec1 = (struct dt_insert_rec *)rec;
899 LASSERT(fid != NULL);
900 LASSERT(rec1->rec_type != 0);
903 oh = container_of(th, struct osd_thandle, ot_super);
905 idc = osd_idc_find_or_init(env, osd, fid);
907 RETURN(PTR_ERR(idc));
909 if (idc->oic_remote) {
910 const char *name = (const char *)key;
912 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
913 /* Prepare agent object for remote entry that will
914 * be used for operations via ZPL, such as MDT side
915 * file-level backup and restore.
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
935 osd_tx_hold_zap(oh->ot_tx, object, obj->oo_dn, TRUE, NULL);
940 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
943 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
944 struct seq_server_site *ss = osd_seq_site(osd);
949 LASSERT(ss->ss_server_fld != NULL);
951 rc = osd_fld_lookup(env, osd, seq, range);
954 CERROR("%s: Can not lookup fld for %#llx: rc = %d\n",
955 osd_name(osd), seq, rc);
959 RETURN(ss->ss_node_id == range->lsr_index);
962 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
963 const struct lu_fid *fid)
965 struct seq_server_site *ss = osd_seq_site(osd);
968 /* FID seqs not in FLDB, must be local seq */
969 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
972 /* If FLD is not being initialized yet, it only happens during the
973 * initialization, likely during mgs initialization, and we assume
976 if (ss == NULL || ss->ss_server_fld == NULL)
979 /* Only check the local FLDB here */
980 if (osd_seq_exists(env, osd, fid_seq(fid)))
987 * Inserts (key, value) pair in \a directory object.
989 * \param dt osd index object
990 * \param key key for index
991 * \param rec record reference
992 * \param th transaction handler
995 * \retval -ve failure
997 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
998 const struct dt_rec *rec, const struct dt_key *key,
1001 struct osd_thread_info *oti = osd_oti_get(env);
1002 struct osd_object *parent = osd_dt_obj(dt);
1003 struct osd_device *osd = osd_obj2dev(parent);
1004 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
1005 const struct lu_fid *fid = rec1->rec_fid;
1006 struct osd_thandle *oh;
1007 struct osd_idmap_cache *idc;
1008 const char *name = (const char *)key;
1009 struct luz_direntry *zde = &oti->oti_zde;
1010 int num = sizeof(*zde) / 8;
1014 LASSERT(parent->oo_dn);
1016 LASSERT(dt_object_exists(dt));
1017 LASSERT(osd_invariant(parent));
1019 LASSERT(th != NULL);
1020 oh = container_of(th, struct osd_thandle, ot_super);
1022 idc = osd_idc_find(env, osd, fid);
1023 if (unlikely(idc == NULL)) {
1024 /* this dt_insert() wasn't declared properly, so
1025 * FID is missing in OI cache. we better do not
1026 * lookup FID in FLDB/OI and don't risk to deadlock,
1027 * but in some special cases (lfsck testing, etc)
1028 * it's much simpler than fixing a caller
1030 idc = osd_idc_find_or_init(env, osd, fid);
1032 CERROR("%s: "DFID" wasn't declared for insert\n",
1033 osd_name(osd), PFID(fid));
1034 RETURN(PTR_ERR(idc));
1038 BUILD_BUG_ON(sizeof(zde->lzd_reg) != 8);
1039 BUILD_BUG_ON(sizeof(*zde) % 8 != 0);
1041 memset(&zde->lzd_reg, 0, sizeof(zde->lzd_reg));
1042 zde->lzd_reg.zde_type = S_DT(rec1->rec_type & S_IFMT);
1043 zde->lzd_fid = *fid;
1045 if (idc->oic_remote) {
1046 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
1047 /* Create agent inode for remote object that will
1048 * be used for MDT file-level backup and restore.
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 "DFID": rc = %d\n",
1054 osd_name(osd), PFID(fid), rc);
1055 /* Ignore the failure since the system can go
1056 * ahead if we do not care about the MDT side
1057 * file-level backup and restore.
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
1075 } else if (name[1] == '.' && name[2] == 0) {
1076 uint64_t dnode = idc->oic_dnode;
1078 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
1081 /* update parent dnode in the child.
1082 * later it will be used to generate ".."
1084 rc = osd_object_sa_update(parent,
1091 zde->lzd_reg.zde_dnode = idc->oic_dnode;
1094 if (CFS_FAIL_CHECK(OBD_FAIL_FID_INDIR))
1095 zde->lzd_fid.f_ver = ~0;
1097 /* The logic is not related with IGIF, just re-use the fail_loc value
1098 * to be consistent with ldiskfs case, then share the same test logic
1100 if (CFS_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1103 /* Insert (key,oid) into ZAP */
1104 rc = osd_zap_add(osd, parent->oo_dn->dn_object, parent->oo_dn,
1105 name, 8, num, (void *)zde, oh->ot_tx);
1106 if (unlikely(rc == -EEXIST &&
1107 name[0] == '.' && name[1] == '.' && name[2] == 0))
1108 /* Update (key,oid) in ZAP */
1109 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object, name, 8,
1110 sizeof(*zde) / 8, (void *)zde, oh->ot_tx);
1117 static int osd_declare_dir_delete(const struct lu_env *env,
1118 struct dt_object *dt,
1119 const struct dt_key *key,
1122 struct osd_object *obj = osd_dt_obj(dt);
1123 dnode_t *zap_dn = obj->oo_dn;
1124 struct osd_thandle *oh;
1125 const char *name = (const char *)key;
1128 LASSERT(dt_object_exists(dt));
1129 LASSERT(osd_invariant(obj));
1130 LASSERT(zap_dn != NULL);
1132 LASSERT(th != NULL);
1133 oh = container_of(th, struct osd_thandle, ot_super);
1136 * In Orion . and .. were stored in the directory (not generated upon
1137 * request as now). We preserve them for backward compatibility.
1139 if (name[0] == '.') {
1142 else if (name[1] == '.' && name[2] == 0)
1146 /* do not specify the key as then DMU is trying to look it up
1147 * which is very expensive. usually the layers above lookup
1150 osd_tx_hold_zap(oh->ot_tx, zap_dn->dn_object, zap_dn, FALSE, NULL);
1152 /* For destroying agent object if have. */
1153 dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1158 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
1159 const struct dt_key *key, struct thandle *th)
1161 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1162 struct osd_object *obj = osd_dt_obj(dt);
1163 struct osd_device *osd = osd_obj2dev(obj);
1164 struct osd_thandle *oh;
1165 dnode_t *zap_dn = obj->oo_dn;
1166 char *name = (char *)key;
1172 LASSERT(th != NULL);
1173 oh = container_of(th, struct osd_thandle, ot_super);
1176 * In Orion . and .. were stored in the directory (not generated upon
1177 * request as now). we preserve them for backward compatibility
1179 if (name[0] == '.') {
1182 else if (name[1] == '.' && name[2] == 0)
1186 /* XXX: We have to say that lookup during delete_declare will affect
1187 * performance, but we have to check whether the name entry (to
1188 * be deleted) has agent object or not to avoid orphans.
1190 * We will improve that in the future, some possible solutions,
1192 * 1) Some hint from the caller via transaction handle to make
1193 * the lookup conditionally.
1194 * 2) Enhance the ZFS logic to recognize the OSD lookup result
1195 * and delete the given entry directly without lookup again
1196 * internally. LU-10190
1198 memset(&zde->lzd_fid, 0, sizeof(zde->lzd_fid));
1199 rc = osd_zap_lookup(osd, zap_dn->dn_object, zap_dn, name, 8, 3, zde);
1202 CERROR("%s: failed to locate entry %s: rc = %d\n",
1203 osd->od_svname, name, rc);
1207 if (unlikely(osd_remote_fid(env, osd, &zde->lzd_fid) > 0)) {
1208 rc = -dmu_object_free(osd->od_os, zde->lzd_reg.zde_dnode,
1211 CERROR("%s: failed to destroy agent object (%llu) for the entry %s: rc = %d\n",
1212 osd->od_svname, (__u64)zde->lzd_reg.zde_dnode,
1216 /* Remove key from the ZAP */
1217 rc = osd_zap_remove(osd, zap_dn->dn_object, zap_dn,
1218 (char *)key, oh->ot_tx);
1220 CERROR("%s: zap_remove %s failed: rc = %d\n",
1221 osd->od_svname, name, rc);
1226 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
1227 struct dt_object *dt,
1230 struct osd_zap_it *it;
1232 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
1234 it->ozi_pos = OZI_POS_INIT;
1236 RETURN((struct dt_it *)it);
1240 * Move Iterator to record specified by \a key
1242 * \param di osd iterator
1243 * \param key key for index
1245 * \retval +ve di points to record with least key not larger than key
1246 * \retval 0 di points to exact matched key
1247 * \retval -ve failure
1249 static int osd_dir_it_get(const struct lu_env *env,
1250 struct dt_it *di, const struct dt_key *key)
1252 struct osd_zap_it *it = (struct osd_zap_it *)di;
1253 struct osd_object *obj = it->ozi_obj;
1254 char *name = (char *)key;
1259 LASSERT(it->ozi_zc);
1261 /* reset the cursor */
1262 zap_cursor_fini(it->ozi_zc);
1263 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
1265 /* XXX: implementation of the API is broken at the moment */
1266 LASSERT(((const char *)key)[0] == 0);
1269 it->ozi_pos = OZI_POS_INIT;
1273 if (name[0] == '.') {
1275 it->ozi_pos = OZI_POS_DOT;
1277 } else if (name[1] == '.' && name[2] == 0) {
1278 it->ozi_pos = OZI_POS_DOTDOT;
1283 /* neither . nor .. - some real record */
1284 it->ozi_pos = OZI_POS_REAL;
1291 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
1293 /* PBS: do nothing : ref are incremented at retrive and decreamented
1299 * in Orion . and .. were stored in the directory, while ZPL
1300 * and current osd-zfs generate them up on request. so, we
1301 * need to ignore previously stored . and ..
1303 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
1304 zap_attribute_t *za)
1309 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1312 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
1313 if (za->za_name[1] == 0) {
1315 } else if (za->za_name[1] == '.' &&
1316 za->za_name[2] == 0) {
1319 if (unlikely(isdot))
1320 zap_cursor_advance(it->ozi_zc);
1322 } while (unlikely(rc == 0 && isdot));
1328 * to load a directory entry at a time and stored it in
1329 * iterator's in-memory data structure.
1331 * \param di, struct osd_it_ea, iterator's in memory structure
1333 * \retval +ve, iterator reached to end
1334 * \retval 0, iterator not reached to end
1335 * \retval -ve, on error
1337 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
1339 struct osd_zap_it *it = (struct osd_zap_it *)di;
1340 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1345 /* temp. storage should be enough for any key supported by ZFS */
1346 BUILD_BUG_ON(sizeof(za->za_name) > sizeof(it->ozi_name));
1349 * the first ->next() moves the cursor to .
1350 * the second ->next() moves the cursor to ..
1351 * then we get to the real records and have to verify any exist
1353 if (it->ozi_pos <= OZI_POS_DOTDOT) {
1355 if (it->ozi_pos <= OZI_POS_DOTDOT)
1358 zap_cursor_advance(it->ozi_zc);
1362 * According to current API we need to return error if its last entry.
1363 * zap_cursor_advance() does not return any value. So we need to call
1364 * retrieve to check if there is any record. We should make
1365 * changes to Iterator API to not return status for this API
1367 rc = osd_index_retrieve_skip_dots(it, za);
1369 if (rc == -ENOENT) /* end of dir */
1375 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
1376 const struct dt_it *di)
1378 struct osd_zap_it *it = (struct osd_zap_it *)di;
1379 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1383 if (it->ozi_pos <= OZI_POS_DOT) {
1384 it->ozi_pos = OZI_POS_DOT;
1385 RETURN((struct dt_key *)".");
1386 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1387 RETURN((struct dt_key *)"..");
1390 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1392 RETURN(ERR_PTR(rc));
1394 strcpy(it->ozi_name, za->za_name);
1396 RETURN((struct dt_key *)it->ozi_name);
1399 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
1401 struct osd_zap_it *it = (struct osd_zap_it *)di;
1402 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1406 if (it->ozi_pos <= OZI_POS_DOT) {
1407 it->ozi_pos = OZI_POS_DOT;
1409 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1413 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1415 rc = strlen(za->za_name);
1421 osd_dirent_update(const struct lu_env *env, struct osd_device *dev,
1422 uint64_t zap, const char *key, struct luz_direntry *zde)
1428 tx = dmu_tx_create(dev->od_os);
1432 dmu_tx_hold_zap(tx, zap, TRUE, NULL);
1433 rc = -dmu_tx_assign(tx, TXG_WAIT);
1435 rc = -zap_update(dev->od_os, zap, key, 8, sizeof(*zde) / 8,
1436 (const void *)zde, tx);
1445 static int osd_update_entry_for_agent(const struct lu_env *env,
1446 struct osd_device *osd,
1447 uint64_t zap, const char *name,
1448 struct luz_direntry *zde, __u32 attr)
1450 dmu_tx_t *tx = NULL;
1454 if (attr & LUDA_VERIFY_DRYRUN)
1457 tx = dmu_tx_create(osd->od_os);
1459 GOTO(out, rc = -ENOMEM);
1461 dmu_tx_hold_sa_create(tx, osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
1462 dmu_tx_hold_zap(tx, zap, FALSE, NULL);
1463 rc = -dmu_tx_assign(tx, TXG_WAIT);
1469 rc = osd_create_agent_object(env, osd, zde, zap, tx);
1471 rc = -zap_update(osd->od_os, zap, name, 8, sizeof(*zde) / 8,
1472 (const void *)zde, tx);
1478 CDEBUG(D_LFSCK, "%s: Updated (%s) remote entry for "DFID": rc = %d\n",
1479 osd_name(osd), (attr & LUDA_VERIFY_DRYRUN) ? "(ro)" : "(rw)",
1480 PFID(&zde->lzd_fid), rc);
1484 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
1485 struct dt_rec *dtrec, __u32 attr)
1487 struct osd_zap_it *it = (struct osd_zap_it *)di;
1488 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
1489 struct osd_thread_info *info = osd_oti_get(env);
1490 struct luz_direntry *zde = &info->oti_zde;
1491 zap_attribute_t *za = &info->oti_za;
1492 struct lu_fid *fid = &info->oti_fid;
1493 struct osd_device *osd = osd_obj2dev(it->ozi_obj);
1498 if (it->ozi_pos <= OZI_POS_DOT) {
1499 /* notice hash=0 here, this is needed to avoid
1500 * case when some real entry (after ./..) may
1501 * have hash=0. in this case the client would
1502 * be confused having records out of hash order.
1504 lde->lde_hash = cpu_to_le64(0);
1505 strcpy(lde->lde_name, ".");
1506 lde->lde_namelen = cpu_to_le16(1);
1507 fid_cpu_to_le(&lde->lde_fid,
1508 lu_object_fid(&it->ozi_obj->oo_dt.do_lu));
1509 lde->lde_attrs = LUDA_FID;
1510 /* append lustre attributes */
1511 osd_it_append_attrs(lde, attr, 1, S_DT(S_IFDIR));
1512 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
1513 it->ozi_pos = OZI_POS_DOT;
1515 } else if (it->ozi_pos == OZI_POS_DOTDOT) {
1516 /* same as for . above */
1517 lde->lde_hash = cpu_to_le64(0);
1518 strcpy(lde->lde_name, "..");
1519 lde->lde_namelen = cpu_to_le16(2);
1520 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, fid, NULL);
1522 fid_cpu_to_le(&lde->lde_fid, fid);
1523 lde->lde_attrs = LUDA_FID;
1524 } else if (rc != -ENOENT) {
1525 /* ENOENT happens at the root of filesystem, ignore */
1529 /* append lustre attributes */
1530 osd_it_append_attrs(lde, attr, 2, S_DT(S_IFDIR));
1531 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
1537 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1541 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
1542 namelen = strlen(za->za_name);
1543 if (namelen > NAME_MAX)
1545 strcpy(lde->lde_name, za->za_name);
1546 lde->lde_namelen = cpu_to_le16(namelen);
1548 if (za->za_integer_length != 8) {
1549 CERROR("%s: unsupported direntry format: %d %d\n",
1551 za->za_integer_length, (int)za->za_num_integers);
1555 rc = osd_zap_lookup(osd, it->ozi_zc->zc_zapobj, it->ozi_obj->oo_dn,
1556 za->za_name, za->za_integer_length, 3, zde);
1560 if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid)) {
1561 lde->lde_attrs = LUDA_FID;
1562 fid_cpu_to_le(&lde->lde_fid, &zde->lzd_fid);
1563 if (unlikely(zde->lzd_reg.zde_dnode == ZFS_NO_OBJECT &&
1564 osd_remote_fid(env, osd, &zde->lzd_fid) > 0 &&
1565 attr & LUDA_VERIFY)) {
1566 /* It is mainly used for handling the MDT
1567 * upgraded from old ZFS based backend.
1569 rc = osd_update_entry_for_agent(env, osd,
1570 it->ozi_obj->oo_dn->dn_object,
1571 za->za_name, zde, attr);
1573 lde->lde_attrs |= LUDA_REPAIR;
1575 lde->lde_attrs |= LUDA_UNKNOWN;
1578 if (!(attr & (LUDA_VERIFY | LUDA_VERIFY_DRYRUN)))
1579 GOTO(pack_attr, rc = 0);
1582 if (CFS_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
1585 rc = osd_get_fid_by_oid(env, osd, zde->lzd_reg.zde_dnode, fid);
1587 lde->lde_attrs = LUDA_UNKNOWN;
1588 GOTO(pack_attr, rc = 0);
1591 if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid) &&
1592 lu_fid_eq(&zde->lzd_fid, fid))
1593 GOTO(pack_attr, rc = 0);
1595 if (!(attr & LUDA_VERIFY)) {
1596 fid_cpu_to_le(&lde->lde_fid, fid);
1597 lde->lde_attrs = LUDA_FID;
1598 GOTO(pack_attr, rc = 0);
1601 if (attr & LUDA_VERIFY_DRYRUN) {
1602 fid_cpu_to_le(&lde->lde_fid, fid);
1603 lde->lde_attrs = LUDA_FID | LUDA_REPAIR;
1604 GOTO(pack_attr, rc = 0);
1607 fid_cpu_to_le(&lde->lde_fid, fid);
1608 lde->lde_attrs = LUDA_FID;
1609 zde->lzd_fid = *fid;
1610 rc = osd_dirent_update(env, osd, it->ozi_zc->zc_zapobj,
1613 lde->lde_attrs |= LUDA_UNKNOWN;
1614 GOTO(pack_attr, rc = 0);
1617 lde->lde_attrs |= LUDA_REPAIR;
1619 GOTO(pack_attr, rc = 0);
1622 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1623 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1627 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1630 struct osd_zap_it *it = (struct osd_zap_it *)di;
1631 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1636 if (it->ozi_pos <= OZI_POS_DOT)
1638 else if (it->ozi_pos == OZI_POS_DOTDOT)
1642 rc = lu_dirent_calc_size(namelen, attr);
1646 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1647 if (unlikely(rc != 0))
1650 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1651 CERROR("%s: unsupported direntry format: %d %d\n",
1652 osd_obj2dev(it->ozi_obj)->od_svname,
1653 za->za_integer_length, (int)za->za_num_integers);
1657 namelen = strlen(za->za_name);
1658 if (namelen > NAME_MAX)
1661 rc = lu_dirent_calc_size(namelen, attr);
1666 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1668 struct osd_zap_it *it = (struct osd_zap_it *)di;
1672 if (it->ozi_pos <= OZI_POS_DOTDOT)
1675 pos = osd_zap_cursor_serialize(it->ozi_zc);
1682 * rc == 0 -> end of directory.
1683 * rc > 0 -> ok, proceed.
1684 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1686 static int osd_dir_it_load(const struct lu_env *env,
1687 const struct dt_it *di, __u64 hash)
1689 struct osd_zap_it *it = (struct osd_zap_it *)di;
1690 struct osd_object *obj = it->ozi_obj;
1691 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1695 /* reset the cursor */
1696 zap_cursor_fini(it->ozi_zc);
1697 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1700 it->ozi_pos = OZI_POS_INIT;
1701 rc = 1; /* there will be ./.. at least */
1703 it->ozi_pos = OZI_POS_REAL;
1704 /* to return whether the end has been reached */
1705 rc = osd_index_retrieve_skip_dots(it, za);
1708 else if (rc == -ENOENT)
1715 const struct dt_index_operations osd_dir_ops = {
1716 .dio_lookup = osd_dir_lookup,
1717 .dio_declare_insert = osd_declare_dir_insert,
1718 .dio_insert = osd_dir_insert,
1719 .dio_declare_delete = osd_declare_dir_delete,
1720 .dio_delete = osd_dir_delete,
1722 .init = osd_dir_it_init,
1723 .fini = osd_index_it_fini,
1724 .get = osd_dir_it_get,
1725 .put = osd_dir_it_put,
1726 .next = osd_dir_it_next,
1727 .key = osd_dir_it_key,
1728 .key_size = osd_dir_it_key_size,
1729 .rec = osd_dir_it_rec,
1730 .rec_size = osd_dir_it_rec_size,
1731 .store = osd_dir_it_store,
1732 .load = osd_dir_it_load
1737 * Primitives for index files using binary keys.
1740 /* key integer_size is 8 */
1741 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1742 const struct dt_key *src)
1749 /* align keysize to 64bit */
1750 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1751 size *= sizeof(__u64);
1753 LASSERT(size <= MAXNAMELEN);
1755 if (unlikely(size > o->oo_keysize))
1756 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1757 memcpy(dst, (const char *)src, o->oo_keysize);
1759 return (size/sizeof(__u64));
1762 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1763 struct dt_rec *rec, const struct dt_key *key)
1765 struct osd_object *obj = osd_dt_obj(dt);
1766 struct osd_device *osd = osd_obj2dev(obj);
1767 __u64 *k = osd_oti_get(env)->oti_key64;
1771 rc = osd_prepare_key_uint64(obj, k, key);
1773 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1774 k, rc, obj->oo_recusize, obj->oo_recsize,
1776 RETURN(rc == 0 ? 1 : rc);
1779 static int osd_declare_index_insert(const struct lu_env *env,
1780 struct dt_object *dt,
1781 const struct dt_rec *rec,
1782 const struct dt_key *key,
1785 struct osd_object *obj = osd_dt_obj(dt);
1786 struct osd_thandle *oh;
1789 LASSERT(th != NULL);
1790 oh = container_of(th, struct osd_thandle, ot_super);
1792 LASSERT(obj->oo_dn);
1794 /* do not specify the key as then DMU is trying to look it up
1795 * which is very expensive. usually the layers above lookup
1798 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1804 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1805 const struct dt_rec *rec, const struct dt_key *key,
1808 struct osd_object *obj = osd_dt_obj(dt);
1809 struct osd_device *osd = osd_obj2dev(obj);
1810 struct osd_thandle *oh;
1811 __u64 *k = osd_oti_get(env)->oti_key64;
1815 LASSERT(obj->oo_dn);
1816 LASSERT(dt_object_exists(dt));
1817 LASSERT(osd_invariant(obj));
1818 LASSERT(th != NULL);
1820 oh = container_of(th, struct osd_thandle, ot_super);
1822 rc = osd_prepare_key_uint64(obj, k, key);
1824 /* Insert (key,oid) into ZAP */
1825 rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1826 k, rc, obj->oo_recusize, obj->oo_recsize,
1827 (void *)rec, oh->ot_tx);
1831 static int osd_declare_index_delete(const struct lu_env *env,
1832 struct dt_object *dt,
1833 const struct dt_key *key,
1836 struct osd_object *obj = osd_dt_obj(dt);
1837 struct osd_thandle *oh;
1840 LASSERT(dt_object_exists(dt));
1841 LASSERT(osd_invariant(obj));
1842 LASSERT(th != NULL);
1843 LASSERT(obj->oo_dn);
1845 oh = container_of(th, struct osd_thandle, ot_super);
1847 /* do not specify the key as then DMU is trying to look it up
1848 * which is very expensive. usually the layers above lookup
1851 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1857 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1858 const struct dt_key *key, struct thandle *th)
1860 struct osd_object *obj = osd_dt_obj(dt);
1861 struct osd_device *osd = osd_obj2dev(obj);
1862 struct osd_thandle *oh;
1863 __u64 *k = osd_oti_get(env)->oti_key64;
1867 LASSERT(obj->oo_dn);
1868 LASSERT(th != NULL);
1869 oh = container_of(th, struct osd_thandle, ot_super);
1871 rc = osd_prepare_key_uint64(obj, k, key);
1873 /* Remove binary key from the ZAP */
1874 rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1879 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1880 const struct dt_key *key)
1882 struct osd_zap_it *it = (struct osd_zap_it *)di;
1883 struct osd_object *obj = it->ozi_obj;
1884 struct osd_device *osd = osd_obj2dev(obj);
1888 LASSERT(it->ozi_zc);
1890 /* XXX: we need a binary version of zap_cursor_move_to_key()
1891 * to implement this API
1893 if (*((const __u64 *)key) != 0)
1894 CERROR("NOT IMPLEMETED YET (move to %#llx)\n", *((__u64 *)key));
1896 zap_cursor_fini(it->ozi_zc);
1897 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1903 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1905 struct osd_zap_it *it = (struct osd_zap_it *)di;
1906 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1910 if (it->ozi_reset == 0)
1911 zap_cursor_advance(it->ozi_zc);
1915 * According to current API we need to return error if it's last entry.
1916 * zap_cursor_advance() does not return any value. So we need to call
1917 * retrieve to check if there is any record. We should make
1918 * changes to Iterator API to not return status for this API
1920 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1927 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1928 const struct dt_it *di)
1930 struct osd_zap_it *it = (struct osd_zap_it *)di;
1931 struct osd_object *obj = it->ozi_obj;
1932 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1937 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1939 RETURN(ERR_PTR(rc));
1941 /* the binary key is stored in the name */
1942 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1944 RETURN((struct dt_key *)&it->ozi_key);
1947 static int osd_index_it_key_size(const struct lu_env *env,
1948 const struct dt_it *di)
1950 struct osd_zap_it *it = (struct osd_zap_it *)di;
1951 struct osd_object *obj = it->ozi_obj;
1953 RETURN(obj->oo_keysize);
1956 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1957 struct dt_rec *rec, __u32 attr)
1959 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1960 struct osd_zap_it *it = (struct osd_zap_it *)di;
1961 struct osd_object *obj = it->ozi_obj;
1962 struct osd_device *osd = osd_obj2dev(obj);
1963 __u64 *k = osd_oti_get(env)->oti_key64;
1968 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1972 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1974 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1975 k, rc, obj->oo_recusize, obj->oo_recsize,
1980 static __u64 osd_index_it_store(const struct lu_env *env,
1981 const struct dt_it *di)
1983 struct osd_zap_it *it = (struct osd_zap_it *)di;
1986 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1989 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1992 struct osd_zap_it *it = (struct osd_zap_it *)di;
1993 struct osd_object *obj = it->ozi_obj;
1994 struct osd_device *osd = osd_obj2dev(obj);
1995 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1999 /* reset the cursor */
2000 zap_cursor_fini(it->ozi_zc);
2001 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
2002 obj->oo_dn->dn_object, hash);
2005 rc = -zap_cursor_retrieve(it->ozi_zc, za);
2008 else if (rc == -ENOENT)
2014 static const struct dt_index_operations osd_index_ops = {
2015 .dio_lookup = osd_index_lookup,
2016 .dio_declare_insert = osd_declare_index_insert,
2017 .dio_insert = osd_index_insert,
2018 .dio_declare_delete = osd_declare_index_delete,
2019 .dio_delete = osd_index_delete,
2021 .init = osd_index_it_init,
2022 .fini = osd_index_it_fini,
2023 .get = osd_index_it_get,
2024 .put = osd_index_it_put,
2025 .next = osd_index_it_next,
2026 .key = osd_index_it_key,
2027 .key_size = osd_index_it_key_size,
2028 .rec = osd_index_it_rec,
2029 .store = osd_index_it_store,
2030 .load = osd_index_it_load
2034 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2035 const struct dt_index_features *feat)
2037 struct osd_object *obj = osd_dt_obj(dt);
2038 struct osd_device *osd = osd_obj2dev(obj);
2039 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2043 down_read(&obj->oo_guard);
2046 * XXX: implement support for fixed-size keys sorted with natural
2047 * numerical way (not using internal hash value)
2049 if (feat->dif_flags & DT_IND_RANGE)
2050 GOTO(out, rc = -ERANGE);
2052 if (unlikely(feat == &dt_otable_features)) {
2053 dt->do_index_ops = &osd_otable_ops;
2057 LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
2058 if (likely(feat == &dt_directory_features)) {
2059 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
2060 dt->do_index_ops = &osd_dir_ops;
2062 GOTO(out, rc = -ENOTDIR);
2063 } else if (unlikely(feat == &dt_acct_features)) {
2064 LASSERT(fid_is_acct(fid));
2065 dt->do_index_ops = &osd_acct_index_ops;
2066 } else if (dt->do_index_ops == NULL) {
2067 /* For index file, we don't support variable key & record sizes
2068 * and the key has to be unique
2070 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
2071 GOTO(out, rc = -EINVAL);
2073 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
2074 GOTO(out, rc = -E2BIG);
2075 if (feat->dif_keysize_max != feat->dif_keysize_min)
2076 GOTO(out, rc = -EINVAL);
2078 /* As for the record size, it should be a multiple of 8 bytes
2079 * and smaller than the maximum value length supported by ZAP.
2081 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
2082 GOTO(out, rc = -E2BIG);
2083 if (feat->dif_recsize_max != feat->dif_recsize_min)
2084 GOTO(out, rc = -EINVAL);
2086 obj->oo_keysize = feat->dif_keysize_max;
2087 obj->oo_recsize = feat->dif_recsize_max;
2088 obj->oo_recusize = 1;
2090 /* ZFS prefers to work with array of 64bits */
2091 if ((obj->oo_recsize & 7) == 0) {
2092 obj->oo_recsize >>= 3;
2093 obj->oo_recusize = 8;
2095 dt->do_index_ops = &osd_index_ops;
2097 if (feat == &dt_lfsck_layout_orphan_features ||
2098 feat == &dt_lfsck_layout_dangling_features ||
2099 feat == &dt_lfsck_namespace_features)
2102 rc = osd_index_register(osd, fid, obj->oo_keysize,
2103 obj->oo_recusize * obj->oo_recsize);
2105 CWARN("%s: failed to register index "DFID": rc = %d\n",
2106 osd_name(osd), PFID(fid), rc);
2110 CDEBUG(D_LFSCK, "%s: index object "DFID
2111 " (%u/%u/%u) registered\n",
2112 osd_name(osd), PFID(fid), obj->oo_keysize,
2113 obj->oo_recusize, obj->oo_recsize);
2117 up_read(&obj->oo_guard);