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