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