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>
62 static inline int osd_object_is_zap(dnode_t *dn)
64 return (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
65 dn->dn_type == DMU_OT_USERGROUP_USED);
68 /* We don't actually have direct access to the zap_hashbits() function
69 * so just pretend like we do for now. If this ever breaks we can look at
71 #define zap_hashbits(zc) 48
74 * | cd (16 bits) | hash (48 bits) |
75 * we need it in other form:
76 * |0| hash (48 bit) | cd (15 bit) |
77 * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
78 * the readdir hashes from multiple directory stripes uniformly on the client.
79 * Another point is sign bit, the hash range should be in [0, 2^63-1] because
80 * loff_t (for llseek) needs to be a positive value. This means the "cd" field
81 * should only be the low 15 bits.
83 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
85 uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
87 return (zfs_hash >> zap_hashbits(zc)) |
88 (zfs_hash << (63 - zap_hashbits(zc)));
91 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
92 uint64_t id, uint64_t dirhash)
94 uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
95 (dirhash >> (63 - zap_hashbits(zc)));
97 zap_cursor_init_serialized(zc, os, id, zfs_hash);
100 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
101 uint64_t id, uint64_t dirhash)
106 if (unlikely(t == NULL))
109 osd_zap_cursor_init_serialized(t, os, id, dirhash);
115 void osd_zap_cursor_fini(zap_cursor_t *zc)
121 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
122 struct osd_object *o,
125 struct osd_device *d = osd_obj2dev(o);
126 osd_zap_cursor_init_serialized(zc, d->od_os,
127 o->oo_dn->dn_object, dirhash);
130 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
133 struct osd_device *d = osd_obj2dev(o);
134 return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
137 static struct dt_it *osd_index_it_init(const struct lu_env *env,
138 struct dt_object *dt,
141 struct osd_thread_info *info = osd_oti_get(env);
142 struct osd_zap_it *it;
143 struct osd_object *obj = osd_dt_obj(dt);
144 struct lu_object *lo = &dt->do_lu;
148 if (obj->oo_destroyed)
149 RETURN(ERR_PTR(-ENOENT));
151 LASSERT(lu_object_exists(lo));
155 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
157 RETURN(ERR_PTR(-ENOMEM));
159 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
161 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
169 RETURN((struct dt_it *)it);
172 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
174 struct osd_zap_it *it = (struct osd_zap_it *)di;
175 struct osd_object *obj;
179 LASSERT(it->ozi_obj);
183 osd_zap_cursor_fini(it->ozi_zc);
184 osd_object_put(env, obj);
185 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
191 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
193 /* PBS: do nothing : ref are incremented at retrive and decreamented
197 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
200 const unsigned align = sizeof(struct luda_type) - 1;
201 struct luda_type *lt;
203 /* check if file type is required */
204 if (attr & LUDA_TYPE) {
205 len = (len + align) & ~align;
207 lt = (void *)ent->lde_name + len;
208 lt->lt_type = cpu_to_le16(DTTOIF(type));
209 ent->lde_attrs |= LUDA_TYPE;
212 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
215 int __osd_xattr_load_by_oid(struct osd_device *osd, uint64_t oid, nvlist_t **sa)
221 rc = -dmu_bonus_hold(osd->od_os, oid, osd_obj_tag, &db);
223 CERROR("%s: can't get bonus, rc = %d\n", osd->od_svname, rc);
227 rc = -sa_handle_get_from_db(osd->od_os, db, NULL, SA_HDL_PRIVATE, &hdl);
229 dmu_buf_rele(db, osd_obj_tag);
233 rc = __osd_xattr_load(osd, hdl, sa);
235 sa_handle_destroy(hdl);
240 * Get the object's FID from its LMA EA.
242 * \param[in] env pointer to the thread context
243 * \param[in] osd pointer to the OSD device
244 * \param[in] oid the object's local identifier
245 * \param[out] fid the buffer to hold the object's FID
247 * \retval 0 for success
248 * \retval negative error number on failure
250 static int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
251 uint64_t oid, struct lu_fid *fid)
253 struct objset *os = osd->od_os;
254 struct osd_thread_info *oti = osd_oti_get(env);
255 struct lustre_mdt_attrs *lma =
256 (struct lustre_mdt_attrs *)oti->oti_buf;
258 nvlist_t *sa_xattr = NULL;
259 sa_handle_t *sa_hdl = NULL;
260 uchar_t *nv_value = NULL;
261 uint64_t xattr = ZFS_NO_OBJECT;
266 rc = __osd_xattr_load_by_oid(osd, oid, &sa_xattr);
273 rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
281 if (unlikely(size > sizeof(oti->oti_buf)))
282 GOTO(out, rc = -ERANGE);
284 memcpy(lma, nv_value, size);
289 rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
293 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
294 sa_handle_destroy(sa_hdl);
299 buf.lb_len = sizeof(oti->oti_buf);
300 rc = __osd_xattr_get_large(env, osd, xattr, &buf,
301 XATTR_NAME_LMA, &size);
306 if (size < sizeof(*lma))
307 GOTO(out, rc = -EIO);
309 lustre_lma_swab(lma);
310 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
311 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
312 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
313 "oid = %#llx\n", osd->od_svname,
314 lma->lma_incompat & ~LMA_INCOMPAT_SUPP, oid);
315 GOTO(out, rc = -EOPNOTSUPP);
317 *fid = lma->lma_self_fid;
322 if (sa_xattr != NULL)
323 nvlist_free(sa_xattr);
328 * As we don't know FID, we can't use LU object, so this function
329 * partially duplicate osd_xattr_get_internal() which is built around
330 * LU-object and uses it to cache data like regular EA dnode, etc
332 static int osd_find_parent_by_dnode(const struct lu_env *env,
334 struct lu_fid *fid, uint64_t *oid)
336 struct osd_object *obj = osd_dt_obj(o);
337 struct osd_device *osd = osd_obj2dev(obj);
338 uint64_t dnode = ZFS_NO_OBJECT;
342 /* first of all, get parent dnode from own attributes */
343 rc = osd_sa_handle_get(obj);
346 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
350 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
356 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
357 struct lu_fid *fid, uint64_t *oid)
359 struct link_ea_header *leh;
360 struct link_ea_entry *lee;
365 buf.lb_buf = osd_oti_get(env)->oti_buf;
366 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
368 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
370 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
374 OBD_ALLOC(buf.lb_buf, rc);
375 if (buf.lb_buf == NULL)
378 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
382 if (rc < sizeof(*leh) + sizeof(*lee))
383 GOTO(out, rc = -EINVAL);
386 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
387 leh->leh_magic = LINK_EA_MAGIC;
388 leh->leh_reccount = __swab32(leh->leh_reccount);
389 leh->leh_len = __swab64(leh->leh_len);
391 if (leh->leh_magic != LINK_EA_MAGIC)
392 GOTO(out, rc = -EINVAL);
393 if (leh->leh_reccount == 0)
394 GOTO(out, rc = -ENODATA);
396 lee = (struct link_ea_entry *)(leh + 1);
397 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
401 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
402 OBD_FREE(buf.lb_buf, buf.lb_len);
405 /* this block can be enabled for additional verification
406 * it's trying to match FID from LinkEA vs. FID from LMA */
410 rc2 = osd_find_parent_by_dnode(env, o, &fid2, oid);
412 if (lu_fid_eq(fid, &fid2) == 0)
413 CERROR("wrong parent: "DFID" != "DFID"\n",
414 PFID(fid), PFID(&fid2));
418 /* no LinkEA is found, let's try to find the fid in parent's LMA */
419 if (unlikely(rc != 0))
420 rc = osd_find_parent_by_dnode(env, o, fid, oid);
426 * When lookup item under striped directory, we need to locate the master
427 * MDT-object of the striped directory firstly, then the client will send
428 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
429 * and the item's name. If the system is restored from MDT file level backup,
430 * then before the OI scrub completely built the OI files, the OI mappings of
431 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
432 * not a problem for the master MDT-object. Because when locate the master
433 * MDT-object, we will do name based lookup (for the striped directory itself)
434 * firstly, during such process we can setup the correct OI mapping for the
435 * master MDT-object. But it will be trouble for the slave MDT-object. Because
436 * the client will not trigger name based lookup on the MDT to locate the slave
437 * MDT-object before locating item under the striped directory, then when
438 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
439 * is invalid and does not know what the right OI mapping is, then the MDT has
440 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
441 * the OI file, related OI mapping is unknown yet, please try again later. And
442 * then client will re-try the RPC again and again until related OI mapping has
443 * been updated. That is quite inefficient.
445 * To resolve above trouble, we will handle it as the following two cases:
447 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
448 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
449 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
450 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
451 * directly. Please check osd_fid_lookup().
453 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
454 * Under such case, during lookup the master MDT-object, we will lookup the
455 * slave MDT-object via readdir against the master MDT-object, because the
456 * slave MDT-objects information are stored as sub-directories with the name
457 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
458 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
459 * the correct OI mapping for the slave MDT-object.
461 static int osd_check_lmv(const struct lu_env *env, struct osd_device *osd,
462 uint64_t oid, const struct lu_fid *fid)
464 struct osd_thread_info *info = osd_oti_get(env);
465 struct luz_direntry *zde = &info->oti_zde;
466 zap_attribute_t *za = &info->oti_za;
467 zap_cursor_t *zc = &info->oti_zc;
468 struct lu_fid *tfid = &info->oti_fid;
469 nvlist_t *nvbuf = NULL;
470 struct lmv_mds_md_v1 *lmv = NULL;
475 rc = __osd_xattr_load_by_oid(osd, oid, &nvbuf);
476 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
482 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMV,
483 (uchar_t **)&lmv, &size);
484 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
485 GOTO(out_nvbuf, rc = 0);
487 if (rc || le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
490 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
491 rc = -zap_cursor_retrieve(zc, za);
493 zap_cursor_advance(zc);
495 CERROR("%s: fail to init for check LMV "DFID"(%llu): rc = %d\n",
496 osd_name(osd), PFID(fid), oid, rc);
501 rc = -zap_cursor_retrieve(zc, za);
503 GOTO(out_zc, rc = 0);
506 CERROR("%s: fail to locate next for check LMV "
507 DFID"(%llu): rc = %d\n",
508 osd_name(osd), PFID(fid), oid, rc);
513 sscanf(za->za_name + 1, SFID, RFID(tfid));
514 if (fid_is_sane(tfid) && !osd_remote_fid(env, osd, tfid)) {
515 rc = osd_zap_lookup(osd, oid, NULL, za->za_name,
516 za->za_integer_length,
517 sizeof(*zde) / za->za_integer_length,
520 CERROR("%s: fail to lookup for check LMV "
521 DFID"(%llu): rc = %d\n",
522 osd_name(osd), PFID(fid), oid, rc);
526 rc = osd_oii_insert(env, osd, tfid,
527 zde->lzd_reg.zde_dnode, false);
531 zap_cursor_advance(zc);
543 osd_consistency_check(const struct lu_env *env, struct osd_device *osd,
544 struct osd_object *obj, const struct lu_fid *fid,
545 uint64_t oid, bool is_dir)
547 struct lustre_scrub *scrub = &osd->od_scrub;
555 if (!fid_is_norm(fid) && !fid_is_igif(fid))
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 (thread_is_running(&scrub->os_thread)) {
570 if (scrub->os_pos_current > oid)
572 } else if (osd->od_auto_scrub_interval == AS_NEVER) {
575 if (cfs_time_before(cfs_time_current_sec(),
576 scrub->os_file.sf_time_last_complete +
577 osd->od_auto_scrub_interval))
582 rc = osd_fid_lookup(env, osd, fid, &oid2);
588 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
589 /* The object has been removed (by race maybe). */
591 RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
594 } else if (rc || oid == oid2) {
601 if (thread_is_running(&scrub->os_thread)) {
603 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
604 /* The object has been removed (by race maybe). */
606 RETURN(rc = (rc == -EEXIST ? -ENOENT : rc));
609 rc = osd_oii_insert(env, osd, fid, oid, insert);
610 /* There is race condition between osd_oi_lookup and OI scrub.
611 * The OI scrub finished just after osd_oi_lookup() failure.
612 * Under such case, it is unnecessary to trigger OI scrub again,
613 * but try to call osd_oi_lookup() again. */
614 if (unlikely(rc == -EAGAIN))
618 rc = osd_check_lmv(env, osd, oid, fid);
625 if (osd->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
626 rc = osd_scrub_start(env, osd, SS_AUTO_FULL |
627 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
628 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
629 "%s: trigger partial OI scrub for RPC inconsistency "
630 "checking FID "DFID": rc = %d\n",
631 osd_name(osd), PFID(fid), rc);
645 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
646 struct dt_rec *rec, const struct dt_key *key)
648 struct osd_thread_info *oti = osd_oti_get(env);
649 struct osd_object *obj = osd_dt_obj(dt);
650 struct osd_device *osd = osd_obj2dev(obj);
651 struct lu_fid *fid = (struct lu_fid *)rec;
652 char *name = (char *)key;
653 uint64_t oid = ZFS_NO_OBJECT;
657 if (name[0] == '.') {
659 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
660 memcpy(rec, f, sizeof(*f));
662 } else if (name[1] == '.' && name[2] == 0) {
663 rc = osd_find_parent_fid(env, dt, fid, &oid);
668 memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
669 rc = osd_zap_lookup(osd, obj->oo_dn->dn_object, obj->oo_dn,
670 (char *)key, 8, sizeof(oti->oti_zde) / 8,
671 (void *)&oti->oti_zde);
675 oid = oti->oti_zde.lzd_reg.zde_dnode;
676 if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
677 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
681 rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode, fid);
686 if (!rc && !osd_remote_fid(env, osd, fid)) {
687 rc = osd_consistency_check(env, osd, obj, fid, oid,
688 S_ISDIR(DTTOIF(oti->oti_zde.lzd_reg.zde_type)));
689 /* Only -ENOENT error will affect the lookup result. */
694 return rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc);
698 * In DNE environment, the object and its name entry may reside on different
699 * MDTs. Under such case, we will create an agent object on the MDT where the
700 * name entry resides. The agent object is empty, and indicates that the real
701 * object for the name entry resides on another MDT. If without agent object,
702 * related name entry will be skipped when perform MDT side file level backup
703 * and restore via ZPL by userspace tool, such as 'tar'.
705 static int osd_create_agent_object(const struct lu_env *env,
706 struct osd_device *osd,
707 struct luz_direntry *zde,
708 uint64_t parent, dmu_tx_t *tx)
710 struct osd_thread_info *info = osd_oti_get(env);
711 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
712 struct lu_attr *la = &info->oti_la;
713 nvlist_t *nvbuf = NULL;
719 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTOBJ))
722 rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
726 lustre_lma_init(lma, &zde->lzd_fid, 0, LMAI_AGENT);
727 lustre_lma_swab(lma);
728 rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA, (uchar_t *)lma,
733 la->la_valid = LA_TYPE | LA_MODE;
734 la->la_mode = (DTTOIF(zde->lzd_reg.zde_type) & S_IFMT) |
735 S_IRUGO | S_IWUSR | S_IXUGO;
737 if (S_ISDIR(la->la_mode))
738 rc = __osd_zap_create(env, osd, &dn, tx, la,
739 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS), 0);
741 rc = __osd_object_create(env, osd, NULL, &zde->lzd_fid,
746 zde->lzd_reg.zde_dnode = dn->dn_object;
747 rc = -sa_handle_get(osd->od_os, dn->dn_object, NULL,
748 SA_HDL_PRIVATE, &hdl);
750 rc = __osd_attr_init(env, osd, NULL, hdl, tx,
752 sa_handle_destroy(hdl);
760 dmu_object_free(osd->od_os, dn->dn_object, tx);
770 int osd_add_to_remote_parent(const struct lu_env *env,
771 struct osd_device *osd,
772 struct osd_object *obj,
773 struct osd_thandle *oh)
775 struct osd_thread_info *info = osd_oti_get(env);
776 struct luz_direntry *zde = &info->oti_zde;
777 char *name = info->oti_str;
778 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
779 struct lustre_mdt_attrs *lma = (struct lustre_mdt_attrs *)info->oti_buf;
780 struct lu_buf buf = {
782 .lb_len = sizeof(info->oti_buf),
788 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AGENTENT))
791 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
793 CWARN("%s: fail to load LMA for adding "
794 DFID" to remote parent: rc = %d\n",
795 osd_name(osd), PFID(fid), rc);
799 lustre_lma_swab(lma);
800 lma->lma_incompat |= LMAI_REMOTE_PARENT;
801 lustre_lma_swab(lma);
803 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
804 LU_XATTR_REPLACE, oh);
806 CWARN("%s: fail to update LMA for adding "
807 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 = IFTODT(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 "
855 "parent for "DFID": rc = %d\n",
856 osd_name(osd), PFID(fid), rc);
861 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
863 CERROR("%s: fail to load LMA for removing "
864 DFID" from remote parent: rc = %d\n",
865 osd_name(osd), PFID(fid), rc);
869 lustre_lma_swab(lma);
870 lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
871 lustre_lma_swab(lma);
873 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
874 LU_XATTR_REPLACE, oh);
876 CERROR("%s: fail to update LMA for removing "
877 DFID" from remote parent: rc = %d\n",
878 osd_name(osd), PFID(fid), rc);
880 lu_object_clear_agent_entry(&obj->oo_dt.do_lu);
885 static int osd_declare_dir_insert(const struct lu_env *env,
886 struct dt_object *dt,
887 const struct dt_rec *rec,
888 const struct dt_key *key,
891 struct osd_object *obj = osd_dt_obj(dt);
892 struct osd_device *osd = osd_obj2dev(obj);
893 const struct dt_insert_rec *rec1;
894 const struct lu_fid *fid;
895 struct osd_thandle *oh;
897 struct osd_idmap_cache *idc;
900 rec1 = (struct dt_insert_rec *)rec;
902 LASSERT(fid != NULL);
903 LASSERT(rec1->rec_type != 0);
906 oh = container_of0(th, struct osd_thandle, ot_super);
908 idc = osd_idc_find_or_init(env, osd, fid);
910 RETURN(PTR_ERR(idc));
912 if (idc->oic_remote) {
913 const char *name = (const char *)key;
915 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
916 /* Prepare agent object for remote entry that will
917 * be used for operations via ZPL, such as MDT side
918 * file-level backup and restore. */
919 dmu_tx_hold_sa_create(oh->ot_tx,
920 osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
921 if (S_ISDIR(rec1->rec_type))
922 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT,
927 /* This is for inserting dot/dotdot for new created dir. */
928 if (obj->oo_dn == NULL)
929 object = DMU_NEW_OBJECT;
931 object = obj->oo_dn->dn_object;
933 /* do not specify the key as then DMU is trying to look it up
934 * which is very expensive. usually the layers above lookup
935 * before insertion */
936 osd_tx_hold_zap(oh->ot_tx, object, obj->oo_dn, TRUE, NULL);
941 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
944 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
945 struct seq_server_site *ss = osd_seq_site(osd);
950 LASSERT(ss->ss_server_fld != NULL);
952 rc = osd_fld_lookup(env, osd, seq, range);
955 CERROR("%s: Can not lookup fld for %#llx\n",
960 RETURN(ss->ss_node_id == range->lsr_index);
963 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
964 const struct lu_fid *fid)
966 struct seq_server_site *ss = osd_seq_site(osd);
969 /* FID seqs not in FLDB, must be local seq */
970 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
973 /* If FLD is not being initialized yet, it only happens during the
974 * initialization, likely during mgs initialization, and we assume
975 * this is local FID. */
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
993 * \param ignore_quota update should not affect quota
996 * \retval -ve failure
998 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
999 const struct dt_rec *rec, const struct dt_key *key,
1000 struct thandle *th, int ignore_quota)
1002 struct osd_thread_info *oti = osd_oti_get(env);
1003 struct osd_object *parent = osd_dt_obj(dt);
1004 struct osd_device *osd = osd_obj2dev(parent);
1005 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
1006 const struct lu_fid *fid = rec1->rec_fid;
1007 struct osd_thandle *oh;
1008 struct osd_idmap_cache *idc;
1009 const char *name = (const char *)key;
1010 struct luz_direntry *zde = &oti->oti_zde;
1011 int num = sizeof(*zde) / 8;
1015 LASSERT(parent->oo_dn);
1017 LASSERT(dt_object_exists(dt));
1018 LASSERT(osd_invariant(parent));
1020 LASSERT(th != NULL);
1021 oh = container_of0(th, struct osd_thandle, ot_super);
1023 idc = osd_idc_find(env, osd, fid);
1024 if (unlikely(idc == NULL)) {
1025 /* this dt_insert() wasn't declared properly, so
1026 * FID is missing in OI cache. we better do not
1027 * lookup FID in FLDB/OI and don't risk to deadlock,
1028 * but in some special cases (lfsck testing, etc)
1029 * it's much simpler than fixing a caller */
1030 CERROR("%s: "DFID" wasn't declared for insert\n",
1031 osd_name(osd), PFID(fid));
1032 idc = osd_idc_find_or_init(env, osd, fid);
1034 RETURN(PTR_ERR(idc));
1037 CLASSERT(sizeof(zde->lzd_reg) == 8);
1038 CLASSERT(sizeof(*zde) % 8 == 0);
1040 memset(&zde->lzd_reg, 0, sizeof(zde->lzd_reg));
1041 zde->lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
1042 zde->lzd_fid = *fid;
1044 if (idc->oic_remote) {
1045 if (name[0] != '.' || name[1] != '.' || name[2] != 0) {
1046 /* Create agent inode for remote object that will
1047 * be used for MDT file-level backup and restore. */
1048 rc = osd_create_agent_object(env, osd, zde,
1049 parent->oo_dn->dn_object, oh->ot_tx);
1051 CWARN("%s: Fail to create agent object for "
1053 osd_name(osd), PFID(fid), rc);
1054 /* Ignore the failure since the system can go
1055 * ahead if we do not care about the MDT side
1056 * file-level backup and restore. */
1061 if (unlikely(idc->oic_dnode == 0)) {
1062 /* for a reason OI cache wasn't filled properly */
1063 CERROR("%s: OIC for "DFID" isn't filled\n",
1064 osd_name(osd), PFID(fid));
1067 if (name[0] == '.') {
1069 /* do not store ".", instead generate it
1070 * during iteration */
1072 } else if (name[1] == '.' && name[2] == 0) {
1073 uint64_t dnode = idc->oic_dnode;
1074 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
1077 /* update parent dnode in the child.
1078 * later it will be used to generate ".." */
1079 rc = osd_object_sa_update(parent,
1086 zde->lzd_reg.zde_dnode = idc->oic_dnode;
1089 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR))
1090 zde->lzd_fid.f_ver = ~0;
1092 /* The logic is not related with IGIF, just re-use the fail_loc value
1093 * to be consistent with ldiskfs case, then share the same test logic */
1094 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1097 /* Insert (key,oid) into ZAP */
1098 rc = osd_zap_add(osd, parent->oo_dn->dn_object, parent->oo_dn,
1099 name, 8, num, (void *)zde, oh->ot_tx);
1100 if (unlikely(rc == -EEXIST &&
1101 name[0] == '.' && name[1] == '.' && name[2] == 0))
1102 /* Update (key,oid) in ZAP */
1103 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object, name, 8,
1104 sizeof(*zde) / 8, (void *)zde, oh->ot_tx);
1111 static int osd_declare_dir_delete(const struct lu_env *env,
1112 struct dt_object *dt,
1113 const struct dt_key *key,
1116 struct osd_object *obj = osd_dt_obj(dt);
1117 dnode_t *zap_dn = obj->oo_dn;
1118 struct osd_thandle *oh;
1119 const char *name = (const char *)key;
1122 LASSERT(dt_object_exists(dt));
1123 LASSERT(osd_invariant(obj));
1124 LASSERT(zap_dn != NULL);
1126 LASSERT(th != NULL);
1127 oh = container_of0(th, struct osd_thandle, ot_super);
1130 * In Orion . and .. were stored in the directory (not generated upon
1131 * request as now). We preserve them for backward compatibility.
1133 if (name[0] == '.') {
1136 else if (name[1] == '.' && name[2] == 0)
1140 /* do not specify the key as then DMU is trying to look it up
1141 * which is very expensive. usually the layers above lookup
1142 * before deletion */
1143 osd_tx_hold_zap(oh->ot_tx, zap_dn->dn_object, zap_dn, FALSE, NULL);
1145 /* For destroying agent object if have. */
1146 dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1151 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
1152 const struct dt_key *key, struct thandle *th)
1154 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1155 struct osd_object *obj = osd_dt_obj(dt);
1156 struct osd_device *osd = osd_obj2dev(obj);
1157 struct osd_thandle *oh;
1158 dnode_t *zap_dn = obj->oo_dn;
1159 char *name = (char *)key;
1165 LASSERT(th != NULL);
1166 oh = container_of0(th, struct osd_thandle, ot_super);
1169 * In Orion . and .. were stored in the directory (not generated upon
1170 * request as now). we preserve them for backward compatibility
1172 if (name[0] == '.') {
1175 } else if (name[1] == '.' && name[2] == 0) {
1180 /* XXX: We have to say that lookup during delete_declare will affect
1181 * performance, but we have to check whether the name entry (to
1182 * be deleted) has agent object or not to avoid orphans.
1184 * We will improve that in the future, some possible solutions,
1186 * 1) Some hint from the caller via transaction handle to make
1187 * the lookup conditionally.
1188 * 2) Enhance the ZFS logic to recognize the OSD lookup result
1189 * and delete the given entry directly without lookup again
1190 * internally. LU-10190 */
1191 memset(&zde->lzd_fid, 0, sizeof(zde->lzd_fid));
1192 rc = osd_zap_lookup(osd, zap_dn->dn_object, zap_dn, name, 8, 3, zde);
1195 CERROR("%s: failed to locate entry %s: rc = %d\n",
1196 osd->od_svname, name, rc);
1200 if (unlikely(osd_remote_fid(env, osd, &zde->lzd_fid) > 0)) {
1201 rc = -dmu_object_free(osd->od_os, zde->lzd_reg.zde_dnode,
1204 CERROR("%s: failed to destroy agent object (%llu) "
1205 "for the entry %s: rc = %d\n", osd->od_svname,
1206 (__u64)zde->lzd_reg.zde_dnode, name, rc);
1209 /* Remove key from the ZAP */
1210 rc = osd_zap_remove(osd, zap_dn->dn_object, zap_dn,
1211 (char *)key, oh->ot_tx);
1213 CERROR("%s: zap_remove %s failed: rc = %d\n",
1214 osd->od_svname, name, rc);
1219 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
1220 struct dt_object *dt,
1223 struct osd_zap_it *it;
1225 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
1229 RETURN((struct dt_it *)it);
1233 * Move Iterator to record specified by \a key
1235 * \param di osd iterator
1236 * \param key key for index
1238 * \retval +ve di points to record with least key not larger than key
1239 * \retval 0 di points to exact matched key
1240 * \retval -ve failure
1242 static int osd_dir_it_get(const struct lu_env *env,
1243 struct dt_it *di, const struct dt_key *key)
1245 struct osd_zap_it *it = (struct osd_zap_it *)di;
1246 struct osd_object *obj = it->ozi_obj;
1247 char *name = (char *)key;
1252 LASSERT(it->ozi_zc);
1254 /* reset the cursor */
1255 zap_cursor_fini(it->ozi_zc);
1256 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
1258 /* XXX: implementation of the API is broken at the moment */
1259 LASSERT(((const char *)key)[0] == 0);
1266 if (name[0] == '.') {
1270 } else if (name[1] == '.' && name[2] == 0) {
1276 /* neither . nor .. - some real record */
1284 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
1286 /* PBS: do nothing : ref are incremented at retrive and decreamented
1291 * in Orion . and .. were stored in the directory, while ZPL
1292 * and current osd-zfs generate them up on request. so, we
1293 * need to ignore previously stored . and ..
1295 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
1296 zap_attribute_t *za)
1301 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1304 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
1305 if (za->za_name[1] == 0) {
1307 } else if (za->za_name[1] == '.' &&
1308 za->za_name[2] == 0) {
1311 if (unlikely(isdot))
1312 zap_cursor_advance(it->ozi_zc);
1314 } while (unlikely(rc == 0 && isdot));
1320 * to load a directory entry at a time and stored it in
1321 * iterator's in-memory data structure.
1323 * \param di, struct osd_it_ea, iterator's in memory structure
1325 * \retval +ve, iterator reached to end
1326 * \retval 0, iterator not reached to end
1327 * \retval -ve, on error
1329 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
1331 struct osd_zap_it *it = (struct osd_zap_it *)di;
1332 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1337 /* temp. storage should be enough for any key supported by ZFS */
1338 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
1341 * the first ->next() moves the cursor to .
1342 * the second ->next() moves the cursor to ..
1343 * then we get to the real records and have to verify any exist
1345 if (it->ozi_pos <= 2) {
1347 if (it->ozi_pos <=2)
1351 zap_cursor_advance(it->ozi_zc);
1355 * According to current API we need to return error if its last entry.
1356 * zap_cursor_advance() does not return any value. So we need to call
1357 * retrieve to check if there is any record. We should make
1358 * changes to Iterator API to not return status for this API
1360 rc = osd_index_retrieve_skip_dots(it, za);
1362 if (rc == -ENOENT) /* end of dir */
1368 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
1369 const struct dt_it *di)
1371 struct osd_zap_it *it = (struct osd_zap_it *)di;
1372 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1376 if (it->ozi_pos <= 1) {
1378 RETURN((struct dt_key *)".");
1379 } else if (it->ozi_pos == 2) {
1380 RETURN((struct dt_key *)"..");
1383 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
1384 RETURN(ERR_PTR(rc));
1386 strcpy(it->ozi_name, za->za_name);
1388 RETURN((struct dt_key *)it->ozi_name);
1391 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
1393 struct osd_zap_it *it = (struct osd_zap_it *)di;
1394 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1398 if (it->ozi_pos <= 1) {
1401 } else if (it->ozi_pos == 2) {
1405 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
1406 rc = strlen(za->za_name);
1412 osd_dirent_update(const struct lu_env *env, struct osd_device *dev,
1413 uint64_t zap, const char *key, struct luz_direntry *zde)
1419 tx = dmu_tx_create(dev->od_os);
1423 dmu_tx_hold_zap(tx, zap, TRUE, NULL);
1424 rc = -dmu_tx_assign(tx, TXG_WAIT);
1426 rc = -zap_update(dev->od_os, zap, key, 8, sizeof(*zde) / 8,
1427 (const void *)zde, tx);
1436 static int osd_update_entry_for_agent(const struct lu_env *env,
1437 struct osd_device *osd,
1438 uint64_t zap, const char *name,
1439 struct luz_direntry *zde, __u32 attr)
1441 dmu_tx_t *tx = NULL;
1445 if (attr & LUDA_VERIFY_DRYRUN)
1448 tx = dmu_tx_create(osd->od_os);
1450 GOTO(out, rc = -ENOMEM);
1452 dmu_tx_hold_sa_create(tx, osd_find_dnsize(osd, OSD_BASE_EA_IN_BONUS));
1453 dmu_tx_hold_zap(tx, zap, FALSE, NULL);
1454 rc = -dmu_tx_assign(tx, TXG_WAIT);
1460 rc = osd_create_agent_object(env, osd, zde, zap, tx);
1462 rc = -zap_update(osd->od_os, zap, name, 8, sizeof(*zde) / 8,
1463 (const void *)zde, tx);
1469 CDEBUG(D_LFSCK, "%s: Updated (%s) remote entry for "DFID": rc = %d\n",
1470 osd_name(osd), (attr & LUDA_VERIFY_DRYRUN) ? "(ro)" : "(rw)",
1471 PFID(&zde->lzd_fid), rc);
1475 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
1476 struct dt_rec *dtrec, __u32 attr)
1478 struct osd_zap_it *it = (struct osd_zap_it *)di;
1479 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
1480 struct osd_thread_info *info = osd_oti_get(env);
1481 struct luz_direntry *zde = &info->oti_zde;
1482 zap_attribute_t *za = &info->oti_za;
1483 struct lu_fid *fid = &info->oti_fid;
1484 struct osd_device *osd = osd_obj2dev(it->ozi_obj);
1489 if (it->ozi_pos <= 1) {
1490 lde->lde_hash = cpu_to_le64(1);
1491 strcpy(lde->lde_name, ".");
1492 lde->lde_namelen = cpu_to_le16(1);
1493 fid_cpu_to_le(&lde->lde_fid,
1494 lu_object_fid(&it->ozi_obj->oo_dt.do_lu));
1495 lde->lde_attrs = LUDA_FID;
1496 /* append lustre attributes */
1497 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
1498 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
1501 } else if (it->ozi_pos == 2) {
1502 lde->lde_hash = cpu_to_le64(2);
1503 strcpy(lde->lde_name, "..");
1504 lde->lde_namelen = cpu_to_le16(2);
1505 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, fid, NULL);
1507 fid_cpu_to_le(&lde->lde_fid, fid);
1508 lde->lde_attrs = LUDA_FID;
1509 } else if (rc != -ENOENT) {
1510 /* ENOENT happens at the root of filesystem, ignore */
1514 /* append lustre attributes */
1515 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
1516 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
1522 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1526 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
1527 namelen = strlen(za->za_name);
1528 if (namelen > NAME_MAX)
1530 strcpy(lde->lde_name, za->za_name);
1531 lde->lde_namelen = cpu_to_le16(namelen);
1533 if (za->za_integer_length != 8) {
1534 CERROR("%s: unsupported direntry format: %d %d\n",
1536 za->za_integer_length, (int)za->za_num_integers);
1540 rc = osd_zap_lookup(osd, it->ozi_zc->zc_zapobj, it->ozi_obj->oo_dn,
1541 za->za_name, za->za_integer_length, 3, zde);
1545 if (za->za_num_integers >= 3 && fid_is_sane(&zde->lzd_fid)) {
1546 lde->lde_attrs = LUDA_FID;
1547 fid_cpu_to_le(&lde->lde_fid, &zde->lzd_fid);
1548 if (unlikely(zde->lzd_reg.zde_dnode == ZFS_NO_OBJECT &&
1549 osd_remote_fid(env, osd, &zde->lzd_fid) > 0 &&
1550 attr & LUDA_VERIFY)) {
1551 /* It is mainly used for handling the MDT
1552 * upgraded from old ZFS based backend. */
1553 rc = osd_update_entry_for_agent(env, osd,
1554 it->ozi_obj->oo_dn->dn_object,
1555 za->za_name, zde, attr);
1557 lde->lde_attrs |= LUDA_REPAIR;
1559 lde->lde_attrs |= LUDA_UNKNOWN;
1562 GOTO(pack_attr, rc = 0);
1565 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
1568 rc = osd_get_fid_by_oid(env, osd, zde->lzd_reg.zde_dnode, fid);
1570 lde->lde_attrs = LUDA_UNKNOWN;
1571 GOTO(pack_attr, rc = 0);
1574 if (!(attr & LUDA_VERIFY)) {
1575 fid_cpu_to_le(&lde->lde_fid, fid);
1576 lde->lde_attrs = LUDA_FID;
1577 GOTO(pack_attr, rc = 0);
1580 if (attr & LUDA_VERIFY_DRYRUN) {
1581 fid_cpu_to_le(&lde->lde_fid, fid);
1582 lde->lde_attrs = LUDA_FID | LUDA_REPAIR;
1583 GOTO(pack_attr, rc = 0);
1586 fid_cpu_to_le(&lde->lde_fid, fid);
1587 lde->lde_attrs = LUDA_FID;
1588 zde->lzd_fid = *fid;
1589 rc = osd_dirent_update(env, osd, it->ozi_zc->zc_zapobj,
1592 lde->lde_attrs |= LUDA_UNKNOWN;
1593 GOTO(pack_attr, rc = 0);
1596 lde->lde_attrs |= LUDA_REPAIR;
1598 GOTO(pack_attr, rc = 0);
1601 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1602 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1606 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1609 struct osd_zap_it *it = (struct osd_zap_it *)di;
1610 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1615 if (it->ozi_pos <= 1)
1617 else if (it->ozi_pos == 2)
1621 rc = lu_dirent_calc_size(namelen, attr);
1625 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1626 if (unlikely(rc != 0))
1629 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1630 CERROR("%s: unsupported direntry format: %d %d\n",
1631 osd_obj2dev(it->ozi_obj)->od_svname,
1632 za->za_integer_length, (int)za->za_num_integers);
1636 namelen = strlen(za->za_name);
1637 if (namelen > NAME_MAX)
1640 rc = lu_dirent_calc_size(namelen, attr);
1645 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1647 struct osd_zap_it *it = (struct osd_zap_it *)di;
1651 if (it->ozi_pos <= 2)
1654 pos = osd_zap_cursor_serialize(it->ozi_zc);
1661 * rc == 0 -> end of directory.
1662 * rc > 0 -> ok, proceed.
1663 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1665 static int osd_dir_it_load(const struct lu_env *env,
1666 const struct dt_it *di, __u64 hash)
1668 struct osd_zap_it *it = (struct osd_zap_it *)di;
1669 struct osd_object *obj = it->ozi_obj;
1670 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1674 /* reset the cursor */
1675 zap_cursor_fini(it->ozi_zc);
1676 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1683 /* to return whether the end has been reached */
1684 rc = osd_index_retrieve_skip_dots(it, za);
1687 else if (rc == -ENOENT)
1694 struct dt_index_operations osd_dir_ops = {
1695 .dio_lookup = osd_dir_lookup,
1696 .dio_declare_insert = osd_declare_dir_insert,
1697 .dio_insert = osd_dir_insert,
1698 .dio_declare_delete = osd_declare_dir_delete,
1699 .dio_delete = osd_dir_delete,
1701 .init = osd_dir_it_init,
1702 .fini = osd_index_it_fini,
1703 .get = osd_dir_it_get,
1704 .put = osd_dir_it_put,
1705 .next = osd_dir_it_next,
1706 .key = osd_dir_it_key,
1707 .key_size = osd_dir_it_key_size,
1708 .rec = osd_dir_it_rec,
1709 .rec_size = osd_dir_it_rec_size,
1710 .store = osd_dir_it_store,
1711 .load = osd_dir_it_load
1716 * Primitives for index files using binary keys.
1719 /* key integer_size is 8 */
1720 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1721 const struct dt_key *src)
1728 /* align keysize to 64bit */
1729 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1730 size *= sizeof(__u64);
1732 LASSERT(size <= MAXNAMELEN);
1734 if (unlikely(size > o->oo_keysize))
1735 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1736 memcpy(dst, (const char *)src, o->oo_keysize);
1738 return (size/sizeof(__u64));
1741 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1742 struct dt_rec *rec, const struct dt_key *key)
1744 struct osd_object *obj = osd_dt_obj(dt);
1745 struct osd_device *osd = osd_obj2dev(obj);
1746 __u64 *k = osd_oti_get(env)->oti_key64;
1750 rc = osd_prepare_key_uint64(obj, k, key);
1752 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1753 k, rc, obj->oo_recusize, obj->oo_recsize,
1755 RETURN(rc == 0 ? 1 : rc);
1758 static int osd_declare_index_insert(const struct lu_env *env,
1759 struct dt_object *dt,
1760 const struct dt_rec *rec,
1761 const struct dt_key *key,
1764 struct osd_object *obj = osd_dt_obj(dt);
1765 struct osd_thandle *oh;
1768 LASSERT(th != NULL);
1769 oh = container_of0(th, struct osd_thandle, ot_super);
1771 LASSERT(obj->oo_dn);
1773 /* do not specify the key as then DMU is trying to look it up
1774 * which is very expensive. usually the layers above lookup
1775 * before insertion */
1776 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1782 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1783 const struct dt_rec *rec, const struct dt_key *key,
1784 struct thandle *th, int ignore_quota)
1786 struct osd_object *obj = osd_dt_obj(dt);
1787 struct osd_device *osd = osd_obj2dev(obj);
1788 struct osd_thandle *oh;
1789 __u64 *k = osd_oti_get(env)->oti_key64;
1793 LASSERT(obj->oo_dn);
1794 LASSERT(dt_object_exists(dt));
1795 LASSERT(osd_invariant(obj));
1796 LASSERT(th != NULL);
1798 oh = container_of0(th, struct osd_thandle, ot_super);
1800 rc = osd_prepare_key_uint64(obj, k, key);
1802 /* Insert (key,oid) into ZAP */
1803 rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1804 k, rc, obj->oo_recusize, obj->oo_recsize,
1805 (void *)rec, oh->ot_tx);
1809 static int osd_declare_index_delete(const struct lu_env *env,
1810 struct dt_object *dt,
1811 const struct dt_key *key,
1814 struct osd_object *obj = osd_dt_obj(dt);
1815 struct osd_thandle *oh;
1818 LASSERT(dt_object_exists(dt));
1819 LASSERT(osd_invariant(obj));
1820 LASSERT(th != NULL);
1821 LASSERT(obj->oo_dn);
1823 oh = container_of0(th, struct osd_thandle, ot_super);
1825 /* do not specify the key as then DMU is trying to look it up
1826 * which is very expensive. usually the layers above lookup
1827 * before deletion */
1828 osd_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
1834 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1835 const struct dt_key *key, struct thandle *th)
1837 struct osd_object *obj = osd_dt_obj(dt);
1838 struct osd_device *osd = osd_obj2dev(obj);
1839 struct osd_thandle *oh;
1840 __u64 *k = osd_oti_get(env)->oti_key64;
1844 LASSERT(obj->oo_dn);
1845 LASSERT(th != NULL);
1846 oh = container_of0(th, struct osd_thandle, ot_super);
1848 rc = osd_prepare_key_uint64(obj, k, key);
1850 /* Remove binary key from the ZAP */
1851 rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1856 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1857 const struct dt_key *key)
1859 struct osd_zap_it *it = (struct osd_zap_it *)di;
1860 struct osd_object *obj = it->ozi_obj;
1861 struct osd_device *osd = osd_obj2dev(obj);
1865 LASSERT(it->ozi_zc);
1868 * XXX: we need a binary version of zap_cursor_move_to_key()
1869 * to implement this API */
1870 if (*((const __u64 *)key) != 0)
1871 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1874 zap_cursor_fini(it->ozi_zc);
1875 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1881 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1883 struct osd_zap_it *it = (struct osd_zap_it *)di;
1884 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1888 if (it->ozi_reset == 0)
1889 zap_cursor_advance(it->ozi_zc);
1893 * According to current API we need to return error if it's last entry.
1894 * zap_cursor_advance() does not return any value. So we need to call
1895 * retrieve to check if there is any record. We should make
1896 * changes to Iterator API to not return status for this API
1898 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1905 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1906 const struct dt_it *di)
1908 struct osd_zap_it *it = (struct osd_zap_it *)di;
1909 struct osd_object *obj = it->ozi_obj;
1910 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1915 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1917 RETURN(ERR_PTR(rc));
1919 /* the binary key is stored in the name */
1920 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1922 RETURN((struct dt_key *)&it->ozi_key);
1925 static int osd_index_it_key_size(const struct lu_env *env,
1926 const struct dt_it *di)
1928 struct osd_zap_it *it = (struct osd_zap_it *)di;
1929 struct osd_object *obj = it->ozi_obj;
1930 RETURN(obj->oo_keysize);
1933 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1934 struct dt_rec *rec, __u32 attr)
1936 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1937 struct osd_zap_it *it = (struct osd_zap_it *)di;
1938 struct osd_object *obj = it->ozi_obj;
1939 struct osd_device *osd = osd_obj2dev(obj);
1940 __u64 *k = osd_oti_get(env)->oti_key64;
1945 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1949 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1951 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1952 k, rc, obj->oo_recusize, obj->oo_recsize,
1957 static __u64 osd_index_it_store(const struct lu_env *env,
1958 const struct dt_it *di)
1960 struct osd_zap_it *it = (struct osd_zap_it *)di;
1963 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1966 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1969 struct osd_zap_it *it = (struct osd_zap_it *)di;
1970 struct osd_object *obj = it->ozi_obj;
1971 struct osd_device *osd = osd_obj2dev(obj);
1972 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1976 /* reset the cursor */
1977 zap_cursor_fini(it->ozi_zc);
1978 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1979 obj->oo_dn->dn_object, hash);
1982 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1985 else if (rc == -ENOENT)
1991 static struct dt_index_operations osd_index_ops = {
1992 .dio_lookup = osd_index_lookup,
1993 .dio_declare_insert = osd_declare_index_insert,
1994 .dio_insert = osd_index_insert,
1995 .dio_declare_delete = osd_declare_index_delete,
1996 .dio_delete = osd_index_delete,
1998 .init = osd_index_it_init,
1999 .fini = osd_index_it_fini,
2000 .get = osd_index_it_get,
2001 .put = osd_index_it_put,
2002 .next = osd_index_it_next,
2003 .key = osd_index_it_key,
2004 .key_size = osd_index_it_key_size,
2005 .rec = osd_index_it_rec,
2006 .store = osd_index_it_store,
2007 .load = osd_index_it_load
2011 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2012 const struct dt_index_features *feat)
2014 struct osd_object *obj = osd_dt_obj(dt);
2018 down_read(&obj->oo_guard);
2021 * XXX: implement support for fixed-size keys sorted with natural
2022 * numerical way (not using internal hash value)
2024 if (feat->dif_flags & DT_IND_RANGE)
2025 GOTO(out, rc = -ERANGE);
2027 if (unlikely(feat == &dt_otable_features)) {
2028 dt->do_index_ops = &osd_otable_ops;
2032 LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
2033 if (likely(feat == &dt_directory_features)) {
2034 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
2035 dt->do_index_ops = &osd_dir_ops;
2037 GOTO(out, rc = -ENOTDIR);
2038 } else if (unlikely(feat == &dt_acct_features)) {
2039 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
2040 dt->do_index_ops = &osd_acct_index_ops;
2041 } else if (dt->do_index_ops == NULL) {
2042 /* For index file, we don't support variable key & record sizes
2043 * and the key has to be unique */
2044 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
2045 GOTO(out, rc = -EINVAL);
2047 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
2048 GOTO(out, rc = -E2BIG);
2049 if (feat->dif_keysize_max != feat->dif_keysize_min)
2050 GOTO(out, rc = -EINVAL);
2052 /* As for the record size, it should be a multiple of 8 bytes
2053 * and smaller than the maximum value length supported by ZAP.
2055 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
2056 GOTO(out, rc = -E2BIG);
2057 if (feat->dif_recsize_max != feat->dif_recsize_min)
2058 GOTO(out, rc = -EINVAL);
2060 obj->oo_keysize = feat->dif_keysize_max;
2061 obj->oo_recsize = feat->dif_recsize_max;
2062 obj->oo_recusize = 1;
2064 /* ZFS prefers to work with array of 64bits */
2065 if ((obj->oo_recsize & 7) == 0) {
2066 obj->oo_recsize >>= 3;
2067 obj->oo_recusize = 8;
2069 dt->do_index_ops = &osd_index_ops;
2073 up_read(&obj->oo_guard);