Whamcloud - gitweb
r=zab,phil
[fs/lustre-release.git] / lustre / include / linux / obd_class.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef __LINUX_CLASS_OBD_H
24 #define __LINUX_CLASS_OBD_H
25
26 #ifndef __KERNEL__
27 #include <sys/types.h>
28 #include <portals/list.h>
29 #else
30 #include <asm/segment.h>
31 #include <asm/uaccess.h>
32 #include <linux/types.h>
33 #include <linux/fs.h>
34 #include <linux/time.h>
35 #include <linux/timer.h>
36 #endif
37
38 #include <linux/obd_support.h>
39 #include <linux/lustre_import.h>
40 #include <linux/lustre_net.h>
41 #include <linux/obd.h>
42 #include <linux/lustre_lib.h>
43 #include <linux/lustre_idl.h>
44 #include <linux/lprocfs_status.h>
45
46 /* OBD Device Declarations */
47 #define MAX_OBD_DEVICES 128
48 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
49
50 /* OBD Operations Declarations */
51 extern struct obd_device *class_conn2obd(struct lustre_handle *);
52 extern struct obd_device *class_exp2obd(struct obd_export *);
53
54 /* genops.c */
55 struct obd_export *class_conn2export(struct lustre_handle *);
56 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *, char *nm);
57 int class_unregister_type(char *nm);
58
59 struct obd_device *class_newdev(int *dev);
60
61 int class_name2dev(char *name);
62 struct obd_device *class_name2obd(char *name);
63 int class_uuid2dev(struct obd_uuid *uuid);
64 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
65 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid, 
66                                           char * typ_name,
67                                           struct obd_uuid *grp_uuid);
68
69 void osic_init(struct obd_sync_io_container **osic);
70 void osic_add_one(struct obd_sync_io_container *osic);
71 void osic_complete_one(struct obd_sync_io_container *osic, int rc);
72 void osic_release(struct obd_sync_io_container *osic);
73 int osic_wait(struct obd_sync_io_container *osic);
74
75 /* config.c */
76 int class_process_config(struct lustre_cfg *lcfg);
77 int class_attach(struct lustre_cfg *lcfg);
78 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
79 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
80 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
81
82 /* Passed as data param to class_config_parse_llog */
83 struct config_llog_instance {
84         char * cfg_instance;
85         struct obd_uuid cfg_uuid;
86         ptl_nid_t  cfg_local_nid;
87 };
88 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name, 
89                             struct config_llog_instance *cfg);
90 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name, 
91                            struct config_llog_instance *cfg);
92
93 struct lustre_profile {
94         struct list_head lp_list;
95         char * lp_profile;
96         char * lp_osc;
97         char * lp_mdc;
98 };
99
100 struct lustre_profile *class_get_profile(char * prof);
101
102 #define class_export_get(exp)                                                  \
103 ({                                                                             \
104         struct obd_export *exp_ = exp;                                         \
105         atomic_inc(&exp_->exp_refcount);                                       \
106         CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp_,          \
107                atomic_read(&exp_->exp_refcount));                              \
108         exp_;                                                                  \
109 })
110
111 #define class_export_put(exp)                                                  \
112 do {                                                                           \
113         LASSERT((exp) != NULL);                                                \
114         CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", (exp),         \
115                atomic_read(&(exp)->exp_refcount) - 1);                         \
116         LASSERT(atomic_read(&(exp)->exp_refcount) > 0);                        \
117         LASSERT(atomic_read(&(exp)->exp_refcount) < 0x5a5a5a);                 \
118         __class_export_put(exp);                                               \
119 } while (0)
120 void __class_export_put(struct obd_export *);
121 struct obd_export *class_new_export(struct obd_device *obddev);
122 void class_unlink_export(struct obd_export *exp);
123
124 struct obd_import *class_import_get(struct obd_import *);
125 void class_import_put(struct obd_import *);
126 struct obd_import *class_new_import(void);
127 void class_destroy_import(struct obd_import *exp);
128
129 struct obd_type *class_get_type(char *name);
130 void class_put_type(struct obd_type *type);
131 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
132                   struct obd_uuid *cluuid);
133 int class_disconnect(struct obd_export *exp, int failover);
134 void class_disconnect_exports(struct obd_device *obddev, int failover);
135 /* generic operations shared by various OBD types */
136 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
137 int class_multi_cleanup(struct obd_device *obddev);
138
139 /* obdo.c */
140 #ifdef __KERNEL__
141 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned ia_valid);
142 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid);
143 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid);
144 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid);
145 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid);
146 #endif
147 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid);
148 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare);
149 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj);
150
151 static inline int obd_check_conn(struct lustre_handle *conn)
152 {
153         struct obd_device *obd;
154         if (!conn) {
155                 CERROR("NULL conn\n");
156                 RETURN(-ENOTCONN);
157         }
158
159         obd = class_conn2obd(conn);
160         if (!obd) {
161                 CERROR("NULL obd\n");
162                 RETURN(-ENODEV);
163         }
164
165         if (!obd->obd_attached) {
166                 CERROR("obd %d not attached\n", obd->obd_minor);
167                 RETURN(-ENODEV);
168         }
169
170         if (!obd->obd_set_up) {
171                 CERROR("obd %d not setup\n", obd->obd_minor);
172                 RETURN(-ENODEV);
173         }
174
175         if (!obd->obd_type) {
176                 CERROR("obd %d not typed\n", obd->obd_minor);
177                 RETURN(-ENODEV);
178         }
179
180         if (!obd->obd_type->typ_ops) {
181                 CERROR("obd_check_conn: obd %d no operations\n",
182                        obd->obd_minor);
183                 RETURN(-EOPNOTSUPP);
184         }
185         return 0;
186 }
187
188
189 #define OBT(dev)        (dev)->obd_type
190 #define OBP(dev, op)    (dev)->obd_type->typ_ops->o_ ## op
191 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
192
193 /* Ensure obd_setup: used for disconnect which might be called while
194    an obd is stopping. */
195 #define OBD_CHECK_SETUP(conn, exp)                                      \
196 do {                                                                    \
197         if (!(conn)) {                                                  \
198                 CERROR("NULL connection\n");                            \
199                 RETURN(-EINVAL);                                        \
200         }                                                               \
201                                                                         \
202         exp = class_conn2export(conn);                                  \
203         if (!(exp)) {                                                   \
204                 CERROR("No export for conn "LPX64"\n", (conn)->cookie); \
205                 RETURN(-EINVAL);                                        \
206         }                                                               \
207                                                                         \
208         if (!(exp)->exp_obd->obd_set_up) {                              \
209                 CERROR("Device %d not setup\n",                         \
210                        (exp)->exp_obd->obd_minor);                      \
211                 class_export_put(exp);                                  \
212                 RETURN(-EINVAL);                                        \
213         }                                                               \
214 } while (0)
215
216 /* Ensure obd_setup and !obd_stopping. */
217 #define OBD_CHECK_ACTIVE(conn, exp)                                     \
218 do {                                                                    \
219         if (!(conn)) {                                                  \
220                 CERROR("NULL connection\n");                            \
221                 RETURN(-EINVAL);                                        \
222         }                                                               \
223                                                                         \
224         exp = class_conn2export(conn);                                  \
225         if (!(exp)) {                                                   \
226                 CERROR("No export for conn "LPX64"\n", (conn)->cookie); \
227                 RETURN(-EINVAL);                                        \
228         }                                                               \
229                                                                         \
230         if (!(exp)->exp_obd->obd_set_up || (exp)->exp_obd->obd_stopping) { \
231                 CERROR("Device %d not setup\n",                         \
232                        (exp)->exp_obd->obd_minor);                      \
233                 class_export_put(exp);                                  \
234                 RETURN(-EINVAL);                                        \
235         }                                                               \
236 } while (0)
237
238 /* Ensure obd_setup: used for cleanup which must be called
239    while obd is stopping */
240 #define OBD_CHECK_DEV_STOPPING(obd)                             \
241 do {                                                            \
242         if (!(obd)) {                                           \
243                 CERROR("NULL device\n");                        \
244                 RETURN(-ENODEV);                                \
245         }                                                       \
246                                                                 \
247         if (!(obd)->obd_set_up) {                               \
248                 CERROR("Device %d not setup\n",                 \
249                        (obd)->obd_minor);                       \
250                 RETURN(-ENODEV);                                \
251         }                                                       \
252                                                                 \
253         if (!(obd)->obd_stopping) {                             \
254                 CERROR("Device %d not stopping\n",              \
255                        (obd)->obd_minor);                       \
256                 RETURN(-ENODEV);                                \
257         }                                                       \
258 } while (0)
259
260 /* ensure obd_setup and !obd_stopping */
261 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
262 do {                                                            \
263         if (!(obd)) {                                           \
264                 CERROR("NULL device\n");                        \
265                 RETURN(-ENODEV);                                \
266         }                                                       \
267                                                                 \
268         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
269                 CERROR("Device %d not setup\n",                 \
270                        (obd)->obd_minor);                       \
271                 RETURN(-ENODEV);                                \
272         }                                                       \
273 } while (0)
274
275
276 #ifdef LPROCFS
277 #define OBD_COUNTER_OFFSET(op)                                  \
278         ((offsetof(struct obd_ops, o_ ## op) -                  \
279           offsetof(struct obd_ops, o_iocontrol))                \
280          / sizeof(((struct obd_ops *)(0))->o_iocontrol))
281
282 #define OBD_COUNTER_INCREMENT(obd, op)                          \
283         if ((obd)->obd_stats != NULL) {                         \
284                 unsigned int coffset;                           \
285                 coffset = (unsigned int)(obd)->obd_cntr_base +  \
286                         OBD_COUNTER_OFFSET(op);                 \
287                 LASSERT(coffset < obd->obd_stats->ls_num);      \
288                 lprocfs_counter_incr(obd->obd_stats, coffset);  \
289         }
290 #else
291 #define OBD_COUNTER_OFFSET(op)
292 #define OBD_COUNTER_INCREMENT(obd, op)
293 #endif
294
295 #define OBD_CHECK_OP(obd, op, err)                              \
296 do {                                                            \
297         if (!OBT(obd) || !OBP((obd), op)) {\
298                 if (err)                                        \
299                         CERROR("obd_" #op ": dev %d no operation\n",    \
300                                obd->obd_minor);                         \
301                 RETURN(err);                                    \
302         }                                                       \
303 } while (0)
304
305 #define EXP_CHECK_OP(exp, op)                                   \
306 do {                                                            \
307         if ((exp) == NULL) {                                    \
308                 CERROR("obd_" #op ": NULL export\n");           \
309                 RETURN(-ENODEV);                                \
310         }                                                       \
311         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
312                 CERROR("obd_" #op ": cleaned up obd\n");        \
313                 RETURN(-EOPNOTSUPP);                            \
314         }                                                       \
315         if (!OBT((exp)->exp_obd) || !OBP((exp)->exp_obd, op)) { \
316                 CERROR("obd_" #op ": dev %d no operation\n",    \
317                        (exp)->exp_obd->obd_minor);              \
318                 RETURN(-EOPNOTSUPP);                            \
319         }                                                       \
320 } while (0)
321
322 #define CTXT_CHECK_OP(ctxt, op, err)                                         \
323 do {                                                            \
324         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {                     \
325                 if (err)                                        \
326                         CERROR("lop_" #op ": dev %d no operation\n",    \
327                                ctxt->loc_obd->obd_minor);                         \
328                 RETURN(err);                                    \
329         }                                                       \
330 } while (0)
331
332 static inline int obd_get_info(struct obd_export *exp, __u32 keylen,
333                                void *key, __u32 *vallen, void *val)
334 {
335         int rc;
336         ENTRY;
337
338         EXP_CHECK_OP(exp, get_info);
339         OBD_COUNTER_INCREMENT(exp->exp_obd, get_info);
340
341         rc = OBP(exp->exp_obd, get_info)(exp, keylen, key, vallen, val);
342         RETURN(rc);
343 }
344
345 static inline int obd_set_info(struct obd_export *exp, obd_count keylen,
346                                void *key, obd_count vallen, void *val)
347 {
348         int rc;
349         ENTRY;
350
351         EXP_CHECK_OP(exp, set_info);
352         OBD_COUNTER_INCREMENT(exp->exp_obd, set_info);
353
354         rc = OBP(exp->exp_obd, set_info)(exp, keylen, key, vallen, val);
355         RETURN(rc);
356 }
357
358 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
359 {
360         int rc;
361         ENTRY;
362
363         OBD_CHECK_OP(obd, setup, -EOPNOTSUPP);
364         OBD_COUNTER_INCREMENT(obd, setup);
365
366         rc = OBP(obd, setup)(obd, datalen, data);
367         RETURN(rc);
368 }
369
370 static inline int obd_postsetup(struct obd_device *obd)
371 {
372         int rc;
373         ENTRY;
374
375         OBD_CHECK_DEV_ACTIVE(obd);
376         OBD_CHECK_OP(obd, postsetup, 0);
377         OBD_COUNTER_INCREMENT(obd, postsetup);
378
379         rc = OBP(obd, postsetup)(obd);
380         RETURN(rc);
381 }
382
383 static inline int obd_precleanup(struct obd_device *obd, int flags)
384 {
385         int rc;
386         ENTRY;
387
388         OBD_CHECK_OP(obd, precleanup, 0);
389         OBD_COUNTER_INCREMENT(obd, precleanup);
390
391         rc = OBP(obd, precleanup)(obd, flags);
392         RETURN(rc);
393 }
394
395 static inline int obd_cleanup(struct obd_device *obd, int flags)
396 {
397         int rc;
398         ENTRY;
399
400         OBD_CHECK_DEV_STOPPING(obd);
401         OBD_CHECK_OP(obd, cleanup, 0);
402         OBD_COUNTER_INCREMENT(obd, cleanup);
403
404         rc = OBP(obd, cleanup)(obd, flags);
405         RETURN(rc);
406 }
407
408 /* Pack an in-memory MD struct for storage on disk.
409  * Returns +ve size of packed MD (0 for free), or -ve error.
410  *
411  * If @disk_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
412  * If @*disk_tgt != NULL and @mem_src == NULL, @*disk_tgt will be freed.
413  * If @*disk_tgt == NULL, it will be allocated
414  */
415 static inline int obd_packmd(struct obd_export *exp,
416                              struct lov_mds_md **disk_tgt,
417                              struct lov_stripe_md *mem_src)
418 {
419         int rc;
420         ENTRY;
421
422         EXP_CHECK_OP(exp, packmd);
423         OBD_COUNTER_INCREMENT(exp->exp_obd, packmd);
424
425         rc = OBP(exp->exp_obd, packmd)(exp, disk_tgt, mem_src);
426         RETURN(rc);
427 }
428
429 static inline int obd_size_diskmd(struct obd_export *exp,
430                                   struct lov_stripe_md *mem_src)
431 {
432         return obd_packmd(exp, NULL, mem_src);
433 }
434
435 /* helper functions */
436 static inline int obd_alloc_diskmd(struct obd_export *exp,
437                                    struct lov_mds_md **disk_tgt)
438 {
439         LASSERT(disk_tgt);
440         LASSERT(*disk_tgt == NULL);
441         return obd_packmd(exp, disk_tgt, NULL);
442 }
443
444 static inline int obd_free_diskmd(struct obd_export *exp,
445                                   struct lov_mds_md **disk_tgt)
446 {
447         LASSERT(disk_tgt);
448         LASSERT(*disk_tgt);
449         return obd_packmd(exp, disk_tgt, NULL);
450 }
451
452 /* Unpack an MD struct from disk to in-memory format.
453  * Returns +ve size of unpacked MD (0 for free), or -ve error.
454  *
455  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
456  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
457  * If @*mem_tgt == NULL, it will be allocated
458  */
459 static inline int obd_unpackmd(struct obd_export *exp,
460                                struct lov_stripe_md **mem_tgt,
461                                struct lov_mds_md *disk_src,
462                                int disk_len)
463 {
464         int rc;
465         ENTRY;
466
467         EXP_CHECK_OP(exp, unpackmd);
468         OBD_COUNTER_INCREMENT(exp->exp_obd, unpackmd);
469
470         rc = OBP(exp->exp_obd, unpackmd)(exp, mem_tgt, disk_src, disk_len);
471         RETURN(rc);
472 }
473
474 /* helper functions */
475 static inline int obd_alloc_memmd(struct obd_export *exp,
476                                   struct lov_stripe_md **mem_tgt)
477 {
478         LASSERT(mem_tgt);
479         LASSERT(*mem_tgt == NULL);
480         return obd_unpackmd(exp, mem_tgt, NULL, 0);
481 }
482
483 static inline int obd_free_memmd(struct obd_export *exp,
484                                  struct lov_stripe_md **mem_tgt)
485 {
486         LASSERT(mem_tgt);
487         LASSERT(*mem_tgt);
488         return obd_unpackmd(exp, mem_tgt, NULL, 0);
489 }
490
491 static inline int obd_create(struct obd_export *exp, struct obdo *obdo,
492                              struct lov_stripe_md **ea,
493                              struct obd_trans_info *oti)
494 {
495         int rc;
496         ENTRY;
497
498         EXP_CHECK_OP(exp, create);
499         OBD_COUNTER_INCREMENT(exp->exp_obd, create);
500
501         rc = OBP(exp->exp_obd, create)(exp, obdo, ea, oti);
502         RETURN(rc);
503 }
504
505 static inline int obd_destroy(struct obd_export *exp, struct obdo *obdo,
506                               struct lov_stripe_md *ea,
507                               struct obd_trans_info *oti)
508 {
509         int rc;
510         ENTRY;
511
512         EXP_CHECK_OP(exp, destroy);
513         OBD_COUNTER_INCREMENT(exp->exp_obd, destroy);
514
515         rc = OBP(exp->exp_obd, destroy)(exp, obdo, ea, oti);
516         RETURN(rc);
517 }
518
519 static inline int obd_getattr(struct obd_export *exp, struct obdo *obdo,
520                               struct lov_stripe_md *ea)
521 {
522         int rc;
523         ENTRY;
524
525         EXP_CHECK_OP(exp, getattr);
526         OBD_COUNTER_INCREMENT(exp->exp_obd, getattr);
527
528         rc = OBP(exp->exp_obd, getattr)(exp, obdo, ea);
529         RETURN(rc);
530 }
531
532 static inline int obd_getattr_async(struct obd_export *exp,
533                                     struct obdo *obdo, struct lov_stripe_md *ea,
534                                     struct ptlrpc_request_set *set)
535 {
536         int rc;
537         ENTRY;
538
539         EXP_CHECK_OP(exp, getattr);
540         OBD_COUNTER_INCREMENT(exp->exp_obd, getattr);
541
542         rc = OBP(exp->exp_obd, getattr_async)(exp, obdo, ea, set);
543         RETURN(rc);
544 }
545
546 static inline int obd_setattr(struct obd_export *exp, struct obdo *obdo,
547                               struct lov_stripe_md *ea,
548                               struct obd_trans_info *oti)
549 {
550         int rc;
551         ENTRY;
552
553         EXP_CHECK_OP(exp, setattr);
554         OBD_COUNTER_INCREMENT(exp->exp_obd, setattr);
555
556         rc = OBP(exp->exp_obd, setattr)(exp, obdo, ea, oti);
557         RETURN(rc);
558 }
559
560 static inline int obd_connect(struct lustre_handle *conn,
561                               struct obd_device *obd, struct obd_uuid *cluuid)
562 {
563         int rc;
564         ENTRY;
565
566         OBD_CHECK_DEV_ACTIVE(obd);
567         OBD_CHECK_OP(obd, connect, -EOPNOTSUPP);
568         OBD_COUNTER_INCREMENT(obd, connect);
569
570         rc = OBP(obd, connect)(conn, obd, cluuid);
571         RETURN(rc);
572 }
573
574 static inline int obd_disconnect(struct obd_export *exp, int flags)
575 {
576         int rc;
577         ENTRY;
578
579         EXP_CHECK_OP(exp, disconnect);
580         OBD_COUNTER_INCREMENT(exp->exp_obd, disconnect);
581
582         rc = OBP(exp->exp_obd, disconnect)(exp, flags);
583         RETURN(rc);
584 }
585
586 static inline int obd_init_export(struct obd_export *exp)
587 {
588         int rc = 0;
589
590         ENTRY;
591         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
592             OBP((exp)->exp_obd, init_export))
593                 rc = OBP(exp->exp_obd, init_export)(exp);
594         RETURN(rc);
595 }
596
597 static inline int obd_destroy_export(struct obd_export *exp)
598 {
599         ENTRY;
600         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
601             OBP((exp)->exp_obd, destroy_export))
602                 OBP(exp->exp_obd, destroy_export)(exp);
603         RETURN(0);
604 }
605
606 static inline struct dentry *
607 obd_lvfs_fid2dentry(struct obd_export *exp, __u64 id_ino, __u32 gen, __u64 gr)
608 {
609         LASSERT(exp->exp_obd);
610
611         return lvfs_fid2dentry(&exp->exp_obd->obd_ctxt, id_ino, gen, gr,
612                                exp->exp_obd);
613 }
614
615 #ifndef time_before
616 #define time_before(t1, t2) ((long)t2 - (long)t1 > 0)
617 #endif
618
619 static inline int obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
620                              unsigned long max_age)
621 {
622         int rc = 0;
623         ENTRY;
624
625         if (obd == NULL)
626                 RETURN(-EINVAL);
627
628         OBD_CHECK_OP(obd, statfs, -EOPNOTSUPP);
629         OBD_COUNTER_INCREMENT(obd, statfs);
630
631         CDEBUG(D_SUPER, "osfs %lu, max_age %lu\n", obd->obd_osfs_age, max_age);
632         if (time_before(obd->obd_osfs_age, max_age)) {
633                 rc = OBP(obd, statfs)(obd, osfs, max_age);
634                 spin_lock(&obd->obd_osfs_lock);
635                 memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
636                 obd->obd_osfs_age = jiffies;
637                 spin_unlock(&obd->obd_osfs_lock);
638         } else {
639                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
640                 spin_lock(&obd->obd_osfs_lock);
641                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
642                 spin_unlock(&obd->obd_osfs_lock);
643         }
644         RETURN(rc);
645 }
646
647 static inline int obd_sync(struct obd_export *exp, struct obdo *oa,
648                            struct lov_stripe_md *ea, obd_size start, 
649                            obd_size end)
650 {
651         int rc;
652         ENTRY;
653
654         OBD_CHECK_OP(exp->exp_obd, sync, -EOPNOTSUPP);
655         OBD_COUNTER_INCREMENT(exp->exp_obd, sync);
656
657         rc = OBP(exp->exp_obd, sync)(exp, oa, ea, start, end);
658         RETURN(rc);
659 }
660
661 static inline int obd_punch(struct obd_export *exp, struct obdo *oa,
662                             struct lov_stripe_md *ea, obd_size start,
663                             obd_size end, struct obd_trans_info *oti)
664 {
665         int rc;
666         ENTRY;
667
668         EXP_CHECK_OP(exp, punch);
669         OBD_COUNTER_INCREMENT(exp->exp_obd, punch);
670
671         rc = OBP(exp->exp_obd, punch)(exp, oa, ea, start, end, oti);
672         RETURN(rc);
673 }
674
675 static inline int obd_brw(int cmd, struct obd_export *exp, struct obdo *oa,
676                           struct lov_stripe_md *ea, obd_count oa_bufs,
677                           struct brw_page *pg, struct obd_trans_info *oti)
678 {
679         int rc;
680         ENTRY;
681
682         EXP_CHECK_OP(exp, brw);
683         OBD_COUNTER_INCREMENT(exp->exp_obd, brw);
684
685         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
686                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
687                        "or OBD_BRW_CHECK\n");
688                 LBUG();
689         }
690
691         rc = OBP(exp->exp_obd, brw)(cmd, exp, oa, ea, oa_bufs, pg, oti);
692         RETURN(rc);
693 }
694
695 static inline int obd_brw_async(int cmd, struct obd_export *exp,
696                                 struct obdo *oa, struct lov_stripe_md *ea,
697                                 obd_count oa_bufs, struct brw_page *pg,
698                                 struct ptlrpc_request_set *set,
699                                 struct obd_trans_info *oti)
700 {
701         int rc;
702         ENTRY;
703
704         EXP_CHECK_OP(exp, brw_async);
705         OBD_COUNTER_INCREMENT(exp->exp_obd, brw_async);
706
707         if (!(cmd & OBD_BRW_RWMASK)) {
708                 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
709                 LBUG();
710         }
711
712         rc = OBP(exp->exp_obd, brw_async)(cmd, exp, oa, ea, oa_bufs, pg, set,
713                                           oti);
714         RETURN(rc);
715 }
716
717 static inline  int obd_prep_async_page(struct obd_export *exp, 
718                                        struct lov_stripe_md *lsm,
719                                        struct lov_oinfo *loi, 
720                                        struct page *page, obd_off offset, 
721                                        struct obd_async_page_ops *ops, 
722                                        void *data, void **res)
723 {
724         int ret;
725         ENTRY;
726
727         OBD_CHECK_OP(exp->exp_obd, prep_async_page, -EOPNOTSUPP);
728         OBD_COUNTER_INCREMENT(exp->exp_obd, prep_async_page);
729
730         ret = OBP(exp->exp_obd, prep_async_page)(exp, lsm, loi, page, offset,
731                                                  ops, data, res);
732         RETURN(ret);
733 }
734
735 static inline int obd_queue_async_io(struct obd_export *exp, 
736                                      struct lov_stripe_md *lsm, 
737                                      struct lov_oinfo *loi, void *cookie, 
738                                      int cmd, obd_off off, int count, 
739                                      obd_flag brw_flags, obd_flag async_flags)
740 {
741         int rc;
742         ENTRY;
743
744         OBD_CHECK_OP(exp->exp_obd, queue_async_io, -EOPNOTSUPP);
745         OBD_COUNTER_INCREMENT(exp->exp_obd, queue_async_io);
746         LASSERT(cmd & OBD_BRW_RWMASK);
747
748         rc = OBP(exp->exp_obd, queue_async_io)(exp, lsm, loi, cookie, cmd, off,
749                                                count, brw_flags, async_flags);
750         RETURN(rc);
751 }
752
753 static inline int obd_set_async_flags(struct obd_export *exp,
754                                       struct lov_stripe_md *lsm,
755                                       struct lov_oinfo *loi, void *cookie,
756                                       obd_flag async_flags)
757 {
758         int rc;
759         ENTRY;
760
761         OBD_CHECK_OP(exp->exp_obd, set_async_flags, -EOPNOTSUPP);
762         OBD_COUNTER_INCREMENT(exp->exp_obd, set_async_flags);
763
764         rc = OBP(exp->exp_obd, set_async_flags)(exp, lsm, loi, cookie, 
765                                                 async_flags);
766         RETURN(rc);
767 }
768
769 static inline int obd_queue_sync_io(struct obd_export *exp, 
770                                     struct lov_stripe_md *lsm, 
771                                     struct lov_oinfo *loi, 
772                                     struct obd_sync_io_container *osic, 
773                                     void *cookie, int cmd, obd_off off, 
774                                     int count, obd_flag brw_flags)
775 {
776         int rc;
777         ENTRY;
778
779         OBD_CHECK_OP(exp->exp_obd, queue_sync_io, -EOPNOTSUPP);
780         OBD_COUNTER_INCREMENT(exp->exp_obd, queue_sync_io);
781         LASSERT(cmd & OBD_BRW_RWMASK);
782
783         rc = OBP(exp->exp_obd, queue_sync_io)(exp, lsm, loi, osic, cookie, 
784                                               cmd, off, count, brw_flags);
785         RETURN(rc);
786 }
787
788 static inline int obd_trigger_sync_io(struct obd_export *exp, 
789                                       struct lov_stripe_md *lsm, 
790                                       struct lov_oinfo *loi,
791                                       struct obd_sync_io_container *osic)
792 {
793         int rc;
794         ENTRY;
795
796         OBD_CHECK_OP(exp->exp_obd, trigger_sync_io, -EOPNOTSUPP);
797         OBD_COUNTER_INCREMENT(exp->exp_obd, trigger_sync_io);
798
799         rc = OBP(exp->exp_obd, trigger_sync_io)(exp, lsm, loi, osic);
800         RETURN(rc);
801 }
802
803 static inline int obd_teardown_async_page(struct obd_export *exp,
804                                           struct lov_stripe_md *lsm,
805                                           struct lov_oinfo *loi, void *cookie)
806 {
807         int rc;
808         ENTRY;
809
810         OBD_CHECK_OP(exp->exp_obd, teardown_async_page, -EOPNOTSUPP);
811         OBD_COUNTER_INCREMENT(exp->exp_obd, teardown_async_page);
812
813         rc = OBP(exp->exp_obd, teardown_async_page)(exp, lsm, loi, cookie);
814         RETURN(rc);
815 }
816
817 static inline int obd_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
818                              int objcount, struct obd_ioobj *obj,
819                              int niocount, struct niobuf_remote *remote,
820                              struct niobuf_local *local,
821                              struct obd_trans_info *oti)
822 {
823         int rc;
824         ENTRY;
825
826         OBD_CHECK_OP(exp->exp_obd, preprw, -EOPNOTSUPP);
827         OBD_COUNTER_INCREMENT(exp->exp_obd, preprw);
828
829         rc = OBP(exp->exp_obd, preprw)(cmd, exp, oa, objcount, obj, niocount,
830                                        remote, local, oti);
831         RETURN(rc);
832 }
833
834 static inline int obd_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
835                                int objcount, struct obd_ioobj *obj,
836                                int niocount, struct niobuf_local *local,
837                                struct obd_trans_info *oti)
838 {
839         int rc;
840         ENTRY;
841
842         OBD_CHECK_OP(exp->exp_obd, commitrw, -EOPNOTSUPP);
843         OBD_COUNTER_INCREMENT(exp->exp_obd, commitrw);
844
845         rc = OBP(exp->exp_obd, commitrw)(cmd, exp, oa, objcount, obj, niocount,
846                                          local, oti);
847         RETURN(rc);
848 }
849
850 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
851                                 int len, void *karg, void *uarg)
852 {
853         int rc;
854         ENTRY;
855
856         EXP_CHECK_OP(exp, iocontrol);
857         OBD_COUNTER_INCREMENT(exp->exp_obd, iocontrol);
858
859         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
860         RETURN(rc);
861 }
862
863 static inline int obd_enqueue(struct obd_export *exp,
864                               struct lov_stripe_md *ea,
865                               struct lustre_handle *parent_lock,
866                               __u32 type, void *cookie, int cookielen,
867                               __u32 mode, int *flags, void *cb, void *data,
868                               struct lustre_handle *lockh)
869 {
870         int rc;
871         ENTRY;
872
873         EXP_CHECK_OP(exp, enqueue);
874         OBD_COUNTER_INCREMENT(exp->exp_obd, enqueue);
875
876         rc = OBP(exp->exp_obd, enqueue)(exp, ea, parent_lock, type,
877                                         cookie, cookielen, mode, flags, cb,
878                                         data, lockh);
879         RETURN(rc);
880 }
881
882 static inline int obd_match(struct obd_export *exp,
883                             struct lov_stripe_md *ea, __u32 type, void *cookie,
884                             int cookielen, __u32 mode, int *flags, void *data,
885                             struct lustre_handle *lockh)
886 {
887         int rc;
888         ENTRY;
889
890         EXP_CHECK_OP(exp, match);
891         OBD_COUNTER_INCREMENT(exp->exp_obd, match);
892
893         rc = OBP(exp->exp_obd, match)(exp, ea, type, cookie, cookielen, mode,
894                                       flags, data, lockh);
895         RETURN(rc);
896 }
897
898
899 static inline int obd_change_cbdata(struct obd_export *exp,
900                                     struct lov_stripe_md *lsm, 
901                                     ldlm_iterator_t it, void *data)
902 {
903         int rc;
904         ENTRY;
905
906         EXP_CHECK_OP(exp, change_cbdata);
907         OBD_COUNTER_INCREMENT(exp->exp_obd, change_cbdata);
908
909         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
910         RETURN(rc);
911 }
912
913 static inline int obd_cancel(struct obd_export *exp,
914                              struct lov_stripe_md *ea, __u32 mode,
915                              struct lustre_handle *lockh)
916 {
917         int rc;
918         ENTRY;
919
920         EXP_CHECK_OP(exp, cancel);
921         OBD_COUNTER_INCREMENT(exp->exp_obd, cancel);
922
923         rc = OBP(exp->exp_obd, cancel)(exp, ea, mode, lockh);
924         RETURN(rc);
925 }
926
927 static inline int obd_cancel_unused(struct obd_export *exp,
928                                     struct lov_stripe_md *ea, int flags,
929                                     void *opaque)
930 {
931         int rc;
932         ENTRY;
933
934         EXP_CHECK_OP(exp, cancel_unused);
935         OBD_COUNTER_INCREMENT(exp->exp_obd, cancel_unused);
936
937         rc = OBP(exp->exp_obd, cancel_unused)(exp, ea, flags, opaque);
938         RETURN(rc);
939 }
940
941
942 static inline int obd_san_preprw(int cmd, struct obd_export *exp,
943                                  struct obdo *oa,
944                                  int objcount, struct obd_ioobj *obj,
945                                  int niocount, struct niobuf_remote *remote)
946 {
947         int rc;
948
949         EXP_CHECK_OP(exp, preprw);
950         OBD_COUNTER_INCREMENT(exp->exp_obd, preprw);
951
952         rc = OBP(exp->exp_obd, san_preprw)(cmd, exp, oa, objcount, obj,
953                                            niocount, remote);
954         class_export_put(exp);
955         return(rc);
956 }
957
958 static inline int obd_pin(struct obd_export *exp, obd_id ino, __u32 gen,
959                           int type, struct obd_client_handle *handle, int flag)
960 {
961         int rc;
962
963         EXP_CHECK_OP(exp, pin);
964         OBD_COUNTER_INCREMENT(exp->exp_obd, pin);
965
966         rc = OBP(exp->exp_obd, pin)(exp, ino, gen, type, handle, flag);
967         return(rc);
968 }
969
970 static inline int obd_unpin(struct obd_export *exp,
971                             struct obd_client_handle *handle, int flag)
972 {
973         int rc;
974
975         EXP_CHECK_OP(exp, unpin);
976         OBD_COUNTER_INCREMENT(exp->exp_obd, unpin);
977
978         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
979         return(rc);
980 }
981
982 static inline int obd_lock_contains(struct obd_export *exp,
983                                     struct lov_stripe_md *lsm, 
984                                     struct ldlm_lock *lock, obd_off offset)
985 {
986         int rc;
987         ENTRY;
988
989         EXP_CHECK_OP(exp, lock_contains);
990         OBD_COUNTER_INCREMENT(exp->exp_obd, lock_contains);
991
992         rc = OBP(exp->exp_obd, lock_contains)(exp, lsm, lock, offset);
993         RETURN(rc);
994 }
995
996 static inline void obd_invalidate_import(struct obd_device *obd,
997                                          struct obd_import *imp)
998 {
999         if (obd->obd_set_up && OBP(obd, invalidate_import)) {
1000                 OBD_COUNTER_INCREMENT(obd, invalidate_import);
1001                 OBP(obd, invalidate_import)(obd, imp);
1002         }
1003 }
1004
1005 static inline int obd_notify(struct obd_device *obd,
1006                              struct obd_device *watched,
1007                              int active)
1008 {
1009         if (!obd->obd_set_up) {
1010                 CERROR("obd %s not set up\n", obd->obd_name);
1011                 return -EINVAL;
1012         }
1013
1014         if (!OBP(obd, notify)) {
1015                 CERROR("obd %s has no notify handler\n", obd->obd_name);
1016                 return -ENOSYS;
1017         }
1018
1019         OBD_COUNTER_INCREMENT(obd, notify);
1020         return OBP(obd, notify)(obd, watched, active);
1021 }
1022
1023 static inline int obd_register_observer(struct obd_device *obd,
1024                                         struct obd_device *observer)
1025 {
1026         ENTRY;
1027         if (obd->obd_observer && observer)
1028                 RETURN(-EALREADY);
1029         obd->obd_observer = observer;
1030         RETURN(0);
1031 }
1032
1033 /* OBD Metadata Support */
1034
1035 extern int obd_init_caches(void);
1036 extern void obd_cleanup_caches(void);
1037
1038 /* async page pushing io helpers */
1039 struct obd_client_page *ocp_alloc(struct page *page);
1040 void ocp_free(struct page *page);
1041
1042 /* support routines */
1043 extern kmem_cache_t *obdo_cachep;
1044 static inline struct obdo *obdo_alloc(void)
1045 {
1046         struct obdo *oa;
1047
1048         oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
1049         if (oa == NULL)
1050                 LBUG();
1051         CDEBUG(D_MALLOC, "kmem_cache_alloced oa at %p\n", oa);
1052         memset(oa, 0, sizeof (*oa));
1053
1054         return oa;
1055 }
1056
1057 static inline void obdo_free(struct obdo *oa)
1058 {
1059         if (!oa)
1060                 return;
1061         CDEBUG(D_MALLOC, "kmem_cache_freed oa at %p\n", oa);
1062         kmem_cache_free(obdo_cachep, oa);
1063 }
1064
1065 #if !defined(__KERNEL__) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1066 #define to_kdev_t(dev) dev
1067 #define kdev_t_to_nr(dev) dev
1068 #endif
1069
1070 /* I'm as embarrassed about this as you are.
1071  *
1072  * <shaver> // XXX do not look into _superhack with remaining eye
1073  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1074 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1075 extern void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp);
1076
1077 struct obd_class_user_state {
1078         struct obd_device     *ocus_current_obd;
1079         struct list_head       ocus_conns;
1080 };
1081
1082 struct obd_class_user_conn {
1083         struct list_head       ocuc_chain;
1084         struct obd_export     *ocuc_exp;
1085 };
1086
1087
1088 /* sysctl.c */
1089 extern void obd_sysctl_init (void);
1090 extern void obd_sysctl_clean (void);
1091
1092 /* uuid.c  */
1093 typedef __u8 class_uuid_t[16];
1094 //int class_uuid_parse(struct obd_uuid in, class_uuid_t out);
1095 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1096
1097 /* lustre_peer.c    */
1098 int lustre_uuid_to_peer(char *uuid, struct lustre_peer *peer);
1099 int class_add_uuid(char *uuid, __u64 nid, __u32 nal);
1100 int class_del_uuid (char *uuid);
1101 void class_init_uuidlist(void);
1102 void class_exit_uuidlist(void);
1103 #endif /* __LINUX_OBD_CLASS_H */