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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Routines used in checking limits on file system usage.
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/signal.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_quota.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/session.h>
#include <sys/debug.h>
/*
* Find the dquot structure that should
* be used in checking i/o on inode ip.
*/
struct dquot *
getinoquota(struct inode *ip)
{
struct dquot *dqp, *xdqp;
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
ASSERT(RW_WRITE_HELD(&ip->i_contents));
/*
* Check for quotas enabled.
*/
if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
return (NULL);
}
/*
* Check for someone doing I/O to quota file.
*/
if (ip == ufsvfsp->vfs_qinod) {
return (NULL);
}
/*
* Check for a legal inode, e.g. not a shadow inode,
* not a extended attribute directory inode and a valid mode.
*/
ASSERT((ip->i_mode & IFMT) != IFSHAD);
ASSERT((ip->i_mode & IFMT) != IFATTRDIR);
ASSERT(ip->i_mode);
if (getdiskquota((uid_t)ip->i_uid, ufsvfsp, 0, &xdqp)) {
return (NULL);
}
dqp = xdqp;
mutex_enter(&dqp->dq_lock);
ASSERT(ip->i_uid == dqp->dq_uid);
if (dqp->dq_fhardlimit == 0 && dqp->dq_fsoftlimit == 0 &&
dqp->dq_bhardlimit == 0 && dqp->dq_bsoftlimit == 0) {
dqput(dqp);
mutex_exit(&dqp->dq_lock);
dqp = NULL;
} else {
mutex_exit(&dqp->dq_lock);
}
return (dqp);
}
/*
* Update disk usage, and take corrective action.
*/
int
chkdq(struct inode *ip, long change, int force, struct cred *cr,
char **uerrp, size_t *lenp)
{
struct dquot *dqp;
uint64_t ncurblocks;
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
int error = 0;
long abs_change;
char *msg1 =
"!quota_ufs: over hard disk limit (pid %d, uid %d, inum %d, fs %s)\n";
char *msg2 =
"!quota_ufs: Warning: over disk limit (pid %d, uid %d, inum %d, fs %s)\n";
char *msg3 =
"!quota_ufs: over disk and time limit (pid %d, uid %d, inum %d, fs %s)\n";
char *msg4 =
"!quota_ufs: Warning: quota overflow (pid %d, uid %d, inum %d, fs %s)\n";
char *errmsg = NULL;
time_t now;
/*
* Shadow inodes do not need to hold the vfs_dqrwlock lock.
*/
ASSERT((ip->i_mode & IFMT) == IFSHAD ||
RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
ASSERT(RW_WRITE_HELD(&ip->i_contents));
if (change == 0)
return (0);
dqp = ip->i_dquot;
/*
* Make sure the quota info record matches the owner.
*/
ASSERT(dqp == NULL || ip->i_uid == dqp->dq_uid);
#ifdef DEBUG
/*
* Shadow inodes and extended attribute directories
* should not have quota info records.
*/
if ((ip->i_mode & IFMT) == IFSHAD || (ip->i_mode & IFMT) == IFATTRDIR) {
ASSERT(dqp == NULL);
}
/*
* Paranoia for verifying that quotas are okay.
*/
else {
struct dquot *expect_dq;
int mismatch_ok = 0;
/* Get current quota information */
expect_dq = getinoquota(ip);
/*
* We got NULL back from getinoquota(), but there is
* no error code return from that interface and some
* errors are "ok" because we may be testing via error
* injection. If this is not the quota inode then we
* use getdiskquota() to see if there is an error and
* if the error is ok.
*/
if (expect_dq == NULL && ip != ufsvfsp->vfs_qinod) {
int error;
struct dquot *xdqp;
error = getdiskquota((uid_t)ip->i_uid, ufsvfsp, 0,
&xdqp);
switch (error) {
/*
* Either the error was transient or the quota
* info record has no limits which gets optimized
* out by getinoquota().
*/
case 0:
if (xdqp->dq_fhardlimit == 0 &&
xdqp->dq_fsoftlimit == 0 &&
xdqp->dq_bhardlimit == 0 &&
xdqp->dq_bsoftlimit == 0) {
mutex_enter(&xdqp->dq_lock);
dqput(xdqp);
mutex_exit(&xdqp->dq_lock);
} else {
expect_dq = xdqp;
}
break;
case ESRCH: /* quotas are not enabled */
case EINVAL: /* error flag set on cached record */
case EUSERS: /* quota table is full */
case EIO: /* I/O error */
mismatch_ok = 1;
break;
}
}
/*
* Make sure dqp and the current quota info agree.
* The first part of the #ifndef is the quick way to
* do the check and should be part of the standard
* DEBUG code. The #else part is useful if you are
* actually chasing an inconsistency and don't want
* to have to look at stack frames to figure which
* variable has what value.
*/
#ifndef CHASE_QUOTA
ASSERT(mismatch_ok || dqp == expect_dq);
#else /* CHASE_QUOTA */
if (expect_dq == NULL) {
/*
* If you hit this ASSERT() you know that quota
* subsystem does not expect quota info for this
* inode, but the inode has it.
*/
ASSERT(mismatch_ok || dqp == NULL);
} else {
/*
* If you hit this ASSERT() you know that quota
* subsystem expects quota info for this inode,
* but the inode does not have it.
*/
ASSERT(dqp);
/*
* If you hit this ASSERT() you know that quota
* subsystem expects quota info for this inode
* and the inode has quota info, but the two
* quota info pointers are not the same.
*/
ASSERT(dqp == expect_dq);
}
#endif /* !CHASE_QUOTA */
/*
* Release for getinoquota() above or getdiskquota()
* call when error is transient.
*/
if (expect_dq) {
mutex_enter(&expect_dq->dq_lock);
dqput(expect_dq);
mutex_exit(&expect_dq->dq_lock);
}
}
#endif /* DEBUG */
/*
* Shadow inodes and extended attribute directories
* do not have quota info records.
*/
if (dqp == NULL)
return (0);
/*
* Quotas are not enabled on this file system so there is nothing
* more to do.
*/
if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
return (0);
}
mutex_enter(&dqp->dq_lock);
if (change < 0) {
dqp->dq_flags |= DQ_MOD;
abs_change = -change; /* abs_change must be positive */
if (dqp->dq_curblocks < abs_change)
dqp->dq_curblocks = 0;
else
dqp->dq_curblocks += change;
if (dqp->dq_curblocks < dqp->dq_bsoftlimit)
dqp->dq_btimelimit = 0;
dqp->dq_flags &= ~DQ_BLKS;
TRANS_QUOTA(dqp);
mutex_exit(&dqp->dq_lock);
return (0);
}
/*
* Adding 'change' to dq_curblocks could cause an overflow.
* So store the result in a 64-bit variable and check for
* overflow below.
*/
ncurblocks = (uint64_t)dqp->dq_curblocks + change;
/*
* Allocation. Check hard and soft limits.
* Skip checks for uid 0 owned files.
* This check used to require both euid and ip->i_uid
* to be 0; but there are no quotas for uid 0 so
* it really doesn't matter who is writing to the
* root owned file. And even root cannot write
* past a user's quota limit.
*/
if (ip->i_uid == 0)
goto out;
/*
* Disallow allocation if it would bring the current usage over
* the hard limit or if the user is over their soft limit and their
* time has run out.
*/
if (dqp->dq_bhardlimit && ncurblocks >= (uint64_t)dqp->dq_bhardlimit &&
!force) {
/* If the user was not informed yet and the caller */
/* is the owner of the file */
if ((dqp->dq_flags & DQ_BLKS) == 0 &&
ip->i_uid == crgetruid(cr)) {
errmsg = msg1;
dqp->dq_flags |= DQ_BLKS;
}
error = EDQUOT;
goto out;
}
if (dqp->dq_bsoftlimit && ncurblocks >= (uint64_t)dqp->dq_bsoftlimit) {
now = gethrestime_sec();
if (dqp->dq_curblocks < dqp->dq_bsoftlimit ||
dqp->dq_btimelimit == 0) {
dqp->dq_flags |= DQ_MOD;
dqp->dq_btimelimit = now +
((struct ufsvfs *)ITOV(ip)->v_vfsp->vfs_data)
->vfs_btimelimit;
if (ip->i_uid == crgetruid(cr)) {
errmsg = msg2;
}
} else if (now > dqp->dq_btimelimit && !force) {
/* If the user was not informed yet and the */
/* caller is the owner of the file */
if ((dqp->dq_flags & DQ_BLKS) == 0 &&
ip->i_uid == crgetruid(cr)) {
errmsg = msg3;
dqp->dq_flags |= DQ_BLKS;
}
error = EDQUOT;
}
}
out:
if (error == 0) {
dqp->dq_flags |= DQ_MOD;
/*
* ncurblocks can be bigger than the maximum
* number that can be represented in 32-bits.
* When copying ncurblocks to dq_curblocks
* (an unsigned 32-bit quantity), make sure there
* is no overflow. The only way this can happen
* is if "force" is set. Otherwise, this allocation
* would have exceeded the hard limit check above
* (since the hard limit is a 32-bit quantity).
*/
if (ncurblocks > 0xffffffffLL) {
dqp->dq_curblocks = 0xffffffff;
errmsg = msg4;
} else {
dqp->dq_curblocks = ncurblocks;
}
}
if (dqp->dq_flags & DQ_MOD)
TRANS_QUOTA(dqp);
mutex_exit(&dqp->dq_lock);
/*
* Check for any error messages to be sent
*/
if (errmsg != NULL) {
/*
* Send message to the error log.
*/
if (uerrp != NULL) {
/*
* Set up message caller should send to user;
* gets copied to the message buffer as a side-
* effect of the caller's uprintf().
*/
*lenp = strlen(errmsg) + 20 + 20 +
strlen(ip->i_fs->fs_fsmnt) + 1;
*uerrp = (char *)kmem_alloc(*lenp, KM_NOSLEEP);
if (*uerrp != NULL) {
/* errmsg+1 => skip leading ! */
(void) sprintf(*uerrp, errmsg+1,
(int)ttoproc(curthread)->p_pid,
(int)ip->i_uid, (int)ip->i_number,
ip->i_fs->fs_fsmnt);
}
} else {
/*
* Caller doesn't care, so just copy to the
* message buffer.
*/
cmn_err(CE_NOTE, errmsg,
(int)ttoproc(curthread)->p_pid,
(int)ip->i_uid, (int)ip->i_number,
ip->i_fs->fs_fsmnt);
}
}
return (error);
}
/*
* Check the inode limit, applying corrective action.
*/
int
chkiq(struct ufsvfs *ufsvfsp, int change, struct inode *ip, uid_t uid,
int force, struct cred *cr, char **uerrp, size_t *lenp)
{
struct dquot *dqp, *xdqp;
unsigned int ncurfiles;
char *errmsg = NULL;
char *err1 =
"!quota_ufs: over file hard limit (pid %d, uid %d, fs %s)\n";
char *err2 =
"!quota_ufs: Warning: too many files (pid %d, uid %d, fs %s)\n";
char *err3 =
"!quota_ufs: over file and time limit (pid %d, uid %d, fs %s)\n";
int error = 0;
time_t now;
ASSERT(RW_READ_HELD(&ufsvfsp->vfs_dqrwlock));
/*
* Change must be either a single increment or decrement.
* If change is an increment, then ip must be NULL.
*/
ASSERT(change == 1 || change == -1);
ASSERT(change != 1 || ip == NULL);
/*
* Quotas are not enabled so bail out now.
*/
if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
return (0);
}
/*
* Free a specific inode.
*/
if (change == -1 && ip) {
dqp = ip->i_dquot;
/*
* Shadow inodes and extended attribute directories
* do not have quota info records.
*/
if (dqp == NULL)
return (0);
mutex_enter(&dqp->dq_lock);
if (dqp->dq_curfiles) {
dqp->dq_curfiles--;
dqp->dq_flags |= DQ_MOD;
}
if (dqp->dq_curfiles < dqp->dq_fsoftlimit) {
dqp->dq_ftimelimit = 0;
dqp->dq_flags |= DQ_MOD;
}
dqp->dq_flags &= ~DQ_FILES;
if (dqp->dq_flags & DQ_MOD)
TRANS_QUOTA(dqp);
mutex_exit(&dqp->dq_lock);
return (0);
}
/*
* Allocation or deallocation without a specific inode.
* Get dquot for for uid, fs.
*/
if (getdiskquota(uid, ufsvfsp, 0, &xdqp)) {
return (0);
}
dqp = xdqp;
mutex_enter(&dqp->dq_lock);
if (dqp->dq_fsoftlimit == 0 && dqp->dq_fhardlimit == 0) {
dqput(dqp);
mutex_exit(&dqp->dq_lock);
return (0);
}
/*
* Skip checks for uid 0 owned files.
* This check used to require both euid and uid
* to be 0; but there are no quotas for uid 0 so
* it really doesn't matter who is writing to the
* root owned file. And even root can not write
* past the user's quota limit.
*/
if (uid == 0)
goto out;
/*
* Theoretically, this could overflow, but in practice, it
* won't. Multi-terabyte file systems are required to have an
* nbpi value of at least 1MB. In order to overflow this
* field, there would have to be 2^32 inodes in the file.
* That would imply a file system of 2^32 * 1MB, which is
* 2^(32 + 20), which is 4096 terabytes, which is not
* contemplated for ufs any time soon.
*/
ncurfiles = dqp->dq_curfiles + change;
/*
* Dissallow allocation if it would bring the current usage over
* the hard limit or if the user is over their soft limit and their
* time has run out.
*/
if (change == 1 && ncurfiles >= dqp->dq_fhardlimit &&
dqp->dq_fhardlimit && !force) {
/* If the user was not informed yet and the caller */
/* is the owner of the file */
if ((dqp->dq_flags & DQ_FILES) == 0 && uid == crgetruid(cr)) {
errmsg = err1;
dqp->dq_flags |= DQ_FILES;
}
error = EDQUOT;
} else if (change == 1 && ncurfiles >= dqp->dq_fsoftlimit &&
dqp->dq_fsoftlimit) {
now = gethrestime_sec();
if (ncurfiles == dqp->dq_fsoftlimit ||
dqp->dq_ftimelimit == 0) {
dqp->dq_flags |= DQ_MOD;
dqp->dq_ftimelimit = now + ufsvfsp->vfs_ftimelimit;
/* If the caller owns the file */
if (uid == crgetruid(cr))
errmsg = err2;
} else if (now > dqp->dq_ftimelimit && !force) {
/* If the user was not informed yet and the */
/* caller is the owner of the file */
if ((dqp->dq_flags & DQ_FILES) == 0 &&
uid == crgetruid(cr)) {
errmsg = err3;
dqp->dq_flags |= DQ_FILES;
}
error = EDQUOT;
}
}
out:
if (error == 0) {
dqp->dq_flags |= DQ_MOD;
dqp->dq_curfiles += change;
}
if (dqp->dq_flags & DQ_MOD)
TRANS_QUOTA(dqp);
dqput(dqp);
mutex_exit(&dqp->dq_lock);
/*
* Check for any error messages to be sent
*/
if (errmsg != NULL) {
/*
* Send message to the error log.
*/
if (uerrp != NULL) {
/*
* Set up message caller should send to user;
* gets copied to the message buffer as a side-
* effect of the caller's uprintf().
*/
*lenp = strlen(errmsg) + 20 + 20 +
strlen(ufsvfsp->vfs_fs->fs_fsmnt) + 1;
*uerrp = (char *)kmem_alloc(*lenp, KM_NOSLEEP);
if (*uerrp != NULL) {
/* errmsg+1 => skip leading ! */
(void) sprintf(*uerrp, errmsg+1,
(int)ttoproc(curthread)->p_pid,
(int)uid, ufsvfsp->vfs_fs->fs_fsmnt);
}
} else {
/*
* Caller doesn't care, so just copy to the
* message buffer.
*/
cmn_err(CE_NOTE, errmsg,
(int)ttoproc(curthread)->p_pid,
(int)uid, ufsvfsp->vfs_fs->fs_fsmnt);
}
}
return (error);
}
/*
* Release a dquot.
*/
void
dqrele(struct dquot *dqp)
{
/*
* Shadow inodes and extended attribute directories
* do not have quota info records.
*/
if (dqp != NULL) {
mutex_enter(&dqp->dq_lock);
if (dqp->dq_cnt == 1 && dqp->dq_flags & DQ_MOD)
dqupdate(dqp);
dqput(dqp);
mutex_exit(&dqp->dq_lock);
}
}
|