]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
4ce3121f NS |
2 | * Copyright (c) 2000-2003 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
4ce3121f NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
4ce3121f NS |
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. | |
1da177e4 | 13 | * |
4ce3121f NS |
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 | |
1da177e4 | 17 | */ |
1da177e4 LT |
18 | #include "xfs.h" |
19 | #include "xfs_fs.h" | |
a844f451 | 20 | #include "xfs_bit.h" |
1da177e4 | 21 | #include "xfs_log.h" |
a844f451 | 22 | #include "xfs_inum.h" |
1da177e4 LT |
23 | #include "xfs_trans.h" |
24 | #include "xfs_sb.h" | |
25 | #include "xfs_ag.h" | |
1da177e4 | 26 | #include "xfs_alloc.h" |
1da177e4 LT |
27 | #include "xfs_quota.h" |
28 | #include "xfs_mount.h" | |
1da177e4 | 29 | #include "xfs_bmap_btree.h" |
1da177e4 LT |
30 | #include "xfs_inode.h" |
31 | #include "xfs_bmap.h" | |
1da177e4 LT |
32 | #include "xfs_rtalloc.h" |
33 | #include "xfs_error.h" | |
34 | #include "xfs_itable.h" | |
1da177e4 LT |
35 | #include "xfs_attr.h" |
36 | #include "xfs_buf_item.h" | |
37 | #include "xfs_trans_space.h" | |
38 | #include "xfs_trans_priv.h" | |
1da177e4 | 39 | #include "xfs_qm.h" |
0b1b213f | 40 | #include "xfs_trace.h" |
1da177e4 | 41 | |
1da177e4 | 42 | /* |
bf72de31 CH |
43 | * Lock order: |
44 | * | |
45 | * ip->i_lock | |
46 | * qh->qh_lock | |
47 | * qi->qi_dqlist_lock | |
48 | * dquot->q_qlock (xfs_dqlock() and friends) | |
49 | * dquot->q_flush (xfs_dqflock() and friends) | |
50 | * xfs_Gqm->qm_dqfrlist_lock | |
51 | * | |
52 | * If two dquots need to be locked the order is user before group/project, | |
53 | * otherwise by the lowest id first, see xfs_dqlock2. | |
54 | */ | |
1da177e4 | 55 | |
1da177e4 LT |
56 | #ifdef DEBUG |
57 | xfs_buftarg_t *xfs_dqerror_target; | |
58 | int xfs_do_dqerror; | |
59 | int xfs_dqreq_num; | |
60 | int xfs_dqerror_mod = 33; | |
61 | #endif | |
62 | ||
98b8c7a0 CH |
63 | static struct lock_class_key xfs_dquot_other_class; |
64 | ||
1da177e4 LT |
65 | /* |
66 | * Allocate and initialize a dquot. We don't always allocate fresh memory; | |
67 | * we try to reclaim a free dquot if the number of incore dquots are above | |
68 | * a threshold. | |
69 | * The only field inside the core that gets initialized at this point | |
70 | * is the d_id field. The idea is to fill in the entire q_core | |
71 | * when we read in the on disk dquot. | |
72 | */ | |
ba0f32d4 | 73 | STATIC xfs_dquot_t * |
1da177e4 LT |
74 | xfs_qm_dqinit( |
75 | xfs_mount_t *mp, | |
76 | xfs_dqid_t id, | |
77 | uint type) | |
78 | { | |
79 | xfs_dquot_t *dqp; | |
80 | boolean_t brandnewdquot; | |
81 | ||
82 | brandnewdquot = xfs_qm_dqalloc_incore(&dqp); | |
83 | dqp->dq_flags = type; | |
1149d96a | 84 | dqp->q_core.d_id = cpu_to_be32(id); |
1da177e4 LT |
85 | dqp->q_mount = mp; |
86 | ||
87 | /* | |
88 | * No need to re-initialize these if this is a reclaimed dquot. | |
89 | */ | |
90 | if (brandnewdquot) { | |
3a8406f6 | 91 | INIT_LIST_HEAD(&dqp->q_freelist); |
794ee1ba | 92 | mutex_init(&dqp->q_qlock); |
bc3048e3 | 93 | init_waitqueue_head(&dqp->q_pinwait); |
1da177e4 | 94 | |
e1f49cf2 DC |
95 | /* |
96 | * Because we want to use a counting completion, complete | |
97 | * the flush completion once to allow a single access to | |
98 | * the flush completion without blocking. | |
99 | */ | |
100 | init_completion(&dqp->q_flush); | |
101 | complete(&dqp->q_flush); | |
102 | ||
0b1b213f | 103 | trace_xfs_dqinit(dqp); |
1da177e4 LT |
104 | } else { |
105 | /* | |
106 | * Only the q_core portion was zeroed in dqreclaim_one(). | |
107 | * So, we need to reset others. | |
108 | */ | |
3a8406f6 DC |
109 | dqp->q_nrefs = 0; |
110 | dqp->q_blkno = 0; | |
111 | INIT_LIST_HEAD(&dqp->q_mplist); | |
112 | INIT_LIST_HEAD(&dqp->q_hashlist); | |
113 | dqp->q_bufoffset = 0; | |
114 | dqp->q_fileoffset = 0; | |
115 | dqp->q_transp = NULL; | |
116 | dqp->q_gdquot = NULL; | |
117 | dqp->q_res_bcount = 0; | |
118 | dqp->q_res_icount = 0; | |
119 | dqp->q_res_rtbcount = 0; | |
120 | atomic_set(&dqp->q_pincount, 0); | |
121 | dqp->q_hash = NULL; | |
122 | ASSERT(list_empty(&dqp->q_freelist)); | |
1da177e4 | 123 | |
0b1b213f | 124 | trace_xfs_dqreuse(dqp); |
98b8c7a0 CH |
125 | } |
126 | ||
127 | /* | |
128 | * In either case we need to make sure group quotas have a different | |
129 | * lock class than user quotas, to make sure lockdep knows we can | |
130 | * locks of one of each at the same time. | |
131 | */ | |
132 | if (!(type & XFS_DQ_USER)) | |
133 | lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class); | |
1da177e4 LT |
134 | |
135 | /* | |
136 | * log item gets initialized later | |
137 | */ | |
138 | return (dqp); | |
139 | } | |
140 | ||
141 | /* | |
142 | * This is called to free all the memory associated with a dquot | |
143 | */ | |
144 | void | |
145 | xfs_qm_dqdestroy( | |
146 | xfs_dquot_t *dqp) | |
147 | { | |
3a8406f6 | 148 | ASSERT(list_empty(&dqp->q_freelist)); |
1da177e4 LT |
149 | |
150 | mutex_destroy(&dqp->q_qlock); | |
1da177e4 | 151 | kmem_zone_free(xfs_Gqm->qm_dqzone, dqp); |
0b1b213f | 152 | |
1da177e4 LT |
153 | atomic_dec(&xfs_Gqm->qm_totaldquots); |
154 | } | |
155 | ||
1da177e4 LT |
156 | /* |
157 | * If default limits are in force, push them into the dquot now. | |
158 | * We overwrite the dquot limits only if they are zero and this | |
159 | * is not the root dquot. | |
160 | */ | |
161 | void | |
162 | xfs_qm_adjust_dqlimits( | |
163 | xfs_mount_t *mp, | |
164 | xfs_disk_dquot_t *d) | |
165 | { | |
166 | xfs_quotainfo_t *q = mp->m_quotainfo; | |
167 | ||
168 | ASSERT(d->d_id); | |
169 | ||
170 | if (q->qi_bsoftlimit && !d->d_blk_softlimit) | |
1149d96a | 171 | d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit); |
1da177e4 | 172 | if (q->qi_bhardlimit && !d->d_blk_hardlimit) |
1149d96a | 173 | d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit); |
1da177e4 | 174 | if (q->qi_isoftlimit && !d->d_ino_softlimit) |
1149d96a | 175 | d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit); |
1da177e4 | 176 | if (q->qi_ihardlimit && !d->d_ino_hardlimit) |
1149d96a | 177 | d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit); |
1da177e4 | 178 | if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit) |
1149d96a | 179 | d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit); |
1da177e4 | 180 | if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit) |
1149d96a | 181 | d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit); |
1da177e4 LT |
182 | } |
183 | ||
184 | /* | |
185 | * Check the limits and timers of a dquot and start or reset timers | |
186 | * if necessary. | |
187 | * This gets called even when quota enforcement is OFF, which makes our | |
188 | * life a little less complicated. (We just don't reject any quota | |
189 | * reservations in that case, when enforcement is off). | |
190 | * We also return 0 as the values of the timers in Q_GETQUOTA calls, when | |
191 | * enforcement's off. | |
192 | * In contrast, warnings are a little different in that they don't | |
754002b4 NS |
193 | * 'automatically' get started when limits get exceeded. They do |
194 | * get reset to zero, however, when we find the count to be under | |
195 | * the soft limit (they are only ever set non-zero via userspace). | |
1da177e4 LT |
196 | */ |
197 | void | |
198 | xfs_qm_adjust_dqtimers( | |
199 | xfs_mount_t *mp, | |
200 | xfs_disk_dquot_t *d) | |
201 | { | |
202 | ASSERT(d->d_id); | |
203 | ||
ea15ab3c | 204 | #ifdef DEBUG |
1149d96a CH |
205 | if (d->d_blk_hardlimit) |
206 | ASSERT(be64_to_cpu(d->d_blk_softlimit) <= | |
207 | be64_to_cpu(d->d_blk_hardlimit)); | |
208 | if (d->d_ino_hardlimit) | |
209 | ASSERT(be64_to_cpu(d->d_ino_softlimit) <= | |
210 | be64_to_cpu(d->d_ino_hardlimit)); | |
211 | if (d->d_rtb_hardlimit) | |
212 | ASSERT(be64_to_cpu(d->d_rtb_softlimit) <= | |
213 | be64_to_cpu(d->d_rtb_hardlimit)); | |
1da177e4 | 214 | #endif |
ea15ab3c | 215 | |
1da177e4 | 216 | if (!d->d_btimer) { |
1149d96a CH |
217 | if ((d->d_blk_softlimit && |
218 | (be64_to_cpu(d->d_bcount) >= | |
219 | be64_to_cpu(d->d_blk_softlimit))) || | |
220 | (d->d_blk_hardlimit && | |
221 | (be64_to_cpu(d->d_bcount) >= | |
222 | be64_to_cpu(d->d_blk_hardlimit)))) { | |
223 | d->d_btimer = cpu_to_be32(get_seconds() + | |
8a7b8a89 | 224 | mp->m_quotainfo->qi_btimelimit); |
754002b4 NS |
225 | } else { |
226 | d->d_bwarns = 0; | |
1da177e4 LT |
227 | } |
228 | } else { | |
229 | if ((!d->d_blk_softlimit || | |
1149d96a CH |
230 | (be64_to_cpu(d->d_bcount) < |
231 | be64_to_cpu(d->d_blk_softlimit))) && | |
1da177e4 | 232 | (!d->d_blk_hardlimit || |
1149d96a CH |
233 | (be64_to_cpu(d->d_bcount) < |
234 | be64_to_cpu(d->d_blk_hardlimit)))) { | |
1da177e4 LT |
235 | d->d_btimer = 0; |
236 | } | |
237 | } | |
238 | ||
239 | if (!d->d_itimer) { | |
1149d96a CH |
240 | if ((d->d_ino_softlimit && |
241 | (be64_to_cpu(d->d_icount) >= | |
242 | be64_to_cpu(d->d_ino_softlimit))) || | |
243 | (d->d_ino_hardlimit && | |
244 | (be64_to_cpu(d->d_icount) >= | |
245 | be64_to_cpu(d->d_ino_hardlimit)))) { | |
246 | d->d_itimer = cpu_to_be32(get_seconds() + | |
8a7b8a89 | 247 | mp->m_quotainfo->qi_itimelimit); |
754002b4 NS |
248 | } else { |
249 | d->d_iwarns = 0; | |
1da177e4 LT |
250 | } |
251 | } else { | |
252 | if ((!d->d_ino_softlimit || | |
1149d96a CH |
253 | (be64_to_cpu(d->d_icount) < |
254 | be64_to_cpu(d->d_ino_softlimit))) && | |
1da177e4 | 255 | (!d->d_ino_hardlimit || |
1149d96a CH |
256 | (be64_to_cpu(d->d_icount) < |
257 | be64_to_cpu(d->d_ino_hardlimit)))) { | |
1da177e4 LT |
258 | d->d_itimer = 0; |
259 | } | |
260 | } | |
261 | ||
262 | if (!d->d_rtbtimer) { | |
1149d96a CH |
263 | if ((d->d_rtb_softlimit && |
264 | (be64_to_cpu(d->d_rtbcount) >= | |
265 | be64_to_cpu(d->d_rtb_softlimit))) || | |
266 | (d->d_rtb_hardlimit && | |
267 | (be64_to_cpu(d->d_rtbcount) >= | |
268 | be64_to_cpu(d->d_rtb_hardlimit)))) { | |
269 | d->d_rtbtimer = cpu_to_be32(get_seconds() + | |
8a7b8a89 | 270 | mp->m_quotainfo->qi_rtbtimelimit); |
754002b4 NS |
271 | } else { |
272 | d->d_rtbwarns = 0; | |
1da177e4 LT |
273 | } |
274 | } else { | |
275 | if ((!d->d_rtb_softlimit || | |
1149d96a CH |
276 | (be64_to_cpu(d->d_rtbcount) < |
277 | be64_to_cpu(d->d_rtb_softlimit))) && | |
1da177e4 | 278 | (!d->d_rtb_hardlimit || |
1149d96a CH |
279 | (be64_to_cpu(d->d_rtbcount) < |
280 | be64_to_cpu(d->d_rtb_hardlimit)))) { | |
1da177e4 LT |
281 | d->d_rtbtimer = 0; |
282 | } | |
283 | } | |
284 | } | |
285 | ||
1da177e4 LT |
286 | /* |
287 | * initialize a buffer full of dquots and log the whole thing | |
288 | */ | |
289 | STATIC void | |
290 | xfs_qm_init_dquot_blk( | |
291 | xfs_trans_t *tp, | |
292 | xfs_mount_t *mp, | |
293 | xfs_dqid_t id, | |
294 | uint type, | |
295 | xfs_buf_t *bp) | |
296 | { | |
8a7b8a89 | 297 | struct xfs_quotainfo *q = mp->m_quotainfo; |
1da177e4 LT |
298 | xfs_dqblk_t *d; |
299 | int curid, i; | |
300 | ||
301 | ASSERT(tp); | |
0c842ad4 | 302 | ASSERT(xfs_buf_islocked(bp)); |
1da177e4 | 303 | |
62926044 | 304 | d = bp->b_addr; |
1da177e4 LT |
305 | |
306 | /* | |
307 | * ID of the first dquot in the block - id's are zero based. | |
308 | */ | |
8a7b8a89 | 309 | curid = id - (id % q->qi_dqperchunk); |
1da177e4 | 310 | ASSERT(curid >= 0); |
8a7b8a89 | 311 | memset(d, 0, BBTOB(q->qi_dqchunklen)); |
49d35a5c CH |
312 | for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) { |
313 | d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC); | |
314 | d->dd_diskdq.d_version = XFS_DQUOT_VERSION; | |
315 | d->dd_diskdq.d_id = cpu_to_be32(curid); | |
316 | d->dd_diskdq.d_flags = type; | |
317 | } | |
318 | ||
1da177e4 | 319 | xfs_trans_dquot_buf(tp, bp, |
c1155410 DC |
320 | (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF : |
321 | ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF : | |
322 | XFS_BLF_GDQUOT_BUF))); | |
8a7b8a89 | 323 | xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1); |
1da177e4 LT |
324 | } |
325 | ||
326 | ||
327 | ||
328 | /* | |
329 | * Allocate a block and fill it with dquots. | |
330 | * This is called when the bmapi finds a hole. | |
331 | */ | |
332 | STATIC int | |
333 | xfs_qm_dqalloc( | |
efa092f3 | 334 | xfs_trans_t **tpp, |
1da177e4 LT |
335 | xfs_mount_t *mp, |
336 | xfs_dquot_t *dqp, | |
337 | xfs_inode_t *quotip, | |
338 | xfs_fileoff_t offset_fsb, | |
339 | xfs_buf_t **O_bpp) | |
340 | { | |
341 | xfs_fsblock_t firstblock; | |
342 | xfs_bmap_free_t flist; | |
343 | xfs_bmbt_irec_t map; | |
344 | int nmaps, error, committed; | |
345 | xfs_buf_t *bp; | |
efa092f3 | 346 | xfs_trans_t *tp = *tpp; |
1da177e4 LT |
347 | |
348 | ASSERT(tp != NULL); | |
0b1b213f CH |
349 | |
350 | trace_xfs_dqalloc(dqp); | |
1da177e4 LT |
351 | |
352 | /* | |
353 | * Initialize the bmap freelist prior to calling bmapi code. | |
354 | */ | |
9d87c319 | 355 | xfs_bmap_init(&flist, &firstblock); |
1da177e4 LT |
356 | xfs_ilock(quotip, XFS_ILOCK_EXCL); |
357 | /* | |
358 | * Return if this type of quotas is turned off while we didn't | |
359 | * have an inode lock | |
360 | */ | |
6967b964 | 361 | if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) { |
1da177e4 LT |
362 | xfs_iunlock(quotip, XFS_ILOCK_EXCL); |
363 | return (ESRCH); | |
364 | } | |
365 | ||
ddc3415a | 366 | xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL); |
1da177e4 | 367 | nmaps = 1; |
c0dc7828 DC |
368 | error = xfs_bmapi_write(tp, quotip, offset_fsb, |
369 | XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, | |
370 | &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp), | |
371 | &map, &nmaps, &flist); | |
372 | if (error) | |
1da177e4 | 373 | goto error0; |
1da177e4 LT |
374 | ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB); |
375 | ASSERT(nmaps == 1); | |
376 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && | |
377 | (map.br_startblock != HOLESTARTBLOCK)); | |
378 | ||
379 | /* | |
380 | * Keep track of the blkno to save a lookup later | |
381 | */ | |
382 | dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock); | |
383 | ||
384 | /* now we can just get the buffer (there's nothing to read yet) */ | |
385 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, | |
386 | dqp->q_blkno, | |
8a7b8a89 | 387 | mp->m_quotainfo->qi_dqchunklen, |
1da177e4 | 388 | 0); |
2a30f36d CS |
389 | |
390 | error = xfs_buf_geterror(bp); | |
391 | if (error) | |
1da177e4 | 392 | goto error1; |
2a30f36d | 393 | |
1da177e4 LT |
394 | /* |
395 | * Make a chunk of dquots out of this buffer and log | |
396 | * the entire thing. | |
397 | */ | |
1149d96a | 398 | xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id), |
c8ad20ff | 399 | dqp->dq_flags & XFS_DQ_ALLTYPES, bp); |
1da177e4 | 400 | |
efa092f3 TS |
401 | /* |
402 | * xfs_bmap_finish() may commit the current transaction and | |
403 | * start a second transaction if the freelist is not empty. | |
404 | * | |
405 | * Since we still want to modify this buffer, we need to | |
406 | * ensure that the buffer is not released on commit of | |
407 | * the first transaction and ensure the buffer is added to the | |
408 | * second transaction. | |
409 | * | |
410 | * If there is only one transaction then don't stop the buffer | |
411 | * from being released when it commits later on. | |
412 | */ | |
413 | ||
414 | xfs_trans_bhold(tp, bp); | |
415 | ||
f7c99b6f | 416 | if ((error = xfs_bmap_finish(tpp, &flist, &committed))) { |
1da177e4 LT |
417 | goto error1; |
418 | } | |
419 | ||
efa092f3 TS |
420 | if (committed) { |
421 | tp = *tpp; | |
422 | xfs_trans_bjoin(tp, bp); | |
423 | } else { | |
424 | xfs_trans_bhold_release(tp, bp); | |
425 | } | |
426 | ||
1da177e4 LT |
427 | *O_bpp = bp; |
428 | return 0; | |
429 | ||
430 | error1: | |
431 | xfs_bmap_cancel(&flist); | |
432 | error0: | |
433 | xfs_iunlock(quotip, XFS_ILOCK_EXCL); | |
434 | ||
435 | return (error); | |
436 | } | |
437 | ||
438 | /* | |
439 | * Maps a dquot to the buffer containing its on-disk version. | |
440 | * This returns a ptr to the buffer containing the on-disk dquot | |
441 | * in the bpp param, and a ptr to the on-disk dquot within that buffer | |
442 | */ | |
443 | STATIC int | |
444 | xfs_qm_dqtobp( | |
efa092f3 | 445 | xfs_trans_t **tpp, |
1da177e4 LT |
446 | xfs_dquot_t *dqp, |
447 | xfs_disk_dquot_t **O_ddpp, | |
448 | xfs_buf_t **O_bpp, | |
449 | uint flags) | |
450 | { | |
451 | xfs_bmbt_irec_t map; | |
acecf1b5 | 452 | int nmaps = 1, error; |
1da177e4 | 453 | xfs_buf_t *bp; |
acecf1b5 CH |
454 | xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp); |
455 | xfs_mount_t *mp = dqp->q_mount; | |
1da177e4 | 456 | xfs_disk_dquot_t *ddq; |
acecf1b5 | 457 | xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id); |
efa092f3 | 458 | xfs_trans_t *tp = (tpp ? *tpp : NULL); |
1da177e4 | 459 | |
acecf1b5 | 460 | dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk; |
1da177e4 | 461 | |
acecf1b5 | 462 | xfs_ilock(quotip, XFS_ILOCK_SHARED); |
6967b964 | 463 | if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) { |
1da177e4 | 464 | /* |
acecf1b5 CH |
465 | * Return if this type of quotas is turned off while we |
466 | * didn't have the quota inode lock. | |
1da177e4 | 467 | */ |
acecf1b5 CH |
468 | xfs_iunlock(quotip, XFS_ILOCK_SHARED); |
469 | return ESRCH; | |
470 | } | |
471 | ||
472 | /* | |
473 | * Find the block map; no allocations yet | |
474 | */ | |
5c8ed202 DC |
475 | error = xfs_bmapi_read(quotip, dqp->q_fileoffset, |
476 | XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0); | |
acecf1b5 CH |
477 | |
478 | xfs_iunlock(quotip, XFS_ILOCK_SHARED); | |
479 | if (error) | |
480 | return error; | |
481 | ||
482 | ASSERT(nmaps == 1); | |
483 | ASSERT(map.br_blockcount == 1); | |
484 | ||
485 | /* | |
486 | * Offset of dquot in the (fixed sized) dquot chunk. | |
487 | */ | |
488 | dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) * | |
489 | sizeof(xfs_dqblk_t); | |
490 | ||
491 | ASSERT(map.br_startblock != DELAYSTARTBLOCK); | |
492 | if (map.br_startblock == HOLESTARTBLOCK) { | |
1da177e4 | 493 | /* |
acecf1b5 | 494 | * We don't allocate unless we're asked to |
1da177e4 | 495 | */ |
acecf1b5 CH |
496 | if (!(flags & XFS_QMOPT_DQALLOC)) |
497 | return ENOENT; | |
1da177e4 | 498 | |
acecf1b5 CH |
499 | ASSERT(tp); |
500 | error = xfs_qm_dqalloc(tpp, mp, dqp, quotip, | |
501 | dqp->q_fileoffset, &bp); | |
1da177e4 | 502 | if (error) |
acecf1b5 CH |
503 | return error; |
504 | tp = *tpp; | |
505 | } else { | |
506 | trace_xfs_dqtobp_read(dqp); | |
1da177e4 LT |
507 | |
508 | /* | |
acecf1b5 CH |
509 | * store the blkno etc so that we don't have to do the |
510 | * mapping all the time | |
1da177e4 | 511 | */ |
acecf1b5 | 512 | dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock); |
0b1b213f | 513 | |
8a7b8a89 CH |
514 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
515 | dqp->q_blkno, | |
516 | mp->m_quotainfo->qi_dqchunklen, | |
517 | 0, &bp); | |
1da177e4 LT |
518 | if (error || !bp) |
519 | return XFS_ERROR(error); | |
520 | } | |
acecf1b5 | 521 | |
0c842ad4 | 522 | ASSERT(xfs_buf_islocked(bp)); |
1da177e4 LT |
523 | |
524 | /* | |
525 | * calculate the location of the dquot inside the buffer. | |
526 | */ | |
62926044 | 527 | ddq = bp->b_addr + dqp->q_bufoffset; |
1da177e4 LT |
528 | |
529 | /* | |
530 | * A simple sanity check in case we got a corrupted dquot... | |
531 | */ | |
a0fa2b67 | 532 | error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES, |
1da177e4 | 533 | flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN), |
a0fa2b67 DC |
534 | "dqtobp"); |
535 | if (error) { | |
1da177e4 LT |
536 | if (!(flags & XFS_QMOPT_DQREPAIR)) { |
537 | xfs_trans_brelse(tp, bp); | |
538 | return XFS_ERROR(EIO); | |
539 | } | |
1da177e4 LT |
540 | } |
541 | ||
542 | *O_bpp = bp; | |
543 | *O_ddpp = ddq; | |
544 | ||
545 | return (0); | |
546 | } | |
547 | ||
548 | ||
549 | /* | |
550 | * Read in the ondisk dquot using dqtobp() then copy it to an incore version, | |
551 | * and release the buffer immediately. | |
552 | * | |
97e7ade5 | 553 | * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed. |
1da177e4 | 554 | */ |
7ae44407 | 555 | int |
1da177e4 | 556 | xfs_qm_dqread( |
97e7ade5 CH |
557 | struct xfs_mount *mp, |
558 | xfs_dqid_t id, | |
559 | uint type, | |
560 | uint flags, | |
561 | struct xfs_dquot **O_dqpp) | |
1da177e4 | 562 | { |
97e7ade5 CH |
563 | struct xfs_dquot *dqp; |
564 | struct xfs_disk_dquot *ddqp; | |
565 | struct xfs_buf *bp; | |
566 | struct xfs_trans *tp = NULL; | |
567 | int error; | |
568 | int cancelflags = 0; | |
efa092f3 | 569 | |
97e7ade5 | 570 | dqp = xfs_qm_dqinit(mp, id, type); |
1da177e4 | 571 | |
0b1b213f CH |
572 | trace_xfs_dqread(dqp); |
573 | ||
97e7ade5 CH |
574 | if (flags & XFS_QMOPT_DQALLOC) { |
575 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC); | |
576 | error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp), | |
577 | XFS_WRITE_LOG_RES(mp) + | |
578 | /* | |
579 | * Round the chunklen up to the next multiple | |
580 | * of 128 (buf log item chunk size)). | |
581 | */ | |
582 | BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 + 128, | |
583 | 0, | |
584 | XFS_TRANS_PERM_LOG_RES, | |
585 | XFS_WRITE_LOG_COUNT); | |
586 | if (error) | |
587 | goto error1; | |
588 | cancelflags = XFS_TRANS_RELEASE_LOG_RES; | |
589 | } | |
590 | ||
1da177e4 LT |
591 | /* |
592 | * get a pointer to the on-disk dquot and the buffer containing it | |
593 | * dqp already knows its own type (GROUP/USER). | |
594 | */ | |
97e7ade5 CH |
595 | error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags); |
596 | if (error) { | |
597 | /* | |
598 | * This can happen if quotas got turned off (ESRCH), | |
599 | * or if the dquot didn't exist on disk and we ask to | |
600 | * allocate (ENOENT). | |
601 | */ | |
602 | trace_xfs_dqread_fail(dqp); | |
603 | cancelflags |= XFS_TRANS_ABORT; | |
604 | goto error1; | |
1da177e4 LT |
605 | } |
606 | ||
607 | /* copy everything from disk dquot to the incore dquot */ | |
608 | memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t)); | |
1da177e4 LT |
609 | xfs_qm_dquot_logitem_init(dqp); |
610 | ||
611 | /* | |
612 | * Reservation counters are defined as reservation plus current usage | |
25985edc | 613 | * to avoid having to add every time. |
1da177e4 | 614 | */ |
1149d96a CH |
615 | dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount); |
616 | dqp->q_res_icount = be64_to_cpu(ddqp->d_icount); | |
617 | dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount); | |
1da177e4 LT |
618 | |
619 | /* Mark the buf so that this will stay incore a little longer */ | |
38f23232 | 620 | xfs_buf_set_ref(bp, XFS_DQUOT_REF); |
1da177e4 LT |
621 | |
622 | /* | |
623 | * We got the buffer with a xfs_trans_read_buf() (in dqtobp()) | |
624 | * So we need to release with xfs_trans_brelse(). | |
625 | * The strategy here is identical to that of inodes; we lock | |
626 | * the dquot in xfs_qm_dqget() before making it accessible to | |
627 | * others. This is because dquots, like inodes, need a good level of | |
628 | * concurrency, and we don't want to take locks on the entire buffers | |
629 | * for dquot accesses. | |
630 | * Note also that the dquot buffer may even be dirty at this point, if | |
631 | * this particular dquot was repaired. We still aren't afraid to | |
632 | * brelse it because we have the changes incore. | |
633 | */ | |
0c842ad4 | 634 | ASSERT(xfs_buf_islocked(bp)); |
1da177e4 LT |
635 | xfs_trans_brelse(tp, bp); |
636 | ||
1da177e4 | 637 | if (tp) { |
97e7ade5 CH |
638 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
639 | if (error) | |
640 | goto error0; | |
1da177e4 LT |
641 | } |
642 | ||
643 | *O_dqpp = dqp; | |
97e7ade5 | 644 | return error; |
1da177e4 | 645 | |
97e7ade5 | 646 | error1: |
1da177e4 LT |
647 | if (tp) |
648 | xfs_trans_cancel(tp, cancelflags); | |
97e7ade5 | 649 | error0: |
1da177e4 LT |
650 | xfs_qm_dqdestroy(dqp); |
651 | *O_dqpp = NULL; | |
97e7ade5 | 652 | return error; |
1da177e4 LT |
653 | } |
654 | ||
655 | /* | |
656 | * Lookup a dquot in the incore dquot hashtable. We keep two separate | |
657 | * hashtables for user and group dquots; and, these are global tables | |
658 | * inside the XQM, not per-filesystem tables. | |
659 | * The hash chain must be locked by caller, and it is left locked | |
660 | * on return. Returning dquot is locked. | |
661 | */ | |
662 | STATIC int | |
663 | xfs_qm_dqlookup( | |
664 | xfs_mount_t *mp, | |
665 | xfs_dqid_t id, | |
666 | xfs_dqhash_t *qh, | |
667 | xfs_dquot_t **O_dqpp) | |
668 | { | |
669 | xfs_dquot_t *dqp; | |
1da177e4 | 670 | |
c9a192dc | 671 | ASSERT(mutex_is_locked(&qh->qh_lock)); |
1da177e4 | 672 | |
1da177e4 LT |
673 | /* |
674 | * Traverse the hashchain looking for a match | |
675 | */ | |
e6a81f13 | 676 | list_for_each_entry(dqp, &qh->qh_list, q_hashlist) { |
1da177e4 LT |
677 | /* |
678 | * We already have the hashlock. We don't need the | |
679 | * dqlock to look at the id field of the dquot, since the | |
680 | * id can't be modified without the hashlock anyway. | |
681 | */ | |
be7ffc38 CH |
682 | if (be32_to_cpu(dqp->q_core.d_id) != id || dqp->q_mount != mp) |
683 | continue; | |
0b1b213f | 684 | |
be7ffc38 | 685 | trace_xfs_dqlookup_found(dqp); |
1da177e4 | 686 | |
be7ffc38 | 687 | xfs_dqlock(dqp); |
92678554 CH |
688 | if (dqp->dq_flags & XFS_DQ_FREEING) { |
689 | *O_dqpp = NULL; | |
690 | xfs_dqunlock(dqp); | |
691 | return -1; | |
692 | } | |
693 | ||
78e55892 | 694 | dqp->q_nrefs++; |
be7ffc38 CH |
695 | |
696 | /* | |
697 | * move the dquot to the front of the hashchain | |
698 | */ | |
699 | list_move(&dqp->q_hashlist, &qh->qh_list); | |
700 | trace_xfs_dqlookup_done(dqp); | |
701 | *O_dqpp = dqp; | |
702 | return 0; | |
1da177e4 LT |
703 | } |
704 | ||
705 | *O_dqpp = NULL; | |
be7ffc38 | 706 | return 1; |
1da177e4 LT |
707 | } |
708 | ||
709 | /* | |
710 | * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a | |
711 | * a locked dquot, doing an allocation (if requested) as needed. | |
712 | * When both an inode and an id are given, the inode's id takes precedence. | |
713 | * That is, if the id changes while we don't hold the ilock inside this | |
714 | * function, the new dquot is returned, not necessarily the one requested | |
715 | * in the id argument. | |
716 | */ | |
717 | int | |
718 | xfs_qm_dqget( | |
719 | xfs_mount_t *mp, | |
720 | xfs_inode_t *ip, /* locked inode (optional) */ | |
c8ad20ff NS |
721 | xfs_dqid_t id, /* uid/projid/gid depending on type */ |
722 | uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */ | |
1da177e4 LT |
723 | uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */ |
724 | xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */ | |
725 | { | |
36731410 | 726 | xfs_dquot_t *dqp, *dqp1; |
1da177e4 LT |
727 | xfs_dqhash_t *h; |
728 | uint version; | |
729 | int error; | |
730 | ||
731 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); | |
732 | if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) || | |
c8ad20ff | 733 | (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) || |
1da177e4 LT |
734 | (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) { |
735 | return (ESRCH); | |
736 | } | |
737 | h = XFS_DQ_HASH(mp, id, type); | |
738 | ||
739 | #ifdef DEBUG | |
740 | if (xfs_do_dqerror) { | |
741 | if ((xfs_dqerror_target == mp->m_ddev_targp) && | |
742 | (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) { | |
0b932ccc | 743 | xfs_debug(mp, "Returning error in dqget"); |
1da177e4 LT |
744 | return (EIO); |
745 | } | |
746 | } | |
1da177e4 | 747 | |
c8ad20ff NS |
748 | ASSERT(type == XFS_DQ_USER || |
749 | type == XFS_DQ_PROJ || | |
750 | type == XFS_DQ_GROUP); | |
1da177e4 | 751 | if (ip) { |
579aa9ca | 752 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
36731410 | 753 | ASSERT(xfs_inode_dquot(ip, type) == NULL); |
1da177e4 LT |
754 | } |
755 | #endif | |
92678554 CH |
756 | |
757 | restart: | |
c9a192dc | 758 | mutex_lock(&h->qh_lock); |
1da177e4 LT |
759 | |
760 | /* | |
761 | * Look in the cache (hashtable). | |
762 | * The chain is kept locked during lookup. | |
763 | */ | |
92678554 CH |
764 | switch (xfs_qm_dqlookup(mp, id, h, O_dqpp)) { |
765 | case -1: | |
766 | XQM_STATS_INC(xqmstats.xs_qm_dquot_dups); | |
767 | mutex_unlock(&h->qh_lock); | |
768 | delay(1); | |
769 | goto restart; | |
770 | case 0: | |
1da177e4 LT |
771 | XQM_STATS_INC(xqmstats.xs_qm_dqcachehits); |
772 | /* | |
773 | * The dquot was found, moved to the front of the chain, | |
774 | * taken off the freelist if it was on it, and locked | |
775 | * at this point. Just unlock the hashchain and return. | |
776 | */ | |
777 | ASSERT(*O_dqpp); | |
778 | ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp)); | |
c9a192dc | 779 | mutex_unlock(&h->qh_lock); |
0b1b213f | 780 | trace_xfs_dqget_hit(*O_dqpp); |
92678554 CH |
781 | return 0; /* success */ |
782 | default: | |
783 | XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses); | |
784 | break; | |
1da177e4 | 785 | } |
1da177e4 LT |
786 | |
787 | /* | |
788 | * Dquot cache miss. We don't want to keep the inode lock across | |
789 | * a (potential) disk read. Also we don't want to deal with the lock | |
790 | * ordering between quotainode and this inode. OTOH, dropping the inode | |
791 | * lock here means dealing with a chown that can happen before | |
792 | * we re-acquire the lock. | |
793 | */ | |
794 | if (ip) | |
795 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
796 | /* | |
797 | * Save the hashchain version stamp, and unlock the chain, so that | |
798 | * we don't keep the lock across a disk read | |
799 | */ | |
800 | version = h->qh_version; | |
c9a192dc | 801 | mutex_unlock(&h->qh_lock); |
1da177e4 | 802 | |
97e7ade5 | 803 | error = xfs_qm_dqread(mp, id, type, flags, &dqp); |
1da177e4 | 804 | |
7ae44407 CH |
805 | if (ip) |
806 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
807 | ||
808 | if (error) | |
809 | return error; | |
1da177e4 LT |
810 | |
811 | /* | |
812 | * Dquot lock comes after hashlock in the lock ordering | |
813 | */ | |
814 | if (ip) { | |
1da177e4 LT |
815 | /* |
816 | * A dquot could be attached to this inode by now, since | |
817 | * we had dropped the ilock. | |
818 | */ | |
36731410 CS |
819 | if (xfs_this_quota_on(mp, type)) { |
820 | dqp1 = xfs_inode_dquot(ip, type); | |
821 | if (dqp1) { | |
1da177e4 | 822 | xfs_qm_dqdestroy(dqp); |
36731410 | 823 | dqp = dqp1; |
1da177e4 LT |
824 | xfs_dqlock(dqp); |
825 | goto dqret; | |
826 | } | |
827 | } else { | |
36731410 CS |
828 | /* inode stays locked on return */ |
829 | xfs_qm_dqdestroy(dqp); | |
830 | return XFS_ERROR(ESRCH); | |
1da177e4 LT |
831 | } |
832 | } | |
833 | ||
834 | /* | |
835 | * Hashlock comes after ilock in lock order | |
836 | */ | |
c9a192dc | 837 | mutex_lock(&h->qh_lock); |
1da177e4 LT |
838 | if (version != h->qh_version) { |
839 | xfs_dquot_t *tmpdqp; | |
840 | /* | |
841 | * Now, see if somebody else put the dquot in the | |
842 | * hashtable before us. This can happen because we didn't | |
843 | * keep the hashchain lock. We don't have to worry about | |
844 | * lock order between the two dquots here since dqp isn't | |
845 | * on any findable lists yet. | |
846 | */ | |
92678554 CH |
847 | switch (xfs_qm_dqlookup(mp, id, h, &tmpdqp)) { |
848 | case 0: | |
849 | case -1: | |
1da177e4 | 850 | /* |
92678554 CH |
851 | * Duplicate found, either in cache or on its way out. |
852 | * Just throw away the new dquot and start over. | |
1da177e4 | 853 | */ |
92678554 CH |
854 | if (tmpdqp) |
855 | xfs_qm_dqput(tmpdqp); | |
c9a192dc | 856 | mutex_unlock(&h->qh_lock); |
1da177e4 LT |
857 | xfs_qm_dqdestroy(dqp); |
858 | XQM_STATS_INC(xqmstats.xs_qm_dquot_dups); | |
92678554 CH |
859 | goto restart; |
860 | default: | |
861 | break; | |
1da177e4 LT |
862 | } |
863 | } | |
864 | ||
865 | /* | |
866 | * Put the dquot at the beginning of the hash-chain and mp's list | |
867 | * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock .. | |
868 | */ | |
c9a192dc | 869 | ASSERT(mutex_is_locked(&h->qh_lock)); |
1da177e4 | 870 | dqp->q_hash = h; |
e6a81f13 DC |
871 | list_add(&dqp->q_hashlist, &h->qh_list); |
872 | h->qh_version++; | |
1da177e4 LT |
873 | |
874 | /* | |
875 | * Attach this dquot to this filesystem's list of all dquots, | |
876 | * kept inside the mount structure in m_quotainfo field | |
877 | */ | |
3a25404b | 878 | mutex_lock(&mp->m_quotainfo->qi_dqlist_lock); |
1da177e4 LT |
879 | |
880 | /* | |
881 | * We return a locked dquot to the caller, with a reference taken | |
882 | */ | |
883 | xfs_dqlock(dqp); | |
884 | dqp->q_nrefs = 1; | |
885 | ||
3a25404b DC |
886 | list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist); |
887 | mp->m_quotainfo->qi_dquots++; | |
888 | mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock); | |
c9a192dc | 889 | mutex_unlock(&h->qh_lock); |
1da177e4 | 890 | dqret: |
579aa9ca | 891 | ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
0b1b213f | 892 | trace_xfs_dqget_miss(dqp); |
1da177e4 LT |
893 | *O_dqpp = dqp; |
894 | return (0); | |
895 | } | |
896 | ||
897 | ||
898 | /* | |
899 | * Release a reference to the dquot (decrement ref-count) | |
900 | * and unlock it. If there is a group quota attached to this | |
901 | * dquot, carefully release that too without tripping over | |
902 | * deadlocks'n'stuff. | |
903 | */ | |
904 | void | |
905 | xfs_qm_dqput( | |
bf72de31 | 906 | struct xfs_dquot *dqp) |
1da177e4 | 907 | { |
bf72de31 | 908 | struct xfs_dquot *gdqp; |
1da177e4 LT |
909 | |
910 | ASSERT(dqp->q_nrefs > 0); | |
911 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); | |
0b1b213f CH |
912 | |
913 | trace_xfs_dqput(dqp); | |
1da177e4 | 914 | |
bf72de31 CH |
915 | recurse: |
916 | if (--dqp->q_nrefs > 0) { | |
1da177e4 LT |
917 | xfs_dqunlock(dqp); |
918 | return; | |
919 | } | |
920 | ||
bf72de31 CH |
921 | trace_xfs_dqput_free(dqp); |
922 | ||
923 | mutex_lock(&xfs_Gqm->qm_dqfrlist_lock); | |
924 | if (list_empty(&dqp->q_freelist)) { | |
925 | list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist); | |
926 | xfs_Gqm->qm_dqfrlist_cnt++; | |
927 | } | |
928 | mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock); | |
929 | ||
1da177e4 | 930 | /* |
bf72de31 CH |
931 | * If we just added a udquot to the freelist, then we want to release |
932 | * the gdquot reference that it (probably) has. Otherwise it'll keep | |
933 | * the gdquot from getting reclaimed. | |
1da177e4 | 934 | */ |
bf72de31 CH |
935 | gdqp = dqp->q_gdquot; |
936 | if (gdqp) { | |
937 | xfs_dqlock(gdqp); | |
938 | dqp->q_gdquot = NULL; | |
1da177e4 | 939 | } |
bf72de31 | 940 | xfs_dqunlock(dqp); |
1da177e4 | 941 | |
bf72de31 CH |
942 | /* |
943 | * If we had a group quota hint, release it now. | |
944 | */ | |
945 | if (gdqp) { | |
1da177e4 | 946 | dqp = gdqp; |
bf72de31 | 947 | goto recurse; |
1da177e4 | 948 | } |
1da177e4 LT |
949 | } |
950 | ||
951 | /* | |
952 | * Release a dquot. Flush it if dirty, then dqput() it. | |
953 | * dquot must not be locked. | |
954 | */ | |
955 | void | |
956 | xfs_qm_dqrele( | |
957 | xfs_dquot_t *dqp) | |
958 | { | |
7d095257 CH |
959 | if (!dqp) |
960 | return; | |
961 | ||
0b1b213f | 962 | trace_xfs_dqrele(dqp); |
1da177e4 LT |
963 | |
964 | xfs_dqlock(dqp); | |
965 | /* | |
966 | * We don't care to flush it if the dquot is dirty here. | |
967 | * That will create stutters that we want to avoid. | |
968 | * Instead we do a delayed write when we try to reclaim | |
969 | * a dirty dquot. Also xfs_sync will take part of the burden... | |
970 | */ | |
971 | xfs_qm_dqput(dqp); | |
972 | } | |
973 | ||
ca30b2a7 CH |
974 | /* |
975 | * This is the dquot flushing I/O completion routine. It is called | |
976 | * from interrupt level when the buffer containing the dquot is | |
977 | * flushed to disk. It is responsible for removing the dquot logitem | |
978 | * from the AIL if it has not been re-logged, and unlocking the dquot's | |
979 | * flush lock. This behavior is very similar to that of inodes.. | |
980 | */ | |
981 | STATIC void | |
982 | xfs_qm_dqflush_done( | |
983 | struct xfs_buf *bp, | |
984 | struct xfs_log_item *lip) | |
985 | { | |
986 | xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip; | |
987 | xfs_dquot_t *dqp = qip->qli_dquot; | |
988 | struct xfs_ail *ailp = lip->li_ailp; | |
989 | ||
990 | /* | |
991 | * We only want to pull the item from the AIL if its | |
992 | * location in the log has not changed since we started the flush. | |
993 | * Thus, we only bother if the dquot's lsn has | |
994 | * not changed. First we check the lsn outside the lock | |
995 | * since it's cheaper, and then we recheck while | |
996 | * holding the lock before removing the dquot from the AIL. | |
997 | */ | |
998 | if ((lip->li_flags & XFS_LI_IN_AIL) && | |
999 | lip->li_lsn == qip->qli_flush_lsn) { | |
1000 | ||
1001 | /* xfs_trans_ail_delete() drops the AIL lock. */ | |
1002 | spin_lock(&ailp->xa_lock); | |
1003 | if (lip->li_lsn == qip->qli_flush_lsn) | |
1004 | xfs_trans_ail_delete(ailp, lip); | |
1005 | else | |
1006 | spin_unlock(&ailp->xa_lock); | |
1007 | } | |
1008 | ||
1009 | /* | |
1010 | * Release the dq's flush lock since we're done with it. | |
1011 | */ | |
1012 | xfs_dqfunlock(dqp); | |
1013 | } | |
1da177e4 LT |
1014 | |
1015 | /* | |
1016 | * Write a modified dquot to disk. | |
1017 | * The dquot must be locked and the flush lock too taken by caller. | |
1018 | * The flush lock will not be unlocked until the dquot reaches the disk, | |
1019 | * but the dquot is free to be unlocked and modified by the caller | |
1020 | * in the interim. Dquot is still locked on return. This behavior is | |
1021 | * identical to that of inodes. | |
1022 | */ | |
1023 | int | |
1024 | xfs_qm_dqflush( | |
1025 | xfs_dquot_t *dqp, | |
1026 | uint flags) | |
1027 | { | |
acecf1b5 CH |
1028 | struct xfs_mount *mp = dqp->q_mount; |
1029 | struct xfs_buf *bp; | |
1030 | struct xfs_disk_dquot *ddqp; | |
1da177e4 | 1031 | int error; |
1da177e4 LT |
1032 | |
1033 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); | |
e1f49cf2 | 1034 | ASSERT(!completion_done(&dqp->q_flush)); |
acecf1b5 | 1035 | |
0b1b213f | 1036 | trace_xfs_dqflush(dqp); |
1da177e4 LT |
1037 | |
1038 | /* | |
acecf1b5 | 1039 | * If not dirty, or it's pinned and we are not supposed to block, nada. |
1da177e4 | 1040 | */ |
2f8a3ce1 | 1041 | if (!XFS_DQ_IS_DIRTY(dqp) || |
fdedf28b | 1042 | ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) { |
1da177e4 | 1043 | xfs_dqfunlock(dqp); |
2f8a3ce1 | 1044 | return 0; |
1da177e4 | 1045 | } |
1da177e4 LT |
1046 | xfs_qm_dqunpin_wait(dqp); |
1047 | ||
1048 | /* | |
1049 | * This may have been unpinned because the filesystem is shutting | |
1050 | * down forcibly. If that's the case we must not write this dquot | |
1051 | * to disk, because the log record didn't make it to disk! | |
1052 | */ | |
acecf1b5 CH |
1053 | if (XFS_FORCED_SHUTDOWN(mp)) { |
1054 | dqp->dq_flags &= ~XFS_DQ_DIRTY; | |
1da177e4 LT |
1055 | xfs_dqfunlock(dqp); |
1056 | return XFS_ERROR(EIO); | |
1057 | } | |
1058 | ||
1059 | /* | |
1060 | * Get the buffer containing the on-disk dquot | |
1da177e4 | 1061 | */ |
acecf1b5 CH |
1062 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno, |
1063 | mp->m_quotainfo->qi_dqchunklen, 0, &bp); | |
1064 | if (error) { | |
1da177e4 | 1065 | ASSERT(error != ENOENT); |
1da177e4 | 1066 | xfs_dqfunlock(dqp); |
acecf1b5 | 1067 | return error; |
1da177e4 LT |
1068 | } |
1069 | ||
acecf1b5 CH |
1070 | /* |
1071 | * Calculate the location of the dquot inside the buffer. | |
1072 | */ | |
62926044 | 1073 | ddqp = bp->b_addr + dqp->q_bufoffset; |
acecf1b5 CH |
1074 | |
1075 | /* | |
1076 | * A simple sanity check in case we got a corrupted dquot.. | |
1077 | */ | |
a0fa2b67 DC |
1078 | error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0, |
1079 | XFS_QMOPT_DOWARN, "dqflush (incore copy)"); | |
1080 | if (error) { | |
acecf1b5 CH |
1081 | xfs_buf_relse(bp); |
1082 | xfs_dqfunlock(dqp); | |
1083 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); | |
1da177e4 LT |
1084 | return XFS_ERROR(EIO); |
1085 | } | |
1086 | ||
1087 | /* This is the only portion of data that needs to persist */ | |
acecf1b5 | 1088 | memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t)); |
1da177e4 LT |
1089 | |
1090 | /* | |
1091 | * Clear the dirty field and remember the flush lsn for later use. | |
1092 | */ | |
acecf1b5 | 1093 | dqp->dq_flags &= ~XFS_DQ_DIRTY; |
1da177e4 | 1094 | |
7b2e2a31 DC |
1095 | xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn, |
1096 | &dqp->q_logitem.qli_item.li_lsn); | |
1da177e4 LT |
1097 | |
1098 | /* | |
1099 | * Attach an iodone routine so that we can remove this dquot from the | |
1100 | * AIL and release the flush lock once the dquot is synced to disk. | |
1101 | */ | |
ca30b2a7 CH |
1102 | xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done, |
1103 | &dqp->q_logitem.qli_item); | |
1104 | ||
1da177e4 LT |
1105 | /* |
1106 | * If the buffer is pinned then push on the log so we won't | |
1107 | * get stuck waiting in the write for too long. | |
1108 | */ | |
811e64c7 | 1109 | if (xfs_buf_ispinned(bp)) { |
0b1b213f | 1110 | trace_xfs_dqflush_force(dqp); |
a14a348b | 1111 | xfs_log_force(mp, 0); |
1da177e4 LT |
1112 | } |
1113 | ||
20026d92 | 1114 | if (flags & SYNC_WAIT) |
c2b006c1 CH |
1115 | error = xfs_bwrite(bp); |
1116 | else | |
61551f1e | 1117 | xfs_buf_delwri_queue(bp); |
c2b006c1 CH |
1118 | |
1119 | xfs_buf_relse(bp); | |
0b1b213f CH |
1120 | |
1121 | trace_xfs_dqflush_done(dqp); | |
1122 | ||
1da177e4 LT |
1123 | /* |
1124 | * dqp is still locked, but caller is free to unlock it now. | |
1125 | */ | |
20026d92 | 1126 | return error; |
1da177e4 LT |
1127 | |
1128 | } | |
1129 | ||
1da177e4 LT |
1130 | void |
1131 | xfs_dqunlock( | |
1132 | xfs_dquot_t *dqp) | |
1133 | { | |
800b484e | 1134 | xfs_dqunlock_nonotify(dqp); |
1da177e4 | 1135 | if (dqp->q_logitem.qli_dquot == dqp) { |
783a2f65 | 1136 | xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp, |
800b484e | 1137 | &dqp->q_logitem.qli_item); |
1da177e4 LT |
1138 | } |
1139 | } | |
1140 | ||
5bb87a33 CH |
1141 | /* |
1142 | * Lock two xfs_dquot structures. | |
1143 | * | |
1144 | * To avoid deadlocks we always lock the quota structure with | |
1145 | * the lowerd id first. | |
1146 | */ | |
1da177e4 LT |
1147 | void |
1148 | xfs_dqlock2( | |
1149 | xfs_dquot_t *d1, | |
1150 | xfs_dquot_t *d2) | |
1151 | { | |
1152 | if (d1 && d2) { | |
1153 | ASSERT(d1 != d2); | |
1149d96a CH |
1154 | if (be32_to_cpu(d1->q_core.d_id) > |
1155 | be32_to_cpu(d2->q_core.d_id)) { | |
5bb87a33 CH |
1156 | mutex_lock(&d2->q_qlock); |
1157 | mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED); | |
1da177e4 | 1158 | } else { |
5bb87a33 CH |
1159 | mutex_lock(&d1->q_qlock); |
1160 | mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED); | |
1da177e4 | 1161 | } |
5bb87a33 CH |
1162 | } else if (d1) { |
1163 | mutex_lock(&d1->q_qlock); | |
1164 | } else if (d2) { | |
1165 | mutex_lock(&d2->q_qlock); | |
1da177e4 LT |
1166 | } |
1167 | } | |
1168 | ||
1da177e4 | 1169 | /* |
92678554 CH |
1170 | * Take a dquot out of the mount's dqlist as well as the hashlist. This is |
1171 | * called via unmount as well as quotaoff, and the purge will always succeed. | |
1da177e4 | 1172 | */ |
92678554 | 1173 | void |
1da177e4 | 1174 | xfs_qm_dqpurge( |
92678554 | 1175 | struct xfs_dquot *dqp) |
1da177e4 | 1176 | { |
92678554 CH |
1177 | struct xfs_mount *mp = dqp->q_mount; |
1178 | struct xfs_dqhash *qh = dqp->q_hash; | |
80a376bf | 1179 | |
1da177e4 | 1180 | xfs_dqlock(dqp); |
1da177e4 LT |
1181 | |
1182 | /* | |
1183 | * If we're turning off quotas, we have to make sure that, for | |
1184 | * example, we don't delete quota disk blocks while dquots are | |
1185 | * in the process of getting written to those disk blocks. | |
1186 | * This dquot might well be on AIL, and we can't leave it there | |
1187 | * if we're turning off quotas. Basically, we need this flush | |
1188 | * lock, and are willing to block on it. | |
1189 | */ | |
e1f49cf2 | 1190 | if (!xfs_dqflock_nowait(dqp)) { |
1da177e4 LT |
1191 | /* |
1192 | * Block on the flush lock after nudging dquot buffer, | |
1193 | * if it is incore. | |
1194 | */ | |
800b484e | 1195 | xfs_dqflock_pushbuf_wait(dqp); |
1da177e4 LT |
1196 | } |
1197 | ||
1198 | /* | |
92678554 | 1199 | * If we are turning this type of quotas off, we don't care |
1da177e4 LT |
1200 | * about the dirty metadata sitting in this dquot. OTOH, if |
1201 | * we're unmounting, we do care, so we flush it and wait. | |
1202 | */ | |
1203 | if (XFS_DQ_IS_DIRTY(dqp)) { | |
3c56836f | 1204 | int error; |
0b1b213f | 1205 | |
1da177e4 | 1206 | /* |
1da177e4 LT |
1207 | * We don't care about getting disk errors here. We need |
1208 | * to purge this dquot anyway, so we go ahead regardless. | |
1209 | */ | |
20026d92 | 1210 | error = xfs_qm_dqflush(dqp, SYNC_WAIT); |
3c56836f | 1211 | if (error) |
53487786 DC |
1212 | xfs_warn(mp, "%s: dquot %p flush failed", |
1213 | __func__, dqp); | |
1da177e4 LT |
1214 | xfs_dqflock(dqp); |
1215 | } | |
92678554 | 1216 | |
bc3048e3 | 1217 | ASSERT(atomic_read(&dqp->q_pincount) == 0); |
1da177e4 LT |
1218 | ASSERT(XFS_FORCED_SHUTDOWN(mp) || |
1219 | !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL)); | |
1220 | ||
92678554 CH |
1221 | xfs_dqfunlock(dqp); |
1222 | xfs_dqunlock(dqp); | |
1223 | ||
1224 | mutex_lock(&qh->qh_lock); | |
e6a81f13 DC |
1225 | list_del_init(&dqp->q_hashlist); |
1226 | qh->qh_version++; | |
92678554 | 1227 | mutex_unlock(&qh->qh_lock); |
80a376bf | 1228 | |
92678554 | 1229 | mutex_lock(&mp->m_quotainfo->qi_dqlist_lock); |
3a25404b DC |
1230 | list_del_init(&dqp->q_mplist); |
1231 | mp->m_quotainfo->qi_dqreclaims++; | |
1232 | mp->m_quotainfo->qi_dquots--; | |
92678554 | 1233 | mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock); |
1da177e4 | 1234 | |
92678554 CH |
1235 | /* |
1236 | * We move dquots to the freelist as soon as their reference count | |
1237 | * hits zero, so it really should be on the freelist here. | |
1238 | */ | |
1239 | mutex_lock(&xfs_Gqm->qm_dqfrlist_lock); | |
1240 | ASSERT(!list_empty(&dqp->q_freelist)); | |
80a376bf CH |
1241 | list_del_init(&dqp->q_freelist); |
1242 | xfs_Gqm->qm_dqfrlist_cnt--; | |
80a376bf | 1243 | mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock); |
1da177e4 | 1244 | |
80a376bf | 1245 | xfs_qm_dqdestroy(dqp); |
80a376bf | 1246 | } |
1da177e4 | 1247 | |
1da177e4 LT |
1248 | /* |
1249 | * Give the buffer a little push if it is incore and | |
1250 | * wait on the flush lock. | |
1251 | */ | |
1252 | void | |
800b484e | 1253 | xfs_dqflock_pushbuf_wait( |
1da177e4 LT |
1254 | xfs_dquot_t *dqp) |
1255 | { | |
8a7b8a89 | 1256 | xfs_mount_t *mp = dqp->q_mount; |
1da177e4 LT |
1257 | xfs_buf_t *bp; |
1258 | ||
1259 | /* | |
1260 | * Check to see if the dquot has been flushed delayed | |
1261 | * write. If so, grab its buffer and send it | |
1262 | * out immediately. We'll be able to acquire | |
1263 | * the flush lock when the I/O completes. | |
1264 | */ | |
8a7b8a89 CH |
1265 | bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno, |
1266 | mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK); | |
7d6a7bde DC |
1267 | if (!bp) |
1268 | goto out_lock; | |
1269 | ||
1270 | if (XFS_BUF_ISDELAYWRITE(bp)) { | |
811e64c7 | 1271 | if (xfs_buf_ispinned(bp)) |
8a7b8a89 | 1272 | xfs_log_force(mp, 0); |
7d6a7bde DC |
1273 | xfs_buf_delwri_promote(bp); |
1274 | wake_up_process(bp->b_target->bt_task); | |
1da177e4 | 1275 | } |
7d6a7bde DC |
1276 | xfs_buf_relse(bp); |
1277 | out_lock: | |
1da177e4 LT |
1278 | xfs_dqflock(dqp); |
1279 | } |