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