blob: 583b7f7715f9a18df63e5a0f29db9449b2ef7143 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Jan Kara1ccd14b2008-09-22 05:54:49 +02002/*
3 * vfsv0 quota IO operations on file
4 */
5
6#include <linux/errno.h>
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/dqblk_v2.h>
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/quotaops.h>
15
16#include <asm/byteorder.h>
17
18#include "quota_tree.h"
19
20MODULE_AUTHOR("Jan Kara");
21MODULE_DESCRIPTION("Quota trie support");
22MODULE_LICENSE("GPL");
23
24#define __QUOTA_QT_PARANOIA
25
Jan Kara00663732016-01-25 20:39:27 +010026static int __get_index(struct qtree_mem_dqinfo *info, qid_t id, int depth)
Jan Kara1ccd14b2008-09-22 05:54:49 +020027{
28 unsigned int epb = info->dqi_usable_bs >> 2;
29
30 depth = info->dqi_qtree_depth - depth - 1;
31 while (depth--)
32 id /= epb;
33 return id % epb;
34}
35
Jan Kara00663732016-01-25 20:39:27 +010036static int get_index(struct qtree_mem_dqinfo *info, struct kqid qid, int depth)
37{
38 qid_t id = from_kqid(&init_user_ns, qid);
39
40 return __get_index(info, id, depth);
41}
42
Jan Kara1ccd14b2008-09-22 05:54:49 +020043/* Number of entries in one blocks */
Jan Kara7a2435d2009-01-27 01:47:11 +010044static int qtree_dqstr_in_blk(struct qtree_mem_dqinfo *info)
Jan Kara1ccd14b2008-09-22 05:54:49 +020045{
46 return (info->dqi_usable_bs - sizeof(struct qt_disk_dqdbheader))
47 / info->dqi_entry_size;
48}
49
Jan Kara7a2435d2009-01-27 01:47:11 +010050static ssize_t read_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
Jan Kara1ccd14b2008-09-22 05:54:49 +020051{
52 struct super_block *sb = info->dqi_sb;
53
54 memset(buf, 0, info->dqi_usable_bs);
Jan Karad26ac1a2009-01-26 16:17:50 +010055 return sb->s_op->quota_read(sb, info->dqi_type, buf,
Jan Kara10f04d42020-11-02 16:32:10 +010056 info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
Jan Kara1ccd14b2008-09-22 05:54:49 +020057}
58
Jan Kara7a2435d2009-01-27 01:47:11 +010059static ssize_t write_blk(struct qtree_mem_dqinfo *info, uint blk, char *buf)
Jan Kara1ccd14b2008-09-22 05:54:49 +020060{
61 struct super_block *sb = info->dqi_sb;
Jiaying Zhang19071312010-05-17 18:36:03 +020062 ssize_t ret;
Jan Kara1ccd14b2008-09-22 05:54:49 +020063
Jiaying Zhang19071312010-05-17 18:36:03 +020064 ret = sb->s_op->quota_write(sb, info->dqi_type, buf,
Jan Kara10f04d42020-11-02 16:32:10 +010065 info->dqi_usable_bs, (loff_t)blk << info->dqi_blocksize_bits);
Jiaying Zhang19071312010-05-17 18:36:03 +020066 if (ret != info->dqi_usable_bs) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +020067 quota_error(sb, "dquota write failed");
Jiaying Zhang19071312010-05-17 18:36:03 +020068 if (ret >= 0)
69 ret = -EIO;
70 }
71 return ret;
Jan Kara1ccd14b2008-09-22 05:54:49 +020072}
73
74/* Remove empty block from list and return it */
75static int get_free_dqblk(struct qtree_mem_dqinfo *info)
76{
Zhen Leice1b06c2021-06-18 09:47:20 +080077 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +020078 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
79 int ret, blk;
80
81 if (!buf)
82 return -ENOMEM;
83 if (info->dqi_free_blk) {
84 blk = info->dqi_free_blk;
85 ret = read_blk(info, blk, buf);
86 if (ret < 0)
87 goto out_buf;
88 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
89 }
90 else {
91 memset(buf, 0, info->dqi_usable_bs);
92 /* Assure block allocation... */
93 ret = write_blk(info, info->dqi_blocks, buf);
94 if (ret < 0)
95 goto out_buf;
96 blk = info->dqi_blocks++;
97 }
98 mark_info_dirty(info->dqi_sb, info->dqi_type);
99 ret = blk;
100out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100101 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200102 return ret;
103}
104
105/* Insert empty block to the list */
Jan Karad26ac1a2009-01-26 16:17:50 +0100106static int put_free_dqblk(struct qtree_mem_dqinfo *info, char *buf, uint blk)
Jan Kara1ccd14b2008-09-22 05:54:49 +0200107{
108 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
109 int err;
110
111 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
112 dh->dqdh_prev_free = cpu_to_le32(0);
113 dh->dqdh_entries = cpu_to_le16(0);
114 err = write_blk(info, blk, buf);
115 if (err < 0)
116 return err;
117 info->dqi_free_blk = blk;
118 mark_info_dirty(info->dqi_sb, info->dqi_type);
119 return 0;
120}
121
122/* Remove given block from the list of blocks with free entries */
Jan Kara268157b2009-01-27 15:47:22 +0100123static int remove_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
124 uint blk)
Jan Kara1ccd14b2008-09-22 05:54:49 +0200125{
Zhen Leice1b06c2021-06-18 09:47:20 +0800126 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200127 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
128 uint nextblk = le32_to_cpu(dh->dqdh_next_free);
129 uint prevblk = le32_to_cpu(dh->dqdh_prev_free);
130 int err;
131
132 if (!tmpbuf)
133 return -ENOMEM;
134 if (nextblk) {
135 err = read_blk(info, nextblk, tmpbuf);
136 if (err < 0)
137 goto out_buf;
138 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
139 dh->dqdh_prev_free;
140 err = write_blk(info, nextblk, tmpbuf);
141 if (err < 0)
142 goto out_buf;
143 }
144 if (prevblk) {
145 err = read_blk(info, prevblk, tmpbuf);
146 if (err < 0)
147 goto out_buf;
148 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
149 dh->dqdh_next_free;
150 err = write_blk(info, prevblk, tmpbuf);
151 if (err < 0)
152 goto out_buf;
153 } else {
154 info->dqi_free_entry = nextblk;
155 mark_info_dirty(info->dqi_sb, info->dqi_type);
156 }
Jan Karad26ac1a2009-01-26 16:17:50 +0100157 kfree(tmpbuf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200158 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
159 /* No matter whether write succeeds block is out of list */
160 if (write_blk(info, blk, buf) < 0)
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200161 quota_error(info->dqi_sb, "Can't write block (%u) "
162 "with free entries", blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200163 return 0;
164out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100165 kfree(tmpbuf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200166 return err;
167}
168
169/* Insert given block to the beginning of list with free entries */
Jan Kara268157b2009-01-27 15:47:22 +0100170static int insert_free_dqentry(struct qtree_mem_dqinfo *info, char *buf,
171 uint blk)
Jan Kara1ccd14b2008-09-22 05:54:49 +0200172{
Zhen Leice1b06c2021-06-18 09:47:20 +0800173 char *tmpbuf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200174 struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
175 int err;
176
177 if (!tmpbuf)
178 return -ENOMEM;
179 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
180 dh->dqdh_prev_free = cpu_to_le32(0);
181 err = write_blk(info, blk, buf);
182 if (err < 0)
183 goto out_buf;
184 if (info->dqi_free_entry) {
185 err = read_blk(info, info->dqi_free_entry, tmpbuf);
186 if (err < 0)
187 goto out_buf;
188 ((struct qt_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
189 cpu_to_le32(blk);
190 err = write_blk(info, info->dqi_free_entry, tmpbuf);
191 if (err < 0)
192 goto out_buf;
193 }
Jan Karad26ac1a2009-01-26 16:17:50 +0100194 kfree(tmpbuf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200195 info->dqi_free_entry = blk;
196 mark_info_dirty(info->dqi_sb, info->dqi_type);
197 return 0;
198out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100199 kfree(tmpbuf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200200 return err;
201}
202
203/* Is the entry in the block free? */
204int qtree_entry_unused(struct qtree_mem_dqinfo *info, char *disk)
205{
206 int i;
207
208 for (i = 0; i < info->dqi_entry_size; i++)
209 if (disk[i])
210 return 0;
211 return 1;
212}
213EXPORT_SYMBOL(qtree_entry_unused);
214
215/* Find space for dquot */
216static uint find_free_dqentry(struct qtree_mem_dqinfo *info,
217 struct dquot *dquot, int *err)
218{
219 uint blk, i;
220 struct qt_disk_dqdbheader *dh;
Zhen Leice1b06c2021-06-18 09:47:20 +0800221 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200222 char *ddquot;
223
224 *err = 0;
225 if (!buf) {
226 *err = -ENOMEM;
227 return 0;
228 }
229 dh = (struct qt_disk_dqdbheader *)buf;
230 if (info->dqi_free_entry) {
231 blk = info->dqi_free_entry;
232 *err = read_blk(info, blk, buf);
233 if (*err < 0)
234 goto out_buf;
235 } else {
236 blk = get_free_dqblk(info);
237 if ((int)blk < 0) {
238 *err = blk;
Jan Karad26ac1a2009-01-26 16:17:50 +0100239 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200240 return 0;
241 }
242 memset(buf, 0, info->dqi_usable_bs);
Jan Kara268157b2009-01-27 15:47:22 +0100243 /* This is enough as the block is already zeroed and the entry
244 * list is empty... */
Jan Kara1ccd14b2008-09-22 05:54:49 +0200245 info->dqi_free_entry = blk;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700246 mark_info_dirty(dquot->dq_sb, dquot->dq_id.type);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200247 }
248 /* Block will be full? */
249 if (le16_to_cpu(dh->dqdh_entries) + 1 >= qtree_dqstr_in_blk(info)) {
250 *err = remove_free_dqentry(info, buf, blk);
251 if (*err < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200252 quota_error(dquot->dq_sb, "Can't remove block (%u) "
253 "from entry free list", blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200254 goto out_buf;
255 }
256 }
257 le16_add_cpu(&dh->dqdh_entries, 1);
258 /* Find free structure in block */
Jan Kara268157b2009-01-27 15:47:22 +0100259 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
260 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
261 if (qtree_entry_unused(info, ddquot))
262 break;
263 ddquot += info->dqi_entry_size;
264 }
Jan Kara1ccd14b2008-09-22 05:54:49 +0200265#ifdef __QUOTA_QT_PARANOIA
266 if (i == qtree_dqstr_in_blk(info)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200267 quota_error(dquot->dq_sb, "Data block full but it shouldn't");
Jan Kara1ccd14b2008-09-22 05:54:49 +0200268 *err = -EIO;
269 goto out_buf;
270 }
271#endif
272 *err = write_blk(info, blk, buf);
273 if (*err < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200274 quota_error(dquot->dq_sb, "Can't write quota data block %u",
275 blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200276 goto out_buf;
277 }
Jan Kara10f04d42020-11-02 16:32:10 +0100278 dquot->dq_off = ((loff_t)blk << info->dqi_blocksize_bits) +
Jan Kara1ccd14b2008-09-22 05:54:49 +0200279 sizeof(struct qt_disk_dqdbheader) +
280 i * info->dqi_entry_size;
Jan Karad26ac1a2009-01-26 16:17:50 +0100281 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200282 return blk;
283out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100284 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200285 return 0;
286}
287
288/* Insert reference to structure into the trie */
289static int do_insert_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
290 uint *treeblk, int depth)
291{
Zhen Leice1b06c2021-06-18 09:47:20 +0800292 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200293 int ret = 0, newson = 0, newact = 0;
294 __le32 *ref;
295 uint newblk;
296
297 if (!buf)
298 return -ENOMEM;
299 if (!*treeblk) {
300 ret = get_free_dqblk(info);
301 if (ret < 0)
302 goto out_buf;
303 *treeblk = ret;
304 memset(buf, 0, info->dqi_usable_bs);
305 newact = 1;
306 } else {
307 ret = read_blk(info, *treeblk, buf);
308 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200309 quota_error(dquot->dq_sb, "Can't read tree quota "
310 "block %u", *treeblk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200311 goto out_buf;
312 }
313 }
314 ref = (__le32 *)buf;
315 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
316 if (!newblk)
317 newson = 1;
318 if (depth == info->dqi_qtree_depth - 1) {
319#ifdef __QUOTA_QT_PARANOIA
320 if (newblk) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200321 quota_error(dquot->dq_sb, "Inserting already present "
322 "quota entry (block %u)",
323 le32_to_cpu(ref[get_index(info,
Jan Kara1ccd14b2008-09-22 05:54:49 +0200324 dquot->dq_id, depth)]));
325 ret = -EIO;
326 goto out_buf;
327 }
328#endif
329 newblk = find_free_dqentry(info, dquot, &ret);
330 } else {
331 ret = do_insert_tree(info, dquot, &newblk, depth+1);
332 }
333 if (newson && ret >= 0) {
334 ref[get_index(info, dquot->dq_id, depth)] =
335 cpu_to_le32(newblk);
336 ret = write_blk(info, *treeblk, buf);
337 } else if (newact && ret < 0) {
338 put_free_dqblk(info, buf, *treeblk);
339 }
340out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100341 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200342 return ret;
343}
344
345/* Wrapper for inserting quota structure into tree */
346static inline int dq_insert_tree(struct qtree_mem_dqinfo *info,
347 struct dquot *dquot)
348{
349 int tmp = QT_TREEOFF;
Konstantin Khlebnikov69a25ee2015-02-12 12:36:49 +0300350
351#ifdef __QUOTA_QT_PARANOIA
352 if (info->dqi_blocks <= QT_TREEOFF) {
353 quota_error(dquot->dq_sb, "Quota tree root isn't allocated!");
354 return -EIO;
355 }
356#endif
Jan Kara1ccd14b2008-09-22 05:54:49 +0200357 return do_insert_tree(info, dquot, &tmp, 0);
358}
359
360/*
Jan Kara268157b2009-01-27 15:47:22 +0100361 * We don't have to be afraid of deadlocks as we never have quotas on quota
362 * files...
Jan Kara1ccd14b2008-09-22 05:54:49 +0200363 */
364int qtree_write_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
365{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700366 int type = dquot->dq_id.type;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200367 struct super_block *sb = dquot->dq_sb;
368 ssize_t ret;
Zhen Leice1b06c2021-06-18 09:47:20 +0800369 char *ddquot = kmalloc(info->dqi_entry_size, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200370
371 if (!ddquot)
372 return -ENOMEM;
373
Jan Karabc8230e2017-06-08 14:39:48 +0200374 /* dq_off is guarded by dqio_sem */
Jan Kara1ccd14b2008-09-22 05:54:49 +0200375 if (!dquot->dq_off) {
376 ret = dq_insert_tree(info, dquot);
377 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200378 quota_error(sb, "Error %zd occurred while creating "
379 "quota", ret);
Jan Karad26ac1a2009-01-26 16:17:50 +0100380 kfree(ddquot);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200381 return ret;
382 }
383 }
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200384 spin_lock(&dquot->dq_dqb_lock);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200385 info->dqi_ops->mem2disk_dqblk(ddquot, dquot);
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200386 spin_unlock(&dquot->dq_dqb_lock);
Jan Karad26ac1a2009-01-26 16:17:50 +0100387 ret = sb->s_op->quota_write(sb, type, ddquot, info->dqi_entry_size,
388 dquot->dq_off);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200389 if (ret != info->dqi_entry_size) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200390 quota_error(sb, "dquota write failed");
Jan Kara1ccd14b2008-09-22 05:54:49 +0200391 if (ret >= 0)
392 ret = -ENOSPC;
393 } else {
394 ret = 0;
395 }
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400396 dqstats_inc(DQST_WRITES);
Jan Karad26ac1a2009-01-26 16:17:50 +0100397 kfree(ddquot);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200398
399 return ret;
400}
401EXPORT_SYMBOL(qtree_write_dquot);
402
403/* Free dquot entry in data block */
404static int free_dqentry(struct qtree_mem_dqinfo *info, struct dquot *dquot,
405 uint blk)
406{
407 struct qt_disk_dqdbheader *dh;
Zhen Leice1b06c2021-06-18 09:47:20 +0800408 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200409 int ret = 0;
410
411 if (!buf)
412 return -ENOMEM;
413 if (dquot->dq_off >> info->dqi_blocksize_bits != blk) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200414 quota_error(dquot->dq_sb, "Quota structure has offset to "
415 "other block (%u) than it should (%u)", blk,
416 (uint)(dquot->dq_off >> info->dqi_blocksize_bits));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200417 goto out_buf;
418 }
419 ret = read_blk(info, blk, buf);
420 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200421 quota_error(dquot->dq_sb, "Can't read quota data block %u",
422 blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200423 goto out_buf;
424 }
425 dh = (struct qt_disk_dqdbheader *)buf;
426 le16_add_cpu(&dh->dqdh_entries, -1);
427 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
428 ret = remove_free_dqentry(info, buf, blk);
429 if (ret >= 0)
430 ret = put_free_dqblk(info, buf, blk);
431 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200432 quota_error(dquot->dq_sb, "Can't move quota data block "
433 "(%u) to free list", blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200434 goto out_buf;
435 }
436 } else {
437 memset(buf +
438 (dquot->dq_off & ((1 << info->dqi_blocksize_bits) - 1)),
439 0, info->dqi_entry_size);
440 if (le16_to_cpu(dh->dqdh_entries) ==
441 qtree_dqstr_in_blk(info) - 1) {
442 /* Insert will write block itself */
443 ret = insert_free_dqentry(info, buf, blk);
444 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200445 quota_error(dquot->dq_sb, "Can't insert quota "
446 "data block (%u) to free entry list", blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200447 goto out_buf;
448 }
449 } else {
450 ret = write_blk(info, blk, buf);
451 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200452 quota_error(dquot->dq_sb, "Can't write quota "
453 "data block %u", blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200454 goto out_buf;
455 }
456 }
457 }
458 dquot->dq_off = 0; /* Quota is now unattached */
459out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100460 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200461 return ret;
462}
463
464/* Remove reference to dquot from tree */
465static int remove_tree(struct qtree_mem_dqinfo *info, struct dquot *dquot,
466 uint *blk, int depth)
467{
Zhen Leice1b06c2021-06-18 09:47:20 +0800468 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200469 int ret = 0;
470 uint newblk;
471 __le32 *ref = (__le32 *)buf;
472
473 if (!buf)
474 return -ENOMEM;
475 ret = read_blk(info, *blk, buf);
476 if (ret < 0) {
Joe Perches055adcb2010-11-23 18:49:54 -0800477 quota_error(dquot->dq_sb, "Can't read quota data block %u",
478 *blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200479 goto out_buf;
480 }
481 newblk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
Zhang Yi9bf3d202021-10-08 17:38:20 +0800482 if (newblk < QT_TREEOFF || newblk >= info->dqi_blocks) {
483 quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
484 newblk, info->dqi_blocks);
485 ret = -EUCLEAN;
486 goto out_buf;
487 }
488
Jan Kara1ccd14b2008-09-22 05:54:49 +0200489 if (depth == info->dqi_qtree_depth - 1) {
490 ret = free_dqentry(info, dquot, newblk);
491 newblk = 0;
492 } else {
493 ret = remove_tree(info, dquot, &newblk, depth+1);
494 }
495 if (ret >= 0 && !newblk) {
496 int i;
497 ref[get_index(info, dquot->dq_id, depth)] = cpu_to_le32(0);
498 /* Block got empty? */
Jan Karad26ac1a2009-01-26 16:17:50 +0100499 for (i = 0; i < (info->dqi_usable_bs >> 2) && !ref[i]; i++)
500 ;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200501 /* Don't put the root block into the free block list */
502 if (i == (info->dqi_usable_bs >> 2)
503 && *blk != QT_TREEOFF) {
504 put_free_dqblk(info, buf, *blk);
505 *blk = 0;
506 } else {
507 ret = write_blk(info, *blk, buf);
508 if (ret < 0)
Joe Perches055adcb2010-11-23 18:49:54 -0800509 quota_error(dquot->dq_sb,
510 "Can't write quota tree block %u",
511 *blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200512 }
513 }
514out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100515 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200516 return ret;
517}
518
519/* Delete dquot from tree */
520int qtree_delete_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
521{
522 uint tmp = QT_TREEOFF;
523
524 if (!dquot->dq_off) /* Even not allocated? */
525 return 0;
526 return remove_tree(info, dquot, &tmp, 0);
527}
528EXPORT_SYMBOL(qtree_delete_dquot);
529
530/* Find entry in block */
531static loff_t find_block_dqentry(struct qtree_mem_dqinfo *info,
532 struct dquot *dquot, uint blk)
533{
Zhen Leice1b06c2021-06-18 09:47:20 +0800534 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200535 loff_t ret = 0;
536 int i;
537 char *ddquot;
538
539 if (!buf)
540 return -ENOMEM;
541 ret = read_blk(info, blk, buf);
542 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200543 quota_error(dquot->dq_sb, "Can't read quota tree "
544 "block %u", blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200545 goto out_buf;
546 }
Jan Kara268157b2009-01-27 15:47:22 +0100547 ddquot = buf + sizeof(struct qt_disk_dqdbheader);
548 for (i = 0; i < qtree_dqstr_in_blk(info); i++) {
549 if (info->dqi_ops->is_id(ddquot, dquot))
550 break;
551 ddquot += info->dqi_entry_size;
552 }
Jan Kara1ccd14b2008-09-22 05:54:49 +0200553 if (i == qtree_dqstr_in_blk(info)) {
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700554 quota_error(dquot->dq_sb,
555 "Quota for id %u referenced but not present",
556 from_kqid(&init_user_ns, dquot->dq_id));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200557 ret = -EIO;
558 goto out_buf;
559 } else {
Jan Kara10f04d42020-11-02 16:32:10 +0100560 ret = ((loff_t)blk << info->dqi_blocksize_bits) + sizeof(struct
Jan Kara1ccd14b2008-09-22 05:54:49 +0200561 qt_disk_dqdbheader) + i * info->dqi_entry_size;
562 }
563out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100564 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200565 return ret;
566}
567
568/* Find entry for given id in the tree */
569static loff_t find_tree_dqentry(struct qtree_mem_dqinfo *info,
570 struct dquot *dquot, uint blk, int depth)
571{
Zhen Leice1b06c2021-06-18 09:47:20 +0800572 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200573 loff_t ret = 0;
574 __le32 *ref = (__le32 *)buf;
575
576 if (!buf)
577 return -ENOMEM;
578 ret = read_blk(info, blk, buf);
579 if (ret < 0) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200580 quota_error(dquot->dq_sb, "Can't read quota tree block %u",
581 blk);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200582 goto out_buf;
583 }
584 ret = 0;
585 blk = le32_to_cpu(ref[get_index(info, dquot->dq_id, depth)]);
586 if (!blk) /* No reference? */
587 goto out_buf;
Zhang Yi9bf3d202021-10-08 17:38:20 +0800588 if (blk < QT_TREEOFF || blk >= info->dqi_blocks) {
589 quota_error(dquot->dq_sb, "Getting block too big (%u >= %u)",
590 blk, info->dqi_blocks);
591 ret = -EUCLEAN;
592 goto out_buf;
593 }
594
Jan Kara1ccd14b2008-09-22 05:54:49 +0200595 if (depth < info->dqi_qtree_depth - 1)
596 ret = find_tree_dqentry(info, dquot, blk, depth+1);
597 else
598 ret = find_block_dqentry(info, dquot, blk);
599out_buf:
Jan Karad26ac1a2009-01-26 16:17:50 +0100600 kfree(buf);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200601 return ret;
602}
603
604/* Find entry for given id in the tree - wrapper function */
605static inline loff_t find_dqentry(struct qtree_mem_dqinfo *info,
606 struct dquot *dquot)
607{
608 return find_tree_dqentry(info, dquot, QT_TREEOFF, 0);
609}
610
611int qtree_read_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
612{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700613 int type = dquot->dq_id.type;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200614 struct super_block *sb = dquot->dq_sb;
615 loff_t offset;
Jan Karad26ac1a2009-01-26 16:17:50 +0100616 char *ddquot;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200617 int ret = 0;
618
619#ifdef __QUOTA_QT_PARANOIA
620 /* Invalidated quota? */
621 if (!sb_dqopt(dquot->dq_sb)->files[type]) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200622 quota_error(sb, "Quota invalidated while reading!");
Jan Kara1ccd14b2008-09-22 05:54:49 +0200623 return -EIO;
624 }
625#endif
626 /* Do we know offset of the dquot entry in the quota file? */
627 if (!dquot->dq_off) {
628 offset = find_dqentry(info, dquot);
629 if (offset <= 0) { /* Entry not present? */
630 if (offset < 0)
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700631 quota_error(sb,"Can't read quota structure "
632 "for id %u",
633 from_kqid(&init_user_ns,
634 dquot->dq_id));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200635 dquot->dq_off = 0;
636 set_bit(DQ_FAKE_B, &dquot->dq_flags);
637 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
638 ret = offset;
639 goto out;
640 }
641 dquot->dq_off = offset;
642 }
Zhen Leice1b06c2021-06-18 09:47:20 +0800643 ddquot = kmalloc(info->dqi_entry_size, GFP_NOFS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200644 if (!ddquot)
645 return -ENOMEM;
Jan Karad26ac1a2009-01-26 16:17:50 +0100646 ret = sb->s_op->quota_read(sb, type, ddquot, info->dqi_entry_size,
647 dquot->dq_off);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200648 if (ret != info->dqi_entry_size) {
649 if (ret >= 0)
650 ret = -EIO;
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200651 quota_error(sb, "Error while reading quota structure for id %u",
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700652 from_kqid(&init_user_ns, dquot->dq_id));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200653 set_bit(DQ_FAKE_B, &dquot->dq_flags);
654 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
Jan Karad26ac1a2009-01-26 16:17:50 +0100655 kfree(ddquot);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200656 goto out;
657 }
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200658 spin_lock(&dquot->dq_dqb_lock);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200659 info->dqi_ops->disk2mem_dqblk(dquot, ddquot);
660 if (!dquot->dq_dqb.dqb_bhardlimit &&
661 !dquot->dq_dqb.dqb_bsoftlimit &&
662 !dquot->dq_dqb.dqb_ihardlimit &&
663 !dquot->dq_dqb.dqb_isoftlimit)
664 set_bit(DQ_FAKE_B, &dquot->dq_flags);
Jan Kara7b9ca4c2017-08-07 13:19:50 +0200665 spin_unlock(&dquot->dq_dqb_lock);
Jan Karad26ac1a2009-01-26 16:17:50 +0100666 kfree(ddquot);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200667out:
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400668 dqstats_inc(DQST_READS);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200669 return ret;
670}
671EXPORT_SYMBOL(qtree_read_dquot);
672
673/* Check whether dquot should not be deleted. We know we are
674 * the only one operating on dquot (thanks to dq_lock) */
675int qtree_release_dquot(struct qtree_mem_dqinfo *info, struct dquot *dquot)
676{
Jan Kara268157b2009-01-27 15:47:22 +0100677 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) &&
678 !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
Jan Kara1ccd14b2008-09-22 05:54:49 +0200679 return qtree_delete_dquot(info, dquot);
680 return 0;
681}
682EXPORT_SYMBOL(qtree_release_dquot);
Jan Kara00663732016-01-25 20:39:27 +0100683
684static int find_next_id(struct qtree_mem_dqinfo *info, qid_t *id,
685 unsigned int blk, int depth)
686{
Zhen Leice1b06c2021-06-18 09:47:20 +0800687 char *buf = kmalloc(info->dqi_usable_bs, GFP_NOFS);
Jan Kara00663732016-01-25 20:39:27 +0100688 __le32 *ref = (__le32 *)buf;
689 ssize_t ret;
690 unsigned int epb = info->dqi_usable_bs >> 2;
691 unsigned int level_inc = 1;
692 int i;
693
694 if (!buf)
695 return -ENOMEM;
696
697 for (i = depth; i < info->dqi_qtree_depth - 1; i++)
698 level_inc *= epb;
699
700 ret = read_blk(info, blk, buf);
701 if (ret < 0) {
702 quota_error(info->dqi_sb,
703 "Can't read quota tree block %u", blk);
704 goto out_buf;
705 }
706 for (i = __get_index(info, *id, depth); i < epb; i++) {
707 if (ref[i] == cpu_to_le32(0)) {
708 *id += level_inc;
709 continue;
710 }
711 if (depth == info->dqi_qtree_depth - 1) {
712 ret = 0;
713 goto out_buf;
714 }
715 ret = find_next_id(info, id, le32_to_cpu(ref[i]), depth + 1);
716 if (ret != -ENOENT)
717 break;
718 }
719 if (i == epb) {
720 ret = -ENOENT;
721 goto out_buf;
722 }
723out_buf:
724 kfree(buf);
725 return ret;
726}
727
728int qtree_get_next_id(struct qtree_mem_dqinfo *info, struct kqid *qid)
729{
730 qid_t id = from_kqid(&init_user_ns, *qid);
731 int ret;
732
733 ret = find_next_id(info, &id, QT_TREEOFF, 0);
734 if (ret < 0)
735 return ret;
736 *qid = make_kqid(&init_user_ns, qid->type, id);
737 return 0;
738}
739EXPORT_SYMBOL(qtree_get_next_id);