1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 #ifndef __LINUX_CLASS_OBD_H
24 #define __LINUX_CLASS_OBD_H
29 # include <linux/list.h>
32 #include <asm/segment.h>
33 #include <asm/uaccess.h>
34 #include <linux/types.h>
36 #include <linux/time.h>
38 #include <linux/obd_support.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
46 * ======== OBD Device Declarations ===========
48 #define MAX_OBD_DEVICES 128
49 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
51 #define OBD_ATTACHED 0x1
52 #define OBD_SET_UP 0x2
54 extern struct proc_dir_entry *
55 proc_lustre_register_obd_device(struct obd_device *obd);
56 extern void proc_lustre_release_obd_device(struct obd_device *obd);
57 extern void proc_lustre_remove_obd_entry(const char* name,
58 struct obd_device *obd);
61 * ======== OBD Operations Declarations ===========
65 extern struct obd_export *class_conn2export(struct lustre_handle *conn);
66 extern struct obd_device *class_conn2obd(struct lustre_handle *conn);
67 extern int class_rconn2export(struct lustre_handle *conn,
68 struct lustre_handle *rconn);
72 struct lustre_handle exp_rconnh; /* remote connection handle */
73 struct lustre_handle exp_impconnh;
74 struct list_head exp_chain;
75 struct obd_device *exp_obd;
76 struct ptlrpc_connection *exp_connection;
77 void *exp_data; /* device specific data */
84 struct lustre_handle imp_expconnh;
85 struct list_head imp_chain;
86 struct obd_device *imp_obd;
88 void *imp_data; /* device specific data */
91 static inline int obd_check_conn(struct lustre_handle *conn)
93 struct obd_device *obd;
95 CERROR("NULL conn\n");
98 obd = class_conn2obd(conn);
100 CERROR("NULL obd\n");
104 if (!obd->obd_flags & OBD_ATTACHED ) {
105 CERROR("obd %d not attached\n", obd->obd_minor);
109 if (!obd->obd_flags & OBD_SET_UP) {
110 CERROR("obd %d not setup\n", obd->obd_minor);
114 if (!obd->obd_type) {
115 CERROR("obd %d not typed\n", obd->obd_minor);
119 if (!obd->obd_type->typ_ops) {
120 CERROR("obd_check_conn: obd %d no operations\n",
128 #define OBT(dev) (dev)->obd_type
129 #define OBP(dev,op) (dev)->obd_type->typ_ops->o_ ## op
131 #define OBD_CHECK_SETUP(conn, export) \
134 CERROR("NULL connection\n"); \
138 export = class_conn2export(conn); \
140 CERROR("No export\n"); \
144 if (!((export)->exp_obd->obd_flags & OBD_SET_UP)) { \
145 CERROR("Device %d not setup\n", \
146 (export)->exp_obd->obd_minor); \
151 #define OBD_CHECK_DEVSETUP(obd) \
154 CERROR("NULL device\n"); \
158 if ( !((obd)->obd_flags & OBD_SET_UP) ) { \
159 CERROR("Device %d not setup\n", \
165 #define OBD_CHECK_OP(obd,op) \
167 if (!OBP((obd),op)) { \
168 CERROR("obd_" #op ": dev %d no operation\n", \
170 RETURN(-EOPNOTSUPP); \
174 static inline int obd_get_info(struct lustre_handle *conn, obd_count keylen,
175 void *key, obd_count *vallen, void **val)
178 struct obd_export *export;
179 OBD_CHECK_SETUP(conn, export);
180 OBD_CHECK_OP(export->exp_obd,get_info);
182 rc = OBP(export->exp_obd, get_info)(conn, keylen, key, vallen, val);
186 static inline int obd_set_info(struct lustre_handle *conn, obd_count keylen,
187 void *key, obd_count vallen, void *val)
190 struct obd_export *export;
191 OBD_CHECK_SETUP(conn, export);
192 OBD_CHECK_OP(export->exp_obd,set_info);
194 rc = OBP(export->exp_obd, set_info)(conn, keylen, key, vallen, val);
198 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
202 OBD_CHECK_OP(obd,setup);
204 rc = OBP(obd, setup)(obd, datalen, data);
208 static inline int obd_cleanup(struct obd_device *obd)
212 OBD_CHECK_DEVSETUP(obd);
213 OBD_CHECK_OP(obd,cleanup);
215 rc = OBP(obd, cleanup)(obd);
219 static inline int obd_create(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md **ea)
222 struct obd_export *export;
223 OBD_CHECK_SETUP(conn, export);
224 OBD_CHECK_OP(export->exp_obd,create);
226 rc = OBP(export->exp_obd, create)(conn, obdo, ea);
230 static inline int obd_destroy(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *ea)
233 struct obd_export *export;
234 OBD_CHECK_SETUP(conn, export);
235 OBD_CHECK_OP(export->exp_obd,destroy);
237 rc = OBP(export->exp_obd, destroy)(conn, obdo, ea);
241 static inline int obd_getattr(struct lustre_handle *conn, struct obdo *obdo)
244 struct obd_export *export;
245 OBD_CHECK_SETUP(conn, export);
246 OBD_CHECK_OP(export->exp_obd,getattr);
248 rc = OBP(export->exp_obd, getattr)(conn, obdo);
252 static inline int obd_close(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *md)
255 struct obd_export *export;
256 OBD_CHECK_SETUP(conn, export);
257 OBD_CHECK_OP(export->exp_obd,close);
259 rc = OBP(export->exp_obd, close)(conn, obdo, md);
262 static inline int obd_open(struct lustre_handle *conn, struct obdo *obdo,
263 struct lov_stripe_md *md)
266 struct obd_export *export;
267 OBD_CHECK_SETUP(conn, export);
268 OBD_CHECK_OP(export->exp_obd,open);
270 rc = OBP(export->exp_obd, open) (conn, obdo, md);
274 static inline int obd_setattr(struct lustre_handle *conn, struct obdo *obdo)
277 struct obd_export *export;
278 OBD_CHECK_SETUP(conn, export);
279 OBD_CHECK_OP(export->exp_obd,setattr);
281 rc = OBP(export->exp_obd, setattr)(conn, obdo);
285 static inline int obd_connect(struct lustre_handle *conn, struct obd_device *obd)
288 OBD_CHECK_DEVSETUP(obd);
289 OBD_CHECK_OP(obd,connect);
291 rc = OBP(obd, connect)(conn, obd);
295 static inline int obd_disconnect(struct lustre_handle *conn)
298 struct obd_export *export;
299 OBD_CHECK_SETUP(conn, export);
300 OBD_CHECK_OP(export->exp_obd,disconnect);
302 rc = OBP(export->exp_obd, disconnect)(conn);
306 static inline int obd_statfs(struct lustre_handle *conn, struct statfs *buf)
309 struct obd_export *export;
310 OBD_CHECK_SETUP(conn, export);
311 OBD_CHECK_OP(export->exp_obd,statfs);
313 rc = OBP(export->exp_obd, statfs)(conn, buf);
317 static inline int obd_punch(struct lustre_handle *conn, struct obdo *tgt,
318 struct lov_stripe_md *md,
319 obd_size count, obd_off offset)
322 struct obd_export *export;
323 OBD_CHECK_SETUP(conn, export);
324 OBD_CHECK_OP(export->exp_obd,punch);
326 rc = OBP(export->exp_obd, punch)(conn, tgt, md, count, offset);
330 static inline int obd_brw(int cmd, struct lustre_handle *conn,
331 struct lov_stripe_md *md,
337 brw_callback_t callback, void *data)
340 struct obd_export *export;
341 OBD_CHECK_SETUP(conn, export);
342 OBD_CHECK_OP(export->exp_obd,brw);
344 if (!(cmd & OBD_BRW_RWMASK)) {
345 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
349 rc = OBP(export->exp_obd, brw)(cmd, conn, md, oa_bufs, buf,
350 count, offset, flags, callback, data);
354 static inline int obd_preprw(int cmd, struct lustre_handle *conn,
355 int objcount, struct obd_ioobj *obj,
356 int niocount, struct niobuf_remote *remote,
357 struct niobuf_local *local, void **desc_private)
360 struct obd_export *export;
361 OBD_CHECK_SETUP(conn, export);
362 OBD_CHECK_OP(export->exp_obd,preprw);
364 rc = OBP(export->exp_obd, preprw)(cmd, conn, objcount, obj, niocount,
365 remote, local, desc_private);
369 static inline int obd_commitrw(int cmd, struct lustre_handle *conn,
370 int objcount, struct obd_ioobj *obj,
371 int niocount, struct niobuf_local *local,
375 struct obd_export *export;
376 OBD_CHECK_SETUP(conn, export);
377 OBD_CHECK_OP(export->exp_obd,commitrw);
379 rc = OBP(export->exp_obd, commitrw)(cmd, conn, objcount, obj, niocount,
380 local, desc_private);
384 static inline int obd_iocontrol(int cmd, struct lustre_handle *conn,
385 int len, void *karg, void *uarg)
388 struct obd_export *export;
389 OBD_CHECK_SETUP(conn, export);
390 OBD_CHECK_OP(export->exp_obd,iocontrol);
392 rc = OBP(export->exp_obd, iocontrol)(cmd, conn, len, karg, uarg);
396 static inline int obd_enqueue(struct lustre_handle *conn,
397 struct lustre_handle *parent_lock, __u64 *res_id,
398 __u32 type, void *cookie, int cookielen,
399 __u32 mode, int *flags, void *cb, void *data,
400 int datalen, struct lustre_handle *lockh)
403 struct obd_export *export;
404 OBD_CHECK_SETUP(conn, export);
405 OBD_CHECK_OP(export->exp_obd,enqueue);
407 rc = OBP(export->exp_obd, enqueue)(conn, parent_lock, res_id, type,
408 cookie, cookielen, mode, flags, cb,
409 data, datalen, lockh);
413 static inline int obd_cancel(struct lustre_handle *conn, __u32 mode,
414 struct lustre_handle *lockh)
417 struct obd_export *export;
418 OBD_CHECK_SETUP(conn, export);
419 OBD_CHECK_OP(export->exp_obd,cancel);
421 rc = OBP(export->exp_obd, cancel)(conn, mode, lockh);
428 * ======== OBD Metadata Support ===========
431 extern int obd_init_caches(void);
432 extern void obd_cleanup_caches(void);
434 static inline int obdo_has_inline(struct obdo *obdo)
436 return (obdo->o_valid & OBD_MD_FLINLINE &&
437 obdo->o_obdflags & OBD_FL_INLINEDATA);
441 /* support routines */
442 extern kmem_cache_t *obdo_cachep;
443 static inline struct obdo *obdo_alloc(void)
445 struct obdo *oa = NULL;
447 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
450 memset(oa, 0, sizeof (*oa));
454 static inline void obdo_free(struct obdo *oa)
458 kmem_cache_free(obdo_cachep, oa);
462 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
464 unsigned int ia_valid = attr->ia_valid;
466 if (ia_valid & ATTR_ATIME) {
467 oa->o_atime = attr->ia_atime;
468 oa->o_valid |= OBD_MD_FLATIME;
470 if (ia_valid & ATTR_MTIME) {
471 oa->o_mtime = attr->ia_mtime;
472 oa->o_valid |= OBD_MD_FLMTIME;
474 if (ia_valid & ATTR_CTIME) {
475 oa->o_ctime = attr->ia_ctime;
476 oa->o_valid |= OBD_MD_FLCTIME;
478 if (ia_valid & ATTR_SIZE) {
479 oa->o_size = attr->ia_size;
480 oa->o_valid |= OBD_MD_FLSIZE;
482 if (ia_valid & ATTR_MODE) {
483 oa->o_mode = attr->ia_mode;
484 oa->o_valid |= OBD_MD_FLMODE;
485 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
486 oa->o_mode &= ~S_ISGID;
488 if (ia_valid & ATTR_UID)
490 oa->o_uid = attr->ia_uid;
491 oa->o_valid |= OBD_MD_FLUID;
493 if (ia_valid & ATTR_GID) {
494 oa->o_gid = attr->ia_gid;
495 oa->o_valid |= OBD_MD_FLGID;
500 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
502 unsigned int ia_valid = oa->o_valid;
504 memset(attr, 0, sizeof(*attr));
505 if (ia_valid & OBD_MD_FLATIME) {
506 attr->ia_atime = oa->o_atime;
507 attr->ia_valid |= ATTR_ATIME;
509 if (ia_valid & OBD_MD_FLMTIME) {
510 attr->ia_mtime = oa->o_mtime;
511 attr->ia_valid |= ATTR_MTIME;
513 if (ia_valid & OBD_MD_FLCTIME) {
514 attr->ia_ctime = oa->o_ctime;
515 attr->ia_valid |= ATTR_CTIME;
517 if (ia_valid & OBD_MD_FLSIZE) {
518 attr->ia_size = oa->o_size;
519 attr->ia_valid |= ATTR_SIZE;
521 if (ia_valid & OBD_MD_FLMODE) {
522 attr->ia_mode = oa->o_mode;
523 attr->ia_valid |= ATTR_MODE;
524 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
525 attr->ia_mode &= ~S_ISGID;
527 if (ia_valid & OBD_MD_FLUID)
529 attr->ia_uid = oa->o_uid;
530 attr->ia_valid |= ATTR_UID;
532 if (ia_valid & OBD_MD_FLGID) {
533 attr->ia_gid = oa->o_gid;
534 attr->ia_valid |= ATTR_GID;
539 /* WARNING: the file systems must take care not to tinker with
540 attributes they don't manage (such as blocks). */
542 static inline void obdo_from_inode(struct obdo *dst, struct inode *src)
544 if ( dst->o_valid & OBD_MD_FLID )
545 dst->o_id = src->i_ino;
546 if ( dst->o_valid & OBD_MD_FLATIME )
547 dst->o_atime = src->i_atime;
548 if ( dst->o_valid & OBD_MD_FLMTIME )
549 dst->o_mtime = src->i_mtime;
550 if ( dst->o_valid & OBD_MD_FLCTIME )
551 dst->o_ctime = src->i_ctime;
552 if ( dst->o_valid & OBD_MD_FLSIZE )
553 dst->o_size = src->i_size;
554 if ( dst->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
555 dst->o_blocks = src->i_blocks;
556 if ( dst->o_valid & OBD_MD_FLBLKSZ )
557 dst->o_blksize = src->i_blksize;
558 if ( dst->o_valid & OBD_MD_FLMODE )
559 dst->o_mode = src->i_mode;
560 if ( dst->o_valid & OBD_MD_FLUID )
561 dst->o_uid = src->i_uid;
562 if ( dst->o_valid & OBD_MD_FLGID )
563 dst->o_gid = src->i_gid;
564 if ( dst->o_valid & OBD_MD_FLFLAGS )
565 dst->o_flags = src->i_flags;
566 if ( dst->o_valid & OBD_MD_FLNLINK )
567 dst->o_nlink = src->i_nlink;
568 if ( dst->o_valid & OBD_MD_FLGENER )
569 dst->o_generation = src->i_generation;
570 if ( dst->o_valid & OBD_MD_FLRDEV )
571 dst->o_rdev = src->i_rdev;
574 static inline void obdo_to_inode(struct inode *dst, struct obdo *src)
577 if ( src->o_valid & OBD_MD_FLID )
578 dst->i_ino = src->o_id;
579 if ( src->o_valid & OBD_MD_FLATIME )
580 dst->i_atime = src->o_atime;
581 if ( src->o_valid & OBD_MD_FLMTIME )
582 dst->i_mtime = src->o_mtime;
583 if ( src->o_valid & OBD_MD_FLCTIME )
584 dst->i_ctime = src->o_ctime;
585 if ( src->o_valid & OBD_MD_FLSIZE )
586 dst->i_size = src->o_size;
587 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
588 dst->i_blocks = src->o_blocks;
589 if ( src->o_valid & OBD_MD_FLBLKSZ )
590 dst->i_blksize = src->o_blksize;
591 if ( src->o_valid & OBD_MD_FLMODE )
592 dst->i_mode = src->o_mode;
593 if ( src->o_valid & OBD_MD_FLUID )
594 dst->i_uid = src->o_uid;
595 if ( src->o_valid & OBD_MD_FLGID )
596 dst->i_gid = src->o_gid;
597 if ( src->o_valid & OBD_MD_FLFLAGS )
598 dst->i_flags = src->o_flags;
599 if ( src->o_valid & OBD_MD_FLNLINK )
600 dst->i_nlink = src->o_nlink;
601 if ( src->o_valid & OBD_MD_FLGENER )
602 dst->i_generation = src->o_generation;
603 if ( src->o_valid & OBD_MD_FLRDEV )
604 dst->i_rdev = src->o_rdev;
609 static inline void obdo_cpy_md(struct obdo *dst, struct obdo *src)
612 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
613 (unsigned long long)src->o_id, src->o_valid,
614 (unsigned long long)dst->o_id);
616 if ( src->o_valid & OBD_MD_FLATIME )
617 dst->o_atime = src->o_atime;
618 if ( src->o_valid & OBD_MD_FLMTIME )
619 dst->o_mtime = src->o_mtime;
620 if ( src->o_valid & OBD_MD_FLCTIME )
621 dst->o_ctime = src->o_ctime;
622 if ( src->o_valid & OBD_MD_FLSIZE )
623 dst->o_size = src->o_size;
624 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
625 dst->o_blocks = src->o_blocks;
626 if ( src->o_valid & OBD_MD_FLBLKSZ )
627 dst->o_blksize = src->o_blksize;
628 if ( src->o_valid & OBD_MD_FLMODE )
629 dst->o_mode = src->o_mode;
630 if ( src->o_valid & OBD_MD_FLUID )
631 dst->o_uid = src->o_uid;
632 if ( src->o_valid & OBD_MD_FLGID )
633 dst->o_gid = src->o_gid;
634 if ( src->o_valid & OBD_MD_FLFLAGS )
635 dst->o_flags = src->o_flags;
637 if ( src->o_valid & OBD_MD_FLOBDFLG )
638 dst->o_obdflags = src->o_obdflags;
640 if ( src->o_valid & OBD_MD_FLNLINK )
641 dst->o_nlink = src->o_nlink;
642 if ( src->o_valid & OBD_MD_FLGENER )
643 dst->o_generation = src->o_generation;
644 if ( src->o_valid & OBD_MD_FLRDEV )
645 dst->o_rdev = src->o_rdev;
646 if ( src->o_valid & OBD_MD_FLINLINE &&
647 src->o_obdflags & OBD_FL_INLINEDATA) {
648 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
649 dst->o_obdflags |= OBD_FL_INLINEDATA;
652 dst->o_valid |= src->o_valid;
656 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
657 static inline int obdo_cmp_md(struct obdo *dst, struct obdo *src,
662 if ( compare & OBD_MD_FLATIME )
663 res = (res || (dst->o_atime != src->o_atime));
664 if ( compare & OBD_MD_FLMTIME )
665 res = (res || (dst->o_mtime != src->o_mtime));
666 if ( compare & OBD_MD_FLCTIME )
667 res = (res || (dst->o_ctime != src->o_ctime));
668 if ( compare & OBD_MD_FLSIZE )
669 res = (res || (dst->o_size != src->o_size));
670 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
671 res = (res || (dst->o_blocks != src->o_blocks));
672 if ( compare & OBD_MD_FLBLKSZ )
673 res = (res || (dst->o_blksize != src->o_blksize));
674 if ( compare & OBD_MD_FLMODE )
675 res = (res || (dst->o_mode != src->o_mode));
676 if ( compare & OBD_MD_FLUID )
677 res = (res || (dst->o_uid != src->o_uid));
678 if ( compare & OBD_MD_FLGID )
679 res = (res || (dst->o_gid != src->o_gid));
680 if ( compare & OBD_MD_FLFLAGS )
681 res = (res || (dst->o_flags != src->o_flags));
682 if ( compare & OBD_MD_FLNLINK )
683 res = (res || (dst->o_nlink != src->o_nlink));
684 if ( compare & OBD_MD_FLGENER )
685 res = (res || (dst->o_generation != src->o_generation));
686 /* XXX Don't know if thses should be included here - wasn't previously
687 if ( compare & OBD_MD_FLINLINE )
688 res = (res || memcmp(dst->o_inline, src->o_inline));
695 int class_register_type(struct obd_ops *ops, char *nm);
696 int class_unregister_type(char *nm);
697 int class_name2dev(char *name);
698 int class_uuid2dev(char *name);
699 struct obd_device *class_uuid2obd(char *name);
700 int class_connect (struct lustre_handle *conn, struct obd_device *obd);
701 int class_disconnect(struct lustre_handle *conn);
702 struct obd_export *class_conn2export(struct lustre_handle *);
704 /* generic operations shared by various OBD types */
705 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
706 int class_multi_cleanup(struct obd_device *obddev);
712 extern void obd_sysctl_init (void);
713 extern void obd_sysctl_clean (void);
715 #endif /* __LINUX_CLASS_OBD_H */