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