Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / include / lu_object.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2006 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #ifndef __LUSTRE_LU_OBJECT_H
24 #define __LUSTRE_LU_OBJECT_H
25
26 #include <stdarg.h>
27
28 /*
29  * struct lu_fid
30  */
31 #include <lustre/lustre_idl.h>
32
33 #include <libcfs/list.h>
34 #include <libcfs/kp30.h>
35
36 /*
37  * Layered objects support for CMD3/C5.
38  */
39
40 struct seq_file;
41 struct proc_dir_entry;
42 struct lustre_cfg;
43 struct lprocfs_stats;
44
45 /*
46  * lu_* data-types represent server-side entities shared by data and meta-data
47  * stacks.
48  *
49  * Design goals:
50  *
51  * 0. support for layering.
52  *
53  *     Server side object is split into layers, one per device in the
54  *     corresponding device stack. Individual layer is represented by struct
55  *     lu_object. Compound layered object --- by struct lu_object_header. Most
56  *     interface functions take lu_object as an argument and operate on the
57  *     whole compound object. This decision was made due to the following
58  *     reasons:
59  *
60  *        - it's envisaged that lu_object will be used much more often than
61  *        lu_object_header;
62  *
63  *        - we want lower (non-top) layers to be able to initiate operations
64  *        on the whole object.
65  *
66  *     Generic code supports layering more complex than simple stacking, e.g.,
67  *     it is possible that at some layer object "spawns" multiple sub-objects
68  *     on the lower layer.
69  *
70  * 1. fid-based identification.
71  *
72  *     Compound object is uniquely identified by its fid. Objects are indexed
73  *     by their fids (hash table is used for index).
74  *
75  * 2. caching and life-cycle management.
76  *
77  *     Object's life-time is controlled by reference counting. When reference
78  *     count drops to 0, object is returned to cache. Cached objects still
79  *     retain their identity (i.e., fid), and can be recovered from cache.
80  *
81  *     Objects are kept in the global LRU list, and lu_site_purge() function
82  *     can be used to reclaim given number of unused objects from the tail of
83  *     the LRU.
84  *
85  * 3. avoiding recursion.
86  *
87  *     Generic code tries to replace recursion through layers by iterations
88  *     where possible. Additionally to the end of reducing stack consumption,
89  *     data, when practically possible, are allocated through lu_context_key
90  *     interface rather than on stack.
91  *
92  */
93
94 struct lu_site;
95 struct lu_object;
96 struct lu_device;
97 struct lu_object_header;
98 struct lu_context;
99 struct lu_env;
100
101 /*
102  * Operations common for data and meta-data devices.
103  */
104 struct lu_device_operations {
105         /*
106          * Object creation protocol.
107          *
108          * Due to design goal of avoiding recursion, object creation (see
109          * lu_object_alloc()) is somewhat involved:
110          *
111          *  - first, ->ldo_object_alloc() method of the top-level device
112          *  in the stack is called. It should allocate top level object
113          *  (including lu_object_header), but without any lower-layer
114          *  sub-object(s).
115          *
116          *  - then lu_object_alloc() sets fid in the header of newly created
117          *  object.
118          *
119          *  - then ->loo_object_init() (a method from struct
120          *  lu_object_operations) is called. It has to allocate lower-layer
121          *  object(s). To do this, ->loo_object_init() calls
122          *  ldo_object_alloc() of the lower-layer device(s).
123          *
124          *  - for all new objects allocated by ->loo_object_init() (and
125          *  inserted into object stack), ->loo_object_init() is called again
126          *  repeatedly, until no new objects are created.
127          *
128          */
129
130         /*
131          * Allocate object for the given device (without lower-layer
132          * parts). This is called by ->loo_object_init() from the parent
133          * layer, and should setup at least ->lo_dev and ->lo_ops fields of
134          * resulting lu_object.
135          *
136          * postcondition: ergo(!IS_ERR(result), result->lo_dev ==  d &&
137          *                                      result->lo_ops != NULL);
138          */
139         struct lu_object *(*ldo_object_alloc)(const struct lu_env *env,
140                                               const struct lu_object_header *h,
141                                               struct lu_device *d);
142         /*
143          * process config specific for device
144          */
145         int (*ldo_process_config)(const struct lu_env *env,
146                                   struct lu_device *, struct lustre_cfg *);
147         int (*ldo_recovery_complete)(const struct lu_env *,
148                                      struct lu_device *);
149
150 };
151
152 /*
153  * Type of "printer" function used by ->loo_object_print() method.
154  *
155  * Printer function is needed to provide some flexibility in (semi-)debugging
156  * output: possible implementations: printk, CDEBUG, sysfs/seq_file
157  */
158 typedef int (*lu_printer_t)(const struct lu_env *env,
159                             void *cookie, const char *format, ...)
160         __attribute__ ((format (printf, 3, 4)));
161
162 /*
163  * Operations specific for particular lu_object.
164  */
165 struct lu_object_operations {
166
167         /*
168          * Allocate lower-layer parts of the object by calling
169          * ->ldo_object_alloc() of the corresponding underlying device.
170          *
171          * This method is called once for each object inserted into object
172          * stack. It's responsibility of this method to insert lower-layer
173          * object(s) it create into appropriate places of object stack.
174          */
175         int (*loo_object_init)(const struct lu_env *env,
176                                struct lu_object *o);
177         /*
178          * Called (in top-to-bottom order) during object allocation after all
179          * layers were allocated and initialized. Can be used to perform
180          * initialization depending on lower layers.
181          */
182         int (*loo_object_start)(const struct lu_env *env,
183                                 struct lu_object *o);
184         /*
185          * Called before ->loo_object_free() to signal that object is being
186          * destroyed. Dual to ->loo_object_init().
187          */
188         void (*loo_object_delete)(const struct lu_env *env,
189                                   struct lu_object *o);
190
191         /*
192          * Dual to ->ldo_object_alloc(). Called when object is removed from
193          * memory.
194          */
195         void (*loo_object_free)(const struct lu_env *env,
196                                 struct lu_object *o);
197
198         /*
199          * Called when last active reference to the object is released (and
200          * object returns to the cache). This method is optional.
201          */
202         void (*loo_object_release)(const struct lu_env *env,
203                                    struct lu_object *o);
204         /*
205          * Debugging helper. Print given object.
206          */
207         int (*loo_object_print)(const struct lu_env *env, void *cookie,
208                                 lu_printer_t p, const struct lu_object *o);
209         /*
210          * Optional debugging method. Returns true iff method is internally
211          * consistent.
212          */
213         int (*loo_object_invariant)(const struct lu_object *o);
214 };
215
216 /*
217  * Type of lu_device.
218  */
219 struct lu_device_type;
220
221 /*
222  * Device: a layer in the server side abstraction stacking.
223  */
224 struct lu_device {
225         /*
226          * reference count. This is incremented, in particular, on each object
227          * created at this layer.
228          *
229          * XXX which means that atomic_t is probably too small.
230          */
231         atomic_t                     ld_ref;
232         /*
233          * Pointer to device type. Never modified once set.
234          */
235         struct lu_device_type       *ld_type;
236         /*
237          * Operation vector for this device.
238          */
239         struct lu_device_operations *ld_ops;
240         /*
241          * Stack this device belongs to.
242          */
243         struct lu_site              *ld_site;
244         struct proc_dir_entry       *ld_proc_entry;
245
246         /* XXX: temporary back pointer into obd. */
247         struct obd_device           *ld_obd;
248 };
249
250 struct lu_device_type_operations;
251
252 /*
253  * Tag bits for device type. They are used to distinguish certain groups of
254  * device types.
255  */
256 enum lu_device_tag {
257         /* this is meta-data device */
258         LU_DEVICE_MD = (1 << 0),
259         /* this is data device */
260         LU_DEVICE_DT = (1 << 1)
261 };
262
263 /*
264  * Type of device.
265  */
266 struct lu_device_type {
267         /*
268          * Tag bits. Taken from enum lu_device_tag. Never modified once set.
269          */
270         __u32                             ldt_tags;
271         /*
272          * Name of this class. Unique system-wide. Never modified once set.
273          */
274         char                             *ldt_name;
275         /*
276          * Operations for this type.
277          */
278         struct lu_device_type_operations *ldt_ops;
279         /*
280          * XXX: temporary pointer to associated obd_type.
281          */
282         struct obd_type                  *ldt_obd_type;
283         /*
284          * XXX: temporary: context tags used by obd_*() calls.
285          */
286         __u32                             ldt_ctx_tags;
287 };
288
289 /*
290  * Operations on a device type.
291  */
292 struct lu_device_type_operations {
293         /*
294          * Allocate new device.
295          */
296         struct lu_device *(*ldto_device_alloc)(const struct lu_env *env,
297                                                struct lu_device_type *t,
298                                                struct lustre_cfg *lcfg);
299         /*
300          * Free device. Dual to ->ldto_device_alloc(). Returns pointer to
301          * the next device in the stack.
302          */
303         struct lu_device *(*ldto_device_free)(const struct lu_env *,
304                                               struct lu_device *);
305
306         /*
307          * Initialize the devices after allocation
308          */
309         int  (*ldto_device_init)(const struct lu_env *env,
310                                  struct lu_device *, const char *,
311                                  struct lu_device *);
312         /*
313          * Finalize device. Dual to ->ldto_device_init(). Returns pointer to
314          * the next device in the stack.
315          */
316         struct lu_device *(*ldto_device_fini)(const struct lu_env *env,
317                                               struct lu_device *);
318
319         /*
320          * Initialize device type. This is called on module load.
321          */
322         int  (*ldto_init)(struct lu_device_type *t);
323         /*
324          * Finalize device type. Dual to ->ldto_init(). Called on module
325          * unload.
326          */
327         void (*ldto_fini)(struct lu_device_type *t);
328 };
329
330 /*
331  * Flags for the object layers.
332  */
333 enum lu_object_flags {
334         /*
335          * this flags is set if ->loo_object_init() has been called for this
336          * layer. Used by lu_object_alloc().
337          */
338         LU_OBJECT_ALLOCATED = (1 << 0)
339 };
340
341 /*
342  * Common object attributes.
343  */
344 /* valid flags */
345 enum la_valid {
346         LA_ATIME = 1 << 0,
347         LA_MTIME = 1 << 1,
348         LA_CTIME = 1 << 2,
349         LA_SIZE  = 1 << 3,
350         LA_MODE  = 1 << 4,
351         LA_UID   = 1 << 5,
352         LA_GID   = 1 << 6,
353         LA_BLOCKS = 1 << 7,
354         LA_TYPE   = 1 << 8,
355         LA_FLAGS  = 1 << 9,
356         LA_NLINK  = 1 << 10,
357         LA_RDEV   = 1 << 11,
358         LA_BLKSIZE = 1 << 12,
359         LA_TRUNC = 1 << 13,
360 };
361
362 struct lu_attr {
363         __u64          la_size;   /* size in bytes */
364         __u64          la_mtime;  /* modification time in seconds since Epoch */
365         __u64          la_atime;  /* access time in seconds since Epoch */
366         __u64          la_ctime;  /* change time in seconds since Epoch */
367         __u64          la_blocks; /* 512-byte blocks allocated to object */
368         __u32          la_mode;   /* permission bits and file type */
369         __u32          la_uid;    /* owner id */
370         __u32          la_gid;    /* group id */
371         __u32          la_flags;  /* object flags */
372         __u32          la_nlink;  /* number of persistent references to this
373                                    * object */
374         __u32          la_blkbits; /* blk bits of the object*/
375         __u32          la_blksize; /* blk size of the object*/
376
377         __u32          la_rdev;   /* real device */
378         __u64          la_valid;  /* valid bits */
379 };
380
381 /*
382  * Layer in the layered object.
383  */
384 struct lu_object {
385         /*
386          * Header for this object.
387          */
388         struct lu_object_header     *lo_header;
389         /*
390          * Device for this layer.
391          */
392         struct lu_device            *lo_dev;
393         /*
394          * Operations for this object.
395          */
396         struct lu_object_operations *lo_ops;
397         /*
398          * Linkage into list of all layers.
399          */
400         struct list_head             lo_linkage;
401         /*
402          * Depth. Top level layer depth is 0.
403          */
404         int                          lo_depth;
405         /*
406          * Flags from enum lu_object_flags.
407          */
408         unsigned long                lo_flags;
409 };
410
411 enum lu_object_header_flags {
412         /*
413          * Don't keep this object in cache. Object will be destroyed as soon
414          * as last reference to it is released. This flag cannot be cleared
415          * once set.
416          */
417         LU_OBJECT_HEARD_BANSHEE = 0
418 };
419
420 enum lu_object_header_attr {
421         LOHA_EXISTS   = 1 << 0,
422         LOHA_REMOTE   = 1 << 1,
423         /*
424          * UNIX file type is stored in S_IFMT bits.
425          */
426         LOHA_FT_START = 1 << 12, /* S_IFIFO */
427         LOHA_FT_END   = 1 << 15, /* S_IFREG */
428 };
429
430 /*
431  * "Compound" object, consisting of multiple layers.
432  *
433  * Compound object with given fid is unique with given lu_site.
434  *
435  * Note, that object does *not* necessary correspond to the real object in the
436  * persistent storage: object is an anchor for locking and method calling, so
437  * it is created for things like not-yet-existing child created by mkdir or
438  * create calls. ->loo_exists() can be used to check whether object is backed
439  * by persistent storage entity.
440  */
441 struct lu_object_header {
442         /*
443          * Object flags from enum lu_object_header_flags. Set and checked
444          * atomically.
445          */
446         unsigned long     loh_flags;
447         /*
448          * Object reference count. Protected by site guard lock.
449          */
450         atomic_t          loh_ref;
451         /*
452          * Fid, uniquely identifying this object.
453          */
454         struct lu_fid     loh_fid;
455         /*
456          * Common object attributes, cached for efficiency. From enum
457          * lu_object_header_attr.
458          */
459         __u32             loh_attr;
460         /*
461          * Linkage into per-site hash table. Protected by site guard lock.
462          */
463         struct hlist_node loh_hash;
464         /*
465          * Linkage into per-site LRU list. Protected by site guard lock.
466          */
467         struct list_head  loh_lru;
468         /*
469          * Linkage into list of layers. Never modified once set (except lately
470          * during object destruction). No locking is necessary.
471          */
472         struct list_head  loh_layers;
473 };
474
475 struct fld;
476
477 /*
478  * lu_site is a "compartment" within which objects are unique, and LRU
479  * discipline is maintained.
480  *
481  * lu_site exists so that multiple layered stacks can co-exist in the same
482  * address space.
483  *
484  * lu_site has the same relation to lu_device as lu_object_header to
485  * lu_object.
486  */
487 struct lu_site {
488         /*
489          * lock protecting:
490          *
491          *        - ->ls_hash hash table (and its linkages in objects);
492          *
493          *        - ->ls_lru list (and its linkages in objects);
494          *
495          *        - 0/1 transitions of object ->loh_ref reference count;
496          *
497          * yes, it's heavy.
498          */
499         rwlock_t              ls_guard;
500         /*
501          * Hash-table where objects are indexed by fid.
502          */
503         struct hlist_head    *ls_hash;
504         /*
505          * Bit-mask for hash-table size.
506          */
507         int                   ls_hash_mask;
508         /*
509          * Order of hash-table.
510          */
511         int                   ls_hash_bits;
512         /*
513          * Number of buckets in the hash-table.
514          */
515         int                   ls_hash_size;
516
517         /*
518          * LRU list, updated on each access to object. Protected by
519          * ->ls_guard.
520          *
521          * "Cold" end of LRU is ->ls_lru.next. Accessed object are moved to
522          * the ->ls_lru.prev (this is due to the non-existence of
523          * list_for_each_entry_safe_reverse()).
524          */
525         struct list_head      ls_lru;
526         /*
527          * Total number of objects in this site. Protected by ->ls_guard.
528          */
529         unsigned              ls_total;
530         /*
531          * Total number of objects in this site with reference counter greater
532          * than 0. Protected by ->ls_guard.
533          */
534         unsigned              ls_busy;
535
536         /*
537          * Top-level device for this stack.
538          */
539         struct lu_device     *ls_top_dev;
540         /*
541          * mds number of this site.
542          */
543         mdsno_t               ls_node_id;
544         /*
545          * Fid location database
546          */
547         struct lu_server_fld *ls_server_fld;
548         struct lu_client_fld *ls_client_fld;
549
550         /*
551          * Server Seq Manager
552          */
553         struct lu_server_seq *ls_server_seq;
554
555         /*
556          * Controller Seq Manager
557          */
558         struct lu_server_seq *ls_control_seq;
559         struct obd_export    *ls_control_exp;
560
561         /*
562          * Client Seq Manager
563          */
564         struct lu_client_seq *ls_client_seq;
565
566         /* statistical counters. Protected by nothing, races are accepted. */
567         struct {
568                 __u32 s_created;
569                 __u32 s_cache_hit;
570                 __u32 s_cache_miss;
571                 /*
572                  * Number of hash-table entry checks made.
573                  *
574                  *       ->s_cache_check / (->s_cache_miss + ->s_cache_hit)
575                  *
576                  * is an average number of hash slots inspected during single
577                  * lookup.
578                  */
579                 __u32 s_cache_check;
580                 /* raced cache insertions */
581                 __u32 s_cache_race;
582                 __u32 s_lru_purged;
583         } ls_stats;
584
585         /*
586          * Linkage into global list of sites.
587          */
588         struct list_head      ls_linkage;
589         struct lprocfs_stats *ls_time_stats;
590 };
591
592 /*
593  * Constructors/destructors.
594  */
595
596 /*
597  * Initialize site @s, with @d as the top level device.
598  */
599 int  lu_site_init(struct lu_site *s, struct lu_device *d);
600 /*
601  * Finalize @s and release its resources.
602  */
603 void lu_site_fini(struct lu_site *s);
604
605 /*
606  * Called when initialization of stack for this site is completed.
607  */
608 int lu_site_init_finish(struct lu_site *s);
609
610 /*
611  * Acquire additional reference on device @d
612  */
613 void lu_device_get(struct lu_device *d);
614 /*
615  * Release reference on device @d.
616  */
617 void lu_device_put(struct lu_device *d);
618
619 /*
620  * Initialize device @d of type @t.
621  */
622 int lu_device_init(struct lu_device *d, struct lu_device_type *t);
623 /*
624  * Finalize device @d.
625  */
626 void lu_device_fini(struct lu_device *d);
627
628 /*
629  * Initialize compound object.
630  */
631 int lu_object_header_init(struct lu_object_header *h);
632 /*
633  * Finalize compound object.
634  */
635 void lu_object_header_fini(struct lu_object_header *h);
636
637 /*
638  * Initialize object @o that is part of compound object @h and was created by
639  * device @d.
640  */
641 int lu_object_init(struct lu_object *o,
642                    struct lu_object_header *h, struct lu_device *d);
643 /*
644  * Finalize object and release its resources.
645  */
646 void lu_object_fini(struct lu_object *o);
647 /*
648  * Add object @o as first layer of compound object @h.
649  *
650  * This is typically called by the ->ldo_object_alloc() method of top-level
651  * device.
652  */
653 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o);
654 /*
655  * Add object @o as a layer of compound object, going after @before.1
656  *
657  * This is typically called by the ->ldo_object_alloc() method of
658  * @before->lo_dev.
659  */
660 void lu_object_add(struct lu_object *before, struct lu_object *o);
661
662 /*
663  * Caching and reference counting.
664  */
665
666 /*
667  * Acquire additional reference to the given object. This function is used to
668  * attain additional reference. To acquire initial reference use
669  * lu_object_find().
670  */
671 static inline void lu_object_get(struct lu_object *o)
672 {
673         LASSERT(atomic_read(&o->lo_header->loh_ref) > 0);
674         atomic_inc(&o->lo_header->loh_ref);
675 }
676
677 /*
678  * Return true of object will not be cached after last reference to it is
679  * released.
680  */
681 static inline int lu_object_is_dying(const struct lu_object_header *h)
682 {
683         return test_bit(LU_OBJECT_HEARD_BANSHEE, &h->loh_flags);
684 }
685
686 /*
687  * Decrease reference counter on object. If last reference is freed, return
688  * object to the cache, unless lu_object_is_dying(o) holds. In the latter
689  * case, free object immediately.
690  */
691 void lu_object_put(const struct lu_env *env,
692                    struct lu_object *o);
693
694 /*
695  * Free @nr objects from the cold end of the site LRU list.
696  */
697 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr);
698
699 /*
700  * Print all objects in @s.
701  */
702 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
703                    lu_printer_t printer);
704 /*
705  * Search cache for an object with the fid @f. If such object is found, return
706  * it. Otherwise, create new object, insert it into cache and return it. In
707  * any case, additional reference is acquired on the returned object.
708  */
709 struct lu_object *lu_object_find(const struct lu_env *env,
710                                  struct lu_site *s, const struct lu_fid *f);
711
712 /*
713  * Helpers.
714  */
715
716 /*
717  * First (topmost) sub-object of given compound object
718  */
719 static inline struct lu_object *lu_object_top(struct lu_object_header *h)
720 {
721         LASSERT(!list_empty(&h->loh_layers));
722         return container_of0(h->loh_layers.next, struct lu_object, lo_linkage);
723 }
724
725 /*
726  * Next sub-object in the layering
727  */
728 static inline struct lu_object *lu_object_next(const struct lu_object *o)
729 {
730         return container_of0(o->lo_linkage.next, struct lu_object, lo_linkage);
731 }
732
733 /*
734  * Pointer to the fid of this object.
735  */
736 static inline const struct lu_fid *lu_object_fid(const struct lu_object *o)
737 {
738         return &o->lo_header->loh_fid;
739 }
740
741 /*
742  * return device operations vector for this object
743  */
744 static inline struct lu_device_operations *
745 lu_object_ops(const struct lu_object *o)
746 {
747         return o->lo_dev->ld_ops;
748 }
749
750 /*
751  * Given a compound object, find its slice, corresponding to the device type
752  * @dtype.
753  */
754 struct lu_object *lu_object_locate(struct lu_object_header *h,
755                                    struct lu_device_type *dtype);
756
757 struct lu_cdebug_print_info {
758         int         lpi_subsys;
759         int         lpi_mask;
760         const char *lpi_file;
761         const char *lpi_fn;
762         int         lpi_line;
763 };
764
765 /*
766  * Printer function emitting messages through libcfs_debug_msg().
767  */
768 int lu_cdebug_printer(const struct lu_env *env,
769                       void *cookie, const char *format, ...);
770
771 #define DECLARE_LU_CDEBUG_PRINT_INFO(var, mask) \
772         struct lu_cdebug_print_info var = {     \
773                 .lpi_subsys = DEBUG_SUBSYSTEM,  \
774                 .lpi_mask   = (mask),           \
775                 .lpi_file   = __FILE__,         \
776                 .lpi_fn     = __FUNCTION__,     \
777                 .lpi_line   = __LINE__          \
778         };
779
780 /*
781  * Print object description followed by user-supplied message.
782  */
783 #define LU_OBJECT_DEBUG(mask, env, object, format, ...)                 \
784 ({                                                                      \
785         static DECLARE_LU_CDEBUG_PRINT_INFO(__info, mask);              \
786                                                                         \
787         lu_object_print(env, &__info, lu_cdebug_printer, object);       \
788         CDEBUG(mask, format , ## __VA_ARGS__);                          \
789 })
790
791 /*
792  * Print human readable representation of the @o to the @f.
793  */
794 void lu_object_print(const struct lu_env *env, void *cookie,
795                      lu_printer_t printer, const struct lu_object *o);
796
797 /*
798  * Check object consistency.
799  */
800 int lu_object_invariant(const struct lu_object *o);
801
802 /*
803  * Finalize and free devices in the device stack.
804  */
805 void lu_stack_fini(const struct lu_env *env, struct lu_device *top);
806
807 /*
808  * Returns 1 iff object @o exists on the stable storage,
809  * returns -1 iff object @o is on remote server.
810  */
811 static inline int lu_object_exists(const struct lu_object *o)
812 {
813         __u32 attr;
814
815         attr = o->lo_header->loh_attr;
816         if (attr & LOHA_REMOTE)
817                 return -1;
818         else if (attr & LOHA_EXISTS)
819                 return +1;
820         else
821                 return 0;
822 }
823
824 static inline int lu_object_assert_exists(const struct lu_object *o)
825 {
826         return lu_object_exists(o) != 0;
827 }
828
829 static inline int lu_object_assert_not_exists(const struct lu_object *o)
830 {
831         return lu_object_exists(o) <= 0;
832 }
833
834 /*
835  * Attr of this object.
836  */
837 static inline __u32 lu_object_attr(const struct lu_object *o)
838 {
839         LASSERT(lu_object_exists(o) > 0);
840         return o->lo_header->loh_attr;
841 }
842
843 struct lu_rdpg {
844         /* input params, should be filled out by mdt */
845         __u64                   rp_hash;        /* hash */
846         int                     rp_count;       /* count in bytes       */
847         int                     rp_npages;      /* number of pages      */
848         struct page           **rp_pages;       /* pointers to pages    */
849 };
850
851 enum lu_xattr_flags {
852         LU_XATTR_REPLACE = (1 << 0),
853         LU_XATTR_CREATE  = (1 << 1)
854 };
855
856 /* For lu_context health-checks */
857 enum lu_context_state {
858         LCS_INITIALIZED = 1,
859         LCS_ENTERED,
860         LCS_LEFT,
861         LCS_FINALIZED
862 };
863
864 /*
865  * lu_context. Execution context for lu_object methods. Currently associated
866  * with thread.
867  *
868  * All lu_object methods, except device and device type methods (called during
869  * system initialization and shutdown) are executed "within" some
870  * lu_context. This means, that pointer to some "current" lu_context is passed
871  * as an argument to all methods.
872  *
873  * All service ptlrpc threads create lu_context as part of their
874  * initialization. It is possible to create "stand-alone" context for other
875  * execution environments (like system calls).
876  *
877  * lu_object methods mainly use lu_context through lu_context_key interface
878  * that allows each layer to associate arbitrary pieces of data with each
879  * context (see pthread_key_create(3) for similar interface).
880  *
881  */
882 struct lu_context {
883         /*
884          * Theoretically we'd want to use lu_objects and lu_contexts on the
885          * client side too. On the other hand, we don't want to allocate
886          * values of server-side keys for the client contexts and vice versa.
887          *
888          * To achieve this, set of tags in introduced. Contexts and keys are
889          * marked with tags. Key value are created only for context whose set
890          * of tags has non-empty intersection with one for key. Tags are taken
891          * from enum lu_context_tag.
892          */
893         __u32                  lc_tags;
894         /*
895          * Pointer to the home service thread. NULL for other execution
896          * contexts.
897          */
898         struct ptlrpc_thread  *lc_thread;
899         /*
900          * Pointer to an array with key values. Internal implementation
901          * detail.
902          */
903         void                 **lc_value;
904         enum lu_context_state  lc_state;
905 };
906
907 /*
908  * lu_context_key interface. Similar to pthread_key.
909  */
910
911 enum lu_context_tag {
912         /*
913          * Thread on md server
914          */
915         LCT_MD_THREAD = 1 << 0,
916         /*
917          * Thread on dt server
918          */
919         LCT_DT_THREAD = 1 << 1,
920         /*
921          * Context for transaction handle
922          */
923         LCT_TX_HANDLE = 1 << 2,
924         /*
925          * Thread on client
926          */
927         LCT_CL_THREAD = 1 << 3,
928         /*
929          * Per-request session on server
930          */
931         LCT_SESSION   = 1 << 4,
932         /*
933          * Don't add references for modules creating key values in that context.
934          * This is only for contexts used internally by lu_object framework.
935          */
936         LCT_NOREF     = 1 << 30,
937         /*
938          * Contexts usable in cache shrinker thread.
939          */
940         LCT_SHRINKER  = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD|LCT_NOREF
941 };
942
943 /*
944  * Key. Represents per-context value slot.
945  */
946 struct lu_context_key {
947         /*
948          * Set of tags for which values of this key are to be instantiated.
949          */
950         __u32 lct_tags;
951         /*
952          * Value constructor. This is called when new value is created for a
953          * context. Returns pointer to new value of error pointer.
954          */
955         void  *(*lct_init)(const struct lu_context *ctx,
956                            struct lu_context_key *key);
957         /*
958          * Value destructor. Called when context with previously allocated
959          * value of this slot is destroyed. @data is a value that was returned
960          * by a matching call to ->lct_init().
961          */
962         void   (*lct_fini)(const struct lu_context *ctx,
963                            struct lu_context_key *key, void *data);
964         /*
965          * Optional method called on lu_context_exit() for all allocated
966          * keys. Can be used by debugging code checking that locks are
967          * released, etc.
968          */
969         void   (*lct_exit)(const struct lu_context *ctx,
970                            struct lu_context_key *key, void *data);
971         /*
972          * Internal implementation detail: index within ->lc_value[] reserved
973          * for this key.
974          */
975         int      lct_index;
976         /*
977          * Internal implementation detail: number of values created for this
978          * key.
979          */
980         atomic_t lct_used;
981         /*
982          * Internal implementation detail: module for this key.
983          */
984         struct module *lct_owner;
985 };
986
987 #define LU_KEY_INIT(mod, type)                                    \
988         static void* mod##_key_init(const struct lu_context *ctx, \
989                                     struct lu_context_key *key)   \
990         {                                                         \
991                 type *value;                                      \
992                                                                   \
993                 CLASSERT(CFS_PAGE_SIZE >= sizeof (*value));       \
994                                                                   \
995                 OBD_ALLOC_PTR(value);                             \
996                 if (value == NULL)                                \
997                         value = ERR_PTR(-ENOMEM);                 \
998                                                                   \
999                 return value;                                     \
1000         }                                                         \
1001         struct __##mod##__dummy_init {;} /* semicolon catcher */
1002
1003 #define LU_KEY_FINI(mod, type)                                              \
1004         static void mod##_key_fini(const struct lu_context *ctx,            \
1005                                     struct lu_context_key *key, void* data) \
1006         {                                                                   \
1007                 type *info = data;                                          \
1008                                                                             \
1009                 OBD_FREE_PTR(info);                                         \
1010         }                                                                   \
1011         struct __##mod##__dummy_fini {;} /* semicolon catcher */
1012
1013 #define LU_KEY_INIT_FINI(mod, type)   \
1014         LU_KEY_INIT(mod,type);        \
1015         LU_KEY_FINI(mod,type)
1016
1017 #define LU_CONTEXT_KEY_DEFINE(mod, tags)                \
1018         struct lu_context_key mod##_thread_key = {      \
1019                 .lct_tags = tags,                       \
1020                 .lct_init = mod##_key_init,             \
1021                 .lct_fini = mod##_key_fini              \
1022         }
1023
1024 #define LU_CONTEXT_KEY_INIT(key)                        \
1025 do {                                                    \
1026         (key)->lct_owner = THIS_MODULE;                 \
1027 } while (0)
1028
1029
1030 /*
1031  * Register new key.
1032  */
1033 int   lu_context_key_register(struct lu_context_key *key);
1034 /*
1035  * Deregister key.
1036  */
1037 void  lu_context_key_degister(struct lu_context_key *key);
1038
1039 #define LU_KEY_REGISTER_GENERIC(mod)                                             \
1040         static int mod##_key_register_generic(struct lu_context_key *k, ...)     \
1041         {                                                                        \
1042                 struct lu_context_key* key = k;                                  \
1043                 va_list args;                                                    \
1044                 int result;                                                      \
1045                                                                                  \
1046                 va_start(args, k);                                               \
1047                                                                                  \
1048                 do {                                                             \
1049                         LU_CONTEXT_KEY_INIT(key);                                \
1050                         result = lu_context_key_register(key);                   \
1051                         if (result)                                              \
1052                                 break;                                           \
1053                         key = va_arg(args, struct lu_context_key*);              \
1054                 } while (key != NULL);                                           \
1055                                                                                  \
1056                 va_end(args);                                                    \
1057                                                                                  \
1058                 if (result) {                                                    \
1059                         va_start(args, k);                                       \
1060                         while (k != key) {                                       \
1061                                 lu_context_key_degister(k);                      \
1062                                 k = va_arg(args, struct lu_context_key*);        \
1063                         }                                                        \
1064                         va_end(args);                                            \
1065                 }                                                                \
1066                                                                                  \
1067                 return result;                                                   \
1068         }
1069
1070 #define LU_KEY_DEGISTER_GENERIC(mod)                                             \
1071         static void mod##_key_degister_generic(struct lu_context_key *k, ...)    \
1072         {                                                                        \
1073                 va_list args;                                                    \
1074                                                                                  \
1075                 va_start(args, k);                                               \
1076                                                                                  \
1077                 do {                                                             \
1078                         lu_context_key_degister(k);                              \
1079                         k = va_arg(args, struct lu_context_key*);                \
1080                 } while (k != NULL);                                             \
1081                                                                                  \
1082                 va_end(args);                                                    \
1083         }
1084
1085 #define LU_TYPE_INIT(mod, ...)                                         \
1086         LU_KEY_REGISTER_GENERIC(mod)                                   \
1087         static int mod##_type_init(struct lu_device_type *t)           \
1088         {                                                              \
1089                 return mod##_key_register_generic(__VA_ARGS__, NULL);  \
1090         }                                                              \
1091         struct __##mod##_dummy_type_init {;}
1092
1093 #define LU_TYPE_FINI(mod, ...)                                         \
1094         LU_KEY_DEGISTER_GENERIC(mod)                                   \
1095         static void mod##_type_fini(struct lu_device_type *t)          \
1096         {                                                              \
1097                 mod##_key_degister_generic(__VA_ARGS__, NULL);         \
1098         }                                                              \
1099         struct __##mod##_dummy_type_fini {;}
1100
1101 #define LU_TYPE_INIT_FINI(mod, ...)                                 \
1102         LU_TYPE_INIT(mod, __VA_ARGS__);                             \
1103         LU_TYPE_FINI(mod, __VA_ARGS__)
1104
1105 /*
1106  * Return value associated with key @key in context @ctx.
1107  */
1108 void *lu_context_key_get(const struct lu_context *ctx,
1109                          struct lu_context_key *key);
1110
1111 /*
1112  * Initialize context data-structure. Create values for all keys.
1113  */
1114 int  lu_context_init(struct lu_context *ctx, __u32 tags);
1115 /*
1116  * Finalize context data-structure. Destroy key values.
1117  */
1118 void lu_context_fini(struct lu_context *ctx);
1119
1120 /*
1121  * Called before entering context.
1122  */
1123 void lu_context_enter(struct lu_context *ctx);
1124 /*
1125  * Called after exiting from @ctx
1126  */
1127 void lu_context_exit(struct lu_context *ctx);
1128
1129 /*
1130  * Allocate for context all missing keys that were registered after context
1131  * creation.
1132  */
1133 int lu_context_refill(const struct lu_context *ctx);
1134
1135 /*
1136  * Environment.
1137  */
1138 struct lu_env {
1139         /*
1140          * "Local" context, used to store data instead of stack.
1141          */
1142         struct lu_context  le_ctx;
1143         /*
1144          * "Session" context for per-request data.
1145          */
1146         struct lu_context *le_ses;
1147 };
1148
1149 int  lu_env_init(struct lu_env *env, struct lu_context *ses, __u32 tags);
1150 void lu_env_fini(struct lu_env *env);
1151
1152 /*
1153  * Common name structure to be passed around for various name related methods.
1154  */
1155 struct lu_name {
1156         char    *ln_name;
1157         int      ln_namelen;
1158 };
1159
1160 /*
1161  * Common buffer structure to be passed around for various xattr_{s,g}et()
1162  * methods.
1163  */
1164 struct lu_buf {
1165         void   *lb_buf;
1166         ssize_t lb_len;
1167 };
1168
1169 extern struct lu_buf LU_BUF_NULL; /* null buffer */
1170
1171 #define DLUBUF "(%p %z)"
1172 #define PLUBUF(buf) (buf)->lb_buf, (buf)->lb_len
1173 /*
1174  * One-time initializers, called at obdclass module initialization, not
1175  * exported.
1176  */
1177
1178 /*
1179  * Initialization of global lu_* data.
1180  */
1181 int lu_global_init(void);
1182
1183 /*
1184  * Dual to lu_global_init().
1185  */
1186 void lu_global_fini(void);
1187
1188 enum {
1189         LU_TIME_FIND_LOOKUP,
1190         LU_TIME_FIND_ALLOC,
1191         LU_TIME_FIND_INSERT,
1192         LU_TIME_NR
1193 };
1194
1195 extern const char *lu_time_names[LU_TIME_NR];
1196
1197 #endif /* __LUSTRE_LU_OBJECT_H */