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>
45 * ======== OBD Device Declarations ===========
47 #define MAX_OBD_DEVICES 32
48 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
50 #define OBD_ATTACHED 0x1
51 #define OBD_SET_UP 0x2
53 extern struct proc_dir_entry *
54 proc_lustre_register_obd_device(struct obd_device *obd);
55 extern void proc_lustre_release_obd_device(struct obd_device *obd);
56 extern void proc_lustre_remove_obd_entry(const char* name,
57 struct obd_device *obd);
60 * ======== OBD Operations Declarations ===========
63 #define OBD_BRW_READ 1
64 #define OBD_BRW_WRITE 2
65 #define OBD_BRW_RWMASK (OBD_BRW_READ | OBD_BRW_WRITE)
66 #define OBD_BRW_CREATE 4
71 struct obd_conn *conn;
76 static inline int obd_check_conn(struct obd_conn *conn)
78 struct obd_device *obd;
80 CERROR("obd_check_conn: NULL conn\n");
85 CERROR("obd_check_conn: NULL obd\n");
89 if (!obd->obd_flags & OBD_ATTACHED ) {
90 CERROR("obd_check_conn: obd %d not attached\n", obd->obd_minor);
94 if (!obd->obd_flags & OBD_SET_UP) {
95 CERROR("obd_check_conn: obd %d not setup\n", obd->obd_minor);
100 CERROR("obd_check_conn: obd %d not typed\n", obd->obd_minor);
104 if (!obd->obd_type->typ_ops) {
105 CERROR("obd_check_conn: obd %d no operations\n",
112 #define OBT(dev) (dev)->obd_type
113 #define OBP(dev,op) (dev)->obd_type->typ_ops->o_ ## op
115 #define OBD_CHECK_SETUP(conn) \
118 CERROR("NULL connection\n"); \
122 if (!((conn)->oc_dev)) { \
123 CERROR("NULL device\n"); \
127 if ( !((conn)->oc_dev->obd_flags & OBD_SET_UP) ) { \
128 CERROR("Device %d not setup\n", \
129 (conn)->oc_dev->obd_minor); \
134 #define OBD_CHECK_OP(conn,op) \
136 int rc = obd_check_conn(conn); \
138 CERROR("obd: error in operation: " #op "\n"); \
141 if (!OBP(conn->oc_dev,op)) { \
142 CERROR("obd_" #op ": dev %d no operation\n", \
143 conn->oc_dev->obd_minor); \
144 RETURN(-EOPNOTSUPP); \
148 static inline int obd_get_info(struct obd_conn *conn, obd_count keylen,
149 void *key, obd_count *vallen, void **val)
152 OBD_CHECK_SETUP(conn);
153 OBD_CHECK_OP(conn,get_info);
155 rc = OBP(conn->oc_dev, get_info)(conn, keylen, key, vallen, val);
159 static inline int obd_set_info(struct obd_conn *conn, obd_count keylen,
160 void *key, obd_count vallen, void *val)
163 OBD_CHECK_SETUP(conn);
164 OBD_CHECK_OP(conn,set_info);
166 rc = OBP(conn->oc_dev, set_info)(conn, keylen, key, vallen, val);
170 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
172 struct obd_conn conn;
176 OBD_CHECK_OP((&conn),setup);
178 rc = OBP(conn.oc_dev, setup)(obd, datalen, data);
182 static inline int obd_cleanup(struct obd_device *obd)
184 struct obd_conn conn;
188 OBD_CHECK_SETUP(&conn);
189 OBD_CHECK_OP((&conn),cleanup);
191 rc = OBP(conn.oc_dev, cleanup)(obd);
195 static inline int obd_create(struct obd_conn *conn, struct obdo *obdo)
198 OBD_CHECK_SETUP(conn);
199 OBD_CHECK_OP(conn,create);
201 rc = OBP(conn->oc_dev, create)(conn, obdo);
205 static inline int obd_destroy(struct obd_conn *conn, struct obdo *obdo)
208 OBD_CHECK_SETUP(conn);
209 OBD_CHECK_OP(conn,destroy);
211 rc = OBP(conn->oc_dev, destroy)(conn, obdo);
215 static inline int obd_getattr(struct obd_conn *conn, struct obdo *obdo)
218 OBD_CHECK_SETUP(conn);
219 OBD_CHECK_OP(conn,getattr);
221 rc = OBP(conn->oc_dev, getattr)(conn, obdo);
225 static inline int obd_close(struct obd_conn *conn, struct obdo *obdo)
228 OBD_CHECK_SETUP(conn);
229 OBD_CHECK_OP(conn,close);
231 rc = OBP(conn->oc_dev, close)(conn, obdo);
234 static inline int obd_open(struct obd_conn *conn, struct obdo *obdo)
237 OBD_CHECK_SETUP(conn);
238 OBD_CHECK_OP(conn,open);
240 rc = OBP(conn->oc_dev, open) (conn, obdo);
244 static inline int obd_setattr(struct obd_conn *conn, struct obdo *obdo)
247 OBD_CHECK_SETUP(conn);
248 OBD_CHECK_OP(conn,setattr);
250 rc = OBP(conn->oc_dev, setattr)(conn, obdo);
254 static inline int obd_connect(struct obd_conn *conn)
257 OBD_CHECK_SETUP(conn);
258 OBD_CHECK_OP(conn,connect);
260 rc = OBP(conn->oc_dev, connect)(conn);
264 static inline int obd_disconnect(struct obd_conn *conn)
267 OBD_CHECK_SETUP(conn);
268 OBD_CHECK_OP(conn,disconnect);
270 rc = OBP(conn->oc_dev, disconnect)(conn);
274 static inline int obd_statfs(struct obd_conn *conn, struct statfs *buf)
277 OBD_CHECK_SETUP(conn);
278 OBD_CHECK_OP(conn,statfs);
280 rc = OBP(conn->oc_dev, statfs)(conn, buf);
284 static inline int obd_punch(struct obd_conn *conn, struct obdo *tgt,
285 obd_size count, obd_off offset)
288 OBD_CHECK_SETUP(conn);
289 OBD_CHECK_OP(conn,punch);
291 rc = OBP(conn->oc_dev, punch)(conn, tgt, count, offset);
295 static inline int obd_brw(int cmd, struct obd_conn *conn, obd_count num_oa,
296 struct obdo **oa, obd_count *oa_bufs,
297 struct page **buf, obd_size *count, obd_off *offset,
298 obd_flag *flags, void *callback)
301 OBD_CHECK_SETUP(conn);
302 OBD_CHECK_OP(conn,brw);
304 if (!(cmd & OBD_BRW_RWMASK)) {
305 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
309 rc = OBP(conn->oc_dev, brw)(cmd, conn, num_oa, oa, oa_bufs, buf,
310 count, offset, flags, callback);
314 static inline int obd_preprw(int cmd, struct obd_conn *conn,
315 int objcount, struct obd_ioobj *obj,
316 int niocount, struct niobuf_remote *remote,
317 struct niobuf_local *local, void **desc_private)
320 OBD_CHECK_SETUP(conn);
321 OBD_CHECK_OP(conn, preprw);
323 rc = OBP(conn->oc_dev, preprw)(cmd, conn, objcount, obj, niocount,
324 remote, local, desc_private);
328 static inline int obd_commitrw(int cmd, struct obd_conn *conn,
329 int objcount, struct obd_ioobj *obj,
330 int niocount, struct niobuf_local *local,
334 OBD_CHECK_SETUP(conn);
335 OBD_CHECK_OP(conn, commitrw);
337 rc = OBP(conn->oc_dev, commitrw)(cmd, conn, objcount, obj, niocount,
338 local, desc_private);
342 static inline int obd_iocontrol(int cmd, struct obd_conn *conn,
343 int len, void *karg, void *uarg)
346 OBD_CHECK_SETUP(conn);
347 OBD_CHECK_OP(conn, iocontrol);
349 rc = OBP(conn->oc_dev, iocontrol)(cmd, conn, len, karg, uarg);
353 static inline int obd_enqueue(struct obd_conn *conn,
354 struct lustre_handle *parent_lock, __u64 *res_id,
355 __u32 type, void *cookie, int cookielen,
356 __u32 mode, int *flags, void *cb, void *data,
357 int datalen, struct lustre_handle *lockh)
360 OBD_CHECK_SETUP(conn);
361 OBD_CHECK_OP(conn, enqueue);
363 rc = OBP(conn->oc_dev, enqueue)(conn, parent_lock, res_id, type,
364 cookie, cookielen, mode, flags, cb,
365 data, datalen, lockh);
369 static inline int obd_cancel(struct obd_conn *conn, __u32 mode,
370 struct lustre_handle *lockh)
373 OBD_CHECK_SETUP(conn);
374 OBD_CHECK_OP(conn, cancel);
376 rc = OBP(conn->oc_dev, cancel)(conn, mode, lockh);
383 * ======== OBD Metadata Support ===========
386 extern int obd_init_obdo_cache(void);
387 extern void obd_cleanup_obdo_cache(void);
390 static inline int obdo_has_inline(struct obdo *obdo)
392 return (obdo->o_valid & OBD_MD_FLINLINE &&
393 obdo->o_obdflags & OBD_FL_INLINEDATA);
396 static inline int obdo_has_obdmd(struct obdo *obdo)
398 return (obdo->o_valid & OBD_MD_FLOBDMD &&
399 obdo->o_obdflags & OBD_FL_OBDMDEXISTS);
403 /* support routines */
404 extern kmem_cache_t *obdo_cachep;
406 static __inline__ struct obdo *obdo_alloc(void)
408 struct obdo *oa = NULL;
410 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
413 memset(oa, 0, sizeof (*oa));
418 static __inline__ void obdo_free(struct obdo *oa)
422 kmem_cache_free(obdo_cachep, oa);
425 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id,
426 obd_mode mode, obd_flag valid)
434 RETURN(ERR_PTR(-ENOMEM));
440 if ((err = OBP(conn->oc_dev, getattr)(conn, oa))) {
442 RETURN(ERR_PTR(err));
447 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
449 unsigned int ia_valid = attr->ia_valid;
451 if (ia_valid & ATTR_ATIME) {
452 oa->o_atime = attr->ia_atime;
453 oa->o_valid |= OBD_MD_FLATIME;
455 if (ia_valid & ATTR_MTIME) {
456 oa->o_mtime = attr->ia_mtime;
457 oa->o_valid |= OBD_MD_FLMTIME;
459 if (ia_valid & ATTR_CTIME) {
460 oa->o_ctime = attr->ia_ctime;
461 oa->o_valid |= OBD_MD_FLCTIME;
463 if (ia_valid & ATTR_SIZE) {
464 oa->o_size = attr->ia_size;
465 oa->o_valid |= OBD_MD_FLSIZE;
467 if (ia_valid & ATTR_MODE) {
468 oa->o_mode = attr->ia_mode;
469 oa->o_valid |= OBD_MD_FLMODE;
470 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
471 oa->o_mode &= ~S_ISGID;
473 if (ia_valid & ATTR_UID)
475 oa->o_uid = attr->ia_uid;
476 oa->o_valid |= OBD_MD_FLUID;
478 if (ia_valid & ATTR_GID) {
479 oa->o_gid = attr->ia_gid;
480 oa->o_valid |= OBD_MD_FLGID;
485 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
487 unsigned int ia_valid = oa->o_valid;
489 memset(attr, 0, sizeof(*attr));
490 if (ia_valid & OBD_MD_FLATIME) {
491 attr->ia_atime = oa->o_atime;
492 attr->ia_valid |= ATTR_ATIME;
494 if (ia_valid & OBD_MD_FLMTIME) {
495 attr->ia_mtime = oa->o_mtime;
496 attr->ia_valid |= ATTR_MTIME;
498 if (ia_valid & OBD_MD_FLCTIME) {
499 attr->ia_ctime = oa->o_ctime;
500 attr->ia_valid |= ATTR_CTIME;
502 if (ia_valid & OBD_MD_FLSIZE) {
503 attr->ia_size = oa->o_size;
504 attr->ia_valid |= ATTR_SIZE;
506 if (ia_valid & OBD_MD_FLMODE) {
507 attr->ia_mode = oa->o_mode;
508 attr->ia_valid |= ATTR_MODE;
509 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
510 attr->ia_mode &= ~S_ISGID;
512 if (ia_valid & OBD_MD_FLUID)
514 attr->ia_uid = oa->o_uid;
515 attr->ia_valid |= ATTR_UID;
517 if (ia_valid & OBD_MD_FLGID) {
518 attr->ia_gid = oa->o_gid;
519 attr->ia_valid |= ATTR_GID;
524 /* WARNING: the file systems must take care not to tinker with
525 attributes they don't manage (such as blocks). */
527 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
529 if ( dst->o_valid & OBD_MD_FLID )
530 dst->o_id = src->i_ino;
531 if ( dst->o_valid & OBD_MD_FLATIME )
532 dst->o_atime = src->i_atime;
533 if ( dst->o_valid & OBD_MD_FLMTIME )
534 dst->o_mtime = src->i_mtime;
535 if ( dst->o_valid & OBD_MD_FLCTIME )
536 dst->o_ctime = src->i_ctime;
537 if ( dst->o_valid & OBD_MD_FLSIZE )
538 dst->o_size = src->i_size;
539 if ( dst->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
540 dst->o_blocks = src->i_blocks;
541 if ( dst->o_valid & OBD_MD_FLBLKSZ )
542 dst->o_blksize = src->i_blksize;
543 if ( dst->o_valid & OBD_MD_FLMODE )
544 dst->o_mode = src->i_mode;
545 if ( dst->o_valid & OBD_MD_FLUID )
546 dst->o_uid = src->i_uid;
547 if ( dst->o_valid & OBD_MD_FLGID )
548 dst->o_gid = src->i_gid;
549 if ( dst->o_valid & OBD_MD_FLFLAGS )
550 dst->o_flags = src->i_flags;
551 if ( dst->o_valid & OBD_MD_FLNLINK )
552 dst->o_nlink = src->i_nlink;
553 if ( dst->o_valid & OBD_MD_FLGENER )
554 dst->o_generation = src->i_generation;
557 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
560 if ( src->o_valid & OBD_MD_FLID )
561 dst->i_ino = src->o_id;
562 if ( src->o_valid & OBD_MD_FLATIME )
563 dst->i_atime = src->o_atime;
564 if ( src->o_valid & OBD_MD_FLMTIME )
565 dst->i_mtime = src->o_mtime;
566 if ( src->o_valid & OBD_MD_FLCTIME )
567 dst->i_ctime = src->o_ctime;
568 if ( src->o_valid & OBD_MD_FLSIZE )
569 dst->i_size = src->o_size;
570 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
571 dst->i_blocks = src->o_blocks;
572 if ( src->o_valid & OBD_MD_FLBLKSZ )
573 dst->i_blksize = src->o_blksize;
574 if ( src->o_valid & OBD_MD_FLMODE )
575 dst->i_mode = src->o_mode;
576 if ( src->o_valid & OBD_MD_FLUID )
577 dst->i_uid = src->o_uid;
578 if ( src->o_valid & OBD_MD_FLGID )
579 dst->i_gid = src->o_gid;
580 if ( src->o_valid & OBD_MD_FLFLAGS )
581 dst->i_flags = src->o_flags;
582 if ( src->o_valid & OBD_MD_FLNLINK )
583 dst->i_nlink = src->o_nlink;
584 if ( src->o_valid & OBD_MD_FLGENER )
585 dst->i_generation = src->o_generation;
590 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
593 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
594 (unsigned long long)src->o_id, src->o_valid,
595 (unsigned long long)dst->o_id);
597 if ( src->o_valid & OBD_MD_FLATIME )
598 dst->o_atime = src->o_atime;
599 if ( src->o_valid & OBD_MD_FLMTIME )
600 dst->o_mtime = src->o_mtime;
601 if ( src->o_valid & OBD_MD_FLCTIME )
602 dst->o_ctime = src->o_ctime;
603 if ( src->o_valid & OBD_MD_FLSIZE )
604 dst->o_size = src->o_size;
605 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
606 dst->o_blocks = src->o_blocks;
607 if ( src->o_valid & OBD_MD_FLBLKSZ )
608 dst->o_blksize = src->o_blksize;
609 if ( src->o_valid & OBD_MD_FLMODE )
610 dst->o_mode = src->o_mode;
611 if ( src->o_valid & OBD_MD_FLUID )
612 dst->o_uid = src->o_uid;
613 if ( src->o_valid & OBD_MD_FLGID )
614 dst->o_gid = src->o_gid;
615 if ( src->o_valid & OBD_MD_FLFLAGS )
616 dst->o_flags = src->o_flags;
618 if ( src->o_valid & OBD_MD_FLOBDFLG )
619 dst->o_obdflags = src->o_obdflags;
621 if ( src->o_valid & OBD_MD_FLNLINK )
622 dst->o_nlink = src->o_nlink;
623 if ( src->o_valid & OBD_MD_FLGENER )
624 dst->o_generation = src->o_generation;
625 if ( src->o_valid & OBD_MD_FLINLINE &&
626 src->o_obdflags & OBD_FL_INLINEDATA) {
627 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
628 dst->o_obdflags |= OBD_FL_INLINEDATA;
630 if ( src->o_valid & OBD_MD_FLOBDMD &&
631 src->o_obdflags & OBD_FL_OBDMDEXISTS) {
632 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
633 dst->o_obdflags |= OBD_FL_OBDMDEXISTS;
636 dst->o_valid |= src->o_valid;
640 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
641 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
646 if ( compare & OBD_MD_FLATIME )
647 res = (res || (dst->o_atime != src->o_atime));
648 if ( compare & OBD_MD_FLMTIME )
649 res = (res || (dst->o_mtime != src->o_mtime));
650 if ( compare & OBD_MD_FLCTIME )
651 res = (res || (dst->o_ctime != src->o_ctime));
652 if ( compare & OBD_MD_FLSIZE )
653 res = (res || (dst->o_size != src->o_size));
654 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
655 res = (res || (dst->o_blocks != src->o_blocks));
656 if ( compare & OBD_MD_FLBLKSZ )
657 res = (res || (dst->o_blksize != src->o_blksize));
658 if ( compare & OBD_MD_FLMODE )
659 res = (res || (dst->o_mode != src->o_mode));
660 if ( compare & OBD_MD_FLUID )
661 res = (res || (dst->o_uid != src->o_uid));
662 if ( compare & OBD_MD_FLGID )
663 res = (res || (dst->o_gid != src->o_gid));
664 if ( compare & OBD_MD_FLFLAGS )
665 res = (res || (dst->o_flags != src->o_flags));
666 if ( compare & OBD_MD_FLNLINK )
667 res = (res || (dst->o_nlink != src->o_nlink));
668 if ( compare & OBD_MD_FLGENER )
669 res = (res || (dst->o_generation != src->o_generation));
670 /* XXX Don't know if thses should be included here - wasn't previously
671 if ( compare & OBD_MD_FLINLINE )
672 res = (res || memcmp(dst->o_inline, src->o_inline));
673 if ( compare & OBD_MD_FLOBDMD )
674 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
681 int obd_register_type(struct obd_ops *ops, char *nm);
682 int obd_unregister_type(char *nm);
683 int obd_class_name2dev(char *name);
686 struct list_head cli_chain;
687 struct obd_device *cli_obd;
689 unsigned long cli_prealloc_quota;
690 struct list_head cli_prealloc_inodes;
694 struct obd_prealloc_inode {
695 struct list_head obd_prealloc_chain;
699 /* generic operations shared by various OBD types */
700 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
701 int gen_multi_cleanup(struct obd_device *obddev);
702 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
703 int gen_multi_detach(struct obd_device *obddev);
704 int gen_connect (struct obd_conn *conn);
705 int gen_disconnect(struct obd_conn *conn);
706 struct obd_client *gen_client(const struct obd_conn *);
707 int gen_cleanup(struct obd_device *obddev);
708 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
709 struct obd_conn *src_conn, struct obdo *src,
710 obd_size count, obd_off offset);
715 extern void obd_sysctl_init (void);
716 extern void obd_sysctl_clean (void);
718 #endif /* __LINUX_CLASS_OBD_H */