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