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 ===========
64 #define OBD_BRW_READ 1
65 #define OBD_BRW_WRITE 2
66 #define OBD_BRW_RWMASK (OBD_BRW_READ | OBD_BRW_WRITE)
67 #define OBD_BRW_CREATE 4
70 extern struct obd_export *class_conn2export(struct lustre_handle *conn);
71 extern struct obd_device *class_conn2obd(struct lustre_handle *conn);
75 struct lustre_handle export_import; /* client handle */
76 struct list_head export_chain;
77 struct obd_device *export_obd;
78 struct ptlrpc_connection *export_connection;
79 unsigned int export_id;
80 void *export_data; /* device specific data */
85 struct lustre_handle import_export; /* client handle */
86 struct list_head import_chain;
87 struct obd_device *import_obd;
88 unsigned int import_id;
89 void *import_data; /* device specific data */
92 static inline int obd_check_conn(struct lustre_handle *conn)
94 struct obd_device *obd;
96 CERROR("NULL conn\n");
99 obd = class_conn2obd(conn);
101 CERROR("NULL obd\n");
105 if (!obd->obd_flags & OBD_ATTACHED ) {
106 CERROR("obd %d not attached\n", obd->obd_minor);
110 if (!obd->obd_flags & OBD_SET_UP) {
111 CERROR("obd %d not setup\n", obd->obd_minor);
115 if (!obd->obd_type) {
116 CERROR("obd %d not typed\n", obd->obd_minor);
120 if (!obd->obd_type->typ_ops) {
121 CERROR("obd_check_conn: obd %d no operations\n",
129 #define OBT(dev) (dev)->obd_type
130 #define OBP(dev,op) (dev)->obd_type->typ_ops->o_ ## op
132 #define OBD_CHECK_SETUP(conn, export) \
135 CERROR("NULL connection\n"); \
139 export = class_conn2export(conn);\
141 CERROR("No export\n"); \
145 if ( !((export)->export_obd->obd_flags & OBD_SET_UP) ) { \
146 CERROR("Device %d not setup\n", \
147 (export)->export_obd->obd_minor); \
152 #define OBD_CHECK_DEVSETUP(obd) \
155 CERROR("NULL device\n"); \
159 if ( !((obd)->obd_flags & OBD_SET_UP) ) { \
160 CERROR("Device %d not setup\n", \
166 #define OBD_CHECK_OP(obd,op) \
168 if (!OBP((obd),op)) { \
169 CERROR("obd_" #op ": dev %d no operation\n", \
171 RETURN(-EOPNOTSUPP); \
175 static inline int obd_get_info(struct lustre_handle *conn, obd_count keylen,
176 void *key, obd_count *vallen, void **val)
179 struct obd_export *export;
180 OBD_CHECK_SETUP(conn, export);
181 OBD_CHECK_OP(export->export_obd,get_info);
183 rc = OBP(export->export_obd, get_info)(conn, keylen, key, vallen, val);
187 static inline int obd_set_info(struct lustre_handle *conn, obd_count keylen,
188 void *key, obd_count vallen, void *val)
191 struct obd_export *export;
192 OBD_CHECK_SETUP(conn, export);
193 OBD_CHECK_OP(export->export_obd,set_info);
195 rc = OBP(export->export_obd, set_info)(conn, keylen, key, vallen, val);
199 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
203 OBD_CHECK_OP(obd,setup);
205 rc = OBP(obd, setup)(obd, datalen, data);
209 static inline int obd_cleanup(struct obd_device *obd)
213 OBD_CHECK_DEVSETUP(obd);
214 OBD_CHECK_OP(obd,cleanup);
216 rc = OBP(obd, cleanup)(obd);
220 static inline int obd_create(struct lustre_handle *conn, struct obdo *obdo)
223 struct obd_export *export;
224 OBD_CHECK_SETUP(conn, export);
225 OBD_CHECK_OP(export->export_obd,create);
227 rc = OBP(export->export_obd, create)(conn, obdo);
231 static inline int obd_destroy(struct lustre_handle *conn, struct obdo *obdo)
234 struct obd_export *export;
235 OBD_CHECK_SETUP(conn, export);
236 OBD_CHECK_OP(export->export_obd,destroy);
238 rc = OBP(export->export_obd, destroy)(conn, obdo);
242 static inline int obd_getattr(struct lustre_handle *conn, struct obdo *obdo)
245 struct obd_export *export;
246 OBD_CHECK_SETUP(conn, export);
247 OBD_CHECK_OP(export->export_obd,getattr);
249 rc = OBP(export->export_obd, getattr)(conn, obdo);
253 static inline int obd_close(struct lustre_handle *conn, struct obdo *obdo)
256 struct obd_export *export;
257 OBD_CHECK_SETUP(conn, export);
258 OBD_CHECK_OP(export->export_obd,close);
260 rc = OBP(export->export_obd, close)(conn, obdo);
263 static inline int obd_open(struct lustre_handle *conn, struct obdo *obdo)
266 struct obd_export *export;
267 OBD_CHECK_SETUP(conn, export);
268 OBD_CHECK_OP(export->export_obd,open);
270 rc = OBP(export->export_obd, open) (conn, obdo);
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->export_obd,setattr);
281 rc = OBP(export->export_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->export_obd,disconnect);
302 rc = OBP(export->export_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->export_obd,statfs);
313 rc = OBP(export->export_obd, statfs)(conn, buf);
317 static inline int obd_punch(struct lustre_handle *conn, struct obdo *tgt,
318 obd_size count, obd_off offset)
321 struct obd_export *export;
322 OBD_CHECK_SETUP(conn, export);
323 OBD_CHECK_OP(export->export_obd,punch);
325 rc = OBP(export->export_obd, punch)(conn, tgt, count, offset);
329 static inline int obd_brw(int cmd, struct lustre_handle *conn, obd_count num_oa,
330 struct obdo **oa, obd_count *oa_bufs,
331 struct page **buf, obd_size *count, obd_off *offset,
332 obd_flag *flags, void *callback)
335 struct obd_export *export;
336 OBD_CHECK_SETUP(conn, export);
337 OBD_CHECK_OP(export->export_obd,brw);
339 if (!(cmd & OBD_BRW_RWMASK)) {
340 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
344 rc = OBP(export->export_obd, brw)(cmd, conn, num_oa, oa, oa_bufs, buf,
345 count, offset, flags, callback);
349 static inline int obd_preprw(int cmd, struct lustre_handle *conn,
350 int objcount, struct obd_ioobj *obj,
351 int niocount, struct niobuf_remote *remote,
352 struct niobuf_local *local, void **desc_private)
355 struct obd_export *export;
356 OBD_CHECK_SETUP(conn, export);
357 OBD_CHECK_OP(export->export_obd,preprw);
359 rc = OBP(export->export_obd, preprw)(cmd, conn, objcount, obj, niocount,
360 remote, local, desc_private);
364 static inline int obd_commitrw(int cmd, struct lustre_handle *conn,
365 int objcount, struct obd_ioobj *obj,
366 int niocount, struct niobuf_local *local,
370 struct obd_export *export;
371 OBD_CHECK_SETUP(conn, export);
372 OBD_CHECK_OP(export->export_obd,commitrw);
374 rc = OBP(export->export_obd, commitrw)(cmd, conn, objcount, obj, niocount,
375 local, desc_private);
379 static inline int obd_iocontrol(int cmd, struct lustre_handle *conn,
380 int len, void *karg, void *uarg)
383 struct obd_export *export;
384 OBD_CHECK_SETUP(conn, export);
385 OBD_CHECK_OP(export->export_obd,iocontrol);
387 rc = OBP(export->export_obd, iocontrol)(cmd, conn, len, karg, uarg);
391 static inline int obd_enqueue(struct lustre_handle *conn,
392 struct lustre_handle *parent_lock, __u64 *res_id,
393 __u32 type, void *cookie, int cookielen,
394 __u32 mode, int *flags, void *cb, void *data,
395 int datalen, struct lustre_handle *lockh)
398 struct obd_export *export;
399 OBD_CHECK_SETUP(conn, export);
400 OBD_CHECK_OP(export->export_obd,enqueue);
402 rc = OBP(export->export_obd, enqueue)(conn, parent_lock, res_id, type,
403 cookie, cookielen, mode, flags, cb,
404 data, datalen, lockh);
408 static inline int obd_cancel(struct lustre_handle *conn, __u32 mode,
409 struct lustre_handle *lockh)
412 struct obd_export *export;
413 OBD_CHECK_SETUP(conn, export);
414 OBD_CHECK_OP(export->export_obd,cancel);
416 rc = OBP(export->export_obd, cancel)(conn, mode, lockh);
423 * ======== OBD Metadata Support ===========
426 extern int obd_init_caches(void);
427 extern void obd_cleanup_caches(void);
429 static inline int obdo_has_inline(struct obdo *obdo)
431 return (obdo->o_valid & OBD_MD_FLINLINE &&
432 obdo->o_obdflags & OBD_FL_INLINEDATA);
436 /* support routines */
437 extern kmem_cache_t *obdo_cachep;
438 static __inline__ struct obdo *obdo_alloc(void)
440 struct obdo *oa = NULL;
442 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
445 memset(oa, 0, sizeof (*oa));
449 static __inline__ void obdo_free(struct obdo *oa)
453 kmem_cache_free(obdo_cachep, oa);
457 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
459 unsigned int ia_valid = attr->ia_valid;
461 if (ia_valid & ATTR_ATIME) {
462 oa->o_atime = attr->ia_atime;
463 oa->o_valid |= OBD_MD_FLATIME;
465 if (ia_valid & ATTR_MTIME) {
466 oa->o_mtime = attr->ia_mtime;
467 oa->o_valid |= OBD_MD_FLMTIME;
469 if (ia_valid & ATTR_CTIME) {
470 oa->o_ctime = attr->ia_ctime;
471 oa->o_valid |= OBD_MD_FLCTIME;
473 if (ia_valid & ATTR_SIZE) {
474 oa->o_size = attr->ia_size;
475 oa->o_valid |= OBD_MD_FLSIZE;
477 if (ia_valid & ATTR_MODE) {
478 oa->o_mode = attr->ia_mode;
479 oa->o_valid |= OBD_MD_FLMODE;
480 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
481 oa->o_mode &= ~S_ISGID;
483 if (ia_valid & ATTR_UID)
485 oa->o_uid = attr->ia_uid;
486 oa->o_valid |= OBD_MD_FLUID;
488 if (ia_valid & ATTR_GID) {
489 oa->o_gid = attr->ia_gid;
490 oa->o_valid |= OBD_MD_FLGID;
495 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
497 unsigned int ia_valid = oa->o_valid;
499 memset(attr, 0, sizeof(*attr));
500 if (ia_valid & OBD_MD_FLATIME) {
501 attr->ia_atime = oa->o_atime;
502 attr->ia_valid |= ATTR_ATIME;
504 if (ia_valid & OBD_MD_FLMTIME) {
505 attr->ia_mtime = oa->o_mtime;
506 attr->ia_valid |= ATTR_MTIME;
508 if (ia_valid & OBD_MD_FLCTIME) {
509 attr->ia_ctime = oa->o_ctime;
510 attr->ia_valid |= ATTR_CTIME;
512 if (ia_valid & OBD_MD_FLSIZE) {
513 attr->ia_size = oa->o_size;
514 attr->ia_valid |= ATTR_SIZE;
516 if (ia_valid & OBD_MD_FLMODE) {
517 attr->ia_mode = oa->o_mode;
518 attr->ia_valid |= ATTR_MODE;
519 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
520 attr->ia_mode &= ~S_ISGID;
522 if (ia_valid & OBD_MD_FLUID)
524 attr->ia_uid = oa->o_uid;
525 attr->ia_valid |= ATTR_UID;
527 if (ia_valid & OBD_MD_FLGID) {
528 attr->ia_gid = oa->o_gid;
529 attr->ia_valid |= ATTR_GID;
534 /* WARNING: the file systems must take care not to tinker with
535 attributes they don't manage (such as blocks). */
537 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
539 if ( dst->o_valid & OBD_MD_FLID )
540 dst->o_id = src->i_ino;
541 if ( dst->o_valid & OBD_MD_FLATIME )
542 dst->o_atime = src->i_atime;
543 if ( dst->o_valid & OBD_MD_FLMTIME )
544 dst->o_mtime = src->i_mtime;
545 if ( dst->o_valid & OBD_MD_FLCTIME )
546 dst->o_ctime = src->i_ctime;
547 if ( dst->o_valid & OBD_MD_FLSIZE )
548 dst->o_size = src->i_size;
549 if ( dst->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
550 dst->o_blocks = src->i_blocks;
551 if ( dst->o_valid & OBD_MD_FLBLKSZ )
552 dst->o_blksize = src->i_blksize;
553 if ( dst->o_valid & OBD_MD_FLMODE )
554 dst->o_mode = src->i_mode;
555 if ( dst->o_valid & OBD_MD_FLUID )
556 dst->o_uid = src->i_uid;
557 if ( dst->o_valid & OBD_MD_FLGID )
558 dst->o_gid = src->i_gid;
559 if ( dst->o_valid & OBD_MD_FLFLAGS )
560 dst->o_flags = src->i_flags;
561 if ( dst->o_valid & OBD_MD_FLNLINK )
562 dst->o_nlink = src->i_nlink;
563 if ( dst->o_valid & OBD_MD_FLGENER )
564 dst->o_generation = src->i_generation;
567 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
570 if ( src->o_valid & OBD_MD_FLID )
571 dst->i_ino = src->o_id;
572 if ( src->o_valid & OBD_MD_FLATIME )
573 dst->i_atime = src->o_atime;
574 if ( src->o_valid & OBD_MD_FLMTIME )
575 dst->i_mtime = src->o_mtime;
576 if ( src->o_valid & OBD_MD_FLCTIME )
577 dst->i_ctime = src->o_ctime;
578 if ( src->o_valid & OBD_MD_FLSIZE )
579 dst->i_size = src->o_size;
580 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
581 dst->i_blocks = src->o_blocks;
582 if ( src->o_valid & OBD_MD_FLBLKSZ )
583 dst->i_blksize = src->o_blksize;
584 if ( src->o_valid & OBD_MD_FLMODE )
585 dst->i_mode = src->o_mode;
586 if ( src->o_valid & OBD_MD_FLUID )
587 dst->i_uid = src->o_uid;
588 if ( src->o_valid & OBD_MD_FLGID )
589 dst->i_gid = src->o_gid;
590 if ( src->o_valid & OBD_MD_FLFLAGS )
591 dst->i_flags = src->o_flags;
592 if ( src->o_valid & OBD_MD_FLNLINK )
593 dst->i_nlink = src->o_nlink;
594 if ( src->o_valid & OBD_MD_FLGENER )
595 dst->i_generation = src->o_generation;
600 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
603 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
604 (unsigned long long)src->o_id, src->o_valid,
605 (unsigned long long)dst->o_id);
607 if ( src->o_valid & OBD_MD_FLATIME )
608 dst->o_atime = src->o_atime;
609 if ( src->o_valid & OBD_MD_FLMTIME )
610 dst->o_mtime = src->o_mtime;
611 if ( src->o_valid & OBD_MD_FLCTIME )
612 dst->o_ctime = src->o_ctime;
613 if ( src->o_valid & OBD_MD_FLSIZE )
614 dst->o_size = src->o_size;
615 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
616 dst->o_blocks = src->o_blocks;
617 if ( src->o_valid & OBD_MD_FLBLKSZ )
618 dst->o_blksize = src->o_blksize;
619 if ( src->o_valid & OBD_MD_FLMODE )
620 dst->o_mode = src->o_mode;
621 if ( src->o_valid & OBD_MD_FLUID )
622 dst->o_uid = src->o_uid;
623 if ( src->o_valid & OBD_MD_FLGID )
624 dst->o_gid = src->o_gid;
625 if ( src->o_valid & OBD_MD_FLFLAGS )
626 dst->o_flags = src->o_flags;
628 if ( src->o_valid & OBD_MD_FLOBDFLG )
629 dst->o_obdflags = src->o_obdflags;
631 if ( src->o_valid & OBD_MD_FLNLINK )
632 dst->o_nlink = src->o_nlink;
633 if ( src->o_valid & OBD_MD_FLGENER )
634 dst->o_generation = src->o_generation;
635 if ( src->o_valid & OBD_MD_FLINLINE &&
636 src->o_obdflags & OBD_FL_INLINEDATA) {
637 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
638 dst->o_obdflags |= OBD_FL_INLINEDATA;
641 dst->o_valid |= src->o_valid;
645 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
646 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
651 if ( compare & OBD_MD_FLATIME )
652 res = (res || (dst->o_atime != src->o_atime));
653 if ( compare & OBD_MD_FLMTIME )
654 res = (res || (dst->o_mtime != src->o_mtime));
655 if ( compare & OBD_MD_FLCTIME )
656 res = (res || (dst->o_ctime != src->o_ctime));
657 if ( compare & OBD_MD_FLSIZE )
658 res = (res || (dst->o_size != src->o_size));
659 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
660 res = (res || (dst->o_blocks != src->o_blocks));
661 if ( compare & OBD_MD_FLBLKSZ )
662 res = (res || (dst->o_blksize != src->o_blksize));
663 if ( compare & OBD_MD_FLMODE )
664 res = (res || (dst->o_mode != src->o_mode));
665 if ( compare & OBD_MD_FLUID )
666 res = (res || (dst->o_uid != src->o_uid));
667 if ( compare & OBD_MD_FLGID )
668 res = (res || (dst->o_gid != src->o_gid));
669 if ( compare & OBD_MD_FLFLAGS )
670 res = (res || (dst->o_flags != src->o_flags));
671 if ( compare & OBD_MD_FLNLINK )
672 res = (res || (dst->o_nlink != src->o_nlink));
673 if ( compare & OBD_MD_FLGENER )
674 res = (res || (dst->o_generation != src->o_generation));
675 /* XXX Don't know if thses should be included here - wasn't previously
676 if ( compare & OBD_MD_FLINLINE )
677 res = (res || memcmp(dst->o_inline, src->o_inline));
678 if ( compare & OBD_MD_FLOBDMD )
679 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
686 int class_register_type(struct obd_ops *ops, char *nm);
687 int class_unregister_type(char *nm);
688 int class_name2dev(char *name);
689 int class_uuid2dev(char *name);
690 int class_connect (struct lustre_handle *conn, struct obd_device *obd);
691 int class_disconnect(struct lustre_handle *conn);
692 struct obd_export *class_conn2export(struct lustre_handle *);
694 /* generic operations shared by various OBD types */
695 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
696 int class_multi_cleanup(struct obd_device *obddev);
702 extern void obd_sysctl_init (void);
703 extern void obd_sysctl_clean (void);
705 #endif /* __LINUX_CLASS_OBD_H */