Whamcloud - gitweb
Update snapfs: 1) some fix on clonefs read in lustre
[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 #include <asm/byteorder.h>
29 #ifndef  __CYGWIN__
30 #include <stdint.h>
31 #include <asm/page.h>
32 #else
33 #include <sys/types.h>
34 #include "ioctl.h"
35 #endif
36 #include <stdio.h>
37 #include <sys/ioctl.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/stat.h>
42 #include <sys/vfs.h>
43
44 #include <portals/list.h>
45 #include <portals/p30.h>
46 #include <linux/kp30.h>
47
48 /* definitions for liblustre */
49
50 #ifdef __CYGWIN__
51
52 #define PAGE_SHIFT 12
53 #define PAGE_SIZE (1UL << PAGE_SHIFT)
54 #define PAGE_MASK (~(PAGE_SIZE-1))
55 #define loff_t long long
56 #define ERESTART 2001
57 typedef unsigned short umode_t;
58
59 #endif
60
61 /* This is because lprocfs_status.h gets included here indirectly.  It would
62  * be much better to just avoid lprocfs being included into liblustre entirely
63  * but that requires more header surgery than I can handle right now.
64  */
65 #ifndef smp_processor_id
66 #define smp_processor_id() 0
67 #endif
68
69 /* always adopt 2.5 definitions */
70 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
71 #define LINUX_VERSION_CODE (2*200+5*10+0)
72
73 static inline void inter_module_put(void *a)
74 {
75         return;
76 }
77
78 extern ptl_handle_ni_t         tcpnal_ni;
79
80 void *inter_module_get(char *arg);
81
82 /* cheats for now */
83
84 struct work_struct {
85         void (*ws_task)(void *arg);
86         void *ws_arg;
87 };
88
89 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
90                                 void *arg)
91 {
92         q->ws_task = t;
93         q->ws_arg = arg;
94         return;
95 }
96
97 static inline void schedule_work(struct work_struct *q)
98 {
99         q->ws_task(q->ws_arg);
100 }
101
102
103 #define strnlen(a,b) strlen(a)
104 static inline void *kmalloc(int size, int prot)
105 {
106         return malloc(size);
107 }
108 #define vmalloc malloc
109 #define vfree free
110 #define kfree(a) free(a)
111 #define GFP_KERNEL 1
112 #define GFP_HIGHUSER 1
113 #define GFP_ATOMIC 1
114 #define GFP_NOFS 1
115 #define IS_ERR(a) (((a) && abs((long)(a)) < 500) ? 1 : 0)
116 #define PTR_ERR(a) ((long)(a))
117 #define ERR_PTR(a) ((void*)((long)(a)))
118
119 typedef struct {
120         void *cwd;
121 }mm_segment_t;
122
123 typedef int (read_proc_t)(char *page, char **start, off_t off,
124                           int count, int *eof, void *data);
125
126 struct file; /* forward ref */
127 typedef int (write_proc_t)(struct file *file, const char *buffer,
128                            unsigned long count, void *data);
129
130 # define le16_to_cpu(x) __le16_to_cpu(x)
131 # define cpu_to_le16(x) __cpu_to_le16(x)
132 # define le32_to_cpu(x) __le32_to_cpu(x)
133 # define cpu_to_le32(x) __cpu_to_le32(x)
134 # define le64_to_cpu(x) __le64_to_cpu(x)
135 # define cpu_to_le64(x) __cpu_to_le64(x)
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
300 #define ERESTARTSYS ERESTART
301 #define HZ 1
302
303 /* random */
304
305 void get_random_bytes(void *ptr, int size);
306
307 /* memory */
308
309 /* FIXME */
310 #define num_physpages (16 * 1024)
311
312 static inline int copy_from_user(void *a,void *b, int c)
313 {
314         memcpy(a,b,c);
315         return 0;
316 }
317
318 static inline int copy_to_user(void *a,void *b, int c)
319 {
320         memcpy(a,b,c);
321         return 0;
322 }
323
324
325 /* slabs */
326 typedef struct {
327          int size;
328 } kmem_cache_t;
329 #define SLAB_HWCACHE_ALIGN 0
330 static inline kmem_cache_t *
331 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
332                   unsigned long d,
333                   void (*e)(void *, kmem_cache_t *, unsigned long),
334                   void (*f)(void *, kmem_cache_t *, unsigned long))
335 {
336         kmem_cache_t *c;
337         c = malloc(sizeof(*c));
338         if (!c)
339                 return NULL;
340         c->size = objsize;
341         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
342                name, c, (int)objsize);
343         return c;
344 };
345
346 static inline int kmem_cache_destroy(kmem_cache_t *a)
347 {
348         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
349         free(a);
350         return 0;
351 }
352 #define kmem_cache_alloc(cache, prio) malloc(cache->size)
353 #define kmem_cache_free(cache, obj) free(obj)
354
355 #define PAGE_CACHE_SIZE PAGE_SIZE
356 #define PAGE_CACHE_SHIFT 12
357 #define PAGE_CACHE_MASK PAGE_MASK
358
359 struct page {
360         void   *addr;
361         unsigned long index;
362         struct list_head list;
363         unsigned long private;
364
365         /* internally used by liblustre file i/o */
366         int     _offset;
367         int     _count;
368 };
369
370 /* 2.4 defines */
371 #define PAGE_LIST_ENTRY list
372 #define PAGE_LIST(page) ((page)->list)
373
374 #define kmap(page) (page)->addr
375 #define kunmap(a) do {} while (0)
376
377 static inline struct page *alloc_pages(int mask, unsigned long order)
378 {
379         struct page *pg = malloc(sizeof(*pg));
380
381         if (!pg)
382                 return NULL;
383 #if 0 //#ifdef MAP_ANONYMOUS
384         pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
385 #else
386         pg->addr = malloc(PAGE_SIZE << order);
387 #endif
388
389         if (!pg->addr) {
390                 free(pg);
391                 return NULL;
392         }
393         return pg;
394 }
395
396 #define alloc_page(mask) alloc_pages((mask), 0)
397
398 static inline void __free_pages(struct page *pg, int what)
399 {
400 #if 0 //#ifdef MAP_ANONYMOUS
401         munmap(pg->addr, PAGE_SIZE);
402 #else
403         free(pg->addr);
404 #endif
405         free(pg);
406 }
407
408 #define __free_page(page) __free_pages((page), 0)
409 #define free_page(page) __free_page(page)
410
411 static inline struct page* __grab_cache_page(unsigned long index)
412 {
413         struct page *pg = alloc_pages(0, 0);
414
415         if (pg)
416                 pg->index = index;
417         return pg;
418 }
419
420 #define grab_cache_page(index) __grab_cache_page(index)
421 #define page_cache_release(page) __free_pages(page, 0)
422
423 /* arithmetic */
424 #define do_div(a,b)                     \
425         ({                              \
426                 unsigned long remainder;\
427                 remainder = (a) % (b);  \
428                 (a) = (a) / (b);        \
429                 (remainder);            \
430         })
431
432 /* VFS stuff */
433 #define ATTR_MODE       0x0001
434 #define ATTR_UID        0x0002
435 #define ATTR_GID        0x0004
436 #define ATTR_SIZE       0x0008
437 #define ATTR_ATIME      0x0010
438 #define ATTR_MTIME      0x0020
439 #define ATTR_CTIME      0x0040
440 #define ATTR_ATIME_SET  0x0080
441 #define ATTR_MTIME_SET  0x0100
442 #define ATTR_FORCE      0x0200  /* Not a change, but a change it */
443 #define ATTR_ATTR_FLAG  0x0400
444 #define ATTR_RAW        0x0800  /* file system, not vfs will massage attrs */
445 #define ATTR_FROM_OPEN  0x1000  /* called from open path, ie O_TRUNC */
446 #define ATTR_CTIME_SET  0x2000
447
448 struct iattr {
449         unsigned int    ia_valid;
450         umode_t         ia_mode;
451         uid_t           ia_uid;
452         gid_t           ia_gid;
453         loff_t          ia_size;
454         time_t          ia_atime;
455         time_t          ia_mtime;
456         time_t          ia_ctime;
457         unsigned int    ia_attr_flags;
458 };
459
460 #define IT_OPEN     0x0001
461 #define IT_CREAT    0x0002
462 #define IT_READDIR  0x0004
463 #define IT_GETATTR  0x0008
464 #define IT_LOOKUP   0x0010
465 #define IT_UNLINK   0x0020
466 #define IT_GETXATTR 0x0040
467 #define IT_EXEC     0x0080
468 #define IT_PIN      0x0100
469 #define IT_CHDIR    0x0200
470
471 #define IT_FL_LOCKED   0x0001
472 #define IT_FL_FOLLOWED 0x0002 /* set by vfs_follow_link */
473
474 #define INTENT_MAGIC 0x19620323
475
476 struct lustre_intent_data {
477         int       it_disposition;
478         int       it_status;
479         __u64     it_lock_handle;
480         void     *it_data;
481         int       it_lock_mode;
482         int it_int_flags;
483 };
484 struct lookup_intent {
485         int     it_magic;
486         void    (*it_op_release)(struct lookup_intent *);
487         int     it_op;
488         int     it_flags;
489         int     it_create_mode;
490         union {
491                 struct lustre_intent_data lustre;
492         } d;
493 };
494
495 static inline void intent_init(struct lookup_intent *it, int op, int flags)
496 {
497         memset(it, 0, sizeof(*it));
498         it->it_magic = INTENT_MAGIC;
499         it->it_op = op;
500         it->it_flags = flags;
501 }
502
503
504 struct dentry {
505         int d_count;
506 };
507
508 struct vfsmount {
509         void *pwd;
510 };
511
512 /* semaphores */
513 struct rw_semaphore {
514         int count;
515 };
516
517 /* semaphores */
518 struct semaphore {
519         int count;
520 };
521
522 /* use the macro's argument to avoid unused warnings */
523 #define down(a) do { (void)a; } while (0)
524 #define up(a) do { (void)a; } while (0)
525 #define down_read(a) do { (void)a; } while (0)
526 #define up_read(a) do { (void)a; } while (0)
527 #define down_write(a) do { (void)a; } while (0)
528 #define up_write(a) do { (void)a; } while (0)
529 #define sema_init(a,b) do { (void)a; } while (0)
530 #define init_rwsem(a) do { (void)a; } while (0)
531 #define DECLARE_MUTEX(name)     \
532         struct semaphore name = { 1 }
533 static inline void init_MUTEX (struct semaphore *sem)
534 {
535         sema_init(sem, 1);
536 }
537
538
539 typedef struct  {
540         struct list_head sleepers;
541 } wait_queue_head_t;
542
543 typedef struct  {
544         struct list_head sleeping;
545         void *process;
546 } wait_queue_t;
547
548 struct signal {
549         int signal;
550 };
551
552 struct fs_struct {
553         int umask;
554 };
555
556 struct task_struct {
557         struct fs_struct *fs;
558         int state;
559         struct signal pending;
560         char comm[32];
561         int pid;
562         int fsuid;
563         int fsgid;
564         int max_groups;
565         int ngroups;
566         gid_t *groups;
567         __u32 cap_effective;
568
569         struct fs_struct __fs;
570 };
571
572 extern struct task_struct *current;
573 int in_group_p(gid_t gid);
574 static inline int capable(int cap)
575 {
576         if (current->cap_effective & (1 << cap))
577                 return 1;
578         else
579                 return 0;
580 }
581
582 #define set_current_state(foo) do { current->state = foo; } while (0)
583
584 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
585 #define add_wait_queue(q,p) do {  list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
586 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
587 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
588
589 #define DECLARE_WAIT_QUEUE_HEAD(HEAD)                           \
590         wait_queue_head_t HEAD = {                              \
591                 .sleepers = LIST_HEAD_INIT(HEAD.sleepers)       \
592         }
593 #define init_waitqueue_head(l) INIT_LIST_HEAD(&(l)->sleepers)
594 #define wake_up(l) do { int a; a++; } while (0)
595 #define TASK_INTERRUPTIBLE 0
596 #define TASK_UNINTERRUPTIBLE 1
597 #define TASK_RUNNING 2
598
599 #define wait_event_interruptible(wq, condition)                         \
600 ({                                                                      \
601         struct l_wait_info lwi;                                         \
602         int timeout = 100000000;/* for ever */                          \
603         int ret;                                                        \
604                                                                         \
605         lwi = LWI_TIMEOUT(timeout, NULL, NULL);                         \
606         ret = l_wait_event(NULL, condition, &lwi);                      \
607                                                                         \
608         ret;                                                            \
609 })
610
611 #define in_interrupt() (0)
612
613 #define schedule() do {} while (0)
614 static inline int schedule_timeout(signed long t)
615 {
616         return 0;
617 }
618
619 #define lock_kernel() do {} while (0)
620 #define daemonize(l) do {} while (0)
621 #define sigfillset(l) do {} while (0)
622 #define recalc_sigpending(l) do {} while (0)
623 #define kernel_thread(l,m,n) LBUG()
624
625 #define USERMODEHELPER(path, argv, envp) (0)
626 #define SIGNAL_MASK_ASSERT()
627 #define KERN_INFO
628
629 #include <sys/time.h>
630 #if HZ != 1
631 #error "liblustre's jiffies currently expects HZ to be 1"
632 #endif
633 #define jiffies                                 \
634 ({                                              \
635         unsigned long _ret = 0;                 \
636         struct timeval tv;                      \
637         if (gettimeofday(&tv, NULL) == 0)       \
638                 _ret = tv.tv_sec;               \
639         _ret;                                   \
640 })
641 #define time_after(a, b) ((long)(b) - (long)(a) > 0)
642 #define time_before(a, b) time_after(b,a)
643
644 struct timer_list {
645         struct list_head tl_list;
646         void (*function)(unsigned long unused);
647         unsigned long data;
648         long expires;
649 };
650
651 static inline int timer_pending(struct timer_list *l)
652 {
653         if (l->expires > jiffies)
654                 return 1;
655         else
656                 return 0;
657 }
658
659 static inline int init_timer(struct timer_list *l)
660 {
661         INIT_LIST_HEAD(&l->tl_list);
662         return 0;
663 }
664
665 static inline void mod_timer(struct timer_list *l, int thetime)
666 {
667         l->expires = thetime;
668 }
669
670 static inline void del_timer(struct timer_list *l)
671 {
672         free(l);
673 }
674
675 typedef struct { volatile int counter; } atomic_t;
676
677 #define atomic_read(a) ((a)->counter)
678 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
679 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
680 #define atomic_inc(a)  (((a)->counter)++)
681 #define atomic_dec(a)  do { (a)->counter--; } while (0)
682 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
683 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
684
685 #ifndef likely
686 #define likely(exp) (exp)
687 #endif
688 #ifndef unlikely
689 #define unlikely(exp) (exp)
690 #endif
691
692 /* FIXME sys/capability will finally included linux/fs.h thus
693  * cause numerous trouble on x86-64. as temporary solution for
694  * build broken at cary, we copy definition we need from capability.h
695  * FIXME
696  */
697 struct _cap_struct;
698 typedef struct _cap_struct *cap_t;
699 typedef int cap_value_t;
700 typedef enum {
701     CAP_EFFECTIVE=0,
702     CAP_PERMITTED=1,
703     CAP_INHERITABLE=2
704 } cap_flag_t;
705 typedef enum {
706     CAP_CLEAR=0,
707     CAP_SET=1
708 } cap_flag_value_t;
709
710 #define CAP_FOWNER      3
711 #define CAP_FSETID      4
712 #define CAP_SYS_ADMIN   21
713
714 cap_t   cap_get_proc(void);
715 int     cap_get_flag(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *);
716
717
718
719 /* log related */
720 static inline int llog_init_commit_master(void) { return 0; }
721 static inline int llog_cleanup_commit_master(int force) { return 0; }
722 static inline void portals_run_lbug_upcall(char *file, const char *fn,
723                                            const int l){}
724
725 #define LBUG()                                                          \
726         do {                                                            \
727                 printf("!!!LBUG at %s:%d\n", __FILE__, __LINE__);       \
728                 sleep(1000000);                                         \
729         } while (0)
730
731
732
733 /* completion */
734 struct completion {
735         unsigned int done;
736         wait_queue_head_t wait;
737 };
738
739 #define COMPLETION_INITIALIZER(work) \
740         { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
741
742 #define DECLARE_COMPLETION(work) \
743         struct completion work = COMPLETION_INITIALIZER(work)
744
745 #define INIT_COMPLETION(x)      ((x).done = 0)
746
747 static inline void init_completion(struct completion *x)
748 {
749         x->done = 0;
750         init_waitqueue_head(&x->wait);
751 }
752
753 struct liblustre_wait_callback {
754         struct list_head    llwc_list;
755         int               (*llwc_fn)(void *arg);
756         void               *llwc_arg;
757 };
758
759 void *liblustre_register_wait_callback(int (*fn)(void *arg), void *arg);
760 void liblustre_deregister_wait_callback(void *notifier);
761 int liblustre_wait_event(int timeout);
762
763 #include <linux/obd_support.h>
764 #include <linux/lustre_idl.h>
765 #include <linux/lustre_lib.h>
766 #include <linux/lustre_import.h>
767 #include <linux/lustre_export.h>
768 #include <linux/lustre_net.h>
769
770
771 #endif