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