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