Whamcloud - gitweb
LU-12780 quota: don't use ptlrpc_thread for qmt_reba_thread
[fs/lustre-release.git] / lustre / include / lu_object.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef __LUSTRE_LU_OBJECT_H
34 #define __LUSTRE_LU_OBJECT_H
35
36 #include <stdarg.h>
37 #include <libcfs/libcfs.h>
38 #include <uapi/linux/lustre/lustre_idl.h>
39 #include <lu_ref.h>
40 #include <linux/percpu_counter.h>
41 #include <linux/ctype.h>
42
43 struct seq_file;
44 struct proc_dir_entry;
45 struct lustre_cfg;
46 struct lprocfs_stats;
47 struct obd_type;
48
49 /** \defgroup lu lu
50  * lu_* data-types represent server-side entities shared by data and meta-data
51  * stacks.
52  *
53  * Design goals:
54  *
55  * -# support for layering.
56  *
57  *     Server side object is split into layers, one per device in the
58  *     corresponding device stack. Individual layer is represented by struct
59  *     lu_object. Compound layered object --- by struct lu_object_header. Most
60  *     interface functions take lu_object as an argument and operate on the
61  *     whole compound object. This decision was made due to the following
62  *     reasons:
63  *
64  *        - it's envisaged that lu_object will be used much more often than
65  *        lu_object_header;
66  *
67  *        - we want lower (non-top) layers to be able to initiate operations
68  *        on the whole object.
69  *
70  *     Generic code supports layering more complex than simple stacking, e.g.,
71  *     it is possible that at some layer object "spawns" multiple sub-objects
72  *     on the lower layer.
73  *
74  * -# fid-based identification.
75  *
76  *     Compound object is uniquely identified by its fid. Objects are indexed
77  *     by their fids (hash table is used for index).
78  *
79  * -# caching and life-cycle management.
80  *
81  *     Object's life-time is controlled by reference counting. When reference
82  *     count drops to 0, object is returned to cache. Cached objects still
83  *     retain their identity (i.e., fid), and can be recovered from cache.
84  *
85  *     Objects are kept in the global LRU list, and lu_site_purge() function
86  *     can be used to reclaim given number of unused objects from the tail of
87  *     the LRU.
88  *
89  * -# avoiding recursion.
90  *
91  *     Generic code tries to replace recursion through layers by iterations
92  *     where possible. Additionally to the end of reducing stack consumption,
93  *     data, when practically possible, are allocated through lu_context_key
94  *     interface rather than on stack.
95  * @{
96  */
97
98 struct lu_site;
99 struct lu_object;
100 struct lu_device;
101 struct lu_object_header;
102 struct lu_context;
103 struct lu_env;
104
105 /**
106  * Operations common for data and meta-data devices.
107  */
108 struct lu_device_operations {
109         /**
110          * Allocate object for the given device (without lower-layer
111          * parts). This is called by lu_object_operations::loo_object_init()
112          * from the parent layer, and should setup at least lu_object::lo_dev
113          * and lu_object::lo_ops fields of resulting lu_object.
114          *
115          * Object creation protocol.
116          *
117          * Due to design goal of avoiding recursion, object creation (see
118          * lu_object_alloc()) is somewhat involved:
119          *
120          *  - first, lu_device_operations::ldo_object_alloc() method of the
121          *  top-level device in the stack is called. It should allocate top
122          *  level object (including lu_object_header), but without any
123          *  lower-layer sub-object(s).
124          *
125          *  - then lu_object_alloc() sets fid in the header of newly created
126          *  object.
127          *
128          *  - then lu_object_operations::loo_object_init() is called. It has
129          *  to allocate lower-layer object(s). To do this,
130          *  lu_object_operations::loo_object_init() calls ldo_object_alloc()
131          *  of the lower-layer device(s).
132          *
133          *  - for all new objects allocated by
134          *  lu_object_operations::loo_object_init() (and inserted into object
135          *  stack), lu_object_operations::loo_object_init() is called again
136          *  repeatedly, until no new objects are created.
137          *
138          * \post ergo(!IS_ERR(result), result->lo_dev == d &&
139          *                             result->lo_ops != NULL);
140          */
141         struct lu_object *(*ldo_object_alloc)(const struct lu_env *env,
142                                               const struct lu_object_header *h,
143                                               struct lu_device *d);
144         /**
145          * process config specific for device.
146          */
147         int (*ldo_process_config)(const struct lu_env *env,
148                                   struct lu_device *, struct lustre_cfg *);
149         int (*ldo_recovery_complete)(const struct lu_env *,
150                                      struct lu_device *);
151
152         /**
153          * initialize local objects for device. this method called after layer has
154          * been initialized (after LCFG_SETUP stage) and before it starts serving
155          * user requests.
156          */
157
158         int (*ldo_prepare)(const struct lu_env *,
159                            struct lu_device *parent,
160                            struct lu_device *dev);
161
162 };
163
164 /**
165  * For lu_object_conf flags
166  */
167 typedef enum {
168         /* This is a new object to be allocated, or the file
169          * corresponding to the object does not exists. */
170         LOC_F_NEW       = 0x00000001,
171 } loc_flags_t;
172
173 /**
174  * Object configuration, describing particulars of object being created. On
175  * server this is not used, as server objects are full identified by fid. On
176  * client configuration contains struct lustre_md.
177  */
178 struct lu_object_conf {
179         /**
180          * Some hints for obj find and alloc.
181          */
182         loc_flags_t     loc_flags;
183 };
184
185 /**
186  * Type of "printer" function used by lu_object_operations::loo_object_print()
187  * method.
188  *
189  * Printer function is needed to provide some flexibility in (semi-)debugging
190  * output: possible implementations: printk, CDEBUG, sysfs/seq_file
191  */
192 typedef int (*lu_printer_t)(const struct lu_env *env,
193                             void *cookie, const char *format, ...)
194         __attribute__ ((format (printf, 3, 4)));
195
196 /**
197  * Operations specific for particular lu_object.
198  */
199 struct lu_object_operations {
200
201         /**
202          * Allocate lower-layer parts of the object by calling
203          * lu_device_operations::ldo_object_alloc() of the corresponding
204          * underlying device.
205          *
206          * This method is called once for each object inserted into object
207          * stack. It's responsibility of this method to insert lower-layer
208          * object(s) it create into appropriate places of object stack.
209          */
210         int (*loo_object_init)(const struct lu_env *env,
211                                struct lu_object *o,
212                                const struct lu_object_conf *conf);
213         /**
214          * Called (in top-to-bottom order) during object allocation after all
215          * layers were allocated and initialized. Can be used to perform
216          * initialization depending on lower layers.
217          */
218         int (*loo_object_start)(const struct lu_env *env,
219                                 struct lu_object *o);
220         /**
221          * Called before lu_object_operations::loo_object_free() to signal
222          * that object is being destroyed. Dual to
223          * lu_object_operations::loo_object_init().
224          */
225         void (*loo_object_delete)(const struct lu_env *env,
226                                   struct lu_object *o);
227         /**
228          * Dual to lu_device_operations::ldo_object_alloc(). Called when
229          * object is removed from memory.  Must use call_rcu or kfree_rcu
230          * if the object contains an lu_object_header.
231          */
232         void (*loo_object_free)(const struct lu_env *env,
233                                 struct lu_object *o);
234         /**
235          * Called when last active reference to the object is released (and
236          * object returns to the cache). This method is optional.
237          */
238         void (*loo_object_release)(const struct lu_env *env,
239                                    struct lu_object *o);
240         /**
241          * Optional debugging helper. Print given object.
242          */
243         int (*loo_object_print)(const struct lu_env *env, void *cookie,
244                                 lu_printer_t p, const struct lu_object *o);
245         /**
246          * Optional debugging method. Returns true iff method is internally
247          * consistent.
248          */
249         int (*loo_object_invariant)(const struct lu_object *o);
250 };
251
252 /**
253  * Type of lu_device.
254  */
255 struct lu_device_type;
256
257 /**
258  * Device: a layer in the server side abstraction stacking.
259  */
260 struct lu_device {
261         /**
262          * reference count. This is incremented, in particular, on each object
263          * created at this layer.
264          *
265          * \todo XXX which means that atomic_t is probably too small.
266          */
267         atomic_t                           ld_ref;
268         /**
269          * Pointer to device type. Never modified once set.
270          */
271         struct lu_device_type             *ld_type;
272         /**
273          * Operation vector for this device.
274          */
275         const struct lu_device_operations *ld_ops;
276         /**
277          * Stack this device belongs to.
278          */
279         struct lu_site                    *ld_site;
280         struct proc_dir_entry             *ld_proc_entry;
281
282         /** \todo XXX: temporary back pointer into obd. */
283         struct obd_device                 *ld_obd;
284         /**
285          * A list of references to this object, for debugging.
286          */
287         struct lu_ref                      ld_reference;
288         /**
289          * Link the device to the site.
290          **/
291         struct list_head                   ld_linkage;
292 };
293
294 struct lu_device_type_operations;
295
296 /**
297  * Tag bits for device type. They are used to distinguish certain groups of
298  * device types.
299  */
300 enum lu_device_tag {
301         /** this is meta-data device */
302         LU_DEVICE_MD = (1 << 0),
303         /** this is data device */
304         LU_DEVICE_DT = (1 << 1),
305         /** data device in the client stack */
306         LU_DEVICE_CL = (1 << 2)
307 };
308
309 /**
310  * Type of device.
311  */
312 struct lu_device_type {
313         /**
314          * Tag bits. Taken from enum lu_device_tag. Never modified once set.
315          */
316         __u32                                   ldt_tags;
317         /**
318          * Name of this class. Unique system-wide. Never modified once set.
319          */
320         char                                   *ldt_name;
321         /**
322          * Operations for this type.
323          */
324         const struct lu_device_type_operations *ldt_ops;
325         /**
326          * \todo XXX: temporary: context tags used by obd_*() calls.
327          */
328         __u32                                   ldt_ctx_tags;
329         /**
330          * Number of existing device type instances.
331          */
332         atomic_t                                ldt_device_nr;
333 };
334
335 /**
336  * Operations on a device type.
337  */
338 struct lu_device_type_operations {
339         /**
340          * Allocate new device.
341          */
342         struct lu_device *(*ldto_device_alloc)(const struct lu_env *env,
343                                                struct lu_device_type *t,
344                                                struct lustre_cfg *lcfg);
345         /**
346          * Free device. Dual to
347          * lu_device_type_operations::ldto_device_alloc(). Returns pointer to
348          * the next device in the stack.
349          */
350         struct lu_device *(*ldto_device_free)(const struct lu_env *,
351                                               struct lu_device *);
352
353         /**
354          * Initialize the devices after allocation
355          */
356         int  (*ldto_device_init)(const struct lu_env *env,
357                                  struct lu_device *, const char *,
358                                  struct lu_device *);
359         /**
360          * Finalize device. Dual to
361          * lu_device_type_operations::ldto_device_init(). Returns pointer to
362          * the next device in the stack.
363          */
364         struct lu_device *(*ldto_device_fini)(const struct lu_env *env,
365                                               struct lu_device *);
366         /**
367          * Initialize device type. This is called on module load.
368          */
369         int  (*ldto_init)(struct lu_device_type *t);
370         /**
371          * Finalize device type. Dual to
372          * lu_device_type_operations::ldto_init(). Called on module unload.
373          */
374         void (*ldto_fini)(struct lu_device_type *t);
375         /**
376          * Called when the first device is created.
377          */
378         void (*ldto_start)(struct lu_device_type *t);
379         /**
380          * Called when number of devices drops to 0.
381          */
382         void (*ldto_stop)(struct lu_device_type *t);
383 };
384
385 static inline int lu_device_is_md(const struct lu_device *d)
386 {
387         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_MD);
388 }
389
390 /**
391  * Common object attributes.
392  */
393 struct lu_attr {
394         /**
395          * valid bits
396          *
397          * \see enum la_valid
398          */
399         __u64          la_valid;
400         /** size in bytes */
401         __u64          la_size;
402         /** modification time in seconds since Epoch */
403         s64             la_mtime;
404         /** access time in seconds since Epoch */
405         s64             la_atime;
406         /** change time in seconds since Epoch */
407         s64             la_ctime;
408         /** 512-byte blocks allocated to object */
409         __u64          la_blocks;
410         /** permission bits and file type */
411         __u32          la_mode;
412         /** owner id */
413         __u32          la_uid;
414         /** group id */
415         __u32          la_gid;
416         /** object flags */
417         __u32          la_flags;
418         /** number of persistent references to this object */
419         __u32          la_nlink;
420         /** blk bits of the object*/
421         __u32          la_blkbits;
422         /** blk size of the object*/
423         __u32          la_blksize;
424         /** real device */
425         __u32          la_rdev;
426         /** project id */
427         __u32          la_projid;
428         /** set layout version to OST objects. */
429         __u32           la_layout_version;
430 };
431
432 /**
433  * Layer in the layered object.
434  */
435 struct lu_object {
436         /**
437          * Header for this object.
438          */
439         struct lu_object_header           *lo_header;
440         /**
441          * Device for this layer.
442          */
443         struct lu_device                  *lo_dev;
444         /**
445          * Operations for this object.
446          */
447         const struct lu_object_operations *lo_ops;
448         /**
449          * Linkage into list of all layers.
450          */
451         struct list_head                   lo_linkage;
452         /**
453          * Link to the device, for debugging.
454          */
455         struct lu_ref_link                 lo_dev_ref;
456 };
457
458 enum lu_object_header_flags {
459         /**
460          * Don't keep this object in cache. Object will be destroyed as soon
461          * as last reference to it is released. This flag cannot be cleared
462          * once set.
463          */
464         LU_OBJECT_HEARD_BANSHEE = 0,
465         /**
466          * Mark this object has already been taken out of cache.
467          */
468         LU_OBJECT_UNHASHED      = 1,
469         /**
470          * Object is initialized, when object is found in cache, it may not be
471          * intialized yet, the object allocator will initialize it.
472          */
473         LU_OBJECT_INITED        = 2,
474         /**
475          * Object is being purged, so mustn't be returned by
476          * htable_lookup()
477          */
478         LU_OBJECT_PURGING       = 3,
479 };
480
481 enum lu_object_header_attr {
482         LOHA_EXISTS             = 1 << 0,
483         LOHA_REMOTE             = 1 << 1,
484         LOHA_HAS_AGENT_ENTRY    = 1 << 2,
485         /**
486          * UNIX file type is stored in S_IFMT bits.
487          */
488         LOHA_FT_START           = 001 << 12, /**< S_IFIFO */
489         LOHA_FT_END             = 017 << 12, /**< S_IFMT */
490 };
491
492 /**
493  * "Compound" object, consisting of multiple layers.
494  *
495  * Compound object with given fid is unique with given lu_site.
496  *
497  * Note, that object does *not* necessary correspond to the real object in the
498  * persistent storage: object is an anchor for locking and method calling, so
499  * it is created for things like not-yet-existing child created by mkdir or
500  * create calls. lu_object_operations::loo_exists() can be used to check
501  * whether object is backed by persistent storage entity.
502  */
503 struct lu_object_header {
504         /**
505          * Fid, uniquely identifying this object.
506          */
507         struct lu_fid           loh_fid;
508         /**
509          * Object flags from enum lu_object_header_flags. Set and checked
510          * atomically.
511          */
512         unsigned long           loh_flags;
513         /**
514          * Object reference count. Protected by lu_site::ls_guard.
515          */
516         atomic_t                loh_ref;
517         /**
518          * Common object attributes, cached for efficiency. From enum
519          * lu_object_header_attr.
520          */
521         __u32                   loh_attr;
522         /**
523          * Linkage into per-site hash table. Protected by lu_site::ls_guard.
524          */
525         struct hlist_node       loh_hash;
526         /**
527          * Linkage into per-site LRU list. Protected by lu_site::ls_guard.
528          */
529         struct list_head        loh_lru;
530         /**
531          * Linkage into list of layers. Never modified once set (except lately
532          * during object destruction). No locking is necessary.
533          */
534         struct list_head        loh_layers;
535         /**
536          * A list of references to this object, for debugging.
537          */
538         struct lu_ref           loh_reference;
539         /*
540          * Handle used for kfree_rcu() or similar.
541          */
542         struct rcu_head         loh_rcu;
543 };
544
545 struct fld;
546
547 enum {
548         LU_SS_CREATED           = 0,
549         LU_SS_CACHE_HIT,
550         LU_SS_CACHE_MISS,
551         LU_SS_CACHE_RACE,
552         LU_SS_CACHE_DEATH_RACE,
553         LU_SS_LRU_PURGED,
554         LU_SS_LAST_STAT
555 };
556
557 /**
558  * lu_site is a "compartment" within which objects are unique, and LRU
559  * discipline is maintained.
560  *
561  * lu_site exists so that multiple layered stacks can co-exist in the same
562  * address space.
563  *
564  * lu_site has the same relation to lu_device as lu_object_header to
565  * lu_object.
566  */
567 struct lu_site {
568         /**
569          * objects hash table
570          */
571         struct cfs_hash         *ls_obj_hash;
572         /*
573          * buckets for summary data
574          */
575         struct lu_site_bkt_data *ls_bkts;
576         int                     ls_bkt_cnt;
577         u32                     ls_bkt_seed;
578         /**
579          * index of bucket on hash table while purging
580          */
581         unsigned int            ls_purge_start;
582         /**
583          * Top-level device for this stack.
584          */
585         struct lu_device        *ls_top_dev;
586         /**
587          * Bottom-level device for this stack
588          */
589         struct lu_device        *ls_bottom_dev;
590         /**
591          * Linkage into global list of sites.
592          */
593         struct list_head        ls_linkage;
594         /**
595          * List for lu device for this site, protected
596          * by ls_ld_lock.
597          **/
598         struct list_head        ls_ld_linkage;
599         spinlock_t              ls_ld_lock;
600         /**
601          * Lock to serialize site purge.
602          */
603         struct mutex            ls_purge_mutex;
604         /**
605          * lu_site stats
606          */
607         struct lprocfs_stats    *ls_stats;
608         /**
609          * XXX: a hack! fld has to find md_site via site, remove when possible
610          */
611         struct seq_server_site  *ld_seq_site;
612         /**
613          * Pointer to the lu_target for this site.
614          */
615         struct lu_target        *ls_tgt;
616
617         /**
618          * Number of objects in lsb_lru_lists - used for shrinking
619          */
620         struct percpu_counter   ls_lru_len_counter;
621 };
622
623 wait_queue_head_t *
624 lu_site_wq_from_fid(struct lu_site *site, struct lu_fid *fid);
625
626 static inline struct seq_server_site *lu_site2seq(const struct lu_site *s)
627 {
628         return s->ld_seq_site;
629 }
630
631 /** \name ctors
632  * Constructors/destructors.
633  * @{
634  */
635
636 int  lu_site_init         (struct lu_site *s, struct lu_device *d);
637 void lu_site_fini         (struct lu_site *s);
638 int  lu_site_init_finish  (struct lu_site *s);
639 void lu_stack_fini        (const struct lu_env *env, struct lu_device *top);
640 void lu_device_get        (struct lu_device *d);
641 void lu_device_put        (struct lu_device *d);
642 int  lu_device_init       (struct lu_device *d, struct lu_device_type *t);
643 void lu_device_fini       (struct lu_device *d);
644 int  lu_object_header_init(struct lu_object_header *h);
645 void lu_object_header_fini(struct lu_object_header *h);
646 int  lu_object_init       (struct lu_object *o,
647                            struct lu_object_header *h, struct lu_device *d);
648 void lu_object_fini       (struct lu_object *o);
649 void lu_object_add_top    (struct lu_object_header *h, struct lu_object *o);
650 void lu_object_add        (struct lu_object *before, struct lu_object *o);
651
652 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d);
653 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d);
654
655 /**
656  * Helpers to initialize and finalize device types.
657  */
658
659 int  lu_device_type_init(struct lu_device_type *ldt);
660 void lu_device_type_fini(struct lu_device_type *ldt);
661
662 /** @} ctors */
663
664 /** \name caching
665  * Caching and reference counting.
666  * @{
667  */
668
669 /**
670  * Acquire additional reference to the given object. This function is used to
671  * attain additional reference. To acquire initial reference use
672  * lu_object_find().
673  */
674 static inline void lu_object_get(struct lu_object *o)
675 {
676         LASSERT(atomic_read(&o->lo_header->loh_ref) > 0);
677         atomic_inc(&o->lo_header->loh_ref);
678 }
679
680 /**
681  * Return true if object will not be cached after last reference to it is
682  * released.
683  */
684 static inline int lu_object_is_dying(const struct lu_object_header *h)
685 {
686         return test_bit(LU_OBJECT_HEARD_BANSHEE, &h->loh_flags);
687 }
688
689 /**
690  * Return true if object is initialized.
691  */
692 static inline int lu_object_is_inited(const struct lu_object_header *h)
693 {
694         return test_bit(LU_OBJECT_INITED, &h->loh_flags);
695 }
696
697 void lu_object_put(const struct lu_env *env, struct lu_object *o);
698 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o);
699 void lu_object_unhash(const struct lu_env *env, struct lu_object *o);
700 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s, int nr,
701                           int canblock);
702
703 static inline int lu_site_purge(const struct lu_env *env, struct lu_site *s,
704                                 int nr)
705 {
706         return lu_site_purge_objects(env, s, nr, 1);
707 }
708
709 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
710                    lu_printer_t printer);
711 struct lu_object *lu_object_find(const struct lu_env *env,
712                                  struct lu_device *dev, const struct lu_fid *f,
713                                  const struct lu_object_conf *conf);
714 struct lu_object *lu_object_find_at(const struct lu_env *env,
715                                     struct lu_device *dev,
716                                     const struct lu_fid *f,
717                                     const struct lu_object_conf *conf);
718 struct lu_object *lu_object_find_slice(const struct lu_env *env,
719                                        struct lu_device *dev,
720                                        const struct lu_fid *f,
721                                        const struct lu_object_conf *conf);
722 /** @} caching */
723
724 /** \name helpers
725  * Helpers.
726  * @{
727  */
728
729 /**
730  * First (topmost) sub-object of given compound object
731  */
732 static inline struct lu_object *lu_object_top(struct lu_object_header *h)
733 {
734         LASSERT(!list_empty(&h->loh_layers));
735         return container_of0(h->loh_layers.next, struct lu_object, lo_linkage);
736 }
737
738 /**
739  * Next sub-object in the layering
740  */
741 static inline struct lu_object *lu_object_next(const struct lu_object *o)
742 {
743         return container_of0(o->lo_linkage.next, struct lu_object, lo_linkage);
744 }
745
746 /**
747  * Pointer to the fid of this object.
748  */
749 static inline const struct lu_fid *lu_object_fid(const struct lu_object *o)
750 {
751         return &o->lo_header->loh_fid;
752 }
753
754 /**
755  * return device operations vector for this object
756  */
757 static const 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  * \a dtype.
766  */
767 struct lu_object *lu_object_locate(struct lu_object_header *h,
768                                    const struct lu_device_type *dtype);
769
770 /**
771  * Printer function emitting messages through libcfs_debug_msg().
772  */
773 int lu_cdebug_printer(const struct lu_env *env,
774                       void *cookie, const char *format, ...);
775
776 /**
777  * Print object description followed by a user-supplied message.
778  */
779 #define LU_OBJECT_DEBUG(mask, env, object, format, ...)                   \
780 do {                                                                      \
781         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                     \
782                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, NULL);          \
783                 lu_object_print(env, &msgdata, lu_cdebug_printer, object);\
784                 CDEBUG(mask, format "\n", ## __VA_ARGS__);                \
785         }                                                                 \
786 } while (0)
787
788 /**
789  * Print short object description followed by a user-supplied message.
790  */
791 #define LU_OBJECT_HEADER(mask, env, object, format, ...)                \
792 do {                                                                    \
793         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                   \
794                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, NULL);        \
795                 lu_object_header_print(env, &msgdata, lu_cdebug_printer,\
796                                        (object)->lo_header);            \
797                 lu_cdebug_printer(env, &msgdata, "\n");                 \
798                 CDEBUG(mask, format , ## __VA_ARGS__);                  \
799         }                                                               \
800 } while (0)
801
802 void lu_object_print       (const struct lu_env *env, void *cookie,
803                             lu_printer_t printer, const struct lu_object *o);
804 void lu_object_header_print(const struct lu_env *env, void *cookie,
805                             lu_printer_t printer,
806                             const struct lu_object_header *hdr);
807
808 /**
809  * Check object consistency.
810  */
811 int lu_object_invariant(const struct lu_object *o);
812
813
814 /**
815  * Check whether object exists, no matter on local or remote storage.
816  * Note: LOHA_EXISTS will be set once some one created the object,
817  * and it does not needs to be committed to storage.
818  */
819 #define lu_object_exists(o) ((o)->lo_header->loh_attr & LOHA_EXISTS)
820
821 /**
822  * Check whether object on the remote storage.
823  */
824 #define lu_object_remote(o) unlikely((o)->lo_header->loh_attr & LOHA_REMOTE)
825
826 /**
827  * Check whether the object as agent entry on current target
828  */
829 #define lu_object_has_agent_entry(o) \
830         unlikely((o)->lo_header->loh_attr & LOHA_HAS_AGENT_ENTRY)
831
832 static inline void lu_object_set_agent_entry(struct lu_object *o)
833 {
834         o->lo_header->loh_attr |= LOHA_HAS_AGENT_ENTRY;
835 }
836
837 static inline void lu_object_clear_agent_entry(struct lu_object *o)
838 {
839         o->lo_header->loh_attr &= ~LOHA_HAS_AGENT_ENTRY;
840 }
841
842 static inline int lu_object_assert_exists(const struct lu_object *o)
843 {
844         return lu_object_exists(o);
845 }
846
847 static inline int lu_object_assert_not_exists(const struct lu_object *o)
848 {
849         return !lu_object_exists(o);
850 }
851
852 /**
853  * Attr of this object.
854  */
855 static inline __u32 lu_object_attr(const struct lu_object *o)
856 {
857         LASSERT(lu_object_exists(o) != 0);
858
859         return o->lo_header->loh_attr & S_IFMT;
860 }
861
862 static inline void lu_object_ref_add(struct lu_object *o,
863                                      const char *scope,
864                                      const void *source)
865 {
866         lu_ref_add(&o->lo_header->loh_reference, scope, source);
867 }
868
869 static inline void lu_object_ref_add_at(struct lu_object *o,
870                                         struct lu_ref_link *link,
871                                         const char *scope,
872                                         const void *source)
873 {
874         lu_ref_add_at(&o->lo_header->loh_reference, link, scope, source);
875 }
876
877 static inline void lu_object_ref_del(struct lu_object *o,
878                                      const char *scope, const void *source)
879 {
880         lu_ref_del(&o->lo_header->loh_reference, scope, source);
881 }
882
883 static inline void lu_object_ref_del_at(struct lu_object *o,
884                                         struct lu_ref_link *link,
885                                         const char *scope, const void *source)
886 {
887         lu_ref_del_at(&o->lo_header->loh_reference, link, scope, source);
888 }
889
890 /** input params, should be filled out by mdt */
891 struct lu_rdpg {
892         /** hash */
893         __u64                   rp_hash;
894         /** count in bytes */
895         unsigned int            rp_count;
896         /** number of pages */
897         unsigned int            rp_npages;
898         /** requested attr */
899         __u32                   rp_attrs;
900         /** pointers to pages */
901         struct page           **rp_pages;
902 };
903
904 enum lu_xattr_flags {
905         LU_XATTR_REPLACE = (1 << 0),
906         LU_XATTR_CREATE  = (1 << 1),
907         LU_XATTR_MERGE   = (1 << 2),
908         LU_XATTR_SPLIT   = (1 << 3),
909 };
910
911 /** @} helpers */
912
913 /** \name lu_context
914  * @{ */
915
916 /** For lu_context health-checks */
917 enum lu_context_state {
918         LCS_INITIALIZED = 1,
919         LCS_ENTERED,
920         LCS_LEAVING,
921         LCS_LEFT,
922         LCS_FINALIZED
923 };
924
925 /**
926  * lu_context. Execution context for lu_object methods. Currently associated
927  * with thread.
928  *
929  * All lu_object methods, except device and device type methods (called during
930  * system initialization and shutdown) are executed "within" some
931  * lu_context. This means, that pointer to some "current" lu_context is passed
932  * as an argument to all methods.
933  *
934  * All service ptlrpc threads create lu_context as part of their
935  * initialization. It is possible to create "stand-alone" context for other
936  * execution environments (like system calls).
937  *
938  * lu_object methods mainly use lu_context through lu_context_key interface
939  * that allows each layer to associate arbitrary pieces of data with each
940  * context (see pthread_key_create(3) for similar interface).
941  *
942  * On a client, lu_context is bound to a thread, see cl_env_get().
943  *
944  * \see lu_context_key
945  */
946 struct lu_context {
947         /**
948          * lu_context is used on the client side too. Yet we don't want to
949          * allocate values of server-side keys for the client contexts and
950          * vice versa.
951          *
952          * To achieve this, set of tags in introduced. Contexts and keys are
953          * marked with tags. Key value are created only for context whose set
954          * of tags has non-empty intersection with one for key. Tags are taken
955          * from enum lu_context_tag.
956          */
957         __u32                  lc_tags;
958         enum lu_context_state  lc_state;
959         /**
960          * Pointer to the home service thread. NULL for other execution
961          * contexts.
962          */
963         struct ptlrpc_thread  *lc_thread;
964         /**
965          * Pointer to an array with key values. Internal implementation
966          * detail.
967          */
968         void                  **lc_value;
969         /**
970          * Linkage into a list of all remembered contexts. Only
971          * `non-transient' contexts, i.e., ones created for service threads
972          * are placed here.
973          */
974         struct list_head        lc_remember;
975         /**
976          * Version counter used to skip calls to lu_context_refill() when no
977          * keys were registered.
978          */
979         unsigned                lc_version;
980         /**
981          * Debugging cookie.
982          */
983         unsigned                lc_cookie;
984 };
985
986 /**
987  * lu_context_key interface. Similar to pthread_key.
988  */
989
990 enum lu_context_tag {
991         /**
992          * Thread on md server
993          */
994         LCT_MD_THREAD = 1 << 0,
995         /**
996          * Thread on dt server
997          */
998         LCT_DT_THREAD = 1 << 1,
999         /**
1000          * Thread on client
1001          */
1002         LCT_CL_THREAD = 1 << 3,
1003         /**
1004          * A per-request session on a server, and a per-system-call session on
1005          * a client.
1006          */
1007         LCT_SESSION   = 1 << 4,
1008         /**
1009          * A per-request data on OSP device
1010          */
1011         LCT_OSP_THREAD = 1 << 5,
1012         /**
1013          * MGS device thread
1014          */
1015         LCT_MG_THREAD = 1 << 6,
1016         /**
1017          * Context for local operations
1018          */
1019         LCT_LOCAL = 1 << 7,
1020         /**
1021          * session for server thread
1022          **/
1023         LCT_SERVER_SESSION = 1 << 8,
1024         /**
1025          * Set when at least one of keys, having values in this context has
1026          * non-NULL lu_context_key::lct_exit() method. This is used to
1027          * optimize lu_context_exit() call.
1028          */
1029         LCT_HAS_EXIT  = 1 << 28,
1030         /**
1031          * Don't add references for modules creating key values in that context.
1032          * This is only for contexts used internally by lu_object framework.
1033          */
1034         LCT_NOREF     = 1 << 29,
1035         /**
1036          * Key is being prepared for retiring, don't create new values for it.
1037          */
1038         LCT_QUIESCENT = 1 << 30,
1039         /**
1040          * Context should be remembered.
1041          */
1042         LCT_REMEMBER  = 1 << 31,
1043         /**
1044          * Contexts usable in cache shrinker thread.
1045          */
1046         LCT_SHRINKER  = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD|LCT_NOREF
1047 };
1048
1049 /**
1050  * Key. Represents per-context value slot.
1051  *
1052  * Keys are usually registered when module owning the key is initialized, and
1053  * de-registered when module is unloaded. Once key is registered, all new
1054  * contexts with matching tags, will get key value. "Old" contexts, already
1055  * initialized at the time of key registration, can be forced to get key value
1056  * by calling lu_context_refill().
1057  *
1058  * Every key value is counted in lu_context_key::lct_used and acquires a
1059  * reference on an owning module. This means, that all key values have to be
1060  * destroyed before module can be unloaded. This is usually achieved by
1061  * stopping threads started by the module, that created contexts in their
1062  * entry functions. Situation is complicated by the threads shared by multiple
1063  * modules, like ptlrpcd daemon on a client. To work around this problem,
1064  * contexts, created in such threads, are `remembered' (see
1065  * LCT_REMEMBER)---i.e., added into a global list. When module is preparing
1066  * for unloading it does the following:
1067  *
1068  *     - marks its keys as `quiescent' (lu_context_tag::LCT_QUIESCENT)
1069  *       preventing new key values from being allocated in the new contexts,
1070  *       and
1071  *
1072  *     - scans a list of remembered contexts, destroying values of module
1073  *       keys, thus releasing references to the module.
1074  *
1075  * This is done by lu_context_key_quiesce(). If module is re-activated
1076  * before key has been de-registered, lu_context_key_revive() call clears
1077  * `quiescent' marker.
1078  *
1079  * lu_context code doesn't provide any internal synchronization for these
1080  * activities---it's assumed that startup (including threads start-up) and
1081  * shutdown are serialized by some external means.
1082  *
1083  * \see lu_context
1084  */
1085 struct lu_context_key {
1086         /**
1087          * Set of tags for which values of this key are to be instantiated.
1088          */
1089         __u32 lct_tags;
1090         /**
1091          * Value constructor. This is called when new value is created for a
1092          * context. Returns pointer to new value of error pointer.
1093          */
1094         void  *(*lct_init)(const struct lu_context *ctx,
1095                            struct lu_context_key *key);
1096         /**
1097          * Value destructor. Called when context with previously allocated
1098          * value of this slot is destroyed. \a data is a value that was returned
1099          * by a matching call to lu_context_key::lct_init().
1100          */
1101         void   (*lct_fini)(const struct lu_context *ctx,
1102                            struct lu_context_key *key, void *data);
1103         /**
1104          * Optional method called on lu_context_exit() for all allocated
1105          * keys. Can be used by debugging code checking that locks are
1106          * released, etc.
1107          */
1108         void   (*lct_exit)(const struct lu_context *ctx,
1109                            struct lu_context_key *key, void *data);
1110         /**
1111          * Internal implementation detail: index within lu_context::lc_value[]
1112          * reserved for this key.
1113          */
1114         int             lct_index;
1115         /**
1116          * Internal implementation detail: number of values created for this
1117          * key.
1118          */
1119         atomic_t        lct_used;
1120         /**
1121          * Internal implementation detail: module for this key.
1122          */
1123         struct module   *lct_owner;
1124         /**
1125          * References to this key. For debugging.
1126          */
1127         struct lu_ref   lct_reference;
1128 };
1129
1130 #define LU_KEY_INIT(mod, type)                                    \
1131         static void *mod##_key_init(const struct lu_context *ctx, \
1132                                     struct lu_context_key *key)   \
1133         {                                                         \
1134                 type *value;                                      \
1135                                                                   \
1136                 BUILD_BUG_ON(PAGE_SIZE < sizeof(*value));         \
1137                                                                   \
1138                 OBD_ALLOC_PTR(value);                             \
1139                 if (value == NULL)                                \
1140                         value = ERR_PTR(-ENOMEM);                 \
1141                                                                   \
1142                 return value;                                     \
1143         }                                                         \
1144         struct __##mod##__dummy_init { ; } /* semicolon catcher */
1145
1146 #define LU_KEY_FINI(mod, type)                                              \
1147         static void mod##_key_fini(const struct lu_context *ctx,            \
1148                                     struct lu_context_key *key, void* data) \
1149         {                                                                   \
1150                 type *info = data;                                          \
1151                                                                             \
1152                 OBD_FREE_PTR(info);                                         \
1153         }                                                                   \
1154         struct __##mod##__dummy_fini {;} /* semicolon catcher */
1155
1156 #define LU_KEY_INIT_FINI(mod, type)   \
1157         LU_KEY_INIT(mod,type);        \
1158         LU_KEY_FINI(mod,type)
1159
1160 #define LU_CONTEXT_KEY_DEFINE(mod, tags)                \
1161         struct lu_context_key mod##_thread_key = {      \
1162                 .lct_tags = tags,                       \
1163                 .lct_init = mod##_key_init,             \
1164                 .lct_fini = mod##_key_fini              \
1165         }
1166
1167 #define LU_CONTEXT_KEY_INIT(key)                        \
1168 do {                                                    \
1169         (key)->lct_owner = THIS_MODULE;                 \
1170 } while (0)
1171
1172 int   lu_context_key_register(struct lu_context_key *key);
1173 void  lu_context_key_degister(struct lu_context_key *key);
1174 void *lu_context_key_get     (const struct lu_context *ctx,
1175                                const struct lu_context_key *key);
1176 void  lu_context_key_quiesce (struct lu_context_key *key);
1177 void  lu_context_key_revive  (struct lu_context_key *key);
1178
1179
1180 /*
1181  * LU_KEY_INIT_GENERIC() has to be a macro to correctly determine an
1182  * owning module.
1183  */
1184
1185 #define LU_KEY_INIT_GENERIC(mod)                                        \
1186         static void mod##_key_init_generic(struct lu_context_key *k, ...) \
1187         {                                                               \
1188                 struct lu_context_key *key = k;                         \
1189                 va_list args;                                           \
1190                                                                         \
1191                 va_start(args, k);                                      \
1192                 do {                                                    \
1193                         LU_CONTEXT_KEY_INIT(key);                       \
1194                         key = va_arg(args, struct lu_context_key *);    \
1195                 } while (key != NULL);                                  \
1196                 va_end(args);                                           \
1197         }
1198
1199 #define LU_TYPE_INIT(mod, ...)                                          \
1200         LU_KEY_INIT_GENERIC(mod)                                        \
1201         static int mod##_type_init(struct lu_device_type *t)            \
1202         {                                                               \
1203                 mod##_key_init_generic(__VA_ARGS__, NULL);              \
1204                 return lu_context_key_register_many(__VA_ARGS__, NULL); \
1205         }                                                               \
1206         struct __##mod##_dummy_type_init {;}
1207
1208 #define LU_TYPE_FINI(mod, ...)                                          \
1209         static void mod##_type_fini(struct lu_device_type *t)           \
1210         {                                                               \
1211                 lu_context_key_degister_many(__VA_ARGS__, NULL);        \
1212         }                                                               \
1213         struct __##mod##_dummy_type_fini {;}
1214
1215 #define LU_TYPE_START(mod, ...)                                 \
1216         static void mod##_type_start(struct lu_device_type *t)  \
1217         {                                                       \
1218                 lu_context_key_revive_many(__VA_ARGS__, NULL);  \
1219         }                                                       \
1220         struct __##mod##_dummy_type_start {;}
1221
1222 #define LU_TYPE_STOP(mod, ...)                                  \
1223         static void mod##_type_stop(struct lu_device_type *t)   \
1224         {                                                       \
1225                 lu_context_key_quiesce_many(__VA_ARGS__, NULL); \
1226         }                                                       \
1227         struct __##mod##_dummy_type_stop {;}
1228
1229
1230
1231 #define LU_TYPE_INIT_FINI(mod, ...)             \
1232         LU_TYPE_INIT(mod, __VA_ARGS__);         \
1233         LU_TYPE_FINI(mod, __VA_ARGS__);         \
1234         LU_TYPE_START(mod, __VA_ARGS__);        \
1235         LU_TYPE_STOP(mod, __VA_ARGS__)
1236
1237 int   lu_context_init  (struct lu_context *ctx, __u32 tags);
1238 void  lu_context_fini  (struct lu_context *ctx);
1239 void  lu_context_enter (struct lu_context *ctx);
1240 void  lu_context_exit  (struct lu_context *ctx);
1241 int   lu_context_refill(struct lu_context *ctx);
1242
1243 /*
1244  * Helper functions to operate on multiple keys. These are used by the default
1245  * device type operations, defined by LU_TYPE_INIT_FINI().
1246  */
1247
1248 int  lu_context_key_register_many(struct lu_context_key *k, ...);
1249 void lu_context_key_degister_many(struct lu_context_key *k, ...);
1250 void lu_context_key_revive_many  (struct lu_context_key *k, ...);
1251 void lu_context_key_quiesce_many (struct lu_context_key *k, ...);
1252
1253 /*
1254  * update/clear ctx/ses tags.
1255  */
1256 void lu_context_tags_update(__u32 tags);
1257 void lu_context_tags_clear(__u32 tags);
1258 void lu_session_tags_update(__u32 tags);
1259 void lu_session_tags_clear(__u32 tags);
1260
1261 /**
1262  * Environment.
1263  */
1264 struct lu_env {
1265         /**
1266          * "Local" context, used to store data instead of stack.
1267          */
1268         struct lu_context  le_ctx;
1269         /**
1270          * "Session" context for per-request data.
1271          */
1272         struct lu_context *le_ses;
1273 };
1274
1275 int  lu_env_init  (struct lu_env *env, __u32 tags);
1276 void lu_env_fini  (struct lu_env *env);
1277 int  lu_env_refill(struct lu_env *env);
1278 int  lu_env_refill_by_tags(struct lu_env *env, __u32 ctags, __u32 stags);
1279
1280 static inline void* lu_env_info(const struct lu_env *env,
1281                                 const struct lu_context_key *key)
1282 {
1283         void *info;
1284         info = lu_context_key_get(&env->le_ctx, key);
1285         if (!info) {
1286                 if (!lu_env_refill((struct lu_env *)env))
1287                         info = lu_context_key_get(&env->le_ctx, key);
1288         }
1289         LASSERT(info);
1290         return info;
1291 }
1292
1293 struct lu_env *lu_env_find(void);
1294 int lu_env_add(struct lu_env *env);
1295 int lu_env_add_task(struct lu_env *env, struct task_struct *task);
1296 void lu_env_remove(struct lu_env *env);
1297
1298 /** @} lu_context */
1299
1300 /**
1301  * Output site statistical counters into a buffer. Suitable for
1302  * ll_rd_*()-style functions.
1303  */
1304 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m);
1305
1306 /**
1307  * Common name structure to be passed around for various name related methods.
1308  */
1309 struct lu_name {
1310         const char    *ln_name;
1311         int            ln_namelen;
1312 };
1313
1314 static inline bool name_is_dot_or_dotdot(const char *name, int namelen)
1315 {
1316         return name[0] == '.' &&
1317                (namelen == 1 || (namelen == 2 && name[1] == '.'));
1318 }
1319
1320 static inline bool lu_name_is_dot_or_dotdot(const struct lu_name *lname)
1321 {
1322         return name_is_dot_or_dotdot(lname->ln_name, lname->ln_namelen);
1323 }
1324
1325 static inline bool lu_name_is_temp_file(const char *name, int namelen,
1326                                         bool dot_prefix, int suffixlen)
1327 {
1328         int lower = 0;
1329         int upper = 0;
1330         int digit = 0;
1331         int len = suffixlen;
1332
1333         if (dot_prefix && name[0] != '.')
1334                 return false;
1335
1336         if (namelen < dot_prefix + suffixlen + 2 ||
1337             name[namelen - suffixlen - 1] != '.')
1338                 return false;
1339
1340         while (len) {
1341                 lower += islower(name[namelen - len]);
1342                 upper += isupper(name[namelen - len]);
1343                 digit += isdigit(name[namelen - len]);
1344                 len--;
1345         }
1346         /* mktemp() filename suffixes will have a mix of upper- and lower-case
1347          * letters and/or numbers, not all numbers, or all upper or lower-case.
1348          * About 0.07% of randomly-generated names will slip through,
1349          * but this avoids 99.93% of cross-MDT renames for those files.
1350          */
1351         if (digit >= suffixlen - 2 || upper == suffixlen || lower == suffixlen)
1352                 return false;
1353
1354         return true;
1355 }
1356
1357 static inline bool lu_name_is_backup_file(const char *name, int namelen,
1358                                           int *suffixlen)
1359 {
1360         if (namelen > 1 &&
1361             name[namelen - 2] != '.' && name[namelen - 1] == '~') {
1362                 if (suffixlen)
1363                         *suffixlen = 1;
1364                 return true;
1365         }
1366
1367         if (namelen > 4 && name[namelen - 4] == '.' &&
1368             (!strncasecmp(name + namelen - 3, "bak", 3) ||
1369              !strncasecmp(name + namelen - 3, "sav", 3))) {
1370                 if (suffixlen)
1371                         *suffixlen = 4;
1372                 return true;
1373         }
1374
1375         if (namelen > 5 && name[namelen - 5] == '.' &&
1376             !strncasecmp(name + namelen - 4, "orig", 4)) {
1377                 if (suffixlen)
1378                         *suffixlen = 5;
1379                 return true;
1380         }
1381
1382         return false;
1383 }
1384
1385 static inline bool lu_name_is_valid_len(const char *name, size_t name_len)
1386 {
1387         return name != NULL &&
1388                name_len > 0 &&
1389                name_len < INT_MAX &&
1390                strlen(name) == name_len &&
1391                memchr(name, '/', name_len) == NULL;
1392 }
1393
1394 /**
1395  * Validate names (path components)
1396  *
1397  * To be valid \a name must be non-empty, '\0' terminated of length \a
1398  * name_len, and not contain '/'. The maximum length of a name (before
1399  * say -ENAMETOOLONG will be returned) is really controlled by llite
1400  * and the server. We only check for something insane coming from bad
1401  * integer handling here.
1402  */
1403 static inline bool lu_name_is_valid_2(const char *name, size_t name_len)
1404 {
1405         return lu_name_is_valid_len(name, name_len) && name[name_len] == '\0';
1406 }
1407
1408 static inline bool lu_name_is_valid(const struct lu_name *ln)
1409 {
1410         return lu_name_is_valid_2(ln->ln_name, ln->ln_namelen);
1411 }
1412
1413 #define DNAME "%.*s"
1414 #define PNAME(ln)                                       \
1415         (lu_name_is_valid(ln) ? (ln)->ln_namelen : 0),  \
1416         (lu_name_is_valid(ln) ? (ln)->ln_name : "")
1417
1418 /**
1419  * Common buffer structure to be passed around for various xattr_{s,g}et()
1420  * methods.
1421  */
1422 struct lu_buf {
1423         void   *lb_buf;
1424         size_t  lb_len;
1425 };
1426
1427 #define DLUBUF "(%p %zu)"
1428 #define PLUBUF(buf) (buf)->lb_buf, (buf)->lb_len
1429
1430 /* read buffer params, should be filled out by out */
1431 struct lu_rdbuf {
1432         /** number of buffers */
1433         unsigned int    rb_nbufs;
1434         /** pointers to buffers */
1435         struct lu_buf   rb_bufs[];
1436 };
1437
1438 /**
1439  * One-time initializers, called at obdclass module initialization, not
1440  * exported.
1441  */
1442
1443 /**
1444  * Initialization of global lu_* data.
1445  */
1446 int lu_global_init(void);
1447
1448 /**
1449  * Dual to lu_global_init().
1450  */
1451 void lu_global_fini(void);
1452
1453 struct lu_kmem_descr {
1454         struct kmem_cache **ckd_cache;
1455         const char       *ckd_name;
1456         const size_t      ckd_size;
1457 };
1458
1459 int  lu_kmem_init(struct lu_kmem_descr *caches);
1460 void lu_kmem_fini(struct lu_kmem_descr *caches);
1461
1462 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
1463                           const struct lu_fid *fid);
1464 struct lu_object *lu_object_anon(const struct lu_env *env,
1465                                  struct lu_device *dev,
1466                                  const struct lu_object_conf *conf);
1467
1468 /** null buffer */
1469 extern struct lu_buf LU_BUF_NULL;
1470
1471 void lu_buf_free(struct lu_buf *buf);
1472 void lu_buf_alloc(struct lu_buf *buf, size_t size);
1473 void lu_buf_realloc(struct lu_buf *buf, size_t size);
1474
1475 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len);
1476 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len);
1477
1478 extern __u32 lu_context_tags_default;
1479 extern __u32 lu_session_tags_default;
1480
1481 static inline bool lu_device_is_cl(const struct lu_device *d)
1482 {
1483         return d->ld_type->ldt_tags & LU_DEVICE_CL;
1484 }
1485
1486 static inline bool lu_object_is_cl(const struct lu_object *o)
1487 {
1488         return lu_device_is_cl(o->lo_dev);
1489 }
1490
1491 /* Generic subset of tgts */
1492 struct lu_tgt_pool {
1493         __u32              *op_array;   /* array of index of
1494                                          * lov_obd->lov_tgts */
1495         unsigned int        op_count;   /* number of tgts in the array */
1496         unsigned int        op_size;    /* allocated size of op_array */
1497         struct rw_semaphore op_rw_sem;  /* to protect lu_tgt_pool use */
1498 };
1499
1500 /* round-robin QoS data for LOD/LMV */
1501 struct lu_qos_rr {
1502         spinlock_t               lqr_alloc;     /* protect allocation index */
1503         __u32                    lqr_start_idx; /* start index of new inode */
1504         __u32                    lqr_offset_idx;/* aliasing for start_idx */
1505         int                      lqr_start_count;/* reseed counter */
1506         struct lu_tgt_pool       lqr_pool;      /* round-robin optimized list */
1507         unsigned long            lqr_dirty:1;   /* recalc round-robin list */
1508 };
1509
1510 /* QoS data per MDS/OSS */
1511 struct lu_svr_qos {
1512         struct obd_uuid          lsq_uuid;      /* ptlrpc's c_remote_uuid */
1513         struct list_head         lsq_svr_list;  /* link to lq_svr_list */
1514         __u64                    lsq_bavail;    /* total bytes avail on svr */
1515         __u64                    lsq_iavail;    /* tital inode avail on svr */
1516         __u64                    lsq_penalty;   /* current penalty */
1517         __u64                    lsq_penalty_per_obj; /* penalty decrease
1518                                                        * every obj*/
1519         time64_t                 lsq_used;      /* last used time, seconds */
1520         __u32                    lsq_tgt_count; /* number of tgts on this svr */
1521         __u32                    lsq_id;        /* unique svr id */
1522 };
1523
1524 /* QoS data per MDT/OST */
1525 struct lu_tgt_qos {
1526         struct lu_svr_qos       *ltq_svr;       /* svr info */
1527         __u64                    ltq_penalty;   /* current penalty */
1528         __u64                    ltq_penalty_per_obj; /* penalty decrease
1529                                                        * every obj*/
1530         __u64                    ltq_weight;    /* net weighting */
1531         time64_t                 ltq_used;      /* last used time, seconds */
1532         bool                     ltq_usable:1;  /* usable for striping */
1533 };
1534
1535 /* target descriptor */
1536 struct lu_tgt_desc {
1537         union {
1538                 struct dt_device        *ltd_tgt;
1539                 struct obd_device       *ltd_obd;
1540         };
1541         struct obd_export *ltd_exp;
1542         struct obd_uuid    ltd_uuid;
1543         __u32              ltd_index;
1544         __u32              ltd_gen;
1545         struct list_head   ltd_kill;
1546         struct task_struct *ltd_recovery_task;
1547         struct mutex       ltd_fid_mutex;
1548         struct lu_tgt_qos  ltd_qos; /* qos info per target */
1549         struct obd_statfs  ltd_statfs;
1550         time64_t           ltd_statfs_age;
1551         unsigned long      ltd_active:1,/* is this target up for requests */
1552                            ltd_activate:1,/* should target be activated */
1553                            ltd_reap:1,  /* should this target be deleted */
1554                            ltd_got_update_log:1, /* Already got update log */
1555                            ltd_connecting:1; /* target is connecting */
1556 };
1557
1558 /* number of pointers at 1st level */
1559 #define TGT_PTRS                (PAGE_SIZE / sizeof(void *))
1560 /* number of pointers at 2nd level */
1561 #define TGT_PTRS_PER_BLOCK      (PAGE_SIZE / sizeof(void *))
1562
1563 struct lu_tgt_desc_idx {
1564         struct lu_tgt_desc *ldi_tgt[TGT_PTRS_PER_BLOCK];
1565 };
1566
1567 /* QoS data for LOD/LMV */
1568 struct lu_qos {
1569         struct list_head         lq_svr_list;   /* lu_svr_qos list */
1570         struct rw_semaphore      lq_rw_sem;
1571         __u32                    lq_active_svr_count;
1572         unsigned int             lq_prio_free;   /* priority for free space */
1573         unsigned int             lq_threshold_rr;/* priority for rr */
1574         struct lu_qos_rr         lq_rr;          /* round robin qos data */
1575         unsigned long            lq_dirty:1,     /* recalc qos data */
1576                                  lq_same_space:1,/* the servers all have approx.
1577                                                   * the same space avail */
1578                                  lq_reset:1;     /* zero current penalties */
1579 };
1580
1581 struct lu_tgt_descs {
1582         union {
1583                 struct lov_desc       ltd_lov_desc;
1584                 struct lmv_desc       ltd_lmv_desc;
1585         };
1586         /* list of known TGTs */
1587         struct lu_tgt_desc_idx  *ltd_tgt_idx[TGT_PTRS];
1588         /* Size of the lu_tgts array, granted to be a power of 2 */
1589         __u32                   ltd_tgts_size;
1590         /* bitmap of TGTs available */
1591         struct cfs_bitmap       *ltd_tgt_bitmap;
1592         /* TGTs scheduled to be deleted */
1593         __u32                   ltd_death_row;
1594         /* Table refcount used for delayed deletion */
1595         int                     ltd_refcount;
1596         /* mutex to serialize concurrent updates to the tgt table */
1597         struct mutex            ltd_mutex;
1598         /* read/write semaphore used for array relocation */
1599         struct rw_semaphore     ltd_rw_sem;
1600         /* QoS */
1601         struct lu_qos           ltd_qos;
1602         /* all tgts in a packed array */
1603         struct lu_tgt_pool      ltd_tgt_pool;
1604         /* true if tgt is MDT */
1605         bool                    ltd_is_mdt;
1606 };
1607
1608 #define LTD_TGT(ltd, index)                                             \
1609          (ltd)->ltd_tgt_idx[(index) /                                   \
1610          TGT_PTRS_PER_BLOCK]->ldi_tgt[(index) % TGT_PTRS_PER_BLOCK]
1611
1612 u64 lu_prandom_u64_max(u64 ep_ro);
1613 void lu_qos_rr_init(struct lu_qos_rr *lqr);
1614 int lu_qos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd);
1615 void lu_tgt_qos_weight_calc(struct lu_tgt_desc *tgt);
1616
1617 int lu_tgt_descs_init(struct lu_tgt_descs *ltd, bool is_mdt);
1618 void lu_tgt_descs_fini(struct lu_tgt_descs *ltd);
1619 int ltd_add_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt);
1620 void ltd_del_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt);
1621 bool ltd_qos_is_usable(struct lu_tgt_descs *ltd);
1622 int ltd_qos_penalties_calc(struct lu_tgt_descs *ltd);
1623 int ltd_qos_update(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt,
1624                    __u64 *total_wt);
1625
1626 static inline struct lu_tgt_desc *ltd_first_tgt(struct lu_tgt_descs *ltd)
1627 {
1628         int index;
1629
1630         index = find_first_bit(ltd->ltd_tgt_bitmap->data,
1631                                ltd->ltd_tgt_bitmap->size);
1632         return (index < ltd->ltd_tgt_bitmap->size) ? LTD_TGT(ltd, index) : NULL;
1633 }
1634
1635 static inline struct lu_tgt_desc *ltd_next_tgt(struct lu_tgt_descs *ltd,
1636                                                struct lu_tgt_desc *tgt)
1637 {
1638         int index;
1639
1640         if (!tgt)
1641                 return NULL;
1642
1643         index = tgt->ltd_index;
1644         LASSERT(index < ltd->ltd_tgt_bitmap->size);
1645         index = find_next_bit(ltd->ltd_tgt_bitmap->data,
1646                               ltd->ltd_tgt_bitmap->size, index + 1);
1647         return (index < ltd->ltd_tgt_bitmap->size) ? LTD_TGT(ltd, index) : NULL;
1648 }
1649
1650 #define ltd_foreach_tgt(ltd, tgt) \
1651         for (tgt = ltd_first_tgt(ltd); tgt; tgt = ltd_next_tgt(ltd, tgt))
1652
1653 #define ltd_foreach_tgt_safe(ltd, tgt, tmp)                               \
1654         for (tgt = ltd_first_tgt(ltd), tmp = ltd_next_tgt(ltd, tgt); tgt; \
1655              tgt = tmp, tmp = ltd_next_tgt(ltd, tgt))
1656
1657 /** @} lu */
1658 #endif /* __LUSTRE_LU_OBJECT_H */