Whamcloud - gitweb
db4cc065965112117cbb5f4bc9164e474ec3e948
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  */
39 #ifndef __CLASS_OBD_H
40 #define __CLASS_OBD_H
41
42 #ifndef __KERNEL__
43 # include <liblustre.h>
44 #endif
45
46 #include <obd_support.h>
47 #include <lustre_import.h>
48 #include <lustre_net.h>
49 #include <obd.h>
50 #include <lustre_lib.h>
51 #include <lustre/lustre_idl.h>
52 #include <lprocfs_status.h>
53
54 #if defined(__linux__)
55 #include <linux/obd_class.h>
56 #elif defined(__APPLE__)
57 #include <darwin/obd_class.h>
58 #elif defined(__WINNT__)
59 #include <winnt/obd_class.h>
60 #else
61 #error Unsupported operating system.
62 #endif
63
64 /* OBD Device Declarations */
65 extern struct obd_device *obd_devs[MAX_OBD_DEVICES];
66 extern cfs_spinlock_t obd_dev_lock;
67
68 /* OBD Operations Declarations */
69 extern struct obd_device *class_conn2obd(struct lustre_handle *);
70 extern struct obd_device *class_exp2obd(struct obd_export *);
71 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
72
73 struct lu_device_type;
74
75 /* genops.c */
76 struct obd_export *class_conn2export(struct lustre_handle *);
77 int class_register_type(struct obd_ops *, struct md_ops *,
78                         struct lprocfs_vars *, const char *nm,
79                         struct lu_device_type *ldt);
80 int class_unregister_type(const char *nm);
81
82 struct obd_device *class_newdev(const char *type_name, const char *name);
83 void class_release_dev(struct obd_device *obd);
84
85 int class_name2dev(const char *name);
86 struct obd_device *class_name2obd(const char *name);
87 int class_uuid2dev(struct obd_uuid *uuid);
88 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
89 void class_obd_list(void);
90 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
91                                           const char * typ_name,
92                                           struct obd_uuid *grp_uuid);
93 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid,
94                                            int *next);
95 struct obd_device * class_num2obd(int num);
96
97 int class_notify_sptlrpc_conf(const char *fsname, int namelen);
98
99 char *obd_export_nid2str(struct obd_export *exp);
100
101 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid);
102 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid);
103
104 int obd_zombie_impexp_init(void);
105 void obd_zombie_impexp_stop(void);
106 void obd_zombie_impexp_cull(void);
107 void obd_zombie_barrier(void);
108 void obd_exports_barrier(struct obd_device *obd);
109 int kuc_len(int payload_len);
110 struct kuc_hdr * kuc_ptr(void *p);
111 int kuc_ispayload(void *p);
112 void *kuc_alloc(int payload_len, int transport, int type);
113 void kuc_free(void *p, int payload_len);
114
115 /* obd_config.c */
116 int class_process_config(struct lustre_cfg *lcfg);
117 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
118                              struct lustre_cfg *lcfg, void *data);
119 int class_attach(struct lustre_cfg *lcfg);
120 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
121 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
122 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
123 struct obd_device *class_incref(struct obd_device *obd,
124                                 const char *scope, const void *source);
125 void class_decref(struct obd_device *obd,
126                   const char *scope, const void *source);
127 void dump_exports(struct obd_device *obd, int locks);
128
129 /*obdecho*/
130 #ifdef LPROCFS
131 extern void lprocfs_echo_init_vars(struct lprocfs_static_vars *lvars);
132 #else
133 static inline void lprocfs_echo_init_vars(struct lprocfs_static_vars *lvars)
134 {
135         memset(lvars, 0, sizeof(*lvars));
136 }
137 #endif
138
139 #define CFG_F_START     0x01   /* Set when we start updating from a log */
140 #define CFG_F_MARKER    0x02   /* We are within a maker */
141 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
142 #define CFG_F_COMPAT146 0x08   /* Allow old-style logs */
143 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
144
145 /* Passed as data param to class_config_parse_llog */
146 struct config_llog_instance {
147         char               *cfg_obdname;
148         void               *cfg_instance;
149         struct super_block *cfg_sb;
150         struct obd_uuid     cfg_uuid;
151         int                 cfg_last_idx; /* for partial llog processing */
152         int                 cfg_flags;
153 };
154 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
155                             struct config_llog_instance *cfg);
156 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
157                            struct config_llog_instance *cfg);
158
159 /* list of active configuration logs  */
160 struct config_llog_data {
161         char                       *cld_logname;
162         struct ldlm_res_id          cld_resid;
163         struct config_llog_instance cld_cfg;
164         cfs_list_t                  cld_list_chain;
165         cfs_atomic_t                cld_refcount;
166         struct config_llog_data    *cld_sptlrpc;/* depended sptlrpc log */
167         struct obd_export          *cld_mgcexp;
168         cfs_mutex_t                 cld_lock;
169         unsigned int                cld_stopping:1, /* we were told to stop
170                                                      * watching */
171                                     cld_lostlock:1, /* lock not requeued */
172                                     cld_is_sptlrpc:1;
173 };
174
175 struct lustre_profile {
176         cfs_list_t       lp_list;
177         char            *lp_profile;
178         char            *lp_dt;
179         char            *lp_md;
180 };
181
182 struct lustre_profile *class_get_profile(const char * prof);
183 void class_del_profile(const char *prof);
184 void class_del_profiles(void);
185
186 #if LUSTRE_TRACKS_LOCK_EXP_REFS
187
188 void __class_export_add_lock_ref(struct obd_export *, struct ldlm_lock *);
189 void __class_export_del_lock_ref(struct obd_export *, struct ldlm_lock *);
190 extern void (*class_export_dump_hook)(struct obd_export *);
191
192 #else
193
194 #define __class_export_add_lock_ref(exp, lock)             do {} while(0)
195 #define __class_export_del_lock_ref(exp, lock)             do {} while(0)
196
197 #endif
198
199 #define class_export_rpc_get(exp)                                       \
200 ({                                                                      \
201         cfs_atomic_inc(&(exp)->exp_rpc_count);                          \
202         CDEBUG(D_INFO, "RPC GETting export %p : new rpc_count %d\n",    \
203                (exp), cfs_atomic_read(&(exp)->exp_rpc_count));          \
204         class_export_get(exp);                                          \
205 })
206
207 #define class_export_rpc_put(exp)                                       \
208 ({                                                                      \
209         LASSERT_ATOMIC_POS(&exp->exp_rpc_count);                        \
210         cfs_atomic_dec(&(exp)->exp_rpc_count);                          \
211         CDEBUG(D_INFO, "RPC PUTting export %p : new rpc_count %d\n",    \
212                (exp), cfs_atomic_read(&(exp)->exp_rpc_count));          \
213         class_export_put(exp);                                          \
214 })
215
216 #define class_export_lock_get(exp, lock)                                \
217 ({                                                                      \
218         cfs_atomic_inc(&(exp)->exp_locks_count);                        \
219         __class_export_add_lock_ref(exp, lock);                         \
220         CDEBUG(D_INFO, "lock GETting export %p : new locks_count %d\n", \
221                (exp), cfs_atomic_read(&(exp)->exp_locks_count));        \
222         class_export_get(exp);                                          \
223 })
224
225 #define class_export_lock_put(exp, lock)                                \
226 ({                                                                      \
227         LASSERT_ATOMIC_POS(&exp->exp_locks_count);                      \
228         cfs_atomic_dec(&(exp)->exp_locks_count);                        \
229         __class_export_del_lock_ref(exp, lock);                         \
230         CDEBUG(D_INFO, "lock PUTting export %p : new locks_count %d\n", \
231                (exp), cfs_atomic_read(&(exp)->exp_locks_count));        \
232         class_export_put(exp);                                          \
233 })
234
235 #define class_export_cb_get(exp)                                        \
236 ({                                                                      \
237         cfs_atomic_inc(&(exp)->exp_cb_count);                           \
238         CDEBUG(D_INFO, "callback GETting export %p : new cb_count %d\n",\
239                (exp), cfs_atomic_read(&(exp)->exp_cb_count));           \
240         class_export_get(exp);                                          \
241 })
242
243 #define class_export_cb_put(exp)                                        \
244 ({                                                                      \
245         LASSERT_ATOMIC_POS(&exp->exp_cb_count);                         \
246         cfs_atomic_dec(&(exp)->exp_cb_count);                           \
247         CDEBUG(D_INFO, "callback PUTting export %p : new cb_count %d\n",\
248                (exp), cfs_atomic_read(&(exp)->exp_cb_count));           \
249         class_export_put(exp);                                          \
250 })
251
252 /* genops.c */
253 struct obd_export *class_export_get(struct obd_export *exp);
254 void class_export_put(struct obd_export *exp);
255 struct obd_export *class_new_export(struct obd_device *obddev,
256                                     struct obd_uuid *cluuid);
257 void class_unlink_export(struct obd_export *exp);
258
259 struct obd_import *class_import_get(struct obd_import *);
260 void class_import_put(struct obd_import *);
261 struct obd_import *class_new_import(struct obd_device *obd);
262 void class_destroy_import(struct obd_import *exp);
263
264 struct obd_type *class_search_type(const char *name);
265 struct obd_type *class_get_type(const char *name);
266 void class_put_type(struct obd_type *type);
267 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
268                   struct obd_uuid *cluuid);
269 int class_disconnect(struct obd_export *exp);
270 void class_fail_export(struct obd_export *exp);
271 int class_connected_export(struct obd_export *exp);
272 void class_disconnect_exports(struct obd_device *obddev);
273 int class_manual_cleanup(struct obd_device *obd);
274 void class_disconnect_stale_exports(struct obd_device *,
275                                     int (*test_export)(struct obd_export *));
276 static inline enum obd_option exp_flags_from_obd(struct obd_device *obd)
277 {
278         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
279                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
280                 (obd->obd_abort_recovery ? OBD_OPT_ABORT_RECOV : 0) |
281                 0);
282 }
283
284 static inline struct lu_target *class_exp2tgt(struct obd_export *exp)
285 {
286         LASSERT(exp->exp_obd);
287         return exp->exp_obd->u.obt.obt_lut;
288 }
289
290 static inline struct lr_server_data *class_server_data(struct obd_device *obd)
291 {
292         LASSERT(obd->u.obt.obt_lut);
293         return &obd->u.obt.obt_lut->lut_lsd;
294 }
295
296 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid);
297 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj);
298 void obdo_from_iattr(struct obdo *oa, struct iattr *attr,
299                      unsigned int ia_valid);
300 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid);
301 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid);
302 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
303                   unsigned int valid);
304
305 #define OBT(dev)        (dev)->obd_type
306 #define OBP(dev, op)    (dev)->obd_type->typ_dt_ops->o_ ## op
307 #define MDP(dev, op)    (dev)->obd_type->typ_md_ops->m_ ## op
308 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
309
310 /* Ensure obd_setup: used for cleanup which must be called
311    while obd is stopping */
312 #define OBD_CHECK_DEV(obd)                                      \
313 do {                                                            \
314         if (!(obd)) {                                           \
315                 CERROR("NULL device\n");                        \
316                 RETURN(-ENODEV);                                \
317         }                                                       \
318 } while (0)
319
320 /* ensure obd_setup and !obd_stopping */
321 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
322 do {                                                            \
323         OBD_CHECK_DEV(obd);                                     \
324         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
325                 CERROR("Device %d not setup\n",                 \
326                        (obd)->obd_minor);                       \
327                 RETURN(-ENODEV);                                \
328         }                                                       \
329 } while (0)
330
331
332 #ifdef LPROCFS
333 #define OBD_COUNTER_OFFSET(op)                                  \
334         ((offsetof(struct obd_ops, o_ ## op) -                  \
335           offsetof(struct obd_ops, o_iocontrol))                \
336          / sizeof(((struct obd_ops *)(0))->o_iocontrol))
337
338 #define OBD_COUNTER_INCREMENT(obdx, op)                           \
339         if ((obdx)->obd_stats != NULL) {                          \
340                 unsigned int coffset;                             \
341                 coffset = (unsigned int)((obdx)->obd_cntr_base) + \
342                         OBD_COUNTER_OFFSET(op);                   \
343                 LASSERT(coffset < (obdx)->obd_stats->ls_num);     \
344                 lprocfs_counter_incr((obdx)->obd_stats, coffset); \
345         }
346
347 #define EXP_COUNTER_INCREMENT(export, op)                                    \
348         if ((export)->exp_obd->obd_stats != NULL) {                          \
349                 unsigned int coffset;                                        \
350                 coffset = (unsigned int)((export)->exp_obd->obd_cntr_base) + \
351                         OBD_COUNTER_OFFSET(op);                              \
352                 LASSERT(coffset < (export)->exp_obd->obd_stats->ls_num);     \
353                 lprocfs_counter_incr((export)->exp_obd->obd_stats, coffset); \
354                 if ((export)->exp_nid_stats != NULL &&                       \
355                     (export)->exp_nid_stats->nid_stats != NULL)              \
356                         lprocfs_counter_incr(                                \
357                                 (export)->exp_nid_stats->nid_stats, coffset);\
358         }
359
360 #define MD_COUNTER_OFFSET(op)                                   \
361         ((offsetof(struct md_ops, m_ ## op) -                   \
362           offsetof(struct md_ops, m_getstatus))                 \
363          / sizeof(((struct md_ops *)(0))->m_getstatus))
364
365 #define MD_COUNTER_INCREMENT(obdx, op)                           \
366         if ((obd)->md_stats != NULL) {                           \
367                 unsigned int coffset;                            \
368                 coffset = (unsigned int)((obdx)->md_cntr_base) + \
369                         MD_COUNTER_OFFSET(op);                   \
370                 LASSERT(coffset < (obdx)->md_stats->ls_num);     \
371                 lprocfs_counter_incr((obdx)->md_stats, coffset); \
372         }
373
374 #define EXP_MD_COUNTER_INCREMENT(export, op)                                 \
375         if ((export)->exp_obd->obd_stats != NULL) {                          \
376                 unsigned int coffset;                                        \
377                 coffset = (unsigned int)((export)->exp_obd->md_cntr_base) +  \
378                         MD_COUNTER_OFFSET(op);                               \
379                 LASSERT(coffset < (export)->exp_obd->md_stats->ls_num);      \
380                 lprocfs_counter_incr((export)->exp_obd->md_stats, coffset);  \
381                 if ((export)->exp_md_stats != NULL)                          \
382                         lprocfs_counter_incr(                                \
383                                 (export)->exp_md_stats, coffset);            \
384         }
385
386 #else
387 #define OBD_COUNTER_OFFSET(op)
388 #define OBD_COUNTER_INCREMENT(obd, op)
389 #define EXP_COUNTER_INCREMENT(exp, op)
390 #define MD_COUNTER_INCREMENT(obd, op)
391 #define EXP_MD_COUNTER_INCREMENT(exp, op)
392 #endif
393
394 static inline int lprocfs_nid_ldlm_stats_init(struct nid_stat* tmp)
395 {
396         /* Always add in ldlm_stats */
397         tmp->nid_ldlm_stats = lprocfs_alloc_stats(LDLM_LAST_OPC - LDLM_FIRST_OPC
398                                                   ,LPROCFS_STATS_FLAG_NOPERCPU);
399         if (tmp->nid_ldlm_stats == NULL)
400                 return -ENOMEM;
401
402         lprocfs_init_ldlm_stats(tmp->nid_ldlm_stats);
403
404         return lprocfs_register_stats(tmp->nid_proc, "ldlm_stats",
405                                       tmp->nid_ldlm_stats);
406 }
407
408 #define OBD_CHECK_MD_OP(obd, op, err)                           \
409 do {                                                            \
410         if (!OBT(obd) || !MDP((obd), op)) {                     \
411                 if (err)                                        \
412                         CERROR("md_" #op ": dev %s/%d no operation\n", \
413                                obd->obd_name, obd->obd_minor);  \
414                 RETURN(err);                                    \
415         }                                                       \
416 } while (0)
417
418 #define EXP_CHECK_MD_OP(exp, op)                                \
419 do {                                                            \
420         if ((exp) == NULL) {                                    \
421                 CERROR("obd_" #op ": NULL export\n");           \
422                 RETURN(-ENODEV);                                \
423         }                                                       \
424         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
425                 CERROR("obd_" #op ": cleaned up obd\n");        \
426                 RETURN(-EOPNOTSUPP);                            \
427         }                                                       \
428         if (!OBT((exp)->exp_obd) || !MDP((exp)->exp_obd, op)) { \
429                 CERROR("obd_" #op ": dev %s/%d no operation\n", \
430                        (exp)->exp_obd->obd_name,                \
431                        (exp)->exp_obd->obd_minor);              \
432                 RETURN(-EOPNOTSUPP);                            \
433         }                                                       \
434 } while (0)
435
436
437 #define OBD_CHECK_DT_OP(obd, op, err)                           \
438 do {                                                            \
439         if (!OBT(obd) || !OBP((obd), op)) {                     \
440                 if (err)                                        \
441                         CERROR("obd_" #op ": dev %d no operation\n",    \
442                                obd->obd_minor);                 \
443                 RETURN(err);                                    \
444         }                                                       \
445 } while (0)
446
447 #define EXP_CHECK_DT_OP(exp, op)                                \
448 do {                                                            \
449         if ((exp) == NULL) {                                    \
450                 CERROR("obd_" #op ": NULL export\n");           \
451                 RETURN(-ENODEV);                                \
452         }                                                       \
453         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
454                 CERROR("obd_" #op ": cleaned up obd\n");        \
455                 RETURN(-EOPNOTSUPP);                            \
456         }                                                       \
457         if (!OBT((exp)->exp_obd) || !OBP((exp)->exp_obd, op)) { \
458                 CERROR("obd_" #op ": dev %d no operation\n",    \
459                        (exp)->exp_obd->obd_minor);              \
460                 RETURN(-EOPNOTSUPP);                            \
461         }                                                       \
462 } while (0)
463
464 #define CTXT_CHECK_OP(ctxt, op, err)                                 \
465 do {                                                                 \
466         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {             \
467                 if (err)                                             \
468                         CERROR("lop_" #op ": dev %d no operation\n", \
469                                ctxt->loc_obd->obd_minor);            \
470                 RETURN(err);                                         \
471         }                                                            \
472 } while (0)
473
474 static inline int class_devno_max(void)
475 {
476         return MAX_OBD_DEVICES;
477 }
478
479 static inline int obd_get_info(struct obd_export *exp, __u32 keylen,
480                                void *key, __u32 *vallen, void *val,
481                                struct lov_stripe_md *lsm)
482 {
483         int rc;
484         ENTRY;
485
486         EXP_CHECK_DT_OP(exp, get_info);
487         EXP_COUNTER_INCREMENT(exp, get_info);
488
489         rc = OBP(exp->exp_obd, get_info)(exp, keylen, key, vallen, val, lsm);
490         RETURN(rc);
491 }
492
493 static inline int obd_set_info_async(struct obd_export *exp, obd_count keylen,
494                                      void *key, obd_count vallen, void *val,
495                                      struct ptlrpc_request_set *set)
496 {
497         int rc;
498         ENTRY;
499
500         EXP_CHECK_DT_OP(exp, set_info_async);
501         EXP_COUNTER_INCREMENT(exp, set_info_async);
502
503         rc = OBP(exp->exp_obd, set_info_async)(exp, keylen, key, vallen, val,
504                                                set);
505         RETURN(rc);
506 }
507
508 /*
509  * obd-lu integration.
510  *
511  * Functionality is being moved into new lu_device-based layering, but some
512  * pieces of configuration process are still based on obd devices.
513  *
514  * Specifically, lu_device_type_operations::ldto_device_alloc() methods fully
515  * subsume ->o_setup() methods of obd devices they replace. The same for
516  * lu_device_operations::ldo_process_config() and ->o_process_config(). As a
517  * result, obd_setup() and obd_process_config() branch and call one XOR
518  * another.
519  *
520  * Yet neither lu_device_type_operations::ldto_device_fini() nor
521  * lu_device_type_operations::ldto_device_free() fully implement the
522  * functionality of ->o_precleanup() and ->o_cleanup() they override. Hence,
523  * obd_precleanup() and obd_cleanup() call both lu_device and obd operations.
524  */
525
526 #define DECLARE_LU_VARS(ldt, d)                 \
527         struct lu_device_type *ldt;       \
528         struct lu_device *d
529
530 static inline int obd_setup(struct obd_device *obd, struct lustre_cfg *cfg)
531 {
532         int rc;
533         DECLARE_LU_VARS(ldt, d);
534         ENTRY;
535
536         ldt = obd->obd_type->typ_lu;
537         if (ldt != NULL) {
538                 struct lu_context  session_ctx;
539                 struct lu_env env;
540                 lu_context_init(&session_ctx, LCT_SESSION);
541                 session_ctx.lc_thread = NULL;
542                 lu_context_enter(&session_ctx);
543
544                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
545                 if (rc == 0) {
546                         env.le_ses = &session_ctx;
547                         d = ldt->ldt_ops->ldto_device_alloc(&env, ldt, cfg);
548                         lu_env_fini(&env);
549                         if (!IS_ERR(d)) {
550                                 obd->obd_lu_dev = d;
551                                 d->ld_obd = obd;
552                                 rc = 0;
553                         } else
554                                 rc = PTR_ERR(d);
555                 }
556                 lu_context_exit(&session_ctx);
557                 lu_context_fini(&session_ctx);
558
559         } else {
560                 OBD_CHECK_DT_OP(obd, setup, -EOPNOTSUPP);
561                 OBD_COUNTER_INCREMENT(obd, setup);
562                 rc = OBP(obd, setup)(obd, cfg);
563         }
564         RETURN(rc);
565 }
566
567 static inline int obd_precleanup(struct obd_device *obd,
568                                  enum obd_cleanup_stage cleanup_stage)
569 {
570         int rc;
571         DECLARE_LU_VARS(ldt, d);
572         ENTRY;
573
574         OBD_CHECK_DEV(obd);
575         ldt = obd->obd_type->typ_lu;
576         d = obd->obd_lu_dev;
577         if (ldt != NULL && d != NULL) {
578                 if (cleanup_stage == OBD_CLEANUP_EXPORTS) {
579                         struct lu_env env;
580
581                         rc = lu_env_init(&env, ldt->ldt_ctx_tags);
582                         if (rc == 0) {
583                                 ldt->ldt_ops->ldto_device_fini(&env, d);
584                                 lu_env_fini(&env);
585                         }
586                 }
587         }
588         OBD_CHECK_DT_OP(obd, precleanup, 0);
589         OBD_COUNTER_INCREMENT(obd, precleanup);
590
591         rc = OBP(obd, precleanup)(obd, cleanup_stage);
592         RETURN(rc);
593 }
594
595 static inline int obd_cleanup(struct obd_device *obd)
596 {
597         int rc;
598         DECLARE_LU_VARS(ldt, d);
599         ENTRY;
600
601         OBD_CHECK_DEV(obd);
602
603         ldt = obd->obd_type->typ_lu;
604         d = obd->obd_lu_dev;
605         if (ldt != NULL && d != NULL) {
606                 struct lu_env env;
607
608                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
609                 if (rc == 0) {
610                         ldt->ldt_ops->ldto_device_free(&env, d);
611                         lu_env_fini(&env);
612                         obd->obd_lu_dev = NULL;
613                 }
614         }
615         OBD_CHECK_DT_OP(obd, cleanup, 0);
616         OBD_COUNTER_INCREMENT(obd, cleanup);
617
618         rc = OBP(obd, cleanup)(obd);
619         RETURN(rc);
620 }
621
622 static inline void obd_cleanup_client_import(struct obd_device *obd)
623 {
624         ENTRY;
625
626         /* If we set up but never connected, the
627            client import will not have been cleaned. */
628         cfs_down_write(&obd->u.cli.cl_sem);
629         if (obd->u.cli.cl_import) {
630                 struct obd_import *imp;
631                 imp = obd->u.cli.cl_import;
632                 CDEBUG(D_CONFIG, "%s: client import never connected\n",
633                        obd->obd_name);
634                 ptlrpc_invalidate_import(imp);
635                 if (imp->imp_rq_pool) {
636                         ptlrpc_free_rq_pool(imp->imp_rq_pool);
637                         imp->imp_rq_pool = NULL;
638                 }
639                 class_destroy_import(imp);
640                 obd->u.cli.cl_import = NULL;
641         }
642         cfs_up_write(&obd->u.cli.cl_sem);
643
644         EXIT;
645 }
646
647 static inline int
648 obd_process_config(struct obd_device *obd, int datalen, void *data)
649 {
650         int rc;
651         DECLARE_LU_VARS(ldt, d);
652         ENTRY;
653
654         OBD_CHECK_DEV(obd);
655
656         obd->obd_process_conf = 1;
657         ldt = obd->obd_type->typ_lu;
658         d = obd->obd_lu_dev;
659         if (ldt != NULL && d != NULL) {
660                 struct lu_env env;
661
662                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
663                 if (rc == 0) {
664                         rc = d->ld_ops->ldo_process_config(&env, d, data);
665                         lu_env_fini(&env);
666                 }
667         } else {
668                 OBD_CHECK_DT_OP(obd, process_config, -EOPNOTSUPP);
669                 rc = OBP(obd, process_config)(obd, datalen, data);
670         }
671         OBD_COUNTER_INCREMENT(obd, process_config);
672         obd->obd_process_conf = 0;
673
674         RETURN(rc);
675 }
676
677 /* Pack an in-memory MD struct for storage on disk.
678  * Returns +ve size of packed MD (0 for free), or -ve error.
679  *
680  * If @disk_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
681  * If @*disk_tgt != NULL and @mem_src == NULL, @*disk_tgt will be freed.
682  * If @*disk_tgt == NULL, it will be allocated
683  */
684 static inline int obd_packmd(struct obd_export *exp,
685                              struct lov_mds_md **disk_tgt,
686                              struct lov_stripe_md *mem_src)
687 {
688         int rc;
689         ENTRY;
690
691         EXP_CHECK_DT_OP(exp, packmd);
692         EXP_COUNTER_INCREMENT(exp, packmd);
693
694         rc = OBP(exp->exp_obd, packmd)(exp, disk_tgt, mem_src);
695         RETURN(rc);
696 }
697
698 static inline int obd_size_diskmd(struct obd_export *exp,
699                                   struct lov_stripe_md *mem_src)
700 {
701         return obd_packmd(exp, NULL, mem_src);
702 }
703
704 /* helper functions */
705 static inline int obd_alloc_diskmd(struct obd_export *exp,
706                                    struct lov_mds_md **disk_tgt)
707 {
708         LASSERT(disk_tgt);
709         LASSERT(*disk_tgt == NULL);
710         return obd_packmd(exp, disk_tgt, NULL);
711 }
712
713 static inline int obd_free_diskmd(struct obd_export *exp,
714                                   struct lov_mds_md **disk_tgt)
715 {
716         LASSERT(disk_tgt);
717         LASSERT(*disk_tgt);
718         return obd_packmd(exp, disk_tgt, NULL);
719 }
720
721 /* Unpack an MD struct from disk to in-memory format.
722  * Returns +ve size of unpacked MD (0 for free), or -ve error.
723  *
724  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
725  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
726  * If @*mem_tgt == NULL, it will be allocated
727  */
728 static inline int obd_unpackmd(struct obd_export *exp,
729                                struct lov_stripe_md **mem_tgt,
730                                struct lov_mds_md *disk_src,
731                                int disk_len)
732 {
733         int rc;
734         ENTRY;
735
736         EXP_CHECK_DT_OP(exp, unpackmd);
737         EXP_COUNTER_INCREMENT(exp, unpackmd);
738
739         rc = OBP(exp->exp_obd, unpackmd)(exp, mem_tgt, disk_src, disk_len);
740         RETURN(rc);
741 }
742
743 /* helper functions */
744 static inline int obd_alloc_memmd(struct obd_export *exp,
745                                   struct lov_stripe_md **mem_tgt)
746 {
747         LASSERT(mem_tgt);
748         LASSERT(*mem_tgt == NULL);
749         return obd_unpackmd(exp, mem_tgt, NULL, 0);
750 }
751
752 static inline int obd_free_memmd(struct obd_export *exp,
753                                  struct lov_stripe_md **mem_tgt)
754 {
755         int rc;
756
757         LASSERT(mem_tgt);
758         LASSERT(*mem_tgt);
759         rc = obd_unpackmd(exp, mem_tgt, NULL, 0);
760         *mem_tgt = NULL;
761         return rc;
762 }
763
764 static inline int obd_precreate(struct obd_export *exp)
765 {
766         int rc;
767         ENTRY;
768
769         EXP_CHECK_DT_OP(exp, precreate);
770         OBD_COUNTER_INCREMENT(exp->exp_obd, precreate);
771
772         rc = OBP(exp->exp_obd, precreate)(exp);
773         RETURN(rc);
774 }
775
776 static inline int obd_create_async(struct obd_export *exp,
777                                    struct obd_info *oinfo,
778                                    struct lov_stripe_md **ea,
779                                    struct obd_trans_info *oti)
780 {
781         int rc;
782         ENTRY;
783
784         EXP_CHECK_DT_OP(exp, create_async);
785         EXP_COUNTER_INCREMENT(exp, create_async);
786
787         rc = OBP(exp->exp_obd, create_async)(exp, oinfo, ea, oti);
788         RETURN(rc);
789 }
790
791 static inline int obd_create(struct obd_export *exp, struct obdo *obdo,
792                              struct lov_stripe_md **ea,
793                              struct obd_trans_info *oti)
794 {
795         int rc;
796         ENTRY;
797
798         EXP_CHECK_DT_OP(exp, create);
799         EXP_COUNTER_INCREMENT(exp, create);
800
801         rc = OBP(exp->exp_obd, create)(exp, obdo, ea, oti);
802         RETURN(rc);
803 }
804
805 static inline int obd_destroy(struct obd_export *exp, struct obdo *obdo,
806                               struct lov_stripe_md *ea,
807                               struct obd_trans_info *oti,
808                               struct obd_export *md_exp, void *capa)
809 {
810         int rc;
811         ENTRY;
812
813         EXP_CHECK_DT_OP(exp, destroy);
814         EXP_COUNTER_INCREMENT(exp, destroy);
815
816         rc = OBP(exp->exp_obd, destroy)(exp, obdo, ea, oti, md_exp, capa);
817         RETURN(rc);
818 }
819
820 static inline int obd_getattr(struct obd_export *exp, struct obd_info *oinfo)
821 {
822         int rc;
823         ENTRY;
824
825         EXP_CHECK_DT_OP(exp, getattr);
826         EXP_COUNTER_INCREMENT(exp, getattr);
827
828         rc = OBP(exp->exp_obd, getattr)(exp, oinfo);
829         RETURN(rc);
830 }
831
832 static inline int obd_getattr_async(struct obd_export *exp,
833                                     struct obd_info *oinfo,
834                                     struct ptlrpc_request_set *set)
835 {
836         int rc;
837         ENTRY;
838
839         EXP_CHECK_DT_OP(exp, getattr_async);
840         EXP_COUNTER_INCREMENT(exp, getattr_async);
841
842         rc = OBP(exp->exp_obd, getattr_async)(exp, oinfo, set);
843         RETURN(rc);
844 }
845
846 static inline int obd_setattr(struct obd_export *exp, struct obd_info *oinfo,
847                               struct obd_trans_info *oti)
848 {
849         int rc;
850         ENTRY;
851
852         EXP_CHECK_DT_OP(exp, setattr);
853         EXP_COUNTER_INCREMENT(exp, setattr);
854
855         rc = OBP(exp->exp_obd, setattr)(exp, oinfo, oti);
856         RETURN(rc);
857 }
858
859 /* This performs all the requests set init/wait/destroy actions. */
860 static inline int obd_setattr_rqset(struct obd_export *exp,
861                                     struct obd_info *oinfo,
862                                     struct obd_trans_info *oti)
863 {
864         struct ptlrpc_request_set *set = NULL;
865         int rc;
866         ENTRY;
867
868         EXP_CHECK_DT_OP(exp, setattr_async);
869         EXP_COUNTER_INCREMENT(exp, setattr_async);
870
871         set =  ptlrpc_prep_set();
872         if (set == NULL)
873                 RETURN(-ENOMEM);
874
875         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
876         if (rc == 0)
877                 rc = ptlrpc_set_wait(set);
878         ptlrpc_set_destroy(set);
879         RETURN(rc);
880 }
881
882 /* This adds all the requests into @set if @set != NULL, otherwise
883    all requests are sent asynchronously without waiting for response. */
884 static inline int obd_setattr_async(struct obd_export *exp,
885                                     struct obd_info *oinfo,
886                                     struct obd_trans_info *oti,
887                                     struct ptlrpc_request_set *set)
888 {
889         int rc;
890         ENTRY;
891
892         EXP_CHECK_DT_OP(exp, setattr_async);
893         EXP_COUNTER_INCREMENT(exp, setattr_async);
894
895         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
896         RETURN(rc);
897 }
898
899 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
900                                int priority)
901 {
902         struct obd_device *obd = imp->imp_obd;
903         int rc;
904         ENTRY;
905
906         OBD_CHECK_DEV_ACTIVE(obd);
907         OBD_CHECK_DT_OP(obd, add_conn, -EOPNOTSUPP);
908         OBD_COUNTER_INCREMENT(obd, add_conn);
909
910         rc = OBP(obd, add_conn)(imp, uuid, priority);
911         RETURN(rc);
912 }
913
914 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
915 {
916         struct obd_device *obd = imp->imp_obd;
917         int rc;
918         ENTRY;
919
920         OBD_CHECK_DEV_ACTIVE(obd);
921         OBD_CHECK_DT_OP(obd, del_conn, -EOPNOTSUPP);
922         OBD_COUNTER_INCREMENT(obd, del_conn);
923
924         rc = OBP(obd, del_conn)(imp, uuid);
925         RETURN(rc);
926 }
927
928 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
929 {
930         struct obd_uuid *uuid;
931         ENTRY;
932
933         OBD_CHECK_DT_OP(exp->exp_obd, get_uuid, NULL);
934         EXP_COUNTER_INCREMENT(exp, get_uuid);
935
936         uuid = OBP(exp->exp_obd, get_uuid)(exp);
937         RETURN(uuid);
938 }
939
940 /** Create a new /a exp on device /a obd for the uuid /a cluuid
941  * @param exp New export handle
942  * @param d Connect data, supported flags are set, flags also understood
943  *    by obd are returned.
944  */
945 static inline int obd_connect(const struct lu_env *env,
946                               struct obd_export **exp,struct obd_device *obd,
947                               struct obd_uuid *cluuid,
948                               struct obd_connect_data *d,
949                               void *localdata)
950 {
951         int rc;
952         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
953                                                    * check */
954         ENTRY;
955
956         OBD_CHECK_DEV_ACTIVE(obd);
957         OBD_CHECK_DT_OP(obd, connect, -EOPNOTSUPP);
958         OBD_COUNTER_INCREMENT(obd, connect);
959
960         rc = OBP(obd, connect)(env, exp, obd, cluuid, d, localdata);
961         /* check that only subset is granted */
962         LASSERT(ergo(d != NULL,
963                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
964         RETURN(rc);
965 }
966
967 static inline int obd_reconnect(const struct lu_env *env,
968                                 struct obd_export *exp,
969                                 struct obd_device *obd,
970                                 struct obd_uuid *cluuid,
971                                 struct obd_connect_data *d,
972                                 void *localdata)
973 {
974         int rc;
975         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
976                                                    * check */
977
978         ENTRY;
979
980         OBD_CHECK_DEV_ACTIVE(obd);
981         OBD_CHECK_DT_OP(obd, reconnect, 0);
982         OBD_COUNTER_INCREMENT(obd, reconnect);
983
984         rc = OBP(obd, reconnect)(env, exp, obd, cluuid, d, localdata);
985         /* check that only subset is granted */
986         LASSERT(ergo(d != NULL,
987                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
988         RETURN(rc);
989 }
990
991 static inline int obd_disconnect(struct obd_export *exp)
992 {
993         int rc;
994         ENTRY;
995
996         EXP_CHECK_DT_OP(exp, disconnect);
997         EXP_COUNTER_INCREMENT(exp, disconnect);
998
999         rc = OBP(exp->exp_obd, disconnect)(exp);
1000         RETURN(rc);
1001 }
1002
1003 static inline int obd_fid_init(struct obd_export *exp)
1004 {
1005         int rc;
1006         ENTRY;
1007
1008         OBD_CHECK_DT_OP(exp->exp_obd, fid_init, 0);
1009         EXP_COUNTER_INCREMENT(exp, fid_init);
1010
1011         rc = OBP(exp->exp_obd, fid_init)(exp);
1012         RETURN(rc);
1013 }
1014
1015 static inline int obd_fid_fini(struct obd_export *exp)
1016 {
1017         int rc;
1018         ENTRY;
1019
1020         OBD_CHECK_DT_OP(exp->exp_obd, fid_fini, 0);
1021         EXP_COUNTER_INCREMENT(exp, fid_fini);
1022
1023         rc = OBP(exp->exp_obd, fid_fini)(exp);
1024         RETURN(rc);
1025 }
1026
1027 static inline int obd_fid_alloc(struct obd_export *exp,
1028                                 struct lu_fid *fid,
1029                                 struct md_op_data *op_data)
1030 {
1031         int rc;
1032         ENTRY;
1033
1034         EXP_CHECK_DT_OP(exp, fid_alloc);
1035         EXP_COUNTER_INCREMENT(exp, fid_alloc);
1036
1037         rc = OBP(exp->exp_obd, fid_alloc)(exp, fid, op_data);
1038         RETURN(rc);
1039 }
1040
1041 static inline int obd_fid_delete(struct obd_export *exp,
1042                                  const struct lu_fid *fid)
1043 {
1044         int rc;
1045         ENTRY;
1046
1047         EXP_CHECK_DT_OP(exp, fid_delete);
1048         EXP_COUNTER_INCREMENT(exp, fid_delete);
1049
1050         rc = OBP(exp->exp_obd, fid_delete)(exp, fid);
1051         RETURN(rc);
1052 }
1053
1054 static inline int obd_ping(struct obd_export *exp)
1055 {
1056         int rc;
1057         ENTRY;
1058
1059         OBD_CHECK_DT_OP(exp->exp_obd, ping, 0);
1060         EXP_COUNTER_INCREMENT(exp, ping);
1061
1062         rc = OBP(exp->exp_obd, ping)(exp);
1063         RETURN(rc);
1064 }
1065
1066 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
1067 {
1068         int rc;
1069         ENTRY;
1070
1071         OBD_CHECK_DT_OP(obd, pool_new, -EOPNOTSUPP);
1072         OBD_COUNTER_INCREMENT(obd, pool_new);
1073
1074         rc = OBP(obd, pool_new)(obd, poolname);
1075         RETURN(rc);
1076 }
1077
1078 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
1079 {
1080         int rc;
1081         ENTRY;
1082
1083         OBD_CHECK_DT_OP(obd, pool_del, -EOPNOTSUPP);
1084         OBD_COUNTER_INCREMENT(obd, pool_del);
1085
1086         rc = OBP(obd, pool_del)(obd, poolname);
1087         RETURN(rc);
1088 }
1089
1090 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
1091 {
1092         int rc;
1093         ENTRY;
1094
1095         OBD_CHECK_DT_OP(obd, pool_add, -EOPNOTSUPP);
1096         OBD_COUNTER_INCREMENT(obd, pool_add);
1097
1098         rc = OBP(obd, pool_add)(obd, poolname, ostname);
1099         RETURN(rc);
1100 }
1101
1102 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
1103 {
1104         int rc;
1105         ENTRY;
1106
1107         OBD_CHECK_DT_OP(obd, pool_rem, -EOPNOTSUPP);
1108         OBD_COUNTER_INCREMENT(obd, pool_rem);
1109
1110         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
1111         RETURN(rc);
1112 }
1113
1114 static inline void obd_getref(struct obd_device *obd)
1115 {
1116         ENTRY;
1117         if (OBT(obd) && OBP(obd, getref)) {
1118                 OBD_COUNTER_INCREMENT(obd, getref);
1119                 OBP(obd, getref)(obd);
1120         }
1121         EXIT;
1122 }
1123
1124 static inline void obd_putref(struct obd_device *obd)
1125 {
1126         ENTRY;
1127         if (OBT(obd) && OBP(obd, putref)) {
1128                 OBD_COUNTER_INCREMENT(obd, putref);
1129                 OBP(obd, putref)(obd);
1130         }
1131         EXIT;
1132 }
1133
1134 static inline int obd_init_export(struct obd_export *exp)
1135 {
1136         int rc = 0;
1137
1138         ENTRY;
1139         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1140             OBP((exp)->exp_obd, init_export))
1141                 rc = OBP(exp->exp_obd, init_export)(exp);
1142         RETURN(rc);
1143 }
1144
1145 static inline int obd_destroy_export(struct obd_export *exp)
1146 {
1147         ENTRY;
1148         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1149             OBP((exp)->exp_obd, destroy_export))
1150                 OBP(exp->exp_obd, destroy_export)(exp);
1151         RETURN(0);
1152 }
1153
1154 static inline int obd_extent_calc(struct obd_export *exp,
1155                                   struct lov_stripe_md *md,
1156                                   int cmd, obd_off *offset)
1157 {
1158         int rc;
1159         ENTRY;
1160         EXP_CHECK_DT_OP(exp, extent_calc);
1161         rc = OBP(exp->exp_obd, extent_calc)(exp, md, cmd, offset);
1162         RETURN(rc);
1163 }
1164
1165 static inline struct dentry *
1166 obd_lvfs_fid2dentry(struct obd_export *exp, __u64 id_ino, __u32 gen, __u64 gr)
1167 {
1168         LASSERT(exp->exp_obd);
1169
1170         return lvfs_fid2dentry(&exp->exp_obd->obd_lvfs_ctxt, id_ino, gen, gr,
1171                                exp->exp_obd);
1172 }
1173
1174 static inline int
1175 obd_lvfs_open_llog(struct obd_export *exp, __u64 id_ino, struct dentry *dentry)
1176 {
1177         LASSERT(exp->exp_obd);
1178         CERROR("FIXME what's the story here?  This needs to be an obd fn?\n");
1179 #if 0
1180         return lvfs_open_llog(&exp->exp_obd->obd_lvfs_ctxt, id_ino,
1181                               dentry, exp->exp_obd);
1182 #endif
1183         return 0;
1184 }
1185
1186 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1187  * If the cache is older than @max_age we will get a new value from the
1188  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1189 static inline int obd_statfs_async(struct obd_device *obd,
1190                                    struct obd_info *oinfo,
1191                                    __u64 max_age,
1192                                    struct ptlrpc_request_set *rqset)
1193 {
1194         int rc = 0;
1195         ENTRY;
1196
1197         if (obd == NULL)
1198                 RETURN(-EINVAL);
1199
1200         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1201         OBD_COUNTER_INCREMENT(obd, statfs);
1202
1203         CDEBUG(D_SUPER, "%s: osfs %p age "LPU64", max_age "LPU64"\n",
1204                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
1205         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1206                 rc = OBP(obd, statfs_async)(obd, oinfo, max_age, rqset);
1207         } else {
1208                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
1209                        " objects "LPU64"/"LPU64"\n",
1210                        obd->obd_name, &obd->obd_osfs,
1211                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1212                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1213                 cfs_spin_lock(&obd->obd_osfs_lock);
1214                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1215                 cfs_spin_unlock(&obd->obd_osfs_lock);
1216                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1217                 if (oinfo->oi_cb_up)
1218                         oinfo->oi_cb_up(oinfo, 0);
1219         }
1220         RETURN(rc);
1221 }
1222
1223 static inline int obd_statfs_rqset(struct obd_device *obd,
1224                                    struct obd_statfs *osfs, __u64 max_age,
1225                                    __u32 flags)
1226 {
1227         struct ptlrpc_request_set *set = NULL;
1228         struct obd_info oinfo = { { { 0 } } };
1229         int rc = 0;
1230         ENTRY;
1231
1232         set =  ptlrpc_prep_set();
1233         if (set == NULL)
1234                 RETURN(-ENOMEM);
1235
1236         oinfo.oi_osfs = osfs;
1237         oinfo.oi_flags = flags;
1238         rc = obd_statfs_async(obd, &oinfo, max_age, set);
1239         if (rc == 0)
1240                 rc = ptlrpc_set_wait(set);
1241         ptlrpc_set_destroy(set);
1242         RETURN(rc);
1243 }
1244
1245 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1246  * If the cache is older than @max_age we will get a new value from the
1247  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1248 static inline int obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1249                              __u64 max_age, __u32 flags)
1250 {
1251         int rc = 0;
1252         ENTRY;
1253
1254         if (obd == NULL)
1255                 RETURN(-EINVAL);
1256
1257         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1258         OBD_COUNTER_INCREMENT(obd, statfs);
1259
1260         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
1261                obd->obd_osfs_age, max_age);
1262         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1263                 rc = OBP(obd, statfs)(obd, osfs, max_age, flags);
1264                 if (rc == 0) {
1265                         cfs_spin_lock(&obd->obd_osfs_lock);
1266                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1267                         obd->obd_osfs_age = cfs_time_current_64();
1268                         cfs_spin_unlock(&obd->obd_osfs_lock);
1269                 }
1270         } else {
1271                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
1272                        " objects "LPU64"/"LPU64"\n",
1273                        obd->obd_name, &obd->obd_osfs,
1274                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1275                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1276                 cfs_spin_lock(&obd->obd_osfs_lock);
1277                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1278                 cfs_spin_unlock(&obd->obd_osfs_lock);
1279         }
1280         RETURN(rc);
1281 }
1282
1283 static inline int obd_sync_rqset(struct obd_export *exp, struct obd_info *oinfo,
1284                                  obd_size start, obd_size end)
1285 {
1286         struct ptlrpc_request_set *set = NULL;
1287         int rc;
1288         ENTRY;
1289
1290         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1291         EXP_COUNTER_INCREMENT(exp, sync);
1292
1293         set =  ptlrpc_prep_set();
1294         if (set == NULL)
1295                 RETURN(-ENOMEM);
1296
1297         rc = OBP(exp->exp_obd, sync)(exp, oinfo, start, end, set);
1298         if (rc == 0)
1299                 rc = ptlrpc_set_wait(set);
1300         ptlrpc_set_destroy(set);
1301         RETURN(rc);
1302 }
1303
1304 static inline int obd_sync(struct obd_export *exp, struct obd_info *oinfo,
1305                            obd_size start, obd_size end,
1306                            struct ptlrpc_request_set *set)
1307 {
1308         int rc;
1309         ENTRY;
1310
1311         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1312         EXP_COUNTER_INCREMENT(exp, sync);
1313
1314         rc = OBP(exp->exp_obd, sync)(exp, oinfo, start, end, set);
1315         RETURN(rc);
1316 }
1317
1318 static inline int obd_punch_rqset(struct obd_export *exp,
1319                                   struct obd_info *oinfo,
1320                                   struct obd_trans_info *oti)
1321 {
1322         struct ptlrpc_request_set *set = NULL;
1323         int rc;
1324         ENTRY;
1325
1326         EXP_CHECK_DT_OP(exp, punch);
1327         EXP_COUNTER_INCREMENT(exp, punch);
1328
1329         set =  ptlrpc_prep_set();
1330         if (set == NULL)
1331                 RETURN(-ENOMEM);
1332
1333         rc = OBP(exp->exp_obd, punch)(exp, oinfo, oti, set);
1334         if (rc == 0)
1335                 rc = ptlrpc_set_wait(set);
1336         ptlrpc_set_destroy(set);
1337         RETURN(rc);
1338 }
1339
1340 static inline int obd_punch(struct obd_export *exp, struct obd_info *oinfo,
1341                             struct obd_trans_info *oti,
1342                             struct ptlrpc_request_set *rqset)
1343 {
1344         int rc;
1345         ENTRY;
1346
1347         EXP_CHECK_DT_OP(exp, punch);
1348         EXP_COUNTER_INCREMENT(exp, punch);
1349
1350         rc = OBP(exp->exp_obd, punch)(exp, oinfo, oti, rqset);
1351         RETURN(rc);
1352 }
1353
1354 static inline int obd_brw(int cmd, struct obd_export *exp,
1355                           struct obd_info *oinfo, obd_count oa_bufs,
1356                           struct brw_page *pg, struct obd_trans_info *oti)
1357 {
1358         int rc;
1359         ENTRY;
1360
1361         EXP_CHECK_DT_OP(exp, brw);
1362         EXP_COUNTER_INCREMENT(exp, brw);
1363
1364         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
1365                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
1366                        "or OBD_BRW_CHECK\n");
1367                 LBUG();
1368         }
1369
1370         rc = OBP(exp->exp_obd, brw)(cmd, exp, oinfo, oa_bufs, pg, oti);
1371         RETURN(rc);
1372 }
1373
1374 static inline int obd_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
1375                              int objcount, struct obd_ioobj *obj,
1376                              struct niobuf_remote *remote, int *pages,
1377                              struct niobuf_local *local,
1378                              struct obd_trans_info *oti,
1379                              struct lustre_capa *capa)
1380 {
1381         int rc;
1382         ENTRY;
1383
1384         EXP_CHECK_DT_OP(exp, preprw);
1385         EXP_COUNTER_INCREMENT(exp, preprw);
1386
1387         rc = OBP(exp->exp_obd, preprw)(cmd, exp, oa, objcount, obj, remote,
1388                                        pages, local, oti, capa);
1389         RETURN(rc);
1390 }
1391
1392 static inline int obd_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
1393                                int objcount, struct obd_ioobj *obj,
1394                                struct niobuf_remote *rnb, int pages,
1395                                struct niobuf_local *local,
1396                                struct obd_trans_info *oti, int rc)
1397 {
1398         ENTRY;
1399
1400         EXP_CHECK_DT_OP(exp, commitrw);
1401         EXP_COUNTER_INCREMENT(exp, commitrw);
1402
1403         rc = OBP(exp->exp_obd, commitrw)(cmd, exp, oa, objcount, obj,
1404                                          rnb, pages, local, oti, rc);
1405         RETURN(rc);
1406 }
1407
1408 static inline int obd_merge_lvb(struct obd_export *exp,
1409                                 struct lov_stripe_md *lsm,
1410                                 struct ost_lvb *lvb, int kms_only)
1411 {
1412         int rc;
1413         ENTRY;
1414
1415         EXP_CHECK_DT_OP(exp, merge_lvb);
1416         EXP_COUNTER_INCREMENT(exp, merge_lvb);
1417
1418         rc = OBP(exp->exp_obd, merge_lvb)(exp, lsm, lvb, kms_only);
1419         RETURN(rc);
1420 }
1421
1422 static inline int obd_adjust_kms(struct obd_export *exp,
1423                                  struct lov_stripe_md *lsm, obd_off size,
1424                                  int shrink)
1425 {
1426         int rc;
1427         ENTRY;
1428
1429         EXP_CHECK_DT_OP(exp, adjust_kms);
1430         EXP_COUNTER_INCREMENT(exp, adjust_kms);
1431
1432         rc = OBP(exp->exp_obd, adjust_kms)(exp, lsm, size, shrink);
1433         RETURN(rc);
1434 }
1435
1436 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1437                                 int len, void *karg, void *uarg)
1438 {
1439         int rc;
1440         ENTRY;
1441
1442         EXP_CHECK_DT_OP(exp, iocontrol);
1443         EXP_COUNTER_INCREMENT(exp, iocontrol);
1444
1445         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1446         RETURN(rc);
1447 }
1448
1449 static inline int obd_enqueue_rqset(struct obd_export *exp,
1450                                     struct obd_info *oinfo,
1451                                     struct ldlm_enqueue_info *einfo)
1452 {
1453         struct ptlrpc_request_set *set = NULL;
1454         int rc;
1455         ENTRY;
1456
1457         EXP_CHECK_DT_OP(exp, enqueue);
1458         EXP_COUNTER_INCREMENT(exp, enqueue);
1459
1460         set =  ptlrpc_prep_set();
1461         if (set == NULL)
1462                 RETURN(-ENOMEM);
1463
1464         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1465         if (rc == 0)
1466                 rc = ptlrpc_set_wait(set);
1467         ptlrpc_set_destroy(set);
1468         RETURN(rc);
1469 }
1470
1471 static inline int obd_enqueue(struct obd_export *exp,
1472                               struct obd_info *oinfo,
1473                               struct ldlm_enqueue_info *einfo,
1474                               struct ptlrpc_request_set *set)
1475 {
1476         int rc;
1477         ENTRY;
1478
1479         EXP_CHECK_DT_OP(exp, enqueue);
1480         EXP_COUNTER_INCREMENT(exp, enqueue);
1481
1482         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1483         RETURN(rc);
1484 }
1485
1486 static inline int obd_change_cbdata(struct obd_export *exp,
1487                                     struct lov_stripe_md *lsm,
1488                                     ldlm_iterator_t it, void *data)
1489 {
1490         int rc;
1491         ENTRY;
1492
1493         EXP_CHECK_DT_OP(exp, change_cbdata);
1494         EXP_COUNTER_INCREMENT(exp, change_cbdata);
1495
1496         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1497         RETURN(rc);
1498 }
1499
1500 static inline int obd_find_cbdata(struct obd_export *exp,
1501                                   struct lov_stripe_md *lsm,
1502                                   ldlm_iterator_t it, void *data)
1503 {
1504         int rc;
1505         ENTRY;
1506
1507         EXP_CHECK_DT_OP(exp, find_cbdata);
1508         EXP_COUNTER_INCREMENT(exp, find_cbdata);
1509
1510         rc = OBP(exp->exp_obd, find_cbdata)(exp, lsm, it, data);
1511         RETURN(rc);
1512 }
1513
1514 static inline int obd_cancel(struct obd_export *exp,
1515                              struct lov_stripe_md *ea, __u32 mode,
1516                              struct lustre_handle *lockh)
1517 {
1518         int rc;
1519         ENTRY;
1520
1521         EXP_CHECK_DT_OP(exp, cancel);
1522         EXP_COUNTER_INCREMENT(exp, cancel);
1523
1524         rc = OBP(exp->exp_obd, cancel)(exp, ea, mode, lockh);
1525         RETURN(rc);
1526 }
1527
1528 static inline int obd_cancel_unused(struct obd_export *exp,
1529                                     struct lov_stripe_md *ea,
1530                                     ldlm_cancel_flags_t flags,
1531                                     void *opaque)
1532 {
1533         int rc;
1534         ENTRY;
1535
1536         EXP_CHECK_DT_OP(exp, cancel_unused);
1537         EXP_COUNTER_INCREMENT(exp, cancel_unused);
1538
1539         rc = OBP(exp->exp_obd, cancel_unused)(exp, ea, flags, opaque);
1540         RETURN(rc);
1541 }
1542
1543 static inline int obd_pin(struct obd_export *exp, const struct lu_fid *fid,
1544                           struct obd_capa *oc, struct obd_client_handle *handle,
1545                           int flag)
1546 {
1547         int rc;
1548         ENTRY;
1549
1550         EXP_CHECK_DT_OP(exp, pin);
1551         EXP_COUNTER_INCREMENT(exp, pin);
1552
1553         rc = OBP(exp->exp_obd, pin)(exp, fid, oc, handle, flag);
1554         RETURN(rc);
1555 }
1556
1557 static inline int obd_unpin(struct obd_export *exp,
1558                             struct obd_client_handle *handle, int flag)
1559 {
1560         int rc;
1561         ENTRY;
1562
1563         EXP_CHECK_DT_OP(exp, unpin);
1564         EXP_COUNTER_INCREMENT(exp, unpin);
1565
1566         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
1567         RETURN(rc);
1568 }
1569
1570
1571 static inline void obd_import_event(struct obd_device *obd,
1572                                     struct obd_import *imp,
1573                                     enum obd_import_event event)
1574 {
1575         ENTRY;
1576         if (!obd) {
1577                 CERROR("NULL device\n");
1578                 EXIT;
1579                 return;
1580         }
1581         if (obd->obd_set_up && OBP(obd, import_event)) {
1582                 OBD_COUNTER_INCREMENT(obd, import_event);
1583                 OBP(obd, import_event)(obd, imp, event);
1584         }
1585         EXIT;
1586 }
1587
1588 static inline int obd_llog_connect(struct obd_export *exp,
1589                                    struct llogd_conn_body *body)
1590 {
1591         int rc;
1592         ENTRY;
1593
1594         OBD_CHECK_DT_OP(exp->exp_obd, llog_connect, 0);
1595         EXP_COUNTER_INCREMENT(exp, llog_connect);
1596
1597         rc = OBP(exp->exp_obd, llog_connect)(exp, body);
1598         RETURN(rc);
1599 }
1600
1601
1602 static inline int obd_notify(struct obd_device *obd,
1603                              struct obd_device *watched,
1604                              enum obd_notify_event ev,
1605                              void *data)
1606 {
1607         int rc;
1608         ENTRY;
1609         OBD_CHECK_DEV(obd);
1610
1611         /* the check for async_recov is a complete hack - I'm hereby
1612            overloading the meaning to also mean "this was called from
1613            mds_postsetup".  I know that my mds is able to handle notifies
1614            by this point, and it needs to get them to execute mds_postrecov. */
1615         if (!obd->obd_set_up && !obd->obd_async_recov) {
1616                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1617                 RETURN(-EINVAL);
1618         }
1619
1620         if (!OBP(obd, notify)) {
1621                 CERROR("obd %s has no notify handler\n", obd->obd_name);
1622                 RETURN(-ENOSYS);
1623         }
1624
1625         OBD_COUNTER_INCREMENT(obd, notify);
1626         rc = OBP(obd, notify)(obd, watched, ev, data);
1627         RETURN(rc);
1628 }
1629
1630 static inline int obd_notify_observer(struct obd_device *observer,
1631                                       struct obd_device *observed,
1632                                       enum obd_notify_event ev,
1633                                       void *data)
1634 {
1635         int rc1;
1636         int rc2;
1637
1638         struct obd_notify_upcall *onu;
1639
1640         if (observer->obd_observer)
1641                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1642         else
1643                 rc1 = 0;
1644         /*
1645          * Also, call non-obd listener, if any
1646          */
1647         onu = &observer->obd_upcall;
1648         if (onu->onu_upcall != NULL)
1649                 rc2 = onu->onu_upcall(observer, observed, ev,
1650                                       onu->onu_owner, NULL);
1651         else
1652                 rc2 = 0;
1653
1654         return rc1 ? rc1 : rc2;
1655 }
1656
1657 static inline int obd_quotacheck(struct obd_export *exp,
1658                                  struct obd_quotactl *oqctl)
1659 {
1660         int rc;
1661         ENTRY;
1662
1663         EXP_CHECK_DT_OP(exp, quotacheck);
1664         EXP_COUNTER_INCREMENT(exp, quotacheck);
1665
1666         rc = OBP(exp->exp_obd, quotacheck)(exp->exp_obd, exp, oqctl);
1667         RETURN(rc);
1668 }
1669
1670 static inline int obd_quotactl(struct obd_export *exp,
1671                                struct obd_quotactl *oqctl)
1672 {
1673         int rc;
1674         ENTRY;
1675
1676         EXP_CHECK_DT_OP(exp, quotactl);
1677         EXP_COUNTER_INCREMENT(exp, quotactl);
1678
1679         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1680         RETURN(rc);
1681 }
1682
1683 static inline int obd_quota_adjust_qunit(struct obd_export *exp,
1684                                          struct quota_adjust_qunit *oqaq,
1685                                          struct lustre_quota_ctxt *qctxt,
1686                                          struct ptlrpc_request_set *set)
1687 {
1688 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1689         struct timeval work_start;
1690         struct timeval work_end;
1691         long timediff;
1692 #endif
1693         int rc;
1694         ENTRY;
1695
1696 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1697         if (qctxt)
1698                 cfs_gettimeofday(&work_start);
1699 #endif
1700         EXP_CHECK_DT_OP(exp, quota_adjust_qunit);
1701         EXP_COUNTER_INCREMENT(exp, quota_adjust_qunit);
1702
1703         rc = OBP(exp->exp_obd, quota_adjust_qunit)(exp, oqaq, qctxt, set);
1704
1705 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1706         if (qctxt) {
1707                 cfs_gettimeofday(&work_end);
1708                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1709                 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_ADJUST_QUNIT,
1710                                     timediff);
1711         }
1712 #endif
1713         RETURN(rc);
1714 }
1715
1716 static inline int obd_health_check(struct obd_device *obd)
1717 {
1718         /* returns: 0 on healthy
1719          *         >0 on unhealthy + reason code/flag
1720          *            however the only suppored reason == 1 right now
1721          *            We'll need to define some better reasons
1722          *            or flags in the future.
1723          *         <0 on error
1724          */
1725         int rc;
1726         ENTRY;
1727
1728         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1729         if (obd == NULL || !OBT(obd)) {
1730                 CERROR("cleaned up obd\n");
1731                 RETURN(-EOPNOTSUPP);
1732         }
1733         if (!obd->obd_set_up || obd->obd_stopping)
1734                 RETURN(0);
1735         if (!OBP(obd, health_check))
1736                 RETURN(0);
1737
1738         rc = OBP(obd, health_check)(obd);
1739         RETURN(rc);
1740 }
1741
1742 static inline int obd_register_observer(struct obd_device *obd,
1743                                         struct obd_device *observer)
1744 {
1745         ENTRY;
1746         OBD_CHECK_DEV(obd);
1747         cfs_down_write(&obd->obd_observer_link_sem);
1748         if (obd->obd_observer && observer) {
1749                 cfs_up_write(&obd->obd_observer_link_sem);
1750                 RETURN(-EALREADY);
1751         }
1752         obd->obd_observer = observer;
1753         cfs_up_write(&obd->obd_observer_link_sem);
1754         RETURN(0);
1755 }
1756
1757 static inline int obd_pin_observer(struct obd_device *obd,
1758                                    struct obd_device **observer)
1759 {
1760         ENTRY;
1761         cfs_down_read(&obd->obd_observer_link_sem);
1762         if (!obd->obd_observer) {
1763                 *observer = NULL;
1764                 cfs_up_read(&obd->obd_observer_link_sem);
1765                 RETURN(-ENOENT);
1766         }
1767         *observer = obd->obd_observer;
1768         RETURN(0);
1769 }
1770
1771 static inline int obd_unpin_observer(struct obd_device *obd)
1772 {
1773         ENTRY;
1774         cfs_up_read(&obd->obd_observer_link_sem);
1775         RETURN(0);
1776 }
1777
1778 #if 0
1779 static inline int obd_register_page_removal_cb(struct obd_export *exp,
1780                                                obd_page_removal_cb_t cb,
1781                                                obd_pin_extent_cb pin_cb)
1782 {
1783         int rc;
1784         ENTRY;
1785
1786         OBD_CHECK_DT_OP(exp->exp_obd, register_page_removal_cb, 0);
1787         OBD_COUNTER_INCREMENT(exp->exp_obd, register_page_removal_cb);
1788
1789         rc = OBP(exp->exp_obd, register_page_removal_cb)(exp, cb, pin_cb);
1790         RETURN(rc);
1791 }
1792
1793 static inline int obd_unregister_page_removal_cb(struct obd_export *exp,
1794                                                  obd_page_removal_cb_t cb)
1795 {
1796         int rc;
1797         ENTRY;
1798
1799         OBD_CHECK_DT_OP(exp->exp_obd, unregister_page_removal_cb, 0);
1800         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_page_removal_cb);
1801
1802         rc = OBP(exp->exp_obd, unregister_page_removal_cb)(exp, cb);
1803         RETURN(rc);
1804 }
1805
1806 static inline int obd_register_lock_cancel_cb(struct obd_export *exp,
1807                                               obd_lock_cancel_cb cb)
1808 {
1809         int rc;
1810         ENTRY;
1811
1812         OBD_CHECK_DT_OP(exp->exp_obd, register_lock_cancel_cb, 0);
1813         OBD_COUNTER_INCREMENT(exp->exp_obd, register_lock_cancel_cb);
1814
1815         rc = OBP(exp->exp_obd, register_lock_cancel_cb)(exp, cb);
1816         RETURN(rc);
1817 }
1818
1819 static inline int obd_unregister_lock_cancel_cb(struct obd_export *exp,
1820                                                  obd_lock_cancel_cb cb)
1821 {
1822         int rc;
1823         ENTRY;
1824
1825         OBD_CHECK_DT_OP(exp->exp_obd, unregister_lock_cancel_cb, 0);
1826         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_lock_cancel_cb);
1827
1828         rc = OBP(exp->exp_obd, unregister_lock_cancel_cb)(exp, cb);
1829         RETURN(rc);
1830 }
1831 #endif
1832
1833 /* metadata helpers */
1834 static inline int md_getstatus(struct obd_export *exp,
1835                                struct lu_fid *fid, struct obd_capa **pc)
1836 {
1837         int rc;
1838         ENTRY;
1839
1840         EXP_CHECK_MD_OP(exp, getstatus);
1841         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1842         rc = MDP(exp->exp_obd, getstatus)(exp, fid, pc);
1843         RETURN(rc);
1844 }
1845
1846 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1847                              struct ptlrpc_request **request)
1848 {
1849         int rc;
1850         ENTRY;
1851         EXP_CHECK_MD_OP(exp, getattr);
1852         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1853         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1854         RETURN(rc);
1855 }
1856
1857 static inline int md_change_cbdata(struct obd_export *exp,
1858                                    const struct lu_fid *fid,
1859                                    ldlm_iterator_t it, void *data)
1860 {
1861         int rc;
1862         ENTRY;
1863         EXP_CHECK_MD_OP(exp, change_cbdata);
1864         EXP_MD_COUNTER_INCREMENT(exp, change_cbdata);
1865         rc = MDP(exp->exp_obd, change_cbdata)(exp, fid, it, data);
1866         RETURN(rc);
1867 }
1868
1869 static inline int md_find_cbdata(struct obd_export *exp,
1870                                  const struct lu_fid *fid,
1871                                  ldlm_iterator_t it, void *data)
1872 {
1873         int rc;
1874         ENTRY;
1875         EXP_CHECK_MD_OP(exp, find_cbdata);
1876         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1877         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1878         RETURN(rc);
1879 }
1880
1881 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1882                            struct md_open_data *mod,
1883                            struct ptlrpc_request **request)
1884 {
1885         int rc;
1886         ENTRY;
1887         EXP_CHECK_MD_OP(exp, close);
1888         EXP_MD_COUNTER_INCREMENT(exp, close);
1889         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1890         RETURN(rc);
1891 }
1892
1893 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1894                             const void *data, int datalen, int mode, __u32 uid,
1895                             __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1896                             struct ptlrpc_request **request)
1897 {
1898         int rc;
1899         ENTRY;
1900         EXP_CHECK_MD_OP(exp, create);
1901         EXP_MD_COUNTER_INCREMENT(exp, create);
1902         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1903                                        uid, gid, cap_effective, rdev, request);
1904         RETURN(rc);
1905 }
1906
1907 static inline int md_done_writing(struct obd_export *exp,
1908                                   struct md_op_data *op_data,
1909                                   struct md_open_data *mod)
1910 {
1911         int rc;
1912         ENTRY;
1913         EXP_CHECK_MD_OP(exp, done_writing);
1914         EXP_MD_COUNTER_INCREMENT(exp, done_writing);
1915         rc = MDP(exp->exp_obd, done_writing)(exp, op_data, mod);
1916         RETURN(rc);
1917 }
1918
1919 static inline int md_enqueue(struct obd_export *exp,
1920                              struct ldlm_enqueue_info *einfo,
1921                              struct lookup_intent *it,
1922                              struct md_op_data *op_data,
1923                              struct lustre_handle *lockh,
1924                              void *lmm, int lmmsize,
1925                              struct ptlrpc_request **req,
1926                              int extra_lock_flags)
1927 {
1928         int rc;
1929         ENTRY;
1930         EXP_CHECK_MD_OP(exp, enqueue);
1931         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1932         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, it, op_data, lockh,
1933                                         lmm, lmmsize, req, extra_lock_flags);
1934         RETURN(rc);
1935 }
1936
1937 static inline int md_getattr_name(struct obd_export *exp,
1938                                   struct md_op_data *op_data,
1939                                   struct ptlrpc_request **request)
1940 {
1941         int rc;
1942         ENTRY;
1943         EXP_CHECK_MD_OP(exp, getattr_name);
1944         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1945         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1946         RETURN(rc);
1947 }
1948
1949 static inline int md_intent_lock(struct obd_export *exp,
1950                                  struct md_op_data *op_data, void *lmm,
1951                                  int lmmsize, struct lookup_intent *it,
1952                                  int flags, struct ptlrpc_request **reqp,
1953                                  ldlm_blocking_callback cb_blocking,
1954                                  int extra_lock_flags)
1955 {
1956         int rc;
1957         ENTRY;
1958         EXP_CHECK_MD_OP(exp, intent_lock);
1959         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1960         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, lmm, lmmsize,
1961                                             it, flags, reqp, cb_blocking,
1962                                             extra_lock_flags);
1963         RETURN(rc);
1964 }
1965
1966 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1967                           struct ptlrpc_request **request)
1968 {
1969         int rc;
1970         ENTRY;
1971         EXP_CHECK_MD_OP(exp, link);
1972         EXP_MD_COUNTER_INCREMENT(exp, link);
1973         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1974         RETURN(rc);
1975 }
1976
1977 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1978                             const char *old, int oldlen, const char *new,
1979                             int newlen, struct ptlrpc_request **request)
1980 {
1981         int rc;
1982         ENTRY;
1983         EXP_CHECK_MD_OP(exp, rename);
1984         EXP_MD_COUNTER_INCREMENT(exp, rename);
1985         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1986                                        newlen, request);
1987         RETURN(rc);
1988 }
1989
1990 static inline int md_is_subdir(struct obd_export *exp,
1991                                const struct lu_fid *pfid,
1992                                const struct lu_fid *cfid,
1993                                struct ptlrpc_request **request)
1994 {
1995         int rc;
1996         ENTRY;
1997         EXP_CHECK_MD_OP(exp, is_subdir);
1998         EXP_MD_COUNTER_INCREMENT(exp, is_subdir);
1999         rc = MDP(exp->exp_obd, is_subdir)(exp, pfid, cfid, request);
2000         RETURN(rc);
2001 }
2002
2003 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
2004                              void *ea, int ealen, void *ea2, int ea2len,
2005                              struct ptlrpc_request **request,
2006                              struct md_open_data **mod)
2007 {
2008         int rc;
2009         ENTRY;
2010         EXP_CHECK_MD_OP(exp, setattr);
2011         EXP_MD_COUNTER_INCREMENT(exp, setattr);
2012         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen,
2013                                         ea2, ea2len, request, mod);
2014         RETURN(rc);
2015 }
2016
2017 static inline int md_sync(struct obd_export *exp, const struct lu_fid *fid,
2018                           struct obd_capa *oc, struct ptlrpc_request **request)
2019 {
2020         int rc;
2021         ENTRY;
2022         EXP_CHECK_MD_OP(exp, sync);
2023         EXP_MD_COUNTER_INCREMENT(exp, sync);
2024         rc = MDP(exp->exp_obd, sync)(exp, fid, oc, request);
2025         RETURN(rc);
2026 }
2027
2028 static inline int md_readpage(struct obd_export *exp, const struct lu_fid *fid,
2029                               struct obd_capa *oc, __u64 offset,
2030                               struct page **pages, unsigned npages,
2031                               struct ptlrpc_request **request)
2032 {
2033         int rc;
2034         ENTRY;
2035         EXP_CHECK_MD_OP(exp, readpage);
2036         EXP_MD_COUNTER_INCREMENT(exp, readpage);
2037         rc = MDP(exp->exp_obd, readpage)(exp, fid, oc, offset, pages, npages,
2038                                          request);
2039         RETURN(rc);
2040 }
2041
2042 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
2043                             struct ptlrpc_request **request)
2044 {
2045         int rc;
2046         ENTRY;
2047         EXP_CHECK_MD_OP(exp, unlink);
2048         EXP_MD_COUNTER_INCREMENT(exp, unlink);
2049         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
2050         RETURN(rc);
2051 }
2052
2053 static inline int md_get_lustre_md(struct obd_export *exp,
2054                                    struct ptlrpc_request *req,
2055                                    struct obd_export *dt_exp,
2056                                    struct obd_export *md_exp,
2057                                    struct lustre_md *md)
2058 {
2059         ENTRY;
2060         EXP_CHECK_MD_OP(exp, get_lustre_md);
2061         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
2062         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
2063 }
2064
2065 static inline int md_free_lustre_md(struct obd_export *exp,
2066                                     struct lustre_md *md)
2067 {
2068         ENTRY;
2069         EXP_CHECK_MD_OP(exp, free_lustre_md);
2070         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
2071         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
2072 }
2073
2074 static inline int md_setxattr(struct obd_export *exp,
2075                               const struct lu_fid *fid, struct obd_capa *oc,
2076                               obd_valid valid, const char *name,
2077                               const char *input, int input_size,
2078                               int output_size, int flags, __u32 suppgid,
2079                               struct ptlrpc_request **request)
2080 {
2081         ENTRY;
2082         EXP_CHECK_MD_OP(exp, setxattr);
2083         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
2084         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, oc, valid, name, input,
2085                                            input_size, output_size, flags,
2086                                            suppgid, request));
2087 }
2088
2089 static inline int md_getxattr(struct obd_export *exp,
2090                               const struct lu_fid *fid, struct obd_capa *oc,
2091                               obd_valid valid, const char *name,
2092                               const char *input, int input_size,
2093                               int output_size, int flags,
2094                               struct ptlrpc_request **request)
2095 {
2096         ENTRY;
2097         EXP_CHECK_MD_OP(exp, getxattr);
2098         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
2099         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, oc, valid, name, input,
2100                                            input_size, output_size, flags,
2101                                            request));
2102 }
2103
2104 static inline int md_set_open_replay_data(struct obd_export *exp,
2105                                           struct obd_client_handle *och,
2106                                           struct ptlrpc_request *open_req)
2107 {
2108         ENTRY;
2109         EXP_CHECK_MD_OP(exp, set_open_replay_data);
2110         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
2111         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, open_req));
2112 }
2113
2114 static inline int md_clear_open_replay_data(struct obd_export *exp,
2115                                             struct obd_client_handle *och)
2116 {
2117         ENTRY;
2118         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
2119         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
2120         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
2121 }
2122
2123 static inline int md_set_lock_data(struct obd_export *exp,
2124                                    __u64 *lockh, void *data, __u32 *bits)
2125 {
2126         ENTRY;
2127         EXP_CHECK_MD_OP(exp, set_lock_data);
2128         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
2129         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
2130 }
2131
2132 static inline int md_cancel_unused(struct obd_export *exp,
2133                                    const struct lu_fid *fid,
2134                                    ldlm_policy_data_t *policy,
2135                                    ldlm_mode_t mode,
2136                                    ldlm_cancel_flags_t flags,
2137                                    void *opaque)
2138 {
2139         int rc;
2140         ENTRY;
2141
2142         EXP_CHECK_MD_OP(exp, cancel_unused);
2143         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
2144
2145         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
2146                                               flags, opaque);
2147         RETURN(rc);
2148 }
2149
2150 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, int flags,
2151                                         const struct lu_fid *fid,
2152                                         ldlm_type_t type,
2153                                         ldlm_policy_data_t *policy,
2154                                         ldlm_mode_t mode,
2155                                         struct lustre_handle *lockh)
2156 {
2157         ENTRY;
2158         EXP_CHECK_MD_OP(exp, lock_match);
2159         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
2160         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
2161                                              policy, mode, lockh));
2162 }
2163
2164 static inline int md_init_ea_size(struct obd_export *exp, int easize,
2165                                   int def_asize, int cookiesize)
2166 {
2167         ENTRY;
2168         EXP_CHECK_MD_OP(exp, init_ea_size);
2169         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
2170         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, easize, def_asize,
2171                                                cookiesize));
2172 }
2173
2174 static inline int md_get_remote_perm(struct obd_export *exp,
2175                                      const struct lu_fid *fid,
2176                                      struct obd_capa *oc, __u32 suppgid,
2177                                      struct ptlrpc_request **request)
2178 {
2179         ENTRY;
2180         EXP_CHECK_MD_OP(exp, get_remote_perm);
2181         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
2182         RETURN(MDP(exp->exp_obd, get_remote_perm)(exp, fid, oc, suppgid,
2183                                                   request));
2184 }
2185
2186 static inline int md_renew_capa(struct obd_export *exp, struct obd_capa *ocapa,
2187                                 renew_capa_cb_t cb)
2188 {
2189         int rc;
2190         ENTRY;
2191         EXP_CHECK_MD_OP(exp, renew_capa);
2192         EXP_MD_COUNTER_INCREMENT(exp, renew_capa);
2193         rc = MDP(exp->exp_obd, renew_capa)(exp, ocapa, cb);
2194         RETURN(rc);
2195 }
2196
2197 static inline int md_unpack_capa(struct obd_export *exp,
2198                                  struct ptlrpc_request *req,
2199                                  const struct req_msg_field *field,
2200                                  struct obd_capa **oc)
2201 {
2202         int rc;
2203         ENTRY;
2204         EXP_CHECK_MD_OP(exp, unpack_capa);
2205         EXP_MD_COUNTER_INCREMENT(exp, unpack_capa);
2206         rc = MDP(exp->exp_obd, unpack_capa)(exp, req, field, oc);
2207         RETURN(rc);
2208 }
2209
2210 static inline int md_intent_getattr_async(struct obd_export *exp,
2211                                           struct md_enqueue_info *minfo,
2212                                           struct ldlm_enqueue_info *einfo)
2213 {
2214         int rc;
2215         ENTRY;
2216         EXP_CHECK_MD_OP(exp, intent_getattr_async);
2217         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
2218         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
2219         RETURN(rc);
2220 }
2221
2222 static inline int md_revalidate_lock(struct obd_export *exp,
2223                                      struct lookup_intent *it,
2224                                      struct lu_fid *fid)
2225 {
2226         int rc;
2227         ENTRY;
2228         EXP_CHECK_MD_OP(exp, revalidate_lock);
2229         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
2230         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid);
2231         RETURN(rc);
2232 }
2233
2234
2235 /* OBD Metadata Support */
2236
2237 extern int obd_init_caches(void);
2238 extern void obd_cleanup_caches(void);
2239
2240 /* support routines */
2241 extern cfs_mem_cache_t *obdo_cachep;
2242
2243 #define OBDO_ALLOC(ptr)                                                       \
2244 do {                                                                          \
2245         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, CFS_ALLOC_IO);             \
2246 } while(0)
2247
2248 #define OBDO_FREE(ptr)                                                        \
2249 do {                                                                          \
2250         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
2251 } while(0)
2252
2253
2254 static inline void obdo2fid(struct obdo *oa, struct lu_fid *fid)
2255 {
2256         /* something here */
2257 }
2258
2259 static inline void fid2obdo(struct lu_fid *fid, struct obdo *oa)
2260 {
2261         /* something here */
2262 }
2263
2264 /* I'm as embarrassed about this as you are.
2265  *
2266  * <shaver> // XXX do not look into _superhack with remaining eye
2267  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
2268 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
2269
2270 /* sysctl.c */
2271 extern void obd_sysctl_init (void);
2272 extern void obd_sysctl_clean (void);
2273
2274 /* uuid.c  */
2275 typedef __u8 class_uuid_t[16];
2276 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
2277
2278 /* lustre_peer.c    */
2279 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
2280 int class_add_uuid(const char *uuid, __u64 nid);
2281 int class_del_uuid (const char *uuid);
2282 void class_init_uuidlist(void);
2283 void class_exit_uuidlist(void);
2284
2285 /* mea.c */
2286 int mea_name2idx(struct lmv_stripe_md *mea, const char *name, int namelen);
2287 int raw_name2idx(int hashtype, int count, const char *name, int namelen);
2288
2289 /* prng.c */
2290 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
2291
2292 #endif /* __LINUX_OBD_CLASS_H */