Whamcloud - gitweb
This adds most of the metadata infrastructure: I think that all commands
[fs/lustre-release.git] / lustre / include / linux / lustre_lib.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. <braam@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  * Basic Lustre library routines. 
22  *
23  */
24
25 #ifndef _CFS_LIB_H
26 #define _CFS_LIB_H
27
28 #include <asm/types.h>
29
30 #ifndef __KERNEL__
31 # include <string.h>
32 #endif
33
34 #ifdef __KERNEL__
35 /* page.c */
36 inline void lustre_put_page(struct page *page);
37 struct page * lustre_get_page(struct inode *dir, unsigned long n);
38 void lustre_prepare_page(unsigned from, unsigned to, struct page *page);
39 int lustre_commit_page(struct page *page, unsigned from, unsigned to);
40 #endif
41
42
43 /* macros */ 
44 #undef MIN
45 #define MIN(a,b) (((a)<(b)) ? (a): (b))
46 #undef MAX
47 #define MAX(a,b) (((a)>(b)) ? (a): (b))
48 #define MKSTR(ptr) ((ptr))? (ptr) : ""
49
50 static inline int size_round (int val)
51 {
52         return (val + 3) & (~0x3);
53 }
54
55 static inline int size_round0(int val)
56 {
57         if (!val) 
58                 return 0;
59         return (val + 1 + 3) & (~0x3);
60 }
61
62 static inline size_t round_strlen(char *fset)
63 {
64         return size_round(strlen(fset) + 1);
65 }
66
67 #ifdef __KERNEL__
68 static inline char *strdup(char *str)
69 {
70         char *tmp = kmalloc(strlen(str) + 1, GFP_KERNEL);
71         if (tmp)
72                 memcpy(tmp, str, strlen(str) + 1);
73                 
74         return NULL;
75 }
76 #endif
77
78 #ifdef __KERNEL__
79 # define NTOH__u32(var) le32_to_cpu(var)
80 # define NTOH__u64(var) le64_to_cpu(var)
81 # define HTON__u32(var) cpu_to_le32(var)
82 # define HTON__u64(var) cpu_to_le64(var)
83 #else
84 # include <glib.h>
85 # define NTOH__u32(var) GUINT32_FROM_LE(var)
86 # define NTOH__u64(var) GUINT64_FROM_LE(var)
87 # define HTON__u32(var) GUINT32_TO_LE(var)
88 # define HTON__u64(var) GUINT64_TO_LE(var)
89 #endif
90
91 /* 
92  * copy sizeof(type) bytes from pointer to var and move ptr forward.
93  * return EFAULT if pointer goes beyond end
94  */
95 #define UNLOGV(var,type,ptr,end)                \
96 do {                                            \
97         var = *(type *)ptr;                     \
98         ptr += sizeof(type);                    \
99         if (ptr > end )                         \
100                 return -EFAULT;                 \
101 } while (0)
102
103 /* the following two macros convert to little endian */
104 /* type MUST be __u32 or __u64 */
105 #define LUNLOGV(var,type,ptr,end)               \
106 do {                                            \
107         var = NTOH##type(*(type *)ptr);         \
108         ptr += sizeof(type);                    \
109         if (ptr > end )                         \
110                 return -EFAULT;                 \
111 } while (0)
112
113 /* now log values */
114 #define LOGV(var,type,ptr)                      \
115 do {                                            \
116         *((type *)ptr) = var;                   \
117         ptr += sizeof(type);                    \
118 } while (0)
119
120 /* and in network order */
121 #define LLOGV(var,type,ptr)                     \
122 do {                                            \
123         *((type *)ptr) = HTON##type(var);       \
124         ptr += sizeof(type);                    \
125 } while (0)
126
127
128 /* 
129  * set var to point at (type *)ptr, move ptr forward with sizeof(type)
130  * return from function with EFAULT if ptr goes beyond end
131  */
132 #define UNLOGP(var,type,ptr,end)                \
133 do {                                            \
134         var = (type *)ptr;                      \
135         ptr += sizeof(type);                    \
136         if (ptr > end )                         \
137                 return -EFAULT;                 \
138 } while (0)
139
140 #define LOGP(var,type,ptr)                      \
141 do {                                            \
142         memcpy(ptr, var, sizeof(type));         \
143         ptr += sizeof(type);                    \
144 } while (0)
145
146 /* 
147  * set var to point at (char *)ptr, move ptr forward by size_round(len);
148  * return from function with EFAULT if ptr goes beyond end
149  */
150 #define UNLOGL(var,type,len,ptr,end)            \
151 do {                                            \
152         if (!len) {                             \
153                var = NULL;                      \
154                break;                           \
155         }                                       \
156         var = (type *)ptr;                      \
157         ptr += size_round(len * sizeof(type));  \
158         if (ptr > end )                         \
159                 return -EFAULT;                 \
160 } while (0)
161
162 #define UNLOGL0(var,type,len,ptr,end)           \
163 do {                                            \
164         UNLOGL(var,type,len,ptr,end);           \
165         if ( *((char *)ptr - size_round(len) + len - 1) != '\0')\
166                         return -EFAULT;                        \
167 } while (0)
168
169
170 #define LOGL(var,len,ptr)                               \
171 do {                                                    \
172         if (!len) break;                                \
173         memcpy((char *)ptr, (const char *)var, len);    \
174         ptr += size_round(len);                         \
175 } while (0)
176
177 #define LOGL0(var,len,ptr)                              \
178 do {                                                    \
179         if (!len) break;                                \
180         memcpy((char *)ptr, (const char *)var, len);    \
181         *((char *)(ptr) + len) = 0;                     \
182         ptr += size_round(len + 1);                     \
183 } while (0)
184
185 #endif /* _LUSTRE_LIB_H */