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