Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | |
| 19 | #include <linux/capability.h> |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs.h" |
| 22 | #include "xfs_fs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 25 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_trans.h" |
| 27 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_dir2.h" |
| 30 | #include "xfs_alloc.h" |
| 31 | #include "xfs_dmapi.h" |
| 32 | #include "xfs_quota.h" |
| 33 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 38 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_dinode.h" |
| 40 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 41 | #include "xfs_ialloc.h" |
| 42 | #include "xfs_itable.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_bmap.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 44 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_rtalloc.h" |
| 46 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_attr.h" |
| 49 | #include "xfs_buf_item.h" |
| 50 | #include "xfs_utils.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "xfs_qm.h" |
| 52 | |
| 53 | #ifdef DEBUG |
| 54 | # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args) |
| 55 | #else |
| 56 | # define qdprintk(s, args...) do { } while (0) |
| 57 | #endif |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint); |
| 60 | STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *, |
| 61 | uint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | STATIC uint xfs_qm_export_flags(uint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | STATIC uint xfs_qm_export_qtype_flags(uint); |
| 64 | STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *, |
| 65 | fs_disk_quota_t *); |
| 66 | |
| 67 | |
| 68 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | * Turn off quota accounting and/or enforcement for all udquots and/or |
| 70 | * gdquots. Called only at unmount time. |
| 71 | * |
| 72 | * This assumes that there are no dquots of this file system cached |
| 73 | * incore, and modifies the ondisk dquot directly. Therefore, for example, |
| 74 | * it is an error to call this twice, without purging the cache. |
| 75 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 76 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | xfs_qm_scall_quotaoff( |
| 78 | xfs_mount_t *mp, |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 79 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | uint dqtype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | int error; |
| 83 | uint inactivate_flags; |
| 84 | xfs_qoff_logitem_t *qoffstart; |
| 85 | int nculprits; |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
| 88 | * No file system can have quotas enabled on disk but not in core. |
| 89 | * Note that quota utilities (like quotaoff) _expect_ |
| 90 | * errno == EEXIST here. |
| 91 | */ |
| 92 | if ((mp->m_qflags & flags) == 0) |
| 93 | return XFS_ERROR(EEXIST); |
| 94 | error = 0; |
| 95 | |
| 96 | flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD); |
| 97 | |
| 98 | /* |
| 99 | * We don't want to deal with two quotaoffs messing up each other, |
| 100 | * so we're going to serialize it. quotaoff isn't exactly a performance |
| 101 | * critical thing. |
| 102 | * If quotaoff, then we must be dealing with the root filesystem. |
| 103 | */ |
| 104 | ASSERT(mp->m_quotainfo); |
| 105 | if (mp->m_quotainfo) |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 106 | mutex_lock(&(XFS_QI_QOFFLOCK(mp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | ASSERT(mp->m_quotainfo); |
| 109 | |
| 110 | /* |
| 111 | * If we're just turning off quota enforcement, change mp and go. |
| 112 | */ |
| 113 | if ((flags & XFS_ALL_QUOTA_ACCT) == 0) { |
| 114 | mp->m_qflags &= ~(flags); |
| 115 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 116 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | mp->m_sb.sb_qflags = mp->m_qflags; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 118 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 120 | |
| 121 | /* XXX what to do if error ? Revert back to old vals incore ? */ |
| 122 | error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS); |
| 123 | return (error); |
| 124 | } |
| 125 | |
| 126 | dqtype = 0; |
| 127 | inactivate_flags = 0; |
| 128 | /* |
| 129 | * If accounting is off, we must turn enforcement off, clear the |
| 130 | * quota 'CHKD' certificate to make it known that we have to |
| 131 | * do a quotacheck the next time this quota is turned on. |
| 132 | */ |
| 133 | if (flags & XFS_UQUOTA_ACCT) { |
| 134 | dqtype |= XFS_QMOPT_UQUOTA; |
| 135 | flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD); |
| 136 | inactivate_flags |= XFS_UQUOTA_ACTIVE; |
| 137 | } |
| 138 | if (flags & XFS_GQUOTA_ACCT) { |
| 139 | dqtype |= XFS_QMOPT_GQUOTA; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 140 | flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | inactivate_flags |= XFS_GQUOTA_ACTIVE; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 142 | } else if (flags & XFS_PQUOTA_ACCT) { |
| 143 | dqtype |= XFS_QMOPT_PQUOTA; |
| 144 | flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD); |
| 145 | inactivate_flags |= XFS_PQUOTA_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Nothing to do? Don't complain. This happens when we're just |
| 150 | * turning off quota enforcement. |
| 151 | */ |
| 152 | if ((mp->m_qflags & flags) == 0) { |
| 153 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 154 | return (0); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Write the LI_QUOTAOFF log record, and do SB changes atomically, |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 159 | * and synchronously. If we fail to write, we should abort the |
| 160 | * operation as it cannot be recovered safely if we crash. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | */ |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 162 | error = xfs_qm_log_quotaoff(mp, &qoffstart, flags); |
| 163 | if (error) |
| 164 | goto out_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct |
| 168 | * to take care of the race between dqget and quotaoff. We don't take |
| 169 | * any special locks to reset these bits. All processes need to check |
| 170 | * these bits *after* taking inode lock(s) to see if the particular |
| 171 | * quota type is in the process of being turned off. If *ACTIVE, it is |
| 172 | * guaranteed that all dquot structures and all quotainode ptrs will all |
| 173 | * stay valid as long as that inode is kept locked. |
| 174 | * |
| 175 | * There is no turning back after this. |
| 176 | */ |
| 177 | mp->m_qflags &= ~inactivate_flags; |
| 178 | |
| 179 | /* |
| 180 | * Give back all the dquot reference(s) held by inodes. |
| 181 | * Here we go thru every single incore inode in this file system, and |
| 182 | * do a dqrele on the i_udquot/i_gdquot that it may have. |
| 183 | * Essentially, as long as somebody has an inode locked, this guarantees |
| 184 | * that quotas will not be turned off. This is handy because in a |
| 185 | * transaction once we lock the inode(s) and check for quotaon, we can |
| 186 | * depend on the quota inodes (and other things) being valid as long as |
| 187 | * we keep the lock(s). |
| 188 | */ |
| 189 | xfs_qm_dqrele_all_inodes(mp, flags); |
| 190 | |
| 191 | /* |
| 192 | * Next we make the changes in the quota flag in the mount struct. |
| 193 | * This isn't protected by a particular lock directly, because we |
| 194 | * don't want to take a mrlock everytime we depend on quotas being on. |
| 195 | */ |
| 196 | mp->m_qflags &= ~(flags); |
| 197 | |
| 198 | /* |
| 199 | * Go through all the dquots of this file system and purge them, |
| 200 | * according to what was turned off. We may not be able to get rid |
| 201 | * of all dquots, because dquots can have temporary references that |
| 202 | * are not attached to inodes. eg. xfs_setattr, xfs_create. |
| 203 | * So, if we couldn't purge all the dquots from the filesystem, |
| 204 | * we can't get rid of the incore data structures. |
| 205 | */ |
| 206 | while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF))) |
| 207 | delay(10 * nculprits); |
| 208 | |
| 209 | /* |
| 210 | * Transactions that had started before ACTIVE state bit was cleared |
| 211 | * could have logged many dquots, so they'd have higher LSNs than |
| 212 | * the first QUOTAOFF log record does. If we happen to crash when |
| 213 | * the tail of the log has gone past the QUOTAOFF record, but |
| 214 | * before the last dquot modification, those dquots __will__ |
| 215 | * recover, and that's not good. |
| 216 | * |
| 217 | * So, we have QUOTAOFF start and end logitems; the start |
| 218 | * logitem won't get overwritten until the end logitem appears... |
| 219 | */ |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 220 | error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags); |
| 221 | if (error) { |
| 222 | /* We're screwed now. Shutdown is the only option. */ |
| 223 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 224 | goto out_error; |
| 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * If quotas is completely disabled, close shop. |
| 229 | */ |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 230 | if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) || |
| 231 | ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 233 | xfs_qm_destroy_quotainfo(mp); |
| 234 | return (0); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Release our quotainode references, and vn_purge them, |
| 239 | * if we don't need them anymore. |
| 240 | */ |
| 241 | if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) { |
Christoph Hellwig | 26cc002 | 2008-07-18 17:12:43 +1000 | [diff] [blame] | 242 | IRELE(XFS_QI_UQIP(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | XFS_QI_UQIP(mp) = NULL; |
| 244 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 245 | if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) { |
Christoph Hellwig | 26cc002 | 2008-07-18 17:12:43 +1000 | [diff] [blame] | 246 | IRELE(XFS_QI_GQIP(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | XFS_QI_GQIP(mp) = NULL; |
| 248 | } |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 249 | out_error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 251 | |
| 252 | return (error); |
| 253 | } |
| 254 | |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 255 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | xfs_qm_scall_trunc_qfiles( |
| 257 | xfs_mount_t *mp, |
| 258 | uint flags) |
| 259 | { |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 260 | int error = 0, error2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | xfs_inode_t *qip; |
| 262 | |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 263 | if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags); |
| 265 | return XFS_ERROR(EINVAL); |
| 266 | } |
| 267 | |
| 268 | if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) { |
| 269 | error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0); |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 270 | if (!error) { |
| 271 | error = xfs_truncate_file(mp, qip); |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 272 | IRELE(qip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 276 | if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) && |
| 277 | mp->m_sb.sb_gquotino != NULLFSINO) { |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 278 | error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0); |
| 279 | if (!error2) { |
| 280 | error2 = xfs_truncate_file(mp, qip); |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 281 | IRELE(qip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 285 | return error ? error : error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
| 289 | /* |
| 290 | * Switch on (a given) quota enforcement for a filesystem. This takes |
| 291 | * effect immediately. |
| 292 | * (Switching on quota accounting must be done at mount time.) |
| 293 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 294 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | xfs_qm_scall_quotaon( |
| 296 | xfs_mount_t *mp, |
| 297 | uint flags) |
| 298 | { |
| 299 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | uint qf; |
| 301 | uint accflags; |
| 302 | __int64_t sbflags; |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD); |
| 305 | /* |
| 306 | * Switching on quota accounting must be done at mount time. |
| 307 | */ |
| 308 | accflags = flags & XFS_ALL_QUOTA_ACCT; |
| 309 | flags &= ~(XFS_ALL_QUOTA_ACCT); |
| 310 | |
| 311 | sbflags = 0; |
| 312 | |
| 313 | if (flags == 0) { |
| 314 | qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags); |
| 315 | return XFS_ERROR(EINVAL); |
| 316 | } |
| 317 | |
| 318 | /* No fs can turn on quotas with a delayed effect */ |
| 319 | ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0); |
| 320 | |
| 321 | /* |
| 322 | * Can't enforce without accounting. We check the superblock |
| 323 | * qflags here instead of m_qflags because rootfs can have |
| 324 | * quota acct on ondisk without m_qflags' knowing. |
| 325 | */ |
| 326 | if (((flags & XFS_UQUOTA_ACCT) == 0 && |
| 327 | (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 && |
| 328 | (flags & XFS_UQUOTA_ENFD)) |
| 329 | || |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 330 | ((flags & XFS_PQUOTA_ACCT) == 0 && |
| 331 | (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 && |
Donald Douwsma | 424ea91b | 2007-05-08 13:49:15 +1000 | [diff] [blame] | 332 | (flags & XFS_GQUOTA_ACCT) == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 && |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 334 | (flags & XFS_OQUOTA_ENFD))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n", |
| 336 | flags, mp->m_sb.sb_qflags); |
| 337 | return XFS_ERROR(EINVAL); |
| 338 | } |
| 339 | /* |
| 340 | * If everything's upto-date incore, then don't waste time. |
| 341 | */ |
| 342 | if ((mp->m_qflags & flags) == flags) |
| 343 | return XFS_ERROR(EEXIST); |
| 344 | |
| 345 | /* |
| 346 | * Change sb_qflags on disk but not incore mp->qflags |
| 347 | * if this is the root filesystem. |
| 348 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 349 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | qf = mp->m_sb.sb_qflags; |
| 351 | mp->m_sb.sb_qflags = qf | flags; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 352 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * There's nothing to change if it's the same. |
| 356 | */ |
| 357 | if ((qf & flags) == flags && sbflags == 0) |
| 358 | return XFS_ERROR(EEXIST); |
| 359 | sbflags |= XFS_SB_QFLAGS; |
| 360 | |
| 361 | if ((error = xfs_qm_write_sb_changes(mp, sbflags))) |
| 362 | return (error); |
| 363 | /* |
| 364 | * If we aren't trying to switch on quota enforcement, we are done. |
| 365 | */ |
| 366 | if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) != |
| 367 | (mp->m_qflags & XFS_UQUOTA_ACCT)) || |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 368 | ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) != |
| 369 | (mp->m_qflags & XFS_PQUOTA_ACCT)) || |
| 370 | ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) != |
| 371 | (mp->m_qflags & XFS_GQUOTA_ACCT)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | (flags & XFS_ALL_QUOTA_ENFD) == 0) |
| 373 | return (0); |
| 374 | |
| 375 | if (! XFS_IS_QUOTA_RUNNING(mp)) |
| 376 | return XFS_ERROR(ESRCH); |
| 377 | |
| 378 | /* |
| 379 | * Switch on quota enforcement in core. |
| 380 | */ |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 381 | mutex_lock(&(XFS_QI_QOFFLOCK(mp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD); |
| 383 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 384 | |
| 385 | return (0); |
| 386 | } |
| 387 | |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | /* |
| 390 | * Return quota status information, such as uquota-off, enforcements, etc. |
| 391 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 392 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | xfs_qm_scall_getqstat( |
| 394 | xfs_mount_t *mp, |
| 395 | fs_quota_stat_t *out) |
| 396 | { |
| 397 | xfs_inode_t *uip, *gip; |
| 398 | boolean_t tempuqip, tempgqip; |
| 399 | |
| 400 | uip = gip = NULL; |
| 401 | tempuqip = tempgqip = B_FALSE; |
| 402 | memset(out, 0, sizeof(fs_quota_stat_t)); |
| 403 | |
| 404 | out->qs_version = FS_QSTAT_VERSION; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 405 | if (!xfs_sb_version_hasquota(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | out->qs_uquota.qfs_ino = NULLFSINO; |
| 407 | out->qs_gquota.qfs_ino = NULLFSINO; |
| 408 | return (0); |
| 409 | } |
| 410 | out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags & |
| 411 | (XFS_ALL_QUOTA_ACCT| |
| 412 | XFS_ALL_QUOTA_ENFD)); |
| 413 | out->qs_pad = 0; |
| 414 | out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino; |
| 415 | out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino; |
| 416 | |
| 417 | if (mp->m_quotainfo) { |
| 418 | uip = mp->m_quotainfo->qi_uquotaip; |
| 419 | gip = mp->m_quotainfo->qi_gquotaip; |
| 420 | } |
| 421 | if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) { |
| 422 | if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, |
| 423 | 0, 0, &uip, 0) == 0) |
| 424 | tempuqip = B_TRUE; |
| 425 | } |
| 426 | if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) { |
| 427 | if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, |
| 428 | 0, 0, &gip, 0) == 0) |
| 429 | tempgqip = B_TRUE; |
| 430 | } |
| 431 | if (uip) { |
| 432 | out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks; |
| 433 | out->qs_uquota.qfs_nextents = uip->i_d.di_nextents; |
| 434 | if (tempuqip) |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 435 | IRELE(uip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | if (gip) { |
| 438 | out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks; |
| 439 | out->qs_gquota.qfs_nextents = gip->i_d.di_nextents; |
| 440 | if (tempgqip) |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 441 | IRELE(gip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | if (mp->m_quotainfo) { |
| 444 | out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp); |
| 445 | out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp); |
| 446 | out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp); |
| 447 | out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp); |
| 448 | out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp); |
| 449 | out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp); |
| 450 | } |
| 451 | return (0); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Adjust quota limits, and start/stop timers accordingly. |
| 456 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 457 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | xfs_qm_scall_setqlim( |
| 459 | xfs_mount_t *mp, |
| 460 | xfs_dqid_t id, |
| 461 | uint type, |
| 462 | fs_disk_quota_t *newlim) |
| 463 | { |
| 464 | xfs_disk_dquot_t *ddq; |
| 465 | xfs_dquot_t *dqp; |
| 466 | xfs_trans_t *tp; |
| 467 | int error; |
| 468 | xfs_qcnt_t hard, soft; |
| 469 | |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 470 | if ((newlim->d_fieldmask & |
| 471 | (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | return (0); |
| 473 | |
| 474 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM); |
| 475 | if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128, |
| 476 | 0, 0, XFS_DEFAULT_LOG_COUNT))) { |
| 477 | xfs_trans_cancel(tp, 0); |
| 478 | return (error); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * We don't want to race with a quotaoff so take the quotaoff lock. |
| 483 | * (We don't hold an inode lock, so there's nothing else to stop |
| 484 | * a quotaoff from happening). (XXXThis doesn't currently happen |
| 485 | * because we take the vfslock before calling xfs_qm_sysent). |
| 486 | */ |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 487 | mutex_lock(&(XFS_QI_QOFFLOCK(mp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * Get the dquot (locked), and join it to the transaction. |
| 491 | * Allocate the dquot if this doesn't exist. |
| 492 | */ |
| 493 | if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) { |
| 494 | xfs_trans_cancel(tp, XFS_TRANS_ABORT); |
| 495 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 496 | ASSERT(error != ENOENT); |
| 497 | return (error); |
| 498 | } |
| 499 | xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET"); |
| 500 | xfs_trans_dqjoin(tp, dqp); |
| 501 | ddq = &dqp->q_core; |
| 502 | |
| 503 | /* |
| 504 | * Make sure that hardlimits are >= soft limits before changing. |
| 505 | */ |
| 506 | hard = (newlim->d_fieldmask & FS_DQ_BHARD) ? |
| 507 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 508 | be64_to_cpu(ddq->d_blk_hardlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ? |
| 510 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 511 | be64_to_cpu(ddq->d_blk_softlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (hard == 0 || hard >= soft) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 513 | ddq->d_blk_hardlimit = cpu_to_be64(hard); |
| 514 | ddq->d_blk_softlimit = cpu_to_be64(soft); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (id == 0) { |
| 516 | mp->m_quotainfo->qi_bhardlimit = hard; |
| 517 | mp->m_quotainfo->qi_bsoftlimit = soft; |
| 518 | } |
| 519 | } else { |
| 520 | qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft); |
| 521 | } |
| 522 | hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ? |
| 523 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 524 | be64_to_cpu(ddq->d_rtb_hardlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ? |
| 526 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 527 | be64_to_cpu(ddq->d_rtb_softlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | if (hard == 0 || hard >= soft) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 529 | ddq->d_rtb_hardlimit = cpu_to_be64(hard); |
| 530 | ddq->d_rtb_softlimit = cpu_to_be64(soft); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (id == 0) { |
| 532 | mp->m_quotainfo->qi_rtbhardlimit = hard; |
| 533 | mp->m_quotainfo->qi_rtbsoftlimit = soft; |
| 534 | } |
| 535 | } else { |
| 536 | qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft); |
| 537 | } |
| 538 | |
| 539 | hard = (newlim->d_fieldmask & FS_DQ_IHARD) ? |
| 540 | (xfs_qcnt_t) newlim->d_ino_hardlimit : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 541 | be64_to_cpu(ddq->d_ino_hardlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ? |
| 543 | (xfs_qcnt_t) newlim->d_ino_softlimit : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 544 | be64_to_cpu(ddq->d_ino_softlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (hard == 0 || hard >= soft) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 546 | ddq->d_ino_hardlimit = cpu_to_be64(hard); |
| 547 | ddq->d_ino_softlimit = cpu_to_be64(soft); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | if (id == 0) { |
| 549 | mp->m_quotainfo->qi_ihardlimit = hard; |
| 550 | mp->m_quotainfo->qi_isoftlimit = soft; |
| 551 | } |
| 552 | } else { |
| 553 | qdprintk("ihard %Ld < isoft %Ld\n", hard, soft); |
| 554 | } |
| 555 | |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 556 | /* |
| 557 | * Update warnings counter(s) if requested |
| 558 | */ |
| 559 | if (newlim->d_fieldmask & FS_DQ_BWARNS) |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 560 | ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 561 | if (newlim->d_fieldmask & FS_DQ_IWARNS) |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 562 | ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 563 | if (newlim->d_fieldmask & FS_DQ_RTBWARNS) |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 564 | ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (id == 0) { |
| 567 | /* |
| 568 | * Timelimits for the super user set the relative time |
| 569 | * the other users can be over quota for this file system. |
| 570 | * If it is zero a default is used. Ditto for the default |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 571 | * soft and hard limit values (already done, above), and |
| 572 | * for warnings. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | */ |
| 574 | if (newlim->d_fieldmask & FS_DQ_BTIMER) { |
| 575 | mp->m_quotainfo->qi_btimelimit = newlim->d_btimer; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 576 | ddq->d_btimer = cpu_to_be32(newlim->d_btimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | if (newlim->d_fieldmask & FS_DQ_ITIMER) { |
| 579 | mp->m_quotainfo->qi_itimelimit = newlim->d_itimer; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 580 | ddq->d_itimer = cpu_to_be32(newlim->d_itimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | if (newlim->d_fieldmask & FS_DQ_RTBTIMER) { |
| 583 | mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 584 | ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 586 | if (newlim->d_fieldmask & FS_DQ_BWARNS) |
| 587 | mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns; |
| 588 | if (newlim->d_fieldmask & FS_DQ_IWARNS) |
| 589 | mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns; |
| 590 | if (newlim->d_fieldmask & FS_DQ_RTBWARNS) |
| 591 | mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns; |
| 592 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | /* |
| 594 | * If the user is now over quota, start the timelimit. |
| 595 | * The user will not be 'warned'. |
| 596 | * Note that we keep the timers ticking, whether enforcement |
| 597 | * is on or off. We don't really want to bother with iterating |
| 598 | * over all ondisk dquots and turning the timers on/off. |
| 599 | */ |
| 600 | xfs_qm_adjust_dqtimers(mp, ddq); |
| 601 | } |
| 602 | dqp->dq_flags |= XFS_DQ_DIRTY; |
| 603 | xfs_trans_log_dquot(tp, dqp); |
| 604 | |
| 605 | xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT"); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 606 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | xfs_qm_dqprint(dqp); |
| 608 | xfs_qm_dqrele(dqp); |
| 609 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 610 | |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 611 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 614 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | xfs_qm_scall_getquota( |
| 616 | xfs_mount_t *mp, |
| 617 | xfs_dqid_t id, |
| 618 | uint type, |
| 619 | fs_disk_quota_t *out) |
| 620 | { |
| 621 | xfs_dquot_t *dqp; |
| 622 | int error; |
| 623 | |
| 624 | /* |
| 625 | * Try to get the dquot. We don't want it allocated on disk, so |
| 626 | * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't |
| 627 | * exist, we'll get ENOENT back. |
| 628 | */ |
| 629 | if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) { |
| 630 | return (error); |
| 631 | } |
| 632 | |
| 633 | xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS"); |
| 634 | /* |
| 635 | * If everything's NULL, this dquot doesn't quite exist as far as |
| 636 | * our utility programs are concerned. |
| 637 | */ |
| 638 | if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) { |
| 639 | xfs_qm_dqput(dqp); |
| 640 | return XFS_ERROR(ENOENT); |
| 641 | } |
| 642 | /* xfs_qm_dqprint(dqp); */ |
| 643 | /* |
| 644 | * Convert the disk dquot to the exportable format |
| 645 | */ |
| 646 | xfs_qm_export_dquot(mp, &dqp->q_core, out); |
| 647 | xfs_qm_dqput(dqp); |
| 648 | return (error ? XFS_ERROR(EFAULT) : 0); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | STATIC int |
| 653 | xfs_qm_log_quotaoff_end( |
| 654 | xfs_mount_t *mp, |
| 655 | xfs_qoff_logitem_t *startqoff, |
| 656 | uint flags) |
| 657 | { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 658 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | int error; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 660 | xfs_qoff_logitem_t *qoffi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
| 662 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END); |
| 663 | |
| 664 | if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2, |
| 665 | 0, 0, XFS_DEFAULT_LOG_COUNT))) { |
| 666 | xfs_trans_cancel(tp, 0); |
| 667 | return (error); |
| 668 | } |
| 669 | |
| 670 | qoffi = xfs_trans_get_qoff_item(tp, startqoff, |
| 671 | flags & XFS_ALL_QUOTA_ACCT); |
| 672 | xfs_trans_log_quotaoff_item(tp, qoffi); |
| 673 | |
| 674 | /* |
| 675 | * We have to make sure that the transaction is secure on disk before we |
| 676 | * return and actually stop quota accounting. So, make it synchronous. |
| 677 | * We don't care about quotoff's performance. |
| 678 | */ |
| 679 | xfs_trans_set_sync(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 680 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return (error); |
| 682 | } |
| 683 | |
| 684 | |
| 685 | STATIC int |
| 686 | xfs_qm_log_quotaoff( |
| 687 | xfs_mount_t *mp, |
| 688 | xfs_qoff_logitem_t **qoffstartp, |
| 689 | uint flags) |
| 690 | { |
| 691 | xfs_trans_t *tp; |
| 692 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | xfs_qoff_logitem_t *qoffi=NULL; |
| 694 | uint oldsbqflag=0; |
| 695 | |
| 696 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF); |
| 697 | if ((error = xfs_trans_reserve(tp, 0, |
| 698 | sizeof(xfs_qoff_logitem_t) * 2 + |
| 699 | mp->m_sb.sb_sectsize + 128, |
| 700 | 0, |
| 701 | 0, |
| 702 | XFS_DEFAULT_LOG_COUNT))) { |
| 703 | goto error0; |
| 704 | } |
| 705 | |
| 706 | qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT); |
| 707 | xfs_trans_log_quotaoff_item(tp, qoffi); |
| 708 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 709 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | oldsbqflag = mp->m_sb.sb_qflags; |
| 711 | mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 712 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | xfs_mod_sb(tp, XFS_SB_QFLAGS); |
| 715 | |
| 716 | /* |
| 717 | * We have to make sure that the transaction is secure on disk before we |
| 718 | * return and actually stop quota accounting. So, make it synchronous. |
| 719 | * We don't care about quotoff's performance. |
| 720 | */ |
| 721 | xfs_trans_set_sync(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 722 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | error0: |
| 725 | if (error) { |
| 726 | xfs_trans_cancel(tp, 0); |
| 727 | /* |
| 728 | * No one else is modifying sb_qflags, so this is OK. |
| 729 | * We still hold the quotaofflock. |
| 730 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 731 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | mp->m_sb.sb_qflags = oldsbqflag; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 733 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | *qoffstartp = qoffi; |
| 736 | return (error); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | /* |
| 741 | * Translate an internal style on-disk-dquot to the exportable format. |
| 742 | * The main differences are that the counters/limits are all in Basic |
| 743 | * Blocks (BBs) instead of the internal FSBs, and all on-disk data has |
| 744 | * to be converted to the native endianness. |
| 745 | */ |
| 746 | STATIC void |
| 747 | xfs_qm_export_dquot( |
| 748 | xfs_mount_t *mp, |
| 749 | xfs_disk_dquot_t *src, |
| 750 | struct fs_disk_quota *dst) |
| 751 | { |
| 752 | memset(dst, 0, sizeof(*dst)); |
| 753 | dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */ |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 754 | dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags); |
| 755 | dst->d_id = be32_to_cpu(src->d_id); |
| 756 | dst->d_blk_hardlimit = |
| 757 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit)); |
| 758 | dst->d_blk_softlimit = |
| 759 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit)); |
| 760 | dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit); |
| 761 | dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit); |
| 762 | dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount)); |
| 763 | dst->d_icount = be64_to_cpu(src->d_icount); |
| 764 | dst->d_btimer = be32_to_cpu(src->d_btimer); |
| 765 | dst->d_itimer = be32_to_cpu(src->d_itimer); |
| 766 | dst->d_iwarns = be16_to_cpu(src->d_iwarns); |
| 767 | dst->d_bwarns = be16_to_cpu(src->d_bwarns); |
| 768 | dst->d_rtb_hardlimit = |
| 769 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit)); |
| 770 | dst->d_rtb_softlimit = |
| 771 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit)); |
| 772 | dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount)); |
| 773 | dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer); |
| 774 | dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | /* |
| 777 | * Internally, we don't reset all the timers when quota enforcement |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 778 | * gets turned off. No need to confuse the user level code, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | * so return zeroes in that case. |
| 780 | */ |
Kouta Ooizumi | e6d2942 | 2007-05-08 13:49:33 +1000 | [diff] [blame] | 781 | if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) || |
| 782 | (!XFS_IS_OQUOTA_ENFORCED(mp) && |
| 783 | (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | dst->d_btimer = 0; |
| 785 | dst->d_itimer = 0; |
| 786 | dst->d_rtbtimer = 0; |
| 787 | } |
| 788 | |
| 789 | #ifdef DEBUG |
Kouta Ooizumi | e6d2942 | 2007-05-08 13:49:33 +1000 | [diff] [blame] | 790 | if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) || |
| 791 | (XFS_IS_OQUOTA_ENFORCED(mp) && |
| 792 | (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) && |
| 793 | dst->d_id != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) && |
| 795 | (dst->d_blk_softlimit > 0)) { |
| 796 | ASSERT(dst->d_btimer != 0); |
| 797 | } |
| 798 | if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) && |
| 799 | (dst->d_ino_softlimit > 0)) { |
| 800 | ASSERT(dst->d_itimer != 0); |
| 801 | } |
| 802 | } |
| 803 | #endif |
| 804 | } |
| 805 | |
| 806 | STATIC uint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | xfs_qm_export_qtype_flags( |
| 808 | uint flags) |
| 809 | { |
| 810 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 811 | * Can't be more than one, or none. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | */ |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 813 | ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) != |
| 814 | (XFS_PROJ_QUOTA | XFS_USER_QUOTA)); |
| 815 | ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) != |
| 816 | (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)); |
| 817 | ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) != |
| 818 | (XFS_USER_QUOTA | XFS_GROUP_QUOTA)); |
| 819 | ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | return (flags & XFS_DQ_USER) ? |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 822 | XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ? |
| 823 | XFS_PROJ_QUOTA : XFS_GROUP_QUOTA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | STATIC uint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | xfs_qm_export_flags( |
| 828 | uint flags) |
| 829 | { |
| 830 | uint uflags; |
| 831 | |
| 832 | uflags = 0; |
| 833 | if (flags & XFS_UQUOTA_ACCT) |
| 834 | uflags |= XFS_QUOTA_UDQ_ACCT; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 835 | if (flags & XFS_PQUOTA_ACCT) |
| 836 | uflags |= XFS_QUOTA_PDQ_ACCT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | if (flags & XFS_GQUOTA_ACCT) |
| 838 | uflags |= XFS_QUOTA_GDQ_ACCT; |
| 839 | if (flags & XFS_UQUOTA_ENFD) |
| 840 | uflags |= XFS_QUOTA_UDQ_ENFD; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 841 | if (flags & (XFS_OQUOTA_ENFD)) { |
| 842 | uflags |= (flags & XFS_GQUOTA_ACCT) ? |
| 843 | XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD; |
| 844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | return (uflags); |
| 846 | } |
| 847 | |
| 848 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 849 | STATIC int |
| 850 | xfs_dqrele_inode( |
| 851 | struct xfs_inode *ip, |
| 852 | struct xfs_perag *pag, |
| 853 | int flags) |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 854 | { |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 855 | int error; |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 856 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 857 | /* skip quota inodes */ |
| 858 | if (ip == XFS_QI_UQIP(ip->i_mount) || ip == XFS_QI_GQIP(ip->i_mount)) { |
| 859 | ASSERT(ip->i_udquot == NULL); |
| 860 | ASSERT(ip->i_gdquot == NULL); |
Dave Chinner | cb4f0d1 | 2008-11-10 17:11:18 +1100 | [diff] [blame] | 861 | read_unlock(&pag->pag_ici_lock); |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 862 | return 0; |
| 863 | } |
Dave Chinner | cb4f0d1 | 2008-11-10 17:11:18 +1100 | [diff] [blame] | 864 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 865 | error = xfs_sync_inode_valid(ip, pag); |
| 866 | if (error) |
| 867 | return error; |
Dave Chinner | 6307091 | 2008-11-10 17:13:23 +1100 | [diff] [blame] | 868 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 869 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 870 | if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) { |
| 871 | xfs_qm_dqrele(ip->i_udquot); |
| 872 | ip->i_udquot = NULL; |
| 873 | } |
| 874 | if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) { |
| 875 | xfs_qm_dqrele(ip->i_gdquot); |
| 876 | ip->i_gdquot = NULL; |
| 877 | } |
| 878 | xfs_iput(ip, XFS_ILOCK_EXCL); |
| 879 | IRELE(ip); |
Dave Chinner | cb4f0d1 | 2008-11-10 17:11:18 +1100 | [diff] [blame] | 880 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 881 | return 0; |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 882 | } |
| 883 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 884 | |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 885 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | * Go thru all the inodes in the file system, releasing their dquots. |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 887 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | * Note that the mount structure gets modified to indicate that quotas are off |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 889 | * AFTER this, in the case of quotaoff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | */ |
| 891 | void |
| 892 | xfs_qm_dqrele_all_inodes( |
| 893 | struct xfs_mount *mp, |
| 894 | uint flags) |
| 895 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | ASSERT(mp->m_quotainfo); |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 897 | xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, XFS_ICI_NO_TAG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /*------------------------------------------------------------------------*/ |
| 901 | #ifdef DEBUG |
| 902 | /* |
| 903 | * This contains all the test functions for XFS disk quotas. |
| 904 | * Currently it does a quota accounting check. ie. it walks through |
| 905 | * all inodes in the file system, calculating the dquot accounting fields, |
| 906 | * and prints out any inconsistencies. |
| 907 | */ |
| 908 | xfs_dqhash_t *qmtest_udqtab; |
| 909 | xfs_dqhash_t *qmtest_gdqtab; |
| 910 | int qmtest_hashmask; |
| 911 | int qmtest_nfails; |
Christoph Hellwig | a0b0b8a | 2009-03-29 09:51:00 +0200 | [diff] [blame] | 912 | struct mutex qcheck_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
| 914 | #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \ |
| 915 | (__psunsigned_t)(id)) & \ |
| 916 | (qmtest_hashmask - 1)) |
| 917 | |
| 918 | #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \ |
| 919 | (qmtest_udqtab + \ |
| 920 | DQTEST_HASHVAL(mp, id)) : \ |
| 921 | (qmtest_gdqtab + \ |
| 922 | DQTEST_HASHVAL(mp, id))) |
| 923 | |
| 924 | #define DQTEST_LIST_PRINT(l, NXT, title) \ |
| 925 | { \ |
| 926 | xfs_dqtest_t *dqp; int i = 0;\ |
| 927 | cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \ |
| 928 | for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \ |
| 929 | dqp = (xfs_dqtest_t *)dqp->NXT) { \ |
| 930 | cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \ |
| 931 | ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \ |
| 932 | dqp->d_bcount, dqp->d_icount); } \ |
| 933 | } |
| 934 | |
| 935 | typedef struct dqtest { |
| 936 | xfs_dqmarker_t q_lists; |
| 937 | xfs_dqhash_t *q_hash; /* the hashchain header */ |
| 938 | xfs_mount_t *q_mount; /* filesystem this relates to */ |
| 939 | xfs_dqid_t d_id; /* user id or group id */ |
| 940 | xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */ |
| 941 | xfs_qcnt_t d_icount; /* # inodes owned by the user */ |
| 942 | } xfs_dqtest_t; |
| 943 | |
| 944 | STATIC void |
| 945 | xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp) |
| 946 | { |
| 947 | xfs_dquot_t *d; |
| 948 | if (((d) = (h)->qh_next)) |
| 949 | (d)->HL_PREVP = &((dqp)->HL_NEXT); |
| 950 | (dqp)->HL_NEXT = d; |
| 951 | (dqp)->HL_PREVP = &((h)->qh_next); |
| 952 | (h)->qh_next = (xfs_dquot_t *)dqp; |
| 953 | (h)->qh_version++; |
| 954 | (h)->qh_nelems++; |
| 955 | } |
| 956 | STATIC void |
| 957 | xfs_qm_dqtest_print( |
| 958 | xfs_dqtest_t *d) |
| 959 | { |
| 960 | cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------"); |
| 961 | cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount); |
| 963 | cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)", |
| 964 | d->d_bcount, (int)d->d_bcount); |
| 965 | cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)", |
| 966 | d->d_icount, (int)d->d_icount); |
| 967 | cmn_err(CE_DEBUG, "---------------------------"); |
| 968 | } |
| 969 | |
| 970 | STATIC void |
| 971 | xfs_qm_dqtest_failed( |
| 972 | xfs_dqtest_t *d, |
| 973 | xfs_dquot_t *dqp, |
| 974 | char *reason, |
| 975 | xfs_qcnt_t a, |
| 976 | xfs_qcnt_t b, |
| 977 | int error) |
| 978 | { |
| 979 | qmtest_nfails++; |
| 980 | if (error) |
| 981 | cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 982 | d->d_id, error, reason); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | else |
| 984 | cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 985 | d->d_id, reason, (int)a, (int)b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | xfs_qm_dqtest_print(d); |
| 987 | if (dqp) |
| 988 | xfs_qm_dqprint(dqp); |
| 989 | } |
| 990 | |
| 991 | STATIC int |
| 992 | xfs_dqtest_cmp2( |
| 993 | xfs_dqtest_t *d, |
| 994 | xfs_dquot_t *dqp) |
| 995 | { |
| 996 | int err = 0; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 997 | if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | xfs_qm_dqtest_failed(d, dqp, "icount mismatch", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 999 | be64_to_cpu(dqp->q_core.d_icount), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | d->d_icount, 0); |
| 1001 | err++; |
| 1002 | } |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1003 | if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | xfs_qm_dqtest_failed(d, dqp, "bcount mismatch", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1005 | be64_to_cpu(dqp->q_core.d_bcount), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | d->d_bcount, 0); |
| 1007 | err++; |
| 1008 | } |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1009 | if (dqp->q_core.d_blk_softlimit && |
| 1010 | be64_to_cpu(dqp->q_core.d_bcount) >= |
| 1011 | be64_to_cpu(dqp->q_core.d_blk_softlimit)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | if (!dqp->q_core.d_btimer && dqp->q_core.d_id) { |
| 1013 | cmn_err(CE_DEBUG, |
| 1014 | "%d [%s] [0x%p] BLK TIMER NOT STARTED", |
| 1015 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); |
| 1016 | err++; |
| 1017 | } |
| 1018 | } |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1019 | if (dqp->q_core.d_ino_softlimit && |
| 1020 | be64_to_cpu(dqp->q_core.d_icount) >= |
| 1021 | be64_to_cpu(dqp->q_core.d_ino_softlimit)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | if (!dqp->q_core.d_itimer && dqp->q_core.d_id) { |
| 1023 | cmn_err(CE_DEBUG, |
| 1024 | "%d [%s] [0x%p] INO TIMER NOT STARTED", |
| 1025 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); |
| 1026 | err++; |
| 1027 | } |
| 1028 | } |
| 1029 | #ifdef QUOTADEBUG |
| 1030 | if (!err) { |
| 1031 | cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked", |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1032 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | } |
| 1034 | #endif |
| 1035 | return (err); |
| 1036 | } |
| 1037 | |
| 1038 | STATIC void |
| 1039 | xfs_dqtest_cmp( |
| 1040 | xfs_dqtest_t *d) |
| 1041 | { |
| 1042 | xfs_dquot_t *dqp; |
| 1043 | int error; |
| 1044 | |
| 1045 | /* xfs_qm_dqtest_print(d); */ |
| 1046 | if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0, |
| 1047 | &dqp))) { |
| 1048 | xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error); |
| 1049 | return; |
| 1050 | } |
| 1051 | xfs_dqtest_cmp2(d, dqp); |
| 1052 | xfs_qm_dqput(dqp); |
| 1053 | } |
| 1054 | |
| 1055 | STATIC int |
| 1056 | xfs_qm_internalqcheck_dqget( |
| 1057 | xfs_mount_t *mp, |
| 1058 | xfs_dqid_t id, |
| 1059 | uint type, |
| 1060 | xfs_dqtest_t **O_dq) |
| 1061 | { |
| 1062 | xfs_dqtest_t *d; |
| 1063 | xfs_dqhash_t *h; |
| 1064 | |
| 1065 | h = DQTEST_HASH(mp, id, type); |
| 1066 | for (d = (xfs_dqtest_t *) h->qh_next; d != NULL; |
| 1067 | d = (xfs_dqtest_t *) d->HL_NEXT) { |
| 1068 | /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */ |
| 1069 | if (d->d_id == id && mp == d->q_mount) { |
| 1070 | *O_dq = d; |
| 1071 | return (0); |
| 1072 | } |
| 1073 | } |
| 1074 | d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP); |
| 1075 | d->dq_flags = type; |
| 1076 | d->d_id = id; |
| 1077 | d->q_mount = mp; |
| 1078 | d->q_hash = h; |
| 1079 | xfs_qm_hashinsert(h, d); |
| 1080 | *O_dq = d; |
| 1081 | return (0); |
| 1082 | } |
| 1083 | |
| 1084 | STATIC void |
| 1085 | xfs_qm_internalqcheck_get_dquots( |
| 1086 | xfs_mount_t *mp, |
| 1087 | xfs_dqid_t uid, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1088 | xfs_dqid_t projid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | xfs_dqid_t gid, |
| 1090 | xfs_dqtest_t **ud, |
| 1091 | xfs_dqtest_t **gd) |
| 1092 | { |
| 1093 | if (XFS_IS_UQUOTA_ON(mp)) |
| 1094 | xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud); |
| 1095 | if (XFS_IS_GQUOTA_ON(mp)) |
| 1096 | xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1097 | else if (XFS_IS_PQUOTA_ON(mp)) |
| 1098 | xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | STATIC void |
| 1103 | xfs_qm_internalqcheck_dqadjust( |
| 1104 | xfs_inode_t *ip, |
| 1105 | xfs_dqtest_t *d) |
| 1106 | { |
| 1107 | d->d_icount++; |
| 1108 | d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks; |
| 1109 | } |
| 1110 | |
| 1111 | STATIC int |
| 1112 | xfs_qm_internalqcheck_adjust( |
| 1113 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 1114 | xfs_ino_t ino, /* inode number to get data for */ |
| 1115 | void __user *buffer, /* not used */ |
| 1116 | int ubsize, /* not used */ |
| 1117 | void *private_data, /* not used */ |
| 1118 | xfs_daddr_t bno, /* starting block of inode cluster */ |
| 1119 | int *ubused, /* not used */ |
| 1120 | void *dip, /* not used */ |
| 1121 | int *res) /* bulkstat result code */ |
| 1122 | { |
| 1123 | xfs_inode_t *ip; |
| 1124 | xfs_dqtest_t *ud, *gd; |
| 1125 | uint lock_flags; |
| 1126 | boolean_t ipreleased; |
| 1127 | int error; |
| 1128 | |
| 1129 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
| 1130 | |
| 1131 | if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) { |
| 1132 | *res = BULKSTAT_RV_NOTHING; |
| 1133 | qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n", |
| 1134 | (unsigned long long) ino, |
| 1135 | (unsigned long long) mp->m_sb.sb_uquotino, |
| 1136 | (unsigned long long) mp->m_sb.sb_gquotino); |
| 1137 | return XFS_ERROR(EINVAL); |
| 1138 | } |
| 1139 | ipreleased = B_FALSE; |
| 1140 | again: |
| 1141 | lock_flags = XFS_ILOCK_SHARED; |
| 1142 | if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) { |
| 1143 | *res = BULKSTAT_RV_NOTHING; |
| 1144 | return (error); |
| 1145 | } |
| 1146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | /* |
| 1148 | * This inode can have blocks after eof which can get released |
| 1149 | * when we send it to inactive. Since we don't check the dquot |
| 1150 | * until the after all our calculations are done, we must get rid |
| 1151 | * of those now. |
| 1152 | */ |
| 1153 | if (! ipreleased) { |
| 1154 | xfs_iput(ip, lock_flags); |
| 1155 | ipreleased = B_TRUE; |
| 1156 | goto again; |
| 1157 | } |
| 1158 | xfs_qm_internalqcheck_get_dquots(mp, |
| 1159 | (xfs_dqid_t) ip->i_d.di_uid, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1160 | (xfs_dqid_t) ip->i_d.di_projid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | (xfs_dqid_t) ip->i_d.di_gid, |
| 1162 | &ud, &gd); |
| 1163 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 1164 | ASSERT(ud); |
| 1165 | xfs_qm_internalqcheck_dqadjust(ip, ud); |
| 1166 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1167 | if (XFS_IS_OQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | ASSERT(gd); |
| 1169 | xfs_qm_internalqcheck_dqadjust(ip, gd); |
| 1170 | } |
| 1171 | xfs_iput(ip, lock_flags); |
| 1172 | *res = BULKSTAT_RV_DIDONE; |
| 1173 | return (0); |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | /* PRIVATE, debugging */ |
| 1178 | int |
| 1179 | xfs_qm_internalqcheck( |
| 1180 | xfs_mount_t *mp) |
| 1181 | { |
| 1182 | xfs_ino_t lastino; |
| 1183 | int done, count; |
| 1184 | int i; |
| 1185 | xfs_dqtest_t *d, *e; |
| 1186 | xfs_dqhash_t *h1; |
| 1187 | int error; |
| 1188 | |
| 1189 | lastino = 0; |
| 1190 | qmtest_hashmask = 32; |
| 1191 | count = 5; |
| 1192 | done = 0; |
| 1193 | qmtest_nfails = 0; |
| 1194 | |
| 1195 | if (! XFS_IS_QUOTA_ON(mp)) |
| 1196 | return XFS_ERROR(ESRCH); |
| 1197 | |
| 1198 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC); |
| 1199 | XFS_bflush(mp->m_ddev_targp); |
| 1200 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC); |
| 1201 | XFS_bflush(mp->m_ddev_targp); |
| 1202 | |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 1203 | mutex_lock(&qcheck_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | /* There should be absolutely no quota activity while this |
| 1205 | is going on. */ |
| 1206 | qmtest_udqtab = kmem_zalloc(qmtest_hashmask * |
| 1207 | sizeof(xfs_dqhash_t), KM_SLEEP); |
| 1208 | qmtest_gdqtab = kmem_zalloc(qmtest_hashmask * |
| 1209 | sizeof(xfs_dqhash_t), KM_SLEEP); |
| 1210 | do { |
| 1211 | /* |
| 1212 | * Iterate thru all the inodes in the file system, |
| 1213 | * adjusting the corresponding dquot counters |
| 1214 | */ |
| 1215 | if ((error = xfs_bulkstat(mp, &lastino, &count, |
| 1216 | xfs_qm_internalqcheck_adjust, NULL, |
| 1217 | 0, NULL, BULKSTAT_FG_IGET, &done))) { |
| 1218 | break; |
| 1219 | } |
| 1220 | } while (! done); |
| 1221 | if (error) { |
| 1222 | cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error); |
| 1223 | } |
| 1224 | cmn_err(CE_DEBUG, "Checking results against system dquots"); |
| 1225 | for (i = 0; i < qmtest_hashmask; i++) { |
| 1226 | h1 = &qmtest_udqtab[i]; |
| 1227 | for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) { |
| 1228 | xfs_dqtest_cmp(d); |
| 1229 | e = (xfs_dqtest_t *) d->HL_NEXT; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1230 | kmem_free(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | d = e; |
| 1232 | } |
| 1233 | h1 = &qmtest_gdqtab[i]; |
| 1234 | for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) { |
| 1235 | xfs_dqtest_cmp(d); |
| 1236 | e = (xfs_dqtest_t *) d->HL_NEXT; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1237 | kmem_free(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | d = e; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | if (qmtest_nfails) { |
| 1243 | cmn_err(CE_DEBUG, "******** quotacheck failed ********"); |
| 1244 | cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails); |
| 1245 | } else { |
| 1246 | cmn_err(CE_DEBUG, "******** quotacheck successful! ********"); |
| 1247 | } |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1248 | kmem_free(qmtest_udqtab); |
| 1249 | kmem_free(qmtest_gdqtab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | mutex_unlock(&qcheck_lock); |
| 1251 | return (qmtest_nfails); |
| 1252 | } |
| 1253 | |
| 1254 | #endif /* DEBUG */ |