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