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>
55 #include <sys/spa_impl.h>
56 #include <sys/zfs_znode.h>
57 #include <sys/dmu_tx.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/sa_impl.h>
63 static inline int osd_object_is_zap(dnode_t *dn)
65 return (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
66 dn->dn_type == DMU_OT_USERGROUP_USED);
69 /* We don't actually have direct access to the zap_hashbits() function
70 * so just pretend like we do for now. If this ever breaks we can look at
72 #define zap_hashbits(zc) 48
75 * | cd (16 bits) | hash (48 bits) |
76 * we need it in other form:
77 * |0| hash (48 bit) | cd (15 bit) |
78 * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
79 * the readdir hashes from multiple directory stripes uniformly on the client.
80 * Another point is sign bit, the hash range should be in [0, 2^63-1] because
81 * loff_t (for llseek) needs to be a positive value. This means the "cd" field
82 * should only be the low 15 bits.
84 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
86 uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
88 return (zfs_hash >> zap_hashbits(zc)) |
89 (zfs_hash << (63 - zap_hashbits(zc)));
92 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
93 uint64_t id, uint64_t dirhash)
95 uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
96 (dirhash >> (63 - zap_hashbits(zc)));
98 zap_cursor_init_serialized(zc, os, id, zfs_hash);
101 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
102 uint64_t id, uint64_t dirhash)
107 if (unlikely(t == NULL))
110 osd_zap_cursor_init_serialized(t, os, id, dirhash);
116 void osd_zap_cursor_fini(zap_cursor_t *zc)
122 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
123 struct osd_object *o,
126 struct osd_device *d = osd_obj2dev(o);
127 osd_zap_cursor_init_serialized(zc, d->od_os,
128 o->oo_dn->dn_object, dirhash);
131 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
134 struct osd_device *d = osd_obj2dev(o);
135 return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
138 static struct dt_it *osd_index_it_init(const struct lu_env *env,
139 struct dt_object *dt,
142 struct osd_thread_info *info = osd_oti_get(env);
143 struct osd_zap_it *it;
144 struct osd_object *obj = osd_dt_obj(dt);
145 struct lu_object *lo = &dt->do_lu;
149 if (obj->oo_destroyed)
150 RETURN(ERR_PTR(-ENOENT));
152 LASSERT(lu_object_exists(lo));
156 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
158 RETURN(ERR_PTR(-ENOMEM));
160 rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
162 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
170 RETURN((struct dt_it *)it);
173 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
175 struct osd_zap_it *it = (struct osd_zap_it *)di;
176 struct osd_object *obj;
180 LASSERT(it->ozi_obj);
184 osd_zap_cursor_fini(it->ozi_zc);
185 osd_object_put(env, obj);
186 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
192 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
194 /* PBS: do nothing : ref are incremented at retrive and decreamented
198 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
201 const unsigned align = sizeof(struct luda_type) - 1;
202 struct luda_type *lt;
204 /* check if file type is required */
205 if (attr & LUDA_TYPE) {
206 len = (len + align) & ~align;
208 lt = (void *)ent->lde_name + len;
209 lt->lt_type = cpu_to_le16(DTTOIF(type));
210 ent->lde_attrs |= LUDA_TYPE;
213 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
217 * Get the object's FID from its LMA EA.
219 * \param[in] env pointer to the thread context
220 * \param[in] osd pointer to the OSD device
221 * \param[in] oid the object's local identifier
222 * \param[out] fid the buffer to hold the object's FID
224 * \retval 0 for success
225 * \retval negative error number on failure
227 static int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
228 uint64_t oid, struct lu_fid *fid)
230 struct objset *os = osd->od_os;
231 struct osd_thread_info *oti = osd_oti_get(env);
232 struct lustre_mdt_attrs *lma =
233 (struct lustre_mdt_attrs *)oti->oti_buf;
235 nvlist_t *sa_xattr = NULL;
236 sa_handle_t *sa_hdl = NULL;
237 uchar_t *nv_value = NULL;
238 uint64_t xattr = ZFS_NO_OBJECT;
243 rc = __osd_xattr_load(osd, oid, &sa_xattr);
250 rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
258 if (unlikely(size > sizeof(oti->oti_buf)))
259 GOTO(out, rc = -ERANGE);
261 memcpy(lma, nv_value, size);
266 rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
270 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
271 sa_handle_destroy(sa_hdl);
276 buf.lb_len = sizeof(oti->oti_buf);
277 rc = __osd_xattr_get_large(env, osd, xattr, &buf,
278 XATTR_NAME_LMA, &size);
283 if (size < sizeof(*lma))
284 GOTO(out, rc = -EIO);
286 lustre_lma_swab(lma);
287 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
288 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
289 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
290 "oid = %#llx\n", osd->od_svname,
291 lma->lma_incompat & ~LMA_INCOMPAT_SUPP, oid);
292 GOTO(out, rc = -EOPNOTSUPP);
294 *fid = lma->lma_self_fid;
299 if (sa_xattr != NULL)
300 nvlist_free(sa_xattr);
305 * As we don't know FID, we can't use LU object, so this function
306 * partially duplicate __osd_xattr_get() which is built around
307 * LU-object and uses it to cache data like regular EA dnode, etc
309 static int osd_find_parent_by_dnode(const struct lu_env *env,
313 struct osd_device *osd = osd_obj2dev(osd_dt_obj(o));
315 uint64_t dnode = ZFS_NO_OBJECT;
319 /* first of all, get parent dnode from own attributes */
320 LASSERT(osd_dt_obj(o)->oo_dn);
321 rc = -sa_handle_get(osd->od_os, osd_dt_obj(o)->oo_dn->dn_object,
322 NULL, SA_HDL_PRIVATE, &sa_hdl);
326 rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
327 sa_handle_destroy(sa_hdl);
329 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
334 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
337 struct link_ea_header *leh;
338 struct link_ea_entry *lee;
343 buf.lb_buf = osd_oti_get(env)->oti_buf;
344 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
346 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
348 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
352 OBD_ALLOC(buf.lb_buf, rc);
353 if (buf.lb_buf == NULL)
356 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
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)
406 struct osd_thread_info *oti = osd_oti_get(env);
407 struct osd_object *obj = osd_dt_obj(dt);
408 struct osd_device *osd = osd_obj2dev(obj);
409 char *name = (char *)key;
413 if (name[0] == '.') {
415 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
416 memcpy(rec, f, sizeof(*f));
418 } else if (name[1] == '.' && name[2] == 0) {
419 rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
420 RETURN(rc == 0 ? 1 : rc);
424 memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
425 rc = -zap_lookup(osd->od_os, obj->oo_dn->dn_object,
426 (char *)key, 8, sizeof(oti->oti_zde) / 8,
427 (void *)&oti->oti_zde);
431 if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
432 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
436 rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode,
437 (struct lu_fid *)rec);
439 RETURN(rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc));
442 static int osd_declare_dir_insert(const struct lu_env *env,
443 struct dt_object *dt,
444 const struct dt_rec *rec,
445 const struct dt_key *key,
448 struct osd_object *obj = osd_dt_obj(dt);
449 struct osd_device *osd = osd_obj2dev(obj);
450 const struct dt_insert_rec *rec1;
451 const struct lu_fid *fid;
452 struct osd_thandle *oh;
456 rec1 = (struct dt_insert_rec *)rec;
458 LASSERT(fid != NULL);
459 LASSERT(rec1->rec_type != 0);
462 oh = container_of0(th, struct osd_thandle, ot_super);
464 /* This is for inserting dot/dotdot for new created dir. */
465 if (obj->oo_dn == NULL)
466 object = DMU_NEW_OBJECT;
468 object = obj->oo_dn->dn_object;
470 /* do not specify the key as then DMU is trying to look it up
471 * which is very expensive. usually the layers above lookup
472 * before insertion */
473 dmu_tx_hold_zap(oh->ot_tx, object, TRUE, NULL);
475 osd_idc_find_or_init(env, osd, fid);
480 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
483 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
484 struct seq_server_site *ss = osd_seq_site(osd);
489 LASSERT(ss->ss_server_fld != NULL);
491 rc = osd_fld_lookup(env, osd, seq, range);
494 CERROR("%s: Can not lookup fld for %#llx\n",
499 RETURN(ss->ss_node_id == range->lsr_index);
502 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
503 const struct lu_fid *fid)
505 struct seq_server_site *ss = osd_seq_site(osd);
508 /* FID seqs not in FLDB, must be local seq */
509 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
512 /* If FLD is not being initialized yet, it only happens during the
513 * initialization, likely during mgs initialization, and we assume
514 * this is local FID. */
515 if (ss == NULL || ss->ss_server_fld == NULL)
518 /* Only check the local FLDB here */
519 if (osd_seq_exists(env, osd, fid_seq(fid)))
526 * Inserts (key, value) pair in \a directory object.
528 * \param dt osd index object
529 * \param key key for index
530 * \param rec record reference
531 * \param th transaction handler
532 * \param ignore_quota update should not affect quota
535 * \retval -ve failure
537 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
538 const struct dt_rec *rec, const struct dt_key *key,
539 struct thandle *th, int ignore_quota)
541 struct osd_thread_info *oti = osd_oti_get(env);
542 struct osd_object *parent = osd_dt_obj(dt);
543 struct osd_device *osd = osd_obj2dev(parent);
544 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
545 const struct lu_fid *fid = rec1->rec_fid;
546 struct osd_thandle *oh;
547 struct osd_idmap_cache *idc;
548 char *name = (char *)key;
552 LASSERT(parent->oo_dn);
554 LASSERT(dt_object_exists(dt));
555 LASSERT(osd_invariant(parent));
558 oh = container_of0(th, struct osd_thandle, ot_super);
560 idc = osd_idc_find(env, osd, fid);
561 if (unlikely(idc == NULL)) {
562 /* this dt_insert() wasn't declared properly, so
563 * FID is missing in OI cache. we better do not
564 * lookup FID in FLDB/OI and don't risk to deadlock,
565 * but in some special cases (lfsck testing, etc)
566 * it's much simpler than fixing a caller */
567 CERROR("%s: "DFID" wasn't declared for insert\n",
568 osd_name(osd), PFID(fid));
569 idc = osd_idc_find_or_init(env, osd, fid);
571 RETURN(PTR_ERR(idc));
574 if (idc->oic_remote) {
575 /* Insert remote entry */
576 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
577 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
579 if (unlikely(idc->oic_dnode == 0)) {
580 /* for a reason OI cache wasn't filled properly */
581 CERROR("%s: OIC for "DFID" isn't filled\n",
582 osd_name(osd), PFID(fid));
585 if (name[0] == '.') {
587 /* do not store ".", instead generate it
588 * during iteration */
590 } else if (name[1] == '.' && name[2] == 0) {
591 uint64_t dnode = idc->oic_dnode;
592 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
595 /* update parent dnode in the child.
596 * later it will be used to generate ".." */
597 rc = osd_object_sa_update(parent,
604 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
605 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
606 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
607 oti->oti_zde.lzd_reg.zde_dnode = idc->oic_dnode;
610 oti->oti_zde.lzd_fid = *fid;
611 /* Insert (key,oid) into ZAP */
612 rc = -zap_add(osd->od_os, parent->oo_dn->dn_object,
613 (char *)key, 8, sizeof(oti->oti_zde) / 8,
614 (void *)&oti->oti_zde, oh->ot_tx);
615 if (unlikely(rc == -EEXIST &&
616 name[0] == '.' && name[1] == '.' && name[2] == 0))
617 /* Update (key,oid) in ZAP */
618 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object,
619 (char *)key, 8, sizeof(oti->oti_zde) / 8,
620 (void *)&oti->oti_zde, oh->ot_tx);
627 static int osd_declare_dir_delete(const struct lu_env *env,
628 struct dt_object *dt,
629 const struct dt_key *key,
632 struct osd_object *obj = osd_dt_obj(dt);
633 struct osd_thandle *oh;
637 LASSERT(dt_object_exists(dt));
638 LASSERT(osd_invariant(obj));
641 oh = container_of0(th, struct osd_thandle, ot_super);
643 if (dt_object_exists(dt)) {
645 dnode = obj->oo_dn->dn_object;
647 dnode = DMU_NEW_OBJECT;
650 /* do not specify the key as then DMU is trying to look it up
651 * which is very expensive. usually the layers above lookup
653 dmu_tx_hold_zap(oh->ot_tx, dnode, FALSE, NULL);
658 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
659 const struct dt_key *key, struct thandle *th)
661 struct osd_object *obj = osd_dt_obj(dt);
662 struct osd_device *osd = osd_obj2dev(obj);
663 struct osd_thandle *oh;
664 dnode_t *zap_dn = obj->oo_dn;
665 char *name = (char *)key;
672 oh = container_of0(th, struct osd_thandle, ot_super);
675 * In Orion . and .. were stored in the directory (not generated upon
676 * request as now). we preserve them for backward compatibility
678 if (name[0] == '.') {
681 } else if (name[1] == '.' && name[2] == 0) {
686 /* Remove key from the ZAP */
687 rc = -zap_remove(osd->od_os, zap_dn->dn_object,
688 (char *) key, oh->ot_tx);
690 if (unlikely(rc && rc != -ENOENT))
691 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
696 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
697 struct dt_object *dt,
700 struct osd_zap_it *it;
702 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
706 RETURN((struct dt_it *)it);
710 * Move Iterator to record specified by \a key
712 * \param di osd iterator
713 * \param key key for index
715 * \retval +ve di points to record with least key not larger than key
716 * \retval 0 di points to exact matched key
717 * \retval -ve failure
719 static int osd_dir_it_get(const struct lu_env *env,
720 struct dt_it *di, const struct dt_key *key)
722 struct osd_zap_it *it = (struct osd_zap_it *)di;
723 struct osd_object *obj = it->ozi_obj;
724 char *name = (char *)key;
731 /* reset the cursor */
732 zap_cursor_fini(it->ozi_zc);
733 osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
735 /* XXX: implementation of the API is broken at the moment */
736 LASSERT(((const char *)key)[0] == 0);
743 if (name[0] == '.') {
747 } else if (name[1] == '.' && name[2] == 0) {
753 /* neither . nor .. - some real record */
761 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
763 /* PBS: do nothing : ref are incremented at retrive and decreamented
768 * in Orion . and .. were stored in the directory, while ZPL
769 * and current osd-zfs generate them up on request. so, we
770 * need to ignore previously stored . and ..
772 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
778 rc = -zap_cursor_retrieve(it->ozi_zc, za);
781 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
782 if (za->za_name[1] == 0) {
784 } else if (za->za_name[1] == '.' &&
785 za->za_name[2] == 0) {
789 zap_cursor_advance(it->ozi_zc);
791 } while (unlikely(rc == 0 && isdot));
797 * to load a directory entry at a time and stored it in
798 * iterator's in-memory data structure.
800 * \param di, struct osd_it_ea, iterator's in memory structure
802 * \retval +ve, iterator reached to end
803 * \retval 0, iterator not reached to end
804 * \retval -ve, on error
806 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
808 struct osd_zap_it *it = (struct osd_zap_it *)di;
809 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
814 /* temp. storage should be enough for any key supported by ZFS */
815 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
818 * the first ->next() moves the cursor to .
819 * the second ->next() moves the cursor to ..
820 * then we get to the real records and have to verify any exist
822 if (it->ozi_pos <= 2) {
828 zap_cursor_advance(it->ozi_zc);
832 * According to current API we need to return error if its last entry.
833 * zap_cursor_advance() does not return any value. So we need to call
834 * retrieve to check if there is any record. We should make
835 * changes to Iterator API to not return status for this API
837 rc = osd_index_retrieve_skip_dots(it, za);
839 if (rc == -ENOENT) /* end of dir */
845 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
846 const struct dt_it *di)
848 struct osd_zap_it *it = (struct osd_zap_it *)di;
849 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
853 if (it->ozi_pos <= 1) {
855 RETURN((struct dt_key *)".");
856 } else if (it->ozi_pos == 2) {
857 RETURN((struct dt_key *)"..");
860 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
863 strcpy(it->ozi_name, za->za_name);
865 RETURN((struct dt_key *)it->ozi_name);
868 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
870 struct osd_zap_it *it = (struct osd_zap_it *)di;
871 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
875 if (it->ozi_pos <= 1) {
878 } else if (it->ozi_pos == 2) {
882 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
883 rc = strlen(za->za_name);
888 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
889 struct dt_rec *dtrec, __u32 attr)
891 struct osd_zap_it *it = (struct osd_zap_it *)di;
892 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
893 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
894 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
898 if (it->ozi_pos <= 1) {
899 lde->lde_hash = cpu_to_le64(1);
900 strcpy(lde->lde_name, ".");
901 lde->lde_namelen = cpu_to_le16(1);
902 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
903 lde->lde_attrs = LUDA_FID;
904 /* append lustre attributes */
905 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
906 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
910 } else if (it->ozi_pos == 2) {
911 lde->lde_hash = cpu_to_le64(2);
912 strcpy(lde->lde_name, "..");
913 lde->lde_namelen = cpu_to_le16(2);
914 lde->lde_attrs = LUDA_FID;
915 /* append lustre attributes */
916 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
917 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
918 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
920 /* ENOENT happens at the root of filesystem so ignore it */
928 rc = -zap_cursor_retrieve(it->ozi_zc, za);
929 if (unlikely(rc != 0))
932 lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
933 namelen = strlen(za->za_name);
934 if (namelen > NAME_MAX)
935 GOTO(out, rc = -EOVERFLOW);
936 strcpy(lde->lde_name, za->za_name);
937 lde->lde_namelen = cpu_to_le16(namelen);
939 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
940 CERROR("%s: unsupported direntry format: %d %d\n",
941 osd_obj2dev(it->ozi_obj)->od_svname,
942 za->za_integer_length, (int)za->za_num_integers);
944 GOTO(out, rc = -EIO);
947 rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
948 za->za_name, za->za_integer_length, 3, zde);
952 lde->lde_fid = zde->lzd_fid;
953 lde->lde_attrs = LUDA_FID;
955 /* append lustre attributes */
956 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
958 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
964 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
967 struct osd_zap_it *it = (struct osd_zap_it *)di;
968 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
973 if (it->ozi_pos <= 1)
975 else if (it->ozi_pos == 2)
979 rc = lu_dirent_calc_size(namelen, attr);
983 rc = -zap_cursor_retrieve(it->ozi_zc, za);
984 if (unlikely(rc != 0))
987 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
988 CERROR("%s: unsupported direntry format: %d %d\n",
989 osd_obj2dev(it->ozi_obj)->od_svname,
990 za->za_integer_length, (int)za->za_num_integers);
994 namelen = strlen(za->za_name);
995 if (namelen > NAME_MAX)
998 rc = lu_dirent_calc_size(namelen, attr);
1003 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1005 struct osd_zap_it *it = (struct osd_zap_it *)di;
1009 if (it->ozi_pos <= 2)
1012 pos = osd_zap_cursor_serialize(it->ozi_zc);
1019 * rc == 0 -> end of directory.
1020 * rc > 0 -> ok, proceed.
1021 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1023 static int osd_dir_it_load(const struct lu_env *env,
1024 const struct dt_it *di, __u64 hash)
1026 struct osd_zap_it *it = (struct osd_zap_it *)di;
1027 struct osd_object *obj = it->ozi_obj;
1028 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1032 /* reset the cursor */
1033 zap_cursor_fini(it->ozi_zc);
1034 osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1041 /* to return whether the end has been reached */
1042 rc = osd_index_retrieve_skip_dots(it, za);
1045 else if (rc == -ENOENT)
1052 struct dt_index_operations osd_dir_ops = {
1053 .dio_lookup = osd_dir_lookup,
1054 .dio_declare_insert = osd_declare_dir_insert,
1055 .dio_insert = osd_dir_insert,
1056 .dio_declare_delete = osd_declare_dir_delete,
1057 .dio_delete = osd_dir_delete,
1059 .init = osd_dir_it_init,
1060 .fini = osd_index_it_fini,
1061 .get = osd_dir_it_get,
1062 .put = osd_dir_it_put,
1063 .next = osd_dir_it_next,
1064 .key = osd_dir_it_key,
1065 .key_size = osd_dir_it_key_size,
1066 .rec = osd_dir_it_rec,
1067 .rec_size = osd_dir_it_rec_size,
1068 .store = osd_dir_it_store,
1069 .load = osd_dir_it_load
1074 * Primitives for index files using binary keys.
1077 /* key integer_size is 8 */
1078 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1079 const struct dt_key *src)
1086 /* align keysize to 64bit */
1087 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1088 size *= sizeof(__u64);
1090 LASSERT(size <= MAXNAMELEN);
1092 if (unlikely(size > o->oo_keysize))
1093 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1094 memcpy(dst, (const char *)src, o->oo_keysize);
1096 return (size/sizeof(__u64));
1099 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1100 struct dt_rec *rec, const struct dt_key *key)
1102 struct osd_object *obj = osd_dt_obj(dt);
1103 struct osd_device *osd = osd_obj2dev(obj);
1104 __u64 *k = osd_oti_get(env)->oti_key64;
1108 rc = osd_prepare_key_uint64(obj, k, key);
1110 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1111 k, rc, obj->oo_recusize, obj->oo_recsize,
1113 RETURN(rc == 0 ? 1 : rc);
1116 static int osd_declare_index_insert(const struct lu_env *env,
1117 struct dt_object *dt,
1118 const struct dt_rec *rec,
1119 const struct dt_key *key,
1122 struct osd_object *obj = osd_dt_obj(dt);
1123 struct osd_thandle *oh;
1126 LASSERT(th != NULL);
1127 oh = container_of0(th, struct osd_thandle, ot_super);
1129 LASSERT(obj->oo_dn);
1131 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_dn->dn_object);
1133 /* do not specify the key as then DMU is trying to look it up
1134 * which is very expensive. usually the layers above lookup
1135 * before insertion */
1136 dmu_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, TRUE, NULL);
1141 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1142 const struct dt_rec *rec, const struct dt_key *key,
1143 struct thandle *th, int ignore_quota)
1145 struct osd_object *obj = osd_dt_obj(dt);
1146 struct osd_device *osd = osd_obj2dev(obj);
1147 struct osd_thandle *oh;
1148 __u64 *k = osd_oti_get(env)->oti_key64;
1152 LASSERT(obj->oo_dn);
1153 LASSERT(dt_object_exists(dt));
1154 LASSERT(osd_invariant(obj));
1155 LASSERT(th != NULL);
1157 oh = container_of0(th, struct osd_thandle, ot_super);
1159 rc = osd_prepare_key_uint64(obj, k, key);
1161 /* Insert (key,oid) into ZAP */
1162 rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1163 k, rc, obj->oo_recusize, obj->oo_recsize,
1164 (void *)rec, oh->ot_tx);
1168 static int osd_declare_index_delete(const struct lu_env *env,
1169 struct dt_object *dt,
1170 const struct dt_key *key,
1173 struct osd_object *obj = osd_dt_obj(dt);
1174 struct osd_thandle *oh;
1177 LASSERT(dt_object_exists(dt));
1178 LASSERT(osd_invariant(obj));
1179 LASSERT(th != NULL);
1180 LASSERT(obj->oo_dn);
1182 oh = container_of0(th, struct osd_thandle, ot_super);
1184 /* do not specify the key as then DMU is trying to look it up
1185 * which is very expensive. usually the layers above lookup
1186 * before deletion */
1187 dmu_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, FALSE, NULL);
1192 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1193 const struct dt_key *key, struct thandle *th)
1195 struct osd_object *obj = osd_dt_obj(dt);
1196 struct osd_device *osd = osd_obj2dev(obj);
1197 struct osd_thandle *oh;
1198 __u64 *k = osd_oti_get(env)->oti_key64;
1202 LASSERT(obj->oo_dn);
1203 LASSERT(th != NULL);
1204 oh = container_of0(th, struct osd_thandle, ot_super);
1206 rc = osd_prepare_key_uint64(obj, k, key);
1208 /* Remove binary key from the ZAP */
1209 rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1214 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1215 const struct dt_key *key)
1217 struct osd_zap_it *it = (struct osd_zap_it *)di;
1218 struct osd_object *obj = it->ozi_obj;
1219 struct osd_device *osd = osd_obj2dev(obj);
1223 LASSERT(it->ozi_zc);
1226 * XXX: we need a binary version of zap_cursor_move_to_key()
1227 * to implement this API */
1228 if (*((const __u64 *)key) != 0)
1229 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1232 zap_cursor_fini(it->ozi_zc);
1233 zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1239 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1241 struct osd_zap_it *it = (struct osd_zap_it *)di;
1242 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1246 if (it->ozi_reset == 0)
1247 zap_cursor_advance(it->ozi_zc);
1251 * According to current API we need to return error if it's last entry.
1252 * zap_cursor_advance() does not return any value. So we need to call
1253 * retrieve to check if there is any record. We should make
1254 * changes to Iterator API to not return status for this API
1256 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1263 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1264 const struct dt_it *di)
1266 struct osd_zap_it *it = (struct osd_zap_it *)di;
1267 struct osd_object *obj = it->ozi_obj;
1268 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1273 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1275 RETURN(ERR_PTR(rc));
1277 /* the binary key is stored in the name */
1278 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1280 RETURN((struct dt_key *)&it->ozi_key);
1283 static int osd_index_it_key_size(const struct lu_env *env,
1284 const struct dt_it *di)
1286 struct osd_zap_it *it = (struct osd_zap_it *)di;
1287 struct osd_object *obj = it->ozi_obj;
1288 RETURN(obj->oo_keysize);
1291 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1292 struct dt_rec *rec, __u32 attr)
1294 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1295 struct osd_zap_it *it = (struct osd_zap_it *)di;
1296 struct osd_object *obj = it->ozi_obj;
1297 struct osd_device *osd = osd_obj2dev(obj);
1298 __u64 *k = osd_oti_get(env)->oti_key64;
1303 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1307 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1309 rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1310 k, rc, obj->oo_recusize, obj->oo_recsize,
1315 static __u64 osd_index_it_store(const struct lu_env *env,
1316 const struct dt_it *di)
1318 struct osd_zap_it *it = (struct osd_zap_it *)di;
1321 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1324 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1327 struct osd_zap_it *it = (struct osd_zap_it *)di;
1328 struct osd_object *obj = it->ozi_obj;
1329 struct osd_device *osd = osd_obj2dev(obj);
1330 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1334 /* reset the cursor */
1335 zap_cursor_fini(it->ozi_zc);
1336 zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1337 obj->oo_dn->dn_object, hash);
1340 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1343 else if (rc == -ENOENT)
1349 static struct dt_index_operations osd_index_ops = {
1350 .dio_lookup = osd_index_lookup,
1351 .dio_declare_insert = osd_declare_index_insert,
1352 .dio_insert = osd_index_insert,
1353 .dio_declare_delete = osd_declare_index_delete,
1354 .dio_delete = osd_index_delete,
1356 .init = osd_index_it_init,
1357 .fini = osd_index_it_fini,
1358 .get = osd_index_it_get,
1359 .put = osd_index_it_put,
1360 .next = osd_index_it_next,
1361 .key = osd_index_it_key,
1362 .key_size = osd_index_it_key_size,
1363 .rec = osd_index_it_rec,
1364 .store = osd_index_it_store,
1365 .load = osd_index_it_load
1369 struct osd_metadnode_it {
1370 struct osd_device *mit_dev;
1372 struct lu_fid mit_fid;
1374 __u64 mit_prefetched_dnode;
1377 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1378 struct dt_object *dt, __u32 attr)
1380 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1381 struct osd_metadnode_it *it;
1385 if (unlikely(it == NULL))
1386 RETURN(ERR_PTR(-ENOMEM));
1390 /* XXX: dmu_object_next() does NOT find dnodes allocated
1391 * in the current non-committed txg, so we force txg
1392 * commit to find all existing dnodes ... */
1393 if (!dev->od_dt_dev.dd_rdonly)
1394 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1396 RETURN((struct dt_it *)it);
1399 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1401 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1406 static int osd_zfs_otable_it_get(const struct lu_env *env,
1407 struct dt_it *di, const struct dt_key *key)
1412 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1416 #define OTABLE_PREFETCH 256
1418 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1419 struct osd_metadnode_it *it)
1421 struct osd_device *dev = it->mit_dev;
1424 /* can go negative on the very first access to the iterator
1425 * or if some non-Lustre objects were found */
1426 if (unlikely(it->mit_prefetched < 0))
1427 it->mit_prefetched = 0;
1429 if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1432 if (it->mit_prefetched_dnode == 0)
1433 it->mit_prefetched_dnode = it->mit_pos;
1435 while (it->mit_prefetched < OTABLE_PREFETCH) {
1436 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1438 if (unlikely(rc != 0))
1441 osd_dmu_prefetch(dev->od_os, it->mit_prefetched_dnode,
1442 0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1444 it->mit_prefetched++;
1448 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1450 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1451 struct lustre_mdt_attrs *lma;
1452 struct osd_device *dev = it->mit_dev;
1453 nvlist_t *nvbuf = NULL;
1458 memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1460 dnode = it->mit_pos;
1462 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1463 if (unlikely(rc != 0))
1465 it->mit_prefetched--;
1467 /* LMA is required for this to be a Lustre object.
1468 * If there is no xattr skip it. */
1469 rc = __osd_xattr_load(dev, it->mit_pos, &nvbuf);
1470 if (unlikely(rc != 0))
1473 LASSERT(nvbuf != NULL);
1474 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1475 if (likely(rc == 0)) {
1477 lma = (struct lustre_mdt_attrs *)v;
1478 lustre_lma_swab(lma);
1479 it->mit_fid = lma->lma_self_fid;
1483 /* not a Lustre object, try next one */
1490 /* we aren't prefetching in the above loop because the number of
1491 * non-Lustre objects is very small and we will be repeating very
1492 * rare. in case we want to use this to iterate over non-Lustre
1493 * objects (i.e. when we convert regular ZFS in Lustre) it makes
1494 * sense to initiate prefetching in the loop */
1496 /* 0 - there are more items, +1 - the end */
1497 if (likely(rc == 0))
1498 osd_zfs_otable_prefetch(env, it);
1500 CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1501 it->mit_pos, PFID(&it->mit_fid), rc);
1507 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1508 const struct dt_it *di)
1513 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1514 const struct dt_it *di)
1516 return sizeof(__u64);
1519 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1520 const struct dt_it *di,
1521 struct dt_rec *rec, __u32 attr)
1523 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1524 struct lu_fid *fid = (struct lu_fid *)rec;
1533 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1534 const struct dt_it *di)
1536 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1541 static int osd_zfs_otable_it_load(const struct lu_env *env,
1542 const struct dt_it *di, __u64 hash)
1544 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1547 it->mit_prefetched = 0;
1548 it->mit_prefetched_dnode = 0;
1550 return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1553 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1554 const struct dt_it *di, void *key_rec)
1559 const struct dt_index_operations osd_zfs_otable_ops = {
1561 .init = osd_zfs_otable_it_init,
1562 .fini = osd_zfs_otable_it_fini,
1563 .get = osd_zfs_otable_it_get,
1564 .put = osd_zfs_otable_it_put,
1565 .next = osd_zfs_otable_it_next,
1566 .key = osd_zfs_otable_it_key,
1567 .key_size = osd_zfs_otable_it_key_size,
1568 .rec = osd_zfs_otable_it_rec,
1569 .store = osd_zfs_otable_it_store,
1570 .load = osd_zfs_otable_it_load,
1571 .key_rec = osd_zfs_otable_it_key_rec,
1575 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1576 const struct dt_index_features *feat)
1578 struct osd_object *obj = osd_dt_obj(dt);
1582 down_read(&obj->oo_guard);
1585 * XXX: implement support for fixed-size keys sorted with natural
1586 * numerical way (not using internal hash value)
1588 if (feat->dif_flags & DT_IND_RANGE)
1589 GOTO(out, rc = -ERANGE);
1591 if (unlikely(feat == &dt_otable_features)) {
1592 dt->do_index_ops = &osd_zfs_otable_ops;
1596 LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
1597 if (likely(feat == &dt_directory_features)) {
1598 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
1599 dt->do_index_ops = &osd_dir_ops;
1601 GOTO(out, rc = -ENOTDIR);
1602 } else if (unlikely(feat == &dt_acct_features)) {
1603 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1604 dt->do_index_ops = &osd_acct_index_ops;
1605 } else if (dt->do_index_ops == NULL) {
1606 /* For index file, we don't support variable key & record sizes
1607 * and the key has to be unique */
1608 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1609 GOTO(out, rc = -EINVAL);
1611 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1612 GOTO(out, rc = -E2BIG);
1613 if (feat->dif_keysize_max != feat->dif_keysize_min)
1614 GOTO(out, rc = -EINVAL);
1616 /* As for the record size, it should be a multiple of 8 bytes
1617 * and smaller than the maximum value length supported by ZAP.
1619 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1620 GOTO(out, rc = -E2BIG);
1621 if (feat->dif_recsize_max != feat->dif_recsize_min)
1622 GOTO(out, rc = -EINVAL);
1624 obj->oo_keysize = feat->dif_keysize_max;
1625 obj->oo_recsize = feat->dif_recsize_max;
1626 obj->oo_recusize = 1;
1628 /* ZFS prefers to work with array of 64bits */
1629 if ((obj->oo_recsize & 7) == 0) {
1630 obj->oo_recsize >>= 3;
1631 obj->oo_recusize = 8;
1633 dt->do_index_ops = &osd_index_ops;
1637 up_read(&obj->oo_guard);