Whamcloud - gitweb
pass lu_context to ->o_connect() method
[fs/lustre-release.git] / lustre / include / obd_class.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 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 __CLASS_OBD_H
24 #define __CLASS_OBD_H
25
26 #include <obd_support.h>
27 #include <lustre_import.h>
28 #include <lustre_net.h>
29 #include <obd.h>
30 #include <lustre_lib.h>
31 #include <lustre/lustre_idl.h>
32 #include <lprocfs_status.h>
33
34 #if defined(__linux__)
35 #include <linux/obd_class.h>
36 #elif defined(__APPLE__)
37 #include <darwin/obd_class.h>
38 #elif defined(__WINNT__)
39 #include <winnt/obd_class.h>
40 #else
41 #error Unsupported operating system.
42 #endif
43
44 /* OBD Device Declarations */
45 #define MAX_OBD_DEVICES 520
46 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
47 extern spinlock_t obd_dev_lock;
48
49 /* OBD Operations Declarations */
50 extern struct obd_device *class_conn2obd(struct lustre_handle *);
51 extern struct obd_device *class_exp2obd(struct obd_export *);
52
53 struct lu_device_type;
54
55 /* genops.c */
56 struct obd_export *class_conn2export(struct lustre_handle *);
57 int class_register_type(struct obd_ops *, struct md_ops *,
58                         struct lprocfs_vars *, const char *nm,
59                         struct lu_device_type *ldt);
60 int class_unregister_type(const char *nm);
61
62 struct obd_device *class_newdev(struct obd_type *type, char *name);
63 void class_release_dev(struct obd_device *obd);
64
65 int class_name2dev(const char *name);
66 struct obd_device *class_name2obd(const char *name);
67 int class_uuid2dev(struct obd_uuid *uuid);
68 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
69 void class_obd_list(void);
70 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
71                                           const char * typ_name,
72                                           struct obd_uuid *grp_uuid);
73 struct obd_device * class_find_client_notype(struct obd_uuid *tgt_uuid,
74                                              struct obd_uuid *grp_uuid);
75 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid,
76                                            int *next);
77
78 int oig_init(struct obd_io_group **oig);
79 void oig_add_one(struct obd_io_group *oig,
80                   struct oig_callback_context *occ);
81 void oig_complete_one(struct obd_io_group *oig,
82                       struct oig_callback_context *occ, int rc);
83 void oig_release(struct obd_io_group *oig);
84 int oig_wait(struct obd_io_group *oig);
85
86 char *obd_export_nid2str(struct obd_export *exp);
87
88 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid);
89 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid);
90
91 /* obd_config.c */
92 int class_process_config(struct lustre_cfg *lcfg);
93 int class_attach(struct lustre_cfg *lcfg);
94 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
95 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
96 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
97 struct obd_device *class_incref(struct obd_device *obd);
98 void class_decref(struct obd_device *obd);
99
100 #define CFG_F_START     0x01   /* Set when we start updating from a log */
101 #define CFG_F_MARKER    0x02   /* We are within a maker */
102 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
103 #define CFG_F_COMPAT146 0x08   /* Translation to new obd names required */
104 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
105
106
107 /* Passed as data param to class_config_parse_llog */
108 struct config_llog_instance {
109         char *              cfg_instance;
110         struct super_block *cfg_sb;
111         struct obd_uuid     cfg_uuid;
112         int                 cfg_last_idx; /* for partial llog processing */
113         int                 cfg_flags;
114 };
115 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
116                             struct config_llog_instance *cfg);
117 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
118                            struct config_llog_instance *cfg);
119
120 /* list of active configuration logs  */
121 struct config_llog_data {
122         char               *cld_logname;
123         struct ldlm_res_id  cld_resid;
124         struct config_llog_instance cld_cfg;
125         struct list_head    cld_list_chain;
126         atomic_t            cld_refcount;
127         unsigned int        cld_stopping:1;
128 };
129
130 struct lustre_profile {
131         struct list_head lp_list;
132         char * lp_profile;
133         char * lp_osc;
134         char * lp_mdc;
135 };
136
137 struct lustre_profile *class_get_profile(const char * prof);
138 void class_del_profile(const char *prof);
139
140 /* genops.c */
141 #define class_export_get(exp)                                                  \
142 ({                                                                             \
143         struct obd_export *exp_ = exp;                                         \
144         atomic_inc(&exp_->exp_refcount);                                       \
145         CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp_,          \
146                atomic_read(&exp_->exp_refcount));                              \
147         exp_;                                                                  \
148 })
149
150 #define class_export_put(exp)                                                  \
151 do {                                                                           \
152         LASSERT((exp) != NULL);                                                \
153         CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", (exp),         \
154                atomic_read(&(exp)->exp_refcount) - 1);                         \
155         LASSERT(atomic_read(&(exp)->exp_refcount) > 0);                        \
156         LASSERT(atomic_read(&(exp)->exp_refcount) < 0x5a5a5a);                 \
157         __class_export_put(exp);                                               \
158 } while (0)
159 void __class_export_put(struct obd_export *);
160 struct obd_export *class_new_export(struct obd_device *obddev,
161                                     struct obd_uuid *cluuid);
162 void class_unlink_export(struct obd_export *exp);
163
164 struct obd_import *class_import_get(struct obd_import *);
165 void class_import_put(struct obd_import *);
166 struct obd_import *class_new_import(struct obd_device *obd);
167 void class_destroy_import(struct obd_import *exp);
168
169 struct obd_type *class_search_type(const char *name);
170 struct obd_type *class_get_type(const char *name);
171 void class_put_type(struct obd_type *type);
172 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
173                   struct obd_uuid *cluuid);
174 int class_disconnect(struct obd_export *exp);
175 void class_fail_export(struct obd_export *exp);
176 void class_disconnect_exports(struct obd_device *obddev);
177 void class_disconnect_stale_exports(struct obd_device *obddev);
178 int class_manual_cleanup(struct obd_device *obd);
179
180 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid);
181 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare);
182 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj);
183
184
185 #define OBT(dev)        (dev)->obd_type
186 #define OBP(dev, op)    (dev)->obd_type->typ_dt_ops->o_ ## op
187 #define MDP(dev, op)    (dev)->obd_type->typ_md_ops->m_ ## op
188 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
189
190 /* Ensure obd_setup: used for cleanup which must be called
191    while obd is stopping */
192 #define OBD_CHECK_DEV(obd)                                      \
193 do {                                                            \
194         if (!(obd)) {                                           \
195                 CERROR("NULL device\n");                        \
196                 RETURN(-ENODEV);                                \
197         }                                                       \
198 } while (0)
199
200 /* ensure obd_setup and !obd_stopping */
201 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
202 do {                                                            \
203         OBD_CHECK_DEV(obd);                                     \
204         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
205                 CERROR("Device %d not setup\n",                 \
206                        (obd)->obd_minor);                       \
207                 RETURN(-ENODEV);                                \
208         }                                                       \
209 } while (0)
210
211
212 #ifdef LPROCFS
213 #define OBD_COUNTER_OFFSET(op)                                  \
214         ((offsetof(struct obd_ops, o_ ## op) -                  \
215           offsetof(struct obd_ops, o_iocontrol))                \
216          / sizeof(((struct obd_ops *)(0))->o_iocontrol))
217
218 #define OBD_COUNTER_INCREMENT(obd, op)                          \
219         if ((obd)->obd_stats != NULL) {                         \
220                 unsigned int coffset;                           \
221                 coffset = (unsigned int)(obd)->obd_cntr_base +  \
222                         OBD_COUNTER_OFFSET(op);                 \
223                 LASSERT(coffset < obd->obd_stats->ls_num);      \
224                 lprocfs_counter_incr(obd->obd_stats, coffset);  \
225         }
226
227 #define MD_COUNTER_OFFSET(op)                                  \
228         ((offsetof(struct md_ops, m_ ## op) -                  \
229           offsetof(struct md_ops, m_getstatus))                \
230          / sizeof(((struct md_ops *)(0))->m_getstatus))
231
232 #define MD_COUNTER_INCREMENT(obd, op)                           \
233         if ((obd)->md_stats != NULL) {                          \
234                 unsigned int coffset;                           \
235                 coffset = (unsigned int)(obd)->md_cntr_base +   \
236                         MD_COUNTER_OFFSET(op);                  \
237                 LASSERT(coffset < (obd)->md_stats->ls_num);     \
238                 lprocfs_counter_incr((obd)->md_stats, coffset); \
239         }
240
241 #else
242 #define OBD_COUNTER_OFFSET(op)
243 #define OBD_COUNTER_INCREMENT(obd, op)
244 #define MD_COUNTER_INCREMENT(obd, op)
245 #endif
246
247 #define OBD_CHECK_MD_OP(obd, op, err)                           \
248 do {                                                            \
249         if (!OBT(obd) || !MDP((obd), op)) {                     \
250                 if (err)                                        \
251                         CERROR("md_" #op ": dev %s/%d no operation\n", \
252                                obd->obd_name, obd->obd_minor);  \
253                 RETURN(err);                                    \
254         }                                                       \
255 } while (0)
256
257 #define EXP_CHECK_MD_OP(exp, op)                                \
258 do {                                                            \
259         if ((exp) == NULL) {                                    \
260                 CERROR("obd_" #op ": NULL export\n");           \
261                 RETURN(-ENODEV);                                \
262         }                                                       \
263         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
264                 CERROR("obd_" #op ": cleaned up obd\n");        \
265                 RETURN(-EOPNOTSUPP);                            \
266         }                                                       \
267         if (!OBT((exp)->exp_obd) || !MDP((exp)->exp_obd, op)) { \
268                 CERROR("obd_" #op ": dev %s/%d no operation\n", \
269                        (exp)->exp_obd->obd_name,                \
270                        (exp)->exp_obd->obd_minor);              \
271                 RETURN(-EOPNOTSUPP);                            \
272         }                                                       \
273 } while (0)
274
275
276 #define OBD_CHECK_DT_OP(obd, op, err)                           \
277 do {                                                            \
278         if (!OBT(obd) || !OBP((obd), op)) {                     \
279                 if (err)                                        \
280                         CERROR("obd_" #op ": dev %d no operation\n",    \
281                                obd->obd_minor);                 \
282                 RETURN(err);                                    \
283         }                                                       \
284 } while (0)
285
286 #define EXP_CHECK_DT_OP(exp, op)                                \
287 do {                                                            \
288         if ((exp) == NULL) {                                    \
289                 CERROR("obd_" #op ": NULL export\n");           \
290                 RETURN(-ENODEV);                                \
291         }                                                       \
292         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
293                 CERROR("obd_" #op ": cleaned up obd\n");        \
294                 RETURN(-EOPNOTSUPP);                            \
295         }                                                       \
296         if (!OBT((exp)->exp_obd) || !OBP((exp)->exp_obd, op)) { \
297                 CERROR("obd_" #op ": dev %d no operation\n",    \
298                        (exp)->exp_obd->obd_minor);              \
299                 RETURN(-EOPNOTSUPP);                            \
300         }                                                       \
301 } while (0)
302
303 #define CTXT_CHECK_OP(ctxt, op, err)                                         \
304 do {                                                            \
305         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {                     \
306                 if (err)                                        \
307                         CERROR("lop_" #op ": dev %d no operation\n",    \
308                                ctxt->loc_obd->obd_minor);                         \
309                 RETURN(err);                                    \
310         }                                                       \
311 } while (0)
312
313 static inline int obd_get_info(struct obd_export *exp, __u32 keylen,
314                                void *key, __u32 *vallen, void *val)
315 {
316         int rc;
317         ENTRY;
318
319         EXP_CHECK_DT_OP(exp, get_info);
320         OBD_COUNTER_INCREMENT(exp->exp_obd, get_info);
321
322         rc = OBP(exp->exp_obd, get_info)(exp, keylen, key, vallen, val);
323         RETURN(rc);
324 }
325
326 static inline int obd_set_info_async(struct obd_export *exp, obd_count keylen,
327                                      void *key, obd_count vallen, void *val,
328                                      struct ptlrpc_request_set *set)
329 {
330         int rc;
331         ENTRY;
332
333         EXP_CHECK_DT_OP(exp, set_info_async);
334         OBD_COUNTER_INCREMENT(exp->exp_obd, set_info_async);
335
336         rc = OBP(exp->exp_obd, set_info_async)(exp, keylen, key, vallen, val,
337                                                set);
338         RETURN(rc);
339 }
340
341 #ifdef __KERNEL__
342 #define DECLARE_LU_VARS(ldt, d)                 \
343         struct lu_device_type *ldt;             \
344         struct lu_device *d
345 #else
346 #define DECLARE_LU_VARS(ldt, d)                                 \
347         extern void __placeholder_to_put_a_semicolon(void)
348 #endif
349 static inline int obd_setup(struct obd_device *obd, struct lustre_cfg *cfg)
350 {
351         int rc;
352         DECLARE_LU_VARS(ldt, d);
353         ENTRY;
354
355 #ifdef __KERNEL__
356         ldt = obd->obd_type->typ_lu;
357         if (ldt != NULL) {
358                 struct lu_context ctx;
359
360                 rc = lu_context_init(&ctx, ldt->ldt_ctx_tags);
361                 if (rc == 0) {
362                         lu_context_enter(&ctx);
363                         d = ldt->ldt_ops->ldto_device_alloc(&ctx, ldt, cfg);
364                         lu_context_exit(&ctx);
365                         lu_context_fini(&ctx);
366                         if (!IS_ERR(d)) {
367                                 obd->obd_lu_dev = d;
368                                 d->ld_obd = obd;
369                                 rc = 0;
370                         } else
371                                 rc = PTR_ERR(d);
372                 }
373         } else
374 #endif
375         {
376                 OBD_CHECK_DT_OP(obd, setup, -EOPNOTSUPP);
377                 OBD_COUNTER_INCREMENT(obd, setup);
378                 rc = OBP(obd, setup)(obd, cfg);
379         }
380         RETURN(rc);
381 }
382
383 static inline int obd_precleanup(struct obd_device *obd,
384                                  enum obd_cleanup_stage cleanup_stage)
385 {
386         int rc;
387         ENTRY;
388
389         OBD_CHECK_DT_OP(obd, precleanup, 0);
390         OBD_COUNTER_INCREMENT(obd, precleanup);
391
392         rc = OBP(obd, precleanup)(obd, cleanup_stage);
393         RETURN(rc);
394 }
395
396 static inline int obd_cleanup(struct obd_device *obd)
397 {
398         int rc;
399         DECLARE_LU_VARS(ldt, d);
400         ENTRY;
401
402         OBD_CHECK_DEV(obd);
403
404 #ifdef __KERNEL__
405         ldt = obd->obd_type->typ_lu;
406         d = obd->obd_lu_dev;
407         if (ldt != NULL && d != NULL) {
408                 struct lu_context ctx;
409
410                 rc = lu_context_init(&ctx, ldt->ldt_ctx_tags);
411                 if (rc == 0) {
412                         lu_context_enter(&ctx);
413                         ldt->ldt_ops->ldto_device_free(&ctx, d);
414                         lu_context_exit(&ctx);
415                         lu_context_fini(&ctx);
416                         obd->obd_lu_dev = NULL;
417                         rc = 0;
418                 }
419         } else
420 #endif
421         {
422                 OBD_CHECK_DT_OP(obd, cleanup, 0);
423                 rc = OBP(obd, cleanup)(obd);
424         }
425         OBD_COUNTER_INCREMENT(obd, cleanup);
426         RETURN(rc);
427 }
428
429 static inline int
430 obd_process_config(struct obd_device *obd, int datalen, void *data)
431 {
432         int rc;
433         DECLARE_LU_VARS(ldt, d);
434         ENTRY;
435
436         OBD_CHECK_DEV(obd);
437
438 #ifdef __KERNEL__
439         ldt = obd->obd_type->typ_lu;
440         d = obd->obd_lu_dev;
441         if (ldt != NULL && d != NULL) {
442                 struct lu_context ctx;
443
444                 rc = lu_context_init(&ctx, ldt->ldt_ctx_tags);
445                 if (rc == 0) {
446                         lu_context_enter(&ctx);
447                         rc = d->ld_ops->ldo_process_config(&ctx, d, data);
448                         lu_context_exit(&ctx);
449                         lu_context_fini(&ctx);
450                 }
451         } else
452 #endif
453         {
454                 OBD_CHECK_DT_OP(obd, process_config, -EOPNOTSUPP);
455                 rc = OBP(obd, process_config)(obd, datalen, data);
456         }
457         OBD_COUNTER_INCREMENT(obd, process_config);
458
459         RETURN(rc);
460 }
461
462 /* Pack an in-memory MD struct for storage on disk.
463  * Returns +ve size of packed MD (0 for free), or -ve error.
464  *
465  * If @disk_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
466  * If @*disk_tgt != NULL and @mem_src == NULL, @*disk_tgt will be freed.
467  * If @*disk_tgt == NULL, it will be allocated
468  */
469 static inline int obd_packmd(struct obd_export *exp,
470                              struct lov_mds_md **disk_tgt,
471                              struct lov_stripe_md *mem_src)
472 {
473         int rc;
474         ENTRY;
475
476         EXP_CHECK_DT_OP(exp, packmd);
477         OBD_COUNTER_INCREMENT(exp->exp_obd, packmd);
478
479         rc = OBP(exp->exp_obd, packmd)(exp, disk_tgt, mem_src);
480         RETURN(rc);
481 }
482
483 static inline int obd_size_diskmd(struct obd_export *exp,
484                                   struct lov_stripe_md *mem_src)
485 {
486         return obd_packmd(exp, NULL, mem_src);
487 }
488
489 /* helper functions */
490 static inline int obd_alloc_diskmd(struct obd_export *exp,
491                                    struct lov_mds_md **disk_tgt)
492 {
493         LASSERT(disk_tgt);
494         LASSERT(*disk_tgt == NULL);
495         return obd_packmd(exp, disk_tgt, NULL);
496 }
497
498 static inline int obd_free_diskmd(struct obd_export *exp,
499                                   struct lov_mds_md **disk_tgt)
500 {
501         LASSERT(disk_tgt);
502         LASSERT(*disk_tgt);
503         return obd_packmd(exp, disk_tgt, NULL);
504 }
505
506 /* Unpack an MD struct from disk to in-memory format.
507  * Returns +ve size of unpacked MD (0 for free), or -ve error.
508  *
509  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
510  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
511  * If @*mem_tgt == NULL, it will be allocated
512  */
513 static inline int obd_unpackmd(struct obd_export *exp,
514                                struct lov_stripe_md **mem_tgt,
515                                struct lov_mds_md *disk_src,
516                                int disk_len)
517 {
518         int rc;
519         ENTRY;
520
521         EXP_CHECK_DT_OP(exp, unpackmd);
522         OBD_COUNTER_INCREMENT(exp->exp_obd, unpackmd);
523
524         rc = OBP(exp->exp_obd, unpackmd)(exp, mem_tgt, disk_src, disk_len);
525         RETURN(rc);
526 }
527
528 /* helper functions */
529 static inline int obd_alloc_memmd(struct obd_export *exp,
530                                   struct lov_stripe_md **mem_tgt)
531 {
532         LASSERT(mem_tgt);
533         LASSERT(*mem_tgt == NULL);
534         return obd_unpackmd(exp, mem_tgt, NULL, 0);
535 }
536
537 static inline int obd_free_memmd(struct obd_export *exp,
538                                  struct lov_stripe_md **mem_tgt)
539 {
540         LASSERT(mem_tgt);
541         LASSERT(*mem_tgt);
542         return obd_unpackmd(exp, mem_tgt, NULL, 0);
543 }
544
545 static inline int obd_checkmd(struct obd_export *exp,
546                               struct obd_export *md_exp,
547                               struct lov_stripe_md *mem_tgt)
548 {
549         int rc;
550         ENTRY;
551
552         EXP_CHECK_DT_OP(exp, checkmd);
553         OBD_COUNTER_INCREMENT(exp->exp_obd, checkmd);
554
555         rc = OBP(exp->exp_obd, checkmd)(exp, md_exp, mem_tgt);
556         RETURN(rc);
557 }
558
559 static inline int obd_create(struct obd_export *exp, struct obdo *obdo,
560                              struct lov_stripe_md **ea,
561                              struct obd_trans_info *oti)
562 {
563         int rc;
564         ENTRY;
565
566         EXP_CHECK_DT_OP(exp, create);
567         OBD_COUNTER_INCREMENT(exp->exp_obd, create);
568
569         rc = OBP(exp->exp_obd, create)(exp, obdo, ea, oti);
570         RETURN(rc);
571 }
572
573 static inline int obd_destroy(struct obd_export *exp, struct obdo *obdo,
574                               struct lov_stripe_md *ea,
575                               struct obd_trans_info *oti,
576                               struct obd_export *md_exp)
577 {
578         int rc;
579         ENTRY;
580
581         EXP_CHECK_DT_OP(exp, destroy);
582         OBD_COUNTER_INCREMENT(exp->exp_obd, destroy);
583
584         rc = OBP(exp->exp_obd, destroy)(exp, obdo, ea, oti, md_exp);
585         RETURN(rc);
586 }
587
588 static inline int obd_getattr(struct obd_export *exp, struct obdo *obdo,
589                               struct lov_stripe_md *ea)
590 {
591         int rc;
592         ENTRY;
593
594         EXP_CHECK_DT_OP(exp, getattr);
595         OBD_COUNTER_INCREMENT(exp->exp_obd, getattr);
596
597         rc = OBP(exp->exp_obd, getattr)(exp, obdo, ea);
598         RETURN(rc);
599 }
600
601 static inline int obd_getattr_async(struct obd_export *exp,
602                                     struct obdo *obdo, struct lov_stripe_md *ea,
603                                     struct ptlrpc_request_set *set)
604 {
605         int rc;
606         ENTRY;
607
608         EXP_CHECK_DT_OP(exp, getattr);
609         OBD_COUNTER_INCREMENT(exp->exp_obd, getattr);
610
611         rc = OBP(exp->exp_obd, getattr_async)(exp, obdo, ea, set);
612         RETURN(rc);
613 }
614
615 static inline int obd_setattr(struct obd_export *exp, struct obdo *obdo,
616                               struct lov_stripe_md *ea,
617                               struct obd_trans_info *oti)
618 {
619         int rc;
620         ENTRY;
621
622         EXP_CHECK_DT_OP(exp, setattr);
623         OBD_COUNTER_INCREMENT(exp->exp_obd, setattr);
624
625         rc = OBP(exp->exp_obd, setattr)(exp, obdo, ea, oti);
626         RETURN(rc);
627 }
628
629 static inline int obd_setattr_async(struct obd_export *exp,
630                                     struct obdo *obdo,
631                                     struct lov_stripe_md *ea,
632                                     struct obd_trans_info *oti)
633 {
634         int rc;
635         ENTRY;
636
637         EXP_CHECK_DT_OP(exp, setattr_async);
638         OBD_COUNTER_INCREMENT(exp->exp_obd, setattr_async);
639
640         rc = OBP(exp->exp_obd, setattr_async)(exp, obdo, ea, oti);
641         RETURN(rc);
642 }
643
644 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
645                                int priority)
646 {
647         struct obd_device *obd = imp->imp_obd;
648         int rc;
649         ENTRY;
650
651         OBD_CHECK_DEV_ACTIVE(obd);
652         OBD_CHECK_DT_OP(obd, add_conn, -EOPNOTSUPP);
653         OBD_COUNTER_INCREMENT(obd, add_conn);
654
655         rc = OBP(obd, add_conn)(imp, uuid, priority);
656         RETURN(rc);
657 }
658
659 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
660 {
661         struct obd_device *obd = imp->imp_obd;
662         int rc;
663         ENTRY;
664
665         OBD_CHECK_DEV_ACTIVE(obd);
666         OBD_CHECK_DT_OP(obd, del_conn, -EOPNOTSUPP);
667         OBD_COUNTER_INCREMENT(obd, del_conn);
668
669         rc = OBP(obd, del_conn)(imp, uuid);
670         RETURN(rc);
671 }
672
673 static inline int obd_connect(const struct lu_context *ctx,
674                               struct lustre_handle *conn,struct obd_device *obd,
675                               struct obd_uuid *cluuid,
676                               struct obd_connect_data *d)
677 {
678         int rc;
679         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition check */
680         ENTRY;
681
682         OBD_CHECK_DEV_ACTIVE(obd);
683         OBD_CHECK_DT_OP(obd, connect, -EOPNOTSUPP);
684         OBD_COUNTER_INCREMENT(obd, connect);
685
686         rc = OBP(obd, connect)(ctx, conn, obd, cluuid, d);
687         /* check that only subset is granted */
688         LASSERT(ergo(d != NULL,
689                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
690         RETURN(rc);
691 }
692
693 static inline int obd_reconnect(struct obd_export *exp,
694                                 struct obd_device *obd,
695                                 struct obd_uuid *cluuid,
696                                 struct obd_connect_data *d)
697 {
698         int rc;
699         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition check */
700         ENTRY;
701
702         OBD_CHECK_DEV_ACTIVE(obd);
703         OBD_CHECK_DT_OP(obd, reconnect, 0);
704         OBD_COUNTER_INCREMENT(obd, reconnect);
705
706         rc = OBP(obd, reconnect)(exp, obd, cluuid, d);
707         /* check that only subset is granted */
708         LASSERT(ergo(d != NULL,
709                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
710         RETURN(rc);
711 }
712
713 static inline int obd_disconnect(struct obd_export *exp)
714 {
715         int rc;
716         ENTRY;
717
718         EXP_CHECK_DT_OP(exp, disconnect);
719         OBD_COUNTER_INCREMENT(exp->exp_obd, disconnect);
720
721         rc = OBP(exp->exp_obd, disconnect)(exp);
722         RETURN(rc);
723 }
724
725 static inline int obd_fid_init(struct obd_export *exp)
726 {
727         int rc;
728         ENTRY;
729
730         if (OBP(exp->exp_obd, fid_init) == NULL)
731                 RETURN(-ENOTSUPP);
732
733         OBD_COUNTER_INCREMENT(exp->exp_obd, fid_init);
734
735         rc = OBP(exp->exp_obd, fid_init)(exp);
736         RETURN(rc);
737 }
738
739 static inline int obd_fid_fini(struct obd_export *exp)
740 {
741         int rc;
742         ENTRY;
743
744         if (OBP(exp->exp_obd, fid_fini) == NULL)
745                 RETURN(-ENOTSUPP);
746
747         OBD_COUNTER_INCREMENT(exp->exp_obd, fid_fini);
748
749         rc = OBP(exp->exp_obd, fid_fini)(exp);
750         RETURN(rc);
751 }
752
753 static inline int obd_fid_alloc(struct obd_export *exp,
754                                 struct lu_fid *fid,
755                                 struct lu_placement_hint *hint)
756 {
757         int rc;
758         ENTRY;
759
760         if (OBP(exp->exp_obd, fid_alloc) == NULL)
761                 RETURN(-ENOTSUPP);
762
763         OBD_COUNTER_INCREMENT(exp->exp_obd, fid_alloc);
764
765         rc = OBP(exp->exp_obd, fid_alloc)(exp, fid, hint);
766         RETURN(rc);
767 }
768
769 static inline int obd_fid_delete(struct obd_export *exp,
770                                  struct lu_fid *fid)
771 {
772         int rc;
773         ENTRY;
774
775         if (OBP(exp->exp_obd, fid_delete) == NULL)
776                 RETURN(0);
777
778         OBD_COUNTER_INCREMENT(exp->exp_obd, fid_delete);
779         rc = OBP(exp->exp_obd, fid_delete)(exp, fid);
780         RETURN(rc);
781 }
782
783 static inline int obd_init_export(struct obd_export *exp)
784 {
785         int rc = 0;
786
787         ENTRY;
788         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
789             OBP((exp)->exp_obd, init_export))
790                 rc = OBP(exp->exp_obd, init_export)(exp);
791         RETURN(rc);
792 }
793
794 static inline int obd_destroy_export(struct obd_export *exp)
795 {
796         ENTRY;
797         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
798             OBP((exp)->exp_obd, destroy_export))
799                 OBP(exp->exp_obd, destroy_export)(exp);
800         RETURN(0);
801 }
802
803 static inline struct dentry *
804 obd_lvfs_fid2dentry(struct obd_export *exp, __u64 id_ino, __u32 gen, __u64 gr)
805 {
806         LASSERT(exp->exp_obd);
807
808         return lvfs_fid2dentry(&exp->exp_obd->obd_lvfs_ctxt, id_ino, gen, gr,
809                                exp->exp_obd);
810 }
811
812 static inline int
813 obd_lvfs_open_llog(struct obd_export *exp, __u64 id_ino, struct dentry *dentry)
814 {
815         LASSERT(exp->exp_obd);
816         CERROR("FIXME what's the story here?  This needs to be an obd fn?\n");
817 #if 0
818         return lvfs_open_llog(&exp->exp_obd->obd_lvfs_ctxt, id_ino,
819                               dentry, exp->exp_obd);
820 #endif
821         return 0;
822 }
823
824 #ifndef time_before
825 #define time_before(t1, t2) ((long)t2 - (long)t1 > 0)
826 #endif
827
828 /* @max_age is the oldest time in jiffies that we accept using a cached data.
829  * If the cache is older than @max_age we will get a new value from the
830  * target.  Use a value of "jiffies + HZ" to guarantee freshness. */
831 static inline int obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
832                              unsigned long max_age)
833 {
834         int rc = 0;
835         ENTRY;
836
837         if (obd == NULL)
838                 RETURN(-EINVAL);
839
840         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
841         OBD_COUNTER_INCREMENT(obd, statfs);
842
843         CDEBUG(D_SUPER, "osfs %lu, max_age %lu\n", obd->obd_osfs_age, max_age);
844         if (time_before(obd->obd_osfs_age, max_age)) {
845                 rc = OBP(obd, statfs)(obd, osfs, max_age);
846                 if (rc == 0) {
847                         spin_lock(&obd->obd_osfs_lock);
848                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
849                         obd->obd_osfs_age = jiffies;
850                         spin_unlock(&obd->obd_osfs_lock);
851                 }
852         } else {
853                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
854                 spin_lock(&obd->obd_osfs_lock);
855                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
856                 spin_unlock(&obd->obd_osfs_lock);
857         }
858         RETURN(rc);
859 }
860
861 static inline int obd_sync(struct obd_export *exp, struct obdo *oa,
862                            struct lov_stripe_md *ea, obd_size start,
863                            obd_size end)
864 {
865         int rc;
866         ENTRY;
867
868         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
869         OBD_COUNTER_INCREMENT(exp->exp_obd, sync);
870
871         rc = OBP(exp->exp_obd, sync)(exp, oa, ea, start, end);
872         RETURN(rc);
873 }
874
875 static inline int obd_punch(struct obd_export *exp, struct obdo *oa,
876                             struct lov_stripe_md *ea, obd_size start,
877                             obd_size end, struct obd_trans_info *oti)
878 {
879         int rc;
880         ENTRY;
881
882         EXP_CHECK_DT_OP(exp, punch);
883         OBD_COUNTER_INCREMENT(exp->exp_obd, punch);
884
885         rc = OBP(exp->exp_obd, punch)(exp, oa, ea, start, end, oti);
886         RETURN(rc);
887 }
888
889 static inline int obd_brw(int cmd, struct obd_export *exp, struct obdo *oa,
890                           struct lov_stripe_md *ea, obd_count oa_bufs,
891                           struct brw_page *pg, struct obd_trans_info *oti)
892 {
893         int rc;
894         ENTRY;
895
896         EXP_CHECK_DT_OP(exp, brw);
897         OBD_COUNTER_INCREMENT(exp->exp_obd, brw);
898
899         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
900                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
901                        "or OBD_BRW_CHECK\n");
902                 LBUG();
903         }
904
905         rc = OBP(exp->exp_obd, brw)(cmd, exp, oa, ea, oa_bufs, pg, oti);
906         RETURN(rc);
907 }
908
909 static inline int obd_brw_async(int cmd, struct obd_export *exp,
910                                 struct obdo *oa, struct lov_stripe_md *ea,
911                                 obd_count oa_bufs, struct brw_page *pg,
912                                 struct ptlrpc_request_set *set,
913                                 struct obd_trans_info *oti)
914 {
915         int rc;
916         ENTRY;
917
918         EXP_CHECK_DT_OP(exp, brw_async);
919         OBD_COUNTER_INCREMENT(exp->exp_obd, brw_async);
920
921         if (!(cmd & OBD_BRW_RWMASK)) {
922                 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
923                 LBUG();
924         }
925
926         rc = OBP(exp->exp_obd, brw_async)(cmd, exp, oa, ea, oa_bufs, pg, set,
927                                           oti);
928         RETURN(rc);
929 }
930
931 static inline  int obd_prep_async_page(struct obd_export *exp,
932                                        struct lov_stripe_md *lsm,
933                                        struct lov_oinfo *loi,
934                                        struct page *page, obd_off offset,
935                                        struct obd_async_page_ops *ops,
936                                        void *data, void **res)
937 {
938         int ret;
939         ENTRY;
940
941         OBD_CHECK_DT_OP(exp->exp_obd, prep_async_page, -EOPNOTSUPP);
942         OBD_COUNTER_INCREMENT(exp->exp_obd, prep_async_page);
943
944         ret = OBP(exp->exp_obd, prep_async_page)(exp, lsm, loi, page, offset,
945                                                  ops, data, res);
946         RETURN(ret);
947 }
948
949 static inline int obd_queue_async_io(struct obd_export *exp,
950                                      struct lov_stripe_md *lsm,
951                                      struct lov_oinfo *loi, void *cookie,
952                                      int cmd, obd_off off, int count,
953                                      obd_flag brw_flags, obd_flag async_flags)
954 {
955         int rc;
956         ENTRY;
957
958         OBD_CHECK_DT_OP(exp->exp_obd, queue_async_io, -EOPNOTSUPP);
959         OBD_COUNTER_INCREMENT(exp->exp_obd, queue_async_io);
960         LASSERT(cmd & OBD_BRW_RWMASK);
961
962         rc = OBP(exp->exp_obd, queue_async_io)(exp, lsm, loi, cookie, cmd, off,
963                                                count, brw_flags, async_flags);
964         RETURN(rc);
965 }
966
967 static inline int obd_set_async_flags(struct obd_export *exp,
968                                       struct lov_stripe_md *lsm,
969                                       struct lov_oinfo *loi, void *cookie,
970                                       obd_flag async_flags)
971 {
972         int rc;
973         ENTRY;
974
975         OBD_CHECK_DT_OP(exp->exp_obd, set_async_flags, -EOPNOTSUPP);
976         OBD_COUNTER_INCREMENT(exp->exp_obd, set_async_flags);
977
978         rc = OBP(exp->exp_obd, set_async_flags)(exp, lsm, loi, cookie,
979                                                 async_flags);
980         RETURN(rc);
981 }
982
983 static inline int obd_queue_group_io(struct obd_export *exp,
984                                      struct lov_stripe_md *lsm,
985                                      struct lov_oinfo *loi,
986                                      struct obd_io_group *oig,
987                                      void *cookie, int cmd, obd_off off,
988                                      int count, obd_flag brw_flags,
989                                      obd_flag async_flags)
990 {
991         int rc;
992         ENTRY;
993
994         OBD_CHECK_DT_OP(exp->exp_obd, queue_group_io, -EOPNOTSUPP);
995         OBD_COUNTER_INCREMENT(exp->exp_obd, queue_group_io);
996         LASSERT(cmd & OBD_BRW_RWMASK);
997
998         rc = OBP(exp->exp_obd, queue_group_io)(exp, lsm, loi, oig, cookie,
999                                                cmd, off, count, brw_flags,
1000                                                async_flags);
1001         RETURN(rc);
1002 }
1003
1004 static inline int obd_trigger_group_io(struct obd_export *exp,
1005                                        struct lov_stripe_md *lsm,
1006                                        struct lov_oinfo *loi,
1007                                        struct obd_io_group *oig)
1008 {
1009         int rc;
1010         ENTRY;
1011
1012         OBD_CHECK_DT_OP(exp->exp_obd, trigger_group_io, -EOPNOTSUPP);
1013         OBD_COUNTER_INCREMENT(exp->exp_obd, trigger_group_io);
1014
1015         rc = OBP(exp->exp_obd, trigger_group_io)(exp, lsm, loi, oig);
1016         RETURN(rc);
1017 }
1018
1019 static inline int obd_teardown_async_page(struct obd_export *exp,
1020                                           struct lov_stripe_md *lsm,
1021                                           struct lov_oinfo *loi, void *cookie)
1022 {
1023         int rc;
1024         ENTRY;
1025
1026         OBD_CHECK_DT_OP(exp->exp_obd, teardown_async_page, -EOPNOTSUPP);
1027         OBD_COUNTER_INCREMENT(exp->exp_obd, teardown_async_page);
1028
1029         rc = OBP(exp->exp_obd, teardown_async_page)(exp, lsm, loi, cookie);
1030         RETURN(rc);
1031 }
1032
1033 static inline int obd_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
1034                              int objcount, struct obd_ioobj *obj,
1035                              int niocount, struct niobuf_remote *remote,
1036                              struct niobuf_local *local,
1037                              struct obd_trans_info *oti)
1038 {
1039         int rc;
1040         ENTRY;
1041
1042         OBD_CHECK_DT_OP(exp->exp_obd, preprw, -EOPNOTSUPP);
1043         OBD_COUNTER_INCREMENT(exp->exp_obd, preprw);
1044
1045         rc = OBP(exp->exp_obd, preprw)(cmd, exp, oa, objcount, obj, niocount,
1046                                        remote, local, oti);
1047         RETURN(rc);
1048 }
1049
1050 static inline int obd_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
1051                                int objcount, struct obd_ioobj *obj,
1052                                int niocount, struct niobuf_local *local,
1053                                struct obd_trans_info *oti, int rc)
1054 {
1055         ENTRY;
1056
1057         OBD_CHECK_DT_OP(exp->exp_obd, commitrw, -EOPNOTSUPP);
1058         OBD_COUNTER_INCREMENT(exp->exp_obd, commitrw);
1059
1060         rc = OBP(exp->exp_obd, commitrw)(cmd, exp, oa, objcount, obj, niocount,
1061                                          local, oti, rc);
1062         RETURN(rc);
1063 }
1064
1065 static inline int obd_merge_lvb(struct obd_export *exp,
1066                                 struct lov_stripe_md *lsm,
1067                                 struct ost_lvb *lvb, int kms_only)
1068 {
1069         int rc;
1070         ENTRY;
1071
1072         OBD_CHECK_DT_OP(exp->exp_obd, merge_lvb, -EOPNOTSUPP);
1073         OBD_COUNTER_INCREMENT(exp->exp_obd, merge_lvb);
1074
1075         rc = OBP(exp->exp_obd, merge_lvb)(exp, lsm, lvb, kms_only);
1076         RETURN(rc);
1077 }
1078
1079 static inline int obd_adjust_kms(struct obd_export *exp,
1080                                  struct lov_stripe_md *lsm, obd_off size,
1081                                  int shrink)
1082 {
1083         int rc;
1084         ENTRY;
1085
1086         OBD_CHECK_DT_OP(exp->exp_obd, adjust_kms, -EOPNOTSUPP);
1087         OBD_COUNTER_INCREMENT(exp->exp_obd, adjust_kms);
1088
1089         rc = OBP(exp->exp_obd, adjust_kms)(exp, lsm, size, shrink);
1090         RETURN(rc);
1091 }
1092
1093 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1094                                 int len, void *karg, void *uarg)
1095 {
1096         int rc;
1097         ENTRY;
1098
1099         EXP_CHECK_DT_OP(exp, iocontrol);
1100         OBD_COUNTER_INCREMENT(exp->exp_obd, iocontrol);
1101
1102         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1103         RETURN(rc);
1104 }
1105
1106 static inline int obd_enqueue(struct obd_export *exp, struct lov_stripe_md *ea,
1107                               __u32 type, ldlm_policy_data_t *policy,
1108                               __u32 mode, int *flags, void *bl_cb, void *cp_cb,
1109                               void *gl_cb, void *data, __u32 lvb_len,
1110                               void *lvb_swabber, struct lustre_handle *lockh)
1111 {
1112         int rc;
1113         ENTRY;
1114
1115         EXP_CHECK_DT_OP(exp, enqueue);
1116         OBD_COUNTER_INCREMENT(exp->exp_obd, enqueue);
1117
1118         rc = OBP(exp->exp_obd, enqueue)(exp, ea, type, policy, mode, flags,
1119                                         bl_cb, cp_cb, gl_cb, data, lvb_len,
1120                                         lvb_swabber, lockh);
1121         RETURN(rc);
1122 }
1123
1124 static inline int obd_match(struct obd_export *exp, struct lov_stripe_md *ea,
1125                             __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1126                             int *flags, void *data, struct lustre_handle *lockh)
1127 {
1128         int rc;
1129         ENTRY;
1130
1131         EXP_CHECK_DT_OP(exp, match);
1132         OBD_COUNTER_INCREMENT(exp->exp_obd, match);
1133
1134         rc = OBP(exp->exp_obd, match)(exp, ea, type, policy, mode, flags, data,
1135                                       lockh);
1136         RETURN(rc);
1137 }
1138
1139 static inline int obd_change_cbdata(struct obd_export *exp,
1140                                     struct lov_stripe_md *lsm,
1141                                     ldlm_iterator_t it, void *data)
1142 {
1143         int rc;
1144         ENTRY;
1145
1146         EXP_CHECK_DT_OP(exp, change_cbdata);
1147         OBD_COUNTER_INCREMENT(exp->exp_obd, change_cbdata);
1148
1149         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1150         RETURN(rc);
1151 }
1152
1153 static inline int obd_cancel(struct obd_export *exp,
1154                              struct lov_stripe_md *ea, __u32 mode,
1155                              struct lustre_handle *lockh)
1156 {
1157         int rc;
1158         ENTRY;
1159
1160         EXP_CHECK_DT_OP(exp, cancel);
1161         OBD_COUNTER_INCREMENT(exp->exp_obd, cancel);
1162
1163         rc = OBP(exp->exp_obd, cancel)(exp, ea, mode, lockh);
1164         RETURN(rc);
1165 }
1166
1167 static inline int obd_cancel_unused(struct obd_export *exp,
1168                                     struct lov_stripe_md *ea,
1169                                     int flags, void *opaque)
1170 {
1171         int rc;
1172         ENTRY;
1173
1174         EXP_CHECK_DT_OP(exp, cancel_unused);
1175         OBD_COUNTER_INCREMENT(exp->exp_obd, cancel_unused);
1176
1177         rc = OBP(exp->exp_obd, cancel_unused)(exp, ea, flags, opaque);
1178         RETURN(rc);
1179 }
1180
1181 static inline int obd_join_lru(struct obd_export *exp,
1182                                struct lov_stripe_md *ea, int join)
1183 {
1184         int rc;
1185         ENTRY;
1186
1187         EXP_CHECK_DT_OP(exp, join_lru);
1188         OBD_COUNTER_INCREMENT(exp->exp_obd, join_lru);
1189
1190         rc = OBP(exp->exp_obd, join_lru)(exp, ea, join);
1191         RETURN(rc);
1192 }
1193
1194 static inline int obd_san_preprw(int cmd, struct obd_export *exp,
1195                                  struct obdo *oa,
1196                                  int objcount, struct obd_ioobj *obj,
1197                                  int niocount, struct niobuf_remote *remote)
1198 {
1199         int rc;
1200
1201         EXP_CHECK_DT_OP(exp, preprw);
1202         OBD_COUNTER_INCREMENT(exp->exp_obd, preprw);
1203
1204         rc = OBP(exp->exp_obd, san_preprw)(cmd, exp, oa, objcount, obj,
1205                                            niocount, remote);
1206         class_export_put(exp);
1207         return(rc);
1208 }
1209
1210 static inline int obd_pin(struct obd_export *exp, struct lu_fid *fid,
1211                           struct obd_client_handle *handle, int flag)
1212 {
1213         int rc;
1214
1215         EXP_CHECK_DT_OP(exp, pin);
1216         OBD_COUNTER_INCREMENT(exp->exp_obd, pin);
1217
1218         rc = OBP(exp->exp_obd, pin)(exp, fid, handle, flag);
1219         return(rc);
1220 }
1221
1222 static inline int obd_unpin(struct obd_export *exp,
1223                             struct obd_client_handle *handle, int flag)
1224 {
1225         int rc;
1226
1227         EXP_CHECK_DT_OP(exp, unpin);
1228         OBD_COUNTER_INCREMENT(exp->exp_obd, unpin);
1229
1230         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
1231         return(rc);
1232 }
1233
1234
1235 static inline void obd_import_event(struct obd_device *obd,
1236                                     struct obd_import *imp,
1237                                     enum obd_import_event event)
1238 {
1239         if (!obd) {
1240                 CERROR("NULL device\n");
1241                 EXIT;
1242                 return;
1243         }
1244         if (obd->obd_set_up && OBP(obd, import_event)) {
1245                 OBD_COUNTER_INCREMENT(obd, import_event);
1246                 OBP(obd, import_event)(obd, imp, event);
1247         }
1248 }
1249
1250 static inline int obd_notify(struct obd_device *obd,
1251                              struct obd_device *watched,
1252                              enum obd_notify_event ev,
1253                              void *data)
1254 {
1255         OBD_CHECK_DEV(obd);
1256
1257         /* the check for async_recov is a complete hack - I'm hereby
1258            overloading the meaning to also mean "this was called from
1259            mds_postsetup".  I know that my mds is able to handle notifies
1260            by this point, and it needs to get them to execute mds_postrecov. */
1261         if (!obd->obd_set_up && !obd->obd_async_recov) {
1262                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1263                 return -EINVAL;
1264         }
1265
1266         if (!OBP(obd, notify)) {
1267                 CERROR("obd %s has no notify handler\n", obd->obd_name);
1268                 return -ENOSYS;
1269         }
1270
1271         OBD_COUNTER_INCREMENT(obd, notify);
1272         return OBP(obd, notify)(obd, watched, ev, data);
1273 }
1274
1275 static inline int obd_notify_observer(struct obd_device *observer,
1276                                       struct obd_device *observed,
1277                                       enum obd_notify_event ev,
1278                                       void *data)
1279 {
1280         int rc1;
1281         int rc2;
1282
1283         struct obd_notify_upcall *onu;
1284
1285         if (observer->obd_observer)
1286                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1287         else
1288                 rc1 = 0;
1289         /*
1290          * Also, call non-obd listener, if any
1291          */
1292         onu = &observer->obd_upcall;
1293         if (onu->onu_upcall != NULL)
1294                 rc2 = onu->onu_upcall(observer, observed, ev, onu->onu_owner);
1295         else
1296                 rc2 = 0;
1297
1298         return rc1 ? rc1 : rc2;
1299 }
1300
1301 static inline int obd_quotacheck(struct obd_export *exp,
1302                                  struct obd_quotactl *oqctl)
1303 {
1304         int rc;
1305         ENTRY;
1306
1307         EXP_CHECK_DT_OP(exp, quotacheck);
1308         OBD_COUNTER_INCREMENT(exp->exp_obd, quotacheck);
1309
1310         rc = OBP(exp->exp_obd, quotacheck)(exp, oqctl);
1311         RETURN(rc);
1312 }
1313
1314 static inline int obd_quotactl(struct obd_export *exp,
1315                                struct obd_quotactl *oqctl)
1316 {
1317         int rc;
1318         ENTRY;
1319
1320         EXP_CHECK_DT_OP(exp, quotactl);
1321         OBD_COUNTER_INCREMENT(exp->exp_obd, quotactl);
1322
1323         rc = OBP(exp->exp_obd, quotactl)(exp, oqctl);
1324         RETURN(rc);
1325 }
1326
1327 static inline int obd_health_check(struct obd_device *obd)
1328 {
1329         /* returns: 0 on healthy
1330          *         >0 on unhealthy + reason code/flag
1331          *            however the only suppored reason == 1 right now
1332          *            We'll need to define some better reasons
1333          *            or flags in the future.
1334          *         <0 on error
1335          */
1336         int rc;
1337         ENTRY;
1338
1339         /* don't use EXP_CHECK_OP, because NULL method is normal here */
1340         if (obd == NULL || !OBT(obd)) {
1341                 CERROR("cleaned up obd\n");
1342                 RETURN(-EOPNOTSUPP);
1343         }
1344         if (!obd->obd_set_up || obd->obd_stopping)
1345                 RETURN(0);
1346         if (!OBP(obd, health_check))
1347                 RETURN(0);
1348
1349         rc = OBP(obd, health_check)(obd);
1350         RETURN(rc);
1351 }
1352
1353 static inline int obd_register_observer(struct obd_device *obd,
1354                                         struct obd_device *observer)
1355 {
1356         ENTRY;
1357         OBD_CHECK_DEV(obd);
1358         if (obd->obd_observer && observer)
1359                 RETURN(-EALREADY);
1360         obd->obd_observer = observer;
1361         RETURN(0);
1362 }
1363
1364 /* metadata helpers */
1365 static inline int md_getstatus(struct obd_export *exp, struct lu_fid *fid)
1366 {
1367         int rc;
1368         ENTRY;
1369
1370         EXP_CHECK_MD_OP(exp, getstatus);
1371         MD_COUNTER_INCREMENT(exp->exp_obd, getstatus);
1372         rc = MDP(exp->exp_obd, getstatus)(exp, fid);
1373         RETURN(rc);
1374 }
1375
1376 static inline int md_getattr(struct obd_export *exp, struct lu_fid *fid,
1377                              obd_valid valid, int ea_size,
1378                              struct ptlrpc_request **request)
1379 {
1380         int rc;
1381         ENTRY;
1382         EXP_CHECK_MD_OP(exp, getattr);
1383         MD_COUNTER_INCREMENT(exp->exp_obd, getattr);
1384         rc = MDP(exp->exp_obd, getattr)(exp, fid, valid,
1385                                         ea_size, request);
1386         RETURN(rc);
1387 }
1388
1389 static inline int md_change_cbdata(struct obd_export *exp, struct lu_fid *fid,
1390                                    ldlm_iterator_t it, void *data)
1391 {
1392         int rc;
1393         ENTRY;
1394         EXP_CHECK_MD_OP(exp, change_cbdata);
1395         MD_COUNTER_INCREMENT(exp->exp_obd, change_cbdata);
1396         rc = MDP(exp->exp_obd, change_cbdata)(exp, fid, it, data);
1397         RETURN(rc);
1398 }
1399
1400 static inline int md_close(struct obd_export *exp,
1401                            struct md_op_data *op_data,
1402                            struct obd_client_handle *och,
1403                            struct ptlrpc_request **request)
1404 {
1405         int rc;
1406         ENTRY;
1407         EXP_CHECK_MD_OP(exp, close);
1408         MD_COUNTER_INCREMENT(exp->exp_obd, close);
1409         rc = MDP(exp->exp_obd, close)(exp, op_data, och, request);
1410         RETURN(rc);
1411 }
1412
1413 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1414                             const void *data, int datalen, int mode,
1415                             __u32 uid, __u32 gid, __u32 cap_effective, __u64 rdev,
1416                             struct ptlrpc_request **request)
1417 {
1418         int rc;
1419         ENTRY;
1420         EXP_CHECK_MD_OP(exp, create);
1421         MD_COUNTER_INCREMENT(exp->exp_obd, create);
1422         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1423                                        uid, gid, cap_effective, rdev, request);
1424         RETURN(rc);
1425 }
1426
1427 static inline int md_done_writing(struct obd_export *exp,
1428                                   struct md_op_data *op_data)
1429 {
1430         int rc;
1431         ENTRY;
1432         EXP_CHECK_MD_OP(exp, done_writing);
1433         MD_COUNTER_INCREMENT(exp->exp_obd, done_writing);
1434         rc = MDP(exp->exp_obd, done_writing)(exp, op_data);
1435         RETURN(rc);
1436 }
1437
1438 static inline int md_enqueue(struct obd_export *exp, int lock_type,
1439                              struct lookup_intent *it, int lock_mode,
1440                              struct md_op_data *op_data,
1441                              struct lustre_handle *lockh,
1442                              void *lmm, int lmmsize,
1443                              ldlm_completion_callback cb_completion,
1444                              ldlm_blocking_callback cb_blocking,
1445                              void *cb_data, int extra_lock_flags)
1446 {
1447         int rc;
1448         ENTRY;
1449         EXP_CHECK_MD_OP(exp, enqueue);
1450         MD_COUNTER_INCREMENT(exp->exp_obd, enqueue);
1451         rc = MDP(exp->exp_obd, enqueue)(exp, lock_type, it, lock_mode,
1452                                         op_data, lockh, lmm, lmmsize,
1453                                         cb_completion, cb_blocking,
1454                                         cb_data, extra_lock_flags);
1455         RETURN(rc);
1456 }
1457
1458 static inline int md_getattr_name(struct obd_export *exp, struct lu_fid *fid,
1459                                   const char *filename, int namelen,
1460                                   obd_valid valid, int ea_size,
1461                                   struct ptlrpc_request **request)
1462 {
1463         int rc;
1464         ENTRY;
1465         EXP_CHECK_MD_OP(exp, getattr_name);
1466         MD_COUNTER_INCREMENT(exp->exp_obd, getattr_name);
1467         rc = MDP(exp->exp_obd, getattr_name)(exp, fid, filename, namelen,
1468                                              valid, ea_size, request);
1469         RETURN(rc);
1470 }
1471
1472 static inline int md_intent_lock(struct obd_export *exp,
1473                                  struct md_op_data *op_data,
1474                                  void *lmm, int lmmsize,
1475                                  struct lookup_intent *it,
1476                                  int flags, struct ptlrpc_request **reqp,
1477                                  ldlm_blocking_callback cb_blocking,
1478                                  int extra_lock_flags)
1479 {
1480         int rc;
1481         ENTRY;
1482         EXP_CHECK_MD_OP(exp, intent_lock);
1483         MD_COUNTER_INCREMENT(exp->exp_obd, intent_lock);
1484         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, lmm, lmmsize,
1485                                             it, flags, reqp, cb_blocking,
1486                                             extra_lock_flags);
1487         RETURN(rc);
1488 }
1489
1490 static inline int md_link(struct obd_export *exp,
1491                           struct md_op_data *op_data,
1492                           struct ptlrpc_request **request)
1493 {
1494         int rc;
1495         ENTRY;
1496         EXP_CHECK_MD_OP(exp, link);
1497         MD_COUNTER_INCREMENT(exp->exp_obd, link);
1498         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1499         RETURN(rc);
1500 }
1501
1502 static inline int md_rename(struct obd_export *exp,
1503                             struct md_op_data *op_data,
1504                             const char *old, int oldlen,
1505                             const char *new, int newlen,
1506                             struct ptlrpc_request **request)
1507 {
1508         int rc;
1509         ENTRY;
1510         EXP_CHECK_MD_OP(exp, rename);
1511         MD_COUNTER_INCREMENT(exp->exp_obd, rename);
1512         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1513                                        newlen, request);
1514         RETURN(rc);
1515 }
1516
1517 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1518                              struct iattr *iattr, void *ea, int ealen,
1519                              void *ea2, int ea2len, struct ptlrpc_request **request)
1520 {
1521         int rc;
1522         ENTRY;
1523         EXP_CHECK_MD_OP(exp, setattr);
1524         MD_COUNTER_INCREMENT(exp->exp_obd, setattr);
1525         rc = MDP(exp->exp_obd, setattr)(exp, op_data, iattr, ea, ealen,
1526                                         ea2, ea2len, request);
1527         RETURN(rc);
1528 }
1529
1530 static inline int md_sync(struct obd_export *exp, struct lu_fid *fid,
1531                           struct ptlrpc_request **request)
1532 {
1533         int rc;
1534         ENTRY;
1535         EXP_CHECK_MD_OP(exp, sync);
1536         MD_COUNTER_INCREMENT(exp->exp_obd, sync);
1537         rc = MDP(exp->exp_obd, sync)(exp, fid, request);
1538         RETURN(rc);
1539 }
1540
1541 static inline int md_readpage(struct obd_export *exp, struct lu_fid *fid,
1542                               __u64 offset, struct page *page,
1543                               struct ptlrpc_request **request)
1544 {
1545         int rc;
1546         ENTRY;
1547         EXP_CHECK_MD_OP(exp, readpage);
1548         MD_COUNTER_INCREMENT(exp->exp_obd, readpage);
1549         rc = MDP(exp->exp_obd, readpage)(exp, fid, offset, page, request);
1550         RETURN(rc);
1551 }
1552
1553 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1554                             struct ptlrpc_request **request)
1555 {
1556         int rc;
1557         ENTRY;
1558         EXP_CHECK_MD_OP(exp, unlink);
1559         MD_COUNTER_INCREMENT(exp->exp_obd, unlink);
1560         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
1561         RETURN(rc);
1562 }
1563
1564 static inline int md_get_lustre_md(struct obd_export *exp,
1565                                    struct ptlrpc_request *req,
1566                                    int offset, struct obd_export *dt_exp,
1567                                    struct lustre_md *md)
1568 {
1569         ENTRY;
1570         EXP_CHECK_MD_OP(exp, get_lustre_md);
1571         MD_COUNTER_INCREMENT(exp->exp_obd, get_lustre_md);
1572         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, offset,
1573                                                 dt_exp, md));
1574 }
1575
1576 static inline int md_free_lustre_md(struct obd_export *exp,
1577                                     struct lustre_md *md)
1578 {
1579         ENTRY;
1580         EXP_CHECK_MD_OP(exp, free_lustre_md);
1581         MD_COUNTER_INCREMENT(exp->exp_obd, free_lustre_md);
1582         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
1583 }
1584
1585 static inline int md_setxattr(struct obd_export *exp, struct lu_fid *fid,
1586                               obd_valid valid, const char *name,
1587                               const char *input, int input_size,
1588                               int output_size, int flags,
1589                               struct ptlrpc_request **request)
1590 {
1591         ENTRY;
1592         EXP_CHECK_MD_OP(exp, setxattr);
1593         MD_COUNTER_INCREMENT(exp->exp_obd, setxattr);
1594         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, valid, name, input,
1595                                            input_size, output_size, flags,
1596                                            request));
1597 }
1598
1599 static inline int md_getxattr(struct obd_export *exp, struct lu_fid *fid,
1600                               obd_valid valid, const char *name,
1601                               const char *input, int input_size,
1602                               int output_size, int flags,
1603                               struct ptlrpc_request **request)
1604 {
1605         ENTRY;
1606         EXP_CHECK_MD_OP(exp, getxattr);
1607         MD_COUNTER_INCREMENT(exp->exp_obd, getxattr);
1608         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, valid, name, input,
1609                                            input_size, output_size, flags,
1610                                            request));
1611 }
1612
1613 static inline int md_set_open_replay_data(struct obd_export *exp,
1614                                           struct obd_client_handle *och,
1615                                           struct ptlrpc_request *open_req)
1616 {
1617         ENTRY;
1618         EXP_CHECK_MD_OP(exp, set_open_replay_data);
1619         MD_COUNTER_INCREMENT(exp->exp_obd, set_open_replay_data);
1620         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, open_req));
1621 }
1622
1623 static inline int md_clear_open_replay_data(struct obd_export *exp,
1624                                             struct obd_client_handle *och)
1625 {
1626         ENTRY;
1627         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
1628         MD_COUNTER_INCREMENT(exp->exp_obd, clear_open_replay_data);
1629         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
1630 }
1631
1632 static inline int md_set_lock_data(struct obd_export *exp,
1633                                    __u64 *lockh, void *data)
1634 {
1635         ENTRY;
1636         EXP_CHECK_MD_OP(exp, set_lock_data);
1637         MD_COUNTER_INCREMENT(exp->exp_obd, set_lock_data);
1638         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data));
1639 }
1640
1641 static inline int md_cancel_unused(struct obd_export *exp,
1642                                    struct lu_fid *fid,
1643                                    int flags, void *opaque)
1644 {
1645         int rc;
1646         ENTRY;
1647
1648         EXP_CHECK_MD_OP(exp, cancel_unused);
1649         MD_COUNTER_INCREMENT(exp->exp_obd, cancel_unused);
1650
1651         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, flags, opaque);
1652         RETURN(rc);
1653 }
1654
1655 static inline int md_lock_match(struct obd_export *exp, int flags,
1656                                 struct lu_fid *fid, ldlm_type_t type,
1657                                 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1658                                 struct lustre_handle *lockh)
1659 {
1660         ENTRY;
1661         EXP_CHECK_MD_OP(exp, lock_match);
1662         MD_COUNTER_INCREMENT(exp->exp_obd, lock_match);
1663         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1664                                              policy, mode, lockh));
1665 }
1666
1667 static inline int md_init_ea_size(struct obd_export *exp,
1668                                   int easize, int def_asize,
1669                                   int cookiesize)
1670 {
1671         ENTRY;
1672         EXP_CHECK_MD_OP(exp, init_ea_size);
1673         MD_COUNTER_INCREMENT(exp->exp_obd, init_ea_size);
1674         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, easize,
1675                                                def_asize,
1676                                                cookiesize));
1677 }
1678
1679 /* OBD Metadata Support */
1680 extern int obd_init_caches(void);
1681 extern void obd_cleanup_caches(void);
1682
1683 /* support routines */
1684 extern cfs_mem_cache_t *obdo_cachep;
1685 static inline struct obdo *obdo_alloc(void)
1686 {
1687         struct obdo *oa;
1688         OBD_SLAB_ALLOC(oa, obdo_cachep, CFS_ALLOC_STD, sizeof(*oa));
1689         return oa;
1690 }
1691
1692 static inline void obdo_free(struct obdo *oa)
1693 {
1694         OBD_SLAB_FREE(oa, obdo_cachep, sizeof(*oa));
1695 }
1696
1697 static inline void obdo2fid(struct obdo *oa,
1698                             struct lu_fid *fid)
1699 {
1700         /* something here */
1701 }
1702
1703 static inline void fid2obdo(struct lu_fid *fid,
1704                             struct obdo *oa)
1705 {
1706         /* something here */
1707 }
1708
1709 /* I'm as embarrassed about this as you are.
1710  *
1711  * <shaver> // XXX do not look into _superhack with remaining eye
1712  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1713 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1714
1715 /* sysctl.c */
1716 extern void obd_sysctl_init (void);
1717 extern void obd_sysctl_clean (void);
1718
1719 /* uuid.c  */
1720 typedef __u8 class_uuid_t[16];
1721 void class_generate_random_uuid(class_uuid_t uuid);
1722 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1723
1724 /* lustre_peer.c    */
1725 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
1726 int class_add_uuid(const char *uuid, __u64 nid);
1727 int class_del_uuid (const char *uuid);
1728 void class_init_uuidlist(void);
1729 void class_exit_uuidlist(void);
1730
1731 /* mea.c */
1732 int mea_name2idx(struct lmv_stripe_md *mea, char *name, int namelen);
1733 int raw_name2idx(int hashtype, int count, const char *name, int namelen);
1734
1735 #endif /* __LINUX_OBD_CLASS_H */