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