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>
55 #include <sys/ioctl.h>
66 #include <libcfs/list.h>
67 #include <lnet/lnet.h>
68 #include <libcfs/kp30.h>
69 #include <libcfs/user-bitops.h>
71 /* definitions for liblustre */
75 #define CFS_PAGE_SHIFT 12
76 #define CFS_PAGE_SIZE (1UL << CFS_PAGE_SHIFT)
77 #define CFS_PAGE_MASK (~((__u64)CFS_PAGE_SIZE-1))
78 #define loff_t long long
80 typedef unsigned short umode_t;
84 #ifndef CURRENT_SECONDS
85 # define CURRENT_SECONDS time(0)
89 #define ARRAY_SIZE(a) ((sizeof (a))/(sizeof ((a)[0])))
92 /* This is because lprocfs_status.h gets included here indirectly. It would
93 * be much better to just avoid lprocfs being included into liblustre entirely
94 * but that requires more header surgery than I can handle right now.
96 #ifndef smp_processor_id
97 #define smp_processor_id() 0
99 #ifndef num_online_cpus
100 #define num_online_cpus() 1
102 #ifndef num_possible_cpus
103 #define num_possible_cpus() 1
106 /* always adopt 2.5 definitions */
107 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
108 #define LINUX_VERSION_CODE KERNEL_VERSION(2,5,0)
111 #define page_private(page) ((page)->private)
112 #define set_page_private(page, v) ((page)->private = (v))
116 static inline void inter_module_put(void *a)
121 void *inter_module_get(char *arg);
126 void (*ws_task)(void *arg);
130 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
138 static inline void schedule_work(struct work_struct *q)
140 q->ws_task(q->ws_arg);
144 #define strnlen(a,b) strlen(a)
145 static inline void *kmalloc(int size, int prot)
149 #define vmalloc malloc
151 #define kfree(a) free(a)
153 #define GFP_HIGHUSER 1
156 #define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
157 #define PTR_ERR(a) ((long)(a))
158 #define ERR_PTR(a) ((void*)((long)(a)))
164 typedef int (read_proc_t)(char *page, char **start, off_t off,
165 int count, int *eof, void *data);
167 struct file; /* forward ref */
168 typedef int (write_proc_t)(struct file *file, const char *buffer,
169 unsigned long count, void *data);
171 #define NIPQUAD(addr) \
172 ((unsigned char *)&addr)[0], \
173 ((unsigned char *)&addr)[1], \
174 ((unsigned char *)&addr)[2], \
175 ((unsigned char *)&addr)[3]
177 #if defined(__LITTLE_ENDIAN)
178 #define HIPQUAD(addr) \
179 ((unsigned char *)&addr)[3], \
180 ((unsigned char *)&addr)[2], \
181 ((unsigned char *)&addr)[1], \
182 ((unsigned char *)&addr)[0]
183 #elif defined(__BIG_ENDIAN)
184 #define HIPQUAD NIPQUAD
186 #error "Undefined byteorder??"
187 #endif /* __LITTLE_ENDIAN */
191 /* a long can be more than 32 bits, so use BITS_PER_LONG
192 * to allow the compiler to adjust the bit shifting accordingly
195 static __inline__ int ext2_set_bit(int nr, void *addr)
197 return set_bit(nr, addr);
200 static __inline__ int ext2_clear_bit(int nr, void *addr)
202 return clear_bit(nr, addr);
205 static __inline__ int ext2_test_bit(int nr, void *addr)
207 return test_bit(nr, addr);
216 static inline void MODULE_AUTHOR(char *name)
218 printf("%s\n", name);
220 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
221 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
223 #define THIS_MODULE NULL
229 static inline int misc_register(void *foo)
234 static inline int misc_deregister(void *foo)
239 static inline int request_module(char *name)
244 #define __MOD_INC_USE_COUNT(m) do {} while (0)
245 #define __MOD_DEC_USE_COUNT(m) do {} while (0)
246 #define MOD_INC_USE_COUNT do {} while (0)
247 #define MOD_DEC_USE_COUNT do {} while (0)
248 static inline void __module_get(struct module *module)
252 static inline int try_module_get(struct module *module)
257 static inline void module_put(struct module *module)
261 /* module initialization */
262 extern int init_obdclass(void);
263 extern int ptlrpc_init(void);
264 extern int ldlm_init(void);
265 extern int osc_init(void);
266 extern int lov_init(void);
267 extern int mdc_init(void);
268 extern int mgc_init(void);
269 extern int echo_client_init(void);
275 #define EXPORT_SYMBOL(S)
279 typedef struct { } spinlock_t;
280 typedef __u64 kdev_t;
282 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
283 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
284 #define LASSERT_SEM_LOCKED(sem) do {} while(0)
286 static inline void spin_lock(spinlock_t *l) {return;}
287 static inline void spin_unlock(spinlock_t *l) {return;}
288 static inline void spin_lock_init(spinlock_t *l) {return;}
289 static inline void local_irq_save(unsigned long flag) {return;}
290 static inline void local_irq_restore(unsigned long flag) {return;}
291 static inline int spin_is_locked(spinlock_t *l) {return 1;}
293 static inline void spin_lock_bh(spinlock_t *l) {}
294 static inline void spin_unlock_bh(spinlock_t *l) {}
295 static inline void spin_lock_irqsave(spinlock_t *a, unsigned long b) {}
296 static inline void spin_unlock_irqrestore(spinlock_t *a, unsigned long b) {}
298 typedef spinlock_t rwlock_t;
299 #define RW_LOCK_UNLOCKED SPIN_LOCK_UNLOCKED
300 #define read_lock(l) spin_lock(l)
301 #define read_unlock(l) spin_unlock(l)
302 #define write_lock(l) spin_lock(l)
303 #define write_unlock(l) spin_unlock(l)
304 #define rwlock_init(l) spin_lock_init(l)
306 #define min(x,y) ((x)<(y) ? (x) : (y))
307 #define max(x,y) ((x)>(y) ? (x) : (y))
310 #define min_t(type,x,y) \
311 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
314 #define max_t(type,x,y) \
315 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
318 #define simple_strtol strtol
320 /* registering symbols */
322 #define ERESTARTSYS ERESTART
328 void get_random_bytes(void *ptr, int size);
332 /* memory size: used for some client tunables */
333 #define num_physpages (256 * 1024) /* 1GB */
335 static inline int copy_from_user(void *a,void *b, int c)
341 static inline int copy_to_user(void *a,void *b, int c)
352 #define SLAB_HWCACHE_ALIGN 0
353 static inline kmem_cache_t *
354 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
356 void (*e)(void *, kmem_cache_t *, unsigned long),
357 void (*f)(void *, kmem_cache_t *, unsigned long))
360 c = malloc(sizeof(*c));
364 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
365 name, c, (int)objsize);
369 static inline int kmem_cache_destroy(kmem_cache_t *a)
371 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
376 /* struct page decl moved out from here into portals/include/libcfs/user-prim.h */
379 #define PAGE_LIST_ENTRY list
380 #define PAGE_LIST(page) ((page)->list)
382 #define kmap(page) (page)->addr
383 #define kunmap(a) do {} while (0)
385 static inline cfs_page_t *alloc_pages(int mask, unsigned long order)
387 cfs_page_t *pg = malloc(sizeof(*pg));
391 #if 0 //#ifdef MAP_ANONYMOUS
392 pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
394 pg->addr = malloc(CFS_PAGE_SIZE << order);
403 #define cfs_alloc_pages(mask, order) alloc_pages((mask), (order))
405 #define alloc_page(mask) alloc_pages((mask), 0)
406 #define cfs_alloc_page(mask) alloc_page(mask)
408 static inline void __free_pages(cfs_page_t *pg, int what)
410 #if 0 //#ifdef MAP_ANONYMOUS
411 munmap(pg->addr, PAGE_SIZE);
417 #define __cfs_free_pages(pg, order) __free_pages((pg), (order))
419 #define __free_page(page) __free_pages((page), 0)
420 #define free_page(page) __free_page(page)
421 #define __cfs_free_page(page) __cfs_free_pages((page), 0)
423 static inline cfs_page_t* __grab_cache_page(unsigned long index)
425 cfs_page_t *pg = alloc_pages(0, 0);
432 #define grab_cache_page(index) __grab_cache_page(index)
433 #define page_cache_release(page) __free_pages(page, 0)
436 #define do_div(a,b) \
438 unsigned long remainder;\
439 remainder = (a) % (b); \
445 #define ATTR_MODE 0x0001
446 #define ATTR_UID 0x0002
447 #define ATTR_GID 0x0004
448 #define ATTR_SIZE 0x0008
449 #define ATTR_ATIME 0x0010
450 #define ATTR_MTIME 0x0020
451 #define ATTR_CTIME 0x0040
452 #define ATTR_ATIME_SET 0x0080
453 #define ATTR_MTIME_SET 0x0100
454 #define ATTR_FORCE 0x0200 /* Not a change, but a change it */
455 #define ATTR_ATTR_FLAG 0x0400
456 #define ATTR_RAW 0x0800 /* file system, not vfs will massage attrs */
457 #define ATTR_FROM_OPEN 0x1000 /* called from open path, ie O_TRUNC */
458 #define ATTR_CTIME_SET 0x2000
459 #define ATTR_KILL_SUID 0
460 #define ATTR_KILL_SGID 0
463 unsigned int ia_valid;
471 unsigned int ia_attr_flags;
473 #define ll_iattr_struct iattr
475 #define IT_OPEN 0x0001
476 #define IT_CREAT 0x0002
477 #define IT_READDIR 0x0004
478 #define IT_GETATTR 0x0008
479 #define IT_LOOKUP 0x0010
480 #define IT_UNLINK 0x0020
481 #define IT_GETXATTR 0x0040
482 #define IT_EXEC 0x0080
483 #define IT_PIN 0x0100
485 #define IT_FL_LOCKED 0x0001
486 #define IT_FL_FOLLOWED 0x0002 /* set by vfs_follow_link */
488 #define INTENT_MAGIC 0x19620323
490 struct lustre_intent_data {
493 __u64 it_lock_handle;
498 struct lookup_intent {
500 void (*it_op_release)(struct lookup_intent *);
505 struct lustre_intent_data lustre;
509 static inline void intent_init(struct lookup_intent *it, int op, int flags)
511 memset(it, 0, sizeof(*it));
512 it->it_magic = INTENT_MAGIC;
514 it->it_flags = flags;
527 struct rw_semaphore {
536 /* use the macro's argument to avoid unused warnings */
537 #define down(a) do { (void)a; } while (0)
538 #define mutex_down(a) down(a)
539 #define up(a) do { (void)a; } while (0)
540 #define mutex_up(a) up(a)
541 #define down_read(a) do { (void)a; } while (0)
542 #define up_read(a) do { (void)a; } while (0)
543 #define down_write(a) do { (void)a; } while (0)
544 #define up_write(a) do { (void)a; } while (0)
545 #define sema_init(a,b) do { (void)a; } while (0)
546 #define init_rwsem(a) do { (void)a; } while (0)
547 #define DECLARE_MUTEX(name) \
548 struct semaphore name = { 1 }
549 static inline void init_MUTEX (struct semaphore *sem)
553 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
558 #define init_mutex(s) init_MUTEX(s)
561 struct list_head sleepers;
565 struct list_head sleeping;
575 struct signal pending;
586 typedef struct task_struct cfs_task_t;
587 #define cfs_current() current
588 #define cfs_curproc_pid() (current->pid)
589 #define cfs_curproc_comm() (current->comm)
591 extern struct task_struct *current;
592 int in_group_p(gid_t gid);
593 static inline int capable(int cap)
595 if (current->cap_effective & (1 << cap))
601 #define set_current_state(foo) do { current->state = foo; } while (0)
603 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
604 #define add_wait_queue(q,p) do { list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
605 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
606 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
608 #define DECLARE_WAIT_QUEUE_HEAD(HEAD) \
609 wait_queue_head_t HEAD = { \
610 .sleepers = CFS_LIST_HEAD_INIT(HEAD.sleepers) \
612 #define init_waitqueue_head(l) CFS_INIT_LIST_HEAD(&(l)->sleepers)
613 #define wake_up(l) do { int a = 0; a++; } while (0)
614 #define TASK_INTERRUPTIBLE 0
615 #define TASK_UNINTERRUPTIBLE 1
616 #define TASK_RUNNING 2
618 #define wait_event_interruptible(wq, condition) \
620 struct l_wait_info lwi; \
621 int timeout = 100000000;/* for ever */ \
624 lwi = LWI_TIMEOUT(timeout, NULL, NULL); \
625 ret = l_wait_event(NULL, condition, &lwi); \
630 #define in_interrupt() (0)
632 #define schedule() do {} while (0)
633 static inline int schedule_timeout(signed long t)
638 #define lock_kernel() do {} while (0)
639 #define unlock_kernel() do {} while (0)
640 #define daemonize(l) do {} while (0)
641 #define sigfillset(l) do {} while (0)
642 #define recalc_sigpending(l) do {} while (0)
643 #define kernel_thread(l,m,n) LBUG()
645 #define USERMODEHELPER(path, argv, envp) (0)
646 #define SIGNAL_MASK_ASSERT()
649 #include <sys/time.h>
651 #error "liblustre's jiffies currently expects HZ to be 1"
655 unsigned long _ret = 0; \
657 if (gettimeofday(&tv, NULL) == 0) \
661 #define get_jiffies_64() (__u64)jiffies
662 #define time_after(a, b) ((long)(b) - (long)(a) < 0)
663 #define time_before(a, b) time_after(b,a)
664 #define time_after_eq(a,b) ((long)(a) - (long)(b) >= 0)
667 struct list_head tl_list;
668 void (*function)(unsigned long unused);
673 static inline int timer_pending(struct timer_list *l)
675 if (time_after(l->expires, jiffies))
681 static inline int init_timer(struct timer_list *l)
683 CFS_INIT_LIST_HEAD(&l->tl_list);
687 static inline void mod_timer(struct timer_list *l, int thetime)
689 l->expires = thetime;
692 static inline void del_timer(struct timer_list *l)
697 typedef struct { volatile int counter; } atomic_t;
699 #define ATOMIC_INIT(i) { i }
701 #define atomic_read(a) ((a)->counter)
702 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
703 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
704 #define atomic_dec_and_lock(a,b) ((--((a)->counter)) == 0)
705 #define atomic_inc(a) (((a)->counter)++)
706 #define atomic_dec(a) do { (a)->counter--; } while (0)
707 #define atomic_add(b,a) do {(a)->counter += b;} while (0)
708 #define atomic_add_return(n,a) ((a)->counter += n)
709 #define atomic_inc_return(a) atomic_add_return(1,a)
710 #define atomic_sub(b,a) do {(a)->counter -= b;} while (0)
711 #define atomic_sub_return(n,a) ((a)->counter -= n)
712 #define atomic_dec_return(a) atomic_sub_return(1,a)
715 #define likely(exp) (exp)
718 #define unlikely(exp) (exp)
721 /* FIXME sys/capability will finally included linux/fs.h thus
722 * cause numerous trouble on x86-64. as temporary solution for
723 * build broken at cary, we copy definition we need from capability.h
727 typedef struct _cap_struct *cap_t;
728 typedef int cap_value_t;
739 #define CAP_DAC_OVERRIDE 1
740 #define CAP_DAC_READ_SEARCH 2
743 #define CAP_SYS_ADMIN 21
745 cap_t cap_get_proc(void);
746 int cap_get_flag(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *);
748 static inline void libcfs_run_lbug_upcall(char *file, const char *fn,
757 #define COMPLETION_INITIALIZER(work) \
758 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
760 #define DECLARE_COMPLETION(work) \
761 struct completion work = COMPLETION_INITIALIZER(work)
763 #define INIT_COMPLETION(x) ((x).done = 0)
765 static inline void init_completion(struct completion *x)
768 init_waitqueue_head(&x->wait);
771 struct liblustre_wait_callback {
772 struct list_head llwc_list;
773 const char *llwc_name;
774 int (*llwc_fn)(void *arg);
778 void *liblustre_register_wait_callback(const char *name,
779 int (*fn)(void *arg), void *arg);
780 void liblustre_deregister_wait_callback(void *notifier);
781 int liblustre_wait_event(int timeout);
783 void *liblustre_register_idle_callback(const char *name,
784 int (*fn)(void *arg), void *arg);
785 void liblustre_deregister_idle_callback(void *notifier);
786 void liblustre_wait_idle(void);
789 struct nfs_lock_info {
795 typedef struct file_lock {
796 struct file_lock *fl_next; /* singly linked list for this inode */
797 struct list_head fl_link; /* doubly linked list of all locks */
798 struct list_head fl_block; /* circular list of blocked processes */
802 struct file *fl_file;
803 unsigned char fl_flags;
804 unsigned char fl_type;
808 void (*fl_notify)(struct file_lock *); /* unblock callback */
809 void (*fl_insert)(struct file_lock *); /* lock insertion callback */
810 void (*fl_remove)(struct file_lock *); /* lock removal callback */
812 void *fl_fasync; /* for lease break notifications */
813 unsigned long fl_break_time; /* for nonblocking lease breaks */
816 struct nfs_lock_info nfs_fl;
820 #define cfs_flock_type(fl) ((fl)->fl_type)
821 #define cfs_flock_set_type(fl, type) do { (fl)->fl_type = (type); } while(0)
822 #define cfs_flock_pid(fl) ((fl)->fl_pid)
823 #define cfs_flock_set_pid(fl, pid) do { (fl)->fl_pid = (pid); } while(0)
824 #define cfs_flock_start(fl) ((fl)->fl_start)
825 #define cfs_flock_set_start(fl, start) do { (fl)->fl_start = (start); } while(0)
826 #define cfs_flock_end(fl) ((fl)->fl_end)
827 #define cfs_flock_set_end(fl, end) do { (fl)->fl_end = (end); } while(0)
830 #define INT_LIMIT(x) (~((x)1 << (sizeof(x)*8 - 1)))
831 #define OFFSET_MAX INT_LIMIT(loff_t)
834 /* XXX: defined in kernel */
843 struct posix_acl_entry {
845 unsigned short e_perm;
851 unsigned int a_count;
852 struct posix_acl_entry a_entries[0];
863 xattr_acl_entry a_entries[0];
866 static inline size_t xattr_acl_size(int count)
868 return sizeof(xattr_acl_header) + count * sizeof(xattr_acl_entry);
872 struct posix_acl * posix_acl_from_xattr(const void *value, size_t size)
878 int posix_acl_valid(const struct posix_acl *acl)
884 void posix_acl_release(struct posix_acl *acl)
888 #ifdef LIBLUSTRE_POSIX_ACL
889 #ifndef posix_acl_xattr_entry
890 #define posix_acl_xattr_entry xattr_acl_entry
892 #ifndef posix_acl_xattr_header
893 #define posix_acl_xattr_header xattr_acl_header
895 #ifndef posix_acl_xattr_size
896 #define posix_acl_xattr_size(entry) xattr_acl_size(entry)
898 #ifndef CONFIG_FS_POSIX_ACL
899 #define CONFIG_FS_POSIX_ACL 1
904 #define ENOTSUPP ENOTSUP
907 #include <obd_support.h>
908 #include <lustre/lustre_idl.h>
909 #include <lustre_lib.h>
910 #include <lustre_import.h>
911 #include <lustre_export.h>
912 #include <lustre_net.h>