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