1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Definitions for diskquota-operations. When diskquota is configured these
4  * macros expand to the right source-code.
5  *
6  * Author:  Marco van Wieringen <mvw@planets.elm.net>
7  */
8 #ifndef _LINUX_QUOTAOPS_
9 #define _LINUX_QUOTAOPS_
10 
11 #include <linux/fs.h>
12 
13 #define DQUOT_SPACE_WARN	0x1
14 #define DQUOT_SPACE_RESERVE	0x2
15 #define DQUOT_SPACE_NOFAIL	0x4
16 
sb_dqopt(struct super_block * sb)17 static inline struct quota_info *sb_dqopt(struct super_block *sb)
18 {
19 	return &sb->s_dquot;
20 }
21 
22 /* i_mutex must being held */
is_quota_modification(struct inode * inode,struct iattr * ia)23 static inline bool is_quota_modification(struct inode *inode, struct iattr *ia)
24 {
25 	return (ia->ia_valid & ATTR_SIZE) ||
26 		(ia->ia_valid & ATTR_UID && !uid_eq(ia->ia_uid, inode->i_uid)) ||
27 		(ia->ia_valid & ATTR_GID && !gid_eq(ia->ia_gid, inode->i_gid));
28 }
29 
30 #if defined(CONFIG_QUOTA)
31 
32 #define quota_error(sb, fmt, args...) \
33 	__quota_error((sb), __func__, fmt , ## args)
34 
35 extern __printf(3, 4)
36 void __quota_error(struct super_block *sb, const char *func,
37 		   const char *fmt, ...);
38 
39 /*
40  * declaration of quota_function calls in kernel.
41  */
42 int dquot_initialize(struct inode *inode);
43 bool dquot_initialize_needed(struct inode *inode);
44 void dquot_drop(struct inode *inode);
45 struct dquot *dqget(struct super_block *sb, struct kqid qid);
dqgrab(struct dquot * dquot)46 static inline struct dquot *dqgrab(struct dquot *dquot)
47 {
48 	/* Make sure someone else has active reference to dquot */
49 	WARN_ON_ONCE(!atomic_read(&dquot->dq_count));
50 	WARN_ON_ONCE(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags));
51 	atomic_inc(&dquot->dq_count);
52 	return dquot;
53 }
54 
dquot_is_busy(struct dquot * dquot)55 static inline bool dquot_is_busy(struct dquot *dquot)
56 {
57 	if (test_bit(DQ_MOD_B, &dquot->dq_flags))
58 		return true;
59 	if (atomic_read(&dquot->dq_count) > 1)
60 		return true;
61 	return false;
62 }
63 
64 void dqput(struct dquot *dquot);
65 int dquot_scan_active(struct super_block *sb,
66 		      int (*fn)(struct dquot *dquot, unsigned long priv),
67 		      unsigned long priv);
68 struct dquot *dquot_alloc(struct super_block *sb, int type);
69 void dquot_destroy(struct dquot *dquot);
70 
71 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags);
72 void __dquot_free_space(struct inode *inode, qsize_t number, int flags);
73 
74 int dquot_alloc_inode(struct inode *inode);
75 
76 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number);
77 void dquot_free_inode(struct inode *inode);
78 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number);
79 
80 int dquot_disable(struct super_block *sb, int type, unsigned int flags);
81 /* Suspend quotas on remount RO */
dquot_suspend(struct super_block * sb,int type)82 static inline int dquot_suspend(struct super_block *sb, int type)
83 {
84 	return dquot_disable(sb, type, DQUOT_SUSPENDED);
85 }
86 int dquot_resume(struct super_block *sb, int type);
87 
88 int dquot_commit(struct dquot *dquot);
89 int dquot_acquire(struct dquot *dquot);
90 int dquot_release(struct dquot *dquot);
91 int dquot_commit_info(struct super_block *sb, int type);
92 int dquot_get_next_id(struct super_block *sb, struct kqid *qid);
93 int dquot_mark_dquot_dirty(struct dquot *dquot);
94 
95 int dquot_file_open(struct inode *inode, struct file *file);
96 
97 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
98 	unsigned int flags);
99 int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
100 	unsigned int flags);
101 int dquot_quota_on(struct super_block *sb, int type, int format_id,
102 	const struct path *path);
103 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
104  	int format_id, int type);
105 int dquot_quota_off(struct super_block *sb, int type);
106 int dquot_writeback_dquots(struct super_block *sb, int type);
107 int dquot_quota_sync(struct super_block *sb, int type);
108 int dquot_get_state(struct super_block *sb, struct qc_state *state);
109 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii);
110 int dquot_get_dqblk(struct super_block *sb, struct kqid id,
111 		struct qc_dqblk *di);
112 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *id,
113 		struct qc_dqblk *di);
114 int dquot_set_dqblk(struct super_block *sb, struct kqid id,
115 		struct qc_dqblk *di);
116 
117 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to);
118 int dquot_transfer(struct inode *inode, struct iattr *iattr);
119 
sb_dqinfo(struct super_block * sb,int type)120 static inline struct mem_dqinfo *sb_dqinfo(struct super_block *sb, int type)
121 {
122 	return sb_dqopt(sb)->info + type;
123 }
124 
125 /*
126  * Functions for checking status of quota
127  */
128 
sb_has_quota_usage_enabled(struct super_block * sb,int type)129 static inline bool sb_has_quota_usage_enabled(struct super_block *sb, int type)
130 {
131 	return sb_dqopt(sb)->flags &
132 				dquot_state_flag(DQUOT_USAGE_ENABLED, type);
133 }
134 
sb_has_quota_limits_enabled(struct super_block * sb,int type)135 static inline bool sb_has_quota_limits_enabled(struct super_block *sb, int type)
136 {
137 	return sb_dqopt(sb)->flags &
138 				dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
139 }
140 
sb_has_quota_suspended(struct super_block * sb,int type)141 static inline bool sb_has_quota_suspended(struct super_block *sb, int type)
142 {
143 	return sb_dqopt(sb)->flags &
144 				dquot_state_flag(DQUOT_SUSPENDED, type);
145 }
146 
sb_any_quota_suspended(struct super_block * sb)147 static inline unsigned sb_any_quota_suspended(struct super_block *sb)
148 {
149 	return dquot_state_types(sb_dqopt(sb)->flags, DQUOT_SUSPENDED);
150 }
151 
152 /* Does kernel know about any quota information for given sb + type? */
sb_has_quota_loaded(struct super_block * sb,int type)153 static inline bool sb_has_quota_loaded(struct super_block *sb, int type)
154 {
155 	/* Currently if anything is on, then quota usage is on as well */
156 	return sb_has_quota_usage_enabled(sb, type);
157 }
158 
sb_any_quota_loaded(struct super_block * sb)159 static inline unsigned sb_any_quota_loaded(struct super_block *sb)
160 {
161 	return dquot_state_types(sb_dqopt(sb)->flags, DQUOT_USAGE_ENABLED);
162 }
163 
sb_has_quota_active(struct super_block * sb,int type)164 static inline bool sb_has_quota_active(struct super_block *sb, int type)
165 {
166 	return sb_has_quota_loaded(sb, type) &&
167 	       !sb_has_quota_suspended(sb, type);
168 }
169 
170 /*
171  * Operations supported for diskquotas.
172  */
173 extern const struct dquot_operations dquot_operations;
174 extern const struct quotactl_ops dquot_quotactl_sysfile_ops;
175 
176 #else
177 
sb_has_quota_usage_enabled(struct super_block * sb,int type)178 static inline int sb_has_quota_usage_enabled(struct super_block *sb, int type)
179 {
180 	return 0;
181 }
182 
sb_has_quota_limits_enabled(struct super_block * sb,int type)183 static inline int sb_has_quota_limits_enabled(struct super_block *sb, int type)
184 {
185 	return 0;
186 }
187 
sb_has_quota_suspended(struct super_block * sb,int type)188 static inline int sb_has_quota_suspended(struct super_block *sb, int type)
189 {
190 	return 0;
191 }
192 
sb_any_quota_suspended(struct super_block * sb)193 static inline int sb_any_quota_suspended(struct super_block *sb)
194 {
195 	return 0;
196 }
197 
198 /* Does kernel know about any quota information for given sb + type? */
sb_has_quota_loaded(struct super_block * sb,int type)199 static inline int sb_has_quota_loaded(struct super_block *sb, int type)
200 {
201 	return 0;
202 }
203 
sb_any_quota_loaded(struct super_block * sb)204 static inline int sb_any_quota_loaded(struct super_block *sb)
205 {
206 	return 0;
207 }
208 
sb_has_quota_active(struct super_block * sb,int type)209 static inline int sb_has_quota_active(struct super_block *sb, int type)
210 {
211 	return 0;
212 }
213 
dquot_initialize(struct inode * inode)214 static inline int dquot_initialize(struct inode *inode)
215 {
216 	return 0;
217 }
218 
dquot_initialize_needed(struct inode * inode)219 static inline bool dquot_initialize_needed(struct inode *inode)
220 {
221 	return false;
222 }
223 
dquot_drop(struct inode * inode)224 static inline void dquot_drop(struct inode *inode)
225 {
226 }
227 
dquot_alloc_inode(struct inode * inode)228 static inline int dquot_alloc_inode(struct inode *inode)
229 {
230 	return 0;
231 }
232 
dquot_free_inode(struct inode * inode)233 static inline void dquot_free_inode(struct inode *inode)
234 {
235 }
236 
dquot_transfer(struct inode * inode,struct iattr * iattr)237 static inline int dquot_transfer(struct inode *inode, struct iattr *iattr)
238 {
239 	return 0;
240 }
241 
__dquot_alloc_space(struct inode * inode,qsize_t number,int flags)242 static inline int __dquot_alloc_space(struct inode *inode, qsize_t number,
243 		int flags)
244 {
245 	if (!(flags & DQUOT_SPACE_RESERVE))
246 		inode_add_bytes(inode, number);
247 	return 0;
248 }
249 
__dquot_free_space(struct inode * inode,qsize_t number,int flags)250 static inline void __dquot_free_space(struct inode *inode, qsize_t number,
251 		int flags)
252 {
253 	if (!(flags & DQUOT_SPACE_RESERVE))
254 		inode_sub_bytes(inode, number);
255 }
256 
dquot_claim_space_nodirty(struct inode * inode,qsize_t number)257 static inline int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
258 {
259 	inode_add_bytes(inode, number);
260 	return 0;
261 }
262 
dquot_reclaim_space_nodirty(struct inode * inode,qsize_t number)263 static inline int dquot_reclaim_space_nodirty(struct inode *inode,
264 					      qsize_t number)
265 {
266 	inode_sub_bytes(inode, number);
267 	return 0;
268 }
269 
dquot_disable(struct super_block * sb,int type,unsigned int flags)270 static inline int dquot_disable(struct super_block *sb, int type,
271 		unsigned int flags)
272 {
273 	return 0;
274 }
275 
dquot_suspend(struct super_block * sb,int type)276 static inline int dquot_suspend(struct super_block *sb, int type)
277 {
278 	return 0;
279 }
280 
dquot_resume(struct super_block * sb,int type)281 static inline int dquot_resume(struct super_block *sb, int type)
282 {
283 	return 0;
284 }
285 
286 #define dquot_file_open		generic_file_open
287 
dquot_writeback_dquots(struct super_block * sb,int type)288 static inline int dquot_writeback_dquots(struct super_block *sb, int type)
289 {
290 	return 0;
291 }
292 
293 #endif /* CONFIG_QUOTA */
294 
dquot_alloc_space_nodirty(struct inode * inode,qsize_t nr)295 static inline int dquot_alloc_space_nodirty(struct inode *inode, qsize_t nr)
296 {
297 	return __dquot_alloc_space(inode, nr, DQUOT_SPACE_WARN);
298 }
299 
dquot_alloc_space_nofail(struct inode * inode,qsize_t nr)300 static inline void dquot_alloc_space_nofail(struct inode *inode, qsize_t nr)
301 {
302 	__dquot_alloc_space(inode, nr, DQUOT_SPACE_WARN|DQUOT_SPACE_NOFAIL);
303 	mark_inode_dirty_sync(inode);
304 }
305 
dquot_alloc_space(struct inode * inode,qsize_t nr)306 static inline int dquot_alloc_space(struct inode *inode, qsize_t nr)
307 {
308 	int ret;
309 
310 	ret = dquot_alloc_space_nodirty(inode, nr);
311 	if (!ret) {
312 		/*
313 		 * Mark inode fully dirty. Since we are allocating blocks, inode
314 		 * would become fully dirty soon anyway and it reportedly
315 		 * reduces lock contention.
316 		 */
317 		mark_inode_dirty(inode);
318 	}
319 	return ret;
320 }
321 
dquot_alloc_block_nodirty(struct inode * inode,qsize_t nr)322 static inline int dquot_alloc_block_nodirty(struct inode *inode, qsize_t nr)
323 {
324 	return dquot_alloc_space_nodirty(inode, nr << inode->i_blkbits);
325 }
326 
dquot_alloc_block_nofail(struct inode * inode,qsize_t nr)327 static inline void dquot_alloc_block_nofail(struct inode *inode, qsize_t nr)
328 {
329 	dquot_alloc_space_nofail(inode, nr << inode->i_blkbits);
330 }
331 
dquot_alloc_block(struct inode * inode,qsize_t nr)332 static inline int dquot_alloc_block(struct inode *inode, qsize_t nr)
333 {
334 	return dquot_alloc_space(inode, nr << inode->i_blkbits);
335 }
336 
dquot_prealloc_block_nodirty(struct inode * inode,qsize_t nr)337 static inline int dquot_prealloc_block_nodirty(struct inode *inode, qsize_t nr)
338 {
339 	return __dquot_alloc_space(inode, nr << inode->i_blkbits, 0);
340 }
341 
dquot_prealloc_block(struct inode * inode,qsize_t nr)342 static inline int dquot_prealloc_block(struct inode *inode, qsize_t nr)
343 {
344 	int ret;
345 
346 	ret = dquot_prealloc_block_nodirty(inode, nr);
347 	if (!ret)
348 		mark_inode_dirty_sync(inode);
349 	return ret;
350 }
351 
dquot_reserve_block(struct inode * inode,qsize_t nr)352 static inline int dquot_reserve_block(struct inode *inode, qsize_t nr)
353 {
354 	return __dquot_alloc_space(inode, nr << inode->i_blkbits,
355 				DQUOT_SPACE_WARN|DQUOT_SPACE_RESERVE);
356 }
357 
dquot_claim_block(struct inode * inode,qsize_t nr)358 static inline int dquot_claim_block(struct inode *inode, qsize_t nr)
359 {
360 	int ret;
361 
362 	ret = dquot_claim_space_nodirty(inode, nr << inode->i_blkbits);
363 	if (!ret)
364 		mark_inode_dirty_sync(inode);
365 	return ret;
366 }
367 
dquot_reclaim_block(struct inode * inode,qsize_t nr)368 static inline void dquot_reclaim_block(struct inode *inode, qsize_t nr)
369 {
370 	dquot_reclaim_space_nodirty(inode, nr << inode->i_blkbits);
371 	mark_inode_dirty_sync(inode);
372 }
373 
dquot_free_space_nodirty(struct inode * inode,qsize_t nr)374 static inline void dquot_free_space_nodirty(struct inode *inode, qsize_t nr)
375 {
376 	__dquot_free_space(inode, nr, 0);
377 }
378 
dquot_free_space(struct inode * inode,qsize_t nr)379 static inline void dquot_free_space(struct inode *inode, qsize_t nr)
380 {
381 	dquot_free_space_nodirty(inode, nr);
382 	mark_inode_dirty_sync(inode);
383 }
384 
dquot_free_block_nodirty(struct inode * inode,qsize_t nr)385 static inline void dquot_free_block_nodirty(struct inode *inode, qsize_t nr)
386 {
387 	dquot_free_space_nodirty(inode, nr << inode->i_blkbits);
388 }
389 
dquot_free_block(struct inode * inode,qsize_t nr)390 static inline void dquot_free_block(struct inode *inode, qsize_t nr)
391 {
392 	dquot_free_space(inode, nr << inode->i_blkbits);
393 }
394 
dquot_release_reservation_block(struct inode * inode,qsize_t nr)395 static inline void dquot_release_reservation_block(struct inode *inode,
396 		qsize_t nr)
397 {
398 	__dquot_free_space(inode, nr << inode->i_blkbits, DQUOT_SPACE_RESERVE);
399 }
400 
401 unsigned int qtype_enforce_flag(int type);
402 
403 #endif /* _LINUX_QUOTAOPS_ */
404