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