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