-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquota.c
375 lines (264 loc) · 9.9 KB
/
quota.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
eventfs: a self-cleaning filesystem for event queues.
Copyright (C) 2015 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "quota.h"
#include "util.h"
#include "eventfs.h"
SGLIB_DEFINE_RBTREE_FUNCTIONS( eventfs_quota, left, right, color, EVENTFS_QUOTA_ENTRY_CMP );
SGLIB_DEFINE_RBTREE_FUNCTIONS( eventfs_usage, left, right, color, EVENTFS_QUOTA_ENTRY_CMP );
// set up a new quota.
// always succeeds
int eventfs_quota_init( eventfs_quota* q, int64_t user_or_group, uint64_t max_files, uint64_t max_dirs, uint64_t max_files_per_dir, uint64_t max_bytes ) {
memset( q, 0, sizeof(eventfs_quota) );
q->user_or_group = user_or_group;
q->max_files = max_files;
q->max_dirs = max_dirs;
q->max_files_per_dir = max_files_per_dir;
q->max_bytes = max_bytes;
return 0;
}
// put a new quota into a quota set
// always succeeds
// NOTE: new_quota should be malloc'ed
// NOTE: no checks for existence will be performed; the caller must do so itself
int eventfs_quota_put( eventfs_quota** q, eventfs_quota* new_quota ) {
sglib_eventfs_quota_add( q, new_quota );
return 0;
}
// set a quota for a user or group
// return 0 on success
// return -ENOMEM on OOM
int eventfs_quota_set( eventfs_quota** q, int64_t user_or_group, uint64_t max_files, uint64_t max_dirs, uint64_t max_files_per_dir, uint64_t max_bytes ) {
int rc = 0;
eventfs_quota* member = NULL;
eventfs_quota lookup;
memset( &lookup, 0, sizeof(eventfs_quota) );
lookup.user_or_group = user_or_group;
member = sglib_eventfs_quota_find_member( *q, &lookup );
if( member == NULL ) {
// no quota defined. set it
member = EVENTFS_CALLOC( eventfs_quota, 1 );
if( member == NULL ) {
return -ENOMEM;
}
eventfs_quota_init( member, user_or_group, max_files, max_dirs, max_files_per_dir, max_bytes );
eventfs_quota_put( q, member );
}
else {
// quota is defined. reset it.
eventfs_quota_init( member, user_or_group, max_files, max_dirs, max_files_per_dir, max_bytes );
}
return 0;
}
// remove a quota for a user or group
// return 0 on success
int eventfs_quota_clear( eventfs_quota** q, int64_t user_or_group ) {
int rc = 0;
eventfs_quota* member = NULL;
eventfs_quota lookup;
memset( &lookup, 0, sizeof(eventfs_quota) );
lookup.user_or_group = user_or_group;
sglib_eventfs_quota_delete_if_member( q, &lookup, &member );
if( member != NULL ) {
eventfs_safe_free( member );
}
return 0;
}
// look up the quota entry for a user or group
// return a pointer to the entry on success
// return NULL if not found
eventfs_quota* eventfs_quota_lookup( eventfs_quota* q, int64_t user_or_group ) {
eventfs_quota* member = NULL;
eventfs_quota lookup;
memset( &lookup, 0, sizeof(eventfs_quota) );
lookup.user_or_group = user_or_group;
member = sglib_eventfs_quota_find_member( q, &lookup );
return member;
}
// get the maximum number of files this user may create
// return the number on success
// return 0 if not found
uint64_t eventfs_quota_get_max_files( eventfs_quota* q, int64_t user_or_group ) {
int rc = 0;
eventfs_quota* member = NULL;
member = eventfs_quota_lookup( q, user_or_group );
if( member == NULL ) {
// no quota set? no files
return 0;
}
else {
return member->max_files;
}
}
// get the maximum number of directories this user may create
// return the number on success
// return 0 if not found
uint64_t eventfs_quota_get_max_dirs( eventfs_quota* q, int64_t user_or_group ) {
int rc = 0;
eventfs_quota* member = NULL;
member = eventfs_quota_lookup( q, user_or_group );
if( member == NULL ) {
// no quota set? no dirs
return 0;
}
else {
return member->max_dirs;
}
}
// get the maximum number of files per directory this user may create
// return the number on success
// return 0 if not found
uint64_t eventfs_quota_get_max_files_per_dir( eventfs_quota* q, int64_t user_or_group ) {
int rc = 0;
eventfs_quota* member = NULL;
member = eventfs_quota_lookup( q, user_or_group );
if( member == NULL ) {
// no quota set? no files
return 0;
}
else {
return member->max_files_per_dir;
}
}
// get the maximum number of bytes this user can have
// return the number on success
// return 0 if not found
uint64_t eventfs_quota_get_max_bytes( eventfs_quota* q, int64_t user_or_group ) {
int rc = 0;
eventfs_quota* member = NULL;
member = eventfs_quota_lookup( q, user_or_group );
if( member == NULL ) {
// no quota set? no files
return 0;
}
else {
return member->max_bytes;
}
}
// make a usage instance
int eventfs_usage_init( eventfs_usage* u, int64_t user_or_group, uint64_t num_files, uint64_t num_dirs, uint64_t num_bytes ) {
memset( u, 0, sizeof(eventfs_usage) );
u->user_or_group = user_or_group;
u->num_files = num_files;
u->num_dirs = num_dirs;
u->num_bytes = num_bytes;
return 0;
}
// put a usage
int eventfs_usage_put( eventfs_usage** u, eventfs_usage* new_usage ) {
sglib_eventfs_usage_add( u, new_usage );
return 0;
}
// look up the usage entry for a user or group
// return a pointer to the entry on success
// return NULL if not found
eventfs_usage* eventfs_usage_lookup( eventfs_usage* u, int64_t user_or_group ) {
eventfs_usage* member = NULL;
eventfs_usage lookup;
memset( &lookup, 0, sizeof(eventfs_usage) );
lookup.user_or_group = user_or_group;
member = sglib_eventfs_usage_find_member( u, &lookup );
return member;
}
// get the currently-used number of files this user created
// return the number on success
// return 0 if not found
uint64_t eventfs_usage_get_num_files( eventfs_usage* u, int64_t user_or_group ) {
eventfs_usage* member = NULL;
member = eventfs_usage_lookup( u, user_or_group );
if( member == NULL ) {
// no quota set? no files
return 0;
}
else {
return member->num_files;
}
}
// get the currently-used number of directories this user created
// return the number on success
// return 0 if not found
uint64_t eventfs_usage_get_num_dirs( eventfs_usage* u, int64_t user_or_group ) {
eventfs_usage* member = NULL;
member = eventfs_usage_lookup( u, user_or_group );
if( member == NULL ) {
// no quota set? no dirs
return 0;
}
else {
return member->num_dirs;
}
}
// get the currently-used number of bytes this user wrote
// return the number on success
// return 0 if not found
uint64_t eventfs_usage_get_num_bytes( eventfs_usage* u, int64_t user_or_group ) {
eventfs_usage* member = NULL;
member = eventfs_usage_lookup( u, user_or_group );
if( member == NULL ) {
// no quota set? no bytes
return 0;
}
else {
return member->num_bytes;
}
}
// change the currently-used number of files this user owns
void eventfs_usage_change_num_files( eventfs_usage* u, int64_t user_or_group, int change ) {
eventfs_usage* member = NULL;
member = eventfs_usage_lookup( u, user_or_group );
if( member != NULL ) {
__sync_fetch_and_add( &member->num_files, (uint64_t)change );
}
}
// change the currently-used number of directories this user owns
void eventfs_usage_change_num_dirs( eventfs_usage* u, int64_t user_or_group, int change ) {
eventfs_usage* member = NULL;
member = eventfs_usage_lookup( u, user_or_group );
if( member != NULL ) {
__sync_fetch_and_add( &member->num_dirs, (uint64_t)change );
}
}
// change the currently-used number of bytes this user owns
void eventfs_usage_change_num_bytes( eventfs_usage* u, int64_t user_or_group, int change ) {
eventfs_usage* member = NULL;
member = eventfs_usage_lookup( u, user_or_group );
if( member != NULL ) {
__sync_fetch_and_add( &member->num_bytes, (uint64_t)change );
}
}
// free a quota table
void eventfs_quota_free( eventfs_quota* q ) {
struct sglib_eventfs_quota_iterator itr;
struct eventfs_quota_entry* dp = NULL;
struct eventfs_quota_entry* old_dp = NULL;
for( dp = sglib_eventfs_quota_it_init_inorder( &itr, q ); dp != NULL; ) {
old_dp = dp;
dp = sglib_eventfs_quota_it_next( &itr );
eventfs_safe_free( old_dp );
}
}
// free a usage table
void eventfs_usage_free( eventfs_usage* u ) {
struct sglib_eventfs_usage_iterator itr;
struct eventfs_usage_entry* dp = NULL;
struct eventfs_usage_entry* old_dp = NULL;
for( dp = sglib_eventfs_usage_it_init_inorder( &itr, u ); dp != NULL; ) {
old_dp = dp;
dp = sglib_eventfs_usage_it_next( &itr );
eventfs_safe_free( old_dp );
}
}