These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / lustre / 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 #include "obd_support.h"
40 #include "lustre_import.h"
41 #include "lustre_net.h"
42 #include "obd.h"
43 #include "lustre_lib.h"
44 #include "lustre/lustre_idl.h"
45 #include "lprocfs_status.h"
46
47 #define OBD_STATFS_NODELAY      0x0001  /* requests should be send without delay
48                                          * and resends for avoid deadlocks */
49 #define OBD_STATFS_FROM_CACHE   0x0002  /* the statfs callback should not update
50                                          * obd_osfs_age */
51 #define OBD_STATFS_PTLRPCD      0x0004  /* requests will be sent via ptlrpcd
52                                          * instead of a specific set. This
53                                          * means that we cannot rely on the set
54                                          * interpret routine to be called.
55                                          * lov_statfs_fini() must thus be called
56                                          * by the request interpret routine */
57 #define OBD_STATFS_FOR_MDT0     0x0008  /* The statfs is only for retrieving
58                                          * information from MDT0. */
59 #define OBD_FL_PUNCH    0x00000001      /* To indicate it is punch operation */
60
61 /* OBD Device Declarations */
62 extern struct obd_device *obd_devs[MAX_OBD_DEVICES];
63 extern rwlock_t obd_dev_lock;
64
65 /* OBD Operations Declarations */
66 struct obd_device *class_exp2obd(struct obd_export *);
67 int class_handle_ioctl(unsigned int cmd, unsigned long arg);
68 int lustre_get_jobid(char *jobid);
69
70 struct lu_device_type;
71
72 /* genops.c */
73 extern struct list_head obd_types;
74 struct obd_export *class_conn2export(struct lustre_handle *);
75 int class_register_type(struct obd_ops *, struct md_ops *,
76                         const char *nm, struct lu_device_type *ldt);
77 int class_unregister_type(const char *nm);
78
79 struct obd_device *class_newdev(const char *type_name, const char *name);
80 void class_release_dev(struct obd_device *obd);
81
82 int class_name2dev(const char *name);
83 struct obd_device *class_name2obd(const char *name);
84 int class_uuid2dev(struct obd_uuid *uuid);
85 struct obd_device *class_find_client_obd(struct obd_uuid *tgt_uuid,
86                                           const char *typ_name,
87                                           struct obd_uuid *grp_uuid);
88 struct obd_device *class_devices_in_group(struct obd_uuid *grp_uuid,
89                                            int *next);
90 struct obd_device *class_num2obd(int num);
91
92 int class_notify_sptlrpc_conf(const char *fsname, int namelen);
93
94 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep);
95
96 int obd_zombie_impexp_init(void);
97 void obd_zombie_impexp_stop(void);
98 void obd_zombie_barrier(void);
99
100 struct llog_handle;
101 struct llog_rec_hdr;
102 typedef int (*llog_cb_t)(const struct lu_env *, struct llog_handle *,
103                          struct llog_rec_hdr *, void *);
104 /* obd_config.c */
105 int class_process_config(struct lustre_cfg *lcfg);
106 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
107                              struct lustre_cfg *lcfg, void *data);
108 struct obd_device *class_incref(struct obd_device *obd,
109                                 const char *scope, const void *source);
110 void class_decref(struct obd_device *obd,
111                   const char *scope, const void *source);
112 int class_config_llog_handler(const struct lu_env *env,
113                               struct llog_handle *handle,
114                               struct llog_rec_hdr *rec, void *data);
115 int class_add_uuid(const char *uuid, __u64 nid);
116
117 /*obdecho*/
118 void lprocfs_echo_init_vars(struct lprocfs_static_vars *lvars);
119
120 #define CFG_F_START     0x01   /* Set when we start updating from a log */
121 #define CFG_F_MARKER    0x02   /* We are within a maker */
122 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
123 #define CFG_F_COMPAT146 0x08   /* Allow old-style logs */
124 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
125
126 /* Passed as data param to class_config_parse_llog */
127 struct config_llog_instance {
128         char           *cfg_obdname;
129         void           *cfg_instance;
130         struct super_block *cfg_sb;
131         struct obd_uuid     cfg_uuid;
132         llog_cb_t           cfg_callback;
133         int              cfg_last_idx; /* for partial llog processing */
134         int              cfg_flags;
135 };
136
137 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
138                             char *name, struct config_llog_instance *cfg);
139 enum {
140         CONFIG_T_CONFIG  = 0,
141         CONFIG_T_SPTLRPC = 1,
142         CONFIG_T_RECOVER = 2,
143         CONFIG_T_PARAMS  = 3,
144         CONFIG_T_MAX     = 4
145 };
146
147 #define PARAMS_FILENAME "params"
148 #define LCTL_UPCALL     "lctl"
149
150 /* list of active configuration logs  */
151 struct config_llog_data {
152         struct ldlm_res_id        cld_resid;
153         struct config_llog_instance cld_cfg;
154         struct list_head                  cld_list_chain;
155         atomic_t                cld_refcount;
156         struct config_llog_data    *cld_sptlrpc;/* depended sptlrpc log */
157         struct config_llog_data    *cld_params; /* common parameters log */
158         struct config_llog_data    *cld_recover;/* imperative recover log */
159         struct obd_export         *cld_mgcexp;
160         struct mutex                cld_lock;
161         int                      cld_type;
162         unsigned int            cld_stopping:1, /* we were told to stop
163                                                      * watching */
164                                     cld_lostlock:1; /* lock not requeued */
165         char                    cld_logname[0];
166 };
167
168 struct lustre_profile {
169         struct list_head       lp_list;
170         char        *lp_profile;
171         char        *lp_dt;
172         char        *lp_md;
173 };
174
175 struct lustre_profile *class_get_profile(const char *prof);
176 void class_del_profile(const char *prof);
177 void class_del_profiles(void);
178
179 #if LUSTRE_TRACKS_LOCK_EXP_REFS
180
181 void __class_export_add_lock_ref(struct obd_export *, struct ldlm_lock *);
182 void __class_export_del_lock_ref(struct obd_export *, struct ldlm_lock *);
183 extern void (*class_export_dump_hook)(struct obd_export *);
184
185 #else
186
187 #define __class_export_add_lock_ref(exp, lock)       do {} while (0)
188 #define __class_export_del_lock_ref(exp, lock)       do {} while (0)
189
190 #endif
191
192 /* genops.c */
193 struct obd_export *class_export_get(struct obd_export *exp);
194 void class_export_put(struct obd_export *exp);
195 struct obd_export *class_new_export(struct obd_device *obddev,
196                                 struct obd_uuid *cluuid);
197 void class_unlink_export(struct obd_export *exp);
198
199 struct obd_import *class_import_get(struct obd_import *);
200 void class_import_put(struct obd_import *);
201 struct obd_import *class_new_import(struct obd_device *obd);
202 void class_destroy_import(struct obd_import *exp);
203
204 void class_put_type(struct obd_type *type);
205 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
206                 struct obd_uuid *cluuid);
207 int class_disconnect(struct obd_export *exp);
208 void class_fail_export(struct obd_export *exp);
209 int class_manual_cleanup(struct obd_device *obd);
210
211 static inline void class_export_rpc_inc(struct obd_export *exp)
212 {
213         atomic_inc(&(exp)->exp_rpc_count);
214         CDEBUG(D_INFO, "RPC GETting export %p : new rpc_count %d\n",
215                (exp), atomic_read(&(exp)->exp_rpc_count));
216 }
217
218 static inline void class_export_rpc_dec(struct obd_export *exp)
219 {
220         LASSERT_ATOMIC_POS(&exp->exp_rpc_count);
221         atomic_dec(&(exp)->exp_rpc_count);
222         CDEBUG(D_INFO, "RPC PUTting export %p : new rpc_count %d\n",
223                (exp), atomic_read(&(exp)->exp_rpc_count));
224 }
225
226 static inline struct obd_export *class_export_lock_get(struct obd_export *exp,
227                                                        struct ldlm_lock *lock)
228 {
229         atomic_inc(&(exp)->exp_locks_count);
230         __class_export_add_lock_ref(exp, lock);
231         CDEBUG(D_INFO, "lock GETting export %p : new locks_count %d\n",
232                (exp), atomic_read(&(exp)->exp_locks_count));
233         return class_export_get(exp);
234 }
235
236 static inline void class_export_lock_put(struct obd_export *exp,
237                                          struct ldlm_lock *lock)
238 {
239         LASSERT_ATOMIC_POS(&exp->exp_locks_count);
240         atomic_dec(&(exp)->exp_locks_count);
241         __class_export_del_lock_ref(exp, lock);
242         CDEBUG(D_INFO, "lock PUTting export %p : new locks_count %d\n",
243                (exp), atomic_read(&(exp)->exp_locks_count));
244         class_export_put(exp);
245 }
246
247 static inline enum obd_option exp_flags_from_obd(struct obd_device *obd)
248 {
249         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
250                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
251                 0);
252 }
253
254 static inline int lprocfs_climp_check(struct obd_device *obd)
255 {
256         down_read(&(obd)->u.cli.cl_sem);
257         if (!(obd)->u.cli.cl_import) {
258                 up_read(&(obd)->u.cli.cl_sem);
259                 return -ENODEV;
260         }
261         return 0;
262 }
263
264 struct inode;
265 struct lu_attr;
266 struct obdo;
267 void obdo_refresh_inode(struct inode *dst, struct obdo *src, u32 valid);
268
269 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj);
270 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, u32 valid);
271
272 #define OBT(dev)        (dev)->obd_type
273 #define OBP(dev, op)    (dev)->obd_type->typ_dt_ops->o_ ## op
274 #define MDP(dev, op)    (dev)->obd_type->typ_md_ops->m_ ## op
275 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
276
277 /* Ensure obd_setup: used for cleanup which must be called
278    while obd is stopping */
279 static inline int obd_check_dev(struct obd_device *obd)
280 {
281         if (!obd) {
282                 CERROR("NULL device\n");
283                 return -ENODEV;
284         }
285         return 0;
286 }
287
288 /* ensure obd_setup and !obd_stopping */
289 static inline int obd_check_dev_active(struct obd_device *obd)
290 {
291         int rc;
292
293         rc = obd_check_dev(obd);
294         if (rc)
295                 return rc;
296         if (!obd->obd_set_up || obd->obd_stopping) {
297                 CERROR("Device %d not setup\n", obd->obd_minor);
298                 return -ENODEV;
299         }
300         return rc;
301 }
302
303 #define OBD_COUNTER_OFFSET(op)                            \
304         ((offsetof(struct obd_ops, o_ ## op) -            \
305           offsetof(struct obd_ops, o_iocontrol))                \
306          / sizeof(((struct obd_ops *)(0))->o_iocontrol))
307
308 #define OBD_COUNTER_INCREMENT(obdx, op)                    \
309         if ((obdx)->obd_stats != NULL) {                          \
310                 unsigned int coffset;                        \
311                 coffset = (unsigned int)((obdx)->obd_cntr_base) + \
312                         OBD_COUNTER_OFFSET(op);            \
313                 LASSERT(coffset < (obdx)->obd_stats->ls_num);     \
314                 lprocfs_counter_incr((obdx)->obd_stats, coffset); \
315         }
316
317 #define EXP_COUNTER_INCREMENT(export, op)                                   \
318         if ((export)->exp_obd->obd_stats != NULL) {                       \
319                 unsigned int coffset;                                   \
320                 coffset = (unsigned int)((export)->exp_obd->obd_cntr_base) + \
321                         OBD_COUNTER_OFFSET(op);                       \
322                 LASSERT(coffset < (export)->exp_obd->obd_stats->ls_num);     \
323                 lprocfs_counter_incr((export)->exp_obd->obd_stats, coffset); \
324         }
325
326 #define MD_COUNTER_OFFSET(op)                              \
327         ((offsetof(struct md_ops, m_ ## op) -              \
328           offsetof(struct md_ops, m_getstatus))          \
329          / sizeof(((struct md_ops *)(0))->m_getstatus))
330
331 #define MD_COUNTER_INCREMENT(obdx, op)                     \
332         if ((obd)->md_stats != NULL) {                     \
333                 unsigned int coffset;                       \
334                 coffset = (unsigned int)((obdx)->md_cntr_base) + \
335                         MD_COUNTER_OFFSET(op);             \
336                 LASSERT(coffset < (obdx)->md_stats->ls_num);     \
337                 lprocfs_counter_incr((obdx)->md_stats, coffset); \
338         }
339
340 #define EXP_MD_COUNTER_INCREMENT(export, op)                             \
341         if ((export)->exp_obd->obd_stats != NULL) {                       \
342                 unsigned int coffset;                                   \
343                 coffset = (unsigned int)((export)->exp_obd->md_cntr_base) +  \
344                         MD_COUNTER_OFFSET(op);                         \
345                 LASSERT(coffset < (export)->exp_obd->md_stats->ls_num);      \
346                 lprocfs_counter_incr((export)->exp_obd->md_stats, coffset);  \
347                 if ((export)->exp_md_stats != NULL)                       \
348                         lprocfs_counter_incr(                           \
349                                 (export)->exp_md_stats, coffset);           \
350         }
351
352 #define EXP_CHECK_MD_OP(exp, op)                                \
353 do {                                                        \
354         if ((exp) == NULL) {                                \
355                 CERROR("obd_" #op ": NULL export\n");      \
356                 return -ENODEV;                         \
357         }                                                      \
358         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
359                 CERROR("obd_" #op ": cleaned up obd\n");        \
360                 return -EOPNOTSUPP;                         \
361         }                                                      \
362         if (!OBT((exp)->exp_obd) || !MDP((exp)->exp_obd, op)) { \
363                 CERROR("obd_" #op ": dev %s/%d no operation\n", \
364                        (exp)->exp_obd->obd_name,                \
365                        (exp)->exp_obd->obd_minor);            \
366                 return -EOPNOTSUPP;                         \
367         }                                                      \
368 } while (0)
369
370 #define OBD_CHECK_DT_OP(obd, op, err)                      \
371 do {                                                        \
372         if (!OBT(obd) || !OBP((obd), op)) {                  \
373                 if (err)                                        \
374                         CERROR("obd_" #op ": dev %d no operation\n",    \
375                                obd->obd_minor);          \
376                 return err;                                 \
377         }                                                      \
378 } while (0)
379
380 #define EXP_CHECK_DT_OP(exp, op)                                \
381 do {                                                        \
382         if ((exp) == NULL) {                                \
383                 CERROR("obd_" #op ": NULL export\n");      \
384                 return -ENODEV;                         \
385         }                                                      \
386         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
387                 CERROR("obd_" #op ": cleaned up obd\n");        \
388                 return -EOPNOTSUPP;                         \
389         }                                                      \
390         if (!OBT((exp)->exp_obd) || !OBP((exp)->exp_obd, op)) { \
391                 CERROR("obd_" #op ": dev %d no operation\n",    \
392                        (exp)->exp_obd->obd_minor);            \
393                 return -EOPNOTSUPP;                         \
394         }                                                      \
395 } while (0)
396
397 #define CTXT_CHECK_OP(ctxt, op, err)                             \
398 do {                                                             \
399         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {             \
400                 if (err)                                             \
401                         CERROR("lop_" #op ": dev %d no operation\n", \
402                                ctxt->loc_obd->obd_minor);           \
403                 return err;                                      \
404         }                                                           \
405 } while (0)
406
407 static inline int class_devno_max(void)
408 {
409         return MAX_OBD_DEVICES;
410 }
411
412 static inline int obd_get_info(const struct lu_env *env,
413                                struct obd_export *exp, __u32 keylen,
414                                void *key, __u32 *vallen, void *val,
415                                struct lov_stripe_md *lsm)
416 {
417         int rc;
418
419         EXP_CHECK_DT_OP(exp, get_info);
420         EXP_COUNTER_INCREMENT(exp, get_info);
421
422         rc = OBP(exp->exp_obd, get_info)(env, exp, keylen, key, vallen, val,
423                                          lsm);
424         return rc;
425 }
426
427 static inline int obd_set_info_async(const struct lu_env *env,
428                                      struct obd_export *exp, u32 keylen,
429                                      void *key, u32 vallen, void *val,
430                                      struct ptlrpc_request_set *set)
431 {
432         int rc;
433
434         EXP_CHECK_DT_OP(exp, set_info_async);
435         EXP_COUNTER_INCREMENT(exp, set_info_async);
436
437         rc = OBP(exp->exp_obd, set_info_async)(env, exp, keylen, key, vallen,
438                                                val, set);
439         return rc;
440 }
441
442 /*
443  * obd-lu integration.
444  *
445  * Functionality is being moved into new lu_device-based layering, but some
446  * pieces of configuration process are still based on obd devices.
447  *
448  * Specifically, lu_device_type_operations::ldto_device_alloc() methods fully
449  * subsume ->o_setup() methods of obd devices they replace. The same for
450  * lu_device_operations::ldo_process_config() and ->o_process_config(). As a
451  * result, obd_setup() and obd_process_config() branch and call one XOR
452  * another.
453  *
454  * Yet neither lu_device_type_operations::ldto_device_fini() nor
455  * lu_device_type_operations::ldto_device_free() fully implement the
456  * functionality of ->o_precleanup() and ->o_cleanup() they override. Hence,
457  * obd_precleanup() and obd_cleanup() call both lu_device and obd operations.
458  */
459
460 #define DECLARE_LU_VARS(ldt, d)          \
461         struct lu_device_type *ldt;       \
462         struct lu_device *d
463
464 static inline int obd_setup(struct obd_device *obd, struct lustre_cfg *cfg)
465 {
466         int rc;
467         DECLARE_LU_VARS(ldt, d);
468
469         ldt = obd->obd_type->typ_lu;
470         if (ldt != NULL) {
471                 struct lu_context  session_ctx;
472                 struct lu_env env;
473
474                 lu_context_init(&session_ctx, LCT_SESSION);
475                 session_ctx.lc_thread = NULL;
476                 lu_context_enter(&session_ctx);
477
478                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
479                 if (rc == 0) {
480                         env.le_ses = &session_ctx;
481                         d = ldt->ldt_ops->ldto_device_alloc(&env, ldt, cfg);
482                         lu_env_fini(&env);
483                         if (!IS_ERR(d)) {
484                                 obd->obd_lu_dev = d;
485                                 d->ld_obd = obd;
486                                 rc = 0;
487                         } else
488                                 rc = PTR_ERR(d);
489                 }
490                 lu_context_exit(&session_ctx);
491                 lu_context_fini(&session_ctx);
492
493         } else {
494                 OBD_CHECK_DT_OP(obd, setup, -EOPNOTSUPP);
495                 OBD_COUNTER_INCREMENT(obd, setup);
496                 rc = OBP(obd, setup)(obd, cfg);
497         }
498         return rc;
499 }
500
501 static inline int obd_precleanup(struct obd_device *obd,
502                                  enum obd_cleanup_stage cleanup_stage)
503 {
504         int rc;
505         DECLARE_LU_VARS(ldt, d);
506
507         rc = obd_check_dev(obd);
508         if (rc)
509                 return rc;
510         ldt = obd->obd_type->typ_lu;
511         d = obd->obd_lu_dev;
512         if (ldt != NULL && d != NULL) {
513                 if (cleanup_stage == OBD_CLEANUP_EXPORTS) {
514                         struct lu_env env;
515
516                         rc = lu_env_init(&env, ldt->ldt_ctx_tags);
517                         if (rc == 0) {
518                                 ldt->ldt_ops->ldto_device_fini(&env, d);
519                                 lu_env_fini(&env);
520                         }
521                 }
522         }
523         OBD_CHECK_DT_OP(obd, precleanup, 0);
524         OBD_COUNTER_INCREMENT(obd, precleanup);
525
526         rc = OBP(obd, precleanup)(obd, cleanup_stage);
527         return rc;
528 }
529
530 static inline int obd_cleanup(struct obd_device *obd)
531 {
532         int rc;
533         DECLARE_LU_VARS(ldt, d);
534
535         rc = obd_check_dev(obd);
536         if (rc)
537                 return rc;
538
539         ldt = obd->obd_type->typ_lu;
540         d = obd->obd_lu_dev;
541         if (ldt != NULL && d != NULL) {
542                 struct lu_env env;
543
544                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
545                 if (rc == 0) {
546                         ldt->ldt_ops->ldto_device_free(&env, d);
547                         lu_env_fini(&env);
548                         obd->obd_lu_dev = NULL;
549                 }
550         }
551         OBD_CHECK_DT_OP(obd, cleanup, 0);
552         OBD_COUNTER_INCREMENT(obd, cleanup);
553
554         rc = OBP(obd, cleanup)(obd);
555         return rc;
556 }
557
558 static inline void obd_cleanup_client_import(struct obd_device *obd)
559 {
560         /* If we set up but never connected, the
561            client import will not have been cleaned. */
562         down_write(&obd->u.cli.cl_sem);
563         if (obd->u.cli.cl_import) {
564                 struct obd_import *imp;
565
566                 imp = obd->u.cli.cl_import;
567                 CDEBUG(D_CONFIG, "%s: client import never connected\n",
568                        obd->obd_name);
569                 ptlrpc_invalidate_import(imp);
570                 client_destroy_import(imp);
571                 obd->u.cli.cl_import = NULL;
572         }
573         up_write(&obd->u.cli.cl_sem);
574 }
575
576 static inline int
577 obd_process_config(struct obd_device *obd, int datalen, void *data)
578 {
579         int rc;
580         DECLARE_LU_VARS(ldt, d);
581
582         rc = obd_check_dev(obd);
583         if (rc)
584                 return rc;
585
586         obd->obd_process_conf = 1;
587         ldt = obd->obd_type->typ_lu;
588         d = obd->obd_lu_dev;
589         if (ldt != NULL && d != NULL) {
590                 struct lu_env env;
591
592                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
593                 if (rc == 0) {
594                         rc = d->ld_ops->ldo_process_config(&env, d, data);
595                         lu_env_fini(&env);
596                 }
597         } else {
598                 OBD_CHECK_DT_OP(obd, process_config, -EOPNOTSUPP);
599                 rc = OBP(obd, process_config)(obd, datalen, data);
600         }
601         OBD_COUNTER_INCREMENT(obd, process_config);
602         obd->obd_process_conf = 0;
603
604         return rc;
605 }
606
607 /* Pack an in-memory MD struct for storage on disk.
608  * Returns +ve size of packed MD (0 for free), or -ve error.
609  *
610  * If @disk_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
611  * If @*disk_tgt != NULL and @mem_src == NULL, @*disk_tgt will be freed.
612  * If @*disk_tgt == NULL, it will be allocated
613  */
614 static inline int obd_packmd(struct obd_export *exp,
615                              struct lov_mds_md **disk_tgt,
616                              struct lov_stripe_md *mem_src)
617 {
618         int rc;
619
620         EXP_CHECK_DT_OP(exp, packmd);
621         EXP_COUNTER_INCREMENT(exp, packmd);
622
623         rc = OBP(exp->exp_obd, packmd)(exp, disk_tgt, mem_src);
624         return rc;
625 }
626
627 static inline int obd_size_diskmd(struct obd_export *exp,
628                                   struct lov_stripe_md *mem_src)
629 {
630         return obd_packmd(exp, NULL, mem_src);
631 }
632
633 static inline int obd_free_diskmd(struct obd_export *exp,
634                                   struct lov_mds_md **disk_tgt)
635 {
636         LASSERT(disk_tgt);
637         LASSERT(*disk_tgt);
638         /*
639          * LU-2590, for caller's convenience, *disk_tgt could be host
640          * endianness, it needs swab to LE if necessary, while just
641          * lov_mds_md header needs it for figuring out how much memory
642          * needs to be freed.
643          */
644         if ((cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) &&
645             (((*disk_tgt)->lmm_magic == LOV_MAGIC_V1) ||
646              ((*disk_tgt)->lmm_magic == LOV_MAGIC_V3)))
647                 lustre_swab_lov_mds_md(*disk_tgt);
648         return obd_packmd(exp, disk_tgt, NULL);
649 }
650
651 /* Unpack an MD struct from disk to in-memory format.
652  * Returns +ve size of unpacked MD (0 for free), or -ve error.
653  *
654  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
655  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
656  * If @*mem_tgt == NULL, it will be allocated
657  */
658 static inline int obd_unpackmd(struct obd_export *exp,
659                                struct lov_stripe_md **mem_tgt,
660                                struct lov_mds_md *disk_src,
661                                int disk_len)
662 {
663         int rc;
664
665         EXP_CHECK_DT_OP(exp, unpackmd);
666         EXP_COUNTER_INCREMENT(exp, unpackmd);
667
668         rc = OBP(exp->exp_obd, unpackmd)(exp, mem_tgt, disk_src, disk_len);
669         return rc;
670 }
671
672 /* helper functions */
673 static inline int obd_alloc_memmd(struct obd_export *exp,
674                                   struct lov_stripe_md **mem_tgt)
675 {
676         LASSERT(mem_tgt);
677         LASSERT(*mem_tgt == NULL);
678         return obd_unpackmd(exp, mem_tgt, NULL, 0);
679 }
680
681 static inline int obd_free_memmd(struct obd_export *exp,
682                                  struct lov_stripe_md **mem_tgt)
683 {
684         int rc;
685
686         LASSERT(mem_tgt);
687         LASSERT(*mem_tgt);
688         rc = obd_unpackmd(exp, mem_tgt, NULL, 0);
689         *mem_tgt = NULL;
690         return rc;
691 }
692
693 static inline int obd_create(const struct lu_env *env, struct obd_export *exp,
694                              struct obdo *obdo, struct lov_stripe_md **ea,
695                              struct obd_trans_info *oti)
696 {
697         int rc;
698
699         EXP_CHECK_DT_OP(exp, create);
700         EXP_COUNTER_INCREMENT(exp, create);
701
702         rc = OBP(exp->exp_obd, create)(env, exp, obdo, ea, oti);
703         return rc;
704 }
705
706 static inline int obd_destroy(const struct lu_env *env, struct obd_export *exp,
707                               struct obdo *obdo, struct lov_stripe_md *ea,
708                               struct obd_trans_info *oti,
709                               struct obd_export *md_exp)
710 {
711         int rc;
712
713         EXP_CHECK_DT_OP(exp, destroy);
714         EXP_COUNTER_INCREMENT(exp, destroy);
715
716         rc = OBP(exp->exp_obd, destroy)(env, exp, obdo, ea, oti, md_exp);
717         return rc;
718 }
719
720 static inline int obd_getattr(const struct lu_env *env, struct obd_export *exp,
721                               struct obd_info *oinfo)
722 {
723         int rc;
724
725         EXP_CHECK_DT_OP(exp, getattr);
726         EXP_COUNTER_INCREMENT(exp, getattr);
727
728         rc = OBP(exp->exp_obd, getattr)(env, exp, oinfo);
729         return rc;
730 }
731
732 static inline int obd_getattr_async(struct obd_export *exp,
733                                     struct obd_info *oinfo,
734                                     struct ptlrpc_request_set *set)
735 {
736         int rc;
737
738         EXP_CHECK_DT_OP(exp, getattr_async);
739         EXP_COUNTER_INCREMENT(exp, getattr_async);
740
741         rc = OBP(exp->exp_obd, getattr_async)(exp, oinfo, set);
742         return rc;
743 }
744
745 static inline int obd_setattr(const struct lu_env *env, struct obd_export *exp,
746                               struct obd_info *oinfo,
747                               struct obd_trans_info *oti)
748 {
749         int rc;
750
751         EXP_CHECK_DT_OP(exp, setattr);
752         EXP_COUNTER_INCREMENT(exp, setattr);
753
754         rc = OBP(exp->exp_obd, setattr)(env, exp, oinfo, oti);
755         return rc;
756 }
757
758 /* This performs all the requests set init/wait/destroy actions. */
759 static inline int obd_setattr_rqset(struct obd_export *exp,
760                                     struct obd_info *oinfo,
761                                     struct obd_trans_info *oti)
762 {
763         struct ptlrpc_request_set *set = NULL;
764         int rc;
765
766         EXP_CHECK_DT_OP(exp, setattr_async);
767         EXP_COUNTER_INCREMENT(exp, setattr_async);
768
769         set =  ptlrpc_prep_set();
770         if (set == NULL)
771                 return -ENOMEM;
772
773         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
774         if (rc == 0)
775                 rc = ptlrpc_set_wait(set);
776         ptlrpc_set_destroy(set);
777         return rc;
778 }
779
780 /* This adds all the requests into @set if @set != NULL, otherwise
781    all requests are sent asynchronously without waiting for response. */
782 static inline int obd_setattr_async(struct obd_export *exp,
783                                     struct obd_info *oinfo,
784                                     struct obd_trans_info *oti,
785                                     struct ptlrpc_request_set *set)
786 {
787         int rc;
788
789         EXP_CHECK_DT_OP(exp, setattr_async);
790         EXP_COUNTER_INCREMENT(exp, setattr_async);
791
792         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
793         return rc;
794 }
795
796 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
797                                int priority)
798 {
799         struct obd_device *obd = imp->imp_obd;
800         int rc;
801
802         rc = obd_check_dev_active(obd);
803         if (rc)
804                 return rc;
805         OBD_CHECK_DT_OP(obd, add_conn, -EOPNOTSUPP);
806         OBD_COUNTER_INCREMENT(obd, add_conn);
807
808         rc = OBP(obd, add_conn)(imp, uuid, priority);
809         return rc;
810 }
811
812 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
813 {
814         struct obd_device *obd = imp->imp_obd;
815         int rc;
816
817         rc = obd_check_dev_active(obd);
818         if (rc)
819                 return rc;
820         OBD_CHECK_DT_OP(obd, del_conn, -EOPNOTSUPP);
821         OBD_COUNTER_INCREMENT(obd, del_conn);
822
823         rc = OBP(obd, del_conn)(imp, uuid);
824         return rc;
825 }
826
827 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
828 {
829         struct obd_uuid *uuid;
830
831         OBD_CHECK_DT_OP(exp->exp_obd, get_uuid, NULL);
832         EXP_COUNTER_INCREMENT(exp, get_uuid);
833
834         uuid = OBP(exp->exp_obd, get_uuid)(exp);
835         return uuid;
836 }
837
838 /** Create a new /a exp on device /a obd for the uuid /a cluuid
839  * @param exp New export handle
840  * @param d Connect data, supported flags are set, flags also understood
841  *    by obd are returned.
842  */
843 static inline int obd_connect(const struct lu_env *env,
844                               struct obd_export **exp, struct obd_device *obd,
845                               struct obd_uuid *cluuid,
846                               struct obd_connect_data *data,
847                               void *localdata)
848 {
849         int rc;
850         __u64 ocf = data ? data->ocd_connect_flags : 0; /* for post-condition
851                                                    * check */
852
853         rc = obd_check_dev_active(obd);
854         if (rc)
855                 return rc;
856         OBD_CHECK_DT_OP(obd, connect, -EOPNOTSUPP);
857         OBD_COUNTER_INCREMENT(obd, connect);
858
859         rc = OBP(obd, connect)(env, exp, obd, cluuid, data, localdata);
860         /* check that only subset is granted */
861         LASSERT(ergo(data != NULL, (data->ocd_connect_flags & ocf) ==
862                                     data->ocd_connect_flags));
863         return rc;
864 }
865
866 static inline int obd_reconnect(const struct lu_env *env,
867                                 struct obd_export *exp,
868                                 struct obd_device *obd,
869                                 struct obd_uuid *cluuid,
870                                 struct obd_connect_data *d,
871                                 void *localdata)
872 {
873         int rc;
874         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
875                                                    * check */
876
877         rc = obd_check_dev_active(obd);
878         if (rc)
879                 return rc;
880         OBD_CHECK_DT_OP(obd, reconnect, 0);
881         OBD_COUNTER_INCREMENT(obd, reconnect);
882
883         rc = OBP(obd, reconnect)(env, exp, obd, cluuid, d, localdata);
884         /* check that only subset is granted */
885         LASSERT(ergo(d != NULL,
886                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
887         return rc;
888 }
889
890 static inline int obd_disconnect(struct obd_export *exp)
891 {
892         int rc;
893
894         EXP_CHECK_DT_OP(exp, disconnect);
895         EXP_COUNTER_INCREMENT(exp, disconnect);
896
897         rc = OBP(exp->exp_obd, disconnect)(exp);
898         return rc;
899 }
900
901 static inline int obd_fid_init(struct obd_device *obd, struct obd_export *exp,
902                                enum lu_cli_type type)
903 {
904         int rc;
905
906         OBD_CHECK_DT_OP(obd, fid_init, 0);
907         OBD_COUNTER_INCREMENT(obd, fid_init);
908
909         rc = OBP(obd, fid_init)(obd, exp, type);
910         return rc;
911 }
912
913 static inline int obd_fid_fini(struct obd_device *obd)
914 {
915         int rc;
916
917         OBD_CHECK_DT_OP(obd, fid_fini, 0);
918         OBD_COUNTER_INCREMENT(obd, fid_fini);
919
920         rc = OBP(obd, fid_fini)(obd);
921         return rc;
922 }
923
924 static inline int obd_fid_alloc(struct obd_export *exp,
925                                 struct lu_fid *fid,
926                                 struct md_op_data *op_data)
927 {
928         int rc;
929
930         EXP_CHECK_DT_OP(exp, fid_alloc);
931         EXP_COUNTER_INCREMENT(exp, fid_alloc);
932
933         rc = OBP(exp->exp_obd, fid_alloc)(exp, fid, op_data);
934         return rc;
935 }
936
937 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
938 {
939         int rc;
940
941         OBD_CHECK_DT_OP(obd, pool_new, -EOPNOTSUPP);
942         OBD_COUNTER_INCREMENT(obd, pool_new);
943
944         rc = OBP(obd, pool_new)(obd, poolname);
945         return rc;
946 }
947
948 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
949 {
950         int rc;
951
952         OBD_CHECK_DT_OP(obd, pool_del, -EOPNOTSUPP);
953         OBD_COUNTER_INCREMENT(obd, pool_del);
954
955         rc = OBP(obd, pool_del)(obd, poolname);
956         return rc;
957 }
958
959 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
960 {
961         int rc;
962
963         OBD_CHECK_DT_OP(obd, pool_add, -EOPNOTSUPP);
964         OBD_COUNTER_INCREMENT(obd, pool_add);
965
966         rc = OBP(obd, pool_add)(obd, poolname, ostname);
967         return rc;
968 }
969
970 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
971 {
972         int rc;
973
974         OBD_CHECK_DT_OP(obd, pool_rem, -EOPNOTSUPP);
975         OBD_COUNTER_INCREMENT(obd, pool_rem);
976
977         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
978         return rc;
979 }
980
981 static inline void obd_getref(struct obd_device *obd)
982 {
983         if (OBT(obd) && OBP(obd, getref)) {
984                 OBD_COUNTER_INCREMENT(obd, getref);
985                 OBP(obd, getref)(obd);
986         }
987 }
988
989 static inline void obd_putref(struct obd_device *obd)
990 {
991         if (OBT(obd) && OBP(obd, putref)) {
992                 OBD_COUNTER_INCREMENT(obd, putref);
993                 OBP(obd, putref)(obd);
994         }
995 }
996
997 static inline int obd_init_export(struct obd_export *exp)
998 {
999         int rc = 0;
1000
1001         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1002             OBP((exp)->exp_obd, init_export))
1003                 rc = OBP(exp->exp_obd, init_export)(exp);
1004         return rc;
1005 }
1006
1007 static inline int obd_destroy_export(struct obd_export *exp)
1008 {
1009         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1010             OBP((exp)->exp_obd, destroy_export))
1011                 OBP(exp->exp_obd, destroy_export)(exp);
1012         return 0;
1013 }
1014
1015 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1016  * If the cache is older than @max_age we will get a new value from the
1017  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1018 static inline int obd_statfs_async(struct obd_export *exp,
1019                                    struct obd_info *oinfo,
1020                                    __u64 max_age,
1021                                    struct ptlrpc_request_set *rqset)
1022 {
1023         int rc = 0;
1024         struct obd_device *obd;
1025
1026         if (exp == NULL || exp->exp_obd == NULL)
1027                 return -EINVAL;
1028
1029         obd = exp->exp_obd;
1030         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1031         OBD_COUNTER_INCREMENT(obd, statfs);
1032
1033         CDEBUG(D_SUPER, "%s: osfs %p age %llu, max_age %llu\n",
1034                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
1035         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1036                 rc = OBP(obd, statfs_async)(exp, oinfo, max_age, rqset);
1037         } else {
1038                 CDEBUG(D_SUPER,
1039                        "%s: use %p cache blocks %llu/%llu objects %llu/%llu\n",
1040                        obd->obd_name, &obd->obd_osfs,
1041                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1042                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1043                 spin_lock(&obd->obd_osfs_lock);
1044                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1045                 spin_unlock(&obd->obd_osfs_lock);
1046                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1047                 if (oinfo->oi_cb_up)
1048                         oinfo->oi_cb_up(oinfo, 0);
1049         }
1050         return rc;
1051 }
1052
1053 static inline int obd_statfs_rqset(struct obd_export *exp,
1054                                    struct obd_statfs *osfs, __u64 max_age,
1055                                    __u32 flags)
1056 {
1057         struct ptlrpc_request_set *set = NULL;
1058         struct obd_info oinfo = { };
1059         int rc = 0;
1060
1061         set =  ptlrpc_prep_set();
1062         if (set == NULL)
1063                 return -ENOMEM;
1064
1065         oinfo.oi_osfs = osfs;
1066         oinfo.oi_flags = flags;
1067         rc = obd_statfs_async(exp, &oinfo, max_age, set);
1068         if (rc == 0)
1069                 rc = ptlrpc_set_wait(set);
1070         ptlrpc_set_destroy(set);
1071         return rc;
1072 }
1073
1074 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1075  * If the cache is older than @max_age we will get a new value from the
1076  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1077 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1078                              struct obd_statfs *osfs, __u64 max_age,
1079                              __u32 flags)
1080 {
1081         int rc = 0;
1082         struct obd_device *obd = exp->exp_obd;
1083
1084         if (obd == NULL)
1085                 return -EINVAL;
1086
1087         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1088         OBD_COUNTER_INCREMENT(obd, statfs);
1089
1090         CDEBUG(D_SUPER, "osfs %llu, max_age %llu\n",
1091                obd->obd_osfs_age, max_age);
1092         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1093                 rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1094                 if (rc == 0) {
1095                         spin_lock(&obd->obd_osfs_lock);
1096                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1097                         obd->obd_osfs_age = cfs_time_current_64();
1098                         spin_unlock(&obd->obd_osfs_lock);
1099                 }
1100         } else {
1101                 CDEBUG(D_SUPER, "%s: use %p cache blocks %llu/%llu objects %llu/%llu\n",
1102                        obd->obd_name, &obd->obd_osfs,
1103                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1104                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1105                 spin_lock(&obd->obd_osfs_lock);
1106                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1107                 spin_unlock(&obd->obd_osfs_lock);
1108         }
1109         return rc;
1110 }
1111
1112 static inline int obd_preprw(const struct lu_env *env, int cmd,
1113                              struct obd_export *exp, struct obdo *oa,
1114                              int objcount, struct obd_ioobj *obj,
1115                              struct niobuf_remote *remote, int *pages,
1116                              struct niobuf_local *local,
1117                              struct obd_trans_info *oti)
1118 {
1119         int rc;
1120
1121         EXP_CHECK_DT_OP(exp, preprw);
1122         EXP_COUNTER_INCREMENT(exp, preprw);
1123
1124         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1125                                        pages, local, oti);
1126         return rc;
1127 }
1128
1129 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1130                                struct obd_export *exp, struct obdo *oa,
1131                                int objcount, struct obd_ioobj *obj,
1132                                struct niobuf_remote *rnb, int pages,
1133                                struct niobuf_local *local,
1134                                struct obd_trans_info *oti, int rc)
1135 {
1136         EXP_CHECK_DT_OP(exp, commitrw);
1137         EXP_COUNTER_INCREMENT(exp, commitrw);
1138
1139         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1140                                          rnb, pages, local, oti, rc);
1141         return rc;
1142 }
1143
1144 static inline int obd_adjust_kms(struct obd_export *exp,
1145                                  struct lov_stripe_md *lsm, u64 size,
1146                                  int shrink)
1147 {
1148         int rc;
1149
1150         EXP_CHECK_DT_OP(exp, adjust_kms);
1151         EXP_COUNTER_INCREMENT(exp, adjust_kms);
1152
1153         rc = OBP(exp->exp_obd, adjust_kms)(exp, lsm, size, shrink);
1154         return rc;
1155 }
1156
1157 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1158                                 int len, void *karg, void *uarg)
1159 {
1160         int rc;
1161
1162         EXP_CHECK_DT_OP(exp, iocontrol);
1163         EXP_COUNTER_INCREMENT(exp, iocontrol);
1164
1165         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1166         return rc;
1167 }
1168
1169 static inline int obd_find_cbdata(struct obd_export *exp,
1170                                   struct lov_stripe_md *lsm,
1171                                   ldlm_iterator_t it, void *data)
1172 {
1173         int rc;
1174
1175         EXP_CHECK_DT_OP(exp, find_cbdata);
1176         EXP_COUNTER_INCREMENT(exp, find_cbdata);
1177
1178         rc = OBP(exp->exp_obd, find_cbdata)(exp, lsm, it, data);
1179         return rc;
1180 }
1181
1182 static inline void obd_import_event(struct obd_device *obd,
1183                                     struct obd_import *imp,
1184                                     enum obd_import_event event)
1185 {
1186         if (!obd) {
1187                 CERROR("NULL device\n");
1188                 return;
1189         }
1190         if (obd->obd_set_up && OBP(obd, import_event)) {
1191                 OBD_COUNTER_INCREMENT(obd, import_event);
1192                 OBP(obd, import_event)(obd, imp, event);
1193         }
1194 }
1195
1196 static inline int obd_notify(struct obd_device *obd,
1197                              struct obd_device *watched,
1198                              enum obd_notify_event ev,
1199                              void *data)
1200 {
1201         int rc;
1202
1203         rc = obd_check_dev(obd);
1204         if (rc)
1205                 return rc;
1206
1207         /* the check for async_recov is a complete hack - I'm hereby
1208            overloading the meaning to also mean "this was called from
1209            mds_postsetup".  I know that my mds is able to handle notifies
1210            by this point, and it needs to get them to execute mds_postrecov. */
1211         if (!obd->obd_set_up && !obd->obd_async_recov) {
1212                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1213                 return -EINVAL;
1214         }
1215
1216         if (!OBP(obd, notify)) {
1217                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1218                 return -ENOSYS;
1219         }
1220
1221         OBD_COUNTER_INCREMENT(obd, notify);
1222         rc = OBP(obd, notify)(obd, watched, ev, data);
1223         return rc;
1224 }
1225
1226 static inline int obd_notify_observer(struct obd_device *observer,
1227                                       struct obd_device *observed,
1228                                       enum obd_notify_event ev,
1229                                       void *data)
1230 {
1231         int rc1;
1232         int rc2;
1233
1234         struct obd_notify_upcall *onu;
1235
1236         if (observer->obd_observer)
1237                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1238         else
1239                 rc1 = 0;
1240         /*
1241          * Also, call non-obd listener, if any
1242          */
1243         onu = &observer->obd_upcall;
1244         if (onu->onu_upcall != NULL)
1245                 rc2 = onu->onu_upcall(observer, observed, ev,
1246                                       onu->onu_owner, NULL);
1247         else
1248                 rc2 = 0;
1249
1250         return rc1 ? rc1 : rc2;
1251 }
1252
1253 static inline int obd_quotacheck(struct obd_export *exp,
1254                                  struct obd_quotactl *oqctl)
1255 {
1256         int rc;
1257
1258         EXP_CHECK_DT_OP(exp, quotacheck);
1259         EXP_COUNTER_INCREMENT(exp, quotacheck);
1260
1261         rc = OBP(exp->exp_obd, quotacheck)(exp->exp_obd, exp, oqctl);
1262         return rc;
1263 }
1264
1265 static inline int obd_quotactl(struct obd_export *exp,
1266                                struct obd_quotactl *oqctl)
1267 {
1268         int rc;
1269
1270         EXP_CHECK_DT_OP(exp, quotactl);
1271         EXP_COUNTER_INCREMENT(exp, quotactl);
1272
1273         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1274         return rc;
1275 }
1276
1277 static inline int obd_health_check(const struct lu_env *env,
1278                                    struct obd_device *obd)
1279 {
1280         /* returns: 0 on healthy
1281          *       >0 on unhealthy + reason code/flag
1282          *          however the only supported reason == 1 right now
1283          *          We'll need to define some better reasons
1284          *          or flags in the future.
1285          *       <0 on error
1286          */
1287         int rc;
1288
1289         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1290         if (obd == NULL || !OBT(obd)) {
1291                 CERROR("cleaned up obd\n");
1292                 return -EOPNOTSUPP;
1293         }
1294         if (!obd->obd_set_up || obd->obd_stopping)
1295                 return 0;
1296         if (!OBP(obd, health_check))
1297                 return 0;
1298
1299         rc = OBP(obd, health_check)(env, obd);
1300         return rc;
1301 }
1302
1303 static inline int obd_register_observer(struct obd_device *obd,
1304                                         struct obd_device *observer)
1305 {
1306         int rc;
1307
1308         rc = obd_check_dev(obd);
1309         if (rc)
1310                 return rc;
1311         down_write(&obd->obd_observer_link_sem);
1312         if (obd->obd_observer && observer) {
1313                 up_write(&obd->obd_observer_link_sem);
1314                 return -EALREADY;
1315         }
1316         obd->obd_observer = observer;
1317         up_write(&obd->obd_observer_link_sem);
1318         return 0;
1319 }
1320
1321 #if 0
1322 static inline int obd_register_page_removal_cb(struct obd_export *exp,
1323                                                obd_page_removal_cb_t cb,
1324                                                obd_pin_extent_cb pin_cb)
1325 {
1326         int rc;
1327
1328         OBD_CHECK_DT_OP(exp->exp_obd, register_page_removal_cb, 0);
1329         OBD_COUNTER_INCREMENT(exp->exp_obd, register_page_removal_cb);
1330
1331         rc = OBP(exp->exp_obd, register_page_removal_cb)(exp, cb, pin_cb);
1332         return rc;
1333 }
1334
1335 static inline int obd_unregister_page_removal_cb(struct obd_export *exp,
1336                                                  obd_page_removal_cb_t cb)
1337 {
1338         int rc;
1339
1340         OBD_CHECK_DT_OP(exp->exp_obd, unregister_page_removal_cb, 0);
1341         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_page_removal_cb);
1342
1343         rc = OBP(exp->exp_obd, unregister_page_removal_cb)(exp, cb);
1344         return rc;
1345 }
1346
1347 static inline int obd_register_lock_cancel_cb(struct obd_export *exp,
1348                                               obd_lock_cancel_cb cb)
1349 {
1350         int rc;
1351
1352         OBD_CHECK_DT_OP(exp->exp_obd, register_lock_cancel_cb, 0);
1353         OBD_COUNTER_INCREMENT(exp->exp_obd, register_lock_cancel_cb);
1354
1355         rc = OBP(exp->exp_obd, register_lock_cancel_cb)(exp, cb);
1356         return rc;
1357 }
1358
1359 static inline int obd_unregister_lock_cancel_cb(struct obd_export *exp,
1360                                                  obd_lock_cancel_cb cb)
1361 {
1362         int rc;
1363
1364         OBD_CHECK_DT_OP(exp->exp_obd, unregister_lock_cancel_cb, 0);
1365         OBD_COUNTER_INCREMENT(exp->exp_obd, unregister_lock_cancel_cb);
1366
1367         rc = OBP(exp->exp_obd, unregister_lock_cancel_cb)(exp, cb);
1368         return rc;
1369 }
1370 #endif
1371
1372 /* metadata helpers */
1373 static inline int md_getstatus(struct obd_export *exp, struct lu_fid *fid)
1374 {
1375         int rc;
1376
1377         EXP_CHECK_MD_OP(exp, getstatus);
1378         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1379         rc = MDP(exp->exp_obd, getstatus)(exp, fid);
1380         return rc;
1381 }
1382
1383 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1384                              struct ptlrpc_request **request)
1385 {
1386         int rc;
1387
1388         EXP_CHECK_MD_OP(exp, getattr);
1389         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1390         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1391         return rc;
1392 }
1393
1394 static inline int md_null_inode(struct obd_export *exp,
1395                                    const struct lu_fid *fid)
1396 {
1397         int rc;
1398
1399         EXP_CHECK_MD_OP(exp, null_inode);
1400         EXP_MD_COUNTER_INCREMENT(exp, null_inode);
1401         rc = MDP(exp->exp_obd, null_inode)(exp, fid);
1402         return rc;
1403 }
1404
1405 static inline int md_find_cbdata(struct obd_export *exp,
1406                                  const struct lu_fid *fid,
1407                                  ldlm_iterator_t it, void *data)
1408 {
1409         int rc;
1410
1411         EXP_CHECK_MD_OP(exp, find_cbdata);
1412         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1413         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1414         return rc;
1415 }
1416
1417 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1418                            struct md_open_data *mod,
1419                            struct ptlrpc_request **request)
1420 {
1421         int rc;
1422
1423         EXP_CHECK_MD_OP(exp, close);
1424         EXP_MD_COUNTER_INCREMENT(exp, close);
1425         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1426         return rc;
1427 }
1428
1429 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1430                             const void *data, int datalen, int mode, __u32 uid,
1431                             __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1432                             struct ptlrpc_request **request)
1433 {
1434         int rc;
1435
1436         EXP_CHECK_MD_OP(exp, create);
1437         EXP_MD_COUNTER_INCREMENT(exp, create);
1438         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1439                                        uid, gid, cap_effective, rdev, request);
1440         return rc;
1441 }
1442
1443 static inline int md_done_writing(struct obd_export *exp,
1444                                   struct md_op_data *op_data,
1445                                   struct md_open_data *mod)
1446 {
1447         int rc;
1448
1449         EXP_CHECK_MD_OP(exp, done_writing);
1450         EXP_MD_COUNTER_INCREMENT(exp, done_writing);
1451         rc = MDP(exp->exp_obd, done_writing)(exp, op_data, mod);
1452         return rc;
1453 }
1454
1455 static inline int md_enqueue(struct obd_export *exp,
1456                              struct ldlm_enqueue_info *einfo,
1457                              struct lookup_intent *it,
1458                              struct md_op_data *op_data,
1459                              struct lustre_handle *lockh,
1460                              void *lmm, int lmmsize,
1461                              struct ptlrpc_request **req,
1462                              __u64 extra_lock_flags)
1463 {
1464         int rc;
1465
1466         EXP_CHECK_MD_OP(exp, enqueue);
1467         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1468         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, it, op_data, lockh,
1469                                         lmm, lmmsize, req, extra_lock_flags);
1470         return rc;
1471 }
1472
1473 static inline int md_getattr_name(struct obd_export *exp,
1474                                   struct md_op_data *op_data,
1475                                   struct ptlrpc_request **request)
1476 {
1477         int rc;
1478
1479         EXP_CHECK_MD_OP(exp, getattr_name);
1480         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1481         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1482         return rc;
1483 }
1484
1485 static inline int md_intent_lock(struct obd_export *exp,
1486                                  struct md_op_data *op_data, void *lmm,
1487                                  int lmmsize, struct lookup_intent *it,
1488                                  int lookup_flags, struct ptlrpc_request **reqp,
1489                                  ldlm_blocking_callback cb_blocking,
1490                                  __u64 extra_lock_flags)
1491 {
1492         int rc;
1493
1494         EXP_CHECK_MD_OP(exp, intent_lock);
1495         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1496         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, lmm, lmmsize,
1497                                             it, lookup_flags, reqp, cb_blocking,
1498                                             extra_lock_flags);
1499         return rc;
1500 }
1501
1502 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1503                           struct ptlrpc_request **request)
1504 {
1505         int rc;
1506
1507         EXP_CHECK_MD_OP(exp, link);
1508         EXP_MD_COUNTER_INCREMENT(exp, link);
1509         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1510         return rc;
1511 }
1512
1513 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1514                             const char *old, int oldlen, const char *new,
1515                             int newlen, struct ptlrpc_request **request)
1516 {
1517         int rc;
1518
1519         EXP_CHECK_MD_OP(exp, rename);
1520         EXP_MD_COUNTER_INCREMENT(exp, rename);
1521         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1522                                        newlen, request);
1523         return rc;
1524 }
1525
1526 static inline int md_is_subdir(struct obd_export *exp,
1527                                const struct lu_fid *pfid,
1528                                const struct lu_fid *cfid,
1529                                struct ptlrpc_request **request)
1530 {
1531         int rc;
1532
1533         EXP_CHECK_MD_OP(exp, is_subdir);
1534         EXP_MD_COUNTER_INCREMENT(exp, is_subdir);
1535         rc = MDP(exp->exp_obd, is_subdir)(exp, pfid, cfid, request);
1536         return rc;
1537 }
1538
1539 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1540                              void *ea, int ealen, void *ea2, int ea2len,
1541                              struct ptlrpc_request **request,
1542                              struct md_open_data **mod)
1543 {
1544         int rc;
1545
1546         EXP_CHECK_MD_OP(exp, setattr);
1547         EXP_MD_COUNTER_INCREMENT(exp, setattr);
1548         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen,
1549                                         ea2, ea2len, request, mod);
1550         return rc;
1551 }
1552
1553 static inline int md_sync(struct obd_export *exp, const struct lu_fid *fid,
1554                           struct ptlrpc_request **request)
1555 {
1556         int rc;
1557
1558         EXP_CHECK_MD_OP(exp, sync);
1559         EXP_MD_COUNTER_INCREMENT(exp, sync);
1560         rc = MDP(exp->exp_obd, sync)(exp, fid, request);
1561         return rc;
1562 }
1563
1564 static inline int md_readpage(struct obd_export *exp, struct md_op_data *opdata,
1565                               struct page **pages,
1566                               struct ptlrpc_request **request)
1567 {
1568         int rc;
1569
1570         EXP_CHECK_MD_OP(exp, readpage);
1571         EXP_MD_COUNTER_INCREMENT(exp, readpage);
1572         rc = MDP(exp->exp_obd, readpage)(exp, opdata, pages, request);
1573         return rc;
1574 }
1575
1576 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1577                             struct ptlrpc_request **request)
1578 {
1579         int rc;
1580
1581         EXP_CHECK_MD_OP(exp, unlink);
1582         EXP_MD_COUNTER_INCREMENT(exp, unlink);
1583         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
1584         return rc;
1585 }
1586
1587 static inline int md_get_lustre_md(struct obd_export *exp,
1588                                    struct ptlrpc_request *req,
1589                                    struct obd_export *dt_exp,
1590                                    struct obd_export *md_exp,
1591                                    struct lustre_md *md)
1592 {
1593         EXP_CHECK_MD_OP(exp, get_lustre_md);
1594         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
1595         return MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md);
1596 }
1597
1598 static inline int md_free_lustre_md(struct obd_export *exp,
1599                                     struct lustre_md *md)
1600 {
1601         EXP_CHECK_MD_OP(exp, free_lustre_md);
1602         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
1603         return MDP(exp->exp_obd, free_lustre_md)(exp, md);
1604 }
1605
1606 static inline int md_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1607                               u64 valid, const char *name,
1608                               const char *input, int input_size,
1609                               int output_size, int flags, __u32 suppgid,
1610                               struct ptlrpc_request **request)
1611 {
1612         EXP_CHECK_MD_OP(exp, setxattr);
1613         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
1614         return MDP(exp->exp_obd, setxattr)(exp, fid, valid, name, input,
1615                                            input_size, output_size, flags,
1616                                            suppgid, request);
1617 }
1618
1619 static inline int md_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1620                               u64 valid, const char *name,
1621                               const char *input, int input_size,
1622                               int output_size, int flags,
1623                               struct ptlrpc_request **request)
1624 {
1625         EXP_CHECK_MD_OP(exp, getxattr);
1626         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
1627         return MDP(exp->exp_obd, getxattr)(exp, fid, valid, name, input,
1628                                            input_size, output_size, flags,
1629                                            request);
1630 }
1631
1632 static inline int md_set_open_replay_data(struct obd_export *exp,
1633                                           struct obd_client_handle *och,
1634                                           struct lookup_intent *it)
1635 {
1636         EXP_CHECK_MD_OP(exp, set_open_replay_data);
1637         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
1638         return MDP(exp->exp_obd, set_open_replay_data)(exp, och, it);
1639 }
1640
1641 static inline int md_clear_open_replay_data(struct obd_export *exp,
1642                                             struct obd_client_handle *och)
1643 {
1644         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
1645         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
1646         return MDP(exp->exp_obd, clear_open_replay_data)(exp, och);
1647 }
1648
1649 static inline int md_set_lock_data(struct obd_export *exp,
1650                                    __u64 *lockh, void *data, __u64 *bits)
1651 {
1652         EXP_CHECK_MD_OP(exp, set_lock_data);
1653         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
1654         return MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits);
1655 }
1656
1657 static inline int md_cancel_unused(struct obd_export *exp,
1658                                    const struct lu_fid *fid,
1659                                    ldlm_policy_data_t *policy,
1660                                    ldlm_mode_t mode,
1661                                    ldlm_cancel_flags_t flags,
1662                                    void *opaque)
1663 {
1664         int rc;
1665
1666         EXP_CHECK_MD_OP(exp, cancel_unused);
1667         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
1668
1669         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
1670                                               flags, opaque);
1671         return rc;
1672 }
1673
1674 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, __u64 flags,
1675                                         const struct lu_fid *fid,
1676                                         ldlm_type_t type,
1677                                         ldlm_policy_data_t *policy,
1678                                         ldlm_mode_t mode,
1679                                         struct lustre_handle *lockh)
1680 {
1681         EXP_CHECK_MD_OP(exp, lock_match);
1682         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
1683         return MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1684                                              policy, mode, lockh);
1685 }
1686
1687 static inline int md_init_ea_size(struct obd_export *exp, int easize,
1688                                   int def_asize, int cookiesize,
1689                                   int def_cookiesize)
1690 {
1691         EXP_CHECK_MD_OP(exp, init_ea_size);
1692         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
1693         return MDP(exp->exp_obd, init_ea_size)(exp, easize, def_asize,
1694                                                cookiesize, def_cookiesize);
1695 }
1696
1697 static inline int md_get_remote_perm(struct obd_export *exp,
1698                                      const struct lu_fid *fid, __u32 suppgid,
1699                                      struct ptlrpc_request **request)
1700 {
1701         EXP_CHECK_MD_OP(exp, get_remote_perm);
1702         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
1703         return MDP(exp->exp_obd, get_remote_perm)(exp, fid, suppgid,
1704                                                   request);
1705 }
1706
1707 static inline int md_intent_getattr_async(struct obd_export *exp,
1708                                           struct md_enqueue_info *minfo,
1709                                           struct ldlm_enqueue_info *einfo)
1710 {
1711         int rc;
1712
1713         EXP_CHECK_MD_OP(exp, intent_getattr_async);
1714         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
1715         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
1716         return rc;
1717 }
1718
1719 static inline int md_revalidate_lock(struct obd_export *exp,
1720                                      struct lookup_intent *it,
1721                                      struct lu_fid *fid, __u64 *bits)
1722 {
1723         int rc;
1724
1725         EXP_CHECK_MD_OP(exp, revalidate_lock);
1726         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
1727         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
1728         return rc;
1729 }
1730
1731 /* OBD Metadata Support */
1732
1733 int obd_init_caches(void);
1734 void obd_cleanup_caches(void);
1735
1736 /* support routines */
1737 extern struct kmem_cache *obdo_cachep;
1738
1739 static inline void obdo2fid(struct obdo *oa, struct lu_fid *fid)
1740 {
1741         /* something here */
1742 }
1743
1744 static inline void fid2obdo(struct lu_fid *fid, struct obdo *oa)
1745 {
1746         /* something here */
1747 }
1748
1749 typedef int (*register_lwp_cb)(void *data);
1750
1751 struct lwp_register_item {
1752         struct obd_export **lri_exp;
1753         register_lwp_cb     lri_cb_func;
1754         void               *lri_cb_data;
1755         struct list_head            lri_list;
1756         char                lri_name[MTI_NAME_MAXLEN];
1757 };
1758
1759 /* I'm as embarrassed about this as you are.
1760  *
1761  * <shaver> // XXX do not look into _superhack with remaining eye
1762  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1763 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1764
1765 /* obd_mount.c */
1766
1767 /* sysctl.c */
1768 int obd_sysctl_init(void);
1769
1770 /* uuid.c  */
1771 typedef __u8 class_uuid_t[16];
1772 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1773
1774 /* lustre_peer.c    */
1775 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
1776 int class_add_uuid(const char *uuid, __u64 nid);
1777 int class_del_uuid (const char *uuid);
1778 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
1779 void class_init_uuidlist(void);
1780 void class_exit_uuidlist(void);
1781
1782 /* class_obd.c */
1783 extern char obd_jobid_node[];
1784 extern struct miscdevice obd_psdev;
1785 extern spinlock_t obd_types_lock;
1786
1787 /* prng.c */
1788 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
1789
1790 #endif /* __LINUX_OBD_CLASS_H */