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