/
ufs_quota.c
981 lines (871 loc) · 22.7 KB
/
ufs_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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/* $NetBSD: ufs_quota.c,v 1.118 2023/02/22 21:49:45 riastradh Exp $ */
/*
* Copyright (c) 1982, 1986, 1990, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Robert Elz at The University of Melbourne.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.118 2023/02/22 21:49:45 riastradh Exp $");
#if defined(_KERNEL_OPT)
#include "opt_quota.h"
#endif
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/namei.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/kauth.h>
#include <sys/quotactl.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ufs/ufs_quota.h>
kmutex_t dqlock;
kcondvar_t dqcv;
const char *quotatypes[MAXQUOTAS] = INITQFNAMES;
/*
* Code pertaining to management of the in-core dquot data structures.
*/
#define DQHASH(dqvp, id) \
(((((long)(dqvp)) >> 8) + id) & dqhash)
static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
static u_long dqhash;
static pool_cache_t dquot_cache;
static int quota_handle_cmd_stat(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_idtypestat(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_objtypestat(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_get(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_put(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_cursorget(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_del(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_quotaon(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_quotaoff(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_cursoropen(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_cursorclose(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_cursorskipidtype(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_cursoratend(struct mount *, struct lwp *,
struct quotactl_args *args);
static int quota_handle_cmd_cursorrewind(struct mount *, struct lwp *,
struct quotactl_args *args);
/*
* Initialize the quota fields of an inode.
*/
void
ufsquota_init(struct inode *ip)
{
int i;
for (i = 0; i < MAXQUOTAS; i++)
ip->i_dquot[i] = NODQUOT;
}
/*
* Release the quota fields from an inode.
*/
void
ufsquota_free(struct inode *ip)
{
int i;
for (i = 0; i < MAXQUOTAS; i++) {
dqrele(ITOV(ip), ip->i_dquot[i]);
ip->i_dquot[i] = NODQUOT;
}
}
/*
* Update disk usage, and take corrective action.
*/
int
chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
{
/* do not track snapshot usage, or we will deadlock */
if ((ip->i_flags & SF_SNAPSHOT) != 0)
return 0;
#ifdef QUOTA
if (ip->i_ump->um_flags & UFS_QUOTA)
return chkdq1(ip, change, cred, flags);
#endif
#ifdef QUOTA2
if (ip->i_ump->um_flags & UFS_QUOTA2)
return chkdq2(ip, change, cred, flags);
#endif
return 0;
}
/*
* Check the inode limit, applying corrective action.
*/
int
chkiq(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
{
/* do not track snapshot usage, or we will deadlock */
if ((ip->i_flags & SF_SNAPSHOT) != 0)
return 0;
#ifdef QUOTA
if (ip->i_ump->um_flags & UFS_QUOTA)
return chkiq1(ip, change, cred, flags);
#endif
#ifdef QUOTA2
if (ip->i_ump->um_flags & UFS_QUOTA2)
return chkiq2(ip, change, cred, flags);
#endif
return 0;
}
int
quota_handle_cmd(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
int error = 0;
switch (args->qc_op) {
case QUOTACTL_STAT:
error = quota_handle_cmd_stat(mp, l, args);
break;
case QUOTACTL_IDTYPESTAT:
error = quota_handle_cmd_idtypestat(mp, l, args);
break;
case QUOTACTL_OBJTYPESTAT:
error = quota_handle_cmd_objtypestat(mp, l, args);
break;
case QUOTACTL_QUOTAON:
error = quota_handle_cmd_quotaon(mp, l, args);
break;
case QUOTACTL_QUOTAOFF:
error = quota_handle_cmd_quotaoff(mp, l, args);
break;
case QUOTACTL_GET:
error = quota_handle_cmd_get(mp, l, args);
break;
case QUOTACTL_PUT:
error = quota_handle_cmd_put(mp, l, args);
break;
case QUOTACTL_CURSORGET:
error = quota_handle_cmd_cursorget(mp, l, args);
break;
case QUOTACTL_DEL:
error = quota_handle_cmd_del(mp, l, args);
break;
case QUOTACTL_CURSOROPEN:
error = quota_handle_cmd_cursoropen(mp, l, args);
break;
case QUOTACTL_CURSORCLOSE:
error = quota_handle_cmd_cursorclose(mp, l, args);
break;
case QUOTACTL_CURSORSKIPIDTYPE:
error = quota_handle_cmd_cursorskipidtype(mp, l, args);
break;
case QUOTACTL_CURSORATEND:
error = quota_handle_cmd_cursoratend(mp, l, args);
break;
case QUOTACTL_CURSORREWIND:
error = quota_handle_cmd_cursorrewind(mp, l, args);
break;
default:
panic("Invalid quotactl operation %d\n", args->qc_op);
}
return error;
}
static int
quota_handle_cmd_stat(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct quotastat *info;
KASSERT(args->qc_op == QUOTACTL_STAT);
info = args->u.stat.qc_info;
if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
return EOPNOTSUPP;
#ifdef QUOTA
if (ump->um_flags & UFS_QUOTA) {
strcpy(info->qs_implname, "ufs/ffs quota v1");
info->qs_numidtypes = MAXQUOTAS;
/* XXX no define for this */
info->qs_numobjtypes = 2;
info->qs_restrictions = 0;
info->qs_restrictions |= QUOTA_RESTRICT_NEEDSQUOTACHECK;
info->qs_restrictions |= QUOTA_RESTRICT_UNIFORMGRACE;
info->qs_restrictions |= QUOTA_RESTRICT_32BIT;
} else
#endif
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
strcpy(info->qs_implname, "ufs/ffs quota v2");
info->qs_numidtypes = MAXQUOTAS;
info->qs_numobjtypes = N_QL;
info->qs_restrictions = 0;
} else
#endif
return EOPNOTSUPP;
return 0;
}
static int
quota_handle_cmd_idtypestat(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
int idtype;
struct quotaidtypestat *info;
const char *name;
KASSERT(args->qc_op == QUOTACTL_IDTYPESTAT);
idtype = args->u.idtypestat.qc_idtype;
info = args->u.idtypestat.qc_info;
if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
return EOPNOTSUPP;
/*
* These are the same for both QUOTA and QUOTA2.
*/
switch (idtype) {
case QUOTA_IDTYPE_USER:
name = "user";
break;
case QUOTA_IDTYPE_GROUP:
name = "group";
break;
default:
return EINVAL;
}
strlcpy(info->qis_name, name, sizeof(info->qis_name));
return 0;
}
static int
quota_handle_cmd_objtypestat(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
int objtype;
struct quotaobjtypestat *info;
const char *name;
int isbytes;
KASSERT(args->qc_op == QUOTACTL_OBJTYPESTAT);
objtype = args->u.objtypestat.qc_objtype;
info = args->u.objtypestat.qc_info;
if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
return EOPNOTSUPP;
/*
* These are the same for both QUOTA and QUOTA2.
*/
switch (objtype) {
case QUOTA_OBJTYPE_BLOCKS:
name = "block";
isbytes = 1;
break;
case QUOTA_OBJTYPE_FILES:
name = "file";
isbytes = 0;
break;
default:
return EINVAL;
}
strlcpy(info->qos_name, name, sizeof(info->qos_name));
info->qos_isbytes = isbytes;
return 0;
}
/* XXX shouldn't all this be in kauth ? */
static int
quota_get_auth(struct mount *mp, struct lwp *l, uid_t id) {
/* The user can always query about his own quota. */
if (id == kauth_cred_geteuid(l->l_cred))
return 0;
return kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(id), NULL);
}
static int
quota_handle_cmd_get(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
int error;
const struct quotakey *qk;
struct quotaval *qv;
KASSERT(args->qc_op == QUOTACTL_GET);
qk = args->u.get.qc_key;
qv = args->u.get.qc_val;
if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
return EOPNOTSUPP;
error = quota_get_auth(mp, l, qk->qk_id);
if (error != 0)
return error;
#ifdef QUOTA
if (ump->um_flags & UFS_QUOTA) {
error = quota1_handle_cmd_get(ump, qk, qv);
} else
#endif
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_get(ump, qk, qv);
} else
#endif
panic("quota_handle_cmd_get: no support ?");
if (error != 0)
return error;
return error;
}
static int
quota_handle_cmd_put(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
const struct quotakey *qk;
const struct quotaval *qv;
id_t kauth_id;
int error;
KASSERT(args->qc_op == QUOTACTL_PUT);
qk = args->u.put.qc_key;
qv = args->u.put.qc_val;
if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
return EOPNOTSUPP;
kauth_id = qk->qk_id;
if (kauth_id == QUOTA_DEFAULTID) {
kauth_id = 0;
}
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
NULL);
if (error != 0) {
return error;
}
#ifdef QUOTA
if (ump->um_flags & UFS_QUOTA)
error = quota1_handle_cmd_put(ump, qk, qv);
else
#endif
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_put(ump, qk, qv);
} else
#endif
panic("quota_handle_cmd_get: no support ?");
if (error == ENOENT) {
error = 0;
}
return error;
}
static int
quota_handle_cmd_del(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
const struct quotakey *qk;
id_t kauth_id;
int error;
KASSERT(args->qc_op == QUOTACTL_DEL);
qk = args->u.del.qc_key;
kauth_id = qk->qk_id;
if (kauth_id == QUOTA_DEFAULTID) {
kauth_id = 0;
}
if ((ump->um_flags & UFS_QUOTA2) == 0)
return EOPNOTSUPP;
/* avoid whitespace changes */
{
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
NULL);
if (error != 0)
goto err;
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_del(ump, qk);
} else
#endif
panic("quota_handle_cmd_get: no support ?");
if (error && error != ENOENT)
goto err;
}
return 0;
err:
return error;
}
static int
quota_handle_cmd_cursorget(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
int error;
KASSERT(args->qc_op == QUOTACTL_CURSORGET);
if ((ump->um_flags & UFS_QUOTA2) == 0)
return EOPNOTSUPP;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
if (error)
return error;
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
struct quotakcursor *cursor = args->u.cursorget.qc_cursor;
struct quotakey *keys = args->u.cursorget.qc_keys;
struct quotaval *vals = args->u.cursorget.qc_vals;
unsigned maxnum = args->u.cursorget.qc_maxnum;
unsigned *ret = args->u.cursorget.qc_ret;
error = quota2_handle_cmd_cursorget(ump, cursor, keys, vals,
maxnum, ret);
} else
#endif
panic("quota_handle_cmd_cursorget: no support ?");
return error;
}
static int
quota_handle_cmd_cursoropen(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
#ifdef QUOTA2
struct ufsmount *ump = VFSTOUFS(mp);
struct quotakcursor *cursor = args->u.cursoropen.qc_cursor;
#endif
int error;
KASSERT(args->qc_op == QUOTACTL_CURSOROPEN);
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
if (error)
return error;
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_cursoropen(ump, cursor);
} else
#endif
error = EOPNOTSUPP;
return error;
}
static int
quota_handle_cmd_cursorclose(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
#ifdef QUOTA2
struct ufsmount *ump = VFSTOUFS(mp);
struct quotakcursor *cursor = args->u.cursorclose.qc_cursor;
#endif
int error;
KASSERT(args->qc_op == QUOTACTL_CURSORCLOSE);
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
if (error)
return error;
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_cursorclose(ump, cursor);
} else
#endif
error = EOPNOTSUPP;
return error;
}
static int
quota_handle_cmd_cursorskipidtype(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
#ifdef QUOTA2
struct ufsmount *ump = VFSTOUFS(mp);
struct quotakcursor *cursor = args->u.cursorskipidtype.qc_cursor;
int idtype = args->u.cursorskipidtype.qc_idtype;
#endif
int error;
KASSERT(args->qc_op == QUOTACTL_CURSORSKIPIDTYPE);
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_cursorskipidtype(ump, cursor, idtype);
} else
#endif
error = EOPNOTSUPP;
return error;
}
static int
quota_handle_cmd_cursoratend(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
#ifdef QUOTA2
struct ufsmount *ump = VFSTOUFS(mp);
struct quotakcursor *cursor = args->u.cursoratend.qc_cursor;
unsigned *ret = args->u.cursoratend.qc_ret;
#endif
int error;
KASSERT(args->qc_op == QUOTACTL_CURSORATEND);
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_cursoratend(ump, cursor, ret);
} else
#endif
error = EOPNOTSUPP;
return error;
}
static int
quota_handle_cmd_cursorrewind(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
#ifdef QUOTA2
struct ufsmount *ump = VFSTOUFS(mp);
struct quotakcursor *cursor = args->u.cursorrewind.qc_cursor;
#endif
int error;
KASSERT(args->qc_op == QUOTACTL_CURSORREWIND);
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
error = quota2_handle_cmd_cursorrewind(ump, cursor);
} else
#endif
error = EOPNOTSUPP;
return error;
}
static int
quota_handle_cmd_quotaon(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
int error;
KASSERT(args->qc_op == QUOTACTL_QUOTAON);
if ((ump->um_flags & UFS_QUOTA2) != 0)
return EBUSY;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
if (error != 0) {
return error;
}
#ifdef QUOTA
int idtype = args->u.quotaon.qc_idtype;
const char *qfile = args->u.quotaon.qc_quotafile;
error = quota1_handle_cmd_quotaon(l, ump, idtype, qfile);
#else
error = EOPNOTSUPP;
#endif
return error;
}
static int
quota_handle_cmd_quotaoff(struct mount *mp, struct lwp *l,
struct quotactl_args *args)
{
struct ufsmount *ump = VFSTOUFS(mp);
int error;
KASSERT(args->qc_op == QUOTACTL_QUOTAOFF);
if ((ump->um_flags & UFS_QUOTA2) != 0)
return EOPNOTSUPP;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
if (error != 0) {
return error;
}
#ifdef QUOTA
int idtype = args->u.quotaoff.qc_idtype;
error = quota1_handle_cmd_quotaoff(l, ump, idtype);
#else
error = EOPNOTSUPP;
#endif
return error;
}
/*
* Initialize the quota system.
*/
void
dqinit(void)
{
mutex_init(&dqlock, MUTEX_DEFAULT, IPL_NONE);
cv_init(&dqcv, "quota");
dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "ufsdq",
NULL, IPL_NONE, NULL, NULL, NULL);
}
void
dqreinit(void)
{
struct dquot *dq;
struct dqhashhead *oldhash, *hash;
struct vnode *dqvp;
u_long oldmask, mask, hashval;
int i;
hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
mutex_enter(&dqlock);
oldhash = dqhashtbl;
oldmask = dqhash;
dqhashtbl = hash;
dqhash = mask;
for (i = 0; i <= oldmask; i++) {
while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
dqvp = dq->dq_ump->um_quotas[dq->dq_type];
LIST_REMOVE(dq, dq_hash);
hashval = DQHASH(dqvp, dq->dq_id);
LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
}
}
mutex_exit(&dqlock);
hashdone(oldhash, HASH_LIST, oldmask);
}
/*
* Free resources held by quota system.
*/
void
dqdone(void)
{
pool_cache_destroy(dquot_cache);
hashdone(dqhashtbl, HASH_LIST, dqhash);
cv_destroy(&dqcv);
mutex_destroy(&dqlock);
}
/*
* Set up the quotas for an inode.
*
* This routine completely defines the semantics of quotas.
* If other criterion want to be used to establish quotas, the
* MAXQUOTAS value in quotas.h should be increased, and the
* additional dquots set up here.
*/
int
getinoquota(struct inode *ip)
{
struct ufsmount *ump = ip->i_ump;
struct vnode *vp = ITOV(ip);
int i, error;
u_int32_t ino_ids[MAXQUOTAS];
/*
* To avoid deadlocks never update quotas for quota files
* on the same file system
*/
for (i = 0; i < MAXQUOTAS; i++)
if (vp == ump->um_quotas[i])
return 0;
ino_ids[USRQUOTA] = ip->i_uid;
ino_ids[GRPQUOTA] = ip->i_gid;
for (i = 0; i < MAXQUOTAS; i++) {
/*
* If the file id changed the quota needs update.
*/
if (ip->i_dquot[i] != NODQUOT &&
ip->i_dquot[i]->dq_id != ino_ids[i]) {
dqrele(ITOV(ip), ip->i_dquot[i]);
ip->i_dquot[i] = NODQUOT;
}
/*
* Set up the quota based on file id.
* ENODEV means that quotas are not enabled.
*/
if (ip->i_dquot[i] == NODQUOT &&
(error = dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
error != ENODEV)
return (error);
}
return 0;
}
/*
* Obtain a dquot structure for the specified identifier and quota file
* reading the information from the file if necessary.
*/
int
dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
struct dquot **dqp)
{
struct dquot *dq, *ndq;
struct dqhashhead *dqh;
struct vnode *dqvp;
int error = 0; /* XXX gcc */
/* Lock to see an up to date value for QTF_CLOSING. */
mutex_enter(&dqlock);
if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0) {
mutex_exit(&dqlock);
*dqp = NODQUOT;
return (ENODEV);
}
dqvp = ump->um_quotas[type];
#ifdef QUOTA
if (ump->um_flags & UFS_QUOTA) {
if (dqvp == NULLVP || (ump->umq1_qflags[type] & QTF_CLOSING)) {
mutex_exit(&dqlock);
*dqp = NODQUOT;
return (ENODEV);
}
}
#endif
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2) {
if (dqvp == NULLVP) {
mutex_exit(&dqlock);
*dqp = NODQUOT;
return (ENODEV);
}
}
#endif
KASSERT(dqvp != vp);
/*
* Check the cache first.
*/
dqh = &dqhashtbl[DQHASH(dqvp, id)];
LIST_FOREACH(dq, dqh, dq_hash) {
if (dq->dq_id != id ||
dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
continue;
KASSERT(dq->dq_cnt > 0);
dqref(dq);
mutex_exit(&dqlock);
*dqp = dq;
return (0);
}
/*
* Not in cache, allocate a new one.
*/
mutex_exit(&dqlock);
ndq = pool_cache_get(dquot_cache, PR_WAITOK);
/*
* Initialize the contents of the dquot structure.
*/
memset((char *)ndq, 0, sizeof *ndq);
ndq->dq_flags = 0;
ndq->dq_id = id;
ndq->dq_ump = ump;
ndq->dq_type = type;
mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
mutex_enter(&dqlock);
dqh = &dqhashtbl[DQHASH(dqvp, id)];
LIST_FOREACH(dq, dqh, dq_hash) {
if (dq->dq_id != id ||
dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
continue;
/*
* Another thread beat us allocating this dquot.
*/
KASSERT(dq->dq_cnt > 0);
dqref(dq);
mutex_exit(&dqlock);
mutex_destroy(&ndq->dq_interlock);
pool_cache_put(dquot_cache, ndq);
*dqp = dq;
return 0;
}
dq = ndq;
LIST_INSERT_HEAD(dqh, dq, dq_hash);
dqref(dq);
mutex_enter(&dq->dq_interlock);
mutex_exit(&dqlock);
#ifdef QUOTA
if (ump->um_flags & UFS_QUOTA)
error = dq1get(dqvp, id, ump, type, dq);
#endif
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2)
error = dq2get(dqvp, id, ump, type, dq);
#endif
/*
* I/O error in reading quota file, release
* quota structure and reflect problem to caller.
*/
if (error) {
mutex_enter(&dqlock);
LIST_REMOVE(dq, dq_hash);
mutex_exit(&dqlock);
mutex_exit(&dq->dq_interlock);
dqrele(vp, dq);
*dqp = NODQUOT;
return (error);
}
mutex_exit(&dq->dq_interlock);
*dqp = dq;
return (0);
}
/*
* Obtain a reference to a dquot.
*/
void
dqref(struct dquot *dq)
{
KASSERT(mutex_owned(&dqlock));
dq->dq_cnt++;
KASSERT(dq->dq_cnt > 0);
}
/*
* Release a reference to a dquot.
*/
void
dqrele(struct vnode *vp, struct dquot *dq)
{
if (dq == NODQUOT)
return;
mutex_enter(&dq->dq_interlock);
for (;;) {
mutex_enter(&dqlock);
if (dq->dq_cnt > 1) {
dq->dq_cnt--;
mutex_exit(&dqlock);
mutex_exit(&dq->dq_interlock);
return;
}
if ((dq->dq_flags & DQ_MOD) == 0)
break;
mutex_exit(&dqlock);
#ifdef QUOTA
if (dq->dq_ump->um_flags & UFS_QUOTA)
(void) dq1sync(vp, dq);
#endif
#ifdef QUOTA2
if (dq->dq_ump->um_flags & UFS_QUOTA2)
(void) dq2sync(vp, dq);
#endif
}
KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
LIST_REMOVE(dq, dq_hash);
mutex_exit(&dqlock);
mutex_exit(&dq->dq_interlock);
mutex_destroy(&dq->dq_interlock);
pool_cache_put(dquot_cache, dq);
}
int
qsync(struct mount *mp)
{
struct ufsmount *ump = VFSTOUFS(mp);
#ifdef QUOTA
if (ump->um_flags & UFS_QUOTA)
return q1sync(mp);
#endif
#ifdef QUOTA2
if (ump->um_flags & UFS_QUOTA2)
return q2sync(mp);
#endif
return 0;
}
#ifdef DIAGNOSTIC
/*
* Check the hash chains for stray dquot's.
*/
void
dqflush(struct vnode *vp)
{
struct dquot *dq;
int i;
mutex_enter(&dqlock);
for (i = 0; i <= dqhash; i++)
LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
mutex_exit(&dqlock);
}
#endif