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