1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #ifndef __LUSTRE_LU_OBJECT_H
24 #define __LUSTRE_LU_OBJECT_H
29 #include <lustre/lustre_idl.h>
31 #include <libcfs/list.h>
32 #include <libcfs/kp30.h>
35 * Layered objects support for CMD3/C5.
40 struct proc_dir_entry;
44 * lu_* data-types represent server-side entities shared by data and meta-data
49 * 0. support for layering.
51 * Server side object is split into layers, one per device in the
52 * corresponding device stack. Individual layer is represented by struct
53 * lu_object. Compound layered object --- by struct lu_object_header. Most
54 * interface functions take lu_object as an argument and operate on the
55 * whole compound object. This decision was made due to the following
58 * - it's envisaged that lu_object will be used much more often than
61 * - we want lower (non-top) layers to be able to initiate operations
62 * on the whole object.
64 * Generic code supports layering more complex than simple stacking, e.g.,
65 * it is possible that at some layer object "spawns" multiple sub-objects
68 * 1. fid-based identification.
70 * Compound object is uniquely identified by its fid. Objects are indexed
71 * by their fids (hash table is used for index).
73 * 2. caching and life-cycle management.
75 * Object's life-time is controlled by reference counting. When reference
76 * count drops to 0, object is returned to cache. Cached objects still
77 * retain their identity (i.e., fid), and can be recovered from cache.
79 * Objects are kept in the global LRU list, and lu_site_purge() function
80 * can be used to reclaim given number of unused objects from the tail of
83 * 3. avoiding recursion.
85 * Generic code tries to replace recursion through layers by iterations
86 * where possible. Additionally to the end of reducing stack consumption,
87 * data, when practically possible, are allocated through lu_context_key
88 * interface rather than on stack.
95 struct lu_object_header;
98 * Operations common for data and meta-data devices.
100 struct lu_device_operations {
102 * Object creation protocol.
104 * Due to design goal of avoiding recursion, object creation (see
105 * lu_object_alloc()) is somewhat involved:
107 * - first, ->ldo_object_alloc() method of the top-level device
108 * in the stack is called. It should allocate top level object
109 * (including lu_object_header), but without any lower-layer
112 * - then lu_object_alloc() sets fid in the header of newly created
115 * - then ->loo_object_init() (a method from struct
116 * lu_object_operations) is called. It has to allocate lower-layer
117 * object(s). To do this, ->loo_object_init() calls
118 * ldo_object_alloc() of the lower-layer device(s).
120 * - for all new objects allocated by ->loo_object_init() (and
121 * inserted into object stack), ->loo_object_init() is called again
122 * repeatedly, until no new objects are created.
127 * Allocate object for the given device (without lower-layer
128 * parts). This is called by ->loo_object_init() from the parent
129 * layer, and should setup at least ->lo_dev and ->lo_ops fields of
130 * resulting lu_object.
132 * postcondition: ergo(!IS_ERR(result), result->lo_dev == d &&
133 * result->lo_ops != NULL);
135 struct lu_object *(*ldo_object_alloc)(const struct lu_context *ctx,
136 const struct lu_object_header *h,
137 struct lu_device *d);
139 * process config specific for device
141 int (*ldo_process_config)(const struct lu_context *ctx,
142 struct lu_device *, struct lustre_cfg *);
147 * Type of "printer" function used by ->loo_object_print() method.
149 * Printer function is needed to provide some flexibility in (semi-)debugging
150 * output: possible implementations: printk, CDEBUG, sysfs/seq_file
152 typedef int (*lu_printer_t)(const struct lu_context *ctx,
153 void *cookie, const char *format, ...)
154 __attribute__ ((format (printf, 3, 4)));
157 * Operations specific for particular lu_object.
159 struct lu_object_operations {
162 * Allocate lower-layer parts of the object by calling
163 * ->ldo_object_alloc() of the corresponding underlying device.
165 * This method is called once for each object inserted into object
166 * stack. It's responsibility of this method to insert lower-layer
167 * object(s) it create into appropriate places of object stack.
169 int (*loo_object_init)(const struct lu_context *ctx,
170 struct lu_object *o);
172 * Called before ->loo_object_free() to signal that object is being
173 * destroyed. Dual to ->loo_object_init().
175 void (*loo_object_delete)(const struct lu_context *ctx,
176 struct lu_object *o);
179 * Dual to ->ldo_object_alloc(). Called when object is removed from
182 void (*loo_object_free)(const struct lu_context *ctx,
183 struct lu_object *o);
186 * Called when last active reference to the object is released (and
187 * object returns to the cache). This method is optional.
189 void (*loo_object_release)(const struct lu_context *ctx,
190 struct lu_object *o);
193 * Return true off object @o exists on a storage.
195 int (*loo_object_exists)(const struct lu_context *ctx,
196 const struct lu_object *o);
198 * Debugging helper. Print given object.
200 int (*loo_object_print)(const struct lu_context *ctx, void *cookie,
201 lu_printer_t p, const struct lu_object *o);
203 * Optional debugging method. Returns true iff method is internally
206 int (*loo_object_invariant)(const struct lu_object *o);
212 struct lu_device_type;
215 * Device: a layer in the server side abstraction stacking.
219 * reference count. This is incremented, in particular, on each object
220 * created at this layer.
222 * XXX which means that atomic_t is probably too small.
226 * Pointer to device type. Never modified once set.
228 struct lu_device_type *ld_type;
230 * Operation vector for this device.
232 struct lu_device_operations *ld_ops;
234 * Stack this device belongs to.
236 struct lu_site *ld_site;
237 struct proc_dir_entry *ld_proc_entry;
239 /* XXX: temporary back pointer into obd. */
240 struct obd_device *ld_obd;
243 struct lu_device_type_operations;
246 * Tag bits for device type. They are used to distinguish certain groups of
250 /* this is meta-data device */
251 LU_DEVICE_MD = (1 << 0),
252 /* this is data device */
253 LU_DEVICE_DT = (1 << 1)
259 struct lu_device_type {
261 * Tag bits. Taken from enum lu_device_tag. Never modified once set.
265 * Name of this class. Unique system-wide. Never modified once set.
269 * Operations for this type.
271 struct lu_device_type_operations *ldt_ops;
273 * XXX: temporary pointer to associated obd_type.
275 struct obd_type *ldt_obd_type;
277 * XXX: temporary: context tags used by obd_*() calls.
283 * Operations on a device type.
285 struct lu_device_type_operations {
287 * Allocate new device.
289 struct lu_device *(*ldto_device_alloc)(const struct lu_context *ctx,
290 struct lu_device_type *t,
291 struct lustre_cfg *lcfg);
293 * Free device. Dual to ->ldto_device_alloc().
295 void (*ldto_device_free)(const struct lu_context *ctx,
296 struct lu_device *d);
299 * Initialize the devices after allocation
301 int (*ldto_device_init)(const struct lu_context *ctx,
302 struct lu_device *, struct lu_device *);
304 * Finalize device. Dual to ->ldto_device_init(). Returns pointer to
305 * the next device in the stack.
307 struct lu_device *(*ldto_device_fini)(const struct lu_context *ctx,
311 * Initialize device type. This is called on module load.
313 int (*ldto_init)(struct lu_device_type *t);
315 * Finalize device type. Dual to ->ldto_init(). Called on module
318 void (*ldto_fini)(struct lu_device_type *t);
322 * Flags for the object layers.
324 enum lu_object_flags {
326 * this flags is set if ->loo_object_init() has been called for this
327 * layer. Used by lu_object_alloc().
329 LU_OBJECT_ALLOCATED = (1 << 0)
333 * Common object attributes.
337 LA_ATIME = OBD_MD_FLATIME,
338 LA_MTIME = OBD_MD_FLMTIME,
339 LA_CTIME = OBD_MD_FLCTIME,
340 LA_SIZE = OBD_MD_FLSIZE,
341 LA_BLOCKS = OBD_MD_FLBLOCKS,
342 LA_MODE = OBD_MD_FLMODE,
343 LA_TYPE = OBD_MD_FLTYPE,
344 LA_UID = OBD_MD_FLUID,
345 LA_GID = OBD_MD_FLGID,
346 LA_FLAGS = OBD_MD_FLFLAGS,
347 LA_NLINK = OBD_MD_FLNLINK,
348 LA_RDEV = OBD_MD_FLRDEV,
352 __u64 la_size; /* size in bytes */
353 __u64 la_mtime; /* modification time in seconds since Epoch */
354 __u64 la_atime; /* access time in seconds since Epoch */
355 __u64 la_ctime; /* change time in seconds since Epoch */
356 __u64 la_blocks; /* 512-byte blocks allocated to object */
357 __u32 la_mode; /* permission bits and file type */
358 __u32 la_uid; /* owner id */
359 __u32 la_gid; /* group id */
360 __u32 la_flags; /* object flags */
361 __u32 la_nlink; /* number of persistent references to this
363 __u32 la_rdev; /* real device */
364 __u64 la_valid; /* valid bits */
369 * Layer in the layered object.
373 * Header for this object.
375 struct lu_object_header *lo_header;
377 * Device for this layer.
379 struct lu_device *lo_dev;
381 * Operations for this object.
383 struct lu_object_operations *lo_ops;
385 * Linkage into list of all layers.
387 struct list_head lo_linkage;
389 * Depth. Top level layer depth is 0.
393 * Flags from enum lu_object_flags.
395 unsigned long lo_flags;
398 enum lu_object_header_flags {
400 * Don't keep this object in cache. Object will be destroyed as soon
401 * as last reference to it is released. This flag cannot be cleared
404 LU_OBJECT_HEARD_BANSHEE = 0,
408 * "Compound" object, consisting of multiple layers.
410 * Compound object with given fid is unique with given lu_site.
412 * Note, that object does *not* necessary correspond to the real object in the
413 * persistent storage: object is an anchor for locking and method calling, so
414 * it is created for things like not-yet-existing child created by mkdir or
415 * create calls. ->loo_exists() can be used to check whether object is backed
416 * by persistent storage entity.
418 struct lu_object_header {
420 * Object flags from enum lu_object_header_flags. Set and checked
423 unsigned long loh_flags;
425 * Object reference count. Protected by site guard lock.
429 * Fid, uniquely identifying this object.
431 struct lu_fid loh_fid;
433 * Linkage into per-site hash table. Protected by site guard lock.
435 struct hlist_node loh_hash;
437 * Linkage into per-site LRU list. Protected by site guard lock.
439 struct list_head loh_lru;
441 * Linkage into list of layers. Never modified once set (except lately
442 * during object destruction). No locking is necessary.
444 struct list_head loh_layers;
450 * lu_site is a "compartment" within which objects are unique, and LRU
451 * discipline is maintained.
453 * lu_site exists so that multiple layered stacks can co-exist in the same
456 * lu_site has the same relation to lu_device as lu_object_header to
463 * - ->ls_hash hash table (and its linkages in objects);
465 * - ->ls_lru list (and its linkages in objects);
467 * - 0/1 transitions of object ->loh_ref reference count;
473 * Hash-table where objects are indexed by fid.
475 struct hlist_head *ls_hash;
477 * Bit-mask for hash-table size.
483 * LRU list, updated on each access to object. Protected by
486 * "Cold" end of LRU is ->ls_lru.next. Accessed object are moved to
487 * the ->ls_lru.prev (this is due to the non-existence of
488 * list_for_each_entry_safe_reverse()).
490 struct list_head ls_lru;
492 * Total number of objects in this site. Protected by ->ls_guard.
496 * Total number of objects in this site with reference counter greater
497 * than 0. Protected by ->ls_guard.
502 * Top-level device for this stack.
504 struct lu_device *ls_top_dev;
506 * mds number of this site.
510 * Fid location database
512 struct lu_server_fld *ls_server_fld;
513 struct lu_client_fld *ls_client_fld;
518 struct lu_server_seq *ls_server_seq;
521 * Controller Seq Manager
523 struct lu_server_seq *ls_ctlr_seq;
526 * Clienbt Seq Manager
528 struct lu_client_seq *ls_client_seq;
530 /* sequence controller node */
531 struct obd_export *ls_ctlr_exp;
533 /* statistical counters. Protected by nothing, races are accepted. */
539 * Number of hash-table entry checks made.
541 * ->s_cache_check / (->s_cache_miss + ->s_cache_hit)
543 * is an average number of hash slots inspected during single
547 /* raced cache insertions */
554 * Constructors/destructors.
558 * Initialize site @s, with @d as the top level device.
560 int lu_site_init(struct lu_site *s, struct lu_device *d);
562 * Finalize @s and release its resources.
564 void lu_site_fini(struct lu_site *s);
567 * Acquire additional reference on device @d
569 void lu_device_get(struct lu_device *d);
571 * Release reference on device @d.
573 void lu_device_put(struct lu_device *d);
576 * Initialize device @d of type @t.
578 int lu_device_init(struct lu_device *d, struct lu_device_type *t);
580 * Finalize device @d.
582 void lu_device_fini(struct lu_device *d);
585 * Initialize compound object.
587 int lu_object_header_init(struct lu_object_header *h);
589 * Finalize compound object.
591 void lu_object_header_fini(struct lu_object_header *h);
594 * Initialize object @o that is part of compound object @h and was created by
597 int lu_object_init(struct lu_object *o,
598 struct lu_object_header *h, struct lu_device *d);
600 * Finalize object and release its resources.
602 void lu_object_fini(struct lu_object *o);
604 * Add object @o as first layer of compound object @h.
606 * This is typically called by the ->ldo_object_alloc() method of top-level
609 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o);
611 * Add object @o as a layer of compound object, going after @before.1
613 * This is typically called by the ->ldo_object_alloc() method of
616 void lu_object_add(struct lu_object *before, struct lu_object *o);
619 * Caching and reference counting.
623 * Acquire additional reference to the given object. This function is used to
624 * attain additional reference. To acquire initial reference use
627 static inline void lu_object_get(struct lu_object *o)
629 LASSERT(o->lo_header->loh_ref > 0);
630 spin_lock(&o->lo_dev->ld_site->ls_guard);
631 o->lo_header->loh_ref ++;
632 spin_unlock(&o->lo_dev->ld_site->ls_guard);
636 * Return true of object will not be cached after last reference to it is
639 static inline int lu_object_is_dying(const struct lu_object_header *h)
641 return test_bit(LU_OBJECT_HEARD_BANSHEE, &h->loh_flags);
645 * Decrease reference counter on object. If last reference is freed, return
646 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
647 * case, free object immediately.
649 void lu_object_put(const struct lu_context *ctxt,
650 struct lu_object *o);
653 * Free @nr objects from the cold end of the site LRU list.
655 void lu_site_purge(const struct lu_context *ctx,
656 struct lu_site *s, int nr);
659 * Search cache for an object with the fid @f. If such object is found, return
660 * it. Otherwise, create new object, insert it into cache and return it. In
661 * any case, additional reference is acquired on the returned object.
663 struct lu_object *lu_object_find(const struct lu_context *ctxt,
664 struct lu_site *s, const struct lu_fid *f);
671 * First (topmost) sub-object of given compound object
673 static inline struct lu_object *lu_object_top(struct lu_object_header *h)
675 LASSERT(!list_empty(&h->loh_layers));
676 return container_of0(h->loh_layers.next, struct lu_object, lo_linkage);
680 * Next sub-object in the layering
682 static inline struct lu_object *lu_object_next(const struct lu_object *o)
684 return container_of0(o->lo_linkage.next, struct lu_object, lo_linkage);
688 * Pointer to the fid of this object.
690 static inline const struct lu_fid *lu_object_fid(const struct lu_object *o)
692 return &o->lo_header->loh_fid;
696 * return device operations vector for this object
698 static inline struct lu_device_operations *
699 lu_object_ops(const struct lu_object *o)
701 return o->lo_dev->ld_ops;
705 * Given a compound object, find its slice, corresponding to the device type
708 struct lu_object *lu_object_locate(struct lu_object_header *h,
709 struct lu_device_type *dtype);
711 struct lu_cdebug_print_info {
714 const char *lpi_file;
720 * Printer function emitting messages through libcfs_debug_msg().
722 int lu_cdebug_printer(const struct lu_context *ctx,
723 void *cookie, const char *format, ...);
726 * Print object description followed by user-supplied message.
728 #define LU_OBJECT_DEBUG(mask, ctx, object, format, ...) \
730 static struct lu_cdebug_print_info __info = { \
731 .lpi_subsys = DEBUG_SUBSYSTEM, \
732 .lpi_mask = (mask), \
733 .lpi_file = __FILE__, \
734 .lpi_fn = __FUNCTION__, \
735 .lpi_line = __LINE__ \
737 lu_object_print(ctx, &__info, lu_cdebug_printer, object); \
738 CDEBUG(mask, format , ## __VA_ARGS__); \
742 * Print human readable representation of the @o to the @f.
744 void lu_object_print(const struct lu_context *ctxt, void *cookie,
745 lu_printer_t printer, const struct lu_object *o);
748 * Check object consistency.
750 int lu_object_invariant(const struct lu_object *o);
753 * Returns 1 iff object @o exists on the stable storage,
754 * returns -1 iff object @o is on remote server.
756 static inline int lu_object_exists(const struct lu_context *ctx,
757 const struct lu_object *o)
759 return o->lo_ops->loo_object_exists(ctx, o);
762 static inline int lu_object_assert_exists(const struct lu_context *ctx,
763 const struct lu_object *o)
765 return lu_object_exists(ctx, o) != 0;
768 static inline int lu_object_assert_not_exists(const struct lu_context *ctx,
769 const struct lu_object *o)
771 return lu_object_exists(ctx, o) <= 0;
775 /* input params, should be filled out by mdt */
776 __u32 rp_hash; /* hash */
777 int rp_count; /* count in bytes */
778 int rp_npages; /* number of pages */
779 struct page **rp_pages; /* pointers to pages */
782 enum lu_xattr_flags {
783 LU_XATTR_REPLACE = (1 << 0),
784 LU_XATTR_CREATE = (1 << 1)
788 * lu_context. Execution context for lu_object methods. Currently associated
791 * All lu_object methods, except device and device type methods (called during
792 * system initialization and shutdown) are executed "within" some
793 * lu_context. This means, that pointer to some "current" lu_context is passed
794 * as an argument to all methods.
796 * All service ptlrpc threads create lu_context as part of their
797 * initialization. It is possible to create "stand-alone" context for other
798 * execution environments (like system calls).
800 * lu_object methods mainly use lu_context through lu_context_key interface
801 * that allows each layer to associate arbitrary pieces of data with each
802 * context (see pthread_key_create(3) for similar interface).
807 * Theoretically we'd want to use lu_objects and lu_contexts on the
808 * client side too. On the other hand, we don't want to allocate
809 * values of server-side keys for the client contexts and vice versa.
811 * To achieve this, set of tags in introduced. Contexts and keys are
812 * marked with tags. Key value are created only for context whose set
813 * of tags has non-empty intersection with one for key. Tags are taken
814 * from enum lu_context_tag.
818 * Pointer to the home service thread. NULL for other execution
821 struct ptlrpc_thread *lc_thread;
823 * Pointer to an array with key values. Internal implementation
830 * lu_context_key interface. Similar to pthread_key.
833 enum lu_context_tag {
834 LCT_MD_THREAD = 1 << 0,
835 LCT_DT_THREAD = 1 << 1,
836 LCT_TX_HANDLE = 1 << 2,
837 LCT_CL_THREAD = 1 << 3
841 * Key. Represents per-context value slot.
843 struct lu_context_key {
845 * Set of tags for which values of this key are to be instantiated.
849 * Value constructor. This is called when new value is created for a
850 * context. Returns pointer to new value of error pointer.
852 void *(*lct_init)(const struct lu_context *ctx,
853 struct lu_context_key *key);
855 * Value destructor. Called when context with previously allocated
856 * value of this slot is destroyed. @data is a value that was returned
857 * by a matching call to ->lct_init().
859 void (*lct_fini)(const struct lu_context *ctx,
860 struct lu_context_key *key, void *data);
862 * Internal implementation detail: index within ->lc_value[] reserved
867 * Internal implementation detail: number of values created for this
876 int lu_context_key_register(struct lu_context_key *key);
880 void lu_context_key_degister(struct lu_context_key *key);
882 * Return value associated with key @key in context @ctx.
884 void *lu_context_key_get(const struct lu_context *ctx,
885 struct lu_context_key *key);
888 * Initialize context data-structure. Create values for all keys.
890 int lu_context_init(struct lu_context *ctx, __u32 tags);
892 * Finalize context data-structure. Destroy key values.
894 void lu_context_fini(struct lu_context *ctx);
897 * Called before entering context.
899 void lu_context_enter(struct lu_context *ctx);
901 * Called after exiting from @ctx
903 void lu_context_exit(struct lu_context *ctx);
906 * One-time initializers, called at obdclass module initialization, not
911 * Initialization of global lu_* data.
913 int lu_global_init(void);
916 * Dual to lu_global_init().
918 void lu_global_fini(void);
920 #endif /* __LUSTRE_LU_OBJECT_H */