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);
480 * Inserts (key, value) pair in \a directory object.
482 * \param dt osd index object
483 * \param key key for index
484 * \param rec record reference
485 * \param th transaction handler
486 * \param capa capability descriptor
487 * \param ignore_quota update should not affect quota
490 * \retval -ve failure
492 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
493 const struct dt_rec *rec, const struct dt_key *key,
494 struct thandle *th, struct lustre_capa *capa,
497 struct osd_thread_info *oti = osd_oti_get(env);
498 struct osd_object *parent = osd_dt_obj(dt);
499 struct osd_device *osd = osd_obj2dev(parent);
500 struct lu_fid *fid = (struct lu_fid *)rec;
501 struct osd_thandle *oh;
502 struct osd_object *child;
504 char *name = (char *)key;
508 LASSERT(parent->oo_db);
509 LASSERT(udmu_object_is_zap(parent->oo_db));
511 LASSERT(dt_object_exists(dt));
512 LASSERT(osd_invariant(parent));
515 oh = container_of0(th, struct osd_thandle, ot_super);
517 child = osd_object_find(env, dt, fid);
519 RETURN(PTR_ERR(child));
522 * to simulate old Orion setups with ./.. stored in the directories
524 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 91, 0)
525 #define OSD_ZFS_INSERT_DOTS_FOR_TESTING__
528 LASSERT(child->oo_db);
529 if (name[0] == '.') {
531 /* do not store ".", instead generate it
532 * during iteration */
533 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
536 } else if (name[1] == '.' && name[2] == 0) {
537 /* update parent dnode in the child.
538 * later it will be used to generate ".." */
539 udmu_objset_t *uos = &osd->od_objset;
540 rc = osd_object_sa_update(parent, SA_ZPL_PARENT(uos),
541 &child->oo_db->db_object,
544 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
550 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
551 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
552 attr = child->oo_dt.do_lu.lo_header ->loh_attr;
553 oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
554 oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
555 oti->oti_zde.lzd_fid = *fid;
557 /* Insert (key,oid) into ZAP */
558 rc = -zap_add(osd->od_objset.os, parent->oo_db->db_object,
559 (char *)key, 8, sizeof(oti->oti_zde) / 8,
560 (void *)&oti->oti_zde, oh->ot_tx);
562 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
565 osd_object_put(env, child);
570 static int osd_declare_dir_delete(const struct lu_env *env,
571 struct dt_object *dt,
572 const struct dt_key *key,
575 struct osd_object *obj = osd_dt_obj(dt);
576 struct osd_thandle *oh;
579 LASSERT(dt_object_exists(dt));
580 LASSERT(osd_invariant(obj));
583 oh = container_of0(th, struct osd_thandle, ot_super);
586 LASSERT(udmu_object_is_zap(obj->oo_db));
588 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, (char *)key);
593 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
594 const struct dt_key *key, struct thandle *th,
595 struct lustre_capa *capa)
597 struct osd_object *obj = osd_dt_obj(dt);
598 struct osd_device *osd = osd_obj2dev(obj);
599 struct osd_thandle *oh;
600 dmu_buf_t *zap_db = obj->oo_db;
601 char *name = (char *)key;
606 LASSERT(udmu_object_is_zap(obj->oo_db));
609 oh = container_of0(th, struct osd_thandle, ot_super);
611 #ifndef OSD_ZFS_INSERT_DOTS_FOR_TESTING
613 * in Orion . and .. were stored in the directory (not generated up on
614 * request as now. we preserve them for backward compatibility
616 if (name[0] == '.') {
619 } else if (name[1] == '.' && name[2] == 0) {
625 /* Remove key from the ZAP */
626 rc = -zap_remove(osd->od_objset.os, zap_db->db_object,
627 (char *) key, oh->ot_tx);
629 #if LUSTRE_VERSION_CODE <= OBD_OCD_VERSION(2, 4, 53, 0)
630 if (unlikely(rc == -ENOENT && name[0] == '.' &&
631 (name[1] == 0 || (name[1] == '.' && name[2] == 0))))
634 if (unlikely(rc && rc != -ENOENT))
635 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
640 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
641 struct dt_object *dt,
643 struct lustre_capa *capa)
645 struct osd_zap_it *it;
647 it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused, capa);
651 RETURN((struct dt_it *)it);
655 * Move Iterator to record specified by \a key
657 * \param di osd iterator
658 * \param key key for index
660 * \retval +ve di points to record with least key not larger than key
661 * \retval 0 di points to exact matched key
662 * \retval -ve failure
664 static int osd_dir_it_get(const struct lu_env *env,
665 struct dt_it *di, const struct dt_key *key)
667 struct osd_zap_it *it = (struct osd_zap_it *)di;
668 struct osd_object *obj = it->ozi_obj;
669 struct osd_device *osd = osd_obj2dev(obj);
670 char *name = (char *)key;
677 udmu_zap_cursor_fini(it->ozi_zc);
679 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
680 obj->oo_db->db_object, 0))
683 /* XXX: implementation of the API is broken at the moment */
684 LASSERT(((const char *)key)[0] == 0);
691 if (name[0] == '.') {
695 } else if (name[1] == '.' && name[2] == 0) {
701 /* neither . nor .. - some real record */
709 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
711 /* PBS: do nothing : ref are incremented at retrive and decreamented
716 * in Orion . and .. were stored in the directory, while ZPL
717 * and current osd-zfs generate them up on request. so, we
718 * need to ignore previously stored . and ..
720 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
726 rc = -zap_cursor_retrieve(it->ozi_zc, za);
729 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
730 if (za->za_name[1] == 0) {
732 } else if (za->za_name[1] == '.' &&
733 za->za_name[2] == 0) {
737 zap_cursor_advance(it->ozi_zc);
739 } while (unlikely(rc == 0 && isdot));
745 * to load a directory entry at a time and stored it in
746 * iterator's in-memory data structure.
748 * \param di, struct osd_it_ea, iterator's in memory structure
750 * \retval +ve, iterator reached to end
751 * \retval 0, iterator not reached to end
752 * \retval -ve, on error
754 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
756 struct osd_zap_it *it = (struct osd_zap_it *)di;
757 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
760 /* temp. storage should be enough for any key supported by ZFS */
761 CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
764 * the first ->next() moves the cursor to .
765 * the second ->next() moves the cursor to ..
766 * then we get to the real records and have to verify any exist
768 if (it->ozi_pos <= 2) {
774 zap_cursor_advance(it->ozi_zc);
777 * According to current API we need to return error if its last entry.
778 * zap_cursor_advance() does not return any value. So we need to call
779 * retrieve to check if there is any record. We should make
780 * changes to Iterator API to not return status for this API
782 rc = osd_index_retrieve_skip_dots(it, za);
784 if (rc == -ENOENT) /* end of dir */
790 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
791 const struct dt_it *di)
793 struct osd_zap_it *it = (struct osd_zap_it *)di;
794 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
798 if (it->ozi_pos <= 1) {
800 RETURN((struct dt_key *)".");
801 } else if (it->ozi_pos == 2) {
802 RETURN((struct dt_key *)"..");
805 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
808 strcpy(it->ozi_name, za->za_name);
810 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 91, 0)
811 if (za->za_name[0] == '.') {
812 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
813 za->za_name[2] == 0)) {
814 /* we should not get onto . and ..
815 * stored in the directory. ->next() and
816 * other methods should prevent this
823 RETURN((struct dt_key *)it->ozi_name);
826 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
828 struct osd_zap_it *it = (struct osd_zap_it *)di;
829 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
833 if (it->ozi_pos <= 1) {
836 } else if (it->ozi_pos == 2) {
840 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
841 rc = strlen(za->za_name);
843 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 99, 0)
844 if (rc == 0 && za->za_name[0] == '.') {
845 if (za->za_name[1] == 0 || (za->za_name[1] == '.' &&
846 za->za_name[2] == 0)) {
847 /* we should not get onto . and ..
848 * stored in the directory. ->next() and
849 * other methods should prevent this
858 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
859 struct dt_rec *dtrec, __u32 attr)
861 struct osd_zap_it *it = (struct osd_zap_it *)di;
862 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
863 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
864 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
868 if (it->ozi_pos <= 1) {
869 lde->lde_hash = cpu_to_le64(1);
870 strcpy(lde->lde_name, ".");
871 lde->lde_namelen = cpu_to_le16(1);
872 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
873 lde->lde_attrs = LUDA_FID;
874 /* append lustre attributes */
875 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
876 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
880 } else if (it->ozi_pos == 2) {
881 lde->lde_hash = cpu_to_le64(2);
882 strcpy(lde->lde_name, "..");
883 lde->lde_namelen = cpu_to_le16(2);
884 lde->lde_attrs = LUDA_FID;
885 /* append lustre attributes */
886 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
887 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
888 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
890 * early Orion code was not setting LinkEA, so it's possible
891 * some setups still have objects with no LinkEA set.
892 * but at that time .. was a real record in the directory
893 * so we should try to lookup .. in ZAP
901 lde->lde_hash = cpu_to_le64(udmu_zap_cursor_serialize(it->ozi_zc));
903 if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
906 namelen = strlen(za->za_name);
907 if (namelen > NAME_MAX)
908 GOTO(out, rc = -EOVERFLOW);
909 strcpy(lde->lde_name, za->za_name);
910 lde->lde_namelen = cpu_to_le16(namelen);
912 if (za->za_integer_length != 8 || za->za_num_integers < 3) {
913 CERROR("%s: unsupported direntry format: %d %d\n",
914 osd_obj2dev(it->ozi_obj)->od_svname,
915 za->za_integer_length, (int)za->za_num_integers);
917 GOTO(out, rc = -EIO);
920 rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
921 za->za_name, za->za_integer_length, 3, zde);
925 lde->lde_fid = zde->lzd_fid;
926 lde->lde_attrs = LUDA_FID;
928 /* append lustre attributes */
929 osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
931 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
937 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
939 struct osd_zap_it *it = (struct osd_zap_it *)di;
943 if (it->ozi_pos <= 2)
946 pos = udmu_zap_cursor_serialize(it->ozi_zc);
953 * rc == 0 -> end of directory.
954 * rc > 0 -> ok, proceed.
955 * rc < 0 -> error. ( EOVERFLOW can be masked.)
957 static int osd_dir_it_load(const struct lu_env *env,
958 const struct dt_it *di, __u64 hash)
960 struct osd_zap_it *it = (struct osd_zap_it *)di;
961 struct osd_object *obj = it->ozi_obj;
962 struct osd_device *osd = osd_obj2dev(obj);
963 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
967 if (it->ozi_pos != 0) {
968 /* the cursor wasn't at the beginning
969 * so we should reset ZAP cursor as well */
970 udmu_zap_cursor_fini(it->ozi_zc);
971 if (udmu_zap_cursor_init(&it->ozi_zc, &osd->od_objset,
972 obj->oo_db->db_object, hash))
981 /* to return whether the end has been reached */
982 rc = osd_index_retrieve_skip_dots(it, za);
985 else if (rc == -ENOENT)
992 static struct dt_index_operations osd_dir_ops = {
993 .dio_lookup = osd_dir_lookup,
994 .dio_declare_insert = osd_declare_dir_insert,
995 .dio_insert = osd_dir_insert,
996 .dio_declare_delete = osd_declare_dir_delete,
997 .dio_delete = osd_dir_delete,
999 .init = osd_dir_it_init,
1000 .fini = osd_index_it_fini,
1001 .get = osd_dir_it_get,
1002 .put = osd_dir_it_put,
1003 .next = osd_dir_it_next,
1004 .key = osd_dir_it_key,
1005 .key_size = osd_dir_it_key_size,
1006 .rec = osd_dir_it_rec,
1007 .store = osd_dir_it_store,
1008 .load = osd_dir_it_load
1013 * Primitives for index files using binary keys.
1014 * XXX: only 64-bit keys are supported for now.
1017 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1018 struct dt_rec *rec, const struct dt_key *key,
1019 struct lustre_capa *capa)
1021 struct osd_object *obj = osd_dt_obj(dt);
1022 struct osd_device *osd = osd_obj2dev(obj);
1026 rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1027 (const __u64 *)key, 1, 8, obj->oo_recsize,
1029 RETURN(rc == 0 ? 1 : rc);
1032 static int osd_declare_index_insert(const struct lu_env *env,
1033 struct dt_object *dt,
1034 const struct dt_rec *rec,
1035 const struct dt_key *key,
1038 struct osd_object *obj = osd_dt_obj(dt);
1039 struct osd_thandle *oh;
1042 LASSERT(th != NULL);
1043 oh = container_of0(th, struct osd_thandle, ot_super);
1045 LASSERT(obj->oo_db);
1047 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
1049 /* It is not clear what API should be used for binary keys, so we pass
1050 * a null name which has the side effect of over-reserving space,
1051 * accounting for the worst case. See zap_count_write() */
1052 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1057 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1058 const struct dt_rec *rec, const struct dt_key *key,
1059 struct thandle *th, struct lustre_capa *capa,
1062 struct osd_object *obj = osd_dt_obj(dt);
1063 struct osd_device *osd = osd_obj2dev(obj);
1064 struct osd_thandle *oh;
1068 LASSERT(obj->oo_db);
1069 LASSERT(dt_object_exists(dt));
1070 LASSERT(osd_invariant(obj));
1071 LASSERT(th != NULL);
1073 oh = container_of0(th, struct osd_thandle, ot_super);
1075 /* Insert (key,oid) into ZAP */
1076 rc = -zap_add_uint64(osd->od_objset.os, obj->oo_db->db_object,
1077 (const __u64 *)key, 1, 8, obj->oo_recsize,
1078 (void *)rec, oh->ot_tx);
1082 static int osd_declare_index_delete(const struct lu_env *env,
1083 struct dt_object *dt,
1084 const struct dt_key *key,
1087 struct osd_object *obj = osd_dt_obj(dt);
1088 struct osd_thandle *oh;
1091 LASSERT(dt_object_exists(dt));
1092 LASSERT(osd_invariant(obj));
1093 LASSERT(th != NULL);
1094 LASSERT(obj->oo_db);
1096 oh = container_of0(th, struct osd_thandle, ot_super);
1097 dmu_tx_hold_zap(oh->ot_tx, obj->oo_db->db_object, TRUE, NULL);
1102 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1103 const struct dt_key *key, struct thandle *th,
1104 struct lustre_capa *capa)
1106 struct osd_object *obj = osd_dt_obj(dt);
1107 struct osd_device *osd = osd_obj2dev(obj);
1108 struct osd_thandle *oh;
1112 LASSERT(obj->oo_db);
1113 LASSERT(th != NULL);
1114 oh = container_of0(th, struct osd_thandle, ot_super);
1116 /* Remove binary key from the ZAP */
1117 rc = -zap_remove_uint64(osd->od_objset.os, obj->oo_db->db_object,
1118 (const __u64 *)key, 1, oh->ot_tx);
1122 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1123 const struct dt_key *key)
1125 struct osd_zap_it *it = (struct osd_zap_it *)di;
1126 struct osd_object *obj = it->ozi_obj;
1127 struct osd_device *osd = osd_obj2dev(obj);
1131 LASSERT(it->ozi_zc);
1133 /* XXX: API is broken at the moment */
1134 LASSERT(*((const __u64 *)key) == 0);
1136 zap_cursor_fini(it->ozi_zc);
1137 memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1138 zap_cursor_init(it->ozi_zc, osd->od_objset.os, obj->oo_db->db_object);
1144 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1146 struct osd_zap_it *it = (struct osd_zap_it *)di;
1147 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1151 if (it->ozi_reset == 0)
1152 zap_cursor_advance(it->ozi_zc);
1156 * According to current API we need to return error if it's last entry.
1157 * zap_cursor_advance() does not return any value. So we need to call
1158 * retrieve to check if there is any record. We should make
1159 * changes to Iterator API to not return status for this API
1161 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1168 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1169 const struct dt_it *di)
1171 struct osd_zap_it *it = (struct osd_zap_it *)di;
1172 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1177 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1179 RETURN(ERR_PTR(rc));
1181 /* the binary key is stored in the name */
1182 it->ozi_key = *((__u64 *)za->za_name);
1184 RETURN((struct dt_key *)&it->ozi_key);
1187 static int osd_index_it_key_size(const struct lu_env *env,
1188 const struct dt_it *di)
1190 /* we only support 64-bit binary keys for the time being */
1191 RETURN(sizeof(__u64));
1194 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1195 struct dt_rec *rec, __u32 attr)
1197 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1198 struct osd_zap_it *it = (struct osd_zap_it *)di;
1199 struct osd_object *obj = it->ozi_obj;
1200 struct osd_device *osd = osd_obj2dev(obj);
1205 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1209 rc = -zap_lookup_uint64(osd->od_objset.os, obj->oo_db->db_object,
1210 (const __u64 *)za->za_name, 1, 8,
1211 obj->oo_recsize, (void *)rec);
1215 static __u64 osd_index_it_store(const struct lu_env *env,
1216 const struct dt_it *di)
1218 struct osd_zap_it *it = (struct osd_zap_it *)di;
1221 RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1224 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1227 struct osd_zap_it *it = (struct osd_zap_it *)di;
1228 struct osd_object *obj = it->ozi_obj;
1229 struct osd_device *osd = osd_obj2dev(obj);
1230 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1234 /* close the current cursor */
1235 zap_cursor_fini(it->ozi_zc);
1237 /* create a new one starting at hash */
1238 memset(it->ozi_zc, 0, sizeof(*it->ozi_zc));
1239 zap_cursor_init_serialized(it->ozi_zc, osd->od_objset.os,
1240 obj->oo_db->db_object, hash);
1243 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1246 else if (rc == -ENOENT)
1252 static struct dt_index_operations osd_index_ops = {
1253 .dio_lookup = osd_index_lookup,
1254 .dio_declare_insert = osd_declare_index_insert,
1255 .dio_insert = osd_index_insert,
1256 .dio_declare_delete = osd_declare_index_delete,
1257 .dio_delete = osd_index_delete,
1259 .init = osd_index_it_init,
1260 .fini = osd_index_it_fini,
1261 .get = osd_index_it_get,
1262 .put = osd_index_it_put,
1263 .next = osd_index_it_next,
1264 .key = osd_index_it_key,
1265 .key_size = osd_index_it_key_size,
1266 .rec = osd_index_it_rec,
1267 .store = osd_index_it_store,
1268 .load = osd_index_it_load
1272 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1273 const struct dt_index_features *feat)
1275 struct osd_object *obj = osd_dt_obj(dt);
1278 LASSERT(dt_object_exists(dt));
1281 * XXX: implement support for fixed-size keys sorted with natural
1282 * numerical way (not using internal hash value)
1284 if (feat->dif_flags & DT_IND_RANGE)
1287 if (unlikely(feat == &dt_otable_features))
1288 /* do not support oi scrub yet. */
1291 LASSERT(obj->oo_db != NULL);
1292 if (likely(feat == &dt_directory_features)) {
1293 if (udmu_object_is_zap(obj->oo_db))
1294 dt->do_index_ops = &osd_dir_ops;
1297 } else if (unlikely(feat == &dt_acct_features)) {
1298 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1299 dt->do_index_ops = &osd_acct_index_ops;
1300 } else if (udmu_object_is_zap(obj->oo_db) &&
1301 dt->do_index_ops == NULL) {
1302 /* For index file, we don't support variable key & record sizes
1303 * and the key has to be unique */
1304 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1307 /* Although the zap_*_uint64() primitives support large keys, we
1308 * limit ourselves to 64-bit keys for now */
1309 if (feat->dif_keysize_max != sizeof(__u64) ||
1310 feat->dif_keysize_min != sizeof(__u64))
1313 /* As for the record size, it should be a multiple of 8 bytes
1314 * and smaller than the maximum value length supported by ZAP.
1316 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1318 if (feat->dif_recsize_max != feat->dif_recsize_min ||
1319 (feat->dif_recsize_max & (sizeof(__u64) - 1)))
1322 obj->oo_recsize = feat->dif_recsize_max / sizeof(__u64);
1323 dt->do_index_ops = &osd_index_ops;