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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * Copyright (c) 2012, 2013, Intel Corporation.
32 * Use is subject to license terms.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/osd-zfs/osd_index.c
40 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41 * Author: Mike Pershin <tappro@whamcloud.com>
44 #define DEBUG_SUBSYSTEM S_OSD
46 #include <lustre_ver.h>
47 #include <libcfs/libcfs.h>
48 #include <obd_support.h>
49 #include <lustre_net.h>
51 #include <obd_class.h>
52 #include <lustre_disk.h>
53 #include <lustre_fid.h>
55 #include "osd_internal.h"
57 #include <sys/dnode.h>
62 #include <sys/spa_impl.h>
63 #include <sys/zfs_znode.h>
64 #include <sys/dmu_tx.h>
65 #include <sys/dmu_objset.h>
66 #include <sys/dsl_prop.h>
67 #include <sys/sa_impl.h>
70 static inline int osd_object_is_zap(dmu_buf_t *db)
72 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *) db;
78 rc = (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
79 dn->dn_type == DMU_OT_USERGROUP_USED);
85 /* We don't actually have direct access to the zap_hashbits() function
86 * so just pretend like we do for now. If this ever breaks we can look at
88 #define zap_hashbits(zc) 48
91 * | cd (16 bits) | hash (48 bits) |
92 * we need it in other form:
93 * |0| hash (48 bit) | cd (15 bit) |
94 * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
95 * the readdir hashes from multiple directory stripes uniformly on the client.
96 * Another point is sign bit, the hash range should be in [0, 2^63-1] because
97 * loff_t (for llseek) needs to be a positive value. This means the "cd" field
98 * should only be the low 15 bits.
100 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
102 uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
104 return (zfs_hash >> zap_hashbits(zc)) |
105 (zfs_hash << (63 - zap_hashbits(zc)));
108 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
109 uint64_t id, uint64_t dirhash)
111 uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
112 (dirhash >> (63 - zap_hashbits(zc)));
114 zap_cursor_init_serialized(zc, os, id, zfs_hash);
117 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
118 uint64_t id, uint64_t dirhash)
123 if (unlikely(t == NULL))
126 osd_zap_cursor_init_serialized(t, os, id, dirhash);
132 void osd_zap_cursor_fini(zap_cursor_t *zc)
138 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
139 struct osd_object *o,
142 struct osd_device *d = osd_obj2dev(o);
143 zap_cursor_init_serialized(zc, d->od_os, o->oo_db->db_object, dirhash);
146 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
149 struct osd_device *d = osd_obj2dev(o);
150 return osd_zap_cursor_init(zc, d->od_os, o->oo_db->db_object, dirhash);
153 static struct dt_it *osd_index_it_init(const struct lu_env *env,
154 struct dt_object *dt,
156 struct lustre_capa *capa)
158 struct osd_thread_info *info = osd_oti_get(env);
159 struct osd_zap_it *it;
160 struct osd_object *obj = osd_dt_obj(dt);
161 struct lu_object *lo = &dt->do_lu;
165 /* XXX: check capa ? */
167 LASSERT(lu_object_exists(lo));
169 LASSERT(osd_object_is_zap(obj->oo_db));
172 if (info->oti_it_inline) {
175 RETURN(ERR_PTR(-ENOMEM));
177 it = &info->oti_it_zap;
178 info->oti_it_inline = 1;
181 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
183 if (it != &info->oti_it_zap)
186 info->oti_it_inline = 0;
196 RETURN((struct dt_it *)it);
199 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
201 struct osd_thread_info *info = osd_oti_get(env);
202 struct osd_zap_it *it = (struct osd_zap_it *)di;
203 struct osd_object *obj;
207 LASSERT(it->ozi_obj);
211 osd_zap_cursor_fini(it->ozi_zc);
212 lu_object_put(env, &obj->oo_dt.do_lu);
213 if (it != &info->oti_it_zap)
216 info->oti_it_inline = 0;
222 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
224 /* PBS: do nothing : ref are incremented at retrive and decreamented
228 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
231 const unsigned align = sizeof(struct luda_type) - 1;
232 struct luda_type *lt;
234 /* check if file type is required */
235 if (attr & LUDA_TYPE) {
236 len = (len + align) & ~align;
238 lt = (void *)ent->lde_name + len;
239 lt->lt_type = cpu_to_le16(DTTOIF(type));
240 ent->lde_attrs |= LUDA_TYPE;
243 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
247 * as we don't know FID, we can't use LU object, so this function
248 * partially duplicate __osd_xattr_get() which is built around
249 * LU-object and uses it to cache data like regular EA dnode, etc
251 static int osd_find_parent_by_dnode(const struct lu_env *env,
255 struct osd_device *osd = osd_obj2dev(osd_dt_obj(o));
256 struct lustre_mdt_attrs *lma;
259 nvlist_t *nvbuf = NULL;
265 /* first of all, get parent dnode from own attributes */
266 LASSERT(osd_dt_obj(o)->oo_db);
267 rc = -sa_handle_get(osd->od_os, osd_dt_obj(o)->oo_db->db_object,
268 NULL, SA_HDL_PRIVATE, &sa_hdl);
272 dnode = ZFS_NO_OBJECT;
273 rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
274 sa_handle_destroy(sa_hdl);
278 /* now get EA buffer */
279 rc = __osd_xattr_load(osd, dnode, &nvbuf);
283 /* XXX: if we get that far.. should we cache the result? */
285 /* try to find LMA attribute */
286 LASSERT(nvbuf != NULL);
287 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &value, &size);
288 if (rc == 0 && size >= sizeof(*lma)) {
289 lma = (struct lustre_mdt_attrs *)value;
290 lustre_lma_swab(lma);
291 *fid = lma->lma_self_fid;
296 /* no LMA attribute in SA, let's try regular EA */
298 /* first of all, get parent dnode storing regular EA */
299 rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
303 dnode = ZFS_NO_OBJECT;
304 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &dnode, 8);
305 sa_handle_destroy(sa_hdl);
309 CLASSERT(sizeof(*lma) <= sizeof(osd_oti_get(env)->oti_buf));
310 buf.lb_buf = osd_oti_get(env)->oti_buf;
311 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
313 /* now try to find LMA */
314 rc = __osd_xattr_get_large(env, osd, dnode, &buf,
315 XATTR_NAME_LMA, &size);
316 if (rc == 0 && size >= sizeof(*lma)) {
318 lustre_lma_swab(lma);
319 *fid = lma->lma_self_fid;
324 GOTO(out, rc = -EIO);
333 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
336 struct link_ea_header *leh;
337 struct link_ea_entry *lee;
342 buf.lb_buf = osd_oti_get(env)->oti_buf;
343 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
345 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
347 rc = osd_xattr_get(env, o, &LU_BUF_NULL,
348 XATTR_NAME_LINK, BYPASS_CAPA);
352 OBD_ALLOC(buf.lb_buf, rc);
353 if (buf.lb_buf == NULL)
356 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
360 if (rc < sizeof(*leh) + sizeof(*lee))
361 GOTO(out, rc = -EINVAL);
364 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
365 leh->leh_magic = LINK_EA_MAGIC;
366 leh->leh_reccount = __swab32(leh->leh_reccount);
367 leh->leh_len = __swab64(leh->leh_len);
369 if (leh->leh_magic != LINK_EA_MAGIC)
370 GOTO(out, rc = -EINVAL);
371 if (leh->leh_reccount == 0)
372 GOTO(out, rc = -ENODATA);
374 lee = (struct link_ea_entry *)(leh + 1);
375 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
379 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
380 OBD_FREE(buf.lb_buf, buf.lb_len);
383 /* this block can be enabled for additional verification
384 * it's trying to match FID from LinkEA vs. FID from LMA */
388 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
390 if (lu_fid_eq(fid, &fid2) == 0)
391 CERROR("wrong parent: "DFID" != "DFID"\n",
392 PFID(fid), PFID(&fid2));
396 /* no LinkEA is found, let's try to find the fid in parent's LMA */
397 if (unlikely(rc != 0))
398 rc = osd_find_parent_by_dnode(env, o, fid);
403 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
404 struct dt_rec *rec, const struct dt_key *key,
405 struct lustre_capa *capa)
407 struct osd_thread_info *oti = osd_oti_get(env);
408 struct osd_object *obj = osd_dt_obj(dt);
409 struct osd_device *osd = osd_obj2dev(obj);
410 char *name = (char *)key;
414 LASSERT(osd_object_is_zap(obj->oo_db));
416 if (name[0] == '.') {
418 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
419 memcpy(rec, f, sizeof(*f));
421 } else if (name[1] == '.' && name[2] == 0) {
422 rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
423 RETURN(rc == 0 ? 1 : rc);
427 rc = -zap_lookup(osd->od_os, obj->oo_db->db_object,
428 (char *)key, 8, sizeof(oti->oti_zde) / 8,
429 (void *)&oti->oti_zde);
430 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
432 RETURN(rc == 0 ? 1 : rc);
435 static int osd_declare_dir_insert(const struct lu_env *env,
436 struct dt_object *dt,
437 const struct dt_rec *rec,
438 const struct dt_key *key,
441 struct osd_object *obj = osd_dt_obj(dt);
442 struct osd_thandle *oh;
447 oh = container_of0(th, struct osd_thandle, ot_super);
449 /* This is for inserting dot/dotdot for new created dir. */
450 if (obj->oo_db == NULL)
451 object = DMU_NEW_OBJECT;
453 object = obj->oo_db->db_object;
455 dmu_tx_hold_bonus(oh->ot_tx, object);
456 dmu_tx_hold_zap(oh->ot_tx, object, TRUE, (char *)key);
462 * Find the osd object for given fid.
464 * \param fid need to find the osd object having this fid
466 * \retval osd_object on success
467 * \retval -ve on error
469 struct osd_object *osd_object_find(const struct lu_env *env,
470 struct dt_object *dt,
471 const struct lu_fid *fid)
473 struct lu_device *ludev = dt->do_lu.lo_dev;
474 struct osd_object *child = NULL;
475 struct lu_object *luch;
476 struct lu_object *lo;
479 * at this point topdev might not exist yet
480 * (i.e. MGS is preparing profiles). so we can
481 * not rely on topdev and instead lookup with
482 * our device passed as topdev. this can't work
483 * if the object isn't cached yet (as osd doesn't
484 * allocate lu_header). IOW, the object must be
485 * in the cache, otherwise lu_object_alloc() crashes
488 luch = lu_object_find_at(env, ludev, fid, NULL);
492 if (lu_object_exists(luch)) {
493 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
497 LU_OBJECT_DEBUG(D_ERROR, env, luch,
498 "%s: object can't be located "DFID"\n",
499 osd_dev(ludev)->od_svname, PFID(fid));
502 lu_object_put(env, luch);
503 CERROR("%s: Unable to get osd_object "DFID"\n",
504 osd_dev(ludev)->od_svname, PFID(fid));
505 child = ERR_PTR(-ENOENT);
508 LU_OBJECT_DEBUG(D_ERROR, env, luch,
509 "%s: lu_object does not exists "DFID"\n",
510 osd_dev(ludev)->od_svname, PFID(fid));
511 lu_object_put(env, luch);
512 child = ERR_PTR(-ENOENT);
519 * Put the osd object once done with it.
521 * \param obj osd object that needs to be put
523 static inline void osd_object_put(const struct lu_env *env,
524 struct osd_object *obj)
526 lu_object_put(env, &obj->oo_dt.do_lu);
529 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
532 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
533 struct seq_server_site *ss = osd_seq_site(osd);
538 LASSERT(ss->ss_server_fld != NULL);
540 rc = osd_fld_lookup(env, osd, seq, range);
542 CERROR("%s: Can not lookup fld for "LPX64"\n",
547 RETURN(ss->ss_node_id == range->lsr_index);
550 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
551 const struct lu_fid *fid)
553 struct seq_server_site *ss = osd_seq_site(osd);
556 /* FID seqs not in FLDB, must be local seq */
557 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
560 /* If FLD is not being initialized yet, it only happens during the
561 * initialization, likely during mgs initialization, and we assume
562 * this is local FID. */
563 if (ss == NULL || ss->ss_server_fld == NULL)
566 /* Only check the local FLDB here */
567 if (osd_seq_exists(env, osd, fid_seq(fid)))
574 * Inserts (key, value) pair in \a directory object.
576 * \param dt osd index object
577 * \param key key for index
578 * \param rec record reference
579 * \param th transaction handler
580 * \param capa capability descriptor
581 * \param ignore_quota update should not affect quota
584 * \retval -ve failure
586 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
587 const struct dt_rec *rec, const struct dt_key *key,
588 struct thandle *th, struct lustre_capa *capa,
591 struct osd_thread_info *oti = osd_oti_get(env);
592 struct osd_object *parent = osd_dt_obj(dt);
593 struct osd_device *osd = osd_obj2dev(parent);
594 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
595 const struct lu_fid *fid = rec1->rec_fid;
596 struct osd_thandle *oh;
597 struct osd_object *child = NULL;
599 char *name = (char *)key;
603 LASSERT(parent->oo_db);
604 LASSERT(osd_object_is_zap(parent->oo_db));
606 LASSERT(dt_object_exists(dt));
607 LASSERT(osd_invariant(parent));
610 oh = container_of0(th, struct osd_thandle, ot_super);
612 rc = osd_remote_fid(env, osd, fid);
614 CERROR("%s: Can not find object "DFID": rc = %d\n",
615 osd->od_svname, PFID(fid), rc);
619 if (unlikely(rc == 1)) {
620 /* Insert remote entry */
621 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
622 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
625 * To simulate old Orion setups with ./.. stored in the
628 /* Insert local entry */
629 child = osd_object_find(env, dt, fid);
631 RETURN(PTR_ERR(child));
633 LASSERT(child->oo_db);
634 if (name[0] == '.') {
636 /* do not store ".", instead generate it
637 * during iteration */
639 } else if (name[1] == '.' && name[2] == 0) {
640 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
641 struct lu_fid tfid = *fid;
643 osd_object_put(env, child);
645 child = osd_object_find(env, dt, &tfid);
647 RETURN(PTR_ERR(child));
649 LASSERT(child->oo_db);
652 /* update parent dnode in the child.
653 * later it will be used to generate ".." */
654 rc = osd_object_sa_update(parent,
656 &child->oo_db->db_object,
662 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
663 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
664 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
665 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
666 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
669 oti->oti_zde.lzd_fid = *fid;
670 /* Insert (key,oid) into ZAP */
671 rc = -zap_add(osd->od_os, parent->oo_db->db_object,
672 (char *)key, 8, sizeof(oti->oti_zde) / 8,
673 (void *)&oti->oti_zde, oh->ot_tx);
674 if (unlikely(rc == -EEXIST &&
675 name[0] == '.' && name[1] == '.' && name[2] == 0))
676 /* Update (key,oid) in ZAP */
677 rc = -zap_update(osd->od_os, parent->oo_db->db_object,
678 (char *)key, 8, sizeof(oti->oti_zde) / 8,
679 (void *)&oti->oti_zde, oh->ot_tx);
683 osd_object_put(env, child);
688 static int osd_declare_dir_delete(const struct lu_env *env,
689 struct dt_object *dt,
690 const struct dt_key *key,
693 struct osd_object *obj = osd_dt_obj(dt);
694 struct osd_thandle *oh;
697 LASSERT(dt_object_exists(dt));
698 LASSERT(osd_invariant(obj));
701 oh = container_of0(th, struct osd_thandle, ot_super);
704 LASSERT(osd_object_is_zap(obj->oo_db));
706 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
711 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
712 const struct dt_key *key, struct thandle *th,
713 struct lustre_capa *capa)
715 struct osd_object *obj = osd_dt_obj(dt);
716 struct osd_device *osd = osd_obj2dev(obj);
717 struct osd_thandle *oh;
718 dmu_buf_t *zap_db = obj->oo_db;
719 char *name = (char *)key;
724 LASSERT(osd_object_is_zap(obj->oo_db));
727 oh = container_of0(th, struct osd_thandle, ot_super);
730 * In Orion . and .. were stored in the directory (not generated upon
731 * request as now). we preserve them for backward compatibility
733 if (name[0] == '.') {
736 } else if (name[1] == '.' && name[2] == 0) {
741 /* Remove key from the ZAP */
742 rc = -zap_remove(osd->od_os, zap_db->db_object,
743 (char *) key, oh->ot_tx);
745 if (unlikely(rc && rc != -ENOENT))
746 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
751 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
752 struct dt_object *dt,
754 struct lustre_capa *capa)
756 struct osd_zap_it *it;
758 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
762 RETURN((struct dt_it *)it);
766 * Move Iterator to record specified by \a key
768 * \param di osd iterator
769 * \param key key for index
771 * \retval +ve di points to record with least key not larger than key
772 * \retval 0 di points to exact matched key
773 * \retval -ve failure
775 static int osd_dir_it_get(const struct lu_env *env,
776 struct dt_it *di, const struct dt_key *key)
778 struct osd_zap_it *it = (struct osd_zap_it *)di;
779 struct osd_object *obj = it->ozi_obj;
780 char *name = (char *)key;
787 /* reset the cursor */
788 zap_cursor_fini(it->ozi_zc);
789 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
791 /* XXX: implementation of the API is broken at the moment */
792 LASSERT(((const char *)key)[0] == 0);
799 if (name[0] == '.') {
803 } else if (name[1] == '.' && name[2] == 0) {
809 /* neither . nor .. - some real record */
817 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
819 /* PBS: do nothing : ref are incremented at retrive and decreamented
824 * in Orion . and .. were stored in the directory, while ZPL
825 * and current osd-zfs generate them up on request. so, we
826 * need to ignore previously stored . and ..
828 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
834 rc = -zap_cursor_retrieve(it->ozi_zc, za);
837 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
838 if (za->za_name[1] == 0) {
840 } else if (za->za_name[1] == '.' &&
841 za->za_name[2] == 0) {
845 zap_cursor_advance(it->ozi_zc);
847 } while (unlikely(rc == 0 && isdot));
853 * to load a directory entry at a time and stored it in
854 * iterator's in-memory data structure.
856 * \param di, struct osd_it_ea, iterator's in memory structure
858 * \retval +ve, iterator reached to end
859 * \retval 0, iterator not reached to end
860 * \retval -ve, on error
862 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
864 struct osd_zap_it *it = (struct osd_zap_it *)di;
865 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
870 /* temp. storage should be enough for any key supported by ZFS */
871 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
874 * the first ->next() moves the cursor to .
875 * the second ->next() moves the cursor to ..
876 * then we get to the real records and have to verify any exist
878 if (it->ozi_pos <= 2) {
884 zap_cursor_advance(it->ozi_zc);
888 * According to current API we need to return error if its last entry.
889 * zap_cursor_advance() does not return any value. So we need to call
890 * retrieve to check if there is any record. We should make
891 * changes to Iterator API to not return status for this API
893 rc = osd_index_retrieve_skip_dots(it, za);
895 if (rc == -ENOENT) /* end of dir */
901 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
902 const struct dt_it *di)
904 struct osd_zap_it *it = (struct osd_zap_it *)di;
905 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
909 if (it->ozi_pos <= 1) {
911 RETURN((struct dt_key *)".");
912 } else if (it->ozi_pos == 2) {
913 RETURN((struct dt_key *)"..");
916 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
919 strcpy(it->ozi_name, za->za_name);
921 RETURN((struct dt_key *)it->ozi_name);
924 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
926 struct osd_zap_it *it = (struct osd_zap_it *)di;
927 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
931 if (it->ozi_pos <= 1) {
934 } else if (it->ozi_pos == 2) {
938 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
939 rc = strlen(za->za_name);
944 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
945 struct dt_rec *dtrec, __u32 attr)
947 struct osd_zap_it *it = (struct osd_zap_it *)di;
948 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
949 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
950 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
954 if (it->ozi_pos <= 1) {
955 lde->lde_hash = cpu_to_le64(1);
956 strcpy(lde->lde_name, ".");
957 lde->lde_namelen = cpu_to_le16(1);
958 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
959 lde->lde_attrs = LUDA_FID;
960 /* append lustre attributes */
961 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
962 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
966 } else if (it->ozi_pos == 2) {
967 lde->lde_hash = cpu_to_le64(2);
968 strcpy(lde->lde_name, "..");
969 lde->lde_namelen = cpu_to_le16(2);
970 lde->lde_attrs = LUDA_FID;
971 /* append lustre attributes */
972 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
973 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
974 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
976 * early Orion code was not setting LinkEA, so it's possible
977 * some setups still have objects with no LinkEA set.
978 * but at that time .. was a real record in the directory
979 * so we should try to lookup .. in ZAP
987 rc = -zap_cursor_retrieve(it->ozi_zc, za);
988 if (unlikely(rc != 0))
991 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
992 namelen = strlen(za->za_name);
993 if (namelen > NAME_MAX)
994 GOTO(out, rc = -EOVERFLOW);
995 strcpy(lde->lde_name, za->za_name);
996 lde->lde_namelen = cpu_to_le16(namelen);
998 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
999 CERROR("%s: unsupported direntry format: %d %d\n",
1000 osd_obj2dev(it->ozi_obj)->od_svname,
1001 za->za_integer_length, (int)za->za_num_integers);
1003 GOTO(out, rc = -EIO);
1006 rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
1007 za->za_name, za->za_integer_length, 3, zde);
1011 lde->lde_fid = zde->lzd_fid;
1012 lde->lde_attrs = LUDA_FID;
1014 /* append lustre attributes */
1015 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1017 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1023 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1026 struct osd_zap_it *it = (struct osd_zap_it *)di;
1027 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1032 if (it->ozi_pos <= 1)
1034 else if (it->ozi_pos == 2)
1038 rc = lu_dirent_calc_size(namelen, attr);
1042 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1043 if (unlikely(rc != 0))
1046 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1047 CERROR("%s: unsupported direntry format: %d %d\n",
1048 osd_obj2dev(it->ozi_obj)->od_svname,
1049 za->za_integer_length, (int)za->za_num_integers);
1053 namelen = strlen(za->za_name);
1054 if (namelen > NAME_MAX)
1057 rc = lu_dirent_calc_size(namelen, attr);
1062 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1064 struct osd_zap_it *it = (struct osd_zap_it *)di;
1068 if (it->ozi_pos <= 2)
1071 pos = osd_zap_cursor_serialize(it->ozi_zc);
1078 * rc == 0 -> end of directory.
1079 * rc > 0 -> ok, proceed.
1080 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1082 static int osd_dir_it_load(const struct lu_env *env,
1083 const struct dt_it *di, __u64 hash)
1085 struct osd_zap_it *it = (struct osd_zap_it *)di;
1086 struct osd_object *obj = it->ozi_obj;
1087 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1091 /* reset the cursor */
1092 zap_cursor_fini(it->ozi_zc);
1093 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1100 /* to return whether the end has been reached */
1101 rc = osd_index_retrieve_skip_dots(it, za);
1104 else if (rc == -ENOENT)
1111 struct dt_index_operations osd_dir_ops = {
1112 .dio_lookup = osd_dir_lookup,
1113 .dio_declare_insert = osd_declare_dir_insert,
1114 .dio_insert = osd_dir_insert,
1115 .dio_declare_delete = osd_declare_dir_delete,
1116 .dio_delete = osd_dir_delete,
1118 .init = osd_dir_it_init,
1119 .fini = osd_index_it_fini,
1120 .get = osd_dir_it_get,
1121 .put = osd_dir_it_put,
1122 .next = osd_dir_it_next,
1123 .key = osd_dir_it_key,
1124 .key_size = osd_dir_it_key_size,
1125 .rec = osd_dir_it_rec,
1126 .rec_size = osd_dir_it_rec_size,
1127 .store = osd_dir_it_store,
1128 .load = osd_dir_it_load
1133 * Primitives for index files using binary keys.
1136 /* key integer_size is 8 */
1137 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1138 const struct dt_key *src)
1145 /* align keysize to 64bit */
1146 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1147 size *= sizeof(__u64);
1149 LASSERT(size <= MAXNAMELEN);
1151 if (unlikely(size > o->oo_keysize))
1152 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1153 memcpy(dst, (const char *)src, o->oo_keysize);
1155 return (size/sizeof(__u64));
1158 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1159 struct dt_rec *rec, const struct dt_key *key,
1160 struct lustre_capa *capa)
1162 struct osd_object *obj = osd_dt_obj(dt);
1163 struct osd_device *osd = osd_obj2dev(obj);
1164 __u64 *k = osd_oti_get(env)->oti_key64;
1168 rc = osd_prepare_key_uint64(obj, k, key);
1170 rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1171 k, rc, obj->oo_recusize, obj->oo_recsize,
1173 RETURN(rc == 0 ? 1 : rc);
1176 static int osd_declare_index_insert(const struct lu_env *env,
1177 struct dt_object *dt,
1178 const struct dt_rec *rec,
1179 const struct dt_key *key,
1182 struct osd_object *obj = osd_dt_obj(dt);
1183 struct osd_thandle *oh;
1186 LASSERT(th != NULL);
1187 oh = container_of0(th, struct osd_thandle, ot_super);
1189 LASSERT(obj->oo_db);
1191 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1193 /* It is not clear what API should be used for binary keys, so we pass
1194 * a null name which has the side effect of over-reserving space,
1195 * accounting for the worst case. See zap_count_write() */
1196 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1201 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1202 const struct dt_rec *rec, const struct dt_key *key,
1203 struct thandle *th, struct lustre_capa *capa,
1206 struct osd_object *obj = osd_dt_obj(dt);
1207 struct osd_device *osd = osd_obj2dev(obj);
1208 struct osd_thandle *oh;
1209 __u64 *k = osd_oti_get(env)->oti_key64;
1213 LASSERT(obj->oo_db);
1214 LASSERT(dt_object_exists(dt));
1215 LASSERT(osd_invariant(obj));
1216 LASSERT(th != NULL);
1218 oh = container_of0(th, struct osd_thandle, ot_super);
1220 rc = osd_prepare_key_uint64(obj, k, key);
1222 /* Insert (key,oid) into ZAP */
1223 rc = -zap_add_uint64(osd->od_os, obj->oo_db->db_object,
1224 k, rc, obj->oo_recusize, obj->oo_recsize,
1225 (void *)rec, oh->ot_tx);
1229 static int osd_declare_index_delete(const struct lu_env *env,
1230 struct dt_object *dt,
1231 const struct dt_key *key,
1234 struct osd_object *obj = osd_dt_obj(dt);
1235 struct osd_thandle *oh;
1238 LASSERT(dt_object_exists(dt));
1239 LASSERT(osd_invariant(obj));
1240 LASSERT(th != NULL);
1241 LASSERT(obj->oo_db);
1243 oh = container_of0(th, struct osd_thandle, ot_super);
1244 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1249 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1250 const struct dt_key *key, struct thandle *th,
1251 struct lustre_capa *capa)
1253 struct osd_object *obj = osd_dt_obj(dt);
1254 struct osd_device *osd = osd_obj2dev(obj);
1255 struct osd_thandle *oh;
1256 __u64 *k = osd_oti_get(env)->oti_key64;
1260 LASSERT(obj->oo_db);
1261 LASSERT(th != NULL);
1262 oh = container_of0(th, struct osd_thandle, ot_super);
1264 rc = osd_prepare_key_uint64(obj, k, key);
1266 /* Remove binary key from the ZAP */
1267 rc = -zap_remove_uint64(osd->od_os, obj->oo_db->db_object,
1272 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1273 const struct dt_key *key)
1275 struct osd_zap_it *it = (struct osd_zap_it *)di;
1276 struct osd_object *obj = it->ozi_obj;
1277 struct osd_device *osd = osd_obj2dev(obj);
1281 LASSERT(it->ozi_zc);
1284 * XXX: we need a binary version of zap_cursor_move_to_key()
1285 * to implement this API */
1286 if (*((const __u64 *)key) != 0)
1287 CERROR("NOT IMPLEMETED YET (move to "LPX64")\n",
1290 zap_cursor_fini(it->ozi_zc);
1291 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_db->db_object);
1297 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1299 struct osd_zap_it *it = (struct osd_zap_it *)di;
1300 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1304 if (it->ozi_reset == 0)
1305 zap_cursor_advance(it->ozi_zc);
1309 * According to current API we need to return error if it's last entry.
1310 * zap_cursor_advance() does not return any value. So we need to call
1311 * retrieve to check if there is any record. We should make
1312 * changes to Iterator API to not return status for this API
1314 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1321 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1322 const struct dt_it *di)
1324 struct osd_zap_it *it = (struct osd_zap_it *)di;
1325 struct osd_object *obj = it->ozi_obj;
1326 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1331 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1333 RETURN(ERR_PTR(rc));
1335 /* the binary key is stored in the name */
1336 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1338 RETURN((struct dt_key *)&it->ozi_key);
1341 static int osd_index_it_key_size(const struct lu_env *env,
1342 const struct dt_it *di)
1344 struct osd_zap_it *it = (struct osd_zap_it *)di;
1345 struct osd_object *obj = it->ozi_obj;
1346 RETURN(obj->oo_keysize);
1349 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1350 struct dt_rec *rec, __u32 attr)
1352 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1353 struct osd_zap_it *it = (struct osd_zap_it *)di;
1354 struct osd_object *obj = it->ozi_obj;
1355 struct osd_device *osd = osd_obj2dev(obj);
1356 __u64 *k = osd_oti_get(env)->oti_key64;
1361 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1365 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1367 rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1368 k, rc, obj->oo_recusize, obj->oo_recsize,
1373 static __u64 osd_index_it_store(const struct lu_env *env,
1374 const struct dt_it *di)
1376 struct osd_zap_it *it = (struct osd_zap_it *)di;
1379 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1382 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1385 struct osd_zap_it *it = (struct osd_zap_it *)di;
1386 struct osd_object *obj = it->ozi_obj;
1387 struct osd_device *osd = osd_obj2dev(obj);
1388 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1392 /* reset the cursor */
1393 zap_cursor_fini(it->ozi_zc);
1394 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1395 obj->oo_db->db_object, hash);
1398 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1401 else if (rc == -ENOENT)
1407 static struct dt_index_operations osd_index_ops = {
1408 .dio_lookup = osd_index_lookup,
1409 .dio_declare_insert = osd_declare_index_insert,
1410 .dio_insert = osd_index_insert,
1411 .dio_declare_delete = osd_declare_index_delete,
1412 .dio_delete = osd_index_delete,
1414 .init = osd_index_it_init,
1415 .fini = osd_index_it_fini,
1416 .get = osd_index_it_get,
1417 .put = osd_index_it_put,
1418 .next = osd_index_it_next,
1419 .key = osd_index_it_key,
1420 .key_size = osd_index_it_key_size,
1421 .rec = osd_index_it_rec,
1422 .store = osd_index_it_store,
1423 .load = osd_index_it_load
1427 struct osd_metadnode_it {
1428 struct osd_device *mit_dev;
1430 struct lu_fid mit_fid;
1432 __u64 mit_prefetched_dnode;
1435 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1436 struct dt_object *dt, __u32 attr,
1437 struct lustre_capa *capa)
1439 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1440 struct osd_metadnode_it *it;
1444 if (unlikely(it == NULL))
1445 RETURN(ERR_PTR(-ENOMEM));
1449 /* XXX: dmu_object_next() does NOT find dnodes allocated
1450 * in the current non-committed txg, so we force txg
1451 * commit to find all existing dnodes ... */
1452 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1454 RETURN((struct dt_it *)it);
1457 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1459 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1464 static int osd_zfs_otable_it_get(const struct lu_env *env,
1465 struct dt_it *di, const struct dt_key *key)
1470 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1474 #define OTABLE_PREFETCH 256
1476 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1477 struct osd_metadnode_it *it)
1479 struct osd_device *dev = it->mit_dev;
1482 /* can go negative on the very first access to the iterator
1483 * or if some non-Lustre objects were found */
1484 if (unlikely(it->mit_prefetched < 0))
1485 it->mit_prefetched = 0;
1487 if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1490 if (it->mit_prefetched_dnode == 0)
1491 it->mit_prefetched_dnode = it->mit_pos;
1493 while (it->mit_prefetched < OTABLE_PREFETCH) {
1494 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1496 if (unlikely(rc != 0))
1499 /* dmu_prefetch() was exported in 0.6.2, if you use with
1500 * an older release, just comment it out - this is an
1502 dmu_prefetch(dev->od_os, it->mit_prefetched_dnode, 0, 0);
1504 it->mit_prefetched++;
1508 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1510 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1511 struct lustre_mdt_attrs *lma;
1512 struct osd_device *dev = it->mit_dev;
1513 nvlist_t *nvbuf = NULL;
1518 memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1520 dnode = it->mit_pos;
1522 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1523 if (unlikely(rc != 0))
1525 it->mit_prefetched--;
1527 /* LMA is required for this to be a Lustre object.
1528 * If there is no xattr skip it. */
1529 rc = __osd_xattr_load(dev, it->mit_pos, &nvbuf);
1530 if (unlikely(rc != 0))
1533 LASSERT(nvbuf != NULL);
1534 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1535 if (likely(rc == 0)) {
1537 lma = (struct lustre_mdt_attrs *)v;
1538 lustre_lma_swab(lma);
1539 it->mit_fid = lma->lma_self_fid;
1543 /* not a Lustre object, try next one */
1550 /* we aren't prefetching in the above loop because the number of
1551 * non-Lustre objects is very small and we will be repeating very
1552 * rare. in case we want to use this to iterate over non-Lustre
1553 * objects (i.e. when we convert regular ZFS in Lustre) it makes
1554 * sense to initiate prefetching in the loop */
1556 /* 0 - there are more items, +1 - the end */
1557 if (likely(rc == 0))
1558 osd_zfs_otable_prefetch(env, it);
1560 CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1561 it->mit_pos, PFID(&it->mit_fid), rc);
1567 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1568 const struct dt_it *di)
1573 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1574 const struct dt_it *di)
1576 return sizeof(__u64);
1579 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1580 const struct dt_it *di,
1581 struct dt_rec *rec, __u32 attr)
1583 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1584 struct lu_fid *fid = (struct lu_fid *)rec;
1593 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1594 const struct dt_it *di)
1596 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1601 static int osd_zfs_otable_it_load(const struct lu_env *env,
1602 const struct dt_it *di, __u64 hash)
1604 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1607 it->mit_prefetched = 0;
1608 it->mit_prefetched_dnode = 0;
1610 return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1613 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1614 const struct dt_it *di, void *key_rec)
1619 const struct dt_index_operations osd_zfs_otable_ops = {
1621 .init = osd_zfs_otable_it_init,
1622 .fini = osd_zfs_otable_it_fini,
1623 .get = osd_zfs_otable_it_get,
1624 .put = osd_zfs_otable_it_put,
1625 .next = osd_zfs_otable_it_next,
1626 .key = osd_zfs_otable_it_key,
1627 .key_size = osd_zfs_otable_it_key_size,
1628 .rec = osd_zfs_otable_it_rec,
1629 .store = osd_zfs_otable_it_store,
1630 .load = osd_zfs_otable_it_load,
1631 .key_rec = osd_zfs_otable_it_key_rec,
1635 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1636 const struct dt_index_features *feat)
1638 struct osd_object *obj = osd_dt_obj(dt);
1641 LASSERT(dt_object_exists(dt));
1644 * XXX: implement support for fixed-size keys sorted with natural
1645 * numerical way (not using internal hash value)
1647 if (feat->dif_flags & DT_IND_RANGE)
1650 if (unlikely(feat == &dt_otable_features)) {
1651 dt->do_index_ops = &osd_zfs_otable_ops;
1655 LASSERT(obj->oo_db != NULL);
1656 if (likely(feat == &dt_directory_features)) {
1657 if (osd_object_is_zap(obj->oo_db))
1658 dt->do_index_ops = &osd_dir_ops;
1661 } else if (unlikely(feat == &dt_acct_features)) {
1662 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1663 dt->do_index_ops = &osd_acct_index_ops;
1664 } else if (osd_object_is_zap(obj->oo_db) &&
1665 dt->do_index_ops == NULL) {
1666 /* For index file, we don't support variable key & record sizes
1667 * and the key has to be unique */
1668 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1671 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1673 if (feat->dif_keysize_max != feat->dif_keysize_min)
1676 /* As for the record size, it should be a multiple of 8 bytes
1677 * and smaller than the maximum value length supported by ZAP.
1679 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1681 if (feat->dif_recsize_max != feat->dif_recsize_min)
1684 obj->oo_keysize = feat->dif_keysize_max;
1685 obj->oo_recsize = feat->dif_recsize_max;
1686 obj->oo_recusize = 1;
1688 /* ZFS prefers to work with array of 64bits */
1689 if ((obj->oo_recsize & 7) == 0) {
1690 obj->oo_recsize >>= 3;
1691 obj->oo_recusize = 8;
1693 dt->do_index_ops = &osd_index_ops;