2 * Implementation of new quotafile format
4 * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
16 #include "quotaio_tree.h"
19 typedef char *dqbuf_t;
21 #define freedqbuf(buf) ext2fs_free_mem(&buf)
23 static inline dqbuf_t getdqbuf(void)
26 if (ext2fs_get_memzero(QT_BLKSIZE, &buf)) {
27 log_err("Failed to allocate dqbuf");
34 /* Is given dquot empty? */
35 int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
39 for (i = 0; i < info->dqi_entry_size; i++)
45 int qtree_dqstr_in_blk(struct qtree_mem_dqinfo *info)
47 return (QT_BLKSIZE - sizeof(struct qt_disk_dqdbheader)) /
51 static int get_index(qid_t id, int depth)
53 return (id >> ((QT_TREEDEPTH - depth - 1) * 8)) & 0xff;
56 static inline void mark_quotafile_info_dirty(struct quota_handle *h)
58 h->qh_io_flags |= IOFL_INFODIRTY;
61 /* Read given block */
62 static void read_blk(struct quota_handle *h, unsigned int blk, dqbuf_t buf)
66 err = h->e2fs_read(&h->qh_qf, blk << QT_BLKSIZE_BITS, buf,
69 log_err("Cannot read block %u: %s", blk, strerror(errno));
70 else if (err != QT_BLKSIZE)
71 memset(buf + err, 0, QT_BLKSIZE - err);
75 static int write_blk(struct quota_handle *h, unsigned int blk, dqbuf_t buf)
79 err = h->e2fs_write(&h->qh_qf, blk << QT_BLKSIZE_BITS, buf,
81 if (err < 0 && errno != ENOSPC)
82 log_err("Cannot write block (%u): %s", blk, strerror(errno));
83 if (err != QT_BLKSIZE)
88 /* Get free block in file (either from free list or create new one) */
89 static int get_free_dqblk(struct quota_handle *h)
91 dqbuf_t buf = getdqbuf();
92 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
93 struct qtree_mem_dqinfo *info = &h->qh_info.u.v2_mdqi.dqi_qtree;
99 if (info->dqi_free_blk) {
100 blk = info->dqi_free_blk;
101 read_blk(h, blk, buf);
102 info->dqi_free_blk = ext2fs_le32_to_cpu(dh->dqdh_next_free);
104 memset(buf, 0, QT_BLKSIZE);
105 /* Assure block allocation... */
106 if (write_blk(h, info->dqi_blocks, buf) < 0) {
108 log_err("Cannot allocate new quota block "
109 "(out of disk space).");
112 blk = info->dqi_blocks++;
114 mark_quotafile_info_dirty(h);
119 /* Put given block to free list */
120 static void put_free_dqblk(struct quota_handle *h, dqbuf_t buf,
123 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
124 struct qtree_mem_dqinfo *info = &h->qh_info.u.v2_mdqi.dqi_qtree;
126 dh->dqdh_next_free = ext2fs_cpu_to_le32(info->dqi_free_blk);
127 dh->dqdh_prev_free = ext2fs_cpu_to_le32(0);
128 dh->dqdh_entries = ext2fs_cpu_to_le16(0);
129 info->dqi_free_blk = blk;
130 mark_quotafile_info_dirty(h);
131 write_blk(h, blk, buf);
134 /* Remove given block from the list of blocks with free entries */
135 static void remove_free_dqentry(struct quota_handle *h, dqbuf_t buf,
138 dqbuf_t tmpbuf = getdqbuf();
139 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
140 unsigned int nextblk = ext2fs_le32_to_cpu(dh->dqdh_next_free), prevblk =
142 ext2fs_le32_to_cpu(dh->dqdh_prev_free);
148 read_blk(h, nextblk, tmpbuf);
149 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
151 write_blk(h, nextblk, tmpbuf);
154 read_blk(h, prevblk, tmpbuf);
155 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
157 write_blk(h, prevblk, tmpbuf);
159 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = nextblk;
160 mark_quotafile_info_dirty(h);
163 dh->dqdh_next_free = dh->dqdh_prev_free = ext2fs_cpu_to_le32(0);
164 write_blk(h, blk, buf); /* No matter whether write succeeds
165 * block is out of list */
168 /* Insert given block to the beginning of list with free entries */
169 static void insert_free_dqentry(struct quota_handle *h, dqbuf_t buf,
172 dqbuf_t tmpbuf = getdqbuf();
173 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
174 struct qtree_mem_dqinfo *info = &h->qh_info.u.v2_mdqi.dqi_qtree;
179 dh->dqdh_next_free = ext2fs_cpu_to_le32(info->dqi_free_entry);
180 dh->dqdh_prev_free = ext2fs_cpu_to_le32(0);
181 write_blk(h, blk, buf);
182 if (info->dqi_free_entry) {
183 read_blk(h, info->dqi_free_entry, tmpbuf);
184 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
185 ext2fs_cpu_to_le32(blk);
186 write_blk(h, info->dqi_free_entry, tmpbuf);
189 info->dqi_free_entry = blk;
190 mark_quotafile_info_dirty(h);
193 /* Find space for dquot */
194 static unsigned int find_free_dqentry(struct quota_handle *h,
195 struct dquot *dquot, int *err)
198 struct qt_disk_dqdbheader *dh;
199 struct qtree_mem_dqinfo *info = &h->qh_info.u.v2_mdqi.dqi_qtree;
210 dh = (struct qt_disk_dqdbheader *)buf;
211 if (info->dqi_free_entry) {
212 blk = info->dqi_free_entry;
213 read_blk(h, blk, buf);
215 blk = get_free_dqblk(h);
221 memset(buf, 0, QT_BLKSIZE);
222 info->dqi_free_entry = blk;
223 mark_quotafile_info_dirty(h);
226 /* Block will be full? */
227 if (ext2fs_le16_to_cpu(dh->dqdh_entries) + 1 >=
228 qtree_dqstr_in_blk(info))
229 remove_free_dqentry(h, buf, blk);
232 ext2fs_cpu_to_le16(ext2fs_le16_to_cpu(dh->dqdh_entries) + 1);
233 /* Find free structure in block */
234 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
236 i < qtree_dqstr_in_blk(info) && !qtree_entry_unused(info, ddquot);
238 ddquot += info->dqi_entry_size;
240 if (i == qtree_dqstr_in_blk(info))
241 log_err("find_free_dqentry(): Data block full unexpectedly.");
243 write_blk(h, blk, buf);
244 dquot->dq_dqb.u.v2_mdqb.dqb_off =
245 (blk << QT_BLKSIZE_BITS) + sizeof(struct qt_disk_dqdbheader) +
246 i * info->dqi_entry_size;
251 /* Insert reference to structure into the trie */
252 static int do_insert_tree(struct quota_handle *h, struct dquot *dquot,
253 unsigned int * treeblk, int depth)
256 int newson = 0, newact = 0;
261 log_debug("inserting in tree: treeblk=%u, depth=%d", *treeblk, depth);
267 ret = get_free_dqblk(h);
271 memset(buf, 0, QT_BLKSIZE);
274 read_blk(h, *treeblk, buf);
277 ref = (__le32 *) buf;
278 newblk = ext2fs_le32_to_cpu(ref[get_index(dquot->dq_id, depth)]);
281 if (depth == QT_TREEDEPTH - 1) {
283 log_err("Inserting already present quota entry "
285 ref[get_index(dquot->dq_id, depth)]);
286 newblk = find_free_dqentry(h, dquot, &ret);
288 ret = do_insert_tree(h, dquot, &newblk, depth + 1);
291 if (newson && ret >= 0) {
292 ref[get_index(dquot->dq_id, depth)] =
293 ext2fs_cpu_to_le32(newblk);
294 write_blk(h, *treeblk, buf);
295 } else if (newact && ret < 0) {
296 put_free_dqblk(h, buf, *treeblk);
304 /* Wrapper for inserting quota structure into tree */
305 static void dq_insert_tree(struct quota_handle *h, struct dquot *dquot)
307 unsigned int tmp = QT_TREEOFF;
309 if (do_insert_tree(h, dquot, &tmp, 0) < 0)
310 log_err("Cannot write quota (id %u): %s",
311 (unsigned int) dquot->dq_id, strerror(errno));
314 /* Write dquot to file */
315 void qtree_write_dquot(struct dquot *dquot)
320 struct quota_handle *h = dquot->dq_h;
321 struct qtree_mem_dqinfo *info =
322 &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
323 log_debug("writing ddquot 1: off=%llu, info->dqi_entry_size=%u",
324 dquot->dq_dqb.u.v2_mdqb.dqb_off,
325 info->dqi_entry_size);
326 retval = ext2fs_get_mem(info->dqi_entry_size, &ddquot);
329 log_err("Quota write failed (id %u): %s",
330 (unsigned int)dquot->dq_id, strerror(errno));
333 memset(ddquot, 0, info->dqi_entry_size);
335 if (!dquot->dq_dqb.u.v2_mdqb.dqb_off)
336 dq_insert_tree(dquot->dq_h, dquot);
337 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
338 log_debug("writing ddquot 2: off=%llu, info->dqi_entry_size=%u",
339 dquot->dq_dqb.u.v2_mdqb.dqb_off,
340 info->dqi_entry_size);
341 ret = h->e2fs_write(&h->qh_qf, dquot->dq_dqb.u.v2_mdqb.dqb_off, ddquot,
342 info->dqi_entry_size);
344 if (ret != info->dqi_entry_size) {
347 log_err("Quota write failed (id %u): %s",
348 (unsigned int)dquot->dq_id, strerror(errno));
350 ext2fs_free_mem(&ddquot);
353 /* Free dquot entry in data block */
354 static void free_dqentry(struct quota_handle *h, struct dquot *dquot,
357 struct qt_disk_dqdbheader *dh;
358 struct qtree_mem_dqinfo *info = &h->qh_info.u.v2_mdqi.dqi_qtree;
359 dqbuf_t buf = getdqbuf();
364 if (dquot->dq_dqb.u.v2_mdqb.dqb_off >> QT_BLKSIZE_BITS != blk)
365 log_err("Quota structure has offset to other block (%u) "
366 "than it should (%u).", blk,
367 (unsigned int) (dquot->dq_dqb.u.v2_mdqb.dqb_off >>
370 read_blk(h, blk, buf);
371 dh = (struct qt_disk_dqdbheader *)buf;
373 ext2fs_cpu_to_le16(ext2fs_le16_to_cpu(dh->dqdh_entries) - 1);
375 if (!ext2fs_le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
376 remove_free_dqentry(h, buf, blk);
377 put_free_dqblk(h, buf, blk);
379 memset(buf + (dquot->dq_dqb.u.v2_mdqb.dqb_off &
380 ((1 << QT_BLKSIZE_BITS) - 1)),
381 0, info->dqi_entry_size);
383 /* First free entry? */
384 if (ext2fs_le16_to_cpu(dh->dqdh_entries) ==
385 qtree_dqstr_in_blk(info) - 1)
386 /* This will also write data block */
387 insert_free_dqentry(h, buf, blk);
389 write_blk(h, blk, buf);
391 dquot->dq_dqb.u.v2_mdqb.dqb_off = 0;
395 /* Remove reference to dquot from tree */
396 static void remove_tree(struct quota_handle *h, struct dquot *dquot,
397 unsigned int * blk, int depth)
399 dqbuf_t buf = getdqbuf();
401 __le32 *ref = (__le32 *) buf;
406 read_blk(h, *blk, buf);
407 newblk = ext2fs_le32_to_cpu(ref[get_index(dquot->dq_id, depth)]);
408 if (depth == QT_TREEDEPTH - 1) {
409 free_dqentry(h, dquot, newblk);
412 remove_tree(h, dquot, &newblk, depth + 1);
418 ref[get_index(dquot->dq_id, depth)] = ext2fs_cpu_to_le32(0);
420 /* Block got empty? */
421 for (i = 0; i < QT_BLKSIZE && !buf[i]; i++);
423 /* Don't put the root block into the free block list */
424 if (i == QT_BLKSIZE && *blk != QT_TREEOFF) {
425 put_free_dqblk(h, buf, *blk);
428 write_blk(h, *blk, buf);
434 /* Delete dquot from tree */
435 void qtree_delete_dquot(struct dquot *dquot)
437 unsigned int tmp = QT_TREEOFF;
439 if (!dquot->dq_dqb.u.v2_mdqb.dqb_off) /* Even not allocated? */
441 remove_tree(dquot->dq_h, dquot, &tmp, 0);
444 /* Find entry in block */
445 static ext2_loff_t find_block_dqentry(struct quota_handle *h,
446 struct dquot *dquot, unsigned int blk)
448 struct qtree_mem_dqinfo *info = &h->qh_info.u.v2_mdqi.dqi_qtree;
449 dqbuf_t buf = getdqbuf();
451 char *ddquot = buf + sizeof(struct qt_disk_dqdbheader);
456 read_blk(h, blk, buf);
458 i < qtree_dqstr_in_blk(info) && !info->dqi_ops->is_id(ddquot, dquot);
460 ddquot += info->dqi_entry_size;
462 if (i == qtree_dqstr_in_blk(info))
463 log_err("Quota for id %u referenced but not present.",
466 return (blk << QT_BLKSIZE_BITS) + sizeof(struct qt_disk_dqdbheader) +
467 i * info->dqi_entry_size;
470 /* Find entry for given id in the tree */
471 static ext2_loff_t find_tree_dqentry(struct quota_handle *h,
473 unsigned int blk, int depth)
475 dqbuf_t buf = getdqbuf();
477 __le32 *ref = (__le32 *) buf;
482 read_blk(h, blk, buf);
484 blk = ext2fs_le32_to_cpu(ref[get_index(dquot->dq_id, depth)]);
485 if (!blk) /* No reference? */
487 if (depth < QT_TREEDEPTH - 1)
488 ret = find_tree_dqentry(h, dquot, blk, depth + 1);
490 ret = find_block_dqentry(h, dquot, blk);
496 /* Find entry for given id in the tree - wrapper function */
497 static inline ext2_loff_t find_dqentry(struct quota_handle *h,
500 return find_tree_dqentry(h, dquot, QT_TREEOFF, 0);
504 * Read dquot from disk.
506 struct dquot *qtree_read_dquot(struct quota_handle *h, qid_t id)
508 struct qtree_mem_dqinfo *info = &h->qh_info.u.v2_mdqi.dqi_qtree;
512 struct dquot *dquot = get_empty_dquot();
516 if (ext2fs_get_mem(info->dqi_entry_size, &ddquot)) {
517 ext2fs_free_mem(&dquot);
523 dquot->dq_dqb.u.v2_mdqb.dqb_off = 0;
524 memset(&dquot->dq_dqb, 0, sizeof(struct util_dqblk));
526 offset = find_dqentry(h, dquot);
528 dquot->dq_dqb.u.v2_mdqb.dqb_off = offset;
529 ret = h->e2fs_read(&h->qh_qf, offset, ddquot,
530 info->dqi_entry_size);
531 if (ret != info->dqi_entry_size) {
534 log_err("Cannot read quota structure for id %u: %s",
535 dquot->dq_id, strerror(errno));
537 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
539 ext2fs_free_mem(&ddquot);
543 static int check_reference(struct quota_handle *h, unsigned int blk)
545 if (blk >= h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks) {
546 log_err("Illegal reference (%u >= %u) in %s quota file",
547 blk, h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks,
548 quota_type2name(h->qh_type));
555 * Scan all dquots in file and call callback on each
557 #define set_bit(bmp, ind) ((bmp)[(ind) >> 3] |= (1 << ((ind) & 7)))
558 #define get_bit(bmp, ind) ((bmp)[(ind) >> 3] & (1 << ((ind) & 7)))
560 static int report_block(struct dquot *dquot, unsigned int blk, char *bitmap,
561 int (*process_dquot) (struct dquot *, void *),
564 struct qtree_mem_dqinfo *info =
565 &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
566 dqbuf_t buf = getdqbuf();
567 struct qt_disk_dqdbheader *dh;
574 set_bit(bitmap, blk);
575 read_blk(dquot->dq_h, blk, buf);
576 dh = (struct qt_disk_dqdbheader *)buf;
577 ddata = buf + sizeof(struct qt_disk_dqdbheader);
578 entries = ext2fs_le16_to_cpu(dh->dqdh_entries);
579 for (i = 0; i < qtree_dqstr_in_blk(info);
580 i++, ddata += info->dqi_entry_size)
581 if (!qtree_entry_unused(info, ddata)) {
582 dquot->dq_dqb.u.v2_mdqb.dqb_off =
583 (blk << QT_BLKSIZE_BITS) +
584 sizeof(struct qt_disk_dqdbheader) +
585 i * info->dqi_entry_size;
586 info->dqi_ops->disk2mem_dqblk(dquot, ddata);
587 if (process_dquot(dquot, data) < 0)
594 static int report_tree(struct dquot *dquot, unsigned int blk, int depth,
596 int (*process_dquot) (struct dquot *, void *),
599 int entries = 0, ret, i;
600 dqbuf_t buf = getdqbuf();
601 __le32 *ref = (__le32 *) buf;
606 read_blk(dquot->dq_h, blk, buf);
607 if (depth == QT_TREEDEPTH - 1) {
608 for (i = 0; i < QT_BLKSIZE >> 2; i++) {
609 blk = ext2fs_le32_to_cpu(ref[i]);
610 if (check_reference(dquot->dq_h, blk)) {
614 if (blk && !get_bit(bitmap, blk)) {
615 ret = report_block(dquot, blk, bitmap,
616 process_dquot, data);
625 for (i = 0; i < QT_BLKSIZE >> 2; i++) {
626 blk = ext2fs_le32_to_cpu(ref[i]);
628 if (check_reference(dquot->dq_h, blk)) {
632 ret = report_tree(dquot, blk, depth + 1,
633 bitmap, process_dquot,
648 static unsigned int find_set_bits(char *bmp, int blocks)
650 unsigned int used = 0;
653 for (i = 0; i < blocks; i++)
659 int qtree_scan_dquots(struct quota_handle *h,
660 int (*process_dquot) (struct dquot *, void *),
665 struct v2_mem_dqinfo *v2info = &h->qh_info.u.v2_mdqi;
666 struct qtree_mem_dqinfo *info = &v2info->dqi_qtree;
667 struct dquot *dquot = get_empty_dquot();
673 if (ext2fs_get_memzero((info->dqi_blocks + 7) >> 3, &bitmap)) {
674 ext2fs_free_mem(&dquot);
677 ret = report_tree(dquot, QT_TREEOFF, 0, bitmap, process_dquot, data);
680 v2info->dqi_used_entries = ret;
681 v2info->dqi_data_blocks = find_set_bits(bitmap, info->dqi_blocks);
684 ext2fs_free_mem(&bitmap);
685 ext2fs_free_mem(&dquot);