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