]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Implementation of the diskquota system for the LINUX operating system. QUOTA | |
3 | * is implemented using the BSD system call interface as the means of | |
4 | * communication with the user level. This file contains the generic routines | |
5 | * called by the different filesystems on allocation of an inode or block. | |
6 | * These routines take care of the administration needed to have a consistent | |
7 | * diskquota tracking system. The ideas of both user and group quotas are based | |
8 | * on the Melbourne quota system as used on BSD derived systems. The internal | |
9 | * implementation is based on one of the several variants of the LINUX | |
10 | * inode-subsystem with added complexity of the diskquota system. | |
11 | * | |
1da177e4 LT |
12 | * Author: Marco van Wieringen <mvw@planets.elm.net> |
13 | * | |
14 | * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96 | |
15 | * | |
16 | * Revised list management to avoid races | |
17 | * -- Bill Hawes, <whawes@star.net>, 9/98 | |
18 | * | |
19 | * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...(). | |
20 | * As the consequence the locking was moved from dquot_decr_...(), | |
21 | * dquot_incr_...() to calling functions. | |
22 | * invalidate_dquots() now writes modified dquots. | |
23 | * Serialized quota_off() and quota_on() for mount point. | |
24 | * Fixed a few bugs in grow_dquots(). | |
25 | * Fixed deadlock in write_dquot() - we no longer account quotas on | |
26 | * quota files | |
27 | * remove_dquot_ref() moved to inode.c - it now traverses through inodes | |
28 | * add_dquot_ref() restarts after blocking | |
29 | * Added check for bogus uid and fixed check for group in quotactl. | |
30 | * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99 | |
31 | * | |
32 | * Used struct list_head instead of own list struct | |
33 | * Invalidation of referenced dquots is no longer possible | |
34 | * Improved free_dquots list management | |
35 | * Quota and i_blocks are now updated in one place to avoid races | |
36 | * Warnings are now delayed so we won't block in critical section | |
37 | * Write updated not to require dquot lock | |
38 | * Jan Kara, <jack@suse.cz>, 9/2000 | |
39 | * | |
40 | * Added dynamic quota structure allocation | |
41 | * Jan Kara <jack@suse.cz> 12/2000 | |
42 | * | |
43 | * Rewritten quota interface. Implemented new quota format and | |
44 | * formats registering. | |
45 | * Jan Kara, <jack@suse.cz>, 2001,2002 | |
46 | * | |
47 | * New SMP locking. | |
48 | * Jan Kara, <jack@suse.cz>, 10/2002 | |
49 | * | |
50 | * Added journalled quota support, fix lock inversion problems | |
51 | * Jan Kara, <jack@suse.cz>, 2003,2004 | |
52 | * | |
53 | * (C) Copyright 1994 - 1997 Marco van Wieringen | |
54 | */ | |
55 | ||
56 | #include <linux/errno.h> | |
57 | #include <linux/kernel.h> | |
58 | #include <linux/fs.h> | |
59 | #include <linux/mount.h> | |
60 | #include <linux/mm.h> | |
61 | #include <linux/time.h> | |
62 | #include <linux/types.h> | |
63 | #include <linux/string.h> | |
64 | #include <linux/fcntl.h> | |
65 | #include <linux/stat.h> | |
66 | #include <linux/tty.h> | |
67 | #include <linux/file.h> | |
68 | #include <linux/slab.h> | |
69 | #include <linux/sysctl.h> | |
1da177e4 LT |
70 | #include <linux/init.h> |
71 | #include <linux/module.h> | |
72 | #include <linux/proc_fs.h> | |
73 | #include <linux/security.h> | |
74 | #include <linux/kmod.h> | |
75 | #include <linux/namei.h> | |
76 | #include <linux/buffer_head.h> | |
16f7e0fe | 77 | #include <linux/capability.h> |
be586bab | 78 | #include <linux/quotaops.h> |
55fa6091 | 79 | #include "../internal.h" /* ugh */ |
1da177e4 LT |
80 | |
81 | #include <asm/uaccess.h> | |
82 | ||
1da177e4 | 83 | /* |
cc33412f | 84 | * There are three quota SMP locks. dq_list_lock protects all lists with quotas |
dde95888 | 85 | * and quota formats. |
cc33412f JK |
86 | * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and |
87 | * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes. | |
1da177e4 | 88 | * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly |
cc33412f JK |
89 | * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects |
90 | * modifications of quota state (on quotaon and quotaoff) and readers who care | |
91 | * about latest values take it as well. | |
1da177e4 | 92 | * |
cc33412f JK |
93 | * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock, |
94 | * dq_list_lock > dq_state_lock | |
1da177e4 LT |
95 | * |
96 | * Note that some things (eg. sb pointer, type, id) doesn't change during | |
97 | * the life of the dquot structure and so needn't to be protected by a lock | |
98 | * | |
99 | * Any operation working on dquots via inode pointers must hold dqptr_sem. If | |
100 | * operation is just reading pointers from inode (or not using them at all) the | |
26245c94 JK |
101 | * read lock is enough. If pointers are altered function must hold write lock. |
102 | * Special care needs to be taken about S_NOQUOTA inode flag (marking that | |
103 | * inode is a quota file). Functions adding pointers from inode to dquots have | |
104 | * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they | |
105 | * have to do all pointer modifications before dropping dqptr_sem. This makes | |
106 | * sure they cannot race with quotaon which first sets S_NOQUOTA flag and | |
107 | * then drops all pointers to dquots from an inode. | |
1da177e4 | 108 | * |
d3be915f | 109 | * Each dquot has its dq_lock mutex. Locked dquots might not be referenced |
1da177e4 LT |
110 | * from inodes (dquot_alloc_space() and such don't check the dq_lock). |
111 | * Currently dquot is locked only when it is being read to memory (or space for | |
112 | * it is being allocated) on the first dqget() and when it is being released on | |
113 | * the last dqput(). The allocation and release oparations are serialized by | |
114 | * the dq_lock and by checking the use count in dquot_release(). Write | |
115 | * operations on dquots don't hold dq_lock as they copy data under dq_data_lock | |
116 | * spinlock to internal buffers before writing. | |
117 | * | |
118 | * Lock ordering (including related VFS locks) is the following: | |
d3be915f IM |
119 | * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock > |
120 | * dqio_mutex | |
cc33412f JK |
121 | * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem > |
122 | * dqptr_sem. But filesystem has to count with the fact that functions such as | |
123 | * dquot_alloc_space() acquire dqptr_sem and they usually have to be called | |
124 | * from inside a transaction to keep filesystem consistency after a crash. Also | |
125 | * filesystems usually want to do some IO on dquot from ->mark_dirty which is | |
126 | * called with dqptr_sem held. | |
d3be915f | 127 | * i_mutex on quota files is special (it's below dqio_mutex) |
1da177e4 LT |
128 | */ |
129 | ||
c516610c JK |
130 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock); |
131 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock); | |
132 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock); | |
08d0350c | 133 | EXPORT_SYMBOL(dq_data_lock); |
1da177e4 | 134 | |
fb5ffb0e | 135 | void __quota_error(struct super_block *sb, const char *func, |
055adcbd | 136 | const char *fmt, ...) |
fb5ffb0e | 137 | { |
fb5ffb0e | 138 | if (printk_ratelimit()) { |
055adcbd JP |
139 | va_list args; |
140 | struct va_format vaf; | |
141 | ||
fb5ffb0e | 142 | va_start(args, fmt); |
055adcbd JP |
143 | |
144 | vaf.fmt = fmt; | |
145 | vaf.va = &args; | |
146 | ||
147 | printk(KERN_ERR "Quota error (device %s): %s: %pV\n", | |
148 | sb->s_id, func, &vaf); | |
149 | ||
fb5ffb0e JZ |
150 | va_end(args); |
151 | } | |
152 | } | |
153 | EXPORT_SYMBOL(__quota_error); | |
154 | ||
da8d1ba2 | 155 | #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING) |
1da177e4 | 156 | static char *quotatypes[] = INITQFNAMES; |
da8d1ba2 | 157 | #endif |
1da177e4 LT |
158 | static struct quota_format_type *quota_formats; /* List of registered formats */ |
159 | static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES; | |
160 | ||
161 | /* SLAB cache for dquot structures */ | |
e18b890b | 162 | static struct kmem_cache *dquot_cachep; |
1da177e4 LT |
163 | |
164 | int register_quota_format(struct quota_format_type *fmt) | |
165 | { | |
166 | spin_lock(&dq_list_lock); | |
167 | fmt->qf_next = quota_formats; | |
168 | quota_formats = fmt; | |
169 | spin_unlock(&dq_list_lock); | |
170 | return 0; | |
171 | } | |
08d0350c | 172 | EXPORT_SYMBOL(register_quota_format); |
1da177e4 LT |
173 | |
174 | void unregister_quota_format(struct quota_format_type *fmt) | |
175 | { | |
176 | struct quota_format_type **actqf; | |
177 | ||
178 | spin_lock(&dq_list_lock); | |
268157ba JK |
179 | for (actqf = "a_formats; *actqf && *actqf != fmt; |
180 | actqf = &(*actqf)->qf_next) | |
181 | ; | |
1da177e4 LT |
182 | if (*actqf) |
183 | *actqf = (*actqf)->qf_next; | |
184 | spin_unlock(&dq_list_lock); | |
185 | } | |
08d0350c | 186 | EXPORT_SYMBOL(unregister_quota_format); |
1da177e4 LT |
187 | |
188 | static struct quota_format_type *find_quota_format(int id) | |
189 | { | |
190 | struct quota_format_type *actqf; | |
191 | ||
192 | spin_lock(&dq_list_lock); | |
268157ba JK |
193 | for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; |
194 | actqf = actqf->qf_next) | |
195 | ; | |
1da177e4 LT |
196 | if (!actqf || !try_module_get(actqf->qf_owner)) { |
197 | int qm; | |
198 | ||
199 | spin_unlock(&dq_list_lock); | |
200 | ||
268157ba JK |
201 | for (qm = 0; module_names[qm].qm_fmt_id && |
202 | module_names[qm].qm_fmt_id != id; qm++) | |
203 | ; | |
204 | if (!module_names[qm].qm_fmt_id || | |
205 | request_module(module_names[qm].qm_mod_name)) | |
1da177e4 LT |
206 | return NULL; |
207 | ||
208 | spin_lock(&dq_list_lock); | |
268157ba JK |
209 | for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; |
210 | actqf = actqf->qf_next) | |
211 | ; | |
1da177e4 LT |
212 | if (actqf && !try_module_get(actqf->qf_owner)) |
213 | actqf = NULL; | |
214 | } | |
215 | spin_unlock(&dq_list_lock); | |
216 | return actqf; | |
217 | } | |
218 | ||
219 | static void put_quota_format(struct quota_format_type *fmt) | |
220 | { | |
221 | module_put(fmt->qf_owner); | |
222 | } | |
223 | ||
224 | /* | |
225 | * Dquot List Management: | |
226 | * The quota code uses three lists for dquot management: the inuse_list, | |
227 | * free_dquots, and dquot_hash[] array. A single dquot structure may be | |
228 | * on all three lists, depending on its current state. | |
229 | * | |
230 | * All dquots are placed to the end of inuse_list when first created, and this | |
231 | * list is used for invalidate operation, which must look at every dquot. | |
232 | * | |
233 | * Unused dquots (dq_count == 0) are added to the free_dquots list when freed, | |
234 | * and this list is searched whenever we need an available dquot. Dquots are | |
235 | * removed from the list as soon as they are used again, and | |
236 | * dqstats.free_dquots gives the number of dquots on the list. When | |
237 | * dquot is invalidated it's completely released from memory. | |
238 | * | |
239 | * Dquots with a specific identity (device, type and id) are placed on | |
240 | * one of the dquot_hash[] hash chains. The provides an efficient search | |
241 | * mechanism to locate a specific dquot. | |
242 | */ | |
243 | ||
244 | static LIST_HEAD(inuse_list); | |
245 | static LIST_HEAD(free_dquots); | |
246 | static unsigned int dq_hash_bits, dq_hash_mask; | |
247 | static struct hlist_head *dquot_hash; | |
248 | ||
249 | struct dqstats dqstats; | |
08d0350c | 250 | EXPORT_SYMBOL(dqstats); |
1da177e4 | 251 | |
0a5a9c72 | 252 | static qsize_t inode_get_rsv_space(struct inode *inode); |
871a2931 | 253 | static void __dquot_initialize(struct inode *inode, int type); |
0a5a9c72 | 254 | |
1da177e4 LT |
255 | static inline unsigned int |
256 | hashfn(const struct super_block *sb, unsigned int id, int type) | |
257 | { | |
258 | unsigned long tmp; | |
259 | ||
260 | tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type); | |
261 | return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask; | |
262 | } | |
263 | ||
264 | /* | |
265 | * Following list functions expect dq_list_lock to be held | |
266 | */ | |
267 | static inline void insert_dquot_hash(struct dquot *dquot) | |
268 | { | |
268157ba JK |
269 | struct hlist_head *head; |
270 | head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type); | |
1da177e4 LT |
271 | hlist_add_head(&dquot->dq_hash, head); |
272 | } | |
273 | ||
274 | static inline void remove_dquot_hash(struct dquot *dquot) | |
275 | { | |
276 | hlist_del_init(&dquot->dq_hash); | |
277 | } | |
278 | ||
7a2435d8 JK |
279 | static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, |
280 | unsigned int id, int type) | |
1da177e4 LT |
281 | { |
282 | struct hlist_node *node; | |
283 | struct dquot *dquot; | |
284 | ||
285 | hlist_for_each (node, dquot_hash+hashent) { | |
286 | dquot = hlist_entry(node, struct dquot, dq_hash); | |
268157ba JK |
287 | if (dquot->dq_sb == sb && dquot->dq_id == id && |
288 | dquot->dq_type == type) | |
1da177e4 LT |
289 | return dquot; |
290 | } | |
dd6f3c6d | 291 | return NULL; |
1da177e4 LT |
292 | } |
293 | ||
294 | /* Add a dquot to the tail of the free list */ | |
295 | static inline void put_dquot_last(struct dquot *dquot) | |
296 | { | |
8e13059a | 297 | list_add_tail(&dquot->dq_free, &free_dquots); |
dde95888 | 298 | dqstats_inc(DQST_FREE_DQUOTS); |
1da177e4 LT |
299 | } |
300 | ||
301 | static inline void remove_free_dquot(struct dquot *dquot) | |
302 | { | |
303 | if (list_empty(&dquot->dq_free)) | |
304 | return; | |
305 | list_del_init(&dquot->dq_free); | |
dde95888 | 306 | dqstats_dec(DQST_FREE_DQUOTS); |
1da177e4 LT |
307 | } |
308 | ||
309 | static inline void put_inuse(struct dquot *dquot) | |
310 | { | |
311 | /* We add to the back of inuse list so we don't have to restart | |
312 | * when traversing this list and we block */ | |
8e13059a | 313 | list_add_tail(&dquot->dq_inuse, &inuse_list); |
dde95888 | 314 | dqstats_inc(DQST_ALLOC_DQUOTS); |
1da177e4 LT |
315 | } |
316 | ||
317 | static inline void remove_inuse(struct dquot *dquot) | |
318 | { | |
dde95888 | 319 | dqstats_dec(DQST_ALLOC_DQUOTS); |
1da177e4 LT |
320 | list_del(&dquot->dq_inuse); |
321 | } | |
322 | /* | |
323 | * End of list functions needing dq_list_lock | |
324 | */ | |
325 | ||
326 | static void wait_on_dquot(struct dquot *dquot) | |
327 | { | |
d3be915f IM |
328 | mutex_lock(&dquot->dq_lock); |
329 | mutex_unlock(&dquot->dq_lock); | |
1da177e4 LT |
330 | } |
331 | ||
03f6e92b JK |
332 | static inline int dquot_dirty(struct dquot *dquot) |
333 | { | |
334 | return test_bit(DQ_MOD_B, &dquot->dq_flags); | |
335 | } | |
336 | ||
337 | static inline int mark_dquot_dirty(struct dquot *dquot) | |
338 | { | |
339 | return dquot->dq_sb->dq_op->mark_dirty(dquot); | |
340 | } | |
1da177e4 | 341 | |
eabf290d | 342 | /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */ |
1da177e4 LT |
343 | int dquot_mark_dquot_dirty(struct dquot *dquot) |
344 | { | |
eabf290d DM |
345 | int ret = 1; |
346 | ||
347 | /* If quota is dirty already, we don't have to acquire dq_list_lock */ | |
348 | if (test_bit(DQ_MOD_B, &dquot->dq_flags)) | |
349 | return 1; | |
350 | ||
1da177e4 | 351 | spin_lock(&dq_list_lock); |
eabf290d | 352 | if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) { |
1da177e4 LT |
353 | list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)-> |
354 | info[dquot->dq_type].dqi_dirty_list); | |
eabf290d DM |
355 | ret = 0; |
356 | } | |
1da177e4 | 357 | spin_unlock(&dq_list_lock); |
eabf290d | 358 | return ret; |
1da177e4 | 359 | } |
08d0350c | 360 | EXPORT_SYMBOL(dquot_mark_dquot_dirty); |
1da177e4 | 361 | |
dc52dd3a DM |
362 | /* Dirtify all the dquots - this can block when journalling */ |
363 | static inline int mark_all_dquot_dirty(struct dquot * const *dquot) | |
364 | { | |
365 | int ret, err, cnt; | |
366 | ||
367 | ret = err = 0; | |
368 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
369 | if (dquot[cnt]) | |
370 | /* Even in case of error we have to continue */ | |
371 | ret = mark_dquot_dirty(dquot[cnt]); | |
372 | if (!err) | |
373 | err = ret; | |
374 | } | |
375 | return err; | |
376 | } | |
377 | ||
378 | static inline void dqput_all(struct dquot **dquot) | |
379 | { | |
380 | unsigned int cnt; | |
381 | ||
382 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | |
383 | dqput(dquot[cnt]); | |
384 | } | |
385 | ||
1da177e4 LT |
386 | /* This function needs dq_list_lock */ |
387 | static inline int clear_dquot_dirty(struct dquot *dquot) | |
388 | { | |
389 | if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) | |
390 | return 0; | |
391 | list_del_init(&dquot->dq_dirty); | |
392 | return 1; | |
393 | } | |
394 | ||
395 | void mark_info_dirty(struct super_block *sb, int type) | |
396 | { | |
397 | set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags); | |
398 | } | |
399 | EXPORT_SYMBOL(mark_info_dirty); | |
400 | ||
401 | /* | |
402 | * Read dquot from disk and alloc space for it | |
403 | */ | |
404 | ||
405 | int dquot_acquire(struct dquot *dquot) | |
406 | { | |
407 | int ret = 0, ret2 = 0; | |
408 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); | |
409 | ||
d3be915f IM |
410 | mutex_lock(&dquot->dq_lock); |
411 | mutex_lock(&dqopt->dqio_mutex); | |
1da177e4 LT |
412 | if (!test_bit(DQ_READ_B, &dquot->dq_flags)) |
413 | ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot); | |
414 | if (ret < 0) | |
415 | goto out_iolock; | |
416 | set_bit(DQ_READ_B, &dquot->dq_flags); | |
417 | /* Instantiate dquot if needed */ | |
418 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) { | |
419 | ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot); | |
420 | /* Write the info if needed */ | |
268157ba JK |
421 | if (info_dirty(&dqopt->info[dquot->dq_type])) { |
422 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info( | |
423 | dquot->dq_sb, dquot->dq_type); | |
424 | } | |
1da177e4 LT |
425 | if (ret < 0) |
426 | goto out_iolock; | |
427 | if (ret2 < 0) { | |
428 | ret = ret2; | |
429 | goto out_iolock; | |
430 | } | |
431 | } | |
432 | set_bit(DQ_ACTIVE_B, &dquot->dq_flags); | |
433 | out_iolock: | |
d3be915f IM |
434 | mutex_unlock(&dqopt->dqio_mutex); |
435 | mutex_unlock(&dquot->dq_lock); | |
1da177e4 LT |
436 | return ret; |
437 | } | |
08d0350c | 438 | EXPORT_SYMBOL(dquot_acquire); |
1da177e4 LT |
439 | |
440 | /* | |
441 | * Write dquot to disk | |
442 | */ | |
443 | int dquot_commit(struct dquot *dquot) | |
444 | { | |
b03f2456 | 445 | int ret = 0; |
1da177e4 LT |
446 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); |
447 | ||
d3be915f | 448 | mutex_lock(&dqopt->dqio_mutex); |
1da177e4 LT |
449 | spin_lock(&dq_list_lock); |
450 | if (!clear_dquot_dirty(dquot)) { | |
451 | spin_unlock(&dq_list_lock); | |
452 | goto out_sem; | |
453 | } | |
454 | spin_unlock(&dq_list_lock); | |
455 | /* Inactive dquot can be only if there was error during read/init | |
456 | * => we have better not writing it */ | |
b03f2456 | 457 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) |
1da177e4 | 458 | ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot); |
b03f2456 JK |
459 | else |
460 | ret = -EIO; | |
1da177e4 | 461 | out_sem: |
d3be915f | 462 | mutex_unlock(&dqopt->dqio_mutex); |
1da177e4 LT |
463 | return ret; |
464 | } | |
08d0350c | 465 | EXPORT_SYMBOL(dquot_commit); |
1da177e4 LT |
466 | |
467 | /* | |
468 | * Release dquot | |
469 | */ | |
470 | int dquot_release(struct dquot *dquot) | |
471 | { | |
472 | int ret = 0, ret2 = 0; | |
473 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); | |
474 | ||
d3be915f | 475 | mutex_lock(&dquot->dq_lock); |
1da177e4 LT |
476 | /* Check whether we are not racing with some other dqget() */ |
477 | if (atomic_read(&dquot->dq_count) > 1) | |
478 | goto out_dqlock; | |
d3be915f | 479 | mutex_lock(&dqopt->dqio_mutex); |
1da177e4 LT |
480 | if (dqopt->ops[dquot->dq_type]->release_dqblk) { |
481 | ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot); | |
482 | /* Write the info */ | |
268157ba JK |
483 | if (info_dirty(&dqopt->info[dquot->dq_type])) { |
484 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info( | |
485 | dquot->dq_sb, dquot->dq_type); | |
486 | } | |
1da177e4 LT |
487 | if (ret >= 0) |
488 | ret = ret2; | |
489 | } | |
490 | clear_bit(DQ_ACTIVE_B, &dquot->dq_flags); | |
d3be915f | 491 | mutex_unlock(&dqopt->dqio_mutex); |
1da177e4 | 492 | out_dqlock: |
d3be915f | 493 | mutex_unlock(&dquot->dq_lock); |
1da177e4 LT |
494 | return ret; |
495 | } | |
08d0350c | 496 | EXPORT_SYMBOL(dquot_release); |
1da177e4 | 497 | |
7d9056ba | 498 | void dquot_destroy(struct dquot *dquot) |
74f783af JK |
499 | { |
500 | kmem_cache_free(dquot_cachep, dquot); | |
501 | } | |
7d9056ba | 502 | EXPORT_SYMBOL(dquot_destroy); |
74f783af JK |
503 | |
504 | static inline void do_destroy_dquot(struct dquot *dquot) | |
505 | { | |
506 | dquot->dq_sb->dq_op->destroy_dquot(dquot); | |
507 | } | |
508 | ||
1da177e4 LT |
509 | /* Invalidate all dquots on the list. Note that this function is called after |
510 | * quota is disabled and pointers from inodes removed so there cannot be new | |
6362e4d4 JK |
511 | * quota users. There can still be some users of quotas due to inodes being |
512 | * just deleted or pruned by prune_icache() (those are not attached to any | |
cc33412f | 513 | * list) or parallel quotactl call. We have to wait for such users. |
6362e4d4 | 514 | */ |
1da177e4 LT |
515 | static void invalidate_dquots(struct super_block *sb, int type) |
516 | { | |
c33ed271 | 517 | struct dquot *dquot, *tmp; |
1da177e4 | 518 | |
6362e4d4 | 519 | restart: |
1da177e4 | 520 | spin_lock(&dq_list_lock); |
c33ed271 | 521 | list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) { |
1da177e4 LT |
522 | if (dquot->dq_sb != sb) |
523 | continue; | |
524 | if (dquot->dq_type != type) | |
525 | continue; | |
6362e4d4 JK |
526 | /* Wait for dquot users */ |
527 | if (atomic_read(&dquot->dq_count)) { | |
528 | DEFINE_WAIT(wait); | |
529 | ||
530 | atomic_inc(&dquot->dq_count); | |
531 | prepare_to_wait(&dquot->dq_wait_unused, &wait, | |
532 | TASK_UNINTERRUPTIBLE); | |
533 | spin_unlock(&dq_list_lock); | |
534 | /* Once dqput() wakes us up, we know it's time to free | |
535 | * the dquot. | |
536 | * IMPORTANT: we rely on the fact that there is always | |
537 | * at most one process waiting for dquot to free. | |
538 | * Otherwise dq_count would be > 1 and we would never | |
539 | * wake up. | |
540 | */ | |
541 | if (atomic_read(&dquot->dq_count) > 1) | |
542 | schedule(); | |
543 | finish_wait(&dquot->dq_wait_unused, &wait); | |
544 | dqput(dquot); | |
545 | /* At this moment dquot() need not exist (it could be | |
546 | * reclaimed by prune_dqcache(). Hence we must | |
547 | * restart. */ | |
548 | goto restart; | |
549 | } | |
550 | /* | |
551 | * Quota now has no users and it has been written on last | |
552 | * dqput() | |
553 | */ | |
1da177e4 LT |
554 | remove_dquot_hash(dquot); |
555 | remove_free_dquot(dquot); | |
556 | remove_inuse(dquot); | |
74f783af | 557 | do_destroy_dquot(dquot); |
1da177e4 LT |
558 | } |
559 | spin_unlock(&dq_list_lock); | |
560 | } | |
561 | ||
12c77527 JK |
562 | /* Call callback for every active dquot on given filesystem */ |
563 | int dquot_scan_active(struct super_block *sb, | |
564 | int (*fn)(struct dquot *dquot, unsigned long priv), | |
565 | unsigned long priv) | |
566 | { | |
567 | struct dquot *dquot, *old_dquot = NULL; | |
568 | int ret = 0; | |
569 | ||
570 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); | |
571 | spin_lock(&dq_list_lock); | |
572 | list_for_each_entry(dquot, &inuse_list, dq_inuse) { | |
573 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) | |
574 | continue; | |
575 | if (dquot->dq_sb != sb) | |
576 | continue; | |
577 | /* Now we have active dquot so we can just increase use count */ | |
578 | atomic_inc(&dquot->dq_count); | |
12c77527 | 579 | spin_unlock(&dq_list_lock); |
dde95888 | 580 | dqstats_inc(DQST_LOOKUPS); |
12c77527 JK |
581 | dqput(old_dquot); |
582 | old_dquot = dquot; | |
583 | ret = fn(dquot, priv); | |
584 | if (ret < 0) | |
585 | goto out; | |
586 | spin_lock(&dq_list_lock); | |
587 | /* We are safe to continue now because our dquot could not | |
588 | * be moved out of the inuse list while we hold the reference */ | |
589 | } | |
590 | spin_unlock(&dq_list_lock); | |
591 | out: | |
592 | dqput(old_dquot); | |
593 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); | |
594 | return ret; | |
595 | } | |
08d0350c | 596 | EXPORT_SYMBOL(dquot_scan_active); |
12c77527 | 597 | |
287a8095 | 598 | int dquot_quota_sync(struct super_block *sb, int type, int wait) |
1da177e4 LT |
599 | { |
600 | struct list_head *dirty; | |
601 | struct dquot *dquot; | |
602 | struct quota_info *dqopt = sb_dqopt(sb); | |
603 | int cnt; | |
604 | ||
d3be915f | 605 | mutex_lock(&dqopt->dqonoff_mutex); |
1da177e4 LT |
606 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
607 | if (type != -1 && cnt != type) | |
608 | continue; | |
f55abc0f | 609 | if (!sb_has_quota_active(sb, cnt)) |
1da177e4 LT |
610 | continue; |
611 | spin_lock(&dq_list_lock); | |
612 | dirty = &dqopt->info[cnt].dqi_dirty_list; | |
613 | while (!list_empty(dirty)) { | |
268157ba JK |
614 | dquot = list_first_entry(dirty, struct dquot, |
615 | dq_dirty); | |
1da177e4 LT |
616 | /* Dirty and inactive can be only bad dquot... */ |
617 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { | |
618 | clear_dquot_dirty(dquot); | |
619 | continue; | |
620 | } | |
621 | /* Now we have active dquot from which someone is | |
622 | * holding reference so we can safely just increase | |
623 | * use count */ | |
624 | atomic_inc(&dquot->dq_count); | |
1da177e4 | 625 | spin_unlock(&dq_list_lock); |
dde95888 | 626 | dqstats_inc(DQST_LOOKUPS); |
1da177e4 LT |
627 | sb->dq_op->write_dquot(dquot); |
628 | dqput(dquot); | |
629 | spin_lock(&dq_list_lock); | |
630 | } | |
631 | spin_unlock(&dq_list_lock); | |
632 | } | |
633 | ||
634 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | |
f55abc0f JK |
635 | if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt) |
636 | && info_dirty(&dqopt->info[cnt])) | |
1da177e4 | 637 | sb->dq_op->write_info(sb, cnt); |
dde95888 | 638 | dqstats_inc(DQST_SYNCS); |
d3be915f | 639 | mutex_unlock(&dqopt->dqonoff_mutex); |
1da177e4 | 640 | |
5fb324ad CH |
641 | if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)) |
642 | return 0; | |
643 | ||
644 | /* This is not very clever (and fast) but currently I don't know about | |
645 | * any other simple way of getting quota data to disk and we must get | |
646 | * them there for userspace to be visible... */ | |
647 | if (sb->s_op->sync_fs) | |
648 | sb->s_op->sync_fs(sb, 1); | |
649 | sync_blockdev(sb->s_bdev); | |
650 | ||
651 | /* | |
652 | * Now when everything is written we can discard the pagecache so | |
653 | * that userspace sees the changes. | |
654 | */ | |
655 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); | |
656 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
657 | if (type != -1 && cnt != type) | |
658 | continue; | |
659 | if (!sb_has_quota_active(sb, cnt)) | |
660 | continue; | |
661 | mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex, | |
662 | I_MUTEX_QUOTA); | |
663 | truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0); | |
664 | mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex); | |
665 | } | |
666 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); | |
667 | ||
1da177e4 LT |
668 | return 0; |
669 | } | |
287a8095 | 670 | EXPORT_SYMBOL(dquot_quota_sync); |
1da177e4 LT |
671 | |
672 | /* Free unused dquots from cache */ | |
673 | static void prune_dqcache(int count) | |
674 | { | |
675 | struct list_head *head; | |
676 | struct dquot *dquot; | |
677 | ||
678 | head = free_dquots.prev; | |
679 | while (head != &free_dquots && count) { | |
680 | dquot = list_entry(head, struct dquot, dq_free); | |
681 | remove_dquot_hash(dquot); | |
682 | remove_free_dquot(dquot); | |
683 | remove_inuse(dquot); | |
74f783af | 684 | do_destroy_dquot(dquot); |
1da177e4 LT |
685 | count--; |
686 | head = free_dquots.prev; | |
687 | } | |
688 | } | |
689 | ||
690 | /* | |
691 | * This is called from kswapd when we think we need some | |
692 | * more memory | |
693 | */ | |
1495f230 YH |
694 | static int shrink_dqcache_memory(struct shrinker *shrink, |
695 | struct shrink_control *sc) | |
1da177e4 | 696 | { |
1495f230 YH |
697 | int nr = sc->nr_to_scan; |
698 | ||
1da177e4 LT |
699 | if (nr) { |
700 | spin_lock(&dq_list_lock); | |
701 | prune_dqcache(nr); | |
702 | spin_unlock(&dq_list_lock); | |
703 | } | |
f32764bd DM |
704 | return ((unsigned) |
705 | percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]) | |
706 | /100) * sysctl_vfs_cache_pressure; | |
1da177e4 LT |
707 | } |
708 | ||
8e1f936b RR |
709 | static struct shrinker dqcache_shrinker = { |
710 | .shrink = shrink_dqcache_memory, | |
711 | .seeks = DEFAULT_SEEKS, | |
712 | }; | |
713 | ||
1da177e4 LT |
714 | /* |
715 | * Put reference to dquot | |
716 | * NOTE: If you change this function please check whether dqput_blocks() works right... | |
1da177e4 | 717 | */ |
3d9ea253 | 718 | void dqput(struct dquot *dquot) |
1da177e4 | 719 | { |
b48d3805 JK |
720 | int ret; |
721 | ||
1da177e4 LT |
722 | if (!dquot) |
723 | return; | |
62af9b52 | 724 | #ifdef CONFIG_QUOTA_DEBUG |
1da177e4 | 725 | if (!atomic_read(&dquot->dq_count)) { |
fb5ffb0e JZ |
726 | quota_error(dquot->dq_sb, "trying to free free dquot of %s %d", |
727 | quotatypes[dquot->dq_type], dquot->dq_id); | |
1da177e4 LT |
728 | BUG(); |
729 | } | |
730 | #endif | |
dde95888 | 731 | dqstats_inc(DQST_DROPS); |
1da177e4 LT |
732 | we_slept: |
733 | spin_lock(&dq_list_lock); | |
734 | if (atomic_read(&dquot->dq_count) > 1) { | |
735 | /* We have more than one user... nothing to do */ | |
736 | atomic_dec(&dquot->dq_count); | |
6362e4d4 | 737 | /* Releasing dquot during quotaoff phase? */ |
f55abc0f | 738 | if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) && |
6362e4d4 JK |
739 | atomic_read(&dquot->dq_count) == 1) |
740 | wake_up(&dquot->dq_wait_unused); | |
1da177e4 LT |
741 | spin_unlock(&dq_list_lock); |
742 | return; | |
743 | } | |
744 | /* Need to release dquot? */ | |
745 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) { | |
746 | spin_unlock(&dq_list_lock); | |
747 | /* Commit dquot before releasing */ | |
b48d3805 JK |
748 | ret = dquot->dq_sb->dq_op->write_dquot(dquot); |
749 | if (ret < 0) { | |
fb5ffb0e JZ |
750 | quota_error(dquot->dq_sb, "Can't write quota structure" |
751 | " (error %d). Quota may get out of sync!", | |
752 | ret); | |
b48d3805 JK |
753 | /* |
754 | * We clear dirty bit anyway, so that we avoid | |
755 | * infinite loop here | |
756 | */ | |
757 | spin_lock(&dq_list_lock); | |
758 | clear_dquot_dirty(dquot); | |
759 | spin_unlock(&dq_list_lock); | |
760 | } | |
1da177e4 LT |
761 | goto we_slept; |
762 | } | |
763 | /* Clear flag in case dquot was inactive (something bad happened) */ | |
764 | clear_dquot_dirty(dquot); | |
765 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { | |
766 | spin_unlock(&dq_list_lock); | |
767 | dquot->dq_sb->dq_op->release_dquot(dquot); | |
768 | goto we_slept; | |
769 | } | |
770 | atomic_dec(&dquot->dq_count); | |
62af9b52 | 771 | #ifdef CONFIG_QUOTA_DEBUG |
1da177e4 | 772 | /* sanity check */ |
8abf6a47 | 773 | BUG_ON(!list_empty(&dquot->dq_free)); |
1da177e4 LT |
774 | #endif |
775 | put_dquot_last(dquot); | |
776 | spin_unlock(&dq_list_lock); | |
777 | } | |
08d0350c | 778 | EXPORT_SYMBOL(dqput); |
1da177e4 | 779 | |
7d9056ba | 780 | struct dquot *dquot_alloc(struct super_block *sb, int type) |
74f783af JK |
781 | { |
782 | return kmem_cache_zalloc(dquot_cachep, GFP_NOFS); | |
783 | } | |
7d9056ba | 784 | EXPORT_SYMBOL(dquot_alloc); |
74f783af | 785 | |
1da177e4 LT |
786 | static struct dquot *get_empty_dquot(struct super_block *sb, int type) |
787 | { | |
788 | struct dquot *dquot; | |
789 | ||
74f783af | 790 | dquot = sb->dq_op->alloc_dquot(sb, type); |
1da177e4 | 791 | if(!dquot) |
dd6f3c6d | 792 | return NULL; |
1da177e4 | 793 | |
d3be915f | 794 | mutex_init(&dquot->dq_lock); |
1da177e4 LT |
795 | INIT_LIST_HEAD(&dquot->dq_free); |
796 | INIT_LIST_HEAD(&dquot->dq_inuse); | |
797 | INIT_HLIST_NODE(&dquot->dq_hash); | |
798 | INIT_LIST_HEAD(&dquot->dq_dirty); | |
6362e4d4 | 799 | init_waitqueue_head(&dquot->dq_wait_unused); |
1da177e4 LT |
800 | dquot->dq_sb = sb; |
801 | dquot->dq_type = type; | |
802 | atomic_set(&dquot->dq_count, 1); | |
803 | ||
804 | return dquot; | |
805 | } | |
806 | ||
807 | /* | |
808 | * Get reference to dquot | |
cc33412f JK |
809 | * |
810 | * Locking is slightly tricky here. We are guarded from parallel quotaoff() | |
811 | * destroying our dquot by: | |
812 | * a) checking for quota flags under dq_list_lock and | |
813 | * b) getting a reference to dquot before we release dq_list_lock | |
1da177e4 | 814 | */ |
3d9ea253 | 815 | struct dquot *dqget(struct super_block *sb, unsigned int id, int type) |
1da177e4 LT |
816 | { |
817 | unsigned int hashent = hashfn(sb, id, type); | |
dd6f3c6d | 818 | struct dquot *dquot = NULL, *empty = NULL; |
1da177e4 | 819 | |
f55abc0f | 820 | if (!sb_has_quota_active(sb, type)) |
dd6f3c6d | 821 | return NULL; |
1da177e4 LT |
822 | we_slept: |
823 | spin_lock(&dq_list_lock); | |
cc33412f JK |
824 | spin_lock(&dq_state_lock); |
825 | if (!sb_has_quota_active(sb, type)) { | |
826 | spin_unlock(&dq_state_lock); | |
827 | spin_unlock(&dq_list_lock); | |
828 | goto out; | |
829 | } | |
830 | spin_unlock(&dq_state_lock); | |
831 | ||
dd6f3c6d JK |
832 | dquot = find_dquot(hashent, sb, id, type); |
833 | if (!dquot) { | |
834 | if (!empty) { | |
1da177e4 | 835 | spin_unlock(&dq_list_lock); |
dd6f3c6d JK |
836 | empty = get_empty_dquot(sb, type); |
837 | if (!empty) | |
1da177e4 LT |
838 | schedule(); /* Try to wait for a moment... */ |
839 | goto we_slept; | |
840 | } | |
841 | dquot = empty; | |
dd6f3c6d | 842 | empty = NULL; |
1da177e4 LT |
843 | dquot->dq_id = id; |
844 | /* all dquots go on the inuse_list */ | |
845 | put_inuse(dquot); | |
846 | /* hash it first so it can be found */ | |
847 | insert_dquot_hash(dquot); | |
1da177e4 | 848 | spin_unlock(&dq_list_lock); |
dde95888 | 849 | dqstats_inc(DQST_LOOKUPS); |
1da177e4 LT |
850 | } else { |
851 | if (!atomic_read(&dquot->dq_count)) | |
852 | remove_free_dquot(dquot); | |
853 | atomic_inc(&dquot->dq_count); | |
1da177e4 | 854 | spin_unlock(&dq_list_lock); |
dde95888 DM |
855 | dqstats_inc(DQST_CACHE_HITS); |
856 | dqstats_inc(DQST_LOOKUPS); | |
1da177e4 | 857 | } |
268157ba JK |
858 | /* Wait for dq_lock - after this we know that either dquot_release() is |
859 | * already finished or it will be canceled due to dq_count > 1 test */ | |
1da177e4 | 860 | wait_on_dquot(dquot); |
268157ba JK |
861 | /* Read the dquot / allocate space in quota file */ |
862 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && | |
863 | sb->dq_op->acquire_dquot(dquot) < 0) { | |
1da177e4 | 864 | dqput(dquot); |
dd6f3c6d | 865 | dquot = NULL; |
cc33412f | 866 | goto out; |
1da177e4 | 867 | } |
62af9b52 | 868 | #ifdef CONFIG_QUOTA_DEBUG |
8abf6a47 | 869 | BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */ |
1da177e4 | 870 | #endif |
cc33412f JK |
871 | out: |
872 | if (empty) | |
873 | do_destroy_dquot(empty); | |
1da177e4 LT |
874 | |
875 | return dquot; | |
876 | } | |
08d0350c | 877 | EXPORT_SYMBOL(dqget); |
1da177e4 LT |
878 | |
879 | static int dqinit_needed(struct inode *inode, int type) | |
880 | { | |
881 | int cnt; | |
882 | ||
883 | if (IS_NOQUOTA(inode)) | |
884 | return 0; | |
885 | if (type != -1) | |
dd6f3c6d | 886 | return !inode->i_dquot[type]; |
1da177e4 | 887 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
dd6f3c6d | 888 | if (!inode->i_dquot[cnt]) |
1da177e4 LT |
889 | return 1; |
890 | return 0; | |
891 | } | |
892 | ||
d3be915f | 893 | /* This routine is guarded by dqonoff_mutex mutex */ |
1da177e4 LT |
894 | static void add_dquot_ref(struct super_block *sb, int type) |
895 | { | |
941d2380 | 896 | struct inode *inode, *old_inode = NULL; |
62af9b52 | 897 | #ifdef CONFIG_QUOTA_DEBUG |
0a5a9c72 | 898 | int reserved = 0; |
4c5e6c0e | 899 | #endif |
1da177e4 | 900 | |
55fa6091 | 901 | spin_lock(&inode_sb_list_lock); |
d003fb70 | 902 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
250df6ed DC |
903 | spin_lock(&inode->i_lock); |
904 | if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || | |
905 | !atomic_read(&inode->i_writecount) || | |
906 | !dqinit_needed(inode, type)) { | |
907 | spin_unlock(&inode->i_lock); | |
aabb8fdb | 908 | continue; |
250df6ed | 909 | } |
62af9b52 | 910 | #ifdef CONFIG_QUOTA_DEBUG |
0a5a9c72 JK |
911 | if (unlikely(inode_get_rsv_space(inode) > 0)) |
912 | reserved = 1; | |
4c5e6c0e | 913 | #endif |
d003fb70 | 914 | __iget(inode); |
250df6ed | 915 | spin_unlock(&inode->i_lock); |
55fa6091 | 916 | spin_unlock(&inode_sb_list_lock); |
d003fb70 | 917 | |
941d2380 | 918 | iput(old_inode); |
871a2931 | 919 | __dquot_initialize(inode, type); |
55fa6091 DC |
920 | |
921 | /* | |
922 | * We hold a reference to 'inode' so it couldn't have been | |
923 | * removed from s_inodes list while we dropped the | |
924 | * inode_sb_list_lock We cannot iput the inode now as we can be | |
925 | * holding the last reference and we cannot iput it under | |
926 | * inode_sb_list_lock. So we keep the reference and iput it | |
927 | * later. | |
928 | */ | |
941d2380 | 929 | old_inode = inode; |
55fa6091 | 930 | spin_lock(&inode_sb_list_lock); |
1da177e4 | 931 | } |
55fa6091 | 932 | spin_unlock(&inode_sb_list_lock); |
941d2380 | 933 | iput(old_inode); |
0a5a9c72 | 934 | |
62af9b52 | 935 | #ifdef CONFIG_QUOTA_DEBUG |
0a5a9c72 | 936 | if (reserved) { |
fb5ffb0e JZ |
937 | quota_error(sb, "Writes happened before quota was turned on " |
938 | "thus quota information is probably inconsistent. " | |
939 | "Please run quotacheck(8)"); | |
0a5a9c72 | 940 | } |
4c5e6c0e | 941 | #endif |
1da177e4 LT |
942 | } |
943 | ||
268157ba JK |
944 | /* |
945 | * Return 0 if dqput() won't block. | |
946 | * (note that 1 doesn't necessarily mean blocking) | |
947 | */ | |
1da177e4 LT |
948 | static inline int dqput_blocks(struct dquot *dquot) |
949 | { | |
950 | if (atomic_read(&dquot->dq_count) <= 1) | |
951 | return 1; | |
952 | return 0; | |
953 | } | |
954 | ||
268157ba JK |
955 | /* |
956 | * Remove references to dquots from inode and add dquot to list for freeing | |
25985edc | 957 | * if we have the last reference to dquot |
268157ba JK |
958 | * We can't race with anybody because we hold dqptr_sem for writing... |
959 | */ | |
e5f00f42 AB |
960 | static int remove_inode_dquot_ref(struct inode *inode, int type, |
961 | struct list_head *tofree_head) | |
1da177e4 LT |
962 | { |
963 | struct dquot *dquot = inode->i_dquot[type]; | |
964 | ||
dd6f3c6d JK |
965 | inode->i_dquot[type] = NULL; |
966 | if (dquot) { | |
1da177e4 | 967 | if (dqput_blocks(dquot)) { |
62af9b52 | 968 | #ifdef CONFIG_QUOTA_DEBUG |
1da177e4 | 969 | if (atomic_read(&dquot->dq_count) != 1) |
fb5ffb0e JZ |
970 | quota_error(inode->i_sb, "Adding dquot with " |
971 | "dq_count %d to dispose list", | |
972 | atomic_read(&dquot->dq_count)); | |
1da177e4 LT |
973 | #endif |
974 | spin_lock(&dq_list_lock); | |
268157ba JK |
975 | /* As dquot must have currently users it can't be on |
976 | * the free list... */ | |
977 | list_add(&dquot->dq_free, tofree_head); | |
1da177e4 LT |
978 | spin_unlock(&dq_list_lock); |
979 | return 1; | |
980 | } | |
981 | else | |
982 | dqput(dquot); /* We have guaranteed we won't block */ | |
983 | } | |
984 | return 0; | |
985 | } | |
986 | ||
268157ba JK |
987 | /* |
988 | * Free list of dquots | |
989 | * Dquots are removed from inodes and no new references can be got so we are | |
990 | * the only ones holding reference | |
991 | */ | |
1da177e4 LT |
992 | static void put_dquot_list(struct list_head *tofree_head) |
993 | { | |
994 | struct list_head *act_head; | |
995 | struct dquot *dquot; | |
996 | ||
997 | act_head = tofree_head->next; | |
1da177e4 LT |
998 | while (act_head != tofree_head) { |
999 | dquot = list_entry(act_head, struct dquot, dq_free); | |
1000 | act_head = act_head->next; | |
268157ba JK |
1001 | /* Remove dquot from the list so we won't have problems... */ |
1002 | list_del_init(&dquot->dq_free); | |
1da177e4 LT |
1003 | dqput(dquot); |
1004 | } | |
1005 | } | |
1006 | ||
fb58b731 CH |
1007 | static void remove_dquot_ref(struct super_block *sb, int type, |
1008 | struct list_head *tofree_head) | |
1009 | { | |
1010 | struct inode *inode; | |
7af9cce8 | 1011 | int reserved = 0; |
fb58b731 | 1012 | |
55fa6091 | 1013 | spin_lock(&inode_sb_list_lock); |
fb58b731 | 1014 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
aabb8fdb NP |
1015 | /* |
1016 | * We have to scan also I_NEW inodes because they can already | |
1017 | * have quota pointer initialized. Luckily, we need to touch | |
1018 | * only quota pointers and these have separate locking | |
1019 | * (dqptr_sem). | |
1020 | */ | |
7af9cce8 DM |
1021 | if (!IS_NOQUOTA(inode)) { |
1022 | if (unlikely(inode_get_rsv_space(inode) > 0)) | |
1023 | reserved = 1; | |
fb58b731 | 1024 | remove_inode_dquot_ref(inode, type, tofree_head); |
7af9cce8 | 1025 | } |
fb58b731 | 1026 | } |
55fa6091 | 1027 | spin_unlock(&inode_sb_list_lock); |
7af9cce8 DM |
1028 | #ifdef CONFIG_QUOTA_DEBUG |
1029 | if (reserved) { | |
1030 | printk(KERN_WARNING "VFS (%s): Writes happened after quota" | |
1031 | " was disabled thus quota information is probably " | |
1032 | "inconsistent. Please run quotacheck(8).\n", sb->s_id); | |
1033 | } | |
1034 | #endif | |
fb58b731 CH |
1035 | } |
1036 | ||
1da177e4 LT |
1037 | /* Gather all references from inodes and drop them */ |
1038 | static void drop_dquot_ref(struct super_block *sb, int type) | |
1039 | { | |
1040 | LIST_HEAD(tofree_head); | |
1041 | ||
fb58b731 CH |
1042 | if (sb->dq_op) { |
1043 | down_write(&sb_dqopt(sb)->dqptr_sem); | |
1044 | remove_dquot_ref(sb, type, &tofree_head); | |
1045 | up_write(&sb_dqopt(sb)->dqptr_sem); | |
1046 | put_dquot_list(&tofree_head); | |
1047 | } | |
1da177e4 LT |
1048 | } |
1049 | ||
12095460 | 1050 | static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number) |
1da177e4 LT |
1051 | { |
1052 | dquot->dq_dqb.dqb_curinodes += number; | |
1053 | } | |
1054 | ||
1055 | static inline void dquot_incr_space(struct dquot *dquot, qsize_t number) | |
1056 | { | |
1057 | dquot->dq_dqb.dqb_curspace += number; | |
1058 | } | |
1059 | ||
f18df228 MC |
1060 | static inline void dquot_resv_space(struct dquot *dquot, qsize_t number) |
1061 | { | |
1062 | dquot->dq_dqb.dqb_rsvspace += number; | |
1063 | } | |
1064 | ||
740d9dcd MC |
1065 | /* |
1066 | * Claim reserved quota space | |
1067 | */ | |
0a5a9c72 | 1068 | static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number) |
740d9dcd | 1069 | { |
0a5a9c72 JK |
1070 | if (dquot->dq_dqb.dqb_rsvspace < number) { |
1071 | WARN_ON_ONCE(1); | |
1072 | number = dquot->dq_dqb.dqb_rsvspace; | |
1073 | } | |
740d9dcd MC |
1074 | dquot->dq_dqb.dqb_curspace += number; |
1075 | dquot->dq_dqb.dqb_rsvspace -= number; | |
1076 | } | |
1077 | ||
1078 | static inline | |
1079 | void dquot_free_reserved_space(struct dquot *dquot, qsize_t number) | |
1080 | { | |
0a5a9c72 JK |
1081 | if (dquot->dq_dqb.dqb_rsvspace >= number) |
1082 | dquot->dq_dqb.dqb_rsvspace -= number; | |
1083 | else { | |
1084 | WARN_ON_ONCE(1); | |
1085 | dquot->dq_dqb.dqb_rsvspace = 0; | |
1086 | } | |
740d9dcd MC |
1087 | } |
1088 | ||
7a2435d8 | 1089 | static void dquot_decr_inodes(struct dquot *dquot, qsize_t number) |
1da177e4 | 1090 | { |
db49d2df JK |
1091 | if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE || |
1092 | dquot->dq_dqb.dqb_curinodes >= number) | |
1da177e4 LT |
1093 | dquot->dq_dqb.dqb_curinodes -= number; |
1094 | else | |
1095 | dquot->dq_dqb.dqb_curinodes = 0; | |
1096 | if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit) | |
1097 | dquot->dq_dqb.dqb_itime = (time_t) 0; | |
1098 | clear_bit(DQ_INODES_B, &dquot->dq_flags); | |
1099 | } | |
1100 | ||
7a2435d8 | 1101 | static void dquot_decr_space(struct dquot *dquot, qsize_t number) |
1da177e4 | 1102 | { |
db49d2df JK |
1103 | if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE || |
1104 | dquot->dq_dqb.dqb_curspace >= number) | |
1da177e4 LT |
1105 | dquot->dq_dqb.dqb_curspace -= number; |
1106 | else | |
1107 | dquot->dq_dqb.dqb_curspace = 0; | |
12095460 | 1108 | if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit) |
1da177e4 LT |
1109 | dquot->dq_dqb.dqb_btime = (time_t) 0; |
1110 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); | |
1111 | } | |
1112 | ||
c525460e JK |
1113 | static int warning_issued(struct dquot *dquot, const int warntype) |
1114 | { | |
1115 | int flag = (warntype == QUOTA_NL_BHARDWARN || | |
1116 | warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B : | |
1117 | ((warntype == QUOTA_NL_IHARDWARN || | |
1118 | warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0); | |
1119 | ||
1120 | if (!flag) | |
1121 | return 0; | |
1122 | return test_and_set_bit(flag, &dquot->dq_flags); | |
1123 | } | |
1124 | ||
8e893469 | 1125 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
1da177e4 LT |
1126 | static int flag_print_warnings = 1; |
1127 | ||
7a2435d8 | 1128 | static int need_print_warning(struct dquot *dquot) |
1da177e4 LT |
1129 | { |
1130 | if (!flag_print_warnings) | |
1131 | return 0; | |
1132 | ||
1133 | switch (dquot->dq_type) { | |
1134 | case USRQUOTA: | |
da9592ed | 1135 | return current_fsuid() == dquot->dq_id; |
1da177e4 LT |
1136 | case GRPQUOTA: |
1137 | return in_group_p(dquot->dq_id); | |
1138 | } | |
1139 | return 0; | |
1140 | } | |
1141 | ||
1da177e4 | 1142 | /* Print warning to user which exceeded quota */ |
c525460e | 1143 | static void print_warning(struct dquot *dquot, const int warntype) |
1da177e4 LT |
1144 | { |
1145 | char *msg = NULL; | |
24ec839c | 1146 | struct tty_struct *tty; |
1da177e4 | 1147 | |
657d3bfa JK |
1148 | if (warntype == QUOTA_NL_IHARDBELOW || |
1149 | warntype == QUOTA_NL_ISOFTBELOW || | |
1150 | warntype == QUOTA_NL_BHARDBELOW || | |
1151 | warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot)) | |
1da177e4 LT |
1152 | return; |
1153 | ||
24ec839c PZ |
1154 | tty = get_current_tty(); |
1155 | if (!tty) | |
452a00d2 | 1156 | return; |
24ec839c | 1157 | tty_write_message(tty, dquot->dq_sb->s_id); |
8e893469 | 1158 | if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN) |
24ec839c | 1159 | tty_write_message(tty, ": warning, "); |
1da177e4 | 1160 | else |
24ec839c PZ |
1161 | tty_write_message(tty, ": write failed, "); |
1162 | tty_write_message(tty, quotatypes[dquot->dq_type]); | |
1da177e4 | 1163 | switch (warntype) { |
8e893469 | 1164 | case QUOTA_NL_IHARDWARN: |
1da177e4 LT |
1165 | msg = " file limit reached.\r\n"; |
1166 | break; | |
8e893469 | 1167 | case QUOTA_NL_ISOFTLONGWARN: |
1da177e4 LT |
1168 | msg = " file quota exceeded too long.\r\n"; |
1169 | break; | |
8e893469 | 1170 | case QUOTA_NL_ISOFTWARN: |
1da177e4 LT |
1171 | msg = " file quota exceeded.\r\n"; |
1172 | break; | |
8e893469 | 1173 | case QUOTA_NL_BHARDWARN: |
1da177e4 LT |
1174 | msg = " block limit reached.\r\n"; |
1175 | break; | |
8e893469 | 1176 | case QUOTA_NL_BSOFTLONGWARN: |
1da177e4 LT |
1177 | msg = " block quota exceeded too long.\r\n"; |
1178 | break; | |
8e893469 | 1179 | case QUOTA_NL_BSOFTWARN: |
1da177e4 LT |
1180 | msg = " block quota exceeded.\r\n"; |
1181 | break; | |
1182 | } | |
24ec839c | 1183 | tty_write_message(tty, msg); |
452a00d2 | 1184 | tty_kref_put(tty); |
1da177e4 | 1185 | } |
8e893469 JK |
1186 | #endif |
1187 | ||
f18df228 MC |
1188 | /* |
1189 | * Write warnings to the console and send warning messages over netlink. | |
1190 | * | |
1191 | * Note that this function can sleep. | |
1192 | */ | |
7a2435d8 | 1193 | static void flush_warnings(struct dquot *const *dquots, char *warntype) |
1da177e4 | 1194 | { |
86e931a3 | 1195 | struct dquot *dq; |
1da177e4 LT |
1196 | int i; |
1197 | ||
86e931a3 SW |
1198 | for (i = 0; i < MAXQUOTAS; i++) { |
1199 | dq = dquots[i]; | |
1200 | if (dq && warntype[i] != QUOTA_NL_NOWARN && | |
1201 | !warning_issued(dq, warntype[i])) { | |
8e893469 | 1202 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
86e931a3 | 1203 | print_warning(dq, warntype[i]); |
8e893469 | 1204 | #endif |
86e931a3 SW |
1205 | quota_send_warning(dq->dq_type, dq->dq_id, |
1206 | dq->dq_sb->s_dev, warntype[i]); | |
8e893469 | 1207 | } |
86e931a3 | 1208 | } |
1da177e4 LT |
1209 | } |
1210 | ||
7a2435d8 | 1211 | static int ignore_hardlimit(struct dquot *dquot) |
1da177e4 LT |
1212 | { |
1213 | struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type]; | |
1214 | ||
1215 | return capable(CAP_SYS_RESOURCE) && | |
268157ba JK |
1216 | (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || |
1217 | !(info->dqi_flags & V1_DQF_RSQUASH)); | |
1da177e4 LT |
1218 | } |
1219 | ||
1220 | /* needs dq_data_lock */ | |
12095460 | 1221 | static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype) |
1da177e4 | 1222 | { |
268157ba JK |
1223 | qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes; |
1224 | ||
8e893469 | 1225 | *warntype = QUOTA_NL_NOWARN; |
f55abc0f JK |
1226 | if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) || |
1227 | test_bit(DQ_FAKE_B, &dquot->dq_flags)) | |
efd8f0e6 | 1228 | return 0; |
1da177e4 LT |
1229 | |
1230 | if (dquot->dq_dqb.dqb_ihardlimit && | |
268157ba | 1231 | newinodes > dquot->dq_dqb.dqb_ihardlimit && |
1da177e4 | 1232 | !ignore_hardlimit(dquot)) { |
8e893469 | 1233 | *warntype = QUOTA_NL_IHARDWARN; |
efd8f0e6 | 1234 | return -EDQUOT; |
1da177e4 LT |
1235 | } |
1236 | ||
1237 | if (dquot->dq_dqb.dqb_isoftlimit && | |
268157ba JK |
1238 | newinodes > dquot->dq_dqb.dqb_isoftlimit && |
1239 | dquot->dq_dqb.dqb_itime && | |
1240 | get_seconds() >= dquot->dq_dqb.dqb_itime && | |
1da177e4 | 1241 | !ignore_hardlimit(dquot)) { |
8e893469 | 1242 | *warntype = QUOTA_NL_ISOFTLONGWARN; |
efd8f0e6 | 1243 | return -EDQUOT; |
1da177e4 LT |
1244 | } |
1245 | ||
1246 | if (dquot->dq_dqb.dqb_isoftlimit && | |
268157ba | 1247 | newinodes > dquot->dq_dqb.dqb_isoftlimit && |
1da177e4 | 1248 | dquot->dq_dqb.dqb_itime == 0) { |
8e893469 | 1249 | *warntype = QUOTA_NL_ISOFTWARN; |
268157ba JK |
1250 | dquot->dq_dqb.dqb_itime = get_seconds() + |
1251 | sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace; | |
1da177e4 LT |
1252 | } |
1253 | ||
efd8f0e6 | 1254 | return 0; |
1da177e4 LT |
1255 | } |
1256 | ||
1257 | /* needs dq_data_lock */ | |
1258 | static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype) | |
1259 | { | |
f18df228 | 1260 | qsize_t tspace; |
268157ba | 1261 | struct super_block *sb = dquot->dq_sb; |
f18df228 | 1262 | |
8e893469 | 1263 | *warntype = QUOTA_NL_NOWARN; |
268157ba | 1264 | if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) || |
f55abc0f | 1265 | test_bit(DQ_FAKE_B, &dquot->dq_flags)) |
efd8f0e6 | 1266 | return 0; |
1da177e4 | 1267 | |
f18df228 MC |
1268 | tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace |
1269 | + space; | |
1270 | ||
1da177e4 | 1271 | if (dquot->dq_dqb.dqb_bhardlimit && |
f18df228 | 1272 | tspace > dquot->dq_dqb.dqb_bhardlimit && |
1da177e4 LT |
1273 | !ignore_hardlimit(dquot)) { |
1274 | if (!prealloc) | |
8e893469 | 1275 | *warntype = QUOTA_NL_BHARDWARN; |
efd8f0e6 | 1276 | return -EDQUOT; |
1da177e4 LT |
1277 | } |
1278 | ||
1279 | if (dquot->dq_dqb.dqb_bsoftlimit && | |
f18df228 | 1280 | tspace > dquot->dq_dqb.dqb_bsoftlimit && |
268157ba JK |
1281 | dquot->dq_dqb.dqb_btime && |
1282 | get_seconds() >= dquot->dq_dqb.dqb_btime && | |
1da177e4 LT |
1283 | !ignore_hardlimit(dquot)) { |
1284 | if (!prealloc) | |
8e893469 | 1285 | *warntype = QUOTA_NL_BSOFTLONGWARN; |
efd8f0e6 | 1286 | return -EDQUOT; |
1da177e4 LT |
1287 | } |
1288 | ||
1289 | if (dquot->dq_dqb.dqb_bsoftlimit && | |
f18df228 | 1290 | tspace > dquot->dq_dqb.dqb_bsoftlimit && |
1da177e4 LT |
1291 | dquot->dq_dqb.dqb_btime == 0) { |
1292 | if (!prealloc) { | |
8e893469 | 1293 | *warntype = QUOTA_NL_BSOFTWARN; |
268157ba JK |
1294 | dquot->dq_dqb.dqb_btime = get_seconds() + |
1295 | sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace; | |
1da177e4 LT |
1296 | } |
1297 | else | |
1298 | /* | |
1299 | * We don't allow preallocation to exceed softlimit so exceeding will | |
1300 | * be always printed | |
1301 | */ | |
efd8f0e6 | 1302 | return -EDQUOT; |
1da177e4 LT |
1303 | } |
1304 | ||
efd8f0e6 | 1305 | return 0; |
1da177e4 LT |
1306 | } |
1307 | ||
12095460 | 1308 | static int info_idq_free(struct dquot *dquot, qsize_t inodes) |
657d3bfa | 1309 | { |
268157ba JK |
1310 | qsize_t newinodes; |
1311 | ||
657d3bfa | 1312 | if (test_bit(DQ_FAKE_B, &dquot->dq_flags) || |
f55abc0f JK |
1313 | dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit || |
1314 | !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type)) | |
657d3bfa JK |
1315 | return QUOTA_NL_NOWARN; |
1316 | ||
268157ba JK |
1317 | newinodes = dquot->dq_dqb.dqb_curinodes - inodes; |
1318 | if (newinodes <= dquot->dq_dqb.dqb_isoftlimit) | |
657d3bfa JK |
1319 | return QUOTA_NL_ISOFTBELOW; |
1320 | if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit && | |
268157ba | 1321 | newinodes < dquot->dq_dqb.dqb_ihardlimit) |
657d3bfa JK |
1322 | return QUOTA_NL_IHARDBELOW; |
1323 | return QUOTA_NL_NOWARN; | |
1324 | } | |
1325 | ||
1326 | static int info_bdq_free(struct dquot *dquot, qsize_t space) | |
1327 | { | |
1328 | if (test_bit(DQ_FAKE_B, &dquot->dq_flags) || | |
12095460 | 1329 | dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit) |
657d3bfa JK |
1330 | return QUOTA_NL_NOWARN; |
1331 | ||
12095460 | 1332 | if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit) |
657d3bfa | 1333 | return QUOTA_NL_BSOFTBELOW; |
12095460 JK |
1334 | if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit && |
1335 | dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit) | |
657d3bfa JK |
1336 | return QUOTA_NL_BHARDBELOW; |
1337 | return QUOTA_NL_NOWARN; | |
1338 | } | |
0a5a9c72 | 1339 | |
189eef59 CH |
1340 | static int dquot_active(const struct inode *inode) |
1341 | { | |
1342 | struct super_block *sb = inode->i_sb; | |
1343 | ||
1344 | if (IS_NOQUOTA(inode)) | |
1345 | return 0; | |
1346 | return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb); | |
1347 | } | |
1348 | ||
1da177e4 | 1349 | /* |
871a2931 CH |
1350 | * Initialize quota pointers in inode |
1351 | * | |
1352 | * We do things in a bit complicated way but by that we avoid calling | |
1353 | * dqget() and thus filesystem callbacks under dqptr_sem. | |
1354 | * | |
1355 | * It is better to call this function outside of any transaction as it | |
1356 | * might need a lot of space in journal for dquot structure allocation. | |
1da177e4 | 1357 | */ |
871a2931 | 1358 | static void __dquot_initialize(struct inode *inode, int type) |
1da177e4 LT |
1359 | { |
1360 | unsigned int id = 0; | |
871a2931 | 1361 | int cnt; |
ad1e6e8d | 1362 | struct dquot *got[MAXQUOTAS]; |
cc33412f | 1363 | struct super_block *sb = inode->i_sb; |
0a5a9c72 | 1364 | qsize_t rsv; |
1da177e4 | 1365 | |
d3be915f IM |
1366 | /* First test before acquiring mutex - solves deadlocks when we |
1367 | * re-enter the quota code and are already holding the mutex */ | |
189eef59 | 1368 | if (!dquot_active(inode)) |
871a2931 | 1369 | return; |
cc33412f JK |
1370 | |
1371 | /* First get references to structures we might need. */ | |
1372 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
ad1e6e8d | 1373 | got[cnt] = NULL; |
cc33412f JK |
1374 | if (type != -1 && cnt != type) |
1375 | continue; | |
1376 | switch (cnt) { | |
1377 | case USRQUOTA: | |
1378 | id = inode->i_uid; | |
1379 | break; | |
1380 | case GRPQUOTA: | |
1381 | id = inode->i_gid; | |
1382 | break; | |
1383 | } | |
1384 | got[cnt] = dqget(sb, id, cnt); | |
1385 | } | |
1386 | ||
1387 | down_write(&sb_dqopt(sb)->dqptr_sem); | |
1da177e4 LT |
1388 | if (IS_NOQUOTA(inode)) |
1389 | goto out_err; | |
1390 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
1391 | if (type != -1 && cnt != type) | |
1392 | continue; | |
cc33412f JK |
1393 | /* Avoid races with quotaoff() */ |
1394 | if (!sb_has_quota_active(sb, cnt)) | |
1395 | continue; | |
4408ea41 JK |
1396 | /* We could race with quotaon or dqget() could have failed */ |
1397 | if (!got[cnt]) | |
1398 | continue; | |
dd6f3c6d | 1399 | if (!inode->i_dquot[cnt]) { |
cc33412f | 1400 | inode->i_dquot[cnt] = got[cnt]; |
dd6f3c6d | 1401 | got[cnt] = NULL; |
0a5a9c72 JK |
1402 | /* |
1403 | * Make quota reservation system happy if someone | |
1404 | * did a write before quota was turned on | |
1405 | */ | |
1406 | rsv = inode_get_rsv_space(inode); | |
1407 | if (unlikely(rsv)) | |
1408 | dquot_resv_space(inode->i_dquot[cnt], rsv); | |
1da177e4 LT |
1409 | } |
1410 | } | |
1411 | out_err: | |
cc33412f JK |
1412 | up_write(&sb_dqopt(sb)->dqptr_sem); |
1413 | /* Drop unused references */ | |
dc52dd3a | 1414 | dqput_all(got); |
871a2931 CH |
1415 | } |
1416 | ||
1417 | void dquot_initialize(struct inode *inode) | |
1418 | { | |
1419 | __dquot_initialize(inode, -1); | |
1da177e4 | 1420 | } |
08d0350c | 1421 | EXPORT_SYMBOL(dquot_initialize); |
1da177e4 LT |
1422 | |
1423 | /* | |
1424 | * Release all quotas referenced by inode | |
1da177e4 | 1425 | */ |
9f754758 | 1426 | static void __dquot_drop(struct inode *inode) |
1da177e4 LT |
1427 | { |
1428 | int cnt; | |
cc33412f | 1429 | struct dquot *put[MAXQUOTAS]; |
1da177e4 | 1430 | |
cc33412f | 1431 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
1da177e4 | 1432 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
cc33412f | 1433 | put[cnt] = inode->i_dquot[cnt]; |
dd6f3c6d | 1434 | inode->i_dquot[cnt] = NULL; |
1da177e4 LT |
1435 | } |
1436 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | |
dc52dd3a | 1437 | dqput_all(put); |
1da177e4 LT |
1438 | } |
1439 | ||
9f754758 CH |
1440 | void dquot_drop(struct inode *inode) |
1441 | { | |
1442 | int cnt; | |
1443 | ||
1444 | if (IS_NOQUOTA(inode)) | |
1445 | return; | |
1446 | ||
1447 | /* | |
1448 | * Test before calling to rule out calls from proc and such | |
1449 | * where we are not allowed to block. Note that this is | |
1450 | * actually reliable test even without the lock - the caller | |
1451 | * must assure that nobody can come after the DQUOT_DROP and | |
1452 | * add quota pointers back anyway. | |
1453 | */ | |
1454 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
1455 | if (inode->i_dquot[cnt]) | |
1456 | break; | |
1457 | } | |
1458 | ||
1459 | if (cnt < MAXQUOTAS) | |
1460 | __dquot_drop(inode); | |
1461 | } | |
1462 | EXPORT_SYMBOL(dquot_drop); | |
b85f4b87 | 1463 | |
fd8fbfc1 DM |
1464 | /* |
1465 | * inode_reserved_space is managed internally by quota, and protected by | |
1466 | * i_lock similar to i_blocks+i_bytes. | |
1467 | */ | |
1468 | static qsize_t *inode_reserved_space(struct inode * inode) | |
1469 | { | |
1470 | /* Filesystem must explicitly define it's own method in order to use | |
1471 | * quota reservation interface */ | |
1472 | BUG_ON(!inode->i_sb->dq_op->get_reserved_space); | |
1473 | return inode->i_sb->dq_op->get_reserved_space(inode); | |
1474 | } | |
1475 | ||
c469070a | 1476 | void inode_add_rsv_space(struct inode *inode, qsize_t number) |
fd8fbfc1 DM |
1477 | { |
1478 | spin_lock(&inode->i_lock); | |
1479 | *inode_reserved_space(inode) += number; | |
1480 | spin_unlock(&inode->i_lock); | |
1481 | } | |
c469070a | 1482 | EXPORT_SYMBOL(inode_add_rsv_space); |
fd8fbfc1 | 1483 | |
c469070a | 1484 | void inode_claim_rsv_space(struct inode *inode, qsize_t number) |
fd8fbfc1 DM |
1485 | { |
1486 | spin_lock(&inode->i_lock); | |
1487 | *inode_reserved_space(inode) -= number; | |
1488 | __inode_add_bytes(inode, number); | |
1489 | spin_unlock(&inode->i_lock); | |
1490 | } | |
c469070a | 1491 | EXPORT_SYMBOL(inode_claim_rsv_space); |
fd8fbfc1 | 1492 | |
c469070a | 1493 | void inode_sub_rsv_space(struct inode *inode, qsize_t number) |
fd8fbfc1 DM |
1494 | { |
1495 | spin_lock(&inode->i_lock); | |
1496 | *inode_reserved_space(inode) -= number; | |
1497 | spin_unlock(&inode->i_lock); | |
1498 | } | |
c469070a | 1499 | EXPORT_SYMBOL(inode_sub_rsv_space); |
fd8fbfc1 DM |
1500 | |
1501 | static qsize_t inode_get_rsv_space(struct inode *inode) | |
1502 | { | |
1503 | qsize_t ret; | |
05b5d898 JK |
1504 | |
1505 | if (!inode->i_sb->dq_op->get_reserved_space) | |
1506 | return 0; | |
fd8fbfc1 DM |
1507 | spin_lock(&inode->i_lock); |
1508 | ret = *inode_reserved_space(inode); | |
1509 | spin_unlock(&inode->i_lock); | |
1510 | return ret; | |
1511 | } | |
1512 | ||
1513 | static void inode_incr_space(struct inode *inode, qsize_t number, | |
1514 | int reserve) | |
1515 | { | |
1516 | if (reserve) | |
1517 | inode_add_rsv_space(inode, number); | |
1518 | else | |
1519 | inode_add_bytes(inode, number); | |
1520 | } | |
1521 | ||
1522 | static void inode_decr_space(struct inode *inode, qsize_t number, int reserve) | |
1523 | { | |
1524 | if (reserve) | |
1525 | inode_sub_rsv_space(inode, number); | |
1526 | else | |
1527 | inode_sub_bytes(inode, number); | |
1528 | } | |
1529 | ||
1da177e4 | 1530 | /* |
5dd4056d CH |
1531 | * This functions updates i_blocks+i_bytes fields and quota information |
1532 | * (together with appropriate checks). | |
1533 | * | |
1534 | * NOTE: We absolutely rely on the fact that caller dirties the inode | |
1535 | * (usually helpers in quotaops.h care about this) and holds a handle for | |
1536 | * the current transaction so that dquot write and inode write go into the | |
1537 | * same transaction. | |
1da177e4 LT |
1538 | */ |
1539 | ||
1540 | /* | |
1541 | * This operation can block, but only after everything is updated | |
1542 | */ | |
56246f9a | 1543 | int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags) |
1da177e4 | 1544 | { |
5dd4056d | 1545 | int cnt, ret = 0; |
1da177e4 | 1546 | char warntype[MAXQUOTAS]; |
56246f9a ES |
1547 | int warn = flags & DQUOT_SPACE_WARN; |
1548 | int reserve = flags & DQUOT_SPACE_RESERVE; | |
0e05842b | 1549 | int nofail = flags & DQUOT_SPACE_NOFAIL; |
1da177e4 | 1550 | |
fd8fbfc1 DM |
1551 | /* |
1552 | * First test before acquiring mutex - solves deadlocks when we | |
1553 | * re-enter the quota code and are already holding the mutex | |
1554 | */ | |
189eef59 | 1555 | if (!dquot_active(inode)) { |
fd8fbfc1 DM |
1556 | inode_incr_space(inode, number, reserve); |
1557 | goto out; | |
1558 | } | |
1559 | ||
1560 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | |
1da177e4 | 1561 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
8e893469 | 1562 | warntype[cnt] = QUOTA_NL_NOWARN; |
1da177e4 | 1563 | |
1da177e4 LT |
1564 | spin_lock(&dq_data_lock); |
1565 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
dd6f3c6d | 1566 | if (!inode->i_dquot[cnt]) |
1da177e4 | 1567 | continue; |
efd8f0e6 CH |
1568 | ret = check_bdq(inode->i_dquot[cnt], number, !warn, |
1569 | warntype+cnt); | |
0e05842b | 1570 | if (ret && !nofail) { |
fd8fbfc1 DM |
1571 | spin_unlock(&dq_data_lock); |
1572 | goto out_flush_warn; | |
f18df228 | 1573 | } |
1da177e4 LT |
1574 | } |
1575 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
dd6f3c6d | 1576 | if (!inode->i_dquot[cnt]) |
1da177e4 | 1577 | continue; |
f18df228 MC |
1578 | if (reserve) |
1579 | dquot_resv_space(inode->i_dquot[cnt], number); | |
1580 | else | |
1581 | dquot_incr_space(inode->i_dquot[cnt], number); | |
1da177e4 | 1582 | } |
fd8fbfc1 | 1583 | inode_incr_space(inode, number, reserve); |
1da177e4 | 1584 | spin_unlock(&dq_data_lock); |
f18df228 | 1585 | |
fd8fbfc1 DM |
1586 | if (reserve) |
1587 | goto out_flush_warn; | |
dc52dd3a | 1588 | mark_all_dquot_dirty(inode->i_dquot); |
fd8fbfc1 DM |
1589 | out_flush_warn: |
1590 | flush_warnings(inode->i_dquot, warntype); | |
1da177e4 | 1591 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
f18df228 MC |
1592 | out: |
1593 | return ret; | |
1594 | } | |
5dd4056d | 1595 | EXPORT_SYMBOL(__dquot_alloc_space); |
1da177e4 LT |
1596 | |
1597 | /* | |
1598 | * This operation can block, but only after everything is updated | |
1599 | */ | |
63936dda | 1600 | int dquot_alloc_inode(const struct inode *inode) |
1da177e4 | 1601 | { |
efd8f0e6 | 1602 | int cnt, ret = 0; |
1da177e4 LT |
1603 | char warntype[MAXQUOTAS]; |
1604 | ||
d3be915f IM |
1605 | /* First test before acquiring mutex - solves deadlocks when we |
1606 | * re-enter the quota code and are already holding the mutex */ | |
189eef59 | 1607 | if (!dquot_active(inode)) |
63936dda | 1608 | return 0; |
1da177e4 | 1609 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
8e893469 | 1610 | warntype[cnt] = QUOTA_NL_NOWARN; |
1da177e4 | 1611 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
1da177e4 LT |
1612 | spin_lock(&dq_data_lock); |
1613 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
dd6f3c6d | 1614 | if (!inode->i_dquot[cnt]) |
1da177e4 | 1615 | continue; |
efd8f0e6 CH |
1616 | ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt); |
1617 | if (ret) | |
1da177e4 LT |
1618 | goto warn_put_all; |
1619 | } | |
1620 | ||
1621 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
dd6f3c6d | 1622 | if (!inode->i_dquot[cnt]) |
1da177e4 | 1623 | continue; |
63936dda | 1624 | dquot_incr_inodes(inode->i_dquot[cnt], 1); |
1da177e4 | 1625 | } |
efd8f0e6 | 1626 | |
1da177e4 LT |
1627 | warn_put_all: |
1628 | spin_unlock(&dq_data_lock); | |
63936dda | 1629 | if (ret == 0) |
dc52dd3a | 1630 | mark_all_dquot_dirty(inode->i_dquot); |
087ee8d5 | 1631 | flush_warnings(inode->i_dquot, warntype); |
1da177e4 LT |
1632 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
1633 | return ret; | |
1634 | } | |
08d0350c | 1635 | EXPORT_SYMBOL(dquot_alloc_inode); |
1da177e4 | 1636 | |
5dd4056d CH |
1637 | /* |
1638 | * Convert in-memory reserved quotas to real consumed quotas | |
1639 | */ | |
1640 | int dquot_claim_space_nodirty(struct inode *inode, qsize_t number) | |
740d9dcd MC |
1641 | { |
1642 | int cnt; | |
740d9dcd | 1643 | |
189eef59 | 1644 | if (!dquot_active(inode)) { |
fd8fbfc1 | 1645 | inode_claim_rsv_space(inode, number); |
5dd4056d | 1646 | return 0; |
740d9dcd MC |
1647 | } |
1648 | ||
1649 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | |
740d9dcd MC |
1650 | spin_lock(&dq_data_lock); |
1651 | /* Claim reserved quotas to allocated quotas */ | |
1652 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
dd6f3c6d | 1653 | if (inode->i_dquot[cnt]) |
740d9dcd MC |
1654 | dquot_claim_reserved_space(inode->i_dquot[cnt], |
1655 | number); | |
1656 | } | |
1657 | /* Update inode bytes */ | |
fd8fbfc1 | 1658 | inode_claim_rsv_space(inode, number); |
740d9dcd | 1659 | spin_unlock(&dq_data_lock); |
dc52dd3a | 1660 | mark_all_dquot_dirty(inode->i_dquot); |
740d9dcd | 1661 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
5dd4056d | 1662 | return 0; |
740d9dcd | 1663 | } |
5dd4056d | 1664 | EXPORT_SYMBOL(dquot_claim_space_nodirty); |
740d9dcd | 1665 | |
1da177e4 LT |
1666 | /* |
1667 | * This operation can block, but only after everything is updated | |
1668 | */ | |
56246f9a | 1669 | void __dquot_free_space(struct inode *inode, qsize_t number, int flags) |
1da177e4 LT |
1670 | { |
1671 | unsigned int cnt; | |
657d3bfa | 1672 | char warntype[MAXQUOTAS]; |
56246f9a | 1673 | int reserve = flags & DQUOT_SPACE_RESERVE; |
1da177e4 | 1674 | |
d3be915f IM |
1675 | /* First test before acquiring mutex - solves deadlocks when we |
1676 | * re-enter the quota code and are already holding the mutex */ | |
189eef59 | 1677 | if (!dquot_active(inode)) { |
fd8fbfc1 | 1678 | inode_decr_space(inode, number, reserve); |
5dd4056d | 1679 | return; |
1da177e4 | 1680 | } |
657d3bfa | 1681 | |
1da177e4 | 1682 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
1da177e4 LT |
1683 | spin_lock(&dq_data_lock); |
1684 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
dd6f3c6d | 1685 | if (!inode->i_dquot[cnt]) |
1da177e4 | 1686 | continue; |
657d3bfa | 1687 | warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number); |
fd8fbfc1 DM |
1688 | if (reserve) |
1689 | dquot_free_reserved_space(inode->i_dquot[cnt], number); | |
1690 | else | |
1691 | dquot_decr_space(inode->i_dquot[cnt], number); | |
1da177e4 | 1692 | } |
fd8fbfc1 | 1693 | inode_decr_space(inode, number, reserve); |
1da177e4 | 1694 | spin_unlock(&dq_data_lock); |
fd8fbfc1 DM |
1695 | |
1696 | if (reserve) | |
1697 | goto out_unlock; | |
dc52dd3a | 1698 | mark_all_dquot_dirty(inode->i_dquot); |
fd8fbfc1 | 1699 | out_unlock: |
657d3bfa | 1700 | flush_warnings(inode->i_dquot, warntype); |
1da177e4 | 1701 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
fd8fbfc1 | 1702 | } |
5dd4056d | 1703 | EXPORT_SYMBOL(__dquot_free_space); |
fd8fbfc1 | 1704 | |
1da177e4 LT |
1705 | /* |
1706 | * This operation can block, but only after everything is updated | |
1707 | */ | |
63936dda | 1708 | void dquot_free_inode(const struct inode *inode) |
1da177e4 LT |
1709 | { |
1710 | unsigned int cnt; | |
657d3bfa | 1711 | char warntype[MAXQUOTAS]; |
1da177e4 | 1712 | |
d3be915f IM |
1713 | /* First test before acquiring mutex - solves deadlocks when we |
1714 | * re-enter the quota code and are already holding the mutex */ | |
189eef59 | 1715 | if (!dquot_active(inode)) |
63936dda | 1716 | return; |
657d3bfa | 1717 | |
1da177e4 | 1718 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
1da177e4 LT |
1719 | spin_lock(&dq_data_lock); |
1720 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
dd6f3c6d | 1721 | if (!inode->i_dquot[cnt]) |
1da177e4 | 1722 | continue; |
63936dda CH |
1723 | warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1); |
1724 | dquot_decr_inodes(inode->i_dquot[cnt], 1); | |
1da177e4 LT |
1725 | } |
1726 | spin_unlock(&dq_data_lock); | |
dc52dd3a | 1727 | mark_all_dquot_dirty(inode->i_dquot); |
657d3bfa | 1728 | flush_warnings(inode->i_dquot, warntype); |
1da177e4 | 1729 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
1da177e4 | 1730 | } |
08d0350c | 1731 | EXPORT_SYMBOL(dquot_free_inode); |
1da177e4 LT |
1732 | |
1733 | /* | |
1734 | * Transfer the number of inode and blocks from one diskquota to an other. | |
bc8e5f07 JK |
1735 | * On success, dquot references in transfer_to are consumed and references |
1736 | * to original dquots that need to be released are placed there. On failure, | |
1737 | * references are kept untouched. | |
1da177e4 LT |
1738 | * |
1739 | * This operation can block, but only after everything is updated | |
1740 | * A transaction must be started when entering this function. | |
bc8e5f07 | 1741 | * |
1da177e4 | 1742 | */ |
bc8e5f07 | 1743 | int __dquot_transfer(struct inode *inode, struct dquot **transfer_to) |
1da177e4 | 1744 | { |
740d9dcd MC |
1745 | qsize_t space, cur_space; |
1746 | qsize_t rsv_space = 0; | |
bc8e5f07 | 1747 | struct dquot *transfer_from[MAXQUOTAS] = {}; |
efd8f0e6 | 1748 | int cnt, ret = 0; |
9e32784b | 1749 | char is_valid[MAXQUOTAS] = {}; |
657d3bfa JK |
1750 | char warntype_to[MAXQUOTAS]; |
1751 | char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS]; | |
1da177e4 | 1752 | |
d3be915f IM |
1753 | /* First test before acquiring mutex - solves deadlocks when we |
1754 | * re-enter the quota code and are already holding the mutex */ | |
1da177e4 | 1755 | if (IS_NOQUOTA(inode)) |
efd8f0e6 | 1756 | return 0; |
cc33412f | 1757 | /* Initialize the arrays */ |
bc8e5f07 | 1758 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
657d3bfa | 1759 | warntype_to[cnt] = QUOTA_NL_NOWARN; |
cc33412f | 1760 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
cc33412f JK |
1761 | if (IS_NOQUOTA(inode)) { /* File without quota accounting? */ |
1762 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | |
bc8e5f07 | 1763 | return 0; |
cc33412f | 1764 | } |
1da177e4 | 1765 | spin_lock(&dq_data_lock); |
740d9dcd | 1766 | cur_space = inode_get_bytes(inode); |
fd8fbfc1 | 1767 | rsv_space = inode_get_rsv_space(inode); |
740d9dcd | 1768 | space = cur_space + rsv_space; |
1da177e4 LT |
1769 | /* Build the transfer_from list and check the limits */ |
1770 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
9e32784b D |
1771 | /* |
1772 | * Skip changes for same uid or gid or for turned off quota-type. | |
1773 | */ | |
dd6f3c6d | 1774 | if (!transfer_to[cnt]) |
1da177e4 | 1775 | continue; |
9e32784b D |
1776 | /* Avoid races with quotaoff() */ |
1777 | if (!sb_has_quota_active(inode->i_sb, cnt)) | |
1778 | continue; | |
1779 | is_valid[cnt] = 1; | |
1da177e4 | 1780 | transfer_from[cnt] = inode->i_dquot[cnt]; |
efd8f0e6 CH |
1781 | ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt); |
1782 | if (ret) | |
1783 | goto over_quota; | |
1784 | ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt); | |
1785 | if (ret) | |
cc33412f | 1786 | goto over_quota; |
1da177e4 LT |
1787 | } |
1788 | ||
1789 | /* | |
1790 | * Finally perform the needed transfer from transfer_from to transfer_to | |
1791 | */ | |
1792 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | |
9e32784b | 1793 | if (!is_valid[cnt]) |
1da177e4 | 1794 | continue; |
1da177e4 LT |
1795 | /* Due to IO error we might not have transfer_from[] structure */ |
1796 | if (transfer_from[cnt]) { | |
657d3bfa JK |
1797 | warntype_from_inodes[cnt] = |
1798 | info_idq_free(transfer_from[cnt], 1); | |
1799 | warntype_from_space[cnt] = | |
1800 | info_bdq_free(transfer_from[cnt], space); | |
1da177e4 | 1801 | dquot_decr_inodes(transfer_from[cnt], 1); |
740d9dcd MC |
1802 | dquot_decr_space(transfer_from[cnt], cur_space); |
1803 | dquot_free_reserved_space(transfer_from[cnt], | |
1804 | rsv_space); | |
1da177e4 LT |
1805 | } |
1806 | ||
1807 | dquot_incr_inodes(transfer_to[cnt], 1); | |
740d9dcd MC |
1808 | dquot_incr_space(transfer_to[cnt], cur_space); |
1809 | dquot_resv_space(transfer_to[cnt], rsv_space); | |
1da177e4 LT |
1810 | |
1811 | inode->i_dquot[cnt] = transfer_to[cnt]; | |
1812 | } | |
1da177e4 | 1813 | spin_unlock(&dq_data_lock); |
cc33412f JK |
1814 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
1815 | ||
dc52dd3a DM |
1816 | mark_all_dquot_dirty(transfer_from); |
1817 | mark_all_dquot_dirty(transfer_to); | |
86f3cbec JK |
1818 | flush_warnings(transfer_to, warntype_to); |
1819 | flush_warnings(transfer_from, warntype_from_inodes); | |
1820 | flush_warnings(transfer_from, warntype_from_space); | |
bc8e5f07 | 1821 | /* Pass back references to put */ |
dc52dd3a | 1822 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
9e32784b D |
1823 | if (is_valid[cnt]) |
1824 | transfer_to[cnt] = transfer_from[cnt]; | |
86f3cbec | 1825 | return 0; |
cc33412f JK |
1826 | over_quota: |
1827 | spin_unlock(&dq_data_lock); | |
1828 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | |
86f3cbec JK |
1829 | flush_warnings(transfer_to, warntype_to); |
1830 | return ret; | |
1da177e4 | 1831 | } |
bc8e5f07 | 1832 | EXPORT_SYMBOL(__dquot_transfer); |
1da177e4 | 1833 | |
8ddd69d6 DM |
1834 | /* Wrapper for transferring ownership of an inode for uid/gid only |
1835 | * Called from FSXXX_setattr() | |
1836 | */ | |
b43fa828 | 1837 | int dquot_transfer(struct inode *inode, struct iattr *iattr) |
b85f4b87 | 1838 | { |
bc8e5f07 JK |
1839 | struct dquot *transfer_to[MAXQUOTAS] = {}; |
1840 | struct super_block *sb = inode->i_sb; | |
1841 | int ret; | |
8ddd69d6 | 1842 | |
189eef59 | 1843 | if (!dquot_active(inode)) |
bc8e5f07 | 1844 | return 0; |
12755627 | 1845 | |
bc8e5f07 JK |
1846 | if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) |
1847 | transfer_to[USRQUOTA] = dqget(sb, iattr->ia_uid, USRQUOTA); | |
1848 | if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) | |
4dea4969 | 1849 | transfer_to[GRPQUOTA] = dqget(sb, iattr->ia_gid, GRPQUOTA); |
bc8e5f07 JK |
1850 | |
1851 | ret = __dquot_transfer(inode, transfer_to); | |
1852 | dqput_all(transfer_to); | |
1853 | return ret; | |
b85f4b87 | 1854 | } |
b43fa828 | 1855 | EXPORT_SYMBOL(dquot_transfer); |
b85f4b87 | 1856 | |
1da177e4 LT |
1857 | /* |
1858 | * Write info of quota file to disk | |
1859 | */ | |
1860 | int dquot_commit_info(struct super_block *sb, int type) | |
1861 | { | |
1862 | int ret; | |
1863 | struct quota_info *dqopt = sb_dqopt(sb); | |
1864 | ||
d3be915f | 1865 | mutex_lock(&dqopt->dqio_mutex); |
1da177e4 | 1866 | ret = dqopt->ops[type]->write_file_info(sb, type); |
d3be915f | 1867 | mutex_unlock(&dqopt->dqio_mutex); |
1da177e4 LT |
1868 | return ret; |
1869 | } | |
08d0350c | 1870 | EXPORT_SYMBOL(dquot_commit_info); |
1da177e4 LT |
1871 | |
1872 | /* | |
1873 | * Definitions of diskquota operations. | |
1874 | */ | |
61e225dc | 1875 | const struct dquot_operations dquot_operations = { |
1da177e4 LT |
1876 | .write_dquot = dquot_commit, |
1877 | .acquire_dquot = dquot_acquire, | |
1878 | .release_dquot = dquot_release, | |
1879 | .mark_dirty = dquot_mark_dquot_dirty, | |
74f783af JK |
1880 | .write_info = dquot_commit_info, |
1881 | .alloc_dquot = dquot_alloc, | |
1882 | .destroy_dquot = dquot_destroy, | |
1da177e4 | 1883 | }; |
123e9caf | 1884 | EXPORT_SYMBOL(dquot_operations); |
1da177e4 | 1885 | |
907f4554 CH |
1886 | /* |
1887 | * Generic helper for ->open on filesystems supporting disk quotas. | |
1888 | */ | |
1889 | int dquot_file_open(struct inode *inode, struct file *file) | |
1890 | { | |
1891 | int error; | |
1892 | ||
1893 | error = generic_file_open(inode, file); | |
1894 | if (!error && (file->f_mode & FMODE_WRITE)) | |
871a2931 | 1895 | dquot_initialize(inode); |
907f4554 CH |
1896 | return error; |
1897 | } | |
1898 | EXPORT_SYMBOL(dquot_file_open); | |
1899 | ||
1da177e4 LT |
1900 | /* |
1901 | * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount) | |
1902 | */ | |
0f0dd62f | 1903 | int dquot_disable(struct super_block *sb, int type, unsigned int flags) |
1da177e4 | 1904 | { |
0ff5af83 | 1905 | int cnt, ret = 0; |
1da177e4 LT |
1906 | struct quota_info *dqopt = sb_dqopt(sb); |
1907 | struct inode *toputinode[MAXQUOTAS]; | |
1da177e4 | 1908 | |
f55abc0f JK |
1909 | /* Cannot turn off usage accounting without turning off limits, or |
1910 | * suspend quotas and simultaneously turn quotas off. */ | |
1911 | if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED)) | |
1912 | || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED | | |
1913 | DQUOT_USAGE_ENABLED))) | |
1914 | return -EINVAL; | |
1915 | ||
1da177e4 | 1916 | /* We need to serialize quota_off() for device */ |
d3be915f | 1917 | mutex_lock(&dqopt->dqonoff_mutex); |
9377abd0 JK |
1918 | |
1919 | /* | |
1920 | * Skip everything if there's nothing to do. We have to do this because | |
1921 | * sometimes we are called when fill_super() failed and calling | |
1922 | * sync_fs() in such cases does no good. | |
1923 | */ | |
f55abc0f | 1924 | if (!sb_any_quota_loaded(sb)) { |
9377abd0 JK |
1925 | mutex_unlock(&dqopt->dqonoff_mutex); |
1926 | return 0; | |
1927 | } | |
1da177e4 LT |
1928 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
1929 | toputinode[cnt] = NULL; | |
1da177e4 LT |
1930 | if (type != -1 && cnt != type) |
1931 | continue; | |
f55abc0f | 1932 | if (!sb_has_quota_loaded(sb, cnt)) |
0ff5af83 | 1933 | continue; |
f55abc0f JK |
1934 | |
1935 | if (flags & DQUOT_SUSPENDED) { | |
cc33412f | 1936 | spin_lock(&dq_state_lock); |
f55abc0f JK |
1937 | dqopt->flags |= |
1938 | dquot_state_flag(DQUOT_SUSPENDED, cnt); | |
cc33412f | 1939 | spin_unlock(&dq_state_lock); |
f55abc0f | 1940 | } else { |
cc33412f | 1941 | spin_lock(&dq_state_lock); |
f55abc0f JK |
1942 | dqopt->flags &= ~dquot_state_flag(flags, cnt); |
1943 | /* Turning off suspended quotas? */ | |
1944 | if (!sb_has_quota_loaded(sb, cnt) && | |
1945 | sb_has_quota_suspended(sb, cnt)) { | |
1946 | dqopt->flags &= ~dquot_state_flag( | |
1947 | DQUOT_SUSPENDED, cnt); | |
cc33412f | 1948 | spin_unlock(&dq_state_lock); |
f55abc0f JK |
1949 | iput(dqopt->files[cnt]); |
1950 | dqopt->files[cnt] = NULL; | |
1951 | continue; | |
1952 | } | |
cc33412f | 1953 | spin_unlock(&dq_state_lock); |
0ff5af83 | 1954 | } |
f55abc0f JK |
1955 | |
1956 | /* We still have to keep quota loaded? */ | |
1957 | if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED)) | |
1da177e4 | 1958 | continue; |
1da177e4 LT |
1959 | |
1960 | /* Note: these are blocking operations */ | |
1961 | drop_dquot_ref(sb, cnt); | |
1962 | invalidate_dquots(sb, cnt); | |
1963 | /* | |
268157ba JK |
1964 | * Now all dquots should be invalidated, all writes done so we |
1965 | * should be only users of the info. No locks needed. | |
1da177e4 LT |
1966 | */ |
1967 | if (info_dirty(&dqopt->info[cnt])) | |
1968 | sb->dq_op->write_info(sb, cnt); | |
1969 | if (dqopt->ops[cnt]->free_file_info) | |
1970 | dqopt->ops[cnt]->free_file_info(sb, cnt); | |
1971 | put_quota_format(dqopt->info[cnt].dqi_format); | |
1972 | ||
1973 | toputinode[cnt] = dqopt->files[cnt]; | |
f55abc0f | 1974 | if (!sb_has_quota_loaded(sb, cnt)) |
0ff5af83 | 1975 | dqopt->files[cnt] = NULL; |
1da177e4 LT |
1976 | dqopt->info[cnt].dqi_flags = 0; |
1977 | dqopt->info[cnt].dqi_igrace = 0; | |
1978 | dqopt->info[cnt].dqi_bgrace = 0; | |
1979 | dqopt->ops[cnt] = NULL; | |
1980 | } | |
d3be915f | 1981 | mutex_unlock(&dqopt->dqonoff_mutex); |
ca785ec6 JK |
1982 | |
1983 | /* Skip syncing and setting flags if quota files are hidden */ | |
1984 | if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) | |
1985 | goto put_inodes; | |
1986 | ||
1da177e4 | 1987 | /* Sync the superblock so that buffers with quota data are written to |
7b7b1ace | 1988 | * disk (and so userspace sees correct data afterwards). */ |
1da177e4 LT |
1989 | if (sb->s_op->sync_fs) |
1990 | sb->s_op->sync_fs(sb, 1); | |
1991 | sync_blockdev(sb->s_bdev); | |
1992 | /* Now the quota files are just ordinary files and we can set the | |
1993 | * inode flags back. Moreover we discard the pagecache so that | |
1994 | * userspace sees the writes we did bypassing the pagecache. We | |
1995 | * must also discard the blockdev buffers so that we see the | |
1996 | * changes done by userspace on the next quotaon() */ | |
1997 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | |
1998 | if (toputinode[cnt]) { | |
d3be915f | 1999 | mutex_lock(&dqopt->dqonoff_mutex); |
1da177e4 LT |
2000 | /* If quota was reenabled in the meantime, we have |
2001 | * nothing to do */ | |
f55abc0f | 2002 | if (!sb_has_quota_loaded(sb, cnt)) { |
268157ba JK |
2003 | mutex_lock_nested(&toputinode[cnt]->i_mutex, |
2004 | I_MUTEX_QUOTA); | |
1da177e4 LT |
2005 | toputinode[cnt]->i_flags &= ~(S_IMMUTABLE | |
2006 | S_NOATIME | S_NOQUOTA); | |
268157ba JK |
2007 | truncate_inode_pages(&toputinode[cnt]->i_data, |
2008 | 0); | |
1b1dcc1b | 2009 | mutex_unlock(&toputinode[cnt]->i_mutex); |
43d2932d | 2010 | mark_inode_dirty_sync(toputinode[cnt]); |
1da177e4 | 2011 | } |
d3be915f | 2012 | mutex_unlock(&dqopt->dqonoff_mutex); |
ca785ec6 JK |
2013 | } |
2014 | if (sb->s_bdev) | |
2015 | invalidate_bdev(sb->s_bdev); | |
2016 | put_inodes: | |
2017 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | |
2018 | if (toputinode[cnt]) { | |
0ff5af83 | 2019 | /* On remount RO, we keep the inode pointer so that we |
f55abc0f JK |
2020 | * can reenable quota on the subsequent remount RW. We |
2021 | * have to check 'flags' variable and not use sb_has_ | |
2022 | * function because another quotaon / quotaoff could | |
2023 | * change global state before we got here. We refuse | |
2024 | * to suspend quotas when there is pending delete on | |
2025 | * the quota file... */ | |
2026 | if (!(flags & DQUOT_SUSPENDED)) | |
0ff5af83 JK |
2027 | iput(toputinode[cnt]); |
2028 | else if (!toputinode[cnt]->i_nlink) | |
2029 | ret = -EBUSY; | |
1da177e4 | 2030 | } |
0ff5af83 | 2031 | return ret; |
1da177e4 | 2032 | } |
0f0dd62f | 2033 | EXPORT_SYMBOL(dquot_disable); |
1da177e4 | 2034 | |
287a8095 | 2035 | int dquot_quota_off(struct super_block *sb, int type) |
f55abc0f | 2036 | { |
0f0dd62f CH |
2037 | return dquot_disable(sb, type, |
2038 | DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED); | |
f55abc0f | 2039 | } |
287a8095 | 2040 | EXPORT_SYMBOL(dquot_quota_off); |
0f0dd62f | 2041 | |
1da177e4 LT |
2042 | /* |
2043 | * Turn quotas on on a device | |
2044 | */ | |
2045 | ||
f55abc0f JK |
2046 | /* |
2047 | * Helper function to turn quotas on when we already have the inode of | |
2048 | * quota file and no quota information is loaded. | |
2049 | */ | |
2050 | static int vfs_load_quota_inode(struct inode *inode, int type, int format_id, | |
2051 | unsigned int flags) | |
1da177e4 LT |
2052 | { |
2053 | struct quota_format_type *fmt = find_quota_format(format_id); | |
2054 | struct super_block *sb = inode->i_sb; | |
2055 | struct quota_info *dqopt = sb_dqopt(sb); | |
2056 | int error; | |
2057 | int oldflags = -1; | |
2058 | ||
2059 | if (!fmt) | |
2060 | return -ESRCH; | |
2061 | if (!S_ISREG(inode->i_mode)) { | |
2062 | error = -EACCES; | |
2063 | goto out_fmt; | |
2064 | } | |
2065 | if (IS_RDONLY(inode)) { | |
2066 | error = -EROFS; | |
2067 | goto out_fmt; | |
2068 | } | |
2069 | if (!sb->s_op->quota_write || !sb->s_op->quota_read) { | |
2070 | error = -EINVAL; | |
2071 | goto out_fmt; | |
2072 | } | |
f55abc0f JK |
2073 | /* Usage always has to be set... */ |
2074 | if (!(flags & DQUOT_USAGE_ENABLED)) { | |
2075 | error = -EINVAL; | |
2076 | goto out_fmt; | |
2077 | } | |
1da177e4 | 2078 | |
ca785ec6 | 2079 | if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) { |
ab94c39b JK |
2080 | /* As we bypass the pagecache we must now flush all the |
2081 | * dirty data and invalidate caches so that kernel sees | |
2082 | * changes from userspace. It is not enough to just flush | |
2083 | * the quota file since if blocksize < pagesize, invalidation | |
2084 | * of the cache could fail because of other unrelated dirty | |
2085 | * data */ | |
2086 | sync_filesystem(sb); | |
ca785ec6 JK |
2087 | invalidate_bdev(sb->s_bdev); |
2088 | } | |
d3be915f | 2089 | mutex_lock(&dqopt->dqonoff_mutex); |
f55abc0f | 2090 | if (sb_has_quota_loaded(sb, type)) { |
1da177e4 LT |
2091 | error = -EBUSY; |
2092 | goto out_lock; | |
2093 | } | |
ca785ec6 JK |
2094 | |
2095 | if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) { | |
2096 | /* We don't want quota and atime on quota files (deadlocks | |
2097 | * possible) Also nobody should write to the file - we use | |
2098 | * special IO operations which ignore the immutable bit. */ | |
dee86565 | 2099 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA); |
268157ba JK |
2100 | oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | |
2101 | S_NOQUOTA); | |
ca785ec6 | 2102 | inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE; |
dee86565 | 2103 | mutex_unlock(&inode->i_mutex); |
26245c94 JK |
2104 | /* |
2105 | * When S_NOQUOTA is set, remove dquot references as no more | |
2106 | * references can be added | |
2107 | */ | |
9f754758 | 2108 | __dquot_drop(inode); |
ca785ec6 | 2109 | } |
1da177e4 LT |
2110 | |
2111 | error = -EIO; | |
2112 | dqopt->files[type] = igrab(inode); | |
2113 | if (!dqopt->files[type]) | |
2114 | goto out_lock; | |
2115 | error = -EINVAL; | |
2116 | if (!fmt->qf_ops->check_quota_file(sb, type)) | |
2117 | goto out_file_init; | |
2118 | ||
2119 | dqopt->ops[type] = fmt->qf_ops; | |
2120 | dqopt->info[type].dqi_format = fmt; | |
0ff5af83 | 2121 | dqopt->info[type].dqi_fmt_id = format_id; |
1da177e4 | 2122 | INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list); |
d3be915f | 2123 | mutex_lock(&dqopt->dqio_mutex); |
268157ba JK |
2124 | error = dqopt->ops[type]->read_file_info(sb, type); |
2125 | if (error < 0) { | |
d3be915f | 2126 | mutex_unlock(&dqopt->dqio_mutex); |
1da177e4 LT |
2127 | goto out_file_init; |
2128 | } | |
d3be915f | 2129 | mutex_unlock(&dqopt->dqio_mutex); |
cc33412f | 2130 | spin_lock(&dq_state_lock); |
f55abc0f | 2131 | dqopt->flags |= dquot_state_flag(flags, type); |
cc33412f | 2132 | spin_unlock(&dq_state_lock); |
1da177e4 LT |
2133 | |
2134 | add_dquot_ref(sb, type); | |
d3be915f | 2135 | mutex_unlock(&dqopt->dqonoff_mutex); |
1da177e4 LT |
2136 | |
2137 | return 0; | |
2138 | ||
2139 | out_file_init: | |
2140 | dqopt->files[type] = NULL; | |
2141 | iput(inode); | |
2142 | out_lock: | |
1da177e4 | 2143 | if (oldflags != -1) { |
dee86565 | 2144 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA); |
1da177e4 LT |
2145 | /* Set the flags back (in the case of accidental quotaon() |
2146 | * on a wrong file we don't want to mess up the flags) */ | |
2147 | inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE); | |
2148 | inode->i_flags |= oldflags; | |
dee86565 | 2149 | mutex_unlock(&inode->i_mutex); |
1da177e4 | 2150 | } |
d01730d7 | 2151 | mutex_unlock(&dqopt->dqonoff_mutex); |
1da177e4 LT |
2152 | out_fmt: |
2153 | put_quota_format(fmt); | |
2154 | ||
2155 | return error; | |
2156 | } | |
2157 | ||
0ff5af83 | 2158 | /* Reenable quotas on remount RW */ |
0f0dd62f | 2159 | int dquot_resume(struct super_block *sb, int type) |
0ff5af83 JK |
2160 | { |
2161 | struct quota_info *dqopt = sb_dqopt(sb); | |
2162 | struct inode *inode; | |
0f0dd62f | 2163 | int ret = 0, cnt; |
f55abc0f | 2164 | unsigned int flags; |
0ff5af83 | 2165 | |
0f0dd62f CH |
2166 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
2167 | if (type != -1 && cnt != type) | |
2168 | continue; | |
2169 | ||
2170 | mutex_lock(&dqopt->dqonoff_mutex); | |
2171 | if (!sb_has_quota_suspended(sb, cnt)) { | |
2172 | mutex_unlock(&dqopt->dqonoff_mutex); | |
2173 | continue; | |
2174 | } | |
2175 | inode = dqopt->files[cnt]; | |
2176 | dqopt->files[cnt] = NULL; | |
2177 | spin_lock(&dq_state_lock); | |
2178 | flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED | | |
2179 | DQUOT_LIMITS_ENABLED, | |
2180 | cnt); | |
2181 | dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt); | |
2182 | spin_unlock(&dq_state_lock); | |
0ff5af83 | 2183 | mutex_unlock(&dqopt->dqonoff_mutex); |
0ff5af83 | 2184 | |
0f0dd62f CH |
2185 | flags = dquot_generic_flag(flags, cnt); |
2186 | ret = vfs_load_quota_inode(inode, cnt, | |
2187 | dqopt->info[cnt].dqi_fmt_id, flags); | |
2188 | iput(inode); | |
2189 | } | |
0ff5af83 JK |
2190 | |
2191 | return ret; | |
2192 | } | |
0f0dd62f | 2193 | EXPORT_SYMBOL(dquot_resume); |
0ff5af83 | 2194 | |
f00c9e44 JK |
2195 | int dquot_quota_on(struct super_block *sb, int type, int format_id, |
2196 | struct path *path) | |
77e69dac AV |
2197 | { |
2198 | int error = security_quota_on(path->dentry); | |
2199 | if (error) | |
2200 | return error; | |
2201 | /* Quota file not on the same filesystem? */ | |
2202 | if (path->mnt->mnt_sb != sb) | |
2203 | error = -EXDEV; | |
2204 | else | |
f55abc0f JK |
2205 | error = vfs_load_quota_inode(path->dentry->d_inode, type, |
2206 | format_id, DQUOT_USAGE_ENABLED | | |
2207 | DQUOT_LIMITS_ENABLED); | |
77e69dac AV |
2208 | return error; |
2209 | } | |
287a8095 | 2210 | EXPORT_SYMBOL(dquot_quota_on); |
1da177e4 | 2211 | |
f55abc0f JK |
2212 | /* |
2213 | * More powerful function for turning on quotas allowing setting | |
2214 | * of individual quota flags | |
2215 | */ | |
287a8095 CH |
2216 | int dquot_enable(struct inode *inode, int type, int format_id, |
2217 | unsigned int flags) | |
f55abc0f JK |
2218 | { |
2219 | int ret = 0; | |
2220 | struct super_block *sb = inode->i_sb; | |
2221 | struct quota_info *dqopt = sb_dqopt(sb); | |
2222 | ||
2223 | /* Just unsuspend quotas? */ | |
0f0dd62f CH |
2224 | BUG_ON(flags & DQUOT_SUSPENDED); |
2225 | ||
f55abc0f JK |
2226 | if (!flags) |
2227 | return 0; | |
2228 | /* Just updating flags needed? */ | |
2229 | if (sb_has_quota_loaded(sb, type)) { | |
2230 | mutex_lock(&dqopt->dqonoff_mutex); | |
2231 | /* Now do a reliable test... */ | |
2232 | if (!sb_has_quota_loaded(sb, type)) { | |
2233 | mutex_unlock(&dqopt->dqonoff_mutex); | |
2234 | goto load_quota; | |
2235 | } | |
2236 | if (flags & DQUOT_USAGE_ENABLED && | |
2237 | sb_has_quota_usage_enabled(sb, type)) { | |
2238 | ret = -EBUSY; | |
2239 | goto out_lock; | |
2240 | } | |
2241 | if (flags & DQUOT_LIMITS_ENABLED && | |
2242 | sb_has_quota_limits_enabled(sb, type)) { | |
2243 | ret = -EBUSY; | |
2244 | goto out_lock; | |
2245 | } | |
cc33412f | 2246 | spin_lock(&dq_state_lock); |
f55abc0f | 2247 | sb_dqopt(sb)->flags |= dquot_state_flag(flags, type); |
cc33412f | 2248 | spin_unlock(&dq_state_lock); |
f55abc0f JK |
2249 | out_lock: |
2250 | mutex_unlock(&dqopt->dqonoff_mutex); | |
2251 | return ret; | |
2252 | } | |
2253 | ||
2254 | load_quota: | |
2255 | return vfs_load_quota_inode(inode, type, format_id, flags); | |
2256 | } | |
287a8095 | 2257 | EXPORT_SYMBOL(dquot_enable); |
f55abc0f | 2258 | |
1da177e4 LT |
2259 | /* |
2260 | * This function is used when filesystem needs to initialize quotas | |
2261 | * during mount time. | |
2262 | */ | |
287a8095 | 2263 | int dquot_quota_on_mount(struct super_block *sb, char *qf_name, |
84de856e | 2264 | int format_id, int type) |
1da177e4 | 2265 | { |
84de856e | 2266 | struct dentry *dentry; |
1da177e4 LT |
2267 | int error; |
2268 | ||
c56818d7 | 2269 | mutex_lock(&sb->s_root->d_inode->i_mutex); |
2fa389c5 | 2270 | dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name)); |
c56818d7 | 2271 | mutex_unlock(&sb->s_root->d_inode->i_mutex); |
84de856e CH |
2272 | if (IS_ERR(dentry)) |
2273 | return PTR_ERR(dentry); | |
2274 | ||
154f484b JK |
2275 | if (!dentry->d_inode) { |
2276 | error = -ENOENT; | |
2277 | goto out; | |
2278 | } | |
2279 | ||
1da177e4 | 2280 | error = security_quota_on(dentry); |
84de856e | 2281 | if (!error) |
f55abc0f JK |
2282 | error = vfs_load_quota_inode(dentry->d_inode, type, format_id, |
2283 | DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED); | |
84de856e | 2284 | |
154f484b | 2285 | out: |
84de856e CH |
2286 | dput(dentry); |
2287 | return error; | |
1da177e4 | 2288 | } |
287a8095 | 2289 | EXPORT_SYMBOL(dquot_quota_on_mount); |
b85f4b87 | 2290 | |
12095460 JK |
2291 | static inline qsize_t qbtos(qsize_t blocks) |
2292 | { | |
2293 | return blocks << QIF_DQBLKSIZE_BITS; | |
2294 | } | |
2295 | ||
2296 | static inline qsize_t stoqb(qsize_t space) | |
2297 | { | |
2298 | return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS; | |
2299 | } | |
2300 | ||
1da177e4 | 2301 | /* Generic routine for getting common part of quota structure */ |
b9b2dd36 | 2302 | static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di) |
1da177e4 LT |
2303 | { |
2304 | struct mem_dqblk *dm = &dquot->dq_dqb; | |
2305 | ||
b9b2dd36 CH |
2306 | memset(di, 0, sizeof(*di)); |
2307 | di->d_version = FS_DQUOT_VERSION; | |
2308 | di->d_flags = dquot->dq_type == USRQUOTA ? | |
ade7ce31 | 2309 | FS_USER_QUOTA : FS_GROUP_QUOTA; |
b9b2dd36 CH |
2310 | di->d_id = dquot->dq_id; |
2311 | ||
1da177e4 | 2312 | spin_lock(&dq_data_lock); |
b9b2dd36 CH |
2313 | di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit); |
2314 | di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit); | |
2315 | di->d_ino_hardlimit = dm->dqb_ihardlimit; | |
2316 | di->d_ino_softlimit = dm->dqb_isoftlimit; | |
2317 | di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace; | |
2318 | di->d_icount = dm->dqb_curinodes; | |
2319 | di->d_btimer = dm->dqb_btime; | |
2320 | di->d_itimer = dm->dqb_itime; | |
1da177e4 LT |
2321 | spin_unlock(&dq_data_lock); |
2322 | } | |
2323 | ||
287a8095 CH |
2324 | int dquot_get_dqblk(struct super_block *sb, int type, qid_t id, |
2325 | struct fs_disk_quota *di) | |
1da177e4 LT |
2326 | { |
2327 | struct dquot *dquot; | |
2328 | ||
cc33412f | 2329 | dquot = dqget(sb, id, type); |
dd6f3c6d | 2330 | if (!dquot) |
1da177e4 | 2331 | return -ESRCH; |
1da177e4 LT |
2332 | do_get_dqblk(dquot, di); |
2333 | dqput(dquot); | |
cc33412f | 2334 | |
1da177e4 LT |
2335 | return 0; |
2336 | } | |
287a8095 | 2337 | EXPORT_SYMBOL(dquot_get_dqblk); |
1da177e4 | 2338 | |
c472b432 CH |
2339 | #define VFS_FS_DQ_MASK \ |
2340 | (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \ | |
2341 | FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \ | |
2342 | FS_DQ_BTIMER | FS_DQ_ITIMER) | |
2343 | ||
1da177e4 | 2344 | /* Generic routine for setting common part of quota structure */ |
c472b432 | 2345 | static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di) |
1da177e4 LT |
2346 | { |
2347 | struct mem_dqblk *dm = &dquot->dq_dqb; | |
2348 | int check_blim = 0, check_ilim = 0; | |
338bf9af AP |
2349 | struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type]; |
2350 | ||
c472b432 CH |
2351 | if (di->d_fieldmask & ~VFS_FS_DQ_MASK) |
2352 | return -EINVAL; | |
2353 | ||
2354 | if (((di->d_fieldmask & FS_DQ_BSOFT) && | |
2355 | (di->d_blk_softlimit > dqi->dqi_maxblimit)) || | |
2356 | ((di->d_fieldmask & FS_DQ_BHARD) && | |
2357 | (di->d_blk_hardlimit > dqi->dqi_maxblimit)) || | |
2358 | ((di->d_fieldmask & FS_DQ_ISOFT) && | |
2359 | (di->d_ino_softlimit > dqi->dqi_maxilimit)) || | |
2360 | ((di->d_fieldmask & FS_DQ_IHARD) && | |
2361 | (di->d_ino_hardlimit > dqi->dqi_maxilimit))) | |
338bf9af | 2362 | return -ERANGE; |
1da177e4 LT |
2363 | |
2364 | spin_lock(&dq_data_lock); | |
c472b432 CH |
2365 | if (di->d_fieldmask & FS_DQ_BCOUNT) { |
2366 | dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace; | |
1da177e4 | 2367 | check_blim = 1; |
08261673 | 2368 | set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags); |
1da177e4 | 2369 | } |
c472b432 CH |
2370 | |
2371 | if (di->d_fieldmask & FS_DQ_BSOFT) | |
2372 | dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit); | |
2373 | if (di->d_fieldmask & FS_DQ_BHARD) | |
2374 | dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit); | |
2375 | if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) { | |
1da177e4 | 2376 | check_blim = 1; |
08261673 | 2377 | set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags); |
1da177e4 | 2378 | } |
c472b432 CH |
2379 | |
2380 | if (di->d_fieldmask & FS_DQ_ICOUNT) { | |
2381 | dm->dqb_curinodes = di->d_icount; | |
1da177e4 | 2382 | check_ilim = 1; |
08261673 | 2383 | set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags); |
1da177e4 | 2384 | } |
c472b432 CH |
2385 | |
2386 | if (di->d_fieldmask & FS_DQ_ISOFT) | |
2387 | dm->dqb_isoftlimit = di->d_ino_softlimit; | |
2388 | if (di->d_fieldmask & FS_DQ_IHARD) | |
2389 | dm->dqb_ihardlimit = di->d_ino_hardlimit; | |
2390 | if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) { | |
1da177e4 | 2391 | check_ilim = 1; |
08261673 | 2392 | set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags); |
1da177e4 | 2393 | } |
c472b432 CH |
2394 | |
2395 | if (di->d_fieldmask & FS_DQ_BTIMER) { | |
2396 | dm->dqb_btime = di->d_btimer; | |
e04a88a9 | 2397 | check_blim = 1; |
08261673 | 2398 | set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags); |
4d59bce4 | 2399 | } |
c472b432 CH |
2400 | |
2401 | if (di->d_fieldmask & FS_DQ_ITIMER) { | |
2402 | dm->dqb_itime = di->d_itimer; | |
e04a88a9 | 2403 | check_ilim = 1; |
08261673 | 2404 | set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags); |
4d59bce4 | 2405 | } |
1da177e4 LT |
2406 | |
2407 | if (check_blim) { | |
268157ba JK |
2408 | if (!dm->dqb_bsoftlimit || |
2409 | dm->dqb_curspace < dm->dqb_bsoftlimit) { | |
1da177e4 LT |
2410 | dm->dqb_btime = 0; |
2411 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); | |
c472b432 | 2412 | } else if (!(di->d_fieldmask & FS_DQ_BTIMER)) |
268157ba | 2413 | /* Set grace only if user hasn't provided his own... */ |
338bf9af | 2414 | dm->dqb_btime = get_seconds() + dqi->dqi_bgrace; |
1da177e4 LT |
2415 | } |
2416 | if (check_ilim) { | |
268157ba JK |
2417 | if (!dm->dqb_isoftlimit || |
2418 | dm->dqb_curinodes < dm->dqb_isoftlimit) { | |
1da177e4 LT |
2419 | dm->dqb_itime = 0; |
2420 | clear_bit(DQ_INODES_B, &dquot->dq_flags); | |
c472b432 | 2421 | } else if (!(di->d_fieldmask & FS_DQ_ITIMER)) |
268157ba | 2422 | /* Set grace only if user hasn't provided his own... */ |
338bf9af | 2423 | dm->dqb_itime = get_seconds() + dqi->dqi_igrace; |
1da177e4 | 2424 | } |
268157ba JK |
2425 | if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || |
2426 | dm->dqb_isoftlimit) | |
1da177e4 LT |
2427 | clear_bit(DQ_FAKE_B, &dquot->dq_flags); |
2428 | else | |
2429 | set_bit(DQ_FAKE_B, &dquot->dq_flags); | |
2430 | spin_unlock(&dq_data_lock); | |
2431 | mark_dquot_dirty(dquot); | |
338bf9af AP |
2432 | |
2433 | return 0; | |
1da177e4 LT |
2434 | } |
2435 | ||
287a8095 | 2436 | int dquot_set_dqblk(struct super_block *sb, int type, qid_t id, |
c472b432 | 2437 | struct fs_disk_quota *di) |
1da177e4 LT |
2438 | { |
2439 | struct dquot *dquot; | |
338bf9af | 2440 | int rc; |
1da177e4 | 2441 | |
f55abc0f JK |
2442 | dquot = dqget(sb, id, type); |
2443 | if (!dquot) { | |
2444 | rc = -ESRCH; | |
2445 | goto out; | |
1da177e4 | 2446 | } |
338bf9af | 2447 | rc = do_set_dqblk(dquot, di); |
1da177e4 | 2448 | dqput(dquot); |
f55abc0f | 2449 | out: |
338bf9af | 2450 | return rc; |
1da177e4 | 2451 | } |
287a8095 | 2452 | EXPORT_SYMBOL(dquot_set_dqblk); |
1da177e4 LT |
2453 | |
2454 | /* Generic routine for getting common part of quota file information */ | |
287a8095 | 2455 | int dquot_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) |
1da177e4 LT |
2456 | { |
2457 | struct mem_dqinfo *mi; | |
2458 | ||
d3be915f | 2459 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
f55abc0f | 2460 | if (!sb_has_quota_active(sb, type)) { |
d3be915f | 2461 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
1da177e4 LT |
2462 | return -ESRCH; |
2463 | } | |
2464 | mi = sb_dqopt(sb)->info + type; | |
2465 | spin_lock(&dq_data_lock); | |
2466 | ii->dqi_bgrace = mi->dqi_bgrace; | |
2467 | ii->dqi_igrace = mi->dqi_igrace; | |
2468 | ii->dqi_flags = mi->dqi_flags & DQF_MASK; | |
2469 | ii->dqi_valid = IIF_ALL; | |
2470 | spin_unlock(&dq_data_lock); | |
d3be915f | 2471 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
1da177e4 LT |
2472 | return 0; |
2473 | } | |
287a8095 | 2474 | EXPORT_SYMBOL(dquot_get_dqinfo); |
1da177e4 LT |
2475 | |
2476 | /* Generic routine for setting common part of quota file information */ | |
287a8095 | 2477 | int dquot_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) |
1da177e4 LT |
2478 | { |
2479 | struct mem_dqinfo *mi; | |
f55abc0f | 2480 | int err = 0; |
1da177e4 | 2481 | |
d3be915f | 2482 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
f55abc0f JK |
2483 | if (!sb_has_quota_active(sb, type)) { |
2484 | err = -ESRCH; | |
2485 | goto out; | |
1da177e4 LT |
2486 | } |
2487 | mi = sb_dqopt(sb)->info + type; | |
2488 | spin_lock(&dq_data_lock); | |
2489 | if (ii->dqi_valid & IIF_BGRACE) | |
2490 | mi->dqi_bgrace = ii->dqi_bgrace; | |
2491 | if (ii->dqi_valid & IIF_IGRACE) | |
2492 | mi->dqi_igrace = ii->dqi_igrace; | |
2493 | if (ii->dqi_valid & IIF_FLAGS) | |
268157ba JK |
2494 | mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | |
2495 | (ii->dqi_flags & DQF_MASK); | |
1da177e4 LT |
2496 | spin_unlock(&dq_data_lock); |
2497 | mark_info_dirty(sb, type); | |
2498 | /* Force write to disk */ | |
2499 | sb->dq_op->write_info(sb, type); | |
f55abc0f | 2500 | out: |
d3be915f | 2501 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
f55abc0f | 2502 | return err; |
1da177e4 | 2503 | } |
287a8095 | 2504 | EXPORT_SYMBOL(dquot_set_dqinfo); |
1da177e4 | 2505 | |
287a8095 CH |
2506 | const struct quotactl_ops dquot_quotactl_ops = { |
2507 | .quota_on = dquot_quota_on, | |
2508 | .quota_off = dquot_quota_off, | |
2509 | .quota_sync = dquot_quota_sync, | |
2510 | .get_info = dquot_get_dqinfo, | |
2511 | .set_info = dquot_set_dqinfo, | |
2512 | .get_dqblk = dquot_get_dqblk, | |
2513 | .set_dqblk = dquot_set_dqblk | |
1da177e4 | 2514 | }; |
287a8095 | 2515 | EXPORT_SYMBOL(dquot_quotactl_ops); |
dde95888 DM |
2516 | |
2517 | static int do_proc_dqstats(struct ctl_table *table, int write, | |
2518 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
2519 | { | |
dde95888 | 2520 | unsigned int type = (int *)table->data - dqstats.stat; |
f32764bd DM |
2521 | |
2522 | /* Update global table */ | |
2523 | dqstats.stat[type] = | |
2524 | percpu_counter_sum_positive(&dqstats.counter[type]); | |
dde95888 DM |
2525 | return proc_dointvec(table, write, buffer, lenp, ppos); |
2526 | } | |
2527 | ||
1da177e4 LT |
2528 | static ctl_table fs_dqstats_table[] = { |
2529 | { | |
1da177e4 | 2530 | .procname = "lookups", |
dde95888 | 2531 | .data = &dqstats.stat[DQST_LOOKUPS], |
1da177e4 LT |
2532 | .maxlen = sizeof(int), |
2533 | .mode = 0444, | |
dde95888 | 2534 | .proc_handler = do_proc_dqstats, |
1da177e4 LT |
2535 | }, |
2536 | { | |
1da177e4 | 2537 | .procname = "drops", |
dde95888 | 2538 | .data = &dqstats.stat[DQST_DROPS], |
1da177e4 LT |
2539 | .maxlen = sizeof(int), |
2540 | .mode = 0444, | |
dde95888 | 2541 | .proc_handler = do_proc_dqstats, |
1da177e4 LT |
2542 | }, |
2543 | { | |
1da177e4 | 2544 | .procname = "reads", |
dde95888 | 2545 | .data = &dqstats.stat[DQST_READS], |
1da177e4 LT |
2546 | .maxlen = sizeof(int), |
2547 | .mode = 0444, | |
dde95888 | 2548 | .proc_handler = do_proc_dqstats, |
1da177e4 LT |
2549 | }, |
2550 | { | |
1da177e4 | 2551 | .procname = "writes", |
dde95888 | 2552 | .data = &dqstats.stat[DQST_WRITES], |
1da177e4 LT |
2553 | .maxlen = sizeof(int), |
2554 | .mode = 0444, | |
dde95888 | 2555 | .proc_handler = do_proc_dqstats, |
1da177e4 LT |
2556 | }, |
2557 | { | |
1da177e4 | 2558 | .procname = "cache_hits", |
dde95888 | 2559 | .data = &dqstats.stat[DQST_CACHE_HITS], |
1da177e4 LT |
2560 | .maxlen = sizeof(int), |
2561 | .mode = 0444, | |
dde95888 | 2562 | .proc_handler = do_proc_dqstats, |
1da177e4 LT |
2563 | }, |
2564 | { | |
1da177e4 | 2565 | .procname = "allocated_dquots", |
dde95888 | 2566 | .data = &dqstats.stat[DQST_ALLOC_DQUOTS], |
1da177e4 LT |
2567 | .maxlen = sizeof(int), |
2568 | .mode = 0444, | |
dde95888 | 2569 | .proc_handler = do_proc_dqstats, |
1da177e4 LT |
2570 | }, |
2571 | { | |
1da177e4 | 2572 | .procname = "free_dquots", |
dde95888 | 2573 | .data = &dqstats.stat[DQST_FREE_DQUOTS], |
1da177e4 LT |
2574 | .maxlen = sizeof(int), |
2575 | .mode = 0444, | |
dde95888 | 2576 | .proc_handler = do_proc_dqstats, |
1da177e4 LT |
2577 | }, |
2578 | { | |
1da177e4 | 2579 | .procname = "syncs", |
dde95888 | 2580 | .data = &dqstats.stat[DQST_SYNCS], |
1da177e4 LT |
2581 | .maxlen = sizeof(int), |
2582 | .mode = 0444, | |
dde95888 | 2583 | .proc_handler = do_proc_dqstats, |
1da177e4 | 2584 | }, |
8e893469 | 2585 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
1da177e4 | 2586 | { |
1da177e4 LT |
2587 | .procname = "warnings", |
2588 | .data = &flag_print_warnings, | |
2589 | .maxlen = sizeof(int), | |
2590 | .mode = 0644, | |
6d456111 | 2591 | .proc_handler = proc_dointvec, |
1da177e4 | 2592 | }, |
8e893469 | 2593 | #endif |
ab09203e | 2594 | { }, |
1da177e4 LT |
2595 | }; |
2596 | ||
2597 | static ctl_table fs_table[] = { | |
2598 | { | |
1da177e4 LT |
2599 | .procname = "quota", |
2600 | .mode = 0555, | |
2601 | .child = fs_dqstats_table, | |
2602 | }, | |
ab09203e | 2603 | { }, |
1da177e4 LT |
2604 | }; |
2605 | ||
2606 | static ctl_table sys_table[] = { | |
2607 | { | |
1da177e4 LT |
2608 | .procname = "fs", |
2609 | .mode = 0555, | |
2610 | .child = fs_table, | |
2611 | }, | |
ab09203e | 2612 | { }, |
1da177e4 LT |
2613 | }; |
2614 | ||
2615 | static int __init dquot_init(void) | |
2616 | { | |
f32764bd | 2617 | int i, ret; |
1da177e4 LT |
2618 | unsigned long nr_hash, order; |
2619 | ||
2620 | printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__); | |
2621 | ||
0b4d4147 | 2622 | register_sysctl_table(sys_table); |
1da177e4 | 2623 | |
20c2df83 | 2624 | dquot_cachep = kmem_cache_create("dquot", |
1da177e4 | 2625 | sizeof(struct dquot), sizeof(unsigned long) * 4, |
fffb60f9 PJ |
2626 | (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
2627 | SLAB_MEM_SPREAD|SLAB_PANIC), | |
20c2df83 | 2628 | NULL); |
1da177e4 LT |
2629 | |
2630 | order = 0; | |
2631 | dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order); | |
2632 | if (!dquot_hash) | |
2633 | panic("Cannot create dquot hash table"); | |
2634 | ||
f32764bd DM |
2635 | for (i = 0; i < _DQST_DQSTAT_LAST; i++) { |
2636 | ret = percpu_counter_init(&dqstats.counter[i], 0); | |
2637 | if (ret) | |
2638 | panic("Cannot create dquot stat counters"); | |
2639 | } | |
dde95888 | 2640 | |
1da177e4 LT |
2641 | /* Find power-of-two hlist_heads which can fit into allocation */ |
2642 | nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head); | |
2643 | dq_hash_bits = 0; | |
2644 | do { | |
2645 | dq_hash_bits++; | |
2646 | } while (nr_hash >> dq_hash_bits); | |
2647 | dq_hash_bits--; | |
2648 | ||
2649 | nr_hash = 1UL << dq_hash_bits; | |
2650 | dq_hash_mask = nr_hash - 1; | |
2651 | for (i = 0; i < nr_hash; i++) | |
2652 | INIT_HLIST_HEAD(dquot_hash + i); | |
2653 | ||
2654 | printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n", | |
2655 | nr_hash, order, (PAGE_SIZE << order)); | |
2656 | ||
8e1f936b | 2657 | register_shrinker(&dqcache_shrinker); |
1da177e4 LT |
2658 | |
2659 | return 0; | |
2660 | } | |
2661 | module_init(dquot_init); |