Whamcloud - gitweb
Landing b1_4_quotaoff to b1_4
[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 #include <sys/mman.h>
28 #ifdef HAVE_STDINT_H
29 # include <stdint.h>
30 #endif
31 #ifdef HAVE_ASM_PAGE_H
32 # include <asm/page.h>
33 #endif
34 #ifdef HAVE_SYS_USER_H
35 # include <sys/user.h>
36 #endif
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
39 #else
40 # include "ioctl.h"
41 #endif /* !HAVE_SYS_IOCTL_H */
42
43 #include <stdio.h>
44 #include <sys/ioctl.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <sys/stat.h>
49 #include <sys/vfs.h>
50
51 #include <libcfs/list.h>
52 #include <portals/p30.h>
53 #include <libcfs/kp30.h>
54
55 /* definitions for liblustre */
56
57 #ifdef __CYGWIN__
58
59 #define PAGE_SHIFT 12
60 #define PAGE_SIZE (1UL << PAGE_SHIFT)
61 #define PAGE_MASK (~(PAGE_SIZE-1))
62 #define loff_t long long
63 #define ERESTART 2001
64 typedef unsigned short umode_t;
65
66 #endif
67
68 /* This is because lprocfs_status.h gets included here indirectly.  It would
69  * be much better to just avoid lprocfs being included into liblustre entirely
70  * but that requires more header surgery than I can handle right now.
71  */
72 #ifndef smp_processor_id
73 #define smp_processor_id() 0
74 #endif
75
76 /* always adopt 2.5 definitions */
77 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
78 #define LINUX_VERSION_CODE (2*200+5*10+0)
79
80 static inline void inter_module_put(void *a)
81 {
82         return;
83 }
84
85 extern ptl_handle_ni_t         tcpnal_ni;
86
87 void *inter_module_get(char *arg);
88
89 /* cheats for now */
90
91 struct work_struct {
92         void (*ws_task)(void *arg);
93         void *ws_arg;
94 };
95
96 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
97                                 void *arg)
98 {
99         q->ws_task = t;
100         q->ws_arg = arg;
101         return;
102 }
103
104 static inline void schedule_work(struct work_struct *q)
105 {
106         q->ws_task(q->ws_arg);
107 }
108
109
110 #define strnlen(a,b) strlen(a)
111 static inline void *kmalloc(int size, int prot)
112 {
113         return malloc(size);
114 }
115 #define vmalloc malloc
116 #define vfree free
117 #define kfree(a) free(a)
118 #define GFP_KERNEL 1
119 #define GFP_HIGHUSER 1
120 #define GFP_ATOMIC 1
121 #define GFP_NOFS 1
122 #define IS_ERR(a) (((a) && abs((long)(a)) < 500) ? 1 : 0)
123 #define PTR_ERR(a) ((long)(a))
124 #define ERR_PTR(a) ((void*)((long)(a)))
125
126 typedef struct {
127         void *cwd;
128 }mm_segment_t;
129
130 typedef int (read_proc_t)(char *page, char **start, off_t off,
131                           int count, int *eof, void *data);
132
133 struct file; /* forward ref */
134 typedef int (write_proc_t)(struct file *file, const char *buffer,
135                            unsigned long count, void *data);
136
137 #define NIPQUAD(addr) \
138         ((unsigned char *)&addr)[0], \
139         ((unsigned char *)&addr)[1], \
140         ((unsigned char *)&addr)[2], \
141         ((unsigned char *)&addr)[3]
142
143 #if defined(__LITTLE_ENDIAN)
144 #define HIPQUAD(addr) \
145         ((unsigned char *)&addr)[3], \
146         ((unsigned char *)&addr)[2], \
147         ((unsigned char *)&addr)[1], \
148         ((unsigned char *)&addr)[0]
149 #elif defined(__BIG_ENDIAN)
150 #define HIPQUAD NIPQUAD
151 #else
152 #error "Undefined byteorder??"
153 #endif /* __LITTLE_ENDIAN */
154
155 /* bits ops */
156 static __inline__ int set_bit(int nr,long * addr)
157 {
158         int     mask, retval;
159
160         addr += nr >> 5;
161         mask = 1 << (nr & 0x1f);
162         retval = (mask & *addr) != 0;
163         *addr |= mask;
164         return retval;
165 }
166
167 static __inline__ int clear_bit(int nr, long * addr)
168 {
169         int     mask, retval;
170
171         addr += nr >> 5;
172         mask = 1 << (nr & 0x1f);
173         retval = (mask & *addr) != 0;
174         *addr &= ~mask;
175         return retval;
176 }
177
178 static __inline__ int test_bit(int nr, long * addr)
179 {
180         int     mask;
181
182         addr += nr >> 5;
183         mask = 1 << (nr & 0x1f);
184         return ((mask & *addr) != 0);
185 }
186
187 static __inline__ int ext2_set_bit(int nr, void *addr)
188 {
189         return set_bit(nr, (long*)addr);
190 }
191
192 static __inline__ int ext2_clear_bit(int nr, void *addr)
193 {
194         return clear_bit(nr, (long*)addr);
195 }
196
197 static __inline__ int ext2_test_bit(int nr, void *addr)
198 {
199         return test_bit(nr, (long*)addr);
200 }
201
202 /* modules */
203
204 struct module {
205         int count;
206 };
207
208 static inline void MODULE_AUTHOR(char *name)
209 {
210         printf("%s\n", name);
211 }
212 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
213 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
214
215 #define THIS_MODULE NULL
216 #define __init
217 #define __exit
218
219 /* devices */
220
221 static inline int misc_register(void *foo)
222 {
223         return 0;
224 }
225
226 static inline int misc_deregister(void *foo)
227 {
228         return 0;
229 }
230
231 static inline int request_module(char *name)
232 {
233         return (-EINVAL);
234 }
235
236 #define __MOD_INC_USE_COUNT(m)  do {} while (0)
237 #define __MOD_DEC_USE_COUNT(m)  do {} while (0)
238 #define MOD_INC_USE_COUNT       do {} while (0)
239 #define MOD_DEC_USE_COUNT       do {} while (0)
240 static inline void __module_get(struct module *module)
241 {
242 }
243
244 static inline int try_module_get(struct module *module)
245 {
246         return 1;
247 }
248
249 static inline void module_put(struct module *module)
250 {
251 }
252
253 /* module initialization */
254 extern int init_obdclass(void);
255 extern int ptlrpc_init(void);
256 extern int ldlm_init(void);
257 extern int osc_init(void);
258 extern int lov_init(void);
259 extern int mdc_init(void);
260 extern int echo_client_init(void);
261
262
263
264 /* general stuff */
265
266 #define EXPORT_SYMBOL(S)
267
268 typedef struct { } spinlock_t;
269 typedef __u64 kdev_t;
270
271 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
272 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
273
274 static inline void spin_lock(spinlock_t *l) {return;}
275 static inline void spin_unlock(spinlock_t *l) {return;}
276 static inline void spin_lock_init(spinlock_t *l) {return;}
277 static inline void local_irq_save(unsigned long flag) {return;}
278 static inline void local_irq_restore(unsigned long flag) {return;}
279 static inline int spin_is_locked(spinlock_t *l) {return 1;}
280
281 static inline void spin_lock_bh(spinlock_t *l) {}
282 static inline void spin_unlock_bh(spinlock_t *l) {}
283 static inline void spin_lock_irqsave(spinlock_t *a, unsigned long b) {}
284 static inline void spin_unlock_irqrestore(spinlock_t *a, unsigned long b) {}
285
286 #define min(x,y) ((x)<(y) ? (x) : (y))
287 #define max(x,y) ((x)>(y) ? (x) : (y))
288
289 #ifndef min_t
290 #define min_t(type,x,y) \
291         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
292 #endif
293 #ifndef max_t
294 #define max_t(type,x,y) \
295         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
296 #endif
297
298 /* registering symbols */
299 #ifndef ERESTARTSYS
300 #define ERESTARTSYS ERESTART
301 #endif
302 #define HZ 1
303
304 /* random */
305
306 static inline void get_random_bytes(void *ptr, int size)
307 {
308         int *p = (int *)ptr;
309         int i, count = size/sizeof(int);
310
311         for (i = 0; i< count; i++)
312                 *p++ = rand();
313 }
314
315 /* memory */
316
317 /* FIXME */
318 #define num_physpages (16 * 1024)
319
320 static inline int copy_from_user(void *a,void *b, int c)
321 {
322         memcpy(a,b,c);
323         return 0;
324 }
325
326 static inline int copy_to_user(void *a,void *b, int c)
327 {
328         memcpy(a,b,c);
329         return 0;
330 }
331
332
333 /* slabs */
334 typedef struct {
335          int size;
336 } kmem_cache_t;
337 #define SLAB_HWCACHE_ALIGN 0
338 static inline kmem_cache_t *
339 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
340                   unsigned long d,
341                   void (*e)(void *, kmem_cache_t *, unsigned long),
342                   void (*f)(void *, kmem_cache_t *, unsigned long))
343 {
344         kmem_cache_t *c;
345         c = malloc(sizeof(*c));
346         if (!c)
347                 return NULL;
348         c->size = objsize;
349         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
350                name, c, (int)objsize);
351         return c;
352 };
353
354 static inline int kmem_cache_destroy(kmem_cache_t *a)
355 {
356         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
357         free(a);
358         return 0;
359 }
360 #define kmem_cache_alloc(cache, prio) malloc(cache->size)
361 #define kmem_cache_free(cache, obj) free(obj)
362
363 #define PAGE_CACHE_SIZE  PAGE_SIZE
364 #define PAGE_CACHE_SHIFT PAGE_SHIFT
365 #define PAGE_CACHE_MASK  PAGE_MASK
366
367 /* struct page decl moved out from here into portals/include/libcfs/user-prim.h */
368
369 #define kmap(page) (page)->addr
370 #define kunmap(a) do {} while (0)
371
372 static inline struct page *alloc_pages(int mask, unsigned long order)
373 {
374         struct page *pg = malloc(sizeof(*pg));
375
376         if (!pg)
377                 return NULL;
378 #if 0 //#ifdef MAP_ANONYMOUS
379         pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
380 #else
381         pg->addr = malloc(PAGE_SIZE << order);
382 #endif
383
384         if (!pg->addr) {
385                 free(pg);
386                 return NULL;
387         }
388         return pg;
389 }
390
391 #define alloc_page(mask) alloc_pages((mask), 0)
392
393 static inline void __free_pages(struct page *pg, int what)
394 {
395 #if 0 //#ifdef MAP_ANONYMOUS
396         munmap(pg->addr, PAGE_SIZE);
397 #else
398         free(pg->addr);
399 #endif
400         free(pg);
401 }
402
403 #define __free_page(page) __free_pages((page), 0)
404 #define free_page(page) __free_page(page)
405
406 static inline struct page* __grab_cache_page(unsigned long index)
407 {
408         struct page *pg = alloc_pages(0, 0);
409
410         if (pg)
411                 pg->index = index;
412         return pg;
413 }
414
415 #define grab_cache_page(index) __grab_cache_page(index)
416 #define page_cache_release(page) __free_pages(page, 0)
417
418 /* arithmetic */
419 #define do_div(a,b)                     \
420         ({                              \
421                 unsigned long remainder;\
422                 remainder = (a) % (b);  \
423                 (a) = (a) / (b);        \
424                 (remainder);            \
425         })
426
427 /* VFS stuff */
428 #define ATTR_MODE       0x0001
429 #define ATTR_UID        0x0002
430 #define ATTR_GID        0x0004
431 #define ATTR_SIZE       0x0008
432 #define ATTR_ATIME      0x0010
433 #define ATTR_MTIME      0x0020
434 #define ATTR_CTIME      0x0040
435 #define ATTR_ATIME_SET  0x0080
436 #define ATTR_MTIME_SET  0x0100
437 #define ATTR_FORCE      0x0200  /* Not a change, but a change it */
438 #define ATTR_ATTR_FLAG  0x0400
439 #define ATTR_RAW        0x0800  /* file system, not vfs will massage attrs */
440 #define ATTR_FROM_OPEN  0x1000  /* called from open path, ie O_TRUNC */
441 #define ATTR_CTIME_SET  0x2000
442
443 struct iattr {
444         unsigned int    ia_valid;
445         umode_t         ia_mode;
446         uid_t           ia_uid;
447         gid_t           ia_gid;
448         loff_t          ia_size;
449         time_t          ia_atime;
450         time_t          ia_mtime;
451         time_t          ia_ctime;
452         unsigned int    ia_attr_flags;
453 };
454
455 #define IT_OPEN     0x0001
456 #define IT_CREAT    0x0002
457 #define IT_READDIR  0x0004
458 #define IT_GETATTR  0x0008
459 #define IT_LOOKUP   0x0010
460 #define IT_UNLINK   0x0020
461 #define IT_GETXATTR 0x0040
462 #define IT_EXEC     0x0080
463 #define IT_PIN      0x0100
464
465 #define IT_FL_LOCKED   0x0001
466 #define IT_FL_FOLLOWED 0x0002 /* set by vfs_follow_link */
467
468 #define INTENT_MAGIC 0x19620323
469
470 struct lustre_intent_data {
471         int       it_disposition;
472         int       it_status;
473         __u64     it_lock_handle;
474         void     *it_data;
475         int       it_lock_mode;
476         int it_int_flags;
477 };
478 struct lookup_intent {
479         int     it_magic;
480         void    (*it_op_release)(struct lookup_intent *);
481         int     it_op;
482         int     it_flags;
483         int     it_create_mode;
484         union {
485                 struct lustre_intent_data lustre;
486         } d;
487 };
488
489 static inline void intent_init(struct lookup_intent *it, int op, int flags)
490 {
491         memset(it, 0, sizeof(*it));
492         it->it_magic = INTENT_MAGIC;
493         it->it_op = op;
494         it->it_flags = flags;
495 }
496
497
498 struct dentry {
499         int d_count;
500 };
501
502 struct vfsmount {
503         void *pwd;
504 };
505
506 /* semaphores */
507 struct rw_semaphore {
508         int count;
509 };
510
511 /* semaphores */
512 struct semaphore {
513         int count;
514 };
515
516 /* use the macro's argument to avoid unused warnings */
517 #define down(a) do { (void)a; } while (0)
518 #define up(a) do { (void)a; } while (0)
519 #define down_read(a) do { (void)a; } while (0)
520 #define up_read(a) do { (void)a; } while (0)
521 #define down_write(a) do { (void)a; } while (0)
522 #define up_write(a) do { (void)a; } while (0)
523 #define sema_init(a,b) do { (void)a; } while (0)
524 #define init_rwsem(a) do { (void)a; } while (0)
525 #define DECLARE_MUTEX(name)     \
526         struct semaphore name = { 1 }
527 static inline void init_MUTEX (struct semaphore *sem)
528 {
529         sema_init(sem, 1);
530 }
531
532
533 typedef struct  {
534         struct list_head sleepers;
535 } wait_queue_head_t;
536
537 typedef struct  {
538         struct list_head sleeping;
539         void *process;
540 } wait_queue_t;
541
542 struct signal {
543         int signal;
544 };
545
546 struct fs_struct {
547         int umask;
548 };
549
550 struct task_struct {
551         struct fs_struct *fs;
552         int state;
553         struct signal pending;
554         char comm[32];
555         int pid;
556         int fsuid;
557         int fsgid;
558         __u32 cap_effective;
559 };
560
561 extern struct task_struct *current;
562
563 #define in_group_p(a) 0 /* FIXME */
564
565 #define set_current_state(foo) do { current->state = foo; } while (0)
566
567 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
568 #define add_wait_queue(q,p) do {  list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
569 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
570 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
571
572 #define DECLARE_WAIT_QUEUE_HEAD(HEAD)                           \
573         wait_queue_head_t HEAD = {                              \
574                 .sleepers = LIST_HEAD_INIT(HEAD.sleepers)       \
575         }
576 #define init_waitqueue_head(l) INIT_LIST_HEAD(&(l)->sleepers)
577 #define wake_up(l) do { int a; a++; } while (0)
578 #define TASK_INTERRUPTIBLE 0
579 #define TASK_UNINTERRUPTIBLE 1
580 #define TASK_RUNNING 2
581
582 #define wait_event_interruptible(wq, condition)                         \
583 ({                                                                      \
584         struct l_wait_info lwi;                                         \
585         int timeout = 100000000;/* for ever */                          \
586         int ret;                                                        \
587                                                                         \
588         lwi = LWI_TIMEOUT(timeout, NULL, NULL);                         \
589         ret = l_wait_event(NULL, condition, &lwi);                      \
590                                                                         \
591         ret;                                                            \
592 })
593
594 #define in_interrupt() (0)
595
596 #define schedule() do {} while (0)
597 static inline int schedule_timeout(signed long t)
598 {
599         return 0;
600 }
601
602 #define lock_kernel() do {} while (0)
603 #define daemonize(l) do {} while (0)
604 #define sigfillset(l) do {} while (0)
605 #define recalc_sigpending(l) do {} while (0)
606 #define kernel_thread(l,m,n) LBUG()
607
608 #define USERMODEHELPER(path, argv, envp) (0)
609 #define SIGNAL_MASK_ASSERT()
610 #define KERN_INFO
611
612 #include <sys/time.h>
613 #if HZ != 1
614 #error "liblustre's jiffies currently expects HZ to be 1"
615 #endif
616 #define jiffies                                 \
617 ({                                              \
618         unsigned long _ret = 0;                 \
619         struct timeval tv;                      \
620         if (gettimeofday(&tv, NULL) == 0)       \
621                 _ret = tv.tv_sec;               \
622         _ret;                                   \
623 })
624 #define time_after(a, b) ((long)(b) - (long)(a) < 0)
625 #define time_before(a, b) time_after(b,a)
626 #define time_after_eq(a,b)      ((long)(a) - (long)(b) >= 0)
627
628 struct timer_list {
629         struct list_head tl_list;
630         void (*function)(unsigned long unused);
631         unsigned long data;
632         long expires;
633 };
634
635 static inline int timer_pending(struct timer_list *l)
636 {
637         if (time_after(l->expires, jiffies))
638                 return 1;
639         else
640                 return 0;
641 }
642
643 static inline int init_timer(struct timer_list *l)
644 {
645         INIT_LIST_HEAD(&l->tl_list);
646         return 0;
647 }
648
649 static inline void mod_timer(struct timer_list *l, int thetime)
650 {
651         l->expires = thetime;
652 }
653
654 static inline void del_timer(struct timer_list *l)
655 {
656         free(l);
657 }
658
659 typedef struct { volatile int counter; } atomic_t;
660
661 #define atomic_read(a) ((a)->counter)
662 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
663 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
664 #define atomic_inc(a)  (((a)->counter)++)
665 #define atomic_dec(a)  do { (a)->counter--; } while (0)
666 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
667 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
668
669 #ifndef likely
670 #define likely(exp) (exp)
671 #endif
672 #ifndef unlikely
673 #define unlikely(exp) (exp)
674 #endif
675
676 /* log related */
677 static inline int llog_init_commit_master(void) { return 0; }
678 static inline int llog_cleanup_commit_master(int force) { return 0; }
679 static inline void portals_run_lbug_upcall(char *file, const char *fn,
680                                            const int l){}
681
682 #define LBUG()                                                          \
683         do {                                                            \
684                 printf("!!!LBUG at %s:%d\n", __FILE__, __LINE__);       \
685                 sleep(1000000);                                         \
686         } while (0)
687
688
689
690 /* completion */
691 struct completion {
692         unsigned int done;
693         wait_queue_head_t wait;
694 };
695
696 #define COMPLETION_INITIALIZER(work) \
697         { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
698
699 #define DECLARE_COMPLETION(work) \
700         struct completion work = COMPLETION_INITIALIZER(work)
701
702 #define INIT_COMPLETION(x)      ((x).done = 0)
703
704 static inline void init_completion(struct completion *x)
705 {
706         x->done = 0;
707         init_waitqueue_head(&x->wait);
708 }
709
710 struct liblustre_wait_callback {
711         struct list_head    llwc_list;
712         int               (*llwc_fn)(void *arg);
713         void               *llwc_arg;
714 };
715
716 void *liblustre_register_wait_callback(int (*fn)(void *arg), void *arg);
717 void liblustre_deregister_wait_callback(void *notifier);
718 int liblustre_wait_event(int timeout);
719
720 #include <linux/obd_support.h>
721 #include <linux/lustre_idl.h>
722 #include <linux/lustre_lib.h>
723 #include <linux/lustre_import.h>
724 #include <linux/lustre_export.h>
725 #include <linux/lustre_net.h>
726
727 #endif