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