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