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