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