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