4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * Code pertaining to management of the in-core data structures.
45 #include <sys/types.h>
46 #include <sys/t_lock.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/signal.h>
50 #include <sys/errno.h>
54 #include <sys/vnode.h>
57 #include <sys/fs/ufs_fs.h>
58 #include <sys/fs/ufs_inode.h>
59 #include <sys/fs/ufs_quota.h>
60 #include <sys/cmn_err.h>
62 #include <sys/debug.h>
64 #include <sys/fs/ufs_panic.h>
69 * Dquot in core hash chain headers
71 struct dqhead dqhead
[NDQHASH
];
73 static kmutex_t dq_cachelock
;
74 static kmutex_t dq_freelock
;
81 struct dquot dqfreelist
;
83 #define dqinsheadfree(DQP) { \
84 mutex_enter(&dq_freelock); \
85 (DQP)->dq_freef = dqfreelist.dq_freef; \
86 (DQP)->dq_freeb = &dqfreelist; \
87 dqfreelist.dq_freef->dq_freeb = (DQP); \
88 dqfreelist.dq_freef = (DQP); \
89 mutex_exit(&dq_freelock); \
92 #define dqinstailfree(DQP) { \
93 mutex_enter(&dq_freelock); \
94 (DQP)->dq_freeb = dqfreelist.dq_freeb; \
95 (DQP)->dq_freef = &dqfreelist; \
96 dqfreelist.dq_freeb->dq_freef = (DQP); \
97 dqfreelist.dq_freeb = (DQP); \
98 mutex_exit(&dq_freelock); \
101 /* (clear pointers to make sure we don't use them; catch problems early) */
102 #define dqremfree(DQP) { \
103 (DQP)->dq_freeb->dq_freef = (DQP)->dq_freef; \
104 (DQP)->dq_freef->dq_freeb = (DQP)->dq_freeb; \
105 (DQP)->dq_freef = (DQP)->dq_freeb = NULL; \
108 typedef struct dquot
*DQptr
;
111 * Initialize quota sub-system init lock.
116 rw_init(&dq_rwlock
, NULL
, RW_DEFAULT
, NULL
);
120 * qtinit2 allocated space for the quota structures. Only do this if
121 * if quotas are going to be used so that we can save the space if quotas
127 register struct dqhead
*dhp
;
128 register struct dquot
*dqp
;
130 ASSERT(RW_WRITE_HELD(&dq_rwlock
));
133 ndquot
= ((maxusers
* NMOUNT
) / 4) + v
.v_proc
;
135 dquot
= kmem_zalloc(ndquot
* sizeof (struct dquot
), KM_SLEEP
);
136 dquotNDQUOT
= dquot
+ ndquot
;
139 * Initialize the cache between the in-core structures
140 * and the per-file system quota files on disk.
142 for (dhp
= &dqhead
[0]; dhp
< &dqhead
[NDQHASH
]; dhp
++) {
143 dhp
->dqh_forw
= dhp
->dqh_back
= (DQptr
)dhp
;
145 dqfreelist
.dq_freef
= dqfreelist
.dq_freeb
= (DQptr
)&dqfreelist
;
146 for (dqp
= dquot
; dqp
< dquotNDQUOT
; dqp
++) {
147 mutex_init(&dqp
->dq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
148 dqp
->dq_forw
= dqp
->dq_back
= dqp
;
154 * Obtain the user's on-disk quota limit for file system specified.
155 * dqpp is returned locked.
160 struct ufsvfs
*ufsvfsp
,
161 int force
, /* don't do enable checks */
162 struct dquot
**dqpp
) /* resulting dquot ptr */
168 extern struct cred
*kcred
;
173 ASSERT(RW_LOCK_HELD(&ufsvfsp
->vfs_dqrwlock
));
175 dhp
= &dqhead
[DQHASH(uid
, ufsvfsp
)];
178 * Check for quotas enabled.
180 if ((ufsvfsp
->vfs_qflags
& MQ_ENABLED
) == 0 && !force
)
182 qip
= ufsvfsp
->vfs_qinod
;
184 return (ufs_fault(ufsvfsp
->vfs_root
, "getdiskquota: NULL qip"));
186 * Check the cache first.
188 mutex_enter(&dq_cachelock
);
189 for (dqp
= dhp
->dqh_forw
; dqp
!= (DQptr
)dhp
; dqp
= dqp
->dq_forw
) {
190 if (dqp
->dq_uid
!= uid
|| dqp
->dq_ufsvfsp
!= ufsvfsp
)
192 mutex_exit(&dq_cachelock
);
193 mutex_enter(&dqp
->dq_lock
);
195 * I may have slept in the mutex_enter. Make sure this is
196 * still the one I want.
198 if (dqp
->dq_uid
!= uid
|| dqp
->dq_ufsvfsp
!= ufsvfsp
) {
199 mutex_exit(&dqp
->dq_lock
);
202 if (dqp
->dq_flags
& DQ_ERROR
) {
203 mutex_exit(&dqp
->dq_lock
);
207 * Cache hit with no references.
208 * Take the structure off the free list.
210 if (dqp
->dq_cnt
== 0) {
211 mutex_enter(&dq_freelock
);
213 mutex_exit(&dq_freelock
);
216 mutex_exit(&dqp
->dq_lock
);
222 * Get dquot at head of free list.
224 mutex_enter(&dq_freelock
);
225 if ((dqp
= dqfreelist
.dq_freef
) == &dqfreelist
) {
226 mutex_exit(&dq_freelock
);
227 mutex_exit(&dq_cachelock
);
228 cmn_err(CE_WARN
, "dquot table full");
232 if (dqp
->dq_cnt
!= 0 || dqp
->dq_flags
!= 0) {
233 panic("getdiskquota: dqp->dq_cnt: "
234 "%ld != 0 || dqp->dq_flags: 0x%x != 0 (%s)",
235 dqp
->dq_cnt
, dqp
->dq_flags
, qip
->i_fs
->fs_fsmnt
);
239 * Take it off the free list, and off the hash chain it was on.
240 * Then put it on the new hash chain.
243 mutex_exit(&dq_freelock
);
247 dqp
->dq_ufsvfsp
= ufsvfsp
;
248 dqp
->dq_mof
= UFS_HOLE
;
249 mutex_enter(&dqp
->dq_lock
);
251 mutex_exit(&dq_cachelock
);
253 * Check the uid in case it's too large to fit into the 2Gbyte
254 * 'quotas' file (higher than 67 million or so).
258 * Large Files: i_size need to be accessed atomically now.
260 rw_enter(&qip
->i_contents
, RW_READER
);
261 if (uid
<= MAXUID
&& dqoff(uid
) >= 0 && dqoff(uid
) < qip
->i_size
) {
263 * Read quota info off disk.
265 error
= ufs_rdwri(UIO_READ
, FREAD
, qip
, (caddr_t
)&dqp
->dq_dqb
,
266 sizeof (struct dqblk
), dqoff(uid
), UIO_SYSSPACE
,
269 * We must set the dq_mof even if not we are not logging in case
270 * we are later remount to logging.
272 err
= bmap_read(qip
, dqoff(uid
), &bn
, &contig
);
273 rw_exit(&qip
->i_contents
);
274 if ((bn
!= UFS_HOLE
) && !err
) {
275 dqp
->dq_mof
= ldbtob(bn
) +
276 (offset_t
)(dqoff(uid
) & (DEV_BSIZE
- 1));
278 dqp
->dq_mof
= UFS_HOLE
;
282 * I/O error in reading quota file.
283 * Put dquot on a private, unfindable hash list,
284 * put dquot at the head of the free list and
285 * reflect the problem to caller.
287 dqp
->dq_flags
= DQ_ERROR
;
289 * I must exit the dq_lock so that I can acquire the
290 * dq_cachelock. If another thread finds dqp before
291 * I remove it from the cache it will see the
292 * DQ_ERROR and just return EIO.
294 mutex_exit(&dqp
->dq_lock
);
295 mutex_enter(&dq_cachelock
);
296 mutex_enter(&dqp
->dq_lock
);
298 mutex_exit(&dqp
->dq_lock
);
299 mutex_exit(&dq_cachelock
);
301 * Don't bother reacquiring dq_lock because the dq is
302 * not on the freelist or in the cache so only I have
306 dqp
->dq_ufsvfsp
= NULL
;
309 dqp
->dq_mof
= UFS_HOLE
;
315 rw_exit(&qip
->i_contents
); /* done with i_size */
316 bzero(&dqp
->dq_dqb
, sizeof (struct dqblk
));
317 dqp
->dq_mof
= UFS_HOLE
;
319 mutex_exit(&dqp
->dq_lock
);
329 register struct dquot
*dqp
;
332 ASSERT(dqp
->dq_ufsvfsp
== NULL
||
333 RW_LOCK_HELD(&dqp
->dq_ufsvfsp
->vfs_dqrwlock
));
334 ASSERT(MUTEX_HELD(&dqp
->dq_lock
));
335 if (dqp
->dq_cnt
== 0) {
337 dqp
->dq_ufsvfsp
&& dqp
->dq_ufsvfsp
->vfs_root
?
338 dqp
->dq_ufsvfsp
->vfs_root
: NULL
,
339 "dqput: dqp->dq_cnt == 0");
342 if (--dqp
->dq_cnt
== 0) {
343 if (dqp
->dq_flags
& DQ_MOD
)
346 * DQ_MOD was cleared by dqupdate().
347 * DQ_ERROR shouldn't be set if this dquot was being used.
348 * DQ_FILES/DQ_BLKS don't matter at this point.
351 if (dqp
->dq_ufsvfsp
== NULL
||
352 dqp
->dq_ufsvfsp
->vfs_qflags
== 0) {
353 /* quotas are disabled, discard this dquot struct */
361 * Update on disk quota info.
365 register struct dquot
*dqp
;
367 register struct inode
*qip
;
368 extern struct cred
*kcred
;
369 struct ufsvfs
*ufsvfsp
;
371 struct vnode
*vfs_root
;
373 ASSERT(MUTEX_HELD(&dqp
->dq_lock
));
375 if (!dqp
->dq_ufsvfsp
) {
376 (void) ufs_fault(NULL
, "dqupdate: NULL dq_ufsvfsp");
379 vfs_root
= dqp
->dq_ufsvfsp
->vfs_root
;
381 (void) ufs_fault(NULL
, "dqupdate: NULL vfs_root");
385 * I don't need to hold dq_rwlock when looking at vfs_qinod here
386 * because vfs_qinod is only cleared by closedq after it has called
387 * dqput on all dq's. Since I am holding dq_lock on this dq, closedq
388 * will have to wait until I am done before it can call dqput on
389 * this dq so vfs_qinod will not change value until after I return.
391 qip
= dqp
->dq_ufsvfsp
->vfs_qinod
;
393 (void) ufs_fault(vfs_root
, "dqupdate: NULL vfs_qinod");
396 ufsvfsp
= qip
->i_ufsvfs
;
398 (void) ufs_fault(vfs_root
,
399 "dqupdate: NULL vfs_qinod->i_ufsvfs");
402 if (ufsvfsp
!= dqp
->dq_ufsvfsp
) {
403 (void) ufs_fault(vfs_root
,
404 "dqupdate: vfs_qinod->i_ufsvfs != dqp->dq_ufsvfsp");
407 if (!(dqp
->dq_flags
& DQ_MOD
)) {
408 (void) ufs_fault(vfs_root
,
409 "dqupdate: !(dqp->dq_flags & DQ_MOD)");
413 if (!(curthread
->t_flag
& T_DONTBLOCK
)) {
415 curthread
->t_flag
|= T_DONTBLOCK
;
416 TRANS_BEGIN_ASYNC(ufsvfsp
, TOP_QUOTA
, TOP_QUOTA_SIZE
);
418 if (TRANS_ISTRANS(ufsvfsp
)) {
419 TRANS_DELTA(ufsvfsp
, dqp
->dq_mof
, sizeof (struct dqblk
),
421 TRANS_LOG(ufsvfsp
, (caddr_t
)&dqp
->dq_dqb
, dqp
->dq_mof
,
422 (int)(sizeof (struct dqblk
)), NULL
, 0);
425 * Locknest gets very confused when I lock the quota inode.
426 * It thinks that qip and ip (the inode that caused the
427 * quota routines to get called) are the same inode.
429 rw_enter(&qip
->i_contents
, RW_WRITER
);
431 * refuse to push if offset would be illegal
433 if (dqoff(dqp
->dq_uid
) >= 0) {
434 (void) ufs_rdwri(UIO_WRITE
, FWRITE
, qip
,
435 (caddr_t
)&dqp
->dq_dqb
,
436 sizeof (struct dqblk
),
437 dqoff(dqp
->dq_uid
), UIO_SYSSPACE
,
440 rw_exit(&qip
->i_contents
);
443 dqp
->dq_flags
&= ~DQ_MOD
;
445 TRANS_END_ASYNC(ufsvfsp
, TOP_QUOTA
, TOP_QUOTA_SIZE
);
446 curthread
->t_flag
&= ~T_DONTBLOCK
;
451 * Invalidate a dquot. This function is called when quotas are disabled
452 * for a specific file system via closedq() or when we unmount the file
453 * system and invalidate the quota cache via invalidatedq().
455 * Take the dquot off its hash list and put it on a private, unfindable
456 * hash list (refers to itself). Also, put it at the head of the free list.
457 * Note that even though dq_cnt is zero, this dquot is NOT yet on the
462 register struct dquot
*dqp
;
464 ASSERT(MUTEX_HELD(&dqp
->dq_lock
));
465 ASSERT(dqp
->dq_cnt
== 0);
466 ASSERT(dqp
->dq_flags
== 0);
467 ASSERT(dqp
->dq_freef
== NULL
&& dqp
->dq_freeb
== NULL
);
468 ASSERT(dqp
->dq_ufsvfsp
&&
469 (dqp
->dq_ufsvfsp
->vfs_qflags
& MQ_ENABLED
) == 0);
472 * To preserve lock order, we have to drop dq_lock in order to
473 * grab dq_cachelock. To prevent someone from grabbing this
474 * dquot from the quota cache via getdiskquota() while we are
475 * "unsafe", we clear dq_ufsvfsp so it won't match anything.
477 dqp
->dq_ufsvfsp
= NULL
;
478 mutex_exit(&dqp
->dq_lock
);
479 mutex_enter(&dq_cachelock
);
480 mutex_enter(&dqp
->dq_lock
);
483 * The following paranoia is to make sure that getdiskquota()
484 * has not been broken:
486 ASSERT(dqp
->dq_cnt
== 0);
487 ASSERT(dqp
->dq_flags
== 0);
488 ASSERT(dqp
->dq_freef
== NULL
&& dqp
->dq_freeb
== NULL
);
489 ASSERT(dqp
->dq_ufsvfsp
== NULL
);
492 * Now we have the locks in the right order so we can do the
496 mutex_exit(&dq_cachelock
);
503 * Invalidate all quota information records for the specified file system.
506 invalidatedq(ufsvfsp
)
507 register struct ufsvfs
*ufsvfsp
;
509 register struct dquot
*dqp
;
513 * If quotas are not initialized, then there is nothing to do.
515 rw_enter(&dq_rwlock
, RW_READER
);
516 if (!quotas_initialized
) {
523 rw_enter(&ufsvfsp
->vfs_dqrwlock
, RW_WRITER
);
525 ASSERT((ufsvfsp
->vfs_qflags
& MQ_ENABLED
) == 0);
528 * Invalidate all the quota info records for this file system
529 * that are in the quota cache:
531 for (dqp
= dquot
; dqp
< dquotNDQUOT
; dqp
++) {
533 * If someone else has it, then ignore it. For the target
534 * file system, this is okay for three reasons:
536 * 1) This routine is called after closedq() so the quota
537 * sub-system is disabled for this file system.
538 * 2) We have made the quota sub-system quiescent for
540 * 3) We are in the process of unmounting this file
541 * system so the quota sub-system can't be enabled
544 if (!mutex_tryenter(&dqp
->dq_lock
)) {
550 * At this point, any quota info records that are
551 * associated with the target file system, should have a
552 * reference count of zero and be on the free list.
553 * Why? Because these quota info records went to a zero
554 * dq_cnt (via dqput()) before the file system was
555 * unmounted and are waiting to be found in the quota
556 * cache and reused (via getdiskquota()). The exception
557 * is when a quota transaction is sitting in the deltamap,
558 * indicated by DQ_TRANS being set in dq_flags.
559 * This causes a reference to be held on the quota
560 * information record and it will only be cleared once
561 * the transaction has reached the log. If we find
562 * any of these - we ignore them and let logging do
565 if (dqp
->dq_ufsvfsp
== ufsvfsp
) {
566 ASSERT(dqp
->dq_cnt
== 0 || (dqp
->dq_cnt
== 1 &&
567 (dqp
->dq_flags
& DQ_TRANS
)));
569 /* Cope with those orphaned dquots. */
570 if (dqp
->dq_cnt
== 1 && (dqp
->dq_flags
& DQ_TRANS
)) {
571 mutex_exit(&dqp
->dq_lock
);
575 ASSERT(dqp
->dq_cnt
== 0);
576 ASSERT(dqp
->dq_freef
&& dqp
->dq_freeb
);
579 * Take the quota info record off the free list
580 * so dqinval() can do its job (and put it on the
581 * front of the free list).
583 mutex_enter(&dq_freelock
);
585 mutex_exit(&dq_freelock
);
589 mutex_exit(&dqp
->dq_lock
);
591 rw_exit(&ufsvfsp
->vfs_dqrwlock
);