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.
33 #ifndef __LUSTRE_LU_OBJECT_H
34 #define __LUSTRE_LU_OBJECT_H
37 #include <libcfs/libcfs.h>
38 #include <uapi/linux/lustre/lustre_idl.h>
40 #include <linux/percpu_counter.h>
41 #include <linux/ctype.h>
42 #include <obd_target.h>
45 struct proc_dir_entry;
51 * lu_* data-types represent server-side entities shared by data and meta-data
56 * -# support for layering.
58 * Server side object is split into layers, one per device in the
59 * corresponding device stack. Individual layer is represented by struct
60 * lu_object. Compound layered object --- by struct lu_object_header. Most
61 * interface functions take lu_object as an argument and operate on the
62 * whole compound object. This decision was made due to the following
65 * - it's envisaged that lu_object will be used much more often than
68 * - we want lower (non-top) layers to be able to initiate operations
69 * on the whole object.
71 * Generic code supports layering more complex than simple stacking, e.g.,
72 * it is possible that at some layer object "spawns" multiple sub-objects
75 * -# fid-based identification.
77 * Compound object is uniquely identified by its fid. Objects are indexed
78 * by their fids (hash table is used for index).
80 * -# caching and life-cycle management.
82 * Object's life-time is controlled by reference counting. When reference
83 * count drops to 0, object is returned to cache. Cached objects still
84 * retain their identity (i.e., fid), and can be recovered from cache.
86 * Objects are kept in the global LRU list, and lu_site_purge() function
87 * can be used to reclaim given number of unused objects from the tail of
90 * -# avoiding recursion.
92 * Generic code tries to replace recursion through layers by iterations
93 * where possible. Additionally to the end of reducing stack consumption,
94 * data, when practically possible, are allocated through lu_context_key
95 * interface rather than on stack.
102 struct lu_object_header;
108 * Operations common for data and meta-data devices.
110 struct lu_device_operations {
112 * Allocate object for the given device (without lower-layer
113 * parts). This is called by lu_object_operations::loo_object_init()
114 * from the parent layer, and should setup at least lu_object::lo_dev
115 * and lu_object::lo_ops fields of resulting lu_object.
117 * Object creation protocol.
119 * Due to design goal of avoiding recursion, object creation (see
120 * lu_object_alloc()) is somewhat involved:
122 * - first, lu_device_operations::ldo_object_alloc() method of the
123 * top-level device in the stack is called. It should allocate top
124 * level object (including lu_object_header), but without any
125 * lower-layer sub-object(s).
127 * - then lu_object_alloc() sets fid in the header of newly created
130 * - then lu_object_operations::loo_object_init() is called. It has
131 * to allocate lower-layer object(s). To do this,
132 * lu_object_operations::loo_object_init() calls ldo_object_alloc()
133 * of the lower-layer device(s).
135 * - for all new objects allocated by
136 * lu_object_operations::loo_object_init() (and inserted into object
137 * stack), lu_object_operations::loo_object_init() is called again
138 * repeatedly, until no new objects are created.
140 * \post ergo(!IS_ERR(result), result->lo_dev == d &&
141 * result->lo_ops != NULL);
143 struct lu_object *(*ldo_object_alloc)(const struct lu_env *env,
144 const struct lu_object_header *h,
145 struct lu_device *d);
147 * process config specific for device.
149 int (*ldo_process_config)(const struct lu_env *env,
150 struct lu_device *, struct lustre_cfg *);
151 int (*ldo_recovery_complete)(const struct lu_env *,
155 * initialize local objects for device. this method called after layer has
156 * been initialized (after LCFG_SETUP stage) and before it starts serving
160 int (*ldo_prepare)(const struct lu_env *,
161 struct lu_device *parent,
162 struct lu_device *dev);
166 * Allocate new FID for file with @name under @parent
168 * \param[in] env execution environment for this thread
169 * \param[in] dev dt device
170 * \param[out] fid new FID allocated
171 * \param[in] parent parent object
172 * \param[in] name lu_name
174 * \retval 0 on success
175 * \retval 0 0 FID allocated successfully.
176 * \retval 1 1 FID allocated successfully and new sequence
177 * requested from seq meta server
178 * \retval negative negative errno if FID allocation failed.
180 int (*ldo_fid_alloc)(const struct lu_env *env,
181 struct lu_device *dev,
183 struct lu_object *parent,
184 const struct lu_name *name);
188 * For lu_object_conf flags
191 /* This is a new object to be allocated, or the file
192 * corresponding to the object does not exists. */
193 LOC_F_NEW = 0x00000001,
197 * Object configuration, describing particulars of object being created. On
198 * server this is not used, as server objects are full identified by fid. On
199 * client configuration contains struct lustre_md.
201 struct lu_object_conf {
203 * Some hints for obj find and alloc.
205 loc_flags_t loc_flags;
209 * Type of "printer" function used by lu_object_operations::loo_object_print()
212 * Printer function is needed to provide some flexibility in (semi-)debugging
213 * output: possible implementations: printk, CDEBUG, sysfs/seq_file
215 typedef int (*lu_printer_t)(const struct lu_env *env,
216 void *cookie, const char *format, ...)
217 __attribute__ ((format (printf, 3, 4)));
220 * Operations specific for particular lu_object.
222 struct lu_object_operations {
225 * Allocate lower-layer parts of the object by calling
226 * lu_device_operations::ldo_object_alloc() of the corresponding
229 * This method is called once for each object inserted into object
230 * stack. It's responsibility of this method to insert lower-layer
231 * object(s) it create into appropriate places of object stack.
233 int (*loo_object_init)(const struct lu_env *env,
235 const struct lu_object_conf *conf);
237 * Called (in top-to-bottom order) during object allocation after all
238 * layers were allocated and initialized. Can be used to perform
239 * initialization depending on lower layers.
241 int (*loo_object_start)(const struct lu_env *env,
242 struct lu_object *o);
244 * Called before lu_object_operations::loo_object_free() to signal
245 * that object is being destroyed. Dual to
246 * lu_object_operations::loo_object_init().
248 void (*loo_object_delete)(const struct lu_env *env,
249 struct lu_object *o);
251 * Dual to lu_device_operations::ldo_object_alloc(). Called when
252 * object is removed from memory. Must use call_rcu or kfree_rcu
253 * if the object contains an lu_object_header.
255 void (*loo_object_free)(const struct lu_env *env,
256 struct lu_object *o);
258 * Called when last active reference to the object is released (and
259 * object returns to the cache). This method is optional.
261 void (*loo_object_release)(const struct lu_env *env,
262 struct lu_object *o);
264 * Optional debugging helper. Print given object.
266 int (*loo_object_print)(const struct lu_env *env, void *cookie,
267 lu_printer_t p, const struct lu_object *o);
269 * Optional debugging method. Returns true iff method is internally
272 int (*loo_object_invariant)(const struct lu_object *o);
278 struct lu_device_type;
281 * Device: a layer in the server side abstraction stacking.
285 * reference count. This is incremented, in particular, on each object
286 * created at this layer.
288 * \todo XXX which means that atomic_t is probably too small.
292 * Pointer to device type. Never modified once set.
294 struct lu_device_type *ld_type;
296 * Operation vector for this device.
298 const struct lu_device_operations *ld_ops;
300 * Stack this device belongs to.
302 struct lu_site *ld_site;
303 struct proc_dir_entry *ld_proc_entry;
305 /** \todo XXX: temporary back pointer into obd. */
306 struct obd_device *ld_obd;
308 * A list of references to this object, for debugging.
310 struct lu_ref ld_reference;
312 * Link the device to the site.
314 struct list_head ld_linkage;
317 struct lu_device_type_operations;
320 * Tag bits for device type. They are used to distinguish certain groups of
324 /** this is meta-data device */
325 LU_DEVICE_MD = (1 << 0),
326 /** this is data device */
327 LU_DEVICE_DT = (1 << 1),
328 /** data device in the client stack */
329 LU_DEVICE_CL = (1 << 2)
335 struct lu_device_type {
337 * Tag bits. Taken from enum lu_device_tag. Never modified once set.
341 * Name of this class. Unique system-wide. Never modified once set.
345 * Operations for this type.
347 const struct lu_device_type_operations *ldt_ops;
349 * \todo XXX: temporary: context tags used by obd_*() calls.
353 * Number of existing device type instances.
355 atomic_t ldt_device_nr;
359 * Operations on a device type.
361 struct lu_device_type_operations {
363 * Allocate new device.
365 struct lu_device *(*ldto_device_alloc)(const struct lu_env *env,
366 struct lu_device_type *t,
367 struct lustre_cfg *lcfg);
369 * Free device. Dual to
370 * lu_device_type_operations::ldto_device_alloc(). Returns pointer to
371 * the next device in the stack.
373 struct lu_device *(*ldto_device_free)(const struct lu_env *,
377 * Initialize the devices after allocation
379 int (*ldto_device_init)(const struct lu_env *env,
380 struct lu_device *, const char *,
383 * Finalize device. Dual to
384 * lu_device_type_operations::ldto_device_init(). Returns pointer to
385 * the next device in the stack.
387 struct lu_device *(*ldto_device_fini)(const struct lu_env *env,
390 * Initialize device type. This is called on module load.
392 int (*ldto_init)(struct lu_device_type *t);
394 * Finalize device type. Dual to
395 * lu_device_type_operations::ldto_init(). Called on module unload.
397 void (*ldto_fini)(struct lu_device_type *t);
399 * Called when the first device is created.
401 void (*ldto_start)(struct lu_device_type *t);
403 * Called when number of devices drops to 0.
405 void (*ldto_stop)(struct lu_device_type *t);
408 static inline int lu_device_is_md(const struct lu_device *d)
410 return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_MD);
414 * Common object attributes.
425 /** modification time in seconds since Epoch */
427 /** access time in seconds since Epoch */
429 /** change time in seconds since Epoch */
431 /** create time in seconds since Epoch */
433 /** 512-byte blocks allocated to object */
435 /** permission bits and file type */
443 /** number of persistent references to this object */
445 /** blk bits of the object*/
447 /** blk size of the object*/
453 /** set layout version to OST objects. */
454 __u32 la_layout_version;
458 * Layer in the layered object.
462 * Header for this object.
464 struct lu_object_header *lo_header;
466 * Device for this layer.
468 struct lu_device *lo_dev;
470 * Operations for this object.
472 const struct lu_object_operations *lo_ops;
474 * Linkage into list of all layers.
476 struct list_head lo_linkage;
478 * Link to the device, for debugging.
480 struct lu_ref_link lo_dev_ref;
483 enum lu_object_header_flags {
485 * Don't keep this object in cache. Object will be destroyed as soon
486 * as last reference to it is released. This flag cannot be cleared
489 LU_OBJECT_HEARD_BANSHEE = 0,
491 * Mark this object has already been taken out of cache.
493 LU_OBJECT_UNHASHED = 1,
495 * Object is initialized, when object is found in cache, it may not be
496 * intialized yet, the object allocator will initialize it.
498 LU_OBJECT_INITED = 2,
500 * Object is being purged, so mustn't be returned by
503 LU_OBJECT_PURGING = 3,
506 enum lu_object_header_attr {
507 LOHA_EXISTS = 1 << 0,
508 LOHA_REMOTE = 1 << 1,
509 LOHA_HAS_AGENT_ENTRY = 1 << 2,
511 * UNIX file type is stored in S_IFMT bits.
513 LOHA_FT_START = 001 << 12, /**< S_IFIFO */
514 LOHA_FT_END = 017 << 12, /**< S_IFMT */
518 * "Compound" object, consisting of multiple layers.
520 * Compound object with given fid is unique with given lu_site.
522 * Note, that object does *not* necessary correspond to the real object in the
523 * persistent storage: object is an anchor for locking and method calling, so
524 * it is created for things like not-yet-existing child created by mkdir or
525 * create calls. lu_object_operations::loo_exists() can be used to check
526 * whether object is backed by persistent storage entity.
528 struct lu_object_header {
530 * Fid, uniquely identifying this object.
532 struct lu_fid loh_fid;
534 * Object flags from enum lu_object_header_flags. Set and checked
537 unsigned long loh_flags;
539 * Object reference count. Protected by lu_site::ls_guard.
543 * Common object attributes, cached for efficiency. From enum
544 * lu_object_header_attr.
548 * Linkage into per-site hash table. Protected by lu_site::ls_guard.
550 struct hlist_node loh_hash;
552 * Linkage into per-site LRU list. Protected by lu_site::ls_guard.
554 struct list_head loh_lru;
556 * Linkage into list of layers. Never modified once set (except lately
557 * during object destruction). No locking is necessary.
559 struct list_head loh_layers;
561 * A list of references to this object, for debugging.
563 struct lu_ref loh_reference;
565 * Handle used for kfree_rcu() or similar.
567 struct rcu_head loh_rcu;
577 LU_SS_CACHE_DEATH_RACE,
583 * lu_site is a "compartment" within which objects are unique, and LRU
584 * discipline is maintained.
586 * lu_site exists so that multiple layered stacks can co-exist in the same
589 * lu_site has the same relation to lu_device as lu_object_header to
596 struct cfs_hash *ls_obj_hash;
598 * buckets for summary data
600 struct lu_site_bkt_data *ls_bkts;
604 * index of bucket on hash table while purging
606 unsigned int ls_purge_start;
608 * Top-level device for this stack.
610 struct lu_device *ls_top_dev;
612 * Bottom-level device for this stack
614 struct lu_device *ls_bottom_dev;
616 * Linkage into global list of sites.
618 struct list_head ls_linkage;
620 * List for lu device for this site, protected
623 struct list_head ls_ld_linkage;
624 spinlock_t ls_ld_lock;
626 * Lock to serialize site purge.
628 struct mutex ls_purge_mutex;
632 struct lprocfs_stats *ls_stats;
634 * XXX: a hack! fld has to find md_site via site, remove when possible
636 struct seq_server_site *ld_seq_site;
638 * Pointer to the lu_target for this site.
640 struct lu_target *ls_tgt;
643 * Number of objects in lsb_lru_lists - used for shrinking
645 struct percpu_counter ls_lru_len_counter;
649 lu_site_wq_from_fid(struct lu_site *site, struct lu_fid *fid);
651 static inline struct seq_server_site *lu_site2seq(const struct lu_site *s)
653 return s->ld_seq_site;
657 * Constructors/destructors.
661 int lu_site_init (struct lu_site *s, struct lu_device *d);
662 void lu_site_fini (struct lu_site *s);
663 int lu_site_init_finish (struct lu_site *s);
664 void lu_stack_fini (const struct lu_env *env, struct lu_device *top);
665 void lu_device_get (struct lu_device *d);
666 void lu_device_put (struct lu_device *d);
667 int lu_device_init (struct lu_device *d, struct lu_device_type *t);
668 void lu_device_fini (struct lu_device *d);
669 int lu_object_header_init(struct lu_object_header *h);
670 void lu_object_header_fini(struct lu_object_header *h);
671 int lu_object_init (struct lu_object *o,
672 struct lu_object_header *h, struct lu_device *d);
673 void lu_object_fini (struct lu_object *o);
674 void lu_object_add_top (struct lu_object_header *h, struct lu_object *o);
675 void lu_object_add (struct lu_object *before, struct lu_object *o);
677 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d);
678 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d);
681 * Helpers to initialize and finalize device types.
684 int lu_device_type_init(struct lu_device_type *ldt);
685 void lu_device_type_fini(struct lu_device_type *ldt);
690 * Caching and reference counting.
695 * Acquire additional reference to the given object. This function is used to
696 * attain additional reference. To acquire initial reference use
699 static inline void lu_object_get(struct lu_object *o)
701 LASSERT(atomic_read(&o->lo_header->loh_ref) > 0);
702 atomic_inc(&o->lo_header->loh_ref);
706 * Return true if object will not be cached after last reference to it is
709 static inline int lu_object_is_dying(const struct lu_object_header *h)
711 return test_bit(LU_OBJECT_HEARD_BANSHEE, &h->loh_flags);
715 * Return true if object is initialized.
717 static inline int lu_object_is_inited(const struct lu_object_header *h)
719 return test_bit(LU_OBJECT_INITED, &h->loh_flags);
722 void lu_object_put(const struct lu_env *env, struct lu_object *o);
723 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o);
724 void lu_object_unhash(const struct lu_env *env, struct lu_object *o);
725 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s, int nr,
728 static inline int lu_site_purge(const struct lu_env *env, struct lu_site *s,
731 return lu_site_purge_objects(env, s, nr, 1);
734 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
735 lu_printer_t printer);
736 struct lu_object *lu_object_find(const struct lu_env *env,
737 struct lu_device *dev, const struct lu_fid *f,
738 const struct lu_object_conf *conf);
739 struct lu_object *lu_object_find_at(const struct lu_env *env,
740 struct lu_device *dev,
741 const struct lu_fid *f,
742 const struct lu_object_conf *conf);
743 struct lu_object *lu_object_find_slice(const struct lu_env *env,
744 struct lu_device *dev,
745 const struct lu_fid *f,
746 const struct lu_object_conf *conf);
755 * First (topmost) sub-object of given compound object
757 static inline struct lu_object *lu_object_top(struct lu_object_header *h)
759 LASSERT(!list_empty(&h->loh_layers));
760 return container_of0(h->loh_layers.next, struct lu_object, lo_linkage);
764 * Next sub-object in the layering
766 static inline struct lu_object *lu_object_next(const struct lu_object *o)
768 return container_of0(o->lo_linkage.next, struct lu_object, lo_linkage);
772 * Pointer to the fid of this object.
774 static inline const struct lu_fid *lu_object_fid(const struct lu_object *o)
776 return &o->lo_header->loh_fid;
780 * return device operations vector for this object
782 static const inline struct lu_device_operations *
783 lu_object_ops(const struct lu_object *o)
785 return o->lo_dev->ld_ops;
789 * Given a compound object, find its slice, corresponding to the device type
792 struct lu_object *lu_object_locate(struct lu_object_header *h,
793 const struct lu_device_type *dtype);
796 * Printer function emitting messages through libcfs_debug_msg().
798 int lu_cdebug_printer(const struct lu_env *env,
799 void *cookie, const char *format, ...);
802 * Print object description followed by a user-supplied message.
804 #define LU_OBJECT_DEBUG(mask, env, object, format, ...) \
806 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
807 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, NULL); \
808 lu_object_print(env, &msgdata, lu_cdebug_printer, object);\
809 CDEBUG(mask, format "\n", ## __VA_ARGS__); \
814 * Print short object description followed by a user-supplied message.
816 #define LU_OBJECT_HEADER(mask, env, object, format, ...) \
818 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
819 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, NULL); \
820 lu_object_header_print(env, &msgdata, lu_cdebug_printer,\
821 (object)->lo_header); \
822 lu_cdebug_printer(env, &msgdata, "\n"); \
823 CDEBUG(mask, format , ## __VA_ARGS__); \
827 void lu_object_print (const struct lu_env *env, void *cookie,
828 lu_printer_t printer, const struct lu_object *o);
829 void lu_object_header_print(const struct lu_env *env, void *cookie,
830 lu_printer_t printer,
831 const struct lu_object_header *hdr);
834 * Check object consistency.
836 int lu_object_invariant(const struct lu_object *o);
840 * Check whether object exists, no matter on local or remote storage.
841 * Note: LOHA_EXISTS will be set once some one created the object,
842 * and it does not needs to be committed to storage.
844 #define lu_object_exists(o) ((o)->lo_header->loh_attr & LOHA_EXISTS)
847 * Check whether object on the remote storage.
849 #define lu_object_remote(o) unlikely((o)->lo_header->loh_attr & LOHA_REMOTE)
852 * Check whether the object as agent entry on current target
854 #define lu_object_has_agent_entry(o) \
855 unlikely((o)->lo_header->loh_attr & LOHA_HAS_AGENT_ENTRY)
857 static inline void lu_object_set_agent_entry(struct lu_object *o)
859 o->lo_header->loh_attr |= LOHA_HAS_AGENT_ENTRY;
862 static inline void lu_object_clear_agent_entry(struct lu_object *o)
864 o->lo_header->loh_attr &= ~LOHA_HAS_AGENT_ENTRY;
867 static inline int lu_object_assert_exists(const struct lu_object *o)
869 return lu_object_exists(o);
872 static inline int lu_object_assert_not_exists(const struct lu_object *o)
874 return !lu_object_exists(o);
878 * Attr of this object.
880 static inline __u32 lu_object_attr(const struct lu_object *o)
882 LASSERT(lu_object_exists(o) != 0);
884 return o->lo_header->loh_attr & S_IFMT;
887 static inline void lu_object_ref_add(struct lu_object *o,
891 lu_ref_add(&o->lo_header->loh_reference, scope, source);
894 static inline void lu_object_ref_add_at(struct lu_object *o,
895 struct lu_ref_link *link,
899 lu_ref_add_at(&o->lo_header->loh_reference, link, scope, source);
902 static inline void lu_object_ref_del(struct lu_object *o,
903 const char *scope, const void *source)
905 lu_ref_del(&o->lo_header->loh_reference, scope, source);
908 static inline void lu_object_ref_del_at(struct lu_object *o,
909 struct lu_ref_link *link,
910 const char *scope, const void *source)
912 lu_ref_del_at(&o->lo_header->loh_reference, link, scope, source);
915 /** input params, should be filled out by mdt */
919 /** count in bytes */
920 unsigned int rp_count;
921 /** number of pages */
922 unsigned int rp_npages;
923 /** requested attr */
925 /** pointers to pages */
926 struct page **rp_pages;
929 enum lu_xattr_flags {
930 LU_XATTR_REPLACE = (1 << 0),
931 LU_XATTR_CREATE = (1 << 1),
932 LU_XATTR_MERGE = (1 << 2),
933 LU_XATTR_SPLIT = (1 << 3),
941 /** For lu_context health-checks */
942 enum lu_context_state {
951 * lu_context. Execution context for lu_object methods. Currently associated
954 * All lu_object methods, except device and device type methods (called during
955 * system initialization and shutdown) are executed "within" some
956 * lu_context. This means, that pointer to some "current" lu_context is passed
957 * as an argument to all methods.
959 * All service ptlrpc threads create lu_context as part of their
960 * initialization. It is possible to create "stand-alone" context for other
961 * execution environments (like system calls).
963 * lu_object methods mainly use lu_context through lu_context_key interface
964 * that allows each layer to associate arbitrary pieces of data with each
965 * context (see pthread_key_create(3) for similar interface).
967 * On a client, lu_context is bound to a thread, see cl_env_get().
969 * \see lu_context_key
973 * lu_context is used on the client side too. Yet we don't want to
974 * allocate values of server-side keys for the client contexts and
977 * To achieve this, set of tags in introduced. Contexts and keys are
978 * marked with tags. Key value are created only for context whose set
979 * of tags has non-empty intersection with one for key. Tags are taken
980 * from enum lu_context_tag.
983 enum lu_context_state lc_state;
985 * Pointer to the home service thread. NULL for other execution
988 struct ptlrpc_thread *lc_thread;
990 * Pointer to an array with key values. Internal implementation
995 * Linkage into a list of all remembered contexts. Only
996 * `non-transient' contexts, i.e., ones created for service threads
999 struct list_head lc_remember;
1001 * Version counter used to skip calls to lu_context_refill() when no
1002 * keys were registered.
1004 unsigned lc_version;
1012 * lu_context_key interface. Similar to pthread_key.
1015 enum lu_context_tag {
1017 * Thread on md server
1019 LCT_MD_THREAD = 1 << 0,
1021 * Thread on dt server
1023 LCT_DT_THREAD = 1 << 1,
1027 LCT_CL_THREAD = 1 << 3,
1029 * A per-request session on a server, and a per-system-call session on
1032 LCT_SESSION = 1 << 4,
1034 * A per-request data on OSP device
1036 LCT_OSP_THREAD = 1 << 5,
1040 LCT_MG_THREAD = 1 << 6,
1042 * Context for local operations
1046 * session for server thread
1048 LCT_SERVER_SESSION = 1 << 8,
1050 * Set when at least one of keys, having values in this context has
1051 * non-NULL lu_context_key::lct_exit() method. This is used to
1052 * optimize lu_context_exit() call.
1054 LCT_HAS_EXIT = 1 << 28,
1056 * Don't add references for modules creating key values in that context.
1057 * This is only for contexts used internally by lu_object framework.
1059 LCT_NOREF = 1 << 29,
1061 * Key is being prepared for retiring, don't create new values for it.
1063 LCT_QUIESCENT = 1 << 30,
1065 * Context should be remembered.
1067 LCT_REMEMBER = 1 << 31,
1069 * Contexts usable in cache shrinker thread.
1071 LCT_SHRINKER = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD|LCT_NOREF
1075 * Key. Represents per-context value slot.
1077 * Keys are usually registered when module owning the key is initialized, and
1078 * de-registered when module is unloaded. Once key is registered, all new
1079 * contexts with matching tags, will get key value. "Old" contexts, already
1080 * initialized at the time of key registration, can be forced to get key value
1081 * by calling lu_context_refill().
1083 * Every key value is counted in lu_context_key::lct_used and acquires a
1084 * reference on an owning module. This means, that all key values have to be
1085 * destroyed before module can be unloaded. This is usually achieved by
1086 * stopping threads started by the module, that created contexts in their
1087 * entry functions. Situation is complicated by the threads shared by multiple
1088 * modules, like ptlrpcd daemon on a client. To work around this problem,
1089 * contexts, created in such threads, are `remembered' (see
1090 * LCT_REMEMBER)---i.e., added into a global list. When module is preparing
1091 * for unloading it does the following:
1093 * - marks its keys as `quiescent' (lu_context_tag::LCT_QUIESCENT)
1094 * preventing new key values from being allocated in the new contexts,
1097 * - scans a list of remembered contexts, destroying values of module
1098 * keys, thus releasing references to the module.
1100 * This is done by lu_context_key_quiesce(). If module is re-activated
1101 * before key has been de-registered, lu_context_key_revive() call clears
1102 * `quiescent' marker.
1104 * lu_context code doesn't provide any internal synchronization for these
1105 * activities---it's assumed that startup (including threads start-up) and
1106 * shutdown are serialized by some external means.
1110 struct lu_context_key {
1112 * Set of tags for which values of this key are to be instantiated.
1116 * Value constructor. This is called when new value is created for a
1117 * context. Returns pointer to new value of error pointer.
1119 void *(*lct_init)(const struct lu_context *ctx,
1120 struct lu_context_key *key);
1122 * Value destructor. Called when context with previously allocated
1123 * value of this slot is destroyed. \a data is a value that was returned
1124 * by a matching call to lu_context_key::lct_init().
1126 void (*lct_fini)(const struct lu_context *ctx,
1127 struct lu_context_key *key, void *data);
1129 * Optional method called on lu_context_exit() for all allocated
1130 * keys. Can be used by debugging code checking that locks are
1133 void (*lct_exit)(const struct lu_context *ctx,
1134 struct lu_context_key *key, void *data);
1136 * Internal implementation detail: index within lu_context::lc_value[]
1137 * reserved for this key.
1141 * Internal implementation detail: number of values created for this
1146 * Internal implementation detail: module for this key.
1148 struct module *lct_owner;
1150 * References to this key. For debugging.
1152 struct lu_ref lct_reference;
1155 #define LU_KEY_INIT(mod, type) \
1156 static void *mod##_key_init(const struct lu_context *ctx, \
1157 struct lu_context_key *key) \
1161 BUILD_BUG_ON(PAGE_SIZE < sizeof(*value)); \
1163 OBD_ALLOC_PTR(value); \
1164 if (value == NULL) \
1165 value = ERR_PTR(-ENOMEM); \
1169 struct __##mod##__dummy_init { ; } /* semicolon catcher */
1171 #define LU_KEY_FINI(mod, type) \
1172 static void mod##_key_fini(const struct lu_context *ctx, \
1173 struct lu_context_key *key, void* data) \
1175 type *info = data; \
1177 OBD_FREE_PTR(info); \
1179 struct __##mod##__dummy_fini {;} /* semicolon catcher */
1181 #define LU_KEY_INIT_FINI(mod, type) \
1182 LU_KEY_INIT(mod,type); \
1183 LU_KEY_FINI(mod,type)
1185 #define LU_CONTEXT_KEY_DEFINE(mod, tags) \
1186 struct lu_context_key mod##_thread_key = { \
1188 .lct_init = mod##_key_init, \
1189 .lct_fini = mod##_key_fini \
1192 #define LU_CONTEXT_KEY_INIT(key) \
1194 (key)->lct_owner = THIS_MODULE; \
1197 int lu_context_key_register(struct lu_context_key *key);
1198 void lu_context_key_degister(struct lu_context_key *key);
1199 void *lu_context_key_get (const struct lu_context *ctx,
1200 const struct lu_context_key *key);
1201 void lu_context_key_quiesce (struct lu_context_key *key);
1202 void lu_context_key_revive (struct lu_context_key *key);
1206 * LU_KEY_INIT_GENERIC() has to be a macro to correctly determine an
1210 #define LU_KEY_INIT_GENERIC(mod) \
1211 static void mod##_key_init_generic(struct lu_context_key *k, ...) \
1213 struct lu_context_key *key = k; \
1216 va_start(args, k); \
1218 LU_CONTEXT_KEY_INIT(key); \
1219 key = va_arg(args, struct lu_context_key *); \
1220 } while (key != NULL); \
1224 #define LU_TYPE_INIT(mod, ...) \
1225 LU_KEY_INIT_GENERIC(mod) \
1226 static int mod##_type_init(struct lu_device_type *t) \
1228 mod##_key_init_generic(__VA_ARGS__, NULL); \
1229 return lu_context_key_register_many(__VA_ARGS__, NULL); \
1231 struct __##mod##_dummy_type_init {;}
1233 #define LU_TYPE_FINI(mod, ...) \
1234 static void mod##_type_fini(struct lu_device_type *t) \
1236 lu_context_key_degister_many(__VA_ARGS__, NULL); \
1238 struct __##mod##_dummy_type_fini {;}
1240 #define LU_TYPE_START(mod, ...) \
1241 static void mod##_type_start(struct lu_device_type *t) \
1243 lu_context_key_revive_many(__VA_ARGS__, NULL); \
1245 struct __##mod##_dummy_type_start {;}
1247 #define LU_TYPE_STOP(mod, ...) \
1248 static void mod##_type_stop(struct lu_device_type *t) \
1250 lu_context_key_quiesce_many(__VA_ARGS__, NULL); \
1252 struct __##mod##_dummy_type_stop {;}
1256 #define LU_TYPE_INIT_FINI(mod, ...) \
1257 LU_TYPE_INIT(mod, __VA_ARGS__); \
1258 LU_TYPE_FINI(mod, __VA_ARGS__); \
1259 LU_TYPE_START(mod, __VA_ARGS__); \
1260 LU_TYPE_STOP(mod, __VA_ARGS__)
1262 int lu_context_init (struct lu_context *ctx, __u32 tags);
1263 void lu_context_fini (struct lu_context *ctx);
1264 void lu_context_enter (struct lu_context *ctx);
1265 void lu_context_exit (struct lu_context *ctx);
1266 int lu_context_refill(struct lu_context *ctx);
1269 * Helper functions to operate on multiple keys. These are used by the default
1270 * device type operations, defined by LU_TYPE_INIT_FINI().
1273 int lu_context_key_register_many(struct lu_context_key *k, ...);
1274 void lu_context_key_degister_many(struct lu_context_key *k, ...);
1275 void lu_context_key_revive_many (struct lu_context_key *k, ...);
1276 void lu_context_key_quiesce_many (struct lu_context_key *k, ...);
1279 * update/clear ctx/ses tags.
1281 void lu_context_tags_update(__u32 tags);
1282 void lu_context_tags_clear(__u32 tags);
1283 void lu_session_tags_update(__u32 tags);
1284 void lu_session_tags_clear(__u32 tags);
1291 * "Local" context, used to store data instead of stack.
1293 struct lu_context le_ctx;
1295 * "Session" context for per-request data.
1297 struct lu_context *le_ses;
1300 int lu_env_init (struct lu_env *env, __u32 tags);
1301 void lu_env_fini (struct lu_env *env);
1302 int lu_env_refill(struct lu_env *env);
1303 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags, __u32 stags);
1305 static inline void* lu_env_info(const struct lu_env *env,
1306 const struct lu_context_key *key)
1309 info = lu_context_key_get(&env->le_ctx, key);
1311 if (!lu_env_refill((struct lu_env *)env))
1312 info = lu_context_key_get(&env->le_ctx, key);
1318 struct lu_env *lu_env_find(void);
1319 int lu_env_add(struct lu_env *env);
1320 int lu_env_add_task(struct lu_env *env, struct task_struct *task);
1321 void lu_env_remove(struct lu_env *env);
1323 /** @} lu_context */
1326 * Output site statistical counters into a buffer. Suitable for
1327 * ll_rd_*()-style functions.
1329 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m);
1332 * Common name structure to be passed around for various name related methods.
1335 const char *ln_name;
1339 static inline bool name_is_dot_or_dotdot(const char *name, int namelen)
1341 return name[0] == '.' &&
1342 (namelen == 1 || (namelen == 2 && name[1] == '.'));
1345 static inline bool lu_name_is_dot_or_dotdot(const struct lu_name *lname)
1347 return name_is_dot_or_dotdot(lname->ln_name, lname->ln_namelen);
1350 static inline bool lu_name_is_temp_file(const char *name, int namelen,
1351 bool dot_prefix, int suffixlen)
1356 int len = suffixlen;
1358 if (dot_prefix && name[0] != '.')
1361 if (namelen < dot_prefix + suffixlen + 2 ||
1362 name[namelen - suffixlen - 1] != '.')
1366 lower += islower(name[namelen - len]);
1367 upper += isupper(name[namelen - len]);
1368 digit += isdigit(name[namelen - len]);
1371 /* mktemp() filename suffixes will have a mix of upper- and lower-case
1372 * letters and/or numbers, not all numbers, or all upper or lower-case.
1373 * About 0.07% of randomly-generated names will slip through,
1374 * but this avoids 99.93% of cross-MDT renames for those files.
1376 if (digit >= suffixlen - 2 || upper == suffixlen || lower == suffixlen)
1382 static inline bool lu_name_is_backup_file(const char *name, int namelen,
1386 name[namelen - 2] != '.' && name[namelen - 1] == '~') {
1392 if (namelen > 4 && name[namelen - 4] == '.' &&
1393 (!strncasecmp(name + namelen - 3, "bak", 3) ||
1394 !strncasecmp(name + namelen - 3, "sav", 3))) {
1400 if (namelen > 5 && name[namelen - 5] == '.' &&
1401 !strncasecmp(name + namelen - 4, "orig", 4)) {
1410 static inline bool lu_name_is_valid_len(const char *name, size_t name_len)
1412 return name != NULL &&
1414 name_len < INT_MAX &&
1415 strlen(name) == name_len &&
1416 memchr(name, '/', name_len) == NULL;
1420 * Validate names (path components)
1422 * To be valid \a name must be non-empty, '\0' terminated of length \a
1423 * name_len, and not contain '/'. The maximum length of a name (before
1424 * say -ENAMETOOLONG will be returned) is really controlled by llite
1425 * and the server. We only check for something insane coming from bad
1426 * integer handling here.
1428 static inline bool lu_name_is_valid_2(const char *name, size_t name_len)
1430 return lu_name_is_valid_len(name, name_len) && name[name_len] == '\0';
1433 static inline bool lu_name_is_valid(const struct lu_name *ln)
1435 return lu_name_is_valid_2(ln->ln_name, ln->ln_namelen);
1438 #define DNAME "%.*s"
1440 (lu_name_is_valid(ln) ? (ln)->ln_namelen : 0), \
1441 (lu_name_is_valid(ln) ? (ln)->ln_name : "")
1444 * Common buffer structure to be passed around for various xattr_{s,g}et()
1452 #define DLUBUF "(%p %zu)"
1453 #define PLUBUF(buf) (buf)->lb_buf, (buf)->lb_len
1455 /* read buffer params, should be filled out by out */
1457 /** number of buffers */
1458 unsigned int rb_nbufs;
1459 /** pointers to buffers */
1460 struct lu_buf rb_bufs[];
1464 * One-time initializers, called at obdclass module initialization, not
1469 * Initialization of global lu_* data.
1471 int lu_global_init(void);
1474 * Dual to lu_global_init().
1476 void lu_global_fini(void);
1478 struct lu_kmem_descr {
1479 struct kmem_cache **ckd_cache;
1480 const char *ckd_name;
1481 const size_t ckd_size;
1484 int lu_kmem_init(struct lu_kmem_descr *caches);
1485 void lu_kmem_fini(struct lu_kmem_descr *caches);
1487 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
1488 const struct lu_fid *fid);
1489 struct lu_object *lu_object_anon(const struct lu_env *env,
1490 struct lu_device *dev,
1491 const struct lu_object_conf *conf);
1494 extern struct lu_buf LU_BUF_NULL;
1496 void lu_buf_free(struct lu_buf *buf);
1497 void lu_buf_alloc(struct lu_buf *buf, size_t size);
1498 void lu_buf_realloc(struct lu_buf *buf, size_t size);
1500 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len);
1501 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len);
1503 extern __u32 lu_context_tags_default;
1504 extern __u32 lu_session_tags_default;
1506 static inline bool lu_device_is_cl(const struct lu_device *d)
1508 return d->ld_type->ldt_tags & LU_DEVICE_CL;
1511 static inline bool lu_object_is_cl(const struct lu_object *o)
1513 return lu_device_is_cl(o->lo_dev);
1516 /* round-robin QoS data for LOD/LMV */
1518 spinlock_t lqr_alloc; /* protect allocation index */
1519 __u32 lqr_start_idx; /* start index of new inode */
1520 __u32 lqr_offset_idx;/* aliasing for start_idx */
1521 int lqr_start_count;/* reseed counter */
1522 struct lu_tgt_pool lqr_pool; /* round-robin optimized list */
1523 unsigned long lqr_dirty:1; /* recalc round-robin list */
1526 /* QoS data per MDS/OSS */
1528 struct obd_uuid lsq_uuid; /* ptlrpc's c_remote_uuid */
1529 struct list_head lsq_svr_list; /* link to lq_svr_list */
1530 __u64 lsq_bavail; /* total bytes avail on svr */
1531 __u64 lsq_iavail; /* tital inode avail on svr */
1532 __u64 lsq_penalty; /* current penalty */
1533 __u64 lsq_penalty_per_obj; /* penalty decrease
1535 time64_t lsq_used; /* last used time, seconds */
1536 __u32 lsq_tgt_count; /* number of tgts on this svr */
1537 __u32 lsq_id; /* unique svr id */
1540 /* QoS data per MDT/OST */
1542 struct lu_svr_qos *ltq_svr; /* svr info */
1543 __u64 ltq_penalty; /* current penalty */
1544 __u64 ltq_penalty_per_obj; /* penalty decrease
1546 __u64 ltq_weight; /* net weighting */
1547 time64_t ltq_used; /* last used time, seconds */
1548 bool ltq_usable:1; /* usable for striping */
1551 /* target descriptor */
1552 struct lu_tgt_desc {
1554 struct dt_device *ltd_tgt;
1555 struct obd_device *ltd_obd;
1557 struct obd_export *ltd_exp;
1558 struct obd_uuid ltd_uuid;
1561 struct list_head ltd_kill;
1562 struct task_struct *ltd_recovery_task;
1563 struct mutex ltd_fid_mutex;
1564 struct lu_tgt_qos ltd_qos; /* qos info per target */
1565 struct obd_statfs ltd_statfs;
1566 time64_t ltd_statfs_age;
1567 unsigned long ltd_active:1,/* is this target up for requests */
1568 ltd_activate:1,/* should target be activated */
1569 ltd_reap:1, /* should this target be deleted */
1570 ltd_got_update_log:1, /* Already got update log */
1571 ltd_connecting:1; /* target is connecting */
1574 /* number of pointers at 1st level */
1575 #define TGT_PTRS (PAGE_SIZE / sizeof(void *))
1576 /* number of pointers at 2nd level */
1577 #define TGT_PTRS_PER_BLOCK (PAGE_SIZE / sizeof(void *))
1579 struct lu_tgt_desc_idx {
1580 struct lu_tgt_desc *ldi_tgt[TGT_PTRS_PER_BLOCK];
1583 /* QoS data for LOD/LMV */
1585 struct list_head lq_svr_list; /* lu_svr_qos list */
1586 struct rw_semaphore lq_rw_sem;
1587 __u32 lq_active_svr_count;
1588 unsigned int lq_prio_free; /* priority for free space */
1589 unsigned int lq_threshold_rr;/* priority for rr */
1590 struct lu_qos_rr lq_rr; /* round robin qos data */
1591 unsigned long lq_dirty:1, /* recalc qos data */
1592 lq_same_space:1,/* the servers all have approx.
1593 * the same space avail */
1594 lq_reset:1; /* zero current penalties */
1597 struct lu_tgt_descs {
1599 struct lov_desc ltd_lov_desc;
1600 struct lmv_desc ltd_lmv_desc;
1602 /* list of known TGTs */
1603 struct lu_tgt_desc_idx *ltd_tgt_idx[TGT_PTRS];
1604 /* Size of the lu_tgts array, granted to be a power of 2 */
1605 __u32 ltd_tgts_size;
1606 /* bitmap of TGTs available */
1607 struct cfs_bitmap *ltd_tgt_bitmap;
1608 /* TGTs scheduled to be deleted */
1609 __u32 ltd_death_row;
1610 /* Table refcount used for delayed deletion */
1612 /* mutex to serialize concurrent updates to the tgt table */
1613 struct mutex ltd_mutex;
1614 /* read/write semaphore used for array relocation */
1615 struct rw_semaphore ltd_rw_sem;
1617 struct lu_qos ltd_qos;
1618 /* all tgts in a packed array */
1619 struct lu_tgt_pool ltd_tgt_pool;
1620 /* true if tgt is MDT */
1624 #define LTD_TGT(ltd, index) \
1625 (ltd)->ltd_tgt_idx[(index) / \
1626 TGT_PTRS_PER_BLOCK]->ldi_tgt[(index) % TGT_PTRS_PER_BLOCK]
1628 u64 lu_prandom_u64_max(u64 ep_ro);
1629 void lu_qos_rr_init(struct lu_qos_rr *lqr);
1630 int lu_qos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd);
1631 void lu_tgt_qos_weight_calc(struct lu_tgt_desc *tgt);
1633 int lu_tgt_descs_init(struct lu_tgt_descs *ltd, bool is_mdt);
1634 void lu_tgt_descs_fini(struct lu_tgt_descs *ltd);
1635 int ltd_add_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt);
1636 void ltd_del_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt);
1637 bool ltd_qos_is_usable(struct lu_tgt_descs *ltd);
1638 int ltd_qos_penalties_calc(struct lu_tgt_descs *ltd);
1639 int ltd_qos_update(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt,
1642 static inline struct lu_tgt_desc *ltd_first_tgt(struct lu_tgt_descs *ltd)
1646 index = find_first_bit(ltd->ltd_tgt_bitmap->data,
1647 ltd->ltd_tgt_bitmap->size);
1648 return (index < ltd->ltd_tgt_bitmap->size) ? LTD_TGT(ltd, index) : NULL;
1651 static inline struct lu_tgt_desc *ltd_next_tgt(struct lu_tgt_descs *ltd,
1652 struct lu_tgt_desc *tgt)
1659 index = tgt->ltd_index;
1660 LASSERT(index < ltd->ltd_tgt_bitmap->size);
1661 index = find_next_bit(ltd->ltd_tgt_bitmap->data,
1662 ltd->ltd_tgt_bitmap->size, index + 1);
1663 return (index < ltd->ltd_tgt_bitmap->size) ? LTD_TGT(ltd, index) : NULL;
1666 #define ltd_foreach_tgt(ltd, tgt) \
1667 for (tgt = ltd_first_tgt(ltd); tgt; tgt = ltd_next_tgt(ltd, tgt))
1669 #define ltd_foreach_tgt_safe(ltd, tgt, tmp) \
1670 for (tgt = ltd_first_tgt(ltd), tmp = ltd_next_tgt(ltd, tgt); tgt; \
1671 tgt = tmp, tmp = ltd_next_tgt(ltd, tgt))
1674 #endif /* __LUSTRE_LU_OBJECT_H */