1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc. <info@clusterfs.com>
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.
21 * User-space Lustre headers.
28 #error Kernel files should not #include <liblustre.h>
31 * The userspace implementations of linux/spinlock.h vary; we just
32 * include our own for all of them
34 #define __LINUX_SPINLOCK_H
41 #ifdef HAVE_ASM_PAGE_H
42 # include <asm/page.h>
44 #ifdef HAVE_SYS_USER_H
45 # include <sys/user.h>
47 #ifdef HAVE_SYS_IOCTL_H
48 # include <sys/ioctl.h>
52 #include <sys/ioctl.h>
64 #include <libcfs/list.h>
65 #include <lnet/lnet.h>
66 #include <libcfs/kp30.h>
67 #include <libcfs/user-bitops.h>
73 /* definitions for liblustre */
77 #define CFS_PAGE_SHIFT 12
78 #define CFS_PAGE_SIZE (1UL << CFS_PAGE_SHIFT)
79 #define CFS_PAGE_MASK (~((__u64)CFS_PAGE_SIZE-1))
80 #define loff_t long long
82 typedef unsigned short umode_t;
88 #define ARRAY_SIZE(a) ((sizeof (a))/(sizeof ((a)[0])))
91 /* This is because lprocfs_status.h gets included here indirectly. It would
92 * be much better to just avoid lprocfs being included into liblustre entirely
93 * but that requires more header surgery than I can handle right now.
95 #ifndef smp_processor_id
96 #define smp_processor_id() 0
98 #ifndef num_online_cpus
99 #define num_online_cpus() 1
101 #ifndef num_possible_cpus
102 #define num_possible_cpus() 1
105 /* always adopt 2.5 definitions */
106 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
107 #define LINUX_VERSION_CODE KERNEL_VERSION(2,6,5)
110 #define page_private(page) ((page)->private)
111 #define set_page_private(page, v) ((page)->private = (v))
115 static inline void inter_module_put(void *a)
120 void *inter_module_get(char *arg);
125 void (*ws_task)(void *arg);
129 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
137 static inline void schedule_work(struct work_struct *q)
139 q->ws_task(q->ws_arg);
143 #define strnlen(a,b) strlen(a)
144 static inline void *kmalloc(int size, int prot)
148 #define vmalloc malloc
150 #define kfree(a) free(a)
152 #define GFP_HIGHUSER 1
155 #define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
156 #define PTR_ERR(a) ((long)(a))
157 #define ERR_PTR(a) ((void*)((long)(a)))
159 typedef int (read_proc_t)(char *page, char **start, off_t off,
160 int count, int *eof, void *data);
162 struct file; /* forward ref */
163 typedef int (write_proc_t)(struct file *file, const char *buffer,
164 unsigned long count, void *data);
166 #define NIPQUAD(addr) \
167 ((unsigned char *)&addr)[0], \
168 ((unsigned char *)&addr)[1], \
169 ((unsigned char *)&addr)[2], \
170 ((unsigned char *)&addr)[3]
172 #if defined(__LITTLE_ENDIAN)
173 #define HIPQUAD(addr) \
174 ((unsigned char *)&addr)[3], \
175 ((unsigned char *)&addr)[2], \
176 ((unsigned char *)&addr)[1], \
177 ((unsigned char *)&addr)[0]
178 #elif defined(__BIG_ENDIAN)
179 #define HIPQUAD NIPQUAD
181 #error "Undefined byteorder??"
182 #endif /* __LITTLE_ENDIAN */
186 /* a long can be more than 32 bits, so use BITS_PER_LONG
187 * to allow the compiler to adjust the bit shifting accordingly
190 static __inline__ int ext2_set_bit(int nr, void *addr)
192 return set_bit(nr, addr);
195 static __inline__ int ext2_clear_bit(int nr, void *addr)
197 return clear_bit(nr, addr);
200 static __inline__ int ext2_test_bit(int nr, void *addr)
202 return test_bit(nr, addr);
211 static inline void MODULE_AUTHOR(char *name)
213 printf("%s\n", name);
215 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
216 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
218 #define THIS_MODULE NULL
224 static inline int misc_register(void *foo)
229 static inline int misc_deregister(void *foo)
234 static inline int request_module(char *name)
239 #define __MOD_INC_USE_COUNT(m) do {} while (0)
240 #define __MOD_DEC_USE_COUNT(m) do {} while (0)
241 #define MOD_INC_USE_COUNT do {} while (0)
242 #define MOD_DEC_USE_COUNT do {} while (0)
243 static inline void __module_get(struct module *module)
247 static inline int try_module_get(struct module *module)
252 static inline void module_put(struct module *module)
256 /* module initialization */
257 extern int init_obdclass(void);
258 extern int ptlrpc_init(void);
259 extern int ldlm_init(void);
260 extern int osc_init(void);
261 extern int lov_init(void);
262 extern int mdc_init(void);
263 extern int lmv_init(void);
264 extern int mgc_init(void);
265 extern int echo_client_init(void);
271 #define EXPORT_SYMBOL(S)
275 typedef struct { } spinlock_t;
276 typedef __u64 kdev_t;
278 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
279 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
280 #define LASSERT_SEM_LOCKED(sem) do {} while(0)
282 static inline void spin_lock(spinlock_t *l) {return;}
283 static inline void spin_unlock(spinlock_t *l) {return;}
284 static inline void spin_lock_init(spinlock_t *l) {return;}
285 static inline void local_irq_save(unsigned long flag) {return;}
286 static inline void local_irq_restore(unsigned long flag) {return;}
287 static inline int spin_is_locked(spinlock_t *l) {return 1;}
289 static inline void spin_lock_bh(spinlock_t *l) {}
290 static inline void spin_unlock_bh(spinlock_t *l) {}
291 static inline void spin_lock_irqsave(spinlock_t *a, unsigned long b) {}
292 static inline void spin_unlock_irqrestore(spinlock_t *a, unsigned long b) {}
294 typedef spinlock_t rwlock_t;
295 #define RW_LOCK_UNLOCKED SPIN_LOCK_UNLOCKED
296 #define read_lock(l) spin_lock(l)
297 #define read_unlock(l) spin_unlock(l)
298 #define write_lock(l) spin_lock(l)
299 #define write_unlock(l) spin_unlock(l)
302 #define min(x,y) ((x)<(y) ? (x) : (y))
303 #define max(x,y) ((x)>(y) ? (x) : (y))
306 #define min_t(type,x,y) \
307 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
310 #define max_t(type,x,y) \
311 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
314 #define simple_strtol strtol
316 /* registering symbols */
318 #define ERESTARTSYS ERESTART
324 void get_random_bytes(void *ptr, int size);
328 /* memory size: used for some client tunables */
329 #define num_physpages (256 * 1024) /* 1GB */
331 static inline int copy_from_user(void *a,void *b, int c)
337 static inline int copy_to_user(void *a,void *b, int c)
348 #define SLAB_HWCACHE_ALIGN 0
349 static inline kmem_cache_t *
350 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
352 void (*e)(void *, kmem_cache_t *, unsigned long),
353 void (*f)(void *, kmem_cache_t *, unsigned long))
356 c = malloc(sizeof(*c));
360 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
361 name, c, (int)objsize);
365 static inline int kmem_cache_destroy(kmem_cache_t *a)
367 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
372 /* struct page decl moved out from here into portals/include/libcfs/user-prim.h */
375 #define PAGE_LIST_ENTRY list
376 #define PAGE_LIST(page) ((page)->list)
378 #define kmap(page) (page)->addr
379 #define kunmap(a) do {} while (0)
381 static inline cfs_page_t *alloc_pages(int mask, unsigned long order)
383 cfs_page_t *pg = malloc(sizeof(*pg));
387 #if 0 //#ifdef MAP_ANONYMOUS
388 pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
390 pg->addr = malloc(CFS_PAGE_SIZE << order);
399 #define cfs_alloc_pages(mask, order) alloc_pages((mask), (order))
401 #define alloc_page(mask) alloc_pages((mask), 0)
402 #define cfs_alloc_page(mask) alloc_page(mask)
404 static inline void __free_pages(cfs_page_t *pg, int what)
406 #if 0 //#ifdef MAP_ANONYMOUS
407 munmap(pg->addr, PAGE_SIZE);
413 #define __cfs_free_pages(pg, order) __free_pages((pg), (order))
415 #define __free_page(page) __free_pages((page), 0)
416 #define free_page(page) __free_page(page)
417 #define __cfs_free_page(page) __cfs_free_pages((page), 0)
419 static inline cfs_page_t* __grab_cache_page(unsigned long index)
421 cfs_page_t *pg = alloc_pages(0, 0);
428 #define grab_cache_page(index) __grab_cache_page(index)
429 #define page_cache_release(page) __free_pages(page, 0)
432 #define do_div(a,b) \
434 unsigned long remainder;\
435 remainder = (a) % (b); \
441 #define ATTR_MODE 0x0001
442 #define ATTR_UID 0x0002
443 #define ATTR_GID 0x0004
444 #define ATTR_SIZE 0x0008
445 #define ATTR_ATIME 0x0010
446 #define ATTR_MTIME 0x0020
447 #define ATTR_CTIME 0x0040
448 #define ATTR_ATIME_SET 0x0080
449 #define ATTR_MTIME_SET 0x0100
450 #define ATTR_FORCE 0x0200 /* Not a change, but a change it */
451 #define ATTR_ATTR_FLAG 0x0400
452 #define ATTR_RAW 0x0800 /* file system, not vfs will massage attrs */
453 #define ATTR_FROM_OPEN 0x1000 /* called from open path, ie O_TRUNC */
454 #define ATTR_CTIME_SET 0x2000
455 #define ATTR_BLOCKS 0x4000
456 #define ATTR_KILL_SUID 0
457 #define ATTR_KILL_SGID 0
460 unsigned int ia_valid;
468 unsigned int ia_attr_flags;
471 #define ll_iattr iattr
473 #define IT_OPEN 0x0001
474 #define IT_CREAT 0x0002
475 #define IT_READDIR 0x0004
476 #define IT_GETATTR 0x0008
477 #define IT_LOOKUP 0x0010
478 #define IT_UNLINK 0x0020
479 #define IT_GETXATTR 0x0040
480 #define IT_EXEC 0x0080
481 #define IT_PIN 0x0100
483 #define IT_FL_LOCKED 0x0001
484 #define IT_FL_FOLLOWED 0x0002 /* set by vfs_follow_link */
486 #define INTENT_MAGIC 0x19620323
488 struct lustre_intent_data {
491 __u64 it_lock_handle;
496 struct lookup_intent {
498 void (*it_op_release)(struct lookup_intent *);
503 struct lustre_intent_data lustre;
507 static inline void intent_init(struct lookup_intent *it, int op, int flags)
509 memset(it, 0, sizeof(*it));
510 it->it_magic = INTENT_MAGIC;
512 it->it_flags = flags;
524 struct rw_semaphore {
533 /* use the macro's argument to avoid unused warnings */
534 #define down(a) do { (void)a; } while (0)
535 #define mutex_down(a) down(a)
536 #define up(a) do { (void)a; } while (0)
537 #define mutex_up(a) up(a)
538 #define down_read(a) do { (void)a; } while (0)
539 #define up_read(a) do { (void)a; } while (0)
540 #define down_write(a) do { (void)a; } while (0)
541 #define up_write(a) do { (void)a; } while (0)
542 #define sema_init(a,b) do { (void)a; } while (0)
543 #define init_rwsem(a) do { (void)a; } while (0)
544 #define DECLARE_MUTEX(name) \
545 struct semaphore name = { 1 }
546 static inline void init_MUTEX (struct semaphore *sem)
550 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
555 #define init_mutex(s) init_MUTEX(s)
558 struct list_head sleepers;
562 struct list_head sleeping;
572 struct signal pending;
585 typedef struct task_struct cfs_task_t;
586 #define cfs_current() current
587 #define cfs_curproc_pid() (current->pid)
588 #define cfs_curproc_comm() (current->comm)
590 extern struct task_struct *current;
591 int in_group_p(gid_t gid);
592 static inline int capable(int cap)
594 if (current->cap_effective & (1 << cap))
600 #define set_current_state(foo) do { current->state = foo; } while (0)
602 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
603 #define add_wait_queue(q,p) do { list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
604 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
605 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
607 #define DECLARE_WAIT_QUEUE_HEAD(HEAD) \
608 wait_queue_head_t HEAD = { \
609 .sleepers = CFS_LIST_HEAD_INIT(HEAD.sleepers) \
611 #define init_waitqueue_head(l) CFS_INIT_LIST_HEAD(&(l)->sleepers)
612 #define wake_up(l) do { int a; a++; } while (0)
613 #define TASK_INTERRUPTIBLE 0
614 #define TASK_UNINTERRUPTIBLE 1
615 #define TASK_RUNNING 2
617 #define wait_event_interruptible(wq, condition) \
619 struct l_wait_info lwi; \
620 int timeout = 100000000;/* for ever */ \
623 lwi = LWI_TIMEOUT(timeout, NULL, NULL); \
624 ret = l_wait_event(NULL, condition, &lwi); \
629 #define in_interrupt() (0)
631 #define schedule() do {} while (0)
632 static inline int schedule_timeout(signed long t)
637 #define lock_kernel() do {} while (0)
638 #define unlock_kernel() do {} while (0)
639 #define daemonize(l) do {} while (0)
640 #define sigfillset(l) do {} while (0)
641 #define recalc_sigpending(l) do {} while (0)
642 #define kernel_thread(l,m,n) LBUG()
644 #define USERMODEHELPER(path, argv, envp) (0)
645 #define SIGNAL_MASK_ASSERT()
648 #include <sys/time.h>
650 #error "liblustre's jiffies currently expects HZ to be 1"
654 unsigned long _ret = 0; \
656 if (gettimeofday(&tv, NULL) == 0) \
660 #define get_jiffies_64() (__u64)jiffies
661 #define time_after(a, b) ((long)(b) - (long)(a) < 0)
662 #define time_before(a, b) time_after(b,a)
663 #define time_after_eq(a,b) ((long)(a) - (long)(b) >= 0)
666 struct list_head tl_list;
667 void (*function)(unsigned long unused);
672 static inline int timer_pending(struct timer_list *l)
674 if (time_after(l->expires, jiffies))
680 static inline int init_timer(struct timer_list *l)
682 CFS_INIT_LIST_HEAD(&l->tl_list);
686 static inline void mod_timer(struct timer_list *l, int thetime)
688 l->expires = thetime;
691 static inline void del_timer(struct timer_list *l)
696 typedef struct { volatile int counter; } atomic_t;
698 #define ATOMIC_INIT(i) { (i) }
700 #define atomic_read(a) ((a)->counter)
701 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
702 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
703 #define atomic_dec_and_lock(a,b) ((--((a)->counter)) == 0)
704 #define atomic_inc(a) (((a)->counter)++)
705 #define atomic_dec(a) do { (a)->counter--; } while (0)
706 #define atomic_add(b,a) do {(a)->counter += b;} while (0)
707 #define atomic_add_return(n,a) ((a)->counter += n)
708 #define atomic_inc_return(a) atomic_add_return(1,a)
709 #define atomic_sub(b,a) do {(a)->counter -= b;} while (0)
710 #define atomic_sub_return(n,a) ((a)->counter -= n)
711 #define atomic_dec_return(a) atomic_sub_return(1,a)
714 #define likely(exp) (exp)
717 #define unlikely(exp) (exp)
720 #define might_sleep()
721 #define might_sleep_if(c)
725 int test_and_set_bit(int nr, unsigned long *addr)
729 while (nr >= sizeof(long)) {
734 oldbit = (*addr) & (1 << nr);
740 int test_and_clear_bit(int nr, unsigned long *addr)
744 while (nr >= sizeof(long)) {
749 oldbit = (*addr) & (1 << nr);
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 Cray, we copy definition we need from capability.h
760 typedef struct _cap_struct *cap_t;
761 typedef int cap_value_t;
772 #define CAP_DAC_OVERRIDE 1
773 #define CAP_DAC_READ_SEARCH 2
776 #define CAP_SYS_ADMIN 21
778 cap_t cap_get_proc(void);
779 int cap_get_flag(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *);
781 static inline void libcfs_run_lbug_upcall(char *file, const char *fn,
790 #define COMPLETION_INITIALIZER(work) \
791 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
793 #define DECLARE_COMPLETION(work) \
794 struct completion work = COMPLETION_INITIALIZER(work)
796 #define INIT_COMPLETION(x) ((x).done = 0)
798 static inline void init_completion(struct completion *x)
801 init_waitqueue_head(&x->wait);
804 struct liblustre_wait_callback {
805 struct list_head llwc_list;
806 const char *llwc_name;
807 int (*llwc_fn)(void *arg);
811 void *liblustre_register_wait_callback(const char *name,
812 int (*fn)(void *arg), void *arg);
813 void liblustre_deregister_wait_callback(void *notifier);
814 int liblustre_wait_event(int timeout);
816 void *liblustre_register_idle_callback(const char *name,
817 int (*fn)(void *arg), void *arg);
818 void liblustre_deregister_idle_callback(void *notifier);
819 void liblustre_wait_idle(void);
822 struct nfs_lock_info {
828 typedef struct file_lock {
829 struct file_lock *fl_next; /* singly linked list for this inode */
830 struct list_head fl_link; /* doubly linked list of all locks */
831 struct list_head fl_block; /* circular list of blocked processes */
835 struct file *fl_file;
836 unsigned char fl_flags;
837 unsigned char fl_type;
841 void (*fl_notify)(struct file_lock *); /* unblock callback */
842 void (*fl_insert)(struct file_lock *); /* lock insertion callback */
843 void (*fl_remove)(struct file_lock *); /* lock removal callback */
845 void *fl_fasync; /* for lease break notifications */
846 unsigned long fl_break_time; /* for nonblocking lease breaks */
849 struct nfs_lock_info nfs_fl;
853 #define cfs_flock_type(fl) ((fl)->fl_type)
854 #define cfs_flock_set_type(fl, type) do { (fl)->fl_type = (type); } while(0)
855 #define cfs_flock_pid(fl) ((fl)->fl_pid)
856 #define cfs_flock_set_pid(fl, pid) do { (fl)->fl_pid = (pid); } while(0)
857 #define cfs_flock_start(fl) ((fl)->fl_start)
858 #define cfs_flock_set_start(fl, start) do { (fl)->fl_start = (start); } while(0)
859 #define cfs_flock_end(fl) ((fl)->fl_end)
860 #define cfs_flock_set_end(fl, end) do { (fl)->fl_end = (end); } while(0)
863 #define INT_LIMIT(x) (~((x)1 << (sizeof(x)*8 - 1)))
864 #define OFFSET_MAX INT_LIMIT(loff_t)
867 /* XXX: defined in kernel */
876 struct posix_acl_entry {
878 unsigned short e_perm;
884 unsigned int a_count;
885 struct posix_acl_entry a_entries[0];
896 xattr_acl_entry a_entries[0];
899 static inline size_t xattr_acl_size(int count)
901 return sizeof(xattr_acl_header) + count * sizeof(xattr_acl_entry);
905 struct posix_acl * posix_acl_from_xattr(const void *value, size_t size)
911 int posix_acl_valid(const struct posix_acl *acl)
917 void posix_acl_release(struct posix_acl *acl)
921 #ifdef LIBLUSTRE_POSIX_ACL
922 # ifndef posix_acl_xattr_entry
923 # define posix_acl_xattr_entry xattr_acl_entry
925 # ifndef posix_acl_xattr_header
926 # define posix_acl_xattr_header xattr_acl_header
928 # ifndef posix_acl_xattr_size
929 # define posix_acl_xattr_size(entry) xattr_acl_size(entry)
931 # ifndef CONFIG_FS_POSIX_ACL
932 # define CONFIG_FS_POSIX_ACL 1
937 #define ENOTSUPP ENOTSUP
940 typedef int mm_segment_t;
945 static inline mm_segment_t get_fs(void)
950 static inline void set_fs(mm_segment_t seg)
954 #include <obd_support.h>
955 #include <lustre/lustre_idl.h>
956 #include <lustre_lib.h>
957 #include <lustre_import.h>
958 #include <lustre_export.h>
959 #include <lustre_net.h>
961 /* Fast hashing routine for a long.
962 (C) 2002 William Lee Irwin III, IBM */
965 * Knuth recommends primes in approximately golden ratio to the maximum
966 * integer representable by a machine word for multiplicative hashing.
967 * Chuck Lever verified the effectiveness of this technique:
968 * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
970 * These primes are chosen to be bit-sparse, that is operations on
971 * them can use shifts and additions instead of multiplications for
972 * machines where multiplications are slow.
974 #if BITS_PER_LONG == 32
975 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
976 #define GOLDEN_RATIO_PRIME 0x9e370001UL
977 #elif BITS_PER_LONG == 64
978 /* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
979 #define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
981 #error Define GOLDEN_RATIO_PRIME for your wordsize.
984 static inline unsigned long hash_long(unsigned long val, unsigned int bits)
986 unsigned long hash = val;
988 #if BITS_PER_LONG == 64
989 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
990 unsigned long n = hash;
1004 /* On some cpus multiply is faster, on others gcc will do shifts */
1005 hash *= GOLDEN_RATIO_PRIME;
1008 /* High bits are more random, so use them. */
1009 return hash >> (BITS_PER_LONG - bits);
1012 static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
1014 return hash_long((unsigned long)ptr, bits);