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