Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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;
1050         int rc = 0;
1051
1052         ENTRY;
1053         if (unlikely(exp == NULL || exp->exp_obd == NULL))
1054                 RETURN(-EINVAL);
1055
1056         obd = exp->exp_obd;
1057         OBD_CHECK_DEV_ACTIVE(obd);
1058
1059         if (unlikely(!obd->obd_type || !obd->obd_type->typ_dt_ops->o_statfs)) {
1060                 CERROR("%s: no %s operation\n", obd->obd_name, __func__);
1061                 RETURN(-EOPNOTSUPP);
1062         }
1063
1064         CDEBUG(D_SUPER, "%s: age %lld, max_age %lld\n",
1065                obd->obd_name, obd->obd_osfs_age, max_age);
1066         /* ignore cache if aggregated isn't expected */
1067         if (obd->obd_osfs_age < max_age ||
1068             ((obd->obd_osfs.os_state & OS_STATFS_SUM) &&
1069              !(flags & OBD_STATFS_SUM))) {
1070                 /* the RPC will block anyway, so avoid sending many at once */
1071                 rc = mutex_lock_interruptible_nested(&obd->obd_dev_mutex,
1072                                                      (flags & OBD_STATFS_NESTED)
1073                                                      ? SINGLE_DEPTH_NESTING : 0);
1074                 if (rc)
1075                         RETURN(rc);
1076                 if (obd->obd_osfs_age < max_age ||
1077                     ((obd->obd_osfs.os_state & OS_STATFS_SUM) &&
1078                      !(flags & OBD_STATFS_SUM))) {
1079                         rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1080                 } else {
1081                         mutex_unlock(&obd->obd_dev_mutex);
1082                         GOTO(cached, rc = 0);
1083                 }
1084                 if (rc == 0) {
1085                         CDEBUG(D_SUPER,
1086                                "%s: update %p cache blocks %llu/%llu objects %llu/%llu\n",
1087                                obd->obd_name, &obd->obd_osfs,
1088                                osfs->os_bavail, osfs->os_blocks,
1089                                osfs->os_ffree, osfs->os_files);
1090
1091                         spin_lock(&obd->obd_osfs_lock);
1092                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1093                         obd->obd_osfs_age = ktime_get_seconds();
1094                         spin_unlock(&obd->obd_osfs_lock);
1095                 }
1096                 mutex_unlock(&obd->obd_dev_mutex);
1097         } else {
1098 cached:
1099                 CDEBUG(D_SUPER,
1100                        "%s: use %p cache blocks %llu/%llu objects %llu/%llu\n",
1101                        obd->obd_name, &obd->obd_osfs,
1102                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1103                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1104                 spin_lock(&obd->obd_osfs_lock);
1105                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1106                 spin_unlock(&obd->obd_osfs_lock);
1107         }
1108         RETURN(rc);
1109 }
1110
1111 static inline int obd_preprw(const struct lu_env *env, int cmd,
1112                              struct obd_export *exp, struct obdo *oa,
1113                              int objcount, struct obd_ioobj *obj,
1114                              struct niobuf_remote *remote, int *pages,
1115                              struct niobuf_local *local)
1116 {
1117         int rc;
1118
1119         ENTRY;
1120
1121         rc = exp_check_ops(exp);
1122         if (rc)
1123                 RETURN(rc);
1124
1125         if (!exp->exp_obd->obd_type->typ_dt_ops->o_preprw) {
1126                 CERROR("%s: no %s operation\n",
1127                        (exp)->exp_obd->obd_name, __func__);
1128                 RETURN(-ENOTSUPP);
1129         }
1130
1131         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1132                                        pages, local);
1133
1134         RETURN(rc);
1135 }
1136
1137 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1138                                struct obd_export *exp, struct obdo *oa,
1139                                int objcount, struct obd_ioobj *obj,
1140                                struct niobuf_remote *rnb, int pages,
1141                                struct niobuf_local *local, const int orig_rc)
1142 {
1143         int rc;
1144         ENTRY;
1145
1146         rc = exp_check_ops(exp);
1147         if (rc)
1148                 RETURN(rc);
1149
1150         if (!exp->exp_obd->obd_type->typ_dt_ops->o_commitrw) {
1151                 CERROR("%s: no %s operation\n",
1152                        (exp)->exp_obd->obd_name, __func__);
1153                 RETURN(-ENOTSUPP);
1154         }
1155
1156         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1157                                          rnb, pages, local, orig_rc);
1158
1159         RETURN(rc);
1160 }
1161
1162 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1163                                 int len, void *karg, void __user *uarg)
1164 {
1165         int rc;
1166         ENTRY;
1167
1168         rc = exp_check_ops(exp);
1169         if (rc)
1170                 RETURN(rc);
1171
1172         if (!exp->exp_obd->obd_type->typ_dt_ops->o_iocontrol) {
1173                 CERROR("%s: no %s operation\n",
1174                        (exp)->exp_obd->obd_name, __func__);
1175                 RETURN(-ENOTSUPP);
1176         }
1177
1178         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1179         RETURN(rc);
1180 }
1181
1182 static inline void obd_import_event(struct obd_device *obd,
1183                                     struct obd_import *imp,
1184                                     enum obd_import_event event)
1185 {
1186         ENTRY;
1187         if (!obd) {
1188                 CERROR("NULL device\n");
1189                 EXIT;
1190                 return;
1191         }
1192
1193         if (obd->obd_set_up && OBP(obd, import_event))
1194                 OBP(obd, import_event)(obd, imp, event);
1195
1196         EXIT;
1197 }
1198
1199 static inline int obd_notify(struct obd_device *obd,
1200                              struct obd_device *watched,
1201                              enum obd_notify_event ev)
1202 {
1203         int rc;
1204         ENTRY;
1205
1206         rc = obd_check_dev(obd);
1207         if (rc)
1208                 return rc;
1209
1210         if (!obd->obd_set_up) {
1211                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1212                 RETURN(-EINVAL);
1213         }
1214
1215         if (!OBP(obd, notify)) {
1216                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1217                 RETURN(-ENOSYS);
1218         }
1219
1220         rc = OBP(obd, notify)(obd, watched, ev);
1221
1222         RETURN(rc);
1223 }
1224
1225 static inline int obd_notify_observer(struct obd_device *observer,
1226                                       struct obd_device *observed,
1227                                       enum obd_notify_event ev)
1228 {
1229         int rc = 0;
1230         int rc2 = 0;
1231         struct obd_notify_upcall *onu;
1232
1233         if (observer->obd_observer)
1234                 rc = obd_notify(observer->obd_observer, observed, ev);
1235
1236         /*
1237          * Also, call non-obd listener, if any
1238          */
1239         onu = &observer->obd_upcall;
1240         if (onu->onu_upcall != NULL)
1241                 rc2 = onu->onu_upcall(observer, observed, ev, onu->onu_owner);
1242
1243         return rc ? rc : rc2;
1244 }
1245
1246 static inline int obd_quotactl(struct obd_export *exp,
1247                                struct obd_quotactl *oqctl)
1248 {
1249         int rc;
1250         ENTRY;
1251
1252         rc = exp_check_ops(exp);
1253         if (rc)
1254                 RETURN(rc);
1255
1256         if (!exp->exp_obd->obd_type->typ_dt_ops->o_quotactl) {
1257                 CERROR("%s: no %s operation\n",
1258                        (exp)->exp_obd->obd_name, __func__);
1259                 RETURN(-ENOTSUPP);
1260         }
1261
1262         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1263         RETURN(rc);
1264 }
1265
1266 static inline int obd_health_check(const struct lu_env *env,
1267                                    struct obd_device *obd)
1268 {
1269         /* returns: 0 on healthy
1270          *         >0 on unhealthy + reason code/flag
1271          *            however the only suppored reason == 1 right now
1272          *            We'll need to define some better reasons
1273          *            or flags in the future.
1274          *         <0 on error
1275          */
1276         int rc;
1277
1278         ENTRY;
1279
1280         /* NULL method is normal here */
1281         if (obd == NULL || !obd->obd_type) {
1282                 CERROR("cleaned up obd\n");
1283                 RETURN(-EOPNOTSUPP);
1284         }
1285         if (!obd->obd_set_up || obd->obd_stopping)
1286                 RETURN(0);
1287         if (!OBP(obd, health_check))
1288                 RETURN(0);
1289
1290         rc = OBP(obd, health_check)(env, obd);
1291         RETURN(rc);
1292 }
1293
1294 static inline int obd_register_observer(struct obd_device *obd,
1295                                         struct obd_device *observer)
1296 {
1297         int rc;
1298         ENTRY;
1299
1300         rc = obd_check_dev(obd);
1301         if (rc)
1302                 return rc;
1303
1304         down_write(&obd->obd_observer_link_sem);
1305         if (obd->obd_observer && observer) {
1306                 up_write(&obd->obd_observer_link_sem);
1307                 RETURN(-EALREADY);
1308         }
1309         obd->obd_observer = observer;
1310         up_write(&obd->obd_observer_link_sem);
1311         RETURN(0);
1312 }
1313
1314 /* metadata helpers */
1315 enum mps_stat_idx {
1316         LPROC_MD_CLOSE,
1317         LPROC_MD_CREATE,
1318         LPROC_MD_ENQUEUE,
1319         LPROC_MD_GETATTR,
1320         LPROC_MD_INTENT_LOCK,
1321         LPROC_MD_LINK,
1322         LPROC_MD_RENAME,
1323         LPROC_MD_SETATTR,
1324         LPROC_MD_FSYNC,
1325         LPROC_MD_READ_PAGE,
1326         LPROC_MD_UNLINK,
1327         LPROC_MD_SETXATTR,
1328         LPROC_MD_GETXATTR,
1329         LPROC_MD_INTENT_GETATTR_ASYNC,
1330         LPROC_MD_REVALIDATE_LOCK,
1331         LPROC_MD_LAST_OPC,
1332 };
1333
1334 static inline int md_get_root(struct obd_export *exp, const char *fileset,
1335                               struct lu_fid *fid)
1336 {
1337         int rc;
1338
1339         rc = exp_check_ops(exp);
1340         if (rc)
1341                 return rc;
1342
1343         return MDP(exp->exp_obd, get_root)(exp, fileset, fid);
1344 }
1345
1346 static inline int md_getattr(struct obd_export *exp,
1347                              struct md_op_data *op_data,
1348                              struct ptlrpc_request **request)
1349 {
1350         int rc;
1351
1352         rc = exp_check_ops(exp);
1353         if (rc)
1354                 return rc;
1355
1356         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1357                              LPROC_MD_GETATTR);
1358
1359         return MDP(exp->exp_obd, getattr)(exp, op_data, request);
1360 }
1361
1362 static inline int md_null_inode(struct obd_export *exp,
1363                                    const struct lu_fid *fid)
1364 {
1365         int rc;
1366
1367         rc = exp_check_ops(exp);
1368         if (rc)
1369                 return rc;
1370
1371         return MDP(exp->exp_obd, null_inode)(exp, fid);
1372 }
1373
1374 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1375                            struct md_open_data *mod,
1376                            struct ptlrpc_request **request)
1377 {
1378         int rc;
1379
1380         rc = exp_check_ops(exp);
1381         if (rc)
1382                 return rc;
1383
1384         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1385                              LPROC_MD_CLOSE);
1386
1387         return MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1388 }
1389
1390 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1391                             const void *data, size_t datalen, umode_t mode,
1392                             uid_t uid, gid_t gid, cfs_cap_t cap_effective,
1393                             __u64 rdev, struct ptlrpc_request **request)
1394 {
1395         int rc;
1396
1397         rc = exp_check_ops(exp);
1398         if (rc)
1399                 return rc;
1400
1401         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1402                              LPROC_MD_CREATE);
1403
1404         return MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1405                                          uid, gid, cap_effective, rdev,
1406                                          request);
1407 }
1408
1409 static inline int md_enqueue(struct obd_export *exp,
1410                              struct ldlm_enqueue_info *einfo,
1411                              const union ldlm_policy_data *policy,
1412                              struct md_op_data *op_data,
1413                              struct lustre_handle *lockh,
1414                              __u64 extra_lock_flags)
1415 {
1416         int rc;
1417
1418         rc = exp_check_ops(exp);
1419         if (rc)
1420                 return rc;
1421
1422         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1423                              LPROC_MD_ENQUEUE);
1424
1425         return MDP(exp->exp_obd, enqueue)(exp, einfo, policy, op_data, lockh,
1426                    extra_lock_flags);
1427 }
1428
1429 static inline int md_getattr_name(struct obd_export *exp,
1430                                   struct md_op_data *op_data,
1431                                   struct ptlrpc_request **request)
1432 {
1433         int rc;
1434
1435         rc = exp_check_ops(exp);
1436         if (rc)
1437                 return rc;
1438
1439         return MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1440 }
1441
1442 static inline int md_intent_lock(struct obd_export *exp,
1443                                  struct md_op_data *op_data,
1444                                  struct lookup_intent *it,
1445                                  struct ptlrpc_request **reqp,
1446                                  ldlm_blocking_callback cb_blocking,
1447                                  __u64 extra_lock_flags)
1448 {
1449         int rc;
1450
1451         rc = exp_check_ops(exp);
1452         if (rc)
1453                 return rc;
1454
1455         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1456                              LPROC_MD_INTENT_LOCK);
1457
1458         return MDP(exp->exp_obd, intent_lock)(exp, op_data, it, reqp,
1459                                               cb_blocking, extra_lock_flags);
1460 }
1461
1462 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1463                           struct ptlrpc_request **request)
1464 {
1465         int rc;
1466
1467         rc = exp_check_ops(exp);
1468         if (rc)
1469                 return rc;
1470
1471         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1472                              LPROC_MD_LINK);
1473
1474         return MDP(exp->exp_obd, link)(exp, op_data, request);
1475 }
1476
1477 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1478                             const char *old_name, size_t oldlen,
1479                             const char *new_name, size_t newlen,
1480                             struct ptlrpc_request **request)
1481 {
1482         int rc;
1483
1484         rc = exp_check_ops(exp);
1485         if (rc)
1486                 return rc;
1487
1488         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1489                              LPROC_MD_RENAME);
1490
1491         return MDP(exp->exp_obd, rename)(exp, op_data, old_name, oldlen,
1492                                          new_name, newlen, request);
1493 }
1494
1495 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1496                              void *ea, size_t ealen,
1497                              struct ptlrpc_request **request)
1498 {
1499         int rc;
1500
1501         rc = exp_check_ops(exp);
1502         if (rc)
1503                 return rc;
1504
1505         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1506                              LPROC_MD_SETATTR);
1507
1508         return MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen, request);
1509 }
1510
1511 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
1512                            struct ptlrpc_request **request)
1513 {
1514         int rc;
1515
1516         rc = exp_check_ops(exp);
1517         if (rc)
1518                 return rc;
1519
1520         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1521                              LPROC_MD_FSYNC);
1522
1523         return MDP(exp->exp_obd, fsync)(exp, fid, request);
1524 }
1525
1526 /* FLR: resync mirrored files. */
1527 static inline int md_file_resync(struct obd_export *exp,
1528                                  struct md_op_data *data)
1529 {
1530         int rc;
1531
1532         rc = exp_check_ops(exp);
1533         if (rc)
1534                 return rc;
1535
1536         return MDP(exp->exp_obd, file_resync)(exp, data);
1537 }
1538
1539 static inline int md_read_page(struct obd_export *exp,
1540                                struct md_op_data *op_data,
1541                                struct md_callback *cb_op,
1542                                __u64  hash_offset,
1543                                struct page **ppage)
1544 {
1545         int rc;
1546
1547         rc = exp_check_ops(exp);
1548         if (rc)
1549                 return rc;
1550
1551         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1552                              LPROC_MD_READ_PAGE);
1553
1554         return MDP(exp->exp_obd, read_page)(exp, op_data, cb_op, hash_offset,
1555                                             ppage);
1556 }
1557
1558 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1559                             struct ptlrpc_request **request)
1560 {
1561         int rc;
1562
1563         rc = exp_check_ops(exp);
1564         if (rc)
1565                 return rc;
1566
1567         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1568                              LPROC_MD_UNLINK);
1569
1570         return MDP(exp->exp_obd, unlink)(exp, op_data, request);
1571 }
1572
1573 static inline int md_get_lustre_md(struct obd_export *exp,
1574                                    struct req_capsule *pill,
1575                                    struct obd_export *dt_exp,
1576                                    struct obd_export *md_exp,
1577                                    struct lustre_md *md)
1578 {
1579         int rc;
1580
1581         rc = exp_check_ops(exp);
1582         if (rc)
1583                 return rc;
1584
1585         return MDP(exp->exp_obd, get_lustre_md)(exp, pill, dt_exp, md_exp, md);
1586 }
1587
1588 static inline int md_free_lustre_md(struct obd_export *exp,
1589                                     struct lustre_md *md)
1590 {
1591         int rc;
1592
1593         rc = exp_check_ops(exp);
1594         if (rc)
1595                 return rc;
1596
1597         return MDP(exp->exp_obd, free_lustre_md)(exp, md);
1598 }
1599
1600 static inline int md_merge_attr(struct obd_export *exp,
1601                                 const struct lmv_stripe_md *lsm,
1602                                 struct cl_attr *attr,
1603                                 ldlm_blocking_callback cb)
1604 {
1605         int rc;
1606
1607         rc = exp_check_ops(exp);
1608         if (rc)
1609                 return rc;
1610
1611         return MDP(exp->exp_obd, merge_attr)(exp, lsm, attr, cb);
1612 }
1613
1614 static inline int md_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1615                               u64 obd_md_valid, const char *name,
1616                               const void *value, size_t value_size,
1617                               unsigned int xattr_flags, u32 suppgid,
1618                               struct ptlrpc_request **req)
1619 {
1620         int rc;
1621
1622         rc = exp_check_ops(exp);
1623         if (rc)
1624                 return rc;
1625
1626         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1627                              LPROC_MD_SETXATTR);
1628
1629         return MDP(exp->exp_obd, setxattr)(exp, fid, obd_md_valid, name,
1630                                            value, value_size, xattr_flags,
1631                                            suppgid, req);
1632 }
1633
1634 static inline int md_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1635                               u64 obd_md_valid, const char *name,
1636                               size_t buf_size, struct ptlrpc_request **req)
1637 {
1638         int rc;
1639
1640         rc = exp_check_ops(exp);
1641         if (rc)
1642                 return rc;
1643
1644         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1645                              LPROC_MD_GETXATTR);
1646
1647         return MDP(exp->exp_obd, getxattr)(exp, fid, obd_md_valid, name,
1648                                            buf_size, req);
1649 }
1650
1651 static inline int md_set_open_replay_data(struct obd_export *exp,
1652                                           struct obd_client_handle *och,
1653                                           struct lookup_intent *it)
1654 {
1655         int rc;
1656
1657         rc = exp_check_ops(exp);
1658         if (rc)
1659                 return rc;
1660
1661         return MDP(exp->exp_obd, set_open_replay_data)(exp, och, it);
1662 }
1663
1664 static inline int md_clear_open_replay_data(struct obd_export *exp,
1665                                             struct obd_client_handle *och)
1666 {
1667         int rc;
1668
1669         rc = exp_check_ops(exp);
1670         if (rc)
1671                 return rc;
1672
1673         return MDP(exp->exp_obd, clear_open_replay_data)(exp, och);
1674 }
1675
1676 static inline int md_set_lock_data(struct obd_export *exp,
1677                                    const struct lustre_handle *lockh,
1678                                    void *data, __u64 *bits)
1679 {
1680         int rc;
1681
1682         rc = exp_check_ops(exp);
1683         if (rc)
1684                 return rc;
1685
1686         return MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits);
1687 }
1688
1689 static inline
1690 int md_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
1691                      union ldlm_policy_data *policy, enum ldlm_mode mode,
1692                      enum ldlm_cancel_flags cancel_flags, void *opaque)
1693 {
1694         int rc;
1695
1696         rc = exp_check_ops(exp);
1697         if (rc)
1698                 return rc;
1699
1700         return MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
1701                                                 cancel_flags, opaque);
1702 }
1703
1704 static inline enum ldlm_mode md_lock_match(struct obd_export *exp, __u64 flags,
1705                                            const struct lu_fid *fid,
1706                                            enum ldlm_type type,
1707                                            union ldlm_policy_data *policy,
1708                                            enum ldlm_mode mode,
1709                                            struct lustre_handle *lockh)
1710 {
1711         int rc;
1712
1713         rc = exp_check_ops(exp);
1714         if (rc)
1715                 return rc;
1716
1717         return MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1718                                              policy, mode, lockh);
1719 }
1720
1721 static inline int md_init_ea_size(struct obd_export *exp, __u32 ea_size,
1722                                   __u32 def_ea_size)
1723 {
1724         int rc;
1725
1726         rc = exp_check_ops(exp);
1727         if (rc)
1728                 return rc;
1729
1730         return MDP(exp->exp_obd, init_ea_size)(exp, ea_size, def_ea_size);
1731 }
1732
1733 static inline int md_intent_getattr_async(struct obd_export *exp,
1734                                           struct md_enqueue_info *minfo)
1735 {
1736         int rc;
1737
1738         rc = exp_check_ops(exp);
1739         if (rc)
1740                 return rc;
1741
1742         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1743                              LPROC_MD_INTENT_GETATTR_ASYNC);
1744
1745         return MDP(exp->exp_obd, intent_getattr_async)(exp, minfo);
1746 }
1747
1748 static inline int md_revalidate_lock(struct obd_export *exp,
1749                                      struct lookup_intent *it,
1750                                      struct lu_fid *fid, __u64 *bits)
1751 {
1752         int rc;
1753
1754         rc = exp_check_ops(exp);
1755         if (rc)
1756                 return rc;
1757
1758         lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1759                              LPROC_MD_REVALIDATE_LOCK);
1760
1761         return MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
1762 }
1763
1764 static inline int md_get_fid_from_lsm(struct obd_export *exp,
1765                                       const struct lmv_stripe_md *lsm,
1766                                       const char *name, int namelen,
1767                                       struct lu_fid *fid)
1768 {
1769         int rc;
1770
1771         rc = exp_check_ops(exp);
1772         if (rc)
1773                 return rc;
1774
1775         return MDP(exp->exp_obd, get_fid_from_lsm)(exp, lsm, name, namelen,
1776                                                    fid);
1777 }
1778
1779 /* Unpack an MD struct from disk to in-memory format.
1780  * Returns +ve size of unpacked MD (0 for free), or -ve error.
1781  *
1782  * If *plsm != NULL and lmm == NULL then *lsm will be freed.
1783  * If *plsm == NULL then it will be allocated.
1784  */
1785 static inline int md_unpackmd(struct obd_export *exp,
1786                               struct lmv_stripe_md **plsm,
1787                               const union lmv_mds_md *lmm, size_t lmm_size)
1788 {
1789         int rc;
1790
1791         rc = exp_check_ops(exp);
1792         if (rc)
1793                 return rc;
1794
1795         return MDP(exp->exp_obd, unpackmd)(exp, plsm, lmm, lmm_size);
1796 }
1797
1798 static inline int md_rmfid(struct obd_export *exp, struct fid_array *fa,
1799                            int *rcs, struct ptlrpc_request_set *set)
1800 {
1801         int rc;
1802
1803         rc = exp_check_ops(exp);
1804         if (rc)
1805                 return rc;
1806
1807         return MDP(exp->exp_obd, rmfid)(exp, fa, rcs, set);
1808 }
1809
1810 /* OBD Metadata Support */
1811
1812 extern int obd_init_caches(void);
1813 extern void obd_cleanup_caches(void);
1814
1815 typedef int (*register_lwp_cb)(void *data);
1816
1817 struct lwp_register_item {
1818         struct obd_export **lri_exp;
1819         register_lwp_cb     lri_cb_func;
1820         void               *lri_cb_data;
1821         struct list_head    lri_list;
1822         atomic_t            lri_ref;
1823         char                lri_name[MTI_NAME_MAXLEN];
1824 };
1825
1826 /* obd_mount.c */
1827 #ifdef HAVE_SERVER_SUPPORT
1828 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
1829                              register_lwp_cb cb_func, void *cb_data);
1830 void lustre_deregister_lwp_item(struct obd_export **exp);
1831 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
1832 void lustre_notify_lwp_list(struct obd_export *exp);
1833 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
1834 int lustre_tgt_register_fs(void);
1835 void lustre_tgt_unregister_fs(void);
1836 #endif /* HAVE_SERVER_SUPPORT */
1837 int lustre_check_exclusion(struct super_block *sb, char *svname);
1838
1839 /* lustre_peer.c    */
1840 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
1841 int class_add_uuid(const char *uuid, __u64 nid);
1842 int class_del_uuid (const char *uuid);
1843 int class_add_nids_to_uuid(struct obd_uuid *uuid, lnet_nid_t *nids,
1844                            int nid_count);
1845 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
1846
1847 /* class_obd.c */
1848 extern char obd_jobid_name[];
1849
1850 extern unsigned int obd_lbug_on_eviction;
1851 extern unsigned int obd_dump_on_eviction;
1852
1853 static inline bool do_dump_on_eviction(struct obd_device *exp_obd)
1854 {
1855         if (obd_lbug_on_eviction &&
1856             strncmp(exp_obd->obd_type->typ_name, LUSTRE_MGC_NAME,
1857                     strlen(LUSTRE_MGC_NAME))) {
1858                 CERROR("LBUG upon eviction\n");
1859                 LBUG();
1860         }
1861
1862         return obd_dump_on_eviction;
1863 }
1864
1865 /* statfs_pack.c */
1866 struct kstatfs;
1867 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
1868 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
1869
1870 /* root squash info */
1871 struct root_squash_info {
1872         uid_t                   rsi_uid;
1873         gid_t                   rsi_gid;
1874         struct list_head        rsi_nosquash_nids;
1875         spinlock_t              rsi_lock;
1876 };
1877
1878 int server_name2index(const char *svname, __u32 *idx, const char **endptr);
1879
1880 /* linux-module.c */
1881 struct obd_ioctl_data;
1882 int obd_ioctl_getdata(struct obd_ioctl_data **data, int *len, void __user *arg);
1883 int class_procfs_init(void);
1884 int class_procfs_clean(void);
1885
1886 extern void obd_heat_add(struct obd_heat_instance *instance,
1887                          unsigned int time_second, __u64 count,
1888                          unsigned int weight, unsigned int period_second);
1889 extern void obd_heat_decay(struct obd_heat_instance *instance,
1890                            __u64 time_second, unsigned int weight,
1891                            unsigned int period_second);
1892 extern __u64 obd_heat_get(struct obd_heat_instance *instance,
1893                           unsigned int time_second, unsigned int weight,
1894                           unsigned int period_second);
1895 extern void obd_heat_clear(struct obd_heat_instance *instance, int count);
1896 #endif /* __LINUX_OBD_CLASS_H */