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