Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	vfsv0 quota IO operations on file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/dqblk_v2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "quota_tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) MODULE_AUTHOR("Jan Kara");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) MODULE_DESCRIPTION("Quota trie support");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define __QUOTA_QT_PARANOIA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int __get_index(struct qtree_mem_dqinfo *info, qid_t id, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	unsigned int epb = info->dqi_usable_bs >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	depth = info->dqi_qtree_depth - depth - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	while (depth--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		id /= epb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return id % epb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int get_index(struct qtree_mem_dqinfo *info, struct kqid qid, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	qid_t id = from_kqid(&init_user_ns, qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return __get_index(info, id, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) /* Number of entries in one blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return (info->dqi_usable_bs - sizeof(struct qt_disk_dqdbheader))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	       / info->dqi_entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static char *getdqbuf(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	char *buf = kmalloc(size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		       "VFS: Not enough memory for quota buffers.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static ssize_t read_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct super_block *sb = info->dqi_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	memset(buf, 0, info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return sb->s_op->quota_read(sb, info->dqi_type, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	       info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct super_block *sb = info->dqi_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	ret = sb->s_op->quota_write(sb, info->dqi_type, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	       info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (ret != info->dqi_usable_bs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		quota_error(sb, "dquota write failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) /* Remove empty block from list and return it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int get_free_dqblk(struct qtree_mem_dqinfo *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	int ret, blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (info->dqi_free_blk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		blk = info->dqi_free_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		ret = read_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		memset(buf, 0, info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		/* Assure block allocation... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		ret = write_blk(info, info->dqi_blocks, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		blk = info->dqi_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	mark_info_dirty(info->dqi_sb, info->dqi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	ret = blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Insert empty block to the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	dh->dqdh_prev_free = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	dh->dqdh_entries = cpu_to_le16(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	err = write_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	info->dqi_free_blk = blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	mark_info_dirty(info->dqi_sb, info->dqi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Remove given block from the list of blocks with free entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			       uint blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	char *tmpbuf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	uint nextblk = le32_to_cpu(dh->dqdh_next_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (!tmpbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (nextblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		err = read_blk(info, nextblk, tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 							dh->dqdh_prev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		err = write_blk(info, nextblk, tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (prevblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		err = read_blk(info, prevblk, tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 							dh->dqdh_next_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		err = write_blk(info, prevblk, tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		info->dqi_free_entry = nextblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		mark_info_dirty(info->dqi_sb, info->dqi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	kfree(tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/* No matter whether write succeeds block is out of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (write_blk(info, blk, buf) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		quota_error(info->dqi_sb, "Can't write block (%u) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			    "with free entries", blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	kfree(tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* Insert given block to the beginning of list with free entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			       uint blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	char *tmpbuf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (!tmpbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	dh->dqdh_prev_free = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	err = write_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (info->dqi_free_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		err = read_blk(info, info->dqi_free_entry, tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 							cpu_to_le32(blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		err = write_blk(info, info->dqi_free_entry, tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	kfree(tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	info->dqi_free_entry = blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	mark_info_dirty(info->dqi_sb, info->dqi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	kfree(tmpbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* Is the entry in the block free? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	for (i = 0; i < info->dqi_entry_size; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (disk[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) EXPORT_SYMBOL(qtree_entry_unused);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* Find space for dquot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			      struct dquot *dquot, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	uint blk, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct qt_disk_dqdbheader *dh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	char *ddquot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	*err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		*err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	dh = (struct qt_disk_dqdbheader *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (info->dqi_free_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		blk = info->dqi_free_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		*err = read_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (*err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		blk = get_free_dqblk(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if ((int)blk < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			*err = blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		memset(buf, 0, info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		/* This is enough as the block is already zeroed and the entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		 * list is empty... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		info->dqi_free_entry = blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		mark_info_dirty(dquot->dq_sb, dquot->dq_id.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	/* Block will be full? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		*err = remove_free_dqentry(info, buf, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		if (*err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			quota_error(dquot->dq_sb, "Can't remove block (%u) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				    "from entry free list", blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	le16_add_cpu(&dh->dqdh_entries, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	/* Find free structure in block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	ddquot = buf + sizeof(struct qt_disk_dqdbheader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		if (qtree_entry_unused(info, ddquot))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		ddquot += info->dqi_entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #ifdef __QUOTA_QT_PARANOIA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (i == qtree_dqstr_in_blk(info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		quota_error(dquot->dq_sb, "Data block full but it shouldn't");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		*err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	*err = write_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (*err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		quota_error(dquot->dq_sb, "Can't write quota data block %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			    blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	dquot->dq_off = ((loff_t)blk << info->dqi_blocksize_bits) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			sizeof(struct qt_disk_dqdbheader) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			i * info->dqi_entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* Insert reference to structure into the trie */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			  uint *treeblk, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	int ret = 0, newson = 0, newact = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	__le32 *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	uint newblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (!*treeblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		ret = get_free_dqblk(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		*treeblk = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		memset(buf, 0, info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		newact = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		ret = read_blk(info, *treeblk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			quota_error(dquot->dq_sb, "Can't read tree quota "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				    "block %u", *treeblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	ref = (__le32 *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (!newblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		newson = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (depth == info->dqi_qtree_depth - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) #ifdef __QUOTA_QT_PARANOIA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (newblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			quota_error(dquot->dq_sb, "Inserting already present "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				    "quota entry (block %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				    le32_to_cpu(ref[get_index(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 						dquot->dq_id, depth)]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		newblk = find_free_dqentry(info, dquot, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		ret = do_insert_tree(info, dquot, &newblk, depth+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (newson && ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		ref[get_index(info, dquot->dq_id, depth)] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 							cpu_to_le32(newblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		ret = write_blk(info, *treeblk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	} else if (newact && ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		put_free_dqblk(info, buf, *treeblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /* Wrapper for inserting quota structure into tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				 struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	int tmp = QT_TREEOFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) #ifdef __QUOTA_QT_PARANOIA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (info->dqi_blocks <= QT_TREEOFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		quota_error(dquot->dq_sb, "Quota tree root isn't allocated!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return do_insert_tree(info, dquot, &tmp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  * We don't have to be afraid of deadlocks as we never have quotas on quota
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  * files...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	int type = dquot->dq_id.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	struct super_block *sb = dquot->dq_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	char *ddquot = getdqbuf(info->dqi_entry_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (!ddquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* dq_off is guarded by dqio_sem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (!dquot->dq_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		ret = dq_insert_tree(info, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			quota_error(sb, "Error %zd occurred while creating "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 				    "quota", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			kfree(ddquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	spin_lock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	spin_unlock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 				    dquot->dq_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (ret != info->dqi_entry_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		quota_error(sb, "dquota write failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	dqstats_inc(DQST_WRITES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	kfree(ddquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) EXPORT_SYMBOL(qtree_write_dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /* Free dquot entry in data block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			uint blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	struct qt_disk_dqdbheader *dh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		quota_error(dquot->dq_sb, "Quota structure has offset to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			"other block (%u) than it should (%u)", blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			(uint)(dquot->dq_off >> info->dqi_blocksize_bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	ret = read_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		quota_error(dquot->dq_sb, "Can't read quota data block %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			    blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	dh = (struct qt_disk_dqdbheader *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	le16_add_cpu(&dh->dqdh_entries, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (!le16_to_cpu(dh->dqdh_entries)) {	/* Block got free? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		ret = remove_free_dqentry(info, buf, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			ret = put_free_dqblk(info, buf, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			quota_error(dquot->dq_sb, "Can't move quota data block "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 				    "(%u) to free list", blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		memset(buf +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		       (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		       0, info->dqi_entry_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		if (le16_to_cpu(dh->dqdh_entries) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		    qtree_dqstr_in_blk(info) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			/* Insert will write block itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			ret = insert_free_dqentry(info, buf, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 				quota_error(dquot->dq_sb, "Can't insert quota "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 				    "data block (%u) to free entry list", blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 				goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			ret = write_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				quota_error(dquot->dq_sb, "Can't write quota "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 					    "data block %u", blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 				goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	dquot->dq_off = 0;	/* Quota is now unattached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /* Remove reference to dquot from tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		       uint *blk, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	uint newblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	__le32 *ref = (__le32 *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	ret = read_blk(info, *blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		quota_error(dquot->dq_sb, "Can't read quota data block %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			    *blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	if (newblk < QT_TREEOFF || newblk >= info->dqi_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			    newblk, info->dqi_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (depth == info->dqi_qtree_depth - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		ret = free_dqentry(info, dquot, newblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		newblk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		ret = remove_tree(info, dquot, &newblk, depth+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (ret >= 0 && !newblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		/* Block got empty? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		/* Don't put the root block into the free block list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		if (i == (info->dqi_usable_bs >> 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		    && *blk != QT_TREEOFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			put_free_dqblk(info, buf, *blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			*blk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			ret = write_blk(info, *blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 				quota_error(dquot->dq_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 					    "Can't write quota tree block %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 					    *blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* Delete dquot from tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	uint tmp = QT_TREEOFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (!dquot->dq_off)	/* Even not allocated? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return remove_tree(info, dquot, &tmp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) EXPORT_SYMBOL(qtree_delete_dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* Find entry in block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 				 struct dquot *dquot, uint blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	loff_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	char *ddquot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	ret = read_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		quota_error(dquot->dq_sb, "Can't read quota tree "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			    "block %u", blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	ddquot = buf + sizeof(struct qt_disk_dqdbheader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		if (info->dqi_ops->is_id(ddquot, dquot))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		ddquot += info->dqi_entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	if (i == qtree_dqstr_in_blk(info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		quota_error(dquot->dq_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			    "Quota for id %u referenced but not present",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			    from_kqid(&init_user_ns, dquot->dq_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		ret = ((loff_t)blk << info->dqi_blocksize_bits) + sizeof(struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		  qt_disk_dqdbheader) + i * info->dqi_entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* Find entry for given id in the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 				struct dquot *dquot, uint blk, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	loff_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	__le32 *ref = (__le32 *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	ret = read_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		quota_error(dquot->dq_sb, "Can't read quota tree block %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 			    blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	if (!blk)	/* No reference? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	if (blk < QT_TREEOFF || blk >= info->dqi_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 			    blk, info->dqi_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	if (depth < info->dqi_qtree_depth - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		ret = find_tree_dqentry(info, dquot, blk, depth+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		ret = find_block_dqentry(info, dquot, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) /* Find entry for given id in the tree - wrapper function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 				  struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	return find_tree_dqentry(info, dquot, QT_TREEOFF, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	int type = dquot->dq_id.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	struct super_block *sb = dquot->dq_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	loff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	char *ddquot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) #ifdef __QUOTA_QT_PARANOIA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	/* Invalidated quota? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (!sb_dqopt(dquot->dq_sb)->files[type]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		quota_error(sb, "Quota invalidated while reading!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	/* Do we know offset of the dquot entry in the quota file? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	if (!dquot->dq_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		offset = find_dqentry(info, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		if (offset <= 0) {	/* Entry not present? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 			if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 				quota_error(sb,"Can't read quota structure "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 					    "for id %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 					    from_kqid(&init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 						      dquot->dq_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			dquot->dq_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 			set_bit(DQ_FAKE_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			ret = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		dquot->dq_off = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	ddquot = getdqbuf(info->dqi_entry_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	if (!ddquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 				   dquot->dq_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	if (ret != info->dqi_entry_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		quota_error(sb, "Error while reading quota structure for id %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 			    from_kqid(&init_user_ns, dquot->dq_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		set_bit(DQ_FAKE_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		kfree(ddquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	spin_lock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	if (!dquot->dq_dqb.dqb_bhardlimit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	    !dquot->dq_dqb.dqb_bsoftlimit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	    !dquot->dq_dqb.dqb_ihardlimit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	    !dquot->dq_dqb.dqb_isoftlimit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		set_bit(DQ_FAKE_B, &dquot->dq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	spin_unlock(&dquot->dq_dqb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	kfree(ddquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	dqstats_inc(DQST_READS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) EXPORT_SYMBOL(qtree_read_dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /* Check whether dquot should not be deleted. We know we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)  * the only one operating on dquot (thanks to dq_lock) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	    !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		return qtree_delete_dquot(info, dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) EXPORT_SYMBOL(qtree_release_dquot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 			unsigned int blk, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	char *buf = getdqbuf(info->dqi_usable_bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	__le32 *ref = (__le32 *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	unsigned int epb = info->dqi_usable_bs >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	unsigned int level_inc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	for (i = depth; i < info->dqi_qtree_depth - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		level_inc *= epb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	ret = read_blk(info, blk, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		quota_error(info->dqi_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 			    "Can't read quota tree block %u", blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	for (i = __get_index(info, *id, depth); i < epb; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		if (ref[i] == cpu_to_le32(0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 			*id += level_inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		if (depth == info->dqi_qtree_depth - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 			goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		ret = find_next_id(info, id, le32_to_cpu(ref[i]), depth + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		if (ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	if (i == epb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) int qtree_get_next_id(struct qtree_mem_dqinfo *info, struct kqid *qid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	qid_t id = from_kqid(&init_user_ns, *qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	ret = find_next_id(info, &id, QT_TREEOFF, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	*qid = make_kqid(&init_user_ns, qid->type, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) EXPORT_SYMBOL(qtree_get_next_id);