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