Whamcloud - gitweb
b=13872
[fs/lustre-release.git] / lustre / include / liblustre.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <info@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  * User-space Lustre headers.
22  *
23  */
24 #ifndef LIBLUSTRE_H__
25 #define LIBLUSTRE_H__
26
27 #ifdef __KERNEL__
28 #error Kernel files should not #include <liblustre.h>
29 #else
30 /*
31  * The userspace implementations of linux/spinlock.h vary; we just
32  * include our own for all of them
33  */
34 #define __LINUX_SPINLOCK_H
35 #endif
36
37 #include <sys/mman.h>
38 #ifdef HAVE_STDINT_H
39 # include <stdint.h>
40 #endif
41 #ifdef HAVE_ASM_PAGE_H
42 # include <asm/page.h>
43 #endif
44 #ifdef HAVE_SYS_USER_H
45 # include <sys/user.h>
46 #endif
47 #ifdef HAVE_SYS_IOCTL_H
48 # include <sys/ioctl.h>
49 #endif
50 #ifndef _IOWR
51 # include "ioctl.h"
52 #endif
53
54 #include <stdio.h>
55 #include <sys/ioctl.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <errno.h>
59 #include <sys/stat.h>
60 #ifdef HAVE_SYS_VFS_H
61 # include <sys/vfs.h>
62 #endif
63 #include <unistd.h>
64 #include <fcntl.h>
65 #include <assert.h>
66
67 #include <libcfs/list.h>
68 #include <lnet/lnet.h>
69 #include <libcfs/kp30.h>
70
71 /* definitions for liblustre */
72
73 #ifdef __CYGWIN__
74
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
79 #define ERESTART 2001
80 typedef unsigned short umode_t;
81
82 #endif
83
84 #ifndef CURRENT_SECONDS
85 # define CURRENT_SECONDS time(0)
86 #endif
87
88 #ifndef ARRAY_SIZE
89 #define ARRAY_SIZE(a) ((sizeof (a))/(sizeof ((a)[0])))
90 #endif
91
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.
95  */
96 #ifndef smp_processor_id
97 #define smp_processor_id() 0
98 #endif
99 #ifndef num_online_cpus
100 #define num_online_cpus() 1
101 #endif
102 #ifndef num_possible_cpus
103 #define num_possible_cpus() 1
104 #endif
105
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,6,5)
109
110 #ifndef page_private
111 #define page_private(page) ((page)->private)
112 #define set_page_private(page, v) ((page)->private = (v))
113 #endif
114
115
116 static inline void inter_module_put(void *a)
117 {
118         return;
119 }
120
121 void *inter_module_get(char *arg);
122
123 /* cheats for now */
124
125 struct work_struct {
126         void (*ws_task)(void *arg);
127         void *ws_arg;
128 };
129
130 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
131                                 void *arg)
132 {
133         q->ws_task = t;
134         q->ws_arg = arg;
135         return;
136 }
137
138 static inline void schedule_work(struct work_struct *q)
139 {
140         q->ws_task(q->ws_arg);
141 }
142
143
144 #define strnlen(a,b) strlen(a)
145 static inline void *kmalloc(int size, int prot)
146 {
147         return malloc(size);
148 }
149 #define vmalloc malloc
150 #define vfree free
151 #define kfree(a) free(a)
152 #define GFP_KERNEL 1
153 #define GFP_HIGHUSER 1
154 #define GFP_ATOMIC 1
155 #define GFP_NOFS 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)))
159
160 typedef int (read_proc_t)(char *page, char **start, off_t off,
161                           int count, int *eof, void *data);
162
163 struct file; /* forward ref */
164 typedef int (write_proc_t)(struct file *file, const char *buffer,
165                            unsigned long count, void *data);
166
167 #define NIPQUAD(addr) \
168         ((unsigned char *)&addr)[0], \
169         ((unsigned char *)&addr)[1], \
170         ((unsigned char *)&addr)[2], \
171         ((unsigned char *)&addr)[3]
172
173 #if defined(__LITTLE_ENDIAN)
174 #define HIPQUAD(addr) \
175         ((unsigned char *)&addr)[3], \
176         ((unsigned char *)&addr)[2], \
177         ((unsigned char *)&addr)[1], \
178         ((unsigned char *)&addr)[0]
179 #elif defined(__BIG_ENDIAN)
180 #define HIPQUAD NIPQUAD
181 #else
182 #error "Undefined byteorder??"
183 #endif /* __LITTLE_ENDIAN */
184
185 /* bits ops */
186
187 /* a long can be more than 32 bits, so use BITS_PER_LONG
188  * to allow the compiler to adjust the bit shifting accordingly
189  */
190
191 /* test if bit nr is set in bitmap addr; returns previous value of bit nr */
192 static __inline__ int set_bit(int nr, long * addr)
193 {
194         long    mask;
195
196         addr += nr / BITS_PER_LONG;
197         mask = 1UL << (nr & (BITS_PER_LONG - 1));
198         nr = (mask & *addr) != 0;
199         *addr |= mask;
200         return nr;
201 }
202
203 /* clear bit nr in bitmap addr; returns previous value of bit nr*/
204 static __inline__ int clear_bit(int nr, long * addr)
205 {
206         long    mask;
207
208         addr += nr / BITS_PER_LONG;
209         mask = 1UL << (nr & (BITS_PER_LONG - 1));
210         nr = (mask & *addr) != 0;
211         *addr &= ~mask;
212         return nr;
213 }
214
215 static __inline__ int test_bit(int nr, const long * addr)
216 {
217         return ((1UL << (nr & (BITS_PER_LONG - 1))) & ((addr)[nr / BITS_PER_LONG])) != 0;
218 }
219
220 static __inline__ int ext2_set_bit(int nr, void *addr)
221 {
222         return set_bit(nr, (long*)addr);
223 }
224
225 static __inline__ int ext2_clear_bit(int nr, void *addr)
226 {
227         return clear_bit(nr, (long*)addr);
228 }
229
230 static __inline__ int ext2_test_bit(int nr, void *addr)
231 {
232         return test_bit(nr, (long*)addr);
233 }
234
235 /* modules */
236
237 struct module {
238         int count;
239 };
240
241 static inline void MODULE_AUTHOR(char *name)
242 {
243         printf("%s\n", name);
244 }
245 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
246 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
247
248 #define THIS_MODULE NULL
249 #define __init
250 #define __exit
251
252 /* devices */
253
254 static inline int misc_register(void *foo)
255 {
256         return 0;
257 }
258
259 static inline int misc_deregister(void *foo)
260 {
261         return 0;
262 }
263
264 static inline int request_module(char *name)
265 {
266         return (-EINVAL);
267 }
268
269 #define __MOD_INC_USE_COUNT(m)  do {} while (0)
270 #define __MOD_DEC_USE_COUNT(m)  do {} while (0)
271 #define MOD_INC_USE_COUNT       do {} while (0)
272 #define MOD_DEC_USE_COUNT       do {} while (0)
273 static inline void __module_get(struct module *module)
274 {
275 }
276
277 static inline int try_module_get(struct module *module)
278 {
279         return 1;
280 }
281
282 static inline void module_put(struct module *module)
283 {
284 }
285
286 /* module initialization */
287 extern int init_obdclass(void);
288 extern int ptlrpc_init(void);
289 extern int ldlm_init(void);
290 extern int osc_init(void);
291 extern int lov_init(void);
292 extern int mdc_init(void);
293 extern int lmv_init(void);
294 extern int mgc_init(void);
295 extern int echo_client_init(void);
296
297
298
299 /* general stuff */
300
301 #define EXPORT_SYMBOL(S)
302
303 struct rcu_head { };
304
305 typedef struct { } spinlock_t;
306 typedef __u64 kdev_t;
307
308 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
309 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
310 #define LASSERT_SEM_LOCKED(sem) do {} while(0)
311
312 static inline void spin_lock(spinlock_t *l) {return;}
313 static inline void spin_unlock(spinlock_t *l) {return;}
314 static inline void spin_lock_init(spinlock_t *l) {return;}
315 static inline void local_irq_save(unsigned long flag) {return;}
316 static inline void local_irq_restore(unsigned long flag) {return;}
317 static inline int spin_is_locked(spinlock_t *l) {return 1;}
318
319 static inline void spin_lock_bh(spinlock_t *l) {}
320 static inline void spin_unlock_bh(spinlock_t *l) {}
321 static inline void spin_lock_irqsave(spinlock_t *a, unsigned long b) {}
322 static inline void spin_unlock_irqrestore(spinlock_t *a, unsigned long b) {}
323
324 typedef spinlock_t rwlock_t;
325 #define RW_LOCK_UNLOCKED        SPIN_LOCK_UNLOCKED
326 #define read_lock(l)            spin_lock(l)
327 #define read_unlock(l)          spin_unlock(l)
328 #define write_lock(l)           spin_lock(l)
329 #define write_unlock(l)         spin_unlock(l)
330
331
332 #define min(x,y) ((x)<(y) ? (x) : (y))
333 #define max(x,y) ((x)>(y) ? (x) : (y))
334
335 #ifndef min_t
336 #define min_t(type,x,y) \
337         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
338 #endif
339 #ifndef max_t
340 #define max_t(type,x,y) \
341         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
342 #endif
343
344 #define simple_strtol strtol
345
346 /* registering symbols */
347 #ifndef ERESTARTSYS
348 #define ERESTARTSYS ERESTART
349 #endif
350 #define HZ 1
351
352 /* random */
353
354 void get_random_bytes(void *ptr, int size);
355
356 /* memory */
357
358 /* memory size: used for some client tunables */
359 #define num_physpages (256 * 1024) /* 1GB */
360
361 static inline int copy_from_user(void *a,void *b, int c)
362 {
363         memcpy(a,b,c);
364         return 0;
365 }
366
367 static inline int copy_to_user(void *a,void *b, int c)
368 {
369         memcpy(a,b,c);
370         return 0;
371 }
372
373
374 /* slabs */
375 typedef struct {
376          int size;
377 } kmem_cache_t;
378 #define SLAB_HWCACHE_ALIGN 0
379 static inline kmem_cache_t *
380 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
381                   unsigned long d,
382                   void (*e)(void *, kmem_cache_t *, unsigned long),
383                   void (*f)(void *, kmem_cache_t *, unsigned long))
384 {
385         kmem_cache_t *c;
386         c = malloc(sizeof(*c));
387         if (!c)
388                 return NULL;
389         c->size = objsize;
390         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
391                name, c, (int)objsize);
392         return c;
393 };
394
395 static inline int kmem_cache_destroy(kmem_cache_t *a)
396 {
397         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
398         free(a);
399         return 0;
400 }
401
402 /* struct page decl moved out from here into portals/include/libcfs/user-prim.h */
403
404 /* 2.4 defines */
405 #define PAGE_LIST_ENTRY list
406 #define PAGE_LIST(page) ((page)->list)
407
408 #define kmap(page) (page)->addr
409 #define kunmap(a) do {} while (0)
410
411 static inline cfs_page_t *alloc_pages(int mask, unsigned long order)
412 {
413         cfs_page_t *pg = malloc(sizeof(*pg));
414
415         if (!pg)
416                 return NULL;
417 #if 0 //#ifdef MAP_ANONYMOUS
418         pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
419 #else
420         pg->addr = malloc(CFS_PAGE_SIZE << order);
421 #endif
422
423         if (!pg->addr) {
424                 free(pg);
425                 return NULL;
426         }
427         return pg;
428 }
429 #define cfs_alloc_pages(mask, order)  alloc_pages((mask), (order))
430
431 #define alloc_page(mask)      alloc_pages((mask), 0)
432 #define cfs_alloc_page(mask)  alloc_page(mask)
433
434 static inline void __free_pages(cfs_page_t *pg, int what)
435 {
436 #if 0 //#ifdef MAP_ANONYMOUS
437         munmap(pg->addr, PAGE_SIZE);
438 #else
439         free(pg->addr);
440 #endif
441         free(pg);
442 }
443 #define __cfs_free_pages(pg, order)  __free_pages((pg), (order))
444
445 #define __free_page(page) __free_pages((page), 0)
446 #define free_page(page) __free_page(page)
447 #define __cfs_free_page(page)  __cfs_free_pages((page), 0)
448
449 static inline cfs_page_t* __grab_cache_page(unsigned long index)
450 {
451         cfs_page_t *pg = alloc_pages(0, 0);
452
453         if (pg)
454                 pg->index = index;
455         return pg;
456 }
457
458 #define grab_cache_page(index) __grab_cache_page(index)
459 #define page_cache_release(page) __free_pages(page, 0)
460
461 /* arithmetic */
462 #define do_div(a,b)                     \
463         ({                              \
464                 unsigned long remainder;\
465                 remainder = (a) % (b);  \
466                 (a) = (a) / (b);        \
467                 (remainder);            \
468         })
469
470 /* VFS stuff */
471 #define ATTR_MODE       0x0001
472 #define ATTR_UID        0x0002
473 #define ATTR_GID        0x0004
474 #define ATTR_SIZE       0x0008
475 #define ATTR_ATIME      0x0010
476 #define ATTR_MTIME      0x0020
477 #define ATTR_CTIME      0x0040
478 #define ATTR_ATIME_SET  0x0080
479 #define ATTR_MTIME_SET  0x0100
480 #define ATTR_FORCE      0x0200  /* Not a change, but a change it */
481 #define ATTR_ATTR_FLAG  0x0400
482 #define ATTR_RAW        0x0800  /* file system, not vfs will massage attrs */
483 #define ATTR_FROM_OPEN  0x1000  /* called from open path, ie O_TRUNC */
484 #define ATTR_CTIME_SET  0x2000
485 #define ATTR_BLOCKS     0x4000
486 #define ATTR_KILL_SUID  0
487 #define ATTR_KILL_SGID  0
488
489 struct iattr {
490         unsigned int    ia_valid;
491         umode_t         ia_mode;
492         uid_t           ia_uid;
493         gid_t           ia_gid;
494         loff_t          ia_size;
495         time_t          ia_atime;
496         time_t          ia_mtime;
497         time_t          ia_ctime;
498         unsigned int    ia_attr_flags;
499 };
500
501 #define ll_iattr iattr
502
503 #define IT_OPEN     0x0001
504 #define IT_CREAT    0x0002
505 #define IT_READDIR  0x0004
506 #define IT_GETATTR  0x0008
507 #define IT_LOOKUP   0x0010
508 #define IT_UNLINK   0x0020
509 #define IT_GETXATTR 0x0040
510 #define IT_EXEC     0x0080
511 #define IT_PIN      0x0100
512
513 #define IT_FL_LOCKED   0x0001
514 #define IT_FL_FOLLOWED 0x0002 /* set by vfs_follow_link */
515
516 #define INTENT_MAGIC 0x19620323
517
518 struct lustre_intent_data {
519         int       it_disposition;
520         int       it_status;
521         __u64     it_lock_handle;
522         void     *it_data;
523         int       it_lock_mode;
524         int it_int_flags;
525 };
526 struct lookup_intent {
527         int     it_magic;
528         void    (*it_op_release)(struct lookup_intent *);
529         int     it_op;
530         int     it_flags;
531         int     it_create_mode;
532         union {
533                 struct lustre_intent_data lustre;
534         } d;
535 };
536
537 static inline void intent_init(struct lookup_intent *it, int op, int flags)
538 {
539         memset(it, 0, sizeof(*it));
540         it->it_magic = INTENT_MAGIC;
541         it->it_op = op;
542         it->it_flags = flags;
543 }
544
545 struct dentry {
546         int d_count;
547 };
548
549 struct vfsmount {
550         void *pwd;
551 };
552
553 /* semaphores */
554 struct rw_semaphore {
555         int count;
556 };
557
558 /* semaphores */
559 struct semaphore {
560         int count;
561 };
562
563 /* use the macro's argument to avoid unused warnings */
564 #define down(a) do { (void)a; } while (0)
565 #define mutex_down(a)   down(a)
566 #define up(a) do { (void)a; } while (0)
567 #define mutex_up(a)     up(a)
568 #define down_read(a) do { (void)a; } while (0)
569 #define up_read(a) do { (void)a; } while (0)
570 #define down_write(a) do { (void)a; } while (0)
571 #define up_write(a) do { (void)a; } while (0)
572 #define sema_init(a,b) do { (void)a; } while (0)
573 #define init_rwsem(a) do { (void)a; } while (0)
574 #define DECLARE_MUTEX(name)     \
575         struct semaphore name = { 1 }
576 static inline void init_MUTEX (struct semaphore *sem)
577 {
578         sema_init(sem, 1);
579 }
580 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
581 {
582         sema_init(sem, 0);
583 }
584
585 #define init_mutex(s)   init_MUTEX(s)
586
587 typedef struct  {
588         struct list_head sleepers;
589 } wait_queue_head_t;
590
591 typedef struct  {
592         struct list_head sleeping;
593         void *process;
594 } wait_queue_t;
595
596 struct signal {
597         int signal;
598 };
599
600 struct task_struct {
601         int state;
602         struct signal pending;
603         char comm[32];
604         int uid;
605         int gid;
606         int pid;
607         int fsuid;
608         int fsgid;
609         int max_groups;
610         int ngroups;
611         gid_t *groups;
612         __u32 cap_effective;
613 };
614
615 typedef struct task_struct cfs_task_t;
616 #define cfs_current()           current
617 #define cfs_curproc_pid()       (current->pid)
618 #define cfs_curproc_comm()      (current->comm)
619
620 extern struct task_struct *current;
621 int in_group_p(gid_t gid);
622 static inline int capable(int cap)
623 {
624         if (current->cap_effective & (1 << cap))
625                 return 1;
626         else
627                 return 0;
628 }
629
630 #define set_current_state(foo) do { current->state = foo; } while (0)
631
632 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
633 #define add_wait_queue(q,p) do {  list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
634 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
635 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
636
637 #define DECLARE_WAIT_QUEUE_HEAD(HEAD)                           \
638         wait_queue_head_t HEAD = {                              \
639                 .sleepers = LIST_HEAD_INIT(HEAD.sleepers)       \
640         }
641 #define init_waitqueue_head(l) INIT_LIST_HEAD(&(l)->sleepers)
642 #define wake_up(l) do { int a; a++; } while (0)
643 #define TASK_INTERRUPTIBLE 0
644 #define TASK_UNINTERRUPTIBLE 1
645 #define TASK_RUNNING 2
646
647 #define wait_event_interruptible(wq, condition)                         \
648 ({                                                                      \
649         struct l_wait_info lwi;                                         \
650         int timeout = 100000000;/* for ever */                          \
651         int ret;                                                        \
652                                                                         \
653         lwi = LWI_TIMEOUT(timeout, NULL, NULL);                         \
654         ret = l_wait_event(NULL, condition, &lwi);                      \
655                                                                         \
656         ret;                                                            \
657 })
658
659 #define in_interrupt() (0)
660
661 #define schedule() do {} while (0)
662 static inline int schedule_timeout(signed long t)
663 {
664         return 0;
665 }
666
667 #define lock_kernel() do {} while (0)
668 #define unlock_kernel() do {} while (0)
669 #define daemonize(l) do {} while (0)
670 #define sigfillset(l) do {} while (0)
671 #define recalc_sigpending(l) do {} while (0)
672 #define kernel_thread(l,m,n) LBUG()
673
674 #define USERMODEHELPER(path, argv, envp) (0)
675 #define SIGNAL_MASK_ASSERT()
676 #define KERN_INFO
677
678 #include <sys/time.h>
679 #if HZ != 1
680 #error "liblustre's jiffies currently expects HZ to be 1"
681 #endif
682 #define jiffies                                 \
683 ({                                              \
684         unsigned long _ret = 0;                 \
685         struct timeval tv;                      \
686         if (gettimeofday(&tv, NULL) == 0)       \
687                 _ret = tv.tv_sec;               \
688         _ret;                                   \
689 })
690 #define get_jiffies_64()  (__u64)jiffies
691 #define time_after(a, b) ((long)(b) - (long)(a) < 0)
692 #define time_before(a, b) time_after(b,a)
693 #define time_after_eq(a,b)      ((long)(a) - (long)(b) >= 0)
694
695 struct timer_list {
696         struct list_head tl_list;
697         void (*function)(unsigned long unused);
698         unsigned long data;
699         long expires;
700 };
701
702 static inline int timer_pending(struct timer_list *l)
703 {
704         if (time_after(l->expires, jiffies))
705                 return 1;
706         else
707                 return 0;
708 }
709
710 static inline int init_timer(struct timer_list *l)
711 {
712         INIT_LIST_HEAD(&l->tl_list);
713         return 0;
714 }
715
716 static inline void mod_timer(struct timer_list *l, int thetime)
717 {
718         l->expires = thetime;
719 }
720
721 static inline void del_timer(struct timer_list *l)
722 {
723         free(l);
724 }
725
726 typedef struct { volatile int counter; } atomic_t;
727
728 #define ATOMIC_INIT(i) { (i) }
729 #define atomic_read(a) ((a)->counter)
730 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
731 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
732 #define atomic_dec_and_lock(a,b) ((--((a)->counter)) == 0)
733 #define atomic_inc(a)  (((a)->counter)++)
734 #define atomic_dec(a)  do { (a)->counter--; } while (0)
735 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
736 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
737
738 #ifndef likely
739 #define likely(exp) (exp)
740 #endif
741 #ifndef unlikely
742 #define unlikely(exp) (exp)
743 #endif
744
745 #define might_sleep()
746 #define might_sleep_if(c)
747 #define smp_mb()
748
749 static inline
750 int test_and_set_bit(int nr, unsigned long *addr)
751 {
752         int oldbit;
753
754         while (nr >= sizeof(long)) {
755                 nr -= sizeof(long);
756                 addr++;
757         }
758
759         oldbit = (*addr) & (1 << nr);
760         *addr |= (1 << nr);
761         return oldbit;
762 }
763
764 static inline
765 int test_and_clear_bit(int nr, unsigned long *addr)
766 {
767         int oldbit;
768
769         while (nr >= sizeof(long)) {
770                 nr -= sizeof(long);
771                 addr++;
772         }
773
774         oldbit = (*addr) & (1 << nr);
775         *addr &= ~(1 << nr);
776         return oldbit;
777 }
778
779 /* FIXME sys/capability will finally included linux/fs.h thus
780  * cause numerous trouble on x86-64. as temporary solution for
781  * build broken at Cray, we copy definition we need from capability.h
782  * FIXME
783  */
784 struct _cap_struct;
785 typedef struct _cap_struct *cap_t;
786 typedef int cap_value_t;
787 typedef enum {
788     CAP_EFFECTIVE=0,
789     CAP_PERMITTED=1,
790     CAP_INHERITABLE=2
791 } cap_flag_t;
792 typedef enum {
793     CAP_CLEAR=0,
794     CAP_SET=1
795 } cap_flag_value_t;
796
797 #define CAP_DAC_OVERRIDE        1
798 #define CAP_DAC_READ_SEARCH     2
799 #define CAP_FOWNER              3
800 #define CAP_FSETID              4
801 #define CAP_SYS_ADMIN          21
802
803 cap_t   cap_get_proc(void);
804 int     cap_get_flag(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *);
805
806 /* log related */
807 static inline int llog_init_commit_master(void) { return 0; }
808 static inline int llog_cleanup_commit_master(int force) { return 0; }
809 static inline void libcfs_run_lbug_upcall(char *file, const char *fn,
810                                            const int l){}
811
812 /* completion */
813 struct completion {
814         unsigned int done;
815         cfs_waitq_t wait;
816 };
817
818 #define COMPLETION_INITIALIZER(work) \
819         { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
820
821 #define DECLARE_COMPLETION(work) \
822         struct completion work = COMPLETION_INITIALIZER(work)
823
824 #define INIT_COMPLETION(x)      ((x).done = 0)
825
826 static inline void init_completion(struct completion *x)
827 {
828         x->done = 0;
829         init_waitqueue_head(&x->wait);
830 }
831
832 struct liblustre_wait_callback {
833         struct list_head    llwc_list;
834         const char         *llwc_name;
835         int               (*llwc_fn)(void *arg);
836         void               *llwc_arg;
837 };
838
839 void *liblustre_register_wait_callback(const char *name,
840                                        int (*fn)(void *arg), void *arg);
841 void liblustre_deregister_wait_callback(void *notifier);
842 int liblustre_wait_event(int timeout);
843
844 void *liblustre_register_idle_callback(const char *name, 
845                                        int (*fn)(void *arg), void *arg);
846 void liblustre_deregister_idle_callback(void *notifier);
847 void liblustre_wait_idle(void);
848
849 /* flock related */
850 struct nfs_lock_info {
851         __u32             state;
852         __u32             flags;
853         void            *host;
854 };
855
856 typedef struct file_lock {
857         struct file_lock *fl_next;      /* singly linked list for this inode  */
858         struct list_head fl_link;       /* doubly linked list of all locks */
859         struct list_head fl_block;      /* circular list of blocked processes */
860         void *fl_owner;
861         unsigned int fl_pid;
862         cfs_waitq_t fl_wait;
863         struct file *fl_file;
864         unsigned char fl_flags;
865         unsigned char fl_type;
866         loff_t fl_start;
867         loff_t fl_end;
868
869         void (*fl_notify)(struct file_lock *);  /* unblock callback */
870         void (*fl_insert)(struct file_lock *);  /* lock insertion callback */
871         void (*fl_remove)(struct file_lock *);  /* lock removal callback */
872
873         void *fl_fasync; /* for lease break notifications */
874         unsigned long fl_break_time;    /* for nonblocking lease breaks */
875
876         union {
877                 struct nfs_lock_info    nfs_fl;
878         } fl_u;
879 } cfs_flock_t;
880
881 #define cfs_flock_type(fl)                  ((fl)->fl_type)
882 #define cfs_flock_set_type(fl, type)        do { (fl)->fl_type = (type); } while(0)
883 #define cfs_flock_pid(fl)                   ((fl)->fl_pid)
884 #define cfs_flock_set_pid(fl, pid)          do { (fl)->fl_pid = (pid); } while(0)
885 #define cfs_flock_start(fl)                 ((fl)->fl_start)
886 #define cfs_flock_set_start(fl, start)      do { (fl)->fl_start = (start); } while(0)
887 #define cfs_flock_end(fl)                   ((fl)->fl_end)
888 #define cfs_flock_set_end(fl, end)          do { (fl)->fl_end = (end); } while(0)
889
890 #ifndef OFFSET_MAX
891 #define INT_LIMIT(x)    (~((x)1 << (sizeof(x)*8 - 1)))
892 #define OFFSET_MAX      INT_LIMIT(loff_t)
893 #endif
894
895 /* XXX: defined in kernel */
896 #define FL_POSIX        1
897 #define FL_SLEEP        128
898
899 /* quota */
900 #define QUOTA_OK 0
901 #define NO_QUOTA 1
902
903 /* ACL */
904 struct posix_acl_entry {
905         short                   e_tag;
906         unsigned short          e_perm;
907         unsigned int            e_id;
908 };
909
910 struct posix_acl {
911         atomic_t                a_refcount;
912         unsigned int            a_count;
913         struct posix_acl_entry  a_entries[0];
914 };
915
916 typedef struct {
917         __u16           e_tag;
918         __u16           e_perm;
919         __u32           e_id;
920 } xattr_acl_entry;
921
922 typedef struct {
923         __u32           a_version;
924         xattr_acl_entry a_entries[0];
925 } xattr_acl_header;
926
927 static inline size_t xattr_acl_size(int count)
928 {
929         return sizeof(xattr_acl_header) + count * sizeof(xattr_acl_entry);
930 }
931
932 static inline
933 struct posix_acl * posix_acl_from_xattr(const void *value, size_t size)
934 {
935         return NULL;
936 }
937
938 static inline
939 int posix_acl_valid(const struct posix_acl *acl)
940 {
941         return 0;
942 }
943
944 static inline
945 void posix_acl_release(struct posix_acl *acl)
946 {
947 }
948
949 #ifndef ENOTSUPP
950 #define ENOTSUPP ENOTSUP
951 #endif
952
953 typedef int mm_segment_t;
954 enum {
955         KERNEL_DS,
956         USER_DS
957 };
958 static inline mm_segment_t get_fs(void)
959 {
960         return USER_DS;
961 }
962
963 static inline void set_fs(mm_segment_t seg)
964 {
965 }
966
967 #include <obd_support.h>
968 #include <lustre/lustre_idl.h>
969 #include <lustre_lib.h>
970 #include <lustre_import.h>
971 #include <lustre_export.h>
972 #include <lustre_net.h>
973
974 /* Fast hashing routine for a long.
975    (C) 2002 William Lee Irwin III, IBM */
976
977 /*
978  * Knuth recommends primes in approximately golden ratio to the maximum
979  * integer representable by a machine word for multiplicative hashing.
980  * Chuck Lever verified the effectiveness of this technique:
981  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
982  *
983  * These primes are chosen to be bit-sparse, that is operations on
984  * them can use shifts and additions instead of multiplications for
985  * machines where multiplications are slow.
986  */
987 #if BITS_PER_LONG == 32
988 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
989 #define GOLDEN_RATIO_PRIME 0x9e370001UL
990 #elif BITS_PER_LONG == 64
991 /*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
992 #define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
993 #else
994 #error Define GOLDEN_RATIO_PRIME for your wordsize.
995 #endif
996
997 static inline unsigned long hash_long(unsigned long val, unsigned int bits)
998 {
999         unsigned long hash = val;
1000
1001 #if BITS_PER_LONG == 64
1002         /*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
1003         unsigned long n = hash;
1004         n <<= 18;
1005         hash -= n;
1006         n <<= 33;
1007         hash -= n;
1008         n <<= 3;
1009         hash += n;
1010         n <<= 3;
1011         hash -= n;
1012         n <<= 4;
1013         hash += n;
1014         n <<= 2;
1015         hash += n;
1016 #else
1017         /* On some cpus multiply is faster, on others gcc will do shifts */
1018         hash *= GOLDEN_RATIO_PRIME;
1019 #endif
1020
1021         /* High bits are more random, so use them. */
1022         return hash >> (BITS_PER_LONG - bits);
1023 }
1024         
1025 static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
1026 {
1027         return hash_long((unsigned long)ptr, bits);
1028 }
1029
1030 #endif