Whamcloud - gitweb
2692599c00919da01ea11a12823fccb2eecfd1a2
[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 *, bool enable_proc,
90                         struct lprocfs_seq_vars *module_vars,
91 #ifndef HAVE_ONLY_PROCFS_SEQ
92                         struct lprocfs_vars *,
93 #endif
94                         const char *nm, struct lu_device_type *ldt);
95 int class_unregister_type(const char *nm);
96
97 struct obd_device *class_newdev(const char *type_name, const char *name);
98 void class_release_dev(struct obd_device *obd);
99
100 int class_name2dev(const char *name);
101 struct obd_device *class_name2obd(const char *name);
102 int class_uuid2dev(struct obd_uuid *uuid);
103 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
104 void class_obd_list(void);
105 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
106                                           const char * typ_name,
107                                           struct obd_uuid *grp_uuid);
108 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid,
109                                            int *next);
110 struct obd_device * class_num2obd(int num);
111 int get_devices_count(void);
112
113 int class_notify_sptlrpc_conf(const char *fsname, int namelen);
114
115 char *obd_export_nid2str(struct obd_export *exp);
116
117 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid);
118 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid);
119 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep);
120
121 int obd_zombie_impexp_init(void);
122 void obd_zombie_impexp_stop(void);
123 void obd_zombie_impexp_cull(void);
124 void obd_zombie_barrier(void);
125 void obd_exports_barrier(struct obd_device *obd);
126 int kuc_len(int payload_len);
127 struct kuc_hdr * kuc_ptr(void *p);
128 int kuc_ispayload(void *p);
129 void *kuc_alloc(int payload_len, int transport, int type);
130 void kuc_free(void *p, int payload_len);
131 int obd_get_request_slot(struct client_obd *cli);
132 void obd_put_request_slot(struct client_obd *cli);
133 __u32 obd_get_max_rpcs_in_flight(struct client_obd *cli);
134 int obd_set_max_rpcs_in_flight(struct client_obd *cli, __u32 max);
135
136 struct llog_handle;
137 struct llog_rec_hdr;
138 typedef int (*llog_cb_t)(const struct lu_env *, struct llog_handle *,
139                          struct llog_rec_hdr *, void *);
140 /* obd_config.c */
141 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
142                                      const char *new_name);
143 int class_process_config(struct lustre_cfg *lcfg);
144 #ifndef HAVE_ONLY_PROCFS_SEQ
145 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
146                              struct lustre_cfg *lcfg, void *data);
147 #endif
148 int class_process_proc_seq_param(char *prefix, struct lprocfs_seq_vars *lvars,
149                                  struct lustre_cfg *lcfg, void *data);
150 int class_attach(struct lustre_cfg *lcfg);
151 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
152 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
153 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
154 struct obd_device *class_incref(struct obd_device *obd,
155                                 const char *scope, const void *source);
156 void class_decref(struct obd_device *obd,
157                   const char *scope, const void *source);
158 void dump_exports(struct obd_device *obd, int locks);
159 int class_config_llog_handler(const struct lu_env *env,
160                               struct llog_handle *handle,
161                               struct llog_rec_hdr *rec, void *data);
162 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg);
163 int class_add_uuid(const char *uuid, __u64 nid);
164
165 #define CFG_F_START     0x01   /* Set when we start updating from a log */
166 #define CFG_F_MARKER    0x02   /* We are within a maker */
167 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
168 #define CFG_F_COMPAT146 0x08   /* Allow old-style logs */
169 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
170
171 /* Passed as data param to class_config_parse_llog */
172 struct config_llog_instance {
173         char                    *cfg_obdname;
174         void                    *cfg_instance;
175         struct super_block      *cfg_sb;
176         struct obd_uuid          cfg_uuid;
177         llog_cb_t                cfg_callback;
178         int                      cfg_last_idx; /* for partial llog processing */
179         int                      cfg_flags;
180         __u32                    cfg_lwp_idx;
181 };
182 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
183                             char *name, struct config_llog_instance *cfg);
184 int class_config_dump_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
185                            char *name, struct config_llog_instance *cfg);
186
187 enum {
188         CONFIG_T_CONFIG  = 0,
189         CONFIG_T_SPTLRPC = 1,
190         CONFIG_T_RECOVER = 2,
191         CONFIG_T_PARAMS  = 3,
192         CONFIG_T_MAX     = 4
193 };
194
195 #define PARAMS_FILENAME "params"
196 #define LCTL_UPCALL     "lctl"
197
198 /* list of active configuration logs  */
199 struct config_llog_data {
200         struct ldlm_res_id          cld_resid;
201         struct config_llog_instance cld_cfg;
202         cfs_list_t                  cld_list_chain;
203         atomic_t                    cld_refcount;
204         struct config_llog_data    *cld_sptlrpc;/* depended sptlrpc log */
205         struct config_llog_data    *cld_params; /* common parameters log */
206         struct config_llog_data    *cld_recover;/* imperative recover log */
207         struct obd_export          *cld_mgcexp;
208         struct mutex                cld_lock;
209         int                         cld_type;
210         unsigned int                cld_stopping:1, /* we were told to stop
211                                                      * watching */
212                                     cld_lostlock:1; /* lock not requeued */
213         char                        cld_logname[0];
214 };
215
216 struct lustre_profile {
217         cfs_list_t       lp_list;
218         char            *lp_profile;
219         char            *lp_dt;
220         char            *lp_md;
221 };
222
223 struct lustre_profile *class_get_profile(const char * prof);
224 void class_del_profile(const char *prof);
225 void class_del_profiles(void);
226
227 #if LUSTRE_TRACKS_LOCK_EXP_REFS
228
229 void __class_export_add_lock_ref(struct obd_export *, struct ldlm_lock *);
230 void __class_export_del_lock_ref(struct obd_export *, struct ldlm_lock *);
231 extern void (*class_export_dump_hook)(struct obd_export *);
232
233 #else
234
235 #define __class_export_add_lock_ref(exp, lock)             do {} while(0)
236 #define __class_export_del_lock_ref(exp, lock)             do {} while(0)
237
238 #endif
239
240 #define class_export_rpc_inc(exp)                                       \
241 ({                                                                      \
242         atomic_inc(&(exp)->exp_rpc_count);                              \
243         CDEBUG(D_INFO, "RPC GETting export %p : new rpc_count %d\n",    \
244                (exp), atomic_read(&(exp)->exp_rpc_count));              \
245 })
246
247 #define class_export_rpc_dec(exp)                                       \
248 ({                                                                      \
249         LASSERT_ATOMIC_POS(&exp->exp_rpc_count);                        \
250         atomic_dec(&(exp)->exp_rpc_count);                              \
251         CDEBUG(D_INFO, "RPC PUTting export %p : new rpc_count %d\n",    \
252                (exp), atomic_read(&(exp)->exp_rpc_count));              \
253 })
254
255 #define class_export_lock_get(exp, lock)                                \
256 ({                                                                      \
257         atomic_inc(&(exp)->exp_locks_count);                            \
258         __class_export_add_lock_ref(exp, lock);                         \
259         CDEBUG(D_INFO, "lock GETting export %p : new locks_count %d\n", \
260                (exp), atomic_read(&(exp)->exp_locks_count));            \
261         class_export_get(exp);                                          \
262 })
263
264 #define class_export_lock_put(exp, lock)                                \
265 ({                                                                      \
266         LASSERT_ATOMIC_POS(&exp->exp_locks_count);                      \
267         atomic_dec(&(exp)->exp_locks_count);                            \
268         __class_export_del_lock_ref(exp, lock);                         \
269         CDEBUG(D_INFO, "lock PUTting export %p : new locks_count %d\n", \
270                (exp), atomic_read(&(exp)->exp_locks_count));            \
271         class_export_put(exp);                                          \
272 })
273
274 #define class_export_cb_get(exp)                                        \
275 ({                                                                      \
276         atomic_inc(&(exp)->exp_cb_count);                               \
277         CDEBUG(D_INFO, "callback GETting export %p : new cb_count %d\n",\
278                (exp), atomic_read(&(exp)->exp_cb_count));               \
279         class_export_get(exp);                                          \
280 })
281
282 #define class_export_cb_put(exp)                                        \
283 ({                                                                      \
284         LASSERT_ATOMIC_POS(&exp->exp_cb_count);                         \
285         atomic_dec(&(exp)->exp_cb_count);                               \
286         CDEBUG(D_INFO, "callback PUTting export %p : new cb_count %d\n",\
287                (exp), atomic_read(&(exp)->exp_cb_count));               \
288         class_export_put(exp);                                          \
289 })
290
291 /* genops.c */
292 struct obd_export *class_export_get(struct obd_export *exp);
293 void class_export_put(struct obd_export *exp);
294 struct obd_export *class_new_export(struct obd_device *obddev,
295                                     struct obd_uuid *cluuid);
296 void class_unlink_export(struct obd_export *exp);
297
298 struct obd_import *class_import_get(struct obd_import *);
299 void class_import_put(struct obd_import *);
300 struct obd_import *class_new_import(struct obd_device *obd);
301 void class_destroy_import(struct obd_import *exp);
302
303 struct obd_type *class_search_type(const char *name);
304 struct obd_type *class_get_type(const char *name);
305 void class_put_type(struct obd_type *type);
306 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
307                   struct obd_uuid *cluuid);
308 int class_disconnect(struct obd_export *exp);
309 void class_fail_export(struct obd_export *exp);
310 int class_connected_export(struct obd_export *exp);
311 void class_disconnect_exports(struct obd_device *obddev);
312 int class_manual_cleanup(struct obd_device *obd);
313 void class_disconnect_stale_exports(struct obd_device *,
314                                     int (*test_export)(struct obd_export *));
315 static inline enum obd_option exp_flags_from_obd(struct obd_device *obd)
316 {
317         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
318                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
319                 (obd->obd_abort_recovery ? OBD_OPT_ABORT_RECOV : 0) |
320                 0);
321 }
322
323 #ifdef HAVE_SERVER_SUPPORT
324 static inline struct lu_target *class_exp2tgt(struct obd_export *exp)
325 {
326         LASSERT(exp->exp_obd);
327         return exp->exp_obd->u.obt.obt_lut;
328 }
329
330 static inline struct lr_server_data *class_server_data(struct obd_device *obd)
331 {
332         LASSERT(obd->u.obt.obt_lut);
333         return &obd->u.obt.obt_lut->lut_lsd;
334 }
335 #endif
336
337 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid);
338 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj);
339 void obdo_from_iattr(struct obdo *oa, struct iattr *attr,
340                      unsigned int ia_valid);
341 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid);
342 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid);
343 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
344                   unsigned int valid);
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_create_async(struct obd_export *exp,
793                                    struct obd_info *oinfo,
794                                    struct lov_stripe_md **ea,
795                                    struct obd_trans_info *oti)
796 {
797         int rc;
798         ENTRY;
799
800         EXP_CHECK_DT_OP(exp, create_async);
801         EXP_COUNTER_INCREMENT(exp, create_async);
802
803         rc = OBP(exp->exp_obd, create_async)(exp, oinfo, ea, oti);
804         RETURN(rc);
805 }
806
807 static inline int obd_create(const struct lu_env *env, struct obd_export *exp,
808                              struct obdo *obdo, struct lov_stripe_md **ea,
809                              struct obd_trans_info *oti)
810 {
811         int rc;
812         ENTRY;
813
814         EXP_CHECK_DT_OP(exp, create);
815         EXP_COUNTER_INCREMENT(exp, create);
816
817         rc = OBP(exp->exp_obd, create)(env, exp, obdo, ea, oti);
818         RETURN(rc);
819 }
820
821 static inline int obd_destroy(const struct lu_env *env, struct obd_export *exp,
822                               struct obdo *obdo, struct lov_stripe_md *ea,
823                               struct obd_trans_info *oti,
824                               struct obd_export *md_exp, void *capa)
825 {
826         int rc;
827         ENTRY;
828
829         EXP_CHECK_DT_OP(exp, destroy);
830         EXP_COUNTER_INCREMENT(exp, destroy);
831
832         rc = OBP(exp->exp_obd, destroy)(env, exp, obdo, ea, oti, md_exp, capa);
833         RETURN(rc);
834 }
835
836 static inline int obd_getattr(const struct lu_env *env, struct obd_export *exp,
837                               struct obd_info *oinfo)
838 {
839         int rc;
840         ENTRY;
841
842         EXP_CHECK_DT_OP(exp, getattr);
843         EXP_COUNTER_INCREMENT(exp, getattr);
844
845         rc = OBP(exp->exp_obd, getattr)(env, exp, oinfo);
846         RETURN(rc);
847 }
848
849 static inline int obd_getattr_async(struct obd_export *exp,
850                                     struct obd_info *oinfo,
851                                     struct ptlrpc_request_set *set)
852 {
853         int rc;
854         ENTRY;
855
856         EXP_CHECK_DT_OP(exp, getattr_async);
857         EXP_COUNTER_INCREMENT(exp, getattr_async);
858
859         rc = OBP(exp->exp_obd, getattr_async)(exp, oinfo, set);
860         RETURN(rc);
861 }
862
863 static inline int obd_setattr(const struct lu_env *env, struct obd_export *exp,
864                               struct obd_info *oinfo,
865                               struct obd_trans_info *oti)
866 {
867         int rc;
868         ENTRY;
869
870         EXP_CHECK_DT_OP(exp, setattr);
871         EXP_COUNTER_INCREMENT(exp, setattr);
872
873         rc = OBP(exp->exp_obd, setattr)(env, exp, oinfo, oti);
874         RETURN(rc);
875 }
876
877 /* This performs all the requests set init/wait/destroy actions. */
878 static inline int obd_setattr_rqset(struct obd_export *exp,
879                                     struct obd_info *oinfo,
880                                     struct obd_trans_info *oti)
881 {
882         struct ptlrpc_request_set *set = NULL;
883         int rc;
884         ENTRY;
885
886         EXP_CHECK_DT_OP(exp, setattr_async);
887         EXP_COUNTER_INCREMENT(exp, setattr_async);
888
889         set =  ptlrpc_prep_set();
890         if (set == NULL)
891                 RETURN(-ENOMEM);
892
893         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
894         if (rc == 0)
895                 rc = ptlrpc_set_wait(set);
896         ptlrpc_set_destroy(set);
897         RETURN(rc);
898 }
899
900 /* This adds all the requests into @set if @set != NULL, otherwise
901    all requests are sent asynchronously without waiting for response. */
902 static inline int obd_setattr_async(struct obd_export *exp,
903                                     struct obd_info *oinfo,
904                                     struct obd_trans_info *oti,
905                                     struct ptlrpc_request_set *set)
906 {
907         int rc;
908         ENTRY;
909
910         EXP_CHECK_DT_OP(exp, setattr_async);
911         EXP_COUNTER_INCREMENT(exp, setattr_async);
912
913         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
914         RETURN(rc);
915 }
916
917 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
918                                int priority)
919 {
920         struct obd_device *obd = imp->imp_obd;
921         int rc;
922         ENTRY;
923
924         OBD_CHECK_DEV_ACTIVE(obd);
925         OBD_CHECK_DT_OP(obd, add_conn, -EOPNOTSUPP);
926         OBD_COUNTER_INCREMENT(obd, add_conn);
927
928         rc = OBP(obd, add_conn)(imp, uuid, priority);
929         RETURN(rc);
930 }
931
932 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
933 {
934         struct obd_device *obd = imp->imp_obd;
935         int rc;
936         ENTRY;
937
938         OBD_CHECK_DEV_ACTIVE(obd);
939         OBD_CHECK_DT_OP(obd, del_conn, -EOPNOTSUPP);
940         OBD_COUNTER_INCREMENT(obd, del_conn);
941
942         rc = OBP(obd, del_conn)(imp, uuid);
943         RETURN(rc);
944 }
945
946 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
947 {
948         struct obd_uuid *uuid;
949         ENTRY;
950
951         OBD_CHECK_DT_OP(exp->exp_obd, get_uuid, NULL);
952         EXP_COUNTER_INCREMENT(exp, get_uuid);
953
954         uuid = OBP(exp->exp_obd, get_uuid)(exp);
955         RETURN(uuid);
956 }
957
958 /** Create a new /a exp on device /a obd for the uuid /a cluuid
959  * @param exp New export handle
960  * @param d Connect data, supported flags are set, flags also understood
961  *    by obd are returned.
962  */
963 static inline int obd_connect(const struct lu_env *env,
964                               struct obd_export **exp,struct obd_device *obd,
965                               struct obd_uuid *cluuid,
966                               struct obd_connect_data *data,
967                               void *localdata)
968 {
969         int rc;
970         __u64 ocf = data ? data->ocd_connect_flags : 0; /* for post-condition
971                                                    * check */
972         ENTRY;
973
974         OBD_CHECK_DEV_ACTIVE(obd);
975         OBD_CHECK_DT_OP(obd, connect, -EOPNOTSUPP);
976         OBD_COUNTER_INCREMENT(obd, connect);
977
978         rc = OBP(obd, connect)(env, exp, obd, cluuid, data, localdata);
979         /* check that only subset is granted */
980         LASSERT(ergo(data != NULL, (data->ocd_connect_flags & ocf) ==
981                                     data->ocd_connect_flags));
982         RETURN(rc);
983 }
984
985 static inline int obd_reconnect(const struct lu_env *env,
986                                 struct obd_export *exp,
987                                 struct obd_device *obd,
988                                 struct obd_uuid *cluuid,
989                                 struct obd_connect_data *d,
990                                 void *localdata)
991 {
992         int rc;
993         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
994                                                    * check */
995
996         ENTRY;
997
998         OBD_CHECK_DEV_ACTIVE(obd);
999         OBD_CHECK_DT_OP(obd, reconnect, 0);
1000         OBD_COUNTER_INCREMENT(obd, reconnect);
1001
1002         rc = OBP(obd, reconnect)(env, exp, obd, cluuid, d, localdata);
1003         /* check that only subset is granted */
1004         LASSERT(ergo(d != NULL,
1005                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
1006         RETURN(rc);
1007 }
1008
1009 static inline int obd_disconnect(struct obd_export *exp)
1010 {
1011         int rc;
1012         ENTRY;
1013
1014         EXP_CHECK_DT_OP(exp, disconnect);
1015         EXP_COUNTER_INCREMENT(exp, disconnect);
1016
1017         rc = OBP(exp->exp_obd, disconnect)(exp);
1018         RETURN(rc);
1019 }
1020
1021 static inline int obd_fid_init(struct obd_device *obd, struct obd_export *exp,
1022                                enum lu_cli_type type)
1023 {
1024         int rc;
1025         ENTRY;
1026
1027         OBD_CHECK_DT_OP(obd, fid_init, 0);
1028         OBD_COUNTER_INCREMENT(obd, fid_init);
1029
1030         rc = OBP(obd, fid_init)(obd, exp, type);
1031         RETURN(rc);
1032 }
1033
1034 static inline int obd_fid_fini(struct obd_device *obd)
1035 {
1036         int rc;
1037         ENTRY;
1038
1039         OBD_CHECK_DT_OP(obd, fid_fini, 0);
1040         OBD_COUNTER_INCREMENT(obd, fid_fini);
1041
1042         rc = OBP(obd, fid_fini)(obd);
1043         RETURN(rc);
1044 }
1045
1046 static inline int obd_fid_alloc(const struct lu_env *env,
1047                                 struct obd_export *exp,
1048                                 struct lu_fid *fid,
1049                                 struct md_op_data *op_data)
1050 {
1051         int rc;
1052         ENTRY;
1053
1054         EXP_CHECK_DT_OP(exp, fid_alloc);
1055         EXP_COUNTER_INCREMENT(exp, fid_alloc);
1056
1057         rc = OBP(exp->exp_obd, fid_alloc)(env, exp, fid, op_data);
1058         RETURN(rc);
1059 }
1060
1061 static inline int obd_ping(const struct lu_env *env, struct obd_export *exp)
1062 {
1063         int rc;
1064         ENTRY;
1065
1066         OBD_CHECK_DT_OP(exp->exp_obd, ping, 0);
1067         EXP_COUNTER_INCREMENT(exp, ping);
1068
1069         rc = OBP(exp->exp_obd, ping)(env, exp);
1070         RETURN(rc);
1071 }
1072
1073 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
1074 {
1075         int rc;
1076         ENTRY;
1077
1078         OBD_CHECK_DT_OP(obd, pool_new, -EOPNOTSUPP);
1079         OBD_COUNTER_INCREMENT(obd, pool_new);
1080
1081         rc = OBP(obd, pool_new)(obd, poolname);
1082         RETURN(rc);
1083 }
1084
1085 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
1086 {
1087         int rc;
1088         ENTRY;
1089
1090         OBD_CHECK_DT_OP(obd, pool_del, -EOPNOTSUPP);
1091         OBD_COUNTER_INCREMENT(obd, pool_del);
1092
1093         rc = OBP(obd, pool_del)(obd, poolname);
1094         RETURN(rc);
1095 }
1096
1097 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
1098 {
1099         int rc;
1100         ENTRY;
1101
1102         OBD_CHECK_DT_OP(obd, pool_add, -EOPNOTSUPP);
1103         OBD_COUNTER_INCREMENT(obd, pool_add);
1104
1105         rc = OBP(obd, pool_add)(obd, poolname, ostname);
1106         RETURN(rc);
1107 }
1108
1109 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
1110 {
1111         int rc;
1112         ENTRY;
1113
1114         OBD_CHECK_DT_OP(obd, pool_rem, -EOPNOTSUPP);
1115         OBD_COUNTER_INCREMENT(obd, pool_rem);
1116
1117         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
1118         RETURN(rc);
1119 }
1120
1121 static inline void obd_getref(struct obd_device *obd)
1122 {
1123         ENTRY;
1124         if (OBT(obd) && OBP(obd, getref)) {
1125                 OBD_COUNTER_INCREMENT(obd, getref);
1126                 OBP(obd, getref)(obd);
1127         }
1128         EXIT;
1129 }
1130
1131 static inline void obd_putref(struct obd_device *obd)
1132 {
1133         ENTRY;
1134         if (OBT(obd) && OBP(obd, putref)) {
1135                 OBD_COUNTER_INCREMENT(obd, putref);
1136                 OBP(obd, putref)(obd);
1137         }
1138         EXIT;
1139 }
1140
1141 static inline int obd_init_export(struct obd_export *exp)
1142 {
1143         int rc = 0;
1144
1145         ENTRY;
1146         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1147             OBP((exp)->exp_obd, init_export))
1148                 rc = OBP(exp->exp_obd, init_export)(exp);
1149         RETURN(rc);
1150 }
1151
1152 static inline int obd_destroy_export(struct obd_export *exp)
1153 {
1154         ENTRY;
1155         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1156             OBP((exp)->exp_obd, destroy_export))
1157                 OBP(exp->exp_obd, destroy_export)(exp);
1158         RETURN(0);
1159 }
1160
1161 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1162  * If the cache is older than @max_age we will get a new value from the
1163  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1164 static inline int obd_statfs_async(struct obd_export *exp,
1165                                    struct obd_info *oinfo,
1166                                    __u64 max_age,
1167                                    struct ptlrpc_request_set *rqset)
1168 {
1169         int rc = 0;
1170         struct obd_device *obd;
1171         ENTRY;
1172
1173         if (exp == NULL || exp->exp_obd == NULL)
1174                 RETURN(-EINVAL);
1175
1176         obd = exp->exp_obd;
1177         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1178         OBD_COUNTER_INCREMENT(obd, statfs);
1179
1180         CDEBUG(D_SUPER, "%s: osfs %p age "LPU64", max_age "LPU64"\n",
1181                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
1182         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1183                 rc = OBP(obd, statfs_async)(exp, oinfo, max_age, rqset);
1184         } else {
1185                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
1186                        " objects "LPU64"/"LPU64"\n",
1187                        obd->obd_name, &obd->obd_osfs,
1188                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1189                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1190                 spin_lock(&obd->obd_osfs_lock);
1191                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1192                 spin_unlock(&obd->obd_osfs_lock);
1193                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1194                 if (oinfo->oi_cb_up)
1195                         oinfo->oi_cb_up(oinfo, 0);
1196         }
1197         RETURN(rc);
1198 }
1199
1200 static inline int obd_statfs_rqset(struct obd_export *exp,
1201                                    struct obd_statfs *osfs, __u64 max_age,
1202                                    __u32 flags)
1203 {
1204         struct ptlrpc_request_set *set = NULL;
1205         struct obd_info oinfo = { { { 0 } } };
1206         int rc = 0;
1207         ENTRY;
1208
1209         set =  ptlrpc_prep_set();
1210         if (set == NULL)
1211                 RETURN(-ENOMEM);
1212
1213         oinfo.oi_osfs = osfs;
1214         oinfo.oi_flags = flags;
1215         rc = obd_statfs_async(exp, &oinfo, max_age, set);
1216         if (rc == 0)
1217                 rc = ptlrpc_set_wait(set);
1218         ptlrpc_set_destroy(set);
1219         RETURN(rc);
1220 }
1221
1222 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1223  * If the cache is older than @max_age we will get a new value from the
1224  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1225 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1226                              struct obd_statfs *osfs, __u64 max_age,
1227                              __u32 flags)
1228 {
1229         int rc = 0;
1230         struct obd_device *obd = exp->exp_obd;
1231         ENTRY;
1232
1233         if (obd == NULL)
1234                 RETURN(-EINVAL);
1235
1236         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1237         OBD_COUNTER_INCREMENT(obd, statfs);
1238
1239         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
1240                obd->obd_osfs_age, max_age);
1241         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1242                 rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1243                 if (rc == 0) {
1244                         spin_lock(&obd->obd_osfs_lock);
1245                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1246                         obd->obd_osfs_age = cfs_time_current_64();
1247                         spin_unlock(&obd->obd_osfs_lock);
1248                 }
1249         } else {
1250                 CDEBUG(D_SUPER, "%s: use %p cache blocks "LPU64"/"LPU64
1251                        " objects "LPU64"/"LPU64"\n",
1252                        obd->obd_name, &obd->obd_osfs,
1253                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1254                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1255                 spin_lock(&obd->obd_osfs_lock);
1256                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1257                 spin_unlock(&obd->obd_osfs_lock);
1258         }
1259         RETURN(rc);
1260 }
1261
1262 static inline int obd_preprw(const struct lu_env *env, int cmd,
1263                              struct obd_export *exp, struct obdo *oa,
1264                              int objcount, struct obd_ioobj *obj,
1265                              struct niobuf_remote *remote, int *pages,
1266                              struct niobuf_local *local,
1267                              struct obd_trans_info *oti,
1268                              struct lustre_capa *capa)
1269 {
1270         int rc;
1271         ENTRY;
1272
1273         EXP_CHECK_DT_OP(exp, preprw);
1274         EXP_COUNTER_INCREMENT(exp, preprw);
1275
1276         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1277                                        pages, local, oti, capa);
1278         RETURN(rc);
1279 }
1280
1281 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1282                                struct obd_export *exp, struct obdo *oa,
1283                                int objcount, struct obd_ioobj *obj,
1284                                struct niobuf_remote *rnb, int pages,
1285                                struct niobuf_local *local,
1286                                struct obd_trans_info *oti, int rc)
1287 {
1288         ENTRY;
1289
1290         EXP_CHECK_DT_OP(exp, commitrw);
1291         EXP_COUNTER_INCREMENT(exp, commitrw);
1292
1293         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1294                                          rnb, pages, local, oti, rc);
1295         RETURN(rc);
1296 }
1297
1298 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1299                                 int len, void *karg, void *uarg)
1300 {
1301         int rc;
1302         ENTRY;
1303
1304         EXP_CHECK_DT_OP(exp, iocontrol);
1305         EXP_COUNTER_INCREMENT(exp, iocontrol);
1306
1307         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1308         RETURN(rc);
1309 }
1310
1311 static inline int obd_change_cbdata(struct obd_export *exp,
1312                                     struct lov_stripe_md *lsm,
1313                                     ldlm_iterator_t it, void *data)
1314 {
1315         int rc;
1316         ENTRY;
1317
1318         EXP_CHECK_DT_OP(exp, change_cbdata);
1319         EXP_COUNTER_INCREMENT(exp, change_cbdata);
1320
1321         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1322         RETURN(rc);
1323 }
1324
1325 static inline int obd_find_cbdata(struct obd_export *exp,
1326                                   struct lov_stripe_md *lsm,
1327                                   ldlm_iterator_t it, void *data)
1328 {
1329         int rc;
1330         ENTRY;
1331
1332         EXP_CHECK_DT_OP(exp, find_cbdata);
1333         EXP_COUNTER_INCREMENT(exp, find_cbdata);
1334
1335         rc = OBP(exp->exp_obd, find_cbdata)(exp, lsm, it, data);
1336         RETURN(rc);
1337 }
1338
1339 static inline int obd_pin(struct obd_export *exp, const struct lu_fid *fid,
1340                           struct obd_capa *oc, struct obd_client_handle *handle,
1341                           int flag)
1342 {
1343         int rc;
1344         ENTRY;
1345
1346         EXP_CHECK_DT_OP(exp, pin);
1347         EXP_COUNTER_INCREMENT(exp, pin);
1348
1349         rc = OBP(exp->exp_obd, pin)(exp, fid, oc, handle, flag);
1350         RETURN(rc);
1351 }
1352
1353 static inline int obd_unpin(struct obd_export *exp,
1354                             struct obd_client_handle *handle, int flag)
1355 {
1356         int rc;
1357         ENTRY;
1358
1359         EXP_CHECK_DT_OP(exp, unpin);
1360         EXP_COUNTER_INCREMENT(exp, unpin);
1361
1362         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
1363         RETURN(rc);
1364 }
1365
1366
1367 static inline void obd_import_event(struct obd_device *obd,
1368                                     struct obd_import *imp,
1369                                     enum obd_import_event event)
1370 {
1371         ENTRY;
1372         if (!obd) {
1373                 CERROR("NULL device\n");
1374                 EXIT;
1375                 return;
1376         }
1377         if (obd->obd_set_up && OBP(obd, import_event)) {
1378                 OBD_COUNTER_INCREMENT(obd, import_event);
1379                 OBP(obd, import_event)(obd, imp, event);
1380         }
1381         EXIT;
1382 }
1383
1384 static inline int obd_llog_connect(struct obd_export *exp,
1385                                    struct llogd_conn_body *body)
1386 {
1387         int rc;
1388         ENTRY;
1389
1390         OBD_CHECK_DT_OP(exp->exp_obd, llog_connect, 0);
1391         EXP_COUNTER_INCREMENT(exp, llog_connect);
1392
1393         rc = OBP(exp->exp_obd, llog_connect)(exp, body);
1394         RETURN(rc);
1395 }
1396
1397
1398 static inline int obd_notify(struct obd_device *obd,
1399                              struct obd_device *watched,
1400                              enum obd_notify_event ev,
1401                              void *data)
1402 {
1403         int rc;
1404         ENTRY;
1405         OBD_CHECK_DEV(obd);
1406
1407         /* the check for async_recov is a complete hack - I'm hereby
1408            overloading the meaning to also mean "this was called from
1409            mds_postsetup".  I know that my mds is able to handle notifies
1410            by this point, and it needs to get them to execute mds_postrecov. */
1411         if (!obd->obd_set_up && !obd->obd_async_recov) {
1412                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1413                 RETURN(-EINVAL);
1414         }
1415
1416         if (!OBP(obd, notify)) {
1417                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1418                 RETURN(-ENOSYS);
1419         }
1420
1421         OBD_COUNTER_INCREMENT(obd, notify);
1422         rc = OBP(obd, notify)(obd, watched, ev, data);
1423         RETURN(rc);
1424 }
1425
1426 static inline int obd_notify_observer(struct obd_device *observer,
1427                                       struct obd_device *observed,
1428                                       enum obd_notify_event ev,
1429                                       void *data)
1430 {
1431         int rc1;
1432         int rc2;
1433
1434         struct obd_notify_upcall *onu;
1435
1436         if (observer->obd_observer)
1437                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1438         else
1439                 rc1 = 0;
1440         /*
1441          * Also, call non-obd listener, if any
1442          */
1443         onu = &observer->obd_upcall;
1444         if (onu->onu_upcall != NULL)
1445                 rc2 = onu->onu_upcall(observer, observed, ev,
1446                                       onu->onu_owner, NULL);
1447         else
1448                 rc2 = 0;
1449
1450         return rc1 ? rc1 : rc2;
1451 }
1452
1453 static inline int obd_quotacheck(struct obd_export *exp,
1454                                  struct obd_quotactl *oqctl)
1455 {
1456         int rc;
1457         ENTRY;
1458
1459         EXP_CHECK_DT_OP(exp, quotacheck);
1460         EXP_COUNTER_INCREMENT(exp, quotacheck);
1461
1462         rc = OBP(exp->exp_obd, quotacheck)(exp->exp_obd, exp, oqctl);
1463         RETURN(rc);
1464 }
1465
1466 static inline int obd_quotactl(struct obd_export *exp,
1467                                struct obd_quotactl *oqctl)
1468 {
1469         int rc;
1470         ENTRY;
1471
1472         EXP_CHECK_DT_OP(exp, quotactl);
1473         EXP_COUNTER_INCREMENT(exp, quotactl);
1474
1475         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1476         RETURN(rc);
1477 }
1478
1479 static inline int obd_health_check(const struct lu_env *env,
1480                                    struct obd_device *obd)
1481 {
1482         /* returns: 0 on healthy
1483          *         >0 on unhealthy + reason code/flag
1484          *            however the only suppored reason == 1 right now
1485          *            We'll need to define some better reasons
1486          *            or flags in the future.
1487          *         <0 on error
1488          */
1489         int rc;
1490         ENTRY;
1491
1492         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1493         if (obd == NULL || !OBT(obd)) {
1494                 CERROR("cleaned up obd\n");
1495                 RETURN(-EOPNOTSUPP);
1496         }
1497         if (!obd->obd_set_up || obd->obd_stopping)
1498                 RETURN(0);
1499         if (!OBP(obd, health_check))
1500                 RETURN(0);
1501
1502         rc = OBP(obd, health_check)(env, obd);
1503         RETURN(rc);
1504 }
1505
1506 static inline int obd_register_observer(struct obd_device *obd,
1507                                         struct obd_device *observer)
1508 {
1509         ENTRY;
1510         OBD_CHECK_DEV(obd);
1511         down_write(&obd->obd_observer_link_sem);
1512         if (obd->obd_observer && observer) {
1513                 up_write(&obd->obd_observer_link_sem);
1514                 RETURN(-EALREADY);
1515         }
1516         obd->obd_observer = observer;
1517         up_write(&obd->obd_observer_link_sem);
1518         RETURN(0);
1519 }
1520
1521 static inline int obd_pin_observer(struct obd_device *obd,
1522                                    struct obd_device **observer)
1523 {
1524         ENTRY;
1525         down_read(&obd->obd_observer_link_sem);
1526         if (!obd->obd_observer) {
1527                 *observer = NULL;
1528                 up_read(&obd->obd_observer_link_sem);
1529                 RETURN(-ENOENT);
1530         }
1531         *observer = obd->obd_observer;
1532         RETURN(0);
1533 }
1534
1535 static inline int obd_unpin_observer(struct obd_device *obd)
1536 {
1537         ENTRY;
1538         up_read(&obd->obd_observer_link_sem);
1539         RETURN(0);
1540 }
1541
1542 #if 0
1543 static inline int obd_register_page_removal_cb(struct obd_export *exp,
1544                                                obd_page_removal_cb_t cb,
1545                                                obd_pin_extent_cb pin_cb)
1546 {
1547         int rc;
1548         ENTRY;
1549
1550         OBD_CHECK_DT_OP(exp->exp_obd, register_page_removal_cb, 0);
1551         OBD_COUNTER_INCREMENT(exp->exp_obd, register_page_removal_cb);
1552
1553         rc = OBP(exp->exp_obd, register_page_removal_cb)(exp, cb, pin_cb);
1554         RETURN(rc);
1555 }
1556
1557 static inline int obd_unregister_page_removal_cb(struct obd_export *exp,
1558                                                  obd_page_removal_cb_t cb)
1559 {
1560         int rc;
1561         ENTRY;
1562
1563         OBD_CHECK_DT_OP(exp->exp_obd, unregister_page_removal_cb, 0);
1564         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_page_removal_cb);
1565
1566         rc = OBP(exp->exp_obd, unregister_page_removal_cb)(exp, cb);
1567         RETURN(rc);
1568 }
1569
1570 static inline int obd_register_lock_cancel_cb(struct obd_export *exp,
1571                                               obd_lock_cancel_cb cb)
1572 {
1573         int rc;
1574         ENTRY;
1575
1576         OBD_CHECK_DT_OP(exp->exp_obd, register_lock_cancel_cb, 0);
1577         OBD_COUNTER_INCREMENT(exp->exp_obd, register_lock_cancel_cb);
1578
1579         rc = OBP(exp->exp_obd, register_lock_cancel_cb)(exp, cb);
1580         RETURN(rc);
1581 }
1582
1583 static inline int obd_unregister_lock_cancel_cb(struct obd_export *exp,
1584                                                  obd_lock_cancel_cb cb)
1585 {
1586         int rc;
1587         ENTRY;
1588
1589         OBD_CHECK_DT_OP(exp->exp_obd, unregister_lock_cancel_cb, 0);
1590         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_lock_cancel_cb);
1591
1592         rc = OBP(exp->exp_obd, unregister_lock_cancel_cb)(exp, cb);
1593         RETURN(rc);
1594 }
1595 #endif
1596
1597 /* metadata helpers */
1598 static inline int md_getstatus(struct obd_export *exp,
1599                                struct lu_fid *fid, struct obd_capa **pc)
1600 {
1601         int rc;
1602         ENTRY;
1603
1604         EXP_CHECK_MD_OP(exp, getstatus);
1605         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1606         rc = MDP(exp->exp_obd, getstatus)(exp, fid, pc);
1607         RETURN(rc);
1608 }
1609
1610 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1611                              struct ptlrpc_request **request)
1612 {
1613         int rc;
1614         ENTRY;
1615         EXP_CHECK_MD_OP(exp, getattr);
1616         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1617         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1618         RETURN(rc);
1619 }
1620
1621 static inline int md_null_inode(struct obd_export *exp,
1622                                    const struct lu_fid *fid)
1623 {
1624         int rc;
1625         ENTRY;
1626         EXP_CHECK_MD_OP(exp, null_inode);
1627         EXP_MD_COUNTER_INCREMENT(exp, null_inode);
1628         rc = MDP(exp->exp_obd, null_inode)(exp, fid);
1629         RETURN(rc);
1630 }
1631
1632 static inline int md_find_cbdata(struct obd_export *exp,
1633                                  const struct lu_fid *fid,
1634                                  ldlm_iterator_t it, void *data)
1635 {
1636         int rc;
1637         ENTRY;
1638         EXP_CHECK_MD_OP(exp, find_cbdata);
1639         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1640         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1641         RETURN(rc);
1642 }
1643
1644 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1645                            struct md_open_data *mod,
1646                            struct ptlrpc_request **request)
1647 {
1648         int rc;
1649         ENTRY;
1650         EXP_CHECK_MD_OP(exp, close);
1651         EXP_MD_COUNTER_INCREMENT(exp, close);
1652         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1653         RETURN(rc);
1654 }
1655
1656 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1657                             const void *data, int datalen, int mode, __u32 uid,
1658                             __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1659                             struct ptlrpc_request **request)
1660 {
1661         int rc;
1662         ENTRY;
1663         EXP_CHECK_MD_OP(exp, create);
1664         EXP_MD_COUNTER_INCREMENT(exp, create);
1665         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1666                                        uid, gid, cap_effective, rdev, request);
1667         RETURN(rc);
1668 }
1669
1670 static inline int md_done_writing(struct obd_export *exp,
1671                                   struct md_op_data *op_data,
1672                                   struct md_open_data *mod)
1673 {
1674         int rc;
1675         ENTRY;
1676         EXP_CHECK_MD_OP(exp, done_writing);
1677         EXP_MD_COUNTER_INCREMENT(exp, done_writing);
1678         rc = MDP(exp->exp_obd, done_writing)(exp, op_data, mod);
1679         RETURN(rc);
1680 }
1681
1682 static inline int md_enqueue(struct obd_export *exp,
1683                              struct ldlm_enqueue_info *einfo,
1684                              const union ldlm_policy_data *policy,
1685                              struct lookup_intent *it,
1686                              struct md_op_data *op_data,
1687                              struct lustre_handle *lockh,
1688                              __u64 extra_lock_flags)
1689 {
1690         int rc;
1691         ENTRY;
1692         EXP_CHECK_MD_OP(exp, enqueue);
1693         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1694         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, policy, it, op_data, lockh,
1695                                         extra_lock_flags);
1696         RETURN(rc);
1697 }
1698
1699 static inline int md_getattr_name(struct obd_export *exp,
1700                                   struct md_op_data *op_data,
1701                                   struct ptlrpc_request **request)
1702 {
1703         int rc;
1704         ENTRY;
1705         EXP_CHECK_MD_OP(exp, getattr_name);
1706         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1707         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1708         RETURN(rc);
1709 }
1710
1711 static inline int md_intent_lock(struct obd_export *exp,
1712                                  struct md_op_data *op_data,
1713                                  struct lookup_intent *it,
1714                                  struct ptlrpc_request **reqp,
1715                                  ldlm_blocking_callback cb_blocking,
1716                                  __u64 extra_lock_flags)
1717 {
1718         int rc;
1719         ENTRY;
1720         EXP_CHECK_MD_OP(exp, intent_lock);
1721         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1722         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, it, reqp, cb_blocking,
1723                                             extra_lock_flags);
1724         RETURN(rc);
1725 }
1726
1727 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1728                           struct ptlrpc_request **request)
1729 {
1730         int rc;
1731         ENTRY;
1732         EXP_CHECK_MD_OP(exp, link);
1733         EXP_MD_COUNTER_INCREMENT(exp, link);
1734         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1735         RETURN(rc);
1736 }
1737
1738 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1739                             const char *old, int oldlen, const char *new,
1740                             int newlen, struct ptlrpc_request **request)
1741 {
1742         int rc;
1743         ENTRY;
1744         EXP_CHECK_MD_OP(exp, rename);
1745         EXP_MD_COUNTER_INCREMENT(exp, rename);
1746         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1747                                        newlen, request);
1748         RETURN(rc);
1749 }
1750
1751 static inline int md_is_subdir(struct obd_export *exp,
1752                                const struct lu_fid *pfid,
1753                                const struct lu_fid *cfid,
1754                                struct ptlrpc_request **request)
1755 {
1756         int rc;
1757         ENTRY;
1758         EXP_CHECK_MD_OP(exp, is_subdir);
1759         EXP_MD_COUNTER_INCREMENT(exp, is_subdir);
1760         rc = MDP(exp->exp_obd, is_subdir)(exp, pfid, cfid, request);
1761         RETURN(rc);
1762 }
1763
1764 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1765                              void *ea, int ealen, void *ea2, int ea2len,
1766                              struct ptlrpc_request **request,
1767                              struct md_open_data **mod)
1768 {
1769         int rc;
1770         ENTRY;
1771         EXP_CHECK_MD_OP(exp, setattr);
1772         EXP_MD_COUNTER_INCREMENT(exp, setattr);
1773         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen,
1774                                         ea2, ea2len, request, mod);
1775         RETURN(rc);
1776 }
1777
1778 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
1779                            struct obd_capa *oc, struct ptlrpc_request **request)
1780 {
1781         int rc;
1782         ENTRY;
1783         EXP_CHECK_MD_OP(exp, fsync);
1784         EXP_MD_COUNTER_INCREMENT(exp, fsync);
1785         rc = MDP(exp->exp_obd, fsync)(exp, fid, oc, request);
1786         RETURN(rc);
1787 }
1788
1789 static inline int md_readpage(struct obd_export *exp, struct md_op_data *opdata,
1790                               struct page **pages,
1791                               struct ptlrpc_request **request)
1792 {
1793         int rc;
1794         ENTRY;
1795         EXP_CHECK_MD_OP(exp, readpage);
1796         EXP_MD_COUNTER_INCREMENT(exp, readpage);
1797         rc = MDP(exp->exp_obd, readpage)(exp, opdata, pages, request);
1798         RETURN(rc);
1799 }
1800
1801 static inline int md_read_entry(struct obd_export *exp,
1802                                 struct md_op_data *op_data,
1803                                 struct md_callback *cb_op,
1804                                 struct lu_dirent **ld,
1805                                 struct page **ppage)
1806 {
1807         int rc;
1808         ENTRY;
1809         EXP_CHECK_MD_OP(exp, read_entry);
1810         EXP_MD_COUNTER_INCREMENT(exp, read_entry);
1811         rc = MDP(exp->exp_obd, read_entry)(exp, op_data, cb_op, ld, ppage);
1812         RETURN(rc);
1813 }
1814
1815 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1816                             struct ptlrpc_request **request)
1817 {
1818         int rc;
1819         ENTRY;
1820         EXP_CHECK_MD_OP(exp, unlink);
1821         EXP_MD_COUNTER_INCREMENT(exp, unlink);
1822         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
1823         RETURN(rc);
1824 }
1825
1826 static inline int md_get_lustre_md(struct obd_export *exp,
1827                                    struct ptlrpc_request *req,
1828                                    struct obd_export *dt_exp,
1829                                    struct obd_export *md_exp,
1830                                    struct lustre_md *md)
1831 {
1832         ENTRY;
1833         EXP_CHECK_MD_OP(exp, get_lustre_md);
1834         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
1835         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
1836 }
1837
1838 static inline int md_free_lustre_md(struct obd_export *exp,
1839                                     struct lustre_md *md)
1840 {
1841         ENTRY;
1842         EXP_CHECK_MD_OP(exp, free_lustre_md);
1843         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
1844         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
1845 }
1846
1847 static inline int md_update_lsm_md(struct obd_export *exp,
1848                                    struct lmv_stripe_md *lsm,
1849                                    struct mdt_body *body,
1850                                    ldlm_blocking_callback cb)
1851 {
1852         ENTRY;
1853         EXP_CHECK_MD_OP(exp, update_lsm_md);
1854         EXP_MD_COUNTER_INCREMENT(exp, update_lsm_md);
1855         RETURN(MDP(exp->exp_obd, update_lsm_md)(exp, lsm, body, cb));
1856 }
1857
1858 static inline int md_merge_attr(struct obd_export *exp,
1859                                 const struct lmv_stripe_md *lsm,
1860                                 struct cl_attr *attr)
1861 {
1862         ENTRY;
1863         EXP_CHECK_MD_OP(exp, merge_attr);
1864         EXP_MD_COUNTER_INCREMENT(exp, merge_attr);
1865         RETURN(MDP(exp->exp_obd, merge_attr)(exp, lsm, attr));
1866 }
1867
1868 static inline int md_setxattr(struct obd_export *exp,
1869                               const struct lu_fid *fid, struct obd_capa *oc,
1870                               obd_valid valid, const char *name,
1871                               const char *input, int input_size,
1872                               int output_size, int flags, __u32 suppgid,
1873                               struct ptlrpc_request **request)
1874 {
1875         ENTRY;
1876         EXP_CHECK_MD_OP(exp, setxattr);
1877         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
1878         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, oc, valid, name, input,
1879                                            input_size, output_size, flags,
1880                                            suppgid, request));
1881 }
1882
1883 static inline int md_getxattr(struct obd_export *exp,
1884                               const struct lu_fid *fid, struct obd_capa *oc,
1885                               obd_valid valid, const char *name,
1886                               const char *input, int input_size,
1887                               int output_size, int flags,
1888                               struct ptlrpc_request **request)
1889 {
1890         ENTRY;
1891         EXP_CHECK_MD_OP(exp, getxattr);
1892         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
1893         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, oc, valid, name, input,
1894                                            input_size, output_size, flags,
1895                                            request));
1896 }
1897
1898 static inline int md_set_open_replay_data(struct obd_export *exp,
1899                                           struct obd_client_handle *och,
1900                                           struct lookup_intent *it)
1901 {
1902         ENTRY;
1903         EXP_CHECK_MD_OP(exp, set_open_replay_data);
1904         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
1905         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, it));
1906 }
1907
1908 static inline int md_clear_open_replay_data(struct obd_export *exp,
1909                                             struct obd_client_handle *och)
1910 {
1911         ENTRY;
1912         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
1913         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
1914         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
1915 }
1916
1917 static inline int md_set_lock_data(struct obd_export *exp,
1918                                    __u64 *lockh, void *data, __u64 *bits)
1919 {
1920         ENTRY;
1921         EXP_CHECK_MD_OP(exp, set_lock_data);
1922         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
1923         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
1924 }
1925
1926 static inline int md_cancel_unused(struct obd_export *exp,
1927                                    const struct lu_fid *fid,
1928                                    ldlm_policy_data_t *policy,
1929                                    ldlm_mode_t mode,
1930                                    ldlm_cancel_flags_t flags,
1931                                    void *opaque)
1932 {
1933         int rc;
1934         ENTRY;
1935
1936         EXP_CHECK_MD_OP(exp, cancel_unused);
1937         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
1938
1939         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
1940                                               flags, opaque);
1941         RETURN(rc);
1942 }
1943
1944 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, __u64 flags,
1945                                         const struct lu_fid *fid,
1946                                         ldlm_type_t type,
1947                                         ldlm_policy_data_t *policy,
1948                                         ldlm_mode_t mode,
1949                                         struct lustre_handle *lockh)
1950 {
1951         ENTRY;
1952         EXP_CHECK_MD_OP(exp, lock_match);
1953         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
1954         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1955                                              policy, mode, lockh));
1956 }
1957
1958 static inline int md_init_ea_size(struct obd_export *exp, int easize,
1959                                   int def_asize, int cookiesize,
1960                                   int def_cookiesize)
1961 {
1962         ENTRY;
1963         EXP_CHECK_MD_OP(exp, init_ea_size);
1964         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
1965         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, easize, def_asize,
1966                                                cookiesize, def_cookiesize));
1967 }
1968
1969 static inline int md_get_remote_perm(struct obd_export *exp,
1970                                      const struct lu_fid *fid,
1971                                      struct obd_capa *oc, __u32 suppgid,
1972                                      struct ptlrpc_request **request)
1973 {
1974         ENTRY;
1975         EXP_CHECK_MD_OP(exp, get_remote_perm);
1976         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
1977         RETURN(MDP(exp->exp_obd, get_remote_perm)(exp, fid, oc, suppgid,
1978                                                   request));
1979 }
1980
1981 static inline int md_renew_capa(struct obd_export *exp, struct obd_capa *ocapa,
1982                                 renew_capa_cb_t cb)
1983 {
1984         int rc;
1985         ENTRY;
1986         EXP_CHECK_MD_OP(exp, renew_capa);
1987         EXP_MD_COUNTER_INCREMENT(exp, renew_capa);
1988         rc = MDP(exp->exp_obd, renew_capa)(exp, ocapa, cb);
1989         RETURN(rc);
1990 }
1991
1992 static inline int md_unpack_capa(struct obd_export *exp,
1993                                  struct ptlrpc_request *req,
1994                                  const struct req_msg_field *field,
1995                                  struct obd_capa **oc)
1996 {
1997         int rc;
1998         ENTRY;
1999         EXP_CHECK_MD_OP(exp, unpack_capa);
2000         EXP_MD_COUNTER_INCREMENT(exp, unpack_capa);
2001         rc = MDP(exp->exp_obd, unpack_capa)(exp, req, field, oc);
2002         RETURN(rc);
2003 }
2004
2005 static inline int md_intent_getattr_async(struct obd_export *exp,
2006                                           struct md_enqueue_info *minfo,
2007                                           struct ldlm_enqueue_info *einfo)
2008 {
2009         int rc;
2010         ENTRY;
2011         EXP_CHECK_MD_OP(exp, intent_getattr_async);
2012         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
2013         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
2014         RETURN(rc);
2015 }
2016
2017 static inline int md_revalidate_lock(struct obd_export *exp,
2018                                      struct lookup_intent *it,
2019                                      struct lu_fid *fid, __u64 *bits)
2020 {
2021         int rc;
2022         ENTRY;
2023         EXP_CHECK_MD_OP(exp, revalidate_lock);
2024         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
2025         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
2026         RETURN(rc);
2027 }
2028
2029
2030 /* OBD Metadata Support */
2031
2032 extern int obd_init_caches(void);
2033 extern void obd_cleanup_caches(void);
2034
2035 /* support routines */
2036 extern struct kmem_cache *obdo_cachep;
2037
2038 #define OBDO_ALLOC(ptr)                                                       \
2039 do {                                                                          \
2040         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, GFP_NOFS);             \
2041 } while(0)
2042
2043 #define OBDO_FREE(ptr)                                                        \
2044 do {                                                                          \
2045         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
2046 } while(0)
2047
2048
2049 static inline void obdo2fid(struct obdo *oa, struct lu_fid *fid)
2050 {
2051         /* something here */
2052 }
2053
2054 static inline void fid2obdo(struct lu_fid *fid, struct obdo *oa)
2055 {
2056         /* something here */
2057 }
2058
2059 typedef int (*register_lwp_cb)(void *data);
2060
2061 struct lwp_register_item {
2062         struct obd_export **lri_exp;
2063         register_lwp_cb     lri_cb_func;
2064         void               *lri_cb_data;
2065         cfs_list_t          lri_list;
2066         char                lri_name[MTI_NAME_MAXLEN];
2067 };
2068
2069 /* I'm as embarrassed about this as you are.
2070  *
2071  * <shaver> // XXX do not look into _superhack with remaining eye
2072  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
2073 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
2074
2075 /* obd_mount.c */
2076 #ifdef HAVE_SERVER_SUPPORT
2077 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
2078                              register_lwp_cb cb_func, void *cb_data);
2079 void lustre_deregister_lwp_item(struct obd_export **exp);
2080 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
2081 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
2082 #endif /* HAVE_SERVER_SUPPORT */
2083
2084 /* sysctl.c */
2085 extern void obd_sysctl_init (void);
2086 extern void obd_sysctl_clean (void);
2087
2088 /* uuid.c  */
2089 typedef __u8 class_uuid_t[16];
2090 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
2091
2092 /* lustre_peer.c    */
2093 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
2094 int class_add_uuid(const char *uuid, __u64 nid);
2095 int class_del_uuid (const char *uuid);
2096 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
2097 void class_init_uuidlist(void);
2098 void class_exit_uuidlist(void);
2099
2100 /* prng.c */
2101 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
2102
2103 /* root squash info */
2104 struct rw_semaphore;
2105 struct root_squash_info {
2106         uid_t                   rsi_uid;
2107         gid_t                   rsi_gid;
2108         struct list_head        rsi_nosquash_nids;
2109         struct rw_semaphore     rsi_sem;
2110 };
2111
2112 #ifdef __KERNEL__
2113 int server_name2index(const char *svname, __u32 *idx, const char **endptr);
2114 #else
2115 # define server_name2index(name, idx, ptr)      do {} while (0)
2116 #endif
2117
2118 #endif /* __LINUX_OBD_CLASS_H */