Whamcloud - gitweb
LU-8066 obd: embed typ_kobj in obd_type
[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
62 struct lu_device_type;
63
64 /* genops.c */
65 struct obd_export *class_conn2export(struct lustre_handle *);
66 #ifdef HAVE_SERVER_SUPPORT
67 struct obd_type *class_setup_tunables(const char *name);
68 #endif
69 int class_register_type(struct obd_ops *, struct md_ops *, bool enable_proc,
70                         struct lprocfs_vars *module_vars,
71                         const char *nm, struct lu_device_type *ldt);
72 int class_unregister_type(const char *nm);
73
74 struct obd_device *class_newdev(const char *type_name, const char *name,
75                                 const char *uuid);
76 int class_register_device(struct obd_device *obd);
77 void class_unregister_device(struct obd_device *obd);
78 void class_free_dev(struct obd_device *obd);
79
80 struct obd_device *class_dev_by_str(const char *str);
81 int class_name2dev(const char *name);
82 struct obd_device *class_name2obd(const char *name);
83 int class_uuid2dev(struct obd_uuid *uuid);
84 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
85 void class_obd_list(void);
86 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
87                                           const char * typ_name,
88                                           struct obd_uuid *grp_uuid);
89 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid,
90                                            int *next);
91 struct obd_device * class_num2obd(int num);
92 int get_devices_count(void);
93
94 int class_notify_sptlrpc_conf(const char *fsname, int namelen);
95
96 static inline char *obd_export_nid2str(struct obd_export *exp)
97 {
98         return exp->exp_connection == NULL ?
99                "<unknown>" : libcfs_nid2str(exp->exp_connection->c_peer.nid);
100 }
101
102 static inline char *obd_import_nid2str(struct obd_import *imp)
103 {
104         return imp->imp_connection == NULL ?
105                "<unknown>" : libcfs_nid2str(imp->imp_connection->c_peer.nid);
106 }
107
108 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid);
109 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid);
110 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
111                           const char *sep);
112
113 int obd_zombie_impexp_init(void);
114 void obd_zombie_impexp_stop(void);
115 void obd_zombie_impexp_cull(void);
116 void obd_zombie_barrier(void);
117 void obd_exports_barrier(struct obd_device *obd);
118 int kuc_len(int payload_len);
119 struct kuc_hdr * kuc_ptr(void *p);
120 void *kuc_alloc(int payload_len, int transport, int type);
121 void kuc_free(void *p, int payload_len);
122 int obd_get_request_slot(struct client_obd *cli);
123 void obd_put_request_slot(struct client_obd *cli);
124 __u32 obd_get_max_rpcs_in_flight(struct client_obd *cli);
125 int obd_set_max_rpcs_in_flight(struct client_obd *cli, __u32 max);
126 __u16 obd_get_max_mod_rpcs_in_flight(struct client_obd *cli);
127 int obd_set_max_mod_rpcs_in_flight(struct client_obd *cli, __u16 max);
128 int obd_mod_rpc_stats_seq_show(struct client_obd *cli, struct seq_file *seq);
129
130 __u16 obd_get_mod_rpc_slot(struct client_obd *cli, __u32 opc,
131                            struct lookup_intent *it);
132 void obd_put_mod_rpc_slot(struct client_obd *cli, __u32 opc,
133                           struct lookup_intent *it, __u16 tag);
134
135 struct llog_handle;
136 struct llog_rec_hdr;
137 typedef int (*llog_cb_t)(const struct lu_env *, struct llog_handle *,
138                          struct llog_rec_hdr *, void *);
139
140 struct obd_export *obd_stale_export_get(void);
141 void obd_stale_export_put(struct obd_export *exp);
142 void obd_stale_export_adjust(struct obd_export *exp);
143
144 /* obd_config.c */
145 /* For interoperability */
146 struct cfg_interop_param {
147         char *old_param;
148         char *new_param;
149 };
150
151 char *lustre_cfg_string(struct lustre_cfg *lcfg, u32 index);
152 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
153                                      const char *new_name);
154 void print_lustre_cfg(struct lustre_cfg *lcfg);
155 int class_process_config(struct lustre_cfg *lcfg);
156 ssize_t class_set_global(const char *param);
157 ssize_t class_modify_config(struct lustre_cfg *lcfg, const char *prefix,
158                             struct kobject *kobj);
159 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
160                              struct lustre_cfg *lcfg, void *data);
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;
580
581                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
582                 if (rc == 0) {
583                         ldt->ldt_ops->ldto_device_fini(&env, d);
584                         lu_env_fini(&env);
585                 }
586         }
587
588         if (!obd->obd_type->typ_dt_ops->o_precleanup)
589                 RETURN(0);
590
591         rc = OBP(obd, precleanup)(obd);
592         RETURN(rc);
593 }
594
595 static inline int obd_cleanup(struct obd_device *obd)
596 {
597         int rc;
598         struct lu_device_type *ldt = obd->obd_type->typ_lu;
599         struct lu_device *d = obd->obd_lu_dev;
600
601         ENTRY;
602         if (ldt != NULL && d != NULL) {
603                 struct lu_env env;
604
605                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
606                 if (rc == 0) {
607                         ldt->ldt_ops->ldto_device_free(&env, d);
608                         lu_env_fini(&env);
609                         obd->obd_lu_dev = NULL;
610                 }
611         }
612         if (!obd->obd_type->typ_dt_ops->o_cleanup)
613                 RETURN(0);
614
615         rc = OBP(obd, cleanup)(obd);
616         RETURN(rc);
617 }
618
619 static inline void obd_cleanup_client_import(struct obd_device *obd)
620 {
621         ENTRY;
622
623         /* If we set up but never connected, the
624            client import will not have been cleaned. */
625         down_write(&obd->u.cli.cl_sem);
626         if (obd->u.cli.cl_import) {
627                 struct obd_import *imp;
628                 imp = obd->u.cli.cl_import;
629                 CDEBUG(D_CONFIG, "%s: client import never connected\n",
630                        obd->obd_name);
631                 ptlrpc_invalidate_import(imp);
632                 client_destroy_import(imp);
633                 obd->u.cli.cl_import = NULL;
634         }
635         up_write(&obd->u.cli.cl_sem);
636
637         EXIT;
638 }
639
640 static inline int obd_process_config(struct obd_device *obd, int datalen,
641                                      void *data)
642 {
643         int rc;
644         struct lu_device_type *ldt = obd->obd_type->typ_lu;
645         struct lu_device *d = obd->obd_lu_dev;
646
647         ENTRY;
648
649         obd->obd_process_conf = 1;
650         if (ldt != NULL && d != NULL) {
651                 struct lu_env env;
652
653                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
654                 if (rc == 0) {
655                         rc = d->ld_ops->ldo_process_config(&env, d, data);
656                         lu_env_fini(&env);
657                 }
658         } else {
659                 if (!obd->obd_type->typ_dt_ops->o_process_config) {
660                         CERROR("%s: no %s operation\n",
661                                obd->obd_name, __func__);
662                         RETURN(-EOPNOTSUPP);
663                 }
664                 rc = OBP(obd, process_config)(obd, datalen, data);
665         }
666
667         obd->obd_process_conf = 0;
668
669         RETURN(rc);
670 }
671
672 static inline int obd_create(const struct lu_env *env, struct obd_export *exp,
673                              struct obdo *obdo)
674 {
675         int rc;
676         ENTRY;
677
678         rc = exp_check_ops(exp);
679         if (rc)
680                 RETURN(rc);
681
682         if (!exp->exp_obd->obd_type->typ_dt_ops->o_create) {
683                 CERROR("%s: no %s operation\n",
684                        (exp)->exp_obd->obd_name, __func__);
685                 RETURN(-ENOTSUPP);
686         }
687
688         rc = OBP(exp->exp_obd, create)(env, exp, obdo);
689         RETURN(rc);
690 }
691
692 static inline int obd_destroy(const struct lu_env *env, struct obd_export *exp,
693                               struct obdo *obdo)
694 {
695         int rc;
696         ENTRY;
697
698         rc = exp_check_ops(exp);
699         if (rc)
700                 RETURN(rc);
701
702         if (!exp->exp_obd->obd_type->typ_dt_ops->o_destroy) {
703                 CERROR("%s: no %s operation\n",
704                        (exp)->exp_obd->obd_name, __func__);
705                 RETURN(-ENOTSUPP);
706         }
707
708         rc = OBP(exp->exp_obd, destroy)(env, exp, obdo);
709         RETURN(rc);
710 }
711
712 static inline int obd_getattr(const struct lu_env *env, struct obd_export *exp,
713                               struct obdo *oa)
714 {
715         int rc;
716
717         ENTRY;
718         rc = exp_check_ops(exp);
719         if (rc)
720                 RETURN(rc);
721
722         if (!exp->exp_obd->obd_type->typ_dt_ops->o_getattr) {
723                 CERROR("%s: no %s operation\n",
724                        (exp)->exp_obd->obd_name, __func__);
725                 RETURN(-ENOTSUPP);
726         }
727
728         rc = OBP(exp->exp_obd, getattr)(env, exp, oa);
729
730         RETURN(rc);
731 }
732
733 static inline int obd_setattr(const struct lu_env *env, struct obd_export *exp,
734                               struct obdo *oa)
735 {
736         int rc;
737
738         ENTRY;
739         rc = exp_check_ops(exp);
740         if (rc)
741                 RETURN(rc);
742
743         if (!exp->exp_obd->obd_type->typ_dt_ops->o_setattr) {
744                 CERROR("%s: no %s operation\n",
745                        (exp)->exp_obd->obd_name, __func__);
746                 RETURN(-ENOTSUPP);
747         }
748
749         rc = OBP(exp->exp_obd, setattr)(env, exp, oa);
750
751         RETURN(rc);
752 }
753
754 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
755                                int priority)
756 {
757         struct obd_device *obd = imp->imp_obd;
758         int rc;
759         ENTRY;
760
761         OBD_CHECK_DEV_ACTIVE(obd);
762         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_add_conn) {
763                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
764                 RETURN(-EOPNOTSUPP);
765         }
766
767         rc = OBP(obd, add_conn)(imp, uuid, priority);
768         RETURN(rc);
769 }
770
771 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
772 {
773         struct obd_device *obd = imp->imp_obd;
774         int rc;
775         ENTRY;
776
777         OBD_CHECK_DEV_ACTIVE(obd);
778         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_del_conn) {
779                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
780                 RETURN(-EOPNOTSUPP);
781         }
782
783         rc = OBP(obd, del_conn)(imp, uuid);
784         RETURN(rc);
785 }
786
787 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
788 {
789         struct obd_uuid *uuid;
790         ENTRY;
791
792         if (!exp->exp_obd->obd_type ||
793             !exp->exp_obd->obd_type->typ_dt_ops->o_get_uuid)
794                 RETURN(NULL);
795
796         uuid = OBP(exp->exp_obd, get_uuid)(exp);
797         RETURN(uuid);
798 }
799
800 /** Create a new /a exp on device /a obd for the uuid /a cluuid
801  * @param exp New export handle
802  * @param d Connect data, supported flags are set, flags also understood
803  *    by obd are returned.
804  */
805 static inline int obd_connect(const struct lu_env *env,
806                               struct obd_export **exp,struct obd_device *obd,
807                               struct obd_uuid *cluuid,
808                               struct obd_connect_data *data,
809                               void *localdata)
810 {
811         int rc;
812         __u64 ocf = data ? data->ocd_connect_flags : 0; /* for post-condition
813                                                    * check */
814         ENTRY;
815
816         OBD_CHECK_DEV_ACTIVE(obd);
817         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_connect) {
818                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
819                 RETURN(-EOPNOTSUPP);
820         }
821
822         rc = OBP(obd, connect)(env, exp, obd, cluuid, data, localdata);
823         /* check that only subset is granted */
824         LASSERT(ergo(data != NULL, (data->ocd_connect_flags & ocf) ==
825                                     data->ocd_connect_flags));
826         RETURN(rc);
827 }
828
829 static inline int obd_reconnect(const struct lu_env *env,
830                                 struct obd_export *exp,
831                                 struct obd_device *obd,
832                                 struct obd_uuid *cluuid,
833                                 struct obd_connect_data *d,
834                                 void *localdata)
835 {
836         int rc;
837         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
838                                                    * check */
839
840         ENTRY;
841
842         OBD_CHECK_DEV_ACTIVE(obd);
843         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_reconnect)
844                 RETURN(0);
845
846         rc = OBP(obd, reconnect)(env, exp, obd, cluuid, d, localdata);
847         /* check that only subset is granted */
848         LASSERT(ergo(d != NULL,
849                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
850         RETURN(rc);
851 }
852
853 static inline int obd_disconnect(struct obd_export *exp)
854 {
855         int rc;
856         ENTRY;
857         rc = exp_check_ops(exp);
858         if (rc)
859                 RETURN(rc);
860
861         if (!exp->exp_obd->obd_type->typ_dt_ops->o_disconnect) {
862                 CERROR("%s: no %s operation\n",
863                        (exp)->exp_obd->obd_name, __func__);
864                 RETURN(-ENOTSUPP);
865         }
866
867         rc = OBP(exp->exp_obd, disconnect)(exp);
868         RETURN(rc);
869 }
870
871 static inline int obd_fid_init(struct obd_device *obd, struct obd_export *exp,
872                                enum lu_cli_type type)
873 {
874         int rc;
875         ENTRY;
876
877         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_fid_init)
878                 RETURN(0);
879
880         rc = OBP(obd, fid_init)(obd, exp, type);
881         RETURN(rc);
882 }
883
884 static inline int obd_fid_fini(struct obd_device *obd)
885 {
886         int rc;
887         ENTRY;
888         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_fid_fini)
889                 RETURN(0);
890
891         rc = OBP(obd, fid_fini)(obd);
892         RETURN(rc);
893 }
894
895 static inline int obd_fid_alloc(const struct lu_env *env,
896                                 struct obd_export *exp,
897                                 struct lu_fid *fid,
898                                 struct md_op_data *op_data)
899 {
900         int rc;
901         ENTRY;
902         rc = exp_check_ops(exp);
903         if (rc)
904                 RETURN(rc);
905
906         if (!exp->exp_obd->obd_type->typ_dt_ops->o_fid_alloc) {
907                 CERROR("%s: no %s operation\n",
908                        (exp)->exp_obd->obd_name, __func__);
909                 RETURN(-ENOTSUPP);
910         }
911
912         rc = OBP(exp->exp_obd, fid_alloc)(env, exp, fid, op_data);
913         RETURN(rc);
914 }
915
916 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
917 {
918         int rc;
919         ENTRY;
920
921         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_new) {
922                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
923                 RETURN(-EOPNOTSUPP);
924         }
925
926         rc = OBP(obd, pool_new)(obd, poolname);
927         RETURN(rc);
928 }
929
930 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
931 {
932         int rc;
933         ENTRY;
934         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_del) {
935                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
936                 RETURN(-EOPNOTSUPP);
937         }
938
939         rc = OBP(obd, pool_del)(obd, poolname);
940         RETURN(rc);
941 }
942
943 static inline int obd_pool_add(struct obd_device *obd, char *poolname,
944                                char *ostname)
945 {
946         int rc;
947         ENTRY;
948
949         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_add) {
950                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
951                 RETURN(-EOPNOTSUPP);
952         }
953
954         rc = OBP(obd, pool_add)(obd, poolname, ostname);
955         RETURN(rc);
956 }
957
958 static inline int obd_pool_rem(struct obd_device *obd, char *poolname,
959                                char *ostname)
960 {
961         int rc;
962
963         ENTRY;
964         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_pool_rem) {
965                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
966                 RETURN(-EOPNOTSUPP);
967         }
968
969         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
970         RETURN(rc);
971 }
972
973 static inline int obd_init_export(struct obd_export *exp)
974 {
975         int rc = 0;
976
977         ENTRY;
978         if (exp->exp_obd != NULL && exp->exp_obd->obd_type &&
979             OBP((exp)->exp_obd, init_export))
980                 rc = OBP(exp->exp_obd, init_export)(exp);
981         RETURN(rc);
982 }
983
984 static inline int obd_destroy_export(struct obd_export *exp)
985 {
986         ENTRY;
987         if (exp->exp_obd != NULL && exp->exp_obd->obd_type &&
988             OBP(exp->exp_obd, destroy_export))
989                 OBP(exp->exp_obd, destroy_export)(exp);
990         RETURN(0);
991 }
992
993 /* @max_age is the oldest time in seconds that we accept using a cached data.
994  * If the cache is older than @max_age we will get a new value from the
995  * target. Use a value of 'ktime_get_seconds() + X' to guarantee freshness.
996  */
997 static inline int obd_statfs_async(struct obd_export *exp,
998                                    struct obd_info *oinfo,
999                                    time64_t max_age,
1000                                    struct ptlrpc_request_set *rqset)
1001 {
1002         struct obd_device *obd;
1003         int rc = 0;
1004
1005         ENTRY;
1006
1007         if (exp == NULL || exp->exp_obd == NULL)
1008                 RETURN(-EINVAL);
1009
1010         obd = exp->exp_obd;
1011         if (!obd->obd_type || !obd->obd_type->typ_dt_ops->o_statfs) {
1012                 rc = -EOPNOTSUPP;
1013                 CERROR("%s: no statfs operation: rc = %d\n", obd->obd_name, rc);
1014                 RETURN(rc);
1015         }
1016
1017         CDEBUG(D_SUPER, "%s: age %lld, max_age %lld\n",
1018                obd->obd_name, obd->obd_osfs_age, max_age);
1019         if (obd->obd_osfs_age < max_age) {
1020                 rc = OBP(obd, statfs_async)(exp, oinfo, max_age, rqset);
1021         } else {
1022                 CDEBUG(D_SUPER,
1023                        "%s: use %p cache blocks %llu/%llu objects %llu/%llu\n",
1024                        obd->obd_name, &obd->obd_osfs,
1025                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1026                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1027                 spin_lock(&obd->obd_osfs_lock);
1028                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1029                 spin_unlock(&obd->obd_osfs_lock);
1030                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1031                 if (oinfo->oi_cb_up)
1032                         oinfo->oi_cb_up(oinfo, 0);
1033         }
1034         RETURN(rc);
1035 }
1036
1037 /* @max_age is the oldest time in seconds that we accept using a cached data.
1038  * If the cache is older than @max_age we will get a new value from the
1039  * target. Use a value of 'ktime_get_seconds() + X' to guarantee freshness.
1040  */
1041 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1042                              struct obd_statfs *osfs, time64_t max_age,
1043                              __u32 flags)
1044 {
1045         struct obd_device *obd = exp->exp_obd;
1046         int rc = 0;
1047
1048         ENTRY;
1049         if (unlikely(obd == NULL))
1050                 RETURN(-EINVAL);
1051
1052         OBD_CHECK_DEV_ACTIVE(obd);
1053
1054         if (unlikely(!obd->obd_type || !obd->obd_type->typ_dt_ops->o_statfs)) {
1055                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
1056                 RETURN(-EOPNOTSUPP);
1057         }
1058
1059         CDEBUG(D_SUPER, "%s: age %lld, max_age %lld\n",
1060                obd->obd_name, obd->obd_osfs_age, max_age);
1061         /* ignore cache if aggregated isn't expected */
1062         if (obd->obd_osfs_age < max_age ||
1063             ((obd->obd_osfs.os_state & OS_STATE_SUM) &&
1064              !(flags & OBD_STATFS_SUM))) {
1065                 rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1066                 if (rc == 0) {
1067                         spin_lock(&obd->obd_osfs_lock);
1068                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1069                         obd->obd_osfs_age = ktime_get_seconds();
1070                         spin_unlock(&obd->obd_osfs_lock);
1071                 }
1072         } else {
1073                 CDEBUG(D_SUPER,
1074                        "%s: use %p cache blocks %llu/%llu objects %llu/%llu\n",
1075                        obd->obd_name, &obd->obd_osfs,
1076                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1077                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1078                 spin_lock(&obd->obd_osfs_lock);
1079                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1080                 spin_unlock(&obd->obd_osfs_lock);
1081         }
1082         RETURN(rc);
1083 }
1084
1085 static inline int obd_preprw(const struct lu_env *env, int cmd,
1086                              struct obd_export *exp, struct obdo *oa,
1087                              int objcount, struct obd_ioobj *obj,
1088                              struct niobuf_remote *remote, int *pages,
1089                              struct niobuf_local *local)
1090 {
1091         int rc;
1092
1093         ENTRY;
1094
1095         rc = exp_check_ops(exp);
1096         if (rc)
1097                 RETURN(rc);
1098
1099         if (!exp->exp_obd->obd_type->typ_dt_ops->o_preprw) {
1100                 CERROR("%s: no %s operation\n",
1101                        (exp)->exp_obd->obd_name, __func__);
1102                 RETURN(-ENOTSUPP);
1103         }
1104
1105         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1106                                        pages, local);
1107
1108         RETURN(rc);
1109 }
1110
1111 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1112                                struct obd_export *exp, struct obdo *oa,
1113                                int objcount, struct obd_ioobj *obj,
1114                                struct niobuf_remote *rnb, int pages,
1115                                struct niobuf_local *local, const int orig_rc)
1116 {
1117         int rc;
1118         ENTRY;
1119
1120         rc = exp_check_ops(exp);
1121         if (rc)
1122                 RETURN(rc);
1123
1124         if (!exp->exp_obd->obd_type->typ_dt_ops->o_commitrw) {
1125                 CERROR("%s: no %s operation\n",
1126                        (exp)->exp_obd->obd_name, __func__);
1127                 RETURN(-ENOTSUPP);
1128         }
1129
1130         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1131                                          rnb, pages, local, orig_rc);
1132
1133         RETURN(rc);
1134 }
1135
1136 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1137                                 int len, void *karg, void __user *uarg)
1138 {
1139         int rc;
1140         ENTRY;
1141
1142         rc = exp_check_ops(exp);
1143         if (rc)
1144                 RETURN(rc);
1145
1146         if (!exp->exp_obd->obd_type->typ_dt_ops->o_iocontrol) {
1147                 CERROR("%s: no %s operation\n",
1148                        (exp)->exp_obd->obd_name, __func__);
1149                 RETURN(-ENOTSUPP);
1150         }
1151
1152         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1153         RETURN(rc);
1154 }
1155
1156 static inline void obd_import_event(struct obd_device *obd,
1157                                     struct obd_import *imp,
1158                                     enum obd_import_event event)
1159 {
1160         ENTRY;
1161         if (!obd) {
1162                 CERROR("NULL device\n");
1163                 EXIT;
1164                 return;
1165         }
1166
1167         if (obd->obd_set_up && OBP(obd, import_event))
1168                 OBP(obd, import_event)(obd, imp, event);
1169
1170         EXIT;
1171 }
1172
1173 static inline int obd_notify(struct obd_device *obd,
1174                              struct obd_device *watched,
1175                              enum obd_notify_event ev)
1176 {
1177         int rc;
1178         ENTRY;
1179
1180         rc = obd_check_dev(obd);
1181         if (rc)
1182                 return rc;
1183
1184         if (!obd->obd_set_up) {
1185                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1186                 RETURN(-EINVAL);
1187         }
1188
1189         if (!OBP(obd, notify)) {
1190                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1191                 RETURN(-ENOSYS);
1192         }
1193
1194         rc = OBP(obd, notify)(obd, watched, ev);
1195
1196         RETURN(rc);
1197 }
1198
1199 static inline int obd_notify_observer(struct obd_device *observer,
1200                                       struct obd_device *observed,
1201                                       enum obd_notify_event ev)
1202 {
1203         int rc = 0;
1204         int rc2 = 0;
1205         struct obd_notify_upcall *onu;
1206
1207         if (observer->obd_observer)
1208                 rc = obd_notify(observer->obd_observer, observed, ev);
1209
1210         /*
1211          * Also, call non-obd listener, if any
1212          */
1213         onu = &observer->obd_upcall;
1214         if (onu->onu_upcall != NULL)
1215                 rc2 = onu->onu_upcall(observer, observed, ev, onu->onu_owner);
1216
1217         return rc ? rc : rc2;
1218 }
1219
1220 static inline int obd_quotactl(struct obd_export *exp,
1221                                struct obd_quotactl *oqctl)
1222 {
1223         int rc;
1224         ENTRY;
1225
1226         rc = exp_check_ops(exp);
1227         if (rc)
1228                 RETURN(rc);
1229
1230         if (!exp->exp_obd->obd_type->typ_dt_ops->o_quotactl) {
1231                 CERROR("%s: no %s operation\n",
1232                        (exp)->exp_obd->obd_name, __func__);
1233                 RETURN(-ENOTSUPP);
1234         }
1235
1236         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1237         RETURN(rc);
1238 }
1239
1240 static inline int obd_health_check(const struct lu_env *env,
1241                                    struct obd_device *obd)
1242 {
1243         /* returns: 0 on healthy
1244          *         >0 on unhealthy + reason code/flag
1245          *            however the only suppored reason == 1 right now
1246          *            We'll need to define some better reasons
1247          *            or flags in the future.
1248          *         <0 on error
1249          */
1250         int rc;
1251
1252         ENTRY;
1253
1254         /* NULL method is normal here */
1255         if (obd == NULL || !obd->obd_type) {
1256                 CERROR("cleaned up obd\n");
1257                 RETURN(-EOPNOTSUPP);
1258         }
1259         if (!obd->obd_set_up || obd->obd_stopping)
1260                 RETURN(0);
1261         if (!OBP(obd, health_check))
1262                 RETURN(0);
1263
1264         rc = OBP(obd, health_check)(env, obd);
1265         RETURN(rc);
1266 }
1267
1268 static inline int obd_register_observer(struct obd_device *obd,
1269                                         struct obd_device *observer)
1270 {
1271         int rc;
1272         ENTRY;
1273
1274         rc = obd_check_dev(obd);
1275         if (rc)
1276                 return rc;
1277
1278         down_write(&obd->obd_observer_link_sem);
1279         if (obd->obd_observer && observer) {
1280                 up_write(&obd->obd_observer_link_sem);
1281                 RETURN(-EALREADY);
1282         }
1283         obd->obd_observer = observer;
1284         up_write(&obd->obd_observer_link_sem);
1285         RETURN(0);
1286 }
1287
1288 /* metadata helpers */
1289 enum mps_stat_idx {
1290         LPROC_MD_CLOSE,
1291         LPROC_MD_CREATE,
1292         LPROC_MD_ENQUEUE,
1293         LPROC_MD_GETATTR,
1294         LPROC_MD_INTENT_LOCK,
1295         LPROC_MD_LINK,
1296         LPROC_MD_RENAME,
1297         LPROC_MD_SETATTR,
1298         LPROC_MD_FSYNC,
1299         LPROC_MD_READ_PAGE,
1300         LPROC_MD_UNLINK,
1301         LPROC_MD_SETXATTR,
1302         LPROC_MD_GETXATTR,
1303         LPROC_MD_INTENT_GETATTR_ASYNC,
1304         LPROC_MD_REVALIDATE_LOCK,
1305         LPROC_MD_LAST_OPC,
1306 };
1307
1308 static inline int md_get_root(struct obd_export *exp, const char *fileset,
1309                               struct lu_fid *fid)
1310 {
1311         int rc;
1312
1313         rc = exp_check_ops(exp);
1314         if (rc)
1315                 return rc;
1316
1317         return MDP(exp->exp_obd, get_root)(exp, fileset, fid);
1318 }
1319
1320 static inline int md_getattr(struct obd_export *exp,
1321                              struct md_op_data *op_data,
1322                              struct ptlrpc_request **request)
1323 {
1324         int rc;
1325
1326         rc = exp_check_ops(exp);
1327         if (rc)
1328                 return rc;
1329
1330         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1331                              LPROC_MD_GETATTR);
1332
1333         return MDP(exp->exp_obd, getattr)(exp, op_data, request);
1334 }
1335
1336 static inline int md_null_inode(struct obd_export *exp,
1337                                    const struct lu_fid *fid)
1338 {
1339         int rc;
1340
1341         rc = exp_check_ops(exp);
1342         if (rc)
1343                 return rc;
1344
1345         return MDP(exp->exp_obd, null_inode)(exp, fid);
1346 }
1347
1348 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1349                            struct md_open_data *mod,
1350                            struct ptlrpc_request **request)
1351 {
1352         int rc;
1353
1354         rc = exp_check_ops(exp);
1355         if (rc)
1356                 return rc;
1357
1358         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1359                              LPROC_MD_CLOSE);
1360
1361         return MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1362 }
1363
1364 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1365                             const void *data, size_t datalen, umode_t mode,
1366                             uid_t uid, gid_t gid, cfs_cap_t cap_effective,
1367                             __u64 rdev, struct ptlrpc_request **request)
1368 {
1369         int rc;
1370
1371         rc = exp_check_ops(exp);
1372         if (rc)
1373                 return rc;
1374
1375         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1376                              LPROC_MD_CREATE);
1377
1378         return MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1379                                          uid, gid, cap_effective, rdev,
1380                                          request);
1381 }
1382
1383 static inline int md_enqueue(struct obd_export *exp,
1384                              struct ldlm_enqueue_info *einfo,
1385                              const union ldlm_policy_data *policy,
1386                              struct md_op_data *op_data,
1387                              struct lustre_handle *lockh,
1388                              __u64 extra_lock_flags)
1389 {
1390         int rc;
1391
1392         rc = exp_check_ops(exp);
1393         if (rc)
1394                 return rc;
1395
1396         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1397                              LPROC_MD_ENQUEUE);
1398
1399         return MDP(exp->exp_obd, enqueue)(exp, einfo, policy, op_data, lockh,
1400                    extra_lock_flags);
1401 }
1402
1403 static inline int md_getattr_name(struct obd_export *exp,
1404                                   struct md_op_data *op_data,
1405                                   struct ptlrpc_request **request)
1406 {
1407         int rc;
1408
1409         rc = exp_check_ops(exp);
1410         if (rc)
1411                 return rc;
1412
1413         return MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1414 }
1415
1416 static inline int md_intent_lock(struct obd_export *exp,
1417                                  struct md_op_data *op_data,
1418                                  struct lookup_intent *it,
1419                                  struct ptlrpc_request **reqp,
1420                                  ldlm_blocking_callback cb_blocking,
1421                                  __u64 extra_lock_flags)
1422 {
1423         int rc;
1424
1425         rc = exp_check_ops(exp);
1426         if (rc)
1427                 return rc;
1428
1429         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1430                              LPROC_MD_INTENT_LOCK);
1431
1432         return MDP(exp->exp_obd, intent_lock)(exp, op_data, it, reqp,
1433                                               cb_blocking, extra_lock_flags);
1434 }
1435
1436 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1437                           struct ptlrpc_request **request)
1438 {
1439         int rc;
1440
1441         rc = exp_check_ops(exp);
1442         if (rc)
1443                 return rc;
1444
1445         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1446                              LPROC_MD_LINK);
1447
1448         return MDP(exp->exp_obd, link)(exp, op_data, request);
1449 }
1450
1451 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1452                             const char *old_name, size_t oldlen,
1453                             const char *new_name, size_t newlen,
1454                             struct ptlrpc_request **request)
1455 {
1456         int rc;
1457
1458         rc = exp_check_ops(exp);
1459         if (rc)
1460                 return rc;
1461
1462         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1463                              LPROC_MD_RENAME);
1464
1465         return MDP(exp->exp_obd, rename)(exp, op_data, old_name, oldlen,
1466                                          new_name, newlen, request);
1467 }
1468
1469 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1470                              void *ea, size_t ealen,
1471                              struct ptlrpc_request **request)
1472 {
1473         int rc;
1474
1475         rc = exp_check_ops(exp);
1476         if (rc)
1477                 return rc;
1478
1479         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1480                              LPROC_MD_SETATTR);
1481
1482         return MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen, request);
1483 }
1484
1485 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
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_FSYNC);
1496
1497         return MDP(exp->exp_obd, fsync)(exp, fid, request);
1498 }
1499
1500 /* FLR: resync mirrored files. */
1501 static inline int md_file_resync(struct obd_export *exp,
1502                                  struct md_op_data *data)
1503 {
1504         int rc;
1505
1506         rc = exp_check_ops(exp);
1507         if (rc)
1508                 return rc;
1509
1510         return MDP(exp->exp_obd, file_resync)(exp, data);
1511 }
1512
1513 static inline int md_read_page(struct obd_export *exp,
1514                                struct md_op_data *op_data,
1515                                struct md_callback *cb_op,
1516                                __u64  hash_offset,
1517                                struct page **ppage)
1518 {
1519         int rc;
1520
1521         rc = exp_check_ops(exp);
1522         if (rc)
1523                 return rc;
1524
1525         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1526                              LPROC_MD_READ_PAGE);
1527
1528         return MDP(exp->exp_obd, read_page)(exp, op_data, cb_op, hash_offset,
1529                                             ppage);
1530 }
1531
1532 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1533                             struct ptlrpc_request **request)
1534 {
1535         int rc;
1536
1537         rc = exp_check_ops(exp);
1538         if (rc)
1539                 return rc;
1540
1541         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1542                              LPROC_MD_UNLINK);
1543
1544         return MDP(exp->exp_obd, unlink)(exp, op_data, request);
1545 }
1546
1547 static inline int md_get_lustre_md(struct obd_export *exp,
1548                                    struct ptlrpc_request *req,
1549                                    struct obd_export *dt_exp,
1550                                    struct obd_export *md_exp,
1551                                    struct lustre_md *md)
1552 {
1553         int rc;
1554
1555         rc = exp_check_ops(exp);
1556         if (rc)
1557                 return rc;
1558
1559         return MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md);
1560 }
1561
1562 static inline int md_free_lustre_md(struct obd_export *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, free_lustre_md)(exp, md);
1572 }
1573
1574 static inline int md_merge_attr(struct obd_export *exp,
1575                                 const struct lmv_stripe_md *lsm,
1576                                 struct cl_attr *attr,
1577                                 ldlm_blocking_callback cb)
1578 {
1579         int rc;
1580
1581         rc = exp_check_ops(exp);
1582         if (rc)
1583                 return rc;
1584
1585         return MDP(exp->exp_obd, merge_attr)(exp, lsm, attr, cb);
1586 }
1587
1588 static inline int md_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1589                               u64 obd_md_valid, const char *name,
1590                               const void *value, size_t value_size,
1591                               unsigned int xattr_flags, u32 suppgid,
1592                               struct ptlrpc_request **req)
1593 {
1594         int rc;
1595
1596         rc = exp_check_ops(exp);
1597         if (rc)
1598                 return rc;
1599
1600         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1601                              LPROC_MD_SETXATTR);
1602
1603         return MDP(exp->exp_obd, setxattr)(exp, fid, obd_md_valid, name,
1604                                            value, value_size, xattr_flags,
1605                                            suppgid, req);
1606 }
1607
1608 static inline int md_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1609                               u64 obd_md_valid, const char *name,
1610                               size_t buf_size, struct ptlrpc_request **req)
1611 {
1612         int rc;
1613
1614         rc = exp_check_ops(exp);
1615         if (rc)
1616                 return rc;
1617
1618         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1619                              LPROC_MD_GETXATTR);
1620
1621         return MDP(exp->exp_obd, getxattr)(exp, fid, obd_md_valid, name,
1622                                            buf_size, req);
1623 }
1624
1625 static inline int md_set_open_replay_data(struct obd_export *exp,
1626                                           struct obd_client_handle *och,
1627                                           struct lookup_intent *it)
1628 {
1629         int rc;
1630
1631         rc = exp_check_ops(exp);
1632         if (rc)
1633                 return rc;
1634
1635         return MDP(exp->exp_obd, set_open_replay_data)(exp, och, it);
1636 }
1637
1638 static inline int md_clear_open_replay_data(struct obd_export *exp,
1639                                             struct obd_client_handle *och)
1640 {
1641         int rc;
1642
1643         rc = exp_check_ops(exp);
1644         if (rc)
1645                 return rc;
1646
1647         return MDP(exp->exp_obd, clear_open_replay_data)(exp, och);
1648 }
1649
1650 static inline int md_set_lock_data(struct obd_export *exp,
1651                                    const struct lustre_handle *lockh,
1652                                    void *data, __u64 *bits)
1653 {
1654         int rc;
1655
1656         rc = exp_check_ops(exp);
1657         if (rc)
1658                 return rc;
1659
1660         return MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits);
1661 }
1662
1663 static inline
1664 int md_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
1665                      union ldlm_policy_data *policy, enum ldlm_mode mode,
1666                      enum ldlm_cancel_flags cancel_flags, void *opaque)
1667 {
1668         int rc;
1669
1670         rc = exp_check_ops(exp);
1671         if (rc)
1672                 return rc;
1673
1674         return MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
1675                                                 cancel_flags, opaque);
1676 }
1677
1678 static inline enum ldlm_mode md_lock_match(struct obd_export *exp, __u64 flags,
1679                                            const struct lu_fid *fid,
1680                                            enum ldlm_type type,
1681                                            union ldlm_policy_data *policy,
1682                                            enum ldlm_mode mode,
1683                                            struct lustre_handle *lockh)
1684 {
1685         int rc;
1686
1687         rc = exp_check_ops(exp);
1688         if (rc)
1689                 return rc;
1690
1691         return MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1692                                              policy, mode, lockh);
1693 }
1694
1695 static inline int md_init_ea_size(struct obd_export *exp, __u32 ea_size,
1696                                   __u32 def_ea_size)
1697 {
1698         int rc;
1699
1700         rc = exp_check_ops(exp);
1701         if (rc)
1702                 return rc;
1703
1704         return MDP(exp->exp_obd, init_ea_size)(exp, ea_size, def_ea_size);
1705 }
1706
1707 static inline int md_intent_getattr_async(struct obd_export *exp,
1708                                           struct md_enqueue_info *minfo)
1709 {
1710         int rc;
1711
1712         rc = exp_check_ops(exp);
1713         if (rc)
1714                 return rc;
1715
1716         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1717                              LPROC_MD_INTENT_GETATTR_ASYNC);
1718
1719         return MDP(exp->exp_obd, intent_getattr_async)(exp, minfo);
1720 }
1721
1722 static inline int md_revalidate_lock(struct obd_export *exp,
1723                                      struct lookup_intent *it,
1724                                      struct lu_fid *fid, __u64 *bits)
1725 {
1726         int rc;
1727
1728         rc = exp_check_ops(exp);
1729         if (rc)
1730                 return rc;
1731
1732         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1733                              LPROC_MD_REVALIDATE_LOCK);
1734
1735         return MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
1736 }
1737
1738 static inline int md_get_fid_from_lsm(struct obd_export *exp,
1739                                       const struct lmv_stripe_md *lsm,
1740                                       const char *name, int namelen,
1741                                       struct lu_fid *fid)
1742 {
1743         int rc;
1744
1745         rc = exp_check_ops(exp);
1746         if (rc)
1747                 return rc;
1748
1749         return MDP(exp->exp_obd, get_fid_from_lsm)(exp, lsm, name, namelen,
1750                                                    fid);
1751 }
1752
1753 /* Unpack an MD struct from disk to in-memory format.
1754  * Returns +ve size of unpacked MD (0 for free), or -ve error.
1755  *
1756  * If *plsm != NULL and lmm == NULL then *lsm will be freed.
1757  * If *plsm == NULL then it will be allocated.
1758  */
1759 static inline int md_unpackmd(struct obd_export *exp,
1760                               struct lmv_stripe_md **plsm,
1761                               const union lmv_mds_md *lmm, size_t lmm_size)
1762 {
1763         int rc;
1764
1765         rc = exp_check_ops(exp);
1766         if (rc)
1767                 return rc;
1768
1769         return MDP(exp->exp_obd, unpackmd)(exp, plsm, lmm, lmm_size);
1770 }
1771
1772 /* OBD Metadata Support */
1773
1774 extern int obd_init_caches(void);
1775 extern void obd_cleanup_caches(void);
1776
1777 typedef int (*register_lwp_cb)(void *data);
1778
1779 struct lwp_register_item {
1780         struct obd_export **lri_exp;
1781         register_lwp_cb     lri_cb_func;
1782         void               *lri_cb_data;
1783         struct list_head    lri_list;
1784         atomic_t            lri_ref;
1785         char                lri_name[MTI_NAME_MAXLEN];
1786 };
1787
1788 /* I'm as embarrassed about this as you are.
1789  *
1790  * <shaver> // XXX do not look into _superhack with remaining eye
1791  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1792 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1793
1794 /* obd_mount.c */
1795 #ifdef HAVE_SERVER_SUPPORT
1796 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
1797                              register_lwp_cb cb_func, void *cb_data);
1798 void lustre_deregister_lwp_item(struct obd_export **exp);
1799 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
1800 void lustre_notify_lwp_list(struct obd_export *exp);
1801 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
1802 #endif /* HAVE_SERVER_SUPPORT */
1803 int lustre_register_fs(void);
1804 int lustre_unregister_fs(void);
1805 int lustre_check_exclusion(struct super_block *sb, char *svname);
1806
1807 /* lustre_peer.c    */
1808 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
1809 int class_add_uuid(const char *uuid, __u64 nid);
1810 int class_del_uuid (const char *uuid);
1811 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
1812
1813 /* class_obd.c */
1814 extern char obd_jobid_name[];
1815
1816 /* statfs_pack.c */
1817 struct kstatfs;
1818 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
1819 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
1820
1821 /* root squash info */
1822 struct rw_semaphore;
1823 struct root_squash_info {
1824         uid_t                   rsi_uid;
1825         gid_t                   rsi_gid;
1826         struct list_head        rsi_nosquash_nids;
1827         struct rw_semaphore     rsi_sem;
1828 };
1829
1830 int server_name2index(const char *svname, __u32 *idx, const char **endptr);
1831
1832 /* linux-module.c */
1833 extern struct miscdevice obd_psdev;
1834 int obd_ioctl_getdata(char **buf, int *len, void __user *arg);
1835 int class_procfs_init(void);
1836 int class_procfs_clean(void);
1837
1838 extern void obd_heat_add(struct obd_heat_instance *instance,
1839                          unsigned int time_second, __u64 count,
1840                          unsigned int weight, unsigned int period_second);
1841 extern void obd_heat_decay(struct obd_heat_instance *instance,
1842                            __u64 time_second, unsigned int weight,
1843                            unsigned int period_second);
1844 extern __u64 obd_heat_get(struct obd_heat_instance *instance,
1845                           unsigned int time_second, unsigned int weight,
1846                           unsigned int period_second);
1847 extern void obd_heat_clear(struct obd_heat_instance *instance, int count);
1848 #endif /* __LINUX_OBD_CLASS_H */