Whamcloud - gitweb
LU-4499 nrs: adjust the order of REQ NRS initilization
[fs/lustre-release.git] / lustre / include / obd_class.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #ifndef __CLASS_OBD_H
37 #define __CLASS_OBD_H
38
39
40 #include <obd_support.h>
41 #include <lustre_import.h>
42 #include <lustre_net.h>
43 #include <obd.h>
44 #include <lustre_lib.h>
45 #include <lustre/lustre_idl.h>
46 #include <lprocfs_status.h>
47
48 #define OBD_STATFS_NODELAY      0x0001  /* requests should be send without delay
49                                          * and resends for avoid deadlocks */
50 #define OBD_STATFS_FROM_CACHE   0x0002  /* the statfs callback should not update
51                                          * obd_osfs_age */
52 #define OBD_STATFS_PTLRPCD      0x0004  /* requests will be sent via ptlrpcd
53                                          * instead of a specific set. This
54                                          * means that we cannot rely on the set
55                                          * interpret routine to be called.
56                                          * lov_statfs_fini() must thus be called
57                                          * by the request interpret routine */
58 #define OBD_STATFS_FOR_MDT0     0x0008  /* The statfs is only for retrieving
59                                          * information from MDT0. */
60
61 /* OBD Device Declarations */
62 extern struct obd_device *obd_devs[MAX_OBD_DEVICES];
63 extern struct list_head obd_types;
64 extern spinlock_t obd_types_lock;
65 extern rwlock_t obd_dev_lock;
66
67 /* OBD Operations Declarations */
68 extern struct obd_device *class_conn2obd(struct lustre_handle *);
69 extern struct obd_device *class_exp2obd(struct obd_export *);
70 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
71 extern int lustre_get_jobid(char *jobid);
72
73 struct lu_device_type;
74
75 /* genops.c */
76 struct obd_export *class_conn2export(struct lustre_handle *);
77 int class_register_type(struct obd_ops *, struct md_ops *, bool enable_proc,
78                         struct lprocfs_vars *module_vars,
79                         const char *nm, struct lu_device_type *ldt);
80 int class_unregister_type(const char *nm);
81
82 struct obd_device *class_newdev(const char *type_name, const char *name);
83 void class_release_dev(struct obd_device *obd);
84
85 int class_name2dev(const char *name);
86 struct obd_device *class_name2obd(const char *name);
87 int class_uuid2dev(struct obd_uuid *uuid);
88 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
89 void class_obd_list(void);
90 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
91                                           const char * typ_name,
92                                           struct obd_uuid *grp_uuid);
93 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid,
94                                            int *next);
95 struct obd_device * class_num2obd(int num);
96 int get_devices_count(void);
97
98 int class_notify_sptlrpc_conf(const char *fsname, int namelen);
99
100 char *obd_export_nid2str(struct obd_export *exp);
101
102 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid);
103 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid);
104 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep);
105
106 int obd_zombie_impexp_init(void);
107 void obd_zombie_impexp_stop(void);
108 void obd_zombie_impexp_cull(void);
109 void obd_zombie_barrier(void);
110 void obd_exports_barrier(struct obd_device *obd);
111 int kuc_len(int payload_len);
112 struct kuc_hdr * kuc_ptr(void *p);
113 int kuc_ispayload(void *p);
114 void *kuc_alloc(int payload_len, int transport, int type);
115 void kuc_free(void *p, int payload_len);
116 int obd_get_request_slot(struct client_obd *cli);
117 void obd_put_request_slot(struct client_obd *cli);
118 __u32 obd_get_max_rpcs_in_flight(struct client_obd *cli);
119 int obd_set_max_rpcs_in_flight(struct client_obd *cli, __u32 max);
120 __u16 obd_get_max_mod_rpcs_in_flight(struct client_obd *cli);
121 int obd_set_max_mod_rpcs_in_flight(struct client_obd *cli, __u16 max);
122 int obd_mod_rpc_stats_seq_show(struct client_obd *cli, struct seq_file *seq);
123
124 __u16 obd_get_mod_rpc_slot(struct client_obd *cli, __u32 opc,
125                            struct lookup_intent *it);
126 void obd_put_mod_rpc_slot(struct client_obd *cli, __u32 opc,
127                           struct lookup_intent *it, __u16 tag);
128
129 struct llog_handle;
130 struct llog_rec_hdr;
131 typedef int (*llog_cb_t)(const struct lu_env *, struct llog_handle *,
132                          struct llog_rec_hdr *, void *);
133
134 extern atomic_t         obd_stale_export_num;
135 extern struct list_head obd_stale_exports;
136 extern spinlock_t       obd_stale_export_lock;
137
138 struct obd_export *obd_stale_export_get(void);
139 void obd_stale_export_put(struct obd_export *exp);
140 void obd_stale_export_adjust(struct obd_export *exp);
141
142 /* obd_config.c */
143 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
144                                      const char *new_name);
145 int class_process_config(struct lustre_cfg *lcfg);
146 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
147                              struct lustre_cfg *lcfg, void *data);
148 int class_attach(struct lustre_cfg *lcfg);
149 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
150 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
151 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
152 struct obd_device *class_incref(struct obd_device *obd,
153                                 const char *scope, const void *source);
154 void class_decref(struct obd_device *obd,
155                   const char *scope, const void *source);
156 void dump_exports(struct obd_device *obd, int locks);
157 int class_config_llog_handler(const struct lu_env *env,
158                               struct llog_handle *handle,
159                               struct llog_rec_hdr *rec, void *data);
160 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg);
161 int class_add_uuid(const char *uuid, __u64 nid);
162
163 #define CFG_F_START     0x01   /* Set when we start updating from a log */
164 #define CFG_F_MARKER    0x02   /* We are within a maker */
165 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
166 #define CFG_F_COMPAT146 0x08   /* Allow old-style logs */
167 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
168
169 /* Passed as data param to class_config_parse_llog */
170 struct config_llog_instance {
171         char                    *cfg_obdname;
172         void                    *cfg_instance;
173         struct super_block      *cfg_sb;
174         struct obd_uuid          cfg_uuid;
175         llog_cb_t                cfg_callback;
176         int                      cfg_last_idx; /* for partial llog processing */
177         int                      cfg_flags;
178         __u32                    cfg_lwp_idx;
179 };
180 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
181                             char *name, struct config_llog_instance *cfg);
182 int class_config_dump_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
183                            char *name, struct config_llog_instance *cfg);
184
185 enum {
186         CONFIG_T_CONFIG  = 0,
187         CONFIG_T_SPTLRPC = 1,
188         CONFIG_T_RECOVER = 2,
189         CONFIG_T_PARAMS  = 3,
190         CONFIG_T_MAX     = 4
191 };
192
193 #define PARAMS_FILENAME "params"
194 #define LCTL_UPCALL     "lctl"
195
196 /* list of active configuration logs  */
197 struct config_llog_data {
198         struct ldlm_res_id          cld_resid;
199         struct config_llog_instance cld_cfg;
200         struct list_head            cld_list_chain;
201         atomic_t                    cld_refcount;
202         struct config_llog_data    *cld_sptlrpc;/* depended sptlrpc log */
203         struct config_llog_data    *cld_params; /* common parameters log */
204         struct config_llog_data    *cld_recover;/* imperative recover log */
205         struct obd_export          *cld_mgcexp;
206         struct mutex                cld_lock;
207         int                         cld_type;
208         unsigned int                cld_stopping:1, /* we were told to stop
209                                                      * watching */
210                                     cld_lostlock:1; /* lock not requeued */
211         char                        cld_logname[0];
212 };
213
214 struct lustre_profile {
215         struct list_head         lp_list;
216         char                    *lp_profile;
217         char                    *lp_dt;
218         char                    *lp_md;
219 };
220
221 struct lustre_profile *class_get_profile(const char * prof);
222 void class_del_profile(const char *prof);
223 void class_del_profiles(void);
224
225 #if LUSTRE_TRACKS_LOCK_EXP_REFS
226
227 void __class_export_add_lock_ref(struct obd_export *, struct ldlm_lock *);
228 void __class_export_del_lock_ref(struct obd_export *, struct ldlm_lock *);
229 extern void (*class_export_dump_hook)(struct obd_export *);
230
231 #else
232
233 #define __class_export_add_lock_ref(exp, lock)             do {} while(0)
234 #define __class_export_del_lock_ref(exp, lock)             do {} while(0)
235
236 #endif
237
238 #define class_export_rpc_inc(exp)                                       \
239 ({                                                                      \
240         atomic_inc(&(exp)->exp_rpc_count);                              \
241         CDEBUG(D_INFO, "RPC GETting export %p : new rpc_count %d\n",    \
242                (exp), atomic_read(&(exp)->exp_rpc_count));              \
243 })
244
245 #define class_export_rpc_dec(exp)                                       \
246 ({                                                                      \
247         LASSERT_ATOMIC_POS(&exp->exp_rpc_count);                        \
248         atomic_dec(&(exp)->exp_rpc_count);                              \
249         CDEBUG(D_INFO, "RPC PUTting export %p : new rpc_count %d\n",    \
250                (exp), atomic_read(&(exp)->exp_rpc_count));              \
251 })
252
253 #define class_export_lock_get(exp, lock)                                \
254 ({                                                                      \
255         atomic_inc(&(exp)->exp_locks_count);                            \
256         __class_export_add_lock_ref(exp, lock);                         \
257         CDEBUG(D_INFO, "lock GETting export %p : new locks_count %d\n", \
258                (exp), atomic_read(&(exp)->exp_locks_count));            \
259         class_export_get(exp);                                          \
260 })
261
262 #define class_export_lock_put(exp, lock)                                \
263 ({                                                                      \
264         LASSERT_ATOMIC_POS(&exp->exp_locks_count);                      \
265         atomic_dec(&(exp)->exp_locks_count);                            \
266         __class_export_del_lock_ref(exp, lock);                         \
267         CDEBUG(D_INFO, "lock PUTting export %p : new locks_count %d\n", \
268                (exp), atomic_read(&(exp)->exp_locks_count));            \
269         class_export_put(exp);                                          \
270 })
271
272 #define class_export_cb_get(exp)                                        \
273 ({                                                                      \
274         atomic_inc(&(exp)->exp_cb_count);                               \
275         CDEBUG(D_INFO, "callback GETting export %p : new cb_count %d\n",\
276                (exp), atomic_read(&(exp)->exp_cb_count));               \
277         class_export_get(exp);                                          \
278 })
279
280 #define class_export_cb_put(exp)                                        \
281 ({                                                                      \
282         LASSERT_ATOMIC_POS(&exp->exp_cb_count);                         \
283         atomic_dec(&(exp)->exp_cb_count);                               \
284         CDEBUG(D_INFO, "callback PUTting export %p : new cb_count %d\n",\
285                (exp), atomic_read(&(exp)->exp_cb_count));               \
286         class_export_put(exp);                                          \
287 })
288
289 /* genops.c */
290 struct obd_export *class_export_get(struct obd_export *exp);
291 void class_export_put(struct obd_export *exp);
292 struct obd_export *class_new_export(struct obd_device *obddev,
293                                     struct obd_uuid *cluuid);
294 void class_unlink_export(struct obd_export *exp);
295
296 struct obd_import *class_import_get(struct obd_import *);
297 void class_import_put(struct obd_import *);
298 struct obd_import *class_new_import(struct obd_device *obd);
299 void class_destroy_import(struct obd_import *exp);
300
301 struct obd_type *class_search_type(const char *name);
302 struct obd_type *class_get_type(const char *name);
303 void class_put_type(struct obd_type *type);
304 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
305                   struct obd_uuid *cluuid);
306 int class_disconnect(struct obd_export *exp);
307 void class_fail_export(struct obd_export *exp);
308 int class_connected_export(struct obd_export *exp);
309 void class_disconnect_exports(struct obd_device *obddev);
310 int class_manual_cleanup(struct obd_device *obd);
311 void class_disconnect_stale_exports(struct obd_device *,
312                                     int (*test_export)(struct obd_export *));
313 static inline enum obd_option exp_flags_from_obd(struct obd_device *obd)
314 {
315         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
316                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
317                 (obd->obd_abort_recovery ? OBD_OPT_ABORT_RECOV : 0) |
318                 0);
319 }
320
321 #ifdef HAVE_SERVER_SUPPORT
322 static inline struct lu_target *class_exp2tgt(struct obd_export *exp)
323 {
324         LASSERT(exp->exp_obd);
325         if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC)
326                 return NULL;
327         return exp->exp_obd->u.obt.obt_lut;
328 }
329
330 static inline struct lr_server_data *class_server_data(struct obd_device *obd)
331 {
332         LASSERT(obd->u.obt.obt_lut);
333         return &obd->u.obt.obt_lut->lut_lsd;
334 }
335 #endif
336
337 /* obdo.c */
338 struct lu_attr;
339 struct inode;
340
341 void obdo_from_la(struct obdo *dst, const struct lu_attr *la, u64 valid);
342 void la_from_obdo(struct lu_attr *la, const struct obdo *dst, u64 valid);
343
344 void obdo_cpy_md(struct obdo *dst, const struct obdo *src, u64 valid);
345 void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj);
346
347 #define OBT(dev)        (dev)->obd_type
348 #define OBP(dev, op)    (dev)->obd_type->typ_dt_ops->o_ ## op
349 #define MDP(dev, op)    (dev)->obd_type->typ_md_ops->m_ ## op
350 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
351
352 /* Ensure obd_setup: used for cleanup which must be called
353    while obd is stopping */
354 #define OBD_CHECK_DEV(obd)                                      \
355 do {                                                            \
356         if (!(obd)) {                                           \
357                 CERROR("NULL device\n");                        \
358                 RETURN(-ENODEV);                                \
359         }                                                       \
360 } while (0)
361
362 /* ensure obd_setup and !obd_stopping */
363 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
364 do {                                                            \
365         OBD_CHECK_DEV(obd);                                     \
366         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
367                 CERROR("Device %d not setup\n",                 \
368                        (obd)->obd_minor);                       \
369                 RETURN(-ENODEV);                                \
370         }                                                       \
371 } while (0)
372
373
374 #ifdef CONFIG_PROC_FS
375 #define OBD_COUNTER_OFFSET(op)                                                 \
376         ((offsetof(struct obd_ops, o_ ## op) -                                 \
377           offsetof(struct obd_ops, o_iocontrol))                               \
378          / sizeof(((struct obd_ops *)NULL)->o_iocontrol))
379
380 /* The '- 1' below is for o_owner. */
381 #define NUM_OBD_STATS                                                          \
382         (sizeof(struct obd_ops) /                                              \
383          sizeof(((struct obd_ops *)NULL)->o_iocontrol) - 1)
384
385 #define OBD_COUNTER_INCREMENT(obd, op)                                         \
386         lprocfs_counter_incr((obd)->obd_stats,                                 \
387                              (obd)->obd_cntr_base + OBD_COUNTER_OFFSET(op))
388
389 #define EXP_COUNTER_INCREMENT(exp, op)                                         \
390         do {                                                                   \
391                 unsigned int _off;                                             \
392                 _off = (exp)->exp_obd->obd_cntr_base + OBD_COUNTER_OFFSET(op); \
393                 lprocfs_counter_incr((exp)->exp_obd->obd_stats, _off);         \
394                 if ((exp)->exp_obd->obd_uses_nid_stats &&                      \
395                     (exp)->exp_nid_stats != NULL)                              \
396                         lprocfs_counter_incr((exp)->exp_nid_stats->nid_stats,  \
397                                              _off);                            \
398         } while (0)
399
400 #define _MD_COUNTER_OFFSET(m_op)                                               \
401         ((offsetof(struct md_ops, m_op) -                                      \
402           offsetof(struct md_ops, MD_STATS_FIRST_OP)) /                        \
403          sizeof(((struct md_ops *)NULL)->MD_STATS_FIRST_OP))
404
405 #define MD_COUNTER_OFFSET(op) _MD_COUNTER_OFFSET(m_ ## op)
406
407 #define NUM_MD_STATS                                                           \
408         (_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) -                                \
409          _MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) + 1)
410
411 /* Note that we only increment md counters for ops whose offset is less
412  * than NUM_MD_STATS. This is explained in a comment in the definition
413  * of struct md_ops. */
414 #define EXP_MD_COUNTER_INCREMENT(exp, op)                                      \
415         do {                                                                   \
416                 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS)                      \
417                         lprocfs_counter_incr((exp)->exp_obd->obd_md_stats,     \
418                                         (exp)->exp_obd->obd_md_cntr_base +     \
419                                         MD_COUNTER_OFFSET(op));                \
420         } while (0)
421
422 #else
423 #define OBD_COUNTER_OFFSET(op)
424 #define OBD_COUNTER_INCREMENT(obd, op)
425 #define EXP_COUNTER_INCREMENT(exp, op)
426 #define EXP_MD_COUNTER_INCREMENT(exp, op)
427 #endif
428
429 static inline int lprocfs_nid_ldlm_stats_init(struct nid_stat* tmp)
430 {
431         /* Always add in ldlm_stats */
432         tmp->nid_ldlm_stats = lprocfs_alloc_stats(LDLM_LAST_OPC - LDLM_FIRST_OPC
433                                                   ,LPROCFS_STATS_FLAG_NOPERCPU);
434         if (tmp->nid_ldlm_stats == NULL)
435                 return -ENOMEM;
436
437         lprocfs_init_ldlm_stats(tmp->nid_ldlm_stats);
438
439         return lprocfs_register_stats(tmp->nid_proc, "ldlm_stats",
440                                       tmp->nid_ldlm_stats);
441 }
442
443 #define EXP_CHECK_MD_OP(exp, op)                                \
444 do {                                                            \
445         if ((exp) == NULL) {                                    \
446                 CERROR("obd_" #op ": NULL export\n");           \
447                 RETURN(-ENODEV);                                \
448         }                                                       \
449         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
450                 CERROR("obd_" #op ": cleaned up obd\n");        \
451                 RETURN(-EOPNOTSUPP);                            \
452         }                                                       \
453         if (!OBT((exp)->exp_obd) || !MDP((exp)->exp_obd, op)) { \
454                 CERROR("obd_" #op ": dev %s/%d no operation\n", \
455                        (exp)->exp_obd->obd_name,                \
456                        (exp)->exp_obd->obd_minor);              \
457                 RETURN(-EOPNOTSUPP);                            \
458         }                                                       \
459 } while (0)
460
461
462 #define OBD_CHECK_DT_OP(obd, op, err)                           \
463 do {                                                            \
464         if (!OBT(obd) || !OBP((obd), op)) {                     \
465                 if (err)                                        \
466                         CERROR("obd_" #op ": dev %d no operation\n",    \
467                                obd->obd_minor);                 \
468                 RETURN(err);                                    \
469         }                                                       \
470 } while (0)
471
472 #define EXP_CHECK_DT_OP(exp, op)                                \
473 do {                                                            \
474         if ((exp) == NULL) {                                    \
475                 CERROR("obd_" #op ": NULL export\n");           \
476                 RETURN(-ENODEV);                                \
477         }                                                       \
478         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
479                 CERROR("obd_" #op ": cleaned up obd\n");        \
480                 RETURN(-EOPNOTSUPP);                            \
481         }                                                       \
482         if (!OBT((exp)->exp_obd) || !OBP((exp)->exp_obd, op)) { \
483                 CERROR("obd_" #op ": dev %d no operation\n",    \
484                        (exp)->exp_obd->obd_minor);              \
485                 RETURN(-EOPNOTSUPP);                            \
486         }                                                       \
487 } while (0)
488
489 #define CTXT_CHECK_OP(ctxt, op, err)                                 \
490 do {                                                                 \
491         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {             \
492                 if (err)                                             \
493                         CERROR("lop_" #op ": dev %d no operation\n", \
494                                ctxt->loc_obd->obd_minor);            \
495                 RETURN(err);                                         \
496         }                                                            \
497 } while (0)
498
499 static inline int class_devno_max(void)
500 {
501         return MAX_OBD_DEVICES;
502 }
503
504 static inline int obd_get_info(const struct lu_env *env, struct obd_export *exp,
505                                __u32 keylen, void *key,
506                                __u32 *vallen, void *val)
507 {
508         int rc;
509         ENTRY;
510
511         EXP_CHECK_DT_OP(exp, get_info);
512         EXP_COUNTER_INCREMENT(exp, get_info);
513
514         rc = OBP(exp->exp_obd, get_info)(env, exp, keylen, key, vallen, val);
515         RETURN(rc);
516 }
517
518 static inline int obd_set_info_async(const struct lu_env *env,
519                                      struct obd_export *exp,
520                                      __u32 keylen, void *key,
521                                      __u32 vallen, void *val,
522                                      struct ptlrpc_request_set *set)
523 {
524         int rc;
525         ENTRY;
526
527         EXP_CHECK_DT_OP(exp, set_info_async);
528         EXP_COUNTER_INCREMENT(exp, set_info_async);
529
530         rc = OBP(exp->exp_obd, set_info_async)(env, exp, keylen, key, vallen,
531                                                val, set);
532         RETURN(rc);
533 }
534
535 /*
536  * obd-lu integration.
537  *
538  * Functionality is being moved into new lu_device-based layering, but some
539  * pieces of configuration process are still based on obd devices.
540  *
541  * Specifically, lu_device_type_operations::ldto_device_alloc() methods fully
542  * subsume ->o_setup() methods of obd devices they replace. The same for
543  * lu_device_operations::ldo_process_config() and ->o_process_config(). As a
544  * result, obd_setup() and obd_process_config() branch and call one XOR
545  * another.
546  *
547  * Yet neither lu_device_type_operations::ldto_device_fini() nor
548  * lu_device_type_operations::ldto_device_free() fully implement the
549  * functionality of ->o_precleanup() and ->o_cleanup() they override. Hence,
550  * obd_precleanup() and obd_cleanup() call both lu_device and obd operations.
551  */
552
553 #define DECLARE_LU_VARS(ldt, d)                 \
554         struct lu_device_type *ldt;       \
555         struct lu_device *d
556
557 static inline int obd_setup(struct obd_device *obd, struct lustre_cfg *cfg)
558 {
559         int rc;
560         DECLARE_LU_VARS(ldt, d);
561         ENTRY;
562
563         ldt = obd->obd_type->typ_lu;
564         if (ldt != NULL) {
565                 struct lu_context  session_ctx;
566                 struct lu_env env;
567                 lu_context_init(&session_ctx, LCT_SESSION | LCT_SERVER_SESSION);
568                 session_ctx.lc_thread = NULL;
569                 lu_context_enter(&session_ctx);
570
571                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
572                 if (rc == 0) {
573                         env.le_ses = &session_ctx;
574                         d = ldt->ldt_ops->ldto_device_alloc(&env, ldt, cfg);
575                         lu_env_fini(&env);
576                         if (!IS_ERR(d)) {
577                                 obd->obd_lu_dev = d;
578                                 d->ld_obd = obd;
579                                 rc = 0;
580                         } else
581                                 rc = PTR_ERR(d);
582                 }
583                 lu_context_exit(&session_ctx);
584                 lu_context_fini(&session_ctx);
585
586         } else {
587                 OBD_CHECK_DT_OP(obd, setup, -EOPNOTSUPP);
588                 OBD_COUNTER_INCREMENT(obd, setup);
589                 rc = OBP(obd, setup)(obd, cfg);
590         }
591         RETURN(rc);
592 }
593
594 static inline int obd_precleanup(struct obd_device *obd,
595                                  enum obd_cleanup_stage cleanup_stage)
596 {
597         int rc;
598         DECLARE_LU_VARS(ldt, d);
599         ENTRY;
600
601         OBD_CHECK_DEV(obd);
602         ldt = obd->obd_type->typ_lu;
603         d = obd->obd_lu_dev;
604         if (ldt != NULL && d != NULL) {
605                 if (cleanup_stage == OBD_CLEANUP_EXPORTS) {
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         }
615         OBD_CHECK_DT_OP(obd, precleanup, 0);
616         OBD_COUNTER_INCREMENT(obd, precleanup);
617
618         rc = OBP(obd, precleanup)(obd, cleanup_stage);
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_getstatus(struct obd_export *exp, struct lu_fid *fid)
1272 {
1273         int rc;
1274
1275         ENTRY;
1276         EXP_CHECK_MD_OP(exp, getstatus);
1277         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1278         rc = MDP(exp->exp_obd, getstatus)(exp, fid);
1279
1280         RETURN(rc);
1281 }
1282
1283 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1284                              struct ptlrpc_request **request)
1285 {
1286         int rc;
1287         ENTRY;
1288         EXP_CHECK_MD_OP(exp, getattr);
1289         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1290         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1291         RETURN(rc);
1292 }
1293
1294 static inline int md_null_inode(struct obd_export *exp,
1295                                    const struct lu_fid *fid)
1296 {
1297         int rc;
1298         ENTRY;
1299         EXP_CHECK_MD_OP(exp, null_inode);
1300         EXP_MD_COUNTER_INCREMENT(exp, null_inode);
1301         rc = MDP(exp->exp_obd, null_inode)(exp, fid);
1302         RETURN(rc);
1303 }
1304
1305 static inline int md_find_cbdata(struct obd_export *exp,
1306                                  const struct lu_fid *fid,
1307                                  ldlm_iterator_t it, void *data)
1308 {
1309         int rc;
1310         ENTRY;
1311         EXP_CHECK_MD_OP(exp, find_cbdata);
1312         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1313         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1314         RETURN(rc);
1315 }
1316
1317 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1318                            struct md_open_data *mod,
1319                            struct ptlrpc_request **request)
1320 {
1321         int rc;
1322         ENTRY;
1323         EXP_CHECK_MD_OP(exp, close);
1324         EXP_MD_COUNTER_INCREMENT(exp, close);
1325         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1326         RETURN(rc);
1327 }
1328
1329 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1330                             const void *data, size_t datalen, umode_t mode,
1331                             uid_t uid, gid_t gid, cfs_cap_t cap_effective,
1332                             __u64 rdev, struct ptlrpc_request **request)
1333 {
1334         int rc;
1335         ENTRY;
1336         EXP_CHECK_MD_OP(exp, create);
1337         EXP_MD_COUNTER_INCREMENT(exp, create);
1338         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1339                                        uid, gid, cap_effective, rdev, request);
1340         RETURN(rc);
1341 }
1342
1343 static inline int md_enqueue(struct obd_export *exp,
1344                              struct ldlm_enqueue_info *einfo,
1345                              const union ldlm_policy_data *policy,
1346                              struct lookup_intent *it,
1347                              struct md_op_data *op_data,
1348                              struct lustre_handle *lockh,
1349                              __u64 extra_lock_flags)
1350 {
1351         int rc;
1352         ENTRY;
1353         EXP_CHECK_MD_OP(exp, enqueue);
1354         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1355         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, policy, it, op_data, lockh,
1356                                         extra_lock_flags);
1357         RETURN(rc);
1358 }
1359
1360 static inline int md_getattr_name(struct obd_export *exp,
1361                                   struct md_op_data *op_data,
1362                                   struct ptlrpc_request **request)
1363 {
1364         int rc;
1365         ENTRY;
1366         EXP_CHECK_MD_OP(exp, getattr_name);
1367         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1368         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1369         RETURN(rc);
1370 }
1371
1372 static inline int md_intent_lock(struct obd_export *exp,
1373                                  struct md_op_data *op_data,
1374                                  struct lookup_intent *it,
1375                                  struct ptlrpc_request **reqp,
1376                                  ldlm_blocking_callback cb_blocking,
1377                                  __u64 extra_lock_flags)
1378 {
1379         int rc;
1380         ENTRY;
1381         EXP_CHECK_MD_OP(exp, intent_lock);
1382         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1383         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, it, reqp, cb_blocking,
1384                                             extra_lock_flags);
1385         RETURN(rc);
1386 }
1387
1388 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1389                           struct ptlrpc_request **request)
1390 {
1391         int rc;
1392         ENTRY;
1393         EXP_CHECK_MD_OP(exp, link);
1394         EXP_MD_COUNTER_INCREMENT(exp, link);
1395         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1396         RETURN(rc);
1397 }
1398
1399 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1400                             const char *old, size_t oldlen, const char *new,
1401                             size_t newlen, struct ptlrpc_request **request)
1402 {
1403         int rc;
1404         ENTRY;
1405         EXP_CHECK_MD_OP(exp, rename);
1406         EXP_MD_COUNTER_INCREMENT(exp, rename);
1407         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1408                                        newlen, request);
1409         RETURN(rc);
1410 }
1411
1412 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1413                              void *ea, size_t ealen,
1414                              struct ptlrpc_request **request)
1415 {
1416         int rc;
1417         ENTRY;
1418         EXP_CHECK_MD_OP(exp, setattr);
1419         EXP_MD_COUNTER_INCREMENT(exp, setattr);
1420         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen, request);
1421         RETURN(rc);
1422 }
1423
1424 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
1425                            struct ptlrpc_request **request)
1426 {
1427         int rc;
1428
1429         ENTRY;
1430         EXP_CHECK_MD_OP(exp, fsync);
1431         EXP_MD_COUNTER_INCREMENT(exp, fsync);
1432         rc = MDP(exp->exp_obd, fsync)(exp, fid, request);
1433
1434         RETURN(rc);
1435 }
1436
1437 static inline int md_read_page(struct obd_export *exp,
1438                                struct md_op_data *op_data,
1439                                struct md_callback *cb_op,
1440                                __u64  hash_offset,
1441                                struct page **ppage)
1442 {
1443         int rc;
1444         ENTRY;
1445         EXP_CHECK_MD_OP(exp, read_page);
1446         EXP_MD_COUNTER_INCREMENT(exp, read_page);
1447         rc = MDP(exp->exp_obd, read_page)(exp, op_data, cb_op, hash_offset,
1448                                           ppage);
1449         RETURN(rc);
1450 }
1451
1452 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1453                             struct ptlrpc_request **request)
1454 {
1455         int rc;
1456         ENTRY;
1457         EXP_CHECK_MD_OP(exp, unlink);
1458         EXP_MD_COUNTER_INCREMENT(exp, unlink);
1459         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
1460         RETURN(rc);
1461 }
1462
1463 static inline int md_get_lustre_md(struct obd_export *exp,
1464                                    struct ptlrpc_request *req,
1465                                    struct obd_export *dt_exp,
1466                                    struct obd_export *md_exp,
1467                                    struct lustre_md *md)
1468 {
1469         ENTRY;
1470         EXP_CHECK_MD_OP(exp, get_lustre_md);
1471         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
1472         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
1473 }
1474
1475 static inline int md_free_lustre_md(struct obd_export *exp,
1476                                     struct lustre_md *md)
1477 {
1478         ENTRY;
1479         EXP_CHECK_MD_OP(exp, free_lustre_md);
1480         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
1481         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
1482 }
1483
1484 static inline int md_merge_attr(struct obd_export *exp,
1485                                 const struct lmv_stripe_md *lsm,
1486                                 struct cl_attr *attr,
1487                                 ldlm_blocking_callback cb)
1488 {
1489         ENTRY;
1490         EXP_CHECK_MD_OP(exp, merge_attr);
1491         EXP_MD_COUNTER_INCREMENT(exp, merge_attr);
1492         RETURN(MDP(exp->exp_obd, merge_attr)(exp, lsm, attr, cb));
1493 }
1494
1495 static inline int md_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1496                               u64 valid, const char *name,
1497                               const char *input, int input_size,
1498                               int output_size, int flags, __u32 suppgid,
1499                               struct ptlrpc_request **request)
1500 {
1501         ENTRY;
1502         EXP_CHECK_MD_OP(exp, setxattr);
1503         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
1504         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, valid, name, input,
1505                                            input_size, output_size, flags,
1506                                            suppgid, request));
1507 }
1508
1509 static inline int md_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1510                               u64 valid, const char *name,
1511                               const char *input, int input_size,
1512                               int output_size, int flags,
1513                               struct ptlrpc_request **request)
1514 {
1515         ENTRY;
1516         EXP_CHECK_MD_OP(exp, getxattr);
1517         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
1518         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, valid, name, input,
1519                                            input_size, output_size, flags,
1520                                            request));
1521 }
1522
1523 static inline int md_set_open_replay_data(struct obd_export *exp,
1524                                           struct obd_client_handle *och,
1525                                           struct lookup_intent *it)
1526 {
1527         ENTRY;
1528         EXP_CHECK_MD_OP(exp, set_open_replay_data);
1529         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
1530         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, it));
1531 }
1532
1533 static inline int md_clear_open_replay_data(struct obd_export *exp,
1534                                             struct obd_client_handle *och)
1535 {
1536         ENTRY;
1537         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
1538         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
1539         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
1540 }
1541
1542 static inline int md_set_lock_data(struct obd_export *exp,
1543                                    __u64 *lockh, void *data, __u64 *bits)
1544 {
1545         ENTRY;
1546         EXP_CHECK_MD_OP(exp, set_lock_data);
1547         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
1548         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
1549 }
1550
1551 static inline int md_cancel_unused(struct obd_export *exp,
1552                                    const struct lu_fid *fid,
1553                                    ldlm_policy_data_t *policy,
1554                                    ldlm_mode_t mode,
1555                                    ldlm_cancel_flags_t flags,
1556                                    void *opaque)
1557 {
1558         int rc;
1559         ENTRY;
1560
1561         EXP_CHECK_MD_OP(exp, cancel_unused);
1562         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
1563
1564         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
1565                                               flags, opaque);
1566         RETURN(rc);
1567 }
1568
1569 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, __u64 flags,
1570                                         const struct lu_fid *fid,
1571                                         ldlm_type_t type,
1572                                         ldlm_policy_data_t *policy,
1573                                         ldlm_mode_t mode,
1574                                         struct lustre_handle *lockh)
1575 {
1576         ENTRY;
1577         EXP_CHECK_MD_OP(exp, lock_match);
1578         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
1579         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1580                                              policy, mode, lockh));
1581 }
1582
1583 static inline int md_init_ea_size(struct obd_export *exp, __u32 ea_size,
1584                                   __u32 def_ea_size)
1585 {
1586         ENTRY;
1587         EXP_CHECK_MD_OP(exp, init_ea_size);
1588         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
1589         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, ea_size, def_ea_size));
1590 }
1591
1592 static inline int md_get_remote_perm(struct obd_export *exp,
1593                                      const struct lu_fid *fid, u32 suppgid,
1594                                      struct ptlrpc_request **request)
1595 {
1596         ENTRY;
1597         EXP_CHECK_MD_OP(exp, get_remote_perm);
1598         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
1599
1600         RETURN(MDP(exp->exp_obd, get_remote_perm)(exp, fid, suppgid, request));
1601 }
1602
1603 static inline int md_intent_getattr_async(struct obd_export *exp,
1604                                           struct md_enqueue_info *minfo,
1605                                           struct ldlm_enqueue_info *einfo)
1606 {
1607         int rc;
1608         ENTRY;
1609         EXP_CHECK_MD_OP(exp, intent_getattr_async);
1610         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
1611         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
1612         RETURN(rc);
1613 }
1614
1615 static inline int md_revalidate_lock(struct obd_export *exp,
1616                                      struct lookup_intent *it,
1617                                      struct lu_fid *fid, __u64 *bits)
1618 {
1619         int rc;
1620         ENTRY;
1621         EXP_CHECK_MD_OP(exp, revalidate_lock);
1622         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
1623         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
1624         RETURN(rc);
1625 }
1626
1627 static inline int md_get_fid_from_lsm(struct obd_export *exp,
1628                                       const struct lmv_stripe_md *lsm,
1629                                       const char *name, int namelen,
1630                                       struct lu_fid *fid)
1631 {
1632         int rc;
1633         ENTRY;
1634         EXP_CHECK_MD_OP(exp, get_fid_from_lsm);
1635         EXP_MD_COUNTER_INCREMENT(exp, get_fid_from_lsm);
1636         rc = MDP(exp->exp_obd, get_fid_from_lsm)(exp, lsm, name, namelen, fid);
1637         RETURN(rc);
1638 }
1639
1640
1641 /* Unpack an MD struct from disk to in-memory format.
1642  * Returns +ve size of unpacked MD (0 for free), or -ve error.
1643  *
1644  * If *plsm != NULL and lmm == NULL then *lsm will be freed.
1645  * If *plsm == NULL then it will be allocated.
1646  */
1647 static inline int md_unpackmd(struct obd_export *exp,
1648                               struct lmv_stripe_md **plsm,
1649                               const union lmv_mds_md *lmm, size_t lmm_size)
1650 {
1651         int rc;
1652         ENTRY;
1653         EXP_CHECK_MD_OP(exp, unpackmd);
1654         EXP_MD_COUNTER_INCREMENT(exp, unpackmd);
1655         rc = MDP(exp->exp_obd, unpackmd)(exp, plsm, lmm, lmm_size);
1656         RETURN(rc);
1657 }
1658
1659 /* OBD Metadata Support */
1660
1661 extern int obd_init_caches(void);
1662 extern void obd_cleanup_caches(void);
1663
1664 /* support routines */
1665 extern struct kmem_cache *obdo_cachep;
1666
1667 #define OBDO_ALLOC(ptr)                                                       \
1668 do {                                                                          \
1669         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, GFP_NOFS);             \
1670 } while(0)
1671
1672 #define OBDO_FREE(ptr)                                                        \
1673 do {                                                                          \
1674         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
1675 } while(0)
1676
1677
1678 typedef int (*register_lwp_cb)(void *data);
1679
1680 struct lwp_register_item {
1681         struct obd_export **lri_exp;
1682         register_lwp_cb     lri_cb_func;
1683         void               *lri_cb_data;
1684         struct list_head            lri_list;
1685         char                lri_name[MTI_NAME_MAXLEN];
1686 };
1687
1688 /* I'm as embarrassed about this as you are.
1689  *
1690  * <shaver> // XXX do not look into _superhack with remaining eye
1691  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1692 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1693
1694 /* obd_mount.c */
1695 #ifdef HAVE_SERVER_SUPPORT
1696 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
1697                              register_lwp_cb cb_func, void *cb_data);
1698 void lustre_deregister_lwp_item(struct obd_export **exp);
1699 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
1700 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
1701 #endif /* HAVE_SERVER_SUPPORT */
1702 int lustre_register_fs(void);
1703 int lustre_unregister_fs(void);
1704 int lustre_check_exclusion(struct super_block *sb, char *svname);
1705
1706 /* sysctl.c */
1707 extern void obd_sysctl_init (void);
1708 extern void obd_sysctl_clean (void);
1709
1710 /* uuid.c  */
1711 typedef __u8 class_uuid_t[16];
1712 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1713
1714 /* lustre_peer.c    */
1715 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
1716 int class_add_uuid(const char *uuid, __u64 nid);
1717 int class_del_uuid (const char *uuid);
1718 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
1719 void class_init_uuidlist(void);
1720 void class_exit_uuidlist(void);
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 */