Whamcloud - gitweb
dba18090e861fc1f7be96aad4dbd996ce5626185
[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 /* 
46  *   OST requests: OBDO & OBD request records
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_GET_INFO   6
56 #define OST_CONNECT    7
57 #define OST_DISCONNECT 8
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 ptlreq_hdr { 
65         __u32 opc;
66         __u64 seqno;
67         __u32 status;
68         __u32 type;
69 };
70
71 struct ptlrep_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_FLOBJID  (0x00020000UL)
140 #define OBD_MD_FLNOTOBD (~(OBD_MD_FLOBDMD | OBD_MD_FLOBDFLG | OBD_MD_FLBLOCKS))
141
142 /* request structure for OST's */
143
144 #define OST_REQ_HAS_OA1  0x1
145
146 struct ost_req { 
147         __u32   connid;
148         __u32   cmd; 
149         struct  obdo oa;
150         __u32   buflen1;
151         __u32   buflen2;
152 };
153
154 struct ost_rep {
155         __u32   result;
156         __u32   connid;
157         struct  obdo oa;
158         __u32   buflen1;
159         __u32   buflen2;
160 };
161
162 struct obd_ioobj { 
163         obd_id    ioo_id;
164         obd_gr    ioo_gr;
165         __u32     ioo_type;
166         __u32     ioo_bufcnt;
167 };
168
169
170 /* 
171  *   MDS REQ RECORDS
172  */
173
174
175 #define MDS_TYPE_REQ 1
176 #define MDS_TYPE_REP 2
177 #define MDS_TYPE_ERR 3
178
179 #define MDS_GETATTR   1
180 #define MDS_REINT     2
181 #define MDS_READPAGE  3
182
183 #define REINT_SETATTR 0
184 #define REINT_CREATE  1
185 #define REINT_LINK    2
186 #define REINT_UNLINK  3
187 #define REINT_RENAME  4
188 #define REINT_MAX     4
189
190 struct ll_fid { 
191         __u64 id;
192         __u32 generation;
193         __u32 f_type;
194 };
195
196 struct niobuf { 
197         __u64 addr;
198         __u64 offset; 
199         __u32 len;
200         __u32 flags;
201         void *page;
202 };
203
204 struct mds_req {
205         struct ll_fid        fid1;
206         struct ll_fid        fid2;
207         __u32                       namelen;
208         __u32                       tgtlen;
209         __u32                       opcode;
210         __u32                       valid;
211         __u32                       mode;
212         __u32                       uid;
213         __u32                       gid;
214         __u64                       size;
215         __u32                       mtime;
216         __u32                       ctime;
217         __u32                       atime;
218         __u32                       flags;
219         __u32                       major;
220         __u32                       minor;
221         __u32                       ino;
222         __u32                       nlink;
223         __u32                       generation;
224         __u64                       objid;
225 };
226
227 struct mds_rep {
228         struct ll_fid               fid1;
229         struct ll_fid               fid2;
230         __u32                       namelen;
231         __u32                       tgtlen;
232         __u32                       valid;
233         __u32                       mode;
234         __u32                       uid;
235         __u32                       gid;
236         __u64                       size;
237         __u32                       mtime;
238         __u32                       ctime;
239         __u32                       atime;
240         __u32                       flags;
241         __u32                       major;
242         __u32                       minor;
243         __u32                       ino;
244         __u32                       nlink;
245         __u32                       generation;
246         __u64                       objid;
247 };
248
249 /* MDS update records */ 
250 struct mds_update_record_hdr { 
251         __u32 ur_reclen;
252         __u32 ur_opcode;
253 };
254
255 struct mds_rec_setattr { 
256         __u32           sa_reclen;
257         __u32           sa_opcode;
258         struct ll_fid   sa_fid;
259         __u32           sa_valid;
260         __u32           sa_mode;
261         __u32           sa_uid;
262         __u32           sa_gid;
263         __u64           sa_size;
264         __u64           sa_atime;
265         __u64           sa_mtime;
266         __u64           sa_ctime;
267         __u32           sa_attr_flags;
268 };
269
270 struct mds_rec_create { 
271         __u32           cr_reclen;
272         __u32           cr_opcode;
273         struct ll_fid   cr_fid;
274         __u32           cr_uid;
275         __u32           cr_gid;
276         __u64           cr_time;
277         __u32           cr_mode;
278         /* overloaded: id for create, tgtlen for symlink, rdev for mknod */ 
279         __u64           cr_id; 
280         __u32           cr_namelen;
281         __u32           cr_tgtlen;
282         /* name here */
283         /* symlink target here */ 
284 };
285
286 struct mds_rec_link { 
287         __u32           lk_reclen;
288         __u32           lk_opcode;
289         struct ll_fid   lk_fid1;
290         struct ll_fid   lk_fid2;
291         __u32           lk_namelen;
292 };
293
294 struct mds_rec_unlink { 
295         __u32           ul_reclen;
296         __u32           ul_opcode;
297         struct ll_fid   ul_fid1;
298         __u32           ul_namelen;
299 };
300
301 struct mds_rec_rename { 
302         __u32           rn_reclen;
303         __u32           rn_opcode;
304         struct ll_fid   rn_fid1;
305         struct ll_fid   rn_fid2;
306         __u32           rn_namelen;
307         __u32           rn_tgtlen;
308 };
309
310 #ifdef __KERNEL__ 
311
312 static inline void ll_ino2fid(struct ll_fid *fid, ino_t ino, __u32 generation, int type)
313 {
314         fid->id = HTON__u64((__u64)ino);
315         fid->generation = HTON__u32(generation);
316         fid->f_type = HTON__u32(type);
317 }
318
319 static inline void ll_inode2fid(struct ll_fid *fid, struct inode *inode)
320 {
321         fid->id = HTON__u64((__u64)inode->i_ino);
322         fid->generation = HTON__u32(inode->i_generation);
323         fid->f_type = HTON__u32(inode->i_mode & S_IFMT);
324 }
325
326 #endif 
327
328 /* 
329  *   OBD IOCTLS
330  */
331
332
333 #define OBD_IOCTL_VERSION 0x00010001
334
335 struct obd_ioctl_data { 
336         uint32_t ioc_len;
337         uint32_t ioc_version;
338         uint32_t ioc_conn1;
339         uint32_t ioc_conn2;
340         struct obdo ioc_obdo1;
341         struct obdo ioc_obdo2;
342         obd_size         ioc_count;
343         obd_off          ioc_offset;
344         uint32_t         ioc_dev;
345
346         /* buffers the kernel will treat as user pointers */
347         uint32_t ioc_plen1;
348         char    *ioc_pbuf1;
349         uint32_t ioc_plen2;
350         char    *ioc_pbuf2;
351
352         /* two inline buffers */
353         uint32_t ioc_inllen1;
354         char    *ioc_inlbuf1;
355         uint32_t ioc_inllen2;
356         char    *ioc_inlbuf2;
357
358         char    ioc_bulk[0];
359 };
360
361 struct obd_ioctl_hdr { 
362         uint32_t ioc_len;
363         uint32_t ioc_version;
364 };
365
366 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
367 {
368         int len = sizeof(struct obd_ioctl_data);
369         len += size_round(data->ioc_inllen1);
370         len += size_round(data->ioc_inllen2);
371         return len;
372 }
373
374 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
375 {
376         if (data->ioc_len > (1<<30)) { 
377                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
378                 return 1;
379         }
380         if (data->ioc_inllen1 > (1<<30)) { 
381                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
382                 return 1;
383         }
384         if (data->ioc_inllen2 > (1<<30)) { 
385                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
386                 return 1;
387         }
388         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
389                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
390                 return 1;
391         }
392         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
393                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
394                 return 1;
395         }
396         if (data->ioc_pbuf1 && !data->ioc_plen1) {
397                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
398                 return 1;
399         }
400         if (data->ioc_pbuf2 && !data->ioc_plen2) {
401                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
402                 return 1;
403         }
404         if (obd_ioctl_packlen(data) != data->ioc_len ) {
405                 printk("OBD ioctl: packlen exceeds ioc_len\n");
406                 return 1;
407         }
408         if (data->ioc_inllen1 && 
409             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') { 
410                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
411                 return 1;
412         }
413         if (data->ioc_inllen2 && 
414             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') { 
415                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
416                 return 1;
417         }
418         return 0;
419 }
420
421 #ifndef __KERNEL__
422 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max)
423 {
424         char *ptr;
425         struct obd_ioctl_data *overlay;
426         data->ioc_len = obd_ioctl_packlen(data);
427         data->ioc_version = OBD_IOCTL_VERSION;
428
429         if (*pbuf && obd_ioctl_packlen(data) > max) 
430                 return 1;
431         if (*pbuf == NULL) { 
432                 *pbuf = malloc(data->ioc_len);
433         }
434         if (!*pbuf)
435                 return 1;
436         overlay = (struct obd_ioctl_data *)*pbuf;
437         memcpy(*pbuf, data, sizeof(*data));
438
439         ptr = overlay->ioc_bulk;
440         if (data->ioc_inlbuf1)
441                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
442         if (data->ioc_inlbuf2)
443                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
444         if (obd_ioctl_is_invalid(overlay))
445                 return 1;
446
447         return 0;
448 }
449 #else
450
451
452 /* buffer MUST be at least the size of obd_ioctl_hdr */
453 static inline int obd_ioctl_getdata(char *buf, char *end, void *arg)
454 {
455         struct obd_ioctl_hdr *hdr;
456         struct obd_ioctl_data *data;
457         int err;
458         ENTRY;
459
460         hdr = (struct obd_ioctl_hdr *)buf;
461         data = (struct obd_ioctl_data *)buf;
462
463         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
464         if ( err ) {
465                 EXIT;
466                 return err;
467         }
468
469         if (hdr->ioc_version != OBD_IOCTL_VERSION) { 
470                 printk("OBD: version mismatch kernel vs application\n");
471                 return -EINVAL;
472         }
473
474         if (hdr->ioc_len + buf >= end) { 
475                 printk("OBD: user buffer exceeds kernel buffer\n");
476                 return -EINVAL;
477         }
478
479
480         if (hdr->ioc_len < sizeof(struct obd_ioctl_data)) { 
481                 printk("OBD: user buffer too small for ioctl\n");
482                 return -EINVAL;
483         }
484
485         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
486         if ( err ) {
487                 EXIT;
488                 return err;
489         }
490
491         if (obd_ioctl_is_invalid(data)) { 
492                 printk("OBD: ioctl not correctly formatted\n");
493                 return -EINVAL;
494         }
495
496         if (data->ioc_inllen1) { 
497                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
498         }
499
500         if (data->ioc_inllen2) { 
501                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
502         }
503
504         EXIT;
505         return 0;
506 }
507 #endif
508
509
510 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
511 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
512 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
513 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
514 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
515 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
516 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
517 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
518 #define OBD_IOC_READ                   _IOWR('f', 11, long)
519 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
520 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
521 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
522 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
523 #define OBD_IOC_SYNC                   _IOR ('f', 16, long)
524 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
525 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
526 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
527 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
528 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
529 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
530 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
531 #define OBD_IOC_PUNCH                  _IOWR('f', 24, long)
532 #define OBD_IOC_DEVICE                 _IOWR('f', 25, long)
533 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 26, long)
534
535 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
536
537
538 /* GENERAL THINGS */
539 union ptl_rep { 
540         struct mds_rep *mds;
541         struct ost_rep *ost;
542 };
543
544 union ptl_req { 
545         struct mds_req *mds;
546         struct ost_req *ost;
547 };
548
549
550 #endif