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