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 struct dt_it *osd_index_it_init(const struct lu_env *env,
73 struct lustre_capa *capa)
75 struct osd_thread_info *info = osd_oti_get(env);
76 struct osd_zap_it *it;
77 struct osd_object *obj = osd_dt_obj(dt);
78 struct osd_device *osd = osd_obj2dev(obj);
79 struct lu_object *lo = &dt->do_lu;
82 /* XXX: check capa ? */
84 LASSERT(lu_object_exists(lo));
86 LASSERT(udmu_object_is_zap(obj->oo_db));
89 it = &info->oti_it_zap;
91 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
92 obj->oo_db->db_object, 0))
93 RETURN(ERR_PTR(-ENOMEM));
100 RETURN((struct dt_it *)it);
103 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
105 struct osd_zap_it *it = (struct osd_zap_it *)di;
106 struct osd_object *obj;
110 LASSERT(it->ozi_obj);
114 udmu_zap_cursor_fini(it->ozi_zc);
115 lu_object_put(env, &obj->oo_dt.do_lu);
121 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
123 /* PBS: do nothing : ref are incremented at retrive and decreamented
127 int udmu_zap_cursor_retrieve_key(const struct lu_env *env,
128 zap_cursor_t *zc, char *key, int max)
130 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
133 if ((err = zap_cursor_retrieve(zc, za)))
137 strcpy(key, za->za_name);
143 * zap_cursor_retrieve read from current record.
144 * to read bytes we need to call zap_lookup explicitly.
146 int udmu_zap_cursor_retrieve_value(const struct lu_env *env,
147 zap_cursor_t *zc, char *buf,
148 int buf_size, int *bytes_read)
150 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
151 int err, actual_size;
153 if ((err = zap_cursor_retrieve(zc, za)))
156 if (za->za_integer_length <= 0)
159 actual_size = za->za_integer_length * za->za_num_integers;
161 if (actual_size > buf_size) {
162 actual_size = buf_size;
163 buf_size = actual_size / za->za_integer_length;
165 buf_size = za->za_num_integers;
168 err = -zap_lookup(zc->zc_objset, zc->zc_zapobj,
169 za->za_name, za->za_integer_length,
173 *bytes_read = actual_size;
178 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
181 const unsigned align = sizeof(struct luda_type) - 1;
182 struct luda_type *lt;
184 /* check if file type is required */
185 if (attr & LUDA_TYPE) {
186 len = (len + align) & ~align;
188 lt = (void *)ent->lde_name + len;
189 lt->lt_type = cpu_to_le16(DTTOIF(type));
190 ent->lde_attrs |= LUDA_TYPE;
193 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
197 * as we don't know FID, we can't use LU object, so this function
198 * partially duplicate __osd_xattr_get() which is built around
199 * LU-object and uses it to cache data like regular EA dnode, etc
201 static int osd_find_parent_by_dnode(const struct lu_env *env,
205 struct lustre_mdt_attrs *lma;
206 udmu_objset_t *uos = &osd_obj2dev(osd_dt_obj(o))->od_objset;
209 nvlist_t *nvbuf = NULL;
215 /* first of all, get parent dnode from own attributes */
216 LASSERT(osd_dt_obj(o)->oo_db);
217 rc = -sa_handle_get(uos->os, osd_dt_obj(o)->oo_db->db_object,
218 NULL, SA_HDL_PRIVATE, &sa_hdl);
222 dnode = ZFS_NO_OBJECT;
223 rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(uos), &dnode, 8);
224 sa_handle_destroy(sa_hdl);
228 /* now get EA buffer */
229 rc = __osd_xattr_load(uos, dnode, &nvbuf);
233 /* XXX: if we get that far.. should we cache the result? */
235 /* try to find LMA attribute */
236 LASSERT(nvbuf != NULL);
237 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &value, &size);
238 if (rc == 0 && size >= sizeof(*lma)) {
239 lma = (struct lustre_mdt_attrs *)value;
240 lustre_lma_swab(lma);
241 *fid = lma->lma_self_fid;
246 /* no LMA attribute in SA, let's try regular EA */
248 /* first of all, get parent dnode storing regular EA */
249 rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
253 dnode = ZFS_NO_OBJECT;
254 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(uos), &dnode, 8);
255 sa_handle_destroy(sa_hdl);
259 CLASSERT(sizeof(*lma) <= sizeof(osd_oti_get(env)->oti_buf));
260 buf.lb_buf = osd_oti_get(env)->oti_buf;
261 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
263 /* now try to find LMA */
264 rc = __osd_xattr_get_large(env, uos, dnode, &buf,
265 XATTR_NAME_LMA, &size);
266 if (rc == 0 && size >= sizeof(*lma)) {
268 lustre_lma_swab(lma);
269 *fid = lma->lma_self_fid;
274 GOTO(out, rc = -EIO);
283 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
286 struct link_ea_header *leh;
287 struct link_ea_entry *lee;
292 buf.lb_buf = osd_oti_get(env)->oti_buf;
293 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
295 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
297 rc = osd_xattr_get(env, o, &LU_BUF_NULL,
298 XATTR_NAME_LINK, BYPASS_CAPA);
302 OBD_ALLOC(buf.lb_buf, rc);
303 if (buf.lb_buf == NULL)
306 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
310 if (rc < sizeof(*leh) + sizeof(*lee))
311 GOTO(out, rc = -EINVAL);
314 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
315 leh->leh_magic = LINK_EA_MAGIC;
316 leh->leh_reccount = __swab32(leh->leh_reccount);
317 leh->leh_len = __swab64(leh->leh_len);
319 if (leh->leh_magic != LINK_EA_MAGIC)
320 GOTO(out, rc = -EINVAL);
321 if (leh->leh_reccount == 0)
322 GOTO(out, rc = -ENODATA);
324 lee = (struct link_ea_entry *)(leh + 1);
325 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
329 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
330 OBD_FREE(buf.lb_buf, buf.lb_len);
333 /* this block can be enabled for additional verification
334 * it's trying to match FID from LinkEA vs. FID from LMA */
338 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
340 if (lu_fid_eq(fid, &fid2) == 0)
341 CERROR("wrong parent: "DFID" != "DFID"\n",
342 PFID(fid), PFID(&fid2));
346 /* no LinkEA is found, let's try to find the fid in parent's LMA */
347 if (unlikely(rc != 0))
348 rc = osd_find_parent_by_dnode(env, o, fid);
353 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
354 struct dt_rec *rec, const struct dt_key *key,
355 struct lustre_capa *capa)
357 struct osd_thread_info *oti = osd_oti_get(env);
358 struct osd_object *obj = osd_dt_obj(dt);
359 struct osd_device *osd = osd_obj2dev(obj);
360 char *name = (char *)key;
364 LASSERT(udmu_object_is_zap(obj->oo_db));
366 if (name[0] == '.') {
368 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
369 memcpy(rec, f, sizeof(*f));
371 } else if (name[1] == '.' && name[2] == 0) {
372 rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
373 RETURN(rc == 0 ? 1 : rc);
377 rc = -zap_lookup(osd->od_objset.os, obj->oo_db->db_object,
378 (char *)key, 8, sizeof(oti->oti_zde) / 8,
379 (void *)&oti->oti_zde);
380 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
382 RETURN(rc == 0 ? 1 : rc);
385 static int osd_declare_dir_insert(const struct lu_env *env,
386 struct dt_object *dt,
387 const struct dt_rec *rec,
388 const struct dt_key *key,
391 struct osd_object *obj = osd_dt_obj(dt);
392 struct osd_thandle *oh;
396 oh = container_of0(th, struct osd_thandle, ot_super);
399 LASSERT(udmu_object_is_zap(obj->oo_db));
401 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
402 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
408 * Find the osd object for given fid.
410 * \param fid need to find the osd object having this fid
412 * \retval osd_object on success
413 * \retval -ve on error
415 struct osd_object *osd_object_find(const struct lu_env *env,
416 struct dt_object *dt,
417 const struct lu_fid *fid)
419 struct lu_device *ludev = dt->do_lu.lo_dev;
420 struct osd_object *child = NULL;
421 struct lu_object *luch;
422 struct lu_object *lo;
425 * at this point topdev might not exist yet
426 * (i.e. MGS is preparing profiles). so we can
427 * not rely on topdev and instead lookup with
428 * our device passed as topdev. this can't work
429 * if the object isn't cached yet (as osd doesn't
430 * allocate lu_header). IOW, the object must be
431 * in the cache, otherwise lu_object_alloc() crashes
434 luch = lu_object_find_at(env, ludev, fid, NULL);
438 if (lu_object_exists(luch)) {
439 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
443 LU_OBJECT_DEBUG(D_ERROR, env, luch,
444 "%s: object can't be located "DFID"\n",
445 osd_dev(ludev)->od_svname, PFID(fid));
448 lu_object_put(env, luch);
449 CERROR("%s: Unable to get osd_object "DFID"\n",
450 osd_dev(ludev)->od_svname, PFID(fid));
451 child = ERR_PTR(-ENOENT);
454 LU_OBJECT_DEBUG(D_ERROR, env, luch,
455 "%s: lu_object does not exists "DFID"\n",
456 osd_dev(ludev)->od_svname, PFID(fid));
457 lu_object_put(env, luch);
458 child = ERR_PTR(-ENOENT);
465 * Put the osd object once done with it.
467 * \param obj osd object that needs to be put
469 static inline void osd_object_put(const struct lu_env *env,
470 struct osd_object *obj)
472 lu_object_put(env, &obj->oo_dt.do_lu);
475 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
478 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
479 struct seq_server_site *ss = osd_seq_site(osd);
484 LASSERT(ss->ss_server_fld != NULL);
486 rc = osd_fld_lookup(env, osd, seq, range);
488 CERROR("%s: Can not lookup fld for "LPX64"\n",
493 RETURN(ss->ss_node_id == range->lsr_index);
496 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
497 const struct lu_fid *fid)
499 struct seq_server_site *ss = osd_seq_site(osd);
502 /* FID seqs not in FLDB, must be local seq */
503 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
506 /* If FLD is not being initialized yet, it only happens during the
507 * initialization, likely during mgs initialization, and we assume
508 * this is local FID. */
509 if (ss == NULL || ss->ss_server_fld == NULL)
512 /* Only check the local FLDB here */
513 if (osd_seq_exists(env, osd, fid_seq(fid)))
520 * Inserts (key, value) pair in \a directory object.
522 * \param dt osd index object
523 * \param key key for index
524 * \param rec record reference
525 * \param th transaction handler
526 * \param capa capability descriptor
527 * \param ignore_quota update should not affect quota
530 * \retval -ve failure
532 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
533 const struct dt_rec *rec, const struct dt_key *key,
534 struct thandle *th, struct lustre_capa *capa,
537 struct osd_thread_info *oti = osd_oti_get(env);
538 struct osd_object *parent = osd_dt_obj(dt);
539 struct osd_device *osd = osd_obj2dev(parent);
540 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
541 const struct lu_fid *fid = rec1->rec_fid;
542 struct osd_thandle *oh;
543 struct osd_object *child = NULL;
545 char *name = (char *)key;
549 LASSERT(parent->oo_db);
550 LASSERT(udmu_object_is_zap(parent->oo_db));
552 LASSERT(dt_object_exists(dt));
553 LASSERT(osd_invariant(parent));
556 oh = container_of0(th, struct osd_thandle, ot_super);
558 rc = osd_remote_fid(env, osd, fid);
560 CERROR("%s: Can not find object "DFID": rc = %d\n",
561 osd->od_svname, PFID(fid), rc);
565 if (unlikely(rc == 1)) {
566 /* Insert remote entry */
567 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
568 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
571 * To simulate old Orion setups with ./.. stored in the
574 /* Insert local entry */
575 child = osd_object_find(env, dt, fid);
577 RETURN(PTR_ERR(child));
579 LASSERT(child->oo_db);
580 if (name[0] == '.') {
582 /* do not store ".", instead generate it
583 * during iteration */
585 } else if (name[1] == '.' && name[2] == 0) {
586 /* update parent dnode in the child.
587 * later it will be used to generate ".." */
588 udmu_objset_t *uos = &osd->od_objset;
589 rc = osd_object_sa_update(parent,
591 &child->oo_db->db_object,
596 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
597 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
598 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
599 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
600 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
603 oti->oti_zde.lzd_fid = *fid;
604 /* Insert (key,oid) into ZAP */
605 rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
606 (char *)key, 8, sizeof(oti->oti_zde) / 8,
607 (void *)&oti->oti_zde, oh->ot_tx);
611 osd_object_put(env, child);
616 static int osd_declare_dir_delete(const struct lu_env *env,
617 struct dt_object *dt,
618 const struct dt_key *key,
621 struct osd_object *obj = osd_dt_obj(dt);
622 struct osd_thandle *oh;
625 LASSERT(dt_object_exists(dt));
626 LASSERT(osd_invariant(obj));
629 oh = container_of0(th, struct osd_thandle, ot_super);
632 LASSERT(udmu_object_is_zap(obj->oo_db));
634 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
639 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
640 const struct dt_key *key, struct thandle *th,
641 struct lustre_capa *capa)
643 struct osd_object *obj = osd_dt_obj(dt);
644 struct osd_device *osd = osd_obj2dev(obj);
645 struct osd_thandle *oh;
646 dmu_buf_t *zap_db = obj->oo_db;
647 char *name = (char *)key;
652 LASSERT(udmu_object_is_zap(obj->oo_db));
655 oh = container_of0(th, struct osd_thandle, ot_super);
658 * In Orion . and .. were stored in the directory (not generated upon
659 * request as now). we preserve them for backward compatibility
661 if (name[0] == '.') {
664 } else if (name[1] == '.' && name[2] == 0) {
669 /* Remove key from the ZAP */
670 rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
671 (char *) key, oh->ot_tx);
673 #if LUSTRE_VERSION_CODE <= OBD_OCD_VERSION(2, 4, 53, 0)
674 if (unlikely(rc == -ENOENT && name[0] == '.' &&
675 (name[1] == 0 || (name[1] == '.' && name[2] == 0))))
678 if (unlikely(rc && rc != -ENOENT))
679 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
684 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
685 struct dt_object *dt,
687 struct lustre_capa *capa)
689 struct osd_zap_it *it;
691 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
695 RETURN((struct dt_it *)it);
699 * Move Iterator to record specified by \a key
701 * \param di osd iterator
702 * \param key key for index
704 * \retval +ve di points to record with least key not larger than key
705 * \retval 0 di points to exact matched key
706 * \retval -ve failure
708 static int osd_dir_it_get(const struct lu_env *env,
709 struct dt_it *di, const struct dt_key *key)
711 struct osd_zap_it *it = (struct osd_zap_it *)di;
712 struct osd_object *obj = it->ozi_obj;
713 struct osd_device *osd = osd_obj2dev(obj);
714 char *name = (char *)key;
721 udmu_zap_cursor_fini(it->ozi_zc);
723 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
724 obj->oo_db->db_object, 0))
727 /* XXX: implementation of the API is broken at the moment */
728 LASSERT(((const char *)key)[0] == 0);
735 if (name[0] == '.') {
739 } else if (name[1] == '.' && name[2] == 0) {
745 /* neither . nor .. - some real record */
753 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
755 /* PBS: do nothing : ref are incremented at retrive and decreamented
760 * in Orion . and .. were stored in the directory, while ZPL
761 * and current osd-zfs generate them up on request. so, we
762 * need to ignore previously stored . and ..
764 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
770 rc = -zap_cursor_retrieve(it->ozi_zc, za);
773 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
774 if (za->za_name[1] == 0) {
776 } else if (za->za_name[1] == '.' &&
777 za->za_name[2] == 0) {
781 zap_cursor_advance(it->ozi_zc);
783 } while (unlikely(rc == 0 && isdot));
789 * to load a directory entry at a time and stored it in
790 * iterator's in-memory data structure.
792 * \param di, struct osd_it_ea, iterator's in memory structure
794 * \retval +ve, iterator reached to end
795 * \retval 0, iterator not reached to end
796 * \retval -ve, on error
798 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
800 struct osd_zap_it *it = (struct osd_zap_it *)di;
801 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
804 /* temp. storage should be enough for any key supported by ZFS */
805 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
808 * the first ->next() moves the cursor to .
809 * the second ->next() moves the cursor to ..
810 * then we get to the real records and have to verify any exist
812 if (it->ozi_pos <= 2) {
818 zap_cursor_advance(it->ozi_zc);
821 * According to current API we need to return error if its last entry.
822 * zap_cursor_advance() does not return any value. So we need to call
823 * retrieve to check if there is any record. We should make
824 * changes to Iterator API to not return status for this API
826 rc = osd_index_retrieve_skip_dots(it, za);
828 if (rc == -ENOENT) /* end of dir */
834 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
835 const struct dt_it *di)
837 struct osd_zap_it *it = (struct osd_zap_it *)di;
838 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
842 if (it->ozi_pos <= 1) {
844 RETURN((struct dt_key *)".");
845 } else if (it->ozi_pos == 2) {
846 RETURN((struct dt_key *)"..");
849 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
852 strcpy(it->ozi_name, za->za_name);
854 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
855 if (za->za_name[0] == '.') {
856 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
857 za->za_name[2] == 0)) {
858 /* we should not get onto . and ..
859 * stored in the directory. ->next() and
860 * other methods should prevent this
867 RETURN((struct dt_key *)it->ozi_name);
870 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
872 struct osd_zap_it *it = (struct osd_zap_it *)di;
873 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
877 if (it->ozi_pos <= 1) {
880 } else if (it->ozi_pos == 2) {
884 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
885 rc = strlen(za->za_name);
887 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
888 if (rc == 0 && za->za_name[0] == '.') {
889 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
890 za->za_name[2] == 0)) {
891 /* we should not get onto . and ..
892 * stored in the directory. ->next() and
893 * other methods should prevent this
902 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
903 struct dt_rec *dtrec, __u32 attr)
905 struct osd_zap_it *it = (struct osd_zap_it *)di;
906 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
907 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
908 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
912 if (it->ozi_pos <= 1) {
913 lde->lde_hash = cpu_to_le64(1);
914 strcpy(lde->lde_name, ".");
915 lde->lde_namelen = cpu_to_le16(1);
916 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
917 lde->lde_attrs = LUDA_FID;
918 /* append lustre attributes */
919 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
920 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
924 } else if (it->ozi_pos == 2) {
925 lde->lde_hash = cpu_to_le64(2);
926 strcpy(lde->lde_name, "..");
927 lde->lde_namelen = cpu_to_le16(2);
928 lde->lde_attrs = LUDA_FID;
929 /* append lustre attributes */
930 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
931 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
932 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
934 * early Orion code was not setting LinkEA, so it's possible
935 * some setups still have objects with no LinkEA set.
936 * but at that time .. was a real record in the directory
937 * so we should try to lookup .. in ZAP
945 rc = -zap_cursor_retrieve(it->ozi_zc, za);
946 if (unlikely(rc != 0))
949 lde->lde_hash = cpu_to_le64(udmu_zap_cursor_serialize(it->ozi_zc));
950 namelen = strlen(za->za_name);
951 if (namelen > NAME_MAX)
952 GOTO(out, rc = -EOVERFLOW);
953 strcpy(lde->lde_name, za->za_name);
954 lde->lde_namelen = cpu_to_le16(namelen);
956 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
957 CERROR("%s: unsupported direntry format: %d %d\n",
958 osd_obj2dev(it->ozi_obj)->od_svname,
959 za->za_integer_length, (int)za->za_num_integers);
961 GOTO(out, rc = -EIO);
964 rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
965 za->za_name, za->za_integer_length, 3, zde);
969 lde->lde_fid = zde->lzd_fid;
970 lde->lde_attrs = LUDA_FID;
972 /* append lustre attributes */
973 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
975 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
981 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
984 struct osd_zap_it *it = (struct osd_zap_it *)di;
985 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
989 if (it->ozi_pos <= 1)
991 else if (it->ozi_pos == 2)
995 rc = lu_dirent_calc_size(namelen, attr);
999 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1000 if (unlikely(rc != 0))
1003 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
1004 CERROR("%s: unsupported direntry format: %d %d\n",
1005 osd_obj2dev(it->ozi_obj)->od_svname,
1006 za->za_integer_length, (int)za->za_num_integers);
1010 namelen = strlen(za->za_name);
1011 if (namelen > NAME_MAX)
1014 rc = lu_dirent_calc_size(namelen, attr);
1019 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1021 struct osd_zap_it *it = (struct osd_zap_it *)di;
1025 if (it->ozi_pos <= 2)
1028 pos = udmu_zap_cursor_serialize(it->ozi_zc);
1035 * rc == 0 -> end of directory.
1036 * rc > 0 -> ok, proceed.
1037 * rc < 0 -> error. ( EOVERFLOW can be masked.)
1039 static int osd_dir_it_load(const struct lu_env *env,
1040 const struct dt_it *di, __u64 hash)
1042 struct osd_zap_it *it = (struct osd_zap_it *)di;
1043 struct osd_object *obj = it->ozi_obj;
1044 struct osd_device *osd = osd_obj2dev(obj);
1045 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1049 udmu_zap_cursor_fini(it->ozi_zc);
1050 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
1051 obj->oo_db->db_object, hash))
1059 /* to return whether the end has been reached */
1060 rc = osd_index_retrieve_skip_dots(it, za);
1063 else if (rc == -ENOENT)
1070 static struct dt_index_operations osd_dir_ops = {
1071 .dio_lookup = osd_dir_lookup,
1072 .dio_declare_insert = osd_declare_dir_insert,
1073 .dio_insert = osd_dir_insert,
1074 .dio_declare_delete = osd_declare_dir_delete,
1075 .dio_delete = osd_dir_delete,
1077 .init = osd_dir_it_init,
1078 .fini = osd_index_it_fini,
1079 .get = osd_dir_it_get,
1080 .put = osd_dir_it_put,
1081 .next = osd_dir_it_next,
1082 .key = osd_dir_it_key,
1083 .key_size = osd_dir_it_key_size,
1084 .rec = osd_dir_it_rec,
1085 .rec_size = osd_dir_it_rec_size,
1086 .store = osd_dir_it_store,
1087 .load = osd_dir_it_load
1092 * Primitives for index files using binary keys.
1095 /* key integer_size is 8 */
1096 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1097 const struct dt_key *src)
1104 /* align keysize to 64bit */
1105 size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1106 size *= sizeof(__u64);
1108 LASSERT(size <= MAXNAMELEN);
1110 if (unlikely(size > o->oo_keysize))
1111 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1112 memcpy(dst, (const char *)src, o->oo_keysize);
1114 return (size/sizeof(__u64));
1117 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1118 struct dt_rec *rec, const struct dt_key *key,
1119 struct lustre_capa *capa)
1121 struct osd_object *obj = osd_dt_obj(dt);
1122 struct osd_device *osd = osd_obj2dev(obj);
1123 __u64 *k = osd_oti_get(env)->oti_key64;
1127 rc = osd_prepare_key_uint64(obj, k, key);
1129 rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1130 k, rc, obj->oo_recusize, obj->oo_recsize,
1132 RETURN(rc == 0 ? 1 : rc);
1135 static int osd_declare_index_insert(const struct lu_env *env,
1136 struct dt_object *dt,
1137 const struct dt_rec *rec,
1138 const struct dt_key *key,
1141 struct osd_object *obj = osd_dt_obj(dt);
1142 struct osd_thandle *oh;
1145 LASSERT(th != NULL);
1146 oh = container_of0(th, struct osd_thandle, ot_super);
1148 LASSERT(obj->oo_db);
1150 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1152 /* It is not clear what API should be used for binary keys, so we pass
1153 * a null name which has the side effect of over-reserving space,
1154 * accounting for the worst case. See zap_count_write() */
1155 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1160 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1161 const struct dt_rec *rec, const struct dt_key *key,
1162 struct thandle *th, struct lustre_capa *capa,
1165 struct osd_object *obj = osd_dt_obj(dt);
1166 struct osd_device *osd = osd_obj2dev(obj);
1167 struct osd_thandle *oh;
1168 __u64 *k = osd_oti_get(env)->oti_key64;
1172 LASSERT(obj->oo_db);
1173 LASSERT(dt_object_exists(dt));
1174 LASSERT(osd_invariant(obj));
1175 LASSERT(th != NULL);
1177 oh = container_of0(th, struct osd_thandle, ot_super);
1179 rc = osd_prepare_key_uint64(obj, k, key);
1181 /* Insert (key,oid) into ZAP */
1182 rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
1183 k, rc, obj->oo_recusize, obj->oo_recsize,
1184 (void *)rec, oh->ot_tx);
1188 static int osd_declare_index_delete(const struct lu_env *env,
1189 struct dt_object *dt,
1190 const struct dt_key *key,
1193 struct osd_object *obj = osd_dt_obj(dt);
1194 struct osd_thandle *oh;
1197 LASSERT(dt_object_exists(dt));
1198 LASSERT(osd_invariant(obj));
1199 LASSERT(th != NULL);
1200 LASSERT(obj->oo_db);
1202 oh = container_of0(th, struct osd_thandle, ot_super);
1203 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1208 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1209 const struct dt_key *key, struct thandle *th,
1210 struct lustre_capa *capa)
1212 struct osd_object *obj = osd_dt_obj(dt);
1213 struct osd_device *osd = osd_obj2dev(obj);
1214 struct osd_thandle *oh;
1215 __u64 *k = osd_oti_get(env)->oti_key64;
1219 LASSERT(obj->oo_db);
1220 LASSERT(th != NULL);
1221 oh = container_of0(th, struct osd_thandle, ot_super);
1223 rc = osd_prepare_key_uint64(obj, k, key);
1225 /* Remove binary key from the ZAP */
1226 rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
1231 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1232 const struct dt_key *key)
1234 struct osd_zap_it *it = (struct osd_zap_it *)di;
1235 struct osd_object *obj = it->ozi_obj;
1236 struct osd_device *osd = osd_obj2dev(obj);
1240 LASSERT(it->ozi_zc);
1243 * XXX: we need a binary version of zap_cursor_move_to_key()
1244 * to implement this API */
1245 if (*((const __u64 *)key) != 0)
1246 CERROR("NOT IMPLEMETED YET (move to "LPX64")\n",
1249 zap_cursor_fini(it->ozi_zc);
1250 memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1251 zap_cursor_init(it->ozi_zc, osd->od_objset.os, obj->oo_db->db_object);
1257 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1259 struct osd_zap_it *it = (struct osd_zap_it *)di;
1260 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1264 if (it->ozi_reset == 0)
1265 zap_cursor_advance(it->ozi_zc);
1269 * According to current API we need to return error if it's last entry.
1270 * zap_cursor_advance() does not return any value. So we need to call
1271 * retrieve to check if there is any record. We should make
1272 * changes to Iterator API to not return status for this API
1274 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1281 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1282 const struct dt_it *di)
1284 struct osd_zap_it *it = (struct osd_zap_it *)di;
1285 struct osd_object *obj = it->ozi_obj;
1286 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1291 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1293 RETURN(ERR_PTR(rc));
1295 /* the binary key is stored in the name */
1296 memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1298 RETURN((struct dt_key *)&it->ozi_key);
1301 static int osd_index_it_key_size(const struct lu_env *env,
1302 const struct dt_it *di)
1304 struct osd_zap_it *it = (struct osd_zap_it *)di;
1305 struct osd_object *obj = it->ozi_obj;
1306 RETURN(obj->oo_keysize);
1309 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1310 struct dt_rec *rec, __u32 attr)
1312 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1313 struct osd_zap_it *it = (struct osd_zap_it *)di;
1314 struct osd_object *obj = it->ozi_obj;
1315 struct osd_device *osd = osd_obj2dev(obj);
1316 __u64 *k = osd_oti_get(env)->oti_key64;
1321 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1325 rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1327 rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1328 k, rc, obj->oo_recusize, obj->oo_recsize,
1333 static __u64 osd_index_it_store(const struct lu_env *env,
1334 const struct dt_it *di)
1336 struct osd_zap_it *it = (struct osd_zap_it *)di;
1339 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1342 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1345 struct osd_zap_it *it = (struct osd_zap_it *)di;
1346 struct osd_object *obj = it->ozi_obj;
1347 struct osd_device *osd = osd_obj2dev(obj);
1348 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1352 /* close the current cursor */
1353 zap_cursor_fini(it->ozi_zc);
1355 /* create a new one starting at hash */
1356 memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1357 zap_cursor_init_serialized(it->ozi_zc, osd->od_objset.os,
1358 obj->oo_db->db_object, hash);
1361 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1364 else if (rc == -ENOENT)
1370 static struct dt_index_operations osd_index_ops = {
1371 .dio_lookup = osd_index_lookup,
1372 .dio_declare_insert = osd_declare_index_insert,
1373 .dio_insert = osd_index_insert,
1374 .dio_declare_delete = osd_declare_index_delete,
1375 .dio_delete = osd_index_delete,
1377 .init = osd_index_it_init,
1378 .fini = osd_index_it_fini,
1379 .get = osd_index_it_get,
1380 .put = osd_index_it_put,
1381 .next = osd_index_it_next,
1382 .key = osd_index_it_key,
1383 .key_size = osd_index_it_key_size,
1384 .rec = osd_index_it_rec,
1385 .store = osd_index_it_store,
1386 .load = osd_index_it_load
1390 struct osd_metadnode_it {
1391 struct osd_device *mit_dev;
1393 struct lu_fid mit_fid;
1395 __u64 mit_prefetched_dnode;
1398 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1399 struct dt_object *dt, __u32 attr,
1400 struct lustre_capa *capa)
1402 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1403 struct osd_metadnode_it *it;
1407 if (unlikely(it == NULL))
1408 RETURN(ERR_PTR(-ENOMEM));
1412 /* XXX: dmu_object_next() does NOT find dnodes allocated
1413 * in the current non-committed txg, so we force txg
1414 * commit to find all existing dnodes ... */
1415 txg_wait_synced(dmu_objset_pool(dev->od_objset.os), 0ULL);
1417 RETURN((struct dt_it *)it);
1420 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1422 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1427 static int osd_zfs_otable_it_get(const struct lu_env *env,
1428 struct dt_it *di, const struct dt_key *key)
1433 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1437 #define OTABLE_PREFETCH 256
1439 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1440 struct osd_metadnode_it *it)
1442 struct osd_device *dev = it->mit_dev;
1443 udmu_objset_t *uos = &dev->od_objset;
1446 /* can go negative on the very first access to the iterator
1447 * or if some non-Lustre objects were found */
1448 if (unlikely(it->mit_prefetched < 0))
1449 it->mit_prefetched = 0;
1451 if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1454 if (it->mit_prefetched_dnode == 0)
1455 it->mit_prefetched_dnode = it->mit_pos;
1457 while (it->mit_prefetched < OTABLE_PREFETCH) {
1458 rc = -dmu_object_next(uos->os, &it->mit_prefetched_dnode,
1460 if (unlikely(rc != 0))
1463 /* dmu_prefetch() was exported in 0.6.2, if you use with
1464 * an older release, just comment it out - this is an
1466 dmu_prefetch(uos->os, it->mit_prefetched_dnode, 0, 0);
1468 it->mit_prefetched++;
1472 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1474 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1475 struct lustre_mdt_attrs *lma;
1476 struct osd_device *dev = it->mit_dev;
1477 udmu_objset_t *uos = &dev->od_objset;
1478 nvlist_t *nvbuf = NULL;
1483 memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1485 dnode = it->mit_pos;
1487 rc = -dmu_object_next(uos->os, &it->mit_pos, B_FALSE, 0);
1488 if (unlikely(rc != 0))
1490 it->mit_prefetched--;
1492 /* LMA is required for this to be a Lustre object.
1493 * If there is no xattr skip it. */
1494 rc = __osd_xattr_load(uos, it->mit_pos, &nvbuf);
1495 if (unlikely(rc != 0))
1498 LASSERT(nvbuf != NULL);
1499 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1500 if (likely(rc == 0)) {
1502 lma = (struct lustre_mdt_attrs *)v;
1503 lustre_lma_swab(lma);
1504 it->mit_fid = lma->lma_self_fid;
1508 /* not a Lustre object, try next one */
1515 /* we aren't prefetching in the above loop because the number of
1516 * non-Lustre objects is very small and we will be repeating very
1517 * rare. in case we want to use this to iterate over non-Lustre
1518 * objects (i.e. when we convert regular ZFS in Lustre) it makes
1519 * sense to initiate prefetching in the loop */
1521 /* 0 - there are more items, +1 - the end */
1522 if (likely(rc == 0))
1523 osd_zfs_otable_prefetch(env, it);
1525 CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1526 it->mit_pos, PFID(&it->mit_fid), rc);
1532 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1533 const struct dt_it *di)
1538 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1539 const struct dt_it *di)
1541 return sizeof(__u64);
1544 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1545 const struct dt_it *di,
1546 struct dt_rec *rec, __u32 attr)
1548 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1549 struct lu_fid *fid = (struct lu_fid *)rec;
1558 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1559 const struct dt_it *di)
1561 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1566 static int osd_zfs_otable_it_load(const struct lu_env *env,
1567 const struct dt_it *di, __u64 hash)
1569 struct osd_metadnode_it *it = (struct osd_metadnode_it *)di;
1572 it->mit_prefetched = 0;
1573 it->mit_prefetched_dnode = 0;
1575 return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1578 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1579 const struct dt_it *di, void *key_rec)
1584 const struct dt_index_operations osd_zfs_otable_ops = {
1586 .init = osd_zfs_otable_it_init,
1587 .fini = osd_zfs_otable_it_fini,
1588 .get = osd_zfs_otable_it_get,
1589 .put = osd_zfs_otable_it_put,
1590 .next = osd_zfs_otable_it_next,
1591 .key = osd_zfs_otable_it_key,
1592 .key_size = osd_zfs_otable_it_key_size,
1593 .rec = osd_zfs_otable_it_rec,
1594 .store = osd_zfs_otable_it_store,
1595 .load = osd_zfs_otable_it_load,
1596 .key_rec = osd_zfs_otable_it_key_rec,
1600 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1601 const struct dt_index_features *feat)
1603 struct osd_object *obj = osd_dt_obj(dt);
1606 LASSERT(dt_object_exists(dt));
1609 * XXX: implement support for fixed-size keys sorted with natural
1610 * numerical way (not using internal hash value)
1612 if (feat->dif_flags & DT_IND_RANGE)
1615 if (unlikely(feat == &dt_otable_features)) {
1616 dt->do_index_ops = &osd_zfs_otable_ops;
1620 LASSERT(obj->oo_db != NULL);
1621 if (likely(feat == &dt_directory_features)) {
1622 if (udmu_object_is_zap(obj->oo_db))
1623 dt->do_index_ops = &osd_dir_ops;
1626 } else if (unlikely(feat == &dt_acct_features)) {
1627 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1628 dt->do_index_ops = &osd_acct_index_ops;
1629 } else if (udmu_object_is_zap(obj->oo_db) &&
1630 dt->do_index_ops == NULL) {
1631 /* For index file, we don't support variable key & record sizes
1632 * and the key has to be unique */
1633 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1636 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1638 if (feat->dif_keysize_max != feat->dif_keysize_min)
1641 /* As for the record size, it should be a multiple of 8 bytes
1642 * and smaller than the maximum value length supported by ZAP.
1644 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1646 if (feat->dif_recsize_max != feat->dif_recsize_min)
1649 obj->oo_keysize = feat->dif_keysize_max;
1650 obj->oo_recsize = feat->dif_recsize_max;
1651 obj->oo_recusize = 1;
1653 /* ZFS prefers to work with array of 64bits */
1654 if ((obj->oo_recsize & 7) == 0) {
1655 obj->oo_recsize >>= 3;
1656 obj->oo_recusize = 8;
1658 dt->do_index_ops = &osd_index_ops;