4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/dt_object.c
35 * Generic functions from dt_object.h
37 * Author: Nikita Danilov <nikita@clusterfs.com>
40 #define DEBUG_SUBSYSTEM S_CLASS
42 #include <linux/list.h>
43 #include <obd_class.h>
44 #include <dt_object.h>
46 #include <lustre_fid.h>
47 #include <lustre_nodemap.h>
48 #include <lustre_quota.h>
49 #include <lustre_lfsck.h>
51 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
52 LU_KEY_INIT(dt_global, struct dt_thread_info);
53 LU_KEY_FINI(dt_global, struct dt_thread_info);
55 struct lu_context_key dt_key = {
56 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD | LCT_LOCAL,
57 .lct_init = dt_global_key_init,
58 .lct_fini = dt_global_key_fini
62 * no lock is necessary to protect the list, because call-backs
63 * are added during system startup. Please refer to "struct dt_device".
65 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
67 list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
69 EXPORT_SYMBOL(dt_txn_callback_add);
71 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
73 list_del_init(&cb->dtc_linkage);
75 EXPORT_SYMBOL(dt_txn_callback_del);
77 int dt_txn_hook_start(const struct lu_env *env,
78 struct dt_device *dev, struct thandle *th)
81 struct dt_txn_callback *cb;
86 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
87 struct thandle *dtc_th = th;
89 if (cb->dtc_txn_start == NULL ||
90 !(cb->dtc_tag & env->le_ctx.lc_tags))
94 * Usually dt_txn_hook_start is called from bottom device,
95 * and if the thandle has th_top, then we need use top
96 * thandle for the callback in the top thandle layer
98 if (th->th_top != NULL)
101 rc = cb->dtc_txn_start(env, dtc_th, cb->dtc_cookie);
107 EXPORT_SYMBOL(dt_txn_hook_start);
109 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *th)
111 struct dt_device *dev = th->th_dev;
112 struct dt_txn_callback *cb;
118 if (OBD_FAIL_CHECK(OBD_FAIL_DT_TXN_STOP))
121 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
122 struct thandle *dtc_th = th;
124 if (cb->dtc_txn_stop == NULL ||
125 !(cb->dtc_tag & env->le_ctx.lc_tags))
129 * Usually dt_txn_hook_stop is called from bottom device,
130 * and if the thandle has th_top, then we need use top
131 * thandle for the callback in the top thandle layer
133 if (th->th_top != NULL)
136 rc = cb->dtc_txn_stop(env, dtc_th, cb->dtc_cookie);
142 EXPORT_SYMBOL(dt_txn_hook_stop);
144 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
146 INIT_LIST_HEAD(&dev->dd_txn_callbacks);
147 return lu_device_init(&dev->dd_lu_dev, t);
149 EXPORT_SYMBOL(dt_device_init);
151 void dt_device_fini(struct dt_device *dev)
153 lu_device_fini(&dev->dd_lu_dev);
155 EXPORT_SYMBOL(dt_device_fini);
157 int dt_object_init(struct dt_object *obj,
158 struct lu_object_header *h, struct lu_device *d)
161 return lu_object_init(&obj->do_lu, h, d);
163 EXPORT_SYMBOL(dt_object_init);
165 void dt_object_fini(struct dt_object *obj)
167 lu_object_fini(&obj->do_lu);
169 EXPORT_SYMBOL(dt_object_fini);
171 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
173 if (obj->do_index_ops == NULL)
174 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
175 return obj->do_index_ops != NULL;
177 EXPORT_SYMBOL(dt_try_as_dir);
179 enum dt_format_type dt_mode_to_dft(__u32 mode)
181 enum dt_format_type result;
183 switch (mode & S_IFMT) {
188 result = DFT_REGULAR;
200 LASSERTF(0, "invalid mode %o\n", mode);
201 result = 0; /* Just for satisfying compiler. */
206 EXPORT_SYMBOL(dt_mode_to_dft);
209 * lookup fid for object named \a name in directory \a dir.
212 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
213 const char *name, struct lu_fid *fid)
215 if (dt_try_as_dir(env, dir))
216 return dt_lookup(env, dir, (struct dt_rec *)fid,
217 (const struct dt_key *)name);
220 EXPORT_SYMBOL(dt_lookup_dir);
223 * this differs from dt_locate by top_dev as parameter
224 * but not one from lu_site
226 struct dt_object *dt_locate_at(const struct lu_env *env,
227 struct dt_device *dev,
228 const struct lu_fid *fid,
229 struct lu_device *top_dev,
230 const struct lu_object_conf *conf)
232 struct lu_object *lo;
235 lo = lu_object_find_at(env, top_dev, fid, conf);
237 return ERR_PTR(PTR_ERR(lo));
241 list_for_each_entry(n, &lo->lo_header->loh_layers, lo_linkage) {
242 if (n->lo_dev == &dev->dd_lu_dev)
243 return container_of0(n, struct dt_object, do_lu);
246 lu_object_put(env, lo);
247 return ERR_PTR(-ENOENT);
249 EXPORT_SYMBOL(dt_locate_at);
252 * find an object named \a entry in given \a dfh->dfh_o directory.
254 static int dt_find_entry(const struct lu_env *env, const char *entry,
257 struct dt_find_hint *dfh = data;
258 struct dt_device *dt = dfh->dfh_dt;
259 struct lu_fid *fid = dfh->dfh_fid;
260 struct dt_object *obj = dfh->dfh_o;
263 rc = dt_lookup_dir(env, obj, entry, fid);
264 dt_object_put(env, obj);
266 obj = dt_locate(env, dt, fid);
276 * Abstract function which parses path name. This function feeds
277 * path component to \a entry_func.
279 int dt_path_parser(const struct lu_env *env,
280 char *path, dt_entry_func_t entry_func,
287 e = strsep(&path, "/");
292 if (!path || path[0] == '\0')
296 rc = entry_func(env, e, data);
305 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
306 const char *path, struct lu_fid *fid)
308 struct dt_thread_info *info = dt_info(env);
309 struct dt_find_hint *dfh = &info->dti_dfh;
310 struct dt_object *obj;
317 strlcpy(info->dti_buf, path, sizeof(info->dti_buf));
319 result = dt->dd_ops->dt_root_get(env, dt, fid);
321 obj = dt_locate(env, dt, fid);
324 result = dt_path_parser(env, info->dti_buf,
327 obj = ERR_PTR(result);
332 obj = ERR_PTR(result);
337 static struct dt_object *dt_reg_open(const struct lu_env *env,
338 struct dt_device *dt,
346 result = dt_lookup_dir(env, p, name, fid);
348 o = dt_locate(env, dt, fid);
356 * Open dt object named \a filename from \a dirname directory.
357 * \param dt dt device
358 * \param fid on success, object fid is stored in *fid
360 struct dt_object *dt_store_open(const struct lu_env *env, struct dt_device *dt,
361 const char *dirname, const char *filename,
364 struct dt_object *file;
365 struct dt_object *dir;
367 dir = dt_store_resolve(env, dt, dirname, fid);
369 file = dt_reg_open(env, dt, dir, filename, fid);
370 dt_object_put(env, dir);
378 struct dt_object *dt_find_or_create(const struct lu_env *env,
379 struct dt_device *dt,
380 const struct lu_fid *fid,
381 struct dt_object_format *dof,
384 struct dt_object *dto;
390 dto = dt_locate(env, dt, fid);
394 LASSERT(dto != NULL);
395 if (dt_object_exists(dto))
398 th = dt_trans_create(env, dt);
400 GOTO(out, rc = PTR_ERR(th));
402 rc = dt_declare_create(env, dto, at, NULL, dof, th);
404 GOTO(trans_stop, rc);
406 rc = dt_trans_start_local(env, dt, th);
408 GOTO(trans_stop, rc);
410 dt_write_lock(env, dto, 0);
411 if (dt_object_exists(dto))
412 GOTO(unlock, rc = 0);
414 CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(fid));
416 rc = dt_create(env, dto, at, NULL, dof, th);
419 LASSERT(dt_object_exists(dto));
421 dt_write_unlock(env, dto);
423 dt_trans_stop(env, dt, th);
426 dt_object_put(env, dto);
432 EXPORT_SYMBOL(dt_find_or_create);
434 /* dt class init function. */
435 int dt_global_init(void)
439 LU_CONTEXT_KEY_INIT(&dt_key);
440 result = lu_context_key_register(&dt_key);
444 void dt_global_fini(void)
446 lu_context_key_degister(&dt_key);
450 * Generic read helper. May return an error for partial reads.
452 * \param env lustre environment
453 * \param dt object to be read
454 * \param buf lu_buf to be filled, with buffer pointer and length
455 * \param pos position to start reading, updated as data is read
457 * \retval real size of data read
458 * \retval -ve errno on failure
460 int dt_read(const struct lu_env *env, struct dt_object *dt,
461 struct lu_buf *buf, loff_t *pos)
463 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
464 return dt->do_body_ops->dbo_read(env, dt, buf, pos);
466 EXPORT_SYMBOL(dt_read);
469 * Read structures of fixed size from storage. Unlike dt_read(), using
470 * dt_record_read() will return an error for partial reads.
472 * \param env lustre environment
473 * \param dt object to be read
474 * \param buf lu_buf to be filled, with buffer pointer and length
475 * \param pos position to start reading, updated as data is read
477 * \retval 0 on successfully reading full buffer
478 * \retval -EFAULT on short read
479 * \retval -ve errno on failure
481 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
482 struct lu_buf *buf, loff_t *pos)
486 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
488 size = dt->do_body_ops->dbo_read(env, dt, buf, pos);
491 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
493 EXPORT_SYMBOL(dt_record_read);
495 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
496 const struct lu_buf *buf, loff_t *pos, struct thandle *th)
500 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
502 LASSERT(dt->do_body_ops);
503 LASSERT(dt->do_body_ops->dbo_write);
505 size = dt->do_body_ops->dbo_write(env, dt, buf, pos, th);
508 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
510 EXPORT_SYMBOL(dt_record_write);
512 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
516 char *xname = XATTR_NAME_VERSION;
520 vbuf.lb_len = sizeof(dt_obj_version_t);
521 return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
524 EXPORT_SYMBOL(dt_declare_version_set);
526 void dt_version_set(const struct lu_env *env, struct dt_object *o,
527 dt_obj_version_t version, struct thandle *th)
530 char *xname = XATTR_NAME_VERSION;
534 vbuf.lb_buf = &version;
535 vbuf.lb_len = sizeof(version);
537 rc = dt_xattr_set(env, o, &vbuf, xname, 0, th);
539 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
541 EXPORT_SYMBOL(dt_version_set);
543 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
546 char *xname = XATTR_NAME_VERSION;
547 dt_obj_version_t version;
551 vbuf.lb_buf = &version;
552 vbuf.lb_len = sizeof(version);
553 rc = dt_xattr_get(env, o, &vbuf, xname);
554 if (rc != sizeof(version)) {
555 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
560 EXPORT_SYMBOL(dt_version_get);
562 /* list of all supported index types */
565 const struct dt_index_features dt_directory_features;
566 EXPORT_SYMBOL(dt_directory_features);
569 const struct dt_index_features dt_otable_features;
570 EXPORT_SYMBOL(dt_otable_features);
572 /* lfsck layout orphan */
573 const struct dt_index_features dt_lfsck_layout_orphan_features = {
575 .dif_keysize_min = sizeof(struct lu_fid),
576 .dif_keysize_max = sizeof(struct lu_fid),
577 .dif_recsize_min = sizeof(struct lu_orphan_rec_v3),
578 .dif_recsize_max = sizeof(struct lu_orphan_rec_v3),
581 EXPORT_SYMBOL(dt_lfsck_layout_orphan_features);
583 /* lfsck layout dangling */
584 const struct dt_index_features dt_lfsck_layout_dangling_features = {
585 .dif_flags = DT_IND_UPDATE,
586 .dif_keysize_min = sizeof(struct lfsck_layout_dangling_key),
587 .dif_keysize_max = sizeof(struct lfsck_layout_dangling_key),
588 .dif_recsize_min = sizeof(struct lu_fid),
589 .dif_recsize_max = sizeof(struct lu_fid),
592 EXPORT_SYMBOL(dt_lfsck_layout_dangling_features);
594 /* lfsck namespace */
595 const struct dt_index_features dt_lfsck_namespace_features = {
596 .dif_flags = DT_IND_UPDATE,
597 .dif_keysize_min = sizeof(struct lu_fid),
598 .dif_keysize_max = sizeof(struct lu_fid),
599 .dif_recsize_min = sizeof(__u8),
600 .dif_recsize_max = sizeof(__u8),
603 EXPORT_SYMBOL(dt_lfsck_namespace_features);
605 /* accounting indexes */
606 const struct dt_index_features dt_acct_features = {
607 .dif_flags = DT_IND_UPDATE,
608 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
609 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
610 .dif_recsize_min = sizeof(struct lquota_acct_rec), /* 16 bytes */
611 .dif_recsize_max = sizeof(struct lquota_acct_rec), /* 16 bytes */
614 EXPORT_SYMBOL(dt_acct_features);
616 /* global quota files */
617 const struct dt_index_features dt_quota_glb_features = {
618 .dif_flags = DT_IND_UPDATE,
619 /* a different key would have to be used for per-directory quota */
620 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
621 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
622 .dif_recsize_min = sizeof(struct lquota_glb_rec), /* 32 bytes */
623 .dif_recsize_max = sizeof(struct lquota_glb_rec), /* 32 bytes */
626 EXPORT_SYMBOL(dt_quota_glb_features);
628 /* slave quota files */
629 const struct dt_index_features dt_quota_slv_features = {
630 .dif_flags = DT_IND_UPDATE,
631 /* a different key would have to be used for per-directory quota */
632 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
633 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
634 .dif_recsize_min = sizeof(struct lquota_slv_rec), /* 8 bytes */
635 .dif_recsize_max = sizeof(struct lquota_slv_rec), /* 8 bytes */
638 EXPORT_SYMBOL(dt_quota_slv_features);
640 /* nodemap files, nodemap_rec size asserted in nodemap_storage.c */
641 const struct dt_index_features dt_nodemap_features = {
642 .dif_flags = DT_IND_UPDATE,
643 .dif_keysize_min = sizeof(__u64), /* 64-bit nodemap/record id */
644 .dif_keysize_max = sizeof(__u64), /* 64-bit nodemap/record id */
645 .dif_recsize_min = sizeof(union nodemap_rec), /* 32 bytes */
646 .dif_recsize_max = sizeof(union nodemap_rec), /* 32 bytes */
649 EXPORT_SYMBOL(dt_nodemap_features);
652 * helper function returning what dt_index_features structure should be used
653 * based on the FID sequence. This is used by OBD_IDX_READ RPC
655 static inline const struct dt_index_features *dt_index_feat_select(__u64 seq,
658 if (seq == FID_SEQ_QUOTA_GLB) {
659 /* global quota index */
661 /* global quota index should be a regular file */
662 return ERR_PTR(-ENOENT);
663 return &dt_quota_glb_features;
664 } else if (seq == FID_SEQ_QUOTA) {
665 /* quota slave index */
667 /* slave index should be a regular file */
668 return ERR_PTR(-ENOENT);
669 return &dt_quota_slv_features;
670 } else if (seq == FID_SEQ_LAYOUT_RBTREE){
671 return &dt_lfsck_layout_orphan_features;
672 } else if (seq >= FID_SEQ_NORMAL) {
673 /* object is part of the namespace, verify that it is a
676 /* sorry, we can only deal with directory */
677 return ERR_PTR(-ENOTDIR);
678 return &dt_directory_features;
681 return ERR_PTR(-EOPNOTSUPP);
685 * Fill a lu_idxpage with key/record pairs read for transfer via OBD_IDX_READ
688 * \param env - is the environment passed by the caller
689 * \param lp - is a pointer to the lu_page to fill
690 * \param nob - is the maximum number of bytes that should be copied
691 * \param iops - is the index operation vector associated with the index object
692 * \param it - is a pointer to the current iterator
693 * \param attr - is the index attribute to pass to iops->rec()
694 * \param arg - is a pointer to the idx_info structure
696 static int dt_index_page_build(const struct lu_env *env, union lu_page *lp,
697 size_t nob, const struct dt_it_ops *iops,
698 struct dt_it *it, __u32 attr, void *arg)
700 struct idx_info *ii = (struct idx_info *)arg;
701 struct lu_idxpage *lip = &lp->lp_idx;
711 if (nob < LIP_HDR_SIZE)
714 /* initialize the header of the new container */
715 memset(lip, 0, LIP_HDR_SIZE);
716 lip->lip_magic = LIP_MAGIC;
719 /* client wants to the 64-bit hash value associated with each record */
720 if (!(ii->ii_flags & II_FL_NOHASH))
721 hashsize = sizeof(hash);
723 entry = lip->lip_entries;
725 /* fetch 64-bit hash value */
726 hash = iops->store(env, it);
727 ii->ii_hash_end = hash;
729 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
730 if (lip->lip_nr != 0)
734 if (!(ii->ii_flags & II_FL_NOKEY)) {
735 keysize = iops->key_size(env, it);
736 if (!(ii->ii_flags & II_FL_VARKEY) &&
737 keysize != ii->ii_keysize) {
738 CERROR("keysize mismatch %hu != %hu.\n",
739 keysize, ii->ii_keysize);
740 GOTO(out, rc = -EINVAL);
744 /* and finally the record */
745 if (ii->ii_flags & II_FL_VARREC)
746 recsize = iops->rec_size(env, it, attr);
748 recsize = ii->ii_recsize;
750 if (nob < hashsize + keysize + recsize) {
751 if (lip->lip_nr == 0)
752 GOTO(out, rc = -E2BIG);
756 rc = iops->rec(env, it,
757 (struct dt_rec *)(entry + hashsize + keysize),
761 memcpy(entry, &hash, hashsize);
765 key = iops->key(env, it);
766 memcpy(entry + hashsize, key, keysize);
768 /* hash/key/record successfully copied! */
770 if (unlikely(lip->lip_nr == 1 && ii->ii_count == 0))
771 ii->ii_hash_start = hash;
772 entry += hashsize + keysize + recsize;
773 nob -= hashsize + keysize + recsize;
774 } else if (rc != -ESTALE) {
778 /* move on to the next record */
780 rc = iops->next(env, it);
781 } while (rc == -ESTALE);
786 if (rc >= 0 && lip->lip_nr > 0)
787 /* one more container */
790 /* no more entries */
791 ii->ii_hash_end = II_END_OFF;
797 * Walk index and fill lu_page containers with key/record pairs
799 * \param env - is the environment passed by the caller
800 * \param obj - is the index object to parse
801 * \param rdpg - is the lu_rdpg descriptor associated with the transfer
802 * \param filler - is the callback function responsible for filling a lu_page
803 * with key/record pairs in the format wanted by the caller.
804 * If NULL, uses dt_index_page_build
805 * \param arg - is an opaq argument passed to the filler function
807 * \retval sum (in bytes) of all filled lu_pages
808 * \retval -ve errno on failure
810 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
811 const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
815 const struct dt_it_ops *iops;
816 size_t pageidx, nob, nlupgs = 0;
820 LASSERT(rdpg->rp_pages != NULL);
821 LASSERT(obj->do_index_ops != NULL);
824 filler = dt_index_page_build;
826 nob = rdpg->rp_count;
830 /* Iterate through index and fill containers from @rdpg */
831 iops = &obj->do_index_ops->dio_it;
832 LASSERT(iops != NULL);
833 it = iops->init(env, obj, rdpg->rp_attrs);
837 rc = iops->load(env, it, rdpg->rp_hash);
840 * Iterator didn't find record with exactly the key requested.
842 * It is currently either
844 * - positioned above record with key less than
845 * requested---skip it.
846 * - or not positioned at all (is in IAM_IT_SKEWED
847 * state)---position it on the next item.
849 rc = iops->next(env, it);
859 * Fill containers one after the other. There might be multiple
860 * containers per physical page.
862 * At this point and across for-loop:
863 * rc == 0 -> ok, proceed.
864 * rc > 0 -> end of index.
867 for (pageidx = 0; rc == 0 && nob > 0; pageidx++) {
871 LASSERT(pageidx < rdpg->rp_npages);
872 lp = kmap(rdpg->rp_pages[pageidx]);
875 for (i = 0; i < LU_PAGE_COUNT; i++, lp++, nob -= LU_PAGE_SIZE) {
876 rc = filler(env, lp, min_t(size_t, nob, LU_PAGE_SIZE),
877 iops, it, rdpg->rp_attrs, arg);
880 /* one more lu_page */
886 kunmap(rdpg->rp_pages[i]);
894 rc = min_t(size_t, nlupgs * LU_PAGE_SIZE, rdpg->rp_count);
898 EXPORT_SYMBOL(dt_index_walk);
901 * Walk key/record pairs of an index and copy them into 4KB containers to be
902 * transferred over the network. This is the common handler for OBD_IDX_READ
905 * \param env - is the environment passed by the caller
906 * \param dev - is the dt_device storing the index
907 * \param ii - is the idx_info structure packed by the client in the
908 * OBD_IDX_READ request
909 * \param rdpg - is the lu_rdpg descriptor
911 * \retval on success, return sum (in bytes) of all filled containers
912 * \retval appropriate error otherwise.
914 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
915 struct idx_info *ii, const struct lu_rdpg *rdpg)
917 const struct dt_index_features *feat;
918 struct dt_object *obj;
923 * rp_count shouldn't be null and should be a multiple of the container
926 if (rdpg->rp_count == 0 || (rdpg->rp_count & (LU_PAGE_SIZE - 1)) != 0)
929 if (!fid_is_quota(&ii->ii_fid) && !fid_is_layout_rbtree(&ii->ii_fid) &&
930 !fid_is_norm(&ii->ii_fid))
933 /* lookup index object subject to the transfer */
934 obj = dt_locate(env, dev, &ii->ii_fid);
936 RETURN(PTR_ERR(obj));
937 if (dt_object_exists(obj) == 0)
938 GOTO(out, rc = -ENOENT);
940 /* fetch index features associated with index object */
941 feat = dt_index_feat_select(fid_seq(&ii->ii_fid),
942 lu_object_attr(&obj->do_lu));
944 GOTO(out, rc = PTR_ERR(feat));
946 /* load index feature if not done already */
947 if (obj->do_index_ops == NULL) {
948 rc = obj->do_ops->do_index_try(env, obj, feat);
953 /* fill ii_flags with supported index features */
954 ii->ii_flags &= (II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
957 if (!(feat->dif_flags & DT_IND_VARKEY))
958 ii->ii_keysize = feat->dif_keysize_max;
960 if (!(feat->dif_flags & DT_IND_VARREC))
961 ii->ii_recsize = feat->dif_recsize_max;
963 if (feat->dif_flags & DT_IND_NONUNQ)
964 /* key isn't necessarily unique */
965 ii->ii_flags |= II_FL_NONUNQ;
967 if (!fid_is_layout_rbtree(&ii->ii_fid)) {
968 dt_read_lock(env, obj, 0);
969 /* fetch object version before walking the index */
970 ii->ii_version = dt_version_get(env, obj);
973 /* walk the index and fill lu_idxpages with key/record pairs */
974 rc = dt_index_walk(env, obj, rdpg, dt_index_page_build, ii);
975 if (!fid_is_layout_rbtree(&ii->ii_fid))
976 dt_read_unlock(env, obj);
980 LASSERT(ii->ii_count == 0);
981 ii->ii_hash_end = II_END_OFF;
986 dt_object_put(env, obj);
989 EXPORT_SYMBOL(dt_index_read);
991 #ifdef CONFIG_PROC_FS
992 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v)
994 struct dt_device *dt = m->private;
995 struct obd_statfs osfs;
997 int rc = dt_statfs(NULL, dt, &osfs);
999 seq_printf(m, "%u\n", (unsigned) osfs.os_bsize);
1002 EXPORT_SYMBOL(lprocfs_dt_blksize_seq_show);
1004 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v)
1006 struct dt_device *dt = m->private;
1007 struct obd_statfs osfs;
1009 int rc = dt_statfs(NULL, dt, &osfs);
1011 __u32 blk_size = osfs.os_bsize >> 10;
1012 __u64 result = osfs.os_blocks;
1014 while (blk_size >>= 1)
1017 seq_printf(m, "%llu\n", result);
1021 EXPORT_SYMBOL(lprocfs_dt_kbytestotal_seq_show);
1023 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v)
1025 struct dt_device *dt = m->private;
1026 struct obd_statfs osfs;
1028 int rc = dt_statfs(NULL, dt, &osfs);
1030 __u32 blk_size = osfs.os_bsize >> 10;
1031 __u64 result = osfs.os_bfree;
1033 while (blk_size >>= 1)
1036 seq_printf(m, "%llu\n", result);
1040 EXPORT_SYMBOL(lprocfs_dt_kbytesfree_seq_show);
1042 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v)
1044 struct dt_device *dt = m->private;
1045 struct obd_statfs osfs;
1047 int rc = dt_statfs(NULL, dt, &osfs);
1049 __u32 blk_size = osfs.os_bsize >> 10;
1050 __u64 result = osfs.os_bavail;
1052 while (blk_size >>= 1)
1055 seq_printf(m, "%llu\n", result);
1059 EXPORT_SYMBOL(lprocfs_dt_kbytesavail_seq_show);
1061 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v)
1063 struct dt_device *dt = m->private;
1064 struct obd_statfs osfs;
1066 int rc = dt_statfs(NULL, dt, &osfs);
1068 seq_printf(m, "%llu\n", osfs.os_files);
1071 EXPORT_SYMBOL(lprocfs_dt_filestotal_seq_show);
1073 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v)
1075 struct dt_device *dt = m->private;
1076 struct obd_statfs osfs;
1078 int rc = dt_statfs(NULL, dt, &osfs);
1080 seq_printf(m, "%llu\n", osfs.os_ffree);
1083 EXPORT_SYMBOL(lprocfs_dt_filesfree_seq_show);
1085 #endif /* CONFIG_PROC_FS */
1087 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
1090 struct dt_device *dt = container_of(kobj, struct dt_device,
1092 struct lu_device *lu = dt2lu_dev(dt);
1097 return sprintf(buf, "%s\n", lu->ld_obd->obd_uuid.uuid);
1099 LUSTRE_RO_ATTR(uuid);
1101 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
1104 struct dt_device *dt = container_of(kobj, struct dt_device,
1106 struct obd_statfs osfs;
1109 rc = dt_statfs(NULL, dt, &osfs);
1113 return sprintf(buf, "%u\n", (unsigned) osfs.os_bsize);
1115 LUSTRE_RO_ATTR(blocksize);
1117 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
1120 struct dt_device *dt = container_of(kobj, struct dt_device,
1122 struct obd_statfs osfs;
1127 rc = dt_statfs(NULL, dt, &osfs);
1131 blk_size = osfs.os_bsize >> 10;
1132 result = osfs.os_blocks;
1134 while (blk_size >>= 1)
1137 return sprintf(buf, "%llu\n", result);
1139 LUSTRE_RO_ATTR(kbytestotal);
1141 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
1144 struct dt_device *dt = container_of(kobj, struct dt_device,
1146 struct obd_statfs osfs;
1151 rc = dt_statfs(NULL, dt, &osfs);
1155 blk_size = osfs.os_bsize >> 10;
1156 result = osfs.os_bfree;
1158 while (blk_size >>= 1)
1161 return sprintf(buf, "%llu\n", result);
1163 LUSTRE_RO_ATTR(kbytesfree);
1165 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
1168 struct dt_device *dt = container_of(kobj, struct dt_device,
1170 struct obd_statfs osfs;
1175 rc = dt_statfs(NULL, dt, &osfs);
1179 blk_size = osfs.os_bsize >> 10;
1180 result = osfs.os_bavail;
1182 while (blk_size >>= 1)
1185 return sprintf(buf, "%llu\n", result);
1187 LUSTRE_RO_ATTR(kbytesavail);
1189 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
1192 struct dt_device *dt = container_of(kobj, struct dt_device,
1194 struct obd_statfs osfs;
1197 rc = dt_statfs(NULL, dt, &osfs);
1201 return sprintf(buf, "%llu\n", osfs.os_files);
1203 LUSTRE_RO_ATTR(filestotal);
1205 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
1208 struct dt_device *dt = container_of(kobj, struct dt_device,
1210 struct obd_statfs osfs;
1213 rc = dt_statfs(NULL, dt, &osfs);
1217 return sprintf(buf, "%llu\n", osfs.os_ffree);
1219 LUSTRE_RO_ATTR(filesfree);
1221 static const struct attribute *dt_def_attrs[] = {
1222 &lustre_attr_uuid.attr,
1223 &lustre_attr_blocksize.attr,
1224 &lustre_attr_kbytestotal.attr,
1225 &lustre_attr_kbytesfree.attr,
1226 &lustre_attr_kbytesavail.attr,
1227 &lustre_attr_filestotal.attr,
1228 &lustre_attr_filesfree.attr,
1232 static void dt_sysfs_release(struct kobject *kobj)
1234 struct dt_device *dt = container_of(kobj, struct dt_device,
1237 debugfs_remove_recursive(dt->dd_debugfs_entry);
1238 dt->dd_debugfs_entry = NULL;
1240 complete(&dt->dd_kobj_unregister);
1243 int dt_tunables_fini(struct dt_device *dt)
1248 if (dt->dd_def_attrs)
1249 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1251 kobject_put(&dt->dd_kobj);
1252 wait_for_completion(&dt->dd_kobj_unregister);
1256 EXPORT_SYMBOL(dt_tunables_fini);
1258 int dt_tunables_init(struct dt_device *dt, struct obd_type *type,
1259 const char *name, struct lprocfs_vars *list)
1263 dt->dd_ktype.sysfs_ops = &lustre_sysfs_ops;
1264 dt->dd_ktype.release = dt_sysfs_release;
1266 init_completion(&dt->dd_kobj_unregister);
1267 rc = kobject_init_and_add(&dt->dd_kobj, &dt->dd_ktype, &type->typ_kobj,
1272 dt->dd_def_attrs = dt_def_attrs;
1274 rc = sysfs_create_files(&dt->dd_kobj, dt->dd_def_attrs);
1276 kobject_put(&dt->dd_kobj);
1281 * No need to register debugfs if no enteries. This allows us to
1282 * choose between using dt_device or obd_device for debugfs.
1287 dt->dd_debugfs_entry = ldebugfs_register(name,
1288 type->typ_debugfs_entry,
1290 if (IS_ERR_OR_NULL(dt->dd_debugfs_entry)) {
1291 rc = dt->dd_debugfs_entry ? PTR_ERR(dt->dd_debugfs_entry)
1293 CERROR("%s: error %d setting up debugfs\n",
1295 dt->dd_debugfs_entry = NULL;
1296 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1297 kobject_put(&dt->dd_kobj);
1303 EXPORT_SYMBOL(dt_tunables_init);