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