]>
Commit | Line | Data |
---|---|---|
9e33d69f JK |
1 | /* |
2 | * Implementation of operations over local quota file | |
3 | */ | |
4 | ||
5 | #include <linux/fs.h> | |
5a0e3ad6 | 6 | #include <linux/slab.h> |
9e33d69f JK |
7 | #include <linux/quota.h> |
8 | #include <linux/quotaops.h> | |
9 | #include <linux/module.h> | |
10 | ||
11 | #define MLOG_MASK_PREFIX ML_QUOTA | |
12 | #include <cluster/masklog.h> | |
13 | ||
14 | #include "ocfs2_fs.h" | |
15 | #include "ocfs2.h" | |
16 | #include "inode.h" | |
17 | #include "alloc.h" | |
18 | #include "file.h" | |
19 | #include "buffer_head_io.h" | |
20 | #include "journal.h" | |
21 | #include "sysfile.h" | |
22 | #include "dlmglue.h" | |
23 | #include "quota.h" | |
4b3fa190 | 24 | #include "uptodate.h" |
9e33d69f JK |
25 | |
26 | /* Number of local quota structures per block */ | |
27 | static inline unsigned int ol_quota_entries_per_block(struct super_block *sb) | |
28 | { | |
29 | return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) / | |
30 | sizeof(struct ocfs2_local_disk_dqblk)); | |
31 | } | |
32 | ||
33 | /* Number of blocks with entries in one chunk */ | |
34 | static inline unsigned int ol_chunk_blocks(struct super_block *sb) | |
35 | { | |
36 | return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) - | |
37 | OCFS2_QBLK_RESERVED_SPACE) << 3) / | |
38 | ol_quota_entries_per_block(sb); | |
39 | } | |
40 | ||
41 | /* Number of entries in a chunk bitmap */ | |
42 | static unsigned int ol_chunk_entries(struct super_block *sb) | |
43 | { | |
44 | return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb); | |
45 | } | |
46 | ||
47 | /* Offset of the chunk in quota file */ | |
48 | static unsigned int ol_quota_chunk_block(struct super_block *sb, int c) | |
49 | { | |
50 | /* 1 block for local quota file info, 1 block per chunk for chunk info */ | |
51 | return 1 + (ol_chunk_blocks(sb) + 1) * c; | |
52 | } | |
53 | ||
2205363d JK |
54 | static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off) |
55 | { | |
56 | int epb = ol_quota_entries_per_block(sb); | |
57 | ||
58 | return ol_quota_chunk_block(sb, c) + 1 + off / epb; | |
59 | } | |
60 | ||
61 | static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off) | |
9e33d69f JK |
62 | { |
63 | int epb = ol_quota_entries_per_block(sb); | |
64 | ||
2205363d JK |
65 | return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk); |
66 | } | |
67 | ||
68 | /* Offset of the dquot structure in the quota file */ | |
69 | static loff_t ol_dqblk_off(struct super_block *sb, int c, int off) | |
70 | { | |
71 | return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) + | |
72 | ol_dqblk_block_off(sb, c, off); | |
9e33d69f JK |
73 | } |
74 | ||
75 | /* Compute block number from given offset */ | |
76 | static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off) | |
77 | { | |
78 | return off >> sb->s_blocksize_bits; | |
79 | } | |
80 | ||
81 | static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off) | |
82 | { | |
83 | return off & ((1 << sb->s_blocksize_bits) - 1); | |
84 | } | |
85 | ||
86 | /* Compute offset in the chunk of a structure with the given offset */ | |
87 | static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off) | |
88 | { | |
89 | int epb = ol_quota_entries_per_block(sb); | |
90 | ||
91 | return ((off >> sb->s_blocksize_bits) - | |
92 | ol_quota_chunk_block(sb, c) - 1) * epb | |
93 | + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) / | |
94 | sizeof(struct ocfs2_local_disk_dqblk); | |
95 | } | |
96 | ||
97 | /* Write bufferhead into the fs */ | |
98 | static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh, | |
99 | void (*modify)(struct buffer_head *, void *), void *private) | |
100 | { | |
101 | struct super_block *sb = inode->i_sb; | |
102 | handle_t *handle; | |
103 | int status; | |
104 | ||
0584974a JK |
105 | handle = ocfs2_start_trans(OCFS2_SB(sb), |
106 | OCFS2_QUOTA_BLOCK_UPDATE_CREDITS); | |
9e33d69f JK |
107 | if (IS_ERR(handle)) { |
108 | status = PTR_ERR(handle); | |
109 | mlog_errno(status); | |
110 | return status; | |
111 | } | |
0cf2f763 | 112 | status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh, |
13723d00 | 113 | OCFS2_JOURNAL_ACCESS_WRITE); |
9e33d69f JK |
114 | if (status < 0) { |
115 | mlog_errno(status); | |
116 | ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
117 | return status; | |
118 | } | |
119 | lock_buffer(bh); | |
120 | modify(bh, private); | |
121 | unlock_buffer(bh); | |
122 | status = ocfs2_journal_dirty(handle, bh); | |
123 | if (status < 0) { | |
124 | mlog_errno(status); | |
125 | ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
126 | return status; | |
127 | } | |
128 | status = ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
129 | if (status < 0) { | |
130 | mlog_errno(status); | |
131 | return status; | |
132 | } | |
133 | return 0; | |
134 | } | |
135 | ||
136 | /* Check whether we understand format of quota files */ | |
137 | static int ocfs2_local_check_quota_file(struct super_block *sb, int type) | |
138 | { | |
139 | unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS; | |
140 | unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS; | |
141 | unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS; | |
142 | unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS; | |
143 | unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE, | |
144 | GROUP_QUOTA_SYSTEM_INODE }; | |
85eb8b73 | 145 | struct buffer_head *bh = NULL; |
9e33d69f JK |
146 | struct inode *linode = sb_dqopt(sb)->files[type]; |
147 | struct inode *ginode = NULL; | |
148 | struct ocfs2_disk_dqheader *dqhead; | |
149 | int status, ret = 0; | |
150 | ||
151 | /* First check whether we understand local quota file */ | |
85eb8b73 JB |
152 | status = ocfs2_read_quota_block(linode, 0, &bh); |
153 | if (status) { | |
9e33d69f JK |
154 | mlog_errno(status); |
155 | mlog(ML_ERROR, "failed to read quota file header (type=%d)\n", | |
156 | type); | |
157 | goto out_err; | |
158 | } | |
159 | dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data); | |
160 | if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) { | |
161 | mlog(ML_ERROR, "quota file magic does not match (%u != %u)," | |
162 | " type=%d\n", le32_to_cpu(dqhead->dqh_magic), | |
163 | lmagics[type], type); | |
164 | goto out_err; | |
165 | } | |
166 | if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) { | |
167 | mlog(ML_ERROR, "quota file version does not match (%u != %u)," | |
168 | " type=%d\n", le32_to_cpu(dqhead->dqh_version), | |
169 | lversions[type], type); | |
170 | goto out_err; | |
171 | } | |
172 | brelse(bh); | |
173 | bh = NULL; | |
174 | ||
175 | /* Next check whether we understand global quota file */ | |
176 | ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type], | |
177 | OCFS2_INVALID_SLOT); | |
178 | if (!ginode) { | |
179 | mlog(ML_ERROR, "cannot get global quota file inode " | |
180 | "(type=%d)\n", type); | |
181 | goto out_err; | |
182 | } | |
183 | /* Since the header is read only, we don't care about locking */ | |
85eb8b73 JB |
184 | status = ocfs2_read_quota_block(ginode, 0, &bh); |
185 | if (status) { | |
9e33d69f JK |
186 | mlog_errno(status); |
187 | mlog(ML_ERROR, "failed to read global quota file header " | |
188 | "(type=%d)\n", type); | |
189 | goto out_err; | |
190 | } | |
191 | dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data); | |
192 | if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) { | |
193 | mlog(ML_ERROR, "global quota file magic does not match " | |
194 | "(%u != %u), type=%d\n", | |
195 | le32_to_cpu(dqhead->dqh_magic), gmagics[type], type); | |
196 | goto out_err; | |
197 | } | |
198 | if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) { | |
199 | mlog(ML_ERROR, "global quota file version does not match " | |
200 | "(%u != %u), type=%d\n", | |
201 | le32_to_cpu(dqhead->dqh_version), gversions[type], | |
202 | type); | |
203 | goto out_err; | |
204 | } | |
205 | ||
206 | ret = 1; | |
207 | out_err: | |
208 | brelse(bh); | |
209 | iput(ginode); | |
210 | return ret; | |
211 | } | |
212 | ||
213 | /* Release given list of quota file chunks */ | |
214 | static void ocfs2_release_local_quota_bitmaps(struct list_head *head) | |
215 | { | |
216 | struct ocfs2_quota_chunk *pos, *next; | |
217 | ||
218 | list_for_each_entry_safe(pos, next, head, qc_chunk) { | |
219 | list_del(&pos->qc_chunk); | |
220 | brelse(pos->qc_headerbh); | |
221 | kmem_cache_free(ocfs2_qf_chunk_cachep, pos); | |
222 | } | |
223 | } | |
224 | ||
225 | /* Load quota bitmaps into memory */ | |
226 | static int ocfs2_load_local_quota_bitmaps(struct inode *inode, | |
227 | struct ocfs2_local_disk_dqinfo *ldinfo, | |
228 | struct list_head *head) | |
229 | { | |
230 | struct ocfs2_quota_chunk *newchunk; | |
231 | int i, status; | |
232 | ||
233 | INIT_LIST_HEAD(head); | |
234 | for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) { | |
235 | newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS); | |
236 | if (!newchunk) { | |
237 | ocfs2_release_local_quota_bitmaps(head); | |
238 | return -ENOMEM; | |
239 | } | |
240 | newchunk->qc_num = i; | |
85eb8b73 JB |
241 | newchunk->qc_headerbh = NULL; |
242 | status = ocfs2_read_quota_block(inode, | |
9e33d69f | 243 | ol_quota_chunk_block(inode->i_sb, i), |
85eb8b73 JB |
244 | &newchunk->qc_headerbh); |
245 | if (status) { | |
9e33d69f JK |
246 | mlog_errno(status); |
247 | kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk); | |
248 | ocfs2_release_local_quota_bitmaps(head); | |
249 | return status; | |
250 | } | |
251 | list_add_tail(&newchunk->qc_chunk, head); | |
252 | } | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static void olq_update_info(struct buffer_head *bh, void *private) | |
257 | { | |
258 | struct mem_dqinfo *info = private; | |
259 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
260 | struct ocfs2_local_disk_dqinfo *ldinfo; | |
261 | ||
262 | ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data + | |
263 | OCFS2_LOCAL_INFO_OFF); | |
264 | spin_lock(&dq_data_lock); | |
265 | ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK); | |
266 | ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks); | |
267 | ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks); | |
268 | spin_unlock(&dq_data_lock); | |
269 | } | |
270 | ||
2205363d JK |
271 | static int ocfs2_add_recovery_chunk(struct super_block *sb, |
272 | struct ocfs2_local_disk_chunk *dchunk, | |
273 | int chunk, | |
274 | struct list_head *head) | |
275 | { | |
276 | struct ocfs2_recovery_chunk *rc; | |
277 | ||
278 | rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS); | |
279 | if (!rc) | |
280 | return -ENOMEM; | |
281 | rc->rc_chunk = chunk; | |
282 | rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS); | |
283 | if (!rc->rc_bitmap) { | |
284 | kfree(rc); | |
285 | return -ENOMEM; | |
286 | } | |
287 | memcpy(rc->rc_bitmap, dchunk->dqc_bitmap, | |
288 | (ol_chunk_entries(sb) + 7) >> 3); | |
289 | list_add_tail(&rc->rc_list, head); | |
290 | return 0; | |
291 | } | |
292 | ||
293 | static void free_recovery_list(struct list_head *head) | |
294 | { | |
295 | struct ocfs2_recovery_chunk *next; | |
296 | struct ocfs2_recovery_chunk *rchunk; | |
297 | ||
298 | list_for_each_entry_safe(rchunk, next, head, rc_list) { | |
299 | list_del(&rchunk->rc_list); | |
300 | kfree(rchunk->rc_bitmap); | |
301 | kfree(rchunk); | |
302 | } | |
303 | } | |
304 | ||
305 | void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec) | |
306 | { | |
307 | int type; | |
308 | ||
309 | for (type = 0; type < MAXQUOTAS; type++) | |
310 | free_recovery_list(&(rec->r_list[type])); | |
311 | kfree(rec); | |
312 | } | |
313 | ||
314 | /* Load entries in our quota file we have to recover*/ | |
315 | static int ocfs2_recovery_load_quota(struct inode *lqinode, | |
316 | struct ocfs2_local_disk_dqinfo *ldinfo, | |
317 | int type, | |
318 | struct list_head *head) | |
319 | { | |
320 | struct super_block *sb = lqinode->i_sb; | |
321 | struct buffer_head *hbh; | |
322 | struct ocfs2_local_disk_chunk *dchunk; | |
323 | int i, chunks = le32_to_cpu(ldinfo->dqi_chunks); | |
324 | int status = 0; | |
325 | ||
326 | for (i = 0; i < chunks; i++) { | |
85eb8b73 JB |
327 | hbh = NULL; |
328 | status = ocfs2_read_quota_block(lqinode, | |
329 | ol_quota_chunk_block(sb, i), | |
330 | &hbh); | |
331 | if (status) { | |
2205363d JK |
332 | mlog_errno(status); |
333 | break; | |
334 | } | |
335 | dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data; | |
336 | if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb)) | |
337 | status = ocfs2_add_recovery_chunk(sb, dchunk, i, head); | |
338 | brelse(hbh); | |
339 | if (status < 0) | |
340 | break; | |
341 | } | |
342 | if (status < 0) | |
343 | free_recovery_list(head); | |
344 | return status; | |
345 | } | |
346 | ||
347 | static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void) | |
348 | { | |
349 | int type; | |
350 | struct ocfs2_quota_recovery *rec; | |
351 | ||
352 | rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS); | |
353 | if (!rec) | |
354 | return NULL; | |
355 | for (type = 0; type < MAXQUOTAS; type++) | |
356 | INIT_LIST_HEAD(&(rec->r_list[type])); | |
357 | return rec; | |
358 | } | |
359 | ||
360 | /* Load information we need for quota recovery into memory */ | |
361 | struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery( | |
362 | struct ocfs2_super *osb, | |
363 | int slot_num) | |
364 | { | |
365 | unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA, | |
366 | OCFS2_FEATURE_RO_COMPAT_GRPQUOTA}; | |
367 | unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE, | |
368 | LOCAL_GROUP_QUOTA_SYSTEM_INODE }; | |
369 | struct super_block *sb = osb->sb; | |
370 | struct ocfs2_local_disk_dqinfo *ldinfo; | |
371 | struct inode *lqinode; | |
372 | struct buffer_head *bh; | |
373 | int type; | |
374 | int status = 0; | |
375 | struct ocfs2_quota_recovery *rec; | |
376 | ||
377 | mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num); | |
378 | rec = ocfs2_alloc_quota_recovery(); | |
379 | if (!rec) | |
380 | return ERR_PTR(-ENOMEM); | |
381 | /* First init... */ | |
382 | ||
383 | for (type = 0; type < MAXQUOTAS; type++) { | |
384 | if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type])) | |
385 | continue; | |
386 | /* At this point, journal of the slot is already replayed so | |
387 | * we can trust metadata and data of the quota file */ | |
388 | lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num); | |
389 | if (!lqinode) { | |
390 | status = -ENOENT; | |
391 | goto out; | |
392 | } | |
393 | status = ocfs2_inode_lock_full(lqinode, NULL, 1, | |
394 | OCFS2_META_LOCK_RECOVERY); | |
395 | if (status < 0) { | |
396 | mlog_errno(status); | |
397 | goto out_put; | |
398 | } | |
399 | /* Now read local header */ | |
85eb8b73 JB |
400 | bh = NULL; |
401 | status = ocfs2_read_quota_block(lqinode, 0, &bh); | |
402 | if (status) { | |
2205363d JK |
403 | mlog_errno(status); |
404 | mlog(ML_ERROR, "failed to read quota file info header " | |
405 | "(slot=%d type=%d)\n", slot_num, type); | |
406 | goto out_lock; | |
407 | } | |
408 | ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data + | |
409 | OCFS2_LOCAL_INFO_OFF); | |
410 | status = ocfs2_recovery_load_quota(lqinode, ldinfo, type, | |
411 | &rec->r_list[type]); | |
412 | brelse(bh); | |
413 | out_lock: | |
414 | ocfs2_inode_unlock(lqinode, 1); | |
415 | out_put: | |
416 | iput(lqinode); | |
417 | if (status < 0) | |
418 | break; | |
419 | } | |
420 | out: | |
421 | if (status < 0) { | |
422 | ocfs2_free_quota_recovery(rec); | |
423 | rec = ERR_PTR(status); | |
424 | } | |
425 | return rec; | |
426 | } | |
427 | ||
428 | /* Sync changes in local quota file into global quota file and | |
429 | * reinitialize local quota file. | |
430 | * The function expects local quota file to be already locked and | |
431 | * dqonoff_mutex locked. */ | |
432 | static int ocfs2_recover_local_quota_file(struct inode *lqinode, | |
433 | int type, | |
434 | struct ocfs2_quota_recovery *rec) | |
435 | { | |
436 | struct super_block *sb = lqinode->i_sb; | |
437 | struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv; | |
438 | struct ocfs2_local_disk_chunk *dchunk; | |
439 | struct ocfs2_local_disk_dqblk *dqblk; | |
440 | struct dquot *dquot; | |
441 | handle_t *handle; | |
442 | struct buffer_head *hbh = NULL, *qbh = NULL; | |
443 | int status = 0; | |
444 | int bit, chunk; | |
445 | struct ocfs2_recovery_chunk *rchunk, *next; | |
446 | qsize_t spacechange, inodechange; | |
447 | ||
448 | mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type); | |
449 | ||
2205363d JK |
450 | list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) { |
451 | chunk = rchunk->rc_chunk; | |
85eb8b73 JB |
452 | hbh = NULL; |
453 | status = ocfs2_read_quota_block(lqinode, | |
454 | ol_quota_chunk_block(sb, chunk), | |
455 | &hbh); | |
456 | if (status) { | |
2205363d JK |
457 | mlog_errno(status); |
458 | break; | |
459 | } | |
460 | dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data; | |
984b3f57 | 461 | for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) { |
85eb8b73 JB |
462 | qbh = NULL; |
463 | status = ocfs2_read_quota_block(lqinode, | |
2205363d | 464 | ol_dqblk_block(sb, chunk, bit), |
85eb8b73 JB |
465 | &qbh); |
466 | if (status) { | |
2205363d JK |
467 | mlog_errno(status); |
468 | break; | |
469 | } | |
470 | dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data + | |
471 | ol_dqblk_block_off(sb, chunk, bit)); | |
472 | dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type); | |
473 | if (!dquot) { | |
474 | status = -EIO; | |
475 | mlog(ML_ERROR, "Failed to get quota structure " | |
476 | "for id %u, type %d. Cannot finish quota " | |
477 | "file recovery.\n", | |
478 | (unsigned)le64_to_cpu(dqblk->dqb_id), | |
479 | type); | |
480 | goto out_put_bh; | |
481 | } | |
80d73f15 JK |
482 | status = ocfs2_lock_global_qf(oinfo, 1); |
483 | if (status < 0) { | |
484 | mlog_errno(status); | |
485 | goto out_put_dquot; | |
486 | } | |
487 | ||
2205363d JK |
488 | handle = ocfs2_start_trans(OCFS2_SB(sb), |
489 | OCFS2_QSYNC_CREDITS); | |
490 | if (IS_ERR(handle)) { | |
491 | status = PTR_ERR(handle); | |
492 | mlog_errno(status); | |
80d73f15 | 493 | goto out_drop_lock; |
2205363d JK |
494 | } |
495 | mutex_lock(&sb_dqopt(sb)->dqio_mutex); | |
496 | spin_lock(&dq_data_lock); | |
497 | /* Add usage from quota entry into quota changes | |
498 | * of our node. Auxiliary variables are important | |
499 | * due to signedness */ | |
500 | spacechange = le64_to_cpu(dqblk->dqb_spacemod); | |
501 | inodechange = le64_to_cpu(dqblk->dqb_inodemod); | |
502 | dquot->dq_dqb.dqb_curspace += spacechange; | |
503 | dquot->dq_dqb.dqb_curinodes += inodechange; | |
504 | spin_unlock(&dq_data_lock); | |
505 | /* We want to drop reference held by the crashed | |
506 | * node. Since we have our own reference we know | |
507 | * global structure actually won't be freed. */ | |
508 | status = ocfs2_global_release_dquot(dquot); | |
509 | if (status < 0) { | |
510 | mlog_errno(status); | |
511 | goto out_commit; | |
512 | } | |
513 | /* Release local quota file entry */ | |
0cf2f763 JB |
514 | status = ocfs2_journal_access_dq(handle, |
515 | INODE_CACHE(lqinode), | |
2205363d JK |
516 | qbh, OCFS2_JOURNAL_ACCESS_WRITE); |
517 | if (status < 0) { | |
518 | mlog_errno(status); | |
519 | goto out_commit; | |
520 | } | |
521 | lock_buffer(qbh); | |
522 | WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap)); | |
523 | ocfs2_clear_bit(bit, dchunk->dqc_bitmap); | |
524 | le32_add_cpu(&dchunk->dqc_free, 1); | |
525 | unlock_buffer(qbh); | |
526 | status = ocfs2_journal_dirty(handle, qbh); | |
527 | if (status < 0) | |
528 | mlog_errno(status); | |
529 | out_commit: | |
530 | mutex_unlock(&sb_dqopt(sb)->dqio_mutex); | |
531 | ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
80d73f15 JK |
532 | out_drop_lock: |
533 | ocfs2_unlock_global_qf(oinfo, 1); | |
2205363d JK |
534 | out_put_dquot: |
535 | dqput(dquot); | |
536 | out_put_bh: | |
537 | brelse(qbh); | |
538 | if (status < 0) | |
539 | break; | |
540 | } | |
541 | brelse(hbh); | |
542 | list_del(&rchunk->rc_list); | |
543 | kfree(rchunk->rc_bitmap); | |
544 | kfree(rchunk); | |
545 | if (status < 0) | |
546 | break; | |
547 | } | |
2205363d JK |
548 | if (status < 0) |
549 | free_recovery_list(&(rec->r_list[type])); | |
550 | mlog_exit(status); | |
551 | return status; | |
552 | } | |
553 | ||
554 | /* Recover local quota files for given node different from us */ | |
555 | int ocfs2_finish_quota_recovery(struct ocfs2_super *osb, | |
556 | struct ocfs2_quota_recovery *rec, | |
557 | int slot_num) | |
558 | { | |
559 | unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE, | |
560 | LOCAL_GROUP_QUOTA_SYSTEM_INODE }; | |
561 | struct super_block *sb = osb->sb; | |
562 | struct ocfs2_local_disk_dqinfo *ldinfo; | |
563 | struct buffer_head *bh; | |
564 | handle_t *handle; | |
565 | int type; | |
566 | int status = 0; | |
567 | struct inode *lqinode; | |
568 | unsigned int flags; | |
569 | ||
570 | mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num); | |
571 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); | |
572 | for (type = 0; type < MAXQUOTAS; type++) { | |
573 | if (list_empty(&(rec->r_list[type]))) | |
574 | continue; | |
575 | mlog(0, "Recovering quota in slot %d\n", slot_num); | |
576 | lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num); | |
577 | if (!lqinode) { | |
578 | status = -ENOENT; | |
579 | goto out; | |
580 | } | |
581 | status = ocfs2_inode_lock_full(lqinode, NULL, 1, | |
582 | OCFS2_META_LOCK_NOQUEUE); | |
583 | /* Someone else is holding the lock? Then he must be | |
584 | * doing the recovery. Just skip the file... */ | |
585 | if (status == -EAGAIN) { | |
586 | mlog(ML_NOTICE, "skipping quota recovery for slot %d " | |
587 | "because quota file is locked.\n", slot_num); | |
588 | status = 0; | |
589 | goto out_put; | |
590 | } else if (status < 0) { | |
591 | mlog_errno(status); | |
592 | goto out_put; | |
593 | } | |
594 | /* Now read local header */ | |
85eb8b73 JB |
595 | bh = NULL; |
596 | status = ocfs2_read_quota_block(lqinode, 0, &bh); | |
597 | if (status) { | |
2205363d JK |
598 | mlog_errno(status); |
599 | mlog(ML_ERROR, "failed to read quota file info header " | |
600 | "(slot=%d type=%d)\n", slot_num, type); | |
601 | goto out_lock; | |
602 | } | |
603 | ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data + | |
604 | OCFS2_LOCAL_INFO_OFF); | |
605 | /* Is recovery still needed? */ | |
606 | flags = le32_to_cpu(ldinfo->dqi_flags); | |
607 | if (!(flags & OLQF_CLEAN)) | |
608 | status = ocfs2_recover_local_quota_file(lqinode, | |
609 | type, | |
610 | rec); | |
611 | /* We don't want to mark file as clean when it is actually | |
612 | * active */ | |
613 | if (slot_num == osb->slot_num) | |
614 | goto out_bh; | |
615 | /* Mark quota file as clean if we are recovering quota file of | |
616 | * some other node. */ | |
0584974a JK |
617 | handle = ocfs2_start_trans(osb, |
618 | OCFS2_LOCAL_QINFO_WRITE_CREDITS); | |
2205363d JK |
619 | if (IS_ERR(handle)) { |
620 | status = PTR_ERR(handle); | |
621 | mlog_errno(status); | |
622 | goto out_bh; | |
623 | } | |
0cf2f763 JB |
624 | status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), |
625 | bh, | |
13723d00 | 626 | OCFS2_JOURNAL_ACCESS_WRITE); |
2205363d JK |
627 | if (status < 0) { |
628 | mlog_errno(status); | |
629 | goto out_trans; | |
630 | } | |
631 | lock_buffer(bh); | |
632 | ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN); | |
633 | unlock_buffer(bh); | |
634 | status = ocfs2_journal_dirty(handle, bh); | |
635 | if (status < 0) | |
636 | mlog_errno(status); | |
637 | out_trans: | |
638 | ocfs2_commit_trans(osb, handle); | |
639 | out_bh: | |
640 | brelse(bh); | |
641 | out_lock: | |
642 | ocfs2_inode_unlock(lqinode, 1); | |
643 | out_put: | |
644 | iput(lqinode); | |
645 | if (status < 0) | |
646 | break; | |
647 | } | |
648 | out: | |
649 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); | |
650 | kfree(rec); | |
651 | return status; | |
652 | } | |
653 | ||
9e33d69f JK |
654 | /* Read information header from quota file */ |
655 | static int ocfs2_local_read_info(struct super_block *sb, int type) | |
656 | { | |
657 | struct ocfs2_local_disk_dqinfo *ldinfo; | |
658 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
659 | struct ocfs2_mem_dqinfo *oinfo; | |
660 | struct inode *lqinode = sb_dqopt(sb)->files[type]; | |
661 | int status; | |
662 | struct buffer_head *bh = NULL; | |
2205363d | 663 | struct ocfs2_quota_recovery *rec; |
9e33d69f JK |
664 | int locked = 0; |
665 | ||
b4c30de3 JK |
666 | /* We don't need the lock and we have to acquire quota file locks |
667 | * which will later depend on this lock */ | |
668 | mutex_unlock(&sb_dqopt(sb)->dqio_mutex); | |
9e33d69f JK |
669 | info->dqi_maxblimit = 0x7fffffffffffffffLL; |
670 | info->dqi_maxilimit = 0x7fffffffffffffffLL; | |
671 | oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS); | |
672 | if (!oinfo) { | |
673 | mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota" | |
674 | " info."); | |
675 | goto out_err; | |
676 | } | |
677 | info->dqi_priv = oinfo; | |
678 | oinfo->dqi_type = type; | |
679 | INIT_LIST_HEAD(&oinfo->dqi_chunk); | |
2205363d | 680 | oinfo->dqi_rec = NULL; |
9e33d69f JK |
681 | oinfo->dqi_lqi_bh = NULL; |
682 | oinfo->dqi_ibh = NULL; | |
683 | ||
684 | status = ocfs2_global_read_info(sb, type); | |
685 | if (status < 0) | |
686 | goto out_err; | |
687 | ||
688 | status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1); | |
689 | if (status < 0) { | |
690 | mlog_errno(status); | |
691 | goto out_err; | |
692 | } | |
693 | locked = 1; | |
694 | ||
695 | /* Now read local header */ | |
85eb8b73 JB |
696 | status = ocfs2_read_quota_block(lqinode, 0, &bh); |
697 | if (status) { | |
9e33d69f JK |
698 | mlog_errno(status); |
699 | mlog(ML_ERROR, "failed to read quota file info header " | |
700 | "(type=%d)\n", type); | |
701 | goto out_err; | |
702 | } | |
703 | ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data + | |
704 | OCFS2_LOCAL_INFO_OFF); | |
705 | info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags); | |
706 | oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks); | |
707 | oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks); | |
708 | oinfo->dqi_ibh = bh; | |
709 | ||
710 | /* We crashed when using local quota file? */ | |
2205363d JK |
711 | if (!(info->dqi_flags & OLQF_CLEAN)) { |
712 | rec = OCFS2_SB(sb)->quota_rec; | |
713 | if (!rec) { | |
714 | rec = ocfs2_alloc_quota_recovery(); | |
715 | if (!rec) { | |
716 | status = -ENOMEM; | |
717 | mlog_errno(status); | |
718 | goto out_err; | |
719 | } | |
720 | OCFS2_SB(sb)->quota_rec = rec; | |
721 | } | |
9e33d69f | 722 | |
2205363d JK |
723 | status = ocfs2_recovery_load_quota(lqinode, ldinfo, type, |
724 | &rec->r_list[type]); | |
725 | if (status < 0) { | |
726 | mlog_errno(status); | |
727 | goto out_err; | |
728 | } | |
729 | } | |
730 | ||
731 | status = ocfs2_load_local_quota_bitmaps(lqinode, | |
9e33d69f JK |
732 | ldinfo, |
733 | &oinfo->dqi_chunk); | |
734 | if (status < 0) { | |
735 | mlog_errno(status); | |
736 | goto out_err; | |
737 | } | |
738 | ||
739 | /* Now mark quota file as used */ | |
740 | info->dqi_flags &= ~OLQF_CLEAN; | |
741 | status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info); | |
742 | if (status < 0) { | |
743 | mlog_errno(status); | |
744 | goto out_err; | |
745 | } | |
746 | ||
b4c30de3 | 747 | mutex_lock(&sb_dqopt(sb)->dqio_mutex); |
9e33d69f JK |
748 | return 0; |
749 | out_err: | |
750 | if (oinfo) { | |
751 | iput(oinfo->dqi_gqinode); | |
752 | ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock); | |
753 | ocfs2_lock_res_free(&oinfo->dqi_gqlock); | |
754 | brelse(oinfo->dqi_lqi_bh); | |
755 | if (locked) | |
756 | ocfs2_inode_unlock(lqinode, 1); | |
757 | ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk); | |
758 | kfree(oinfo); | |
759 | } | |
760 | brelse(bh); | |
b4c30de3 | 761 | mutex_lock(&sb_dqopt(sb)->dqio_mutex); |
9e33d69f JK |
762 | return -1; |
763 | } | |
764 | ||
765 | /* Write local info to quota file */ | |
766 | static int ocfs2_local_write_info(struct super_block *sb, int type) | |
767 | { | |
768 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
769 | struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv) | |
770 | ->dqi_ibh; | |
771 | int status; | |
772 | ||
773 | status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info, | |
774 | info); | |
775 | if (status < 0) { | |
776 | mlog_errno(status); | |
777 | return -1; | |
778 | } | |
779 | ||
780 | return 0; | |
781 | } | |
782 | ||
783 | /* Release info from memory */ | |
784 | static int ocfs2_local_free_info(struct super_block *sb, int type) | |
785 | { | |
786 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
787 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
788 | struct ocfs2_quota_chunk *chunk; | |
789 | struct ocfs2_local_disk_chunk *dchunk; | |
790 | int mark_clean = 1, len; | |
791 | int status; | |
792 | ||
171bf93c MF |
793 | /* At this point we know there are no more dquots and thus |
794 | * even if there's some sync in the pdflush queue, it won't | |
795 | * find any dquots and return without doing anything */ | |
796 | cancel_delayed_work_sync(&oinfo->dqi_sync_work); | |
9e33d69f JK |
797 | iput(oinfo->dqi_gqinode); |
798 | ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock); | |
799 | ocfs2_lock_res_free(&oinfo->dqi_gqlock); | |
800 | list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) { | |
801 | dchunk = (struct ocfs2_local_disk_chunk *) | |
802 | (chunk->qc_headerbh->b_data); | |
803 | if (chunk->qc_num < oinfo->dqi_chunks - 1) { | |
804 | len = ol_chunk_entries(sb); | |
805 | } else { | |
806 | len = (oinfo->dqi_blocks - | |
807 | ol_quota_chunk_block(sb, chunk->qc_num) - 1) | |
808 | * ol_quota_entries_per_block(sb); | |
809 | } | |
810 | /* Not all entries free? Bug! */ | |
811 | if (le32_to_cpu(dchunk->dqc_free) != len) { | |
812 | mlog(ML_ERROR, "releasing quota file with used " | |
813 | "entries (type=%d)\n", type); | |
814 | mark_clean = 0; | |
815 | } | |
816 | } | |
817 | ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk); | |
818 | ||
2205363d JK |
819 | /* dqonoff_mutex protects us against racing with recovery thread... */ |
820 | if (oinfo->dqi_rec) { | |
821 | ocfs2_free_quota_recovery(oinfo->dqi_rec); | |
822 | mark_clean = 0; | |
823 | } | |
824 | ||
9e33d69f JK |
825 | if (!mark_clean) |
826 | goto out; | |
827 | ||
828 | /* Mark local file as clean */ | |
829 | info->dqi_flags |= OLQF_CLEAN; | |
830 | status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], | |
831 | oinfo->dqi_ibh, | |
832 | olq_update_info, | |
833 | info); | |
834 | if (status < 0) { | |
835 | mlog_errno(status); | |
836 | goto out; | |
837 | } | |
838 | ||
839 | out: | |
840 | ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1); | |
841 | brelse(oinfo->dqi_ibh); | |
842 | brelse(oinfo->dqi_lqi_bh); | |
843 | kfree(oinfo); | |
844 | return 0; | |
845 | } | |
846 | ||
847 | static void olq_set_dquot(struct buffer_head *bh, void *private) | |
848 | { | |
849 | struct ocfs2_dquot *od = private; | |
850 | struct ocfs2_local_disk_dqblk *dqblk; | |
851 | struct super_block *sb = od->dq_dquot.dq_sb; | |
852 | ||
853 | dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data | |
854 | + ol_dqblk_block_offset(sb, od->dq_local_off)); | |
855 | ||
856 | dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id); | |
857 | spin_lock(&dq_data_lock); | |
858 | dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace - | |
859 | od->dq_origspace); | |
860 | dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes - | |
861 | od->dq_originodes); | |
862 | spin_unlock(&dq_data_lock); | |
863 | mlog(0, "Writing local dquot %u space %lld inodes %lld\n", | |
9a2f3866 JK |
864 | od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod), |
865 | (long long)le64_to_cpu(dqblk->dqb_inodemod)); | |
9e33d69f JK |
866 | } |
867 | ||
868 | /* Write dquot to local quota file */ | |
869 | static int ocfs2_local_write_dquot(struct dquot *dquot) | |
870 | { | |
871 | struct super_block *sb = dquot->dq_sb; | |
872 | struct ocfs2_dquot *od = OCFS2_DQUOT(dquot); | |
85eb8b73 | 873 | struct buffer_head *bh = NULL; |
9e33d69f JK |
874 | int status; |
875 | ||
85eb8b73 | 876 | status = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type], |
9e33d69f | 877 | ol_dqblk_file_block(sb, od->dq_local_off), |
85eb8b73 JB |
878 | &bh); |
879 | if (status) { | |
9e33d69f JK |
880 | mlog_errno(status); |
881 | goto out; | |
882 | } | |
883 | status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh, | |
884 | olq_set_dquot, od); | |
885 | if (status < 0) { | |
886 | mlog_errno(status); | |
887 | goto out; | |
888 | } | |
889 | out: | |
890 | brelse(bh); | |
891 | return status; | |
892 | } | |
893 | ||
894 | /* Find free entry in local quota file */ | |
895 | static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb, | |
896 | int type, | |
897 | int *offset) | |
898 | { | |
899 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
900 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
901 | struct ocfs2_quota_chunk *chunk; | |
902 | struct ocfs2_local_disk_chunk *dchunk; | |
903 | int found = 0, len; | |
904 | ||
905 | list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) { | |
906 | dchunk = (struct ocfs2_local_disk_chunk *) | |
907 | chunk->qc_headerbh->b_data; | |
908 | if (le32_to_cpu(dchunk->dqc_free) > 0) { | |
909 | found = 1; | |
910 | break; | |
911 | } | |
912 | } | |
913 | if (!found) | |
914 | return NULL; | |
915 | ||
916 | if (chunk->qc_num < oinfo->dqi_chunks - 1) { | |
917 | len = ol_chunk_entries(sb); | |
918 | } else { | |
919 | len = (oinfo->dqi_blocks - | |
920 | ol_quota_chunk_block(sb, chunk->qc_num) - 1) | |
921 | * ol_quota_entries_per_block(sb); | |
922 | } | |
923 | ||
924 | found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0); | |
925 | /* We failed? */ | |
926 | if (found == len) { | |
927 | mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u" | |
928 | " entries free (type=%d)\n", chunk->qc_num, | |
929 | le32_to_cpu(dchunk->dqc_free), type); | |
930 | return ERR_PTR(-EIO); | |
931 | } | |
932 | *offset = found; | |
933 | return chunk; | |
934 | } | |
935 | ||
936 | /* Add new chunk to the local quota file */ | |
937 | static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk( | |
938 | struct super_block *sb, | |
939 | int type, | |
940 | int *offset) | |
941 | { | |
942 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
943 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
944 | struct inode *lqinode = sb_dqopt(sb)->files[type]; | |
945 | struct ocfs2_quota_chunk *chunk = NULL; | |
946 | struct ocfs2_local_disk_chunk *dchunk; | |
947 | int status; | |
948 | handle_t *handle; | |
0e7f387b | 949 | struct buffer_head *bh = NULL, *dbh = NULL; |
9e33d69f JK |
950 | u64 p_blkno; |
951 | ||
952 | /* We are protected by dqio_sem so no locking needed */ | |
953 | status = ocfs2_extend_no_holes(lqinode, | |
954 | lqinode->i_size + 2 * sb->s_blocksize, | |
955 | lqinode->i_size); | |
956 | if (status < 0) { | |
957 | mlog_errno(status); | |
958 | goto out; | |
959 | } | |
960 | status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh, | |
961 | lqinode->i_size + 2 * sb->s_blocksize); | |
962 | if (status < 0) { | |
963 | mlog_errno(status); | |
964 | goto out; | |
965 | } | |
966 | ||
967 | chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS); | |
968 | if (!chunk) { | |
969 | status = -ENOMEM; | |
970 | mlog_errno(status); | |
971 | goto out; | |
972 | } | |
0584974a JK |
973 | /* Local quota info and two new blocks we initialize */ |
974 | handle = ocfs2_start_trans(OCFS2_SB(sb), | |
975 | OCFS2_LOCAL_QINFO_WRITE_CREDITS + | |
976 | 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS); | |
0e7f387b JK |
977 | if (IS_ERR(handle)) { |
978 | status = PTR_ERR(handle); | |
979 | mlog_errno(status); | |
980 | goto out; | |
981 | } | |
9e33d69f | 982 | |
0e7f387b | 983 | /* Initialize chunk header */ |
9e33d69f JK |
984 | down_read(&OCFS2_I(lqinode)->ip_alloc_sem); |
985 | status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks, | |
986 | &p_blkno, NULL, NULL); | |
987 | up_read(&OCFS2_I(lqinode)->ip_alloc_sem); | |
988 | if (status < 0) { | |
989 | mlog_errno(status); | |
0e7f387b | 990 | goto out_trans; |
9e33d69f JK |
991 | } |
992 | bh = sb_getblk(sb, p_blkno); | |
993 | if (!bh) { | |
994 | status = -ENOMEM; | |
995 | mlog_errno(status); | |
0e7f387b | 996 | goto out_trans; |
9e33d69f JK |
997 | } |
998 | dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data; | |
8cb471e8 | 999 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh); |
0cf2f763 | 1000 | status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh, |
0e7f387b | 1001 | OCFS2_JOURNAL_ACCESS_CREATE); |
9e33d69f JK |
1002 | if (status < 0) { |
1003 | mlog_errno(status); | |
1004 | goto out_trans; | |
1005 | } | |
1006 | lock_buffer(bh); | |
df32b334 | 1007 | dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb)); |
9e33d69f JK |
1008 | memset(dchunk->dqc_bitmap, 0, |
1009 | sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) - | |
1010 | OCFS2_QBLK_RESERVED_SPACE); | |
9e33d69f JK |
1011 | unlock_buffer(bh); |
1012 | status = ocfs2_journal_dirty(handle, bh); | |
1013 | if (status < 0) { | |
1014 | mlog_errno(status); | |
1015 | goto out_trans; | |
1016 | } | |
1017 | ||
0e7f387b JK |
1018 | /* Initialize new block with structures */ |
1019 | down_read(&OCFS2_I(lqinode)->ip_alloc_sem); | |
1020 | status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1, | |
1021 | &p_blkno, NULL, NULL); | |
1022 | up_read(&OCFS2_I(lqinode)->ip_alloc_sem); | |
1023 | if (status < 0) { | |
1024 | mlog_errno(status); | |
1025 | goto out_trans; | |
1026 | } | |
1027 | dbh = sb_getblk(sb, p_blkno); | |
1028 | if (!dbh) { | |
1029 | status = -ENOMEM; | |
1030 | mlog_errno(status); | |
1031 | goto out_trans; | |
1032 | } | |
8cb471e8 | 1033 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh); |
0cf2f763 | 1034 | status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh, |
0e7f387b JK |
1035 | OCFS2_JOURNAL_ACCESS_CREATE); |
1036 | if (status < 0) { | |
1037 | mlog_errno(status); | |
1038 | goto out_trans; | |
1039 | } | |
1040 | lock_buffer(dbh); | |
1041 | memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE); | |
1042 | unlock_buffer(dbh); | |
1043 | status = ocfs2_journal_dirty(handle, dbh); | |
1044 | if (status < 0) { | |
1045 | mlog_errno(status); | |
1046 | goto out_trans; | |
1047 | } | |
1048 | ||
1049 | /* Update local quotafile info */ | |
9e33d69f JK |
1050 | oinfo->dqi_blocks += 2; |
1051 | oinfo->dqi_chunks++; | |
1052 | status = ocfs2_local_write_info(sb, type); | |
1053 | if (status < 0) { | |
1054 | mlog_errno(status); | |
1055 | goto out_trans; | |
1056 | } | |
1057 | status = ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
1058 | if (status < 0) { | |
1059 | mlog_errno(status); | |
1060 | goto out; | |
1061 | } | |
1062 | ||
1063 | list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk); | |
1064 | chunk->qc_num = list_entry(chunk->qc_chunk.prev, | |
1065 | struct ocfs2_quota_chunk, | |
1066 | qc_chunk)->qc_num + 1; | |
1067 | chunk->qc_headerbh = bh; | |
1068 | *offset = 0; | |
1069 | return chunk; | |
1070 | out_trans: | |
1071 | ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
1072 | out: | |
1073 | brelse(bh); | |
0e7f387b | 1074 | brelse(dbh); |
9e33d69f JK |
1075 | kmem_cache_free(ocfs2_qf_chunk_cachep, chunk); |
1076 | return ERR_PTR(status); | |
1077 | } | |
1078 | ||
1079 | /* Find free entry in local quota file */ | |
1080 | static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file( | |
1081 | struct super_block *sb, | |
1082 | int type, | |
1083 | int *offset) | |
1084 | { | |
1085 | struct mem_dqinfo *info = sb_dqinfo(sb, type); | |
1086 | struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv; | |
1087 | struct ocfs2_quota_chunk *chunk; | |
1088 | struct inode *lqinode = sb_dqopt(sb)->files[type]; | |
1089 | struct ocfs2_local_disk_chunk *dchunk; | |
1090 | int epb = ol_quota_entries_per_block(sb); | |
1091 | unsigned int chunk_blocks; | |
0e7f387b JK |
1092 | struct buffer_head *bh; |
1093 | u64 p_blkno; | |
9e33d69f JK |
1094 | int status; |
1095 | handle_t *handle; | |
1096 | ||
1097 | if (list_empty(&oinfo->dqi_chunk)) | |
1098 | return ocfs2_local_quota_add_chunk(sb, type, offset); | |
1099 | /* Is the last chunk full? */ | |
1100 | chunk = list_entry(oinfo->dqi_chunk.prev, | |
1101 | struct ocfs2_quota_chunk, qc_chunk); | |
1102 | chunk_blocks = oinfo->dqi_blocks - | |
1103 | ol_quota_chunk_block(sb, chunk->qc_num) - 1; | |
1104 | if (ol_chunk_blocks(sb) == chunk_blocks) | |
1105 | return ocfs2_local_quota_add_chunk(sb, type, offset); | |
1106 | ||
1107 | /* We are protected by dqio_sem so no locking needed */ | |
1108 | status = ocfs2_extend_no_holes(lqinode, | |
1109 | lqinode->i_size + sb->s_blocksize, | |
1110 | lqinode->i_size); | |
1111 | if (status < 0) { | |
1112 | mlog_errno(status); | |
1113 | goto out; | |
1114 | } | |
1115 | status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh, | |
1116 | lqinode->i_size + sb->s_blocksize); | |
1117 | if (status < 0) { | |
1118 | mlog_errno(status); | |
1119 | goto out; | |
1120 | } | |
0e7f387b JK |
1121 | |
1122 | /* Get buffer from the just added block */ | |
1123 | down_read(&OCFS2_I(lqinode)->ip_alloc_sem); | |
1124 | status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks, | |
1125 | &p_blkno, NULL, NULL); | |
1126 | up_read(&OCFS2_I(lqinode)->ip_alloc_sem); | |
1127 | if (status < 0) { | |
1128 | mlog_errno(status); | |
1129 | goto out; | |
1130 | } | |
1131 | bh = sb_getblk(sb, p_blkno); | |
1132 | if (!bh) { | |
1133 | status = -ENOMEM; | |
1134 | mlog_errno(status); | |
1135 | goto out; | |
1136 | } | |
8cb471e8 | 1137 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh); |
0e7f387b | 1138 | |
0584974a JK |
1139 | /* Local quota info, chunk header and the new block we initialize */ |
1140 | handle = ocfs2_start_trans(OCFS2_SB(sb), | |
1141 | OCFS2_LOCAL_QINFO_WRITE_CREDITS + | |
1142 | 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS); | |
9e33d69f JK |
1143 | if (IS_ERR(handle)) { |
1144 | status = PTR_ERR(handle); | |
1145 | mlog_errno(status); | |
1146 | goto out; | |
1147 | } | |
0e7f387b | 1148 | /* Zero created block */ |
0cf2f763 | 1149 | status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh, |
0e7f387b JK |
1150 | OCFS2_JOURNAL_ACCESS_CREATE); |
1151 | if (status < 0) { | |
1152 | mlog_errno(status); | |
1153 | goto out_trans; | |
1154 | } | |
1155 | lock_buffer(bh); | |
1156 | memset(bh->b_data, 0, sb->s_blocksize); | |
1157 | unlock_buffer(bh); | |
1158 | status = ocfs2_journal_dirty(handle, bh); | |
1159 | if (status < 0) { | |
1160 | mlog_errno(status); | |
1161 | goto out_trans; | |
1162 | } | |
1163 | /* Update chunk header */ | |
0cf2f763 JB |
1164 | status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), |
1165 | chunk->qc_headerbh, | |
9e33d69f JK |
1166 | OCFS2_JOURNAL_ACCESS_WRITE); |
1167 | if (status < 0) { | |
1168 | mlog_errno(status); | |
1169 | goto out_trans; | |
1170 | } | |
1171 | ||
1172 | dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data; | |
1173 | lock_buffer(chunk->qc_headerbh); | |
1174 | le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb)); | |
1175 | unlock_buffer(chunk->qc_headerbh); | |
1176 | status = ocfs2_journal_dirty(handle, chunk->qc_headerbh); | |
1177 | if (status < 0) { | |
1178 | mlog_errno(status); | |
1179 | goto out_trans; | |
1180 | } | |
0e7f387b | 1181 | /* Update file header */ |
9e33d69f JK |
1182 | oinfo->dqi_blocks++; |
1183 | status = ocfs2_local_write_info(sb, type); | |
1184 | if (status < 0) { | |
1185 | mlog_errno(status); | |
1186 | goto out_trans; | |
1187 | } | |
1188 | ||
1189 | status = ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
1190 | if (status < 0) { | |
1191 | mlog_errno(status); | |
1192 | goto out; | |
1193 | } | |
1194 | *offset = chunk_blocks * epb; | |
1195 | return chunk; | |
1196 | out_trans: | |
1197 | ocfs2_commit_trans(OCFS2_SB(sb), handle); | |
1198 | out: | |
1199 | return ERR_PTR(status); | |
1200 | } | |
1201 | ||
df32b334 | 1202 | static void olq_alloc_dquot(struct buffer_head *bh, void *private) |
9e33d69f JK |
1203 | { |
1204 | int *offset = private; | |
1205 | struct ocfs2_local_disk_chunk *dchunk; | |
1206 | ||
1207 | dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data; | |
1208 | ocfs2_set_bit(*offset, dchunk->dqc_bitmap); | |
1209 | le32_add_cpu(&dchunk->dqc_free, -1); | |
1210 | } | |
1211 | ||
1212 | /* Create dquot in the local file for given id */ | |
1213 | static int ocfs2_create_local_dquot(struct dquot *dquot) | |
1214 | { | |
1215 | struct super_block *sb = dquot->dq_sb; | |
1216 | int type = dquot->dq_type; | |
1217 | struct inode *lqinode = sb_dqopt(sb)->files[type]; | |
1218 | struct ocfs2_quota_chunk *chunk; | |
1219 | struct ocfs2_dquot *od = OCFS2_DQUOT(dquot); | |
1220 | int offset; | |
1221 | int status; | |
1222 | ||
1223 | chunk = ocfs2_find_free_entry(sb, type, &offset); | |
1224 | if (!chunk) { | |
1225 | chunk = ocfs2_extend_local_quota_file(sb, type, &offset); | |
1226 | if (IS_ERR(chunk)) | |
1227 | return PTR_ERR(chunk); | |
1228 | } else if (IS_ERR(chunk)) { | |
1229 | return PTR_ERR(chunk); | |
1230 | } | |
1231 | od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset); | |
1232 | od->dq_chunk = chunk; | |
1233 | ||
1234 | /* Initialize dquot structure on disk */ | |
1235 | status = ocfs2_local_write_dquot(dquot); | |
1236 | if (status < 0) { | |
1237 | mlog_errno(status); | |
1238 | goto out; | |
1239 | } | |
1240 | ||
1241 | /* Mark structure as allocated */ | |
1242 | status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot, | |
1243 | &offset); | |
1244 | if (status < 0) { | |
1245 | mlog_errno(status); | |
1246 | goto out; | |
1247 | } | |
1248 | out: | |
1249 | return status; | |
1250 | } | |
1251 | ||
1252 | /* Create entry in local file for dquot, load data from the global file */ | |
1253 | static int ocfs2_local_read_dquot(struct dquot *dquot) | |
1254 | { | |
1255 | int status; | |
1256 | ||
1257 | mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type); | |
1258 | ||
1259 | status = ocfs2_global_read_dquot(dquot); | |
1260 | if (status < 0) { | |
1261 | mlog_errno(status); | |
1262 | goto out_err; | |
1263 | } | |
1264 | ||
1265 | /* Now create entry in the local quota file */ | |
1266 | status = ocfs2_create_local_dquot(dquot); | |
1267 | if (status < 0) { | |
1268 | mlog_errno(status); | |
1269 | goto out_err; | |
1270 | } | |
1271 | mlog_exit(0); | |
1272 | return 0; | |
1273 | out_err: | |
1274 | mlog_exit(status); | |
1275 | return status; | |
1276 | } | |
1277 | ||
1278 | /* Release dquot structure from local quota file. ocfs2_release_dquot() has | |
1279 | * already started a transaction and obtained exclusive lock for global | |
1280 | * quota file. */ | |
1281 | static int ocfs2_local_release_dquot(struct dquot *dquot) | |
1282 | { | |
1283 | int status; | |
1284 | int type = dquot->dq_type; | |
1285 | struct ocfs2_dquot *od = OCFS2_DQUOT(dquot); | |
1286 | struct super_block *sb = dquot->dq_sb; | |
1287 | struct ocfs2_local_disk_chunk *dchunk; | |
1288 | int offset; | |
1289 | handle_t *handle = journal_current_handle(); | |
1290 | ||
1291 | BUG_ON(!handle); | |
1292 | /* First write all local changes to global file */ | |
1293 | status = ocfs2_global_release_dquot(dquot); | |
1294 | if (status < 0) { | |
1295 | mlog_errno(status); | |
1296 | goto out; | |
1297 | } | |
1298 | ||
0cf2f763 JB |
1299 | status = ocfs2_journal_access_dq(handle, |
1300 | INODE_CACHE(sb_dqopt(sb)->files[type]), | |
9e33d69f JK |
1301 | od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE); |
1302 | if (status < 0) { | |
1303 | mlog_errno(status); | |
1304 | goto out; | |
1305 | } | |
1306 | offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num, | |
1307 | od->dq_local_off); | |
1308 | dchunk = (struct ocfs2_local_disk_chunk *) | |
1309 | (od->dq_chunk->qc_headerbh->b_data); | |
1310 | /* Mark structure as freed */ | |
1311 | lock_buffer(od->dq_chunk->qc_headerbh); | |
1312 | ocfs2_clear_bit(offset, dchunk->dqc_bitmap); | |
1313 | le32_add_cpu(&dchunk->dqc_free, 1); | |
1314 | unlock_buffer(od->dq_chunk->qc_headerbh); | |
1315 | status = ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh); | |
1316 | if (status < 0) { | |
1317 | mlog_errno(status); | |
1318 | goto out; | |
1319 | } | |
1320 | status = 0; | |
1321 | out: | |
1322 | /* Clear the read bit so that next time someone uses this | |
1323 | * dquot he reads fresh info from disk and allocates local | |
1324 | * dquot structure */ | |
1325 | clear_bit(DQ_READ_B, &dquot->dq_flags); | |
1326 | return status; | |
1327 | } | |
1328 | ||
1472da5f | 1329 | static const struct quota_format_ops ocfs2_format_ops = { |
9e33d69f JK |
1330 | .check_quota_file = ocfs2_local_check_quota_file, |
1331 | .read_file_info = ocfs2_local_read_info, | |
1332 | .write_file_info = ocfs2_global_write_info, | |
1333 | .free_file_info = ocfs2_local_free_info, | |
1334 | .read_dqblk = ocfs2_local_read_dquot, | |
1335 | .commit_dqblk = ocfs2_local_write_dquot, | |
1336 | .release_dqblk = ocfs2_local_release_dquot, | |
1337 | }; | |
1338 | ||
1339 | struct quota_format_type ocfs2_quota_format = { | |
1340 | .qf_fmt_id = QFMT_OCFS2, | |
1341 | .qf_ops = &ocfs2_format_ops, | |
1342 | .qf_owner = THIS_MODULE | |
1343 | }; |