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