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