]>
Commit | Line | Data |
---|---|---|
9e33d69f JK |
1 | /* |
2 | * Implementation of operations over global quota file | |
3 | */ | |
171bf93c | 4 | #include <linux/spinlock.h> |
9e33d69f JK |
5 | #include <linux/fs.h> |
6 | #include <linux/quota.h> | |
7 | #include <linux/quotaops.h> | |
8 | #include <linux/dqblk_qtree.h> | |
171bf93c MF |
9 | #include <linux/jiffies.h> |
10 | #include <linux/writeback.h> | |
11 | #include <linux/workqueue.h> | |
9e33d69f JK |
12 | |
13 | #define MLOG_MASK_PREFIX ML_QUOTA | |
14 | #include <cluster/masklog.h> | |
15 | ||
16 | #include "ocfs2_fs.h" | |
17 | #include "ocfs2.h" | |
18 | #include "alloc.h" | |
d6b32bbb | 19 | #include "blockcheck.h" |
9e33d69f JK |
20 | #include "inode.h" |
21 | #include "journal.h" | |
22 | #include "file.h" | |
23 | #include "sysfile.h" | |
24 | #include "dlmglue.h" | |
25 | #include "uptodate.h" | |
ada50827 | 26 | #include "super.h" |
9e33d69f JK |
27 | #include "quota.h" |
28 | ||
171bf93c MF |
29 | static struct workqueue_struct *ocfs2_quota_wq = NULL; |
30 | ||
31 | static void qsync_work_fn(struct work_struct *work); | |
32 | ||
9e33d69f JK |
33 | static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp) |
34 | { | |
35 | struct ocfs2_global_disk_dqblk *d = dp; | |
36 | struct mem_dqblk *m = &dquot->dq_dqb; | |
37 | ||
38 | /* Update from disk only entries not set by the admin */ | |
39 | if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) { | |
40 | m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit); | |
41 | m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit); | |
42 | } | |
43 | if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags)) | |
44 | m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes); | |
45 | if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) { | |
46 | m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit); | |
47 | m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit); | |
48 | } | |
49 | if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags)) | |
50 | m->dqb_curspace = le64_to_cpu(d->dqb_curspace); | |
51 | if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags)) | |
52 | m->dqb_btime = le64_to_cpu(d->dqb_btime); | |
53 | if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags)) | |
54 | m->dqb_itime = le64_to_cpu(d->dqb_itime); | |
55 | OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count); | |
56 | } | |
57 | ||
58 | static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot) | |
59 | { | |
60 | struct ocfs2_global_disk_dqblk *d = dp; | |
61 | struct mem_dqblk *m = &dquot->dq_dqb; | |
62 | ||
63 | d->dqb_id = cpu_to_le32(dquot->dq_id); | |
64 | d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count); | |
65 | d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit); | |
66 | d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit); | |
67 | d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes); | |
68 | d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit); | |
69 | d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit); | |
70 | d->dqb_curspace = cpu_to_le64(m->dqb_curspace); | |
71 | d->dqb_btime = cpu_to_le64(m->dqb_btime); | |
72 | d->dqb_itime = cpu_to_le64(m->dqb_itime); | |
7669f54c | 73 | d->dqb_pad1 = d->dqb_pad2 = 0; |
9e33d69f JK |
74 | } |
75 | ||
76 | static int ocfs2_global_is_id(void *dp, struct dquot *dquot) | |
77 | { | |
78 | struct ocfs2_global_disk_dqblk *d = dp; | |
79 | struct ocfs2_mem_dqinfo *oinfo = | |
80 | sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; | |
81 | ||
82 | if (qtree_entry_unused(&oinfo->dqi_gi, dp)) | |
83 | return 0; | |
84 | return le32_to_cpu(d->dqb_id) == dquot->dq_id; | |
85 | } | |
86 | ||
87 | struct qtree_fmt_operations ocfs2_global_ops = { | |
88 | .mem2disk_dqblk = ocfs2_global_mem2diskdqb, | |
89 | .disk2mem_dqblk = ocfs2_global_disk2memdqb, | |
90 | .is_id = ocfs2_global_is_id, | |
91 | }; | |
92 | ||
684ef278 JB |
93 | static int ocfs2_validate_quota_block(struct super_block *sb, |
94 | struct buffer_head *bh) | |
95 | { | |
d6b32bbb JB |
96 | struct ocfs2_disk_dqtrailer *dqt = |
97 | ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data); | |
684ef278 JB |
98 | |
99 | mlog(0, "Validating quota block %llu\n", | |
100 | (unsigned long long)bh->b_blocknr); | |
101 | ||
d6b32bbb JB |
102 | BUG_ON(!buffer_uptodate(bh)); |
103 | ||
104 | /* | |
105 | * If the ecc fails, we return the error but otherwise | |
106 | * leave the filesystem running. We know any error is | |
107 | * local to this block. | |
108 | */ | |
109 | return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check); | |
684ef278 JB |
110 | } |
111 | ||
85eb8b73 JB |
112 | int ocfs2_read_quota_block(struct inode *inode, u64 v_block, |
113 | struct buffer_head **bh) | |
9e33d69f | 114 | { |
85eb8b73 JB |
115 | int rc = 0; |
116 | struct buffer_head *tmp = *bh; | |
9e33d69f | 117 | |
ada50827 JK |
118 | if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) { |
119 | ocfs2_error(inode->i_sb, | |
120 | "Quota file %llu is probably corrupted! Requested " | |
121 | "to read block %Lu but file has size only %Lu\n", | |
122 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
123 | (unsigned long long)v_block, | |
124 | (unsigned long long)i_size_read(inode)); | |
125 | return -EIO; | |
126 | } | |
684ef278 JB |
127 | rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0, |
128 | ocfs2_validate_quota_block); | |
85eb8b73 JB |
129 | if (rc) |
130 | mlog_errno(rc); | |
131 | ||
132 | /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */ | |
133 | if (!rc && !*bh) | |
134 | *bh = tmp; | |
9e33d69f | 135 | |
85eb8b73 | 136 | return rc; |
9e33d69f JK |
137 | } |
138 | ||
53a36046 JK |
139 | static int ocfs2_get_quota_block(struct inode *inode, int block, |
140 | struct buffer_head **bh) | |
9e33d69f JK |
141 | { |
142 | u64 pblock, pcount; | |
53a36046 | 143 | int err; |
9e33d69f JK |
144 | |
145 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
53a36046 | 146 | err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL); |
9e33d69f | 147 | up_read(&OCFS2_I(inode)->ip_alloc_sem); |
53a36046 JK |
148 | if (err) { |
149 | mlog_errno(err); | |
150 | return err; | |
9e33d69f | 151 | } |
53a36046 JK |
152 | *bh = sb_getblk(inode->i_sb, pblock); |
153 | if (!*bh) { | |
154 | err = -EIO; | |
155 | mlog_errno(err); | |
9e33d69f | 156 | } |
a419aef8 | 157 | return err; |
9e33d69f JK |
158 | } |
159 | ||
160 | /* Read data from global quotafile - avoid pagecache and such because we cannot | |
161 | * afford acquiring the locks... We use quota cluster lock to serialize | |
162 | * operations. Caller is responsible for acquiring it. */ | |
163 | ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data, | |
164 | size_t len, loff_t off) | |
165 | { | |
166 | struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; | |
167 | struct inode *gqinode = oinfo->dqi_gqinode; | |
168 | loff_t i_size = i_size_read(gqinode); | |
169 | int offset = off & (sb->s_blocksize - 1); | |
170 | sector_t blk = off >> sb->s_blocksize_bits; | |
171 | int err = 0; | |
172 | struct buffer_head *bh; | |
173 | size_t toread, tocopy; | |
174 | ||
175 | if (off > i_size) | |
176 | return 0; | |
177 | if (off + len > i_size) | |
178 | len = i_size - off; | |
179 | toread = len; | |
180 | while (toread > 0) { | |
dad7d975 | 181 | tocopy = min_t(size_t, (sb->s_blocksize - offset), toread); |
85eb8b73 JB |
182 | bh = NULL; |
183 | err = ocfs2_read_quota_block(gqinode, blk, &bh); | |
184 | if (err) { | |
9e33d69f JK |
185 | mlog_errno(err); |
186 | return err; | |
187 | } | |
188 | memcpy(data, bh->b_data + offset, tocopy); | |
189 | brelse(bh); | |
190 | offset = 0; | |
191 | toread -= tocopy; | |
192 | data += tocopy; | |
193 | blk++; | |
194 | } | |
195 | return len; | |
196 | } | |
197 | ||
198 | /* Write to quotafile (we know the transaction is already started and has | |
199 | * enough credits) */ | |
200 | ssize_t ocfs2_quota_write(struct super_block *sb, int type, | |
201 | const char *data, size_t len, loff_t off) | |
202 | { | |
203 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
204 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
205 | struct inode *gqinode = oinfo->dqi_gqinode; | |
206 | int offset = off & (sb->s_blocksize - 1); | |
207 | sector_t blk = off >> sb->s_blocksize_bits; | |
af09e51b | 208 | int err = 0, new = 0, ja_type; |
85eb8b73 | 209 | struct buffer_head *bh = NULL; |
9e33d69f JK |
210 | handle_t *handle = journal_current_handle(); |
211 | ||
212 | if (!handle) { | |
213 | mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled " | |
214 | "because transaction was not started.\n", | |
215 | (unsigned long long)off, (unsigned long long)len); | |
216 | return -EIO; | |
217 | } | |
218 | if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) { | |
219 | WARN_ON(1); | |
220 | len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset; | |
221 | } | |
222 | ||
223 | mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA); | |
224 | if (gqinode->i_size < off + len) { | |
b57ac2c4 JK |
225 | loff_t rounded_end = |
226 | ocfs2_align_bytes_to_blocks(sb, off + len); | |
227 | ||
b409d7a0 | 228 | /* Space is already allocated in ocfs2_global_read_dquot() */ |
9e33d69f JK |
229 | err = ocfs2_simple_size_update(gqinode, |
230 | oinfo->dqi_gqi_bh, | |
b57ac2c4 | 231 | rounded_end); |
9e33d69f JK |
232 | if (err < 0) |
233 | goto out; | |
234 | new = 1; | |
235 | } | |
236 | /* Not rewriting whole block? */ | |
237 | if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) && | |
238 | !new) { | |
85eb8b73 | 239 | err = ocfs2_read_quota_block(gqinode, blk, &bh); |
af09e51b | 240 | ja_type = OCFS2_JOURNAL_ACCESS_WRITE; |
9e33d69f | 241 | } else { |
53a36046 | 242 | err = ocfs2_get_quota_block(gqinode, blk, &bh); |
af09e51b | 243 | ja_type = OCFS2_JOURNAL_ACCESS_CREATE; |
9e33d69f | 244 | } |
af09e51b JK |
245 | if (err) { |
246 | mlog_errno(err); | |
e9956fae | 247 | goto out; |
9e33d69f JK |
248 | } |
249 | lock_buffer(bh); | |
250 | if (new) | |
251 | memset(bh->b_data, 0, sb->s_blocksize); | |
252 | memcpy(bh->b_data + offset, data, len); | |
253 | flush_dcache_page(bh->b_page); | |
af09e51b | 254 | set_buffer_uptodate(bh); |
9e33d69f | 255 | unlock_buffer(bh); |
8cb471e8 | 256 | ocfs2_set_buffer_uptodate(INODE_CACHE(gqinode), bh); |
0cf2f763 JB |
257 | err = ocfs2_journal_access_dq(handle, INODE_CACHE(gqinode), bh, |
258 | ja_type); | |
af09e51b JK |
259 | if (err < 0) { |
260 | brelse(bh); | |
261 | goto out; | |
262 | } | |
9e33d69f JK |
263 | err = ocfs2_journal_dirty(handle, bh); |
264 | brelse(bh); | |
265 | if (err < 0) | |
266 | goto out; | |
267 | out: | |
268 | if (err) { | |
269 | mutex_unlock(&gqinode->i_mutex); | |
270 | mlog_errno(err); | |
271 | return err; | |
272 | } | |
273 | gqinode->i_version++; | |
274 | ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh); | |
275 | mutex_unlock(&gqinode->i_mutex); | |
276 | return len; | |
277 | } | |
278 | ||
279 | int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex) | |
280 | { | |
281 | int status; | |
282 | struct buffer_head *bh = NULL; | |
283 | ||
284 | status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex); | |
285 | if (status < 0) | |
286 | return status; | |
287 | spin_lock(&dq_data_lock); | |
288 | if (!oinfo->dqi_gqi_count++) | |
289 | oinfo->dqi_gqi_bh = bh; | |
290 | else | |
291 | WARN_ON(bh != oinfo->dqi_gqi_bh); | |
292 | spin_unlock(&dq_data_lock); | |
293 | return 0; | |
294 | } | |
295 | ||
296 | void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex) | |
297 | { | |
298 | ocfs2_inode_unlock(oinfo->dqi_gqinode, ex); | |
299 | brelse(oinfo->dqi_gqi_bh); | |
300 | spin_lock(&dq_data_lock); | |
301 | if (!--oinfo->dqi_gqi_count) | |
302 | oinfo->dqi_gqi_bh = NULL; | |
303 | spin_unlock(&dq_data_lock); | |
304 | } | |
305 | ||
306 | /* Read information header from global quota file */ | |
307 | int ocfs2_global_read_info(struct super_block *sb, int type) | |
308 | { | |
309 | struct inode *gqinode = NULL; | |
310 | unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE, | |
311 | GROUP_QUOTA_SYSTEM_INODE }; | |
312 | struct ocfs2_global_disk_dqinfo dinfo; | |
313 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
314 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
315 | int status; | |
316 | ||
317 | mlog_entry_void(); | |
318 | ||
319 | /* Read global header */ | |
320 | gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type], | |
321 | OCFS2_INVALID_SLOT); | |
322 | if (!gqinode) { | |
323 | mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n", | |
324 | type); | |
325 | status = -EINVAL; | |
326 | goto out_err; | |
327 | } | |
328 | oinfo->dqi_gi.dqi_sb = sb; | |
329 | oinfo->dqi_gi.dqi_type = type; | |
330 | ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo); | |
331 | oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk); | |
332 | oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops; | |
333 | oinfo->dqi_gqi_bh = NULL; | |
334 | oinfo->dqi_gqi_count = 0; | |
335 | oinfo->dqi_gqinode = gqinode; | |
336 | status = ocfs2_lock_global_qf(oinfo, 0); | |
337 | if (status < 0) { | |
338 | mlog_errno(status); | |
339 | goto out_err; | |
340 | } | |
341 | status = sb->s_op->quota_read(sb, type, (char *)&dinfo, | |
342 | sizeof(struct ocfs2_global_disk_dqinfo), | |
343 | OCFS2_GLOBAL_INFO_OFF); | |
344 | ocfs2_unlock_global_qf(oinfo, 0); | |
345 | if (status != sizeof(struct ocfs2_global_disk_dqinfo)) { | |
346 | mlog(ML_ERROR, "Cannot read global quota info (%d).\n", | |
347 | status); | |
348 | if (status >= 0) | |
349 | status = -EIO; | |
350 | mlog_errno(status); | |
351 | goto out_err; | |
352 | } | |
353 | info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace); | |
354 | info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace); | |
355 | oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms); | |
356 | oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks); | |
357 | oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk); | |
358 | oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry); | |
359 | oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits; | |
360 | oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize - | |
361 | OCFS2_QBLK_RESERVED_SPACE; | |
362 | oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi); | |
171bf93c MF |
363 | INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn); |
364 | queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work, | |
4539f1df | 365 | msecs_to_jiffies(oinfo->dqi_syncms)); |
171bf93c | 366 | |
9e33d69f JK |
367 | out_err: |
368 | mlog_exit(status); | |
369 | return status; | |
370 | } | |
371 | ||
372 | /* Write information to global quota file. Expects exlusive lock on quota | |
373 | * file inode and quota info */ | |
374 | static int __ocfs2_global_write_info(struct super_block *sb, int type) | |
375 | { | |
376 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
377 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
378 | struct ocfs2_global_disk_dqinfo dinfo; | |
379 | ssize_t size; | |
380 | ||
381 | spin_lock(&dq_data_lock); | |
382 | info->dqi_flags &= ~DQF_INFO_DIRTY; | |
383 | dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace); | |
384 | dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace); | |
385 | spin_unlock(&dq_data_lock); | |
386 | dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms); | |
387 | dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks); | |
388 | dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk); | |
389 | dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry); | |
390 | size = sb->s_op->quota_write(sb, type, (char *)&dinfo, | |
391 | sizeof(struct ocfs2_global_disk_dqinfo), | |
392 | OCFS2_GLOBAL_INFO_OFF); | |
393 | if (size != sizeof(struct ocfs2_global_disk_dqinfo)) { | |
394 | mlog(ML_ERROR, "Cannot write global quota info structure\n"); | |
395 | if (size >= 0) | |
396 | size = -EIO; | |
397 | return size; | |
398 | } | |
399 | return 0; | |
400 | } | |
401 | ||
402 | int ocfs2_global_write_info(struct super_block *sb, int type) | |
403 | { | |
404 | int err; | |
405 | struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv; | |
406 | ||
407 | err = ocfs2_qinfo_lock(info, 1); | |
408 | if (err < 0) | |
409 | return err; | |
410 | err = __ocfs2_global_write_info(sb, type); | |
411 | ocfs2_qinfo_unlock(info, 1); | |
412 | return err; | |
413 | } | |
414 | ||
b409d7a0 JK |
415 | static int ocfs2_global_qinit_alloc(struct super_block *sb, int type) |
416 | { | |
417 | struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; | |
418 | ||
419 | /* | |
420 | * We may need to allocate tree blocks and a leaf block but not the | |
421 | * root block | |
422 | */ | |
423 | return oinfo->dqi_gi.dqi_qtree_depth; | |
424 | } | |
425 | ||
426 | static int ocfs2_calc_global_qinit_credits(struct super_block *sb, int type) | |
427 | { | |
428 | /* We modify all the allocated blocks, tree root, and info block */ | |
429 | return (ocfs2_global_qinit_alloc(sb, type) + 2) * | |
430 | OCFS2_QUOTA_BLOCK_UPDATE_CREDITS; | |
431 | } | |
432 | ||
9e33d69f JK |
433 | /* Read in information from global quota file and acquire a reference to it. |
434 | * dquot_acquire() has already started the transaction and locked quota file */ | |
435 | int ocfs2_global_read_dquot(struct dquot *dquot) | |
436 | { | |
437 | int err, err2, ex = 0; | |
b409d7a0 JK |
438 | struct super_block *sb = dquot->dq_sb; |
439 | int type = dquot->dq_type; | |
440 | struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv; | |
441 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
442 | struct inode *gqinode = info->dqi_gqinode; | |
443 | int need_alloc = ocfs2_global_qinit_alloc(sb, type); | |
444 | handle_t *handle = NULL; | |
9e33d69f JK |
445 | |
446 | err = ocfs2_qinfo_lock(info, 0); | |
447 | if (err < 0) | |
448 | goto out; | |
449 | err = qtree_read_dquot(&info->dqi_gi, dquot); | |
450 | if (err < 0) | |
451 | goto out_qlock; | |
452 | OCFS2_DQUOT(dquot)->dq_use_count++; | |
453 | OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace; | |
454 | OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes; | |
b409d7a0 JK |
455 | ocfs2_qinfo_unlock(info, 0); |
456 | ||
9e33d69f | 457 | if (!dquot->dq_off) { /* No real quota entry? */ |
9e33d69f | 458 | ex = 1; |
b409d7a0 JK |
459 | /* |
460 | * Add blocks to quota file before we start a transaction since | |
461 | * locking allocators ranks above a transaction start | |
462 | */ | |
463 | WARN_ON(journal_current_handle()); | |
464 | down_write(&OCFS2_I(gqinode)->ip_alloc_sem); | |
465 | err = ocfs2_extend_no_holes(gqinode, | |
466 | gqinode->i_size + (need_alloc << sb->s_blocksize_bits), | |
467 | gqinode->i_size); | |
468 | up_write(&OCFS2_I(gqinode)->ip_alloc_sem); | |
469 | if (err < 0) | |
470 | goto out; | |
9e33d69f | 471 | } |
b409d7a0 JK |
472 | |
473 | handle = ocfs2_start_trans(osb, | |
474 | ocfs2_calc_global_qinit_credits(sb, type)); | |
475 | if (IS_ERR(handle)) { | |
476 | err = PTR_ERR(handle); | |
477 | goto out; | |
478 | } | |
479 | err = ocfs2_qinfo_lock(info, ex); | |
480 | if (err < 0) | |
481 | goto out_trans; | |
9e33d69f JK |
482 | err = qtree_write_dquot(&info->dqi_gi, dquot); |
483 | if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) { | |
484 | err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type); | |
485 | if (!err) | |
486 | err = err2; | |
487 | } | |
488 | out_qlock: | |
489 | if (ex) | |
490 | ocfs2_qinfo_unlock(info, 1); | |
4e8a3019 JK |
491 | else |
492 | ocfs2_qinfo_unlock(info, 0); | |
b409d7a0 JK |
493 | out_trans: |
494 | if (handle) | |
495 | ocfs2_commit_trans(osb, handle); | |
9e33d69f JK |
496 | out: |
497 | if (err < 0) | |
498 | mlog_errno(err); | |
499 | return err; | |
500 | } | |
501 | ||
502 | /* Sync local information about quota modifications with global quota file. | |
503 | * Caller must have started the transaction and obtained exclusive lock for | |
504 | * global quota file inode */ | |
505 | int __ocfs2_sync_dquot(struct dquot *dquot, int freeing) | |
506 | { | |
507 | int err, err2; | |
508 | struct super_block *sb = dquot->dq_sb; | |
509 | int type = dquot->dq_type; | |
510 | struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv; | |
511 | struct ocfs2_global_disk_dqblk dqblk; | |
512 | s64 spacechange, inodechange; | |
513 | time_t olditime, oldbtime; | |
514 | ||
515 | err = sb->s_op->quota_read(sb, type, (char *)&dqblk, | |
516 | sizeof(struct ocfs2_global_disk_dqblk), | |
517 | dquot->dq_off); | |
518 | if (err != sizeof(struct ocfs2_global_disk_dqblk)) { | |
519 | if (err >= 0) { | |
520 | mlog(ML_ERROR, "Short read from global quota file " | |
521 | "(%u read)\n", err); | |
522 | err = -EIO; | |
523 | } | |
524 | goto out; | |
525 | } | |
526 | ||
527 | /* Update space and inode usage. Get also other information from | |
528 | * global quota file so that we don't overwrite any changes there. | |
529 | * We are */ | |
530 | spin_lock(&dq_data_lock); | |
531 | spacechange = dquot->dq_dqb.dqb_curspace - | |
532 | OCFS2_DQUOT(dquot)->dq_origspace; | |
533 | inodechange = dquot->dq_dqb.dqb_curinodes - | |
534 | OCFS2_DQUOT(dquot)->dq_originodes; | |
535 | olditime = dquot->dq_dqb.dqb_itime; | |
536 | oldbtime = dquot->dq_dqb.dqb_btime; | |
537 | ocfs2_global_disk2memdqb(dquot, &dqblk); | |
9a2f3866 JK |
538 | mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n", |
539 | dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange, | |
540 | dquot->dq_dqb.dqb_curinodes, (long long)inodechange); | |
9e33d69f JK |
541 | if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags)) |
542 | dquot->dq_dqb.dqb_curspace += spacechange; | |
543 | if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags)) | |
544 | dquot->dq_dqb.dqb_curinodes += inodechange; | |
545 | /* Set properly space grace time... */ | |
546 | if (dquot->dq_dqb.dqb_bsoftlimit && | |
547 | dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) { | |
548 | if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) && | |
549 | oldbtime > 0) { | |
550 | if (dquot->dq_dqb.dqb_btime > 0) | |
551 | dquot->dq_dqb.dqb_btime = | |
552 | min(dquot->dq_dqb.dqb_btime, oldbtime); | |
553 | else | |
554 | dquot->dq_dqb.dqb_btime = oldbtime; | |
555 | } | |
556 | } else { | |
557 | dquot->dq_dqb.dqb_btime = 0; | |
558 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); | |
559 | } | |
560 | /* Set properly inode grace time... */ | |
561 | if (dquot->dq_dqb.dqb_isoftlimit && | |
562 | dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) { | |
563 | if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) && | |
564 | olditime > 0) { | |
565 | if (dquot->dq_dqb.dqb_itime > 0) | |
566 | dquot->dq_dqb.dqb_itime = | |
567 | min(dquot->dq_dqb.dqb_itime, olditime); | |
568 | else | |
569 | dquot->dq_dqb.dqb_itime = olditime; | |
570 | } | |
571 | } else { | |
572 | dquot->dq_dqb.dqb_itime = 0; | |
573 | clear_bit(DQ_INODES_B, &dquot->dq_flags); | |
574 | } | |
575 | /* All information is properly updated, clear the flags */ | |
576 | __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags); | |
577 | __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags); | |
578 | __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags); | |
579 | __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags); | |
580 | __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags); | |
581 | __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags); | |
582 | OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace; | |
583 | OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes; | |
584 | spin_unlock(&dq_data_lock); | |
585 | err = ocfs2_qinfo_lock(info, freeing); | |
586 | if (err < 0) { | |
587 | mlog(ML_ERROR, "Failed to lock quota info, loosing quota write" | |
588 | " (type=%d, id=%u)\n", dquot->dq_type, | |
589 | (unsigned)dquot->dq_id); | |
590 | goto out; | |
591 | } | |
592 | if (freeing) | |
593 | OCFS2_DQUOT(dquot)->dq_use_count--; | |
594 | err = qtree_write_dquot(&info->dqi_gi, dquot); | |
595 | if (err < 0) | |
596 | goto out_qlock; | |
597 | if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) { | |
598 | err = qtree_release_dquot(&info->dqi_gi, dquot); | |
599 | if (info_dirty(sb_dqinfo(sb, type))) { | |
600 | err2 = __ocfs2_global_write_info(sb, type); | |
601 | if (!err) | |
602 | err = err2; | |
603 | } | |
604 | } | |
605 | out_qlock: | |
606 | ocfs2_qinfo_unlock(info, freeing); | |
607 | out: | |
608 | if (err < 0) | |
609 | mlog_errno(err); | |
610 | return err; | |
611 | } | |
612 | ||
171bf93c MF |
613 | /* |
614 | * Functions for periodic syncing of dquots with global file | |
615 | */ | |
616 | static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type) | |
617 | { | |
618 | handle_t *handle; | |
619 | struct super_block *sb = dquot->dq_sb; | |
620 | struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; | |
621 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
622 | int status = 0; | |
623 | ||
624 | mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id, | |
625 | dquot->dq_type, type, sb->s_id); | |
626 | if (type != dquot->dq_type) | |
627 | goto out; | |
628 | status = ocfs2_lock_global_qf(oinfo, 1); | |
629 | if (status < 0) | |
630 | goto out; | |
631 | ||
632 | handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS); | |
633 | if (IS_ERR(handle)) { | |
634 | status = PTR_ERR(handle); | |
635 | mlog_errno(status); | |
636 | goto out_ilock; | |
637 | } | |
638 | mutex_lock(&sb_dqopt(sb)->dqio_mutex); | |
639 | status = ocfs2_sync_dquot(dquot); | |
640 | mutex_unlock(&sb_dqopt(sb)->dqio_mutex); | |
641 | if (status < 0) | |
642 | mlog_errno(status); | |
643 | /* We have to write local structure as well... */ | |
644 | dquot_mark_dquot_dirty(dquot); | |
645 | status = dquot_commit(dquot); | |
646 | if (status < 0) | |
647 | mlog_errno(status); | |
648 | ocfs2_commit_trans(osb, handle); | |
649 | out_ilock: | |
650 | ocfs2_unlock_global_qf(oinfo, 1); | |
651 | out: | |
652 | mlog_exit(status); | |
653 | return status; | |
654 | } | |
655 | ||
656 | static void qsync_work_fn(struct work_struct *work) | |
657 | { | |
658 | struct ocfs2_mem_dqinfo *oinfo = container_of(work, | |
659 | struct ocfs2_mem_dqinfo, | |
660 | dqi_sync_work.work); | |
661 | struct super_block *sb = oinfo->dqi_gqinode->i_sb; | |
662 | ||
663 | dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type); | |
664 | queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work, | |
4539f1df | 665 | msecs_to_jiffies(oinfo->dqi_syncms)); |
171bf93c MF |
666 | } |
667 | ||
9e33d69f JK |
668 | /* |
669 | * Wrappers for generic quota functions | |
670 | */ | |
671 | ||
672 | static int ocfs2_write_dquot(struct dquot *dquot) | |
673 | { | |
674 | handle_t *handle; | |
675 | struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb); | |
676 | int status = 0; | |
677 | ||
678 | mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type); | |
679 | ||
680 | handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS); | |
681 | if (IS_ERR(handle)) { | |
682 | status = PTR_ERR(handle); | |
683 | mlog_errno(status); | |
684 | goto out; | |
685 | } | |
686 | status = dquot_commit(dquot); | |
687 | ocfs2_commit_trans(osb, handle); | |
688 | out: | |
689 | mlog_exit(status); | |
690 | return status; | |
691 | } | |
692 | ||
b409d7a0 | 693 | static int ocfs2_calc_qdel_credits(struct super_block *sb, int type) |
9e33d69f | 694 | { |
b409d7a0 | 695 | struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; |
0584974a JK |
696 | /* |
697 | * We modify tree, leaf block, global info, local chunk header, | |
698 | * global and local inode; OCFS2_QINFO_WRITE_CREDITS already | |
699 | * accounts for inode update | |
700 | */ | |
b409d7a0 JK |
701 | return (oinfo->dqi_gi.dqi_qtree_depth + 2) * |
702 | OCFS2_QUOTA_BLOCK_UPDATE_CREDITS + | |
0584974a | 703 | OCFS2_QINFO_WRITE_CREDITS + |
0584974a | 704 | OCFS2_INODE_UPDATE_CREDITS; |
9e33d69f JK |
705 | } |
706 | ||
707 | static int ocfs2_release_dquot(struct dquot *dquot) | |
708 | { | |
709 | handle_t *handle; | |
710 | struct ocfs2_mem_dqinfo *oinfo = | |
711 | sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; | |
712 | struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb); | |
713 | int status = 0; | |
714 | ||
715 | mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type); | |
716 | ||
717 | status = ocfs2_lock_global_qf(oinfo, 1); | |
718 | if (status < 0) | |
719 | goto out; | |
720 | handle = ocfs2_start_trans(osb, | |
721 | ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type)); | |
722 | if (IS_ERR(handle)) { | |
723 | status = PTR_ERR(handle); | |
724 | mlog_errno(status); | |
725 | goto out_ilock; | |
726 | } | |
727 | status = dquot_release(dquot); | |
728 | ocfs2_commit_trans(osb, handle); | |
729 | out_ilock: | |
730 | ocfs2_unlock_global_qf(oinfo, 1); | |
731 | out: | |
732 | mlog_exit(status); | |
733 | return status; | |
734 | } | |
735 | ||
9e33d69f JK |
736 | static int ocfs2_acquire_dquot(struct dquot *dquot) |
737 | { | |
9e33d69f JK |
738 | struct ocfs2_mem_dqinfo *oinfo = |
739 | sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; | |
9e33d69f JK |
740 | int status = 0; |
741 | ||
742 | mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type); | |
743 | /* We need an exclusive lock, because we're going to update use count | |
744 | * and instantiate possibly new dquot structure */ | |
745 | status = ocfs2_lock_global_qf(oinfo, 1); | |
746 | if (status < 0) | |
747 | goto out; | |
9e33d69f | 748 | status = dquot_acquire(dquot); |
9e33d69f JK |
749 | ocfs2_unlock_global_qf(oinfo, 1); |
750 | out: | |
751 | mlog_exit(status); | |
752 | return status; | |
753 | } | |
754 | ||
755 | static int ocfs2_mark_dquot_dirty(struct dquot *dquot) | |
756 | { | |
757 | unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) | | |
758 | (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) | | |
759 | (1 << (DQ_LASTSET_B + QIF_INODES_B)) | | |
760 | (1 << (DQ_LASTSET_B + QIF_SPACE_B)) | | |
761 | (1 << (DQ_LASTSET_B + QIF_BTIME_B)) | | |
762 | (1 << (DQ_LASTSET_B + QIF_ITIME_B)); | |
763 | int sync = 0; | |
764 | int status; | |
765 | struct super_block *sb = dquot->dq_sb; | |
766 | int type = dquot->dq_type; | |
767 | struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; | |
768 | handle_t *handle; | |
769 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
770 | ||
771 | mlog_entry("id=%u, type=%d", dquot->dq_id, type); | |
772 | dquot_mark_dquot_dirty(dquot); | |
773 | ||
774 | /* In case user set some limits, sync dquot immediately to global | |
775 | * quota file so that information propagates quicker */ | |
776 | spin_lock(&dq_data_lock); | |
777 | if (dquot->dq_flags & mask) | |
778 | sync = 1; | |
779 | spin_unlock(&dq_data_lock); | |
f8afead7 JK |
780 | /* This is a slight hack but we can't afford getting global quota |
781 | * lock if we already have a transaction started. */ | |
782 | if (!sync || journal_current_handle()) { | |
9e33d69f JK |
783 | status = ocfs2_write_dquot(dquot); |
784 | goto out; | |
785 | } | |
786 | status = ocfs2_lock_global_qf(oinfo, 1); | |
787 | if (status < 0) | |
788 | goto out; | |
789 | handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS); | |
790 | if (IS_ERR(handle)) { | |
791 | status = PTR_ERR(handle); | |
792 | mlog_errno(status); | |
793 | goto out_ilock; | |
794 | } | |
795 | status = ocfs2_sync_dquot(dquot); | |
796 | if (status < 0) { | |
797 | mlog_errno(status); | |
798 | goto out_trans; | |
799 | } | |
800 | /* Now write updated local dquot structure */ | |
801 | status = dquot_commit(dquot); | |
802 | out_trans: | |
803 | ocfs2_commit_trans(osb, handle); | |
804 | out_ilock: | |
805 | ocfs2_unlock_global_qf(oinfo, 1); | |
806 | out: | |
807 | mlog_exit(status); | |
808 | return status; | |
809 | } | |
810 | ||
811 | /* This should happen only after set_dqinfo(). */ | |
812 | static int ocfs2_write_info(struct super_block *sb, int type) | |
813 | { | |
814 | handle_t *handle; | |
815 | int status = 0; | |
816 | struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; | |
817 | ||
818 | mlog_entry_void(); | |
819 | ||
820 | status = ocfs2_lock_global_qf(oinfo, 1); | |
821 | if (status < 0) | |
822 | goto out; | |
823 | handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS); | |
824 | if (IS_ERR(handle)) { | |
825 | status = PTR_ERR(handle); | |
826 | mlog_errno(status); | |
827 | goto out_ilock; | |
828 | } | |
829 | status = dquot_commit_info(sb, type); | |
830 | ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
831 | out_ilock: | |
832 | ocfs2_unlock_global_qf(oinfo, 1); | |
833 | out: | |
834 | mlog_exit(status); | |
835 | return status; | |
836 | } | |
837 | ||
9e33d69f JK |
838 | static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type) |
839 | { | |
840 | struct ocfs2_dquot *dquot = | |
841 | kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS); | |
842 | ||
843 | if (!dquot) | |
844 | return NULL; | |
845 | return &dquot->dq_dquot; | |
846 | } | |
847 | ||
848 | static void ocfs2_destroy_dquot(struct dquot *dquot) | |
849 | { | |
850 | kmem_cache_free(ocfs2_dquot_cachep, dquot); | |
851 | } | |
852 | ||
61e225dc | 853 | const struct dquot_operations ocfs2_quota_operations = { |
c475146d JK |
854 | .initialize = dquot_initialize, |
855 | .drop = dquot_drop, | |
9e33d69f | 856 | .alloc_inode = dquot_alloc_inode, |
9e33d69f JK |
857 | .free_inode = dquot_free_inode, |
858 | .transfer = dquot_transfer, | |
859 | .write_dquot = ocfs2_write_dquot, | |
860 | .acquire_dquot = ocfs2_acquire_dquot, | |
861 | .release_dquot = ocfs2_release_dquot, | |
862 | .mark_dirty = ocfs2_mark_dquot_dirty, | |
863 | .write_info = ocfs2_write_info, | |
864 | .alloc_dquot = ocfs2_alloc_dquot, | |
865 | .destroy_dquot = ocfs2_destroy_dquot, | |
866 | }; | |
171bf93c MF |
867 | |
868 | int ocfs2_quota_setup(void) | |
869 | { | |
870 | ocfs2_quota_wq = create_workqueue("o2quot"); | |
871 | if (!ocfs2_quota_wq) | |
872 | return -ENOMEM; | |
873 | return 0; | |
874 | } | |
875 | ||
876 | void ocfs2_quota_shutdown(void) | |
877 | { | |
878 | if (ocfs2_quota_wq) { | |
879 | flush_workqueue(ocfs2_quota_wq); | |
880 | destroy_workqueue(ocfs2_quota_wq); | |
881 | ocfs2_quota_wq = NULL; | |
882 | } | |
883 | } |