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