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