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