Blame view

fs/ocfs2/quota_local.c 35.8 KB
9e33d69f5   Jan Kara   ocfs2: Implementa...
1
2
3
4
5
  /*
   *  Implementation of operations over local quota file
   */
  
  #include <linux/fs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
6
  #include <linux/slab.h>
9e33d69f5   Jan Kara   ocfs2: Implementa...
7
8
9
  #include <linux/quota.h>
  #include <linux/quotaops.h>
  #include <linux/module.h>
9e33d69f5   Jan Kara   ocfs2: Implementa...
10
11
12
13
14
15
16
17
18
19
20
21
  #include <cluster/masklog.h>
  
  #include "ocfs2_fs.h"
  #include "ocfs2.h"
  #include "inode.h"
  #include "alloc.h"
  #include "file.h"
  #include "buffer_head_io.h"
  #include "journal.h"
  #include "sysfile.h"
  #include "dlmglue.h"
  #include "quota.h"
4b3fa1904   Jan Kara   ocfs2: Mark buffe...
22
  #include "uptodate.h"
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
23
  #include "super.h"
38877a437   Tao Ma   ocfs2: Remove mlo...
24
  #include "ocfs2_trace.h"
9e33d69f5   Jan Kara   ocfs2: Implementa...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  
  /* Number of local quota structures per block */
  static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
  {
  	return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
  		sizeof(struct ocfs2_local_disk_dqblk));
  }
  
  /* Number of blocks with entries in one chunk */
  static inline unsigned int ol_chunk_blocks(struct super_block *sb)
  {
  	return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
  		 OCFS2_QBLK_RESERVED_SPACE) << 3) /
  	       ol_quota_entries_per_block(sb);
  }
  
  /* Number of entries in a chunk bitmap */
  static unsigned int ol_chunk_entries(struct super_block *sb)
  {
  	return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
  }
  
  /* Offset of the chunk in quota file */
  static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
  {
  	/* 1 block for local quota file info, 1 block per chunk for chunk info */
  	return 1 + (ol_chunk_blocks(sb) + 1) * c;
  }
2205363dc   Jan Kara   ocfs2: Implement ...
53
54
55
56
57
58
59
60
  static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
  {
  	int epb = ol_quota_entries_per_block(sb);
  
  	return ol_quota_chunk_block(sb, c) + 1 + off / epb;
  }
  
  static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
9e33d69f5   Jan Kara   ocfs2: Implementa...
61
62
  {
  	int epb = ol_quota_entries_per_block(sb);
2205363dc   Jan Kara   ocfs2: Implement ...
63
64
65
66
67
68
69
70
  	return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
  }
  
  /* Offset of the dquot structure in the quota file */
  static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
  {
  	return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
  	       ol_dqblk_block_off(sb, c, off);
9e33d69f5   Jan Kara   ocfs2: Implementa...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  }
  
  /* Compute block number from given offset */
  static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
  {
  	return off >> sb->s_blocksize_bits;
  }
  
  static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
  {
  	return off & ((1 << sb->s_blocksize_bits) - 1);
  }
  
  /* Compute offset in the chunk of a structure with the given offset */
  static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
  {
  	int epb = ol_quota_entries_per_block(sb);
  
  	return ((off >> sb->s_blocksize_bits) -
  			ol_quota_chunk_block(sb, c) - 1) * epb
  	       + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
  		 sizeof(struct ocfs2_local_disk_dqblk);
  }
  
  /* Write bufferhead into the fs */
  static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
  		void (*modify)(struct buffer_head *, void *), void *private)
  {
  	struct super_block *sb = inode->i_sb;
  	handle_t *handle;
  	int status;
0584974a7   Jan Kara   ocfs2: Define cre...
102
103
  	handle = ocfs2_start_trans(OCFS2_SB(sb),
  				   OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
9e33d69f5   Jan Kara   ocfs2: Implementa...
104
105
106
107
108
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		return status;
  	}
0cf2f7632   Joel Becker   ocfs2: Pass struc...
109
  	status = ocfs2_journal_access_dq(handle, INODE_CACHE(inode), bh,
13723d00e   Joel Becker   ocfs2: Use metada...
110
  					 OCFS2_JOURNAL_ACCESS_WRITE);
9e33d69f5   Jan Kara   ocfs2: Implementa...
111
112
113
114
115
116
117
118
  	if (status < 0) {
  		mlog_errno(status);
  		ocfs2_commit_trans(OCFS2_SB(sb), handle);
  		return status;
  	}
  	lock_buffer(bh);
  	modify(bh, private);
  	unlock_buffer(bh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
119
  	ocfs2_journal_dirty(handle, bh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
120
121
122
123
124
125
126
  	status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  	if (status < 0) {
  		mlog_errno(status);
  		return status;
  	}
  	return 0;
  }
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  /*
   * Read quota block from a given logical offset.
   *
   * This function acquires ip_alloc_sem and thus it must not be called with a
   * transaction started.
   */
  static int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
  				  struct buffer_head **bh)
  {
  	int rc = 0;
  	struct buffer_head *tmp = *bh;
  
  	if (i_size_read(inode) >> inode->i_sb->s_blocksize_bits <= v_block) {
  		ocfs2_error(inode->i_sb,
  			    "Quota file %llu is probably corrupted! Requested "
  			    "to read block %Lu but file has size only %Lu
  ",
  			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
  			    (unsigned long long)v_block,
  			    (unsigned long long)i_size_read(inode));
  		return -EIO;
  	}
  	rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
  				    ocfs2_validate_quota_block);
  	if (rc)
  		mlog_errno(rc);
  
  	/* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
  	if (!rc && !*bh)
  		*bh = tmp;
  
  	return rc;
  }
9e33d69f5   Jan Kara   ocfs2: Implementa...
160
161
162
163
164
165
166
167
168
  /* Check whether we understand format of quota files */
  static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
  {
  	unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
  	unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
  	unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
  	unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
  	unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
  					GROUP_QUOTA_SYSTEM_INODE };
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
169
  	struct buffer_head *bh = NULL;
9e33d69f5   Jan Kara   ocfs2: Implementa...
170
171
172
173
174
175
  	struct inode *linode = sb_dqopt(sb)->files[type];
  	struct inode *ginode = NULL;
  	struct ocfs2_disk_dqheader *dqhead;
  	int status, ret = 0;
  
  	/* First check whether we understand local quota file */
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
176
177
  	status = ocfs2_read_quota_block(linode, 0, &bh);
  	if (status) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  		mlog_errno(status);
  		mlog(ML_ERROR, "failed to read quota file header (type=%d)
  ",
  			type);
  		goto out_err;
  	}
  	dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
  	if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
  		mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
  			" type=%d
  ", le32_to_cpu(dqhead->dqh_magic),
  			lmagics[type], type);
  		goto out_err;
  	}
  	if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
  		mlog(ML_ERROR, "quota file version does not match (%u != %u),"
  			" type=%d
  ", le32_to_cpu(dqhead->dqh_version),
  			lversions[type], type);
  		goto out_err;
  	}
  	brelse(bh);
  	bh = NULL;
  
  	/* Next check whether we understand global quota file */
  	ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
  						OCFS2_INVALID_SLOT);
  	if (!ginode) {
  		mlog(ML_ERROR, "cannot get global quota file inode "
  				"(type=%d)
  ", type);
  		goto out_err;
  	}
  	/* Since the header is read only, we don't care about locking */
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
212
213
  	status = ocfs2_read_quota_block(ginode, 0, &bh);
  	if (status) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  		mlog_errno(status);
  		mlog(ML_ERROR, "failed to read global quota file header "
  				"(type=%d)
  ", type);
  		goto out_err;
  	}
  	dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
  	if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
  		mlog(ML_ERROR, "global quota file magic does not match "
  			"(%u != %u), type=%d
  ",
  			le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
  		goto out_err;
  	}
  	if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
  		mlog(ML_ERROR, "global quota file version does not match "
  			"(%u != %u), type=%d
  ",
  			le32_to_cpu(dqhead->dqh_version), gversions[type],
  			type);
  		goto out_err;
  	}
  
  	ret = 1;
  out_err:
  	brelse(bh);
  	iput(ginode);
  	return ret;
  }
  
  /* Release given list of quota file chunks */
  static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
  {
  	struct ocfs2_quota_chunk *pos, *next;
  
  	list_for_each_entry_safe(pos, next, head, qc_chunk) {
  		list_del(&pos->qc_chunk);
  		brelse(pos->qc_headerbh);
  		kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
  	}
  }
  
  /* Load quota bitmaps into memory */
  static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
  			struct ocfs2_local_disk_dqinfo *ldinfo,
  			struct list_head *head)
  {
  	struct ocfs2_quota_chunk *newchunk;
  	int i, status;
  
  	INIT_LIST_HEAD(head);
  	for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
  		newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
  		if (!newchunk) {
  			ocfs2_release_local_quota_bitmaps(head);
  			return -ENOMEM;
  		}
  		newchunk->qc_num = i;
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
272
273
  		newchunk->qc_headerbh = NULL;
  		status = ocfs2_read_quota_block(inode,
9e33d69f5   Jan Kara   ocfs2: Implementa...
274
  				ol_quota_chunk_block(inode->i_sb, i),
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
275
276
  				&newchunk->qc_headerbh);
  		if (status) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  			mlog_errno(status);
  			kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
  			ocfs2_release_local_quota_bitmaps(head);
  			return status;
  		}
  		list_add_tail(&newchunk->qc_chunk, head);
  	}
  	return 0;
  }
  
  static void olq_update_info(struct buffer_head *bh, void *private)
  {
  	struct mem_dqinfo *info = private;
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  	struct ocfs2_local_disk_dqinfo *ldinfo;
  
  	ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  						OCFS2_LOCAL_INFO_OFF);
  	spin_lock(&dq_data_lock);
  	ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
  	ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
  	ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
  	spin_unlock(&dq_data_lock);
  }
2205363dc   Jan Kara   ocfs2: Implement ...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  static int ocfs2_add_recovery_chunk(struct super_block *sb,
  				    struct ocfs2_local_disk_chunk *dchunk,
  				    int chunk,
  				    struct list_head *head)
  {
  	struct ocfs2_recovery_chunk *rc;
  
  	rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
  	if (!rc)
  		return -ENOMEM;
  	rc->rc_chunk = chunk;
  	rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
  	if (!rc->rc_bitmap) {
  		kfree(rc);
  		return -ENOMEM;
  	}
  	memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
  	       (ol_chunk_entries(sb) + 7) >> 3);
  	list_add_tail(&rc->rc_list, head);
  	return 0;
  }
  
  static void free_recovery_list(struct list_head *head)
  {
  	struct ocfs2_recovery_chunk *next;
  	struct ocfs2_recovery_chunk *rchunk;
  
  	list_for_each_entry_safe(rchunk, next, head, rc_list) {
  		list_del(&rchunk->rc_list);
  		kfree(rchunk->rc_bitmap);
  		kfree(rchunk);
  	}
  }
  
  void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
  {
  	int type;
  
  	for (type = 0; type < MAXQUOTAS; type++)
  		free_recovery_list(&(rec->r_list[type]));
  	kfree(rec);
  }
  
  /* Load entries in our quota file we have to recover*/
  static int ocfs2_recovery_load_quota(struct inode *lqinode,
  				     struct ocfs2_local_disk_dqinfo *ldinfo,
  				     int type,
  				     struct list_head *head)
  {
  	struct super_block *sb = lqinode->i_sb;
  	struct buffer_head *hbh;
  	struct ocfs2_local_disk_chunk *dchunk;
  	int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
  	int status = 0;
  
  	for (i = 0; i < chunks; i++) {
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
357
358
359
360
361
  		hbh = NULL;
  		status = ocfs2_read_quota_block(lqinode,
  						ol_quota_chunk_block(sb, i),
  						&hbh);
  		if (status) {
2205363dc   Jan Kara   ocfs2: Implement ...
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  			mlog_errno(status);
  			break;
  		}
  		dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
  		if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
  			status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
  		brelse(hbh);
  		if (status < 0)
  			break;
  	}
  	if (status < 0)
  		free_recovery_list(head);
  	return status;
  }
  
  static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
  {
  	int type;
  	struct ocfs2_quota_recovery *rec;
  
  	rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
  	if (!rec)
  		return NULL;
  	for (type = 0; type < MAXQUOTAS; type++)
  		INIT_LIST_HEAD(&(rec->r_list[type]));
  	return rec;
  }
  
  /* Load information we need for quota recovery into memory */
  struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
  						struct ocfs2_super *osb,
  						int slot_num)
  {
  	unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  					    OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
  	unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
  					LOCAL_GROUP_QUOTA_SYSTEM_INODE };
  	struct super_block *sb = osb->sb;
  	struct ocfs2_local_disk_dqinfo *ldinfo;
  	struct inode *lqinode;
  	struct buffer_head *bh;
  	int type;
  	int status = 0;
  	struct ocfs2_quota_recovery *rec;
619c200de   Sunil Mushran   ocfs2: Clean up m...
406
407
408
  	printk(KERN_NOTICE "ocfs2: Beginning quota recovery on device (%s) for "
  	       "slot %u
  ", osb->dev_str, slot_num);
2205363dc   Jan Kara   ocfs2: Implement ...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  	rec = ocfs2_alloc_quota_recovery();
  	if (!rec)
  		return ERR_PTR(-ENOMEM);
  	/* First init... */
  
  	for (type = 0; type < MAXQUOTAS; type++) {
  		if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
  			continue;
  		/* At this point, journal of the slot is already replayed so
  		 * we can trust metadata and data of the quota file */
  		lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
  		if (!lqinode) {
  			status = -ENOENT;
  			goto out;
  		}
  		status = ocfs2_inode_lock_full(lqinode, NULL, 1,
  					       OCFS2_META_LOCK_RECOVERY);
  		if (status < 0) {
  			mlog_errno(status);
  			goto out_put;
  		}
  		/* Now read local header */
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
431
432
433
  		bh = NULL;
  		status = ocfs2_read_quota_block(lqinode, 0, &bh);
  		if (status) {
2205363dc   Jan Kara   ocfs2: Implement ...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  			mlog_errno(status);
  			mlog(ML_ERROR, "failed to read quota file info header "
  				"(slot=%d type=%d)
  ", slot_num, type);
  			goto out_lock;
  		}
  		ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  							OCFS2_LOCAL_INFO_OFF);
  		status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
  						   &rec->r_list[type]);
  		brelse(bh);
  out_lock:
  		ocfs2_inode_unlock(lqinode, 1);
  out_put:
  		iput(lqinode);
  		if (status < 0)
  			break;
  	}
  out:
  	if (status < 0) {
  		ocfs2_free_quota_recovery(rec);
  		rec = ERR_PTR(status);
  	}
  	return rec;
  }
  
  /* Sync changes in local quota file into global quota file and
   * reinitialize local quota file.
   * The function expects local quota file to be already locked and
   * dqonoff_mutex locked. */
  static int ocfs2_recover_local_quota_file(struct inode *lqinode,
  					  int type,
  					  struct ocfs2_quota_recovery *rec)
  {
  	struct super_block *sb = lqinode->i_sb;
  	struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  	struct ocfs2_local_disk_chunk *dchunk;
  	struct ocfs2_local_disk_dqblk *dqblk;
  	struct dquot *dquot;
  	handle_t *handle;
  	struct buffer_head *hbh = NULL, *qbh = NULL;
  	int status = 0;
  	int bit, chunk;
  	struct ocfs2_recovery_chunk *rchunk, *next;
  	qsize_t spacechange, inodechange;
38877a437   Tao Ma   ocfs2: Remove mlo...
479
  	trace_ocfs2_recover_local_quota_file((unsigned long)lqinode->i_ino, type);
2205363dc   Jan Kara   ocfs2: Implement ...
480

2205363dc   Jan Kara   ocfs2: Implement ...
481
482
  	list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
  		chunk = rchunk->rc_chunk;
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
483
484
485
486
487
  		hbh = NULL;
  		status = ocfs2_read_quota_block(lqinode,
  						ol_quota_chunk_block(sb, chunk),
  						&hbh);
  		if (status) {
2205363dc   Jan Kara   ocfs2: Implement ...
488
489
490
491
  			mlog_errno(status);
  			break;
  		}
  		dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
984b3f574   Akinobu Mita   bitops: rename fo...
492
  		for_each_set_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
493
494
  			qbh = NULL;
  			status = ocfs2_read_quota_block(lqinode,
2205363dc   Jan Kara   ocfs2: Implement ...
495
  						ol_dqblk_block(sb, chunk, bit),
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
496
497
  						&qbh);
  			if (status) {
2205363dc   Jan Kara   ocfs2: Implement ...
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  				mlog_errno(status);
  				break;
  			}
  			dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
  				ol_dqblk_block_off(sb, chunk, bit));
  			dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
  			if (!dquot) {
  				status = -EIO;
  				mlog(ML_ERROR, "Failed to get quota structure "
  				     "for id %u, type %d. Cannot finish quota "
  				     "file recovery.
  ",
  				     (unsigned)le64_to_cpu(dqblk->dqb_id),
  				     type);
  				goto out_put_bh;
  			}
80d73f15d   Jan Kara   ocfs2: Fix possib...
514
515
516
517
518
  			status = ocfs2_lock_global_qf(oinfo, 1);
  			if (status < 0) {
  				mlog_errno(status);
  				goto out_put_dquot;
  			}
2205363dc   Jan Kara   ocfs2: Implement ...
519
520
521
522
523
  			handle = ocfs2_start_trans(OCFS2_SB(sb),
  						   OCFS2_QSYNC_CREDITS);
  			if (IS_ERR(handle)) {
  				status = PTR_ERR(handle);
  				mlog_errno(status);
80d73f15d   Jan Kara   ocfs2: Fix possib...
524
  				goto out_drop_lock;
2205363dc   Jan Kara   ocfs2: Implement ...
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
  			}
  			mutex_lock(&sb_dqopt(sb)->dqio_mutex);
  			spin_lock(&dq_data_lock);
  			/* Add usage from quota entry into quota changes
  			 * of our node. Auxiliary variables are important
  			 * due to signedness */
  			spacechange = le64_to_cpu(dqblk->dqb_spacemod);
  			inodechange = le64_to_cpu(dqblk->dqb_inodemod);
  			dquot->dq_dqb.dqb_curspace += spacechange;
  			dquot->dq_dqb.dqb_curinodes += inodechange;
  			spin_unlock(&dq_data_lock);
  			/* We want to drop reference held by the crashed
  			 * node. Since we have our own reference we know
  			 * global structure actually won't be freed. */
  			status = ocfs2_global_release_dquot(dquot);
  			if (status < 0) {
  				mlog_errno(status);
  				goto out_commit;
  			}
  			/* Release local quota file entry */
0cf2f7632   Joel Becker   ocfs2: Pass struc...
545
546
  			status = ocfs2_journal_access_dq(handle,
  					INODE_CACHE(lqinode),
2205363dc   Jan Kara   ocfs2: Implement ...
547
548
549
550
551
552
  					qbh, OCFS2_JOURNAL_ACCESS_WRITE);
  			if (status < 0) {
  				mlog_errno(status);
  				goto out_commit;
  			}
  			lock_buffer(qbh);
939255798   Akinobu Mita   ocfs2: avoid unal...
553
554
  			WARN_ON(!ocfs2_test_bit_unaligned(bit, dchunk->dqc_bitmap));
  			ocfs2_clear_bit_unaligned(bit, dchunk->dqc_bitmap);
2205363dc   Jan Kara   ocfs2: Implement ...
555
556
  			le32_add_cpu(&dchunk->dqc_free, 1);
  			unlock_buffer(qbh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
557
  			ocfs2_journal_dirty(handle, qbh);
2205363dc   Jan Kara   ocfs2: Implement ...
558
559
560
  out_commit:
  			mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
  			ocfs2_commit_trans(OCFS2_SB(sb), handle);
80d73f15d   Jan Kara   ocfs2: Fix possib...
561
562
  out_drop_lock:
  			ocfs2_unlock_global_qf(oinfo, 1);
2205363dc   Jan Kara   ocfs2: Implement ...
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  out_put_dquot:
  			dqput(dquot);
  out_put_bh:
  			brelse(qbh);
  			if (status < 0)
  				break;
  		}
  		brelse(hbh);
  		list_del(&rchunk->rc_list);
  		kfree(rchunk->rc_bitmap);
  		kfree(rchunk);
  		if (status < 0)
  			break;
  	}
2205363dc   Jan Kara   ocfs2: Implement ...
577
578
  	if (status < 0)
  		free_recovery_list(&(rec->r_list[type]));
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
579
580
  	if (status)
  		mlog_errno(status);
2205363dc   Jan Kara   ocfs2: Implement ...
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
  	return status;
  }
  
  /* Recover local quota files for given node different from us */
  int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
  				struct ocfs2_quota_recovery *rec,
  				int slot_num)
  {
  	unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
  					LOCAL_GROUP_QUOTA_SYSTEM_INODE };
  	struct super_block *sb = osb->sb;
  	struct ocfs2_local_disk_dqinfo *ldinfo;
  	struct buffer_head *bh;
  	handle_t *handle;
  	int type;
  	int status = 0;
  	struct inode *lqinode;
  	unsigned int flags;
619c200de   Sunil Mushran   ocfs2: Clean up m...
599
600
601
  	printk(KERN_NOTICE "ocfs2: Finishing quota recovery on device (%s) for "
  	       "slot %u
  ", osb->dev_str, slot_num);
2205363dc   Jan Kara   ocfs2: Implement ...
602
603
604
605
  	mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  	for (type = 0; type < MAXQUOTAS; type++) {
  		if (list_empty(&(rec->r_list[type])))
  			continue;
38877a437   Tao Ma   ocfs2: Remove mlo...
606
  		trace_ocfs2_finish_quota_recovery(slot_num);
2205363dc   Jan Kara   ocfs2: Implement ...
607
608
609
610
611
612
613
614
615
616
  		lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
  		if (!lqinode) {
  			status = -ENOENT;
  			goto out;
  		}
  		status = ocfs2_inode_lock_full(lqinode, NULL, 1,
  						       OCFS2_META_LOCK_NOQUEUE);
  		/* Someone else is holding the lock? Then he must be
  		 * doing the recovery. Just skip the file... */
  		if (status == -EAGAIN) {
619c200de   Sunil Mushran   ocfs2: Clean up m...
617
618
619
620
  			printk(KERN_NOTICE "ocfs2: Skipping quota recovery on "
  			       "device (%s) for slot %d because quota file is "
  			       "locked.
  ", osb->dev_str, slot_num);
2205363dc   Jan Kara   ocfs2: Implement ...
621
622
623
624
625
626
627
  			status = 0;
  			goto out_put;
  		} else if (status < 0) {
  			mlog_errno(status);
  			goto out_put;
  		}
  		/* Now read local header */
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
628
629
630
  		bh = NULL;
  		status = ocfs2_read_quota_block(lqinode, 0, &bh);
  		if (status) {
2205363dc   Jan Kara   ocfs2: Implement ...
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  			mlog_errno(status);
  			mlog(ML_ERROR, "failed to read quota file info header "
  				"(slot=%d type=%d)
  ", slot_num, type);
  			goto out_lock;
  		}
  		ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  							OCFS2_LOCAL_INFO_OFF);
  		/* Is recovery still needed? */
  		flags = le32_to_cpu(ldinfo->dqi_flags);
  		if (!(flags & OLQF_CLEAN))
  			status = ocfs2_recover_local_quota_file(lqinode,
  								type,
  								rec);
  		/* We don't want to mark file as clean when it is actually
  		 * active */
  		if (slot_num == osb->slot_num)
  			goto out_bh;
  		/* Mark quota file as clean if we are recovering quota file of
  		 * some other node. */
0584974a7   Jan Kara   ocfs2: Define cre...
651
652
  		handle = ocfs2_start_trans(osb,
  					   OCFS2_LOCAL_QINFO_WRITE_CREDITS);
2205363dc   Jan Kara   ocfs2: Implement ...
653
654
655
656
657
  		if (IS_ERR(handle)) {
  			status = PTR_ERR(handle);
  			mlog_errno(status);
  			goto out_bh;
  		}
0cf2f7632   Joel Becker   ocfs2: Pass struc...
658
659
  		status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
  						 bh,
13723d00e   Joel Becker   ocfs2: Use metada...
660
  						 OCFS2_JOURNAL_ACCESS_WRITE);
2205363dc   Jan Kara   ocfs2: Implement ...
661
662
663
664
665
666
667
  		if (status < 0) {
  			mlog_errno(status);
  			goto out_trans;
  		}
  		lock_buffer(bh);
  		ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
  		unlock_buffer(bh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
668
  		ocfs2_journal_dirty(handle, bh);
2205363dc   Jan Kara   ocfs2: Implement ...
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  out_trans:
  		ocfs2_commit_trans(osb, handle);
  out_bh:
  		brelse(bh);
  out_lock:
  		ocfs2_inode_unlock(lqinode, 1);
  out_put:
  		iput(lqinode);
  		if (status < 0)
  			break;
  	}
  out:
  	mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  	kfree(rec);
  	return status;
  }
9e33d69f5   Jan Kara   ocfs2: Implementa...
685
686
687
688
689
690
691
692
693
  /* Read information header from quota file */
  static int ocfs2_local_read_info(struct super_block *sb, int type)
  {
  	struct ocfs2_local_disk_dqinfo *ldinfo;
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo;
  	struct inode *lqinode = sb_dqopt(sb)->files[type];
  	int status;
  	struct buffer_head *bh = NULL;
2205363dc   Jan Kara   ocfs2: Implement ...
694
  	struct ocfs2_quota_recovery *rec;
9e33d69f5   Jan Kara   ocfs2: Implementa...
695
  	int locked = 0;
b4c30de39   Jan Kara   ocfs2: Fix lock i...
696
697
698
  	/* We don't need the lock and we have to acquire quota file locks
  	 * which will later depend on this lock */
  	mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f5   Jan Kara   ocfs2: Implementa...
699
700
701
702
703
704
705
706
707
708
709
  	info->dqi_maxblimit = 0x7fffffffffffffffLL;
  	info->dqi_maxilimit = 0x7fffffffffffffffLL;
  	oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
  	if (!oinfo) {
  		mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
  			       " info.");
  		goto out_err;
  	}
  	info->dqi_priv = oinfo;
  	oinfo->dqi_type = type;
  	INIT_LIST_HEAD(&oinfo->dqi_chunk);
2205363dc   Jan Kara   ocfs2: Implement ...
710
  	oinfo->dqi_rec = NULL;
9e33d69f5   Jan Kara   ocfs2: Implementa...
711
  	oinfo->dqi_lqi_bh = NULL;
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
712
  	oinfo->dqi_libh = NULL;
9e33d69f5   Jan Kara   ocfs2: Implementa...
713
714
715
716
717
718
719
720
721
722
723
724
725
  
  	status = ocfs2_global_read_info(sb, type);
  	if (status < 0)
  		goto out_err;
  
  	status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_err;
  	}
  	locked = 1;
  
  	/* Now read local header */
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
726
727
  	status = ocfs2_read_quota_block(lqinode, 0, &bh);
  	if (status) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
728
729
730
731
732
733
734
735
736
737
738
  		mlog_errno(status);
  		mlog(ML_ERROR, "failed to read quota file info header "
  			"(type=%d)
  ", type);
  		goto out_err;
  	}
  	ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  						OCFS2_LOCAL_INFO_OFF);
  	info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
  	oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
  	oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
739
  	oinfo->dqi_libh = bh;
9e33d69f5   Jan Kara   ocfs2: Implementa...
740
741
  
  	/* We crashed when using local quota file? */
2205363dc   Jan Kara   ocfs2: Implement ...
742
743
744
745
746
747
748
749
750
751
752
  	if (!(info->dqi_flags & OLQF_CLEAN)) {
  		rec = OCFS2_SB(sb)->quota_rec;
  		if (!rec) {
  			rec = ocfs2_alloc_quota_recovery();
  			if (!rec) {
  				status = -ENOMEM;
  				mlog_errno(status);
  				goto out_err;
  			}
  			OCFS2_SB(sb)->quota_rec = rec;
  		}
9e33d69f5   Jan Kara   ocfs2: Implementa...
753

2205363dc   Jan Kara   ocfs2: Implement ...
754
755
756
757
758
759
760
761
762
  		status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
                                                     &rec->r_list[type]);
  		if (status < 0) {
  			mlog_errno(status);
  			goto out_err;
  		}
  	}
  
  	status = ocfs2_load_local_quota_bitmaps(lqinode,
9e33d69f5   Jan Kara   ocfs2: Implementa...
763
764
765
766
767
768
769
770
771
772
773
774
775
776
  						ldinfo,
  						&oinfo->dqi_chunk);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_err;
  	}
  
  	/* Now mark quota file as used */
  	info->dqi_flags &= ~OLQF_CLEAN;
  	status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_err;
  	}
b4c30de39   Jan Kara   ocfs2: Fix lock i...
777
  	mutex_lock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f5   Jan Kara   ocfs2: Implementa...
778
779
780
781
782
783
784
785
786
787
788
789
790
  	return 0;
  out_err:
  	if (oinfo) {
  		iput(oinfo->dqi_gqinode);
  		ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
  		ocfs2_lock_res_free(&oinfo->dqi_gqlock);
  		brelse(oinfo->dqi_lqi_bh);
  		if (locked)
  			ocfs2_inode_unlock(lqinode, 1);
  		ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
  		kfree(oinfo);
  	}
  	brelse(bh);
b4c30de39   Jan Kara   ocfs2: Fix lock i...
791
  	mutex_lock(&sb_dqopt(sb)->dqio_mutex);
9e33d69f5   Jan Kara   ocfs2: Implementa...
792
793
794
795
796
797
798
799
  	return -1;
  }
  
  /* Write local info to quota file */
  static int ocfs2_local_write_info(struct super_block *sb, int type)
  {
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
800
  						->dqi_libh;
9e33d69f5   Jan Kara   ocfs2: Implementa...
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
  	int status;
  
  	status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
  				 info);
  	if (status < 0) {
  		mlog_errno(status);
  		return -1;
  	}
  
  	return 0;
  }
  
  /* Release info from memory */
  static int ocfs2_local_free_info(struct super_block *sb, int type)
  {
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  	struct ocfs2_quota_chunk *chunk;
  	struct ocfs2_local_disk_chunk *dchunk;
  	int mark_clean = 1, len;
  	int status;
  
  	iput(oinfo->dqi_gqinode);
  	ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
  	ocfs2_lock_res_free(&oinfo->dqi_gqlock);
  	list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  		dchunk = (struct ocfs2_local_disk_chunk *)
  					(chunk->qc_headerbh->b_data);
  		if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  			len = ol_chunk_entries(sb);
  		} else {
  			len = (oinfo->dqi_blocks -
  			       ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  			      * ol_quota_entries_per_block(sb);
  		}
  		/* Not all entries free? Bug! */
  		if (le32_to_cpu(dchunk->dqc_free) != len) {
  			mlog(ML_ERROR, "releasing quota file with used "
  					"entries (type=%d)
  ", type);
  			mark_clean = 0;
  		}
  	}
  	ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
2205363dc   Jan Kara   ocfs2: Implement ...
845
846
847
848
849
  	/* dqonoff_mutex protects us against racing with recovery thread... */
  	if (oinfo->dqi_rec) {
  		ocfs2_free_quota_recovery(oinfo->dqi_rec);
  		mark_clean = 0;
  	}
9e33d69f5   Jan Kara   ocfs2: Implementa...
850
851
852
853
854
855
  	if (!mark_clean)
  		goto out;
  
  	/* Mark local file as clean */
  	info->dqi_flags |= OLQF_CLEAN;
  	status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
856
  				 oinfo->dqi_libh,
9e33d69f5   Jan Kara   ocfs2: Implementa...
857
858
859
860
861
862
863
864
865
  				 olq_update_info,
  				 info);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  
  out:
  	ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
ae4f6ef13   Jan Kara   ocfs2: Avoid unne...
866
  	brelse(oinfo->dqi_libh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
  	brelse(oinfo->dqi_lqi_bh);
  	kfree(oinfo);
  	return 0;
  }
  
  static void olq_set_dquot(struct buffer_head *bh, void *private)
  {
  	struct ocfs2_dquot *od = private;
  	struct ocfs2_local_disk_dqblk *dqblk;
  	struct super_block *sb = od->dq_dquot.dq_sb;
  
  	dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
  		+ ol_dqblk_block_offset(sb, od->dq_local_off));
  
  	dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
  	spin_lock(&dq_data_lock);
  	dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
  					  od->dq_origspace);
  	dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
  					  od->dq_originodes);
  	spin_unlock(&dq_data_lock);
38877a437   Tao Ma   ocfs2: Remove mlo...
888
889
890
891
  	trace_olq_set_dquot(
  		(unsigned long long)le64_to_cpu(dqblk->dqb_spacemod),
  		(unsigned long long)le64_to_cpu(dqblk->dqb_inodemod),
  		od->dq_dquot.dq_id);
9e33d69f5   Jan Kara   ocfs2: Implementa...
892
893
894
  }
  
  /* Write dquot to local quota file */
741e12893   Jan Kara   ocfs2: Fix NULL p...
895
  int ocfs2_local_write_dquot(struct dquot *dquot)
9e33d69f5   Jan Kara   ocfs2: Implementa...
896
897
898
  {
  	struct super_block *sb = dquot->dq_sb;
  	struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
f64dd44eb   Jan Kara   ocfs2: Do not map...
899
900
  	struct buffer_head *bh;
  	struct inode *lqinode = sb_dqopt(sb)->files[dquot->dq_type];
9e33d69f5   Jan Kara   ocfs2: Implementa...
901
  	int status;
f64dd44eb   Jan Kara   ocfs2: Do not map...
902
903
  	status = ocfs2_read_quota_phys_block(lqinode, od->dq_local_phys_blk,
  					     &bh);
85eb8b73d   Joel Becker   ocfs2: Fix ocfs2_...
904
  	if (status) {
9e33d69f5   Jan Kara   ocfs2: Implementa...
905
906
907
  		mlog_errno(status);
  		goto out;
  	}
f64dd44eb   Jan Kara   ocfs2: Do not map...
908
  	status = ocfs2_modify_bh(lqinode, bh, olq_set_dquot, od);
9e33d69f5   Jan Kara   ocfs2: Implementa...
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  out:
  	brelse(bh);
  	return status;
  }
  
  /* Find free entry in local quota file */
  static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
  						       int type,
  						       int *offset)
  {
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  	struct ocfs2_quota_chunk *chunk;
  	struct ocfs2_local_disk_chunk *dchunk;
  	int found = 0, len;
  
  	list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  		dchunk = (struct ocfs2_local_disk_chunk *)
  						chunk->qc_headerbh->b_data;
  		if (le32_to_cpu(dchunk->dqc_free) > 0) {
  			found = 1;
  			break;
  		}
  	}
  	if (!found)
  		return NULL;
  
  	if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  		len = ol_chunk_entries(sb);
  	} else {
  		len = (oinfo->dqi_blocks -
  		       ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  		      * ol_quota_entries_per_block(sb);
  	}
939255798   Akinobu Mita   ocfs2: avoid unal...
947
  	found = ocfs2_find_next_zero_bit_unaligned(dchunk->dqc_bitmap, len, 0);
9e33d69f5   Jan Kara   ocfs2: Implementa...
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
  	/* We failed? */
  	if (found == len) {
  		mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
  		     " entries free (type=%d)
  ", chunk->qc_num,
  		     le32_to_cpu(dchunk->dqc_free), type);
  		return ERR_PTR(-EIO);
  	}
  	*offset = found;
  	return chunk;
  }
  
  /* Add new chunk to the local quota file */
  static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
  							struct super_block *sb,
  							int type,
  							int *offset)
  {
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  	struct inode *lqinode = sb_dqopt(sb)->files[type];
  	struct ocfs2_quota_chunk *chunk = NULL;
  	struct ocfs2_local_disk_chunk *dchunk;
  	int status;
  	handle_t *handle;
0e7f387bf   Jan Kara   ocfs2: Initialize...
973
  	struct buffer_head *bh = NULL, *dbh = NULL;
9e33d69f5   Jan Kara   ocfs2: Implementa...
974
975
976
  	u64 p_blkno;
  
  	/* We are protected by dqio_sem so no locking needed */
5693486ba   Joel Becker   ocfs2: Zero the t...
977
  	status = ocfs2_extend_no_holes(lqinode, NULL,
9e33d69f5   Jan Kara   ocfs2: Implementa...
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
  				       lqinode->i_size + 2 * sb->s_blocksize,
  				       lqinode->i_size);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  	status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  					  lqinode->i_size + 2 * sb->s_blocksize);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  
  	chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
  	if (!chunk) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto out;
  	}
0584974a7   Jan Kara   ocfs2: Define cre...
997
998
999
1000
  	/* Local quota info and two new blocks we initialize */
  	handle = ocfs2_start_trans(OCFS2_SB(sb),
  			OCFS2_LOCAL_QINFO_WRITE_CREDITS +
  			2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1001
1002
1003
1004
1005
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto out;
  	}
9e33d69f5   Jan Kara   ocfs2: Implementa...
1006

0e7f387bf   Jan Kara   ocfs2: Initialize...
1007
  	/* Initialize chunk header */
9e33d69f5   Jan Kara   ocfs2: Implementa...
1008
1009
  	status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
  					     &p_blkno, NULL, NULL);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1010
1011
  	if (status < 0) {
  		mlog_errno(status);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1012
  		goto out_trans;
9e33d69f5   Jan Kara   ocfs2: Implementa...
1013
1014
1015
1016
1017
  	}
  	bh = sb_getblk(sb, p_blkno);
  	if (!bh) {
  		status = -ENOMEM;
  		mlog_errno(status);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1018
  		goto out_trans;
9e33d69f5   Jan Kara   ocfs2: Implementa...
1019
1020
  	}
  	dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
8cb471e8f   Joel Becker   ocfs2: Take the i...
1021
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1022
  	status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
0e7f387bf   Jan Kara   ocfs2: Initialize...
1023
  					 OCFS2_JOURNAL_ACCESS_CREATE);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1024
1025
1026
1027
1028
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  	lock_buffer(bh);
df32b3343   Tao Ma   ocfs2/quota: spar...
1029
  	dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
9e33d69f5   Jan Kara   ocfs2: Implementa...
1030
1031
1032
  	memset(dchunk->dqc_bitmap, 0,
  	       sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
  	       OCFS2_QBLK_RESERVED_SPACE);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1033
  	unlock_buffer(bh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1034
  	ocfs2_journal_dirty(handle, bh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1035

0e7f387bf   Jan Kara   ocfs2: Initialize...
1036
  	/* Initialize new block with structures */
0e7f387bf   Jan Kara   ocfs2: Initialize...
1037
1038
  	status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks + 1,
  					     &p_blkno, NULL, NULL);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  	dbh = sb_getblk(sb, p_blkno);
  	if (!dbh) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto out_trans;
  	}
8cb471e8f   Joel Becker   ocfs2: Take the i...
1049
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), dbh);
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1050
  	status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), dbh,
0e7f387bf   Jan Kara   ocfs2: Initialize...
1051
1052
1053
1054
1055
1056
1057
1058
  					 OCFS2_JOURNAL_ACCESS_CREATE);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  	lock_buffer(dbh);
  	memset(dbh->b_data, 0, sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE);
  	unlock_buffer(dbh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1059
  	ocfs2_journal_dirty(handle, dbh);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1060
1061
  
  	/* Update local quotafile info */
9e33d69f5   Jan Kara   ocfs2: Implementa...
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
  	oinfo->dqi_blocks += 2;
  	oinfo->dqi_chunks++;
  	status = ocfs2_local_write_info(sb, type);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  	status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  
  	list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
  	chunk->qc_num = list_entry(chunk->qc_chunk.prev,
  				   struct ocfs2_quota_chunk,
  				   qc_chunk)->qc_num + 1;
  	chunk->qc_headerbh = bh;
  	*offset = 0;
  	return chunk;
  out_trans:
  	ocfs2_commit_trans(OCFS2_SB(sb), handle);
  out:
  	brelse(bh);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1086
  	brelse(dbh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
  	kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
  	return ERR_PTR(status);
  }
  
  /* Find free entry in local quota file */
  static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
  						       struct super_block *sb,
  						       int type,
  						       int *offset)
  {
  	struct mem_dqinfo *info = sb_dqinfo(sb, type);
  	struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  	struct ocfs2_quota_chunk *chunk;
  	struct inode *lqinode = sb_dqopt(sb)->files[type];
  	struct ocfs2_local_disk_chunk *dchunk;
  	int epb = ol_quota_entries_per_block(sb);
  	unsigned int chunk_blocks;
0e7f387bf   Jan Kara   ocfs2: Initialize...
1104
1105
  	struct buffer_head *bh;
  	u64 p_blkno;
9e33d69f5   Jan Kara   ocfs2: Implementa...
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
  	int status;
  	handle_t *handle;
  
  	if (list_empty(&oinfo->dqi_chunk))
  		return ocfs2_local_quota_add_chunk(sb, type, offset);
  	/* Is the last chunk full? */
  	chunk = list_entry(oinfo->dqi_chunk.prev,
  			struct ocfs2_quota_chunk, qc_chunk);
  	chunk_blocks = oinfo->dqi_blocks -
  			ol_quota_chunk_block(sb, chunk->qc_num) - 1;
  	if (ol_chunk_blocks(sb) == chunk_blocks)
  		return ocfs2_local_quota_add_chunk(sb, type, offset);
  
  	/* We are protected by dqio_sem so no locking needed */
5693486ba   Joel Becker   ocfs2: Zero the t...
1120
  	status = ocfs2_extend_no_holes(lqinode, NULL,
9e33d69f5   Jan Kara   ocfs2: Implementa...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
  				       lqinode->i_size + sb->s_blocksize,
  				       lqinode->i_size);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  	status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  					  lqinode->i_size + sb->s_blocksize);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
0e7f387bf   Jan Kara   ocfs2: Initialize...
1133
1134
  
  	/* Get buffer from the just added block */
0e7f387bf   Jan Kara   ocfs2: Initialize...
1135
1136
  	status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
  					     &p_blkno, NULL, NULL);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  	bh = sb_getblk(sb, p_blkno);
  	if (!bh) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto out;
  	}
8cb471e8f   Joel Becker   ocfs2: Take the i...
1147
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(lqinode), bh);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1148

0584974a7   Jan Kara   ocfs2: Define cre...
1149
1150
1151
1152
  	/* Local quota info, chunk header and the new block we initialize */
  	handle = ocfs2_start_trans(OCFS2_SB(sb),
  			OCFS2_LOCAL_QINFO_WRITE_CREDITS +
  			2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1153
1154
1155
1156
1157
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto out;
  	}
0e7f387bf   Jan Kara   ocfs2: Initialize...
1158
  	/* Zero created block */
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1159
  	status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode), bh,
0e7f387bf   Jan Kara   ocfs2: Initialize...
1160
1161
1162
1163
1164
1165
1166
1167
  				 OCFS2_JOURNAL_ACCESS_CREATE);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  	lock_buffer(bh);
  	memset(bh->b_data, 0, sb->s_blocksize);
  	unlock_buffer(bh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1168
  	ocfs2_journal_dirty(handle, bh);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1169
  	/* Update chunk header */
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1170
1171
  	status = ocfs2_journal_access_dq(handle, INODE_CACHE(lqinode),
  					 chunk->qc_headerbh,
9e33d69f5   Jan Kara   ocfs2: Implementa...
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
  				 OCFS2_JOURNAL_ACCESS_WRITE);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  
  	dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
  	lock_buffer(chunk->qc_headerbh);
  	le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
  	unlock_buffer(chunk->qc_headerbh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1182
  	ocfs2_journal_dirty(handle, chunk->qc_headerbh);
0e7f387bf   Jan Kara   ocfs2: Initialize...
1183
  	/* Update file header */
9e33d69f5   Jan Kara   ocfs2: Implementa...
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
  	oinfo->dqi_blocks++;
  	status = ocfs2_local_write_info(sb, type);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_trans;
  	}
  
  	status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  	*offset = chunk_blocks * epb;
  	return chunk;
  out_trans:
  	ocfs2_commit_trans(OCFS2_SB(sb), handle);
  out:
  	return ERR_PTR(status);
  }
df32b3343   Tao Ma   ocfs2/quota: spar...
1203
  static void olq_alloc_dquot(struct buffer_head *bh, void *private)
9e33d69f5   Jan Kara   ocfs2: Implementa...
1204
1205
1206
1207
1208
  {
  	int *offset = private;
  	struct ocfs2_local_disk_chunk *dchunk;
  
  	dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
939255798   Akinobu Mita   ocfs2: avoid unal...
1209
  	ocfs2_set_bit_unaligned(*offset, dchunk->dqc_bitmap);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1210
1211
1212
1213
  	le32_add_cpu(&dchunk->dqc_free, -1);
  }
  
  /* Create dquot in the local file for given id */
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
1214
  int ocfs2_create_local_dquot(struct dquot *dquot)
9e33d69f5   Jan Kara   ocfs2: Implementa...
1215
1216
1217
1218
1219
1220
1221
1222
  {
  	struct super_block *sb = dquot->dq_sb;
  	int type = dquot->dq_type;
  	struct inode *lqinode = sb_dqopt(sb)->files[type];
  	struct ocfs2_quota_chunk *chunk;
  	struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  	int offset;
  	int status;
f64dd44eb   Jan Kara   ocfs2: Do not map...
1223
  	u64 pcount;
9e33d69f5   Jan Kara   ocfs2: Implementa...
1224

f64dd44eb   Jan Kara   ocfs2: Do not map...
1225
  	down_write(&OCFS2_I(lqinode)->ip_alloc_sem);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1226
1227
1228
  	chunk = ocfs2_find_free_entry(sb, type, &offset);
  	if (!chunk) {
  		chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
f64dd44eb   Jan Kara   ocfs2: Do not map...
1229
1230
1231
1232
  		if (IS_ERR(chunk)) {
  			status = PTR_ERR(chunk);
  			goto out;
  		}
9e33d69f5   Jan Kara   ocfs2: Implementa...
1233
  	} else if (IS_ERR(chunk)) {
f64dd44eb   Jan Kara   ocfs2: Do not map...
1234
1235
  		status = PTR_ERR(chunk);
  		goto out;
9e33d69f5   Jan Kara   ocfs2: Implementa...
1236
1237
1238
  	}
  	od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
  	od->dq_chunk = chunk;
f64dd44eb   Jan Kara   ocfs2: Do not map...
1239
1240
1241
1242
1243
  	status = ocfs2_extent_map_get_blocks(lqinode,
  				     ol_dqblk_block(sb, chunk->qc_num, offset),
  				     &od->dq_local_phys_blk,
  				     &pcount,
  				     NULL);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
  
  	/* Initialize dquot structure on disk */
  	status = ocfs2_local_write_dquot(dquot);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  
  	/* Mark structure as allocated */
  	status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
  				 &offset);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  out:
f64dd44eb   Jan Kara   ocfs2: Do not map...
1260
  	up_write(&OCFS2_I(lqinode)->ip_alloc_sem);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1261
1262
  	return status;
  }
fb8dd8d78   Jan Kara   ocfs2: Fix quota ...
1263
1264
1265
1266
1267
  /*
   * Release dquot structure from local quota file. ocfs2_release_dquot() has
   * already started a transaction and written all changes to global quota file
   */
  int ocfs2_local_release_dquot(handle_t *handle, struct dquot *dquot)
9e33d69f5   Jan Kara   ocfs2: Implementa...
1268
1269
1270
1271
1272
1273
1274
  {
  	int status;
  	int type = dquot->dq_type;
  	struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  	struct super_block *sb = dquot->dq_sb;
  	struct ocfs2_local_disk_chunk *dchunk;
  	int offset;
9e33d69f5   Jan Kara   ocfs2: Implementa...
1275

0cf2f7632   Joel Becker   ocfs2: Pass struc...
1276
1277
  	status = ocfs2_journal_access_dq(handle,
  			INODE_CACHE(sb_dqopt(sb)->files[type]),
9e33d69f5   Jan Kara   ocfs2: Implementa...
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  			od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out;
  	}
  	offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
  					     od->dq_local_off);
  	dchunk = (struct ocfs2_local_disk_chunk *)
  			(od->dq_chunk->qc_headerbh->b_data);
  	/* Mark structure as freed */
  	lock_buffer(od->dq_chunk->qc_headerbh);
939255798   Akinobu Mita   ocfs2: avoid unal...
1289
  	ocfs2_clear_bit_unaligned(offset, dchunk->dqc_bitmap);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1290
1291
  	le32_add_cpu(&dchunk->dqc_free, 1);
  	unlock_buffer(od->dq_chunk->qc_headerbh);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1292
  	ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
9e33d69f5   Jan Kara   ocfs2: Implementa...
1293
1294
1295
1296
1297
1298
1299
  out:
  	/* Clear the read bit so that next time someone uses this
  	 * dquot he reads fresh info from disk and allocates local
  	 * dquot structure */
  	clear_bit(DQ_READ_B, &dquot->dq_flags);
  	return status;
  }
1472da5fd   Alexey Dobriyan   const: struct quo...
1300
  static const struct quota_format_ops ocfs2_format_ops = {
9e33d69f5   Jan Kara   ocfs2: Implementa...
1301
1302
1303
1304
  	.check_quota_file	= ocfs2_local_check_quota_file,
  	.read_file_info		= ocfs2_local_read_info,
  	.write_file_info	= ocfs2_global_write_info,
  	.free_file_info		= ocfs2_local_free_info,
9e33d69f5   Jan Kara   ocfs2: Implementa...
1305
1306
1307
1308
1309
1310
1311
  };
  
  struct quota_format_type ocfs2_quota_format = {
  	.qf_fmt_id = QFMT_OCFS2,
  	.qf_ops = &ocfs2_format_ops,
  	.qf_owner = THIS_MODULE
  };