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