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, 2016, 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 <lustre_ver.h>
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
49 #include "osd_internal.h"
51 #include <sys/dnode.h>
56 #include <sys/spa_impl.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/dmu_objset.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/sa_impl.h>
64 static inline int osd_object_is_zap(dmu_buf_t *db)
66 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *) db;
72 rc = (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
73 dn->dn_type == DMU_OT_USERGROUP_USED);
79 /* We don't actually have direct access to the zap_hashbits() function
80 * so just pretend like we do for now. If this ever breaks we can look at
82 #define zap_hashbits(zc) 48
85 * | cd (16 bits) | hash (48 bits) |
86 * we need it in other form:
87 * |0| hash (48 bit) | cd (15 bit) |
88 * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
89 * the readdir hashes from multiple directory stripes uniformly on the client.
90 * Another point is sign bit, the hash range should be in [0, 2^63-1] because
91 * loff_t (for llseek) needs to be a positive value. This means the "cd" field
92 * should only be the low 15 bits.
94 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
96 uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
98 return (zfs_hash >> zap_hashbits(zc)) |
99 (zfs_hash << (63 - zap_hashbits(zc)));
102 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
103 uint64_t id, uint64_t dirhash)
105 uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
106 (dirhash >> (63 - zap_hashbits(zc)));
108 zap_cursor_init_serialized(zc, os, id, zfs_hash);
111 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
112 uint64_t id, uint64_t dirhash)
117 if (unlikely(t == NULL))
120 osd_zap_cursor_init_serialized(t, os, id, dirhash);
126 void osd_zap_cursor_fini(zap_cursor_t *zc)
132 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
133 struct osd_object *o,
136 struct osd_device *d = osd_obj2dev(o);
137 osd_zap_cursor_init_serialized(zc, d->od_os,
138 o->oo_db->db_object, dirhash);
141 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
144 struct osd_device *d = osd_obj2dev(o);
145 return osd_zap_cursor_init(zc, d->od_os, o->oo_db->db_object, dirhash);
148 static struct dt_it *osd_index_it_init(const struct lu_env *env,
149 struct dt_object *dt,
152 struct osd_thread_info *info = osd_oti_get(env);
153 struct osd_zap_it *it;
154 struct osd_object *obj = osd_dt_obj(dt);
155 struct lu_object *lo = &dt->do_lu;
159 if (obj->oo_destroyed)
160 RETURN(ERR_PTR(-ENOENT));
162 LASSERT(lu_object_exists(lo));
166 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
168 RETURN(ERR_PTR(-ENOMEM));
170 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
172 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
180 RETURN((struct dt_it *)it);
183 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
185 struct osd_zap_it *it = (struct osd_zap_it *)di;
186 struct osd_object *obj;
190 LASSERT(it->ozi_obj);
194 osd_zap_cursor_fini(it->ozi_zc);
195 lu_object_put(env, &obj->oo_dt.do_lu);
196 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
202 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
204 /* PBS: do nothing : ref are incremented at retrive and decreamented
208 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
211 const unsigned align = sizeof(struct luda_type) - 1;
212 struct luda_type *lt;
214 /* check if file type is required */
215 if (attr & LUDA_TYPE) {
216 len = (len + align) & ~align;
218 lt = (void *)ent->lde_name + len;
219 lt->lt_type = cpu_to_le16(DTTOIF(type));
220 ent->lde_attrs |= LUDA_TYPE;
223 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
227 * Get the object's FID from its LMA EA.
229 * \param[in] env pointer to the thread context
230 * \param[in] osd pointer to the OSD device
231 * \param[in] oid the object's local identifier
232 * \param[out] fid the buffer to hold the object's FID
234 * \retval 0 for success
235 * \retval negative error number on failure
237 static int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
238 uint64_t oid, struct lu_fid *fid)
240 struct objset *os = osd->od_os;
241 struct osd_thread_info *oti = osd_oti_get(env);
242 struct lustre_mdt_attrs *lma =
243 (struct lustre_mdt_attrs *)oti->oti_buf;
245 nvlist_t *sa_xattr = NULL;
246 sa_handle_t *sa_hdl = NULL;
247 uchar_t *nv_value = NULL;
248 uint64_t xattr = ZFS_NO_OBJECT;
253 rc = __osd_xattr_load(osd, oid, &sa_xattr);
260 rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
268 if (unlikely(size > sizeof(oti->oti_buf)))
269 GOTO(out, rc = -ERANGE);
271 memcpy(lma, nv_value, size);
276 rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
280 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
281 sa_handle_destroy(sa_hdl);
286 buf.lb_len = sizeof(oti->oti_buf);
287 rc = __osd_xattr_get_large(env, osd, xattr, &buf,
288 XATTR_NAME_LMA, &size);
293 if (size < sizeof(*lma))
294 GOTO(out, rc = -EIO);
296 lustre_lma_swab(lma);
297 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
298 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
299 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
300 "oid = %#llx\n", osd->od_svname,
301 lma->lma_incompat & ~LMA_INCOMPAT_SUPP, oid);
302 GOTO(out, rc = -EOPNOTSUPP);
304 *fid = lma->lma_self_fid;
309 if (sa_xattr != NULL)
310 nvlist_free(sa_xattr);
315 * As we don't know FID, we can't use LU object, so this function
316 * partially duplicate __osd_xattr_get() which is built around
317 * LU-object and uses it to cache data like regular EA dnode, etc
319 static int osd_find_parent_by_dnode(const struct lu_env *env,
323 struct osd_device *osd = osd_obj2dev(osd_dt_obj(o));
325 uint64_t dnode = ZFS_NO_OBJECT;
329 /* first of all, get parent dnode from own attributes */
330 LASSERT(osd_dt_obj(o)->oo_db);
331 rc = -sa_handle_get(osd->od_os, osd_dt_obj(o)->oo_db->db_object,
332 NULL, SA_HDL_PRIVATE, &sa_hdl);
336 rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
337 sa_handle_destroy(sa_hdl);
339 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
344 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
347 struct link_ea_header *leh;
348 struct link_ea_entry *lee;
353 buf.lb_buf = osd_oti_get(env)->oti_buf;
354 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
356 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
358 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
362 OBD_ALLOC(buf.lb_buf, rc);
363 if (buf.lb_buf == NULL)
366 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
370 if (rc < sizeof(*leh) + sizeof(*lee))
371 GOTO(out, rc = -EINVAL);
374 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
375 leh->leh_magic = LINK_EA_MAGIC;
376 leh->leh_reccount = __swab32(leh->leh_reccount);
377 leh->leh_len = __swab64(leh->leh_len);
379 if (leh->leh_magic != LINK_EA_MAGIC)
380 GOTO(out, rc = -EINVAL);
381 if (leh->leh_reccount == 0)
382 GOTO(out, rc = -ENODATA);
384 lee = (struct link_ea_entry *)(leh + 1);
385 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
389 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
390 OBD_FREE(buf.lb_buf, buf.lb_len);
393 /* this block can be enabled for additional verification
394 * it's trying to match FID from LinkEA vs. FID from LMA */
398 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
400 if (lu_fid_eq(fid, &fid2) == 0)
401 CERROR("wrong parent: "DFID" != "DFID"\n",
402 PFID(fid), PFID(&fid2));
406 /* no LinkEA is found, let's try to find the fid in parent's LMA */
407 if (unlikely(rc != 0))
408 rc = osd_find_parent_by_dnode(env, o, fid);
413 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
414 struct dt_rec *rec, const struct dt_key *key)
416 struct osd_thread_info *oti = osd_oti_get(env);
417 struct osd_object *obj = osd_dt_obj(dt);
418 struct osd_device *osd = osd_obj2dev(obj);
419 char *name = (char *)key;
423 if (name[0] == '.') {
425 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
426 memcpy(rec, f, sizeof(*f));
428 } else if (name[1] == '.' && name[2] == 0) {
429 rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
430 RETURN(rc == 0 ? 1 : rc);
434 memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
435 rc = -zap_lookup(osd->od_os, obj->oo_db->db_object,
436 (char *)key, 8, sizeof(oti->oti_zde) / 8,
437 (void *)&oti->oti_zde);
441 if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
442 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
446 rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode,
447 (struct lu_fid *)rec);
449 RETURN(rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc));
452 static int osd_declare_dir_insert(const struct lu_env *env,
453 struct dt_object *dt,
454 const struct dt_rec *rec,
455 const struct dt_key *key,
458 struct osd_object *obj = osd_dt_obj(dt);
459 struct osd_device *osd = osd_obj2dev(obj);
460 const struct dt_insert_rec *rec1;
461 const struct lu_fid *fid;
462 struct osd_thandle *oh;
466 rec1 = (struct dt_insert_rec *)rec;
468 LASSERT(fid != NULL);
469 LASSERT(rec1->rec_type != 0);
472 oh = container_of0(th, struct osd_thandle, ot_super);
474 /* This is for inserting dot/dotdot for new created dir. */
475 if (obj->oo_db == NULL)
476 object = DMU_NEW_OBJECT;
478 object = obj->oo_db->db_object;
480 /* do not specify the key as then DMU is trying to look it up
481 * which is very expensive. usually the layers above lookup
482 * before insertion */
483 dmu_tx_hold_zap(oh->ot_tx, object, TRUE, NULL);
485 osd_idc_find_or_init(env, osd, fid);
491 * Put the osd object once done with it.
493 * \param obj osd object that needs to be put
495 static inline void osd_object_put(const struct lu_env *env,
496 struct osd_object *obj)
498 lu_object_put(env, &obj->oo_dt.do_lu);
501 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
504 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
505 struct seq_server_site *ss = osd_seq_site(osd);
510 LASSERT(ss->ss_server_fld != NULL);
512 rc = osd_fld_lookup(env, osd, seq, range);
515 CERROR("%s: Can not lookup fld for %#llx\n",
520 RETURN(ss->ss_node_id == range->lsr_index);
523 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
524 const struct lu_fid *fid)
526 struct seq_server_site *ss = osd_seq_site(osd);
529 /* FID seqs not in FLDB, must be local seq */
530 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
533 /* If FLD is not being initialized yet, it only happens during the
534 * initialization, likely during mgs initialization, and we assume
535 * this is local FID. */
536 if (ss == NULL || ss->ss_server_fld == NULL)
539 /* Only check the local FLDB here */
540 if (osd_seq_exists(env, osd, fid_seq(fid)))
547 * Inserts (key, value) pair in \a directory object.
549 * \param dt osd index object
550 * \param key key for index
551 * \param rec record reference
552 * \param th transaction handler
553 * \param ignore_quota update should not affect quota
556 * \retval -ve failure
558 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
559 const struct dt_rec *rec, const struct dt_key *key,
560 struct thandle *th, int ignore_quota)
562 struct osd_thread_info *oti = osd_oti_get(env);
563 struct osd_object *parent = osd_dt_obj(dt);
564 struct osd_device *osd = osd_obj2dev(parent);
565 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
566 const struct lu_fid *fid = rec1->rec_fid;
567 struct osd_thandle *oh;
568 struct osd_idmap_cache *idc;
569 char *name = (char *)key;
573 LASSERT(parent->oo_db);
575 LASSERT(dt_object_exists(dt));
576 LASSERT(osd_invariant(parent));
579 oh = container_of0(th, struct osd_thandle, ot_super);
581 idc = osd_idc_find(env, osd, fid);
582 if (unlikely(idc == NULL)) {
583 /* this dt_insert() wasn't declared properly, so
584 * FID is missing in OI cache. we better do not
585 * lookup FID in FLDB/OI and don't risk to deadlock,
586 * but in some special cases (lfsck testing, etc)
587 * it's much simpler than fixing a caller */
588 CERROR("%s: "DFID" wasn't declared for insert\n",
589 osd_name(osd), PFID(fid));
590 idc = osd_idc_find_or_init(env, osd, fid);
592 RETURN(PTR_ERR(idc));
595 if (idc->oic_remote) {
596 /* Insert remote entry */
597 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
598 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
600 if (unlikely(idc->oic_dnode == 0)) {
601 /* for a reason OI cache wasn't filled properly */
602 CERROR("%s: OIC for "DFID" isn't filled\n",
603 osd_name(osd), PFID(fid));
606 if (name[0] == '.') {
608 /* do not store ".", instead generate it
609 * during iteration */
611 } else if (name[1] == '.' && name[2] == 0) {
612 uint64_t dnode = idc->oic_dnode;
613 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
616 /* update parent dnode in the child.
617 * later it will be used to generate ".." */
618 rc = osd_object_sa_update(parent,
625 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
626 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
627 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
628 oti->oti_zde.lzd_reg.zde_dnode = idc->oic_dnode;
631 oti->oti_zde.lzd_fid = *fid;
632 /* Insert (key,oid) into ZAP */
633 rc = -zap_add(osd->od_os, parent->oo_db->db_object,
634 (char *)key, 8, sizeof(oti->oti_zde) / 8,
635 (void *)&oti->oti_zde, oh->ot_tx);
636 if (unlikely(rc == -EEXIST &&
637 name[0] == '.' && name[1] == '.' && name[2] == 0))
638 /* Update (key,oid) in ZAP */
639 rc = -zap_update(osd->od_os, parent->oo_db->db_object,
640 (char *)key, 8, sizeof(oti->oti_zde) / 8,
641 (void *)&oti->oti_zde, oh->ot_tx);
648 static int osd_declare_dir_delete(const struct lu_env *env,
649 struct dt_object *dt,
650 const struct dt_key *key,
653 struct osd_object *obj = osd_dt_obj(dt);
654 struct osd_thandle *oh;
658 LASSERT(dt_object_exists(dt));
659 LASSERT(osd_invariant(obj));
662 oh = container_of0(th, struct osd_thandle, ot_super);
664 if (dt_object_exists(dt)) {
666 dnode = obj->oo_db->db_object;
668 dnode = DMU_NEW_OBJECT;
671 /* do not specify the key as then DMU is trying to look it up
672 * which is very expensive. usually the layers above lookup
674 dmu_tx_hold_zap(oh->ot_tx, dnode, FALSE, NULL);
679 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
680 const struct dt_key *key, struct thandle *th)
682 struct osd_object *obj = osd_dt_obj(dt);
683 struct osd_device *osd = osd_obj2dev(obj);
684 struct osd_thandle *oh;
685 dmu_buf_t *zap_db = obj->oo_db;
686 char *name = (char *)key;
693 oh = container_of0(th, struct osd_thandle, ot_super);
696 * In Orion . and .. were stored in the directory (not generated upon
697 * request as now). we preserve them for backward compatibility
699 if (name[0] == '.') {
702 } else if (name[1] == '.' && name[2] == 0) {
707 /* Remove key from the ZAP */
708 rc = -zap_remove(osd->od_os, zap_db->db_object,
709 (char *) key, oh->ot_tx);
711 if (unlikely(rc && rc != -ENOENT))
712 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
717 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
718 struct dt_object *dt,
721 struct osd_zap_it *it;
723 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
727 RETURN((struct dt_it *)it);
731 * Move Iterator to record specified by \a key
733 * \param di osd iterator
734 * \param key key for index
736 * \retval +ve di points to record with least key not larger than key
737 * \retval 0 di points to exact matched key
738 * \retval -ve failure
740 static int osd_dir_it_get(const struct lu_env *env,
741 struct dt_it *di, const struct dt_key *key)
743 struct osd_zap_it *it = (struct osd_zap_it *)di;
744 struct osd_object *obj = it->ozi_obj;
745 char *name = (char *)key;
752 /* reset the cursor */
753 zap_cursor_fini(it->ozi_zc);
754 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
756 /* XXX: implementation of the API is broken at the moment */
757 LASSERT(((const char *)key)[0] == 0);
764 if (name[0] == '.') {
768 } else if (name[1] == '.' && name[2] == 0) {
774 /* neither . nor .. - some real record */
782 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
784 /* PBS: do nothing : ref are incremented at retrive and decreamented
789 * in Orion . and .. were stored in the directory, while ZPL
790 * and current osd-zfs generate them up on request. so, we
791 * need to ignore previously stored . and ..
793 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
799 rc = -zap_cursor_retrieve(it->ozi_zc, za);
802 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
803 if (za->za_name[1] == 0) {
805 } else if (za->za_name[1] == '.' &&
806 za->za_name[2] == 0) {
810 zap_cursor_advance(it->ozi_zc);
812 } while (unlikely(rc == 0 && isdot));
818 * to load a directory entry at a time and stored it in
819 * iterator's in-memory data structure.
821 * \param di, struct osd_it_ea, iterator's in memory structure
823 * \retval +ve, iterator reached to end
824 * \retval 0, iterator not reached to end
825 * \retval -ve, on error
827 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
829 struct osd_zap_it *it = (struct osd_zap_it *)di;
830 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
835 /* temp. storage should be enough for any key supported by ZFS */
836 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
839 * the first ->next() moves the cursor to .
840 * the second ->next() moves the cursor to ..
841 * then we get to the real records and have to verify any exist
843 if (it->ozi_pos <= 2) {
849 zap_cursor_advance(it->ozi_zc);
853 * According to current API we need to return error if its last entry.
854 * zap_cursor_advance() does not return any value. So we need to call
855 * retrieve to check if there is any record. We should make
856 * changes to Iterator API to not return status for this API
858 rc = osd_index_retrieve_skip_dots(it, za);
860 if (rc == -ENOENT) /* end of dir */
866 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
867 const struct dt_it *di)
869 struct osd_zap_it *it = (struct osd_zap_it *)di;
870 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
874 if (it->ozi_pos <= 1) {
876 RETURN((struct dt_key *)".");
877 } else if (it->ozi_pos == 2) {
878 RETURN((struct dt_key *)"..");
881 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
884 strcpy(it->ozi_name, za->za_name);
886 RETURN((struct dt_key *)it->ozi_name);
889 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
891 struct osd_zap_it *it = (struct osd_zap_it *)di;
892 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
896 if (it->ozi_pos <= 1) {
899 } else if (it->ozi_pos == 2) {
903 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
904 rc = strlen(za->za_name);
909 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
910 struct dt_rec *dtrec, __u32 attr)
912 struct osd_zap_it *it = (struct osd_zap_it *)di;
913 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
914 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
915 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
919 if (it->ozi_pos <= 1) {
920 lde->lde_hash = cpu_to_le64(1);
921 strcpy(lde->lde_name, ".");
922 lde->lde_namelen = cpu_to_le16(1);
923 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
924 lde->lde_attrs = LUDA_FID;
925 /* append lustre attributes */
926 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
927 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
931 } else if (it->ozi_pos == 2) {
932 lde->lde_hash = cpu_to_le64(2);
933 strcpy(lde->lde_name, "..");
934 lde->lde_namelen = cpu_to_le16(2);
935 lde->lde_attrs = LUDA_FID;
936 /* append lustre attributes */
937 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
938 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
939 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
941 /* ENOENT happens at the root of filesystem so ignore it */
949 rc = -zap_cursor_retrieve(it->ozi_zc, za);
950 if (unlikely(rc != 0))
953 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
954 namelen = strlen(za->za_name);
955 if (namelen > NAME_MAX)
956 GOTO(out, rc = -EOVERFLOW);
957 strcpy(lde->lde_name, za->za_name);
958 lde->lde_namelen = cpu_to_le16(namelen);
960 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
961 CERROR("%s: unsupported direntry format: %d %d\n",
962 osd_obj2dev(it->ozi_obj)->od_svname,
963 za->za_integer_length, (int)za->za_num_integers);
965 GOTO(out, rc = -EIO);
968 rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
969 za->za_name, za->za_integer_length, 3, zde);
973 lde->lde_fid = zde->lzd_fid;
974 lde->lde_attrs = LUDA_FID;
976 /* append lustre attributes */
977 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
979 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
985 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
988 struct osd_zap_it *it = (struct osd_zap_it *)di;
989 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
994 if (it->ozi_pos <= 1)
996 else if (it->ozi_pos == 2)
1000 rc = lu_dirent_calc_size(namelen, attr);
1004 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1005 if (unlikely(rc != 0))
1008 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1009 CERROR("%s: unsupported direntry format: %d %d\n",
1010 osd_obj2dev(it->ozi_obj)->od_svname,
1011 za->za_integer_length, (int)za->za_num_integers);
1015 namelen = strlen(za->za_name);
1016 if (namelen > NAME_MAX)
1019 rc = lu_dirent_calc_size(namelen, attr);
1024 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1026 struct osd_zap_it *it = (struct osd_zap_it *)di;
1030 if (it->ozi_pos <= 2)
1033 pos = osd_zap_cursor_serialize(it->ozi_zc);
1040 * rc == 0 -> end of directory.
1041 * rc > 0 -> ok, proceed.
1042 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1044 static int osd_dir_it_load(const struct lu_env *env,
1045 const struct dt_it *di, __u64 hash)
1047 struct osd_zap_it *it = (struct osd_zap_it *)di;
1048 struct osd_object *obj = it->ozi_obj;
1049 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1053 /* reset the cursor */
1054 zap_cursor_fini(it->ozi_zc);
1055 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1062 /* to return whether the end has been reached */
1063 rc = osd_index_retrieve_skip_dots(it, za);
1066 else if (rc == -ENOENT)
1073 struct dt_index_operations osd_dir_ops = {
1074 .dio_lookup = osd_dir_lookup,
1075 .dio_declare_insert = osd_declare_dir_insert,
1076 .dio_insert = osd_dir_insert,
1077 .dio_declare_delete = osd_declare_dir_delete,
1078 .dio_delete = osd_dir_delete,
1080 .init = osd_dir_it_init,
1081 .fini = osd_index_it_fini,
1082 .get = osd_dir_it_get,
1083 .put = osd_dir_it_put,
1084 .next = osd_dir_it_next,
1085 .key = osd_dir_it_key,
1086 .key_size = osd_dir_it_key_size,
1087 .rec = osd_dir_it_rec,
1088 .rec_size = osd_dir_it_rec_size,
1089 .store = osd_dir_it_store,
1090 .load = osd_dir_it_load
1095 * Primitives for index files using binary keys.
1098 /* key integer_size is 8 */
1099 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1100 const struct dt_key *src)
1107 /* align keysize to 64bit */
1108 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1109 size *= sizeof(__u64);
1111 LASSERT(size <= MAXNAMELEN);
1113 if (unlikely(size > o->oo_keysize))
1114 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1115 memcpy(dst, (const char *)src, o->oo_keysize);
1117 return (size/sizeof(__u64));
1120 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1121 struct dt_rec *rec, const struct dt_key *key)
1123 struct osd_object *obj = osd_dt_obj(dt);
1124 struct osd_device *osd = osd_obj2dev(obj);
1125 __u64 *k = osd_oti_get(env)->oti_key64;
1129 rc = osd_prepare_key_uint64(obj, k, key);
1131 rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1132 k, rc, obj->oo_recusize, obj->oo_recsize,
1134 RETURN(rc == 0 ? 1 : rc);
1137 static int osd_declare_index_insert(const struct lu_env *env,
1138 struct dt_object *dt,
1139 const struct dt_rec *rec,
1140 const struct dt_key *key,
1143 struct osd_object *obj = osd_dt_obj(dt);
1144 struct osd_thandle *oh;
1147 LASSERT(th != NULL);
1148 oh = container_of0(th, struct osd_thandle, ot_super);
1150 LASSERT(obj->oo_db);
1152 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1154 /* do not specify the key as then DMU is trying to look it up
1155 * which is very expensive. usually the layers above lookup
1156 * before insertion */
1157 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1162 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1163 const struct dt_rec *rec, const struct dt_key *key,
1164 struct thandle *th, int ignore_quota)
1166 struct osd_object *obj = osd_dt_obj(dt);
1167 struct osd_device *osd = osd_obj2dev(obj);
1168 struct osd_thandle *oh;
1169 __u64 *k = osd_oti_get(env)->oti_key64;
1173 LASSERT(obj->oo_db);
1174 LASSERT(dt_object_exists(dt));
1175 LASSERT(osd_invariant(obj));
1176 LASSERT(th != NULL);
1178 oh = container_of0(th, struct osd_thandle, ot_super);
1180 rc = osd_prepare_key_uint64(obj, k, key);
1182 /* Insert (key,oid) into ZAP */
1183 rc = -zap_add_uint64(osd->od_os, obj->oo_db->db_object,
1184 k, rc, obj->oo_recusize, obj->oo_recsize,
1185 (void *)rec, oh->ot_tx);
1189 static int osd_declare_index_delete(const struct lu_env *env,
1190 struct dt_object *dt,
1191 const struct dt_key *key,
1194 struct osd_object *obj = osd_dt_obj(dt);
1195 struct osd_thandle *oh;
1198 LASSERT(dt_object_exists(dt));
1199 LASSERT(osd_invariant(obj));
1200 LASSERT(th != NULL);
1201 LASSERT(obj->oo_db);
1203 oh = container_of0(th, struct osd_thandle, ot_super);
1205 /* do not specify the key as then DMU is trying to look it up
1206 * which is very expensive. usually the layers above lookup
1207 * before deletion */
1208 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, FALSE, NULL);
1213 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1214 const struct dt_key *key, struct thandle *th)
1216 struct osd_object *obj = osd_dt_obj(dt);
1217 struct osd_device *osd = osd_obj2dev(obj);
1218 struct osd_thandle *oh;
1219 __u64 *k = osd_oti_get(env)->oti_key64;
1223 LASSERT(obj->oo_db);
1224 LASSERT(th != NULL);
1225 oh = container_of0(th, struct osd_thandle, ot_super);
1227 rc = osd_prepare_key_uint64(obj, k, key);
1229 /* Remove binary key from the ZAP */
1230 rc = -zap_remove_uint64(osd->od_os, obj->oo_db->db_object,
1235 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1236 const struct dt_key *key)
1238 struct osd_zap_it *it = (struct osd_zap_it *)di;
1239 struct osd_object *obj = it->ozi_obj;
1240 struct osd_device *osd = osd_obj2dev(obj);
1244 LASSERT(it->ozi_zc);
1247 * XXX: we need a binary version of zap_cursor_move_to_key()
1248 * to implement this API */
1249 if (*((const __u64 *)key) != 0)
1250 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1253 zap_cursor_fini(it->ozi_zc);
1254 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_db->db_object);
1260 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1262 struct osd_zap_it *it = (struct osd_zap_it *)di;
1263 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1267 if (it->ozi_reset == 0)
1268 zap_cursor_advance(it->ozi_zc);
1272 * According to current API we need to return error if it's last entry.
1273 * zap_cursor_advance() does not return any value. So we need to call
1274 * retrieve to check if there is any record. We should make
1275 * changes to Iterator API to not return status for this API
1277 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1284 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1285 const struct dt_it *di)
1287 struct osd_zap_it *it = (struct osd_zap_it *)di;
1288 struct osd_object *obj = it->ozi_obj;
1289 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1294 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1296 RETURN(ERR_PTR(rc));
1298 /* the binary key is stored in the name */
1299 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1301 RETURN((struct dt_key *)&it->ozi_key);
1304 static int osd_index_it_key_size(const struct lu_env *env,
1305 const struct dt_it *di)
1307 struct osd_zap_it *it = (struct osd_zap_it *)di;
1308 struct osd_object *obj = it->ozi_obj;
1309 RETURN(obj->oo_keysize);
1312 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1313 struct dt_rec *rec, __u32 attr)
1315 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1316 struct osd_zap_it *it = (struct osd_zap_it *)di;
1317 struct osd_object *obj = it->ozi_obj;
1318 struct osd_device *osd = osd_obj2dev(obj);
1319 __u64 *k = osd_oti_get(env)->oti_key64;
1324 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1328 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1330 rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1331 k, rc, obj->oo_recusize, obj->oo_recsize,
1336 static __u64 osd_index_it_store(const struct lu_env *env,
1337 const struct dt_it *di)
1339 struct osd_zap_it *it = (struct osd_zap_it *)di;
1342 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1345 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1348 struct osd_zap_it *it = (struct osd_zap_it *)di;
1349 struct osd_object *obj = it->ozi_obj;
1350 struct osd_device *osd = osd_obj2dev(obj);
1351 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1355 /* reset the cursor */
1356 zap_cursor_fini(it->ozi_zc);
1357 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1358 obj->oo_db->db_object, hash);
1361 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1364 else if (rc == -ENOENT)
1370 static struct dt_index_operations osd_index_ops = {
1371 .dio_lookup = osd_index_lookup,
1372 .dio_declare_insert = osd_declare_index_insert,
1373 .dio_insert = osd_index_insert,
1374 .dio_declare_delete = osd_declare_index_delete,
1375 .dio_delete = osd_index_delete,
1377 .init = osd_index_it_init,
1378 .fini = osd_index_it_fini,
1379 .get = osd_index_it_get,
1380 .put = osd_index_it_put,
1381 .next = osd_index_it_next,
1382 .key = osd_index_it_key,
1383 .key_size = osd_index_it_key_size,
1384 .rec = osd_index_it_rec,
1385 .store = osd_index_it_store,
1386 .load = osd_index_it_load
1390 struct osd_metadnode_it {
1391 struct osd_device *mit_dev;
1393 struct lu_fid mit_fid;
1395 __u64 mit_prefetched_dnode;
1398 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1399 struct dt_object *dt, __u32 attr)
1401 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1402 struct osd_metadnode_it *it;
1406 if (unlikely(it == NULL))
1407 RETURN(ERR_PTR(-ENOMEM));
1411 /* XXX: dmu_object_next() does NOT find dnodes allocated
1412 * in the current non-committed txg, so we force txg
1413 * commit to find all existing dnodes ... */
1414 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1416 RETURN((struct dt_it *)it);
1419 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1421 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1426 static int osd_zfs_otable_it_get(const struct lu_env *env,
1427 struct dt_it *di, const struct dt_key *key)
1432 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1436 #define OTABLE_PREFETCH 256
1438 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1439 struct osd_metadnode_it *it)
1441 struct osd_device *dev = it->mit_dev;
1444 /* can go negative on the very first access to the iterator
1445 * or if some non-Lustre objects were found */
1446 if (unlikely(it->mit_prefetched < 0))
1447 it->mit_prefetched = 0;
1449 if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1452 if (it->mit_prefetched_dnode == 0)
1453 it->mit_prefetched_dnode = it->mit_pos;
1455 while (it->mit_prefetched < OTABLE_PREFETCH) {
1456 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1458 if (unlikely(rc != 0))
1461 osd_dmu_prefetch(dev->od_os, it->mit_prefetched_dnode,
1462 0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1464 it->mit_prefetched++;
1468 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1470 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1471 struct lustre_mdt_attrs *lma;
1472 struct osd_device *dev = it->mit_dev;
1473 nvlist_t *nvbuf = NULL;
1478 memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1480 dnode = it->mit_pos;
1482 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1483 if (unlikely(rc != 0))
1485 it->mit_prefetched--;
1487 /* LMA is required for this to be a Lustre object.
1488 * If there is no xattr skip it. */
1489 rc = __osd_xattr_load(dev, it->mit_pos, &nvbuf);
1490 if (unlikely(rc != 0))
1493 LASSERT(nvbuf != NULL);
1494 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1495 if (likely(rc == 0)) {
1497 lma = (struct lustre_mdt_attrs *)v;
1498 lustre_lma_swab(lma);
1499 it->mit_fid = lma->lma_self_fid;
1503 /* not a Lustre object, try next one */
1510 /* we aren't prefetching in the above loop because the number of
1511 * non-Lustre objects is very small and we will be repeating very
1512 * rare. in case we want to use this to iterate over non-Lustre
1513 * objects (i.e. when we convert regular ZFS in Lustre) it makes
1514 * sense to initiate prefetching in the loop */
1516 /* 0 - there are more items, +1 - the end */
1517 if (likely(rc == 0))
1518 osd_zfs_otable_prefetch(env, it);
1520 CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1521 it->mit_pos, PFID(&it->mit_fid), rc);
1527 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1528 const struct dt_it *di)
1533 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1534 const struct dt_it *di)
1536 return sizeof(__u64);
1539 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1540 const struct dt_it *di,
1541 struct dt_rec *rec, __u32 attr)
1543 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1544 struct lu_fid *fid = (struct lu_fid *)rec;
1553 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1554 const struct dt_it *di)
1556 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1561 static int osd_zfs_otable_it_load(const struct lu_env *env,
1562 const struct dt_it *di, __u64 hash)
1564 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1567 it->mit_prefetched = 0;
1568 it->mit_prefetched_dnode = 0;
1570 return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1573 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1574 const struct dt_it *di, void *key_rec)
1579 const struct dt_index_operations osd_zfs_otable_ops = {
1581 .init = osd_zfs_otable_it_init,
1582 .fini = osd_zfs_otable_it_fini,
1583 .get = osd_zfs_otable_it_get,
1584 .put = osd_zfs_otable_it_put,
1585 .next = osd_zfs_otable_it_next,
1586 .key = osd_zfs_otable_it_key,
1587 .key_size = osd_zfs_otable_it_key_size,
1588 .rec = osd_zfs_otable_it_rec,
1589 .store = osd_zfs_otable_it_store,
1590 .load = osd_zfs_otable_it_load,
1591 .key_rec = osd_zfs_otable_it_key_rec,
1595 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1596 const struct dt_index_features *feat)
1598 struct osd_object *obj = osd_dt_obj(dt);
1602 down_read(&obj->oo_guard);
1605 * XXX: implement support for fixed-size keys sorted with natural
1606 * numerical way (not using internal hash value)
1608 if (feat->dif_flags & DT_IND_RANGE)
1609 GOTO(out, rc = -ERANGE);
1611 if (unlikely(feat == &dt_otable_features)) {
1612 dt->do_index_ops = &osd_zfs_otable_ops;
1616 LASSERT(!dt_object_exists(dt) || obj->oo_db != NULL);
1617 if (likely(feat == &dt_directory_features)) {
1618 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_db))
1619 dt->do_index_ops = &osd_dir_ops;
1621 GOTO(out, rc = -ENOTDIR);
1622 } else if (unlikely(feat == &dt_acct_features)) {
1623 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1624 dt->do_index_ops = &osd_acct_index_ops;
1625 } else if (dt->do_index_ops == NULL) {
1626 /* For index file, we don't support variable key & record sizes
1627 * and the key has to be unique */
1628 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1629 GOTO(out, rc = -EINVAL);
1631 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1632 GOTO(out, rc = -E2BIG);
1633 if (feat->dif_keysize_max != feat->dif_keysize_min)
1634 GOTO(out, rc = -EINVAL);
1636 /* As for the record size, it should be a multiple of 8 bytes
1637 * and smaller than the maximum value length supported by ZAP.
1639 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1640 GOTO(out, rc = -E2BIG);
1641 if (feat->dif_recsize_max != feat->dif_recsize_min)
1642 GOTO(out, rc = -EINVAL);
1644 obj->oo_keysize = feat->dif_keysize_max;
1645 obj->oo_recsize = feat->dif_recsize_max;
1646 obj->oo_recusize = 1;
1648 /* ZFS prefers to work with array of 64bits */
1649 if ((obj->oo_recsize & 7) == 0) {
1650 obj->oo_recsize >>= 3;
1651 obj->oo_recusize = 8;
1653 dt->do_index_ops = &osd_index_ops;
1657 up_read(&obj->oo_guard);