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