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