Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2003 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 8 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_log_format.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 10 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 11 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 12 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 14 | #include "xfs_defer.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "xfs_inode.h" |
| 16 | #include "xfs_bmap.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 17 | #include "xfs_quota.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 18 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs_buf_item.h" |
| 20 | #include "xfs_trans_space.h" |
| 21 | #include "xfs_trans_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_qm.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 23 | #include "xfs_trace.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 24 | #include "xfs_log.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 25 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* |
Christoph Hellwig | bf72de3 | 2011-12-06 21:58:19 +0000 | [diff] [blame] | 28 | * Lock order: |
| 29 | * |
| 30 | * ip->i_lock |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 31 | * qi->qi_tree_lock |
Christoph Hellwig | b84a3a9 | 2012-03-14 11:53:34 -0500 | [diff] [blame] | 32 | * dquot->q_qlock (xfs_dqlock() and friends) |
| 33 | * dquot->q_flush (xfs_dqflock() and friends) |
| 34 | * qi->qi_lru_lock |
Christoph Hellwig | bf72de3 | 2011-12-06 21:58:19 +0000 | [diff] [blame] | 35 | * |
| 36 | * If two dquots need to be locked the order is user before group/project, |
| 37 | * otherwise by the lowest id first, see xfs_dqlock2. |
| 38 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Christoph Hellwig | a05931c | 2012-03-13 08:52:37 +0000 | [diff] [blame] | 40 | struct kmem_zone *xfs_qm_dqtrxzone; |
| 41 | static struct kmem_zone *xfs_qm_dqzone; |
| 42 | |
Dave Chinner | f112a04 | 2013-09-30 09:37:03 +1000 | [diff] [blame] | 43 | static struct lock_class_key xfs_dquot_group_class; |
| 44 | static struct lock_class_key xfs_dquot_project_class; |
Christoph Hellwig | 98b8c7a | 2009-01-19 02:03:25 +0100 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * This is called to free all the memory associated with a dquot |
| 48 | */ |
| 49 | void |
| 50 | xfs_qm_dqdestroy( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 51 | struct xfs_dquot *dqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Christoph Hellwig | f8739c3 | 2012-03-13 08:52:34 +0000 | [diff] [blame] | 53 | ASSERT(list_empty(&dqp->q_lru)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Dave Chinner | b1c5ebb | 2016-07-22 09:52:35 +1000 | [diff] [blame] | 55 | kmem_free(dqp->q_logitem.qli_item.li_lv_shadow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | mutex_destroy(&dqp->q_qlock); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 57 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 58 | XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot); |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 59 | kmem_cache_free(xfs_qm_dqzone, dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * If default limits are in force, push them into the dquot now. |
| 64 | * We overwrite the dquot limits only if they are zero and this |
| 65 | * is not the root dquot. |
| 66 | */ |
| 67 | void |
| 68 | xfs_qm_adjust_dqlimits( |
Brian Foster | 4b6eae2e | 2013-03-18 10:51:45 -0400 | [diff] [blame] | 69 | struct xfs_mount *mp, |
| 70 | struct xfs_dquot *dq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Brian Foster | 4b6eae2e | 2013-03-18 10:51:45 -0400 | [diff] [blame] | 72 | struct xfs_quotainfo *q = mp->m_quotainfo; |
| 73 | struct xfs_disk_dquot *d = &dq->q_core; |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 74 | struct xfs_def_quota *defq; |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 75 | int prealloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | ASSERT(d->d_id); |
Eric Sandeen | ce6e7e79c | 2020-05-21 13:07:00 -0700 | [diff] [blame] | 78 | defq = xfs_get_defquota(q, xfs_dquot_type(dq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 80 | if (defq->bsoftlimit && !d->d_blk_softlimit) { |
| 81 | d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit); |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 82 | prealloc = 1; |
| 83 | } |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 84 | if (defq->bhardlimit && !d->d_blk_hardlimit) { |
| 85 | d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit); |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 86 | prealloc = 1; |
| 87 | } |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 88 | if (defq->isoftlimit && !d->d_ino_softlimit) |
| 89 | d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit); |
| 90 | if (defq->ihardlimit && !d->d_ino_hardlimit) |
| 91 | d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit); |
| 92 | if (defq->rtbsoftlimit && !d->d_rtb_softlimit) |
| 93 | d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit); |
| 94 | if (defq->rtbhardlimit && !d->d_rtb_hardlimit) |
| 95 | d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit); |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 96 | |
| 97 | if (prealloc) |
| 98 | xfs_dquot_set_prealloc_limits(dq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Check the limits and timers of a dquot and start or reset timers |
| 103 | * if necessary. |
| 104 | * This gets called even when quota enforcement is OFF, which makes our |
| 105 | * life a little less complicated. (We just don't reject any quota |
| 106 | * reservations in that case, when enforcement is off). |
| 107 | * We also return 0 as the values of the timers in Q_GETQUOTA calls, when |
| 108 | * enforcement's off. |
| 109 | * In contrast, warnings are a little different in that they don't |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 110 | * 'automatically' get started when limits get exceeded. They do |
| 111 | * get reset to zero, however, when we find the count to be under |
| 112 | * the soft limit (they are only ever set non-zero via userspace). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | */ |
| 114 | void |
| 115 | xfs_qm_adjust_dqtimers( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 116 | struct xfs_mount *mp, |
Eric Sandeen | 3dbb9aa | 2020-05-21 13:07:00 -0700 | [diff] [blame] | 117 | struct xfs_dquot *dq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Eric Sandeen | e850301 | 2020-05-21 13:07:01 -0700 | [diff] [blame^] | 119 | struct xfs_quotainfo *qi = mp->m_quotainfo; |
Eric Sandeen | 3dbb9aa | 2020-05-21 13:07:00 -0700 | [diff] [blame] | 120 | struct xfs_disk_dquot *d = &dq->q_core; |
Eric Sandeen | e850301 | 2020-05-21 13:07:01 -0700 | [diff] [blame^] | 121 | struct xfs_def_quota *defq; |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | ASSERT(d->d_id); |
Eric Sandeen | e850301 | 2020-05-21 13:07:01 -0700 | [diff] [blame^] | 124 | defq = xfs_get_defquota(qi, xfs_dquot_type(dq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Christoph Hellwig | ea15ab3 | 2011-07-13 13:43:50 +0200 | [diff] [blame] | 126 | #ifdef DEBUG |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 127 | if (d->d_blk_hardlimit) |
| 128 | ASSERT(be64_to_cpu(d->d_blk_softlimit) <= |
| 129 | be64_to_cpu(d->d_blk_hardlimit)); |
| 130 | if (d->d_ino_hardlimit) |
| 131 | ASSERT(be64_to_cpu(d->d_ino_softlimit) <= |
| 132 | be64_to_cpu(d->d_ino_hardlimit)); |
| 133 | if (d->d_rtb_hardlimit) |
| 134 | ASSERT(be64_to_cpu(d->d_rtb_softlimit) <= |
| 135 | be64_to_cpu(d->d_rtb_hardlimit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | #endif |
Christoph Hellwig | ea15ab3 | 2011-07-13 13:43:50 +0200 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (!d->d_btimer) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 139 | if ((d->d_blk_softlimit && |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 140 | (be64_to_cpu(d->d_bcount) > |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 141 | be64_to_cpu(d->d_blk_softlimit))) || |
| 142 | (d->d_blk_hardlimit && |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 143 | (be64_to_cpu(d->d_bcount) > |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 144 | be64_to_cpu(d->d_blk_hardlimit)))) { |
Arnd Bergmann | b8a0880 | 2020-01-02 13:27:45 -0800 | [diff] [blame] | 145 | d->d_btimer = cpu_to_be32(ktime_get_real_seconds() + |
Eric Sandeen | e850301 | 2020-05-21 13:07:01 -0700 | [diff] [blame^] | 146 | defq->btimelimit); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 147 | } else { |
| 148 | d->d_bwarns = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | } else { |
| 151 | if ((!d->d_blk_softlimit || |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 152 | (be64_to_cpu(d->d_bcount) <= |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 153 | be64_to_cpu(d->d_blk_softlimit))) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | (!d->d_blk_hardlimit || |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 155 | (be64_to_cpu(d->d_bcount) <= |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 156 | be64_to_cpu(d->d_blk_hardlimit)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | d->d_btimer = 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (!d->d_itimer) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 162 | if ((d->d_ino_softlimit && |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 163 | (be64_to_cpu(d->d_icount) > |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 164 | be64_to_cpu(d->d_ino_softlimit))) || |
| 165 | (d->d_ino_hardlimit && |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 166 | (be64_to_cpu(d->d_icount) > |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 167 | be64_to_cpu(d->d_ino_hardlimit)))) { |
Arnd Bergmann | b8a0880 | 2020-01-02 13:27:45 -0800 | [diff] [blame] | 168 | d->d_itimer = cpu_to_be32(ktime_get_real_seconds() + |
Eric Sandeen | e850301 | 2020-05-21 13:07:01 -0700 | [diff] [blame^] | 169 | defq->itimelimit); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 170 | } else { |
| 171 | d->d_iwarns = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | } else { |
| 174 | if ((!d->d_ino_softlimit || |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 175 | (be64_to_cpu(d->d_icount) <= |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 176 | be64_to_cpu(d->d_ino_softlimit))) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | (!d->d_ino_hardlimit || |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 178 | (be64_to_cpu(d->d_icount) <= |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 179 | be64_to_cpu(d->d_ino_hardlimit)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | d->d_itimer = 0; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (!d->d_rtbtimer) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 185 | if ((d->d_rtb_softlimit && |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 186 | (be64_to_cpu(d->d_rtbcount) > |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 187 | be64_to_cpu(d->d_rtb_softlimit))) || |
| 188 | (d->d_rtb_hardlimit && |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 189 | (be64_to_cpu(d->d_rtbcount) > |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 190 | be64_to_cpu(d->d_rtb_hardlimit)))) { |
Arnd Bergmann | b8a0880 | 2020-01-02 13:27:45 -0800 | [diff] [blame] | 191 | d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() + |
Eric Sandeen | e850301 | 2020-05-21 13:07:01 -0700 | [diff] [blame^] | 192 | defq->rtbtimelimit); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 193 | } else { |
| 194 | d->d_rtbwarns = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | } else { |
| 197 | if ((!d->d_rtb_softlimit || |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 198 | (be64_to_cpu(d->d_rtbcount) <= |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 199 | be64_to_cpu(d->d_rtb_softlimit))) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | (!d->d_rtb_hardlimit || |
Mitsuo Hayasaka | d0a3fe6 | 2012-02-06 12:50:07 +0000 | [diff] [blame] | 201 | (be64_to_cpu(d->d_rtbcount) <= |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 202 | be64_to_cpu(d->d_rtb_hardlimit)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | d->d_rtbtimer = 0; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | * initialize a buffer full of dquots and log the whole thing |
| 210 | */ |
| 211 | STATIC void |
| 212 | xfs_qm_init_dquot_blk( |
Darrick J. Wong | 78bba5c | 2020-05-13 15:33:27 -0700 | [diff] [blame] | 213 | struct xfs_trans *tp, |
| 214 | struct xfs_mount *mp, |
| 215 | xfs_dqid_t id, |
| 216 | uint type, |
| 217 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 219 | struct xfs_quotainfo *q = mp->m_quotainfo; |
Darrick J. Wong | 78bba5c | 2020-05-13 15:33:27 -0700 | [diff] [blame] | 220 | struct xfs_dqblk *d; |
| 221 | xfs_dqid_t curid; |
| 222 | unsigned int qflag; |
| 223 | unsigned int blftype; |
| 224 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | ASSERT(tp); |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 227 | ASSERT(xfs_buf_islocked(bp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 229 | d = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * ID of the first dquot in the block - id's are zero based. |
| 233 | */ |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 234 | curid = id - (id % q->qi_dqperchunk); |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 235 | memset(d, 0, BBTOB(q->qi_dqchunklen)); |
Christoph Hellwig | 49d35a5 | 2011-12-06 21:58:23 +0000 | [diff] [blame] | 236 | for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) { |
| 237 | d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC); |
| 238 | d->dd_diskdq.d_version = XFS_DQUOT_VERSION; |
| 239 | d->dd_diskdq.d_id = cpu_to_be32(curid); |
| 240 | d->dd_diskdq.d_flags = type; |
Dave Chinner | 6fcdc59 | 2013-06-03 15:28:46 +1000 | [diff] [blame] | 241 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
Dave Chinner | 9286345 | 2015-08-19 10:32:01 +1000 | [diff] [blame] | 242 | uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid); |
Dave Chinner | 6fcdc59 | 2013-06-03 15:28:46 +1000 | [diff] [blame] | 243 | xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk), |
| 244 | XFS_DQUOT_CRC_OFF); |
| 245 | } |
Christoph Hellwig | 49d35a5 | 2011-12-06 21:58:23 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Darrick J. Wong | 78bba5c | 2020-05-13 15:33:27 -0700 | [diff] [blame] | 248 | if (type & XFS_DQ_USER) { |
| 249 | qflag = XFS_UQUOTA_CHKD; |
| 250 | blftype = XFS_BLF_UDQUOT_BUF; |
| 251 | } else if (type & XFS_DQ_PROJ) { |
| 252 | qflag = XFS_PQUOTA_CHKD; |
| 253 | blftype = XFS_BLF_PDQUOT_BUF; |
| 254 | } else { |
| 255 | qflag = XFS_GQUOTA_CHKD; |
| 256 | blftype = XFS_BLF_GDQUOT_BUF; |
| 257 | } |
| 258 | |
| 259 | xfs_trans_dquot_buf(tp, bp, blftype); |
| 260 | |
| 261 | /* |
| 262 | * quotacheck uses delayed writes to update all the dquots on disk in an |
| 263 | * efficient manner instead of logging the individual dquot changes as |
| 264 | * they are made. However if we log the buffer allocated here and crash |
| 265 | * after quotacheck while the logged initialisation is still in the |
| 266 | * active region of the log, log recovery can replay the dquot buffer |
| 267 | * initialisation over the top of the checked dquots and corrupt quota |
| 268 | * accounting. |
| 269 | * |
| 270 | * To avoid this problem, quotacheck cannot log the initialised buffer. |
| 271 | * We must still dirty the buffer and write it back before the |
| 272 | * allocation transaction clears the log. Therefore, mark the buffer as |
| 273 | * ordered instead of logging it directly. This is safe for quotacheck |
| 274 | * because it detects and repairs allocated but initialized dquot blocks |
| 275 | * in the quota inodes. |
| 276 | */ |
| 277 | if (!(mp->m_qflags & qflag)) |
| 278 | xfs_trans_ordered_buf(tp, bp); |
| 279 | else |
| 280 | xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 283 | /* |
| 284 | * Initialize the dynamic speculative preallocation thresholds. The lo/hi |
| 285 | * watermarks correspond to the soft and hard limits by default. If a soft limit |
| 286 | * is not specified, we use 95% of the hard limit. |
| 287 | */ |
| 288 | void |
| 289 | xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp) |
| 290 | { |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 291 | uint64_t space; |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 292 | |
| 293 | dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit); |
| 294 | dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit); |
| 295 | if (!dqp->q_prealloc_lo_wmark) { |
| 296 | dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark; |
| 297 | do_div(dqp->q_prealloc_lo_wmark, 100); |
| 298 | dqp->q_prealloc_lo_wmark *= 95; |
| 299 | } |
| 300 | |
| 301 | space = dqp->q_prealloc_hi_wmark; |
| 302 | |
| 303 | do_div(space, 100); |
| 304 | dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space; |
| 305 | dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3; |
| 306 | dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5; |
| 307 | } |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | /* |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 310 | * Ensure that the given in-core dquot has a buffer on disk backing it, and |
Darrick J. Wong | 710d707 | 2019-04-24 09:27:41 -0700 | [diff] [blame] | 311 | * return the buffer locked and held. This is called when the bmapi finds a |
| 312 | * hole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | */ |
| 314 | STATIC int |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 315 | xfs_dquot_disk_alloc( |
| 316 | struct xfs_trans **tpp, |
| 317 | struct xfs_dquot *dqp, |
| 318 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 320 | struct xfs_bmbt_irec map; |
Brian Foster | 2ba1372 | 2018-07-11 22:26:11 -0700 | [diff] [blame] | 321 | struct xfs_trans *tp = *tpp; |
| 322 | struct xfs_mount *mp = tp->t_mountp; |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 323 | struct xfs_buf *bp; |
| 324 | struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 325 | int nmaps = 1; |
| 326 | int error; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 327 | |
| 328 | trace_xfs_dqalloc(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | xfs_ilock(quotip, XFS_ILOCK_EXCL); |
Chandra Seetharaman | 6967b96 | 2012-01-23 17:31:25 +0000 | [diff] [blame] | 331 | if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) { |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 332 | /* |
| 333 | * Return if this type of quotas is turned off while we didn't |
| 334 | * have an inode lock |
| 335 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | xfs_iunlock(quotip, XFS_ILOCK_EXCL); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 337 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 340 | /* Create the block mapping. */ |
Brian Foster | 2ba1372 | 2018-07-11 22:26:11 -0700 | [diff] [blame] | 341 | xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL); |
| 342 | error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset, |
Brian Foster | da781e6 | 2019-10-21 09:26:48 -0700 | [diff] [blame] | 343 | XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map, |
| 344 | &nmaps); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 345 | if (error) |
Brian Foster | 73971b1 | 2018-08-07 10:57:13 -0700 | [diff] [blame] | 346 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB); |
| 348 | ASSERT(nmaps == 1); |
| 349 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && |
| 350 | (map.br_startblock != HOLESTARTBLOCK)); |
| 351 | |
| 352 | /* |
| 353 | * Keep track of the blkno to save a lookup later |
| 354 | */ |
| 355 | dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock); |
| 356 | |
| 357 | /* now we can just get the buffer (there's nothing to read yet) */ |
Darrick J. Wong | ce92464 | 2020-01-23 17:01:18 -0800 | [diff] [blame] | 358 | error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno, |
| 359 | mp->m_quotainfo->qi_dqchunklen, 0, &bp); |
| 360 | if (error) |
| 361 | return error; |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 362 | bp->b_ops = &xfs_dquot_buf_ops; |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* |
| 365 | * Make a chunk of dquots out of this buffer and log |
| 366 | * the entire thing. |
| 367 | */ |
Brian Foster | 2ba1372 | 2018-07-11 22:26:11 -0700 | [diff] [blame] | 368 | xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id), |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 369 | dqp->dq_flags & XFS_DQ_ALLTYPES, bp); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 370 | xfs_buf_set_ref(bp, XFS_DQUOT_REF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 372 | /* |
Darrick J. Wong | 7b6b50f | 2018-05-04 15:30:19 -0700 | [diff] [blame] | 373 | * Hold the buffer and join it to the dfops so that we'll still own |
| 374 | * the buffer when we return to the caller. The buffer disposal on |
| 375 | * error must be paid attention to very carefully, as it has been |
| 376 | * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota |
| 377 | * code when allocating a new dquot record" in 2005, and the later |
| 378 | * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep |
| 379 | * the buffer locked across the _defer_finish call. We can now do |
| 380 | * this correctly with xfs_defer_bjoin. |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 381 | * |
Brian Foster | 73971b1 | 2018-08-07 10:57:13 -0700 | [diff] [blame] | 382 | * Above, we allocated a disk block for the dquot information and used |
| 383 | * get_buf to initialize the dquot. If the _defer_finish fails, the old |
Darrick J. Wong | 7b6b50f | 2018-05-04 15:30:19 -0700 | [diff] [blame] | 384 | * transaction is gone but the new buffer is not joined or held to any |
| 385 | * transaction, so we must _buf_relse it. |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 386 | * |
Darrick J. Wong | 7b6b50f | 2018-05-04 15:30:19 -0700 | [diff] [blame] | 387 | * If everything succeeds, the caller of this function is returned a |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 388 | * buffer that is locked and held to the transaction. The caller |
Darrick J. Wong | 7b6b50f | 2018-05-04 15:30:19 -0700 | [diff] [blame] | 389 | * is responsible for unlocking any buffer passed back, either |
Darrick J. Wong | 710d707 | 2019-04-24 09:27:41 -0700 | [diff] [blame] | 390 | * manually or by committing the transaction. On error, the buffer is |
| 391 | * released and not passed back. |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 392 | */ |
Brian Foster | 2ba1372 | 2018-07-11 22:26:11 -0700 | [diff] [blame] | 393 | xfs_trans_bhold(tp, bp); |
Brian Foster | 9e28a24 | 2018-07-24 13:43:15 -0700 | [diff] [blame] | 394 | error = xfs_defer_finish(tpp); |
Darrick J. Wong | 7b6b50f | 2018-05-04 15:30:19 -0700 | [diff] [blame] | 395 | if (error) { |
Darrick J. Wong | 710d707 | 2019-04-24 09:27:41 -0700 | [diff] [blame] | 396 | xfs_trans_bhold_release(*tpp, bp); |
| 397 | xfs_trans_brelse(*tpp, bp); |
Brian Foster | 73971b1 | 2018-08-07 10:57:13 -0700 | [diff] [blame] | 398 | return error; |
Darrick J. Wong | 7b6b50f | 2018-05-04 15:30:19 -0700 | [diff] [blame] | 399 | } |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 400 | *bpp = bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | /* |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 405 | * Read in the in-core dquot's on-disk metadata and return the buffer. |
| 406 | * Returns ENOENT to signal a hole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | */ |
| 408 | STATIC int |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 409 | xfs_dquot_disk_read( |
| 410 | struct xfs_mount *mp, |
| 411 | struct xfs_dquot *dqp, |
| 412 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 414 | struct xfs_bmbt_irec map; |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 415 | struct xfs_buf *bp; |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 416 | struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags); |
Christoph Hellwig | 0891f99 | 2017-07-13 12:14:34 -0700 | [diff] [blame] | 417 | uint lock_mode; |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 418 | int nmaps = 1; |
| 419 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Christoph Hellwig | 0891f99 | 2017-07-13 12:14:34 -0700 | [diff] [blame] | 421 | lock_mode = xfs_ilock_data_map_shared(quotip); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 422 | if (!xfs_this_quota_on(mp, dqp->dq_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | /* |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 424 | * Return if this type of quotas is turned off while we |
| 425 | * didn't have the quota inode lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | */ |
Christoph Hellwig | 0891f99 | 2017-07-13 12:14:34 -0700 | [diff] [blame] | 427 | xfs_iunlock(quotip, lock_mode); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 428 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | /* |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 432 | * Find the block map; no allocations yet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | */ |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 434 | error = xfs_bmapi_read(quotip, dqp->q_fileoffset, |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 435 | XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0); |
Christoph Hellwig | 0891f99 | 2017-07-13 12:14:34 -0700 | [diff] [blame] | 436 | xfs_iunlock(quotip, lock_mode); |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 437 | if (error) |
| 438 | return error; |
| 439 | |
| 440 | ASSERT(nmaps == 1); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 441 | ASSERT(map.br_blockcount >= 1); |
| 442 | ASSERT(map.br_startblock != DELAYSTARTBLOCK); |
| 443 | if (map.br_startblock == HOLESTARTBLOCK) |
| 444 | return -ENOENT; |
| 445 | |
| 446 | trace_xfs_dqtobp_read(dqp); |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 447 | |
| 448 | /* |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 449 | * store the blkno etc so that we don't have to do the |
| 450 | * mapping all the time |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 451 | */ |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 452 | dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock); |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 453 | |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 454 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno, |
| 455 | mp->m_quotainfo->qi_dqchunklen, 0, &bp, |
| 456 | &xfs_dquot_buf_ops); |
| 457 | if (error) { |
| 458 | ASSERT(bp == NULL); |
| 459 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Dave Chinner | c631919 | 2012-11-14 17:50:13 +1100 | [diff] [blame] | 462 | ASSERT(xfs_buf_islocked(bp)); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 463 | xfs_buf_set_ref(bp, XFS_DQUOT_REF); |
| 464 | *bpp = bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 466 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 469 | /* Allocate and initialize everything we need for an incore dquot. */ |
| 470 | STATIC struct xfs_dquot * |
| 471 | xfs_dquot_alloc( |
Christoph Hellwig | 97e7ade | 2011-12-06 21:58:24 +0000 | [diff] [blame] | 472 | struct xfs_mount *mp, |
| 473 | xfs_dqid_t id, |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 474 | uint type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Christoph Hellwig | 97e7ade | 2011-12-06 21:58:24 +0000 | [diff] [blame] | 476 | struct xfs_dquot *dqp; |
Christoph Hellwig | 92b2e5b | 2012-02-01 13:57:20 +0000 | [diff] [blame] | 477 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 478 | dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0); |
Christoph Hellwig | 92b2e5b | 2012-02-01 13:57:20 +0000 | [diff] [blame] | 479 | |
| 480 | dqp->dq_flags = type; |
| 481 | dqp->q_core.d_id = cpu_to_be32(id); |
| 482 | dqp->q_mount = mp; |
Christoph Hellwig | f8739c3 | 2012-03-13 08:52:34 +0000 | [diff] [blame] | 483 | INIT_LIST_HEAD(&dqp->q_lru); |
Christoph Hellwig | 92b2e5b | 2012-02-01 13:57:20 +0000 | [diff] [blame] | 484 | mutex_init(&dqp->q_qlock); |
| 485 | init_waitqueue_head(&dqp->q_pinwait); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 486 | dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk; |
| 487 | /* |
| 488 | * Offset of dquot in the (fixed sized) dquot chunk. |
| 489 | */ |
| 490 | dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) * |
| 491 | sizeof(xfs_dqblk_t); |
Christoph Hellwig | 92b2e5b | 2012-02-01 13:57:20 +0000 | [diff] [blame] | 492 | |
| 493 | /* |
| 494 | * Because we want to use a counting completion, complete |
| 495 | * the flush completion once to allow a single access to |
| 496 | * the flush completion without blocking. |
| 497 | */ |
| 498 | init_completion(&dqp->q_flush); |
| 499 | complete(&dqp->q_flush); |
| 500 | |
| 501 | /* |
| 502 | * Make sure group quotas have a different lock class than user |
| 503 | * quotas. |
| 504 | */ |
Dave Chinner | f112a04 | 2013-09-30 09:37:03 +1000 | [diff] [blame] | 505 | switch (type) { |
| 506 | case XFS_DQ_USER: |
| 507 | /* uses the default lock class */ |
| 508 | break; |
| 509 | case XFS_DQ_GROUP: |
| 510 | lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class); |
| 511 | break; |
| 512 | case XFS_DQ_PROJ: |
| 513 | lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class); |
| 514 | break; |
| 515 | default: |
| 516 | ASSERT(0); |
| 517 | break; |
| 518 | } |
Christoph Hellwig | 92b2e5b | 2012-02-01 13:57:20 +0000 | [diff] [blame] | 519 | |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 520 | xfs_qm_dquot_logitem_init(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 522 | XFS_STATS_INC(mp, xs_qm_dquot); |
| 523 | return dqp; |
| 524 | } |
| 525 | |
| 526 | /* Copy the in-core quota fields in from the on-disk buffer. */ |
| 527 | STATIC void |
| 528 | xfs_dquot_from_disk( |
| 529 | struct xfs_dquot *dqp, |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 530 | struct xfs_buf *bp) |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 531 | { |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 532 | struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset; |
| 533 | |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 534 | /* copy everything from disk dquot to the incore dquot */ |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 535 | memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot)); |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Reservation counters are defined as reservation plus current usage |
| 539 | * to avoid having to add every time. |
| 540 | */ |
| 541 | dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount); |
| 542 | dqp->q_res_icount = be64_to_cpu(ddqp->d_icount); |
| 543 | dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount); |
| 544 | |
| 545 | /* initialize the dquot speculative prealloc thresholds */ |
| 546 | xfs_dquot_set_prealloc_limits(dqp); |
| 547 | } |
| 548 | |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 549 | /* Allocate and initialize the dquot buffer for this in-core dquot. */ |
| 550 | static int |
| 551 | xfs_qm_dqread_alloc( |
| 552 | struct xfs_mount *mp, |
| 553 | struct xfs_dquot *dqp, |
| 554 | struct xfs_buf **bpp) |
| 555 | { |
| 556 | struct xfs_trans *tp; |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 557 | int error; |
| 558 | |
| 559 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc, |
| 560 | XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp); |
| 561 | if (error) |
| 562 | goto err; |
| 563 | |
Darrick J. Wong | 710d707 | 2019-04-24 09:27:41 -0700 | [diff] [blame] | 564 | error = xfs_dquot_disk_alloc(&tp, dqp, bpp); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 565 | if (error) |
| 566 | goto err_cancel; |
| 567 | |
| 568 | error = xfs_trans_commit(tp); |
| 569 | if (error) { |
| 570 | /* |
| 571 | * Buffer was held to the transaction, so we have to unlock it |
| 572 | * manually here because we're not passing it back. |
| 573 | */ |
Darrick J. Wong | 710d707 | 2019-04-24 09:27:41 -0700 | [diff] [blame] | 574 | xfs_buf_relse(*bpp); |
| 575 | *bpp = NULL; |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 576 | goto err; |
| 577 | } |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 578 | return 0; |
| 579 | |
| 580 | err_cancel: |
| 581 | xfs_trans_cancel(tp); |
| 582 | err: |
| 583 | return error; |
| 584 | } |
| 585 | |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 586 | /* |
| 587 | * Read in the ondisk dquot using dqtobp() then copy it to an incore version, |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 588 | * and release the buffer immediately. If @can_alloc is true, fill any |
| 589 | * holes in the on-disk metadata. |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 590 | */ |
Darrick J. Wong | 114e73c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 591 | static int |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 592 | xfs_qm_dqread( |
| 593 | struct xfs_mount *mp, |
| 594 | xfs_dqid_t id, |
| 595 | uint type, |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 596 | bool can_alloc, |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 597 | struct xfs_dquot **dqpp) |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 598 | { |
| 599 | struct xfs_dquot *dqp; |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 600 | struct xfs_buf *bp; |
Darrick J. Wong | 617cd5c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 601 | int error; |
| 602 | |
| 603 | dqp = xfs_dquot_alloc(mp, id, type); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 604 | trace_xfs_dqread(dqp); |
| 605 | |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 606 | /* Try to read the buffer, allocating if necessary. */ |
| 607 | error = xfs_dquot_disk_read(mp, dqp, &bp); |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 608 | if (error == -ENOENT && can_alloc) |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 609 | error = xfs_qm_dqread_alloc(mp, dqp, &bp); |
| 610 | if (error) |
| 611 | goto err; |
Christoph Hellwig | 97e7ade | 2011-12-06 21:58:24 +0000 | [diff] [blame] | 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 614 | * At this point we should have a clean locked buffer. Copy the data |
| 615 | * to the incore dquot and release the buffer since the incore dquot |
| 616 | * has its own locking protocol so we needn't tie up the buffer any |
| 617 | * further. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | */ |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 619 | ASSERT(xfs_buf_islocked(bp)); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 620 | xfs_dquot_from_disk(dqp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 622 | xfs_buf_relse(bp); |
| 623 | *dqpp = dqp; |
Christoph Hellwig | 97e7ade | 2011-12-06 21:58:24 +0000 | [diff] [blame] | 624 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 626 | err: |
| 627 | trace_xfs_dqread_fail(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | xfs_qm_dqdestroy(dqp); |
Darrick J. Wong | d63192c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 629 | *dqpp = NULL; |
Christoph Hellwig | 97e7ade | 2011-12-06 21:58:24 +0000 | [diff] [blame] | 630 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 634 | * Advance to the next id in the current chunk, or if at the |
| 635 | * end of the chunk, skip ahead to first id in next allocated chunk |
| 636 | * using the SEEK_DATA interface. |
| 637 | */ |
Eryu Guan | 6e3e6d5 | 2016-04-06 09:47:21 +1000 | [diff] [blame] | 638 | static int |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 639 | xfs_dq_get_next_id( |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 640 | struct xfs_mount *mp, |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 641 | uint type, |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 642 | xfs_dqid_t *id) |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 643 | { |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 644 | struct xfs_inode *quotip = xfs_quota_inode(mp, type); |
| 645 | xfs_dqid_t next_id = *id + 1; /* simple advance */ |
| 646 | uint lock_flags; |
| 647 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 648 | struct xfs_iext_cursor cur; |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 649 | xfs_fsblock_t start; |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 650 | int error = 0; |
| 651 | |
Eric Sandeen | 657bdfb | 2017-01-17 11:43:38 -0800 | [diff] [blame] | 652 | /* If we'd wrap past the max ID, stop */ |
| 653 | if (next_id < *id) |
| 654 | return -ENOENT; |
| 655 | |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 656 | /* If new ID is within the current chunk, advancing it sufficed */ |
| 657 | if (next_id % mp->m_quotainfo->qi_dqperchunk) { |
| 658 | *id = next_id; |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | /* Nope, next_id is now past the current chunk, so find the next one */ |
| 663 | start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk; |
| 664 | |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 665 | lock_flags = xfs_ilock_data_map_shared(quotip); |
| 666 | if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) { |
| 667 | error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK); |
| 668 | if (error) |
| 669 | return error; |
| 670 | } |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 671 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 672 | if (xfs_iext_lookup_extent(quotip, "ip->i_df, start, &cur, &got)) { |
Brian Foster | 2192b0b | 2017-07-05 12:07:52 -0700 | [diff] [blame] | 673 | /* contiguous chunk, bump startoff for the id calculation */ |
| 674 | if (got.br_startoff < start) |
| 675 | got.br_startoff = start; |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 676 | *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk; |
Brian Foster | 2192b0b | 2017-07-05 12:07:52 -0700 | [diff] [blame] | 677 | } else { |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 678 | error = -ENOENT; |
Brian Foster | 2192b0b | 2017-07-05 12:07:52 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 681 | xfs_iunlock(quotip, lock_flags); |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 682 | |
Christoph Hellwig | bda250d | 2017-06-29 12:28:36 -0700 | [diff] [blame] | 683 | return error; |
Eric Sandeen | 296c24e | 2016-02-08 11:27:38 +1100 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* |
Darrick J. Wong | cc2047c | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 687 | * Look up the dquot in the in-core cache. If found, the dquot is returned |
| 688 | * locked and ready to go. |
| 689 | */ |
| 690 | static struct xfs_dquot * |
| 691 | xfs_qm_dqget_cache_lookup( |
| 692 | struct xfs_mount *mp, |
| 693 | struct xfs_quotainfo *qi, |
| 694 | struct radix_tree_root *tree, |
| 695 | xfs_dqid_t id) |
| 696 | { |
| 697 | struct xfs_dquot *dqp; |
| 698 | |
| 699 | restart: |
| 700 | mutex_lock(&qi->qi_tree_lock); |
| 701 | dqp = radix_tree_lookup(tree, id); |
| 702 | if (!dqp) { |
| 703 | mutex_unlock(&qi->qi_tree_lock); |
| 704 | XFS_STATS_INC(mp, xs_qm_dqcachemisses); |
| 705 | return NULL; |
| 706 | } |
| 707 | |
| 708 | xfs_dqlock(dqp); |
| 709 | if (dqp->dq_flags & XFS_DQ_FREEING) { |
| 710 | xfs_dqunlock(dqp); |
| 711 | mutex_unlock(&qi->qi_tree_lock); |
| 712 | trace_xfs_dqget_freeing(dqp); |
| 713 | delay(1); |
| 714 | goto restart; |
| 715 | } |
| 716 | |
| 717 | dqp->q_nrefs++; |
| 718 | mutex_unlock(&qi->qi_tree_lock); |
| 719 | |
| 720 | trace_xfs_dqget_hit(dqp); |
| 721 | XFS_STATS_INC(mp, xs_qm_dqcachehits); |
| 722 | return dqp; |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * Try to insert a new dquot into the in-core cache. If an error occurs the |
| 727 | * caller should throw away the dquot and start over. Otherwise, the dquot |
| 728 | * is returned locked (and held by the cache) as if there had been a cache |
| 729 | * hit. |
| 730 | */ |
| 731 | static int |
| 732 | xfs_qm_dqget_cache_insert( |
| 733 | struct xfs_mount *mp, |
| 734 | struct xfs_quotainfo *qi, |
| 735 | struct radix_tree_root *tree, |
| 736 | xfs_dqid_t id, |
| 737 | struct xfs_dquot *dqp) |
| 738 | { |
| 739 | int error; |
| 740 | |
| 741 | mutex_lock(&qi->qi_tree_lock); |
| 742 | error = radix_tree_insert(tree, id, dqp); |
| 743 | if (unlikely(error)) { |
| 744 | /* Duplicate found! Caller must try again. */ |
| 745 | WARN_ON(error != -EEXIST); |
| 746 | mutex_unlock(&qi->qi_tree_lock); |
| 747 | trace_xfs_dqget_dup(dqp); |
| 748 | return error; |
| 749 | } |
| 750 | |
| 751 | /* Return a locked dquot to the caller, with a reference taken. */ |
| 752 | xfs_dqlock(dqp); |
| 753 | dqp->q_nrefs = 1; |
| 754 | |
| 755 | qi->qi_dquots++; |
| 756 | mutex_unlock(&qi->qi_tree_lock); |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
Darrick J. Wong | d7103ee | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 761 | /* Check our input parameters. */ |
| 762 | static int |
| 763 | xfs_qm_dqget_checks( |
| 764 | struct xfs_mount *mp, |
| 765 | uint type) |
| 766 | { |
| 767 | if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp))) |
| 768 | return -ESRCH; |
| 769 | |
| 770 | switch (type) { |
| 771 | case XFS_DQ_USER: |
| 772 | if (!XFS_IS_UQUOTA_ON(mp)) |
| 773 | return -ESRCH; |
| 774 | return 0; |
| 775 | case XFS_DQ_GROUP: |
| 776 | if (!XFS_IS_GQUOTA_ON(mp)) |
| 777 | return -ESRCH; |
| 778 | return 0; |
| 779 | case XFS_DQ_PROJ: |
| 780 | if (!XFS_IS_PQUOTA_ON(mp)) |
| 781 | return -ESRCH; |
| 782 | return 0; |
| 783 | default: |
| 784 | WARN_ON_ONCE(0); |
| 785 | return -EINVAL; |
| 786 | } |
| 787 | } |
| 788 | |
Darrick J. Wong | cc2047c | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 789 | /* |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 790 | * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked |
| 791 | * dquot, doing an allocation (if requested) as needed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | */ |
| 793 | int |
| 794 | xfs_qm_dqget( |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 795 | struct xfs_mount *mp, |
| 796 | xfs_dqid_t id, |
| 797 | uint type, |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 798 | bool can_alloc, |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 799 | struct xfs_dquot **O_dqpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 801 | struct xfs_quotainfo *qi = mp->m_quotainfo; |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 802 | struct radix_tree_root *tree = xfs_dquot_tree(qi, type); |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 803 | struct xfs_dquot *dqp; |
| 804 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
Darrick J. Wong | d7103ee | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 806 | error = xfs_qm_dqget_checks(mp, type); |
| 807 | if (error) |
| 808 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 810 | restart: |
| 811 | dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id); |
| 812 | if (dqp) { |
| 813 | *O_dqpp = dqp; |
| 814 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
Christoph Hellwig | 9267855 | 2011-12-06 21:58:18 +0000 | [diff] [blame] | 816 | |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 817 | error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp); |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 818 | if (error) |
| 819 | return error; |
| 820 | |
| 821 | error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp); |
| 822 | if (error) { |
| 823 | /* |
| 824 | * Duplicate found. Just throw away the new dquot and start |
| 825 | * over. |
| 826 | */ |
| 827 | xfs_qm_dqdestroy(dqp); |
| 828 | XFS_STATS_INC(mp, xs_qm_dquot_dups); |
| 829 | goto restart; |
| 830 | } |
| 831 | |
| 832 | trace_xfs_dqget_miss(dqp); |
| 833 | *O_dqpp = dqp; |
| 834 | return 0; |
| 835 | } |
| 836 | |
Darrick J. Wong | 114e73c | 2018-05-04 15:30:23 -0700 | [diff] [blame] | 837 | /* |
| 838 | * Given a dquot id and type, read and initialize a dquot from the on-disk |
| 839 | * metadata. This function is only for use during quota initialization so |
| 840 | * it ignores the dquot cache assuming that the dquot shrinker isn't set up. |
| 841 | * The caller is responsible for _qm_dqdestroy'ing the returned dquot. |
| 842 | */ |
| 843 | int |
| 844 | xfs_qm_dqget_uncached( |
| 845 | struct xfs_mount *mp, |
| 846 | xfs_dqid_t id, |
| 847 | uint type, |
| 848 | struct xfs_dquot **dqpp) |
| 849 | { |
| 850 | int error; |
| 851 | |
| 852 | error = xfs_qm_dqget_checks(mp, type); |
| 853 | if (error) |
| 854 | return error; |
| 855 | |
| 856 | return xfs_qm_dqread(mp, id, type, 0, dqpp); |
| 857 | } |
| 858 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 859 | /* Return the quota id for a given inode and type. */ |
| 860 | xfs_dqid_t |
| 861 | xfs_qm_id_for_quotatype( |
| 862 | struct xfs_inode *ip, |
| 863 | uint type) |
| 864 | { |
| 865 | switch (type) { |
| 866 | case XFS_DQ_USER: |
Christoph Hellwig | ba8adad | 2020-02-21 08:31:27 -0800 | [diff] [blame] | 867 | return i_uid_read(VFS_I(ip)); |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 868 | case XFS_DQ_GROUP: |
Christoph Hellwig | ba8adad | 2020-02-21 08:31:27 -0800 | [diff] [blame] | 869 | return i_gid_read(VFS_I(ip)); |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 870 | case XFS_DQ_PROJ: |
Christoph Hellwig | de7a866 | 2019-11-12 08:22:54 -0800 | [diff] [blame] | 871 | return ip->i_d.di_projid; |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 872 | } |
| 873 | ASSERT(0); |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * Return the dquot for a given inode and type. If @can_alloc is true, then |
| 879 | * allocate blocks if needed. The inode's ILOCK must be held and it must not |
| 880 | * have already had an inode attached. |
| 881 | */ |
| 882 | int |
| 883 | xfs_qm_dqget_inode( |
| 884 | struct xfs_inode *ip, |
| 885 | uint type, |
| 886 | bool can_alloc, |
| 887 | struct xfs_dquot **O_dqpp) |
| 888 | { |
| 889 | struct xfs_mount *mp = ip->i_mount; |
| 890 | struct xfs_quotainfo *qi = mp->m_quotainfo; |
| 891 | struct radix_tree_root *tree = xfs_dquot_tree(qi, type); |
| 892 | struct xfs_dquot *dqp; |
| 893 | xfs_dqid_t id; |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 894 | int error; |
| 895 | |
| 896 | error = xfs_qm_dqget_checks(mp, type); |
| 897 | if (error) |
| 898 | return error; |
| 899 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 900 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 901 | ASSERT(xfs_inode_dquot(ip, type) == NULL); |
| 902 | |
| 903 | id = xfs_qm_id_for_quotatype(ip, type); |
| 904 | |
Christoph Hellwig | 9267855 | 2011-12-06 21:58:18 +0000 | [diff] [blame] | 905 | restart: |
Darrick J. Wong | cc2047c | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 906 | dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id); |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 907 | if (dqp) { |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 908 | *O_dqpp = dqp; |
| 909 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
| 912 | /* |
| 913 | * Dquot cache miss. We don't want to keep the inode lock across |
| 914 | * a (potential) disk read. Also we don't want to deal with the lock |
| 915 | * ordering between quotainode and this inode. OTOH, dropping the inode |
| 916 | * lock here means dealing with a chown that can happen before |
| 917 | * we re-acquire the lock. |
| 918 | */ |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 919 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 920 | error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp); |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 921 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7ae4440 | 2011-12-06 21:58:25 +0000 | [diff] [blame] | 922 | if (error) |
| 923 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 925 | /* |
| 926 | * A dquot could be attached to this inode by now, since we had |
| 927 | * dropped the ilock. |
| 928 | */ |
| 929 | if (xfs_this_quota_on(mp, type)) { |
| 930 | struct xfs_dquot *dqp1; |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 931 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 932 | dqp1 = xfs_inode_dquot(ip, type); |
| 933 | if (dqp1) { |
Chandra Seetharaman | 3673141 | 2012-01-23 17:31:30 +0000 | [diff] [blame] | 934 | xfs_qm_dqdestroy(dqp); |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 935 | dqp = dqp1; |
| 936 | xfs_dqlock(dqp); |
| 937 | goto dqret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 939 | } else { |
| 940 | /* inode stays locked on return */ |
| 941 | xfs_qm_dqdestroy(dqp); |
| 942 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Darrick J. Wong | cc2047c | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 945 | error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp); |
| 946 | if (error) { |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 947 | /* |
| 948 | * Duplicate found. Just throw away the new dquot and start |
| 949 | * over. |
| 950 | */ |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 951 | xfs_qm_dqdestroy(dqp); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 952 | XFS_STATS_INC(mp, xs_qm_dquot_dups); |
Christoph Hellwig | 9f920f1 | 2012-03-13 08:52:35 +0000 | [diff] [blame] | 953 | goto restart; |
| 954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 956 | dqret: |
| 957 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 958 | trace_xfs_dqget_miss(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | *O_dqpp = dqp; |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 960 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Christoph Hellwig | f8739c3 | 2012-03-13 08:52:34 +0000 | [diff] [blame] | 963 | /* |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 964 | * Starting at @id and progressing upwards, look for an initialized incore |
| 965 | * dquot, lock it, and return it. |
| 966 | */ |
| 967 | int |
| 968 | xfs_qm_dqget_next( |
| 969 | struct xfs_mount *mp, |
| 970 | xfs_dqid_t id, |
| 971 | uint type, |
| 972 | struct xfs_dquot **dqpp) |
| 973 | { |
| 974 | struct xfs_dquot *dqp; |
| 975 | int error = 0; |
| 976 | |
| 977 | *dqpp = NULL; |
| 978 | for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) { |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 979 | error = xfs_qm_dqget(mp, id, type, false, &dqp); |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 980 | if (error == -ENOENT) |
| 981 | continue; |
| 982 | else if (error != 0) |
| 983 | break; |
| 984 | |
| 985 | if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) { |
| 986 | *dqpp = dqp; |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | xfs_qm_dqput(dqp); |
| 991 | } |
| 992 | |
| 993 | return error; |
| 994 | } |
| 995 | |
| 996 | /* |
Christoph Hellwig | f8739c3 | 2012-03-13 08:52:34 +0000 | [diff] [blame] | 997 | * Release a reference to the dquot (decrement ref-count) and unlock it. |
| 998 | * |
| 999 | * If there is a group quota attached to this dquot, carefully release that |
| 1000 | * too without tripping over deadlocks'n'stuff. |
| 1001 | */ |
| 1002 | void |
| 1003 | xfs_qm_dqput( |
| 1004 | struct xfs_dquot *dqp) |
| 1005 | { |
| 1006 | ASSERT(dqp->q_nrefs > 0); |
| 1007 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
| 1008 | |
| 1009 | trace_xfs_dqput(dqp); |
| 1010 | |
Dave Chinner | 3c3533757 | 2014-05-05 17:30:15 +1000 | [diff] [blame] | 1011 | if (--dqp->q_nrefs == 0) { |
| 1012 | struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo; |
| 1013 | trace_xfs_dqput_free(dqp); |
| 1014 | |
| 1015 | if (list_lru_add(&qi->qi_lru, &dqp->q_lru)) |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1016 | XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused); |
Dave Chinner | 3c3533757 | 2014-05-05 17:30:15 +1000 | [diff] [blame] | 1017 | } |
| 1018 | xfs_dqunlock(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * Release a dquot. Flush it if dirty, then dqput() it. |
| 1023 | * dquot must not be locked. |
| 1024 | */ |
| 1025 | void |
| 1026 | xfs_qm_dqrele( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 1027 | struct xfs_dquot *dqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1029 | if (!dqp) |
| 1030 | return; |
| 1031 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1032 | trace_xfs_dqrele(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | xfs_dqlock(dqp); |
| 1035 | /* |
| 1036 | * We don't care to flush it if the dquot is dirty here. |
| 1037 | * That will create stutters that we want to avoid. |
| 1038 | * Instead we do a delayed write when we try to reclaim |
| 1039 | * a dirty dquot. Also xfs_sync will take part of the burden... |
| 1040 | */ |
| 1041 | xfs_qm_dqput(dqp); |
| 1042 | } |
| 1043 | |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1044 | /* |
| 1045 | * This is the dquot flushing I/O completion routine. It is called |
| 1046 | * from interrupt level when the buffer containing the dquot is |
| 1047 | * flushed to disk. It is responsible for removing the dquot logitem |
| 1048 | * from the AIL if it has not been re-logged, and unlocking the dquot's |
| 1049 | * flush lock. This behavior is very similar to that of inodes.. |
| 1050 | */ |
| 1051 | STATIC void |
| 1052 | xfs_qm_dqflush_done( |
| 1053 | struct xfs_buf *bp, |
| 1054 | struct xfs_log_item *lip) |
| 1055 | { |
Pavel Reichl | fd8b81d | 2019-11-12 17:04:26 -0800 | [diff] [blame] | 1056 | struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip; |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 1057 | struct xfs_dquot *dqp = qip->qli_dquot; |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1058 | struct xfs_ail *ailp = lip->li_ailp; |
Brian Foster | 849274c | 2020-05-06 13:25:23 -0700 | [diff] [blame] | 1059 | xfs_lsn_t tail_lsn; |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1060 | |
| 1061 | /* |
| 1062 | * We only want to pull the item from the AIL if its |
| 1063 | * location in the log has not changed since we started the flush. |
| 1064 | * Thus, we only bother if the dquot's lsn has |
| 1065 | * not changed. First we check the lsn outside the lock |
| 1066 | * since it's cheaper, and then we recheck while |
| 1067 | * holding the lock before removing the dquot from the AIL. |
| 1068 | */ |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 1069 | if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) && |
Carlos Maiolino | 373b058 | 2017-11-28 08:54:10 -0800 | [diff] [blame] | 1070 | ((lip->li_lsn == qip->qli_flush_lsn) || |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 1071 | test_bit(XFS_LI_FAILED, &lip->li_flags))) { |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1072 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 1073 | spin_lock(&ailp->ail_lock); |
Carlos Maiolino | 373b058 | 2017-11-28 08:54:10 -0800 | [diff] [blame] | 1074 | if (lip->li_lsn == qip->qli_flush_lsn) { |
Brian Foster | 849274c | 2020-05-06 13:25:23 -0700 | [diff] [blame] | 1075 | /* xfs_ail_update_finish() drops the AIL lock */ |
| 1076 | tail_lsn = xfs_ail_delete_one(ailp, lip); |
| 1077 | xfs_ail_update_finish(ailp, tail_lsn); |
Carlos Maiolino | 373b058 | 2017-11-28 08:54:10 -0800 | [diff] [blame] | 1078 | } else { |
| 1079 | /* |
| 1080 | * Clear the failed state since we are about to drop the |
| 1081 | * flush lock |
| 1082 | */ |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 1083 | xfs_clear_li_failed(lip); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 1084 | spin_unlock(&ailp->ail_lock); |
Carlos Maiolino | 373b058 | 2017-11-28 08:54:10 -0800 | [diff] [blame] | 1085 | } |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Release the dq's flush lock since we're done with it. |
| 1090 | */ |
| 1091 | xfs_dqfunlock(dqp); |
| 1092 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
| 1094 | /* |
| 1095 | * Write a modified dquot to disk. |
| 1096 | * The dquot must be locked and the flush lock too taken by caller. |
| 1097 | * The flush lock will not be unlocked until the dquot reaches the disk, |
| 1098 | * but the dquot is free to be unlocked and modified by the caller |
| 1099 | * in the interim. Dquot is still locked on return. This behavior is |
| 1100 | * identical to that of inodes. |
| 1101 | */ |
| 1102 | int |
| 1103 | xfs_qm_dqflush( |
Christoph Hellwig | fe7257f | 2012-04-23 15:58:37 +1000 | [diff] [blame] | 1104 | struct xfs_dquot *dqp, |
| 1105 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1107 | struct xfs_mount *mp = dqp->q_mount; |
Brian Foster | b707fff | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1108 | struct xfs_log_item *lip = &dqp->q_logitem.qli_item; |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1109 | struct xfs_buf *bp; |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 1110 | struct xfs_dqblk *dqb; |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1111 | struct xfs_disk_dquot *ddqp; |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 1112 | xfs_failaddr_t fa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 1116 | ASSERT(!completion_done(&dqp->q_flush)); |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1117 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1118 | trace_xfs_dqflush(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
Christoph Hellwig | fe7257f | 2012-04-23 15:58:37 +1000 | [diff] [blame] | 1120 | *bpp = NULL; |
| 1121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | xfs_qm_dqunpin_wait(dqp); |
| 1123 | |
| 1124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | * Get the buffer containing the on-disk dquot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | */ |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1127 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno, |
Brian Foster | 8d3d7e2 | 2020-03-27 08:29:45 -0700 | [diff] [blame] | 1128 | mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK, |
| 1129 | &bp, &xfs_dquot_buf_ops); |
Brian Foster | b707fff | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1130 | if (error == -EAGAIN) |
Christoph Hellwig | fe7257f | 2012-04-23 15:58:37 +1000 | [diff] [blame] | 1131 | goto out_unlock; |
Brian Foster | b707fff | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1132 | if (error) |
| 1133 | goto out_abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1135 | /* |
| 1136 | * Calculate the location of the dquot inside the buffer. |
| 1137 | */ |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 1138 | dqb = bp->b_addr + dqp->q_bufoffset; |
| 1139 | ddqp = &dqb->dd_diskdq; |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1140 | |
Brian Foster | 629dcb3 | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1141 | /* sanity check the in-core structure before we flush */ |
| 1142 | fa = xfs_dquot_verify(mp, &dqp->q_core, be32_to_cpu(dqp->q_core.d_id), |
| 1143 | 0); |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 1144 | if (fa) { |
| 1145 | xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS", |
Brian Foster | 629dcb3 | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1146 | be32_to_cpu(dqp->q_core.d_id), fa); |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1147 | xfs_buf_relse(bp); |
Brian Foster | b707fff | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1148 | error = -EFSCORRUPTED; |
| 1149 | goto out_abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* This is the only portion of data that needs to persist */ |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 1153 | memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
| 1155 | /* |
| 1156 | * Clear the dirty field and remember the flush lsn for later use. |
| 1157 | */ |
Christoph Hellwig | acecf1b | 2010-09-06 01:44:45 +0000 | [diff] [blame] | 1158 | dqp->dq_flags &= ~XFS_DQ_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 1160 | xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn, |
| 1161 | &dqp->q_logitem.qli_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
| 1163 | /* |
Christoph Hellwig | 3fe58f3 | 2013-04-03 16:11:16 +1100 | [diff] [blame] | 1164 | * copy the lsn into the on-disk dquot now while we have the in memory |
| 1165 | * dquot here. This can't be done later in the write verifier as we |
| 1166 | * can't get access to the log item at that point in time. |
Dave Chinner | 6fcdc59 | 2013-06-03 15:28:46 +1000 | [diff] [blame] | 1167 | * |
| 1168 | * We also calculate the CRC here so that the on-disk dquot in the |
| 1169 | * buffer always has a valid CRC. This ensures there is no possibility |
| 1170 | * of a dquot without an up-to-date CRC getting to disk. |
Christoph Hellwig | 3fe58f3 | 2013-04-03 16:11:16 +1100 | [diff] [blame] | 1171 | */ |
| 1172 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
Christoph Hellwig | 3fe58f3 | 2013-04-03 16:11:16 +1100 | [diff] [blame] | 1173 | dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn); |
Dave Chinner | 6fcdc59 | 2013-06-03 15:28:46 +1000 | [diff] [blame] | 1174 | xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk), |
| 1175 | XFS_DQUOT_CRC_OFF); |
Christoph Hellwig | 3fe58f3 | 2013-04-03 16:11:16 +1100 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | * Attach an iodone routine so that we can remove this dquot from the |
| 1180 | * AIL and release the flush lock once the dquot is synced to disk. |
| 1181 | */ |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1182 | xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done, |
| 1183 | &dqp->q_logitem.qli_item); |
| 1184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | /* |
| 1186 | * If the buffer is pinned then push on the log so we won't |
| 1187 | * get stuck waiting in the write for too long. |
| 1188 | */ |
Chandra Seetharaman | 811e64c | 2011-07-22 23:40:27 +0000 | [diff] [blame] | 1189 | if (xfs_buf_ispinned(bp)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1190 | trace_xfs_dqflush_force(dqp); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 1191 | xfs_log_force(mp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1194 | trace_xfs_dqflush_done(dqp); |
Christoph Hellwig | fe7257f | 2012-04-23 15:58:37 +1000 | [diff] [blame] | 1195 | *bpp = bp; |
| 1196 | return 0; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1197 | |
Brian Foster | b707fff | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1198 | out_abort: |
| 1199 | dqp->dq_flags &= ~XFS_DQ_DIRTY; |
Brian Foster | 2b3cf09 | 2020-05-06 13:27:04 -0700 | [diff] [blame] | 1200 | xfs_trans_ail_delete(lip, 0); |
Brian Foster | b707fff | 2020-05-06 13:25:22 -0700 | [diff] [blame] | 1201 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Christoph Hellwig | fe7257f | 2012-04-23 15:58:37 +1000 | [diff] [blame] | 1202 | out_unlock: |
| 1203 | xfs_dqfunlock(dqp); |
Brian Foster | 8d3d7e2 | 2020-03-27 08:29:45 -0700 | [diff] [blame] | 1204 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Christoph Hellwig | 5bb87a3 | 2009-01-19 02:03:19 +0100 | [diff] [blame] | 1207 | /* |
| 1208 | * Lock two xfs_dquot structures. |
| 1209 | * |
| 1210 | * To avoid deadlocks we always lock the quota structure with |
| 1211 | * the lowerd id first. |
| 1212 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | void |
| 1214 | xfs_dqlock2( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 1215 | struct xfs_dquot *d1, |
| 1216 | struct xfs_dquot *d2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | { |
| 1218 | if (d1 && d2) { |
| 1219 | ASSERT(d1 != d2); |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1220 | if (be32_to_cpu(d1->q_core.d_id) > |
| 1221 | be32_to_cpu(d2->q_core.d_id)) { |
Christoph Hellwig | 5bb87a3 | 2009-01-19 02:03:19 +0100 | [diff] [blame] | 1222 | mutex_lock(&d2->q_qlock); |
| 1223 | mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | } else { |
Christoph Hellwig | 5bb87a3 | 2009-01-19 02:03:19 +0100 | [diff] [blame] | 1225 | mutex_lock(&d1->q_qlock); |
| 1226 | mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } |
Christoph Hellwig | 5bb87a3 | 2009-01-19 02:03:19 +0100 | [diff] [blame] | 1228 | } else if (d1) { |
| 1229 | mutex_lock(&d1->q_qlock); |
| 1230 | } else if (d2) { |
| 1231 | mutex_lock(&d2->q_qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Christoph Hellwig | a05931c | 2012-03-13 08:52:37 +0000 | [diff] [blame] | 1235 | int __init |
| 1236 | xfs_qm_init(void) |
| 1237 | { |
Carlos Maiolino | b123176 | 2019-11-14 12:43:03 -0800 | [diff] [blame] | 1238 | xfs_qm_dqzone = kmem_cache_create("xfs_dquot", |
| 1239 | sizeof(struct xfs_dquot), |
| 1240 | 0, 0, NULL); |
Christoph Hellwig | a05931c | 2012-03-13 08:52:37 +0000 | [diff] [blame] | 1241 | if (!xfs_qm_dqzone) |
| 1242 | goto out; |
| 1243 | |
Carlos Maiolino | b123176 | 2019-11-14 12:43:03 -0800 | [diff] [blame] | 1244 | xfs_qm_dqtrxzone = kmem_cache_create("xfs_dqtrx", |
| 1245 | sizeof(struct xfs_dquot_acct), |
| 1246 | 0, 0, NULL); |
Christoph Hellwig | a05931c | 2012-03-13 08:52:37 +0000 | [diff] [blame] | 1247 | if (!xfs_qm_dqtrxzone) |
| 1248 | goto out_free_dqzone; |
| 1249 | |
| 1250 | return 0; |
| 1251 | |
| 1252 | out_free_dqzone: |
Carlos Maiolino | aaf54eb | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 1253 | kmem_cache_destroy(xfs_qm_dqzone); |
Christoph Hellwig | a05931c | 2012-03-13 08:52:37 +0000 | [diff] [blame] | 1254 | out: |
| 1255 | return -ENOMEM; |
| 1256 | } |
| 1257 | |
Gerard Snitselaar | 1c2ccc6 | 2012-03-16 18:36:18 +0000 | [diff] [blame] | 1258 | void |
Christoph Hellwig | a05931c | 2012-03-13 08:52:37 +0000 | [diff] [blame] | 1259 | xfs_qm_exit(void) |
| 1260 | { |
Carlos Maiolino | aaf54eb | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 1261 | kmem_cache_destroy(xfs_qm_dqtrxzone); |
| 1262 | kmem_cache_destroy(xfs_qm_dqzone); |
Christoph Hellwig | a05931c | 2012-03-13 08:52:37 +0000 | [diff] [blame] | 1263 | } |
Darrick J. Wong | 554ba96 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 1264 | |
| 1265 | /* |
| 1266 | * Iterate every dquot of a particular type. The caller must ensure that the |
| 1267 | * particular quota type is active. iter_fn can return negative error codes, |
Darrick J. Wong | e7ee96d | 2019-08-28 14:37:57 -0700 | [diff] [blame] | 1268 | * or -ECANCELED to indicate that it wants to stop iterating. |
Darrick J. Wong | 554ba96 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 1269 | */ |
| 1270 | int |
| 1271 | xfs_qm_dqiterate( |
| 1272 | struct xfs_mount *mp, |
| 1273 | uint dqtype, |
| 1274 | xfs_qm_dqiterate_fn iter_fn, |
| 1275 | void *priv) |
| 1276 | { |
| 1277 | struct xfs_dquot *dq; |
| 1278 | xfs_dqid_t id = 0; |
| 1279 | int error; |
| 1280 | |
| 1281 | do { |
| 1282 | error = xfs_qm_dqget_next(mp, id, dqtype, &dq); |
| 1283 | if (error == -ENOENT) |
| 1284 | return 0; |
| 1285 | if (error) |
| 1286 | return error; |
| 1287 | |
| 1288 | error = iter_fn(dq, dqtype, priv); |
| 1289 | id = be32_to_cpu(dq->q_core.d_id); |
| 1290 | xfs_qm_dqput(dq); |
| 1291 | id++; |
| 1292 | } while (error == 0 && id != 0); |
| 1293 | |
| 1294 | return error; |
| 1295 | } |