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