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 8
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 rw, 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 rc = OBP(conn->oc_dev, brw)(rw, conn, num_oa, oa, oa_bufs, buf,
305 count, offset, flags, callback);
309 static inline int obd_preprw(int cmd, struct obd_conn *conn,
310 int objcount, struct obd_ioobj *obj,
311 int niocount, struct niobuf_remote *remote,
312 struct niobuf_local *local, void **desc_private)
315 OBD_CHECK_SETUP(conn);
316 OBD_CHECK_OP(conn, preprw);
318 rc = OBP(conn->oc_dev, preprw)(cmd, conn, objcount, obj, niocount,
319 remote, local, desc_private);
323 static inline int obd_commitrw(int cmd, struct obd_conn *conn,
324 int objcount, struct obd_ioobj *obj,
325 int niocount, struct niobuf_local *local,
329 OBD_CHECK_SETUP(conn);
330 OBD_CHECK_OP(conn, commitrw);
332 rc = OBP(conn->oc_dev, commitrw)(cmd, conn, objcount, obj, niocount,
333 local, desc_private);
337 static inline int obd_iocontrol(int cmd, struct obd_conn *conn,
338 int len, void *karg, void *uarg)
341 OBD_CHECK_SETUP(conn);
342 OBD_CHECK_OP(conn, iocontrol);
344 rc = OBP(conn->oc_dev, iocontrol)(cmd, conn, len, karg, uarg);
348 static inline int obd_enqueue(struct obd_conn *conn, struct ldlm_namespace *ns,
349 struct ldlm_handle *parent_lock, __u64 *res_id,
350 __u32 type, struct ldlm_extent *extent,
351 __u32 mode, int *flags, void *data, int datalen,
352 struct ldlm_handle *lockh)
355 OBD_CHECK_SETUP(conn);
356 OBD_CHECK_OP(conn, enqueue);
358 rc = OBP(conn->oc_dev, enqueue)(conn, ns, parent_lock, res_id, type,
359 extent, mode, flags, data, datalen,
364 static inline int obd_cancel(struct obd_conn *conn, __u32 mode,
365 struct ldlm_handle *lockh)
368 OBD_CHECK_SETUP(conn);
369 OBD_CHECK_OP(conn, cancel);
371 rc = OBP(conn->oc_dev, cancel)(conn, mode, lockh);
378 * ======== OBD Metadata Support ===========
381 extern int obd_init_obdo_cache(void);
382 extern void obd_cleanup_obdo_cache(void);
385 static inline int obdo_has_inline(struct obdo *obdo)
387 return (obdo->o_valid & OBD_MD_FLINLINE &&
388 obdo->o_obdflags & OBD_FL_INLINEDATA);
391 static inline int obdo_has_obdmd(struct obdo *obdo)
393 return (obdo->o_valid & OBD_MD_FLOBDMD &&
394 obdo->o_obdflags & OBD_FL_OBDMDEXISTS);
398 /* support routines */
399 extern kmem_cache_t *obdo_cachep;
401 static __inline__ struct obdo *obdo_alloc(void)
403 struct obdo *oa = NULL;
405 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
408 memset(oa, 0, sizeof (*oa));
413 static __inline__ void obdo_free(struct obdo *oa)
417 kmem_cache_free(obdo_cachep, oa);
420 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id,
421 obd_mode mode, obd_flag valid)
429 RETURN(ERR_PTR(-ENOMEM));
435 if ((err = OBP(conn->oc_dev, getattr)(conn, oa))) {
437 RETURN(ERR_PTR(err));
442 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
444 unsigned int ia_valid = attr->ia_valid;
446 if (ia_valid & ATTR_ATIME) {
447 oa->o_atime = attr->ia_atime;
448 oa->o_valid |= OBD_MD_FLATIME;
450 if (ia_valid & ATTR_MTIME) {
451 oa->o_mtime = attr->ia_mtime;
452 oa->o_valid |= OBD_MD_FLMTIME;
454 if (ia_valid & ATTR_CTIME) {
455 oa->o_ctime = attr->ia_ctime;
456 oa->o_valid |= OBD_MD_FLCTIME;
458 if (ia_valid & ATTR_SIZE) {
459 oa->o_size = attr->ia_size;
460 oa->o_valid |= OBD_MD_FLSIZE;
462 if (ia_valid & ATTR_MODE) {
463 oa->o_mode = attr->ia_mode;
464 oa->o_valid |= OBD_MD_FLMODE;
465 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
466 oa->o_mode &= ~S_ISGID;
468 if (ia_valid & ATTR_UID)
470 oa->o_uid = attr->ia_uid;
471 oa->o_valid |= OBD_MD_FLUID;
473 if (ia_valid & ATTR_GID) {
474 oa->o_gid = attr->ia_gid;
475 oa->o_valid |= OBD_MD_FLGID;
480 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
482 unsigned int ia_valid = oa->o_valid;
484 memset(attr, 0, sizeof(*attr));
485 if (ia_valid & OBD_MD_FLATIME) {
486 attr->ia_atime = oa->o_atime;
487 attr->ia_valid |= ATTR_ATIME;
489 if (ia_valid & OBD_MD_FLMTIME) {
490 attr->ia_mtime = oa->o_mtime;
491 attr->ia_valid |= ATTR_MTIME;
493 if (ia_valid & OBD_MD_FLCTIME) {
494 attr->ia_ctime = oa->o_ctime;
495 attr->ia_valid |= ATTR_CTIME;
497 if (ia_valid & OBD_MD_FLSIZE) {
498 attr->ia_size = oa->o_size;
499 attr->ia_valid |= ATTR_SIZE;
501 if (ia_valid & OBD_MD_FLMODE) {
502 attr->ia_mode = oa->o_mode;
503 attr->ia_valid |= ATTR_MODE;
504 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
505 attr->ia_mode &= ~S_ISGID;
507 if (ia_valid & OBD_MD_FLUID)
509 attr->ia_uid = oa->o_uid;
510 attr->ia_valid |= ATTR_UID;
512 if (ia_valid & OBD_MD_FLGID) {
513 attr->ia_gid = oa->o_gid;
514 attr->ia_valid |= ATTR_GID;
519 /* WARNING: the file systems must take care not to tinker with
520 attributes they don't manage (such as blocks). */
522 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
524 if ( dst->o_valid & OBD_MD_FLID )
525 dst->o_id = src->i_ino;
526 if ( dst->o_valid & OBD_MD_FLATIME )
527 dst->o_atime = src->i_atime;
528 if ( dst->o_valid & OBD_MD_FLMTIME )
529 dst->o_mtime = src->i_mtime;
530 if ( dst->o_valid & OBD_MD_FLCTIME )
531 dst->o_ctime = src->i_ctime;
532 if ( dst->o_valid & OBD_MD_FLSIZE )
533 dst->o_size = src->i_size;
534 if ( dst->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
535 dst->o_blocks = src->i_blocks;
536 if ( dst->o_valid & OBD_MD_FLBLKSZ )
537 dst->o_blksize = src->i_blksize;
538 if ( dst->o_valid & OBD_MD_FLMODE )
539 dst->o_mode = src->i_mode;
540 if ( dst->o_valid & OBD_MD_FLUID )
541 dst->o_uid = src->i_uid;
542 if ( dst->o_valid & OBD_MD_FLGID )
543 dst->o_gid = src->i_gid;
544 if ( dst->o_valid & OBD_MD_FLFLAGS )
545 dst->o_flags = src->i_flags;
546 if ( dst->o_valid & OBD_MD_FLNLINK )
547 dst->o_nlink = src->i_nlink;
548 if ( dst->o_valid & OBD_MD_FLGENER )
549 dst->o_generation = src->i_generation;
552 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
555 if ( src->o_valid & OBD_MD_FLID )
556 dst->i_ino = src->o_id;
557 if ( src->o_valid & OBD_MD_FLATIME )
558 dst->i_atime = src->o_atime;
559 if ( src->o_valid & OBD_MD_FLMTIME )
560 dst->i_mtime = src->o_mtime;
561 if ( src->o_valid & OBD_MD_FLCTIME )
562 dst->i_ctime = src->o_ctime;
563 if ( src->o_valid & OBD_MD_FLSIZE )
564 dst->i_size = src->o_size;
565 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
566 dst->i_blocks = src->o_blocks;
567 if ( src->o_valid & OBD_MD_FLBLKSZ )
568 dst->i_blksize = src->o_blksize;
569 if ( src->o_valid & OBD_MD_FLMODE )
570 dst->i_mode = src->o_mode;
571 if ( src->o_valid & OBD_MD_FLUID )
572 dst->i_uid = src->o_uid;
573 if ( src->o_valid & OBD_MD_FLGID )
574 dst->i_gid = src->o_gid;
575 if ( src->o_valid & OBD_MD_FLFLAGS )
576 dst->i_flags = src->o_flags;
577 if ( src->o_valid & OBD_MD_FLNLINK )
578 dst->i_nlink = src->o_nlink;
579 if ( src->o_valid & OBD_MD_FLGENER )
580 dst->i_generation = src->o_generation;
585 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
588 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
589 (unsigned long long)src->o_id, src->o_valid,
590 (unsigned long long)dst->o_id);
592 if ( src->o_valid & OBD_MD_FLATIME )
593 dst->o_atime = src->o_atime;
594 if ( src->o_valid & OBD_MD_FLMTIME )
595 dst->o_mtime = src->o_mtime;
596 if ( src->o_valid & OBD_MD_FLCTIME )
597 dst->o_ctime = src->o_ctime;
598 if ( src->o_valid & OBD_MD_FLSIZE )
599 dst->o_size = src->o_size;
600 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
601 dst->o_blocks = src->o_blocks;
602 if ( src->o_valid & OBD_MD_FLBLKSZ )
603 dst->o_blksize = src->o_blksize;
604 if ( src->o_valid & OBD_MD_FLMODE )
605 dst->o_mode = src->o_mode;
606 if ( src->o_valid & OBD_MD_FLUID )
607 dst->o_uid = src->o_uid;
608 if ( src->o_valid & OBD_MD_FLGID )
609 dst->o_gid = src->o_gid;
610 if ( src->o_valid & OBD_MD_FLFLAGS )
611 dst->o_flags = src->o_flags;
613 if ( src->o_valid & OBD_MD_FLOBDFLG )
614 dst->o_obdflags = src->o_obdflags;
616 if ( src->o_valid & OBD_MD_FLNLINK )
617 dst->o_nlink = src->o_nlink;
618 if ( src->o_valid & OBD_MD_FLGENER )
619 dst->o_generation = src->o_generation;
620 if ( src->o_valid & OBD_MD_FLINLINE &&
621 src->o_obdflags & OBD_FL_INLINEDATA) {
622 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
623 dst->o_obdflags |= OBD_FL_INLINEDATA;
625 if ( src->o_valid & OBD_MD_FLOBDMD &&
626 src->o_obdflags & OBD_FL_OBDMDEXISTS) {
627 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
628 dst->o_obdflags |= OBD_FL_OBDMDEXISTS;
631 dst->o_valid |= src->o_valid;
635 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
636 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
641 if ( compare & OBD_MD_FLATIME )
642 res = (res || (dst->o_atime != src->o_atime));
643 if ( compare & OBD_MD_FLMTIME )
644 res = (res || (dst->o_mtime != src->o_mtime));
645 if ( compare & OBD_MD_FLCTIME )
646 res = (res || (dst->o_ctime != src->o_ctime));
647 if ( compare & OBD_MD_FLSIZE )
648 res = (res || (dst->o_size != src->o_size));
649 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
650 res = (res || (dst->o_blocks != src->o_blocks));
651 if ( compare & OBD_MD_FLBLKSZ )
652 res = (res || (dst->o_blksize != src->o_blksize));
653 if ( compare & OBD_MD_FLMODE )
654 res = (res || (dst->o_mode != src->o_mode));
655 if ( compare & OBD_MD_FLUID )
656 res = (res || (dst->o_uid != src->o_uid));
657 if ( compare & OBD_MD_FLGID )
658 res = (res || (dst->o_gid != src->o_gid));
659 if ( compare & OBD_MD_FLFLAGS )
660 res = (res || (dst->o_flags != src->o_flags));
661 if ( compare & OBD_MD_FLNLINK )
662 res = (res || (dst->o_nlink != src->o_nlink));
663 if ( compare & OBD_MD_FLGENER )
664 res = (res || (dst->o_generation != src->o_generation));
665 /* XXX Don't know if thses should be included here - wasn't previously
666 if ( compare & OBD_MD_FLINLINE )
667 res = (res || memcmp(dst->o_inline, src->o_inline));
668 if ( compare & OBD_MD_FLOBDMD )
669 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
676 int obd_register_type(struct obd_ops *ops, char *nm);
677 int obd_unregister_type(char *nm);
680 struct list_head cli_chain;
681 struct obd_device *cli_obd;
683 unsigned long cli_prealloc_quota;
684 struct list_head cli_prealloc_inodes;
688 struct obd_prealloc_inode {
689 struct list_head obd_prealloc_chain;
693 /* generic operations shared by various OBD types */
694 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
695 int gen_multi_cleanup(struct obd_device *obddev);
696 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
697 int gen_multi_detach(struct obd_device *obddev);
698 int gen_connect (struct obd_conn *conn);
699 int gen_disconnect(struct obd_conn *conn);
700 struct obd_client *gen_client(const struct obd_conn *);
701 int gen_cleanup(struct obd_device *obddev);
702 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
703 struct obd_conn *src_conn, struct obdo *src,
704 obd_size count, obd_off offset);
709 extern void obd_sysctl_init (void);
710 extern void obd_sysctl_clean (void);
712 #endif /* __LINUX_CLASS_OBD_H */