Whamcloud - gitweb
Most of the code for the OST target.
[fs/lustre-release.git] / lustre / include / linux / lustre_idl.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  * (Un)packing of OST requests
22  */
23
24 #ifndef __LUSTRE_IDL_H__
25 #define __LUSTRE_IDL_H__
26 #ifdef __KERNEL__
27 #include <linux/ioctl.h>
28 #include <asm/types.h>
29
30 #include <linux/types.h>
31 #else 
32 #define __KERNEL__
33 #include <linux/list.h>
34 #undef __KERNEL__
35 #include <stdint.h>
36 #endif
37 /* 
38  * this file contains all data structures used in Lustre interfaces:
39  * - obdo and obd_request records
40  * - mds_request records
41  * - ioctl's
42  */ 
43
44 /* 
45  *   OST requests: OBDO & OBD request records
46  */
47
48
49 /* opcodes */
50 #define OST_GETATTR  1
51 #define OST_SETATTR  2
52 #define OST_BRW      3
53 #define OST_CREATE   4
54 #define OST_DESTROY  5
55 #define OST_GETINFO  6
56
57 /* packet types */
58 #define OST_TYPE_REQ 1
59 #define OST_TYPE_REP 2
60 #define OST_TYPE_ERR 3
61
62 struct ost_req_hdr { 
63         __u32 opc;
64         __u64 seqno;
65         __u32 status;
66         __u32 type;
67 };
68
69 struct ost_rep_hdr { 
70         __u32 opc;
71         __u64 seqno;
72         __u32 status;
73         __u32 type;
74 };
75
76 typedef uint64_t        obd_id;
77 typedef uint64_t        obd_gr;
78 typedef uint64_t        obd_time;
79 typedef uint64_t        obd_size;
80 typedef uint64_t        obd_off;
81 typedef uint64_t        obd_blocks;
82 typedef uint32_t        obd_blksize;
83 typedef uint32_t        obd_mode;
84 typedef uint32_t        obd_uid;
85 typedef uint32_t        obd_gid;
86 typedef uint32_t        obd_rdev;
87 typedef uint32_t        obd_flag;
88 typedef uint32_t        obd_count;
89
90 #define OBD_FL_INLINEDATA       (0x00000001UL)  
91 #define OBD_FL_OBDMDEXISTS      (0x00000002UL)
92
93 #define OBD_INLINESZ    60
94 #define OBD_OBDMDSZ     60
95 /* Note: 64-bit types are 64-bit aligned in structure */
96 struct obdo {
97         obd_id                  o_id;
98         obd_gr                  o_gr;
99         obd_time                o_atime;
100         obd_time                o_mtime;
101         obd_time                o_ctime;
102         obd_size                o_size;
103         obd_blocks              o_blocks;
104         obd_blksize             o_blksize;
105         obd_mode                o_mode;
106         obd_uid                 o_uid;
107         obd_gid                 o_gid;
108         obd_flag                o_flags;
109         obd_flag                o_obdflags;
110         obd_count               o_nlink;
111         obd_count               o_generation;
112         obd_flag                o_valid;        /* hot fields in this obdo */
113         char                    o_inline[OBD_INLINESZ];
114         char                    o_obdmd[OBD_OBDMDSZ];
115         struct list_head        o_list;
116         struct obd_ops          *o_op;
117 };
118
119 #define OBD_MD_FLALL    (~0UL)
120 #define OBD_MD_FLID     (0x00000001UL)
121 #define OBD_MD_FLATIME  (0x00000002UL)
122 #define OBD_MD_FLMTIME  (0x00000004UL)
123 #define OBD_MD_FLCTIME  (0x00000008UL)
124 #define OBD_MD_FLSIZE   (0x00000010UL)
125 #define OBD_MD_FLBLOCKS (0x00000020UL)
126 #define OBD_MD_FLBLKSZ  (0x00000040UL)
127 #define OBD_MD_FLMODE   (0x00000080UL)
128 #define OBD_MD_FLTYPE   (0x00000100UL)
129 #define OBD_MD_FLUID    (0x00000200UL)
130 #define OBD_MD_FLGID    (0x00000400UL)
131 #define OBD_MD_FLFLAGS  (0x00000800UL)
132 #define OBD_MD_FLOBDFLG (0x00001000UL)
133 #define OBD_MD_FLNLINK  (0x00002000UL)
134 #define OBD_MD_FLGENER  (0x00004000UL)
135 #define OBD_MD_FLINLINE (0x00008000UL)
136 #define OBD_MD_FLOBDMD  (0x00010000UL)
137 #define OBD_MD_FLNOTOBD (~(OBD_MD_FLOBDMD | OBD_MD_FLOBDFLG | OBD_MD_FLBLOCKS))
138
139 /* request structure for OST's */
140
141 #define OST_REQ_HAS_OA1  0x1
142
143 struct ost_req_packed { 
144         __u32   connid;
145         __u32   cmd; 
146         struct obdo oa;
147         __u32   buflen1;
148         __u32   buflen2;
149         __u32   bufoffset1;
150         __u32   bufoffset2;
151 };
152
153 struct ost_rep_packed {
154         __u32   result;
155         struct obdo oa;
156         __u32   buflen1;
157         __u32   buflen2;
158         __u32   bufoffset1;
159         __u32   bufoffset2;
160 };
161
162
163 /* reply structure for OST's */
164
165
166
167
168
169 /* 
170  *   MDS REQ RECORDS
171  */
172
173
174 #define MDS_TYPE_REQ 1
175 #define MDS_TYPE_REP 2
176 #define MDS_TYPE_ERR 3
177
178 struct mds_req_hdr { 
179         __u32 opc;
180         __u64 seqno;
181         __u32 status;
182         __u32 type;
183 };
184
185 struct lustre_fid { 
186         __u64 id;
187         __u32 generation;
188         __u32 f_type;
189 };
190
191
192 struct mds_rep_hdr { 
193         __u32 opc;
194         __u64 seqno;
195         __u32 status;
196         __u32 type;
197 };
198
199 struct mds_req_packed {
200         struct lustre_fid        fid1;
201         struct lustre_fid        fid2;
202         int                        namelen;
203         int                        tgtlen;
204         __u32                       valid;
205         __u32                       mode;
206         __u32                       uid;
207         __u32                       gid;
208         __u64                       size;
209         __u32                       mtime;
210         __u32                       ctime;
211         __u32                       atime;
212         __u32                       flags;
213         __u32                       major;
214         __u32                       minor;
215         __u32                       ino;
216         __u32                       generation;
217         __u32                       name_offset;
218         __u32                       tgt_offset;
219 };
220
221 struct mds_rep_packed {
222         struct lustre_fid        fid1;
223         struct lustre_fid        fid2;
224         int                        namelen;
225         int                        tgtlen;
226         __u32                       valid;
227         __u32                       mode;
228         __u32                       uid;
229         __u32                       gid;
230         __u64                       size;
231         __u32                       mtime;
232         __u32                       ctime;
233         __u32                       atime;
234         __u32                       flags;
235         __u32                       major;
236         __u32                       minor;
237         __u32                       ino;
238         __u32                       generation;
239         __u32                       name_offset;
240         __u32                       tgt_offset;
241 };
242
243 /* 
244  *   OBD IOCTLS
245  */
246
247
248 #define OBD_IOCTL_VERSION 0x00010001
249
250 struct obd_ioctl_data { 
251         uint32_t ioc_len;
252         uint32_t ioc_version;
253         uint32_t ioc_conn1;
254         uint32_t ioc_conn2;
255         struct obdo ioc_obdo1;
256         struct obdo ioc_obdo2;
257         obd_size         ioc_count;
258         obd_off          ioc_offset;
259         uint32_t         ioc_dev;
260
261         /* buffers the kernel will treat as user pointers */
262         uint32_t ioc_plen1;
263         char    *ioc_pbuf1;
264         uint32_t ioc_plen2;
265         char    *ioc_pbuf2;
266
267         /* two inline buffers */
268         uint32_t ioc_inllen1;
269         char    *ioc_inlbuf1;
270         uint32_t ioc_inllen2;
271         char    *ioc_inlbuf2;
272
273         char    ioc_bulk[0];
274 };
275
276 struct obd_ioctl_hdr { 
277         uint32_t ioc_len;
278         uint32_t ioc_version;
279 };
280
281 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
282 {
283         int len = sizeof(struct obd_ioctl_data);
284         len += size_round(data->ioc_inllen1);
285         len += size_round(data->ioc_inllen2);
286         return len;
287 }
288
289 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
290 {
291         if (data->ioc_len > (1<<30)) { 
292                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
293                 return 1;
294         }
295         if (data->ioc_inllen1 > (1<<30)) { 
296                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
297                 return 1;
298         }
299         if (data->ioc_inllen2 > (1<<30)) { 
300                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
301                 return 1;
302         }
303         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
304                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
305                 return 1;
306         }
307         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
308                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
309                 return 1;
310         }
311         if (data->ioc_pbuf1 && !data->ioc_plen1) {
312                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
313                 return 1;
314         }
315         if (data->ioc_pbuf2 && !data->ioc_plen2) {
316                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
317                 return 1;
318         }
319         if (obd_ioctl_packlen(data) != data->ioc_len ) {
320                 printk("OBD ioctl: packlen exceeds ioc_len\n");
321                 return 1;
322         }
323         if (data->ioc_inllen1 && 
324             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') { 
325                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
326                 return 1;
327         }
328         if (data->ioc_inllen2 && 
329             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') { 
330                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
331                 return 1;
332         }
333         return 0;
334 }
335
336 #ifndef __KERNEL__
337 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max)
338 {
339         char *ptr;
340         struct obd_ioctl_data *overlay;
341         data->ioc_len = obd_ioctl_packlen(data);
342         data->ioc_version = OBD_IOCTL_VERSION;
343
344         if (*pbuf && obd_ioctl_packlen(data) > max) 
345                 return 1;
346         if (*pbuf == NULL) { 
347                 *pbuf = malloc(data->ioc_len);
348         }
349         if (!*pbuf)
350                 return 1;
351         overlay = (struct obd_ioctl_data *)*pbuf;
352         memcpy(*pbuf, data, sizeof(*data));
353
354         ptr = overlay->ioc_bulk;
355         if (data->ioc_inlbuf1)
356                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
357         if (data->ioc_inlbuf2)
358                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
359         if (obd_ioctl_is_invalid(overlay))
360                 return 1;
361
362         return 0;
363 }
364 #else
365
366
367 /* buffer MUST be at least the size of obd_ioctl_hdr */
368 static inline int obd_ioctl_getdata(char *buf, char *end, void *arg)
369 {
370         struct obd_ioctl_hdr *hdr;
371         struct obd_ioctl_data *data;
372         int err;
373         ENTRY;
374
375         hdr = (struct obd_ioctl_hdr *)buf;
376         data = (struct obd_ioctl_data *)buf;
377
378         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
379         if ( err ) {
380                 EXIT;
381                 return err;
382         }
383
384         if (hdr->ioc_version != OBD_IOCTL_VERSION) { 
385                 printk("OBD: version mismatch kernel vs application\n");
386                 return -EINVAL;
387         }
388
389         if (hdr->ioc_len + buf >= end) { 
390                 printk("OBD: user buffer exceeds kernel buffer\n");
391                 return -EINVAL;
392         }
393
394
395         if (hdr->ioc_len < sizeof(struct obd_ioctl_data)) { 
396                 printk("OBD: user buffer too small for ioctl\n");
397                 return -EINVAL;
398         }
399
400         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
401         if ( err ) {
402                 EXIT;
403                 return err;
404         }
405
406         if (obd_ioctl_is_invalid(data)) { 
407                 printk("OBD: ioctl not correctly formatted\n");
408                 return -EINVAL;
409         }
410
411         if (data->ioc_inllen1) { 
412                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
413         }
414
415         if (data->ioc_inllen2) { 
416                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
417         }
418
419         EXIT;
420         return 0;
421 }
422 #endif
423
424
425 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
426 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
427 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
428 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
429 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
430 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
431 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
432 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
433 #define OBD_IOC_READ                   _IOWR('f', 11, long)
434 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
435 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
436 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
437 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
438 #define OBD_IOC_SYNC                   _IOR ('f', 16, long)
439 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
440 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
441 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
442 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
443 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
444 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
445 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
446 #define OBD_IOC_PUNCH                  _IOWR('f', 24, long)
447 #define OBD_IOC_DEVICE                 _IOWR('f', 25, long)
448
449 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
450
451
452
453 #endif