Whamcloud - gitweb
LU-2675 obdclass: remove uses of lov_stripe_md
[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 static inline int obd_free_diskmd(struct obd_export *exp,
732                                   struct lov_mds_md **disk_tgt)
733 {
734         LASSERT(disk_tgt);
735         LASSERT(*disk_tgt);
736         /*
737          * LU-2590, for caller's convenience, *disk_tgt could be host
738          * endianness, it needs swab to LE if necessary, while just
739          * lov_mds_md header needs it for figuring out how much memory
740          * needs to be freed.
741          */
742         if ((cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) &&
743             (((*disk_tgt)->lmm_magic == LOV_MAGIC_V1) ||
744              ((*disk_tgt)->lmm_magic == LOV_MAGIC_V3)))
745                 lustre_swab_lov_mds_md(*disk_tgt);
746         return obd_packmd(exp, disk_tgt, NULL);
747 }
748
749 /* Unpack an MD struct from disk to in-memory format.
750  * Returns +ve size of unpacked MD (0 for free), or -ve error.
751  *
752  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
753  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
754  * If @*mem_tgt == NULL, it will be allocated
755  */
756 static inline int obd_unpackmd(struct obd_export *exp,
757                                struct lov_stripe_md **mem_tgt,
758                                struct lov_mds_md *disk_src,
759                                int disk_len)
760 {
761         int rc;
762         ENTRY;
763
764         EXP_CHECK_DT_OP(exp, unpackmd);
765         EXP_COUNTER_INCREMENT(exp, unpackmd);
766
767         rc = OBP(exp->exp_obd, unpackmd)(exp, mem_tgt, disk_src, disk_len);
768         RETURN(rc);
769 }
770
771 /* helper functions */
772 static inline int obd_alloc_memmd(struct obd_export *exp,
773                                   struct lov_stripe_md **mem_tgt)
774 {
775         LASSERT(mem_tgt);
776         LASSERT(*mem_tgt == NULL);
777         return obd_unpackmd(exp, mem_tgt, NULL, 0);
778 }
779
780 static inline int obd_free_memmd(struct obd_export *exp,
781                                  struct lov_stripe_md **mem_tgt)
782 {
783         int rc;
784
785         LASSERT(mem_tgt);
786         LASSERT(*mem_tgt);
787         rc = obd_unpackmd(exp, mem_tgt, NULL, 0);
788         *mem_tgt = NULL;
789         return rc;
790 }
791
792 static inline int obd_precreate(struct obd_export *exp)
793 {
794         int rc;
795         ENTRY;
796
797         EXP_CHECK_DT_OP(exp, precreate);
798         OBD_COUNTER_INCREMENT(exp->exp_obd, precreate);
799
800         rc = OBP(exp->exp_obd, precreate)(exp);
801         RETURN(rc);
802 }
803
804 static inline int obd_create_async(struct obd_export *exp,
805                                    struct obd_info *oinfo,
806                                    struct lov_stripe_md **ea,
807                                    struct obd_trans_info *oti)
808 {
809         int rc;
810         ENTRY;
811
812         EXP_CHECK_DT_OP(exp, create_async);
813         EXP_COUNTER_INCREMENT(exp, create_async);
814
815         rc = OBP(exp->exp_obd, create_async)(exp, oinfo, ea, oti);
816         RETURN(rc);
817 }
818
819 static inline int obd_create(const struct lu_env *env, struct obd_export *exp,
820                              struct obdo *obdo, struct lov_stripe_md **ea,
821                              struct obd_trans_info *oti)
822 {
823         int rc;
824         ENTRY;
825
826         EXP_CHECK_DT_OP(exp, create);
827         EXP_COUNTER_INCREMENT(exp, create);
828
829         rc = OBP(exp->exp_obd, create)(env, exp, obdo, ea, oti);
830         RETURN(rc);
831 }
832
833 static inline int obd_destroy(const struct lu_env *env, struct obd_export *exp,
834                               struct obdo *obdo, struct lov_stripe_md *ea,
835                               struct obd_trans_info *oti,
836                               struct obd_export *md_exp, void *capa)
837 {
838         int rc;
839         ENTRY;
840
841         EXP_CHECK_DT_OP(exp, destroy);
842         EXP_COUNTER_INCREMENT(exp, destroy);
843
844         rc = OBP(exp->exp_obd, destroy)(env, exp, obdo, ea, oti, md_exp, capa);
845         RETURN(rc);
846 }
847
848 static inline int obd_getattr(const struct lu_env *env, struct obd_export *exp,
849                               struct obd_info *oinfo)
850 {
851         int rc;
852         ENTRY;
853
854         EXP_CHECK_DT_OP(exp, getattr);
855         EXP_COUNTER_INCREMENT(exp, getattr);
856
857         rc = OBP(exp->exp_obd, getattr)(env, exp, oinfo);
858         RETURN(rc);
859 }
860
861 static inline int obd_getattr_async(struct obd_export *exp,
862                                     struct obd_info *oinfo,
863                                     struct ptlrpc_request_set *set)
864 {
865         int rc;
866         ENTRY;
867
868         EXP_CHECK_DT_OP(exp, getattr_async);
869         EXP_COUNTER_INCREMENT(exp, getattr_async);
870
871         rc = OBP(exp->exp_obd, getattr_async)(exp, oinfo, set);
872         RETURN(rc);
873 }
874
875 static inline int obd_setattr(const struct lu_env *env, struct obd_export *exp,
876                               struct obd_info *oinfo,
877                               struct obd_trans_info *oti)
878 {
879         int rc;
880         ENTRY;
881
882         EXP_CHECK_DT_OP(exp, setattr);
883         EXP_COUNTER_INCREMENT(exp, setattr);
884
885         rc = OBP(exp->exp_obd, setattr)(env, exp, oinfo, oti);
886         RETURN(rc);
887 }
888
889 /* This performs all the requests set init/wait/destroy actions. */
890 static inline int obd_setattr_rqset(struct obd_export *exp,
891                                     struct obd_info *oinfo,
892                                     struct obd_trans_info *oti)
893 {
894         struct ptlrpc_request_set *set = NULL;
895         int rc;
896         ENTRY;
897
898         EXP_CHECK_DT_OP(exp, setattr_async);
899         EXP_COUNTER_INCREMENT(exp, setattr_async);
900
901         set =  ptlrpc_prep_set();
902         if (set == NULL)
903                 RETURN(-ENOMEM);
904
905         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
906         if (rc == 0)
907                 rc = ptlrpc_set_wait(set);
908         ptlrpc_set_destroy(set);
909         RETURN(rc);
910 }
911
912 /* This adds all the requests into @set if @set != NULL, otherwise
913    all requests are sent asynchronously without waiting for response. */
914 static inline int obd_setattr_async(struct obd_export *exp,
915                                     struct obd_info *oinfo,
916                                     struct obd_trans_info *oti,
917                                     struct ptlrpc_request_set *set)
918 {
919         int rc;
920         ENTRY;
921
922         EXP_CHECK_DT_OP(exp, setattr_async);
923         EXP_COUNTER_INCREMENT(exp, setattr_async);
924
925         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
926         RETURN(rc);
927 }
928
929 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
930                                int priority)
931 {
932         struct obd_device *obd = imp->imp_obd;
933         int rc;
934         ENTRY;
935
936         OBD_CHECK_DEV_ACTIVE(obd);
937         OBD_CHECK_DT_OP(obd, add_conn, -EOPNOTSUPP);
938         OBD_COUNTER_INCREMENT(obd, add_conn);
939
940         rc = OBP(obd, add_conn)(imp, uuid, priority);
941         RETURN(rc);
942 }
943
944 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
945 {
946         struct obd_device *obd = imp->imp_obd;
947         int rc;
948         ENTRY;
949
950         OBD_CHECK_DEV_ACTIVE(obd);
951         OBD_CHECK_DT_OP(obd, del_conn, -EOPNOTSUPP);
952         OBD_COUNTER_INCREMENT(obd, del_conn);
953
954         rc = OBP(obd, del_conn)(imp, uuid);
955         RETURN(rc);
956 }
957
958 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
959 {
960         struct obd_uuid *uuid;
961         ENTRY;
962
963         OBD_CHECK_DT_OP(exp->exp_obd, get_uuid, NULL);
964         EXP_COUNTER_INCREMENT(exp, get_uuid);
965
966         uuid = OBP(exp->exp_obd, get_uuid)(exp);
967         RETURN(uuid);
968 }
969
970 /** Create a new /a exp on device /a obd for the uuid /a cluuid
971  * @param exp New export handle
972  * @param d Connect data, supported flags are set, flags also understood
973  *    by obd are returned.
974  */
975 static inline int obd_connect(const struct lu_env *env,
976                               struct obd_export **exp,struct obd_device *obd,
977                               struct obd_uuid *cluuid,
978                               struct obd_connect_data *data,
979                               void *localdata)
980 {
981         int rc;
982         __u64 ocf = data ? data->ocd_connect_flags : 0; /* for post-condition
983                                                    * check */
984         ENTRY;
985
986         OBD_CHECK_DEV_ACTIVE(obd);
987         OBD_CHECK_DT_OP(obd, connect, -EOPNOTSUPP);
988         OBD_COUNTER_INCREMENT(obd, connect);
989
990         rc = OBP(obd, connect)(env, exp, obd, cluuid, data, localdata);
991         /* check that only subset is granted */
992         LASSERT(ergo(data != NULL, (data->ocd_connect_flags & ocf) ==
993                                     data->ocd_connect_flags));
994         RETURN(rc);
995 }
996
997 static inline int obd_reconnect(const struct lu_env *env,
998                                 struct obd_export *exp,
999                                 struct obd_device *obd,
1000                                 struct obd_uuid *cluuid,
1001                                 struct obd_connect_data *d,
1002                                 void *localdata)
1003 {
1004         int rc;
1005         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
1006                                                    * check */
1007
1008         ENTRY;
1009
1010         OBD_CHECK_DEV_ACTIVE(obd);
1011         OBD_CHECK_DT_OP(obd, reconnect, 0);
1012         OBD_COUNTER_INCREMENT(obd, reconnect);
1013
1014         rc = OBP(obd, reconnect)(env, exp, obd, cluuid, d, localdata);
1015         /* check that only subset is granted */
1016         LASSERT(ergo(d != NULL,
1017                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
1018         RETURN(rc);
1019 }
1020
1021 static inline int obd_disconnect(struct obd_export *exp)
1022 {
1023         int rc;
1024         ENTRY;
1025
1026         EXP_CHECK_DT_OP(exp, disconnect);
1027         EXP_COUNTER_INCREMENT(exp, disconnect);
1028
1029         rc = OBP(exp->exp_obd, disconnect)(exp);
1030         RETURN(rc);
1031 }
1032
1033 static inline int obd_fid_init(struct obd_device *obd, struct obd_export *exp,
1034                                enum lu_cli_type type)
1035 {
1036         int rc;
1037         ENTRY;
1038
1039         OBD_CHECK_DT_OP(obd, fid_init, 0);
1040         OBD_COUNTER_INCREMENT(obd, fid_init);
1041
1042         rc = OBP(obd, fid_init)(obd, exp, type);
1043         RETURN(rc);
1044 }
1045
1046 static inline int obd_fid_fini(struct obd_device *obd)
1047 {
1048         int rc;
1049         ENTRY;
1050
1051         OBD_CHECK_DT_OP(obd, fid_fini, 0);
1052         OBD_COUNTER_INCREMENT(obd, fid_fini);
1053
1054         rc = OBP(obd, fid_fini)(obd);
1055         RETURN(rc);
1056 }
1057
1058 static inline int obd_fid_alloc(struct obd_export *exp,
1059                                 struct lu_fid *fid,
1060                                 struct md_op_data *op_data)
1061 {
1062         int rc;
1063         ENTRY;
1064
1065         EXP_CHECK_DT_OP(exp, fid_alloc);
1066         EXP_COUNTER_INCREMENT(exp, fid_alloc);
1067
1068         rc = OBP(exp->exp_obd, fid_alloc)(exp, fid, op_data);
1069         RETURN(rc);
1070 }
1071
1072 static inline int obd_ping(const struct lu_env *env, struct obd_export *exp)
1073 {
1074         int rc;
1075         ENTRY;
1076
1077         OBD_CHECK_DT_OP(exp->exp_obd, ping, 0);
1078         EXP_COUNTER_INCREMENT(exp, ping);
1079
1080         rc = OBP(exp->exp_obd, ping)(env, exp);
1081         RETURN(rc);
1082 }
1083
1084 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
1085 {
1086         int rc;
1087         ENTRY;
1088
1089         OBD_CHECK_DT_OP(obd, pool_new, -EOPNOTSUPP);
1090         OBD_COUNTER_INCREMENT(obd, pool_new);
1091
1092         rc = OBP(obd, pool_new)(obd, poolname);
1093         RETURN(rc);
1094 }
1095
1096 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
1097 {
1098         int rc;
1099         ENTRY;
1100
1101         OBD_CHECK_DT_OP(obd, pool_del, -EOPNOTSUPP);
1102         OBD_COUNTER_INCREMENT(obd, pool_del);
1103
1104         rc = OBP(obd, pool_del)(obd, poolname);
1105         RETURN(rc);
1106 }
1107
1108 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
1109 {
1110         int rc;
1111         ENTRY;
1112
1113         OBD_CHECK_DT_OP(obd, pool_add, -EOPNOTSUPP);
1114         OBD_COUNTER_INCREMENT(obd, pool_add);
1115
1116         rc = OBP(obd, pool_add)(obd, poolname, ostname);
1117         RETURN(rc);
1118 }
1119
1120 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
1121 {
1122         int rc;
1123         ENTRY;
1124
1125         OBD_CHECK_DT_OP(obd, pool_rem, -EOPNOTSUPP);
1126         OBD_COUNTER_INCREMENT(obd, pool_rem);
1127
1128         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
1129         RETURN(rc);
1130 }
1131
1132 static inline void obd_getref(struct obd_device *obd)
1133 {
1134         ENTRY;
1135         if (OBT(obd) && OBP(obd, getref)) {
1136                 OBD_COUNTER_INCREMENT(obd, getref);
1137                 OBP(obd, getref)(obd);
1138         }
1139         EXIT;
1140 }
1141
1142 static inline void obd_putref(struct obd_device *obd)
1143 {
1144         ENTRY;
1145         if (OBT(obd) && OBP(obd, putref)) {
1146                 OBD_COUNTER_INCREMENT(obd, putref);
1147                 OBP(obd, putref)(obd);
1148         }
1149         EXIT;
1150 }
1151
1152 static inline int obd_init_export(struct obd_export *exp)
1153 {
1154         int rc = 0;
1155
1156         ENTRY;
1157         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1158             OBP((exp)->exp_obd, init_export))
1159                 rc = OBP(exp->exp_obd, init_export)(exp);
1160         RETURN(rc);
1161 }
1162
1163 static inline int obd_destroy_export(struct obd_export *exp)
1164 {
1165         ENTRY;
1166         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1167             OBP((exp)->exp_obd, destroy_export))
1168                 OBP(exp->exp_obd, destroy_export)(exp);
1169         RETURN(0);
1170 }
1171
1172 static inline int obd_extent_calc(struct obd_export *exp,
1173                                   struct lov_stripe_md *md,
1174                                   int cmd, obd_off *offset)
1175 {
1176         int rc;
1177         ENTRY;
1178         EXP_CHECK_DT_OP(exp, extent_calc);
1179         rc = OBP(exp->exp_obd, extent_calc)(exp, md, cmd, offset);
1180         RETURN(rc);
1181 }
1182
1183 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1184  * If the cache is older than @max_age we will get a new value from the
1185  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1186 static inline int obd_statfs_async(struct obd_export *exp,
1187                                    struct obd_info *oinfo,
1188                                    __u64 max_age,
1189                                    struct ptlrpc_request_set *rqset)
1190 {
1191         int rc = 0;
1192         struct obd_device *obd;
1193         ENTRY;
1194
1195         if (exp == NULL || exp->exp_obd == NULL)
1196                 RETURN(-EINVAL);
1197
1198         obd = exp->exp_obd;
1199         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1200         OBD_COUNTER_INCREMENT(obd, statfs);
1201
1202         CDEBUG(D_SUPER, "%s: osfs %p age "LPU64", max_age "LPU64"\n",
1203                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
1204         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1205                 rc = OBP(obd, statfs_async)(exp, oinfo, max_age, rqset);
1206         } else {
1207                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
1208                        " objects "LPU64"/"LPU64"\n",
1209                        obd->obd_name, &obd->obd_osfs,
1210                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1211                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1212                 spin_lock(&obd->obd_osfs_lock);
1213                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1214                 spin_unlock(&obd->obd_osfs_lock);
1215                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1216                 if (oinfo->oi_cb_up)
1217                         oinfo->oi_cb_up(oinfo, 0);
1218         }
1219         RETURN(rc);
1220 }
1221
1222 static inline int obd_statfs_rqset(struct obd_export *exp,
1223                                    struct obd_statfs *osfs, __u64 max_age,
1224                                    __u32 flags)
1225 {
1226         struct ptlrpc_request_set *set = NULL;
1227         struct obd_info oinfo = { { { 0 } } };
1228         int rc = 0;
1229         ENTRY;
1230
1231         set =  ptlrpc_prep_set();
1232         if (set == NULL)
1233                 RETURN(-ENOMEM);
1234
1235         oinfo.oi_osfs = osfs;
1236         oinfo.oi_flags = flags;
1237         rc = obd_statfs_async(exp, &oinfo, max_age, set);
1238         if (rc == 0)
1239                 rc = ptlrpc_set_wait(set);
1240         ptlrpc_set_destroy(set);
1241         RETURN(rc);
1242 }
1243
1244 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1245  * If the cache is older than @max_age we will get a new value from the
1246  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1247 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1248                              struct obd_statfs *osfs, __u64 max_age,
1249                              __u32 flags)
1250 {
1251         int rc = 0;
1252         struct obd_device *obd = exp->exp_obd;
1253         ENTRY;
1254
1255         if (obd == NULL)
1256                 RETURN(-EINVAL);
1257
1258         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1259         OBD_COUNTER_INCREMENT(obd, statfs);
1260
1261         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
1262                obd->obd_osfs_age, max_age);
1263         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1264                 rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1265                 if (rc == 0) {
1266                         spin_lock(&obd->obd_osfs_lock);
1267                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1268                         obd->obd_osfs_age = cfs_time_current_64();
1269                         spin_unlock(&obd->obd_osfs_lock);
1270                 }
1271         } else {
1272                 CDEBUG(D_SUPER, "%s: use %p cache blocks "LPU64"/"LPU64
1273                        " objects "LPU64"/"LPU64"\n",
1274                        obd->obd_name, &obd->obd_osfs,
1275                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1276                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1277                 spin_lock(&obd->obd_osfs_lock);
1278                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1279                 spin_unlock(&obd->obd_osfs_lock);
1280         }
1281         RETURN(rc);
1282 }
1283
1284 static inline int obd_sync_rqset(struct obd_export *exp, struct obd_info *oinfo,
1285                                  obd_size start, obd_size end)
1286 {
1287         struct ptlrpc_request_set *set = NULL;
1288         int rc;
1289         ENTRY;
1290
1291         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1292         EXP_COUNTER_INCREMENT(exp, sync);
1293
1294         set =  ptlrpc_prep_set();
1295         if (set == NULL)
1296                 RETURN(-ENOMEM);
1297
1298         rc = OBP(exp->exp_obd, sync)(NULL, exp, oinfo, start, end, set);
1299         if (rc == 0)
1300                 rc = ptlrpc_set_wait(set);
1301         ptlrpc_set_destroy(set);
1302         RETURN(rc);
1303 }
1304
1305 static inline int obd_sync(const struct lu_env *env, struct obd_export *exp,
1306                            struct obd_info *oinfo, obd_size start, obd_size end,
1307                            struct ptlrpc_request_set *set)
1308 {
1309         int rc;
1310         ENTRY;
1311
1312         OBD_CHECK_DT_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1313         EXP_COUNTER_INCREMENT(exp, sync);
1314
1315         rc = OBP(exp->exp_obd, sync)(env, exp, oinfo, start, end, set);
1316         RETURN(rc);
1317 }
1318
1319 static inline int obd_punch_rqset(struct obd_export *exp,
1320                                   struct obd_info *oinfo,
1321                                   struct obd_trans_info *oti)
1322 {
1323         struct ptlrpc_request_set *set = NULL;
1324         int rc;
1325         ENTRY;
1326
1327         EXP_CHECK_DT_OP(exp, punch);
1328         EXP_COUNTER_INCREMENT(exp, punch);
1329
1330         set =  ptlrpc_prep_set();
1331         if (set == NULL)
1332                 RETURN(-ENOMEM);
1333
1334         rc = OBP(exp->exp_obd, punch)(NULL, exp, oinfo, oti, set);
1335         if (rc == 0)
1336                 rc = ptlrpc_set_wait(set);
1337         ptlrpc_set_destroy(set);
1338         RETURN(rc);
1339 }
1340
1341 static inline int obd_punch(const struct lu_env *env, struct obd_export *exp,
1342                             struct obd_info *oinfo, struct obd_trans_info *oti,
1343                             struct ptlrpc_request_set *rqset)
1344 {
1345         int rc;
1346         ENTRY;
1347
1348         EXP_CHECK_DT_OP(exp, punch);
1349         EXP_COUNTER_INCREMENT(exp, punch);
1350
1351         rc = OBP(exp->exp_obd, punch)(env, exp, oinfo, oti, rqset);
1352         RETURN(rc);
1353 }
1354
1355 static inline int obd_brw(int cmd, struct obd_export *exp,
1356                           struct obd_info *oinfo, obd_count oa_bufs,
1357                           struct brw_page *pg, struct obd_trans_info *oti)
1358 {
1359         int rc;
1360         ENTRY;
1361
1362         EXP_CHECK_DT_OP(exp, brw);
1363         EXP_COUNTER_INCREMENT(exp, brw);
1364
1365         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
1366                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
1367                        "or OBD_BRW_CHECK\n");
1368                 LBUG();
1369         }
1370
1371         rc = OBP(exp->exp_obd, brw)(cmd, exp, oinfo, oa_bufs, pg, oti);
1372         RETURN(rc);
1373 }
1374
1375 static inline int obd_preprw(const struct lu_env *env, int cmd,
1376                              struct obd_export *exp, struct obdo *oa,
1377                              int objcount, struct obd_ioobj *obj,
1378                              struct niobuf_remote *remote, int *pages,
1379                              struct niobuf_local *local,
1380                              struct obd_trans_info *oti,
1381                              struct lustre_capa *capa)
1382 {
1383         int rc;
1384         ENTRY;
1385
1386         EXP_CHECK_DT_OP(exp, preprw);
1387         EXP_COUNTER_INCREMENT(exp, preprw);
1388
1389         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1390                                        pages, local, oti, capa);
1391         RETURN(rc);
1392 }
1393
1394 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1395                                struct obd_export *exp, struct obdo *oa,
1396                                int objcount, struct obd_ioobj *obj,
1397                                struct niobuf_remote *rnb, int pages,
1398                                struct niobuf_local *local,
1399                                struct obd_trans_info *oti, int rc)
1400 {
1401         ENTRY;
1402
1403         EXP_CHECK_DT_OP(exp, commitrw);
1404         EXP_COUNTER_INCREMENT(exp, commitrw);
1405
1406         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1407                                          rnb, pages, local, oti, rc);
1408         RETURN(rc);
1409 }
1410
1411 static inline int obd_merge_lvb(struct obd_export *exp,
1412                                 struct lov_stripe_md *lsm,
1413                                 struct ost_lvb *lvb, int kms_only)
1414 {
1415         int rc;
1416         ENTRY;
1417
1418         EXP_CHECK_DT_OP(exp, merge_lvb);
1419         EXP_COUNTER_INCREMENT(exp, merge_lvb);
1420
1421         rc = OBP(exp->exp_obd, merge_lvb)(exp, lsm, lvb, kms_only);
1422         RETURN(rc);
1423 }
1424
1425 static inline int obd_adjust_kms(struct obd_export *exp,
1426                                  struct lov_stripe_md *lsm, obd_off size,
1427                                  int shrink)
1428 {
1429         int rc;
1430         ENTRY;
1431
1432         EXP_CHECK_DT_OP(exp, adjust_kms);
1433         EXP_COUNTER_INCREMENT(exp, adjust_kms);
1434
1435         rc = OBP(exp->exp_obd, adjust_kms)(exp, lsm, size, shrink);
1436         RETURN(rc);
1437 }
1438
1439 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1440                                 int len, void *karg, void *uarg)
1441 {
1442         int rc;
1443         ENTRY;
1444
1445         EXP_CHECK_DT_OP(exp, iocontrol);
1446         EXP_COUNTER_INCREMENT(exp, iocontrol);
1447
1448         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1449         RETURN(rc);
1450 }
1451
1452 static inline int obd_enqueue_rqset(struct obd_export *exp,
1453                                     struct obd_info *oinfo,
1454                                     struct ldlm_enqueue_info *einfo)
1455 {
1456         struct ptlrpc_request_set *set = NULL;
1457         int rc;
1458         ENTRY;
1459
1460         EXP_CHECK_DT_OP(exp, enqueue);
1461         EXP_COUNTER_INCREMENT(exp, enqueue);
1462
1463         set =  ptlrpc_prep_set();
1464         if (set == NULL)
1465                 RETURN(-ENOMEM);
1466
1467         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1468         if (rc == 0)
1469                 rc = ptlrpc_set_wait(set);
1470         ptlrpc_set_destroy(set);
1471         RETURN(rc);
1472 }
1473
1474 static inline int obd_enqueue(struct obd_export *exp,
1475                               struct obd_info *oinfo,
1476                               struct ldlm_enqueue_info *einfo,
1477                               struct ptlrpc_request_set *set)
1478 {
1479         int rc;
1480         ENTRY;
1481
1482         EXP_CHECK_DT_OP(exp, enqueue);
1483         EXP_COUNTER_INCREMENT(exp, enqueue);
1484
1485         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1486         RETURN(rc);
1487 }
1488
1489 static inline int obd_change_cbdata(struct obd_export *exp,
1490                                     struct lov_stripe_md *lsm,
1491                                     ldlm_iterator_t it, void *data)
1492 {
1493         int rc;
1494         ENTRY;
1495
1496         EXP_CHECK_DT_OP(exp, change_cbdata);
1497         EXP_COUNTER_INCREMENT(exp, change_cbdata);
1498
1499         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1500         RETURN(rc);
1501 }
1502
1503 static inline int obd_find_cbdata(struct obd_export *exp,
1504                                   struct lov_stripe_md *lsm,
1505                                   ldlm_iterator_t it, void *data)
1506 {
1507         int rc;
1508         ENTRY;
1509
1510         EXP_CHECK_DT_OP(exp, find_cbdata);
1511         EXP_COUNTER_INCREMENT(exp, find_cbdata);
1512
1513         rc = OBP(exp->exp_obd, find_cbdata)(exp, lsm, it, data);
1514         RETURN(rc);
1515 }
1516
1517 static inline int obd_cancel(struct obd_export *exp,
1518                              struct lov_stripe_md *ea, __u32 mode,
1519                              struct lustre_handle *lockh)
1520 {
1521         int rc;
1522         ENTRY;
1523
1524         EXP_CHECK_DT_OP(exp, cancel);
1525         EXP_COUNTER_INCREMENT(exp, cancel);
1526
1527         rc = OBP(exp->exp_obd, cancel)(exp, ea, mode, lockh);
1528         RETURN(rc);
1529 }
1530
1531 static inline int obd_cancel_unused(struct obd_export *exp,
1532                                     struct lov_stripe_md *ea,
1533                                     ldlm_cancel_flags_t flags,
1534                                     void *opaque)
1535 {
1536         int rc;
1537         ENTRY;
1538
1539         EXP_CHECK_DT_OP(exp, cancel_unused);
1540         EXP_COUNTER_INCREMENT(exp, cancel_unused);
1541
1542         rc = OBP(exp->exp_obd, cancel_unused)(exp, ea, flags, opaque);
1543         RETURN(rc);
1544 }
1545
1546 static inline int obd_pin(struct obd_export *exp, const struct lu_fid *fid,
1547                           struct obd_capa *oc, struct obd_client_handle *handle,
1548                           int flag)
1549 {
1550         int rc;
1551         ENTRY;
1552
1553         EXP_CHECK_DT_OP(exp, pin);
1554         EXP_COUNTER_INCREMENT(exp, pin);
1555
1556         rc = OBP(exp->exp_obd, pin)(exp, fid, oc, handle, flag);
1557         RETURN(rc);
1558 }
1559
1560 static inline int obd_unpin(struct obd_export *exp,
1561                             struct obd_client_handle *handle, int flag)
1562 {
1563         int rc;
1564         ENTRY;
1565
1566         EXP_CHECK_DT_OP(exp, unpin);
1567         EXP_COUNTER_INCREMENT(exp, unpin);
1568
1569         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
1570         RETURN(rc);
1571 }
1572
1573
1574 static inline void obd_import_event(struct obd_device *obd,
1575                                     struct obd_import *imp,
1576                                     enum obd_import_event event)
1577 {
1578         ENTRY;
1579         if (!obd) {
1580                 CERROR("NULL device\n");
1581                 EXIT;
1582                 return;
1583         }
1584         if (obd->obd_set_up && OBP(obd, import_event)) {
1585                 OBD_COUNTER_INCREMENT(obd, import_event);
1586                 OBP(obd, import_event)(obd, imp, event);
1587         }
1588         EXIT;
1589 }
1590
1591 static inline int obd_llog_connect(struct obd_export *exp,
1592                                    struct llogd_conn_body *body)
1593 {
1594         int rc;
1595         ENTRY;
1596
1597         OBD_CHECK_DT_OP(exp->exp_obd, llog_connect, 0);
1598         EXP_COUNTER_INCREMENT(exp, llog_connect);
1599
1600         rc = OBP(exp->exp_obd, llog_connect)(exp, body);
1601         RETURN(rc);
1602 }
1603
1604
1605 static inline int obd_notify(struct obd_device *obd,
1606                              struct obd_device *watched,
1607                              enum obd_notify_event ev,
1608                              void *data)
1609 {
1610         int rc;
1611         ENTRY;
1612         OBD_CHECK_DEV(obd);
1613
1614         /* the check for async_recov is a complete hack - I'm hereby
1615            overloading the meaning to also mean "this was called from
1616            mds_postsetup".  I know that my mds is able to handle notifies
1617            by this point, and it needs to get them to execute mds_postrecov. */
1618         if (!obd->obd_set_up && !obd->obd_async_recov) {
1619                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1620                 RETURN(-EINVAL);
1621         }
1622
1623         if (!OBP(obd, notify)) {
1624                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1625                 RETURN(-ENOSYS);
1626         }
1627
1628         OBD_COUNTER_INCREMENT(obd, notify);
1629         rc = OBP(obd, notify)(obd, watched, ev, data);
1630         RETURN(rc);
1631 }
1632
1633 static inline int obd_notify_observer(struct obd_device *observer,
1634                                       struct obd_device *observed,
1635                                       enum obd_notify_event ev,
1636                                       void *data)
1637 {
1638         int rc1;
1639         int rc2;
1640
1641         struct obd_notify_upcall *onu;
1642
1643         if (observer->obd_observer)
1644                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1645         else
1646                 rc1 = 0;
1647         /*
1648          * Also, call non-obd listener, if any
1649          */
1650         onu = &observer->obd_upcall;
1651         if (onu->onu_upcall != NULL)
1652                 rc2 = onu->onu_upcall(observer, observed, ev,
1653                                       onu->onu_owner, NULL);
1654         else
1655                 rc2 = 0;
1656
1657         return rc1 ? rc1 : rc2;
1658 }
1659
1660 static inline int obd_quotacheck(struct obd_export *exp,
1661                                  struct obd_quotactl *oqctl)
1662 {
1663         int rc;
1664         ENTRY;
1665
1666         EXP_CHECK_DT_OP(exp, quotacheck);
1667         EXP_COUNTER_INCREMENT(exp, quotacheck);
1668
1669         rc = OBP(exp->exp_obd, quotacheck)(exp->exp_obd, exp, oqctl);
1670         RETURN(rc);
1671 }
1672
1673 static inline int obd_quotactl(struct obd_export *exp,
1674                                struct obd_quotactl *oqctl)
1675 {
1676         int rc;
1677         ENTRY;
1678
1679         EXP_CHECK_DT_OP(exp, quotactl);
1680         EXP_COUNTER_INCREMENT(exp, quotactl);
1681
1682         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1683         RETURN(rc);
1684 }
1685
1686 static inline int obd_health_check(const struct lu_env *env,
1687                                    struct obd_device *obd)
1688 {
1689         /* returns: 0 on healthy
1690          *         >0 on unhealthy + reason code/flag
1691          *            however the only suppored reason == 1 right now
1692          *            We'll need to define some better reasons
1693          *            or flags in the future.
1694          *         <0 on error
1695          */
1696         int rc;
1697         ENTRY;
1698
1699         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1700         if (obd == NULL || !OBT(obd)) {
1701                 CERROR("cleaned up obd\n");
1702                 RETURN(-EOPNOTSUPP);
1703         }
1704         if (!obd->obd_set_up || obd->obd_stopping)
1705                 RETURN(0);
1706         if (!OBP(obd, health_check))
1707                 RETURN(0);
1708
1709         rc = OBP(obd, health_check)(env, obd);
1710         RETURN(rc);
1711 }
1712
1713 static inline int obd_register_observer(struct obd_device *obd,
1714                                         struct obd_device *observer)
1715 {
1716         ENTRY;
1717         OBD_CHECK_DEV(obd);
1718         down_write(&obd->obd_observer_link_sem);
1719         if (obd->obd_observer && observer) {
1720                 up_write(&obd->obd_observer_link_sem);
1721                 RETURN(-EALREADY);
1722         }
1723         obd->obd_observer = observer;
1724         up_write(&obd->obd_observer_link_sem);
1725         RETURN(0);
1726 }
1727
1728 static inline int obd_pin_observer(struct obd_device *obd,
1729                                    struct obd_device **observer)
1730 {
1731         ENTRY;
1732         down_read(&obd->obd_observer_link_sem);
1733         if (!obd->obd_observer) {
1734                 *observer = NULL;
1735                 up_read(&obd->obd_observer_link_sem);
1736                 RETURN(-ENOENT);
1737         }
1738         *observer = obd->obd_observer;
1739         RETURN(0);
1740 }
1741
1742 static inline int obd_unpin_observer(struct obd_device *obd)
1743 {
1744         ENTRY;
1745         up_read(&obd->obd_observer_link_sem);
1746         RETURN(0);
1747 }
1748
1749 #if 0
1750 static inline int obd_register_page_removal_cb(struct obd_export *exp,
1751                                                obd_page_removal_cb_t cb,
1752                                                obd_pin_extent_cb pin_cb)
1753 {
1754         int rc;
1755         ENTRY;
1756
1757         OBD_CHECK_DT_OP(exp->exp_obd, register_page_removal_cb, 0);
1758         OBD_COUNTER_INCREMENT(exp->exp_obd, register_page_removal_cb);
1759
1760         rc = OBP(exp->exp_obd, register_page_removal_cb)(exp, cb, pin_cb);
1761         RETURN(rc);
1762 }
1763
1764 static inline int obd_unregister_page_removal_cb(struct obd_export *exp,
1765                                                  obd_page_removal_cb_t cb)
1766 {
1767         int rc;
1768         ENTRY;
1769
1770         OBD_CHECK_DT_OP(exp->exp_obd, unregister_page_removal_cb, 0);
1771         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_page_removal_cb);
1772
1773         rc = OBP(exp->exp_obd, unregister_page_removal_cb)(exp, cb);
1774         RETURN(rc);
1775 }
1776
1777 static inline int obd_register_lock_cancel_cb(struct obd_export *exp,
1778                                               obd_lock_cancel_cb cb)
1779 {
1780         int rc;
1781         ENTRY;
1782
1783         OBD_CHECK_DT_OP(exp->exp_obd, register_lock_cancel_cb, 0);
1784         OBD_COUNTER_INCREMENT(exp->exp_obd, register_lock_cancel_cb);
1785
1786         rc = OBP(exp->exp_obd, register_lock_cancel_cb)(exp, cb);
1787         RETURN(rc);
1788 }
1789
1790 static inline int obd_unregister_lock_cancel_cb(struct obd_export *exp,
1791                                                  obd_lock_cancel_cb cb)
1792 {
1793         int rc;
1794         ENTRY;
1795
1796         OBD_CHECK_DT_OP(exp->exp_obd, unregister_lock_cancel_cb, 0);
1797         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_lock_cancel_cb);
1798
1799         rc = OBP(exp->exp_obd, unregister_lock_cancel_cb)(exp, cb);
1800         RETURN(rc);
1801 }
1802 #endif
1803
1804 /* metadata helpers */
1805 static inline int md_getstatus(struct obd_export *exp,
1806                                struct lu_fid *fid, struct obd_capa **pc)
1807 {
1808         int rc;
1809         ENTRY;
1810
1811         EXP_CHECK_MD_OP(exp, getstatus);
1812         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1813         rc = MDP(exp->exp_obd, getstatus)(exp, fid, pc);
1814         RETURN(rc);
1815 }
1816
1817 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1818                              struct ptlrpc_request **request)
1819 {
1820         int rc;
1821         ENTRY;
1822         EXP_CHECK_MD_OP(exp, getattr);
1823         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1824         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1825         RETURN(rc);
1826 }
1827
1828 static inline int md_null_inode(struct obd_export *exp,
1829                                    const struct lu_fid *fid)
1830 {
1831         int rc;
1832         ENTRY;
1833         EXP_CHECK_MD_OP(exp, null_inode);
1834         EXP_MD_COUNTER_INCREMENT(exp, null_inode);
1835         rc = MDP(exp->exp_obd, null_inode)(exp, fid);
1836         RETURN(rc);
1837 }
1838
1839 static inline int md_find_cbdata(struct obd_export *exp,
1840                                  const struct lu_fid *fid,
1841                                  ldlm_iterator_t it, void *data)
1842 {
1843         int rc;
1844         ENTRY;
1845         EXP_CHECK_MD_OP(exp, find_cbdata);
1846         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1847         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1848         RETURN(rc);
1849 }
1850
1851 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1852                            struct md_open_data *mod,
1853                            struct ptlrpc_request **request)
1854 {
1855         int rc;
1856         ENTRY;
1857         EXP_CHECK_MD_OP(exp, close);
1858         EXP_MD_COUNTER_INCREMENT(exp, close);
1859         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1860         RETURN(rc);
1861 }
1862
1863 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1864                             const void *data, int datalen, int mode, __u32 uid,
1865                             __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1866                             struct ptlrpc_request **request)
1867 {
1868         int rc;
1869         ENTRY;
1870         EXP_CHECK_MD_OP(exp, create);
1871         EXP_MD_COUNTER_INCREMENT(exp, create);
1872         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1873                                        uid, gid, cap_effective, rdev, request);
1874         RETURN(rc);
1875 }
1876
1877 static inline int md_done_writing(struct obd_export *exp,
1878                                   struct md_op_data *op_data,
1879                                   struct md_open_data *mod)
1880 {
1881         int rc;
1882         ENTRY;
1883         EXP_CHECK_MD_OP(exp, done_writing);
1884         EXP_MD_COUNTER_INCREMENT(exp, done_writing);
1885         rc = MDP(exp->exp_obd, done_writing)(exp, op_data, mod);
1886         RETURN(rc);
1887 }
1888
1889 static inline int md_enqueue(struct obd_export *exp,
1890                              struct ldlm_enqueue_info *einfo,
1891                              struct lookup_intent *it,
1892                              struct md_op_data *op_data,
1893                              struct lustre_handle *lockh,
1894                              void *lmm, int lmmsize,
1895                              struct ptlrpc_request **req,
1896                              __u64 extra_lock_flags)
1897 {
1898         int rc;
1899         ENTRY;
1900         EXP_CHECK_MD_OP(exp, enqueue);
1901         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1902         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, it, op_data, lockh,
1903                                         lmm, lmmsize, req, extra_lock_flags);
1904         RETURN(rc);
1905 }
1906
1907 static inline int md_getattr_name(struct obd_export *exp,
1908                                   struct md_op_data *op_data,
1909                                   struct ptlrpc_request **request)
1910 {
1911         int rc;
1912         ENTRY;
1913         EXP_CHECK_MD_OP(exp, getattr_name);
1914         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1915         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1916         RETURN(rc);
1917 }
1918
1919 static inline int md_intent_lock(struct obd_export *exp,
1920                                  struct md_op_data *op_data, void *lmm,
1921                                  int lmmsize, struct lookup_intent *it,
1922                                  int lookup_flags, struct ptlrpc_request **reqp,
1923                                  ldlm_blocking_callback cb_blocking,
1924                                  __u64 extra_lock_flags)
1925 {
1926         int rc;
1927         ENTRY;
1928         EXP_CHECK_MD_OP(exp, intent_lock);
1929         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1930         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, lmm, lmmsize,
1931                                             it, lookup_flags, reqp, cb_blocking,
1932                                             extra_lock_flags);
1933         RETURN(rc);
1934 }
1935
1936 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1937                           struct ptlrpc_request **request)
1938 {
1939         int rc;
1940         ENTRY;
1941         EXP_CHECK_MD_OP(exp, link);
1942         EXP_MD_COUNTER_INCREMENT(exp, link);
1943         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1944         RETURN(rc);
1945 }
1946
1947 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1948                             const char *old, int oldlen, const char *new,
1949                             int newlen, struct ptlrpc_request **request)
1950 {
1951         int rc;
1952         ENTRY;
1953         EXP_CHECK_MD_OP(exp, rename);
1954         EXP_MD_COUNTER_INCREMENT(exp, rename);
1955         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1956                                        newlen, request);
1957         RETURN(rc);
1958 }
1959
1960 static inline int md_is_subdir(struct obd_export *exp,
1961                                const struct lu_fid *pfid,
1962                                const struct lu_fid *cfid,
1963                                struct ptlrpc_request **request)
1964 {
1965         int rc;
1966         ENTRY;
1967         EXP_CHECK_MD_OP(exp, is_subdir);
1968         EXP_MD_COUNTER_INCREMENT(exp, is_subdir);
1969         rc = MDP(exp->exp_obd, is_subdir)(exp, pfid, cfid, request);
1970         RETURN(rc);
1971 }
1972
1973 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1974                              void *ea, int ealen, void *ea2, int ea2len,
1975                              struct ptlrpc_request **request,
1976                              struct md_open_data **mod)
1977 {
1978         int rc;
1979         ENTRY;
1980         EXP_CHECK_MD_OP(exp, setattr);
1981         EXP_MD_COUNTER_INCREMENT(exp, setattr);
1982         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen,
1983                                         ea2, ea2len, request, mod);
1984         RETURN(rc);
1985 }
1986
1987 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
1988                            struct obd_capa *oc, struct ptlrpc_request **request)
1989 {
1990         int rc;
1991         ENTRY;
1992         EXP_CHECK_MD_OP(exp, fsync);
1993         EXP_MD_COUNTER_INCREMENT(exp, fsync);
1994         rc = MDP(exp->exp_obd, fsync)(exp, fid, oc, request);
1995         RETURN(rc);
1996 }
1997
1998 static inline int md_readpage(struct obd_export *exp, struct md_op_data *opdata,
1999                               struct page **pages,
2000                               struct ptlrpc_request **request)
2001 {
2002         int rc;
2003         ENTRY;
2004         EXP_CHECK_MD_OP(exp, readpage);
2005         EXP_MD_COUNTER_INCREMENT(exp, readpage);
2006         rc = MDP(exp->exp_obd, readpage)(exp, opdata, pages, request);
2007         RETURN(rc);
2008 }
2009
2010 static inline int md_read_entry(struct obd_export *exp,
2011                                 struct md_op_data *op_data,
2012                                 struct md_callback *cb_op,
2013                                 struct lu_dirent **ld,
2014                                 struct page **ppage)
2015 {
2016         int rc;
2017         ENTRY;
2018         EXP_CHECK_MD_OP(exp, read_entry);
2019         EXP_MD_COUNTER_INCREMENT(exp, read_entry);
2020         rc = MDP(exp->exp_obd, read_entry)(exp, op_data, cb_op, ld, ppage);
2021         RETURN(rc);
2022 }
2023
2024 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
2025                             struct ptlrpc_request **request)
2026 {
2027         int rc;
2028         ENTRY;
2029         EXP_CHECK_MD_OP(exp, unlink);
2030         EXP_MD_COUNTER_INCREMENT(exp, unlink);
2031         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
2032         RETURN(rc);
2033 }
2034
2035 static inline int md_get_lustre_md(struct obd_export *exp,
2036                                    struct ptlrpc_request *req,
2037                                    struct obd_export *dt_exp,
2038                                    struct obd_export *md_exp,
2039                                    struct lustre_md *md)
2040 {
2041         ENTRY;
2042         EXP_CHECK_MD_OP(exp, get_lustre_md);
2043         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
2044         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
2045 }
2046
2047 static inline int md_free_lustre_md(struct obd_export *exp,
2048                                     struct lustre_md *md)
2049 {
2050         ENTRY;
2051         EXP_CHECK_MD_OP(exp, free_lustre_md);
2052         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
2053         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
2054 }
2055
2056 static inline int md_update_lsm_md(struct obd_export *exp,
2057                                    struct lmv_stripe_md *lsm,
2058                                    struct mdt_body *body,
2059                                    ldlm_blocking_callback cb)
2060 {
2061         ENTRY;
2062         EXP_CHECK_MD_OP(exp, update_lsm_md);
2063         EXP_MD_COUNTER_INCREMENT(exp, update_lsm_md);
2064         RETURN(MDP(exp->exp_obd, update_lsm_md)(exp, lsm, body, cb));
2065 }
2066
2067 static inline int md_merge_attr(struct obd_export *exp,
2068                                 const struct lmv_stripe_md *lsm,
2069                                 struct cl_attr *attr)
2070 {
2071         ENTRY;
2072         EXP_CHECK_MD_OP(exp, merge_attr);
2073         EXP_MD_COUNTER_INCREMENT(exp, merge_attr);
2074         RETURN(MDP(exp->exp_obd, merge_attr)(exp, lsm, attr));
2075 }
2076
2077 static inline int md_setxattr(struct obd_export *exp,
2078                               const struct lu_fid *fid, struct obd_capa *oc,
2079                               obd_valid valid, const char *name,
2080                               const char *input, int input_size,
2081                               int output_size, int flags, __u32 suppgid,
2082                               struct ptlrpc_request **request)
2083 {
2084         ENTRY;
2085         EXP_CHECK_MD_OP(exp, setxattr);
2086         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
2087         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, oc, valid, name, input,
2088                                            input_size, output_size, flags,
2089                                            suppgid, request));
2090 }
2091
2092 static inline int md_getxattr(struct obd_export *exp,
2093                               const struct lu_fid *fid, struct obd_capa *oc,
2094                               obd_valid valid, const char *name,
2095                               const char *input, int input_size,
2096                               int output_size, int flags,
2097                               struct ptlrpc_request **request)
2098 {
2099         ENTRY;
2100         EXP_CHECK_MD_OP(exp, getxattr);
2101         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
2102         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, oc, valid, name, input,
2103                                            input_size, output_size, flags,
2104                                            request));
2105 }
2106
2107 static inline int md_set_open_replay_data(struct obd_export *exp,
2108                                           struct obd_client_handle *och,
2109                                           struct lookup_intent *it)
2110 {
2111         ENTRY;
2112         EXP_CHECK_MD_OP(exp, set_open_replay_data);
2113         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
2114         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, it));
2115 }
2116
2117 static inline int md_clear_open_replay_data(struct obd_export *exp,
2118                                             struct obd_client_handle *och)
2119 {
2120         ENTRY;
2121         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
2122         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
2123         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
2124 }
2125
2126 static inline int md_set_lock_data(struct obd_export *exp,
2127                                    __u64 *lockh, void *data, __u64 *bits)
2128 {
2129         ENTRY;
2130         EXP_CHECK_MD_OP(exp, set_lock_data);
2131         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
2132         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
2133 }
2134
2135 static inline int md_cancel_unused(struct obd_export *exp,
2136                                    const struct lu_fid *fid,
2137                                    ldlm_policy_data_t *policy,
2138                                    ldlm_mode_t mode,
2139                                    ldlm_cancel_flags_t flags,
2140                                    void *opaque)
2141 {
2142         int rc;
2143         ENTRY;
2144
2145         EXP_CHECK_MD_OP(exp, cancel_unused);
2146         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
2147
2148         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
2149                                               flags, opaque);
2150         RETURN(rc);
2151 }
2152
2153 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, __u64 flags,
2154                                         const struct lu_fid *fid,
2155                                         ldlm_type_t type,
2156                                         ldlm_policy_data_t *policy,
2157                                         ldlm_mode_t mode,
2158                                         struct lustre_handle *lockh)
2159 {
2160         ENTRY;
2161         EXP_CHECK_MD_OP(exp, lock_match);
2162         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
2163         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
2164                                              policy, mode, lockh));
2165 }
2166
2167 static inline int md_init_ea_size(struct obd_export *exp, int easize,
2168                                   int def_asize, int cookiesize)
2169 {
2170         ENTRY;
2171         EXP_CHECK_MD_OP(exp, init_ea_size);
2172         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
2173         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, easize, def_asize,
2174                                                cookiesize));
2175 }
2176
2177 static inline int md_get_remote_perm(struct obd_export *exp,
2178                                      const struct lu_fid *fid,
2179                                      struct obd_capa *oc, __u32 suppgid,
2180                                      struct ptlrpc_request **request)
2181 {
2182         ENTRY;
2183         EXP_CHECK_MD_OP(exp, get_remote_perm);
2184         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
2185         RETURN(MDP(exp->exp_obd, get_remote_perm)(exp, fid, oc, suppgid,
2186                                                   request));
2187 }
2188
2189 static inline int md_renew_capa(struct obd_export *exp, struct obd_capa *ocapa,
2190                                 renew_capa_cb_t cb)
2191 {
2192         int rc;
2193         ENTRY;
2194         EXP_CHECK_MD_OP(exp, renew_capa);
2195         EXP_MD_COUNTER_INCREMENT(exp, renew_capa);
2196         rc = MDP(exp->exp_obd, renew_capa)(exp, ocapa, cb);
2197         RETURN(rc);
2198 }
2199
2200 static inline int md_unpack_capa(struct obd_export *exp,
2201                                  struct ptlrpc_request *req,
2202                                  const struct req_msg_field *field,
2203                                  struct obd_capa **oc)
2204 {
2205         int rc;
2206         ENTRY;
2207         EXP_CHECK_MD_OP(exp, unpack_capa);
2208         EXP_MD_COUNTER_INCREMENT(exp, unpack_capa);
2209         rc = MDP(exp->exp_obd, unpack_capa)(exp, req, field, oc);
2210         RETURN(rc);
2211 }
2212
2213 static inline int md_intent_getattr_async(struct obd_export *exp,
2214                                           struct md_enqueue_info *minfo,
2215                                           struct ldlm_enqueue_info *einfo)
2216 {
2217         int rc;
2218         ENTRY;
2219         EXP_CHECK_MD_OP(exp, intent_getattr_async);
2220         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
2221         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
2222         RETURN(rc);
2223 }
2224
2225 static inline int md_revalidate_lock(struct obd_export *exp,
2226                                      struct lookup_intent *it,
2227                                      struct lu_fid *fid, __u64 *bits)
2228 {
2229         int rc;
2230         ENTRY;
2231         EXP_CHECK_MD_OP(exp, revalidate_lock);
2232         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
2233         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
2234         RETURN(rc);
2235 }
2236
2237
2238 /* OBD Metadata Support */
2239
2240 extern int obd_init_caches(void);
2241 extern void obd_cleanup_caches(void);
2242
2243 /* support routines */
2244 extern struct kmem_cache *obdo_cachep;
2245
2246 #define OBDO_ALLOC(ptr)                                                       \
2247 do {                                                                          \
2248         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, GFP_NOFS);             \
2249 } while(0)
2250
2251 #define OBDO_FREE(ptr)                                                        \
2252 do {                                                                          \
2253         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
2254 } while(0)
2255
2256
2257 static inline void obdo2fid(struct obdo *oa, struct lu_fid *fid)
2258 {
2259         /* something here */
2260 }
2261
2262 static inline void fid2obdo(struct lu_fid *fid, struct obdo *oa)
2263 {
2264         /* something here */
2265 }
2266
2267 typedef int (*register_lwp_cb)(void *data);
2268
2269 struct lwp_register_item {
2270         struct obd_export **lri_exp;
2271         register_lwp_cb     lri_cb_func;
2272         void               *lri_cb_data;
2273         cfs_list_t          lri_list;
2274         char                lri_name[MTI_NAME_MAXLEN];
2275 };
2276
2277 /* I'm as embarrassed about this as you are.
2278  *
2279  * <shaver> // XXX do not look into _superhack with remaining eye
2280  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
2281 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
2282
2283 /* obd_mount.c */
2284 #ifdef HAVE_SERVER_SUPPORT
2285 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
2286                              register_lwp_cb cb_func, void *cb_data);
2287 void lustre_deregister_lwp_item(struct obd_export **exp);
2288 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
2289 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
2290 #endif /* HAVE_SERVER_SUPPORT */
2291
2292 /* sysctl.c */
2293 extern void obd_sysctl_init (void);
2294 extern void obd_sysctl_clean (void);
2295
2296 /* uuid.c  */
2297 typedef __u8 class_uuid_t[16];
2298 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
2299
2300 /* lustre_peer.c    */
2301 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
2302 int class_add_uuid(const char *uuid, __u64 nid);
2303 int class_del_uuid (const char *uuid);
2304 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
2305 void class_init_uuidlist(void);
2306 void class_exit_uuidlist(void);
2307
2308 /* prng.c */
2309 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
2310
2311 #ifdef __KERNEL__
2312 int server_name2index(const char *svname, __u32 *idx, const char **endptr);
2313 #else
2314 # define server_name2index(name, idx, ptr)      do {} while (0)
2315 #endif
2316
2317 #endif /* __LINUX_OBD_CLASS_H */