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