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