Whamcloud - gitweb
63d409575254a7dc75f911932221052bc7a369c3
[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(struct obd_export *exp, struct obdo *oa,
1256                            struct lov_stripe_md *ea, obd_size start,
1257                            obd_size end, void *capa)
1258 {
1259         int rc;
1260         ENTRY;
1261
1262         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1263         EXP_COUNTER_INCREMENT(exp, sync);
1264
1265         rc = OBP(exp->exp_obd, sync)(exp, oa, ea, start, end, capa);
1266         RETURN(rc);
1267 }
1268
1269 static inline int obd_sync_fs(struct obd_export *exp, struct obd_info *oinfo,
1270                               int wait)
1271 {
1272         int rc;
1273         ENTRY;
1274
1275         OBD_CHECK_DT_OP(exp->exp_obd, sync_fs, -EOPNOTSUPP);
1276         EXP_COUNTER_INCREMENT(exp, sync);
1277
1278         rc = OBP(exp->exp_obd, sync_fs)(exp, oinfo, wait);
1279
1280         RETURN(rc);
1281 }
1282
1283 static inline int obd_punch_rqset(struct obd_export *exp,
1284                                   struct obd_info *oinfo,
1285                                   struct obd_trans_info *oti)
1286 {
1287         struct ptlrpc_request_set *set = NULL;
1288         int rc;
1289         ENTRY;
1290
1291         EXP_CHECK_DT_OP(exp, punch);
1292         EXP_COUNTER_INCREMENT(exp, punch);
1293
1294         set =  ptlrpc_prep_set();
1295         if (set == NULL)
1296                 RETURN(-ENOMEM);
1297
1298         rc = OBP(exp->exp_obd, punch)(exp, oinfo, oti, set);
1299         if (rc == 0)
1300                 rc = ptlrpc_set_wait(set);
1301         ptlrpc_set_destroy(set);
1302         RETURN(rc);
1303 }
1304
1305 static inline int obd_punch(struct obd_export *exp, struct obd_info *oinfo,
1306                             struct obd_trans_info *oti,
1307                             struct ptlrpc_request_set *rqset)
1308 {
1309         int rc;
1310         ENTRY;
1311
1312         EXP_CHECK_DT_OP(exp, punch);
1313         EXP_COUNTER_INCREMENT(exp, punch);
1314
1315         rc = OBP(exp->exp_obd, punch)(exp, oinfo, oti, rqset);
1316         RETURN(rc);
1317 }
1318
1319 static inline int obd_brw(int cmd, struct obd_export *exp,
1320                           struct obd_info *oinfo, obd_count oa_bufs,
1321                           struct brw_page *pg, struct obd_trans_info *oti)
1322 {
1323         int rc;
1324         ENTRY;
1325
1326         EXP_CHECK_DT_OP(exp, brw);
1327         EXP_COUNTER_INCREMENT(exp, brw);
1328
1329         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
1330                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
1331                        "or OBD_BRW_CHECK\n");
1332                 LBUG();
1333         }
1334
1335         rc = OBP(exp->exp_obd, brw)(cmd, exp, oinfo, oa_bufs, pg, oti);
1336         RETURN(rc);
1337 }
1338
1339 static inline int obd_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
1340                              int objcount, struct obd_ioobj *obj,
1341                              struct niobuf_remote *remote, int *pages,
1342                              struct niobuf_local *local,
1343                              struct obd_trans_info *oti,
1344                              struct lustre_capa *capa)
1345 {
1346         int rc;
1347         ENTRY;
1348
1349         EXP_CHECK_DT_OP(exp, preprw);
1350         EXP_COUNTER_INCREMENT(exp, preprw);
1351
1352         rc = OBP(exp->exp_obd, preprw)(cmd, exp, oa, objcount, obj, remote,
1353                                        pages, local, oti, capa);
1354         RETURN(rc);
1355 }
1356
1357 static inline int obd_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
1358                                int objcount, struct obd_ioobj *obj,
1359                                struct niobuf_remote *rnb, int pages,
1360                                struct niobuf_local *local,
1361                                struct obd_trans_info *oti, int rc)
1362 {
1363         ENTRY;
1364
1365         EXP_CHECK_DT_OP(exp, commitrw);
1366         EXP_COUNTER_INCREMENT(exp, commitrw);
1367
1368         rc = OBP(exp->exp_obd, commitrw)(cmd, exp, oa, objcount, obj,
1369                                          rnb, pages, local, oti, rc);
1370         RETURN(rc);
1371 }
1372
1373 static inline int obd_merge_lvb(struct obd_export *exp,
1374                                 struct lov_stripe_md *lsm,
1375                                 struct ost_lvb *lvb, int kms_only)
1376 {
1377         int rc;
1378         ENTRY;
1379
1380         EXP_CHECK_DT_OP(exp, merge_lvb);
1381         EXP_COUNTER_INCREMENT(exp, merge_lvb);
1382
1383         rc = OBP(exp->exp_obd, merge_lvb)(exp, lsm, lvb, kms_only);
1384         RETURN(rc);
1385 }
1386
1387 static inline int obd_adjust_kms(struct obd_export *exp,
1388                                  struct lov_stripe_md *lsm, obd_off size,
1389                                  int shrink)
1390 {
1391         int rc;
1392         ENTRY;
1393
1394         EXP_CHECK_DT_OP(exp, adjust_kms);
1395         EXP_COUNTER_INCREMENT(exp, adjust_kms);
1396
1397         rc = OBP(exp->exp_obd, adjust_kms)(exp, lsm, size, shrink);
1398         RETURN(rc);
1399 }
1400
1401 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1402                                 int len, void *karg, void *uarg)
1403 {
1404         int rc;
1405         ENTRY;
1406
1407         EXP_CHECK_DT_OP(exp, iocontrol);
1408         EXP_COUNTER_INCREMENT(exp, iocontrol);
1409
1410         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1411         RETURN(rc);
1412 }
1413
1414 static inline int obd_enqueue_rqset(struct obd_export *exp,
1415                                     struct obd_info *oinfo,
1416                                     struct ldlm_enqueue_info *einfo)
1417 {
1418         struct ptlrpc_request_set *set = NULL;
1419         int rc;
1420         ENTRY;
1421
1422         EXP_CHECK_DT_OP(exp, enqueue);
1423         EXP_COUNTER_INCREMENT(exp, enqueue);
1424
1425         set =  ptlrpc_prep_set();
1426         if (set == NULL)
1427                 RETURN(-ENOMEM);
1428
1429         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1430         if (rc == 0)
1431                 rc = ptlrpc_set_wait(set);
1432         ptlrpc_set_destroy(set);
1433         RETURN(rc);
1434 }
1435
1436 static inline int obd_enqueue(struct obd_export *exp,
1437                               struct obd_info *oinfo,
1438                               struct ldlm_enqueue_info *einfo,
1439                               struct ptlrpc_request_set *set)
1440 {
1441         int rc;
1442         ENTRY;
1443
1444         EXP_CHECK_DT_OP(exp, enqueue);
1445         EXP_COUNTER_INCREMENT(exp, enqueue);
1446
1447         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1448         RETURN(rc);
1449 }
1450
1451 static inline int obd_change_cbdata(struct obd_export *exp,
1452                                     struct lov_stripe_md *lsm,
1453                                     ldlm_iterator_t it, void *data)
1454 {
1455         int rc;
1456         ENTRY;
1457
1458         EXP_CHECK_DT_OP(exp, change_cbdata);
1459         EXP_COUNTER_INCREMENT(exp, change_cbdata);
1460
1461         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1462         RETURN(rc);
1463 }
1464
1465 static inline int obd_find_cbdata(struct obd_export *exp,
1466                                   struct lov_stripe_md *lsm,
1467                                   ldlm_iterator_t it, void *data)
1468 {
1469         int rc;
1470         ENTRY;
1471
1472         EXP_CHECK_DT_OP(exp, find_cbdata);
1473         EXP_COUNTER_INCREMENT(exp, find_cbdata);
1474
1475         rc = OBP(exp->exp_obd, find_cbdata)(exp, lsm, it, data);
1476         RETURN(rc);
1477 }
1478
1479 static inline int obd_cancel(struct obd_export *exp,
1480                              struct lov_stripe_md *ea, __u32 mode,
1481                              struct lustre_handle *lockh)
1482 {
1483         int rc;
1484         ENTRY;
1485
1486         EXP_CHECK_DT_OP(exp, cancel);
1487         EXP_COUNTER_INCREMENT(exp, cancel);
1488
1489         rc = OBP(exp->exp_obd, cancel)(exp, ea, mode, lockh);
1490         RETURN(rc);
1491 }
1492
1493 static inline int obd_cancel_unused(struct obd_export *exp,
1494                                     struct lov_stripe_md *ea,
1495                                     ldlm_cancel_flags_t flags,
1496                                     void *opaque)
1497 {
1498         int rc;
1499         ENTRY;
1500
1501         EXP_CHECK_DT_OP(exp, cancel_unused);
1502         EXP_COUNTER_INCREMENT(exp, cancel_unused);
1503
1504         rc = OBP(exp->exp_obd, cancel_unused)(exp, ea, flags, opaque);
1505         RETURN(rc);
1506 }
1507
1508 static inline int obd_pin(struct obd_export *exp, const struct lu_fid *fid,
1509                           struct obd_capa *oc, struct obd_client_handle *handle,
1510                           int flag)
1511 {
1512         int rc;
1513         ENTRY;
1514
1515         EXP_CHECK_DT_OP(exp, pin);
1516         EXP_COUNTER_INCREMENT(exp, pin);
1517
1518         rc = OBP(exp->exp_obd, pin)(exp, fid, oc, handle, flag);
1519         RETURN(rc);
1520 }
1521
1522 static inline int obd_unpin(struct obd_export *exp,
1523                             struct obd_client_handle *handle, int flag)
1524 {
1525         int rc;
1526         ENTRY;
1527
1528         EXP_CHECK_DT_OP(exp, unpin);
1529         EXP_COUNTER_INCREMENT(exp, unpin);
1530
1531         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
1532         RETURN(rc);
1533 }
1534
1535
1536 static inline void obd_import_event(struct obd_device *obd,
1537                                     struct obd_import *imp,
1538                                     enum obd_import_event event)
1539 {
1540         ENTRY;
1541         if (!obd) {
1542                 CERROR("NULL device\n");
1543                 EXIT;
1544                 return;
1545         }
1546         if (obd->obd_set_up && OBP(obd, import_event)) {
1547                 OBD_COUNTER_INCREMENT(obd, import_event);
1548                 OBP(obd, import_event)(obd, imp, event);
1549         }
1550         EXIT;
1551 }
1552
1553 static inline int obd_llog_connect(struct obd_export *exp,
1554                                    struct llogd_conn_body *body)
1555 {
1556         int rc;
1557         ENTRY;
1558
1559         OBD_CHECK_DT_OP(exp->exp_obd, llog_connect, 0);
1560         EXP_COUNTER_INCREMENT(exp, llog_connect);
1561
1562         rc = OBP(exp->exp_obd, llog_connect)(exp, body);
1563         RETURN(rc);
1564 }
1565
1566
1567 static inline int obd_notify(struct obd_device *obd,
1568                              struct obd_device *watched,
1569                              enum obd_notify_event ev,
1570                              void *data)
1571 {
1572         int rc;
1573         ENTRY;
1574         OBD_CHECK_DEV(obd);
1575
1576         /* the check for async_recov is a complete hack - I'm hereby
1577            overloading the meaning to also mean "this was called from
1578            mds_postsetup".  I know that my mds is able to handle notifies
1579            by this point, and it needs to get them to execute mds_postrecov. */
1580         if (!obd->obd_set_up && !obd->obd_async_recov) {
1581                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1582                 RETURN(-EINVAL);
1583         }
1584
1585         if (!OBP(obd, notify)) {
1586                 CERROR("obd %s has no notify handler\n", obd->obd_name);
1587                 RETURN(-ENOSYS);
1588         }
1589
1590         OBD_COUNTER_INCREMENT(obd, notify);
1591         rc = OBP(obd, notify)(obd, watched, ev, data);
1592         RETURN(rc);
1593 }
1594
1595 static inline int obd_notify_observer(struct obd_device *observer,
1596                                       struct obd_device *observed,
1597                                       enum obd_notify_event ev,
1598                                       void *data)
1599 {
1600         int rc1;
1601         int rc2;
1602
1603         struct obd_notify_upcall *onu;
1604
1605         if (observer->obd_observer)
1606                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1607         else
1608                 rc1 = 0;
1609         /*
1610          * Also, call non-obd listener, if any
1611          */
1612         onu = &observer->obd_upcall;
1613         if (onu->onu_upcall != NULL)
1614                 rc2 = onu->onu_upcall(observer, observed, ev,
1615                                       onu->onu_owner, NULL);
1616         else
1617                 rc2 = 0;
1618
1619         return rc1 ? rc1 : rc2;
1620 }
1621
1622 static inline int obd_quotacheck(struct obd_export *exp,
1623                                  struct obd_quotactl *oqctl)
1624 {
1625         int rc;
1626         ENTRY;
1627
1628         EXP_CHECK_DT_OP(exp, quotacheck);
1629         EXP_COUNTER_INCREMENT(exp, quotacheck);
1630
1631         rc = OBP(exp->exp_obd, quotacheck)(exp->exp_obd, exp, oqctl);
1632         RETURN(rc);
1633 }
1634
1635 static inline int obd_quotactl(struct obd_export *exp,
1636                                struct obd_quotactl *oqctl)
1637 {
1638         int rc;
1639         ENTRY;
1640
1641         EXP_CHECK_DT_OP(exp, quotactl);
1642         EXP_COUNTER_INCREMENT(exp, quotactl);
1643
1644         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1645         RETURN(rc);
1646 }
1647
1648 static inline int obd_quota_adjust_qunit(struct obd_export *exp,
1649                                          struct quota_adjust_qunit *oqaq,
1650                                          struct lustre_quota_ctxt *qctxt,
1651                                          struct ptlrpc_request_set *set)
1652 {
1653 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1654         struct timeval work_start;
1655         struct timeval work_end;
1656         long timediff;
1657 #endif
1658         int rc;
1659         ENTRY;
1660
1661 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1662         if (qctxt)
1663                 cfs_gettimeofday(&work_start);
1664 #endif
1665         EXP_CHECK_DT_OP(exp, quota_adjust_qunit);
1666         EXP_COUNTER_INCREMENT(exp, quota_adjust_qunit);
1667
1668         rc = OBP(exp->exp_obd, quota_adjust_qunit)(exp, oqaq, qctxt, set);
1669
1670 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1671         if (qctxt) {
1672                 cfs_gettimeofday(&work_end);
1673                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1674                 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_ADJUST_QUNIT,
1675                                     timediff);
1676         }
1677 #endif
1678         RETURN(rc);
1679 }
1680
1681 static inline int obd_health_check(struct obd_device *obd)
1682 {
1683         /* returns: 0 on healthy
1684          *         >0 on unhealthy + reason code/flag
1685          *            however the only suppored reason == 1 right now
1686          *            We'll need to define some better reasons
1687          *            or flags in the future.
1688          *         <0 on error
1689          */
1690         int rc;
1691         ENTRY;
1692
1693         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1694         if (obd == NULL || !OBT(obd)) {
1695                 CERROR("cleaned up obd\n");
1696                 RETURN(-EOPNOTSUPP);
1697         }
1698         if (!obd->obd_set_up || obd->obd_stopping)
1699                 RETURN(0);
1700         if (!OBP(obd, health_check))
1701                 RETURN(0);
1702
1703         rc = OBP(obd, health_check)(obd);
1704         RETURN(rc);
1705 }
1706
1707 static inline int obd_register_observer(struct obd_device *obd,
1708                                         struct obd_device *observer)
1709 {
1710         ENTRY;
1711         OBD_CHECK_DEV(obd);
1712         cfs_down_write(&obd->obd_observer_link_sem);
1713         if (obd->obd_observer && observer) {
1714                 cfs_up_write(&obd->obd_observer_link_sem);
1715                 RETURN(-EALREADY);
1716         }
1717         obd->obd_observer = observer;
1718         cfs_up_write(&obd->obd_observer_link_sem);
1719         RETURN(0);
1720 }
1721
1722 static inline int obd_pin_observer(struct obd_device *obd,
1723                                    struct obd_device **observer)
1724 {
1725         ENTRY;
1726         cfs_down_read(&obd->obd_observer_link_sem);
1727         if (!obd->obd_observer) {
1728                 *observer = NULL;
1729                 cfs_up_read(&obd->obd_observer_link_sem);
1730                 RETURN(-ENOENT);
1731         }
1732         *observer = obd->obd_observer;
1733         RETURN(0);
1734 }
1735
1736 static inline int obd_unpin_observer(struct obd_device *obd)
1737 {
1738         ENTRY;
1739         cfs_up_read(&obd->obd_observer_link_sem);
1740         RETURN(0);
1741 }
1742
1743 #if 0
1744 static inline int obd_register_page_removal_cb(struct obd_export *exp,
1745                                                obd_page_removal_cb_t cb,
1746                                                obd_pin_extent_cb pin_cb)
1747 {
1748         int rc;
1749         ENTRY;
1750
1751         OBD_CHECK_DT_OP(exp->exp_obd, register_page_removal_cb, 0);
1752         OBD_COUNTER_INCREMENT(exp->exp_obd, register_page_removal_cb);
1753
1754         rc = OBP(exp->exp_obd, register_page_removal_cb)(exp, cb, pin_cb);
1755         RETURN(rc);
1756 }
1757
1758 static inline int obd_unregister_page_removal_cb(struct obd_export *exp,
1759                                                  obd_page_removal_cb_t cb)
1760 {
1761         int rc;
1762         ENTRY;
1763
1764         OBD_CHECK_DT_OP(exp->exp_obd, unregister_page_removal_cb, 0);
1765         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_page_removal_cb);
1766
1767         rc = OBP(exp->exp_obd, unregister_page_removal_cb)(exp, cb);
1768         RETURN(rc);
1769 }
1770
1771 static inline int obd_register_lock_cancel_cb(struct obd_export *exp,
1772                                               obd_lock_cancel_cb cb)
1773 {
1774         int rc;
1775         ENTRY;
1776
1777         OBD_CHECK_DT_OP(exp->exp_obd, register_lock_cancel_cb, 0);
1778         OBD_COUNTER_INCREMENT(exp->exp_obd, register_lock_cancel_cb);
1779
1780         rc = OBP(exp->exp_obd, register_lock_cancel_cb)(exp, cb);
1781         RETURN(rc);
1782 }
1783
1784 static inline int obd_unregister_lock_cancel_cb(struct obd_export *exp,
1785                                                  obd_lock_cancel_cb cb)
1786 {
1787         int rc;
1788         ENTRY;
1789
1790         OBD_CHECK_DT_OP(exp->exp_obd, unregister_lock_cancel_cb, 0);
1791         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_lock_cancel_cb);
1792
1793         rc = OBP(exp->exp_obd, unregister_lock_cancel_cb)(exp, cb);
1794         RETURN(rc);
1795 }
1796 #endif
1797
1798 /* metadata helpers */
1799 static inline int md_getstatus(struct obd_export *exp,
1800                                struct lu_fid *fid, struct obd_capa **pc)
1801 {
1802         int rc;
1803         ENTRY;
1804
1805         EXP_CHECK_MD_OP(exp, getstatus);
1806         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1807         rc = MDP(exp->exp_obd, getstatus)(exp, fid, pc);
1808         RETURN(rc);
1809 }
1810
1811 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1812                              struct ptlrpc_request **request)
1813 {
1814         int rc;
1815         ENTRY;
1816         EXP_CHECK_MD_OP(exp, getattr);
1817         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1818         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1819         RETURN(rc);
1820 }
1821
1822 static inline int md_change_cbdata(struct obd_export *exp,
1823                                    const struct lu_fid *fid,
1824                                    ldlm_iterator_t it, void *data)
1825 {
1826         int rc;
1827         ENTRY;
1828         EXP_CHECK_MD_OP(exp, change_cbdata);
1829         EXP_MD_COUNTER_INCREMENT(exp, change_cbdata);
1830         rc = MDP(exp->exp_obd, change_cbdata)(exp, fid, it, data);
1831         RETURN(rc);
1832 }
1833
1834 static inline int md_find_cbdata(struct obd_export *exp,
1835                                  const struct lu_fid *fid,
1836                                  ldlm_iterator_t it, void *data)
1837 {
1838         int rc;
1839         ENTRY;
1840         EXP_CHECK_MD_OP(exp, find_cbdata);
1841         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1842         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1843         RETURN(rc);
1844 }
1845
1846 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1847                            struct md_open_data *mod,
1848                            struct ptlrpc_request **request)
1849 {
1850         int rc;
1851         ENTRY;
1852         EXP_CHECK_MD_OP(exp, close);
1853         EXP_MD_COUNTER_INCREMENT(exp, close);
1854         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1855         RETURN(rc);
1856 }
1857
1858 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1859                             const void *data, int datalen, int mode, __u32 uid,
1860                             __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1861                             struct ptlrpc_request **request)
1862 {
1863         int rc;
1864         ENTRY;
1865         EXP_CHECK_MD_OP(exp, create);
1866         EXP_MD_COUNTER_INCREMENT(exp, create);
1867         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1868                                        uid, gid, cap_effective, rdev, request);
1869         RETURN(rc);
1870 }
1871
1872 static inline int md_done_writing(struct obd_export *exp,
1873                                   struct md_op_data *op_data,
1874                                   struct md_open_data *mod)
1875 {
1876         int rc;
1877         ENTRY;
1878         EXP_CHECK_MD_OP(exp, done_writing);
1879         EXP_MD_COUNTER_INCREMENT(exp, done_writing);
1880         rc = MDP(exp->exp_obd, done_writing)(exp, op_data, mod);
1881         RETURN(rc);
1882 }
1883
1884 static inline int md_enqueue(struct obd_export *exp,
1885                              struct ldlm_enqueue_info *einfo,
1886                              struct lookup_intent *it,
1887                              struct md_op_data *op_data,
1888                              struct lustre_handle *lockh,
1889                              void *lmm, int lmmsize,
1890                              struct ptlrpc_request **req,
1891                              int extra_lock_flags)
1892 {
1893         int rc;
1894         ENTRY;
1895         EXP_CHECK_MD_OP(exp, enqueue);
1896         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1897         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, it, op_data, lockh,
1898                                         lmm, lmmsize, req, extra_lock_flags);
1899         RETURN(rc);
1900 }
1901
1902 static inline int md_getattr_name(struct obd_export *exp,
1903                                   struct md_op_data *op_data,
1904                                   struct ptlrpc_request **request)
1905 {
1906         int rc;
1907         ENTRY;
1908         EXP_CHECK_MD_OP(exp, getattr_name);
1909         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1910         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1911         RETURN(rc);
1912 }
1913
1914 static inline int md_intent_lock(struct obd_export *exp,
1915                                  struct md_op_data *op_data, void *lmm,
1916                                  int lmmsize, struct lookup_intent *it,
1917                                  int flags, struct ptlrpc_request **reqp,
1918                                  ldlm_blocking_callback cb_blocking,
1919                                  int extra_lock_flags)
1920 {
1921         int rc;
1922         ENTRY;
1923         EXP_CHECK_MD_OP(exp, intent_lock);
1924         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1925         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, lmm, lmmsize,
1926                                             it, flags, reqp, cb_blocking,
1927                                             extra_lock_flags);
1928         RETURN(rc);
1929 }
1930
1931 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1932                           struct ptlrpc_request **request)
1933 {
1934         int rc;
1935         ENTRY;
1936         EXP_CHECK_MD_OP(exp, link);
1937         EXP_MD_COUNTER_INCREMENT(exp, link);
1938         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1939         RETURN(rc);
1940 }
1941
1942 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1943                             const char *old, int oldlen, const char *new,
1944                             int newlen, struct ptlrpc_request **request)
1945 {
1946         int rc;
1947         ENTRY;
1948         EXP_CHECK_MD_OP(exp, rename);
1949         EXP_MD_COUNTER_INCREMENT(exp, rename);
1950         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1951                                        newlen, request);
1952         RETURN(rc);
1953 }
1954
1955 static inline int md_is_subdir(struct obd_export *exp,
1956                                const struct lu_fid *pfid,
1957                                const struct lu_fid *cfid,
1958                                struct ptlrpc_request **request)
1959 {
1960         int rc;
1961         ENTRY;
1962         EXP_CHECK_MD_OP(exp, is_subdir);
1963         EXP_MD_COUNTER_INCREMENT(exp, is_subdir);
1964         rc = MDP(exp->exp_obd, is_subdir)(exp, pfid, cfid, request);
1965         RETURN(rc);
1966 }
1967
1968 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1969                              void *ea, int ealen, void *ea2, int ea2len,
1970                              struct ptlrpc_request **request,
1971                              struct md_open_data **mod)
1972 {
1973         int rc;
1974         ENTRY;
1975         EXP_CHECK_MD_OP(exp, setattr);
1976         EXP_MD_COUNTER_INCREMENT(exp, setattr);
1977         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen,
1978                                         ea2, ea2len, request, mod);
1979         RETURN(rc);
1980 }
1981
1982 static inline int md_sync(struct obd_export *exp, const struct lu_fid *fid,
1983                           struct obd_capa *oc, struct ptlrpc_request **request)
1984 {
1985         int rc;
1986         ENTRY;
1987         EXP_CHECK_MD_OP(exp, sync);
1988         EXP_MD_COUNTER_INCREMENT(exp, sync);
1989         rc = MDP(exp->exp_obd, sync)(exp, fid, oc, request);
1990         RETURN(rc);
1991 }
1992
1993 static inline int md_readpage(struct obd_export *exp, const struct lu_fid *fid,
1994                               struct obd_capa *oc, __u64 offset,
1995                               struct page *page,
1996                               struct ptlrpc_request **request)
1997 {
1998         int rc;
1999         ENTRY;
2000         EXP_CHECK_MD_OP(exp, readpage);
2001         EXP_MD_COUNTER_INCREMENT(exp, readpage);
2002         rc = MDP(exp->exp_obd, readpage)(exp, fid, oc, offset, page, request);
2003         RETURN(rc);
2004 }
2005
2006 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
2007                             struct ptlrpc_request **request)
2008 {
2009         int rc;
2010         ENTRY;
2011         EXP_CHECK_MD_OP(exp, unlink);
2012         EXP_MD_COUNTER_INCREMENT(exp, unlink);
2013         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
2014         RETURN(rc);
2015 }
2016
2017 static inline int md_get_lustre_md(struct obd_export *exp,
2018                                    struct ptlrpc_request *req,
2019                                    struct obd_export *dt_exp,
2020                                    struct obd_export *md_exp,
2021                                    struct lustre_md *md)
2022 {
2023         ENTRY;
2024         EXP_CHECK_MD_OP(exp, get_lustre_md);
2025         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
2026         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
2027 }
2028
2029 static inline int md_free_lustre_md(struct obd_export *exp,
2030                                     struct lustre_md *md)
2031 {
2032         ENTRY;
2033         EXP_CHECK_MD_OP(exp, free_lustre_md);
2034         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
2035         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
2036 }
2037
2038 static inline int md_setxattr(struct obd_export *exp,
2039                               const struct lu_fid *fid, struct obd_capa *oc,
2040                               obd_valid valid, const char *name,
2041                               const char *input, int input_size,
2042                               int output_size, int flags, __u32 suppgid,
2043                               struct ptlrpc_request **request)
2044 {
2045         ENTRY;
2046         EXP_CHECK_MD_OP(exp, setxattr);
2047         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
2048         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, oc, valid, name, input,
2049                                            input_size, output_size, flags,
2050                                            suppgid, request));
2051 }
2052
2053 static inline int md_getxattr(struct obd_export *exp,
2054                               const struct lu_fid *fid, struct obd_capa *oc,
2055                               obd_valid valid, const char *name,
2056                               const char *input, int input_size,
2057                               int output_size, int flags,
2058                               struct ptlrpc_request **request)
2059 {
2060         ENTRY;
2061         EXP_CHECK_MD_OP(exp, getxattr);
2062         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
2063         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, oc, valid, name, input,
2064                                            input_size, output_size, flags,
2065                                            request));
2066 }
2067
2068 static inline int md_set_open_replay_data(struct obd_export *exp,
2069                                           struct obd_client_handle *och,
2070                                           struct ptlrpc_request *open_req)
2071 {
2072         ENTRY;
2073         EXP_CHECK_MD_OP(exp, set_open_replay_data);
2074         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
2075         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, open_req));
2076 }
2077
2078 static inline int md_clear_open_replay_data(struct obd_export *exp,
2079                                             struct obd_client_handle *och)
2080 {
2081         ENTRY;
2082         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
2083         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
2084         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
2085 }
2086
2087 static inline int md_set_lock_data(struct obd_export *exp,
2088                                    __u64 *lockh, void *data, __u32 *bits)
2089 {
2090         ENTRY;
2091         EXP_CHECK_MD_OP(exp, set_lock_data);
2092         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
2093         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
2094 }
2095
2096 static inline int md_cancel_unused(struct obd_export *exp,
2097                                    const struct lu_fid *fid,
2098                                    ldlm_policy_data_t *policy,
2099                                    ldlm_mode_t mode,
2100                                    ldlm_cancel_flags_t flags,
2101                                    void *opaque)
2102 {
2103         int rc;
2104         ENTRY;
2105
2106         EXP_CHECK_MD_OP(exp, cancel_unused);
2107         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
2108
2109         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
2110                                               flags, opaque);
2111         RETURN(rc);
2112 }
2113
2114 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, int flags,
2115                                         const struct lu_fid *fid,
2116                                         ldlm_type_t type,
2117                                         ldlm_policy_data_t *policy,
2118                                         ldlm_mode_t mode,
2119                                         struct lustre_handle *lockh)
2120 {
2121         ENTRY;
2122         EXP_CHECK_MD_OP(exp, lock_match);
2123         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
2124         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
2125                                              policy, mode, lockh));
2126 }
2127
2128 static inline int md_init_ea_size(struct obd_export *exp, int easize,
2129                                   int def_asize, int cookiesize)
2130 {
2131         ENTRY;
2132         EXP_CHECK_MD_OP(exp, init_ea_size);
2133         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
2134         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, easize, def_asize,
2135                                                cookiesize));
2136 }
2137
2138 static inline int md_get_remote_perm(struct obd_export *exp,
2139                                      const struct lu_fid *fid,
2140                                      struct obd_capa *oc, __u32 suppgid,
2141                                      struct ptlrpc_request **request)
2142 {
2143         ENTRY;
2144         EXP_CHECK_MD_OP(exp, get_remote_perm);
2145         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
2146         RETURN(MDP(exp->exp_obd, get_remote_perm)(exp, fid, oc, suppgid,
2147                                                   request));
2148 }
2149
2150 static inline int md_renew_capa(struct obd_export *exp, struct obd_capa *ocapa,
2151                                 renew_capa_cb_t cb)
2152 {
2153         int rc;
2154         ENTRY;
2155         EXP_CHECK_MD_OP(exp, renew_capa);
2156         EXP_MD_COUNTER_INCREMENT(exp, renew_capa);
2157         rc = MDP(exp->exp_obd, renew_capa)(exp, ocapa, cb);
2158         RETURN(rc);
2159 }
2160
2161 static inline int md_unpack_capa(struct obd_export *exp,
2162                                  struct ptlrpc_request *req,
2163                                  const struct req_msg_field *field,
2164                                  struct obd_capa **oc)
2165 {
2166         int rc;
2167         ENTRY;
2168         EXP_CHECK_MD_OP(exp, unpack_capa);
2169         EXP_MD_COUNTER_INCREMENT(exp, unpack_capa);
2170         rc = MDP(exp->exp_obd, unpack_capa)(exp, req, field, oc);
2171         RETURN(rc);
2172 }
2173
2174 static inline int md_intent_getattr_async(struct obd_export *exp,
2175                                           struct md_enqueue_info *minfo,
2176                                           struct ldlm_enqueue_info *einfo)
2177 {
2178         int rc;
2179         ENTRY;
2180         EXP_CHECK_MD_OP(exp, intent_getattr_async);
2181         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
2182         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
2183         RETURN(rc);
2184 }
2185
2186 static inline int md_revalidate_lock(struct obd_export *exp,
2187                                      struct lookup_intent *it,
2188                                      struct lu_fid *fid)
2189 {
2190         int rc;
2191         ENTRY;
2192         EXP_CHECK_MD_OP(exp, revalidate_lock);
2193         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
2194         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid);
2195         RETURN(rc);
2196 }
2197
2198
2199 /* OBD Metadata Support */
2200
2201 extern int obd_init_caches(void);
2202 extern void obd_cleanup_caches(void);
2203
2204 /* support routines */
2205 extern cfs_mem_cache_t *obdo_cachep;
2206
2207 #define OBDO_ALLOC(ptr)                                                       \
2208 do {                                                                          \
2209         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, CFS_ALLOC_IO);             \
2210 } while(0)
2211
2212 #define OBDO_FREE(ptr)                                                        \
2213 do {                                                                          \
2214         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
2215 } while(0)
2216
2217
2218 static inline void obdo2fid(struct obdo *oa, struct lu_fid *fid)
2219 {
2220         /* something here */
2221 }
2222
2223 static inline void fid2obdo(struct lu_fid *fid, struct obdo *oa)
2224 {
2225         /* something here */
2226 }
2227
2228 /* I'm as embarrassed about this as you are.
2229  *
2230  * <shaver> // XXX do not look into _superhack with remaining eye
2231  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
2232 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
2233
2234 /* sysctl.c */
2235 extern void obd_sysctl_init (void);
2236 extern void obd_sysctl_clean (void);
2237
2238 /* uuid.c  */
2239 typedef __u8 class_uuid_t[16];
2240 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
2241
2242 /* lustre_peer.c    */
2243 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
2244 int class_add_uuid(const char *uuid, __u64 nid);
2245 int class_del_uuid (const char *uuid);
2246 void class_init_uuidlist(void);
2247 void class_exit_uuidlist(void);
2248
2249 /* mea.c */
2250 int mea_name2idx(struct lmv_stripe_md *mea, const char *name, int namelen);
2251 int raw_name2idx(int hashtype, int count, const char *name, int namelen);
2252
2253 /* prng.c */
2254 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
2255
2256 #endif /* __LINUX_OBD_CLASS_H */