02b3e5169b169747c7368918cd5f4b2fc34e6e5c
[tprouty/samba.git] / source3 / include / samba_linux_quota.h
1 #ifndef _SAMBA_LINUX_QUOTA_H_
2 #define _SAMBA_LINUX_QUOTA_H_
3 /*
4    Unix SMB/CIFS implementation.
5    Copyright (C) Andrew Tridgell 1994-2002
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program 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 this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*
23         This file is needed because Quota support on Linux has
24         been broken since Linus kernel 2.4.x. It will only get
25         better (and this file be removed) when all the distributions
26         ship a glibc with a working quota.h file. This is very
27         bad. JRA.
28
29         Original file came from Christoph Hellwig <hch@infradead.org>.
30         Massaged into one nasty include file (to stop us having to
31         add multiple files into Samba just for Linux braindamage)
32         by JRA.
33 */
34
35 #undef QUOTABLOCK_SIZE
36
37 #ifndef _QUOTAIO_LINUX_V1
38 #define _QUOTAIO_LINUX_V1
39
40 /*
41  *      Headerfile for old quotafile format
42  */
43
44 #include <sys/types.h>
45
46 #define V1_DQBLK_SIZE_BITS 10
47 #define V1_DQBLK_SIZE (1 << V1_DQBLK_SIZE_BITS) /* Size of one quota block in bytes in old format */
48
49 #define V1_DQOFF(__id) ((loff_t) ((__id) * sizeof(struct v1_disk_dqblk)))
50
51 /* Structure of quota on disk */
52 struct v1_disk_dqblk {
53         u_int32_t dqb_bhardlimit;       /* absolute limit on disk blks alloc */
54         u_int32_t dqb_bsoftlimit;       /* preferred limit on disk blks */
55         u_int32_t dqb_curblocks;        /* current block count */
56         u_int32_t dqb_ihardlimit;       /* maximum # allocated inodes */
57         u_int32_t dqb_isoftlimit;       /* preferred limit on inodes */
58         u_int32_t dqb_curinodes;        /* current # allocated inodes */
59         time_t dqb_btime;       /* time limit for excessive disk use */
60         time_t dqb_itime;       /* time limit for excessive files */
61 } __attribute__ ((packed));
62
63 /* Structure used for communication with kernel */
64 struct v1_kern_dqblk {
65         u_int32_t dqb_bhardlimit;       /* absolute limit on disk blks alloc */
66         u_int32_t dqb_bsoftlimit;       /* preferred limit on disk blks */
67         u_int32_t dqb_curblocks;        /* current block count */
68         u_int32_t dqb_ihardlimit;       /* maximum # allocated inodes */
69         u_int32_t dqb_isoftlimit;       /* preferred inode limit */
70         u_int32_t dqb_curinodes;        /* current # allocated inodes */
71         time_t dqb_btime;       /* time limit for excessive disk use */
72         time_t dqb_itime;       /* time limit for excessive files */
73 };
74
75 struct v1_dqstats {
76         u_int32_t lookups;
77         u_int32_t drops;
78         u_int32_t reads;
79         u_int32_t writes;
80         u_int32_t cache_hits;
81         u_int32_t allocated_dquots;
82         u_int32_t free_dquots;
83         u_int32_t syncs;
84 };                                                                               
85
86 #ifndef Q_V1_GETQUOTA
87 #define Q_V1_GETQUOTA  0x300
88 #endif
89 #ifndef Q_V1_SETQUOTA
90 #define Q_V1_SETQUOTA  0x400
91 #endif
92
93 #endif /* _QUOTAIO_LINUX_V1 */
94
95 /*
96  *
97  *      Header file for disk format of new quotafile format
98  *
99  */
100
101 #ifndef _QUOTAIO_LINUX_V2
102 #define _QUOTAIO_LINUX_V2
103
104 #include <sys/types.h>
105
106 #ifndef _QUOTA_LINUX
107 #define _QUOTA_LINUX
108
109 #include <sys/types.h>
110
111 typedef u_int32_t qid_t;        /* Type in which we store ids in memory */
112 typedef u_int64_t qsize_t;      /* Type in which we store size limitations */
113
114 #define MAXQUOTAS 2
115 #define USRQUOTA  0             /* element used for user quotas */
116 #define GRPQUOTA  1             /* element used for group quotas */
117
118 /*
119  * Definitions for the default names of the quotas files.
120  */
121 #define INITQFNAMES { \
122         "user",    /* USRQUOTA */ \
123         "group",   /* GRPQUOTA */ \
124         "undefined", \
125 }
126
127 /*
128  * Definitions of magics and versions of current quota files
129  */
130 #define INITQMAGICS {\
131         0xd9c01f11,     /* USRQUOTA */\
132         0xd9c01927      /* GRPQUOTA */\
133 }
134
135 /* Size of blocks in which are counted size limits in generic utility parts */
136 #define QUOTABLOCK_BITS 10
137 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
138
139 /* Conversion routines from and to quota blocks */
140 #define qb2kb(x) ((x) << (QUOTABLOCK_BITS-10))
141 #define kb2qb(x) ((x) >> (QUOTABLOCK_BITS-10))
142 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
143
144 /*
145  * Command definitions for the 'quotactl' system call.
146  * The commands are broken into a main command defined below
147  * and a subcommand that is used to convey the type of
148  * quota that is being manipulated (see above).
149  */
150 #define SUBCMDMASK  0x00ff
151 #define SUBCMDSHIFT 8
152 #define QCMD(cmd, type)  (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
153
154 #define Q_6_5_QUOTAON  0x0100   /* enable quotas */
155 #define Q_6_5_QUOTAOFF 0x0200   /* disable quotas */
156 #define Q_6_5_SYNC     0x0600   /* sync disk copy of a filesystems quotas */
157
158 #define Q_SYNC     0x800001     /* sync disk copy of a filesystems quotas */
159 #define Q_QUOTAON  0x800002     /* turn quotas on */
160 #define Q_QUOTAOFF 0x800003     /* turn quotas off */
161 #define Q_GETFMT   0x800004     /* get quota format used on given filesystem */
162 #define Q_GETINFO  0x800005     /* get information about quota files */
163 #define Q_SETINFO  0x800006     /* set information about quota files */
164 #define Q_GETQUOTA 0x800007     /* get user quota structure */
165 #define Q_SETQUOTA 0x800008     /* set user quota structure */
166
167 /*
168  * Quota structure used for communication with userspace via quotactl
169  * Following flags are used to specify which fields are valid
170  */
171 #define QIF_BLIMITS     1
172 #define QIF_SPACE       2
173 #define QIF_ILIMITS     4
174 #define QIF_INODES      8
175 #define QIF_BTIME       16
176 #define QIF_ITIME       32
177 #define QIF_LIMITS      (QIF_BLIMITS | QIF_ILIMITS)
178 #define QIF_USAGE       (QIF_SPACE | QIF_INODES)
179 #define QIF_TIMES       (QIF_BTIME | QIF_ITIME)
180 #define QIF_ALL         (QIF_LIMITS | QIF_USAGE | QIF_TIMES)
181
182 struct if_dqblk {
183         u_int64_t dqb_bhardlimit;
184         u_int64_t dqb_bsoftlimit;
185         u_int64_t dqb_curspace;
186         u_int64_t dqb_ihardlimit;
187         u_int64_t dqb_isoftlimit;
188         u_int64_t dqb_curinodes;
189         u_int64_t dqb_btime;
190         u_int64_t dqb_itime;
191         u_int32_t dqb_valid;
192 };
193
194 /*
195  * Structure used for setting quota information about file via quotactl
196  * Following flags are used to specify which fields are valid
197  */
198 #define IIF_BGRACE      1
199 #define IIF_IGRACE      2
200 #define IIF_FLAGS       4
201 #define IIF_ALL         (IIF_BGRACE | IIF_IGRACE | IIF_FLAGS)
202
203 struct if_dqinfo {
204         u_int64_t dqi_bgrace;
205         u_int64_t dqi_igrace;
206         u_int32_t dqi_flags;
207         u_int32_t dqi_valid;
208 };
209
210 /* Quota format identifiers */
211 #define QFMT_VFS_OLD 1
212 #define QFMT_VFS_V0  2
213
214 /* Flags supported by kernel */
215 #define V1_DQF_RSQUASH 1
216
217 /* Ioctl for getting quota size */
218 #include <sys/ioctl.h>
219 #ifndef FIOQSIZE
220         #if defined(__alpha__) || defined(__powerpc__) || defined(__sh__) || defined(__sparc__) || defined(__sparc64__)
221                 #define FIOQSIZE _IOR('f', 128, loff_t)
222         #elif defined(__arm__) || defined(__mc68000__) || defined(__s390__)
223                 #define FIOQSIZE 0x545E
224         #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__ia64__) || defined(__parisc__) || defined(__cris__) || defined(__hppa__)
225                 #define FIOQSIZE 0x5460
226         #elif defined(__mips__) || defined(__mips64__)
227                 #define FIOQSIZE 0x6667
228         #endif
229 #endif
230
231 long quotactl __P((int, const char *, qid_t, caddr_t));
232
233 #endif /* _QUOTA_LINUX */
234
235 #define V2_DQINFOOFF    sizeof(struct v2_disk_dqheader) /* Offset of info header in file */
236 #define V2_DQBLKSIZE_BITS       10
237 #define V2_DQBLKSIZE    (1 << V2_DQBLKSIZE_BITS)        /* Size of block with quota structures */
238 #define V2_DQTREEOFF    1       /* Offset of tree in file in blOcks */
239 #define V2_DQTREEDEPTH  4       /* Depth of quota tree */
240 #define V2_DQSTRINBLK   ((V2_DQBLKSIZE - sizeof(struct v2_disk_dqdbheader)) / sizeof(struct v2_disk_dqblk))     /* Number of entries in one blocks */
241 #define V2_GETIDINDEX(id, depth) (((id) >> ((V2_DQTREEDEPTH-(depth)-1)*8)) & 0xff)
242 #define V2_GETENTRIES(buf) ((struct v2_disk_dqblk *)(((char *)(buf)) + sizeof(struct v2_disk_dqdbheader)))
243 #define INIT_V2_VERSIONS { 0, 0}
244
245 struct v2_disk_dqheader {
246         u_int32_t dqh_magic;    /* Magic number identifying file */
247         u_int32_t dqh_version;  /* File version */
248 } __attribute__ ((packed));
249
250 /* Flags for version specific files */
251 #define V2_DQF_MASK  0x0000     /* Mask for all valid ondisk flags */
252
253 /* Header with type and version specific information */
254 struct v2_disk_dqinfo {
255         u_int32_t dqi_bgrace;   /* Time before block soft limit becomes hard limit */
256         u_int32_t dqi_igrace;   /* Time before inode soft limit becomes hard limit */
257         u_int32_t dqi_flags;    /* Flags for quotafile (DQF_*) */
258         u_int32_t dqi_blocks;   /* Number of blocks in file */
259         u_int32_t dqi_free_blk; /* Number of first free block in the list */
260         u_int32_t dqi_free_entry;       /* Number of block with at least one free entry */
261 } __attribute__ ((packed));
262
263 /*
264  *  Structure of header of block with quota structures. It is padded to 16 bytes so
265  *  there will be space for exactly 18 quota-entries in a block
266  */
267 struct v2_disk_dqdbheader {
268         u_int32_t dqdh_next_free;       /* Number of next block with free entry */
269         u_int32_t dqdh_prev_free;       /* Number of previous block with free entry */
270         u_int16_t dqdh_entries; /* Number of valid entries in block */
271         u_int16_t dqdh_pad1;
272         u_int32_t dqdh_pad2;
273 } __attribute__ ((packed));
274
275 /* Structure of quota for one user on disk */
276 struct v2_disk_dqblk {
277         u_int32_t dqb_id;       /* id this quota applies to */
278         u_int32_t dqb_ihardlimit;       /* absolute limit on allocated inodes */
279         u_int32_t dqb_isoftlimit;       /* preferred inode limit */
280         u_int32_t dqb_curinodes;        /* current # allocated inodes */
281         u_int32_t dqb_bhardlimit;       /* absolute limit on disk space (in QUOTABLOCK_SIZE) */
282         u_int32_t dqb_bsoftlimit;       /* preferred limit on disk space (in QUOTABLOCK_SIZE) */
283         u_int64_t dqb_curspace; /* current space occupied (in bytes) */
284         u_int64_t dqb_btime;    /* time limit for excessive disk use */
285         u_int64_t dqb_itime;    /* time limit for excessive inode use */
286 } __attribute__ ((packed));
287
288 /* Structure of quota for communication with kernel */
289 struct v2_kern_dqblk {
290         unsigned int dqb_ihardlimit;
291         unsigned int dqb_isoftlimit;
292         unsigned int dqb_curinodes;
293         unsigned int dqb_bhardlimit;
294         unsigned int dqb_bsoftlimit;
295         qsize_t dqb_curspace;
296         time_t dqb_btime;
297         time_t dqb_itime;
298 };
299
300 /* Structure of quotafile info for communication with kernel */
301 struct v2_kern_dqinfo {
302         unsigned int dqi_bgrace;
303         unsigned int dqi_igrace;
304         unsigned int dqi_flags;
305         unsigned int dqi_blocks;
306         unsigned int dqi_free_blk;
307         unsigned int dqi_free_entry;
308 };
309
310 /* Structure with gathered statistics from kernel */
311 struct v2_dqstats {
312         u_int32_t lookups;
313         u_int32_t drops;
314         u_int32_t reads;
315         u_int32_t writes;
316         u_int32_t cache_hits;
317         u_int32_t allocated_dquots;
318         u_int32_t free_dquots;
319         u_int32_t syncs;
320         u_int32_t version;
321 };
322
323 #ifndef Q_V2_GETQUOTA
324 #define Q_V2_GETQUOTA  0x0D00
325 #endif
326 #ifndef Q_V2_SETQUOTA
327 #define Q_V2_SETQUOTA  0x0E00
328 #endif
329
330 #endif /* _QUOTAIO_LINUX_V2 */
331
332 #ifndef QUOTABLOCK_SIZE
333 #define QUOTABLOCK_SIZE        1024
334 #endif
335
336 #endif /* _SAMBA_LINUX_QUOTA_H_ */