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