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/
31 * lustre/obdclass/dt_object.c
34 * Generic functions from dt_object.h
36 * Author: Nikita Danilov <nikita@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_CLASS
41 #include <linux/list.h>
42 #include <obd_class.h>
43 #include <dt_object.h>
45 #include <lustre_fid.h>
46 #include <lustre_nodemap.h>
47 #include <lustre_quota.h>
48 #include <lustre_lfsck.h>
49 #include <uapi/linux/lustre/lustre_disk.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);
172 * Set directory .do_index_ops.
174 * Set directory index operations, if the caller knows directory exists,
175 * \a check should be set to ensure object is directory and exists, while for
176 * new directories, skip check and the index operations will be used to create
177 * ".." under directory.
179 * Normally this is called before dt_lookup() to ensure directory objects
180 * exists and .do_index_ops is correctly set.
182 * \param env lu_env object.
183 * \param obj dt object.
184 * \param check check \a obj existence and type, return if index ops is set.
185 * \retval 1 on success.
186 * \retval 0 on error.
188 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj, bool check)
191 if (unlikely(!dt_object_exists(obj)))
194 if (unlikely(!S_ISDIR(lu_object_attr(&obj->do_lu))))
197 if (obj->do_index_ops)
201 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
203 return obj->do_index_ops != NULL;
205 EXPORT_SYMBOL(dt_try_as_dir);
207 enum dt_format_type dt_mode_to_dft(__u32 mode)
209 enum dt_format_type result;
211 switch (mode & S_IFMT) {
216 result = DFT_REGULAR;
228 LASSERTF(0, "invalid mode %o\n", mode);
229 result = 0; /* Just for satisfying compiler. */
234 EXPORT_SYMBOL(dt_mode_to_dft);
237 * lookup fid for object named \a name in directory \a dir.
240 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
241 const char *name, struct lu_fid *fid)
243 if (dt_try_as_dir(env, dir, true))
244 return dt_lookup(env, dir, (struct dt_rec *)fid,
245 (const struct dt_key *)name);
248 EXPORT_SYMBOL(dt_lookup_dir);
251 * this differs from dt_locate by top_dev as parameter
252 * but not one from lu_site
254 struct dt_object *dt_locate_at(const struct lu_env *env,
255 struct dt_device *dev,
256 const struct lu_fid *fid,
257 struct lu_device *top_dev,
258 const struct lu_object_conf *conf)
260 struct lu_object *lo;
263 lo = lu_object_find_at(env, top_dev, fid, conf);
269 list_for_each_entry(n, &lo->lo_header->loh_layers, lo_linkage) {
270 if (n->lo_dev == &dev->dd_lu_dev)
271 return container_of(n, struct dt_object, do_lu);
274 lu_object_put(env, lo);
275 return ERR_PTR(-ENOENT);
277 EXPORT_SYMBOL(dt_locate_at);
280 * find an object named \a entry in given \a dfh->dfh_o directory.
282 static int dt_find_entry(const struct lu_env *env, const char *entry,
285 struct dt_find_hint *dfh = data;
286 struct dt_device *dt = dfh->dfh_dt;
287 struct lu_fid *fid = dfh->dfh_fid;
288 struct dt_object *obj = dfh->dfh_o;
291 rc = dt_lookup_dir(env, obj, entry, fid);
292 dt_object_put(env, obj);
294 obj = dt_locate(env, dt, fid);
304 * Abstract function which parses path name. This function feeds
305 * path component to \a entry_func.
307 int dt_path_parser(const struct lu_env *env,
308 char *path, dt_entry_func_t entry_func,
315 e = strsep(&path, "/");
320 if (!path || path[0] == '\0')
324 rc = entry_func(env, e, data);
333 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
334 const char *path, struct lu_fid *fid)
336 struct dt_thread_info *info = dt_info(env);
337 struct dt_find_hint *dfh = &info->dti_dfh;
338 struct dt_object *obj;
345 strlcpy(info->dti_buf, path, sizeof(info->dti_buf));
347 result = dt->dd_ops->dt_root_get(env, dt, fid);
349 obj = dt_locate(env, dt, fid);
352 result = dt_path_parser(env, info->dti_buf,
355 obj = ERR_PTR(result);
360 obj = ERR_PTR(result);
365 static struct dt_object *dt_reg_open(const struct lu_env *env,
366 struct dt_device *dt,
374 result = dt_lookup_dir(env, p, name, fid);
376 o = dt_locate(env, dt, fid);
384 * Open dt object named \a filename from \a dirname directory.
385 * \param dt dt device
386 * \param fid on success, object fid is stored in *fid
388 struct dt_object *dt_store_open(const struct lu_env *env, struct dt_device *dt,
389 const char *dirname, const char *filename,
392 struct dt_object *file;
393 struct dt_object *dir;
395 dir = dt_store_resolve(env, dt, dirname, fid);
397 file = dt_reg_open(env, dt, dir, filename, fid);
398 dt_object_put(env, dir);
406 struct dt_object *dt_find_or_create(const struct lu_env *env,
407 struct dt_device *dt,
408 const struct lu_fid *fid,
409 struct dt_object_format *dof,
412 struct dt_object *dto;
418 dto = dt_locate(env, dt, fid);
422 LASSERT(dto != NULL);
423 if (dt_object_exists(dto))
426 th = dt_trans_create(env, dt);
428 GOTO(out, rc = PTR_ERR(th));
430 rc = dt_declare_create(env, dto, at, NULL, dof, th);
432 GOTO(trans_stop, rc);
434 rc = dt_trans_start_local(env, dt, th);
436 GOTO(trans_stop, rc);
438 dt_write_lock(env, dto, 0);
439 if (dt_object_exists(dto))
440 GOTO(unlock, rc = 0);
442 CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(fid));
444 rc = dt_create(env, dto, at, NULL, dof, th);
447 LASSERT(dt_object_exists(dto));
449 dt_write_unlock(env, dto);
451 dt_trans_stop(env, dt, th);
454 dt_object_put(env, dto);
460 EXPORT_SYMBOL(dt_find_or_create);
462 /* dt class init function. */
463 int dt_global_init(void)
467 LU_CONTEXT_KEY_INIT(&dt_key);
468 result = lu_context_key_register(&dt_key);
472 void dt_global_fini(void)
474 lu_context_key_degister(&dt_key);
478 * Generic read helper. May return an error for partial reads.
480 * \param env lustre environment
481 * \param dt object to be read
482 * \param buf lu_buf to be filled, with buffer pointer and length
483 * \param pos position to start reading, updated as data is read
485 * \retval real size of data read
486 * \retval -ve errno on failure
488 int dt_read(const struct lu_env *env, struct dt_object *dt,
489 struct lu_buf *buf, loff_t *pos)
491 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
492 return dt->do_body_ops->dbo_read(env, dt, buf, pos);
494 EXPORT_SYMBOL(dt_read);
497 * Read structures of fixed size from storage. Unlike dt_read(), using
498 * dt_record_read() will return an error for partial reads.
500 * \param env lustre environment
501 * \param dt object to be read
502 * \param buf lu_buf to be filled, with buffer pointer and length
503 * \param pos position to start reading, updated as data is read
505 * \retval 0 on successfully reading full buffer
506 * \retval -EFAULT on short read
507 * \retval -ve errno on failure
509 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
510 struct lu_buf *buf, loff_t *pos)
514 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
516 size = dt->do_body_ops->dbo_read(env, dt, buf, pos);
519 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
521 EXPORT_SYMBOL(dt_record_read);
523 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
524 const struct lu_buf *buf, loff_t *pos, struct thandle *th)
528 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
530 LASSERT(dt->do_body_ops);
531 LASSERTF(dt->do_body_ops->dbo_write, DFID"\n",
532 PFID(lu_object_fid(&dt->do_lu)));
534 size = dt->do_body_ops->dbo_write(env, dt, buf, pos, th);
537 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
539 EXPORT_SYMBOL(dt_record_write);
541 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
545 char *xname = XATTR_NAME_VERSION;
549 vbuf.lb_len = sizeof(dt_obj_version_t);
550 return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
553 EXPORT_SYMBOL(dt_declare_version_set);
555 void dt_version_set(const struct lu_env *env, struct dt_object *o,
556 dt_obj_version_t version, struct thandle *th)
559 char *xname = XATTR_NAME_VERSION;
563 vbuf.lb_buf = &version;
564 vbuf.lb_len = sizeof(version);
566 rc = dt_xattr_set(env, o, &vbuf, xname, 0, th);
568 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
570 EXPORT_SYMBOL(dt_version_set);
572 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
575 char *xname = XATTR_NAME_VERSION;
576 dt_obj_version_t version;
580 vbuf.lb_buf = &version;
581 vbuf.lb_len = sizeof(version);
582 rc = dt_xattr_get(env, o, &vbuf, xname);
583 if (rc != sizeof(version)) {
584 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
589 EXPORT_SYMBOL(dt_version_get);
591 /* list of all supported index types */
594 const struct dt_index_features dt_directory_features;
595 EXPORT_SYMBOL(dt_directory_features);
598 const struct dt_index_features dt_otable_features;
599 EXPORT_SYMBOL(dt_otable_features);
601 /* lfsck layout orphan */
602 const struct dt_index_features dt_lfsck_layout_orphan_features = {
604 .dif_keysize_min = sizeof(struct lu_fid),
605 .dif_keysize_max = sizeof(struct lu_fid),
606 .dif_recsize_min = sizeof(struct lu_orphan_rec_v3),
607 .dif_recsize_max = sizeof(struct lu_orphan_rec_v3),
610 EXPORT_SYMBOL(dt_lfsck_layout_orphan_features);
612 /* lfsck layout dangling */
613 const struct dt_index_features dt_lfsck_layout_dangling_features = {
614 .dif_flags = DT_IND_UPDATE,
615 .dif_keysize_min = sizeof(struct lfsck_layout_dangling_key),
616 .dif_keysize_max = sizeof(struct lfsck_layout_dangling_key),
617 .dif_recsize_min = sizeof(struct lu_fid),
618 .dif_recsize_max = sizeof(struct lu_fid),
621 EXPORT_SYMBOL(dt_lfsck_layout_dangling_features);
623 /* lfsck namespace */
624 const struct dt_index_features dt_lfsck_namespace_features = {
625 .dif_flags = DT_IND_UPDATE,
626 .dif_keysize_min = sizeof(struct lu_fid),
627 .dif_keysize_max = sizeof(struct lu_fid),
628 .dif_recsize_min = sizeof(__u8),
629 .dif_recsize_max = sizeof(__u8),
632 EXPORT_SYMBOL(dt_lfsck_namespace_features);
634 /* accounting indexes */
635 const struct dt_index_features dt_acct_features = {
636 .dif_flags = DT_IND_UPDATE,
637 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
638 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
639 .dif_recsize_min = sizeof(struct lquota_acct_rec), /* 16 bytes */
640 .dif_recsize_max = sizeof(struct lquota_acct_rec), /* 16 bytes */
643 EXPORT_SYMBOL(dt_acct_features);
645 /* global quota files */
646 const struct dt_index_features dt_quota_glb_features = {
647 .dif_flags = DT_IND_UPDATE,
648 /* a different key would have to be used for per-directory quota */
649 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
650 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
651 .dif_recsize_min = sizeof(struct lquota_glb_rec), /* 32 bytes */
652 .dif_recsize_max = sizeof(struct lquota_glb_rec), /* 32 bytes */
655 EXPORT_SYMBOL(dt_quota_glb_features);
657 /* slave quota files */
658 const struct dt_index_features dt_quota_slv_features = {
659 .dif_flags = DT_IND_UPDATE,
660 /* a different key would have to be used for per-directory quota */
661 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
662 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
663 .dif_recsize_min = sizeof(struct lquota_slv_rec), /* 8 bytes */
664 .dif_recsize_max = sizeof(struct lquota_slv_rec), /* 8 bytes */
667 EXPORT_SYMBOL(dt_quota_slv_features);
669 /* nodemap files, nodemap_rec size asserted in nodemap_storage.c */
670 const struct dt_index_features dt_nodemap_features = {
671 .dif_flags = DT_IND_UPDATE,
672 .dif_keysize_min = sizeof(__u64), /* 64-bit nodemap/record id */
673 .dif_keysize_max = sizeof(__u64), /* 64-bit nodemap/record id */
674 .dif_recsize_min = sizeof(union nodemap_rec), /* 32 bytes */
675 .dif_recsize_max = sizeof(union nodemap_rec), /* 32 bytes */
678 EXPORT_SYMBOL(dt_nodemap_features);
681 * helper function returning what dt_index_features structure should be used
682 * based on the FID sequence. This is used by OBD_IDX_READ RPC
684 static inline const struct dt_index_features *dt_index_feat_select(__u64 seq,
687 if (seq == FID_SEQ_QUOTA_GLB) {
688 /* global quota index */
690 /* global quota index should be a regular file */
691 return ERR_PTR(-ENOENT);
692 return &dt_quota_glb_features;
693 } else if (seq == FID_SEQ_QUOTA) {
694 /* quota slave index */
696 /* slave index should be a regular file */
697 return ERR_PTR(-ENOENT);
698 return &dt_quota_slv_features;
699 } else if (seq == FID_SEQ_LAYOUT_RBTREE){
700 return &dt_lfsck_layout_orphan_features;
701 } else if (seq >= FID_SEQ_NORMAL) {
702 /* object is part of the namespace, verify that it is a
705 /* sorry, we can only deal with directory */
706 return ERR_PTR(-ENOTDIR);
707 return &dt_directory_features;
710 return ERR_PTR(-EOPNOTSUPP);
714 * Fill a lu_idxpage with key/record pairs read for transfer via OBD_IDX_READ
717 * \param env - is the environment passed by the caller
718 * \param obj - index object being traversed (mostly for debugging)
719 * \param lp - is a pointer to the lu_page to fill
720 * \param bytes - is the maximum number of bytes that should be copied
721 * \param iops - is the index operation vector associated with the index object
722 * \param it - is a pointer to the current iterator
723 * \param attr - is the index attribute to pass to iops->rec()
724 * \param arg - is a pointer to the idx_info structure
726 static int dt_index_page_build(const struct lu_env *env, struct dt_object *obj,
727 union lu_page *lp, size_t bytes,
728 const struct dt_it_ops *iops,
729 struct dt_it *it, __u32 attr, void *arg)
731 struct idx_info *ii = (struct idx_info *)arg;
732 struct lu_idxpage *lip = &lp->lp_idx;
742 if (bytes < LIP_HDR_SIZE)
745 /* initialize the header of the new container */
746 memset(lip, 0, LIP_HDR_SIZE);
747 lip->lip_magic = LIP_MAGIC;
748 bytes -= LIP_HDR_SIZE;
750 /* client wants to the 64-bit hash value associated with each record */
751 if (!(ii->ii_flags & II_FL_NOHASH))
752 hashsize = sizeof(hash);
754 entry = lip->lip_entries;
756 /* fetch 64-bit hash value */
757 hash = iops->store(env, it);
758 ii->ii_hash_end = hash;
760 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
761 if (lip->lip_nr != 0)
765 if (!(ii->ii_flags & II_FL_NOKEY)) {
766 keysize = iops->key_size(env, it);
767 if (!(ii->ii_flags & II_FL_VARKEY) &&
768 keysize != ii->ii_keysize) {
770 CERROR("%s: keysize mismatch %hu != %hu on "
772 lu_dev_name(obj->do_lu.lo_dev),
773 keysize, ii->ii_keysize,
774 PFID(lu_object_fid(&obj->do_lu)), rc);
779 /* and finally the record */
780 if (ii->ii_flags & II_FL_VARREC)
781 recsize = iops->rec_size(env, it, attr);
783 recsize = ii->ii_recsize;
785 if (bytes < hashsize + keysize + recsize) {
786 if (lip->lip_nr == 0)
787 GOTO(out, rc = -E2BIG);
791 rc = iops->rec(env, it,
792 (struct dt_rec *)(entry + hashsize + keysize),
796 memcpy(entry, &hash, hashsize);
800 key = iops->key(env, it);
801 memcpy(entry + hashsize, key, keysize);
803 /* hash/key/record successfully copied! */
805 if (unlikely(lip->lip_nr == 1 && ii->ii_count == 0))
806 ii->ii_hash_start = hash;
807 entry += hashsize + keysize + recsize;
808 bytes -= hashsize + keysize + recsize;
809 } else if (rc != -ESTALE) {
813 /* move on to the next record */
815 rc = iops->next(env, it);
816 } while (rc == -ESTALE);
821 if (rc >= 0 && lip->lip_nr > 0)
822 /* one more container */
825 /* no more entries */
826 ii->ii_hash_end = II_END_OFF;
832 * Walk index and fill lu_page containers with key/record pairs
834 * \param env - is the environment passed by the caller
835 * \param obj - is the index object to parse
836 * \param rdpg - is the lu_rdpg descriptor associated with the transfer
837 * \param filler - is the callback function responsible for filling a lu_page
838 * with key/record pairs in the format wanted by the caller.
839 * If NULL, uses dt_index_page_build
840 * \param arg - is an opaq argument passed to the filler function
842 * \retval sum (in bytes) of all filled lu_pages
843 * \retval -ve errno on failure
845 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
846 const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
850 const struct dt_it_ops *iops;
851 size_t pageidx, bytes, nlupgs = 0;
855 LASSERT(rdpg->rp_pages != NULL);
856 LASSERT(obj->do_index_ops != NULL);
859 filler = dt_index_page_build;
861 bytes = rdpg->rp_count;
865 /* Iterate through index and fill containers from @rdpg */
866 iops = &obj->do_index_ops->dio_it;
867 LASSERT(iops != NULL);
868 it = iops->init(env, obj, rdpg->rp_attrs);
872 rc = iops->load(env, it, rdpg->rp_hash);
875 * Iterator didn't find record with exactly the key requested.
877 * It is currently either
879 * - positioned above record with key less than
880 * requested---skip it.
881 * - or not positioned at all (is in IAM_IT_SKEWED
882 * state)---position it on the next item.
884 rc = iops->next(env, it);
894 * Fill containers one after the other. There might be multiple
895 * containers per physical page.
897 * At this point and across for-loop:
898 * rc == 0 -> ok, proceed.
899 * rc > 0 -> end of index.
902 for (pageidx = 0; rc == 0 && bytes > 0; pageidx++) {
906 LASSERT(pageidx < rdpg->rp_npages);
907 lp = kmap(rdpg->rp_pages[pageidx]);
910 for (i = 0; i < LU_PAGE_COUNT; i++, lp++, bytes-=LU_PAGE_SIZE) {
911 rc = filler(env, obj, lp,
912 min_t(size_t, bytes, LU_PAGE_SIZE),
913 iops, it, rdpg->rp_attrs, arg);
916 /* one more lu_page */
922 kunmap(rdpg->rp_pages[i]);
930 rc = min_t(size_t, nlupgs * LU_PAGE_SIZE, rdpg->rp_count);
934 EXPORT_SYMBOL(dt_index_walk);
937 * Walk key/record pairs of an index and copy them into 4KB containers to be
938 * transferred over the network. This is the common handler for OBD_IDX_READ
941 * \param env - is the environment passed by the caller
942 * \param dev - is the dt_device storing the index
943 * \param ii - is the idx_info structure packed by the client in the
944 * OBD_IDX_READ request
945 * \param rdpg - is the lu_rdpg descriptor
947 * \retval on success, return sum (in bytes) of all filled containers
948 * \retval appropriate error otherwise.
950 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
951 struct idx_info *ii, const struct lu_rdpg *rdpg)
953 const struct dt_index_features *feat;
954 struct dt_object *obj;
959 * rp_count shouldn't be null and should be a multiple of the container
962 if (rdpg->rp_count == 0 || (rdpg->rp_count & (LU_PAGE_SIZE - 1)) != 0)
965 if (!fid_is_quota(&ii->ii_fid) && !fid_is_layout_rbtree(&ii->ii_fid) &&
966 !fid_is_norm(&ii->ii_fid))
969 /* lookup index object subject to the transfer */
970 obj = dt_locate(env, dev, &ii->ii_fid);
972 RETURN(PTR_ERR(obj));
973 if (dt_object_exists(obj) == 0)
974 GOTO(out, rc = -ENOENT);
976 /* fetch index features associated with index object */
977 feat = dt_index_feat_select(fid_seq(&ii->ii_fid),
978 lu_object_attr(&obj->do_lu));
980 GOTO(out, rc = PTR_ERR(feat));
982 /* load index feature if not done already */
983 if (obj->do_index_ops == NULL) {
984 rc = obj->do_ops->do_index_try(env, obj, feat);
989 /* fill ii_flags with supported index features */
990 ii->ii_flags &= (II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
993 if (!(feat->dif_flags & DT_IND_VARKEY))
994 ii->ii_keysize = feat->dif_keysize_max;
996 if (!(feat->dif_flags & DT_IND_VARREC))
997 ii->ii_recsize = feat->dif_recsize_max;
999 if (feat->dif_flags & DT_IND_NONUNQ)
1000 /* key isn't necessarily unique */
1001 ii->ii_flags |= II_FL_NONUNQ;
1003 if (!fid_is_layout_rbtree(&ii->ii_fid)) {
1004 dt_read_lock(env, obj, 0);
1005 /* fetch object version before walking the index */
1006 ii->ii_version = dt_version_get(env, obj);
1009 /* walk the index and fill lu_idxpages with key/record pairs */
1010 rc = dt_index_walk(env, obj, rdpg, dt_index_page_build, ii);
1011 if (!fid_is_layout_rbtree(&ii->ii_fid))
1012 dt_read_unlock(env, obj);
1015 /* index is empty */
1016 LASSERT(ii->ii_count == 0);
1017 ii->ii_hash_end = II_END_OFF;
1022 dt_object_put(env, obj);
1025 EXPORT_SYMBOL(dt_index_read);
1027 #ifdef CONFIG_PROC_FS
1028 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v)
1030 struct dt_device *dt = m->private;
1031 struct obd_statfs osfs;
1033 int rc = dt_statfs(NULL, dt, &osfs);
1035 seq_printf(m, "%u\n", (unsigned) osfs.os_bsize);
1038 EXPORT_SYMBOL(lprocfs_dt_blksize_seq_show);
1040 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v)
1042 struct dt_device *dt = m->private;
1043 struct obd_statfs osfs;
1045 int rc = dt_statfs(NULL, dt, &osfs);
1047 __u32 blk_size = osfs.os_bsize >> 10;
1048 __u64 result = osfs.os_blocks;
1050 while (blk_size >>= 1)
1053 seq_printf(m, "%llu\n", result);
1057 EXPORT_SYMBOL(lprocfs_dt_kbytestotal_seq_show);
1059 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v)
1061 struct dt_device *dt = m->private;
1062 struct obd_statfs osfs;
1064 int rc = dt_statfs(NULL, dt, &osfs);
1066 __u32 blk_size = osfs.os_bsize >> 10;
1067 __u64 result = osfs.os_bfree;
1069 while (blk_size >>= 1)
1072 seq_printf(m, "%llu\n", result);
1076 EXPORT_SYMBOL(lprocfs_dt_kbytesfree_seq_show);
1078 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v)
1080 struct dt_device *dt = m->private;
1081 struct obd_statfs osfs;
1083 int rc = dt_statfs(NULL, dt, &osfs);
1085 __u32 blk_size = osfs.os_bsize >> 10;
1086 __u64 result = osfs.os_bavail;
1088 while (blk_size >>= 1)
1091 seq_printf(m, "%llu\n", result);
1095 EXPORT_SYMBOL(lprocfs_dt_kbytesavail_seq_show);
1097 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v)
1099 struct dt_device *dt = m->private;
1100 struct obd_statfs osfs;
1102 int rc = dt_statfs(NULL, dt, &osfs);
1104 seq_printf(m, "%llu\n", osfs.os_files);
1107 EXPORT_SYMBOL(lprocfs_dt_filestotal_seq_show);
1109 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v)
1111 struct dt_device *dt = m->private;
1112 struct obd_statfs osfs;
1114 int rc = dt_statfs(NULL, dt, &osfs);
1116 seq_printf(m, "%llu\n", osfs.os_ffree);
1119 EXPORT_SYMBOL(lprocfs_dt_filesfree_seq_show);
1121 #endif /* CONFIG_PROC_FS */
1123 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
1126 struct dt_device *dt = container_of(kobj, struct dt_device,
1128 struct lu_device *lu = dt2lu_dev(dt);
1133 return sprintf(buf, "%s\n", lu->ld_obd->obd_uuid.uuid);
1135 LUSTRE_RO_ATTR(uuid);
1137 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
1140 struct dt_device *dt = container_of(kobj, struct dt_device,
1142 struct obd_statfs osfs;
1145 rc = dt_statfs(NULL, dt, &osfs);
1149 return sprintf(buf, "%u\n", (unsigned) osfs.os_bsize);
1151 LUSTRE_RO_ATTR(blocksize);
1153 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
1156 struct dt_device *dt = container_of(kobj, struct dt_device,
1158 struct obd_statfs osfs;
1163 rc = dt_statfs(NULL, dt, &osfs);
1167 blk_size = osfs.os_bsize >> 10;
1168 result = osfs.os_blocks;
1170 while (blk_size >>= 1)
1173 return sprintf(buf, "%llu\n", result);
1175 LUSTRE_RO_ATTR(kbytestotal);
1177 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
1180 struct dt_device *dt = container_of(kobj, struct dt_device,
1182 struct obd_statfs osfs;
1187 rc = dt_statfs(NULL, dt, &osfs);
1191 blk_size = osfs.os_bsize >> 10;
1192 result = osfs.os_bfree;
1194 while (blk_size >>= 1)
1197 return sprintf(buf, "%llu\n", result);
1199 LUSTRE_RO_ATTR(kbytesfree);
1201 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
1204 struct dt_device *dt = container_of(kobj, struct dt_device,
1206 struct obd_statfs osfs;
1211 rc = dt_statfs(NULL, dt, &osfs);
1215 blk_size = osfs.os_bsize >> 10;
1216 result = osfs.os_bavail;
1218 while (blk_size >>= 1)
1221 return sprintf(buf, "%llu\n", result);
1223 LUSTRE_RO_ATTR(kbytesavail);
1225 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
1228 struct dt_device *dt = container_of(kobj, struct dt_device,
1230 struct obd_statfs osfs;
1233 rc = dt_statfs(NULL, dt, &osfs);
1237 return sprintf(buf, "%llu\n", osfs.os_files);
1239 LUSTRE_RO_ATTR(filestotal);
1241 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
1244 struct dt_device *dt = container_of(kobj, struct dt_device,
1246 struct obd_statfs osfs;
1249 rc = dt_statfs(NULL, dt, &osfs);
1253 return sprintf(buf, "%llu\n", osfs.os_ffree);
1255 LUSTRE_RO_ATTR(filesfree);
1257 static const struct attribute *dt_def_attrs[] = {
1258 &lustre_attr_uuid.attr,
1259 &lustre_attr_blocksize.attr,
1260 &lustre_attr_kbytestotal.attr,
1261 &lustre_attr_kbytesfree.attr,
1262 &lustre_attr_kbytesavail.attr,
1263 &lustre_attr_filestotal.attr,
1264 &lustre_attr_filesfree.attr,
1268 static void dt_sysfs_release(struct kobject *kobj)
1270 struct dt_device *dt = container_of(kobj, struct dt_device,
1273 debugfs_remove_recursive(dt->dd_debugfs_entry);
1274 dt->dd_debugfs_entry = NULL;
1276 complete(&dt->dd_kobj_unregister);
1279 int dt_tunables_fini(struct dt_device *dt)
1284 if (dt->dd_def_attrs) {
1285 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1286 kobject_put(&dt->dd_kobj);
1287 wait_for_completion(&dt->dd_kobj_unregister);
1292 EXPORT_SYMBOL(dt_tunables_fini);
1294 int dt_tunables_init(struct dt_device *dt, struct obd_type *type,
1295 const char *name, struct ldebugfs_vars *list)
1299 dt->dd_ktype.sysfs_ops = &lustre_sysfs_ops;
1300 dt->dd_ktype.release = dt_sysfs_release;
1302 init_completion(&dt->dd_kobj_unregister);
1303 rc = kobject_init_and_add(&dt->dd_kobj, &dt->dd_ktype, &type->typ_kobj,
1308 dt->dd_def_attrs = dt_def_attrs;
1310 rc = sysfs_create_files(&dt->dd_kobj, dt->dd_def_attrs);
1312 kobject_put(&dt->dd_kobj);
1313 dt->dd_def_attrs = NULL;
1318 * No need to register debugfs if no enteries. This allows us to
1319 * choose between using dt_device or obd_device for debugfs.
1324 dt->dd_debugfs_entry = debugfs_create_dir(name,
1325 type->typ_debugfs_entry);
1326 ldebugfs_add_vars(dt->dd_debugfs_entry, list, dt);
1330 EXPORT_SYMBOL(dt_tunables_init);