xref: /linux/mm/shmem_quota.c (revision 617a814f14b8914271f7a70366d72c6196d17663)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * In memory quota format relies on quota infrastructure to store dquot
4  * information for us. While conventional quota formats for file systems
5  * with persistent storage can load quota information into dquot from the
6  * storage on-demand and hence quota dquot shrinker can free any dquot
7  * that is not currently being used, it must be avoided here. Otherwise we
8  * can lose valuable information, user provided limits, because there is
9  * no persistent storage to load the information from afterwards.
10  *
11  * One information that in-memory quota format needs to keep track of is
12  * a sorted list of ids for each quota type. This is done by utilizing
13  * an rb tree which root is stored in mem_dqinfo->dqi_priv for each quota
14  * type.
15  *
16  * This format can be used to support quota on file system without persistent
17  * storage such as tmpfs.
18  *
19  * Author:	Lukas Czerner <lczerner@redhat.com>
20  *		Carlos Maiolino <cmaiolino@redhat.com>
21  *
22  * Copyright (C) 2023 Red Hat, Inc.
23  */
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/mount.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/rbtree.h>
32 #include <linux/shmem_fs.h>
33 
34 #include <linux/quotaops.h>
35 #include <linux/quota.h>
36 
37 /*
38  * The following constants define the amount of time given a user
39  * before the soft limits are treated as hard limits (usually resulting
40  * in an allocation failure). The timer is started when the user crosses
41  * their soft limit, it is reset when they go below their soft limit.
42  */
43 #define SHMEM_MAX_IQ_TIME 604800	/* (7*24*60*60) 1 week */
44 #define SHMEM_MAX_DQ_TIME 604800	/* (7*24*60*60) 1 week */
45 
46 struct quota_id {
47 	struct rb_node	node;
48 	qid_t		id;
49 	qsize_t		bhardlimit;
50 	qsize_t		bsoftlimit;
51 	qsize_t		ihardlimit;
52 	qsize_t		isoftlimit;
53 };
54 
55 static int shmem_check_quota_file(struct super_block *sb, int type)
56 {
57 	/* There is no real quota file, nothing to do */
58 	return 1;
59 }
60 
61 /*
62  * There is no real quota file. Just allocate rb_root for quota ids and
63  * set limits
64  */
65 static int shmem_read_file_info(struct super_block *sb, int type)
66 {
67 	struct quota_info *dqopt = sb_dqopt(sb);
68 	struct mem_dqinfo *info = &dqopt->info[type];
69 
70 	info->dqi_priv = kzalloc(sizeof(struct rb_root), GFP_NOFS);
71 	if (!info->dqi_priv)
72 		return -ENOMEM;
73 
74 	info->dqi_max_spc_limit = SHMEM_QUOTA_MAX_SPC_LIMIT;
75 	info->dqi_max_ino_limit = SHMEM_QUOTA_MAX_INO_LIMIT;
76 
77 	info->dqi_bgrace = SHMEM_MAX_DQ_TIME;
78 	info->dqi_igrace = SHMEM_MAX_IQ_TIME;
79 	info->dqi_flags = 0;
80 
81 	return 0;
82 }
83 
84 static int shmem_write_file_info(struct super_block *sb, int type)
85 {
86 	/* There is no real quota file, nothing to do */
87 	return 0;
88 }
89 
90 /*
91  * Free all the quota_id entries in the rb tree and rb_root.
92  */
93 static int shmem_free_file_info(struct super_block *sb, int type)
94 {
95 	struct mem_dqinfo *info = &sb_dqopt(sb)->info[type];
96 	struct rb_root *root = info->dqi_priv;
97 	struct quota_id *entry;
98 	struct rb_node *node;
99 
100 	info->dqi_priv = NULL;
101 	node = rb_first(root);
102 	while (node) {
103 		entry = rb_entry(node, struct quota_id, node);
104 		node = rb_next(&entry->node);
105 
106 		rb_erase(&entry->node, root);
107 		kfree(entry);
108 	}
109 
110 	kfree(root);
111 	return 0;
112 }
113 
114 static int shmem_get_next_id(struct super_block *sb, struct kqid *qid)
115 {
116 	struct mem_dqinfo *info = sb_dqinfo(sb, qid->type);
117 	struct rb_node *node;
118 	qid_t id = from_kqid(&init_user_ns, *qid);
119 	struct quota_info *dqopt = sb_dqopt(sb);
120 	struct quota_id *entry = NULL;
121 	int ret = 0;
122 
123 	if (!sb_has_quota_active(sb, qid->type))
124 		return -ESRCH;
125 
126 	down_read(&dqopt->dqio_sem);
127 	node = ((struct rb_root *)info->dqi_priv)->rb_node;
128 	while (node) {
129 		entry = rb_entry(node, struct quota_id, node);
130 
131 		if (id < entry->id)
132 			node = node->rb_left;
133 		else if (id > entry->id)
134 			node = node->rb_right;
135 		else
136 			goto got_next_id;
137 	}
138 
139 	if (!entry) {
140 		ret = -ENOENT;
141 		goto out_unlock;
142 	}
143 
144 	if (id > entry->id) {
145 		node = rb_next(&entry->node);
146 		if (!node) {
147 			ret = -ENOENT;
148 			goto out_unlock;
149 		}
150 		entry = rb_entry(node, struct quota_id, node);
151 	}
152 
153 got_next_id:
154 	*qid = make_kqid(&init_user_ns, qid->type, entry->id);
155 out_unlock:
156 	up_read(&dqopt->dqio_sem);
157 	return ret;
158 }
159 
160 /*
161  * Load dquot with limits from existing entry, or create the new entry if
162  * it does not exist.
163  */
164 static int shmem_acquire_dquot(struct dquot *dquot)
165 {
166 	struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type);
167 	struct rb_node **n;
168 	struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info;
169 	struct rb_node *parent = NULL, *new_node = NULL;
170 	struct quota_id *new_entry, *entry;
171 	qid_t id = from_kqid(&init_user_ns, dquot->dq_id);
172 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
173 	int ret = 0;
174 
175 	mutex_lock(&dquot->dq_lock);
176 
177 	down_write(&dqopt->dqio_sem);
178 	n = &((struct rb_root *)info->dqi_priv)->rb_node;
179 
180 	while (*n) {
181 		parent = *n;
182 		entry = rb_entry(parent, struct quota_id, node);
183 
184 		if (id < entry->id)
185 			n = &(*n)->rb_left;
186 		else if (id > entry->id)
187 			n = &(*n)->rb_right;
188 		else
189 			goto found;
190 	}
191 
192 	/* We don't have entry for this id yet, create it */
193 	new_entry = kzalloc(sizeof(struct quota_id), GFP_NOFS);
194 	if (!new_entry) {
195 		ret = -ENOMEM;
196 		goto out_unlock;
197 	}
198 
199 	new_entry->id = id;
200 	if (dquot->dq_id.type == USRQUOTA) {
201 		new_entry->bhardlimit = sbinfo->qlimits.usrquota_bhardlimit;
202 		new_entry->ihardlimit = sbinfo->qlimits.usrquota_ihardlimit;
203 	} else if (dquot->dq_id.type == GRPQUOTA) {
204 		new_entry->bhardlimit = sbinfo->qlimits.grpquota_bhardlimit;
205 		new_entry->ihardlimit = sbinfo->qlimits.grpquota_ihardlimit;
206 	}
207 
208 	new_node = &new_entry->node;
209 	rb_link_node(new_node, parent, n);
210 	rb_insert_color(new_node, (struct rb_root *)info->dqi_priv);
211 	entry = new_entry;
212 
213 found:
214 	/* Load the stored limits from the tree */
215 	spin_lock(&dquot->dq_dqb_lock);
216 	dquot->dq_dqb.dqb_bhardlimit = entry->bhardlimit;
217 	dquot->dq_dqb.dqb_bsoftlimit = entry->bsoftlimit;
218 	dquot->dq_dqb.dqb_ihardlimit = entry->ihardlimit;
219 	dquot->dq_dqb.dqb_isoftlimit = entry->isoftlimit;
220 
221 	if (!dquot->dq_dqb.dqb_bhardlimit &&
222 	    !dquot->dq_dqb.dqb_bsoftlimit &&
223 	    !dquot->dq_dqb.dqb_ihardlimit &&
224 	    !dquot->dq_dqb.dqb_isoftlimit)
225 		set_bit(DQ_FAKE_B, &dquot->dq_flags);
226 	spin_unlock(&dquot->dq_dqb_lock);
227 
228 	/* Make sure flags update is visible after dquot has been filled */
229 	smp_mb__before_atomic();
230 	set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
231 out_unlock:
232 	up_write(&dqopt->dqio_sem);
233 	mutex_unlock(&dquot->dq_lock);
234 	return ret;
235 }
236 
237 static bool shmem_is_empty_dquot(struct dquot *dquot)
238 {
239 	struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info;
240 	qsize_t bhardlimit;
241 	qsize_t ihardlimit;
242 
243 	if (dquot->dq_id.type == USRQUOTA) {
244 		bhardlimit = sbinfo->qlimits.usrquota_bhardlimit;
245 		ihardlimit = sbinfo->qlimits.usrquota_ihardlimit;
246 	} else if (dquot->dq_id.type == GRPQUOTA) {
247 		bhardlimit = sbinfo->qlimits.grpquota_bhardlimit;
248 		ihardlimit = sbinfo->qlimits.grpquota_ihardlimit;
249 	}
250 
251 	if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
252 		(dquot->dq_dqb.dqb_curspace == 0 &&
253 		 dquot->dq_dqb.dqb_curinodes == 0 &&
254 		 dquot->dq_dqb.dqb_bhardlimit == bhardlimit &&
255 		 dquot->dq_dqb.dqb_ihardlimit == ihardlimit))
256 		return true;
257 
258 	return false;
259 }
260 /*
261  * Store limits from dquot in the tree unless it's fake. If it is fake
262  * remove the id from the tree since there is no useful information in
263  * there.
264  */
265 static int shmem_release_dquot(struct dquot *dquot)
266 {
267 	struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type);
268 	struct rb_node *node;
269 	qid_t id = from_kqid(&init_user_ns, dquot->dq_id);
270 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
271 	struct quota_id *entry = NULL;
272 
273 	mutex_lock(&dquot->dq_lock);
274 	/* Check whether we are not racing with some other dqget() */
275 	if (dquot_is_busy(dquot))
276 		goto out_dqlock;
277 
278 	down_write(&dqopt->dqio_sem);
279 	node = ((struct rb_root *)info->dqi_priv)->rb_node;
280 	while (node) {
281 		entry = rb_entry(node, struct quota_id, node);
282 
283 		if (id < entry->id)
284 			node = node->rb_left;
285 		else if (id > entry->id)
286 			node = node->rb_right;
287 		else
288 			goto found;
289 	}
290 
291 	/* We should always find the entry in the rb tree */
292 	WARN_ONCE(1, "quota id %u from dquot %p, not in rb tree!\n", id, dquot);
293 	up_write(&dqopt->dqio_sem);
294 	mutex_unlock(&dquot->dq_lock);
295 	return -ENOENT;
296 
297 found:
298 	if (shmem_is_empty_dquot(dquot)) {
299 		/* Remove entry from the tree */
300 		rb_erase(&entry->node, info->dqi_priv);
301 		kfree(entry);
302 	} else {
303 		/* Store the limits in the tree */
304 		spin_lock(&dquot->dq_dqb_lock);
305 		entry->bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
306 		entry->bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
307 		entry->ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
308 		entry->isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
309 		spin_unlock(&dquot->dq_dqb_lock);
310 	}
311 
312 	clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
313 	up_write(&dqopt->dqio_sem);
314 
315 out_dqlock:
316 	mutex_unlock(&dquot->dq_lock);
317 	return 0;
318 }
319 
320 static int shmem_mark_dquot_dirty(struct dquot *dquot)
321 {
322 	return 0;
323 }
324 
325 static int shmem_dquot_write_info(struct super_block *sb, int type)
326 {
327 	return 0;
328 }
329 
330 static const struct quota_format_ops shmem_format_ops = {
331 	.check_quota_file	= shmem_check_quota_file,
332 	.read_file_info		= shmem_read_file_info,
333 	.write_file_info	= shmem_write_file_info,
334 	.free_file_info		= shmem_free_file_info,
335 };
336 
337 struct quota_format_type shmem_quota_format = {
338 	.qf_fmt_id = QFMT_SHMEM,
339 	.qf_ops = &shmem_format_ops,
340 	.qf_owner = THIS_MODULE
341 };
342 
343 const struct dquot_operations shmem_quota_operations = {
344 	.acquire_dquot		= shmem_acquire_dquot,
345 	.release_dquot		= shmem_release_dquot,
346 	.alloc_dquot		= dquot_alloc,
347 	.destroy_dquot		= dquot_destroy,
348 	.write_info		= shmem_dquot_write_info,
349 	.mark_dirty		= shmem_mark_dquot_dirty,
350 	.get_next_id		= shmem_get_next_id,
351 };
352