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