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