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