Whamcloud - gitweb
LU-17000 utils: handle_yaml_no_op() has wrong signature
[fs/lustre-release.git] / lustre / include / obd_class.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /*
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2011, 2017, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  *
13  * Header defining common operations on OBD devices.
14  */
15
16 #ifndef __CLASS_OBD_H
17 #define __CLASS_OBD_H
18
19 #include <linux/kobject.h>
20 #include <obd_support.h>
21 #include <lustre_import.h>
22 #include <lustre_net.h>
23 #include <obd.h>
24 #include <lustre_lib.h>
25 #include <uapi/linux/lustre/lustre_idl.h>
26 #include <lprocfs_status.h>
27 #ifdef HAVE_SERVER_SUPPORT
28 #include <lu_target.h>
29 #include <obd_target.h>
30 #include <dt_object.h>
31 #endif
32
33 #ifdef HAVE_SERVER_SUPPORT
34 #define SERVER_ONLY_EXPORT_SYMBOL(symbol)       EXPORT_SYMBOL(symbol)
35 #define SERVER_ONLY
36 #else
37 #define SERVER_ONLY static
38 #define SERVER_ONLY_EXPORT_SYMBOL(symbol)
39 #endif
40
41 #define OBD_STATFS_NODELAY      0x0001  /* requests should be send without delay
42                                          * and resends for avoid deadlocks */
43 #define OBD_STATFS_FROM_CACHE   0x0002  /* the statfs callback should not update
44                                          * obd_osfs_age */
45 #define OBD_STATFS_FOR_MDT0     0x0004  /* The statfs is only for retrieving
46                                          * information from MDT0. */
47 #define OBD_STATFS_SUM          0x0008  /* get aggregated statfs from MDT */
48 #define OBD_STATFS_NESTED       0x0010  /* Call while already holding
49                                          * obd_dev_mutex of a difference
50                                          * device.
51                                          */
52
53 #define OBD_MAX_INDEX xa_limit_31b.max
54
55 #define obd_device_find(devno)                                          \
56         xa_find(&obd_devs, &devno, OBD_MAX_INDEX, XA_PRESENT)
57
58 #define obd_device_find_after(devno)                                    \
59         xa_find_after(&obd_devs, &devno, OBD_MAX_INDEX, XA_PRESENT)
60
61 #define obd_device_for_each(devno, obd)                 \
62         xa_for_each(&obd_devs, devno, obd)
63
64 #define obd_device_for_each_start(devno, obd, start)    \
65         xa_for_each_start(&obd_devs, devno, obd, start)
66
67 #define obd_device_for_each_cond(devno, obd, cond)       \
68         obd_device_for_each(devno, obd)                  \
69         if (cond)
70
71 #define obd_device_for_each_uuid(devno, obd, uuid)       \
72         obd_device_for_each_cond(devno, obd,             \
73                                  obd_uuid_equals(uuid, &obd->obd_uuid))
74
75 #define obd_device_lock() xa_lock(&obd_devs)
76 #define obd_device_unlock() xa_unlock(&obd_devs)
77
78 /* OBD Operations Declarations */
79 extern struct obd_device *class_exp2obd(struct obd_export *);
80 extern int class_handle_ioctl(unsigned int cmd, void __user *uarg);
81 int lustre_get_jobid(char *jobid, size_t len);
82 void lustre_jobid_clear(const char *jobid);
83 void jobid_cache_fini(void);
84 int jobid_cache_init(void);
85 char *jobid_current(void);
86 int jobid_set_current(char *jobid);
87
88 struct lu_device_type;
89
90 /* genops.c */
91 extern struct xarray obd_devs;
92 struct obd_export *class_conn2export(struct lustre_handle *);
93 #ifdef HAVE_SERVER_SUPPORT
94 struct obd_type *class_add_symlinks(const char *name, bool enable_proc);
95 #endif
96 int class_register_type(const struct obd_ops *dt_ops,
97                         const struct md_ops *md_ops, bool enable_proc,
98                         const char *nm, struct lu_device_type *ldt);
99 int class_unregister_type(const char *nm);
100
101 struct obd_device *class_newdev(const char *type_name, const char *name,
102                                 const char *uuid);
103 int class_register_device(struct obd_device *obd);
104 void class_unregister_device(struct obd_device *obd);
105 void class_free_dev(struct obd_device *obd);
106
107 struct obd_device *class_str2obd(const char *str);
108 int class_name2dev(const char *name);
109 struct obd_device *class_name2obd(const char *name);
110 int class_uuid2dev(struct obd_uuid *uuid);
111 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
112 struct obd_device *class_find_client_obd(struct obd_uuid *tgt_uuid,
113                                          const char *type_name,
114                                          struct obd_uuid *grp_uuid);
115 struct obd_device *class_num2obd(int num);
116 int class_obd_devs_count(void);
117
118 int class_notify_sptlrpc_conf(const char *fsname, int namelen);
119
120 static inline char *obd_export_nid2str(struct obd_export *exp)
121 {
122         return exp->exp_connection == NULL ?
123                "<unknown>" : libcfs_nidstr(&exp->exp_connection->c_peer.nid);
124 }
125
126 static inline char *obd_import_nid2str(struct obd_import *imp)
127 {
128         return imp->imp_connection == NULL ?
129                "<unknown>" : libcfs_nidstr(&imp->imp_connection->c_peer.nid);
130 }
131
132 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid);
133 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid);
134 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
135                           const char *sep);
136
137 int obd_zombie_impexp_init(void);
138 void obd_zombie_impexp_stop(void);
139 void obd_zombie_impexp_cull(void);
140 void obd_zombie_barrier(void);
141 void obd_exports_barrier(struct obd_device *obd);
142 int kuc_len(int payload_len);
143 struct kuc_hdr * kuc_ptr(void *p);
144 void *kuc_alloc(int payload_len, int transport, int type);
145 void kuc_free(void *p, int payload_len);
146 int obd_get_request_slot(struct client_obd *cli);
147 void obd_put_request_slot(struct client_obd *cli);
148 __u32 obd_get_max_rpcs_in_flight(struct client_obd *cli);
149 int obd_set_max_rpcs_in_flight(struct client_obd *cli, __u32 max);
150 __u16 obd_get_max_mod_rpcs_in_flight(struct client_obd *cli);
151 int obd_set_max_mod_rpcs_in_flight(struct client_obd *cli, __u16 max);
152 int obd_mod_rpc_stats_seq_show(struct client_obd *cli, struct seq_file *seq);
153
154 /* page_pools.c */
155 int  obd_pool_init(void);
156 void obd_pool_fini(void);
157 void obd_pool_add_user(void);
158 int obd_pool_get_desc_pages(struct ptlrpc_bulk_desc *desc);
159 int obd_pool_get_pages_array(struct page **pa, unsigned int count);
160 int obd_pool_get_objects(void **buf, unsigned int order);
161 void obd_pool_put_desc_pages(struct ptlrpc_bulk_desc *desc);
162 void obd_pool_put_pages_array(struct page **pa, unsigned int count);
163 void obd_pool_put_objects(void *buf, unsigned int order);
164 int obd_pool_get_free_objects(unsigned int order);
165 int pool_is_at_full_capacity(int order);
166
167 /* cfs_hash.c */
168 int cfs_hash_init(void);
169 void cfs_hash_fini(void);
170
171 int encrypt_page_pools_seq_show(struct seq_file *m, void *v);
172 int page_pools_seq_show(struct seq_file *m, void *v);
173
174 __u16 obd_get_mod_rpc_slot(struct client_obd *cli, __u32 opc);
175 void obd_put_mod_rpc_slot(struct client_obd *cli, __u32 opc, __u16 tag);
176
177 struct llog_handle;
178 struct llog_rec_hdr;
179 typedef int (*llog_cb_t)(const struct lu_env *, struct llog_handle *,
180                          struct llog_rec_hdr *, void *);
181
182 struct obd_export *obd_stale_export_get(void);
183 void obd_stale_export_put(struct obd_export *exp);
184 void obd_stale_export_adjust(struct obd_export *exp);
185
186 /* obd_config.c */
187 /* For interoperability */
188 struct cfg_interop_param {
189         char *old_param;
190         char *new_param;
191 };
192
193 #ifdef HAVE_SERVER_SUPPORT
194 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg));
195 #endif
196
197 char *lustre_cfg_string(struct lustre_cfg *lcfg, u32 index);
198 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
199                                      const char *new_name);
200 void print_lustre_cfg(struct lustre_cfg *lcfg);
201 int class_process_config(struct lustre_cfg *lcfg, struct kobject *kobj);
202 ssize_t class_set_global(const char *param);
203 ssize_t class_modify_config(struct lustre_cfg *lcfg, const char *prefix,
204                             struct kobject *kobj);
205 int class_attach(struct lustre_cfg *lcfg);
206 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
207 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
208 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
209
210 int class_find_param(char *buf, char *key, char **valp);
211 struct cfg_interop_param *class_find_old_param(const char *param,
212                                                struct cfg_interop_param *ptr);
213 int class_get_next_param(char **params, char *copy);
214 int class_match_param(char *buf, const char *key, char **valp);
215 int class_parse_nid(char *buf, struct lnet_nid *nid, char **endh);
216 int class_parse_nid_quiet(char *buf, struct lnet_nid *nid, char **endh);
217 int class_parse_net(char *buf, u32 *net, char **endh);
218 int class_match_nid(char *buf, char *key, struct lnet_nid *nid);
219 int class_match_net(char *buf, char *key, u32 net);
220
221 struct obd_device *class_incref(struct obd_device *obd,
222                                 const char *scope, const void *source);
223 void class_decref(struct obd_device *obd,
224                   const char *scope, const void *source);
225 void dump_exports(struct obd_device *obd, int locks, int debug_level);
226 int class_config_llog_handler(const struct lu_env *env,
227                               struct llog_handle *handle,
228                               struct llog_rec_hdr *rec, void *data);
229 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg);
230
231 #define CFG_F_START     0x01   /* Set when we start updating from a log */
232 #define CFG_F_MARKER    0x02   /* We are within a maker */
233 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
234 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
235
236 /* Passed as data param to class_config_parse_llog */
237 struct config_llog_instance {
238         unsigned long            cfg_instance;
239         struct kobject          *cfg_kobj;
240         struct super_block      *cfg_sb;
241         struct obd_uuid          cfg_uuid;
242         llog_cb_t                cfg_callback;
243         int                      cfg_last_idx; /* for partial llog processing */
244         int                      cfg_flags;
245         __u32                    cfg_lwp_idx;
246         __u32                    cfg_sub_clds;
247 };
248 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
249                             char *name, struct config_llog_instance *cfg);
250
251 /**
252  * Generate a unique configuration instance for this mount
253  *
254  * Temporary hack to bypass ASLR in 4.15+ kernels, a better fix soon.
255  * For now, use the same value as before - the superblock pointer value.
256  *
257  * Using the client UUID would be an option, but it needs more testing.
258  */
259 static inline unsigned long ll_get_cfg_instance(struct super_block *sb)
260 {
261         return (unsigned long)sb;
262 }
263
264 #define CONFIG_SUB_SPTLRPC      0x01
265 #define CONFIG_SUB_RECOVER      0x02
266 #define CONFIG_SUB_PARAMS       0x04
267 #define CONFIG_SUB_NODEMAP      0x08
268 #define CONFIG_SUB_BARRIER      0x10
269
270 /* Sub clds should be attached to the config_llog_data when processing
271  * config log for client or server target. */
272 #define CONFIG_SUB_CLIENT       (CONFIG_SUB_SPTLRPC | CONFIG_SUB_RECOVER | \
273                                  CONFIG_SUB_PARAMS)
274 #define CONFIG_SUB_SERVER       (CONFIG_SUB_CLIENT | CONFIG_SUB_NODEMAP | \
275                                  CONFIG_SUB_BARRIER)
276
277 #define PARAMS_FILENAME         "params"
278 #define BARRIER_FILENAME        "barrier"
279 #define LCTL_UPCALL             "lctl"
280
281 static inline bool logname_is_barrier(const char *logname)
282 {
283         char *ptr;
284
285         /* logname for barrier is "fsname-barrier" */
286         ptr = strstr(logname, BARRIER_FILENAME);
287         if (ptr && (ptr - logname) >= 2 &&
288             *(ptr - 1) == '-' && *(ptr + 7) == '\0')
289                 return true;
290
291         return false;
292 }
293
294 /* list of active configuration logs  */
295 struct config_llog_data {
296         struct ldlm_res_id          cld_resid;
297         struct lustre_handle        cld_lockh;
298         struct config_llog_instance cld_cfg;
299         struct list_head            cld_list_chain;/* on config_llog_list */
300         refcount_t                  cld_refcount;
301         struct config_llog_data    *cld_sptlrpc;/* depended sptlrpc log */
302         struct config_llog_data    *cld_params; /* common parameters log */
303         struct config_llog_data    *cld_recover;/* imperative recover log */
304         struct config_llog_data    *cld_nodemap;/* nodemap log */
305         struct config_llog_data    *cld_barrier;/* barrier log (for MDT only) */
306         struct obd_export          *cld_mgcexp;
307         struct mutex                cld_lock;
308         enum mgs_cfg_type           cld_type;
309         unsigned int                cld_stopping:1, /* we were told to stop
310                                                      * watching */
311                                     cld_lostlock:1, /* lock not requeued */
312                                     cld_processed:1;  /* successfully fetched */
313         char                        cld_logname[];
314 };
315
316 struct lustre_profile {
317         struct list_head         lp_list;
318         char                    *lp_profile;
319         char                    *lp_dt;
320         char                    *lp_md;
321         int                      lp_refs;
322         bool                     lp_list_deleted;
323 };
324
325 struct lustre_profile *class_get_profile(const char * prof);
326 void class_del_profile(const char *prof);
327 void class_put_profile(struct lustre_profile *lprof);
328 void class_del_profiles(void);
329
330
331 #if LUSTRE_TRACKS_LOCK_EXP_REFS
332
333 void __class_export_add_lock_ref(struct obd_export *, struct ldlm_lock *);
334 void __class_export_del_lock_ref(struct obd_export *, struct ldlm_lock *);
335 extern void (*class_export_dump_hook)(struct obd_export *);
336
337 #else
338
339 #define __class_export_add_lock_ref(exp, lock)             do {} while (0)
340 #define __class_export_del_lock_ref(exp, lock)             do {} while (0)
341
342 #endif
343
344 #define class_export_rpc_inc(exp)                                       \
345 ({                                                                      \
346         atomic_inc(&(exp)->exp_rpc_count);                              \
347         CDEBUG(D_INFO, "RPC GETting export %p : new rpc_count %d\n",    \
348                (exp), atomic_read(&(exp)->exp_rpc_count));              \
349 })
350
351 #define class_export_rpc_dec(exp)                                       \
352 ({                                                                      \
353         LASSERT(atomic_read(&(exp)->exp_rpc_count) > 0);                \
354         atomic_dec(&(exp)->exp_rpc_count);                              \
355         CDEBUG(D_INFO, "RPC PUTting export %p : new rpc_count %d\n",    \
356                (exp), atomic_read(&(exp)->exp_rpc_count));              \
357 })
358
359 #define class_export_lock_get(exp, lock)                                \
360 ({                                                                      \
361         atomic_inc(&(exp)->exp_locks_count);                            \
362         __class_export_add_lock_ref(exp, lock);                         \
363         CDEBUG(D_INFO, "lock GETting export %p : new locks_count %d\n", \
364                (exp), atomic_read(&(exp)->exp_locks_count));            \
365         class_export_get(exp);                                          \
366 })
367
368 #define class_export_lock_put(exp, lock)                                \
369 ({                                                                      \
370         LASSERT(atomic_read(&(exp)->exp_locks_count) > 0);              \
371         atomic_dec(&(exp)->exp_locks_count);                            \
372         __class_export_del_lock_ref(exp, lock);                         \
373         CDEBUG(D_INFO, "lock PUTting export %p : new locks_count %d\n", \
374                (exp), atomic_read(&(exp)->exp_locks_count));            \
375         class_export_put(exp);                                          \
376 })
377
378 #define class_export_cb_get(exp)                                        \
379 ({                                                                      \
380         atomic_inc(&(exp)->exp_cb_count);                               \
381         CDEBUG(D_INFO, "callback GETting export %p : new cb_count %d\n",\
382                (exp), atomic_read(&(exp)->exp_cb_count));               \
383         class_export_get(exp);                                          \
384 })
385
386 #define class_export_cb_put(exp)                                        \
387 ({                                                                      \
388         LASSERT(atomic_read(&(exp)->exp_cb_count) > 0);                 \
389         atomic_dec(&(exp)->exp_cb_count);                               \
390         CDEBUG(D_INFO, "callback PUTting export %p : new cb_count %d\n",\
391                (exp), atomic_read(&(exp)->exp_cb_count));               \
392         class_export_put(exp);                                          \
393 })
394
395 /* genops.c */
396 struct obd_export *class_export_get(struct obd_export *exp);
397 void class_export_put(struct obd_export *exp);
398 struct obd_export *class_new_export(struct obd_device *obd,
399                                     struct obd_uuid *cluuid);
400 struct obd_export *class_new_export_self(struct obd_device *obd,
401                                          struct obd_uuid *uuid);
402 void class_unlink_export(struct obd_export *exp);
403
404 struct obd_import *class_import_get(struct obd_import *);
405 void class_import_put(struct obd_import *);
406 struct obd_import *class_new_import(struct obd_device *obd);
407 void class_destroy_import(struct obd_import *exp);
408
409 int obd_export_timed_init(struct obd_export *exp, void **data);
410 void obd_export_timed_fini(struct obd_export *exp, void **data);
411 void obd_export_timed_add(struct obd_export *exp, void **data);
412 void obd_export_timed_del(struct obd_export *exp);
413 struct obd_export *obd_export_timed_get(struct obd_device *obd, bool last);
414
415 #ifdef HAVE_SERVER_SUPPORT
416 struct obd_type *class_search_type(const char *name);
417 struct obd_type *class_get_type(const char *name);
418 #endif
419 void class_put_type(struct obd_type *type);
420 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
421                   struct obd_uuid *cluuid);
422 int class_disconnect(struct obd_export *exp);
423 void class_fail_export(struct obd_export *exp);
424 int class_connected_export(struct obd_export *exp);
425 void class_disconnect_exports(struct obd_device *obd);
426 int class_manual_cleanup(struct obd_device *obd);
427 void class_disconnect_stale_exports(struct obd_device *,
428                                     int (*test_export)(struct obd_export *));
429
430 static inline enum obd_option exp_flags_from_obd(struct obd_device *obd)
431 {
432         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
433                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
434                 (test_bit(OBDF_ABORT_RECOVERY, obd->obd_flags) ? OBD_OPT_ABORT_RECOV : 0) |
435                 0);
436 }
437
438 #ifdef HAVE_SERVER_SUPPORT
439 static inline struct lu_target *class_exp2tgt(struct obd_export *exp)
440 {
441         struct obd_device_target *obt;
442
443         LASSERT(exp->exp_obd);
444         obt = (void *)&exp->exp_obd->u;
445         if (obt->obt_magic != OBT_MAGIC)
446                 return NULL;
447         return obt->obt_lut;
448 }
449
450 static inline struct lr_server_data *class_server_data(struct obd_device *obd)
451 {
452         struct obd_device_target *obt = obd2obt(obd);
453
454         LASSERT(obt);
455         return &obt->obt_lut->lut_lsd;
456 }
457 #endif
458
459 /* obdo.c */
460 struct lu_attr;
461 struct inode;
462
463 void obdo_from_la(struct obdo *dst, const struct lu_attr *la, u64 valid);
464 void la_from_obdo(struct lu_attr *la, const struct obdo *dst, u64 valid);
465
466 void obdo_cpy_md(struct obdo *dst, const struct obdo *src, u64 valid);
467 void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj);
468
469 static inline int obd_check_dev(struct obd_device *obd)
470 {
471         if (!obd) {
472                 CERROR("NULL device\n");
473                 return -ENODEV;
474         }
475
476         if (!test_bit(OBDF_SET_UP, (obd)->obd_flags) ||
477             (obd)->obd_stopping) {
478                 CERROR("Device %d not setup\n",
479                        (obd)->obd_minor);
480                 return -ENODEV;
481         }
482
483         return 0;
484 }
485
486 static inline int exp_check_ops(struct obd_export *exp)
487 {
488         if (!exp)
489                 return -ENODEV;
490
491         if (!exp->exp_obd || !exp->exp_obd->obd_type)
492                 return -EOPNOTSUPP;
493
494         return 0;
495 }
496
497 static inline int obd_get_info(const struct lu_env *env, struct obd_export *exp,
498                                __u32 keylen, void *key,
499                                __u32 *vallen, void *val)
500 {
501         int rc;
502
503         ENTRY;
504
505         rc = exp_check_ops(exp);
506         if (rc)
507                 RETURN(rc);
508
509         if (!exp->exp_obd->obd_type->typ_dt_ops->o_get_info) {
510                 CERROR("%s: no %s operation\n",
511                        (exp)->exp_obd->obd_name, __func__);
512                 RETURN(-ENOTSUPP);
513         }
514
515         rc = exp->exp_obd->obd_type->typ_dt_ops->o_get_info(env, exp, keylen,
516                                                             key, vallen, val);
517         RETURN(rc);
518 }
519
520 static inline int obd_set_info_async(const struct lu_env *env,
521                                      struct obd_export *exp,
522                                      __u32 keylen, void *key,
523                                      __u32 vallen, void *val,
524                                      struct ptlrpc_request_set *set)
525 {
526         int rc;
527
528         ENTRY;
529
530         rc = exp_check_ops(exp);
531         if (rc)
532                 RETURN(rc);
533
534         if (!exp->exp_obd->obd_type->typ_dt_ops->o_set_info_async) {
535                 CERROR("%s: no %s operation\n",
536                        (exp)->exp_obd->obd_name, __func__);
537                 RETURN(-ENOTSUPP);
538         }
539
540         rc = exp->exp_obd->obd_type->typ_dt_ops->o_set_info_async(env, exp,
541                                                                   keylen,
542                                                                   key,
543                                                                   vallen,
544                                                                   val, set);
545         RETURN(rc);
546 }
547
548 /*
549  * obd-lu integration.
550  *
551  * Functionality is being moved into new lu_device-based layering, but some
552  * pieces of configuration process are still based on obd devices.
553  *
554  * Specifically, lu_device_type_operations::ldto_device_alloc() methods fully
555  * subsume ->o_setup() methods of obd devices they replace. The same for
556  * lu_device_operations::ldo_process_config() and ->o_process_config(). As a
557  * result, obd_setup() and obd_process_config() branch and call one XOR
558  * another.
559  *
560  * Yet neither lu_device_type_operations::ldto_device_fini() nor
561  * lu_device_type_operations::ldto_device_free() fully implement the
562  * functionality of ->o_precleanup() and ->o_cleanup() they override. Hence,
563  * obd_precleanup() and obd_cleanup() call both lu_device and obd operations.
564  */
565 static inline int obd_setup(struct obd_device *obd, struct lustre_cfg *cfg)
566 {
567         int rc;
568         struct obd_type *type = obd->obd_type;
569         struct lu_device_type *ldt;
570
571         ENTRY;
572
573         wait_var_event(&type->typ_lu,
574                        smp_load_acquire(&type->typ_lu) != OBD_LU_TYPE_SETUP);
575         ldt = type->typ_lu;
576         if (ldt != NULL) {
577                 struct lu_context session_ctx;
578                 struct lu_device *dev;
579                 struct lu_env env;
580
581                 lu_context_init(&session_ctx, LCT_SESSION | LCT_SERVER_SESSION);
582                 session_ctx.lc_thread = NULL;
583                 lu_context_enter(&session_ctx);
584
585                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
586                 if (rc == 0) {
587                         env.le_ses = &session_ctx;
588                         dev = ldto_device_alloc(&env, ldt, cfg);
589                         if (!IS_ERR(dev)) {
590                                 obd->obd_lu_dev = dev;
591                                 dev->ld_obd = obd;
592                                 dev->ld_type = ldt;
593 #ifdef HAVE_SERVER_SUPPORT
594                                 if (lu_device_is_dt(dev) &&
595                                     lu2dt_dev(dev)->dd_rdonly)
596                                         obd->obd_read_only = 1;
597 #endif
598                                 rc = ldto_device_init(&env, dev, ldt->ldt_name, NULL);
599                         } else {
600                                 rc = PTR_ERR(dev);
601                         }
602                         lu_env_fini(&env);
603                 }
604                 lu_context_exit(&session_ctx);
605                 lu_context_fini(&session_ctx);
606         } else {
607                 if (!obd->obd_type->typ_dt_ops->o_setup) {
608                         CERROR("%s: no %s operation\n", obd->obd_name,
609                                __func__);
610                         RETURN(-EOPNOTSUPP);
611                 }
612                 rc = obd->obd_type->typ_dt_ops->o_setup(obd, cfg);
613         }
614         RETURN(rc);
615 }
616
617 static inline int obd_precleanup(struct obd_device *obd)
618 {
619         struct lu_device_type *ldt = obd->obd_type->typ_lu;
620         struct lu_device *d = obd->obd_lu_dev;
621         int rc = -ENOMEM;
622
623         ENTRY;
624
625         if (ldt != NULL && d != NULL) {
626                 struct lu_env *env = lu_env_find();
627                 struct lu_env _env;
628
629                 if (!env && lu_env_init(&_env, ldt->ldt_ctx_tags) == 0) {
630                         env = &_env;
631                         rc = lu_env_add(env);
632                 }
633                 ldto_device_fini(env, d);
634                 if (env == &_env) {
635                         if (rc == 0)
636                                 lu_env_remove(env);
637                         lu_env_fini(env);
638                 }
639         }
640
641         if (!obd->obd_type->typ_dt_ops->o_precleanup)
642                 RETURN(0);
643
644         rc = obd->obd_type->typ_dt_ops->o_precleanup(obd);
645         RETURN(rc);
646 }
647
648 static inline int obd_cleanup(struct obd_device *obd)
649 {
650         int rc;
651         struct lu_device_type *ldt = obd->obd_type->typ_lu;
652         struct lu_device *d = obd->obd_lu_dev;
653
654         ENTRY;
655         if (ldt != NULL && d != NULL) {
656                 struct lu_env env;
657
658                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
659                 if (rc == 0) {
660                         ldto_device_free(&env, d);
661                         lu_env_fini(&env);
662                         obd->obd_lu_dev = NULL;
663                 }
664         }
665         if (!obd->obd_type->typ_dt_ops->o_cleanup)
666                 RETURN(0);
667
668         rc = obd->obd_type->typ_dt_ops->o_cleanup(obd);
669         RETURN(rc);
670 }
671
672 static inline void obd_cleanup_client_import(struct obd_device *obd)
673 {
674         ENTRY;
675
676         /* If we set up but never connected, the client import will not
677          * have been cleaned.
678          */
679         down_write(&obd->u.cli.cl_sem);
680         if (obd->u.cli.cl_import) {
681                 struct obd_import *imp;
682
683                 imp = obd->u.cli.cl_import;
684                 CDEBUG(D_CONFIG, "%s: client import never connected\n",
685                        obd->obd_name);
686                 ptlrpc_invalidate_import(imp);
687                 client_destroy_import(imp);
688                 obd->u.cli.cl_import = NULL;
689         }
690         up_write(&obd->u.cli.cl_sem);
691
692         EXIT;
693 }
694
695 static inline int obd_process_config(struct obd_device *obd, int datalen,
696                                      void *data)
697 {
698         int rc;
699         struct lu_device_type *ldt = obd->obd_type->typ_lu;
700         struct lu_device *d = obd->obd_lu_dev;
701
702         ENTRY;
703
704         obd->obd_process_conf = 1;
705         if (ldt != NULL && d != NULL) {
706                 struct lu_env env;
707
708                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
709                 if (rc == 0) {
710                         rc = d->ld_ops->ldo_process_config(&env, d, data);
711                         lu_env_fini(&env);
712                 }
713         } else {
714                 if (!obd->obd_type->typ_dt_ops->o_process_config) {
715                         CERROR("%s: no %s operation\n",
716                                obd->obd_name, __func__);
717                         RETURN(-EOPNOTSUPP);
718                 }
719                 rc = obd->obd_type->typ_dt_ops->o_process_config(obd, datalen,
720                                                                  data);
721         }
722
723         obd->obd_process_conf = 0;
724
725         RETURN(rc);
726 }
727
728 static inline int obd_create(const struct lu_env *env, struct obd_export *exp,
729                              struct obdo *obdo)
730 {
731         int rc;
732
733         ENTRY;
734
735         rc = exp_check_ops(exp);
736         if (rc)
737                 RETURN(rc);
738
739         if (!exp->exp_obd->obd_type->typ_dt_ops->o_create) {
740                 CERROR("%s: no %s operation\n",
741                        (exp)->exp_obd->obd_name, __func__);
742                 RETURN(-ENOTSUPP);
743         }
744
745         rc = exp->exp_obd->obd_type->typ_dt_ops->o_create(env, exp, obdo);
746         RETURN(rc);
747 }
748
749 static inline int obd_destroy(const struct lu_env *env, struct obd_export *exp,
750                               struct obdo *obdo)
751 {
752         int rc;
753
754         ENTRY;
755
756         rc = exp_check_ops(exp);
757         if (rc)
758                 RETURN(rc);
759
760         if (!exp->exp_obd->obd_type->typ_dt_ops->o_destroy) {
761                 CERROR("%s: no %s operation\n",
762                        (exp)->exp_obd->obd_name, __func__);
763                 RETURN(-ENOTSUPP);
764         }
765
766         rc = exp->exp_obd->obd_type->typ_dt_ops->o_destroy(env, exp, obdo);
767         RETURN(rc);
768 }
769
770 static inline int obd_getattr(const struct lu_env *env, struct obd_export *exp,
771                               struct obdo *oa)
772 {
773         int rc;
774
775         ENTRY;
776         rc = exp_check_ops(exp);
777         if (rc)
778                 RETURN(rc);
779
780         if (!exp->exp_obd->obd_type->typ_dt_ops->o_getattr) {
781                 CERROR("%s: no %s operation\n",
782                        (exp)->exp_obd->obd_name, __func__);
783                 RETURN(-ENOTSUPP);
784         }
785
786         rc = exp->exp_obd->obd_type->typ_dt_ops->o_getattr(env, exp, oa);
787
788         RETURN(rc);
789 }
790
791 static inline int obd_setattr(const struct lu_env *env, struct obd_export *exp,
792                               struct obdo *oa)
793 {
794         int rc;
795
796         ENTRY;
797         rc = exp_check_ops(exp);
798         if (rc)
799                 RETURN(rc);
800
801         if (!exp->exp_obd->obd_type->typ_dt_ops->o_setattr) {
802                 CERROR("%s: no %s operation\n",
803                        (exp)->exp_obd->obd_name, __func__);
804                 RETURN(-ENOTSUPP);
805         }
806
807         rc = exp->exp_obd->obd_type->typ_dt_ops->o_setattr(env, exp, oa);
808
809         RETURN(rc);
810 }
811
812 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
813                                int priority)
814 {
815         struct obd_device *obd = imp->imp_obd;
816         int rc;
817
818         ENTRY;
819
820         rc = obd_check_dev(obd);
821         if (rc)
822                 RETURN(rc);
823
824         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_add_conn) {
825                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
826                 RETURN(-EOPNOTSUPP);
827         }
828
829         rc = obd->obd_type->typ_dt_ops->o_add_conn(imp, uuid, priority);
830         RETURN(rc);
831 }
832
833 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
834 {
835         struct obd_device *obd = imp->imp_obd;
836         int rc;
837
838         ENTRY;
839
840         rc = obd_check_dev(obd);
841         if (rc)
842                 RETURN(rc);
843
844         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_del_conn) {
845                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
846                 RETURN(-EOPNOTSUPP);
847         }
848
849         rc = obd->obd_type->typ_dt_ops->o_del_conn(imp, uuid);
850         RETURN(rc);
851 }
852
853 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
854 {
855         struct obd_uuid *uuid;
856
857         ENTRY;
858
859         if (!exp->exp_obd->obd_type ||
860             !exp->exp_obd->obd_type->typ_dt_ops->o_get_uuid)
861                 RETURN(NULL);
862
863         uuid = exp->exp_obd->obd_type->typ_dt_ops->o_get_uuid(exp);
864         RETURN(uuid);
865 }
866
867 /** Create a new /a exp on device /a obd for the uuid /a cluuid
868  * @param exp New export handle
869  * @param d Connect data, supported flags are set, flags also understood
870  *    by obd are returned.
871  */
872 static inline int obd_connect(const struct lu_env *env,
873                               struct obd_export **exp, struct obd_device *obd,
874                               struct obd_uuid *cluuid,
875                               struct obd_connect_data *data,
876                               void *localdata)
877 {
878         int rc;
879         __u64 ocf = data ? data->ocd_connect_flags : 0;
880
881         ENTRY;
882
883         rc = obd_check_dev(obd);
884         if (rc)
885                 RETURN(rc);
886
887         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_connect) {
888                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
889                 RETURN(-EOPNOTSUPP);
890         }
891
892         rc = obd->obd_type->typ_dt_ops->o_connect(env, exp, obd, cluuid, data,
893                                                   localdata);
894         /* check that only subset is granted */
895         LASSERT(ergo(data != NULL, (data->ocd_connect_flags & ocf) ==
896                                     data->ocd_connect_flags));
897         RETURN(rc);
898 }
899
900 static inline int obd_reconnect(const struct lu_env *env,
901                                 struct obd_export *exp,
902                                 struct obd_device *obd,
903                                 struct obd_uuid *cluuid,
904                                 struct obd_connect_data *d,
905                                 void *localdata)
906 {
907         int rc;
908         __u64 ocf = d ? d->ocd_connect_flags : 0;
909
910         ENTRY;
911
912         rc = obd_check_dev(obd);
913         if (rc)
914                 RETURN(rc);
915
916         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_reconnect)
917                 RETURN(0);
918
919         rc = obd->obd_type->typ_dt_ops->o_reconnect(env, exp, obd, cluuid, d,
920                                                     localdata);
921         /* check that only subset is granted */
922         LASSERT(ergo(d != NULL,
923                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
924         RETURN(rc);
925 }
926
927 static inline int obd_disconnect(struct obd_export *exp)
928 {
929         int rc;
930
931         ENTRY;
932         rc = exp_check_ops(exp);
933         if (rc)
934                 RETURN(rc);
935
936         if (!exp->exp_obd->obd_type->typ_dt_ops->o_disconnect) {
937                 CERROR("%s: no %s operation\n",
938                        (exp)->exp_obd->obd_name, __func__);
939                 RETURN(-ENOTSUPP);
940         }
941
942         rc = exp->exp_obd->obd_type->typ_dt_ops->o_disconnect(exp);
943         RETURN(rc);
944 }
945
946 static inline int obd_fid_alloc(const struct lu_env *env,
947                                 struct obd_export *exp,
948                                 struct lu_fid *fid,
949                                 struct md_op_data *op_data)
950 {
951         int rc;
952
953         ENTRY;
954         rc = exp_check_ops(exp);
955         if (rc)
956                 RETURN(rc);
957
958         if (!exp->exp_obd->obd_type->typ_dt_ops->o_fid_alloc) {
959                 CERROR("%s: no %s operation\n",
960                        (exp)->exp_obd->obd_name, __func__);
961                 RETURN(-ENOTSUPP);
962         }
963
964         rc = exp->exp_obd->obd_type->typ_dt_ops->o_fid_alloc(env, exp, fid,
965                                                              op_data);
966         RETURN(rc);
967 }
968
969 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
970 {
971         int rc;
972
973         ENTRY;
974
975         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_new) {
976                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
977                 RETURN(-EOPNOTSUPP);
978         }
979
980         /* Check poolname validity */
981         if (!poolname || poolname[0] == '\0' || lov_pool_is_reserved(poolname))
982                 RETURN(-EINVAL);
983
984         rc = obd->obd_type->typ_dt_ops->o_pool_new(obd, poolname);
985         RETURN(rc);
986 }
987
988 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
989 {
990         int rc;
991
992         ENTRY;
993         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_del) {
994                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
995                 RETURN(-EOPNOTSUPP);
996         }
997
998         rc = obd->obd_type->typ_dt_ops->o_pool_del(obd, poolname);
999         RETURN(rc);
1000 }
1001
1002 static inline int obd_pool_add(struct obd_device *obd, char *poolname,
1003                                char *ostname)
1004 {
1005         int rc;
1006
1007         ENTRY;
1008
1009         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_add) {
1010                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
1011                 RETURN(-EOPNOTSUPP);
1012         }
1013
1014         rc = obd->obd_type->typ_dt_ops->o_pool_add(obd, poolname, ostname);
1015         RETURN(rc);
1016 }
1017
1018 static inline int obd_pool_rem(struct obd_device *obd, char *poolname,
1019                                char *ostname)
1020 {
1021         int rc;
1022
1023         ENTRY;
1024         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_rem) {
1025                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
1026                 RETURN(-EOPNOTSUPP);
1027         }
1028
1029         rc = obd->obd_type->typ_dt_ops->o_pool_rem(obd, poolname, ostname);
1030         RETURN(rc);
1031 }
1032
1033 static inline int obd_init_export(struct obd_export *exp)
1034 {
1035         int rc = 0;
1036
1037         ENTRY;
1038         if (exp->exp_obd != NULL && exp->exp_obd->obd_type &&
1039             exp->exp_obd->obd_type->typ_dt_ops->o_init_export)
1040                 rc = exp->exp_obd->obd_type->typ_dt_ops->o_init_export(exp);
1041         RETURN(rc);
1042 }
1043
1044 static inline int obd_destroy_export(struct obd_export *exp)
1045 {
1046         ENTRY;
1047         if (exp->exp_obd != NULL && exp->exp_obd->obd_type &&
1048             exp->exp_obd->obd_type->typ_dt_ops->o_destroy_export)
1049                 exp->exp_obd->obd_type->typ_dt_ops->o_destroy_export(exp);
1050         RETURN(0);
1051 }
1052
1053 /* @max_age is the oldest time in seconds that we accept using a cached data.
1054  * If the cache is older than @max_age we will get a new value from the
1055  * target. Use a value of 'ktime_get_seconds() + X' to guarantee freshness.
1056  */
1057 static inline int obd_statfs_async(struct obd_export *exp,
1058                                    struct obd_info *oinfo,
1059                                    time64_t max_age,
1060                                    struct ptlrpc_request_set *rqset)
1061 {
1062         struct obd_device *obd;
1063         int rc = 0;
1064
1065         ENTRY;
1066
1067         if (exp == NULL || exp->exp_obd == NULL)
1068                 RETURN(-EINVAL);
1069
1070         obd = exp->exp_obd;
1071         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_statfs) {
1072                 rc = -EOPNOTSUPP;
1073                 CERROR("%s: no statfs operation: rc = %d\n", obd->obd_name, rc);
1074                 RETURN(rc);
1075         }
1076
1077         CDEBUG(D_SUPER, "%s: age %lld, max_age %lld\n",
1078                obd->obd_name, obd->obd_osfs_age, max_age);
1079         rc = obd->obd_type->typ_dt_ops->o_statfs_async(exp, oinfo, max_age,
1080                                                        rqset);
1081
1082         RETURN(rc);
1083 }
1084
1085 /* @max_age is the oldest time in seconds that we accept using a cached data.
1086  * If the cache is older than @max_age we will get a new value from the
1087  * target. Use a value of 'ktime_get_seconds() + X' to guarantee freshness.
1088  */
1089 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1090                              struct obd_statfs *osfs, time64_t max_age,
1091                              __u32 flags)
1092 {
1093         struct obd_device *obd;
1094         int rc = 0;
1095
1096         ENTRY;
1097         if (unlikely(exp == NULL || exp->exp_obd == NULL))
1098                 RETURN(-EINVAL);
1099
1100         obd = exp->exp_obd;
1101
1102         rc = obd_check_dev(obd);
1103         if (rc)
1104                 RETURN(rc);
1105
1106         if (unlikely(!obd->obd_type || !obd->obd_type->typ_dt_ops->o_statfs)) {
1107                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
1108                 RETURN(-EOPNOTSUPP);
1109         }
1110
1111         CDEBUG(D_SUPER, "%s: age %lld, max_age %lld\n",
1112                obd->obd_name, obd->obd_osfs_age, max_age);
1113         /* ignore cache if aggregated isn't expected */
1114         if (obd->obd_osfs_age < max_age ||
1115             ((obd->obd_osfs.os_state & OS_STATFS_SUM) &&
1116              !(flags & OBD_STATFS_SUM))) {
1117                 /* the RPC will block anyway, so avoid sending many at once */
1118                 rc = mutex_lock_interruptible_nested(&obd->obd_dev_mutex,
1119                                                      (flags & OBD_STATFS_NESTED)
1120                                                      ? SINGLE_DEPTH_NESTING : 0);
1121                 if (rc)
1122                         RETURN(rc);
1123                 if (obd->obd_osfs_age < max_age ||
1124                     ((obd->obd_osfs.os_state & OS_STATFS_SUM) &&
1125                      !(flags & OBD_STATFS_SUM))) {
1126                         rc = obd->obd_type->typ_dt_ops->o_statfs(env, exp, osfs,
1127                                                                  max_age,
1128                                                                  flags);
1129                 } else {
1130                         mutex_unlock(&obd->obd_dev_mutex);
1131                         GOTO(cached, rc = 0);
1132                 }
1133                 if (rc == 0) {
1134                         CDEBUG(D_SUPER,
1135                                "%s: update %p cache blocks %llu/%llu objects %llu/%llu\n",
1136                                obd->obd_name, &obd->obd_osfs,
1137                                osfs->os_bavail, osfs->os_blocks,
1138                                osfs->os_ffree, osfs->os_files);
1139
1140                         spin_lock(&obd->obd_osfs_lock);
1141                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1142                         obd->obd_osfs_age = ktime_get_seconds();
1143                         spin_unlock(&obd->obd_osfs_lock);
1144                 }
1145                 mutex_unlock(&obd->obd_dev_mutex);
1146         } else {
1147 cached:
1148                 CDEBUG(D_SUPER,
1149                        "%s: use %p cache blocks %llu/%llu objects %llu/%llu\n",
1150                        obd->obd_name, &obd->obd_osfs,
1151                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1152                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1153                 spin_lock(&obd->obd_osfs_lock);
1154                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1155                 spin_unlock(&obd->obd_osfs_lock);
1156         }
1157         RETURN(rc);
1158 }
1159
1160 static inline int obd_preprw(const struct lu_env *env, int cmd,
1161                              struct obd_export *exp, struct obdo *oa,
1162                              int objcount, struct obd_ioobj *obj,
1163                              struct niobuf_remote *remote, int *pages,
1164                              struct niobuf_local *local)
1165 {
1166         int rc;
1167
1168         ENTRY;
1169
1170         rc = exp_check_ops(exp);
1171         if (rc)
1172                 RETURN(rc);
1173
1174         if (!exp->exp_obd->obd_type->typ_dt_ops->o_preprw) {
1175                 CERROR("%s: no %s operation\n",
1176                        (exp)->exp_obd->obd_name, __func__);
1177                 RETURN(-ENOTSUPP);
1178         }
1179
1180         rc = exp->exp_obd->obd_type->typ_dt_ops->o_preprw(env, cmd, exp,
1181                                                           oa, objcount, obj,
1182                                                           remote, pages, local);
1183
1184         RETURN(rc);
1185 }
1186
1187 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1188                                struct obd_export *exp, struct obdo *oa,
1189                                int objcount, struct obd_ioobj *obj,
1190                                struct niobuf_remote *rnb, int pages,
1191                                struct niobuf_local *local, const int orig_rc,
1192                                int nob, ktime_t kstart)
1193 {
1194         int rc;
1195
1196         ENTRY;
1197
1198         rc = exp_check_ops(exp);
1199         if (rc)
1200                 RETURN(rc);
1201
1202         if (!exp->exp_obd->obd_type->typ_dt_ops->o_commitrw) {
1203                 CERROR("%s: no %s operation\n",
1204                        (exp)->exp_obd->obd_name, __func__);
1205                 RETURN(-ENOTSUPP);
1206         }
1207
1208         rc = exp->exp_obd->obd_type->typ_dt_ops->o_commitrw(env, cmd, exp, oa,
1209                                                             objcount, obj,
1210                                                             rnb, pages, local,
1211                                                             orig_rc, nob,
1212                                                             kstart);
1213
1214         RETURN(rc);
1215 }
1216
1217 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1218                                 int len, void *karg, void __user *uarg)
1219 {
1220         int rc;
1221
1222         ENTRY;
1223
1224         rc = exp_check_ops(exp);
1225         if (rc)
1226                 RETURN(rc);
1227
1228         if (!exp->exp_obd->obd_type->typ_dt_ops->o_iocontrol) {
1229                 CERROR("%s: no %s operation\n",
1230                        (exp)->exp_obd->obd_name, __func__);
1231                 RETURN(-ENOTSUPP);
1232         }
1233
1234         rc = exp->exp_obd->obd_type->typ_dt_ops->o_iocontrol(cmd, exp,
1235                                                              len, karg,
1236                                                              uarg);
1237         RETURN(rc);
1238 }
1239
1240 static inline void obd_import_event(struct obd_device *obd,
1241                                     struct obd_import *imp,
1242                                     enum obd_import_event event)
1243 {
1244         int rc;
1245
1246         ENTRY;
1247
1248         rc = obd_check_dev(obd);
1249         if (rc)
1250                 RETURN_EXIT;
1251
1252         if (obd->obd_type->typ_dt_ops->o_import_event)
1253                 obd->obd_type->typ_dt_ops->o_import_event(obd, imp, event);
1254
1255         EXIT;
1256 }
1257
1258 static inline int obd_notify(struct obd_device *obd,
1259                              struct obd_device *watched,
1260                              enum obd_notify_event ev)
1261 {
1262         int rc;
1263
1264         ENTRY;
1265
1266         rc = obd_check_dev(obd);
1267         if (rc)
1268                 return rc;
1269
1270         if (!obd->obd_type->typ_dt_ops->o_notify) {
1271                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1272                 RETURN(-EOPNOTSUPP);
1273         }
1274
1275         rc = obd->obd_type->typ_dt_ops->o_notify(obd, watched, ev);
1276
1277         RETURN(rc);
1278 }
1279
1280 static inline int obd_notify_observer(struct obd_device *observer,
1281                                       struct obd_device *observed,
1282                                       enum obd_notify_event ev)
1283 {
1284         int rc = 0;
1285         int rc2 = 0;
1286         struct obd_notify_upcall *onu;
1287
1288         if (WARN_ON_ONCE(!observer))
1289                 return -ENODEV;
1290
1291         if (observer->obd_observer)
1292                 rc = obd_notify(observer->obd_observer, observed, ev);
1293
1294         /*
1295          * Also, call non-obd listener, if any
1296          */
1297         onu = &observer->obd_upcall;
1298         if (onu->onu_upcall != NULL)
1299                 rc2 = onu->onu_upcall(observer, observed, ev, onu->onu_owner);
1300
1301         return rc ? rc : rc2;
1302 }
1303
1304 static inline int obd_quotactl(struct obd_export *exp,
1305                                struct obd_quotactl *oqctl)
1306 {
1307         int rc;
1308
1309         ENTRY;
1310
1311         rc = exp_check_ops(exp);
1312         if (rc)
1313                 RETURN(rc);
1314
1315         if (!exp->exp_obd->obd_type->typ_dt_ops->o_quotactl) {
1316                 CERROR("%s: no %s operation\n",
1317                        (exp)->exp_obd->obd_name, __func__);
1318                 RETURN(-ENOTSUPP);
1319         }
1320
1321         rc = exp->exp_obd->obd_type->typ_dt_ops->o_quotactl(exp->exp_obd,
1322                                                             exp, oqctl);
1323         RETURN(rc);
1324 }
1325
1326 static inline int obd_quota_iter(struct obd_export *exp,
1327                                  struct obd_quotactl *oqctl,
1328                                  struct list_head *list)
1329 {
1330         int rc = 0;
1331
1332         do {
1333                 oqctl->qc_iter_list = (uintptr_t)list;
1334                 rc = obd_quotactl(exp, oqctl);
1335                 if (rc)
1336                         break;
1337
1338         } while (oqctl->qc_iter_md_offset || oqctl->qc_iter_dt_offset);
1339
1340         return rc;
1341 }
1342
1343 static inline int obd_health_check(const struct lu_env *env,
1344                                    struct obd_device *obd)
1345 {
1346         /* returns: 0 on healthy
1347          *         >0 on unhealthy + reason code/flag
1348          *            however the only suppored reason == 1 right now
1349          *            We'll need to define some better reasons
1350          *            or flags in the future.
1351          *         <0 on error
1352          */
1353         int rc;
1354
1355         ENTRY;
1356
1357         /* NULL method is normal here */
1358         if (obd == NULL || !obd->obd_type) {
1359                 CERROR("cleaned up obd\n");
1360                 RETURN(-EOPNOTSUPP);
1361         }
1362         if (!test_bit(OBDF_SET_UP, obd->obd_flags) || obd->obd_stopping)
1363                 RETURN(0);
1364         if (!obd->obd_type->typ_dt_ops->o_health_check)
1365                 RETURN(0);
1366
1367         rc = obd->obd_type->typ_dt_ops->o_health_check(env, obd);
1368         RETURN(rc);
1369 }
1370
1371 static inline int obd_register_observer(struct obd_device *obd,
1372                                         struct obd_device *observer)
1373 {
1374         int rc;
1375
1376         ENTRY;
1377
1378         rc = obd_check_dev(obd);
1379         if (rc)
1380                 RETURN(rc);
1381
1382         down_write(&obd->obd_observer_link_sem);
1383         if (obd->obd_observer && observer) {
1384                 up_write(&obd->obd_observer_link_sem);
1385                 RETURN(-EALREADY);
1386         }
1387         obd->obd_observer = observer;
1388         up_write(&obd->obd_observer_link_sem);
1389         RETURN(0);
1390 }
1391
1392 /* metadata helpers */
1393 enum mps_stat_idx {
1394         LPROC_MD_CLOSE,
1395         LPROC_MD_CREATE,
1396         LPROC_MD_ENQUEUE,
1397         LPROC_MD_ENQUEUE_ASYNC,
1398         LPROC_MD_GETATTR,
1399         LPROC_MD_INTENT_LOCK,
1400         LPROC_MD_LINK,
1401         LPROC_MD_RENAME,
1402         LPROC_MD_SETATTR,
1403         LPROC_MD_FSYNC,
1404         LPROC_MD_READ_PAGE,
1405         LPROC_MD_UNLINK,
1406         LPROC_MD_SETXATTR,
1407         LPROC_MD_GETXATTR,
1408         LPROC_MD_INTENT_GETATTR_ASYNC,
1409         LPROC_MD_REVALIDATE_LOCK,
1410         LPROC_MD_LAST_OPC,
1411 };
1412
1413 static inline int md_get_root(struct obd_export *exp, const char *fileset,
1414                               struct lu_fid *fid)
1415 {
1416         int rc;
1417
1418         rc = exp_check_ops(exp);
1419         if (rc)
1420                 return rc;
1421
1422         return exp->exp_obd->obd_type->typ_md_ops->m_get_root(exp, fileset,
1423                                                               fid);
1424 }
1425
1426 static inline int md_getattr(struct obd_export *exp,
1427                              struct md_op_data *op_data,
1428                              struct ptlrpc_request **request)
1429 {
1430         int rc;
1431
1432         rc = exp_check_ops(exp);
1433         if (rc)
1434                 return rc;
1435
1436         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1437                              LPROC_MD_GETATTR);
1438
1439         return exp->exp_obd->obd_type->typ_md_ops->m_getattr(exp, op_data,
1440                                                              request);
1441 }
1442
1443 static inline int md_null_inode(struct obd_export *exp,
1444                                    const struct lu_fid *fid)
1445 {
1446         int rc;
1447
1448         rc = exp_check_ops(exp);
1449         if (rc)
1450                 return rc;
1451
1452         return exp->exp_obd->obd_type->typ_md_ops->m_null_inode(exp, fid);
1453 }
1454
1455 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1456                            struct md_open_data *mod,
1457                            struct ptlrpc_request **request)
1458 {
1459         int rc;
1460
1461         rc = exp_check_ops(exp);
1462         if (rc)
1463                 return rc;
1464
1465         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1466                              LPROC_MD_CLOSE);
1467
1468         return exp->exp_obd->obd_type->typ_md_ops->m_close(exp, op_data, mod,
1469                                                            request);
1470 }
1471
1472 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1473                             const void *data, size_t datalen, umode_t mode,
1474                             uid_t uid, gid_t gid, kernel_cap_t cap_effective,
1475                             __u64 rdev, struct ptlrpc_request **request)
1476 {
1477         int rc;
1478
1479         rc = exp_check_ops(exp);
1480         if (rc)
1481                 return rc;
1482
1483         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1484                              LPROC_MD_CREATE);
1485
1486         return exp->exp_obd->obd_type->typ_md_ops->m_create(exp, op_data, data,
1487                                                             datalen, mode, uid,
1488                                                             gid, cap_effective,
1489                                                             rdev, request);
1490 }
1491
1492 static inline int md_enqueue(struct obd_export *exp,
1493                              struct ldlm_enqueue_info *einfo,
1494                              const union ldlm_policy_data *policy,
1495                              struct md_op_data *op_data,
1496                              struct lustre_handle *lockh,
1497                              __u64 extra_lock_flags)
1498 {
1499         int rc;
1500
1501         rc = exp_check_ops(exp);
1502         if (rc)
1503                 return rc;
1504
1505         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1506                              LPROC_MD_ENQUEUE);
1507
1508         return exp->exp_obd->obd_type->typ_md_ops->m_enqueue(exp, einfo, policy,
1509                                                              op_data, lockh,
1510                                                              extra_lock_flags);
1511 }
1512
1513 static inline int md_enqueue_async(struct obd_export *exp,
1514                                    struct ldlm_enqueue_info *einfo,
1515                                    obd_enqueue_update_f upcall,
1516                                    struct md_op_data *op_data,
1517                                    const union ldlm_policy_data *policy,
1518                                    __u64 lock_flags)
1519 {
1520         int rc;
1521
1522         ENTRY;
1523         rc = exp_check_ops(exp);
1524         if (rc)
1525                 RETURN(rc);
1526
1527         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1528                              LPROC_MD_ENQUEUE_ASYNC);
1529
1530         rc = exp->exp_obd->obd_type->typ_md_ops->m_enqueue_async(exp, einfo,
1531                         upcall, op_data,
1532                         policy, lock_flags);
1533         RETURN(rc);
1534 }
1535
1536 static inline int md_getattr_name(struct obd_export *exp,
1537                                   struct md_op_data *op_data,
1538                                   struct ptlrpc_request **request)
1539 {
1540         int rc;
1541
1542         rc = exp_check_ops(exp);
1543         if (rc)
1544                 return rc;
1545
1546         return exp->exp_obd->obd_type->typ_md_ops->m_getattr_name(exp, op_data,
1547                                                                   request);
1548 }
1549
1550 static inline int md_intent_lock(struct obd_export *exp,
1551                                  struct md_op_data *op_data,
1552                                  struct lookup_intent *it,
1553                                  struct ptlrpc_request **reqp,
1554                                  ldlm_blocking_callback cb_blocking,
1555                                  __u64 extra_lock_flags)
1556 {
1557         int rc;
1558
1559         rc = exp_check_ops(exp);
1560         if (rc)
1561                 return rc;
1562
1563         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1564                              LPROC_MD_INTENT_LOCK);
1565
1566         return exp->exp_obd->obd_type->typ_md_ops->m_intent_lock(exp, op_data,
1567                                                                  it, reqp,
1568                                                                  cb_blocking,
1569                                                                  extra_lock_flags);
1570 }
1571
1572 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1573                           struct ptlrpc_request **request)
1574 {
1575         int rc;
1576
1577         rc = exp_check_ops(exp);
1578         if (rc)
1579                 return rc;
1580
1581         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1582                              LPROC_MD_LINK);
1583
1584         return exp->exp_obd->obd_type->typ_md_ops->m_link(exp, op_data,
1585                                                           request);
1586 }
1587
1588 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1589                             const char *old_name, size_t oldlen,
1590                             const char *new_name, size_t newlen,
1591                             struct ptlrpc_request **request)
1592 {
1593         int rc;
1594
1595         rc = exp_check_ops(exp);
1596         if (rc)
1597                 return rc;
1598
1599         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1600                              LPROC_MD_RENAME);
1601
1602         return exp->exp_obd->obd_type->typ_md_ops->m_rename(exp, op_data,
1603                                                             old_name,
1604                                                             oldlen, new_name,
1605                                                             newlen, request);
1606 }
1607
1608 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1609                              void *ea, size_t ealen,
1610                              struct ptlrpc_request **request)
1611 {
1612         int rc;
1613
1614         rc = exp_check_ops(exp);
1615         if (rc)
1616                 return rc;
1617
1618         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1619                              LPROC_MD_SETATTR);
1620
1621         return exp->exp_obd->obd_type->typ_md_ops->m_setattr(exp, op_data, ea,
1622                                                              ealen, request);
1623 }
1624
1625 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
1626                            struct ptlrpc_request **request)
1627 {
1628         int rc;
1629
1630         rc = exp_check_ops(exp);
1631         if (rc)
1632                 return rc;
1633
1634         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1635                              LPROC_MD_FSYNC);
1636
1637         return exp->exp_obd->obd_type->typ_md_ops->m_fsync(exp, fid, request);
1638 }
1639
1640 /* FLR: resync mirrored files. */
1641 static inline int md_file_resync(struct obd_export *exp,
1642                                  struct md_op_data *data)
1643 {
1644         int rc;
1645
1646         rc = exp_check_ops(exp);
1647         if (rc)
1648                 return rc;
1649
1650         return exp->exp_obd->obd_type->typ_md_ops->m_file_resync(exp, data);
1651 }
1652
1653 static inline int md_read_page(struct obd_export *exp,
1654                                struct md_op_data *op_data,
1655                                struct md_readdir_info *mrinfo,
1656                                __u64  hash_offset, struct page **ppage)
1657 {
1658         int rc;
1659
1660         rc = exp_check_ops(exp);
1661         if (rc)
1662                 return rc;
1663
1664         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1665                              LPROC_MD_READ_PAGE);
1666
1667         return exp->exp_obd->obd_type->typ_md_ops->m_read_page(exp, op_data,
1668                                                                mrinfo,
1669                                                                hash_offset,
1670                                                                ppage);
1671 }
1672
1673 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1674                             struct ptlrpc_request **request)
1675 {
1676         int rc;
1677
1678         rc = exp_check_ops(exp);
1679         if (rc)
1680                 return rc;
1681
1682         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1683                              LPROC_MD_UNLINK);
1684
1685         return exp->exp_obd->obd_type->typ_md_ops->m_unlink(exp, op_data,
1686                                                             request);
1687 }
1688
1689 static inline int md_get_lustre_md(struct obd_export *exp,
1690                                    struct req_capsule *pill,
1691                                    struct obd_export *dt_exp,
1692                                    struct obd_export *md_exp,
1693                                    struct lustre_md *md)
1694 {
1695         int rc;
1696
1697         rc = exp_check_ops(exp);
1698         if (rc)
1699                 return rc;
1700
1701         return exp->exp_obd->obd_type->typ_md_ops->m_get_lustre_md(exp, pill,
1702                                                                    dt_exp,
1703                                                                    md_exp, md);
1704 }
1705
1706 static inline int md_put_lustre_md(struct obd_export *exp,
1707                                     struct lustre_md *md)
1708 {
1709         int rc;
1710
1711         rc = exp_check_ops(exp);
1712         if (rc)
1713                 return rc;
1714
1715         return exp->exp_obd->obd_type->typ_md_ops->m_put_lustre_md(exp, md);
1716 }
1717
1718 static inline int md_merge_attr(struct obd_export *exp,
1719                                 const struct lmv_stripe_object *lso,
1720                                 struct cl_attr *attr,
1721                                 ldlm_blocking_callback cb)
1722 {
1723         int rc;
1724
1725         rc = exp_check_ops(exp);
1726         if (rc)
1727                 return rc;
1728
1729         return exp->exp_obd->obd_type->typ_md_ops->m_merge_attr(exp, lso, attr,
1730                                                                 cb);
1731 }
1732
1733 static inline int md_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1734                               u64 obd_md_valid, const char *name,
1735                               const void *value, size_t value_size,
1736                               unsigned int xattr_flags, u32 suppgid, u32 projid,
1737                               struct ptlrpc_request **req)
1738 {
1739         int rc;
1740
1741         rc = exp_check_ops(exp);
1742         if (rc)
1743                 return rc;
1744
1745         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1746                              LPROC_MD_SETXATTR);
1747
1748         return exp->exp_obd->obd_type->typ_md_ops->m_setxattr(exp, fid,
1749                                                               obd_md_valid,
1750                                                               name, value,
1751                                                               value_size,
1752                                                               xattr_flags,
1753                                                               suppgid, projid,
1754                                                               req);
1755 }
1756
1757 static inline int md_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1758                               u64 obd_md_valid, const char *name,
1759                               size_t buf_size, u32 projid,
1760                               struct ptlrpc_request **req)
1761 {
1762         int rc;
1763
1764         rc = exp_check_ops(exp);
1765         if (rc)
1766                 return rc;
1767
1768         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1769                              LPROC_MD_GETXATTR);
1770
1771         return exp->exp_obd->obd_type->typ_md_ops->m_getxattr(exp, fid,
1772                                                               obd_md_valid,
1773                                                               name, buf_size,
1774                                                               projid, req);
1775 }
1776
1777 static inline int md_set_open_replay_data(struct obd_export *exp,
1778                                           struct obd_client_handle *och,
1779                                           struct lookup_intent *it)
1780 {
1781         int rc;
1782
1783         rc = exp_check_ops(exp);
1784         if (rc)
1785                 return rc;
1786
1787         return exp->exp_obd->obd_type->typ_md_ops->m_set_open_replay_data(exp,
1788                                                                           och,
1789                                                                           it);
1790 }
1791
1792 static inline int md_clear_open_replay_data(struct obd_export *exp,
1793                                             struct obd_client_handle *och)
1794 {
1795         int rc;
1796
1797         rc = exp_check_ops(exp);
1798         if (rc)
1799                 return rc;
1800
1801         return exp->exp_obd->obd_type->typ_md_ops->m_clear_open_replay_data(exp, och);
1802 }
1803
1804 static inline int md_set_lock_data(struct obd_export *exp,
1805                                    const struct lustre_handle *lockh,
1806                                    void *data, enum mds_ibits_locks *bits)
1807 {
1808         int rc;
1809
1810         rc = exp_check_ops(exp);
1811         if (rc)
1812                 return rc;
1813
1814         return exp->exp_obd->obd_type->typ_md_ops->m_set_lock_data(exp, lockh,
1815                                                                    data,
1816                                                                    bits);
1817 }
1818
1819 static inline
1820 int md_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
1821                      union ldlm_policy_data *policy, enum ldlm_mode mode,
1822                      enum ldlm_cancel_flags cancel_flags, void *opaque)
1823 {
1824         int rc;
1825
1826         rc = exp_check_ops(exp);
1827         if (rc)
1828                 return rc;
1829
1830         return exp->exp_obd->obd_type->typ_md_ops->m_cancel_unused(exp,
1831                                                                    fid, policy,
1832                                                                    mode,
1833                                                                    cancel_flags,
1834                                                                    opaque);
1835 }
1836
1837 static inline enum ldlm_mode md_lock_match(struct obd_export *exp, __u64 flags,
1838                                            const struct lu_fid *fid,
1839                                            enum ldlm_type type,
1840                                            union ldlm_policy_data *policy,
1841                                            enum ldlm_mode mode,
1842                                            enum ldlm_match_flags match_flags,
1843                                            struct lustre_handle *lockh)
1844 {
1845         int rc;
1846
1847         rc = exp_check_ops(exp);
1848         if (rc)
1849                 return rc;
1850
1851         return exp->exp_obd->obd_type->typ_md_ops->m_lock_match(exp, flags,
1852                                                                 fid, type,
1853                                                                 policy, mode,
1854                                                                 match_flags,
1855                                                                 lockh);
1856 }
1857
1858 static inline int md_init_ea_size(struct obd_export *exp, __u32 ea_size,
1859                                   __u32 def_ea_size)
1860 {
1861         int rc;
1862
1863         rc = exp_check_ops(exp);
1864         if (rc)
1865                 return rc;
1866
1867         return exp->exp_obd->obd_type->typ_md_ops->m_init_ea_size(exp, ea_size,
1868                                                                   def_ea_size);
1869 }
1870
1871 static inline int md_intent_getattr_async(struct obd_export *exp,
1872                                           struct md_op_item *item)
1873 {
1874         int rc;
1875
1876         rc = exp_check_ops(exp);
1877         if (rc)
1878                 return rc;
1879
1880         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1881                              LPROC_MD_INTENT_GETATTR_ASYNC);
1882
1883         return exp->exp_obd->obd_type->typ_md_ops->m_intent_getattr_async(exp,
1884                                                                           item);
1885 }
1886
1887 static inline int md_revalidate_lock(struct obd_export *exp,
1888                                      struct lookup_intent *it,
1889                                      struct lu_fid *fid,
1890                                      enum mds_ibits_locks *bits)
1891 {
1892         int rc;
1893
1894         rc = exp_check_ops(exp);
1895         if (rc)
1896                 return rc;
1897
1898         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1899                              LPROC_MD_REVALIDATE_LOCK);
1900
1901         return exp->exp_obd->obd_type->typ_md_ops->m_revalidate_lock(exp,
1902                                                                      it, fid,
1903                                                                      bits);
1904 }
1905
1906 static inline int md_get_fid_from_lsm(struct obd_export *exp,
1907                                       const struct lmv_stripe_object *lso,
1908                                       const char *name, int namelen,
1909                                       struct lu_fid *fid)
1910 {
1911         int rc;
1912
1913         rc = exp_check_ops(exp);
1914         if (rc)
1915                 return rc;
1916
1917         return exp->exp_obd->obd_type->typ_md_ops->m_get_fid_from_lsm(exp,
1918                                                                       lso, name,
1919                                                                       namelen,
1920                                                                       fid);
1921 }
1922
1923 /* Unpack an MD struct from disk to in-memory format.
1924  * Returns +ve size of unpacked MD (0 for free), or -ve error.
1925  *
1926  * If *plsm != NULL and lmm == NULL then *lsm will be freed.
1927  * If *plsm == NULL then it will be allocated.
1928  */
1929 static inline int md_stripe_object_create(struct obd_export *exp,
1930                                           struct lmv_stripe_object **lsop,
1931                                           const union lmv_mds_md *lmm,
1932                                           size_t lmm_size)
1933 {
1934         int rc;
1935
1936         rc = exp_check_ops(exp);
1937         if (rc)
1938                 return rc;
1939
1940         return exp->exp_obd->obd_type->typ_md_ops->m_stripe_object_create(exp,
1941                                                                           lsop,
1942                                                                           lmm,
1943                                                                           lmm_size);
1944 }
1945
1946 static inline int md_rmfid(struct obd_export *exp, struct fid_array *fa,
1947                            int *rcs, struct ptlrpc_request_set *set)
1948 {
1949         int rc;
1950
1951         rc = exp_check_ops(exp);
1952         if (rc)
1953                 return rc;
1954
1955         return exp->exp_obd->obd_type->typ_md_ops->m_rmfid(exp, fa, rcs, set);
1956 }
1957
1958 static inline struct lu_batch *
1959 md_batch_create(struct obd_export *exp, enum lu_batch_flags flags,
1960                 __u32 max_count)
1961 {
1962         int rc;
1963
1964         rc = exp_check_ops(exp);
1965         if (rc)
1966                 return ERR_PTR(rc);
1967
1968         return exp->exp_obd->obd_type->typ_md_ops->m_batch_create(exp, flags,
1969                                                                   max_count);
1970 }
1971
1972 static inline int md_batch_stop(struct obd_export *exp, struct lu_batch *bh)
1973 {
1974         int rc;
1975
1976         rc = exp_check_ops(exp);
1977         if (rc)
1978                 return rc;
1979
1980         return exp->exp_obd->obd_type->typ_md_ops->m_batch_stop(exp, bh);
1981 }
1982
1983 static inline int md_batch_flush(struct obd_export *exp, struct lu_batch *bh,
1984                                  bool wait)
1985 {
1986         int rc;
1987
1988         rc = exp_check_ops(exp);
1989         if (rc)
1990                 return rc;
1991
1992         return exp->exp_obd->obd_type->typ_md_ops->m_batch_flush(exp, bh, wait);
1993 }
1994
1995 static inline int md_batch_add(struct obd_export *exp, struct lu_batch *bh,
1996                                struct md_op_item *item)
1997 {
1998         int rc;
1999
2000         rc = exp_check_ops(exp);
2001         if (rc)
2002                 return rc;
2003
2004         return exp->exp_obd->obd_type->typ_md_ops->m_batch_add(exp, bh, item);
2005 }
2006
2007 /* OBD Metadata Support */
2008
2009 extern int obd_init_caches(void);
2010 extern void obd_cleanup_caches(void);
2011
2012 typedef int (*register_lwp_cb)(void *data);
2013
2014 struct lwp_register_item {
2015         struct obd_export **lri_exp;
2016         register_lwp_cb     lri_cb_func;
2017         void               *lri_cb_data;
2018         struct list_head    lri_list;
2019         atomic_t            lri_ref;
2020         char                lri_name[MTI_NAME_MAXLEN];
2021 };
2022
2023 /* obd_mount.c */
2024 #ifdef HAVE_SERVER_SUPPORT
2025 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
2026                              register_lwp_cb cb_func, void *cb_data);
2027 void lustre_deregister_lwp_item(struct obd_export **exp);
2028 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
2029 void lustre_notify_lwp_list(struct obd_export *exp);
2030 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
2031 #endif /* HAVE_SERVER_SUPPORT */
2032 int lustre_check_exclusion(struct super_block *sb, char *svname);
2033
2034 /* lustre_peer.c    */
2035 int lustre_uuid_to_peer(const char *uuid, struct lnet_nid *peer_nid,
2036                         int index);
2037 int class_add_uuid(const char *uuid, struct lnet_nid *nid);
2038 int class_del_uuid (const char *uuid);
2039 int class_add_nids_to_uuid(struct obd_uuid *uuid, struct lnet_nid *nidlist,
2040                            int nid_count, int nid_size);
2041 int class_check_uuid(struct obd_uuid *uuid, struct lnet_nid *nid);
2042
2043 /* class_obd.c */
2044 extern char obd_jobid_name[];
2045
2046 extern unsigned int obd_lbug_on_eviction;
2047 extern unsigned int obd_dump_on_eviction;
2048
2049 static inline bool do_dump_on_eviction(struct obd_device *exp_obd)
2050 {
2051         if (obd_lbug_on_eviction &&
2052             strncmp(exp_obd->obd_type->typ_name, LUSTRE_MGC_NAME,
2053                     strlen(LUSTRE_MGC_NAME))) {
2054                 CERROR("LBUG upon eviction\n");
2055                 LBUG();
2056         }
2057
2058         return obd_dump_on_eviction;
2059 }
2060
2061 /* statfs_pack.c */
2062 struct kstatfs;
2063 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
2064 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
2065
2066 /* root squash info */
2067 struct root_squash_info {
2068         uid_t                   rsi_uid;
2069         gid_t                   rsi_gid;
2070         struct list_head        rsi_nosquash_nids;
2071         spinlock_t              rsi_lock;
2072 };
2073
2074 int server_name2index(const char *svname, __u32 *idx, const char **endptr);
2075
2076 /* linux-module.c */
2077 struct obd_ioctl_data;
2078 int obd_ioctl_getdata(struct obd_ioctl_data **data, int *len, void __user *arg);
2079 int class_procfs_init(void);
2080 int class_procfs_clean(void);
2081
2082 extern void obd_heat_add(struct obd_heat_instance *instance,
2083                          unsigned int time_second, __u64 count,
2084                          unsigned int weight, unsigned int period_second);
2085 extern void obd_heat_decay(struct obd_heat_instance *instance,
2086                            __u64 time_second, unsigned int weight,
2087                            unsigned int period_second);
2088 extern __u64 obd_heat_get(struct obd_heat_instance *instance,
2089                           unsigned int time_second, unsigned int weight,
2090                           unsigned int period_second);
2091 extern void obd_heat_clear(struct obd_heat_instance *instance, int count);
2092
2093 /* struct kobj_type */
2094 static inline
2095 struct attribute *_get_attr_matches(const struct kobj_type *typ,
2096                                     const char *key, size_t keylen,
2097                                     int (*is_match)(const char *, const char *,
2098                                                     size_t))
2099 {
2100         int i;
2101
2102 #ifdef HAVE_KOBJ_TYPE_DEFAULT_GROUPS
2103         for (i = 0; typ->default_groups[i]; i++) {
2104                 int k;
2105                 struct attribute **attrs;
2106
2107                 attrs = (struct attribute **)typ->default_groups[i]->attrs;
2108                 for (k = 0; attrs[k]; k++) {
2109                         if (is_match(attrs[k]->name, key, keylen))
2110                                 return (struct attribute *)attrs[k];
2111                 }
2112         }
2113 #else
2114         for (i = 0; typ->default_attrs[i]; i++) {
2115                 if (is_match(typ->default_attrs[i]->name, key, keylen))
2116                         return typ->default_attrs[i];
2117         }
2118 #endif
2119         return NULL;
2120 }
2121
2122 static inline
2123 int _attr_name_exact(const char *attr_name, const char *key, size_t len)
2124 {
2125         return !strcmp(attr_name, key);
2126 }
2127
2128 static inline
2129 struct attribute *get_attr_by_name(const struct kobj_type *typ,
2130                                    const char *name)
2131 {
2132         return _get_attr_matches(typ, name, 0, _attr_name_exact);
2133 }
2134
2135 static inline
2136 int _attr_name_starts_with(const char *attr_name, const char *name, size_t len)
2137 {
2138         return !strncmp(attr_name, name, len);
2139 }
2140
2141 static inline
2142 struct attribute *get_attr_starts_with(const struct kobj_type *typ,
2143                                        const char *name,
2144                                        size_t len)
2145 {
2146         return _get_attr_matches(typ, name, len, _attr_name_starts_with);
2147 }
2148
2149 int obd_ioctl_msg(const char *file, const char *func, int line, int level,
2150                   const char *name, unsigned int cmd, const char *msg, int rc);
2151 #define OBD_IOC_DEBUG(level, dev, cmd, msg, rc) \
2152         obd_ioctl_msg(__FILE__, __func__, __LINE__, level, dev, cmd, msg, rc)
2153 #define OBD_IOC_ERROR(dev, cmd, msg, rc)        \
2154         obd_ioctl_msg(__FILE__, __func__, __LINE__, D_ERROR, dev, cmd, msg, rc)
2155
2156 /* ldlm/ldlm_lib.c */
2157 void target_recovery_fini(struct obd_device *obd);
2158 void target_recovery_init(struct lu_target *lut, svc_handler_t handler);
2159
2160
2161 #endif /* __LINUX_OBD_CLASS_H */