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