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