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