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