blob: 95aecf52475d29a4237d8e200a0ecf9b81f686b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
30#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_ialloc.h"
34#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_rtalloc.h"
36#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_buf_item.h"
40#include "xfs_trans_space.h"
41#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * The global quota manager. There is only one of these for the entire
47 * system, _not_ one per file system. XQM keeps track of the overall
48 * quota functionality, including maintaining the freelist and hash
49 * tables of dquots.
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100052STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
Ying Han1495f232011-05-24 17:12:27 -070053STATIC int xfs_qm_shake(struct shrinker *, struct shrink_control *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050056 * We use the batch lookup interface to iterate over the dquots as it
57 * currently is the only interface into the radix tree code that allows
58 * fuzzy lookups instead of exact matches. Holding the lock over multiple
59 * operations is fine as all callers are used either during mount/umount
60 * or quotaoff.
61 */
62#define XFS_DQ_LOOKUP_BATCH 32
63
64STATIC int
65xfs_qm_dquot_walk(
66 struct xfs_mount *mp,
67 int type,
68 int (*execute)(struct xfs_dquot *dqp))
69{
70 struct xfs_quotainfo *qi = mp->m_quotainfo;
71 struct radix_tree_root *tree = XFS_DQUOT_TREE(qi, type);
72 uint32_t next_index;
73 int last_error = 0;
74 int skipped;
75 int nr_found;
76
77restart:
78 skipped = 0;
79 next_index = 0;
80 nr_found = 0;
81
82 while (1) {
83 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
84 int error = 0;
85 int i;
86
87 mutex_lock(&qi->qi_tree_lock);
88 nr_found = radix_tree_gang_lookup(tree, (void **)batch,
89 next_index, XFS_DQ_LOOKUP_BATCH);
90 if (!nr_found) {
91 mutex_unlock(&qi->qi_tree_lock);
92 break;
93 }
94
95 for (i = 0; i < nr_found; i++) {
96 struct xfs_dquot *dqp = batch[i];
97
98 next_index = be32_to_cpu(dqp->q_core.d_id) + 1;
99
100 error = execute(batch[i]);
101 if (error == EAGAIN) {
102 skipped++;
103 continue;
104 }
105 if (error && last_error != EFSCORRUPTED)
106 last_error = error;
107 }
108
109 mutex_unlock(&qi->qi_tree_lock);
110
111 /* bail out if the filesystem is corrupted. */
112 if (last_error == EFSCORRUPTED) {
113 skipped = 0;
114 break;
115 }
116 }
117
118 if (skipped) {
119 delay(1);
120 goto restart;
121 }
122
123 return last_error;
124}
125
126
127/*
128 * Purge a dquot from all tracking data structures and free it.
129 */
130STATIC int
131xfs_qm_dqpurge(
132 struct xfs_dquot *dqp)
133{
134 struct xfs_mount *mp = dqp->q_mount;
135 struct xfs_quotainfo *qi = mp->m_quotainfo;
136 struct xfs_dquot *gdqp = NULL;
137
138 xfs_dqlock(dqp);
139 if ((dqp->dq_flags & XFS_DQ_FREEING) || dqp->q_nrefs != 0) {
140 xfs_dqunlock(dqp);
141 return EAGAIN;
142 }
143
144 /*
145 * If this quota has a group hint attached, prepare for releasing it
146 * now.
147 */
148 gdqp = dqp->q_gdquot;
149 if (gdqp) {
150 xfs_dqlock(gdqp);
151 dqp->q_gdquot = NULL;
152 }
153
154 dqp->dq_flags |= XFS_DQ_FREEING;
155
156 /*
157 * If we're turning off quotas, we have to make sure that, for
158 * example, we don't delete quota disk blocks while dquots are
159 * in the process of getting written to those disk blocks.
160 * This dquot might well be on AIL, and we can't leave it there
161 * if we're turning off quotas. Basically, we need this flush
162 * lock, and are willing to block on it.
163 */
164 if (!xfs_dqflock_nowait(dqp)) {
165 /*
166 * Block on the flush lock after nudging dquot buffer,
167 * if it is incore.
168 */
169 xfs_dqflock_pushbuf_wait(dqp);
170 }
171
172 /*
173 * If we are turning this type of quotas off, we don't care
174 * about the dirty metadata sitting in this dquot. OTOH, if
175 * we're unmounting, we do care, so we flush it and wait.
176 */
177 if (XFS_DQ_IS_DIRTY(dqp)) {
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000178 struct xfs_buf *bp = NULL;
179 int error;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500180
181 /*
182 * We don't care about getting disk errors here. We need
183 * to purge this dquot anyway, so we go ahead regardless.
184 */
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000185 error = xfs_qm_dqflush(dqp, &bp);
186 if (error) {
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500187 xfs_warn(mp, "%s: dquot %p flush failed",
188 __func__, dqp);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000189 } else {
190 error = xfs_bwrite(bp);
191 xfs_buf_relse(bp);
192 }
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500193 xfs_dqflock(dqp);
194 }
195
196 ASSERT(atomic_read(&dqp->q_pincount) == 0);
197 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
198 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
199
200 xfs_dqfunlock(dqp);
201 xfs_dqunlock(dqp);
202
203 radix_tree_delete(XFS_DQUOT_TREE(qi, dqp->q_core.d_flags),
204 be32_to_cpu(dqp->q_core.d_id));
205 qi->qi_dquots--;
206
207 /*
208 * We move dquots to the freelist as soon as their reference count
209 * hits zero, so it really should be on the freelist here.
210 */
211 mutex_lock(&qi->qi_lru_lock);
212 ASSERT(!list_empty(&dqp->q_lru));
213 list_del_init(&dqp->q_lru);
214 qi->qi_lru_count--;
215 XFS_STATS_DEC(xs_qm_dquot_unused);
216 mutex_unlock(&qi->qi_lru_lock);
217
218 xfs_qm_dqdestroy(dqp);
219
220 if (gdqp)
221 xfs_qm_dqput(gdqp);
222 return 0;
223}
224
225/*
226 * Purge the dquot cache.
227 */
228void
229xfs_qm_dqpurge_all(
230 struct xfs_mount *mp,
231 uint flags)
232{
233 if (flags & XFS_QMOPT_UQUOTA)
234 xfs_qm_dquot_walk(mp, XFS_DQ_USER, xfs_qm_dqpurge);
235 if (flags & XFS_QMOPT_GQUOTA)
236 xfs_qm_dquot_walk(mp, XFS_DQ_GROUP, xfs_qm_dqpurge);
237 if (flags & XFS_QMOPT_PQUOTA)
238 xfs_qm_dquot_walk(mp, XFS_DQ_PROJ, xfs_qm_dqpurge);
239}
240
241/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * Just destroy the quotainfo structure.
243 */
244void
Christoph Hellwig7d095252009-06-08 15:33:32 +0200245xfs_qm_unmount(
246 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200248 if (mp->m_quotainfo) {
Christoph Hellwig8112e9d2010-04-20 17:02:29 +1000249 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 xfs_qm_destroy_quotainfo(mp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254
255/*
256 * This is called from xfs_mountfs to start quotas and initialize all
257 * necessary data structures like quotainfo. This is also responsible for
258 * running a quotacheck as necessary. We are guaranteed that the superblock
259 * is consistently read in at this point.
David Chinner53aa7912008-04-10 12:20:31 +1000260 *
261 * If we fail here, the mount will continue with quota turned off. We don't
262 * need to inidicate success or failure at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
David Chinner53aa7912008-04-10 12:20:31 +1000264void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265xfs_qm_mount_quotas(
Christoph Hellwig42490232008-08-13 16:49:32 +1000266 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int error = 0;
269 uint sbf;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /*
272 * If quotas on realtime volumes is not supported, we disable
273 * quotas immediately.
274 */
275 if (mp->m_sb.sb_rextents) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100276 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 mp->m_qflags = 0;
278 goto write_changes;
279 }
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Nathan Scott155ffd02005-09-02 16:43:48 +1000282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /*
284 * Allocate the quotainfo structure inside the mount struct, and
285 * create quotainode(s), and change/rev superblock if necessary.
286 */
David Chinner53aa7912008-04-10 12:20:31 +1000287 error = xfs_qm_init_quotainfo(mp);
288 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /*
290 * We must turn off quotas.
291 */
292 ASSERT(mp->m_quotainfo == NULL);
293 mp->m_qflags = 0;
294 goto write_changes;
295 }
296 /*
297 * If any of the quotas are not consistent, do a quotacheck.
298 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000299 if (XFS_QM_NEED_QUOTACHECK(mp)) {
David Chinner53aa7912008-04-10 12:20:31 +1000300 error = xfs_qm_quotacheck(mp);
301 if (error) {
302 /* Quotacheck failed and disabled quotas. */
303 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000306 /*
307 * If one type of quotas is off, then it will lose its
308 * quotachecked status, since we won't be doing accounting for
309 * that type anymore.
310 */
David Chinner53aa7912008-04-10 12:20:31 +1000311 if (!XFS_IS_UQUOTA_ON(mp))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000312 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
David Chinner53aa7912008-04-10 12:20:31 +1000313 if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
Donald Douwsma646d5bd2007-05-08 13:49:09 +1000314 mp->m_qflags &= ~XFS_OQUOTA_CHKD;
Nathan Scott155ffd02005-09-02 16:43:48 +1000315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 write_changes:
317 /*
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000318 * We actually don't have to acquire the m_sb_lock at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * This can only be called from mount, and that's single threaded. XXX
320 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000321 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 sbf = mp->m_sb.sb_qflags;
323 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000324 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
327 if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
328 /*
329 * We could only have been turning quotas off.
330 * We aren't in very good shape actually because
331 * the incore structures are convinced that quotas are
332 * off, but the on disk superblock doesn't know that !
333 */
334 ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
Dave Chinner53487782011-03-07 10:05:35 +1100335 xfs_alert(mp, "%s: Superblock update failed!",
336 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 }
339
340 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +1100341 xfs_warn(mp, "Failed to initialize disk quotas.");
Christoph Hellwig7d095252009-06-08 15:33:32 +0200342 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346/*
347 * Called from the vfsops layer.
348 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100349void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350xfs_qm_unmount_quotas(
351 xfs_mount_t *mp)
352{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
354 * Release the dquots that root inode, et al might be holding,
355 * before we flush quotas and blow away the quotainfo structure.
356 */
357 ASSERT(mp->m_rootip);
358 xfs_qm_dqdetach(mp->m_rootip);
359 if (mp->m_rbmip)
360 xfs_qm_dqdetach(mp->m_rbmip);
361 if (mp->m_rsumip)
362 xfs_qm_dqdetach(mp->m_rsumip);
363
364 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100365 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100368 if (mp->m_quotainfo->qi_uquotaip) {
369 IRELE(mp->m_quotainfo->qi_uquotaip);
370 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100372 if (mp->m_quotainfo->qi_gquotaip) {
373 IRELE(mp->m_quotainfo->qi_gquotaip);
374 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379STATIC int
380xfs_qm_dqattach_one(
381 xfs_inode_t *ip,
382 xfs_dqid_t id,
383 uint type,
384 uint doalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 xfs_dquot_t *udqhint, /* hint */
386 xfs_dquot_t **IO_idqpp)
387{
388 xfs_dquot_t *dqp;
389 int error;
390
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000391 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * See if we already have it in the inode itself. IO_idqpp is
396 * &i_udquot or &i_gdquot. This made the code look weird, but
397 * made the logic a lot simpler.
398 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100399 dqp = *IO_idqpp;
400 if (dqp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000401 trace_xfs_dqattach_found(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 /*
406 * udqhint is the i_udquot field in inode, and is non-NULL only
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000407 * when the type arg is group/project. Its purpose is to save a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
409 * the user dquot.
410 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100411 if (udqhint) {
412 ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 xfs_dqlock(udqhint);
414
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100415 /*
416 * No need to take dqlock to look at the id.
417 *
418 * The ID can't change until it gets reclaimed, and it won't
419 * be reclaimed as long as we have a ref from inode and we
420 * hold the ilock.
421 */
422 dqp = udqhint->q_gdquot;
423 if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100424 ASSERT(*IO_idqpp == NULL);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100425
Christoph Hellwig78e55892011-12-06 21:58:22 +0000426 *IO_idqpp = xfs_qm_dqhold(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100430
431 /*
432 * We can't hold a dquot lock when we call the dqget code.
433 * We'll deadlock in no time, because of (not conforming to)
434 * lock ordering - the inodelock comes before any dquot lock,
435 * and we may drop and reacquire the ilock in xfs_qm_dqget().
436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 xfs_dqunlock(udqhint);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100438 }
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * Find the dquot from somewhere. This bumps the
442 * reference count of dquot and returns it locked.
443 * This can return ENOENT if dquot didn't exist on
444 * disk and we didn't ask it to allocate;
445 * ESRCH if quotas got turned off suddenly.
446 */
Mitsuo Hayasakadb3e74b2011-11-10 01:33:10 +0000447 error = xfs_qm_dqget(ip->i_mount, ip, id, type,
448 doalloc | XFS_QMOPT_DOWARN, &dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100449 if (error)
450 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000452 trace_xfs_dqattach_get(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * dqget may have dropped and re-acquired the ilock, but it guarantees
456 * that the dquot returned is the one that should go in the inode.
457 */
458 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100459 xfs_dqunlock(dqp);
460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463
464/*
465 * Given a udquot and gdquot, attach a ptr to the group dquot in the
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000466 * udquot as a hint for future lookups.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
468STATIC void
469xfs_qm_dqattach_grouphint(
470 xfs_dquot_t *udq,
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100471 xfs_dquot_t *gdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 xfs_dquot_t *tmp;
474
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100475 xfs_dqlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000477 tmp = udq->q_gdquot;
478 if (tmp) {
479 if (tmp == gdq)
480 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 udq->q_gdquot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 xfs_qm_dqrele(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Christoph Hellwig78e55892011-12-06 21:58:22 +0000486 udq->q_gdquot = xfs_qm_dqhold(gdq);
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000487done:
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100488 xfs_dqunlock(udq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400491static bool
492xfs_qm_need_dqattach(
493 struct xfs_inode *ip)
494{
495 struct xfs_mount *mp = ip->i_mount;
496
497 if (!XFS_IS_QUOTA_RUNNING(mp))
498 return false;
499 if (!XFS_IS_QUOTA_ON(mp))
500 return false;
501 if (!XFS_NOT_DQATTACHED(mp, ip))
502 return false;
503 if (ip->i_ino == mp->m_sb.sb_uquotino ||
504 ip->i_ino == mp->m_sb.sb_gquotino)
505 return false;
506 return true;
507}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000510 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
511 * into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * Inode may get unlocked and relocked in here, and the caller must deal with
514 * the consequences.
515 */
516int
Christoph Hellwig7d095252009-06-08 15:33:32 +0200517xfs_qm_dqattach_locked(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 xfs_inode_t *ip,
519 uint flags)
520{
521 xfs_mount_t *mp = ip->i_mount;
522 uint nquotas = 0;
523 int error = 0;
524
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400525 if (!xfs_qm_need_dqattach(ip))
Jesper Juhl014c2542006-01-15 02:37:08 +0100526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Christoph Hellwig7d095252009-06-08 15:33:32 +0200528 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (XFS_IS_UQUOTA_ON(mp)) {
531 error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
532 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 NULL, &ip->i_udquot);
534 if (error)
535 goto done;
536 nquotas++;
537 }
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000538
539 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000540 if (XFS_IS_OQUOTA_ON(mp)) {
541 error = XFS_IS_GQUOTA_ON(mp) ?
542 xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
543 flags & XFS_QMOPT_DQALLOC,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000544 ip->i_udquot, &ip->i_gdquot) :
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000545 xfs_qm_dqattach_one(ip, xfs_get_projid(ip), XFS_DQ_PROJ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 flags & XFS_QMOPT_DQALLOC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ip->i_udquot, &ip->i_gdquot);
548 /*
549 * Don't worry about the udquot that we may have
550 * attached above. It'll get detached, if not already.
551 */
552 if (error)
553 goto done;
554 nquotas++;
555 }
556
557 /*
558 * Attach this group quota to the user quota as a hint.
559 * This WON'T, in general, result in a thrash.
560 */
561 if (nquotas == 2) {
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000562 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ASSERT(ip->i_udquot);
564 ASSERT(ip->i_gdquot);
565
566 /*
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000567 * We do not have i_udquot locked at this point, but this check
568 * is OK since we don't depend on the i_gdquot to be accurate
569 * 100% all the time. It is just a hint, and this will
570 * succeed in general.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Christoph Hellwigab680bb2011-12-06 21:58:20 +0000572 if (ip->i_udquot->q_gdquot != ip->i_gdquot)
573 xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Christoph Hellwig7d095252009-06-08 15:33:32 +0200576 done:
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200577#ifdef DEBUG
578 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (XFS_IS_UQUOTA_ON(mp))
580 ASSERT(ip->i_udquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000581 if (XFS_IS_OQUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ASSERT(ip->i_gdquot);
583 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200584 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#endif
Christoph Hellwig7d095252009-06-08 15:33:32 +0200586 return error;
587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Christoph Hellwig7d095252009-06-08 15:33:32 +0200589int
590xfs_qm_dqattach(
591 struct xfs_inode *ip,
592 uint flags)
593{
594 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400596 if (!xfs_qm_need_dqattach(ip))
597 return 0;
598
Christoph Hellwig7d095252009-06-08 15:33:32 +0200599 xfs_ilock(ip, XFS_ILOCK_EXCL);
600 error = xfs_qm_dqattach_locked(ip, flags);
601 xfs_iunlock(ip, XFS_ILOCK_EXCL);
602
Jesper Juhl014c2542006-01-15 02:37:08 +0100603 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * Release dquots (and their references) if any.
608 * The inode should be locked EXCL except when this's called by
609 * xfs_ireclaim.
610 */
611void
612xfs_qm_dqdetach(
613 xfs_inode_t *ip)
614{
615 if (!(ip->i_udquot || ip->i_gdquot))
616 return;
617
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000618 trace_xfs_dquot_dqdetach(ip);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
621 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (ip->i_udquot) {
623 xfs_qm_dqrele(ip->i_udquot);
624 ip->i_udquot = NULL;
625 }
626 if (ip->i_gdquot) {
627 xfs_qm_dqrele(ip->i_gdquot);
628 ip->i_gdquot = NULL;
629 }
630}
631
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +0100632/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * This initializes all the quota information that's kept in the
634 * mount structure
635 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000636STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637xfs_qm_init_quotainfo(
638 xfs_mount_t *mp)
639{
640 xfs_quotainfo_t *qinf;
641 int error;
642 xfs_dquot_t *dqp;
643
644 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
647
648 /*
649 * See if quotainodes are setup, and if not, allocate them,
650 * and change the superblock accordingly.
651 */
652 if ((error = xfs_qm_init_quotainos(mp))) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000653 kmem_free(qinf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 mp->m_quotainfo = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100655 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000658 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_NOFS);
659 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_NOFS);
660 mutex_init(&qinf->qi_tree_lock);
661
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000662 INIT_LIST_HEAD(&qinf->qi_lru_list);
663 qinf->qi_lru_count = 0;
664 mutex_init(&qinf->qi_lru_lock);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800667 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* Precalc some constants */
670 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
671 ASSERT(qinf->qi_dqchunklen);
672 qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
673 do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
674
675 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
676
677 /*
678 * We try to get the limits from the superuser's limits fields.
679 * This is quite hacky, but it is standard quota practice.
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * We look at the USR dquot with id == 0 first, but if user quotas
682 * are not enabled we goto the GRP dquot with id == 0.
683 * We don't really care to keep separate default limits for user
684 * and group quotas, at least not at this point.
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000685 *
686 * Since we may not have done a quotacheck by this point, just read
687 * the dquot without attaching it to any hashtables or lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000689 error = xfs_qm_dqread(mp, 0,
690 XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
691 (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
692 XFS_DQ_PROJ),
693 XFS_QMOPT_DOWARN, &dqp);
694 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 xfs_disk_dquot_t *ddqp = &dqp->q_core;
696
697 /*
698 * The warnings and timers set the grace period given to
699 * a user or group before he or she can not perform any
700 * more writing. If it is zero, a default is used.
701 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100702 qinf->qi_btimelimit = ddqp->d_btimer ?
703 be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
704 qinf->qi_itimelimit = ddqp->d_itimer ?
705 be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
706 qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
707 be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
708 qinf->qi_bwarnlimit = ddqp->d_bwarns ?
709 be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
710 qinf->qi_iwarnlimit = ddqp->d_iwarns ?
711 be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
712 qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
713 be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
714 qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
715 qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
716 qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
717 qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
718 qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
719 qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 xfs_qm_dqdestroy(dqp);
722 } else {
723 qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
724 qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
725 qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
726 qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
727 qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000728 qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000731 qinf->qi_shrinker.shrink = xfs_qm_shake;
732 qinf->qi_shrinker.seeks = DEFAULT_SEEKS;
733 register_shrinker(&qinf->qi_shrinker);
Jesper Juhl014c2542006-01-15 02:37:08 +0100734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737
738/*
739 * Gets called when unmounting a filesystem or when all quotas get
740 * turned off.
741 * This purges the quota inodes, destroys locks and frees itself.
742 */
743void
744xfs_qm_destroy_quotainfo(
745 xfs_mount_t *mp)
746{
747 xfs_quotainfo_t *qi;
748
749 qi = mp->m_quotainfo;
750 ASSERT(qi != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000752 unregister_shrinker(&qi->qi_shrinker);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (qi->qi_uquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000755 IRELE(qi->qi_uquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 qi->qi_uquotaip = NULL; /* paranoia */
757 }
758 if (qi->qi_gquotaip) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000759 IRELE(qi->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 qi->qi_gquotaip = NULL;
761 }
762 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000763 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mp->m_quotainfo = NULL;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
768 * Create an inode and return with a reference already taken, but unlocked
769 * This is how we create quota inodes
770 */
771STATIC int
772xfs_qm_qino_alloc(
773 xfs_mount_t *mp,
774 xfs_inode_t **ip,
775 __int64_t sbfields,
776 uint flags)
777{
778 xfs_trans_t *tp;
779 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int committed;
781
Nathan Scott061f7202006-01-11 15:27:50 +1100782 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if ((error = xfs_trans_reserve(tp,
784 XFS_QM_QINOCREATE_SPACE_RES(mp),
785 XFS_CREATE_LOG_RES(mp), 0,
786 XFS_TRANS_PERM_LOG_RES,
787 XFS_CREATE_LOG_COUNT))) {
788 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +0100789 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000792 error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, 1, ip, &committed);
793 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
795 XFS_TRANS_ABORT);
Jesper Juhl014c2542006-01-15 02:37:08 +0100796 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * Make the changes in the superblock, and log those too.
801 * sbfields arg may contain fields other than *QUOTINO;
802 * VERSIONNUM for example.
803 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000804 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (flags & XFS_QMOPT_SBVERSION) {
Eric Sandeen62118702008-03-06 13:44:28 +1100806 ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
808 XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
809 (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
810 XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
811
Eric Sandeen62118702008-03-06 13:44:28 +1100812 xfs_sb_version_addquota(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 mp->m_sb.sb_uquotino = NULLFSINO;
814 mp->m_sb.sb_gquotino = NULLFSINO;
815
816 /* qflags will get updated _after_ quotacheck */
817 mp->m_sb.sb_qflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 if (flags & XFS_QMOPT_UQUOTA)
820 mp->m_sb.sb_uquotino = (*ip)->i_ino;
821 else
822 mp->m_sb.sb_gquotino = (*ip)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000823 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 xfs_mod_sb(tp, sbfields);
825
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000826 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
Dave Chinner53487782011-03-07 10:05:35 +1100827 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
Jesper Juhl014c2542006-01-15 02:37:08 +0100828 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833
David Chinner5b139732008-04-10 12:20:10 +1000834STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835xfs_qm_reset_dqcounts(
836 xfs_mount_t *mp,
837 xfs_buf_t *bp,
838 xfs_dqid_t id,
839 uint type)
840{
841 xfs_disk_dquot_t *ddq;
842 int j;
843
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000844 trace_xfs_reset_dqcounts(bp, _RET_IP_);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
847 * Reset all counters and timers. They'll be
848 * started afresh by xfs_qm_quotacheck.
849 */
850#ifdef DEBUG
851 j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
852 do_div(j, sizeof(xfs_dqblk_t));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000853 ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854#endif
Chandra Seetharaman62926042011-07-22 23:40:15 +0000855 ddq = bp->b_addr;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000856 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
858 * Do a sanity check, and if needed, repair the dqblk. Don't
859 * output any warnings because it's perfectly possible to
Nathan Scottc41564b2006-03-29 08:55:14 +1000860 * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100862 (void) xfs_qm_dqcheck(mp, ddq, id+j, type, XFS_QMOPT_DQREPAIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 "xfs_quotacheck");
Christoph Hellwig1149d962005-11-02 15:01:12 +1100864 ddq->d_bcount = 0;
865 ddq->d_icount = 0;
866 ddq->d_rtbcount = 0;
867 ddq->d_btimer = 0;
868 ddq->d_itimer = 0;
869 ddq->d_rtbtimer = 0;
870 ddq->d_bwarns = 0;
871 ddq->d_iwarns = 0;
872 ddq->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877STATIC int
878xfs_qm_dqiter_bufs(
879 xfs_mount_t *mp,
880 xfs_dqid_t firstid,
881 xfs_fsblock_t bno,
882 xfs_filblks_t blkcnt,
883 uint flags)
884{
885 xfs_buf_t *bp;
886 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000887 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 ASSERT(blkcnt > 0);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000890 type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
891 (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 error = 0;
893
894 /*
895 * Blkcnt arg can be a very big number, and might even be
896 * larger than the log itself. So, we have to break it up into
897 * manageable-sized transactions.
898 * Note that we don't start a permanent transaction here; we might
899 * not be able to get a log reservation for the whole thing up front,
900 * and we don't really care to either, because we just discard
901 * everything if we were to crash in the middle of this loop.
902 */
903 while (blkcnt--) {
904 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
905 XFS_FSB_TO_DADDR(mp, bno),
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000906 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (error)
908 break;
909
David Chinner5b139732008-04-10 12:20:10 +1000910 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Christoph Hellwig61551f12011-08-23 08:28:06 +0000911 xfs_buf_delwri_queue(bp);
912 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * goto the next block.
915 */
916 bno++;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000917 firstid += mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100919 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000923 * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * caller supplied function for every chunk of dquots that we find.
925 */
926STATIC int
927xfs_qm_dqiterate(
928 xfs_mount_t *mp,
929 xfs_inode_t *qip,
930 uint flags)
931{
932 xfs_bmbt_irec_t *map;
933 int i, nmaps; /* number of map entries */
934 int error; /* return value */
935 xfs_fileoff_t lblkno;
936 xfs_filblks_t maxlblkcnt;
937 xfs_dqid_t firstid;
938 xfs_fsblock_t rablkno;
939 xfs_filblks_t rablkcnt;
940
941 error = 0;
942 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000943 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * trans_reserve. But, this gets called during quotacheck, and that
945 * happens only at mount time which is single threaded.
946 */
947 if (qip->i_d.di_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
951
952 lblkno = 0;
953 maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
954 do {
955 nmaps = XFS_DQITER_MAP_SIZE;
956 /*
957 * We aren't changing the inode itself. Just changing
958 * some of its data. No new blocks are added here, and
959 * the inode is never added to the transaction.
960 */
961 xfs_ilock(qip, XFS_ILOCK_SHARED);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000962 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
963 map, &nmaps, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 xfs_iunlock(qip, XFS_ILOCK_SHARED);
965 if (error)
966 break;
967
968 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
969 for (i = 0; i < nmaps; i++) {
970 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
971 ASSERT(map[i].br_blockcount);
972
973
974 lblkno += map[i].br_blockcount;
975
976 if (map[i].br_startblock == HOLESTARTBLOCK)
977 continue;
978
979 firstid = (xfs_dqid_t) map[i].br_startoff *
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000980 mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /*
982 * Do a read-ahead on the next extent.
983 */
984 if ((i+1 < nmaps) &&
985 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
986 rablkcnt = map[i+1].br_blockcount;
987 rablkno = map[i+1].br_startblock;
988 while (rablkcnt--) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000989 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 XFS_FSB_TO_DADDR(mp, rablkno),
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000991 mp->m_quotainfo->qi_dqchunklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 rablkno++;
993 }
994 }
995 /*
996 * Iterate thru all the blks in the extent and
997 * reset the counters of all the dquots inside them.
998 */
999 if ((error = xfs_qm_dqiter_bufs(mp,
1000 firstid,
1001 map[i].br_startblock,
1002 map[i].br_blockcount,
1003 flags))) {
1004 break;
1005 }
1006 }
1007
1008 if (error)
1009 break;
1010 } while (nmaps > 0);
1011
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001012 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Jesper Juhl014c2542006-01-15 02:37:08 +01001014 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/*
1018 * Called by dqusage_adjust in doing a quotacheck.
Christoph Hellwig52fda112010-09-06 01:44:22 +00001019 *
1020 * Given the inode, and a dquot id this updates both the incore dqout as well
1021 * as the buffer copy. This is so that once the quotacheck is done, we can
1022 * just log all the buffers, as opposed to logging numerous updates to
1023 * individual dquots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001025STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026xfs_qm_quotacheck_dqadjust(
Christoph Hellwig52fda112010-09-06 01:44:22 +00001027 struct xfs_inode *ip,
1028 xfs_dqid_t id,
1029 uint type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 xfs_qcnt_t nblks,
1031 xfs_qcnt_t rtblks)
1032{
Christoph Hellwig52fda112010-09-06 01:44:22 +00001033 struct xfs_mount *mp = ip->i_mount;
1034 struct xfs_dquot *dqp;
1035 int error;
1036
1037 error = xfs_qm_dqget(mp, ip, id, type,
1038 XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN, &dqp);
1039 if (error) {
1040 /*
1041 * Shouldn't be able to turn off quotas here.
1042 */
1043 ASSERT(error != ESRCH);
1044 ASSERT(error != ENOENT);
1045 return error;
1046 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001047
1048 trace_xfs_dqadjust(dqp);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * Adjust the inode count and the block count to reflect this inode's
1052 * resource usage.
1053 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001054 be64_add_cpu(&dqp->q_core.d_icount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 dqp->q_res_icount++;
1056 if (nblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001057 be64_add_cpu(&dqp->q_core.d_bcount, nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 dqp->q_res_bcount += nblks;
1059 }
1060 if (rtblks) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001061 be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 dqp->q_res_rtbcount += rtblks;
1063 }
1064
1065 /*
1066 * Set default limits, adjust timers (since we changed usages)
Christoph Hellwig191f8482010-04-20 17:01:53 +10001067 *
1068 * There are no timers for the default values set in the root dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Christoph Hellwig191f8482010-04-20 17:01:53 +10001070 if (dqp->q_core.d_id) {
Christoph Hellwig52fda112010-09-06 01:44:22 +00001071 xfs_qm_adjust_dqlimits(mp, &dqp->q_core);
1072 xfs_qm_adjust_dqtimers(mp, &dqp->q_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
1075 dqp->dq_flags |= XFS_DQ_DIRTY;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001076 xfs_qm_dqput(dqp);
1077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080STATIC int
1081xfs_qm_get_rtblks(
1082 xfs_inode_t *ip,
1083 xfs_qcnt_t *O_rtblks)
1084{
1085 xfs_filblks_t rtblks; /* total rt blks */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001086 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 xfs_ifork_t *ifp; /* inode fork pointer */
1088 xfs_extnum_t nextents; /* number of extent entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 int error;
1090
1091 ASSERT(XFS_IS_REALTIME_INODE(ip));
1092 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1093 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1094 if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001095 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 rtblks = 0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001098 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001099 for (idx = 0; idx < nextents; idx++)
1100 rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 *O_rtblks = (xfs_qcnt_t)rtblks;
Jesper Juhl014c2542006-01-15 02:37:08 +01001102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105/*
1106 * callback routine supplied to bulkstat(). Given an inumber, find its
1107 * dquots and update them to account for resources taken by that inode.
1108 */
1109/* ARGSUSED */
1110STATIC int
1111xfs_qm_dqusage_adjust(
1112 xfs_mount_t *mp, /* mount point for filesystem */
1113 xfs_ino_t ino, /* inode number to get data for */
1114 void __user *buffer, /* not used */
1115 int ubsize, /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 int *ubused, /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int *res) /* result code value */
1118{
1119 xfs_inode_t *ip;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001120 xfs_qcnt_t nblks, rtblks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int error;
1122
1123 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1124
1125 /*
1126 * rootino must have its resources accounted for, not so with the quota
1127 * inodes.
1128 */
1129 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1130 *res = BULKSTAT_RV_NOTHING;
1131 return XFS_ERROR(EINVAL);
1132 }
1133
1134 /*
1135 * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
1136 * interface expects the inode to be exclusively locked because that's
1137 * the case in all other instances. It's OK that we do this because
1138 * quotacheck is done only at mount time.
1139 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001140 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip);
1141 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 *res = BULKSTAT_RV_NOTHING;
Jesper Juhl014c2542006-01-15 02:37:08 +01001143 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145
Christoph Hellwig52fda112010-09-06 01:44:22 +00001146 ASSERT(ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Christoph Hellwig52fda112010-09-06 01:44:22 +00001148 if (XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
1150 * Walk thru the extent list and count the realtime blocks.
1151 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001152 error = xfs_qm_get_rtblks(ip, &rtblks);
1153 if (error)
1154 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Christoph Hellwig52fda112010-09-06 01:44:22 +00001157 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /*
1160 * Add the (disk blocks and inode) resources occupied by this
1161 * inode to its dquots. We do this adjustment in the incore dquot,
1162 * and also copy the changes to its buffer.
1163 * We don't care about putting these changes in a transaction
1164 * envelope because if we crash in the middle of a 'quotacheck'
1165 * we have to start from the beginning anyway.
1166 * Once we're done, we'll log all the dquot bufs.
1167 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001168 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1170 */
1171 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig52fda112010-09-06 01:44:22 +00001172 error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_uid,
1173 XFS_DQ_USER, nblks, rtblks);
1174 if (error)
1175 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Christoph Hellwig52fda112010-09-06 01:44:22 +00001178 if (XFS_IS_GQUOTA_ON(mp)) {
1179 error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_gid,
1180 XFS_DQ_GROUP, nblks, rtblks);
1181 if (error)
1182 goto error0;
1183 }
1184
1185 if (XFS_IS_PQUOTA_ON(mp)) {
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001186 error = xfs_qm_quotacheck_dqadjust(ip, xfs_get_projid(ip),
Christoph Hellwig52fda112010-09-06 01:44:22 +00001187 XFS_DQ_PROJ, nblks, rtblks);
1188 if (error)
1189 goto error0;
1190 }
1191
1192 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1193 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 *res = BULKSTAT_RV_DIDONE;
Jesper Juhl014c2542006-01-15 02:37:08 +01001195 return 0;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001196
1197error0:
1198 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1199 IRELE(ip);
1200 *res = BULKSTAT_RV_GIVEUP;
1201 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001204STATIC int
1205xfs_qm_flush_one(
1206 struct xfs_dquot *dqp)
1207{
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001208 struct xfs_buf *bp = NULL;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001209 int error = 0;
1210
1211 xfs_dqlock(dqp);
1212 if (dqp->dq_flags & XFS_DQ_FREEING)
1213 goto out_unlock;
1214 if (!XFS_DQ_IS_DIRTY(dqp))
1215 goto out_unlock;
1216
1217 if (!xfs_dqflock_nowait(dqp))
1218 xfs_dqflock_pushbuf_wait(dqp);
1219
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001220 error = xfs_qm_dqflush(dqp, &bp);
1221 if (error)
1222 goto out_unlock;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001223
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001224 xfs_buf_delwri_queue(bp);
1225 xfs_buf_relse(bp);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001226out_unlock:
1227 xfs_dqunlock(dqp);
1228 return error;
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
1232 * Walk thru all the filesystem inodes and construct a consistent view
1233 * of the disk quota world. If the quotacheck fails, disable quotas.
1234 */
1235int
1236xfs_qm_quotacheck(
1237 xfs_mount_t *mp)
1238{
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001239 int done, count, error, error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 xfs_ino_t lastino;
1241 size_t structsz;
1242 xfs_inode_t *uip, *gip;
1243 uint flags;
1244
1245 count = INT_MAX;
1246 structsz = 1;
1247 lastino = 0;
1248 flags = 0;
1249
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001250 ASSERT(mp->m_quotainfo->qi_uquotaip || mp->m_quotainfo->qi_gquotaip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1252
Dave Chinner0b932cc2011-03-07 10:08:35 +11001253 xfs_notice(mp, "Quotacheck needed: Please wait.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001256 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 * their counters to zero. We need a clean slate.
1258 * We don't log our changes till later.
1259 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001260 uip = mp->m_quotainfo->qi_uquotaip;
1261 if (uip) {
1262 error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA);
1263 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto error_return;
1265 flags |= XFS_UQUOTA_CHKD;
1266 }
1267
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001268 gip = mp->m_quotainfo->qi_gquotaip;
1269 if (gip) {
1270 error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
1271 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1272 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto error_return;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001274 flags |= XFS_OQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
1277 do {
1278 /*
1279 * Iterate thru all the inodes in the file system,
1280 * adjusting the corresponding dquot counters in core.
1281 */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +10001282 error = xfs_bulkstat(mp, &lastino, &count,
1283 xfs_qm_dqusage_adjust,
1284 structsz, NULL, &done);
1285 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
1287
Christoph Hellwig7dce11d2010-06-23 18:11:11 +10001288 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001291 * We've made all the changes that we need to make incore. Flush them
1292 * down to disk buffers if everything was updated successfully.
David Chinner4b8879d2008-04-10 12:20:17 +10001293 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001294 if (XFS_IS_UQUOTA_ON(mp))
1295 error = xfs_qm_dquot_walk(mp, XFS_DQ_USER, xfs_qm_flush_one);
1296 if (XFS_IS_GQUOTA_ON(mp)) {
1297 error2 = xfs_qm_dquot_walk(mp, XFS_DQ_GROUP, xfs_qm_flush_one);
1298 if (!error)
1299 error = error2;
1300 }
1301 if (XFS_IS_PQUOTA_ON(mp)) {
1302 error2 = xfs_qm_dquot_walk(mp, XFS_DQ_PROJ, xfs_qm_flush_one);
1303 if (!error)
1304 error = error2;
1305 }
David Chinner4b8879d2008-04-10 12:20:17 +10001306
1307 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * We can get this error if we couldn't do a dquot allocation inside
1309 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1310 * dirty dquots that might be cached, we just want to get rid of them
1311 * and turn quotaoff. The dquots won't be attached to any of the inodes
1312 * at this point (because we intentionally didn't in dqget_noattach).
1313 */
1314 if (error) {
Christoph Hellwig8112e9d2010-04-20 17:02:29 +10001315 xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto error_return;
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /*
1320 * We didn't log anything, because if we crashed, we'll have to
1321 * start the quotacheck from scratch anyway. However, we must make
1322 * sure that our dquot changes are secure before we put the
1323 * quotacheck'd stamp on the superblock. So, here we do a synchronous
1324 * flush.
1325 */
Christoph Hellwiga9add832011-10-10 16:52:52 +00001326 xfs_flush_buftarg(mp->m_ddev_targp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /*
1329 * If one type of quotas is off, then it will lose its
1330 * quotachecked status, since we won't be doing accounting for
1331 * that type anymore.
1332 */
Chandra Seetharaman4177af32012-01-23 17:31:43 +00001333 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 mp->m_qflags |= flags;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 error_return:
1337 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001338 xfs_warn(mp,
1339 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
1340 error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /*
1342 * We must turn off quotas.
1343 */
1344 ASSERT(mp->m_quotainfo != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001346 if (xfs_mount_reset_sbqflags(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001347 xfs_warn(mp,
1348 "Quotacheck: Failed to reset quota flags.");
David Chinner31d55772008-04-10 12:20:38 +10001349 }
Dave Chinner0b932cc2011-03-07 10:08:35 +11001350 } else
1351 xfs_notice(mp, "Quotacheck: Done.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return (error);
1353}
1354
1355/*
1356 * This is called after the superblock has been read in and we're ready to
1357 * iget the quota inodes.
1358 */
1359STATIC int
1360xfs_qm_init_quotainos(
1361 xfs_mount_t *mp)
1362{
1363 xfs_inode_t *uip, *gip;
1364 int error;
1365 __int64_t sbflags;
1366 uint flags;
1367
1368 ASSERT(mp->m_quotainfo);
1369 uip = gip = NULL;
1370 sbflags = 0;
1371 flags = 0;
1372
1373 /*
1374 * Get the uquota and gquota inodes
1375 */
Eric Sandeen62118702008-03-06 13:44:28 +11001376 if (xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (XFS_IS_UQUOTA_ON(mp) &&
1378 mp->m_sb.sb_uquotino != NULLFSINO) {
1379 ASSERT(mp->m_sb.sb_uquotino > 0);
1380 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +10001381 0, 0, &uip)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return XFS_ERROR(error);
1383 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001384 if (XFS_IS_OQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 mp->m_sb.sb_gquotino != NULLFSINO) {
1386 ASSERT(mp->m_sb.sb_gquotino > 0);
1387 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +10001388 0, 0, &gip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001390 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return XFS_ERROR(error);
1392 }
1393 }
1394 } else {
1395 flags |= XFS_QMOPT_SBVERSION;
1396 sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1397 XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
1398 }
1399
1400 /*
1401 * Create the two inodes, if they don't exist already. The changes
1402 * made above will get added to a transaction and logged in one of
1403 * the qino_alloc calls below. If the device is readonly,
1404 * temporarily switch to read-write to do this.
1405 */
1406 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
1407 if ((error = xfs_qm_qino_alloc(mp, &uip,
1408 sbflags | XFS_SB_UQUOTINO,
1409 flags | XFS_QMOPT_UQUOTA)))
1410 return XFS_ERROR(error);
1411
1412 flags &= ~XFS_QMOPT_SBVERSION;
1413 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001414 if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
1415 flags |= (XFS_IS_GQUOTA_ON(mp) ?
1416 XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
1417 error = xfs_qm_qino_alloc(mp, &gip,
1418 sbflags | XFS_SB_GQUOTINO, flags);
1419 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (uip)
Christoph Hellwig43355092008-03-27 18:01:08 +11001421 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 return XFS_ERROR(error);
1424 }
1425 }
1426
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001427 mp->m_quotainfo->qi_uquotaip = uip;
1428 mp->m_quotainfo->qi_gquotaip = gip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Jesper Juhl014c2542006-01-15 02:37:08 +01001430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001433STATIC void
1434xfs_qm_dqfree_one(
1435 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001437 struct xfs_mount *mp = dqp->q_mount;
1438 struct xfs_quotainfo *qi = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Christoph Hellwig9f920f12012-03-13 08:52:35 +00001440 mutex_lock(&qi->qi_tree_lock);
1441 radix_tree_delete(XFS_DQUOT_TREE(qi, dqp->q_core.d_flags),
1442 be32_to_cpu(dqp->q_core.d_id));
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001443
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001444 qi->qi_dquots--;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001445 mutex_unlock(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001447 xfs_qm_dqdestroy(dqp);
1448}
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +00001449
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001450STATIC void
1451xfs_qm_dqreclaim_one(
1452 struct xfs_dquot *dqp,
1453 struct list_head *dispose_list)
1454{
1455 struct xfs_mount *mp = dqp->q_mount;
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001456 struct xfs_quotainfo *qi = mp->m_quotainfo;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001457 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001459 if (!xfs_dqlock_nowait(dqp))
1460 goto out_busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001462 /*
1463 * This dquot has acquired a reference in the meantime remove it from
1464 * the freelist and try again.
1465 */
1466 if (dqp->q_nrefs) {
Christoph Hellwig92678552011-12-06 21:58:18 +00001467 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001469 trace_xfs_dqreclaim_want(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001470 XFS_STATS_INC(xs_qm_dqwants);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001471
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001472 list_del_init(&dqp->q_lru);
1473 qi->qi_lru_count--;
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001474 XFS_STATS_DEC(xs_qm_dquot_unused);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001475 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
Christoph Hellwig92678552011-12-06 21:58:18 +00001477
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001478 /*
1479 * Try to grab the flush lock. If this dquot is in the process of
1480 * getting flushed to disk, we don't want to reclaim it.
1481 */
1482 if (!xfs_dqflock_nowait(dqp))
1483 goto out_busy;
Dave Chinner368e1362010-04-13 15:06:50 +10001484
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001485 /*
1486 * We have the flush lock so we know that this is not in the
1487 * process of being flushed. So, if this is dirty, flush it
1488 * DELWRI so that we don't get a freelist infested with
1489 * dirty dquots.
1490 */
1491 if (XFS_DQ_IS_DIRTY(dqp)) {
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001492 struct xfs_buf *bp = NULL;
1493
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001494 trace_xfs_dqreclaim_dirty(dqp);
Dave Chinner368e1362010-04-13 15:06:50 +10001495
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001496 /*
1497 * We flush it delayed write, so don't bother releasing the
1498 * freelist lock.
1499 */
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001500 error = xfs_qm_dqflush(dqp, &bp);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001501 if (error) {
1502 xfs_warn(mp, "%s: dquot %p flush failed",
1503 __func__, dqp);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001504 goto out_busy;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001505 }
1506
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001507 xfs_buf_delwri_queue(bp);
1508 xfs_buf_relse(bp);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001509 /*
1510 * Give the dquot another try on the freelist, as the
1511 * flushing will take some time.
1512 */
1513 goto out_busy;
Dave Chinner368e1362010-04-13 15:06:50 +10001514 }
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001515 xfs_dqfunlock(dqp);
1516
1517 /*
1518 * Prevent lookups now that we are past the point of no return.
1519 */
1520 dqp->dq_flags |= XFS_DQ_FREEING;
1521 xfs_dqunlock(dqp);
1522
1523 ASSERT(dqp->q_nrefs == 0);
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001524 list_move_tail(&dqp->q_lru, dispose_list);
1525 qi->qi_lru_count--;
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001526 XFS_STATS_DEC(xs_qm_dquot_unused);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001527
1528 trace_xfs_dqreclaim_done(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001529 XFS_STATS_INC(xs_qm_dqreclaims);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001530 return;
1531
1532out_busy:
1533 xfs_dqunlock(dqp);
1534
1535 /*
1536 * Move the dquot to the tail of the list so that we don't spin on it.
1537 */
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001538 list_move_tail(&dqp->q_lru, &qi->qi_lru_list);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001539
1540 trace_xfs_dqreclaim_busy(dqp);
Christoph Hellwig48776fd2012-03-13 08:52:33 +00001541 XFS_STATS_INC(xs_qm_dqreclaim_misses);
Dave Chinner368e1362010-04-13 15:06:50 +10001542}
1543
Dave Chinner368e1362010-04-13 15:06:50 +10001544STATIC int
Dave Chinner7f8275d2010-07-19 14:56:17 +10001545xfs_qm_shake(
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001546 struct shrinker *shrink,
1547 struct shrink_control *sc)
Dave Chinner368e1362010-04-13 15:06:50 +10001548{
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001549 struct xfs_quotainfo *qi =
1550 container_of(shrink, struct xfs_quotainfo, qi_shrinker);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001551 int nr_to_scan = sc->nr_to_scan;
1552 LIST_HEAD (dispose_list);
1553 struct xfs_dquot *dqp;
Dave Chinner368e1362010-04-13 15:06:50 +10001554
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001555 if ((sc->gfp_mask & (__GFP_FS|__GFP_WAIT)) != (__GFP_FS|__GFP_WAIT))
Dave Chinner368e1362010-04-13 15:06:50 +10001556 return 0;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001557 if (!nr_to_scan)
1558 goto out;
Dave Chinner368e1362010-04-13 15:06:50 +10001559
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001560 mutex_lock(&qi->qi_lru_lock);
1561 while (!list_empty(&qi->qi_lru_list)) {
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001562 if (nr_to_scan-- <= 0)
1563 break;
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001564 dqp = list_first_entry(&qi->qi_lru_list, struct xfs_dquot,
1565 q_lru);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001566 xfs_qm_dqreclaim_one(dqp, &dispose_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001568 mutex_unlock(&qi->qi_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001570 while (!list_empty(&dispose_list)) {
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001571 dqp = list_first_entry(&dispose_list, struct xfs_dquot, q_lru);
1572 list_del_init(&dqp->q_lru);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001573 xfs_qm_dqfree_one(dqp);
1574 }
1575out:
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001576 return (qi->qi_lru_count / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579/*
1580 * Start a transaction and write the incore superblock changes to
1581 * disk. flags parameter indicates which fields have changed.
1582 */
1583int
1584xfs_qm_write_sb_changes(
1585 xfs_mount_t *mp,
1586 __int64_t flags)
1587{
1588 xfs_trans_t *tp;
1589 int error;
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
1592 if ((error = xfs_trans_reserve(tp, 0,
1593 mp->m_sb.sb_sectsize + 128, 0,
1594 0,
1595 XFS_DEFAULT_LOG_COUNT))) {
1596 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001597 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599
1600 xfs_mod_sb(tp, flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001601 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
David Chinnere5720ee2008-04-10 12:21:18 +10001603 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606
1607/* --------------- utility functions for vnodeops ---------------- */
1608
1609
1610/*
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001611 * Given an inode, a uid, gid and prid make sure that we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * allocated relevant dquot(s) on disk, and that we won't exceed inode
1613 * quotas by creating this file.
1614 * This also attaches dquot(s) to the given inode after locking it,
1615 * and returns the dquots corresponding to the uid and/or gid.
1616 *
1617 * in : inode (unlocked)
1618 * out : udquot, gdquot with references taken and unlocked
1619 */
1620int
1621xfs_qm_vop_dqalloc(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001622 struct xfs_inode *ip,
1623 uid_t uid,
1624 gid_t gid,
1625 prid_t prid,
1626 uint flags,
1627 struct xfs_dquot **O_udqpp,
1628 struct xfs_dquot **O_gdqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001630 struct xfs_mount *mp = ip->i_mount;
1631 struct xfs_dquot *uq, *gq;
1632 int error;
1633 uint lockflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Christoph Hellwig7d095252009-06-08 15:33:32 +02001635 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return 0;
1637
1638 lockflags = XFS_ILOCK_EXCL;
1639 xfs_ilock(ip, lockflags);
1640
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001641 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 gid = ip->i_d.di_gid;
1643
1644 /*
1645 * Attach the dquot(s) to this inode, doing a dquot allocation
1646 * if necessary. The dquot(s) will not be locked.
1647 */
1648 if (XFS_NOT_DQATTACHED(mp, ip)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001649 error = xfs_qm_dqattach_locked(ip, XFS_QMOPT_DQALLOC);
1650 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01001652 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654 }
1655
1656 uq = gq = NULL;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001657 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (ip->i_d.di_uid != uid) {
1659 /*
1660 * What we need is the dquot that has this uid, and
1661 * if we send the inode to dqget, the uid of the inode
1662 * takes priority over what's sent in the uid argument.
1663 * We must unlock inode here before calling dqget if
1664 * we're not sending the inode, because otherwise
1665 * we'll deadlock by doing trans_reserve while
1666 * holding ilock.
1667 */
1668 xfs_iunlock(ip, lockflags);
1669 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
1670 XFS_DQ_USER,
1671 XFS_QMOPT_DQALLOC |
1672 XFS_QMOPT_DOWARN,
1673 &uq))) {
1674 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001675 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677 /*
1678 * Get the ilock in the right order.
1679 */
1680 xfs_dqunlock(uq);
1681 lockflags = XFS_ILOCK_SHARED;
1682 xfs_ilock(ip, lockflags);
1683 } else {
1684 /*
1685 * Take an extra reference, because we'll return
1686 * this to caller
1687 */
1688 ASSERT(ip->i_udquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001689 uq = xfs_qm_dqhold(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001692 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (ip->i_d.di_gid != gid) {
1694 xfs_iunlock(ip, lockflags);
1695 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
1696 XFS_DQ_GROUP,
1697 XFS_QMOPT_DQALLOC |
1698 XFS_QMOPT_DOWARN,
1699 &gq))) {
1700 if (uq)
1701 xfs_qm_dqrele(uq);
1702 ASSERT(error != ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001703 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 xfs_dqunlock(gq);
1706 lockflags = XFS_ILOCK_SHARED;
1707 xfs_ilock(ip, lockflags);
1708 } else {
1709 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001710 gq = xfs_qm_dqhold(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001712 } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001713 if (xfs_get_projid(ip) != prid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001714 xfs_iunlock(ip, lockflags);
1715 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
1716 XFS_DQ_PROJ,
1717 XFS_QMOPT_DQALLOC |
1718 XFS_QMOPT_DOWARN,
1719 &gq))) {
1720 if (uq)
1721 xfs_qm_dqrele(uq);
1722 ASSERT(error != ENOENT);
1723 return (error);
1724 }
1725 xfs_dqunlock(gq);
1726 lockflags = XFS_ILOCK_SHARED;
1727 xfs_ilock(ip, lockflags);
1728 } else {
1729 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001730 gq = xfs_qm_dqhold(ip->i_gdquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733 if (uq)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001734 trace_xfs_dquot_dqalloc(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 xfs_iunlock(ip, lockflags);
1737 if (O_udqpp)
1738 *O_udqpp = uq;
1739 else if (uq)
1740 xfs_qm_dqrele(uq);
1741 if (O_gdqpp)
1742 *O_gdqpp = gq;
1743 else if (gq)
1744 xfs_qm_dqrele(gq);
Jesper Juhl014c2542006-01-15 02:37:08 +01001745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
1748/*
1749 * Actually transfer ownership, and do dquot modifications.
1750 * These were already reserved.
1751 */
1752xfs_dquot_t *
1753xfs_qm_vop_chown(
1754 xfs_trans_t *tp,
1755 xfs_inode_t *ip,
1756 xfs_dquot_t **IO_olddq,
1757 xfs_dquot_t *newdq)
1758{
1759 xfs_dquot_t *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001760 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
1761 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
1762
Christoph Hellwig7d095252009-06-08 15:33:32 +02001763
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001764 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
1766
1767 /* old dquot */
1768 prevdq = *IO_olddq;
1769 ASSERT(prevdq);
1770 ASSERT(prevdq != newdq);
1771
Nathan Scott06d10dd2005-06-21 15:48:47 +10001772 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
1773 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* the sparkling new dquot */
Nathan Scott06d10dd2005-06-21 15:48:47 +10001776 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
1777 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 /*
Christoph Hellwig78e55892011-12-06 21:58:22 +00001780 * Take an extra reference, because the inode is going to keep
1781 * this dquot pointer even after the trans_commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Christoph Hellwig78e55892011-12-06 21:58:22 +00001783 *IO_olddq = xfs_qm_dqhold(newdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jesper Juhl014c2542006-01-15 02:37:08 +01001785 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
1788/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001789 * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
1791int
1792xfs_qm_vop_chown_reserve(
1793 xfs_trans_t *tp,
1794 xfs_inode_t *ip,
1795 xfs_dquot_t *udqp,
1796 xfs_dquot_t *gdqp,
1797 uint flags)
1798{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001799 xfs_mount_t *mp = ip->i_mount;
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001800 uint delblks, blkflags, prjflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001802 int error;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001805 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1807
1808 delblks = ip->i_delayed_blks;
1809 delblksudq = delblksgdq = unresudq = unresgdq = NULL;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001810 blkflags = XFS_IS_REALTIME_INODE(ip) ?
1811 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 if (XFS_IS_UQUOTA_ON(mp) && udqp &&
Christoph Hellwig1149d962005-11-02 15:01:12 +11001814 ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 delblksudq = udqp;
1816 /*
1817 * If there are delayed allocation blocks, then we have to
1818 * unreserve those from the old dquot, and add them to the
1819 * new dquot.
1820 */
1821 if (delblks) {
1822 ASSERT(ip->i_udquot);
1823 unresudq = ip->i_udquot;
1824 }
1825 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001826 if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001827 if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001828 xfs_get_projid(ip) != be32_to_cpu(gdqp->q_core.d_id))
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001829 prjflags = XFS_QMOPT_ENOSPC;
1830
1831 if (prjflags ||
1832 (XFS_IS_GQUOTA_ON(ip->i_mount) &&
1833 ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001834 delblksgdq = gdqp;
1835 if (delblks) {
1836 ASSERT(ip->i_gdquot);
1837 unresgdq = ip->i_gdquot;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840 }
1841
1842 if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
1843 delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001844 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return (error);
1846
1847 /*
1848 * Do the delayed blks reservations/unreservations now. Since, these
1849 * are done without the help of a transaction, if a reservation fails
1850 * its previous reservations won't be automatically undone by trans
1851 * code. So, we have to do it manually here.
1852 */
1853 if (delblks) {
1854 /*
1855 * Do the reservations first. Unreservation can't fail.
1856 */
1857 ASSERT(delblksudq || delblksgdq);
1858 ASSERT(unresudq || unresgdq);
1859 if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
1860 delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
Nathan Scott9a2a7de2006-03-31 13:04:49 +10001861 flags | blkflags | prjflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return (error);
1863 xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
1864 unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10001865 blkflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
1868 return (0);
1869}
1870
1871int
1872xfs_qm_vop_rename_dqattach(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001873 struct xfs_inode **i_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001875 struct xfs_mount *mp = i_tab[0]->i_mount;
1876 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Christoph Hellwig7d095252009-06-08 15:33:32 +02001878 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +01001879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Christoph Hellwig7d095252009-06-08 15:33:32 +02001881 for (i = 0; (i < 4 && i_tab[i]); i++) {
1882 struct xfs_inode *ip = i_tab[i];
1883 int error;
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
1886 * Watch out for duplicate entries in the table.
1887 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001888 if (i == 0 || ip != i_tab[i-1]) {
1889 if (XFS_NOT_DQATTACHED(mp, ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 error = xfs_qm_dqattach(ip, 0);
1891 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001892 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 }
1895 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
1899void
Christoph Hellwig7d095252009-06-08 15:33:32 +02001900xfs_qm_vop_create_dqattach(
1901 struct xfs_trans *tp,
1902 struct xfs_inode *ip,
1903 struct xfs_dquot *udqp,
1904 struct xfs_dquot *gdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001906 struct xfs_mount *mp = tp->t_mountp;
1907
1908 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return;
1910
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001911 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig7d095252009-06-08 15:33:32 +02001912 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 if (udqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ASSERT(ip->i_udquot == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001916 ASSERT(XFS_IS_UQUOTA_ON(mp));
Christoph Hellwig1149d962005-11-02 15:01:12 +11001917 ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
Christoph Hellwig78e55892011-12-06 21:58:22 +00001918
1919 ip->i_udquot = xfs_qm_dqhold(udqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
1921 }
1922 if (gdqp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 ASSERT(ip->i_gdquot == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001924 ASSERT(XFS_IS_OQUOTA_ON(mp));
1925 ASSERT((XFS_IS_GQUOTA_ON(mp) ?
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001926 ip->i_d.di_gid : xfs_get_projid(ip)) ==
Nathan Scottee2a4f72006-01-11 15:33:36 +11001927 be32_to_cpu(gdqp->q_core.d_id));
Christoph Hellwig78e55892011-12-06 21:58:22 +00001928
1929 ip->i_gdquot = xfs_qm_dqhold(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
1931 }
1932}
1933