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>
50 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
51 LU_KEY_INIT(dt_global, struct dt_thread_info);
52 LU_KEY_FINI(dt_global, struct dt_thread_info);
54 struct lu_context_key dt_key = {
55 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD | LCT_LOCAL,
56 .lct_init = dt_global_key_init,
57 .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))
93 * Usually dt_txn_hook_start is called from bottom device,
94 * and if the thandle has th_top, then we need use top
95 * thandle for the callback in the top thandle layer
97 if (th->th_top != NULL)
100 rc = cb->dtc_txn_start(env, dtc_th, cb->dtc_cookie);
106 EXPORT_SYMBOL(dt_txn_hook_start);
108 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *th)
110 struct dt_device *dev = th->th_dev;
111 struct dt_txn_callback *cb;
117 if (OBD_FAIL_CHECK(OBD_FAIL_DT_TXN_STOP))
120 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
121 struct thandle *dtc_th = th;
123 if (cb->dtc_txn_stop == NULL ||
124 !(cb->dtc_tag & env->le_ctx.lc_tags))
128 * Usually dt_txn_hook_stop is called from bottom device,
129 * and if the thandle has th_top, then we need use top
130 * thandle for the callback in the top thandle layer
132 if (th->th_top != NULL)
135 rc = cb->dtc_txn_stop(env, dtc_th, cb->dtc_cookie);
141 EXPORT_SYMBOL(dt_txn_hook_stop);
143 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
145 INIT_LIST_HEAD(&dev->dd_txn_callbacks);
146 return lu_device_init(&dev->dd_lu_dev, t);
148 EXPORT_SYMBOL(dt_device_init);
150 void dt_device_fini(struct dt_device *dev)
152 lu_device_fini(&dev->dd_lu_dev);
154 EXPORT_SYMBOL(dt_device_fini);
156 int dt_object_init(struct dt_object *obj,
157 struct lu_object_header *h, struct lu_device *d)
160 return lu_object_init(&obj->do_lu, h, d);
162 EXPORT_SYMBOL(dt_object_init);
164 void dt_object_fini(struct dt_object *obj)
166 lu_object_fini(&obj->do_lu);
168 EXPORT_SYMBOL(dt_object_fini);
171 * Set directory .do_index_ops.
173 * Set directory index operations, if the caller knows directory exists,
174 * \a check should be set to ensure object is directory and exists, while for
175 * new directories, skip check and the index operations will be used to create
176 * ".." under directory.
178 * Normally this is called before dt_lookup() to ensure directory objects
179 * exists and .do_index_ops is correctly set.
181 * \param env lu_env object.
182 * \param obj dt object.
183 * \param check check \a obj existence and type, return if index ops is set.
184 * \retval 1 on success.
185 * \retval 0 on error.
187 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj, bool check)
190 if (unlikely(!dt_object_exists(obj)))
193 if (unlikely(!S_ISDIR(lu_object_attr(&obj->do_lu))))
196 if (obj->do_index_ops)
200 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
202 return obj->do_index_ops != NULL;
204 EXPORT_SYMBOL(dt_try_as_dir);
206 enum dt_format_type dt_mode_to_dft(__u32 mode)
208 enum dt_format_type result;
210 switch (mode & S_IFMT) {
215 result = DFT_REGULAR;
227 LASSERTF(0, "invalid mode %o\n", mode);
228 result = 0; /* Just for satisfying compiler. */
233 EXPORT_SYMBOL(dt_mode_to_dft);
236 * lookup fid for object named \a name in directory \a dir.
239 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
240 const char *name, struct lu_fid *fid)
242 if (dt_try_as_dir(env, dir, true))
243 return dt_lookup(env, dir, (struct dt_rec *)fid,
244 (const struct dt_key *)name);
247 EXPORT_SYMBOL(dt_lookup_dir);
250 * this differs from dt_locate by top_dev as parameter
251 * but not one from lu_site
253 struct dt_object *dt_locate_at(const struct lu_env *env,
254 struct dt_device *dev,
255 const struct lu_fid *fid,
256 struct lu_device *top_dev,
257 const struct lu_object_conf *conf)
259 struct lu_object *lo;
262 lo = lu_object_find_at(env, top_dev, fid, conf);
268 list_for_each_entry(n, &lo->lo_header->loh_layers, lo_linkage) {
269 if (n->lo_dev == &dev->dd_lu_dev)
270 return container_of(n, struct dt_object, do_lu);
273 lu_object_put(env, lo);
274 return ERR_PTR(-ENOENT);
276 EXPORT_SYMBOL(dt_locate_at);
279 * find an object named \a entry in given \a dfh->dfh_o directory.
281 static int dt_find_entry(const struct lu_env *env, const char *entry,
284 struct dt_find_hint *dfh = data;
285 struct dt_device *dt = dfh->dfh_dt;
286 struct lu_fid *fid = dfh->dfh_fid;
287 struct dt_object *obj = dfh->dfh_o;
290 rc = dt_lookup_dir(env, obj, entry, fid);
291 dt_object_put(env, obj);
293 obj = dt_locate(env, dt, fid);
303 * Abstract function which parses path name. This function feeds
304 * path component to \a entry_func.
306 int dt_path_parser(const struct lu_env *env,
307 char *path, dt_entry_func_t entry_func,
314 e = strsep(&path, "/");
319 if (!path || path[0] == '\0')
323 rc = entry_func(env, e, data);
332 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
333 const char *path, struct lu_fid *fid)
335 struct dt_thread_info *info = dt_info(env);
336 struct dt_find_hint *dfh = &info->dti_dfh;
337 struct dt_object *obj;
344 strlcpy(info->dti_buf, path, sizeof(info->dti_buf));
346 result = dt->dd_ops->dt_root_get(env, dt, fid);
348 obj = dt_locate(env, dt, fid);
351 result = dt_path_parser(env, info->dti_buf,
354 obj = ERR_PTR(result);
359 obj = ERR_PTR(result);
364 static struct dt_object *dt_reg_open(const struct lu_env *env,
365 struct dt_device *dt,
373 result = dt_lookup_dir(env, p, name, fid);
375 o = dt_locate(env, dt, fid);
383 * Open dt object named \a filename from \a dirname directory.
384 * \param dt dt device
385 * \param fid on success, object fid is stored in *fid
387 struct dt_object *dt_store_open(const struct lu_env *env, struct dt_device *dt,
388 const char *dirname, const char *filename,
391 struct dt_object *file;
392 struct dt_object *dir;
394 dir = dt_store_resolve(env, dt, dirname, fid);
396 file = dt_reg_open(env, dt, dir, filename, fid);
397 dt_object_put(env, dir);
405 struct dt_object *dt_find_or_create(const struct lu_env *env,
406 struct dt_device *dt,
407 const struct lu_fid *fid,
408 struct dt_object_format *dof,
411 struct dt_object *dto;
417 dto = dt_locate(env, dt, fid);
421 LASSERT(dto != NULL);
422 if (dt_object_exists(dto))
425 th = dt_trans_create(env, dt);
427 GOTO(out, rc = PTR_ERR(th));
429 rc = dt_declare_create(env, dto, at, NULL, dof, th);
431 GOTO(trans_stop, rc);
433 rc = dt_trans_start_local(env, dt, th);
435 GOTO(trans_stop, rc);
437 dt_write_lock(env, dto, 0);
438 if (dt_object_exists(dto))
439 GOTO(unlock, rc = 0);
441 CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(fid));
443 rc = dt_create(env, dto, at, NULL, dof, th);
446 LASSERT(dt_object_exists(dto));
448 dt_write_unlock(env, dto);
450 dt_trans_stop(env, dt, th);
453 dt_object_put(env, dto);
459 EXPORT_SYMBOL(dt_find_or_create);
461 /* dt class init function. */
462 int dt_global_init(void)
466 LU_CONTEXT_KEY_INIT(&dt_key);
467 result = lu_context_key_register(&dt_key);
471 void dt_global_fini(void)
473 lu_context_key_degister(&dt_key);
477 * Generic read helper. May return an error for partial reads.
479 * \param env lustre environment
480 * \param dt object to be read
481 * \param buf lu_buf to be filled, with buffer pointer and length
482 * \param pos position to start reading, updated as data is read
484 * \retval real size of data read
485 * \retval -ve errno on failure
487 int dt_read(const struct lu_env *env, struct dt_object *dt,
488 struct lu_buf *buf, loff_t *pos)
490 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
491 return dt->do_body_ops->dbo_read(env, dt, buf, pos);
493 EXPORT_SYMBOL(dt_read);
496 * Read structures of fixed size from storage. Unlike dt_read(), using
497 * dt_record_read() will return an error for partial reads.
499 * \param env lustre environment
500 * \param dt object to be read
501 * \param buf lu_buf to be filled, with buffer pointer and length
502 * \param pos position to start reading, updated as data is read
504 * \retval 0 on successfully reading full buffer
505 * \retval -EFAULT on short read
506 * \retval -ve errno on failure
508 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
509 struct lu_buf *buf, loff_t *pos)
513 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
515 size = dt->do_body_ops->dbo_read(env, dt, buf, pos);
518 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
520 EXPORT_SYMBOL(dt_record_read);
522 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
523 const struct lu_buf *buf, loff_t *pos, struct thandle *th)
527 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
529 LASSERT(dt->do_body_ops);
530 LASSERTF(dt->do_body_ops->dbo_write, DFID"\n",
531 PFID(lu_object_fid(&dt->do_lu)));
533 size = dt->do_body_ops->dbo_write(env, dt, buf, pos, th);
536 return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
538 EXPORT_SYMBOL(dt_record_write);
540 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
544 char *xname = XATTR_NAME_VERSION;
548 vbuf.lb_len = sizeof(dt_obj_version_t);
549 return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
552 EXPORT_SYMBOL(dt_declare_version_set);
554 void dt_version_set(const struct lu_env *env, struct dt_object *o,
555 dt_obj_version_t version, struct thandle *th)
558 char *xname = XATTR_NAME_VERSION;
562 vbuf.lb_buf = &version;
563 vbuf.lb_len = sizeof(version);
565 rc = dt_xattr_set(env, o, &vbuf, xname, 0, th);
567 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
569 EXPORT_SYMBOL(dt_version_set);
571 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
574 char *xname = XATTR_NAME_VERSION;
575 dt_obj_version_t version;
579 vbuf.lb_buf = &version;
580 vbuf.lb_len = sizeof(version);
581 rc = dt_xattr_get(env, o, &vbuf, xname);
582 if (rc != sizeof(version)) {
583 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
588 EXPORT_SYMBOL(dt_version_get);
590 /* list of all supported index types */
593 const struct dt_index_features dt_directory_features;
594 EXPORT_SYMBOL(dt_directory_features);
597 const struct dt_index_features dt_otable_features;
598 EXPORT_SYMBOL(dt_otable_features);
600 /* lfsck layout orphan */
601 const struct dt_index_features dt_lfsck_layout_orphan_features = {
603 .dif_keysize_min = sizeof(struct lu_fid),
604 .dif_keysize_max = sizeof(struct lu_fid),
605 .dif_recsize_min = sizeof(struct lu_orphan_rec_v3),
606 .dif_recsize_max = sizeof(struct lu_orphan_rec_v3),
609 EXPORT_SYMBOL(dt_lfsck_layout_orphan_features);
611 /* lfsck layout dangling */
612 const struct dt_index_features dt_lfsck_layout_dangling_features = {
613 .dif_flags = DT_IND_UPDATE,
614 .dif_keysize_min = sizeof(struct lfsck_layout_dangling_key),
615 .dif_keysize_max = sizeof(struct lfsck_layout_dangling_key),
616 .dif_recsize_min = sizeof(struct lu_fid),
617 .dif_recsize_max = sizeof(struct lu_fid),
620 EXPORT_SYMBOL(dt_lfsck_layout_dangling_features);
622 /* lfsck namespace */
623 const struct dt_index_features dt_lfsck_namespace_features = {
624 .dif_flags = DT_IND_UPDATE,
625 .dif_keysize_min = sizeof(struct lu_fid),
626 .dif_keysize_max = sizeof(struct lu_fid),
627 .dif_recsize_min = sizeof(__u8),
628 .dif_recsize_max = sizeof(__u8),
631 EXPORT_SYMBOL(dt_lfsck_namespace_features);
633 /* accounting indexes */
634 const struct dt_index_features dt_acct_features = {
635 .dif_flags = DT_IND_UPDATE,
636 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
637 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
638 .dif_recsize_min = sizeof(struct lquota_acct_rec), /* 16 bytes */
639 .dif_recsize_max = sizeof(struct lquota_acct_rec), /* 16 bytes */
642 EXPORT_SYMBOL(dt_acct_features);
644 /* global quota files */
645 const struct dt_index_features dt_quota_glb_features = {
646 .dif_flags = DT_IND_UPDATE,
647 /* a different key would have to be used for per-directory quota */
648 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
649 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
650 .dif_recsize_min = sizeof(struct lquota_glb_rec), /* 32 bytes */
651 .dif_recsize_max = sizeof(struct lquota_glb_rec), /* 32 bytes */
654 EXPORT_SYMBOL(dt_quota_glb_features);
656 /* slave quota files */
657 const struct dt_index_features dt_quota_slv_features = {
658 .dif_flags = DT_IND_UPDATE,
659 /* a different key would have to be used for per-directory quota */
660 .dif_keysize_min = sizeof(__u64), /* 64-bit uid/gid */
661 .dif_keysize_max = sizeof(__u64), /* 64-bit uid/gid */
662 .dif_recsize_min = sizeof(struct lquota_slv_rec), /* 8 bytes */
663 .dif_recsize_max = sizeof(struct lquota_slv_rec), /* 8 bytes */
666 EXPORT_SYMBOL(dt_quota_slv_features);
668 /* nodemap files, nodemap_rec size asserted in nodemap_storage.c */
669 const struct dt_index_features dt_nodemap_features = {
670 .dif_flags = DT_IND_UPDATE,
671 .dif_keysize_min = sizeof(__u64), /* 64-bit nodemap/record id */
672 .dif_keysize_max = sizeof(__u64), /* 64-bit nodemap/record id */
673 .dif_recsize_min = sizeof(union nodemap_rec), /* 32 bytes */
674 .dif_recsize_max = sizeof(union nodemap_rec), /* 32 bytes */
677 EXPORT_SYMBOL(dt_nodemap_features);
680 * helper function returning what dt_index_features structure should be used
681 * based on the FID sequence. This is used by OBD_IDX_READ RPC
683 static inline const struct dt_index_features *dt_index_feat_select(__u64 seq,
686 if (seq == FID_SEQ_QUOTA_GLB) {
687 /* global quota index */
689 /* global quota index should be a regular file */
690 return ERR_PTR(-ENOENT);
691 return &dt_quota_glb_features;
692 } else if (seq == FID_SEQ_QUOTA) {
693 /* quota slave index */
695 /* slave index should be a regular file */
696 return ERR_PTR(-ENOENT);
697 return &dt_quota_slv_features;
698 } else if (seq == FID_SEQ_LAYOUT_RBTREE){
699 return &dt_lfsck_layout_orphan_features;
700 } else if (seq >= FID_SEQ_NORMAL) {
701 /* object is part of the namespace, verify that it is a
704 /* sorry, we can only deal with directory */
705 return ERR_PTR(-ENOTDIR);
706 return &dt_directory_features;
709 return ERR_PTR(-EOPNOTSUPP);
713 * Fill a lu_idxpage with key/record pairs read for transfer via OBD_IDX_READ
716 * \param env - is the environment passed by the caller
717 * \param obj - index object being traversed (mostly for debugging)
718 * \param lp - is a pointer to the lu_page to fill
719 * \param bytes - is the maximum number of bytes that should be copied
720 * \param iops - is the index operation vector associated with the index object
721 * \param it - is a pointer to the current iterator
722 * \param attr - is the index attribute to pass to iops->rec()
723 * \param arg - is a pointer to the idx_info structure
725 static int dt_index_page_build(const struct lu_env *env, struct dt_object *obj,
726 union lu_page *lp, size_t bytes,
727 const struct dt_it_ops *iops,
728 struct dt_it *it, __u32 attr, void *arg)
730 struct idx_info *ii = (struct idx_info *)arg;
731 struct lu_idxpage *lip = &lp->lp_idx;
741 if (bytes < LIP_HDR_SIZE)
744 /* initialize the header of the new container */
745 memset(lip, 0, LIP_HDR_SIZE);
746 lip->lip_magic = LIP_MAGIC;
747 bytes -= LIP_HDR_SIZE;
749 /* client wants to the 64-bit hash value associated with each record */
750 if (!(ii->ii_flags & II_FL_NOHASH))
751 hashsize = sizeof(hash);
753 entry = lip->lip_entries;
755 /* fetch 64-bit hash value */
756 hash = iops->store(env, it);
757 ii->ii_hash_end = hash;
759 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
760 if (lip->lip_nr != 0)
764 if (!(ii->ii_flags & II_FL_NOKEY)) {
765 keysize = iops->key_size(env, it);
766 if (!(ii->ii_flags & II_FL_VARKEY) &&
767 keysize != ii->ii_keysize) {
769 CERROR("%s: keysize mismatch %hu != %hu on "
771 lu_dev_name(obj->do_lu.lo_dev),
772 keysize, ii->ii_keysize,
773 PFID(lu_object_fid(&obj->do_lu)), rc);
778 /* and finally the record */
779 if (ii->ii_flags & II_FL_VARREC)
780 recsize = iops->rec_size(env, it, attr);
782 recsize = ii->ii_recsize;
784 if (bytes < hashsize + keysize + recsize) {
785 if (lip->lip_nr == 0)
786 GOTO(out, rc = -E2BIG);
790 rc = iops->rec(env, it,
791 (struct dt_rec *)(entry + hashsize + keysize),
795 memcpy(entry, &hash, hashsize);
799 key = iops->key(env, it);
800 memcpy(entry + hashsize, key, keysize);
802 /* hash/key/record successfully copied! */
804 if (unlikely(lip->lip_nr == 1 && ii->ii_count == 0))
805 ii->ii_hash_start = hash;
806 entry += hashsize + keysize + recsize;
807 bytes -= hashsize + keysize + recsize;
808 } else if (rc != -ESTALE) {
812 /* move on to the next record */
814 rc = iops->next(env, it);
815 } while (rc == -ESTALE);
820 if (rc >= 0 && lip->lip_nr > 0)
821 /* one more container */
824 /* no more entries */
825 ii->ii_hash_end = II_END_OFF;
831 * Walk index and fill lu_page containers with key/record pairs
833 * \param env - is the environment passed by the caller
834 * \param obj - is the index object to parse
835 * \param rdpg - is the lu_rdpg descriptor associated with the transfer
836 * \param filler - is the callback function responsible for filling a lu_page
837 * with key/record pairs in the format wanted by the caller.
838 * If NULL, uses dt_index_page_build
839 * \param arg - is an opaq argument passed to the filler function
841 * \retval sum (in bytes) of all filled lu_pages
842 * \retval -ve errno on failure
844 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
845 const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
849 const struct dt_it_ops *iops;
850 size_t pageidx, bytes, nlupgs = 0;
854 LASSERT(rdpg->rp_pages != NULL);
855 LASSERT(obj->do_index_ops != NULL);
858 filler = dt_index_page_build;
860 bytes = rdpg->rp_count;
864 /* Iterate through index and fill containers from @rdpg */
865 iops = &obj->do_index_ops->dio_it;
866 LASSERT(iops != NULL);
867 it = iops->init(env, obj, rdpg->rp_attrs);
871 rc = iops->load(env, it, rdpg->rp_hash);
874 * Iterator didn't find record with exactly the key requested.
876 * It is currently either
878 * - positioned above record with key less than
879 * requested---skip it.
880 * - or not positioned at all (is in IAM_IT_SKEWED
881 * state)---position it on the next item.
883 rc = iops->next(env, it);
893 * Fill containers one after the other. There might be multiple
894 * containers per physical page.
896 * At this point and across for-loop:
897 * rc == 0 -> ok, proceed.
898 * rc > 0 -> end of index.
901 for (pageidx = 0; rc == 0 && bytes > 0; pageidx++) {
905 LASSERT(pageidx < rdpg->rp_npages);
906 lp = kmap(rdpg->rp_pages[pageidx]);
909 for (i = 0; i < LU_PAGE_COUNT; i++, lp++, bytes-=LU_PAGE_SIZE) {
910 rc = filler(env, obj, lp,
911 min_t(size_t, bytes, LU_PAGE_SIZE),
912 iops, it, rdpg->rp_attrs, arg);
915 /* one more lu_page */
921 kunmap(rdpg->rp_pages[i]);
929 rc = min_t(size_t, nlupgs * LU_PAGE_SIZE, rdpg->rp_count);
933 EXPORT_SYMBOL(dt_index_walk);
936 * Walk key/record pairs of an index and copy them into 4KB containers to be
937 * transferred over the network. This is the common handler for OBD_IDX_READ
940 * \param env - is the environment passed by the caller
941 * \param dev - is the dt_device storing the index
942 * \param ii - is the idx_info structure packed by the client in the
943 * OBD_IDX_READ request
944 * \param rdpg - is the lu_rdpg descriptor
946 * \retval on success, return sum (in bytes) of all filled containers
947 * \retval appropriate error otherwise.
949 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
950 struct idx_info *ii, const struct lu_rdpg *rdpg)
952 const struct dt_index_features *feat;
953 struct dt_object *obj;
958 * rp_count shouldn't be null and should be a multiple of the container
961 if (rdpg->rp_count == 0 || (rdpg->rp_count & (LU_PAGE_SIZE - 1)) != 0)
964 if (!fid_is_quota(&ii->ii_fid) && !fid_is_layout_rbtree(&ii->ii_fid) &&
965 !fid_is_norm(&ii->ii_fid))
968 /* lookup index object subject to the transfer */
969 obj = dt_locate(env, dev, &ii->ii_fid);
971 RETURN(PTR_ERR(obj));
972 if (dt_object_exists(obj) == 0)
973 GOTO(out, rc = -ENOENT);
975 /* fetch index features associated with index object */
976 feat = dt_index_feat_select(fid_seq(&ii->ii_fid),
977 lu_object_attr(&obj->do_lu));
979 GOTO(out, rc = PTR_ERR(feat));
981 /* load index feature if not done already */
982 if (obj->do_index_ops == NULL) {
983 rc = obj->do_ops->do_index_try(env, obj, feat);
988 /* fill ii_flags with supported index features */
989 ii->ii_flags &= (II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
992 if (!(feat->dif_flags & DT_IND_VARKEY))
993 ii->ii_keysize = feat->dif_keysize_max;
995 if (!(feat->dif_flags & DT_IND_VARREC))
996 ii->ii_recsize = feat->dif_recsize_max;
998 if (feat->dif_flags & DT_IND_NONUNQ)
999 /* key isn't necessarily unique */
1000 ii->ii_flags |= II_FL_NONUNQ;
1002 if (!fid_is_layout_rbtree(&ii->ii_fid)) {
1003 dt_read_lock(env, obj, 0);
1004 /* fetch object version before walking the index */
1005 ii->ii_version = dt_version_get(env, obj);
1008 /* walk the index and fill lu_idxpages with key/record pairs */
1009 rc = dt_index_walk(env, obj, rdpg, dt_index_page_build, ii);
1010 if (!fid_is_layout_rbtree(&ii->ii_fid))
1011 dt_read_unlock(env, obj);
1014 /* index is empty */
1015 LASSERT(ii->ii_count == 0);
1016 ii->ii_hash_end = II_END_OFF;
1021 dt_object_put(env, obj);
1024 EXPORT_SYMBOL(dt_index_read);
1026 #ifdef CONFIG_PROC_FS
1027 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v)
1029 struct dt_device *dt = m->private;
1030 struct obd_statfs osfs;
1032 int rc = dt_statfs(NULL, dt, &osfs);
1034 seq_printf(m, "%u\n", (unsigned) osfs.os_bsize);
1037 EXPORT_SYMBOL(lprocfs_dt_blksize_seq_show);
1039 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v)
1041 struct dt_device *dt = m->private;
1042 struct obd_statfs osfs;
1044 int rc = dt_statfs(NULL, dt, &osfs);
1046 __u32 blk_size = osfs.os_bsize >> 10;
1047 __u64 result = osfs.os_blocks;
1049 while (blk_size >>= 1)
1052 seq_printf(m, "%llu\n", result);
1056 EXPORT_SYMBOL(lprocfs_dt_kbytestotal_seq_show);
1058 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v)
1060 struct dt_device *dt = m->private;
1061 struct obd_statfs osfs;
1063 int rc = dt_statfs(NULL, dt, &osfs);
1065 __u32 blk_size = osfs.os_bsize >> 10;
1066 __u64 result = osfs.os_bfree;
1068 while (blk_size >>= 1)
1071 seq_printf(m, "%llu\n", result);
1075 EXPORT_SYMBOL(lprocfs_dt_kbytesfree_seq_show);
1077 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v)
1079 struct dt_device *dt = m->private;
1080 struct obd_statfs osfs;
1082 int rc = dt_statfs(NULL, dt, &osfs);
1084 __u32 blk_size = osfs.os_bsize >> 10;
1085 __u64 result = osfs.os_bavail;
1087 while (blk_size >>= 1)
1090 seq_printf(m, "%llu\n", result);
1094 EXPORT_SYMBOL(lprocfs_dt_kbytesavail_seq_show);
1096 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v)
1098 struct dt_device *dt = m->private;
1099 struct obd_statfs osfs;
1101 int rc = dt_statfs(NULL, dt, &osfs);
1103 seq_printf(m, "%llu\n", osfs.os_files);
1106 EXPORT_SYMBOL(lprocfs_dt_filestotal_seq_show);
1108 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v)
1110 struct dt_device *dt = m->private;
1111 struct obd_statfs osfs;
1113 int rc = dt_statfs(NULL, dt, &osfs);
1115 seq_printf(m, "%llu\n", osfs.os_ffree);
1118 EXPORT_SYMBOL(lprocfs_dt_filesfree_seq_show);
1120 #endif /* CONFIG_PROC_FS */
1122 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
1125 struct dt_device *dt = container_of(kobj, struct dt_device,
1127 struct lu_device *lu = dt2lu_dev(dt);
1132 return sprintf(buf, "%s\n", lu->ld_obd->obd_uuid.uuid);
1134 LUSTRE_RO_ATTR(uuid);
1136 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
1139 struct dt_device *dt = container_of(kobj, struct dt_device,
1141 struct obd_statfs osfs;
1144 rc = dt_statfs(NULL, dt, &osfs);
1148 return sprintf(buf, "%u\n", (unsigned) osfs.os_bsize);
1150 LUSTRE_RO_ATTR(blocksize);
1152 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
1155 struct dt_device *dt = container_of(kobj, struct dt_device,
1157 struct obd_statfs osfs;
1162 rc = dt_statfs(NULL, dt, &osfs);
1166 blk_size = osfs.os_bsize >> 10;
1167 result = osfs.os_blocks;
1169 while (blk_size >>= 1)
1172 return sprintf(buf, "%llu\n", result);
1174 LUSTRE_RO_ATTR(kbytestotal);
1176 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
1179 struct dt_device *dt = container_of(kobj, struct dt_device,
1181 struct obd_statfs osfs;
1186 rc = dt_statfs(NULL, dt, &osfs);
1190 blk_size = osfs.os_bsize >> 10;
1191 result = osfs.os_bfree;
1193 while (blk_size >>= 1)
1196 return sprintf(buf, "%llu\n", result);
1198 LUSTRE_RO_ATTR(kbytesfree);
1200 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
1203 struct dt_device *dt = container_of(kobj, struct dt_device,
1205 struct obd_statfs osfs;
1210 rc = dt_statfs(NULL, dt, &osfs);
1214 blk_size = osfs.os_bsize >> 10;
1215 result = osfs.os_bavail;
1217 while (blk_size >>= 1)
1220 return sprintf(buf, "%llu\n", result);
1222 LUSTRE_RO_ATTR(kbytesavail);
1224 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
1227 struct dt_device *dt = container_of(kobj, struct dt_device,
1229 struct obd_statfs osfs;
1232 rc = dt_statfs(NULL, dt, &osfs);
1236 return sprintf(buf, "%llu\n", osfs.os_files);
1238 LUSTRE_RO_ATTR(filestotal);
1240 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
1243 struct dt_device *dt = container_of(kobj, struct dt_device,
1245 struct obd_statfs osfs;
1248 rc = dt_statfs(NULL, dt, &osfs);
1252 return sprintf(buf, "%llu\n", osfs.os_ffree);
1254 LUSTRE_RO_ATTR(filesfree);
1256 static const struct attribute *dt_def_attrs[] = {
1257 &lustre_attr_uuid.attr,
1258 &lustre_attr_blocksize.attr,
1259 &lustre_attr_kbytestotal.attr,
1260 &lustre_attr_kbytesfree.attr,
1261 &lustre_attr_kbytesavail.attr,
1262 &lustre_attr_filestotal.attr,
1263 &lustre_attr_filesfree.attr,
1267 static void dt_sysfs_release(struct kobject *kobj)
1269 struct dt_device *dt = container_of(kobj, struct dt_device,
1272 debugfs_remove_recursive(dt->dd_debugfs_entry);
1273 dt->dd_debugfs_entry = NULL;
1275 complete(&dt->dd_kobj_unregister);
1278 int dt_tunables_fini(struct dt_device *dt)
1283 if (dt->dd_def_attrs) {
1284 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1285 kobject_put(&dt->dd_kobj);
1286 wait_for_completion(&dt->dd_kobj_unregister);
1291 EXPORT_SYMBOL(dt_tunables_fini);
1293 int dt_tunables_init(struct dt_device *dt, struct obd_type *type,
1294 const char *name, struct ldebugfs_vars *list)
1298 dt->dd_ktype.sysfs_ops = &lustre_sysfs_ops;
1299 dt->dd_ktype.release = dt_sysfs_release;
1301 init_completion(&dt->dd_kobj_unregister);
1302 rc = kobject_init_and_add(&dt->dd_kobj, &dt->dd_ktype, &type->typ_kobj,
1307 dt->dd_def_attrs = dt_def_attrs;
1309 rc = sysfs_create_files(&dt->dd_kobj, dt->dd_def_attrs);
1311 kobject_put(&dt->dd_kobj);
1312 dt->dd_def_attrs = NULL;
1317 * No need to register debugfs if no enteries. This allows us to
1318 * choose between using dt_device or obd_device for debugfs.
1323 dt->dd_debugfs_entry = debugfs_create_dir(name,
1324 type->typ_debugfs_entry);
1325 ldebugfs_add_vars(dt->dd_debugfs_entry, list, dt);
1329 EXPORT_SYMBOL(dt_tunables_init);