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