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>
43 struct proc_dir_entry;
49 * lu_* data-types represent server-side entities shared by data and meta-data
54 * -# support for layering.
56 * Server side object is split into layers, one per device in the
57 * corresponding device stack. Individual layer is represented by struct
58 * lu_object. Compound layered object --- by struct lu_object_header. Most
59 * interface functions take lu_object as an argument and operate on the
60 * whole compound object. This decision was made due to the following
63 * - it's envisaged that lu_object will be used much more often than
66 * - we want lower (non-top) layers to be able to initiate operations
67 * on the whole object.
69 * Generic code supports layering more complex than simple stacking, e.g.,
70 * it is possible that at some layer object "spawns" multiple sub-objects
73 * -# fid-based identification.
75 * Compound object is uniquely identified by its fid. Objects are indexed
76 * by their fids (hash table is used for index).
78 * -# caching and life-cycle management.
80 * Object's life-time is controlled by reference counting. When reference
81 * count drops to 0, object is returned to cache. Cached objects still
82 * retain their identity (i.e., fid), and can be recovered from cache.
84 * Objects are kept in the global LRU list, and lu_site_purge() function
85 * can be used to reclaim given number of unused objects from the tail of
88 * -# avoiding recursion.
90 * Generic code tries to replace recursion through layers by iterations
91 * where possible. Additionally to the end of reducing stack consumption,
92 * data, when practically possible, are allocated through lu_context_key
93 * interface rather than on stack.
100 struct lu_object_header;
105 * Operations common for data and meta-data devices.
107 struct lu_device_operations {
109 * Allocate object for the given device (without lower-layer
110 * parts). This is called by lu_object_operations::loo_object_init()
111 * from the parent layer, and should setup at least lu_object::lo_dev
112 * and lu_object::lo_ops fields of resulting lu_object.
114 * Object creation protocol.
116 * Due to design goal of avoiding recursion, object creation (see
117 * lu_object_alloc()) is somewhat involved:
119 * - first, lu_device_operations::ldo_object_alloc() method of the
120 * top-level device in the stack is called. It should allocate top
121 * level object (including lu_object_header), but without any
122 * lower-layer sub-object(s).
124 * - then lu_object_alloc() sets fid in the header of newly created
127 * - then lu_object_operations::loo_object_init() is called. It has
128 * to allocate lower-layer object(s). To do this,
129 * lu_object_operations::loo_object_init() calls ldo_object_alloc()
130 * of the lower-layer device(s).
132 * - for all new objects allocated by
133 * lu_object_operations::loo_object_init() (and inserted into object
134 * stack), lu_object_operations::loo_object_init() is called again
135 * repeatedly, until no new objects are created.
137 * \post ergo(!IS_ERR(result), result->lo_dev == d &&
138 * result->lo_ops != NULL);
140 struct lu_object *(*ldo_object_alloc)(const struct lu_env *env,
141 const struct lu_object_header *h,
142 struct lu_device *d);
144 * process config specific for device.
146 int (*ldo_process_config)(const struct lu_env *env,
147 struct lu_device *, struct lustre_cfg *);
148 int (*ldo_recovery_complete)(const struct lu_env *,
152 * initialize local objects for device. this method called after layer has
153 * been initialized (after LCFG_SETUP stage) and before it starts serving
157 int (*ldo_prepare)(const struct lu_env *,
158 struct lu_device *parent,
159 struct lu_device *dev);
164 * For lu_object_conf flags
167 /* This is a new object to be allocated, or the file
168 * corresponding to the object does not exists. */
169 LOC_F_NEW = 0x00000001,
173 * Object configuration, describing particulars of object being created. On
174 * server this is not used, as server objects are full identified by fid. On
175 * client configuration contains struct lustre_md.
177 struct lu_object_conf {
179 * Some hints for obj find and alloc.
181 loc_flags_t loc_flags;
185 * Type of "printer" function used by lu_object_operations::loo_object_print()
188 * Printer function is needed to provide some flexibility in (semi-)debugging
189 * output: possible implementations: printk, CDEBUG, sysfs/seq_file
191 typedef int (*lu_printer_t)(const struct lu_env *env,
192 void *cookie, const char *format, ...)
193 __attribute__ ((format (printf, 3, 4)));
196 * Operations specific for particular lu_object.
198 struct lu_object_operations {
201 * Allocate lower-layer parts of the object by calling
202 * lu_device_operations::ldo_object_alloc() of the corresponding
205 * This method is called once for each object inserted into object
206 * stack. It's responsibility of this method to insert lower-layer
207 * object(s) it create into appropriate places of object stack.
209 int (*loo_object_init)(const struct lu_env *env,
211 const struct lu_object_conf *conf);
213 * Called (in top-to-bottom order) during object allocation after all
214 * layers were allocated and initialized. Can be used to perform
215 * initialization depending on lower layers.
217 int (*loo_object_start)(const struct lu_env *env,
218 struct lu_object *o);
220 * Called before lu_object_operations::loo_object_free() to signal
221 * that object is being destroyed. Dual to
222 * lu_object_operations::loo_object_init().
224 void (*loo_object_delete)(const struct lu_env *env,
225 struct lu_object *o);
227 * Dual to lu_device_operations::ldo_object_alloc(). Called when
228 * object is removed from memory.
230 void (*loo_object_free)(const struct lu_env *env,
231 struct lu_object *o);
233 * Called when last active reference to the object is released (and
234 * object returns to the cache). This method is optional.
236 void (*loo_object_release)(const struct lu_env *env,
237 struct lu_object *o);
239 * Optional debugging helper. Print given object.
241 int (*loo_object_print)(const struct lu_env *env, void *cookie,
242 lu_printer_t p, const struct lu_object *o);
244 * Optional debugging method. Returns true iff method is internally
247 int (*loo_object_invariant)(const struct lu_object *o);
253 struct lu_device_type;
256 * Device: a layer in the server side abstraction stacking.
260 * reference count. This is incremented, in particular, on each object
261 * created at this layer.
263 * \todo XXX which means that atomic_t is probably too small.
267 * Pointer to device type. Never modified once set.
269 struct lu_device_type *ld_type;
271 * Operation vector for this device.
273 const struct lu_device_operations *ld_ops;
275 * Stack this device belongs to.
277 struct lu_site *ld_site;
278 struct proc_dir_entry *ld_proc_entry;
280 /** \todo XXX: temporary back pointer into obd. */
281 struct obd_device *ld_obd;
283 * A list of references to this object, for debugging.
285 struct lu_ref ld_reference;
287 * Link the device to the site.
289 struct list_head ld_linkage;
292 struct lu_device_type_operations;
295 * Tag bits for device type. They are used to distinguish certain groups of
299 /** this is meta-data device */
300 LU_DEVICE_MD = (1 << 0),
301 /** this is data device */
302 LU_DEVICE_DT = (1 << 1),
303 /** data device in the client stack */
304 LU_DEVICE_CL = (1 << 2)
310 struct lu_device_type {
312 * Tag bits. Taken from enum lu_device_tag. Never modified once set.
316 * Name of this class. Unique system-wide. Never modified once set.
320 * Operations for this type.
322 const struct lu_device_type_operations *ldt_ops;
324 * \todo XXX: temporary: context tags used by obd_*() calls.
328 * Number of existing device type instances.
330 atomic_t ldt_device_nr;
334 * Operations on a device type.
336 struct lu_device_type_operations {
338 * Allocate new device.
340 struct lu_device *(*ldto_device_alloc)(const struct lu_env *env,
341 struct lu_device_type *t,
342 struct lustre_cfg *lcfg);
344 * Free device. Dual to
345 * lu_device_type_operations::ldto_device_alloc(). Returns pointer to
346 * the next device in the stack.
348 struct lu_device *(*ldto_device_free)(const struct lu_env *,
352 * Initialize the devices after allocation
354 int (*ldto_device_init)(const struct lu_env *env,
355 struct lu_device *, const char *,
358 * Finalize device. Dual to
359 * lu_device_type_operations::ldto_device_init(). Returns pointer to
360 * the next device in the stack.
362 struct lu_device *(*ldto_device_fini)(const struct lu_env *env,
365 * Initialize device type. This is called on module load.
367 int (*ldto_init)(struct lu_device_type *t);
369 * Finalize device type. Dual to
370 * lu_device_type_operations::ldto_init(). Called on module unload.
372 void (*ldto_fini)(struct lu_device_type *t);
374 * Called when the first device is created.
376 void (*ldto_start)(struct lu_device_type *t);
378 * Called when number of devices drops to 0.
380 void (*ldto_stop)(struct lu_device_type *t);
383 static inline int lu_device_is_md(const struct lu_device *d)
385 return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_MD);
389 * Common object attributes.
400 /** modification time in seconds since Epoch */
402 /** access time in seconds since Epoch */
404 /** change time in seconds since Epoch */
406 /** 512-byte blocks allocated to object */
408 /** permission bits and file type */
416 /** number of persistent references to this object */
418 /** blk bits of the object*/
420 /** blk size of the object*/
426 /** set layout version to OST objects. */
427 __u32 la_layout_version;
431 * Layer in the layered object.
435 * Header for this object.
437 struct lu_object_header *lo_header;
439 * Device for this layer.
441 struct lu_device *lo_dev;
443 * Operations for this object.
445 const struct lu_object_operations *lo_ops;
447 * Linkage into list of all layers.
449 struct list_head lo_linkage;
451 * Link to the device, for debugging.
453 struct lu_ref_link lo_dev_ref;
456 enum lu_object_header_flags {
458 * Don't keep this object in cache. Object will be destroyed as soon
459 * as last reference to it is released. This flag cannot be cleared
462 LU_OBJECT_HEARD_BANSHEE = 0,
464 * Mark this object has already been taken out of cache.
466 LU_OBJECT_UNHASHED = 1,
468 * Object is initialized, when object is found in cache, it may not be
469 * intialized yet, the object allocator will initialize it.
474 enum lu_object_header_attr {
475 LOHA_EXISTS = 1 << 0,
476 LOHA_REMOTE = 1 << 1,
477 LOHA_HAS_AGENT_ENTRY = 1 << 2,
479 * UNIX file type is stored in S_IFMT bits.
481 LOHA_FT_START = 001 << 12, /**< S_IFIFO */
482 LOHA_FT_END = 017 << 12, /**< S_IFMT */
486 * "Compound" object, consisting of multiple layers.
488 * Compound object with given fid is unique with given lu_site.
490 * Note, that object does *not* necessary correspond to the real object in the
491 * persistent storage: object is an anchor for locking and method calling, so
492 * it is created for things like not-yet-existing child created by mkdir or
493 * create calls. lu_object_operations::loo_exists() can be used to check
494 * whether object is backed by persistent storage entity.
496 struct lu_object_header {
498 * Fid, uniquely identifying this object.
500 struct lu_fid loh_fid;
502 * Object flags from enum lu_object_header_flags. Set and checked
505 unsigned long loh_flags;
507 * Object reference count. Protected by lu_site::ls_guard.
511 * Common object attributes, cached for efficiency. From enum
512 * lu_object_header_attr.
516 * Linkage into per-site hash table. Protected by lu_site::ls_guard.
518 struct hlist_node loh_hash;
520 * Linkage into per-site LRU list. Protected by lu_site::ls_guard.
522 struct list_head loh_lru;
524 * Linkage into list of layers. Never modified once set (except lately
525 * during object destruction). No locking is necessary.
527 struct list_head loh_layers;
529 * A list of references to this object, for debugging.
531 struct lu_ref loh_reference;
541 LU_SS_CACHE_DEATH_RACE,
547 * lu_site is a "compartment" within which objects are unique, and LRU
548 * discipline is maintained.
550 * lu_site exists so that multiple layered stacks can co-exist in the same
553 * lu_site has the same relation to lu_device as lu_object_header to
560 struct cfs_hash *ls_obj_hash;
562 * index of bucket on hash table while purging
564 unsigned int ls_purge_start;
566 * Top-level device for this stack.
568 struct lu_device *ls_top_dev;
570 * Bottom-level device for this stack
572 struct lu_device *ls_bottom_dev;
574 * Linkage into global list of sites.
576 struct list_head ls_linkage;
578 * List for lu device for this site, protected
581 struct list_head ls_ld_linkage;
582 spinlock_t ls_ld_lock;
584 * Lock to serialize site purge.
586 struct mutex ls_purge_mutex;
590 struct lprocfs_stats *ls_stats;
592 * XXX: a hack! fld has to find md_site via site, remove when possible
594 struct seq_server_site *ld_seq_site;
596 * Pointer to the lu_target for this site.
598 struct lu_target *ls_tgt;
601 * Number of objects in lsb_lru_lists - used for shrinking
603 struct percpu_counter ls_lru_len_counter;
607 lu_site_wq_from_fid(struct lu_site *site, struct lu_fid *fid);
609 static inline struct seq_server_site *lu_site2seq(const struct lu_site *s)
611 return s->ld_seq_site;
615 * Constructors/destructors.
619 int lu_site_init (struct lu_site *s, struct lu_device *d);
620 void lu_site_fini (struct lu_site *s);
621 int lu_site_init_finish (struct lu_site *s);
622 void lu_stack_fini (const struct lu_env *env, struct lu_device *top);
623 void lu_device_get (struct lu_device *d);
624 void lu_device_put (struct lu_device *d);
625 int lu_device_init (struct lu_device *d, struct lu_device_type *t);
626 void lu_device_fini (struct lu_device *d);
627 int lu_object_header_init(struct lu_object_header *h);
628 void lu_object_header_fini(struct lu_object_header *h);
629 int lu_object_init (struct lu_object *o,
630 struct lu_object_header *h, struct lu_device *d);
631 void lu_object_fini (struct lu_object *o);
632 void lu_object_add_top (struct lu_object_header *h, struct lu_object *o);
633 void lu_object_add (struct lu_object *before, struct lu_object *o);
635 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d);
636 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d);
639 * Helpers to initialize and finalize device types.
642 int lu_device_type_init(struct lu_device_type *ldt);
643 void lu_device_type_fini(struct lu_device_type *ldt);
648 * Caching and reference counting.
653 * Acquire additional reference to the given object. This function is used to
654 * attain additional reference. To acquire initial reference use
657 static inline void lu_object_get(struct lu_object *o)
659 LASSERT(atomic_read(&o->lo_header->loh_ref) > 0);
660 atomic_inc(&o->lo_header->loh_ref);
664 * Return true if object will not be cached after last reference to it is
667 static inline int lu_object_is_dying(const struct lu_object_header *h)
669 return test_bit(LU_OBJECT_HEARD_BANSHEE, &h->loh_flags);
673 * Return true if object is initialized.
675 static inline int lu_object_is_inited(const struct lu_object_header *h)
677 return test_bit(LU_OBJECT_INITED, &h->loh_flags);
680 void lu_object_put(const struct lu_env *env, struct lu_object *o);
681 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o);
682 void lu_object_unhash(const struct lu_env *env, struct lu_object *o);
683 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s, int nr,
686 static inline int lu_site_purge(const struct lu_env *env, struct lu_site *s,
689 return lu_site_purge_objects(env, s, nr, 1);
692 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
693 lu_printer_t printer);
694 struct lu_object *lu_object_find(const struct lu_env *env,
695 struct lu_device *dev, const struct lu_fid *f,
696 const struct lu_object_conf *conf);
697 struct lu_object *lu_object_find_at(const struct lu_env *env,
698 struct lu_device *dev,
699 const struct lu_fid *f,
700 const struct lu_object_conf *conf);
701 struct lu_object *lu_object_find_slice(const struct lu_env *env,
702 struct lu_device *dev,
703 const struct lu_fid *f,
704 const struct lu_object_conf *conf);
713 * First (topmost) sub-object of given compound object
715 static inline struct lu_object *lu_object_top(struct lu_object_header *h)
717 LASSERT(!list_empty(&h->loh_layers));
718 return container_of0(h->loh_layers.next, struct lu_object, lo_linkage);
722 * Next sub-object in the layering
724 static inline struct lu_object *lu_object_next(const struct lu_object *o)
726 return container_of0(o->lo_linkage.next, struct lu_object, lo_linkage);
730 * Pointer to the fid of this object.
732 static inline const struct lu_fid *lu_object_fid(const struct lu_object *o)
734 return &o->lo_header->loh_fid;
738 * return device operations vector for this object
740 static const inline struct lu_device_operations *
741 lu_object_ops(const struct lu_object *o)
743 return o->lo_dev->ld_ops;
747 * Given a compound object, find its slice, corresponding to the device type
750 struct lu_object *lu_object_locate(struct lu_object_header *h,
751 const struct lu_device_type *dtype);
754 * Printer function emitting messages through libcfs_debug_msg().
756 int lu_cdebug_printer(const struct lu_env *env,
757 void *cookie, const char *format, ...);
760 * Print object description followed by a user-supplied message.
762 #define LU_OBJECT_DEBUG(mask, env, object, format, ...) \
764 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
765 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, NULL); \
766 lu_object_print(env, &msgdata, lu_cdebug_printer, object);\
767 CDEBUG(mask, format "\n", ## __VA_ARGS__); \
772 * Print short object description followed by a user-supplied message.
774 #define LU_OBJECT_HEADER(mask, env, object, format, ...) \
776 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
777 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, NULL); \
778 lu_object_header_print(env, &msgdata, lu_cdebug_printer,\
779 (object)->lo_header); \
780 lu_cdebug_printer(env, &msgdata, "\n"); \
781 CDEBUG(mask, format , ## __VA_ARGS__); \
785 void lu_object_print (const struct lu_env *env, void *cookie,
786 lu_printer_t printer, const struct lu_object *o);
787 void lu_object_header_print(const struct lu_env *env, void *cookie,
788 lu_printer_t printer,
789 const struct lu_object_header *hdr);
792 * Check object consistency.
794 int lu_object_invariant(const struct lu_object *o);
798 * Check whether object exists, no matter on local or remote storage.
799 * Note: LOHA_EXISTS will be set once some one created the object,
800 * and it does not needs to be committed to storage.
802 #define lu_object_exists(o) ((o)->lo_header->loh_attr & LOHA_EXISTS)
805 * Check whether object on the remote storage.
807 #define lu_object_remote(o) unlikely((o)->lo_header->loh_attr & LOHA_REMOTE)
810 * Check whether the object as agent entry on current target
812 #define lu_object_has_agent_entry(o) \
813 unlikely((o)->lo_header->loh_attr & LOHA_HAS_AGENT_ENTRY)
815 static inline void lu_object_set_agent_entry(struct lu_object *o)
817 o->lo_header->loh_attr |= LOHA_HAS_AGENT_ENTRY;
820 static inline void lu_object_clear_agent_entry(struct lu_object *o)
822 o->lo_header->loh_attr &= ~LOHA_HAS_AGENT_ENTRY;
825 static inline int lu_object_assert_exists(const struct lu_object *o)
827 return lu_object_exists(o);
830 static inline int lu_object_assert_not_exists(const struct lu_object *o)
832 return !lu_object_exists(o);
836 * Attr of this object.
838 static inline __u32 lu_object_attr(const struct lu_object *o)
840 LASSERT(lu_object_exists(o) != 0);
842 return o->lo_header->loh_attr & S_IFMT;
845 static inline void lu_object_ref_add(struct lu_object *o,
849 lu_ref_add(&o->lo_header->loh_reference, scope, source);
852 static inline void lu_object_ref_add_at(struct lu_object *o,
853 struct lu_ref_link *link,
857 lu_ref_add_at(&o->lo_header->loh_reference, link, scope, source);
860 static inline void lu_object_ref_del(struct lu_object *o,
861 const char *scope, const void *source)
863 lu_ref_del(&o->lo_header->loh_reference, scope, source);
866 static inline void lu_object_ref_del_at(struct lu_object *o,
867 struct lu_ref_link *link,
868 const char *scope, const void *source)
870 lu_ref_del_at(&o->lo_header->loh_reference, link, scope, source);
873 /** input params, should be filled out by mdt */
877 /** count in bytes */
878 unsigned int rp_count;
879 /** number of pages */
880 unsigned int rp_npages;
881 /** requested attr */
883 /** pointers to pages */
884 struct page **rp_pages;
887 enum lu_xattr_flags {
888 LU_XATTR_REPLACE = (1 << 0),
889 LU_XATTR_CREATE = (1 << 1),
890 LU_XATTR_MERGE = (1 << 2),
891 LU_XATTR_SPLIT = (1 << 3),
899 /** For lu_context health-checks */
900 enum lu_context_state {
909 * lu_context. Execution context for lu_object methods. Currently associated
912 * All lu_object methods, except device and device type methods (called during
913 * system initialization and shutdown) are executed "within" some
914 * lu_context. This means, that pointer to some "current" lu_context is passed
915 * as an argument to all methods.
917 * All service ptlrpc threads create lu_context as part of their
918 * initialization. It is possible to create "stand-alone" context for other
919 * execution environments (like system calls).
921 * lu_object methods mainly use lu_context through lu_context_key interface
922 * that allows each layer to associate arbitrary pieces of data with each
923 * context (see pthread_key_create(3) for similar interface).
925 * On a client, lu_context is bound to a thread, see cl_env_get().
927 * \see lu_context_key
931 * lu_context is used on the client side too. Yet we don't want to
932 * allocate values of server-side keys for the client contexts and
935 * To achieve this, set of tags in introduced. Contexts and keys are
936 * marked with tags. Key value are created only for context whose set
937 * of tags has non-empty intersection with one for key. Tags are taken
938 * from enum lu_context_tag.
941 enum lu_context_state lc_state;
943 * Pointer to the home service thread. NULL for other execution
946 struct ptlrpc_thread *lc_thread;
948 * Pointer to an array with key values. Internal implementation
953 * Linkage into a list of all remembered contexts. Only
954 * `non-transient' contexts, i.e., ones created for service threads
957 struct list_head lc_remember;
959 * Version counter used to skip calls to lu_context_refill() when no
960 * keys were registered.
970 * lu_context_key interface. Similar to pthread_key.
973 enum lu_context_tag {
975 * Thread on md server
977 LCT_MD_THREAD = 1 << 0,
979 * Thread on dt server
981 LCT_DT_THREAD = 1 << 1,
985 LCT_CL_THREAD = 1 << 3,
987 * A per-request session on a server, and a per-system-call session on
990 LCT_SESSION = 1 << 4,
992 * A per-request data on OSP device
994 LCT_OSP_THREAD = 1 << 5,
998 LCT_MG_THREAD = 1 << 6,
1000 * Context for local operations
1004 * session for server thread
1006 LCT_SERVER_SESSION = 1 << 8,
1008 * Set when at least one of keys, having values in this context has
1009 * non-NULL lu_context_key::lct_exit() method. This is used to
1010 * optimize lu_context_exit() call.
1012 LCT_HAS_EXIT = 1 << 28,
1014 * Don't add references for modules creating key values in that context.
1015 * This is only for contexts used internally by lu_object framework.
1017 LCT_NOREF = 1 << 29,
1019 * Key is being prepared for retiring, don't create new values for it.
1021 LCT_QUIESCENT = 1 << 30,
1023 * Context should be remembered.
1025 LCT_REMEMBER = 1 << 31,
1027 * Contexts usable in cache shrinker thread.
1029 LCT_SHRINKER = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD|LCT_NOREF
1033 * Key. Represents per-context value slot.
1035 * Keys are usually registered when module owning the key is initialized, and
1036 * de-registered when module is unloaded. Once key is registered, all new
1037 * contexts with matching tags, will get key value. "Old" contexts, already
1038 * initialized at the time of key registration, can be forced to get key value
1039 * by calling lu_context_refill().
1041 * Every key value is counted in lu_context_key::lct_used and acquires a
1042 * reference on an owning module. This means, that all key values have to be
1043 * destroyed before module can be unloaded. This is usually achieved by
1044 * stopping threads started by the module, that created contexts in their
1045 * entry functions. Situation is complicated by the threads shared by multiple
1046 * modules, like ptlrpcd daemon on a client. To work around this problem,
1047 * contexts, created in such threads, are `remembered' (see
1048 * LCT_REMEMBER)---i.e., added into a global list. When module is preparing
1049 * for unloading it does the following:
1051 * - marks its keys as `quiescent' (lu_context_tag::LCT_QUIESCENT)
1052 * preventing new key values from being allocated in the new contexts,
1055 * - scans a list of remembered contexts, destroying values of module
1056 * keys, thus releasing references to the module.
1058 * This is done by lu_context_key_quiesce(). If module is re-activated
1059 * before key has been de-registered, lu_context_key_revive() call clears
1060 * `quiescent' marker.
1062 * lu_context code doesn't provide any internal synchronization for these
1063 * activities---it's assumed that startup (including threads start-up) and
1064 * shutdown are serialized by some external means.
1068 struct lu_context_key {
1070 * Set of tags for which values of this key are to be instantiated.
1074 * Value constructor. This is called when new value is created for a
1075 * context. Returns pointer to new value of error pointer.
1077 void *(*lct_init)(const struct lu_context *ctx,
1078 struct lu_context_key *key);
1080 * Value destructor. Called when context with previously allocated
1081 * value of this slot is destroyed. \a data is a value that was returned
1082 * by a matching call to lu_context_key::lct_init().
1084 void (*lct_fini)(const struct lu_context *ctx,
1085 struct lu_context_key *key, void *data);
1087 * Optional method called on lu_context_exit() for all allocated
1088 * keys. Can be used by debugging code checking that locks are
1091 void (*lct_exit)(const struct lu_context *ctx,
1092 struct lu_context_key *key, void *data);
1094 * Internal implementation detail: index within lu_context::lc_value[]
1095 * reserved for this key.
1099 * Internal implementation detail: number of values created for this
1104 * Internal implementation detail: module for this key.
1106 struct module *lct_owner;
1108 * References to this key. For debugging.
1110 struct lu_ref lct_reference;
1113 #define LU_KEY_INIT(mod, type) \
1114 static void *mod##_key_init(const struct lu_context *ctx, \
1115 struct lu_context_key *key) \
1119 CLASSERT(PAGE_SIZE >= sizeof(*value)); \
1121 OBD_ALLOC_PTR(value); \
1122 if (value == NULL) \
1123 value = ERR_PTR(-ENOMEM); \
1127 struct __##mod##__dummy_init { ; } /* semicolon catcher */
1129 #define LU_KEY_FINI(mod, type) \
1130 static void mod##_key_fini(const struct lu_context *ctx, \
1131 struct lu_context_key *key, void* data) \
1133 type *info = data; \
1135 OBD_FREE_PTR(info); \
1137 struct __##mod##__dummy_fini {;} /* semicolon catcher */
1139 #define LU_KEY_INIT_FINI(mod, type) \
1140 LU_KEY_INIT(mod,type); \
1141 LU_KEY_FINI(mod,type)
1143 #define LU_CONTEXT_KEY_DEFINE(mod, tags) \
1144 struct lu_context_key mod##_thread_key = { \
1146 .lct_init = mod##_key_init, \
1147 .lct_fini = mod##_key_fini \
1150 #define LU_CONTEXT_KEY_INIT(key) \
1152 (key)->lct_owner = THIS_MODULE; \
1155 int lu_context_key_register(struct lu_context_key *key);
1156 void lu_context_key_degister(struct lu_context_key *key);
1157 void *lu_context_key_get (const struct lu_context *ctx,
1158 const struct lu_context_key *key);
1159 void lu_context_key_quiesce (struct lu_context_key *key);
1160 void lu_context_key_revive (struct lu_context_key *key);
1164 * LU_KEY_INIT_GENERIC() has to be a macro to correctly determine an
1168 #define LU_KEY_INIT_GENERIC(mod) \
1169 static void mod##_key_init_generic(struct lu_context_key *k, ...) \
1171 struct lu_context_key *key = k; \
1174 va_start(args, k); \
1176 LU_CONTEXT_KEY_INIT(key); \
1177 key = va_arg(args, struct lu_context_key *); \
1178 } while (key != NULL); \
1182 #define LU_TYPE_INIT(mod, ...) \
1183 LU_KEY_INIT_GENERIC(mod) \
1184 static int mod##_type_init(struct lu_device_type *t) \
1186 mod##_key_init_generic(__VA_ARGS__, NULL); \
1187 return lu_context_key_register_many(__VA_ARGS__, NULL); \
1189 struct __##mod##_dummy_type_init {;}
1191 #define LU_TYPE_FINI(mod, ...) \
1192 static void mod##_type_fini(struct lu_device_type *t) \
1194 lu_context_key_degister_many(__VA_ARGS__, NULL); \
1196 struct __##mod##_dummy_type_fini {;}
1198 #define LU_TYPE_START(mod, ...) \
1199 static void mod##_type_start(struct lu_device_type *t) \
1201 lu_context_key_revive_many(__VA_ARGS__, NULL); \
1203 struct __##mod##_dummy_type_start {;}
1205 #define LU_TYPE_STOP(mod, ...) \
1206 static void mod##_type_stop(struct lu_device_type *t) \
1208 lu_context_key_quiesce_many(__VA_ARGS__, NULL); \
1210 struct __##mod##_dummy_type_stop {;}
1214 #define LU_TYPE_INIT_FINI(mod, ...) \
1215 LU_TYPE_INIT(mod, __VA_ARGS__); \
1216 LU_TYPE_FINI(mod, __VA_ARGS__); \
1217 LU_TYPE_START(mod, __VA_ARGS__); \
1218 LU_TYPE_STOP(mod, __VA_ARGS__)
1220 int lu_context_init (struct lu_context *ctx, __u32 tags);
1221 void lu_context_fini (struct lu_context *ctx);
1222 void lu_context_enter (struct lu_context *ctx);
1223 void lu_context_exit (struct lu_context *ctx);
1224 int lu_context_refill(struct lu_context *ctx);
1227 * Helper functions to operate on multiple keys. These are used by the default
1228 * device type operations, defined by LU_TYPE_INIT_FINI().
1231 int lu_context_key_register_many(struct lu_context_key *k, ...);
1232 void lu_context_key_degister_many(struct lu_context_key *k, ...);
1233 void lu_context_key_revive_many (struct lu_context_key *k, ...);
1234 void lu_context_key_quiesce_many (struct lu_context_key *k, ...);
1237 * update/clear ctx/ses tags.
1239 void lu_context_tags_update(__u32 tags);
1240 void lu_context_tags_clear(__u32 tags);
1241 void lu_session_tags_update(__u32 tags);
1242 void lu_session_tags_clear(__u32 tags);
1249 * "Local" context, used to store data instead of stack.
1251 struct lu_context le_ctx;
1253 * "Session" context for per-request data.
1255 struct lu_context *le_ses;
1258 int lu_env_init (struct lu_env *env, __u32 tags);
1259 void lu_env_fini (struct lu_env *env);
1260 int lu_env_refill(struct lu_env *env);
1261 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags, __u32 stags);
1263 static inline void* lu_env_info(const struct lu_env *env,
1264 const struct lu_context_key *key)
1267 info = lu_context_key_get(&env->le_ctx, key);
1269 if (!lu_env_refill((struct lu_env *)env))
1270 info = lu_context_key_get(&env->le_ctx, key);
1276 struct lu_env *lu_env_find(void);
1277 int lu_env_add(struct lu_env *env);
1278 void lu_env_remove(struct lu_env *env);
1280 /** @} lu_context */
1283 * Output site statistical counters into a buffer. Suitable for
1284 * ll_rd_*()-style functions.
1286 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m);
1289 * Common name structure to be passed around for various name related methods.
1292 const char *ln_name;
1296 static inline bool name_is_dot_or_dotdot(const char *name, int namelen)
1298 return name[0] == '.' &&
1299 (namelen == 1 || (namelen == 2 && name[1] == '.'));
1302 static inline bool lu_name_is_dot_or_dotdot(const struct lu_name *lname)
1304 return name_is_dot_or_dotdot(lname->ln_name, lname->ln_namelen);
1307 static inline bool lu_name_is_valid_len(const char *name, size_t name_len)
1309 return name != NULL &&
1311 name_len < INT_MAX &&
1312 strlen(name) == name_len &&
1313 memchr(name, '/', name_len) == NULL;
1317 * Validate names (path components)
1319 * To be valid \a name must be non-empty, '\0' terminated of length \a
1320 * name_len, and not contain '/'. The maximum length of a name (before
1321 * say -ENAMETOOLONG will be returned) is really controlled by llite
1322 * and the server. We only check for something insane coming from bad
1323 * integer handling here.
1325 static inline bool lu_name_is_valid_2(const char *name, size_t name_len)
1327 return lu_name_is_valid_len(name, name_len) && name[name_len] == '\0';
1330 static inline bool lu_name_is_valid(const struct lu_name *ln)
1332 return lu_name_is_valid_2(ln->ln_name, ln->ln_namelen);
1335 #define DNAME "%.*s"
1337 (lu_name_is_valid(ln) ? (ln)->ln_namelen : 0), \
1338 (lu_name_is_valid(ln) ? (ln)->ln_name : "")
1341 * Common buffer structure to be passed around for various xattr_{s,g}et()
1349 #define DLUBUF "(%p %zu)"
1350 #define PLUBUF(buf) (buf)->lb_buf, (buf)->lb_len
1352 /* read buffer params, should be filled out by out */
1354 /** number of buffers */
1355 unsigned int rb_nbufs;
1356 /** pointers to buffers */
1357 struct lu_buf rb_bufs[];
1361 * One-time initializers, called at obdclass module initialization, not
1366 * Initialization of global lu_* data.
1368 int lu_global_init(void);
1371 * Dual to lu_global_init().
1373 void lu_global_fini(void);
1375 struct lu_kmem_descr {
1376 struct kmem_cache **ckd_cache;
1377 const char *ckd_name;
1378 const size_t ckd_size;
1381 int lu_kmem_init(struct lu_kmem_descr *caches);
1382 void lu_kmem_fini(struct lu_kmem_descr *caches);
1384 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
1385 const struct lu_fid *fid);
1386 struct lu_object *lu_object_anon(const struct lu_env *env,
1387 struct lu_device *dev,
1388 const struct lu_object_conf *conf);
1391 extern struct lu_buf LU_BUF_NULL;
1393 void lu_buf_free(struct lu_buf *buf);
1394 void lu_buf_alloc(struct lu_buf *buf, size_t size);
1395 void lu_buf_realloc(struct lu_buf *buf, size_t size);
1397 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len);
1398 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len);
1400 extern __u32 lu_context_tags_default;
1401 extern __u32 lu_session_tags_default;
1403 static inline bool lu_device_is_cl(const struct lu_device *d)
1405 return d->ld_type->ldt_tags & LU_DEVICE_CL;
1408 static inline bool lu_object_is_cl(const struct lu_object *o)
1410 return lu_device_is_cl(o->lo_dev);
1413 /* Generic subset of tgts */
1414 struct lu_tgt_pool {
1415 __u32 *op_array; /* array of index of
1416 * lov_obd->lov_tgts */
1417 unsigned int op_count; /* number of tgts in the array */
1418 unsigned int op_size; /* allocated size of op_array */
1419 struct rw_semaphore op_rw_sem; /* to protect lu_tgt_pool use */
1422 /* round-robin QoS data for LOD/LMV */
1424 spinlock_t lqr_alloc; /* protect allocation index */
1425 __u32 lqr_start_idx; /* start index of new inode */
1426 __u32 lqr_offset_idx;/* aliasing for start_idx */
1427 int lqr_start_count;/* reseed counter */
1428 struct lu_tgt_pool lqr_pool; /* round-robin optimized list */
1429 unsigned long lqr_dirty:1; /* recalc round-robin list */
1432 /* QoS data per MDS/OSS */
1434 struct obd_uuid lsq_uuid; /* ptlrpc's c_remote_uuid */
1435 struct list_head lsq_svr_list; /* link to lq_svr_list */
1436 __u64 lsq_bavail; /* total bytes avail on svr */
1437 __u64 lsq_iavail; /* tital inode avail on svr */
1438 __u64 lsq_penalty; /* current penalty */
1439 __u64 lsq_penalty_per_obj; /* penalty decrease
1441 time64_t lsq_used; /* last used time, seconds */
1442 __u32 lsq_tgt_count; /* number of tgts on this svr */
1443 __u32 lsq_id; /* unique svr id */
1446 /* QoS data per MDT/OST */
1448 struct lu_svr_qos *ltq_svr; /* svr info */
1449 __u64 ltq_penalty; /* current penalty */
1450 __u64 ltq_penalty_per_obj; /* penalty decrease
1452 __u64 ltq_weight; /* net weighting */
1453 time64_t ltq_used; /* last used time, seconds */
1454 bool ltq_usable:1; /* usable for striping */
1457 /* target descriptor */
1458 struct lu_tgt_desc {
1460 struct dt_device *ltd_tgt;
1461 struct obd_device *ltd_obd;
1463 struct obd_export *ltd_exp;
1464 struct obd_uuid ltd_uuid;
1467 struct list_head ltd_kill;
1468 struct ptlrpc_thread *ltd_recovery_thread;
1469 struct mutex ltd_fid_mutex;
1470 struct lu_tgt_qos ltd_qos; /* qos info per target */
1471 struct obd_statfs ltd_statfs;
1472 time64_t ltd_statfs_age;
1473 unsigned long ltd_active:1,/* is this target up for requests */
1474 ltd_activate:1,/* should target be activated */
1475 ltd_reap:1, /* should this target be deleted */
1476 ltd_got_update_log:1, /* Already got update log */
1477 ltd_connecting:1; /* target is connecting */
1480 /* number of pointers at 1st level */
1481 #define TGT_PTRS (PAGE_SIZE / sizeof(void *))
1482 /* number of pointers at 2nd level */
1483 #define TGT_PTRS_PER_BLOCK (PAGE_SIZE / sizeof(void *))
1485 struct lu_tgt_desc_idx {
1486 struct lu_tgt_desc *ldi_tgt[TGT_PTRS_PER_BLOCK];
1489 /* QoS data for LOD/LMV */
1491 struct list_head lq_svr_list; /* lu_svr_qos list */
1492 struct rw_semaphore lq_rw_sem;
1493 __u32 lq_active_svr_count;
1494 unsigned int lq_prio_free; /* priority for free space */
1495 unsigned int lq_threshold_rr;/* priority for rr */
1496 struct lu_qos_rr lq_rr; /* round robin qos data */
1497 unsigned long lq_dirty:1, /* recalc qos data */
1498 lq_same_space:1,/* the servers all have approx.
1499 * the same space avail */
1500 lq_reset:1; /* zero current penalties */
1503 struct lu_tgt_descs {
1505 struct lov_desc ltd_lov_desc;
1506 struct lmv_desc ltd_lmv_desc;
1508 /* list of known TGTs */
1509 struct lu_tgt_desc_idx *ltd_tgt_idx[TGT_PTRS];
1510 /* Size of the lu_tgts array, granted to be a power of 2 */
1511 __u32 ltd_tgts_size;
1512 /* bitmap of TGTs available */
1513 struct cfs_bitmap *ltd_tgt_bitmap;
1514 /* TGTs scheduled to be deleted */
1515 __u32 ltd_death_row;
1516 /* Table refcount used for delayed deletion */
1518 /* mutex to serialize concurrent updates to the tgt table */
1519 struct mutex ltd_mutex;
1520 /* read/write semaphore used for array relocation */
1521 struct rw_semaphore ltd_rw_sem;
1523 struct lu_qos ltd_qos;
1524 /* all tgts in a packed array */
1525 struct lu_tgt_pool ltd_tgt_pool;
1526 /* true if tgt is MDT */
1530 #define LTD_TGT(ltd, index) \
1531 (ltd)->ltd_tgt_idx[(index) / \
1532 TGT_PTRS_PER_BLOCK]->ldi_tgt[(index) % TGT_PTRS_PER_BLOCK]
1534 u64 lu_prandom_u64_max(u64 ep_ro);
1535 void lu_qos_rr_init(struct lu_qos_rr *lqr);
1536 int lu_qos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd);
1537 void lu_tgt_qos_weight_calc(struct lu_tgt_desc *tgt);
1539 int lu_tgt_descs_init(struct lu_tgt_descs *ltd, bool is_mdt);
1540 void lu_tgt_descs_fini(struct lu_tgt_descs *ltd);
1541 int ltd_add_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt);
1542 void ltd_del_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt);
1543 bool ltd_qos_is_usable(struct lu_tgt_descs *ltd);
1544 int ltd_qos_penalties_calc(struct lu_tgt_descs *ltd);
1545 int ltd_qos_update(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt,
1548 static inline struct lu_tgt_desc *ltd_first_tgt(struct lu_tgt_descs *ltd)
1552 index = find_first_bit(ltd->ltd_tgt_bitmap->data,
1553 ltd->ltd_tgt_bitmap->size);
1554 return (index < ltd->ltd_tgt_bitmap->size) ? LTD_TGT(ltd, index) : NULL;
1557 static inline struct lu_tgt_desc *ltd_next_tgt(struct lu_tgt_descs *ltd,
1558 struct lu_tgt_desc *tgt)
1565 index = tgt->ltd_index;
1566 LASSERT(index < ltd->ltd_tgt_bitmap->size);
1567 index = find_next_bit(ltd->ltd_tgt_bitmap->data,
1568 ltd->ltd_tgt_bitmap->size, index + 1);
1569 return (index < ltd->ltd_tgt_bitmap->size) ? LTD_TGT(ltd, index) : NULL;
1572 #define ltd_foreach_tgt(ltd, tgt) \
1573 for (tgt = ltd_first_tgt(ltd); tgt; tgt = ltd_next_tgt(ltd, tgt))
1575 #define ltd_foreach_tgt_safe(ltd, tgt, tmp) \
1576 for (tgt = ltd_first_tgt(ltd), tmp = ltd_next_tgt(ltd, tgt); tgt; \
1577 tgt = tmp, tmp = ltd_next_tgt(ltd, tgt))
1580 #endif /* __LUSTRE_LU_OBJECT_H */