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