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