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, 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>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_OSD
49 #include <lustre_ver.h>
50 #include <libcfs/libcfs.h>
51 #include <lustre_fsfilt.h>
52 #include <obd_support.h>
53 #include <lustre_net.h>
55 #include <obd_class.h>
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
59 #include "osd_internal.h"
61 #include <sys/dnode.h>
66 #include <sys/spa_impl.h>
67 #include <sys/zfs_znode.h>
68 #include <sys/dmu_tx.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/dsl_prop.h>
71 #include <sys/sa_impl.h>
74 static struct dt_it *osd_index_it_init(const struct lu_env *env,
77 struct lustre_capa *capa)
79 struct osd_thread_info *info = osd_oti_get(env);
80 struct osd_zap_it *it;
81 struct osd_object *obj = osd_dt_obj(dt);
82 struct osd_device *osd = osd_obj2dev(obj);
83 struct lu_object *lo = &dt->do_lu;
86 /* XXX: check capa ? */
88 LASSERT(lu_object_exists(lo));
90 LASSERT(udmu_object_is_zap(obj->oo_db));
93 it = &info->oti_it_zap;
95 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
96 obj->oo_db->db_object, 0))
97 RETURN(ERR_PTR(-ENOMEM));
104 RETURN((struct dt_it *)it);
107 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
109 struct osd_zap_it *it = (struct osd_zap_it *)di;
110 struct osd_object *obj;
114 LASSERT(it->ozi_obj);
118 udmu_zap_cursor_fini(it->ozi_zc);
119 lu_object_put(env, &obj->oo_dt.do_lu);
125 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
127 /* PBS: do nothing : ref are incremented at retrive and decreamented
131 int udmu_zap_cursor_retrieve_key(const struct lu_env *env,
132 zap_cursor_t *zc, char *key, int max)
134 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
137 if ((err = zap_cursor_retrieve(zc, za)))
141 strcpy(key, za->za_name);
147 * zap_cursor_retrieve read from current record.
148 * to read bytes we need to call zap_lookup explicitly.
150 int udmu_zap_cursor_retrieve_value(const struct lu_env *env,
151 zap_cursor_t *zc, char *buf,
152 int buf_size, int *bytes_read)
154 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
155 int err, actual_size;
157 if ((err = zap_cursor_retrieve(zc, za)))
160 if (za->za_integer_length <= 0)
163 actual_size = za->za_integer_length * za->za_num_integers;
165 if (actual_size > buf_size) {
166 actual_size = buf_size;
167 buf_size = actual_size / za->za_integer_length;
169 buf_size = za->za_num_integers;
172 err = -zap_lookup(zc->zc_objset, zc->zc_zapobj,
173 za->za_name, za->za_integer_length,
177 *bytes_read = actual_size;
182 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
185 const unsigned align = sizeof(struct luda_type) - 1;
186 struct luda_type *lt;
188 /* check if file type is required */
189 if (attr & LUDA_TYPE) {
190 len = (len + align) & ~align;
192 lt = (void *)ent->lde_name + len;
193 lt->lt_type = cpu_to_le16(DTTOIF(type));
194 ent->lde_attrs |= LUDA_TYPE;
197 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
201 * as we don't know FID, we can't use LU object, so this function
202 * partially duplicate __osd_xattr_get() which is built around
203 * LU-object and uses it to cache data like regular EA dnode, etc
205 static int osd_find_parent_by_dnode(const struct lu_env *env,
209 struct lustre_mdt_attrs *lma;
210 udmu_objset_t *uos = &osd_obj2dev(osd_dt_obj(o))->od_objset;
213 nvlist_t *nvbuf = NULL;
219 /* first of all, get parent dnode from own attributes */
220 LASSERT(osd_dt_obj(o)->oo_db);
221 rc = -sa_handle_get(uos->os, osd_dt_obj(o)->oo_db->db_object,
222 NULL, SA_HDL_PRIVATE, &sa_hdl);
226 dnode = ZFS_NO_OBJECT;
227 rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(uos), &dnode, 8);
228 sa_handle_destroy(sa_hdl);
232 /* now get EA buffer */
233 rc = __osd_xattr_load(uos, dnode, &nvbuf);
237 /* XXX: if we get that far.. should we cache the result? */
239 /* try to find LMA attribute */
240 LASSERT(nvbuf != NULL);
241 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &value, &size);
242 if (rc == 0 && size >= sizeof(*lma)) {
243 lma = (struct lustre_mdt_attrs *)value;
244 lustre_lma_swab(lma);
245 *fid = lma->lma_self_fid;
250 /* no LMA attribute in SA, let's try regular EA */
252 /* first of all, get parent dnode storing regular EA */
253 rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
257 dnode = ZFS_NO_OBJECT;
258 rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(uos), &dnode, 8);
259 sa_handle_destroy(sa_hdl);
263 CLASSERT(sizeof(*lma) <= sizeof(osd_oti_get(env)->oti_buf));
264 buf.lb_buf = osd_oti_get(env)->oti_buf;
265 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
267 /* now try to find LMA */
268 rc = __osd_xattr_get_large(env, uos, dnode, &buf,
269 XATTR_NAME_LMA, &size);
270 if (rc == 0 && size >= sizeof(*lma)) {
272 lustre_lma_swab(lma);
273 *fid = lma->lma_self_fid;
278 GOTO(out, rc = -EIO);
287 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
290 struct link_ea_header *leh;
291 struct link_ea_entry *lee;
296 buf.lb_buf = osd_oti_get(env)->oti_buf;
297 buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
299 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
301 rc = osd_xattr_get(env, o, &LU_BUF_NULL,
302 XATTR_NAME_LINK, BYPASS_CAPA);
306 OBD_ALLOC(buf.lb_buf, rc);
307 if (buf.lb_buf == NULL)
310 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK, BYPASS_CAPA);
314 if (rc < sizeof(*leh) + sizeof(*lee))
315 GOTO(out, rc = -EINVAL);
318 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
319 leh->leh_magic = LINK_EA_MAGIC;
320 leh->leh_reccount = __swab32(leh->leh_reccount);
321 leh->leh_len = __swab64(leh->leh_len);
323 if (leh->leh_magic != LINK_EA_MAGIC)
324 GOTO(out, rc = -EINVAL);
325 if (leh->leh_reccount == 0)
326 GOTO(out, rc = -ENODATA);
328 lee = (struct link_ea_entry *)(leh + 1);
329 fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
333 if (buf.lb_buf != osd_oti_get(env)->oti_buf)
334 OBD_FREE(buf.lb_buf, buf.lb_len);
337 /* this block can be enabled for additional verification
338 * it's trying to match FID from LinkEA vs. FID from LMA */
342 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
344 if (lu_fid_eq(fid, &fid2) == 0)
345 CERROR("wrong parent: "DFID" != "DFID"\n",
346 PFID(fid), PFID(&fid2));
350 /* no LinkEA is found, let's try to find the fid in parent's LMA */
351 if (unlikely(rc != 0))
352 rc = osd_find_parent_by_dnode(env, o, fid);
357 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
358 struct dt_rec *rec, const struct dt_key *key,
359 struct lustre_capa *capa)
361 struct osd_thread_info *oti = osd_oti_get(env);
362 struct osd_object *obj = osd_dt_obj(dt);
363 struct osd_device *osd = osd_obj2dev(obj);
364 char *name = (char *)key;
368 LASSERT(udmu_object_is_zap(obj->oo_db));
370 if (name[0] == '.') {
372 const struct lu_fid *f = lu_object_fid(&dt->do_lu);
373 memcpy(rec, f, sizeof(*f));
375 } else if (name[1] == '.' && name[2] == 0) {
376 rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
377 RETURN(rc == 0 ? 1 : rc);
381 rc = -zap_lookup(osd->od_objset.os, obj->oo_db->db_object,
382 (char *)key, 8, sizeof(oti->oti_zde) / 8,
383 (void *)&oti->oti_zde);
384 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
386 RETURN(rc == 0 ? 1 : rc);
389 static int osd_declare_dir_insert(const struct lu_env *env,
390 struct dt_object *dt,
391 const struct dt_rec *rec,
392 const struct dt_key *key,
395 struct osd_object *obj = osd_dt_obj(dt);
396 struct osd_thandle *oh;
400 oh = container_of0(th, struct osd_thandle, ot_super);
403 LASSERT(udmu_object_is_zap(obj->oo_db));
405 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
406 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
412 * Find the osd object for given fid.
414 * \param fid need to find the osd object having this fid
416 * \retval osd_object on success
417 * \retval -ve on error
419 struct osd_object *osd_object_find(const struct lu_env *env,
420 struct dt_object *dt,
421 const struct lu_fid *fid)
423 struct lu_device *ludev = dt->do_lu.lo_dev;
424 struct osd_object *child = NULL;
425 struct lu_object *luch;
426 struct lu_object *lo;
429 * at this point topdev might not exist yet
430 * (i.e. MGS is preparing profiles). so we can
431 * not rely on topdev and instead lookup with
432 * our device passed as topdev. this can't work
433 * if the object isn't cached yet (as osd doesn't
434 * allocate lu_header). IOW, the object must be
435 * in the cache, otherwise lu_object_alloc() crashes
438 luch = lu_object_find_at(env, ludev, fid, NULL);
442 if (lu_object_exists(luch)) {
443 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
447 LU_OBJECT_DEBUG(D_ERROR, env, luch,
448 "%s: object can't be located "DFID"\n",
449 osd_dev(ludev)->od_svname, PFID(fid));
452 lu_object_put(env, luch);
453 CERROR("%s: Unable to get osd_object "DFID"\n",
454 osd_dev(ludev)->od_svname, PFID(fid));
455 child = ERR_PTR(-ENOENT);
458 LU_OBJECT_DEBUG(D_ERROR, env, luch,
459 "%s: lu_object does not exists "DFID"\n",
460 osd_dev(ludev)->od_svname, PFID(fid));
461 lu_object_put(env, luch);
462 child = ERR_PTR(-ENOENT);
469 * Put the osd object once done with it.
471 * \param obj osd object that needs to be put
473 static inline void osd_object_put(const struct lu_env *env,
474 struct osd_object *obj)
476 lu_object_put(env, &obj->oo_dt.do_lu);
479 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
482 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
483 struct seq_server_site *ss = osd_seq_site(osd);
487 if (!fid_is_norm(fid) && !fid_is_root(fid))
490 rc = osd_fld_lookup(env, osd, fid, range);
492 CERROR("%s: Can not lookup fld for "DFID"\n",
493 osd_name(osd), PFID(fid));
497 RETURN(ss->ss_node_id != range->lsr_index);
501 * Inserts (key, value) pair in \a directory object.
503 * \param dt osd index object
504 * \param key key for index
505 * \param rec record reference
506 * \param th transaction handler
507 * \param capa capability descriptor
508 * \param ignore_quota update should not affect quota
511 * \retval -ve failure
513 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
514 const struct dt_rec *rec, const struct dt_key *key,
515 struct thandle *th, struct lustre_capa *capa,
518 struct osd_thread_info *oti = osd_oti_get(env);
519 struct osd_object *parent = osd_dt_obj(dt);
520 struct osd_device *osd = osd_obj2dev(parent);
521 struct lu_fid *fid = (struct lu_fid *)rec;
522 struct osd_thandle *oh;
523 struct osd_object *child = NULL;
525 char *name = (char *)key;
529 LASSERT(parent->oo_db);
530 LASSERT(udmu_object_is_zap(parent->oo_db));
532 LASSERT(dt_object_exists(dt));
533 LASSERT(osd_invariant(parent));
536 oh = container_of0(th, struct osd_thandle, ot_super);
538 rc = osd_remote_fid(env, osd, fid);
540 CERROR("%s: Can not find object "DFID": rc = %d\n",
541 osd->od_svname, PFID(fid), rc);
545 if (unlikely(rc == 1)) {
546 /* Insert remote entry */
547 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
548 oti->oti_zde.lzd_reg.zde_type = IFTODT(S_IFDIR & S_IFMT);
551 * To simulate old Orion setups with ./.. stored in the
554 /* Insert local entry */
555 child = osd_object_find(env, dt, fid);
557 RETURN(PTR_ERR(child));
559 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 91, 0)
560 #define OSD_ZFS_INSERT_DOTS_FOR_TESTING__
562 LASSERT(child->oo_db);
563 if (name[0] == '.') {
565 /* do not store ".", instead generate it
566 * during iteration */
567 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
570 } else if (name[1] == '.' && name[2] == 0) {
571 /* update parent dnode in the child.
572 * later it will be used to generate ".." */
573 udmu_objset_t *uos = &osd->od_objset;
574 rc = osd_object_sa_update(parent, SA_ZPL_PARENT(uos),
575 &child->oo_db->db_object,
577 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
582 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
583 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
584 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
585 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
586 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
589 oti->oti_zde.lzd_fid = *fid;
590 /* Insert (key,oid) into ZAP */
591 rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
592 (char *)key, 8, sizeof(oti->oti_zde) / 8,
593 (void *)&oti->oti_zde, oh->ot_tx);
595 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
599 osd_object_put(env, child);
604 static int osd_declare_dir_delete(const struct lu_env *env,
605 struct dt_object *dt,
606 const struct dt_key *key,
609 struct osd_object *obj = osd_dt_obj(dt);
610 struct osd_thandle *oh;
613 LASSERT(dt_object_exists(dt));
614 LASSERT(osd_invariant(obj));
617 oh = container_of0(th, struct osd_thandle, ot_super);
620 LASSERT(udmu_object_is_zap(obj->oo_db));
622 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
627 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
628 const struct dt_key *key, struct thandle *th,
629 struct lustre_capa *capa)
631 struct osd_object *obj = osd_dt_obj(dt);
632 struct osd_device *osd = osd_obj2dev(obj);
633 struct osd_thandle *oh;
634 dmu_buf_t *zap_db = obj->oo_db;
635 char *name = (char *)key;
640 LASSERT(udmu_object_is_zap(obj->oo_db));
643 oh = container_of0(th, struct osd_thandle, ot_super);
645 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
647 * in Orion . and .. were stored in the directory (not generated up on
648 * request as now. we preserve them for backward compatibility
650 if (name[0] == '.') {
653 } else if (name[1] == '.' && name[2] == 0) {
659 /* Remove key from the ZAP */
660 rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
661 (char *) key, oh->ot_tx);
663 #if LUSTRE_VERSION_CODE <= OBD_OCD_VERSION(2, 4, 53, 0)
664 if (unlikely(rc == -ENOENT && name[0] == '.' &&
665 (name[1] == 0 || (name[1] == '.' && name[2] == 0))))
668 if (unlikely(rc && rc != -ENOENT))
669 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
674 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
675 struct dt_object *dt,
677 struct lustre_capa *capa)
679 struct osd_zap_it *it;
681 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
685 RETURN((struct dt_it *)it);
689 * Move Iterator to record specified by \a key
691 * \param di osd iterator
692 * \param key key for index
694 * \retval +ve di points to record with least key not larger than key
695 * \retval 0 di points to exact matched key
696 * \retval -ve failure
698 static int osd_dir_it_get(const struct lu_env *env,
699 struct dt_it *di, const struct dt_key *key)
701 struct osd_zap_it *it = (struct osd_zap_it *)di;
702 struct osd_object *obj = it->ozi_obj;
703 struct osd_device *osd = osd_obj2dev(obj);
704 char *name = (char *)key;
711 udmu_zap_cursor_fini(it->ozi_zc);
713 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
714 obj->oo_db->db_object, 0))
717 /* XXX: implementation of the API is broken at the moment */
718 LASSERT(((const char *)key)[0] == 0);
725 if (name[0] == '.') {
729 } else if (name[1] == '.' && name[2] == 0) {
735 /* neither . nor .. - some real record */
743 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
745 /* PBS: do nothing : ref are incremented at retrive and decreamented
750 * in Orion . and .. were stored in the directory, while ZPL
751 * and current osd-zfs generate them up on request. so, we
752 * need to ignore previously stored . and ..
754 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
760 rc = -zap_cursor_retrieve(it->ozi_zc, za);
763 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
764 if (za->za_name[1] == 0) {
766 } else if (za->za_name[1] == '.' &&
767 za->za_name[2] == 0) {
771 zap_cursor_advance(it->ozi_zc);
773 } while (unlikely(rc == 0 && isdot));
779 * to load a directory entry at a time and stored it in
780 * iterator's in-memory data structure.
782 * \param di, struct osd_it_ea, iterator's in memory structure
784 * \retval +ve, iterator reached to end
785 * \retval 0, iterator not reached to end
786 * \retval -ve, on error
788 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
790 struct osd_zap_it *it = (struct osd_zap_it *)di;
791 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
794 /* temp. storage should be enough for any key supported by ZFS */
795 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
798 * the first ->next() moves the cursor to .
799 * the second ->next() moves the cursor to ..
800 * then we get to the real records and have to verify any exist
802 if (it->ozi_pos <= 2) {
808 zap_cursor_advance(it->ozi_zc);
811 * According to current API we need to return error if its last entry.
812 * zap_cursor_advance() does not return any value. So we need to call
813 * retrieve to check if there is any record. We should make
814 * changes to Iterator API to not return status for this API
816 rc = osd_index_retrieve_skip_dots(it, za);
818 if (rc == -ENOENT) /* end of dir */
824 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
825 const struct dt_it *di)
827 struct osd_zap_it *it = (struct osd_zap_it *)di;
828 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
832 if (it->ozi_pos <= 1) {
834 RETURN((struct dt_key *)".");
835 } else if (it->ozi_pos == 2) {
836 RETURN((struct dt_key *)"..");
839 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
842 strcpy(it->ozi_name, za->za_name);
844 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 91, 0)
845 if (za->za_name[0] == '.') {
846 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
847 za->za_name[2] == 0)) {
848 /* we should not get onto . and ..
849 * stored in the directory. ->next() and
850 * other methods should prevent this
857 RETURN((struct dt_key *)it->ozi_name);
860 static int osd_dir_it_key_size(const struct lu_env *env, const 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;
867 if (it->ozi_pos <= 1) {
870 } else if (it->ozi_pos == 2) {
874 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
875 rc = strlen(za->za_name);
877 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 99, 0)
878 if (rc == 0 && za->za_name[0] == '.') {
879 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
880 za->za_name[2] == 0)) {
881 /* we should not get onto . and ..
882 * stored in the directory. ->next() and
883 * other methods should prevent this
892 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
893 struct dt_rec *dtrec, __u32 attr)
895 struct osd_zap_it *it = (struct osd_zap_it *)di;
896 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
897 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
898 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
902 if (it->ozi_pos <= 1) {
903 lde->lde_hash = cpu_to_le64(1);
904 strcpy(lde->lde_name, ".");
905 lde->lde_namelen = cpu_to_le16(1);
906 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
907 lde->lde_attrs = LUDA_FID;
908 /* append lustre attributes */
909 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
910 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
914 } else if (it->ozi_pos == 2) {
915 lde->lde_hash = cpu_to_le64(2);
916 strcpy(lde->lde_name, "..");
917 lde->lde_namelen = cpu_to_le16(2);
918 lde->lde_attrs = LUDA_FID;
919 /* append lustre attributes */
920 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
921 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
922 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
924 * early Orion code was not setting LinkEA, so it's possible
925 * some setups still have objects with no LinkEA set.
926 * but at that time .. was a real record in the directory
927 * so we should try to lookup .. in ZAP
935 lde->lde_hash = cpu_to_le64(udmu_zap_cursor_serialize(it->ozi_zc));
937 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
940 namelen = strlen(za->za_name);
941 if (namelen > NAME_MAX)
942 GOTO(out, rc = -EOVERFLOW);
943 strcpy(lde->lde_name, za->za_name);
944 lde->lde_namelen = cpu_to_le16(namelen);
946 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
947 CERROR("%s: unsupported direntry format: %d %d\n",
948 osd_obj2dev(it->ozi_obj)->od_svname,
949 za->za_integer_length, (int)za->za_num_integers);
951 GOTO(out, rc = -EIO);
954 rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
955 za->za_name, za->za_integer_length, 3, zde);
959 lde->lde_fid = zde->lzd_fid;
960 lde->lde_attrs = LUDA_FID;
962 /* append lustre attributes */
963 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
965 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
971 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
973 struct osd_zap_it *it = (struct osd_zap_it *)di;
977 if (it->ozi_pos <= 2)
980 pos = udmu_zap_cursor_serialize(it->ozi_zc);
987 * rc == 0 -> end of directory.
988 * rc > 0 -> ok, proceed.
989 * rc < 0 -> error. ( EOVERFLOW can be masked.)
991 static int osd_dir_it_load(const struct lu_env *env,
992 const struct dt_it *di, __u64 hash)
994 struct osd_zap_it *it = (struct osd_zap_it *)di;
995 struct osd_object *obj = it->ozi_obj;
996 struct osd_device *osd = osd_obj2dev(obj);
997 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1001 udmu_zap_cursor_fini(it->ozi_zc);
1002 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
1003 obj->oo_db->db_object, hash))
1011 /* to return whether the end has been reached */
1012 rc = osd_index_retrieve_skip_dots(it, za);
1015 else if (rc == -ENOENT)
1022 static struct dt_index_operations osd_dir_ops = {
1023 .dio_lookup = osd_dir_lookup,
1024 .dio_declare_insert = osd_declare_dir_insert,
1025 .dio_insert = osd_dir_insert,
1026 .dio_declare_delete = osd_declare_dir_delete,
1027 .dio_delete = osd_dir_delete,
1029 .init = osd_dir_it_init,
1030 .fini = osd_index_it_fini,
1031 .get = osd_dir_it_get,
1032 .put = osd_dir_it_put,
1033 .next = osd_dir_it_next,
1034 .key = osd_dir_it_key,
1035 .key_size = osd_dir_it_key_size,
1036 .rec = osd_dir_it_rec,
1037 .store = osd_dir_it_store,
1038 .load = osd_dir_it_load
1043 * Primitives for index files using binary keys.
1044 * XXX: only 64-bit keys are supported for now.
1047 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1048 struct dt_rec *rec, const struct dt_key *key,
1049 struct lustre_capa *capa)
1051 struct osd_object *obj = osd_dt_obj(dt);
1052 struct osd_device *osd = osd_obj2dev(obj);
1056 rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1057 (const __u64 *)key, 1, 8, obj->oo_recsize,
1059 RETURN(rc == 0 ? 1 : rc);
1062 static int osd_declare_index_insert(const struct lu_env *env,
1063 struct dt_object *dt,
1064 const struct dt_rec *rec,
1065 const struct dt_key *key,
1068 struct osd_object *obj = osd_dt_obj(dt);
1069 struct osd_thandle *oh;
1072 LASSERT(th != NULL);
1073 oh = container_of0(th, struct osd_thandle, ot_super);
1075 LASSERT(obj->oo_db);
1077 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1079 /* It is not clear what API should be used for binary keys, so we pass
1080 * a null name which has the side effect of over-reserving space,
1081 * accounting for the worst case. See zap_count_write() */
1082 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1087 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1088 const struct dt_rec *rec, const struct dt_key *key,
1089 struct thandle *th, struct lustre_capa *capa,
1092 struct osd_object *obj = osd_dt_obj(dt);
1093 struct osd_device *osd = osd_obj2dev(obj);
1094 struct osd_thandle *oh;
1098 LASSERT(obj->oo_db);
1099 LASSERT(dt_object_exists(dt));
1100 LASSERT(osd_invariant(obj));
1101 LASSERT(th != NULL);
1103 oh = container_of0(th, struct osd_thandle, ot_super);
1105 /* Insert (key,oid) into ZAP */
1106 rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
1107 (const __u64 *)key, 1, 8, obj->oo_recsize,
1108 (void *)rec, oh->ot_tx);
1112 static int osd_declare_index_delete(const struct lu_env *env,
1113 struct dt_object *dt,
1114 const struct dt_key *key,
1117 struct osd_object *obj = osd_dt_obj(dt);
1118 struct osd_thandle *oh;
1121 LASSERT(dt_object_exists(dt));
1122 LASSERT(osd_invariant(obj));
1123 LASSERT(th != NULL);
1124 LASSERT(obj->oo_db);
1126 oh = container_of0(th, struct osd_thandle, ot_super);
1127 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1132 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1133 const struct dt_key *key, struct thandle *th,
1134 struct lustre_capa *capa)
1136 struct osd_object *obj = osd_dt_obj(dt);
1137 struct osd_device *osd = osd_obj2dev(obj);
1138 struct osd_thandle *oh;
1142 LASSERT(obj->oo_db);
1143 LASSERT(th != NULL);
1144 oh = container_of0(th, struct osd_thandle, ot_super);
1146 /* Remove binary key from the ZAP */
1147 rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
1148 (const __u64 *)key, 1, oh->ot_tx);
1152 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1153 const struct dt_key *key)
1155 struct osd_zap_it *it = (struct osd_zap_it *)di;
1156 struct osd_object *obj = it->ozi_obj;
1157 struct osd_device *osd = osd_obj2dev(obj);
1161 LASSERT(it->ozi_zc);
1163 /* XXX: API is broken at the moment */
1164 LASSERT(*((const __u64 *)key) == 0);
1166 zap_cursor_fini(it->ozi_zc);
1167 memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1168 zap_cursor_init(it->ozi_zc, osd->od_objset.os, obj->oo_db->db_object);
1174 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1176 struct osd_zap_it *it = (struct osd_zap_it *)di;
1177 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1181 if (it->ozi_reset == 0)
1182 zap_cursor_advance(it->ozi_zc);
1186 * According to current API we need to return error if it's last entry.
1187 * zap_cursor_advance() does not return any value. So we need to call
1188 * retrieve to check if there is any record. We should make
1189 * changes to Iterator API to not return status for this API
1191 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1198 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1199 const struct dt_it *di)
1201 struct osd_zap_it *it = (struct osd_zap_it *)di;
1202 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1207 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1209 RETURN(ERR_PTR(rc));
1211 /* the binary key is stored in the name */
1212 it->ozi_key = *((__u64 *)za->za_name);
1214 RETURN((struct dt_key *)&it->ozi_key);
1217 static int osd_index_it_key_size(const struct lu_env *env,
1218 const struct dt_it *di)
1220 /* we only support 64-bit binary keys for the time being */
1221 RETURN(sizeof(__u64));
1224 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1225 struct dt_rec *rec, __u32 attr)
1227 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1228 struct osd_zap_it *it = (struct osd_zap_it *)di;
1229 struct osd_object *obj = it->ozi_obj;
1230 struct osd_device *osd = osd_obj2dev(obj);
1235 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1239 rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1240 (const __u64 *)za->za_name, 1, 8,
1241 obj->oo_recsize, (void *)rec);
1245 static __u64 osd_index_it_store(const struct lu_env *env,
1246 const struct dt_it *di)
1248 struct osd_zap_it *it = (struct osd_zap_it *)di;
1251 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1254 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1257 struct osd_zap_it *it = (struct osd_zap_it *)di;
1258 struct osd_object *obj = it->ozi_obj;
1259 struct osd_device *osd = osd_obj2dev(obj);
1260 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1264 /* close the current cursor */
1265 zap_cursor_fini(it->ozi_zc);
1267 /* create a new one starting at hash */
1268 memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1269 zap_cursor_init_serialized(it->ozi_zc, osd->od_objset.os,
1270 obj->oo_db->db_object, hash);
1273 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1276 else if (rc == -ENOENT)
1282 static struct dt_index_operations osd_index_ops = {
1283 .dio_lookup = osd_index_lookup,
1284 .dio_declare_insert = osd_declare_index_insert,
1285 .dio_insert = osd_index_insert,
1286 .dio_declare_delete = osd_declare_index_delete,
1287 .dio_delete = osd_index_delete,
1289 .init = osd_index_it_init,
1290 .fini = osd_index_it_fini,
1291 .get = osd_index_it_get,
1292 .put = osd_index_it_put,
1293 .next = osd_index_it_next,
1294 .key = osd_index_it_key,
1295 .key_size = osd_index_it_key_size,
1296 .rec = osd_index_it_rec,
1297 .store = osd_index_it_store,
1298 .load = osd_index_it_load
1302 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1303 const struct dt_index_features *feat)
1305 struct osd_object *obj = osd_dt_obj(dt);
1308 LASSERT(dt_object_exists(dt));
1311 * XXX: implement support for fixed-size keys sorted with natural
1312 * numerical way (not using internal hash value)
1314 if (feat->dif_flags & DT_IND_RANGE)
1317 if (unlikely(feat == &dt_otable_features))
1318 /* do not support oi scrub yet. */
1321 LASSERT(obj->oo_db != NULL);
1322 if (likely(feat == &dt_directory_features)) {
1323 if (udmu_object_is_zap(obj->oo_db))
1324 dt->do_index_ops = &osd_dir_ops;
1327 } else if (unlikely(feat == &dt_acct_features)) {
1328 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1329 dt->do_index_ops = &osd_acct_index_ops;
1330 } else if (udmu_object_is_zap(obj->oo_db) &&
1331 dt->do_index_ops == NULL) {
1332 /* For index file, we don't support variable key & record sizes
1333 * and the key has to be unique */
1334 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1337 /* Although the zap_*_uint64() primitives support large keys, we
1338 * limit ourselves to 64-bit keys for now */
1339 if (feat->dif_keysize_max != sizeof(__u64) ||
1340 feat->dif_keysize_min != sizeof(__u64))
1343 /* As for the record size, it should be a multiple of 8 bytes
1344 * and smaller than the maximum value length supported by ZAP.
1346 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1348 if (feat->dif_recsize_max != feat->dif_recsize_min ||
1349 (feat->dif_recsize_max & (sizeof(__u64) - 1)))
1352 obj->oo_recsize = feat->dif_recsize_max / sizeof(__u64);
1353 dt->do_index_ops = &osd_index_ops;