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