2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "xfs_trans.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
49 #include "xfs_buf_item.h"
50 #include "xfs_trans_priv.h"
54 * returns the number of iovecs needed to log the given dquot item.
58 xfs_qm_dquot_logitem_size(
59 xfs_dq_logitem_t *logitem)
62 * we need only two iovecs, one for the format, one for the real thing
68 * fills in the vector of log iovecs for the given dquot log item.
71 xfs_qm_dquot_logitem_format(
72 xfs_dq_logitem_t *logitem,
73 xfs_log_iovec_t *logvec)
76 ASSERT(logitem->qli_dquot);
78 logvec->i_addr = (xfs_caddr_t)&logitem->qli_format;
79 logvec->i_len = sizeof(xfs_dq_logformat_t);
80 XLOG_VEC_SET_TYPE(logvec, XLOG_REG_TYPE_QFORMAT);
82 logvec->i_addr = (xfs_caddr_t)&logitem->qli_dquot->q_core;
83 logvec->i_len = sizeof(xfs_disk_dquot_t);
84 XLOG_VEC_SET_TYPE(logvec, XLOG_REG_TYPE_DQUOT);
86 ASSERT(2 == logitem->qli_item.li_desc->lid_size);
87 logitem->qli_format.qlf_size = 2;
92 * Increment the pin count of the given dquot.
93 * This value is protected by pinlock spinlock in the xQM structure.
96 xfs_qm_dquot_logitem_pin(
97 xfs_dq_logitem_t *logitem)
102 dqp = logitem->qli_dquot;
103 ASSERT(XFS_DQ_IS_LOCKED(dqp));
104 s = XFS_DQ_PINLOCK(dqp);
106 XFS_DQ_PINUNLOCK(dqp, s);
110 * Decrement the pin count of the given dquot, and wake up
111 * anyone in xfs_dqwait_unpin() if the count goes to 0. The
112 * dquot must have been previously pinned with a call to xfs_dqpin().
116 xfs_qm_dquot_logitem_unpin(
117 xfs_dq_logitem_t *logitem,
123 dqp = logitem->qli_dquot;
124 ASSERT(dqp->q_pincount > 0);
125 s = XFS_DQ_PINLOCK(dqp);
127 if (dqp->q_pincount == 0) {
128 sv_broadcast(&dqp->q_pinwait);
130 XFS_DQ_PINUNLOCK(dqp, s);
135 xfs_qm_dquot_logitem_unpin_remove(
136 xfs_dq_logitem_t *logitem,
139 xfs_qm_dquot_logitem_unpin(logitem, 0);
143 * Given the logitem, this writes the corresponding dquot entry to disk
144 * asynchronously. This is called with the dquot entry securely locked;
145 * we simply get xfs_qm_dqflush() to do the work, and unlock the dquot
149 xfs_qm_dquot_logitem_push(
150 xfs_dq_logitem_t *logitem)
154 dqp = logitem->qli_dquot;
156 ASSERT(XFS_DQ_IS_LOCKED(dqp));
157 ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp));
160 * Since we were able to lock the dquot's flush lock and
161 * we found it on the AIL, the dquot must be dirty. This
162 * is because the dquot is removed from the AIL while still
163 * holding the flush lock in xfs_dqflush_done(). Thus, if
164 * we found it in the AIL and were able to obtain the flush
165 * lock without sleeping, then there must not have been
166 * anyone in the process of flushing the dquot.
168 xfs_qm_dqflush(dqp, XFS_B_DELWRI);
174 xfs_qm_dquot_logitem_committed(
179 * We always re-log the entire dquot when it becomes dirty,
180 * so, the latest copy _is_ the only one that matters.
187 * This is called to wait for the given dquot to be unpinned.
188 * Most of these pin/unpin routines are plagiarized from inode code.
196 ASSERT(XFS_DQ_IS_LOCKED(dqp));
197 if (dqp->q_pincount == 0) {
202 * Give the log a push so we don't wait here too long.
204 xfs_log_force(dqp->q_mount, (xfs_lsn_t)0, XFS_LOG_FORCE);
205 s = XFS_DQ_PINLOCK(dqp);
206 if (dqp->q_pincount == 0) {
207 XFS_DQ_PINUNLOCK(dqp, s);
210 sv_wait(&(dqp->q_pinwait), PINOD,
211 &(XFS_DQ_TO_QINF(dqp)->qi_pinlock), s);
215 * This is called when IOP_TRYLOCK returns XFS_ITEM_PUSHBUF to indicate that
216 * the dquot is locked by us, but the flush lock isn't. So, here we are
217 * going to see if the relevant dquot buffer is incore, waiting on DELWRI.
218 * If so, we want to push it out to help us take this item off the AIL as soon
221 * We must not be holding the AIL_LOCK at this point. Calling incore() to
222 * search the buffer cache can be a time consuming thing, and AIL_LOCK is a
226 xfs_qm_dquot_logitem_pushbuf(
227 xfs_dq_logitem_t *qip)
234 dqp = qip->qli_dquot;
235 ASSERT(XFS_DQ_IS_LOCKED(dqp));
238 * The qli_pushbuf_flag keeps others from
239 * trying to duplicate our effort.
241 ASSERT(qip->qli_pushbuf_flag != 0);
242 ASSERT(qip->qli_push_owner == current_pid());
245 * If flushlock isn't locked anymore, chances are that the
246 * inode flush completed and the inode was taken off the AIL.
249 if (!issemalocked(&(dqp->q_flock)) ||
250 ((qip->qli_item.li_flags & XFS_LI_IN_AIL) == 0)) {
251 qip->qli_pushbuf_flag = 0;
256 bp = xfs_incore(mp->m_ddev_targp, qip->qli_format.qlf_blkno,
257 XFS_QI_DQCHUNKLEN(mp),
260 if (XFS_BUF_ISDELAYWRITE(bp)) {
261 dopush = ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
262 issemalocked(&(dqp->q_flock)));
263 qip->qli_pushbuf_flag = 0;
266 if (XFS_BUF_ISPINNED(bp)) {
267 xfs_log_force(mp, (xfs_lsn_t)0,
280 qip->qli_pushbuf_flag = 0;
287 qip->qli_pushbuf_flag = 0;
292 * This is called to attempt to lock the dquot associated with this
293 * dquot log item. Don't sleep on the dquot lock or the flush lock.
294 * If the flush lock is already held, indicating that the dquot has
295 * been or is in the process of being flushed, then see if we can
296 * find the dquot's buffer in the buffer cache without sleeping. If
297 * we can and it is marked delayed write, then we want to send it out.
298 * We delay doing so until the push routine, though, to avoid sleeping
299 * in any device strategy routines.
302 xfs_qm_dquot_logitem_trylock(
303 xfs_dq_logitem_t *qip)
308 dqp = qip->qli_dquot;
309 if (dqp->q_pincount > 0)
310 return (XFS_ITEM_PINNED);
312 if (! xfs_qm_dqlock_nowait(dqp))
313 return (XFS_ITEM_LOCKED);
315 retval = XFS_ITEM_SUCCESS;
316 if (! xfs_qm_dqflock_nowait(dqp)) {
318 * The dquot is already being flushed. It may have been
319 * flushed delayed write, however, and we don't want to
320 * get stuck waiting for that to complete. So, we want to check
321 * to see if we can lock the dquot's buffer without sleeping.
322 * If we can and it is marked for delayed write, then we
323 * hold it and send it out from the push routine. We don't
324 * want to do that now since we might sleep in the device
325 * strategy routine. We also don't want to grab the buffer lock
326 * here because we'd like not to call into the buffer cache
327 * while holding the AIL_LOCK.
328 * Make sure to only return PUSHBUF if we set pushbuf_flag
329 * ourselves. If someone else is doing it then we don't
330 * want to go to the push routine and duplicate their efforts.
332 if (qip->qli_pushbuf_flag == 0) {
333 qip->qli_pushbuf_flag = 1;
334 ASSERT(qip->qli_format.qlf_blkno == dqp->q_blkno);
336 qip->qli_push_owner = current_pid();
339 * The dquot is left locked.
341 retval = XFS_ITEM_PUSHBUF;
343 retval = XFS_ITEM_FLUSHING;
344 xfs_dqunlock_nonotify(dqp);
348 ASSERT(qip->qli_item.li_flags & XFS_LI_IN_AIL);
354 * Unlock the dquot associated with the log item.
355 * Clear the fields of the dquot and dquot log item that
356 * are specific to the current transaction. If the
357 * hold flags is set, do not unlock the dquot.
360 xfs_qm_dquot_logitem_unlock(
361 xfs_dq_logitem_t *ql)
367 ASSERT(XFS_DQ_IS_LOCKED(dqp));
370 * Clear the transaction pointer in the dquot
372 dqp->q_transp = NULL;
375 * dquots are never 'held' from getting unlocked at the end of
376 * a transaction. Their locking and unlocking is hidden inside the
377 * transaction layer, within trans_commit. Hence, no LI_HOLD flag
385 * The transaction with the dquot locked has aborted. The dquot
386 * must not be dirty within the transaction. We simply unlock just
387 * as if the transaction had been cancelled.
390 xfs_qm_dquot_logitem_abort(
391 xfs_dq_logitem_t *ql)
393 xfs_qm_dquot_logitem_unlock(ql);
397 * this needs to stamp an lsn into the dquot, I think.
398 * rpc's that look at user dquot's would then have to
399 * push on the dependency recorded in the dquot
403 xfs_qm_dquot_logitem_committing(
412 * This is the ops vector for dquots
414 STATIC struct xfs_item_ops xfs_dquot_item_ops = {
415 .iop_size = (uint(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_size,
416 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
417 xfs_qm_dquot_logitem_format,
418 .iop_pin = (void(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_pin,
419 .iop_unpin = (void(*)(xfs_log_item_t*, int))
420 xfs_qm_dquot_logitem_unpin,
421 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
422 xfs_qm_dquot_logitem_unpin_remove,
423 .iop_trylock = (uint(*)(xfs_log_item_t*))
424 xfs_qm_dquot_logitem_trylock,
425 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_unlock,
426 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
427 xfs_qm_dquot_logitem_committed,
428 .iop_push = (void(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_push,
429 .iop_abort = (void(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_abort,
430 .iop_pushbuf = (void(*)(xfs_log_item_t*))
431 xfs_qm_dquot_logitem_pushbuf,
432 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
433 xfs_qm_dquot_logitem_committing
437 * Initialize the dquot log item for a newly allocated dquot.
438 * The dquot isn't locked at this point, but it isn't on any of the lists
439 * either, so we don't care.
442 xfs_qm_dquot_logitem_init(
443 struct xfs_dquot *dqp)
445 xfs_dq_logitem_t *lp;
446 lp = &dqp->q_logitem;
448 lp->qli_item.li_type = XFS_LI_DQUOT;
449 lp->qli_item.li_ops = &xfs_dquot_item_ops;
450 lp->qli_item.li_mountp = dqp->q_mount;
452 lp->qli_format.qlf_type = XFS_LI_DQUOT;
453 lp->qli_format.qlf_id = be32_to_cpu(dqp->q_core.d_id);
454 lp->qli_format.qlf_blkno = dqp->q_blkno;
455 lp->qli_format.qlf_len = 1;
457 * This is just the offset of this dquot within its buffer
458 * (which is currently 1 FSB and probably won't change).
459 * Hence 32 bits for this offset should be just fine.
460 * Alternatively, we can store (bufoffset / sizeof(xfs_dqblk_t))
461 * here, and recompute it at recovery time.
463 lp->qli_format.qlf_boffset = (__uint32_t)dqp->q_bufoffset;
466 /*------------------ QUOTAOFF LOG ITEMS -------------------*/
469 * This returns the number of iovecs needed to log the given quotaoff item.
470 * We only need 1 iovec for an quotaoff item. It just logs the
471 * quotaoff_log_format structure.
475 xfs_qm_qoff_logitem_size(xfs_qoff_logitem_t *qf)
481 * This is called to fill in the vector of log iovecs for the
482 * given quotaoff log item. We use only 1 iovec, and we point that
483 * at the quotaoff_log_format structure embedded in the quotaoff item.
484 * It is at this point that we assert that all of the extent
485 * slots in the quotaoff item have been filled.
488 xfs_qm_qoff_logitem_format(xfs_qoff_logitem_t *qf,
489 xfs_log_iovec_t *log_vector)
491 ASSERT(qf->qql_format.qf_type == XFS_LI_QUOTAOFF);
493 log_vector->i_addr = (xfs_caddr_t)&(qf->qql_format);
494 log_vector->i_len = sizeof(xfs_qoff_logitem_t);
495 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_QUOTAOFF);
496 qf->qql_format.qf_size = 1;
501 * Pinning has no meaning for an quotaoff item, so just return.
505 xfs_qm_qoff_logitem_pin(xfs_qoff_logitem_t *qf)
512 * Since pinning has no meaning for an quotaoff item, unpinning does
517 xfs_qm_qoff_logitem_unpin(xfs_qoff_logitem_t *qf, int stale)
524 xfs_qm_qoff_logitem_unpin_remove(xfs_qoff_logitem_t *qf, xfs_trans_t *tp)
530 * Quotaoff items have no locking, so just return success.
534 xfs_qm_qoff_logitem_trylock(xfs_qoff_logitem_t *qf)
536 return XFS_ITEM_LOCKED;
540 * Quotaoff items have no locking or pushing, so return failure
541 * so that the caller doesn't bother with us.
545 xfs_qm_qoff_logitem_unlock(xfs_qoff_logitem_t *qf)
551 * The quotaoff-start-item is logged only once and cannot be moved in the log,
552 * so simply return the lsn at which it's been logged.
556 xfs_qm_qoff_logitem_committed(xfs_qoff_logitem_t *qf, xfs_lsn_t lsn)
562 * The transaction of which this QUOTAOFF is a part has been aborted.
563 * Just clean up after ourselves.
564 * Shouldn't this never happen in the case of qoffend logitems? XXX
567 xfs_qm_qoff_logitem_abort(xfs_qoff_logitem_t *qf)
569 kmem_free(qf, sizeof(xfs_qoff_logitem_t));
573 * There isn't much you can do to push on an quotaoff item. It is simply
574 * stuck waiting for the log to be flushed to disk.
578 xfs_qm_qoff_logitem_push(xfs_qoff_logitem_t *qf)
586 xfs_qm_qoffend_logitem_committed(
587 xfs_qoff_logitem_t *qfe,
590 xfs_qoff_logitem_t *qfs;
593 qfs = qfe->qql_start_lip;
594 AIL_LOCK(qfs->qql_item.li_mountp,s);
596 * Delete the qoff-start logitem from the AIL.
597 * xfs_trans_delete_ail() drops the AIL lock.
599 xfs_trans_delete_ail(qfs->qql_item.li_mountp, (xfs_log_item_t *)qfs, s);
600 kmem_free(qfs, sizeof(xfs_qoff_logitem_t));
601 kmem_free(qfe, sizeof(xfs_qoff_logitem_t));
602 return (xfs_lsn_t)-1;
606 * XXX rcc - don't know quite what to do with this. I think we can
607 * just ignore it. The only time that isn't the case is if we allow
608 * the client to somehow see that quotas have been turned off in which
609 * we can't allow that to get back until the quotaoff hits the disk.
610 * So how would that happen? Also, do we need different routines for
611 * quotaoff start and quotaoff end? I suspect the answer is yes but
612 * to be sure, I need to look at the recovery code and see how quota off
613 * recovery is handled (do we roll forward or back or do something else).
614 * If we roll forwards or backwards, then we need two separate routines,
615 * one that does nothing and one that stamps in the lsn that matters
616 * (truly makes the quotaoff irrevocable). If we do something else,
617 * then maybe we don't need two.
621 xfs_qm_qoff_logitem_committing(xfs_qoff_logitem_t *qip, xfs_lsn_t commit_lsn)
628 xfs_qm_qoffend_logitem_committing(xfs_qoff_logitem_t *qip, xfs_lsn_t commit_lsn)
633 STATIC struct xfs_item_ops xfs_qm_qoffend_logitem_ops = {
634 .iop_size = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_size,
635 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
636 xfs_qm_qoff_logitem_format,
637 .iop_pin = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_pin,
638 .iop_unpin = (void(*)(xfs_log_item_t* ,int))
639 xfs_qm_qoff_logitem_unpin,
640 .iop_unpin_remove = (void(*)(xfs_log_item_t*,xfs_trans_t*))
641 xfs_qm_qoff_logitem_unpin_remove,
642 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_trylock,
643 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_unlock,
644 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
645 xfs_qm_qoffend_logitem_committed,
646 .iop_push = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_push,
647 .iop_abort = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_abort,
649 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
650 xfs_qm_qoffend_logitem_committing
654 * This is the ops vector shared by all quotaoff-start log items.
656 STATIC struct xfs_item_ops xfs_qm_qoff_logitem_ops = {
657 .iop_size = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_size,
658 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
659 xfs_qm_qoff_logitem_format,
660 .iop_pin = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_pin,
661 .iop_unpin = (void(*)(xfs_log_item_t*, int))
662 xfs_qm_qoff_logitem_unpin,
663 .iop_unpin_remove = (void(*)(xfs_log_item_t*,xfs_trans_t*))
664 xfs_qm_qoff_logitem_unpin_remove,
665 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_trylock,
666 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_unlock,
667 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
668 xfs_qm_qoff_logitem_committed,
669 .iop_push = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_push,
670 .iop_abort = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_abort,
672 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
673 xfs_qm_qoff_logitem_committing
677 * Allocate and initialize an quotaoff item of the correct quota type(s).
680 xfs_qm_qoff_logitem_init(
681 struct xfs_mount *mp,
682 xfs_qoff_logitem_t *start,
685 xfs_qoff_logitem_t *qf;
687 qf = (xfs_qoff_logitem_t*) kmem_zalloc(sizeof(xfs_qoff_logitem_t), KM_SLEEP);
689 qf->qql_item.li_type = XFS_LI_QUOTAOFF;
691 qf->qql_item.li_ops = &xfs_qm_qoffend_logitem_ops;
693 qf->qql_item.li_ops = &xfs_qm_qoff_logitem_ops;
694 qf->qql_item.li_mountp = mp;
695 qf->qql_format.qf_type = XFS_LI_QUOTAOFF;
696 qf->qql_format.qf_flags = flags;
697 qf->qql_start_lip = start;