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