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