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.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd-zfs/osd_index.c
38 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39 * Author: Mike Pershin <tappro@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_OSD
44 #include <lustre_ver.h>
45 #include <libcfs/libcfs.h>
46 #include <obd_support.h>
47 #include <lustre_net.h>
49 #include <obd_class.h>
50 #include <lustre_disk.h>
51 #include <lustre_fid.h>
53 #include "osd_internal.h"
55 #include <sys/dnode.h>
60 #include <sys/spa_impl.h>
61 #include <sys/zfs_znode.h>
62 #include <sys/dmu_tx.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/dsl_prop.h>
65 #include <sys/sa_impl.h>
68 static inline int osd_object_is_zap(dmu_buf_t *db)
70 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *) db;
76 rc = (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
77 dn->dn_type == DMU_OT_USERGROUP_USED);
83 /* We don't actually have direct access to the zap_hashbits() function
84 * so just pretend like we do for now. If this ever breaks we can look at
86 #define zap_hashbits(zc) 48
89 * | cd (16 bits) | hash (48 bits) |
90 * we need it in other form:
91 * |0| hash (48 bit) | cd (15 bit) |
92 * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
93 * the readdir hashes from multiple directory stripes uniformly on the client.
94 * Another point is sign bit, the hash range should be in [0, 2^63-1] because
95 * loff_t (for llseek) needs to be a positive value. This means the "cd" field
96 * should only be the low 15 bits.
98 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
100 uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
102 return (zfs_hash >> zap_hashbits(zc)) |
103 (zfs_hash << (63 - zap_hashbits(zc)));
106 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
107 uint64_t id, uint64_t dirhash)
109 uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
110 (dirhash >> (63 - zap_hashbits(zc)));
112 zap_cursor_init_serialized(zc, os, id, zfs_hash);
115 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
116 uint64_t id, uint64_t dirhash)
121 if (unlikely(t == NULL))
124 osd_zap_cursor_init_serialized(t, os, id, dirhash);
130 void osd_zap_cursor_fini(zap_cursor_t *zc)
136 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
137 struct osd_object *o,
140 struct osd_device *d = osd_obj2dev(o);
141 zap_cursor_init_serialized(zc, d->od_os, o->oo_db->db_object, dirhash);
144 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
147 struct osd_device *d = osd_obj2dev(o);
148 return osd_zap_cursor_init(zc, d->od_os, o->oo_db->db_object, dirhash);
151 static struct dt_it *osd_index_it_init(const struct lu_env *env,
152 struct dt_object *dt,
154 struct lustre_capa *capa)
156 struct osd_thread_info *info = osd_oti_get(env);
157 struct osd_zap_it *it;
158 struct osd_object *obj = osd_dt_obj(dt);
159 struct lu_object *lo = &dt->do_lu;
163 /* XXX: check capa ? */
165 LASSERT(lu_object_exists(lo));
167 LASSERT(osd_object_is_zap(obj->oo_db));
170 if (info->oti_it_inline) {
173 RETURN(ERR_PTR(-ENOMEM));
175 it = &info->oti_it_zap;
176 info->oti_it_inline = 1;
179 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
181 if (it != &info->oti_it_zap)
184 info->oti_it_inline = 0;
194 RETURN((struct dt_it *)it);
197 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
199 struct osd_thread_info *info = osd_oti_get(env);
200 struct osd_zap_it *it = (struct osd_zap_it *)di;
201 struct osd_object *obj;
205 LASSERT(it->ozi_obj);
209 osd_zap_cursor_fini(it->ozi_zc);
210 lu_object_put(env, &obj->oo_dt.do_lu);
211 if (it != &info->oti_it_zap)
214 info->oti_it_inline = 0;
220 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
222 /* PBS: do nothing : ref are incremented at retrive and decreamented
226 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
229 const unsigned align = sizeof(struct luda_type) - 1;
230 struct luda_type *lt;
232 /* check if file type is required */
233 if (attr & LUDA_TYPE) {
234 len = (len + align) & ~align;
236 lt = (void *)ent->lde_name + len;
237 lt->lt_type = cpu_to_le16(DTTOIF(type));
238 ent->lde_attrs |= LUDA_TYPE;
241 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
245 * as we don't know FID, we can't use LU object, so this function
246 * partially duplicate __osd_xattr_get() which is built around
247 * LU-object and uses it to cache data like regular EA dnode, etc
249 static int osd_find_parent_by_dnode(const struct lu_env *env,
253 struct osd_device *osd = osd_obj2dev(osd_dt_obj(o));
254 struct lustre_mdt_attrs *lma;
257 nvlist_t *nvbuf = NULL;
263 /* first of all, get parent dnode from own attributes */
264 LASSERT(osd_dt_obj(o)->oo_db);
265 rc = -sa_handle_get(osd->od_os, osd_dt_obj(o)->oo_db->db_object,
266 NULL, SA_HDL_PRIVATE, &sa_hdl);
270 dnode = ZFS_NO_OBJECT;
271 rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
272 sa_handle_destroy(sa_hdl);
276 /* now get EA buffer */
277 rc = __osd_xattr_load(osd, dnode, &nvbuf);
281 /* XXX: if we get that far.. should we cache the result? */
283 /* try to find LMA attribute */
284 LASSERT(nvbuf != NULL);
285 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &value, &size);
286 if (rc == 0 && size >= sizeof(*lma)) {
287 lma = (struct lustre_mdt_attrs *)value;
288 lustre_lma_swab(lma);
289 *fid = lma->lma_self_fid;
294 /* no LMA attribute in SA, let's try regular EA */
296 /* first of all, get parent dnode storing regular EA */
297 rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
301 dnode = ZFS_NO_OBJECT;
302 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &dnode, 8);
303 sa_handle_destroy(sa_hdl);
307 CLASSERT(sizeof(*lma) <= sizeof(osd_oti_get(env)->oti_buf));
308 buf.lb_buf = osd_oti_get(env)->oti_buf;
309 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
311 /* now try to find LMA */
312 rc = __osd_xattr_get_large(env, osd, dnode, &buf,
313 XATTR_NAME_LMA, &size);
314 if (rc == 0 && size >= sizeof(*lma)) {
316 lustre_lma_swab(lma);
317 *fid = lma->lma_self_fid;
322 GOTO(out, rc = -EIO);
331 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
334 struct link_ea_header *leh;
335 struct link_ea_entry *lee;
340 buf.lb_buf = osd_oti_get(env)->oti_buf;
341 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
343 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
345 rc = osd_xattr_get(env, o, &LU_BUF_NULL,
346 XATTR_NAME_LINK, BYPASS_CAPA);
350 OBD_ALLOC(buf.lb_buf, rc);
351 if (buf.lb_buf == NULL)
354 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
358 if (rc < sizeof(*leh) + sizeof(*lee))
359 GOTO(out, rc = -EINVAL);
362 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
363 leh->leh_magic = LINK_EA_MAGIC;
364 leh->leh_reccount = __swab32(leh->leh_reccount);
365 leh->leh_len = __swab64(leh->leh_len);
367 if (leh->leh_magic != LINK_EA_MAGIC)
368 GOTO(out, rc = -EINVAL);
369 if (leh->leh_reccount == 0)
370 GOTO(out, rc = -ENODATA);
372 lee = (struct link_ea_entry *)(leh + 1);
373 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
377 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
378 OBD_FREE(buf.lb_buf, buf.lb_len);
381 /* this block can be enabled for additional verification
382 * it's trying to match FID from LinkEA vs. FID from LMA */
386 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
388 if (lu_fid_eq(fid, &fid2) == 0)
389 CERROR("wrong parent: "DFID" != "DFID"\n",
390 PFID(fid), PFID(&fid2));
394 /* no LinkEA is found, let's try to find the fid in parent's LMA */
395 if (unlikely(rc != 0))
396 rc = osd_find_parent_by_dnode(env, o, fid);
401 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
402 struct dt_rec *rec, const struct dt_key *key,
403 struct lustre_capa *capa)
405 struct osd_thread_info *oti = osd_oti_get(env);
406 struct osd_object *obj = osd_dt_obj(dt);
407 struct osd_device *osd = osd_obj2dev(obj);
408 char *name = (char *)key;
412 LASSERT(osd_object_is_zap(obj->oo_db));
414 if (name[0] == '.') {
416 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
417 memcpy(rec, f, sizeof(*f));
419 } else if (name[1] == '.' && name[2] == 0) {
420 rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
421 RETURN(rc == 0 ? 1 : rc);
425 rc = -zap_lookup(osd->od_os, obj->oo_db->db_object,
426 (char *)key, 8, sizeof(oti->oti_zde) / 8,
427 (void *)&oti->oti_zde);
428 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
430 RETURN(rc == 0 ? 1 : rc);
433 static int osd_declare_dir_insert(const struct lu_env *env,
434 struct dt_object *dt,
435 const struct dt_rec *rec,
436 const struct dt_key *key,
439 struct osd_object *obj = osd_dt_obj(dt);
440 struct osd_thandle *oh;
445 oh = container_of0(th, struct osd_thandle, ot_super);
447 /* This is for inserting dot/dotdot for new created dir. */
448 if (obj->oo_db == NULL)
449 object = DMU_NEW_OBJECT;
451 object = obj->oo_db->db_object;
453 dmu_tx_hold_bonus(oh->ot_tx, object);
454 dmu_tx_hold_zap(oh->ot_tx, object, TRUE, (char *)key);
460 * Find the osd object for given fid.
462 * \param fid need to find the osd object having this fid
464 * \retval osd_object on success
465 * \retval -ve on error
467 struct osd_object *osd_object_find(const struct lu_env *env,
468 struct dt_object *dt,
469 const struct lu_fid *fid)
471 struct lu_device *ludev = dt->do_lu.lo_dev;
472 struct osd_object *child = NULL;
473 struct lu_object *luch;
474 struct lu_object *lo;
477 * at this point topdev might not exist yet
478 * (i.e. MGS is preparing profiles). so we can
479 * not rely on topdev and instead lookup with
480 * our device passed as topdev. this can't work
481 * if the object isn't cached yet (as osd doesn't
482 * allocate lu_header). IOW, the object must be
483 * in the cache, otherwise lu_object_alloc() crashes
486 luch = lu_object_find_at(env, ludev, fid, NULL);
490 if (lu_object_exists(luch)) {
491 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
495 LU_OBJECT_DEBUG(D_ERROR, env, luch,
496 "%s: object can't be located "DFID"\n",
497 osd_dev(ludev)->od_svname, PFID(fid));
500 lu_object_put(env, luch);
501 CERROR("%s: Unable to get osd_object "DFID"\n",
502 osd_dev(ludev)->od_svname, PFID(fid));
503 child = ERR_PTR(-ENOENT);
506 LU_OBJECT_DEBUG(D_ERROR, env, luch,
507 "%s: lu_object does not exists "DFID"\n",
508 osd_dev(ludev)->od_svname, PFID(fid));
509 lu_object_put(env, luch);
510 child = ERR_PTR(-ENOENT);
517 * Put the osd object once done with it.
519 * \param obj osd object that needs to be put
521 static inline void osd_object_put(const struct lu_env *env,
522 struct osd_object *obj)
524 lu_object_put(env, &obj->oo_dt.do_lu);
527 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
530 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
531 struct seq_server_site *ss = osd_seq_site(osd);
536 LASSERT(ss->ss_server_fld != NULL);
538 rc = osd_fld_lookup(env, osd, seq, range);
540 CERROR("%s: Can not lookup fld for "LPX64"\n",
545 RETURN(ss->ss_node_id == range->lsr_index);
548 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
549 const struct lu_fid *fid)
551 struct seq_server_site *ss = osd_seq_site(osd);
554 /* FID seqs not in FLDB, must be local seq */
555 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
558 /* If FLD is not being initialized yet, it only happens during the
559 * initialization, likely during mgs initialization, and we assume
560 * this is local FID. */
561 if (ss == NULL || ss->ss_server_fld == NULL)
564 /* Only check the local FLDB here */
565 if (osd_seq_exists(env, osd, fid_seq(fid)))
572 * Inserts (key, value) pair in \a directory object.
574 * \param dt osd index object
575 * \param key key for index
576 * \param rec record reference
577 * \param th transaction handler
578 * \param capa capability descriptor
579 * \param ignore_quota update should not affect quota
582 * \retval -ve failure
584 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
585 const struct dt_rec *rec, const struct dt_key *key,
586 struct thandle *th, struct lustre_capa *capa,
589 struct osd_thread_info *oti = osd_oti_get(env);
590 struct osd_object *parent = osd_dt_obj(dt);
591 struct osd_device *osd = osd_obj2dev(parent);
592 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
593 const struct lu_fid *fid = rec1->rec_fid;
594 struct osd_thandle *oh;
595 struct osd_object *child = NULL;
597 char *name = (char *)key;
601 LASSERT(parent->oo_db);
602 LASSERT(osd_object_is_zap(parent->oo_db));
604 LASSERT(dt_object_exists(dt));
605 LASSERT(osd_invariant(parent));
608 oh = container_of0(th, struct osd_thandle, ot_super);
610 rc = osd_remote_fid(env, osd, fid);
612 CERROR("%s: Can not find object "DFID": rc = %d\n",
613 osd->od_svname, PFID(fid), rc);
617 if (unlikely(rc == 1)) {
618 /* Insert remote entry */
619 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
620 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
623 * To simulate old Orion setups with ./.. stored in the
626 /* Insert local entry */
627 child = osd_object_find(env, dt, fid);
629 RETURN(PTR_ERR(child));
631 LASSERT(child->oo_db);
632 if (name[0] == '.') {
634 /* do not store ".", instead generate it
635 * during iteration */
637 } else if (name[1] == '.' && name[2] == 0) {
638 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
639 struct lu_fid tfid = *fid;
641 osd_object_put(env, child);
643 child = osd_object_find(env, dt, &tfid);
645 RETURN(PTR_ERR(child));
647 LASSERT(child->oo_db);
650 /* update parent dnode in the child.
651 * later it will be used to generate ".." */
652 rc = osd_object_sa_update(parent,
654 &child->oo_db->db_object,
660 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
661 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
662 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
663 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
664 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
667 oti->oti_zde.lzd_fid = *fid;
668 /* Insert (key,oid) into ZAP */
669 rc = -zap_add(osd->od_os, parent->oo_db->db_object,
670 (char *)key, 8, sizeof(oti->oti_zde) / 8,
671 (void *)&oti->oti_zde, oh->ot_tx);
672 if (unlikely(rc == -EEXIST &&
673 name[0] == '.' && name[1] == '.' && name[2] == 0))
674 /* Update (key,oid) in ZAP */
675 rc = -zap_update(osd->od_os, parent->oo_db->db_object,
676 (char *)key, 8, sizeof(oti->oti_zde) / 8,
677 (void *)&oti->oti_zde, oh->ot_tx);
681 osd_object_put(env, child);
686 static int osd_declare_dir_delete(const struct lu_env *env,
687 struct dt_object *dt,
688 const struct dt_key *key,
691 struct osd_object *obj = osd_dt_obj(dt);
692 struct osd_thandle *oh;
695 LASSERT(dt_object_exists(dt));
696 LASSERT(osd_invariant(obj));
699 oh = container_of0(th, struct osd_thandle, ot_super);
702 LASSERT(osd_object_is_zap(obj->oo_db));
704 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
709 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
710 const struct dt_key *key, struct thandle *th,
711 struct lustre_capa *capa)
713 struct osd_object *obj = osd_dt_obj(dt);
714 struct osd_device *osd = osd_obj2dev(obj);
715 struct osd_thandle *oh;
716 dmu_buf_t *zap_db = obj->oo_db;
717 char *name = (char *)key;
722 LASSERT(osd_object_is_zap(obj->oo_db));
725 oh = container_of0(th, struct osd_thandle, ot_super);
728 * In Orion . and .. were stored in the directory (not generated upon
729 * request as now). we preserve them for backward compatibility
731 if (name[0] == '.') {
734 } else if (name[1] == '.' && name[2] == 0) {
739 /* Remove key from the ZAP */
740 rc = -zap_remove(osd->od_os, zap_db->db_object,
741 (char *) key, oh->ot_tx);
743 if (unlikely(rc && rc != -ENOENT))
744 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
749 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
750 struct dt_object *dt,
752 struct lustre_capa *capa)
754 struct osd_zap_it *it;
756 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
760 RETURN((struct dt_it *)it);
764 * Move Iterator to record specified by \a key
766 * \param di osd iterator
767 * \param key key for index
769 * \retval +ve di points to record with least key not larger than key
770 * \retval 0 di points to exact matched key
771 * \retval -ve failure
773 static int osd_dir_it_get(const struct lu_env *env,
774 struct dt_it *di, const struct dt_key *key)
776 struct osd_zap_it *it = (struct osd_zap_it *)di;
777 struct osd_object *obj = it->ozi_obj;
778 char *name = (char *)key;
785 /* reset the cursor */
786 zap_cursor_fini(it->ozi_zc);
787 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
789 /* XXX: implementation of the API is broken at the moment */
790 LASSERT(((const char *)key)[0] == 0);
797 if (name[0] == '.') {
801 } else if (name[1] == '.' && name[2] == 0) {
807 /* neither . nor .. - some real record */
815 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
817 /* PBS: do nothing : ref are incremented at retrive and decreamented
822 * in Orion . and .. were stored in the directory, while ZPL
823 * and current osd-zfs generate them up on request. so, we
824 * need to ignore previously stored . and ..
826 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
832 rc = -zap_cursor_retrieve(it->ozi_zc, za);
835 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
836 if (za->za_name[1] == 0) {
838 } else if (za->za_name[1] == '.' &&
839 za->za_name[2] == 0) {
843 zap_cursor_advance(it->ozi_zc);
845 } while (unlikely(rc == 0 && isdot));
851 * to load a directory entry at a time and stored it in
852 * iterator's in-memory data structure.
854 * \param di, struct osd_it_ea, iterator's in memory structure
856 * \retval +ve, iterator reached to end
857 * \retval 0, iterator not reached to end
858 * \retval -ve, on error
860 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
862 struct osd_zap_it *it = (struct osd_zap_it *)di;
863 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
868 /* temp. storage should be enough for any key supported by ZFS */
869 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
872 * the first ->next() moves the cursor to .
873 * the second ->next() moves the cursor to ..
874 * then we get to the real records and have to verify any exist
876 if (it->ozi_pos <= 2) {
882 zap_cursor_advance(it->ozi_zc);
886 * According to current API we need to return error if its last entry.
887 * zap_cursor_advance() does not return any value. So we need to call
888 * retrieve to check if there is any record. We should make
889 * changes to Iterator API to not return status for this API
891 rc = osd_index_retrieve_skip_dots(it, za);
893 if (rc == -ENOENT) /* end of dir */
899 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
900 const struct dt_it *di)
902 struct osd_zap_it *it = (struct osd_zap_it *)di;
903 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
907 if (it->ozi_pos <= 1) {
909 RETURN((struct dt_key *)".");
910 } else if (it->ozi_pos == 2) {
911 RETURN((struct dt_key *)"..");
914 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
917 strcpy(it->ozi_name, za->za_name);
919 RETURN((struct dt_key *)it->ozi_name);
922 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
924 struct osd_zap_it *it = (struct osd_zap_it *)di;
925 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
929 if (it->ozi_pos <= 1) {
932 } else if (it->ozi_pos == 2) {
936 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
937 rc = strlen(za->za_name);
942 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
943 struct dt_rec *dtrec, __u32 attr)
945 struct osd_zap_it *it = (struct osd_zap_it *)di;
946 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
947 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
948 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
952 if (it->ozi_pos <= 1) {
953 lde->lde_hash = cpu_to_le64(1);
954 strcpy(lde->lde_name, ".");
955 lde->lde_namelen = cpu_to_le16(1);
956 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
957 lde->lde_attrs = LUDA_FID;
958 /* append lustre attributes */
959 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
960 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
964 } else if (it->ozi_pos == 2) {
965 lde->lde_hash = cpu_to_le64(2);
966 strcpy(lde->lde_name, "..");
967 lde->lde_namelen = cpu_to_le16(2);
968 lde->lde_attrs = LUDA_FID;
969 /* append lustre attributes */
970 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
971 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
972 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
974 /* ENOENT happens at the root of filesystem so ignore it */
982 rc = -zap_cursor_retrieve(it->ozi_zc, za);
983 if (unlikely(rc != 0))
986 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
987 namelen = strlen(za->za_name);
988 if (namelen > NAME_MAX)
989 GOTO(out, rc = -EOVERFLOW);
990 strcpy(lde->lde_name, za->za_name);
991 lde->lde_namelen = cpu_to_le16(namelen);
993 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
994 CERROR("%s: unsupported direntry format: %d %d\n",
995 osd_obj2dev(it->ozi_obj)->od_svname,
996 za->za_integer_length, (int)za->za_num_integers);
998 GOTO(out, rc = -EIO);
1001 rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
1002 za->za_name, za->za_integer_length, 3, zde);
1006 lde->lde_fid = zde->lzd_fid;
1007 lde->lde_attrs = LUDA_FID;
1009 /* append lustre attributes */
1010 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
1012 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
1018 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
1021 struct osd_zap_it *it = (struct osd_zap_it *)di;
1022 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1027 if (it->ozi_pos <= 1)
1029 else if (it->ozi_pos == 2)
1033 rc = lu_dirent_calc_size(namelen, attr);
1037 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1038 if (unlikely(rc != 0))
1041 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1042 CERROR("%s: unsupported direntry format: %d %d\n",
1043 osd_obj2dev(it->ozi_obj)->od_svname,
1044 za->za_integer_length, (int)za->za_num_integers);
1048 namelen = strlen(za->za_name);
1049 if (namelen > NAME_MAX)
1052 rc = lu_dirent_calc_size(namelen, attr);
1057 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1059 struct osd_zap_it *it = (struct osd_zap_it *)di;
1063 if (it->ozi_pos <= 2)
1066 pos = osd_zap_cursor_serialize(it->ozi_zc);
1073 * rc == 0 -> end of directory.
1074 * rc > 0 -> ok, proceed.
1075 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1077 static int osd_dir_it_load(const struct lu_env *env,
1078 const struct dt_it *di, __u64 hash)
1080 struct osd_zap_it *it = (struct osd_zap_it *)di;
1081 struct osd_object *obj = it->ozi_obj;
1082 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1086 /* reset the cursor */
1087 zap_cursor_fini(it->ozi_zc);
1088 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1095 /* to return whether the end has been reached */
1096 rc = osd_index_retrieve_skip_dots(it, za);
1099 else if (rc == -ENOENT)
1106 struct dt_index_operations osd_dir_ops = {
1107 .dio_lookup = osd_dir_lookup,
1108 .dio_declare_insert = osd_declare_dir_insert,
1109 .dio_insert = osd_dir_insert,
1110 .dio_declare_delete = osd_declare_dir_delete,
1111 .dio_delete = osd_dir_delete,
1113 .init = osd_dir_it_init,
1114 .fini = osd_index_it_fini,
1115 .get = osd_dir_it_get,
1116 .put = osd_dir_it_put,
1117 .next = osd_dir_it_next,
1118 .key = osd_dir_it_key,
1119 .key_size = osd_dir_it_key_size,
1120 .rec = osd_dir_it_rec,
1121 .rec_size = osd_dir_it_rec_size,
1122 .store = osd_dir_it_store,
1123 .load = osd_dir_it_load
1128 * Primitives for index files using binary keys.
1131 /* key integer_size is 8 */
1132 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1133 const struct dt_key *src)
1140 /* align keysize to 64bit */
1141 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1142 size *= sizeof(__u64);
1144 LASSERT(size <= MAXNAMELEN);
1146 if (unlikely(size > o->oo_keysize))
1147 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1148 memcpy(dst, (const char *)src, o->oo_keysize);
1150 return (size/sizeof(__u64));
1153 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1154 struct dt_rec *rec, const struct dt_key *key,
1155 struct lustre_capa *capa)
1157 struct osd_object *obj = osd_dt_obj(dt);
1158 struct osd_device *osd = osd_obj2dev(obj);
1159 __u64 *k = osd_oti_get(env)->oti_key64;
1163 rc = osd_prepare_key_uint64(obj, k, key);
1165 rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1166 k, rc, obj->oo_recusize, obj->oo_recsize,
1168 RETURN(rc == 0 ? 1 : rc);
1171 static int osd_declare_index_insert(const struct lu_env *env,
1172 struct dt_object *dt,
1173 const struct dt_rec *rec,
1174 const struct dt_key *key,
1177 struct osd_object *obj = osd_dt_obj(dt);
1178 struct osd_thandle *oh;
1181 LASSERT(th != NULL);
1182 oh = container_of0(th, struct osd_thandle, ot_super);
1184 LASSERT(obj->oo_db);
1186 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1188 /* It is not clear what API should be used for binary keys, so we pass
1189 * a null name which has the side effect of over-reserving space,
1190 * accounting for the worst case. See zap_count_write() */
1191 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1196 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1197 const struct dt_rec *rec, const struct dt_key *key,
1198 struct thandle *th, struct lustre_capa *capa,
1201 struct osd_object *obj = osd_dt_obj(dt);
1202 struct osd_device *osd = osd_obj2dev(obj);
1203 struct osd_thandle *oh;
1204 __u64 *k = osd_oti_get(env)->oti_key64;
1208 LASSERT(obj->oo_db);
1209 LASSERT(dt_object_exists(dt));
1210 LASSERT(osd_invariant(obj));
1211 LASSERT(th != NULL);
1213 oh = container_of0(th, struct osd_thandle, ot_super);
1215 rc = osd_prepare_key_uint64(obj, k, key);
1217 /* Insert (key,oid) into ZAP */
1218 rc = -zap_add_uint64(osd->od_os, obj->oo_db->db_object,
1219 k, rc, obj->oo_recusize, obj->oo_recsize,
1220 (void *)rec, oh->ot_tx);
1224 static int osd_declare_index_delete(const struct lu_env *env,
1225 struct dt_object *dt,
1226 const struct dt_key *key,
1229 struct osd_object *obj = osd_dt_obj(dt);
1230 struct osd_thandle *oh;
1233 LASSERT(dt_object_exists(dt));
1234 LASSERT(osd_invariant(obj));
1235 LASSERT(th != NULL);
1236 LASSERT(obj->oo_db);
1238 oh = container_of0(th, struct osd_thandle, ot_super);
1239 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1244 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1245 const struct dt_key *key, struct thandle *th,
1246 struct lustre_capa *capa)
1248 struct osd_object *obj = osd_dt_obj(dt);
1249 struct osd_device *osd = osd_obj2dev(obj);
1250 struct osd_thandle *oh;
1251 __u64 *k = osd_oti_get(env)->oti_key64;
1255 LASSERT(obj->oo_db);
1256 LASSERT(th != NULL);
1257 oh = container_of0(th, struct osd_thandle, ot_super);
1259 rc = osd_prepare_key_uint64(obj, k, key);
1261 /* Remove binary key from the ZAP */
1262 rc = -zap_remove_uint64(osd->od_os, obj->oo_db->db_object,
1267 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1268 const struct dt_key *key)
1270 struct osd_zap_it *it = (struct osd_zap_it *)di;
1271 struct osd_object *obj = it->ozi_obj;
1272 struct osd_device *osd = osd_obj2dev(obj);
1276 LASSERT(it->ozi_zc);
1279 * XXX: we need a binary version of zap_cursor_move_to_key()
1280 * to implement this API */
1281 if (*((const __u64 *)key) != 0)
1282 CERROR("NOT IMPLEMETED YET (move to "LPX64")\n",
1285 zap_cursor_fini(it->ozi_zc);
1286 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_db->db_object);
1292 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1294 struct osd_zap_it *it = (struct osd_zap_it *)di;
1295 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1299 if (it->ozi_reset == 0)
1300 zap_cursor_advance(it->ozi_zc);
1304 * According to current API we need to return error if it's last entry.
1305 * zap_cursor_advance() does not return any value. So we need to call
1306 * retrieve to check if there is any record. We should make
1307 * changes to Iterator API to not return status for this API
1309 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1316 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1317 const struct dt_it *di)
1319 struct osd_zap_it *it = (struct osd_zap_it *)di;
1320 struct osd_object *obj = it->ozi_obj;
1321 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1326 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1328 RETURN(ERR_PTR(rc));
1330 /* the binary key is stored in the name */
1331 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1333 RETURN((struct dt_key *)&it->ozi_key);
1336 static int osd_index_it_key_size(const struct lu_env *env,
1337 const struct dt_it *di)
1339 struct osd_zap_it *it = (struct osd_zap_it *)di;
1340 struct osd_object *obj = it->ozi_obj;
1341 RETURN(obj->oo_keysize);
1344 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1345 struct dt_rec *rec, __u32 attr)
1347 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
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 __u64 *k = osd_oti_get(env)->oti_key64;
1356 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1360 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1362 rc = -zap_lookup_uint64(osd->od_os, obj->oo_db->db_object,
1363 k, rc, obj->oo_recusize, obj->oo_recsize,
1368 static __u64 osd_index_it_store(const struct lu_env *env,
1369 const struct dt_it *di)
1371 struct osd_zap_it *it = (struct osd_zap_it *)di;
1374 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1377 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1380 struct osd_zap_it *it = (struct osd_zap_it *)di;
1381 struct osd_object *obj = it->ozi_obj;
1382 struct osd_device *osd = osd_obj2dev(obj);
1383 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1387 /* reset the cursor */
1388 zap_cursor_fini(it->ozi_zc);
1389 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1390 obj->oo_db->db_object, hash);
1393 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1396 else if (rc == -ENOENT)
1402 static struct dt_index_operations osd_index_ops = {
1403 .dio_lookup = osd_index_lookup,
1404 .dio_declare_insert = osd_declare_index_insert,
1405 .dio_insert = osd_index_insert,
1406 .dio_declare_delete = osd_declare_index_delete,
1407 .dio_delete = osd_index_delete,
1409 .init = osd_index_it_init,
1410 .fini = osd_index_it_fini,
1411 .get = osd_index_it_get,
1412 .put = osd_index_it_put,
1413 .next = osd_index_it_next,
1414 .key = osd_index_it_key,
1415 .key_size = osd_index_it_key_size,
1416 .rec = osd_index_it_rec,
1417 .store = osd_index_it_store,
1418 .load = osd_index_it_load
1422 struct osd_metadnode_it {
1423 struct osd_device *mit_dev;
1425 struct lu_fid mit_fid;
1427 __u64 mit_prefetched_dnode;
1430 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1431 struct dt_object *dt, __u32 attr,
1432 struct lustre_capa *capa)
1434 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1435 struct osd_metadnode_it *it;
1439 if (unlikely(it == NULL))
1440 RETURN(ERR_PTR(-ENOMEM));
1444 /* XXX: dmu_object_next() does NOT find dnodes allocated
1445 * in the current non-committed txg, so we force txg
1446 * commit to find all existing dnodes ... */
1447 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1449 RETURN((struct dt_it *)it);
1452 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1454 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1459 static int osd_zfs_otable_it_get(const struct lu_env *env,
1460 struct dt_it *di, const struct dt_key *key)
1465 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1469 #define OTABLE_PREFETCH 256
1471 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1472 struct osd_metadnode_it *it)
1474 struct osd_device *dev = it->mit_dev;
1477 /* can go negative on the very first access to the iterator
1478 * or if some non-Lustre objects were found */
1479 if (unlikely(it->mit_prefetched < 0))
1480 it->mit_prefetched = 0;
1482 if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1485 if (it->mit_prefetched_dnode == 0)
1486 it->mit_prefetched_dnode = it->mit_pos;
1488 while (it->mit_prefetched < OTABLE_PREFETCH) {
1489 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1491 if (unlikely(rc != 0))
1494 /* dmu_prefetch() was exported in 0.6.2, if you use with
1495 * an older release, just comment it out - this is an
1497 dmu_prefetch(dev->od_os, it->mit_prefetched_dnode, 0, 0);
1499 it->mit_prefetched++;
1503 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1505 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1506 struct lustre_mdt_attrs *lma;
1507 struct osd_device *dev = it->mit_dev;
1508 nvlist_t *nvbuf = NULL;
1513 memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1515 dnode = it->mit_pos;
1517 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1518 if (unlikely(rc != 0))
1520 it->mit_prefetched--;
1522 /* LMA is required for this to be a Lustre object.
1523 * If there is no xattr skip it. */
1524 rc = __osd_xattr_load(dev, it->mit_pos, &nvbuf);
1525 if (unlikely(rc != 0))
1528 LASSERT(nvbuf != NULL);
1529 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1530 if (likely(rc == 0)) {
1532 lma = (struct lustre_mdt_attrs *)v;
1533 lustre_lma_swab(lma);
1534 it->mit_fid = lma->lma_self_fid;
1538 /* not a Lustre object, try next one */
1545 /* we aren't prefetching in the above loop because the number of
1546 * non-Lustre objects is very small and we will be repeating very
1547 * rare. in case we want to use this to iterate over non-Lustre
1548 * objects (i.e. when we convert regular ZFS in Lustre) it makes
1549 * sense to initiate prefetching in the loop */
1551 /* 0 - there are more items, +1 - the end */
1552 if (likely(rc == 0))
1553 osd_zfs_otable_prefetch(env, it);
1555 CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1556 it->mit_pos, PFID(&it->mit_fid), rc);
1562 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1563 const struct dt_it *di)
1568 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1569 const struct dt_it *di)
1571 return sizeof(__u64);
1574 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1575 const struct dt_it *di,
1576 struct dt_rec *rec, __u32 attr)
1578 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1579 struct lu_fid *fid = (struct lu_fid *)rec;
1588 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1589 const struct dt_it *di)
1591 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1596 static int osd_zfs_otable_it_load(const struct lu_env *env,
1597 const struct dt_it *di, __u64 hash)
1599 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1602 it->mit_prefetched = 0;
1603 it->mit_prefetched_dnode = 0;
1605 return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1608 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1609 const struct dt_it *di, void *key_rec)
1614 const struct dt_index_operations osd_zfs_otable_ops = {
1616 .init = osd_zfs_otable_it_init,
1617 .fini = osd_zfs_otable_it_fini,
1618 .get = osd_zfs_otable_it_get,
1619 .put = osd_zfs_otable_it_put,
1620 .next = osd_zfs_otable_it_next,
1621 .key = osd_zfs_otable_it_key,
1622 .key_size = osd_zfs_otable_it_key_size,
1623 .rec = osd_zfs_otable_it_rec,
1624 .store = osd_zfs_otable_it_store,
1625 .load = osd_zfs_otable_it_load,
1626 .key_rec = osd_zfs_otable_it_key_rec,
1630 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1631 const struct dt_index_features *feat)
1633 struct osd_object *obj = osd_dt_obj(dt);
1636 LASSERT(dt_object_exists(dt));
1639 * XXX: implement support for fixed-size keys sorted with natural
1640 * numerical way (not using internal hash value)
1642 if (feat->dif_flags & DT_IND_RANGE)
1645 if (unlikely(feat == &dt_otable_features)) {
1646 dt->do_index_ops = &osd_zfs_otable_ops;
1650 LASSERT(obj->oo_db != NULL);
1651 if (likely(feat == &dt_directory_features)) {
1652 if (osd_object_is_zap(obj->oo_db))
1653 dt->do_index_ops = &osd_dir_ops;
1656 } else if (unlikely(feat == &dt_acct_features)) {
1657 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1658 dt->do_index_ops = &osd_acct_index_ops;
1659 } else if (osd_object_is_zap(obj->oo_db) &&
1660 dt->do_index_ops == NULL) {
1661 /* For index file, we don't support variable key & record sizes
1662 * and the key has to be unique */
1663 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1666 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1668 if (feat->dif_keysize_max != feat->dif_keysize_min)
1671 /* As for the record size, it should be a multiple of 8 bytes
1672 * and smaller than the maximum value length supported by ZAP.
1674 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1676 if (feat->dif_recsize_max != feat->dif_recsize_min)
1679 obj->oo_keysize = feat->dif_keysize_max;
1680 obj->oo_recsize = feat->dif_recsize_max;
1681 obj->oo_recusize = 1;
1683 /* ZFS prefers to work with array of 64bits */
1684 if ((obj->oo_recsize & 7) == 0) {
1685 obj->oo_recsize >>= 3;
1686 obj->oo_recusize = 8;
1688 dt->do_index_ops = &osd_index_ops;