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