Whamcloud - gitweb
LU-2237 tests: new test for re-recreating last_rcvd
[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_ping(const struct lu_env *env, struct obd_export *exp)
1081 {
1082         int rc;
1083         ENTRY;
1084
1085         OBD_CHECK_DT_OP(exp->exp_obd, ping, 0);
1086         EXP_COUNTER_INCREMENT(exp, ping);
1087
1088         rc = OBP(exp->exp_obd, ping)(env, exp);
1089         RETURN(rc);
1090 }
1091
1092 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
1093 {
1094         int rc;
1095         ENTRY;
1096
1097         OBD_CHECK_DT_OP(obd, pool_new, -EOPNOTSUPP);
1098         OBD_COUNTER_INCREMENT(obd, pool_new);
1099
1100         rc = OBP(obd, pool_new)(obd, poolname);
1101         RETURN(rc);
1102 }
1103
1104 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
1105 {
1106         int rc;
1107         ENTRY;
1108
1109         OBD_CHECK_DT_OP(obd, pool_del, -EOPNOTSUPP);
1110         OBD_COUNTER_INCREMENT(obd, pool_del);
1111
1112         rc = OBP(obd, pool_del)(obd, poolname);
1113         RETURN(rc);
1114 }
1115
1116 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
1117 {
1118         int rc;
1119         ENTRY;
1120
1121         OBD_CHECK_DT_OP(obd, pool_add, -EOPNOTSUPP);
1122         OBD_COUNTER_INCREMENT(obd, pool_add);
1123
1124         rc = OBP(obd, pool_add)(obd, poolname, ostname);
1125         RETURN(rc);
1126 }
1127
1128 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
1129 {
1130         int rc;
1131         ENTRY;
1132
1133         OBD_CHECK_DT_OP(obd, pool_rem, -EOPNOTSUPP);
1134         OBD_COUNTER_INCREMENT(obd, pool_rem);
1135
1136         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
1137         RETURN(rc);
1138 }
1139
1140 static inline void obd_getref(struct obd_device *obd)
1141 {
1142         ENTRY;
1143         if (OBT(obd) && OBP(obd, getref)) {
1144                 OBD_COUNTER_INCREMENT(obd, getref);
1145                 OBP(obd, getref)(obd);
1146         }
1147         EXIT;
1148 }
1149
1150 static inline void obd_putref(struct obd_device *obd)
1151 {
1152         ENTRY;
1153         if (OBT(obd) && OBP(obd, putref)) {
1154                 OBD_COUNTER_INCREMENT(obd, putref);
1155                 OBP(obd, putref)(obd);
1156         }
1157         EXIT;
1158 }
1159
1160 static inline int obd_init_export(struct obd_export *exp)
1161 {
1162         int rc = 0;
1163
1164         ENTRY;
1165         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1166             OBP((exp)->exp_obd, init_export))
1167                 rc = OBP(exp->exp_obd, init_export)(exp);
1168         RETURN(rc);
1169 }
1170
1171 static inline int obd_destroy_export(struct obd_export *exp)
1172 {
1173         ENTRY;
1174         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1175             OBP((exp)->exp_obd, destroy_export))
1176                 OBP(exp->exp_obd, destroy_export)(exp);
1177         RETURN(0);
1178 }
1179
1180 static inline int obd_extent_calc(struct obd_export *exp,
1181                                   struct lov_stripe_md *md,
1182                                   int cmd, obd_off *offset)
1183 {
1184         int rc;
1185         ENTRY;
1186         EXP_CHECK_DT_OP(exp, extent_calc);
1187         rc = OBP(exp->exp_obd, extent_calc)(exp, md, cmd, offset);
1188         RETURN(rc);
1189 }
1190
1191 static inline struct dentry *
1192 obd_lvfs_fid2dentry(struct obd_export *exp, __u64 id_ino, __u32 gen, __u64 gr)
1193 {
1194         LASSERT(exp->exp_obd);
1195
1196         return lvfs_fid2dentry(&exp->exp_obd->obd_lvfs_ctxt, id_ino, gen, gr,
1197                                exp->exp_obd);
1198 }
1199
1200 static inline int
1201 obd_lvfs_open_llog(struct obd_export *exp, __u64 id_ino, struct dentry *dentry)
1202 {
1203         LASSERT(exp->exp_obd);
1204         CERROR("FIXME what's the story here?  This needs to be an obd fn?\n");
1205 #if 0
1206         return lvfs_open_llog(&exp->exp_obd->obd_lvfs_ctxt, id_ino,
1207                               dentry, exp->exp_obd);
1208 #endif
1209         return 0;
1210 }
1211
1212 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1213  * If the cache is older than @max_age we will get a new value from the
1214  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1215 static inline int obd_statfs_async(struct obd_export *exp,
1216                                    struct obd_info *oinfo,
1217                                    __u64 max_age,
1218                                    struct ptlrpc_request_set *rqset)
1219 {
1220         int rc = 0;
1221         struct obd_device *obd;
1222         ENTRY;
1223
1224         if (exp == NULL || exp->exp_obd == NULL)
1225                 RETURN(-EINVAL);
1226
1227         obd = exp->exp_obd;
1228         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1229         OBD_COUNTER_INCREMENT(obd, statfs);
1230
1231         CDEBUG(D_SUPER, "%s: osfs %p age "LPU64", max_age "LPU64"\n",
1232                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
1233         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1234                 rc = OBP(obd, statfs_async)(exp, oinfo, max_age, rqset);
1235         } else {
1236                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
1237                        " objects "LPU64"/"LPU64"\n",
1238                        obd->obd_name, &obd->obd_osfs,
1239                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1240                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1241                 spin_lock(&obd->obd_osfs_lock);
1242                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1243                 spin_unlock(&obd->obd_osfs_lock);
1244                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1245                 if (oinfo->oi_cb_up)
1246                         oinfo->oi_cb_up(oinfo, 0);
1247         }
1248         RETURN(rc);
1249 }
1250
1251 static inline int obd_statfs_rqset(struct obd_export *exp,
1252                                    struct obd_statfs *osfs, __u64 max_age,
1253                                    __u32 flags)
1254 {
1255         struct ptlrpc_request_set *set = NULL;
1256         struct obd_info oinfo = { { { 0 } } };
1257         int rc = 0;
1258         ENTRY;
1259
1260         set =  ptlrpc_prep_set();
1261         if (set == NULL)
1262                 RETURN(-ENOMEM);
1263
1264         oinfo.oi_osfs = osfs;
1265         oinfo.oi_flags = flags;
1266         rc = obd_statfs_async(exp, &oinfo, max_age, set);
1267         if (rc == 0)
1268                 rc = ptlrpc_set_wait(set);
1269         ptlrpc_set_destroy(set);
1270         RETURN(rc);
1271 }
1272
1273 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1274  * If the cache is older than @max_age we will get a new value from the
1275  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1276 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1277                              struct obd_statfs *osfs, __u64 max_age,
1278                              __u32 flags)
1279 {
1280         int rc = 0;
1281         struct obd_device *obd = exp->exp_obd;
1282         ENTRY;
1283
1284         if (obd == NULL)
1285                 RETURN(-EINVAL);
1286
1287         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1288         OBD_COUNTER_INCREMENT(obd, statfs);
1289
1290         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
1291                obd->obd_osfs_age, max_age);
1292         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1293                 rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1294                 if (rc == 0) {
1295                         spin_lock(&obd->obd_osfs_lock);
1296                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1297                         obd->obd_osfs_age = cfs_time_current_64();
1298                         spin_unlock(&obd->obd_osfs_lock);
1299                 }
1300         } else {
1301                 CDEBUG(D_SUPER, "%s: use %p cache blocks "LPU64"/"LPU64
1302                        " objects "LPU64"/"LPU64"\n",
1303                        obd->obd_name, &obd->obd_osfs,
1304                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1305                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1306                 spin_lock(&obd->obd_osfs_lock);
1307                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1308                 spin_unlock(&obd->obd_osfs_lock);
1309         }
1310         RETURN(rc);
1311 }
1312
1313 static inline int obd_sync_rqset(struct obd_export *exp, struct obd_info *oinfo,
1314                                  obd_size start, obd_size end)
1315 {
1316         struct ptlrpc_request_set *set = NULL;
1317         int rc;
1318         ENTRY;
1319
1320         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1321         EXP_COUNTER_INCREMENT(exp, sync);
1322
1323         set =  ptlrpc_prep_set();
1324         if (set == NULL)
1325                 RETURN(-ENOMEM);
1326
1327         rc = OBP(exp->exp_obd, sync)(NULL, exp, oinfo, start, end, set);
1328         if (rc == 0)
1329                 rc = ptlrpc_set_wait(set);
1330         ptlrpc_set_destroy(set);
1331         RETURN(rc);
1332 }
1333
1334 static inline int obd_sync(const struct lu_env *env, struct obd_export *exp,
1335                            struct obd_info *oinfo, obd_size start, obd_size end,
1336                            struct ptlrpc_request_set *set)
1337 {
1338         int rc;
1339         ENTRY;
1340
1341         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1342         EXP_COUNTER_INCREMENT(exp, sync);
1343
1344         rc = OBP(exp->exp_obd, sync)(env, exp, oinfo, start, end, set);
1345         RETURN(rc);
1346 }
1347
1348 static inline int obd_punch_rqset(struct obd_export *exp,
1349                                   struct obd_info *oinfo,
1350                                   struct obd_trans_info *oti)
1351 {
1352         struct ptlrpc_request_set *set = NULL;
1353         int rc;
1354         ENTRY;
1355
1356         EXP_CHECK_DT_OP(exp, punch);
1357         EXP_COUNTER_INCREMENT(exp, punch);
1358
1359         set =  ptlrpc_prep_set();
1360         if (set == NULL)
1361                 RETURN(-ENOMEM);
1362
1363         rc = OBP(exp->exp_obd, punch)(NULL, exp, oinfo, oti, set);
1364         if (rc == 0)
1365                 rc = ptlrpc_set_wait(set);
1366         ptlrpc_set_destroy(set);
1367         RETURN(rc);
1368 }
1369
1370 static inline int obd_punch(const struct lu_env *env, struct obd_export *exp,
1371                             struct obd_info *oinfo, struct obd_trans_info *oti,
1372                             struct ptlrpc_request_set *rqset)
1373 {
1374         int rc;
1375         ENTRY;
1376
1377         EXP_CHECK_DT_OP(exp, punch);
1378         EXP_COUNTER_INCREMENT(exp, punch);
1379
1380         rc = OBP(exp->exp_obd, punch)(env, exp, oinfo, oti, rqset);
1381         RETURN(rc);
1382 }
1383
1384 static inline int obd_brw(int cmd, struct obd_export *exp,
1385                           struct obd_info *oinfo, obd_count oa_bufs,
1386                           struct brw_page *pg, struct obd_trans_info *oti)
1387 {
1388         int rc;
1389         ENTRY;
1390
1391         EXP_CHECK_DT_OP(exp, brw);
1392         EXP_COUNTER_INCREMENT(exp, brw);
1393
1394         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
1395                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
1396                        "or OBD_BRW_CHECK\n");
1397                 LBUG();
1398         }
1399
1400         rc = OBP(exp->exp_obd, brw)(cmd, exp, oinfo, oa_bufs, pg, oti);
1401         RETURN(rc);
1402 }
1403
1404 static inline int obd_preprw(const struct lu_env *env, int cmd,
1405                              struct obd_export *exp, struct obdo *oa,
1406                              int objcount, struct obd_ioobj *obj,
1407                              struct niobuf_remote *remote, int *pages,
1408                              struct niobuf_local *local,
1409                              struct obd_trans_info *oti,
1410                              struct lustre_capa *capa)
1411 {
1412         int rc;
1413         ENTRY;
1414
1415         EXP_CHECK_DT_OP(exp, preprw);
1416         EXP_COUNTER_INCREMENT(exp, preprw);
1417
1418         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1419                                        pages, local, oti, capa);
1420         RETURN(rc);
1421 }
1422
1423 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1424                                struct obd_export *exp, struct obdo *oa,
1425                                int objcount, struct obd_ioobj *obj,
1426                                struct niobuf_remote *rnb, int pages,
1427                                struct niobuf_local *local,
1428                                struct obd_trans_info *oti, int rc)
1429 {
1430         ENTRY;
1431
1432         EXP_CHECK_DT_OP(exp, commitrw);
1433         EXP_COUNTER_INCREMENT(exp, commitrw);
1434
1435         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1436                                          rnb, pages, local, oti, rc);
1437         RETURN(rc);
1438 }
1439
1440 static inline int obd_merge_lvb(struct obd_export *exp,
1441                                 struct lov_stripe_md *lsm,
1442                                 struct ost_lvb *lvb, int kms_only)
1443 {
1444         int rc;
1445         ENTRY;
1446
1447         EXP_CHECK_DT_OP(exp, merge_lvb);
1448         EXP_COUNTER_INCREMENT(exp, merge_lvb);
1449
1450         rc = OBP(exp->exp_obd, merge_lvb)(exp, lsm, lvb, kms_only);
1451         RETURN(rc);
1452 }
1453
1454 static inline int obd_adjust_kms(struct obd_export *exp,
1455                                  struct lov_stripe_md *lsm, obd_off size,
1456                                  int shrink)
1457 {
1458         int rc;
1459         ENTRY;
1460
1461         EXP_CHECK_DT_OP(exp, adjust_kms);
1462         EXP_COUNTER_INCREMENT(exp, adjust_kms);
1463
1464         rc = OBP(exp->exp_obd, adjust_kms)(exp, lsm, size, shrink);
1465         RETURN(rc);
1466 }
1467
1468 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1469                                 int len, void *karg, void *uarg)
1470 {
1471         int rc;
1472         ENTRY;
1473
1474         EXP_CHECK_DT_OP(exp, iocontrol);
1475         EXP_COUNTER_INCREMENT(exp, iocontrol);
1476
1477         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1478         RETURN(rc);
1479 }
1480
1481 static inline int obd_enqueue_rqset(struct obd_export *exp,
1482                                     struct obd_info *oinfo,
1483                                     struct ldlm_enqueue_info *einfo)
1484 {
1485         struct ptlrpc_request_set *set = NULL;
1486         int rc;
1487         ENTRY;
1488
1489         EXP_CHECK_DT_OP(exp, enqueue);
1490         EXP_COUNTER_INCREMENT(exp, enqueue);
1491
1492         set =  ptlrpc_prep_set();
1493         if (set == NULL)
1494                 RETURN(-ENOMEM);
1495
1496         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1497         if (rc == 0)
1498                 rc = ptlrpc_set_wait(set);
1499         ptlrpc_set_destroy(set);
1500         RETURN(rc);
1501 }
1502
1503 static inline int obd_enqueue(struct obd_export *exp,
1504                               struct obd_info *oinfo,
1505                               struct ldlm_enqueue_info *einfo,
1506                               struct ptlrpc_request_set *set)
1507 {
1508         int rc;
1509         ENTRY;
1510
1511         EXP_CHECK_DT_OP(exp, enqueue);
1512         EXP_COUNTER_INCREMENT(exp, enqueue);
1513
1514         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1515         RETURN(rc);
1516 }
1517
1518 static inline int obd_change_cbdata(struct obd_export *exp,
1519                                     struct lov_stripe_md *lsm,
1520                                     ldlm_iterator_t it, void *data)
1521 {
1522         int rc;
1523         ENTRY;
1524
1525         EXP_CHECK_DT_OP(exp, change_cbdata);
1526         EXP_COUNTER_INCREMENT(exp, change_cbdata);
1527
1528         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1529         RETURN(rc);
1530 }
1531
1532 static inline int obd_find_cbdata(struct obd_export *exp,
1533                                   struct lov_stripe_md *lsm,
1534                                   ldlm_iterator_t it, void *data)
1535 {
1536         int rc;
1537         ENTRY;
1538
1539         EXP_CHECK_DT_OP(exp, find_cbdata);
1540         EXP_COUNTER_INCREMENT(exp, find_cbdata);
1541
1542         rc = OBP(exp->exp_obd, find_cbdata)(exp, lsm, it, data);
1543         RETURN(rc);
1544 }
1545
1546 static inline int obd_cancel(struct obd_export *exp,
1547                              struct lov_stripe_md *ea, __u32 mode,
1548                              struct lustre_handle *lockh)
1549 {
1550         int rc;
1551         ENTRY;
1552
1553         EXP_CHECK_DT_OP(exp, cancel);
1554         EXP_COUNTER_INCREMENT(exp, cancel);
1555
1556         rc = OBP(exp->exp_obd, cancel)(exp, ea, mode, lockh);
1557         RETURN(rc);
1558 }
1559
1560 static inline int obd_cancel_unused(struct obd_export *exp,
1561                                     struct lov_stripe_md *ea,
1562                                     ldlm_cancel_flags_t flags,
1563                                     void *opaque)
1564 {
1565         int rc;
1566         ENTRY;
1567
1568         EXP_CHECK_DT_OP(exp, cancel_unused);
1569         EXP_COUNTER_INCREMENT(exp, cancel_unused);
1570
1571         rc = OBP(exp->exp_obd, cancel_unused)(exp, ea, flags, opaque);
1572         RETURN(rc);
1573 }
1574
1575 static inline int obd_pin(struct obd_export *exp, const struct lu_fid *fid,
1576                           struct obd_capa *oc, struct obd_client_handle *handle,
1577                           int flag)
1578 {
1579         int rc;
1580         ENTRY;
1581
1582         EXP_CHECK_DT_OP(exp, pin);
1583         EXP_COUNTER_INCREMENT(exp, pin);
1584
1585         rc = OBP(exp->exp_obd, pin)(exp, fid, oc, handle, flag);
1586         RETURN(rc);
1587 }
1588
1589 static inline int obd_unpin(struct obd_export *exp,
1590                             struct obd_client_handle *handle, int flag)
1591 {
1592         int rc;
1593         ENTRY;
1594
1595         EXP_CHECK_DT_OP(exp, unpin);
1596         EXP_COUNTER_INCREMENT(exp, unpin);
1597
1598         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
1599         RETURN(rc);
1600 }
1601
1602
1603 static inline void obd_import_event(struct obd_device *obd,
1604                                     struct obd_import *imp,
1605                                     enum obd_import_event event)
1606 {
1607         ENTRY;
1608         if (!obd) {
1609                 CERROR("NULL device\n");
1610                 EXIT;
1611                 return;
1612         }
1613         if (obd->obd_set_up && OBP(obd, import_event)) {
1614                 OBD_COUNTER_INCREMENT(obd, import_event);
1615                 OBP(obd, import_event)(obd, imp, event);
1616         }
1617         EXIT;
1618 }
1619
1620 static inline int obd_llog_connect(struct obd_export *exp,
1621                                    struct llogd_conn_body *body)
1622 {
1623         int rc;
1624         ENTRY;
1625
1626         OBD_CHECK_DT_OP(exp->exp_obd, llog_connect, 0);
1627         EXP_COUNTER_INCREMENT(exp, llog_connect);
1628
1629         rc = OBP(exp->exp_obd, llog_connect)(exp, body);
1630         RETURN(rc);
1631 }
1632
1633
1634 static inline int obd_notify(struct obd_device *obd,
1635                              struct obd_device *watched,
1636                              enum obd_notify_event ev,
1637                              void *data)
1638 {
1639         int rc;
1640         ENTRY;
1641         OBD_CHECK_DEV(obd);
1642
1643         /* the check for async_recov is a complete hack - I'm hereby
1644            overloading the meaning to also mean "this was called from
1645            mds_postsetup".  I know that my mds is able to handle notifies
1646            by this point, and it needs to get them to execute mds_postrecov. */
1647         if (!obd->obd_set_up && !obd->obd_async_recov) {
1648                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1649                 RETURN(-EINVAL);
1650         }
1651
1652         if (!OBP(obd, notify)) {
1653                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1654                 RETURN(-ENOSYS);
1655         }
1656
1657         OBD_COUNTER_INCREMENT(obd, notify);
1658         rc = OBP(obd, notify)(obd, watched, ev, data);
1659         RETURN(rc);
1660 }
1661
1662 static inline int obd_notify_observer(struct obd_device *observer,
1663                                       struct obd_device *observed,
1664                                       enum obd_notify_event ev,
1665                                       void *data)
1666 {
1667         int rc1;
1668         int rc2;
1669
1670         struct obd_notify_upcall *onu;
1671
1672         if (observer->obd_observer)
1673                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1674         else
1675                 rc1 = 0;
1676         /*
1677          * Also, call non-obd listener, if any
1678          */
1679         onu = &observer->obd_upcall;
1680         if (onu->onu_upcall != NULL)
1681                 rc2 = onu->onu_upcall(observer, observed, ev,
1682                                       onu->onu_owner, NULL);
1683         else
1684                 rc2 = 0;
1685
1686         return rc1 ? rc1 : rc2;
1687 }
1688
1689 static inline int obd_quotacheck(struct obd_export *exp,
1690                                  struct obd_quotactl *oqctl)
1691 {
1692         int rc;
1693         ENTRY;
1694
1695         EXP_CHECK_DT_OP(exp, quotacheck);
1696         EXP_COUNTER_INCREMENT(exp, quotacheck);
1697
1698         rc = OBP(exp->exp_obd, quotacheck)(exp->exp_obd, exp, oqctl);
1699         RETURN(rc);
1700 }
1701
1702 static inline int obd_quotactl(struct obd_export *exp,
1703                                struct obd_quotactl *oqctl)
1704 {
1705         int rc;
1706         ENTRY;
1707
1708         EXP_CHECK_DT_OP(exp, quotactl);
1709         EXP_COUNTER_INCREMENT(exp, quotactl);
1710
1711         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1712         RETURN(rc);
1713 }
1714
1715 static inline int obd_health_check(const struct lu_env *env,
1716                                    struct obd_device *obd)
1717 {
1718         /* returns: 0 on healthy
1719          *         >0 on unhealthy + reason code/flag
1720          *            however the only suppored reason == 1 right now
1721          *            We'll need to define some better reasons
1722          *            or flags in the future.
1723          *         <0 on error
1724          */
1725         int rc;
1726         ENTRY;
1727
1728         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1729         if (obd == NULL || !OBT(obd)) {
1730                 CERROR("cleaned up obd\n");
1731                 RETURN(-EOPNOTSUPP);
1732         }
1733         if (!obd->obd_set_up || obd->obd_stopping)
1734                 RETURN(0);
1735         if (!OBP(obd, health_check))
1736                 RETURN(0);
1737
1738         rc = OBP(obd, health_check)(env, obd);
1739         RETURN(rc);
1740 }
1741
1742 static inline int obd_register_observer(struct obd_device *obd,
1743                                         struct obd_device *observer)
1744 {
1745         ENTRY;
1746         OBD_CHECK_DEV(obd);
1747         down_write(&obd->obd_observer_link_sem);
1748         if (obd->obd_observer && observer) {
1749                 up_write(&obd->obd_observer_link_sem);
1750                 RETURN(-EALREADY);
1751         }
1752         obd->obd_observer = observer;
1753         up_write(&obd->obd_observer_link_sem);
1754         RETURN(0);
1755 }
1756
1757 static inline int obd_pin_observer(struct obd_device *obd,
1758                                    struct obd_device **observer)
1759 {
1760         ENTRY;
1761         down_read(&obd->obd_observer_link_sem);
1762         if (!obd->obd_observer) {
1763                 *observer = NULL;
1764                 up_read(&obd->obd_observer_link_sem);
1765                 RETURN(-ENOENT);
1766         }
1767         *observer = obd->obd_observer;
1768         RETURN(0);
1769 }
1770
1771 static inline int obd_unpin_observer(struct obd_device *obd)
1772 {
1773         ENTRY;
1774         up_read(&obd->obd_observer_link_sem);
1775         RETURN(0);
1776 }
1777
1778 #if 0
1779 static inline int obd_register_page_removal_cb(struct obd_export *exp,
1780                                                obd_page_removal_cb_t cb,
1781                                                obd_pin_extent_cb pin_cb)
1782 {
1783         int rc;
1784         ENTRY;
1785
1786         OBD_CHECK_DT_OP(exp->exp_obd, register_page_removal_cb, 0);
1787         OBD_COUNTER_INCREMENT(exp->exp_obd, register_page_removal_cb);
1788
1789         rc = OBP(exp->exp_obd, register_page_removal_cb)(exp, cb, pin_cb);
1790         RETURN(rc);
1791 }
1792
1793 static inline int obd_unregister_page_removal_cb(struct obd_export *exp,
1794                                                  obd_page_removal_cb_t cb)
1795 {
1796         int rc;
1797         ENTRY;
1798
1799         OBD_CHECK_DT_OP(exp->exp_obd, unregister_page_removal_cb, 0);
1800         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_page_removal_cb);
1801
1802         rc = OBP(exp->exp_obd, unregister_page_removal_cb)(exp, cb);
1803         RETURN(rc);
1804 }
1805
1806 static inline int obd_register_lock_cancel_cb(struct obd_export *exp,
1807                                               obd_lock_cancel_cb cb)
1808 {
1809         int rc;
1810         ENTRY;
1811
1812         OBD_CHECK_DT_OP(exp->exp_obd, register_lock_cancel_cb, 0);
1813         OBD_COUNTER_INCREMENT(exp->exp_obd, register_lock_cancel_cb);
1814
1815         rc = OBP(exp->exp_obd, register_lock_cancel_cb)(exp, cb);
1816         RETURN(rc);
1817 }
1818
1819 static inline int obd_unregister_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, unregister_lock_cancel_cb, 0);
1826         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_lock_cancel_cb);
1827
1828         rc = OBP(exp->exp_obd, unregister_lock_cancel_cb)(exp, cb);
1829         RETURN(rc);
1830 }
1831 #endif
1832
1833 /* metadata helpers */
1834 static inline int md_getstatus(struct obd_export *exp,
1835                                struct lu_fid *fid, struct obd_capa **pc)
1836 {
1837         int rc;
1838         ENTRY;
1839
1840         EXP_CHECK_MD_OP(exp, getstatus);
1841         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1842         rc = MDP(exp->exp_obd, getstatus)(exp, fid, pc);
1843         RETURN(rc);
1844 }
1845
1846 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1847                              struct ptlrpc_request **request)
1848 {
1849         int rc;
1850         ENTRY;
1851         EXP_CHECK_MD_OP(exp, getattr);
1852         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1853         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1854         RETURN(rc);
1855 }
1856
1857 static inline int md_change_cbdata(struct obd_export *exp,
1858                                    const struct lu_fid *fid,
1859                                    ldlm_iterator_t it, void *data)
1860 {
1861         int rc;
1862         ENTRY;
1863         EXP_CHECK_MD_OP(exp, change_cbdata);
1864         EXP_MD_COUNTER_INCREMENT(exp, change_cbdata);
1865         rc = MDP(exp->exp_obd, change_cbdata)(exp, fid, it, data);
1866         RETURN(rc);
1867 }
1868
1869 static inline int md_find_cbdata(struct obd_export *exp,
1870                                  const struct lu_fid *fid,
1871                                  ldlm_iterator_t it, void *data)
1872 {
1873         int rc;
1874         ENTRY;
1875         EXP_CHECK_MD_OP(exp, find_cbdata);
1876         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1877         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1878         RETURN(rc);
1879 }
1880
1881 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1882                            struct md_open_data *mod,
1883                            struct ptlrpc_request **request)
1884 {
1885         int rc;
1886         ENTRY;
1887         EXP_CHECK_MD_OP(exp, close);
1888         EXP_MD_COUNTER_INCREMENT(exp, close);
1889         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1890         RETURN(rc);
1891 }
1892
1893 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1894                             const void *data, int datalen, int mode, __u32 uid,
1895                             __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1896                             struct ptlrpc_request **request)
1897 {
1898         int rc;
1899         ENTRY;
1900         EXP_CHECK_MD_OP(exp, create);
1901         EXP_MD_COUNTER_INCREMENT(exp, create);
1902         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1903                                        uid, gid, cap_effective, rdev, request);
1904         RETURN(rc);
1905 }
1906
1907 static inline int md_done_writing(struct obd_export *exp,
1908                                   struct md_op_data *op_data,
1909                                   struct md_open_data *mod)
1910 {
1911         int rc;
1912         ENTRY;
1913         EXP_CHECK_MD_OP(exp, done_writing);
1914         EXP_MD_COUNTER_INCREMENT(exp, done_writing);
1915         rc = MDP(exp->exp_obd, done_writing)(exp, op_data, mod);
1916         RETURN(rc);
1917 }
1918
1919 static inline int md_enqueue(struct obd_export *exp,
1920                              struct ldlm_enqueue_info *einfo,
1921                              struct lookup_intent *it,
1922                              struct md_op_data *op_data,
1923                              struct lustre_handle *lockh,
1924                              void *lmm, int lmmsize,
1925                              struct ptlrpc_request **req,
1926                              int extra_lock_flags)
1927 {
1928         int rc;
1929         ENTRY;
1930         EXP_CHECK_MD_OP(exp, enqueue);
1931         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1932         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, it, op_data, lockh,
1933                                         lmm, lmmsize, req, extra_lock_flags);
1934         RETURN(rc);
1935 }
1936
1937 static inline int md_getattr_name(struct obd_export *exp,
1938                                   struct md_op_data *op_data,
1939                                   struct ptlrpc_request **request)
1940 {
1941         int rc;
1942         ENTRY;
1943         EXP_CHECK_MD_OP(exp, getattr_name);
1944         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1945         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1946         RETURN(rc);
1947 }
1948
1949 static inline int md_intent_lock(struct obd_export *exp,
1950                                  struct md_op_data *op_data, void *lmm,
1951                                  int lmmsize, struct lookup_intent *it,
1952                                  int lookup_flags, struct ptlrpc_request **reqp,
1953                                  ldlm_blocking_callback cb_blocking,
1954                                  __u64 extra_lock_flags)
1955 {
1956         int rc;
1957         ENTRY;
1958         EXP_CHECK_MD_OP(exp, intent_lock);
1959         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1960         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, lmm, lmmsize,
1961                                             it, lookup_flags, reqp, cb_blocking,
1962                                             extra_lock_flags);
1963         RETURN(rc);
1964 }
1965
1966 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1967                           struct ptlrpc_request **request)
1968 {
1969         int rc;
1970         ENTRY;
1971         EXP_CHECK_MD_OP(exp, link);
1972         EXP_MD_COUNTER_INCREMENT(exp, link);
1973         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1974         RETURN(rc);
1975 }
1976
1977 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1978                             const char *old, int oldlen, const char *new,
1979                             int newlen, struct ptlrpc_request **request)
1980 {
1981         int rc;
1982         ENTRY;
1983         EXP_CHECK_MD_OP(exp, rename);
1984         EXP_MD_COUNTER_INCREMENT(exp, rename);
1985         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1986                                        newlen, request);
1987         RETURN(rc);
1988 }
1989
1990 static inline int md_is_subdir(struct obd_export *exp,
1991                                const struct lu_fid *pfid,
1992                                const struct lu_fid *cfid,
1993                                struct ptlrpc_request **request)
1994 {
1995         int rc;
1996         ENTRY;
1997         EXP_CHECK_MD_OP(exp, is_subdir);
1998         EXP_MD_COUNTER_INCREMENT(exp, is_subdir);
1999         rc = MDP(exp->exp_obd, is_subdir)(exp, pfid, cfid, request);
2000         RETURN(rc);
2001 }
2002
2003 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
2004                              void *ea, int ealen, void *ea2, int ea2len,
2005                              struct ptlrpc_request **request,
2006                              struct md_open_data **mod)
2007 {
2008         int rc;
2009         ENTRY;
2010         EXP_CHECK_MD_OP(exp, setattr);
2011         EXP_MD_COUNTER_INCREMENT(exp, setattr);
2012         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen,
2013                                         ea2, ea2len, request, mod);
2014         RETURN(rc);
2015 }
2016
2017 static inline int md_sync(struct obd_export *exp, const struct lu_fid *fid,
2018                           struct obd_capa *oc, struct ptlrpc_request **request)
2019 {
2020         int rc;
2021         ENTRY;
2022         EXP_CHECK_MD_OP(exp, sync);
2023         EXP_MD_COUNTER_INCREMENT(exp, sync);
2024         rc = MDP(exp->exp_obd, sync)(exp, fid, oc, request);
2025         RETURN(rc);
2026 }
2027
2028 static inline int md_readpage(struct obd_export *exp, struct md_op_data *opdata,
2029                               struct page **pages,
2030                               struct ptlrpc_request **request)
2031 {
2032         int rc;
2033         ENTRY;
2034         EXP_CHECK_MD_OP(exp, readpage);
2035         EXP_MD_COUNTER_INCREMENT(exp, readpage);
2036         rc = MDP(exp->exp_obd, readpage)(exp, opdata, pages, request);
2037         RETURN(rc);
2038 }
2039
2040 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
2041                             struct ptlrpc_request **request)
2042 {
2043         int rc;
2044         ENTRY;
2045         EXP_CHECK_MD_OP(exp, unlink);
2046         EXP_MD_COUNTER_INCREMENT(exp, unlink);
2047         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
2048         RETURN(rc);
2049 }
2050
2051 static inline int md_get_lustre_md(struct obd_export *exp,
2052                                    struct ptlrpc_request *req,
2053                                    struct obd_export *dt_exp,
2054                                    struct obd_export *md_exp,
2055                                    struct lustre_md *md)
2056 {
2057         ENTRY;
2058         EXP_CHECK_MD_OP(exp, get_lustre_md);
2059         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
2060         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
2061 }
2062
2063 static inline int md_free_lustre_md(struct obd_export *exp,
2064                                     struct lustre_md *md)
2065 {
2066         ENTRY;
2067         EXP_CHECK_MD_OP(exp, free_lustre_md);
2068         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
2069         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
2070 }
2071
2072 static inline int md_setxattr(struct obd_export *exp,
2073                               const struct lu_fid *fid, struct obd_capa *oc,
2074                               obd_valid valid, const char *name,
2075                               const char *input, int input_size,
2076                               int output_size, int flags, __u32 suppgid,
2077                               struct ptlrpc_request **request)
2078 {
2079         ENTRY;
2080         EXP_CHECK_MD_OP(exp, setxattr);
2081         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
2082         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, oc, valid, name, input,
2083                                            input_size, output_size, flags,
2084                                            suppgid, request));
2085 }
2086
2087 static inline int md_getxattr(struct obd_export *exp,
2088                               const struct lu_fid *fid, struct obd_capa *oc,
2089                               obd_valid valid, const char *name,
2090                               const char *input, int input_size,
2091                               int output_size, int flags,
2092                               struct ptlrpc_request **request)
2093 {
2094         ENTRY;
2095         EXP_CHECK_MD_OP(exp, getxattr);
2096         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
2097         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, oc, valid, name, input,
2098                                            input_size, output_size, flags,
2099                                            request));
2100 }
2101
2102 static inline int md_set_open_replay_data(struct obd_export *exp,
2103                                           struct obd_client_handle *och,
2104                                           struct ptlrpc_request *open_req)
2105 {
2106         ENTRY;
2107         EXP_CHECK_MD_OP(exp, set_open_replay_data);
2108         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
2109         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, open_req));
2110 }
2111
2112 static inline int md_clear_open_replay_data(struct obd_export *exp,
2113                                             struct obd_client_handle *och)
2114 {
2115         ENTRY;
2116         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
2117         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
2118         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
2119 }
2120
2121 static inline int md_set_lock_data(struct obd_export *exp,
2122                                    __u64 *lockh, void *data, __u64 *bits)
2123 {
2124         ENTRY;
2125         EXP_CHECK_MD_OP(exp, set_lock_data);
2126         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
2127         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
2128 }
2129
2130 static inline int md_cancel_unused(struct obd_export *exp,
2131                                    const struct lu_fid *fid,
2132                                    ldlm_policy_data_t *policy,
2133                                    ldlm_mode_t mode,
2134                                    ldlm_cancel_flags_t flags,
2135                                    void *opaque)
2136 {
2137         int rc;
2138         ENTRY;
2139
2140         EXP_CHECK_MD_OP(exp, cancel_unused);
2141         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
2142
2143         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
2144                                               flags, opaque);
2145         RETURN(rc);
2146 }
2147
2148 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, __u64 flags,
2149                                         const struct lu_fid *fid,
2150                                         ldlm_type_t type,
2151                                         ldlm_policy_data_t *policy,
2152                                         ldlm_mode_t mode,
2153                                         struct lustre_handle *lockh)
2154 {
2155         ENTRY;
2156         EXP_CHECK_MD_OP(exp, lock_match);
2157         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
2158         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
2159                                              policy, mode, lockh));
2160 }
2161
2162 static inline int md_init_ea_size(struct obd_export *exp, int easize,
2163                                   int def_asize, int cookiesize)
2164 {
2165         ENTRY;
2166         EXP_CHECK_MD_OP(exp, init_ea_size);
2167         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
2168         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, easize, def_asize,
2169                                                cookiesize));
2170 }
2171
2172 static inline int md_get_remote_perm(struct obd_export *exp,
2173                                      const struct lu_fid *fid,
2174                                      struct obd_capa *oc, __u32 suppgid,
2175                                      struct ptlrpc_request **request)
2176 {
2177         ENTRY;
2178         EXP_CHECK_MD_OP(exp, get_remote_perm);
2179         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
2180         RETURN(MDP(exp->exp_obd, get_remote_perm)(exp, fid, oc, suppgid,
2181                                                   request));
2182 }
2183
2184 static inline int md_renew_capa(struct obd_export *exp, struct obd_capa *ocapa,
2185                                 renew_capa_cb_t cb)
2186 {
2187         int rc;
2188         ENTRY;
2189         EXP_CHECK_MD_OP(exp, renew_capa);
2190         EXP_MD_COUNTER_INCREMENT(exp, renew_capa);
2191         rc = MDP(exp->exp_obd, renew_capa)(exp, ocapa, cb);
2192         RETURN(rc);
2193 }
2194
2195 static inline int md_unpack_capa(struct obd_export *exp,
2196                                  struct ptlrpc_request *req,
2197                                  const struct req_msg_field *field,
2198                                  struct obd_capa **oc)
2199 {
2200         int rc;
2201         ENTRY;
2202         EXP_CHECK_MD_OP(exp, unpack_capa);
2203         EXP_MD_COUNTER_INCREMENT(exp, unpack_capa);
2204         rc = MDP(exp->exp_obd, unpack_capa)(exp, req, field, oc);
2205         RETURN(rc);
2206 }
2207
2208 static inline int md_intent_getattr_async(struct obd_export *exp,
2209                                           struct md_enqueue_info *minfo,
2210                                           struct ldlm_enqueue_info *einfo)
2211 {
2212         int rc;
2213         ENTRY;
2214         EXP_CHECK_MD_OP(exp, intent_getattr_async);
2215         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
2216         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
2217         RETURN(rc);
2218 }
2219
2220 static inline int md_revalidate_lock(struct obd_export *exp,
2221                                      struct lookup_intent *it,
2222                                      struct lu_fid *fid, __u64 *bits)
2223 {
2224         int rc;
2225         ENTRY;
2226         EXP_CHECK_MD_OP(exp, revalidate_lock);
2227         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
2228         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
2229         RETURN(rc);
2230 }
2231
2232
2233 /* OBD Metadata Support */
2234
2235 extern int obd_init_caches(void);
2236 extern void obd_cleanup_caches(void);
2237
2238 /* support routines */
2239 extern cfs_mem_cache_t *obdo_cachep;
2240
2241 #define OBDO_ALLOC(ptr)                                                       \
2242 do {                                                                          \
2243         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, CFS_ALLOC_IO);             \
2244 } while(0)
2245
2246 #define OBDO_FREE(ptr)                                                        \
2247 do {                                                                          \
2248         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
2249 } while(0)
2250
2251
2252 static inline void obdo2fid(struct obdo *oa, struct lu_fid *fid)
2253 {
2254         /* something here */
2255 }
2256
2257 static inline void fid2obdo(struct lu_fid *fid, struct obdo *oa)
2258 {
2259         /* something here */
2260 }
2261
2262 typedef int (*register_osp_cb)(void *data);
2263
2264 struct osp_register_item {
2265         struct obd_export **ori_exp;
2266         register_osp_cb     ori_cb_func;
2267         void               *ori_cb_data;
2268         cfs_list_t          ori_list;
2269         char                ori_name[MTI_NAME_MAXLEN];
2270 };
2271
2272 /* I'm as embarrassed about this as you are.
2273  *
2274  * <shaver> // XXX do not look into _superhack with remaining eye
2275  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
2276 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
2277
2278 /* obd_mount.c */
2279 int server_name2fsname(char *svname, char *fsname, char **endptr);
2280 int lustre_register_osp_item(char *ospname, struct obd_export **exp,
2281                              register_osp_cb cb_func, void *cb_data);
2282 void lustre_deregister_osp_item(struct obd_export **exp);
2283 int tgt_name2ospname(char *ost_name, char *osp_name);
2284
2285 /* sysctl.c */
2286 extern void obd_sysctl_init (void);
2287 extern void obd_sysctl_clean (void);
2288
2289 /* uuid.c  */
2290 typedef __u8 class_uuid_t[16];
2291 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
2292
2293 /* lustre_peer.c    */
2294 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
2295 int class_add_uuid(const char *uuid, __u64 nid);
2296 int class_del_uuid (const char *uuid);
2297 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
2298 void class_init_uuidlist(void);
2299 void class_exit_uuidlist(void);
2300
2301 /* mea.c */
2302 int mea_name2idx(struct lmv_stripe_md *mea, const char *name, int namelen);
2303 int raw_name2idx(int hashtype, int count, const char *name, int namelen);
2304
2305 /* prng.c */
2306 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
2307
2308 #endif /* __LINUX_OBD_CLASS_H */