1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/include/liblustre.h
38 * User-space Lustre headers.
45 #error Kernel files should not #include <liblustre.h>
48 * The userspace implementations of linux/spinlock.h vary; we just
49 * include our own for all of them
51 #define __LINUX_SPINLOCK_H
58 #ifdef HAVE_ASM_PAGE_H
59 # include <asm/page.h>
61 #ifdef HAVE_SYS_USER_H
62 # include <sys/user.h>
64 #ifdef HAVE_SYS_IOCTL_H
65 # include <sys/ioctl.h>
72 #include <sys/ioctl.h>
83 #include <libcfs/list.h>
84 #include <lnet/lnet.h>
85 #include <libcfs/kp30.h>
86 #include <libcfs/user-bitops.h>
88 /* definitions for liblustre */
92 #define CFS_PAGE_SHIFT 12
93 #define CFS_PAGE_SIZE (1UL << CFS_PAGE_SHIFT)
94 #define CFS_PAGE_MASK (~((__u64)CFS_PAGE_SIZE-1))
95 #define loff_t long long
97 typedef unsigned short umode_t;
101 #ifndef CURRENT_SECONDS
102 # define CURRENT_SECONDS time(0)
106 #define ARRAY_SIZE(a) ((sizeof (a))/(sizeof ((a)[0])))
109 /* This is because lprocfs_status.h gets included here indirectly. It would
110 * be much better to just avoid lprocfs being included into liblustre entirely
111 * but that requires more header surgery than I can handle right now.
113 #ifndef smp_processor_id
114 #define smp_processor_id() 0
116 #ifndef num_online_cpus
117 #define num_online_cpus() 1
119 #ifndef num_possible_cpus
120 #define num_possible_cpus() 1
123 /* always adopt 2.5 definitions */
124 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
125 #define LINUX_VERSION_CODE KERNEL_VERSION(2,5,0)
128 #define page_private(page) ((page)->private)
129 #define set_page_private(page, v) ((page)->private = (v))
132 static inline void inter_module_put(void *a)
137 void *inter_module_get(char *arg);
142 void (*ws_task)(void *arg);
146 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
154 static inline void schedule_work(struct work_struct *q)
156 q->ws_task(q->ws_arg);
160 #define strnlen(a,b) strlen(a)
161 static inline void *kmalloc(int size, int prot)
165 #define vmalloc malloc
167 #define kfree(a) free(a)
169 #define GFP_HIGHUSER 1
172 #define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
173 #define PTR_ERR(a) ((long)(a))
174 #define ERR_PTR(a) ((void*)((long)(a)))
180 typedef int (read_proc_t)(char *page, char **start, off_t off,
181 int count, int *eof, void *data);
183 struct file; /* forward ref */
184 typedef int (write_proc_t)(struct file *file, const char *buffer,
185 unsigned long count, void *data);
187 #define NIPQUAD(addr) \
188 ((unsigned char *)&addr)[0], \
189 ((unsigned char *)&addr)[1], \
190 ((unsigned char *)&addr)[2], \
191 ((unsigned char *)&addr)[3]
193 #if defined(__LITTLE_ENDIAN)
194 #define HIPQUAD(addr) \
195 ((unsigned char *)&addr)[3], \
196 ((unsigned char *)&addr)[2], \
197 ((unsigned char *)&addr)[1], \
198 ((unsigned char *)&addr)[0]
199 #elif defined(__BIG_ENDIAN)
200 #define HIPQUAD NIPQUAD
202 #error "Undefined byteorder??"
203 #endif /* __LITTLE_ENDIAN */
207 /* a long can be more than 32 bits, so use BITS_PER_LONG
208 * to allow the compiler to adjust the bit shifting accordingly
211 static __inline__ int ext2_set_bit(int nr, void *addr)
214 return set_bit((nr ^ ((BITS_PER_LONG-1) & ~0x7)), addr);
216 return set_bit(nr, addr);
220 static __inline__ int ext2_clear_bit(int nr, void *addr)
223 return clear_bit((nr ^ ((BITS_PER_LONG-1) & ~0x7)), addr);
225 return clear_bit(nr, addr);
229 static __inline__ int ext2_test_bit(int nr, void *addr)
232 __const__ unsigned char *tmp = (__const__ unsigned char *) addr;
233 return (tmp[nr >> 3] >> (nr & 7)) & 1;
235 return test_bit(nr, addr);
245 static inline void MODULE_AUTHOR(char *name)
247 printf("%s\n", name);
249 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
250 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
252 #define THIS_MODULE NULL
258 static inline int misc_register(void *foo)
263 static inline int misc_deregister(void *foo)
268 static inline int request_module(const char *name, ...)
273 #define __MOD_INC_USE_COUNT(m) do {} while (0)
274 #define __MOD_DEC_USE_COUNT(m) do {} while (0)
275 #define MOD_INC_USE_COUNT do {} while (0)
276 #define MOD_DEC_USE_COUNT do {} while (0)
277 static inline void __module_get(struct module *module)
281 static inline int try_module_get(struct module *module)
286 static inline void module_put(struct module *module)
290 /* module initialization */
291 extern int init_obdclass(void);
292 extern int ptlrpc_init(void);
293 extern int ldlm_init(void);
294 extern int osc_init(void);
295 extern int lov_init(void);
296 extern int mdc_init(void);
297 extern int mgc_init(void);
298 extern int echo_client_init(void);
304 #define EXPORT_SYMBOL(S)
308 typedef struct { } spinlock_t;
309 typedef __u64 kdev_t;
311 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
312 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
313 #define LASSERT_SEM_LOCKED(sem) do {} while(0)
315 static inline void spin_lock(spinlock_t *l) {return;}
316 static inline void spin_unlock(spinlock_t *l) {return;}
317 static inline void spin_lock_init(spinlock_t *l) {return;}
318 static inline void local_irq_save(unsigned long flag) {return;}
319 static inline void local_irq_restore(unsigned long flag) {return;}
320 static inline int spin_is_locked(spinlock_t *l) {return 1;}
322 static inline void spin_lock_bh(spinlock_t *l) {}
323 static inline void spin_unlock_bh(spinlock_t *l) {}
324 static inline void spin_lock_irqsave(spinlock_t *a, unsigned long b) {}
325 static inline void spin_unlock_irqrestore(spinlock_t *a, unsigned long b) {}
327 typedef spinlock_t rwlock_t;
328 #define RW_LOCK_UNLOCKED SPIN_LOCK_UNLOCKED
329 #define read_lock(l) spin_lock(l)
330 #define read_unlock(l) spin_unlock(l)
331 #define write_lock(l) spin_lock(l)
332 #define write_unlock(l) spin_unlock(l)
333 #define rwlock_init(l) spin_lock_init(l)
335 #define min(x,y) ((x)<(y) ? (x) : (y))
336 #define max(x,y) ((x)>(y) ? (x) : (y))
339 #define min_t(type,x,y) \
340 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
343 #define max_t(type,x,y) \
344 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
347 #define simple_strtol strtol
349 /* registering symbols */
351 #define ERESTARTSYS ERESTART
358 void get_random_bytes(void *ptr, int size);
362 /* memory size: used for some client tunables */
363 #define num_physpages (256 * 1024) /* 1GB */
364 #define CFS_NUM_CACHEPAGES num_physpages
366 static inline int copy_from_user(void *a,void *b, int c)
372 static inline int copy_to_user(void *a,void *b, int c)
383 #define SLAB_HWCACHE_ALIGN 0
384 #define SLAB_DESTROY_BY_RCU 0
385 static inline kmem_cache_t *
386 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
388 void (*e)(void *, kmem_cache_t *, unsigned long),
389 void (*f)(void *, kmem_cache_t *, unsigned long))
392 c = malloc(sizeof(*c));
396 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
397 name, c, (int)objsize);
401 static inline int kmem_cache_destroy(kmem_cache_t *a)
403 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
408 /* struct page decl moved out from here into portals/include/libcfs/user-prim.h */
411 #define PAGE_LIST_ENTRY list
412 #define PAGE_LIST(page) ((page)->list)
414 #define kmap(page) (page)->addr
415 #define kunmap(a) do {} while (0)
417 static inline cfs_page_t *alloc_pages(int mask, unsigned long order)
419 cfs_page_t *pg = malloc(sizeof(*pg));
423 #if 0 //#ifdef MAP_ANONYMOUS
424 pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
426 pg->addr = malloc(CFS_PAGE_SIZE << order);
435 #define cfs_alloc_pages(mask, order) alloc_pages((mask), (order))
437 #define alloc_page(mask) alloc_pages((mask), 0)
438 #define cfs_alloc_page(mask) alloc_page(mask)
440 static inline void __free_pages(cfs_page_t *pg, int what)
442 #if 0 //#ifdef MAP_ANONYMOUS
443 munmap(pg->addr, PAGE_SIZE);
449 #define __cfs_free_pages(pg, order) __free_pages((pg), (order))
451 #define __free_page(page) __free_pages((page), 0)
452 #define free_page(page) __free_page(page)
453 #define __cfs_free_page(page) __cfs_free_pages((page), 0)
455 static inline cfs_page_t* __grab_cache_page(unsigned long index)
457 cfs_page_t *pg = alloc_pages(0, 0);
464 #define grab_cache_page(index) __grab_cache_page(index)
465 #define page_cache_release(page) __free_pages(page, 0)
468 #define do_div(a,b) \
470 unsigned long remainder;\
471 remainder = (a) % (b); \
477 #define ATTR_MODE 0x0001
478 #define ATTR_UID 0x0002
479 #define ATTR_GID 0x0004
480 #define ATTR_SIZE 0x0008
481 #define ATTR_ATIME 0x0010
482 #define ATTR_MTIME 0x0020
483 #define ATTR_CTIME 0x0040
484 #define ATTR_ATIME_SET 0x0080
485 #define ATTR_MTIME_SET 0x0100
486 #define ATTR_FORCE 0x0200 /* Not a change, but a change it */
487 #define ATTR_ATTR_FLAG 0x0400
488 #define ATTR_RAW 0x0800 /* file system, not vfs will massage attrs */
489 #define ATTR_FROM_OPEN 0x1000 /* called from open path, ie O_TRUNC */
490 #define ATTR_CTIME_SET 0x2000
491 #define ATTR_KILL_SUID 0
492 #define ATTR_KILL_SGID 0
495 unsigned int ia_valid;
503 unsigned int ia_attr_flags;
505 #define ll_iattr_struct iattr
507 #define IT_OPEN 0x0001
508 #define IT_CREAT 0x0002
509 #define IT_READDIR 0x0004
510 #define IT_GETATTR 0x0008
511 #define IT_LOOKUP 0x0010
512 #define IT_UNLINK 0x0020
513 #define IT_GETXATTR 0x0040
514 #define IT_EXEC 0x0080
515 #define IT_PIN 0x0100
517 #define IT_FL_LOCKED 0x0001
518 #define IT_FL_FOLLOWED 0x0002 /* set by vfs_follow_link */
520 #define INTENT_MAGIC 0x19620323
522 struct lustre_intent_data {
525 __u64 it_lock_handle;
530 struct lookup_intent {
532 void (*it_op_release)(struct lookup_intent *);
537 struct lustre_intent_data lustre;
541 static inline void intent_init(struct lookup_intent *it, int op, int flags)
543 memset(it, 0, sizeof(*it));
544 it->it_magic = INTENT_MAGIC;
546 it->it_flags = flags;
559 struct rw_semaphore {
568 /* use the macro's argument to avoid unused warnings */
569 #define down(a) do { (void)a; } while (0)
570 #define mutex_down(a) down(a)
571 #define up(a) do { (void)a; } while (0)
572 #define mutex_up(a) up(a)
573 #define down_read(a) do { (void)a; } while (0)
574 #define up_read(a) do { (void)a; } while (0)
575 #define down_write(a) do { (void)a; } while (0)
576 #define up_write(a) do { (void)a; } while (0)
577 #define sema_init(a,b) do { (void)a; } while (0)
578 #define init_rwsem(a) do { (void)a; } while (0)
579 #define DECLARE_MUTEX(name) \
580 struct semaphore name = { 1 }
581 static inline void init_MUTEX (struct semaphore *sem)
585 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
590 #define init_mutex(s) init_MUTEX(s)
593 struct list_head sleepers;
597 struct list_head sleeping;
607 struct signal pending;
615 cfs_cap_t cap_effective;
618 typedef struct task_struct cfs_task_t;
619 #define cfs_current() current
620 #define cfs_curproc_pid() (current->pid)
621 #define cfs_curproc_comm() (current->comm)
622 #define cfs_curproc_fsuid() (current->fsuid)
623 #define cfs_curproc_fsgid() (current->fsgid)
625 extern struct task_struct *current;
626 int in_group_p(gid_t gid);
628 #define set_current_state(foo) do { current->state = foo; } while (0)
630 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
631 #define add_wait_queue(q,p) do { list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
632 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
633 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
635 #define DECLARE_WAIT_QUEUE_HEAD(HEAD) \
636 wait_queue_head_t HEAD = { \
637 .sleepers = CFS_LIST_HEAD_INIT(HEAD.sleepers) \
639 #define init_waitqueue_head(l) CFS_INIT_LIST_HEAD(&(l)->sleepers)
640 #define wake_up(l) do { int a = 0; a++; } while (0)
641 #define TASK_INTERRUPTIBLE 0
642 #define TASK_UNINTERRUPTIBLE 1
643 #define TASK_RUNNING 2
645 #define wait_event_interruptible(wq, condition) \
647 struct l_wait_info lwi; \
648 int timeout = 100000000;/* for ever */ \
651 lwi = LWI_TIMEOUT(timeout, NULL, NULL); \
652 ret = l_wait_event(NULL, condition, &lwi); \
657 #define in_interrupt() (0)
659 #define schedule() do {} while (0)
660 static inline int schedule_timeout(signed long t)
665 #define lock_kernel() do {} while (0)
666 #define unlock_kernel() do {} while (0)
667 #define daemonize(l) do {} while (0)
668 #define sigfillset(l) do {} while (0)
669 #define recalc_sigpending(l) do {} while (0)
670 #define kernel_thread(l,m,n) LBUG()
672 #define USERMODEHELPER(path, argv, envp) (0)
673 #define SIGNAL_MASK_ASSERT()
676 #include <sys/time.h>
678 #error "liblustre's jiffies currently expects HZ to be 1"
682 unsigned long _ret = 0; \
684 if (gettimeofday(&tv, NULL) == 0) \
688 #define get_jiffies_64() (__u64)jiffies
689 #define time_after(a, b) ((long)(b) - (long)(a) < 0)
690 #define time_before(a, b) time_after(b,a)
691 #define time_after_eq(a,b) ((long)(a) - (long)(b) >= 0)
694 struct list_head tl_list;
695 void (*function)(unsigned long unused);
700 static inline int timer_pending(struct timer_list *l)
702 if (time_after(l->expires, jiffies))
708 static inline int init_timer(struct timer_list *l)
710 CFS_INIT_LIST_HEAD(&l->tl_list);
714 static inline void mod_timer(struct timer_list *l, int thetime)
716 l->expires = thetime;
719 static inline void del_timer(struct timer_list *l)
724 typedef struct { volatile int counter; } atomic_t;
726 #define ATOMIC_INIT(i) { i }
728 #define atomic_read(a) ((a)->counter)
729 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
730 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
731 #define atomic_dec_and_lock(a,b) ((--((a)->counter)) == 0)
732 #define atomic_inc(a) (((a)->counter)++)
733 #define atomic_dec(a) do { (a)->counter--; } while (0)
734 #define atomic_add(b,a) do {(a)->counter += b;} while (0)
735 #define atomic_add_return(n,a) ((a)->counter += n)
736 #define atomic_inc_return(a) atomic_add_return(1,a)
737 #define atomic_sub(b,a) do {(a)->counter -= b;} while (0)
738 #define atomic_sub_return(n,a) ((a)->counter -= n)
739 #define atomic_dec_return(a) atomic_sub_return(1,a)
740 #define atomic_add_unless(v, a, u) ((v)->counter != u ? (v)->counter += a : 0)
741 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
744 #define likely(exp) (exp)
747 #define unlikely(exp) (exp)
750 #define libcfs_memory_pressure_get() (0)
751 #define libcfs_memory_pressure_set() do {} while (0)
752 #define libcfs_memory_pressure_clr() do {} while (0)
754 /* FIXME sys/capability will finally included linux/fs.h thus
755 * cause numerous trouble on x86-64. as temporary solution for
756 * build broken at cary, we copy definition we need from capability.h
760 typedef struct _cap_struct *cap_t;
761 typedef int cap_value_t;
772 cap_t cap_get_proc(void);
773 int cap_get_flag(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *);
775 static inline void libcfs_run_lbug_upcall(char *file, const char *fn,
784 #define COMPLETION_INITIALIZER(work) \
785 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
787 #define DECLARE_COMPLETION(work) \
788 struct completion work = COMPLETION_INITIALIZER(work)
790 #define INIT_COMPLETION(x) ((x).done = 0)
792 static inline void init_completion(struct completion *x)
795 init_waitqueue_head(&x->wait);
798 struct liblustre_wait_callback {
799 struct list_head llwc_list;
800 const char *llwc_name;
801 int (*llwc_fn)(void *arg);
805 void *liblustre_register_wait_callback(const char *name,
806 int (*fn)(void *arg), void *arg);
807 void liblustre_deregister_wait_callback(void *notifier);
808 int liblustre_wait_event(int timeout);
810 void *liblustre_register_idle_callback(const char *name,
811 int (*fn)(void *arg), void *arg);
812 void liblustre_deregister_idle_callback(void *notifier);
813 void liblustre_wait_idle(void);
816 struct nfs_lock_info {
822 typedef struct file_lock {
823 struct file_lock *fl_next; /* singly linked list for this inode */
824 struct list_head fl_link; /* doubly linked list of all locks */
825 struct list_head fl_block; /* circular list of blocked processes */
829 struct file *fl_file;
830 unsigned char fl_flags;
831 unsigned char fl_type;
835 void (*fl_notify)(struct file_lock *); /* unblock callback */
836 void (*fl_insert)(struct file_lock *); /* lock insertion callback */
837 void (*fl_remove)(struct file_lock *); /* lock removal callback */
839 void *fl_fasync; /* for lease break notifications */
840 unsigned long fl_break_time; /* for nonblocking lease breaks */
843 struct nfs_lock_info nfs_fl;
847 #define cfs_flock_type(fl) ((fl)->fl_type)
848 #define cfs_flock_set_type(fl, type) do { (fl)->fl_type = (type); } while(0)
849 #define cfs_flock_pid(fl) ((fl)->fl_pid)
850 #define cfs_flock_set_pid(fl, pid) do { (fl)->fl_pid = (pid); } while(0)
851 #define cfs_flock_start(fl) ((fl)->fl_start)
852 #define cfs_flock_set_start(fl, start) do { (fl)->fl_start = (start); } while(0)
853 #define cfs_flock_end(fl) ((fl)->fl_end)
854 #define cfs_flock_set_end(fl, end) do { (fl)->fl_end = (end); } while(0)
857 #define INT_LIMIT(x) (~((x)1 << (sizeof(x)*8 - 1)))
858 #define OFFSET_MAX INT_LIMIT(loff_t)
861 /* XXX: defined in kernel */
870 struct posix_acl_entry {
872 unsigned short e_perm;
878 unsigned int a_count;
879 struct posix_acl_entry a_entries[0];
890 xattr_acl_entry a_entries[0];
893 static inline size_t xattr_acl_size(int count)
895 return sizeof(xattr_acl_header) + count * sizeof(xattr_acl_entry);
899 struct posix_acl * posix_acl_from_xattr(const void *value, size_t size)
905 int posix_acl_valid(const struct posix_acl *acl)
911 void posix_acl_release(struct posix_acl *acl)
915 #ifdef LIBLUSTRE_POSIX_ACL
916 #ifndef posix_acl_xattr_entry
917 #define posix_acl_xattr_entry xattr_acl_entry
919 #ifndef posix_acl_xattr_header
920 #define posix_acl_xattr_header xattr_acl_header
922 #ifndef posix_acl_xattr_size
923 #define posix_acl_xattr_size(entry) xattr_acl_size(entry)
925 #ifndef CONFIG_FS_POSIX_ACL
926 #define CONFIG_FS_POSIX_ACL 1
931 #define ENOTSUPP ENOTSUP
934 #include <obd_support.h>
935 #include <lustre/lustre_idl.h>
936 #include <lustre_lib.h>
937 #include <lustre_import.h>
938 #include <lustre_export.h>
939 #include <lustre_net.h>