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