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