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 * this needs to stamp an lsn into the dquot, I think.
386 * rpc's that look at user dquot's would then have to
387 * push on the dependency recorded in the dquot
391 xfs_qm_dquot_logitem_committing(
400 * This is the ops vector for dquots
402 STATIC struct xfs_item_ops xfs_dquot_item_ops = {
403 .iop_size = (uint(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_size,
404 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
405 xfs_qm_dquot_logitem_format,
406 .iop_pin = (void(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_pin,
407 .iop_unpin = (void(*)(xfs_log_item_t*, int))
408 xfs_qm_dquot_logitem_unpin,
409 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
410 xfs_qm_dquot_logitem_unpin_remove,
411 .iop_trylock = (uint(*)(xfs_log_item_t*))
412 xfs_qm_dquot_logitem_trylock,
413 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_unlock,
414 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
415 xfs_qm_dquot_logitem_committed,
416 .iop_push = (void(*)(xfs_log_item_t*))xfs_qm_dquot_logitem_push,
417 .iop_pushbuf = (void(*)(xfs_log_item_t*))
418 xfs_qm_dquot_logitem_pushbuf,
419 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
420 xfs_qm_dquot_logitem_committing
424 * Initialize the dquot log item for a newly allocated dquot.
425 * The dquot isn't locked at this point, but it isn't on any of the lists
426 * either, so we don't care.
429 xfs_qm_dquot_logitem_init(
430 struct xfs_dquot *dqp)
432 xfs_dq_logitem_t *lp;
433 lp = &dqp->q_logitem;
435 lp->qli_item.li_type = XFS_LI_DQUOT;
436 lp->qli_item.li_ops = &xfs_dquot_item_ops;
437 lp->qli_item.li_mountp = dqp->q_mount;
439 lp->qli_format.qlf_type = XFS_LI_DQUOT;
440 lp->qli_format.qlf_id = be32_to_cpu(dqp->q_core.d_id);
441 lp->qli_format.qlf_blkno = dqp->q_blkno;
442 lp->qli_format.qlf_len = 1;
444 * This is just the offset of this dquot within its buffer
445 * (which is currently 1 FSB and probably won't change).
446 * Hence 32 bits for this offset should be just fine.
447 * Alternatively, we can store (bufoffset / sizeof(xfs_dqblk_t))
448 * here, and recompute it at recovery time.
450 lp->qli_format.qlf_boffset = (__uint32_t)dqp->q_bufoffset;
453 /*------------------ QUOTAOFF LOG ITEMS -------------------*/
456 * This returns the number of iovecs needed to log the given quotaoff item.
457 * We only need 1 iovec for an quotaoff item. It just logs the
458 * quotaoff_log_format structure.
462 xfs_qm_qoff_logitem_size(xfs_qoff_logitem_t *qf)
468 * This is called to fill in the vector of log iovecs for the
469 * given quotaoff log item. We use only 1 iovec, and we point that
470 * at the quotaoff_log_format structure embedded in the quotaoff item.
471 * It is at this point that we assert that all of the extent
472 * slots in the quotaoff item have been filled.
475 xfs_qm_qoff_logitem_format(xfs_qoff_logitem_t *qf,
476 xfs_log_iovec_t *log_vector)
478 ASSERT(qf->qql_format.qf_type == XFS_LI_QUOTAOFF);
480 log_vector->i_addr = (xfs_caddr_t)&(qf->qql_format);
481 log_vector->i_len = sizeof(xfs_qoff_logitem_t);
482 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_QUOTAOFF);
483 qf->qql_format.qf_size = 1;
488 * Pinning has no meaning for an quotaoff item, so just return.
492 xfs_qm_qoff_logitem_pin(xfs_qoff_logitem_t *qf)
499 * Since pinning has no meaning for an quotaoff item, unpinning does
504 xfs_qm_qoff_logitem_unpin(xfs_qoff_logitem_t *qf, int stale)
511 xfs_qm_qoff_logitem_unpin_remove(xfs_qoff_logitem_t *qf, xfs_trans_t *tp)
517 * Quotaoff items have no locking, so just return success.
521 xfs_qm_qoff_logitem_trylock(xfs_qoff_logitem_t *qf)
523 return XFS_ITEM_LOCKED;
527 * Quotaoff items have no locking or pushing, so return failure
528 * so that the caller doesn't bother with us.
532 xfs_qm_qoff_logitem_unlock(xfs_qoff_logitem_t *qf)
538 * The quotaoff-start-item is logged only once and cannot be moved in the log,
539 * so simply return the lsn at which it's been logged.
543 xfs_qm_qoff_logitem_committed(xfs_qoff_logitem_t *qf, xfs_lsn_t lsn)
549 * There isn't much you can do to push on an quotaoff item. It is simply
550 * stuck waiting for the log to be flushed to disk.
554 xfs_qm_qoff_logitem_push(xfs_qoff_logitem_t *qf)
562 xfs_qm_qoffend_logitem_committed(
563 xfs_qoff_logitem_t *qfe,
566 xfs_qoff_logitem_t *qfs;
569 qfs = qfe->qql_start_lip;
570 AIL_LOCK(qfs->qql_item.li_mountp,s);
572 * Delete the qoff-start logitem from the AIL.
573 * xfs_trans_delete_ail() drops the AIL lock.
575 xfs_trans_delete_ail(qfs->qql_item.li_mountp, (xfs_log_item_t *)qfs, s);
576 kmem_free(qfs, sizeof(xfs_qoff_logitem_t));
577 kmem_free(qfe, sizeof(xfs_qoff_logitem_t));
578 return (xfs_lsn_t)-1;
582 * XXX rcc - don't know quite what to do with this. I think we can
583 * just ignore it. The only time that isn't the case is if we allow
584 * the client to somehow see that quotas have been turned off in which
585 * we can't allow that to get back until the quotaoff hits the disk.
586 * So how would that happen? Also, do we need different routines for
587 * quotaoff start and quotaoff end? I suspect the answer is yes but
588 * to be sure, I need to look at the recovery code and see how quota off
589 * recovery is handled (do we roll forward or back or do something else).
590 * If we roll forwards or backwards, then we need two separate routines,
591 * one that does nothing and one that stamps in the lsn that matters
592 * (truly makes the quotaoff irrevocable). If we do something else,
593 * then maybe we don't need two.
597 xfs_qm_qoff_logitem_committing(xfs_qoff_logitem_t *qip, xfs_lsn_t commit_lsn)
604 xfs_qm_qoffend_logitem_committing(xfs_qoff_logitem_t *qip, xfs_lsn_t commit_lsn)
609 STATIC struct xfs_item_ops xfs_qm_qoffend_logitem_ops = {
610 .iop_size = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_size,
611 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
612 xfs_qm_qoff_logitem_format,
613 .iop_pin = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_pin,
614 .iop_unpin = (void(*)(xfs_log_item_t* ,int))
615 xfs_qm_qoff_logitem_unpin,
616 .iop_unpin_remove = (void(*)(xfs_log_item_t*,xfs_trans_t*))
617 xfs_qm_qoff_logitem_unpin_remove,
618 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_trylock,
619 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_unlock,
620 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
621 xfs_qm_qoffend_logitem_committed,
622 .iop_push = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_push,
624 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
625 xfs_qm_qoffend_logitem_committing
629 * This is the ops vector shared by all quotaoff-start log items.
631 STATIC struct xfs_item_ops xfs_qm_qoff_logitem_ops = {
632 .iop_size = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_size,
633 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
634 xfs_qm_qoff_logitem_format,
635 .iop_pin = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_pin,
636 .iop_unpin = (void(*)(xfs_log_item_t*, int))
637 xfs_qm_qoff_logitem_unpin,
638 .iop_unpin_remove = (void(*)(xfs_log_item_t*,xfs_trans_t*))
639 xfs_qm_qoff_logitem_unpin_remove,
640 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_trylock,
641 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_unlock,
642 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
643 xfs_qm_qoff_logitem_committed,
644 .iop_push = (void(*)(xfs_log_item_t*))xfs_qm_qoff_logitem_push,
646 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
647 xfs_qm_qoff_logitem_committing
651 * Allocate and initialize an quotaoff item of the correct quota type(s).
654 xfs_qm_qoff_logitem_init(
655 struct xfs_mount *mp,
656 xfs_qoff_logitem_t *start,
659 xfs_qoff_logitem_t *qf;
661 qf = (xfs_qoff_logitem_t*) kmem_zalloc(sizeof(xfs_qoff_logitem_t), KM_SLEEP);
663 qf->qql_item.li_type = XFS_LI_QUOTAOFF;
665 qf->qql_item.li_ops = &xfs_qm_qoffend_logitem_ops;
667 qf->qql_item.li_ops = &xfs_qm_qoff_logitem_ops;
668 qf->qql_item.li_mountp = mp;
669 qf->qql_format.qf_type = XFS_LI_QUOTAOFF;
670 qf->qql_format.qf_flags = flags;
671 qf->qql_start_lip = start;