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
61 /* no lock is necessary to protect the list, because call-backs
62 * are added during system startup. Please refer to "struct dt_device".
64 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
66 list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
68 EXPORT_SYMBOL(dt_txn_callback_add);
70 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
72 list_del_init(&cb->dtc_linkage);
74 EXPORT_SYMBOL(dt_txn_callback_del);
76 int dt_txn_hook_start(const struct lu_env *env,
77 struct dt_device *dev, struct thandle *th)
80 struct dt_txn_callback *cb;
85 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
86 struct thandle *dtc_th = th;
88 if (cb->dtc_txn_start == NULL ||
89 !(cb->dtc_tag & env->le_ctx.lc_tags))
92 /* Usually dt_txn_hook_start is called from bottom device,
93 * and if the thandle has th_top, then we need use top
94 * thandle for the callback in the top thandle layer */
95 if (th->th_top != NULL)
98 rc = cb->dtc_txn_start(env, dtc_th, cb->dtc_cookie);
104 EXPORT_SYMBOL(dt_txn_hook_start);
106 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *th)
108 struct dt_device *dev = th->th_dev;
109 struct dt_txn_callback *cb;
115 if (OBD_FAIL_CHECK(OBD_FAIL_DT_TXN_STOP))
118 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
119 struct thandle *dtc_th = th;
121 if (cb->dtc_txn_stop == NULL ||
122 !(cb->dtc_tag & env->le_ctx.lc_tags))
125 /* Usually dt_txn_hook_stop is called from bottom device,
126 * and if the thandle has th_top, then we need use top
127 * thandle for the callback in the top thandle layer */
128 if (th->th_top != NULL)
131 rc = cb->dtc_txn_stop(env, dtc_th, cb->dtc_cookie);
137 EXPORT_SYMBOL(dt_txn_hook_stop);
139 void dt_txn_hook_commit(struct thandle *th)
141 struct dt_txn_callback *cb;
146 list_for_each_entry(cb, &th->th_dev->dd_txn_callbacks,
148 /* Right now, the bottom device (OSD) will use this hook
149 * commit to notify OSP, so we do not check and replace
150 * the thandle to top thandle now */
151 if (cb->dtc_txn_commit)
152 cb->dtc_txn_commit(th, cb->dtc_cookie);
155 EXPORT_SYMBOL(dt_txn_hook_commit);
157 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
159 INIT_LIST_HEAD(&dev->dd_txn_callbacks);
160 return lu_device_init(&dev->dd_lu_dev, t);
162 EXPORT_SYMBOL(dt_device_init);
164 void dt_device_fini(struct dt_device *dev)
166 lu_device_fini(&dev->dd_lu_dev);
168 EXPORT_SYMBOL(dt_device_fini);
170 int dt_object_init(struct dt_object *obj,
171 struct lu_object_header *h, struct lu_device *d)
174 return lu_object_init(&obj->do_lu, h, d);
176 EXPORT_SYMBOL(dt_object_init);
178 void dt_object_fini(struct dt_object *obj)
180 lu_object_fini(&obj->do_lu);
182 EXPORT_SYMBOL(dt_object_fini);
184 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
186 if (obj->do_index_ops == NULL)
187 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
188 return obj->do_index_ops != NULL;
190 EXPORT_SYMBOL(dt_try_as_dir);
192 enum dt_format_type dt_mode_to_dft(__u32 mode)
194 enum dt_format_type result;
196 switch (mode & S_IFMT) {
201 result = DFT_REGULAR;
213 LASSERTF(0, "invalid mode %o\n", mode);
214 result = 0; /* Just for satisfying compiler. */
219 EXPORT_SYMBOL(dt_mode_to_dft);
222 * lookup fid for object named \a name in directory \a dir.
225 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
226 const char *name, struct lu_fid *fid)
228 if (dt_try_as_dir(env, dir))
229 return dt_lookup(env, dir, (struct dt_rec *)fid,
230 (const struct dt_key *)name);
233 EXPORT_SYMBOL(dt_lookup_dir);
235 /* this differs from dt_locate by top_dev as parameter
236 * but not one from lu_site */
237 struct dt_object *dt_locate_at(const struct lu_env *env,
238 struct dt_device *dev,
239 const struct lu_fid *fid,
240 struct lu_device *top_dev,
241 const struct lu_object_conf *conf)
243 struct lu_object *lo;
246 lo = lu_object_find_at(env, top_dev, fid, conf);
248 return ERR_PTR(PTR_ERR(lo));
252 list_for_each_entry(n, &lo->lo_header->loh_layers, lo_linkage) {
253 if (n->lo_dev == &dev->dd_lu_dev)
254 return container_of0(n, struct dt_object, do_lu);
257 lu_object_put(env, lo);
258 return ERR_PTR(-ENOENT);
260 EXPORT_SYMBOL(dt_locate_at);
263 * find an object named \a entry in given \a dfh->dfh_o directory.
265 static int dt_find_entry(const struct lu_env *env, const char *entry,
268 struct dt_find_hint *dfh = data;
269 struct dt_device *dt = dfh->dfh_dt;
270 struct lu_fid *fid = dfh->dfh_fid;
271 struct dt_object *obj = dfh->dfh_o;
274 rc = dt_lookup_dir(env, obj, entry, fid);
275 dt_object_put(env, obj);
277 obj = dt_locate(env, dt, fid);
287 * Abstract function which parses path name. This function feeds
288 * path component to \a entry_func.
290 int dt_path_parser(const struct lu_env *env,
291 char *path, dt_entry_func_t entry_func,
298 e = strsep(&path, "/");
303 if (!path || path[0] == '\0')
307 rc = entry_func(env, e, data);
316 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
317 const char *path, struct lu_fid *fid)
319 struct dt_thread_info *info = dt_info(env);
320 struct dt_find_hint *dfh = &info->dti_dfh;
321 struct dt_object *obj;
328 strlcpy(info->dti_buf, path, sizeof(info->dti_buf));
330 result = dt->dd_ops->dt_root_get(env, dt, fid);
332 obj = dt_locate(env, dt, fid);
335 result = dt_path_parser(env, info->dti_buf,
338 obj = ERR_PTR(result);
343 obj = ERR_PTR(result);
348 static struct dt_object *dt_reg_open(const struct lu_env *env,
349 struct dt_device *dt,
357 result = dt_lookup_dir(env, p, name, fid);
359 o = dt_locate(env, dt, fid);
368 * Open dt object named \a filename from \a dirname directory.
369 * \param dt dt device
370 * \param fid on success, object fid is stored in *fid
372 struct dt_object *dt_store_open(const struct lu_env *env, struct dt_device *dt,
373 const char *dirname, const char *filename,
376 struct dt_object *file;
377 struct dt_object *dir;
379 dir = dt_store_resolve(env, dt, dirname, fid);
381 file = dt_reg_open(env, dt, dir, filename, fid);
382 dt_object_put(env, dir);
390 struct dt_object *dt_find_or_create(const struct lu_env *env,
391 struct dt_device *dt,
392 const struct lu_fid *fid,
393 struct dt_object_format *dof,
396 struct dt_object *dto;
402 dto = dt_locate(env, dt, fid);
406 LASSERT(dto != NULL);
407 if (dt_object_exists(dto))
410 th = dt_trans_create(env, dt);
412 GOTO(out, rc = PTR_ERR(th));
414 rc = dt_declare_create(env, dto, at, NULL, dof, th);
416 GOTO(trans_stop, rc);
418 rc = dt_trans_start_local(env, dt, th);
420 GOTO(trans_stop, rc);
422 dt_write_lock(env, dto, 0);
423 if (dt_object_exists(dto))
424 GOTO(unlock, rc = 0);
426 CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(fid));
428 rc = dt_create(env, dto, at, NULL, dof, th);
431 LASSERT(dt_object_exists(dto));
433 dt_write_unlock(env, dto);
435 dt_trans_stop(env, dt, th);
438 dt_object_put(env, dto);
444 EXPORT_SYMBOL(dt_find_or_create);
446 /* dt class init function. */
447 int dt_global_init(void)
451 LU_CONTEXT_KEY_INIT(&dt_key);
452 result = lu_context_key_register(&dt_key);
456 void dt_global_fini(void)
458 lu_context_key_degister(&dt_key);
462 * Generic read helper. May return an error for partial reads.
464 * \param env lustre environment
465 * \param dt object to be read
466 * \param buf lu_buf to be filled, with buffer pointer and length
467 * \param pos position to start reading, updated as data is read
469 * \retval real size of data read
470 * \retval -ve errno on failure
472 int dt_read(const struct lu_env *env, struct dt_object *dt,
473 struct lu_buf *buf, loff_t *pos)
475 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
476 return dt->do_body_ops->dbo_read(env, dt, buf, pos);
478 EXPORT_SYMBOL(dt_read);
481 * Read structures of fixed size from storage. Unlike dt_read(), using
482 * dt_record_read() will return an error for partial reads.
484 * \param env lustre environment
485 * \param dt object to be read
486 * \param buf lu_buf to be filled, with buffer pointer and length
487 * \param pos position to start reading, updated as data is read
489 * \retval 0 on successfully reading full buffer
490 * \retval -EFAULT on short read
491 * \retval -ve errno on failure
493 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
494 struct lu_buf *buf, loff_t *pos)
498 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
500 size = dt->do_body_ops->dbo_read(env, dt, buf, pos);
503 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
505 EXPORT_SYMBOL(dt_record_read);
507 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
508 const struct lu_buf *buf, loff_t *pos, struct thandle *th)
512 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
514 LASSERT(dt->do_body_ops);
515 LASSERT(dt->do_body_ops->dbo_write);
517 size = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, 1);
520 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
522 EXPORT_SYMBOL(dt_record_write);
524 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
528 char *xname = XATTR_NAME_VERSION;
532 vbuf.lb_len = sizeof(dt_obj_version_t);
533 return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
536 EXPORT_SYMBOL(dt_declare_version_set);
538 void dt_version_set(const struct lu_env *env, struct dt_object *o,
539 dt_obj_version_t version, struct thandle *th)
542 char *xname = XATTR_NAME_VERSION;
546 vbuf.lb_buf = &version;
547 vbuf.lb_len = sizeof(version);
549 rc = dt_xattr_set(env, o, &vbuf, xname, 0, th);
551 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
554 EXPORT_SYMBOL(dt_version_set);
556 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
559 char *xname = XATTR_NAME_VERSION;
560 dt_obj_version_t version;
564 vbuf.lb_buf = &version;
565 vbuf.lb_len = sizeof(version);
566 rc = dt_xattr_get(env, o, &vbuf, xname);
567 if (rc != sizeof(version)) {
568 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
573 EXPORT_SYMBOL(dt_version_get);
575 /* list of all supported index types */
578 const struct dt_index_features dt_directory_features;
579 EXPORT_SYMBOL(dt_directory_features);
582 const struct dt_index_features dt_otable_features;
583 EXPORT_SYMBOL(dt_otable_features);
585 /* lfsck layout orphan */
586 const struct dt_index_features dt_lfsck_layout_orphan_features = {
588 .dif_keysize_min = sizeof(struct lu_fid),
589 .dif_keysize_max = sizeof(struct lu_fid),
590 .dif_recsize_min = sizeof(struct lu_orphan_rec_v2),
591 .dif_recsize_max = sizeof(struct lu_orphan_rec_v2),
594 EXPORT_SYMBOL(dt_lfsck_layout_orphan_features);
596 /* lfsck layout dangling */
597 const struct dt_index_features dt_lfsck_layout_dangling_features = {
598 .dif_flags = DT_IND_UPDATE,
599 .dif_keysize_min = sizeof(struct lfsck_layout_dangling_key),
600 .dif_keysize_max = sizeof(struct lfsck_layout_dangling_key),
601 .dif_recsize_min = sizeof(struct lu_fid),
602 .dif_recsize_max = sizeof(struct lu_fid),
605 EXPORT_SYMBOL(dt_lfsck_layout_dangling_features);
607 /* lfsck namespace */
608 const struct dt_index_features dt_lfsck_namespace_features = {
609 .dif_flags = DT_IND_UPDATE,
610 .dif_keysize_min = sizeof(struct lu_fid),
611 .dif_keysize_max = sizeof(struct lu_fid),
612 .dif_recsize_min = sizeof(__u8),
613 .dif_recsize_max = sizeof(__u8),
616 EXPORT_SYMBOL(dt_lfsck_namespace_features);
618 /* accounting indexes */
619 const struct dt_index_features dt_acct_features = {
620 .dif_flags = DT_IND_UPDATE,
621 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
622 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
623 .dif_recsize_min = sizeof(struct lquota_acct_rec), /* 16 bytes */
624 .dif_recsize_max = sizeof(struct lquota_acct_rec), /* 16 bytes */
627 EXPORT_SYMBOL(dt_acct_features);
629 /* global quota files */
630 const struct dt_index_features dt_quota_glb_features = {
631 .dif_flags = DT_IND_UPDATE,
632 /* a different key would have to be used for per-directory quota */
633 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
634 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
635 .dif_recsize_min = sizeof(struct lquota_glb_rec), /* 32 bytes */
636 .dif_recsize_max = sizeof(struct lquota_glb_rec), /* 32 bytes */
639 EXPORT_SYMBOL(dt_quota_glb_features);
641 /* slave quota files */
642 const struct dt_index_features dt_quota_slv_features = {
643 .dif_flags = DT_IND_UPDATE,
644 /* a different key would have to be used for per-directory quota */
645 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
646 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
647 .dif_recsize_min = sizeof(struct lquota_slv_rec), /* 8 bytes */
648 .dif_recsize_max = sizeof(struct lquota_slv_rec), /* 8 bytes */
651 EXPORT_SYMBOL(dt_quota_slv_features);
653 /* nodemap files, nodemap_rec size asserted in nodemap_storage.c */
654 const struct dt_index_features dt_nodemap_features = {
655 .dif_flags = DT_IND_UPDATE,
656 .dif_keysize_min = sizeof(__u64), /* 64-bit nodemap/record id */
657 .dif_keysize_max = sizeof(__u64), /* 64-bit nodemap/record id */
658 .dif_recsize_min = sizeof(union nodemap_rec), /* 32 bytes */
659 .dif_recsize_max = sizeof(union nodemap_rec), /* 32 bytes */
662 EXPORT_SYMBOL(dt_nodemap_features);
664 /* helper function returning what dt_index_features structure should be used
665 * based on the FID sequence. This is used by OBD_IDX_READ RPC */
666 static inline const struct dt_index_features *dt_index_feat_select(__u64 seq,
669 if (seq == FID_SEQ_QUOTA_GLB) {
670 /* global quota index */
672 /* global quota index should be a regular file */
673 return ERR_PTR(-ENOENT);
674 return &dt_quota_glb_features;
675 } else if (seq == FID_SEQ_QUOTA) {
676 /* quota slave index */
678 /* slave index should be a regular file */
679 return ERR_PTR(-ENOENT);
680 return &dt_quota_slv_features;
681 } else if (seq == FID_SEQ_LAYOUT_RBTREE){
682 return &dt_lfsck_layout_orphan_features;
683 } else if (seq >= FID_SEQ_NORMAL) {
684 /* object is part of the namespace, verify that it is a
687 /* sorry, we can only deal with directory */
688 return ERR_PTR(-ENOTDIR);
689 return &dt_directory_features;
692 return ERR_PTR(-EOPNOTSUPP);
696 * Fill a lu_idxpage with key/record pairs read for transfer via OBD_IDX_READ
699 * \param env - is the environment passed by the caller
700 * \param lp - is a pointer to the lu_page to fill
701 * \param nob - is the maximum number of bytes that should be copied
702 * \param iops - is the index operation vector associated with the index object
703 * \param it - is a pointer to the current iterator
704 * \param attr - is the index attribute to pass to iops->rec()
705 * \param arg - is a pointer to the idx_info structure
707 static int dt_index_page_build(const struct lu_env *env, union lu_page *lp,
708 size_t nob, const struct dt_it_ops *iops,
709 struct dt_it *it, __u32 attr, void *arg)
711 struct idx_info *ii = (struct idx_info *)arg;
712 struct lu_idxpage *lip = &lp->lp_idx;
718 if (nob < LIP_HDR_SIZE)
721 /* initialize the header of the new container */
722 memset(lip, 0, LIP_HDR_SIZE);
723 lip->lip_magic = LIP_MAGIC;
726 /* compute size needed to store a key/record pair */
727 size = ii->ii_recsize + ii->ii_keysize;
728 if ((ii->ii_flags & II_FL_NOHASH) == 0)
729 /* add hash if the client wants it */
730 size += sizeof(__u64);
732 entry = lip->lip_entries;
734 char *tmp_entry = entry;
740 /* fetch 64-bit hash value */
741 hash = iops->store(env, it);
742 ii->ii_hash_end = hash;
744 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
745 if (lip->lip_nr != 0)
750 if (lip->lip_nr == 0)
751 GOTO(out, rc = -EINVAL);
755 if (!(ii->ii_flags & II_FL_NOHASH)) {
756 /* client wants to the 64-bit hash value associated with
758 memcpy(tmp_entry, &hash, sizeof(hash));
759 tmp_entry += sizeof(hash);
762 if (ii->ii_flags & II_FL_VARKEY)
763 keysize = iops->key_size(env, it);
765 keysize = ii->ii_keysize;
767 if (!(ii->ii_flags & II_FL_NOKEY)) {
768 /* then the key value */
769 key = iops->key(env, it);
770 memcpy(tmp_entry, key, keysize);
771 tmp_entry += keysize;
774 /* and finally the record */
775 rc = iops->rec(env, it, (struct dt_rec *)tmp_entry, attr);
780 /* hash/key/record successfully copied! */
782 if (unlikely(lip->lip_nr == 1 && ii->ii_count == 0))
783 ii->ii_hash_start = hash;
785 if (ii->ii_flags & II_FL_VARREC)
786 recsize = iops->rec_size(env, it, attr);
788 recsize = ii->ii_recsize;
790 entry = tmp_entry + recsize;
794 /* move on to the next record */
796 rc = iops->next(env, it);
797 } while (rc == -ESTALE);
803 if (rc >= 0 && lip->lip_nr > 0)
804 /* one more container */
807 /* no more entries */
808 ii->ii_hash_end = II_END_OFF;
814 * Walk index and fill lu_page containers with key/record pairs
816 * \param env - is the environment passed by the caller
817 * \param obj - is the index object to parse
818 * \param rdpg - is the lu_rdpg descriptor associated with the transfer
819 * \param filler - is the callback function responsible for filling a lu_page
820 * with key/record pairs in the format wanted by the caller.
821 * If NULL, uses dt_index_page_build
822 * \param arg - is an opaq argument passed to the filler function
824 * \retval sum (in bytes) of all filled lu_pages
825 * \retval -ve errno on failure
827 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
828 const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
832 const struct dt_it_ops *iops;
833 size_t pageidx, nob, nlupgs = 0;
837 LASSERT(rdpg->rp_pages != NULL);
838 LASSERT(obj->do_index_ops != NULL);
841 filler = dt_index_page_build;
843 nob = rdpg->rp_count;
847 /* Iterate through index and fill containers from @rdpg */
848 iops = &obj->do_index_ops->dio_it;
849 LASSERT(iops != NULL);
850 it = iops->init(env, obj, rdpg->rp_attrs);
854 rc = iops->load(env, it, rdpg->rp_hash);
857 * Iterator didn't find record with exactly the key requested.
859 * It is currently either
861 * - positioned above record with key less than
862 * requested---skip it.
863 * - or not positioned at all (is in IAM_IT_SKEWED
864 * state)---position it on the next item.
866 rc = iops->next(env, it);
875 /* Fill containers one after the other. There might be multiple
876 * containers per physical page.
878 * At this point and across for-loop:
879 * rc == 0 -> ok, proceed.
880 * rc > 0 -> end of index.
881 * rc < 0 -> error. */
882 for (pageidx = 0; rc == 0 && nob > 0; pageidx++) {
886 LASSERT(pageidx < rdpg->rp_npages);
887 lp = kmap(rdpg->rp_pages[pageidx]);
890 for (i = 0; i < LU_PAGE_COUNT; i++, lp++, nob -= LU_PAGE_SIZE) {
891 rc = filler(env, lp, min_t(size_t, nob, LU_PAGE_SIZE),
892 iops, it, rdpg->rp_attrs, arg);
895 /* one more lu_page */
901 kunmap(rdpg->rp_pages[i]);
909 rc = min_t(size_t, nlupgs * LU_PAGE_SIZE, rdpg->rp_count);
913 EXPORT_SYMBOL(dt_index_walk);
916 * Walk key/record pairs of an index and copy them into 4KB containers to be
917 * transferred over the network. This is the common handler for OBD_IDX_READ
920 * \param env - is the environment passed by the caller
921 * \param dev - is the dt_device storing the index
922 * \param ii - is the idx_info structure packed by the client in the
923 * OBD_IDX_READ request
924 * \param rdpg - is the lu_rdpg descriptor
926 * \retval on success, return sum (in bytes) of all filled containers
927 * \retval appropriate error otherwise.
929 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
930 struct idx_info *ii, const struct lu_rdpg *rdpg)
932 const struct dt_index_features *feat;
933 struct dt_object *obj;
937 /* rp_count shouldn't be null and should be a multiple of the container
939 if (rdpg->rp_count == 0 || (rdpg->rp_count & (LU_PAGE_SIZE - 1)) != 0)
942 if (!fid_is_quota(&ii->ii_fid) && !fid_is_layout_rbtree(&ii->ii_fid) &&
943 !fid_is_norm(&ii->ii_fid))
946 /* lookup index object subject to the transfer */
947 obj = dt_locate(env, dev, &ii->ii_fid);
949 RETURN(PTR_ERR(obj));
950 if (dt_object_exists(obj) == 0)
951 GOTO(out, rc = -ENOENT);
953 /* fetch index features associated with index object */
954 feat = dt_index_feat_select(fid_seq(&ii->ii_fid),
955 lu_object_attr(&obj->do_lu));
957 GOTO(out, rc = PTR_ERR(feat));
959 /* load index feature if not done already */
960 if (obj->do_index_ops == NULL) {
961 rc = obj->do_ops->do_index_try(env, obj, feat);
966 /* fill ii_flags with supported index features */
967 ii->ii_flags &= (II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
970 if (!(feat->dif_flags & DT_IND_VARKEY))
971 ii->ii_keysize = feat->dif_keysize_max;
973 if (!(feat->dif_flags & DT_IND_VARREC))
974 ii->ii_recsize = feat->dif_recsize_max;
976 if (feat->dif_flags & DT_IND_NONUNQ)
977 /* key isn't necessarily unique */
978 ii->ii_flags |= II_FL_NONUNQ;
980 if (!fid_is_layout_rbtree(&ii->ii_fid)) {
981 dt_read_lock(env, obj, 0);
982 /* fetch object version before walking the index */
983 ii->ii_version = dt_version_get(env, obj);
986 /* walk the index and fill lu_idxpages with key/record pairs */
987 rc = dt_index_walk(env, obj, rdpg, dt_index_page_build, ii);
988 if (!fid_is_layout_rbtree(&ii->ii_fid))
989 dt_read_unlock(env, obj);
993 LASSERT(ii->ii_count == 0);
994 ii->ii_hash_end = II_END_OFF;
999 dt_object_put(env, obj);
1002 EXPORT_SYMBOL(dt_index_read);
1004 #ifdef CONFIG_PROC_FS
1005 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v)
1007 struct dt_device *dt = m->private;
1008 struct obd_statfs osfs;
1010 int rc = dt_statfs(NULL, dt, &osfs);
1012 seq_printf(m, "%u\n", (unsigned) osfs.os_bsize);
1015 EXPORT_SYMBOL(lprocfs_dt_blksize_seq_show);
1017 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v)
1019 struct dt_device *dt = m->private;
1020 struct obd_statfs osfs;
1022 int rc = dt_statfs(NULL, dt, &osfs);
1024 __u32 blk_size = osfs.os_bsize >> 10;
1025 __u64 result = osfs.os_blocks;
1027 while (blk_size >>= 1)
1030 seq_printf(m, "%llu\n", result);
1034 EXPORT_SYMBOL(lprocfs_dt_kbytestotal_seq_show);
1036 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v)
1038 struct dt_device *dt = m->private;
1039 struct obd_statfs osfs;
1041 int rc = dt_statfs(NULL, dt, &osfs);
1043 __u32 blk_size = osfs.os_bsize >> 10;
1044 __u64 result = osfs.os_bfree;
1046 while (blk_size >>= 1)
1049 seq_printf(m, "%llu\n", result);
1053 EXPORT_SYMBOL(lprocfs_dt_kbytesfree_seq_show);
1055 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v)
1057 struct dt_device *dt = m->private;
1058 struct obd_statfs osfs;
1060 int rc = dt_statfs(NULL, dt, &osfs);
1062 __u32 blk_size = osfs.os_bsize >> 10;
1063 __u64 result = osfs.os_bavail;
1065 while (blk_size >>= 1)
1068 seq_printf(m, "%llu\n", result);
1072 EXPORT_SYMBOL(lprocfs_dt_kbytesavail_seq_show);
1074 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v)
1076 struct dt_device *dt = m->private;
1077 struct obd_statfs osfs;
1079 int rc = dt_statfs(NULL, dt, &osfs);
1081 seq_printf(m, "%llu\n", osfs.os_files);
1084 EXPORT_SYMBOL(lprocfs_dt_filestotal_seq_show);
1086 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v)
1088 struct dt_device *dt = m->private;
1089 struct obd_statfs osfs;
1091 int rc = dt_statfs(NULL, dt, &osfs);
1093 seq_printf(m, "%llu\n", osfs.os_ffree);
1096 EXPORT_SYMBOL(lprocfs_dt_filesfree_seq_show);
1098 #endif /* CONFIG_PROC_FS */
1100 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
1103 struct dt_device *dt = container_of(kobj, struct dt_device,
1105 struct lu_device *lu = dt2lu_dev(dt);
1110 return sprintf(buf, "%s\n", lu->ld_obd->obd_uuid.uuid);
1112 LUSTRE_RO_ATTR(uuid);
1114 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
1117 struct dt_device *dt = container_of(kobj, struct dt_device,
1119 struct obd_statfs osfs;
1122 rc = dt_statfs(NULL, dt, &osfs);
1126 return sprintf(buf, "%u\n", (unsigned) osfs.os_bsize);
1128 LUSTRE_RO_ATTR(blocksize);
1130 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
1133 struct dt_device *dt = container_of(kobj, struct dt_device,
1135 struct obd_statfs osfs;
1140 rc = dt_statfs(NULL, dt, &osfs);
1144 blk_size = osfs.os_bsize >> 10;
1145 result = osfs.os_blocks;
1147 while (blk_size >>= 1)
1150 return sprintf(buf, "%llu\n", result);
1152 LUSTRE_RO_ATTR(kbytestotal);
1154 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
1157 struct dt_device *dt = container_of(kobj, struct dt_device,
1159 struct obd_statfs osfs;
1164 rc = dt_statfs(NULL, dt, &osfs);
1168 blk_size = osfs.os_bsize >> 10;
1169 result = osfs.os_bfree;
1171 while (blk_size >>= 1)
1174 return sprintf(buf, "%llu\n", result);
1176 LUSTRE_RO_ATTR(kbytesfree);
1178 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
1181 struct dt_device *dt = container_of(kobj, struct dt_device,
1183 struct obd_statfs osfs;
1188 rc = dt_statfs(NULL, dt, &osfs);
1192 blk_size = osfs.os_bsize >> 10;
1193 result = osfs.os_bavail;
1195 while (blk_size >>= 1)
1198 return sprintf(buf, "%llu\n", result);
1200 LUSTRE_RO_ATTR(kbytesavail);
1202 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
1205 struct dt_device *dt = container_of(kobj, struct dt_device,
1207 struct obd_statfs osfs;
1210 rc = dt_statfs(NULL, dt, &osfs);
1214 return sprintf(buf, "%llu\n", osfs.os_files);
1216 LUSTRE_RO_ATTR(filestotal);
1218 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
1221 struct dt_device *dt = container_of(kobj, struct dt_device,
1223 struct obd_statfs osfs;
1226 rc = dt_statfs(NULL, dt, &osfs);
1230 return sprintf(buf, "%llu\n", osfs.os_ffree);
1232 LUSTRE_RO_ATTR(filesfree);
1234 static const struct attribute *dt_def_attrs[] = {
1235 &lustre_attr_uuid.attr,
1236 &lustre_attr_blocksize.attr,
1237 &lustre_attr_kbytestotal.attr,
1238 &lustre_attr_kbytesfree.attr,
1239 &lustre_attr_kbytesavail.attr,
1240 &lustre_attr_filestotal.attr,
1241 &lustre_attr_filesfree.attr,
1245 static void dt_sysfs_release(struct kobject *kobj)
1247 struct dt_device *dt = container_of(kobj, struct dt_device,
1250 complete(&dt->dd_kobj_unregister);
1253 int dt_tunables_fini(struct dt_device *dt)
1258 if (!IS_ERR_OR_NULL(dt->dd_debugfs_entry))
1259 ldebugfs_remove(&dt->dd_debugfs_entry);
1261 if (dt->dd_def_attrs)
1262 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1264 kobject_put(&dt->dd_kobj);
1265 wait_for_completion(&dt->dd_kobj_unregister);
1269 EXPORT_SYMBOL(dt_tunables_fini);
1271 int dt_tunables_init(struct dt_device *dt, struct obd_type *type,
1272 const char *name, struct lprocfs_vars *list)
1276 dt->dd_ktype.sysfs_ops = &lustre_sysfs_ops;
1277 dt->dd_ktype.release = dt_sysfs_release;
1279 init_completion(&dt->dd_kobj_unregister);
1280 rc = kobject_init_and_add(&dt->dd_kobj, &dt->dd_ktype, type->typ_kobj,
1285 dt->dd_def_attrs = dt_def_attrs;
1287 rc = sysfs_create_files(&dt->dd_kobj, dt->dd_def_attrs);
1289 kobject_put(&dt->dd_kobj);
1293 /* No need to register debugfs if no enteries. This allows us to
1294 * choose between using dt_device or obd_device for debugfs.
1299 dt->dd_debugfs_entry = ldebugfs_register(name,
1300 type->typ_debugfs_entry,
1302 if (IS_ERR_OR_NULL(dt->dd_debugfs_entry)) {
1303 rc = dt->dd_debugfs_entry ? PTR_ERR(dt->dd_debugfs_entry)
1305 CERROR("%s: error %d setting up debugfs\n",
1307 dt->dd_debugfs_entry = NULL;
1308 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1309 kobject_put(&dt->dd_kobj);
1315 EXPORT_SYMBOL(dt_tunables_init);