diff options
Diffstat (limited to 'fs/dquot.c')
-rw-r--r-- | fs/dquot.c | 1850 |
1 files changed, 1850 insertions, 0 deletions
diff --git a/fs/dquot.c b/fs/dquot.c new file mode 100644 index 000000000000..11048e0b32c9 --- /dev/null +++ b/fs/dquot.c | |||
@@ -0,0 +1,1850 @@ | |||
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 | * | ||
12 | * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $ | ||
13 | * | ||
14 | * Author: Marco van Wieringen <mvw@planets.elm.net> | ||
15 | * | ||
16 | * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96 | ||
17 | * | ||
18 | * Revised list management to avoid races | ||
19 | * -- Bill Hawes, <whawes@star.net>, 9/98 | ||
20 | * | ||
21 | * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...(). | ||
22 | * As the consequence the locking was moved from dquot_decr_...(), | ||
23 | * dquot_incr_...() to calling functions. | ||
24 | * invalidate_dquots() now writes modified dquots. | ||
25 | * Serialized quota_off() and quota_on() for mount point. | ||
26 | * Fixed a few bugs in grow_dquots(). | ||
27 | * Fixed deadlock in write_dquot() - we no longer account quotas on | ||
28 | * quota files | ||
29 | * remove_dquot_ref() moved to inode.c - it now traverses through inodes | ||
30 | * add_dquot_ref() restarts after blocking | ||
31 | * Added check for bogus uid and fixed check for group in quotactl. | ||
32 | * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99 | ||
33 | * | ||
34 | * Used struct list_head instead of own list struct | ||
35 | * Invalidation of referenced dquots is no longer possible | ||
36 | * Improved free_dquots list management | ||
37 | * Quota and i_blocks are now updated in one place to avoid races | ||
38 | * Warnings are now delayed so we won't block in critical section | ||
39 | * Write updated not to require dquot lock | ||
40 | * Jan Kara, <jack@suse.cz>, 9/2000 | ||
41 | * | ||
42 | * Added dynamic quota structure allocation | ||
43 | * Jan Kara <jack@suse.cz> 12/2000 | ||
44 | * | ||
45 | * Rewritten quota interface. Implemented new quota format and | ||
46 | * formats registering. | ||
47 | * Jan Kara, <jack@suse.cz>, 2001,2002 | ||
48 | * | ||
49 | * New SMP locking. | ||
50 | * Jan Kara, <jack@suse.cz>, 10/2002 | ||
51 | * | ||
52 | * Added journalled quota support, fix lock inversion problems | ||
53 | * Jan Kara, <jack@suse.cz>, 2003,2004 | ||
54 | * | ||
55 | * (C) Copyright 1994 - 1997 Marco van Wieringen | ||
56 | */ | ||
57 | |||
58 | #include <linux/errno.h> | ||
59 | #include <linux/kernel.h> | ||
60 | #include <linux/fs.h> | ||
61 | #include <linux/mount.h> | ||
62 | #include <linux/mm.h> | ||
63 | #include <linux/time.h> | ||
64 | #include <linux/types.h> | ||
65 | #include <linux/string.h> | ||
66 | #include <linux/fcntl.h> | ||
67 | #include <linux/stat.h> | ||
68 | #include <linux/tty.h> | ||
69 | #include <linux/file.h> | ||
70 | #include <linux/slab.h> | ||
71 | #include <linux/sysctl.h> | ||
72 | #include <linux/smp_lock.h> | ||
73 | #include <linux/init.h> | ||
74 | #include <linux/module.h> | ||
75 | #include <linux/proc_fs.h> | ||
76 | #include <linux/security.h> | ||
77 | #include <linux/kmod.h> | ||
78 | #include <linux/namei.h> | ||
79 | #include <linux/buffer_head.h> | ||
80 | |||
81 | #include <asm/uaccess.h> | ||
82 | |||
83 | #define __DQUOT_PARANOIA | ||
84 | |||
85 | /* | ||
86 | * There are two quota SMP locks. dq_list_lock protects all lists with quotas | ||
87 | * and quota formats and also dqstats structure containing statistics about the | ||
88 | * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures | ||
89 | * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes. | ||
90 | * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly | ||
91 | * in inode_add_bytes() and inode_sub_bytes(). | ||
92 | * | ||
93 | * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock | ||
94 | * | ||
95 | * Note that some things (eg. sb pointer, type, id) doesn't change during | ||
96 | * the life of the dquot structure and so needn't to be protected by a lock | ||
97 | * | ||
98 | * Any operation working on dquots via inode pointers must hold dqptr_sem. If | ||
99 | * operation is just reading pointers from inode (or not using them at all) the | ||
100 | * read lock is enough. If pointers are altered function must hold write lock | ||
101 | * (these locking rules also apply for S_NOQUOTA flag in the inode - note that | ||
102 | * for altering the flag i_sem is also needed). If operation is holding | ||
103 | * reference to dquot in other way (e.g. quotactl ops) it must be guarded by | ||
104 | * dqonoff_sem. | ||
105 | * This locking assures that: | ||
106 | * a) update/access to dquot pointers in inode is serialized | ||
107 | * b) everyone is guarded against invalidate_dquots() | ||
108 | * | ||
109 | * Each dquot has its dq_lock semaphore. Locked dquots might not be referenced | ||
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: | ||
119 | * i_sem > dqonoff_sem > iprune_sem > journal_lock > dqptr_sem > | ||
120 | * > dquot->dq_lock > dqio_sem | ||
121 | * i_sem on quota files is special (it's below dqio_sem) | ||
122 | */ | ||
123 | |||
124 | static DEFINE_SPINLOCK(dq_list_lock); | ||
125 | DEFINE_SPINLOCK(dq_data_lock); | ||
126 | |||
127 | static char *quotatypes[] = INITQFNAMES; | ||
128 | static struct quota_format_type *quota_formats; /* List of registered formats */ | ||
129 | static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES; | ||
130 | |||
131 | /* SLAB cache for dquot structures */ | ||
132 | static kmem_cache_t *dquot_cachep; | ||
133 | |||
134 | int register_quota_format(struct quota_format_type *fmt) | ||
135 | { | ||
136 | spin_lock(&dq_list_lock); | ||
137 | fmt->qf_next = quota_formats; | ||
138 | quota_formats = fmt; | ||
139 | spin_unlock(&dq_list_lock); | ||
140 | return 0; | ||
141 | } | ||
142 | |||
143 | void unregister_quota_format(struct quota_format_type *fmt) | ||
144 | { | ||
145 | struct quota_format_type **actqf; | ||
146 | |||
147 | spin_lock(&dq_list_lock); | ||
148 | for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next); | ||
149 | if (*actqf) | ||
150 | *actqf = (*actqf)->qf_next; | ||
151 | spin_unlock(&dq_list_lock); | ||
152 | } | ||
153 | |||
154 | static struct quota_format_type *find_quota_format(int id) | ||
155 | { | ||
156 | struct quota_format_type *actqf; | ||
157 | |||
158 | spin_lock(&dq_list_lock); | ||
159 | for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next); | ||
160 | if (!actqf || !try_module_get(actqf->qf_owner)) { | ||
161 | int qm; | ||
162 | |||
163 | spin_unlock(&dq_list_lock); | ||
164 | |||
165 | for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++); | ||
166 | if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name)) | ||
167 | return NULL; | ||
168 | |||
169 | spin_lock(&dq_list_lock); | ||
170 | for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next); | ||
171 | if (actqf && !try_module_get(actqf->qf_owner)) | ||
172 | actqf = NULL; | ||
173 | } | ||
174 | spin_unlock(&dq_list_lock); | ||
175 | return actqf; | ||
176 | } | ||
177 | |||
178 | static void put_quota_format(struct quota_format_type *fmt) | ||
179 | { | ||
180 | module_put(fmt->qf_owner); | ||
181 | } | ||
182 | |||
183 | /* | ||
184 | * Dquot List Management: | ||
185 | * The quota code uses three lists for dquot management: the inuse_list, | ||
186 | * free_dquots, and dquot_hash[] array. A single dquot structure may be | ||
187 | * on all three lists, depending on its current state. | ||
188 | * | ||
189 | * All dquots are placed to the end of inuse_list when first created, and this | ||
190 | * list is used for invalidate operation, which must look at every dquot. | ||
191 | * | ||
192 | * Unused dquots (dq_count == 0) are added to the free_dquots list when freed, | ||
193 | * and this list is searched whenever we need an available dquot. Dquots are | ||
194 | * removed from the list as soon as they are used again, and | ||
195 | * dqstats.free_dquots gives the number of dquots on the list. When | ||
196 | * dquot is invalidated it's completely released from memory. | ||
197 | * | ||
198 | * Dquots with a specific identity (device, type and id) are placed on | ||
199 | * one of the dquot_hash[] hash chains. The provides an efficient search | ||
200 | * mechanism to locate a specific dquot. | ||
201 | */ | ||
202 | |||
203 | static LIST_HEAD(inuse_list); | ||
204 | static LIST_HEAD(free_dquots); | ||
205 | static unsigned int dq_hash_bits, dq_hash_mask; | ||
206 | static struct hlist_head *dquot_hash; | ||
207 | |||
208 | struct dqstats dqstats; | ||
209 | |||
210 | static void dqput(struct dquot *dquot); | ||
211 | |||
212 | static inline unsigned int | ||
213 | hashfn(const struct super_block *sb, unsigned int id, int type) | ||
214 | { | ||
215 | unsigned long tmp; | ||
216 | |||
217 | tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type); | ||
218 | return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask; | ||
219 | } | ||
220 | |||
221 | /* | ||
222 | * Following list functions expect dq_list_lock to be held | ||
223 | */ | ||
224 | static inline void insert_dquot_hash(struct dquot *dquot) | ||
225 | { | ||
226 | struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type); | ||
227 | hlist_add_head(&dquot->dq_hash, head); | ||
228 | } | ||
229 | |||
230 | static inline void remove_dquot_hash(struct dquot *dquot) | ||
231 | { | ||
232 | hlist_del_init(&dquot->dq_hash); | ||
233 | } | ||
234 | |||
235 | static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type) | ||
236 | { | ||
237 | struct hlist_node *node; | ||
238 | struct dquot *dquot; | ||
239 | |||
240 | hlist_for_each (node, dquot_hash+hashent) { | ||
241 | dquot = hlist_entry(node, struct dquot, dq_hash); | ||
242 | if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type) | ||
243 | return dquot; | ||
244 | } | ||
245 | return NODQUOT; | ||
246 | } | ||
247 | |||
248 | /* Add a dquot to the tail of the free list */ | ||
249 | static inline void put_dquot_last(struct dquot *dquot) | ||
250 | { | ||
251 | list_add(&dquot->dq_free, free_dquots.prev); | ||
252 | dqstats.free_dquots++; | ||
253 | } | ||
254 | |||
255 | static inline void remove_free_dquot(struct dquot *dquot) | ||
256 | { | ||
257 | if (list_empty(&dquot->dq_free)) | ||
258 | return; | ||
259 | list_del_init(&dquot->dq_free); | ||
260 | dqstats.free_dquots--; | ||
261 | } | ||
262 | |||
263 | static inline void put_inuse(struct dquot *dquot) | ||
264 | { | ||
265 | /* We add to the back of inuse list so we don't have to restart | ||
266 | * when traversing this list and we block */ | ||
267 | list_add(&dquot->dq_inuse, inuse_list.prev); | ||
268 | dqstats.allocated_dquots++; | ||
269 | } | ||
270 | |||
271 | static inline void remove_inuse(struct dquot *dquot) | ||
272 | { | ||
273 | dqstats.allocated_dquots--; | ||
274 | list_del(&dquot->dq_inuse); | ||
275 | } | ||
276 | /* | ||
277 | * End of list functions needing dq_list_lock | ||
278 | */ | ||
279 | |||
280 | static void wait_on_dquot(struct dquot *dquot) | ||
281 | { | ||
282 | down(&dquot->dq_lock); | ||
283 | up(&dquot->dq_lock); | ||
284 | } | ||
285 | |||
286 | #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot)) | ||
287 | |||
288 | int dquot_mark_dquot_dirty(struct dquot *dquot) | ||
289 | { | ||
290 | spin_lock(&dq_list_lock); | ||
291 | if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) | ||
292 | list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)-> | ||
293 | info[dquot->dq_type].dqi_dirty_list); | ||
294 | spin_unlock(&dq_list_lock); | ||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | /* This function needs dq_list_lock */ | ||
299 | static inline int clear_dquot_dirty(struct dquot *dquot) | ||
300 | { | ||
301 | if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) | ||
302 | return 0; | ||
303 | list_del_init(&dquot->dq_dirty); | ||
304 | return 1; | ||
305 | } | ||
306 | |||
307 | void mark_info_dirty(struct super_block *sb, int type) | ||
308 | { | ||
309 | set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags); | ||
310 | } | ||
311 | EXPORT_SYMBOL(mark_info_dirty); | ||
312 | |||
313 | /* | ||
314 | * Read dquot from disk and alloc space for it | ||
315 | */ | ||
316 | |||
317 | int dquot_acquire(struct dquot *dquot) | ||
318 | { | ||
319 | int ret = 0, ret2 = 0; | ||
320 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); | ||
321 | |||
322 | down(&dquot->dq_lock); | ||
323 | down(&dqopt->dqio_sem); | ||
324 | if (!test_bit(DQ_READ_B, &dquot->dq_flags)) | ||
325 | ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot); | ||
326 | if (ret < 0) | ||
327 | goto out_iolock; | ||
328 | set_bit(DQ_READ_B, &dquot->dq_flags); | ||
329 | /* Instantiate dquot if needed */ | ||
330 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) { | ||
331 | ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot); | ||
332 | /* Write the info if needed */ | ||
333 | if (info_dirty(&dqopt->info[dquot->dq_type])) | ||
334 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type); | ||
335 | if (ret < 0) | ||
336 | goto out_iolock; | ||
337 | if (ret2 < 0) { | ||
338 | ret = ret2; | ||
339 | goto out_iolock; | ||
340 | } | ||
341 | } | ||
342 | set_bit(DQ_ACTIVE_B, &dquot->dq_flags); | ||
343 | out_iolock: | ||
344 | up(&dqopt->dqio_sem); | ||
345 | up(&dquot->dq_lock); | ||
346 | return ret; | ||
347 | } | ||
348 | |||
349 | /* | ||
350 | * Write dquot to disk | ||
351 | */ | ||
352 | int dquot_commit(struct dquot *dquot) | ||
353 | { | ||
354 | int ret = 0, ret2 = 0; | ||
355 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); | ||
356 | |||
357 | down(&dqopt->dqio_sem); | ||
358 | spin_lock(&dq_list_lock); | ||
359 | if (!clear_dquot_dirty(dquot)) { | ||
360 | spin_unlock(&dq_list_lock); | ||
361 | goto out_sem; | ||
362 | } | ||
363 | spin_unlock(&dq_list_lock); | ||
364 | /* Inactive dquot can be only if there was error during read/init | ||
365 | * => we have better not writing it */ | ||
366 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { | ||
367 | ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot); | ||
368 | if (info_dirty(&dqopt->info[dquot->dq_type])) | ||
369 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type); | ||
370 | if (ret >= 0) | ||
371 | ret = ret2; | ||
372 | } | ||
373 | out_sem: | ||
374 | up(&dqopt->dqio_sem); | ||
375 | return ret; | ||
376 | } | ||
377 | |||
378 | /* | ||
379 | * Release dquot | ||
380 | */ | ||
381 | int dquot_release(struct dquot *dquot) | ||
382 | { | ||
383 | int ret = 0, ret2 = 0; | ||
384 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); | ||
385 | |||
386 | down(&dquot->dq_lock); | ||
387 | /* Check whether we are not racing with some other dqget() */ | ||
388 | if (atomic_read(&dquot->dq_count) > 1) | ||
389 | goto out_dqlock; | ||
390 | down(&dqopt->dqio_sem); | ||
391 | if (dqopt->ops[dquot->dq_type]->release_dqblk) { | ||
392 | ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot); | ||
393 | /* Write the info */ | ||
394 | if (info_dirty(&dqopt->info[dquot->dq_type])) | ||
395 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type); | ||
396 | if (ret >= 0) | ||
397 | ret = ret2; | ||
398 | } | ||
399 | clear_bit(DQ_ACTIVE_B, &dquot->dq_flags); | ||
400 | up(&dqopt->dqio_sem); | ||
401 | out_dqlock: | ||
402 | up(&dquot->dq_lock); | ||
403 | return ret; | ||
404 | } | ||
405 | |||
406 | /* Invalidate all dquots on the list. Note that this function is called after | ||
407 | * quota is disabled and pointers from inodes removed so there cannot be new | ||
408 | * quota users. Also because we hold dqonoff_sem there can be no quota users | ||
409 | * for this sb+type at all. */ | ||
410 | static void invalidate_dquots(struct super_block *sb, int type) | ||
411 | { | ||
412 | struct dquot *dquot; | ||
413 | struct list_head *head; | ||
414 | |||
415 | spin_lock(&dq_list_lock); | ||
416 | for (head = inuse_list.next; head != &inuse_list;) { | ||
417 | dquot = list_entry(head, struct dquot, dq_inuse); | ||
418 | head = head->next; | ||
419 | if (dquot->dq_sb != sb) | ||
420 | continue; | ||
421 | if (dquot->dq_type != type) | ||
422 | continue; | ||
423 | #ifdef __DQUOT_PARANOIA | ||
424 | if (atomic_read(&dquot->dq_count)) | ||
425 | BUG(); | ||
426 | #endif | ||
427 | /* Quota now has no users and it has been written on last dqput() */ | ||
428 | remove_dquot_hash(dquot); | ||
429 | remove_free_dquot(dquot); | ||
430 | remove_inuse(dquot); | ||
431 | kmem_cache_free(dquot_cachep, dquot); | ||
432 | } | ||
433 | spin_unlock(&dq_list_lock); | ||
434 | } | ||
435 | |||
436 | int vfs_quota_sync(struct super_block *sb, int type) | ||
437 | { | ||
438 | struct list_head *dirty; | ||
439 | struct dquot *dquot; | ||
440 | struct quota_info *dqopt = sb_dqopt(sb); | ||
441 | int cnt; | ||
442 | |||
443 | down(&dqopt->dqonoff_sem); | ||
444 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
445 | if (type != -1 && cnt != type) | ||
446 | continue; | ||
447 | if (!sb_has_quota_enabled(sb, cnt)) | ||
448 | continue; | ||
449 | spin_lock(&dq_list_lock); | ||
450 | dirty = &dqopt->info[cnt].dqi_dirty_list; | ||
451 | while (!list_empty(dirty)) { | ||
452 | dquot = list_entry(dirty->next, struct dquot, dq_dirty); | ||
453 | /* Dirty and inactive can be only bad dquot... */ | ||
454 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { | ||
455 | clear_dquot_dirty(dquot); | ||
456 | continue; | ||
457 | } | ||
458 | /* Now we have active dquot from which someone is | ||
459 | * holding reference so we can safely just increase | ||
460 | * use count */ | ||
461 | atomic_inc(&dquot->dq_count); | ||
462 | dqstats.lookups++; | ||
463 | spin_unlock(&dq_list_lock); | ||
464 | sb->dq_op->write_dquot(dquot); | ||
465 | dqput(dquot); | ||
466 | spin_lock(&dq_list_lock); | ||
467 | } | ||
468 | spin_unlock(&dq_list_lock); | ||
469 | } | ||
470 | |||
471 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
472 | if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt) | ||
473 | && info_dirty(&dqopt->info[cnt])) | ||
474 | sb->dq_op->write_info(sb, cnt); | ||
475 | spin_lock(&dq_list_lock); | ||
476 | dqstats.syncs++; | ||
477 | spin_unlock(&dq_list_lock); | ||
478 | up(&dqopt->dqonoff_sem); | ||
479 | |||
480 | return 0; | ||
481 | } | ||
482 | |||
483 | /* Free unused dquots from cache */ | ||
484 | static void prune_dqcache(int count) | ||
485 | { | ||
486 | struct list_head *head; | ||
487 | struct dquot *dquot; | ||
488 | |||
489 | head = free_dquots.prev; | ||
490 | while (head != &free_dquots && count) { | ||
491 | dquot = list_entry(head, struct dquot, dq_free); | ||
492 | remove_dquot_hash(dquot); | ||
493 | remove_free_dquot(dquot); | ||
494 | remove_inuse(dquot); | ||
495 | kmem_cache_free(dquot_cachep, dquot); | ||
496 | count--; | ||
497 | head = free_dquots.prev; | ||
498 | } | ||
499 | } | ||
500 | |||
501 | /* | ||
502 | * This is called from kswapd when we think we need some | ||
503 | * more memory | ||
504 | */ | ||
505 | |||
506 | static int shrink_dqcache_memory(int nr, unsigned int gfp_mask) | ||
507 | { | ||
508 | if (nr) { | ||
509 | spin_lock(&dq_list_lock); | ||
510 | prune_dqcache(nr); | ||
511 | spin_unlock(&dq_list_lock); | ||
512 | } | ||
513 | return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure; | ||
514 | } | ||
515 | |||
516 | /* | ||
517 | * Put reference to dquot | ||
518 | * NOTE: If you change this function please check whether dqput_blocks() works right... | ||
519 | * MUST be called with either dqptr_sem or dqonoff_sem held | ||
520 | */ | ||
521 | static void dqput(struct dquot *dquot) | ||
522 | { | ||
523 | if (!dquot) | ||
524 | return; | ||
525 | #ifdef __DQUOT_PARANOIA | ||
526 | if (!atomic_read(&dquot->dq_count)) { | ||
527 | printk("VFS: dqput: trying to free free dquot\n"); | ||
528 | printk("VFS: device %s, dquot of %s %d\n", | ||
529 | dquot->dq_sb->s_id, | ||
530 | quotatypes[dquot->dq_type], | ||
531 | dquot->dq_id); | ||
532 | BUG(); | ||
533 | } | ||
534 | #endif | ||
535 | |||
536 | spin_lock(&dq_list_lock); | ||
537 | dqstats.drops++; | ||
538 | spin_unlock(&dq_list_lock); | ||
539 | we_slept: | ||
540 | spin_lock(&dq_list_lock); | ||
541 | if (atomic_read(&dquot->dq_count) > 1) { | ||
542 | /* We have more than one user... nothing to do */ | ||
543 | atomic_dec(&dquot->dq_count); | ||
544 | spin_unlock(&dq_list_lock); | ||
545 | return; | ||
546 | } | ||
547 | /* Need to release dquot? */ | ||
548 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) { | ||
549 | spin_unlock(&dq_list_lock); | ||
550 | /* Commit dquot before releasing */ | ||
551 | dquot->dq_sb->dq_op->write_dquot(dquot); | ||
552 | goto we_slept; | ||
553 | } | ||
554 | /* Clear flag in case dquot was inactive (something bad happened) */ | ||
555 | clear_dquot_dirty(dquot); | ||
556 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { | ||
557 | spin_unlock(&dq_list_lock); | ||
558 | dquot->dq_sb->dq_op->release_dquot(dquot); | ||
559 | goto we_slept; | ||
560 | } | ||
561 | atomic_dec(&dquot->dq_count); | ||
562 | #ifdef __DQUOT_PARANOIA | ||
563 | /* sanity check */ | ||
564 | if (!list_empty(&dquot->dq_free)) | ||
565 | BUG(); | ||
566 | #endif | ||
567 | put_dquot_last(dquot); | ||
568 | spin_unlock(&dq_list_lock); | ||
569 | } | ||
570 | |||
571 | static struct dquot *get_empty_dquot(struct super_block *sb, int type) | ||
572 | { | ||
573 | struct dquot *dquot; | ||
574 | |||
575 | dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS); | ||
576 | if(!dquot) | ||
577 | return NODQUOT; | ||
578 | |||
579 | memset((caddr_t)dquot, 0, sizeof(struct dquot)); | ||
580 | sema_init(&dquot->dq_lock, 1); | ||
581 | INIT_LIST_HEAD(&dquot->dq_free); | ||
582 | INIT_LIST_HEAD(&dquot->dq_inuse); | ||
583 | INIT_HLIST_NODE(&dquot->dq_hash); | ||
584 | INIT_LIST_HEAD(&dquot->dq_dirty); | ||
585 | dquot->dq_sb = sb; | ||
586 | dquot->dq_type = type; | ||
587 | atomic_set(&dquot->dq_count, 1); | ||
588 | |||
589 | return dquot; | ||
590 | } | ||
591 | |||
592 | /* | ||
593 | * Get reference to dquot | ||
594 | * MUST be called with either dqptr_sem or dqonoff_sem held | ||
595 | */ | ||
596 | static struct dquot *dqget(struct super_block *sb, unsigned int id, int type) | ||
597 | { | ||
598 | unsigned int hashent = hashfn(sb, id, type); | ||
599 | struct dquot *dquot, *empty = NODQUOT; | ||
600 | |||
601 | if (!sb_has_quota_enabled(sb, type)) | ||
602 | return NODQUOT; | ||
603 | we_slept: | ||
604 | spin_lock(&dq_list_lock); | ||
605 | if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) { | ||
606 | if (empty == NODQUOT) { | ||
607 | spin_unlock(&dq_list_lock); | ||
608 | if ((empty = get_empty_dquot(sb, type)) == NODQUOT) | ||
609 | schedule(); /* Try to wait for a moment... */ | ||
610 | goto we_slept; | ||
611 | } | ||
612 | dquot = empty; | ||
613 | dquot->dq_id = id; | ||
614 | /* all dquots go on the inuse_list */ | ||
615 | put_inuse(dquot); | ||
616 | /* hash it first so it can be found */ | ||
617 | insert_dquot_hash(dquot); | ||
618 | dqstats.lookups++; | ||
619 | spin_unlock(&dq_list_lock); | ||
620 | } else { | ||
621 | if (!atomic_read(&dquot->dq_count)) | ||
622 | remove_free_dquot(dquot); | ||
623 | atomic_inc(&dquot->dq_count); | ||
624 | dqstats.cache_hits++; | ||
625 | dqstats.lookups++; | ||
626 | spin_unlock(&dq_list_lock); | ||
627 | if (empty) | ||
628 | kmem_cache_free(dquot_cachep, empty); | ||
629 | } | ||
630 | /* Wait for dq_lock - after this we know that either dquot_release() is already | ||
631 | * finished or it will be canceled due to dq_count > 1 test */ | ||
632 | wait_on_dquot(dquot); | ||
633 | /* Read the dquot and instantiate it (everything done only if needed) */ | ||
634 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) { | ||
635 | dqput(dquot); | ||
636 | return NODQUOT; | ||
637 | } | ||
638 | #ifdef __DQUOT_PARANOIA | ||
639 | if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */ | ||
640 | BUG(); | ||
641 | #endif | ||
642 | |||
643 | return dquot; | ||
644 | } | ||
645 | |||
646 | static int dqinit_needed(struct inode *inode, int type) | ||
647 | { | ||
648 | int cnt; | ||
649 | |||
650 | if (IS_NOQUOTA(inode)) | ||
651 | return 0; | ||
652 | if (type != -1) | ||
653 | return inode->i_dquot[type] == NODQUOT; | ||
654 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
655 | if (inode->i_dquot[cnt] == NODQUOT) | ||
656 | return 1; | ||
657 | return 0; | ||
658 | } | ||
659 | |||
660 | /* This routine is guarded by dqonoff_sem semaphore */ | ||
661 | static void add_dquot_ref(struct super_block *sb, int type) | ||
662 | { | ||
663 | struct list_head *p; | ||
664 | |||
665 | restart: | ||
666 | file_list_lock(); | ||
667 | list_for_each(p, &sb->s_files) { | ||
668 | struct file *filp = list_entry(p, struct file, f_list); | ||
669 | struct inode *inode = filp->f_dentry->d_inode; | ||
670 | if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) { | ||
671 | struct dentry *dentry = dget(filp->f_dentry); | ||
672 | file_list_unlock(); | ||
673 | sb->dq_op->initialize(inode, type); | ||
674 | dput(dentry); | ||
675 | /* As we may have blocked we had better restart... */ | ||
676 | goto restart; | ||
677 | } | ||
678 | } | ||
679 | file_list_unlock(); | ||
680 | } | ||
681 | |||
682 | /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */ | ||
683 | static inline int dqput_blocks(struct dquot *dquot) | ||
684 | { | ||
685 | if (atomic_read(&dquot->dq_count) <= 1) | ||
686 | return 1; | ||
687 | return 0; | ||
688 | } | ||
689 | |||
690 | /* Remove references to dquots from inode - add dquot to list for freeing if needed */ | ||
691 | /* We can't race with anybody because we hold dqptr_sem for writing... */ | ||
692 | int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head) | ||
693 | { | ||
694 | struct dquot *dquot = inode->i_dquot[type]; | ||
695 | |||
696 | inode->i_dquot[type] = NODQUOT; | ||
697 | if (dquot != NODQUOT) { | ||
698 | if (dqput_blocks(dquot)) { | ||
699 | #ifdef __DQUOT_PARANOIA | ||
700 | if (atomic_read(&dquot->dq_count) != 1) | ||
701 | printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count)); | ||
702 | #endif | ||
703 | spin_lock(&dq_list_lock); | ||
704 | list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */ | ||
705 | spin_unlock(&dq_list_lock); | ||
706 | return 1; | ||
707 | } | ||
708 | else | ||
709 | dqput(dquot); /* We have guaranteed we won't block */ | ||
710 | } | ||
711 | return 0; | ||
712 | } | ||
713 | |||
714 | /* Free list of dquots - called from inode.c */ | ||
715 | /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */ | ||
716 | static void put_dquot_list(struct list_head *tofree_head) | ||
717 | { | ||
718 | struct list_head *act_head; | ||
719 | struct dquot *dquot; | ||
720 | |||
721 | act_head = tofree_head->next; | ||
722 | /* So now we have dquots on the list... Just free them */ | ||
723 | while (act_head != tofree_head) { | ||
724 | dquot = list_entry(act_head, struct dquot, dq_free); | ||
725 | act_head = act_head->next; | ||
726 | list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */ | ||
727 | dqput(dquot); | ||
728 | } | ||
729 | } | ||
730 | |||
731 | /* Gather all references from inodes and drop them */ | ||
732 | static void drop_dquot_ref(struct super_block *sb, int type) | ||
733 | { | ||
734 | LIST_HEAD(tofree_head); | ||
735 | |||
736 | /* We need to be guarded against prune_icache to reach all the | ||
737 | * inodes - otherwise some can be on the local list of prune_icache */ | ||
738 | down(&iprune_sem); | ||
739 | down_write(&sb_dqopt(sb)->dqptr_sem); | ||
740 | remove_dquot_ref(sb, type, &tofree_head); | ||
741 | up_write(&sb_dqopt(sb)->dqptr_sem); | ||
742 | up(&iprune_sem); | ||
743 | put_dquot_list(&tofree_head); | ||
744 | } | ||
745 | |||
746 | static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number) | ||
747 | { | ||
748 | dquot->dq_dqb.dqb_curinodes += number; | ||
749 | } | ||
750 | |||
751 | static inline void dquot_incr_space(struct dquot *dquot, qsize_t number) | ||
752 | { | ||
753 | dquot->dq_dqb.dqb_curspace += number; | ||
754 | } | ||
755 | |||
756 | static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number) | ||
757 | { | ||
758 | if (dquot->dq_dqb.dqb_curinodes > number) | ||
759 | dquot->dq_dqb.dqb_curinodes -= number; | ||
760 | else | ||
761 | dquot->dq_dqb.dqb_curinodes = 0; | ||
762 | if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit) | ||
763 | dquot->dq_dqb.dqb_itime = (time_t) 0; | ||
764 | clear_bit(DQ_INODES_B, &dquot->dq_flags); | ||
765 | } | ||
766 | |||
767 | static inline void dquot_decr_space(struct dquot *dquot, qsize_t number) | ||
768 | { | ||
769 | if (dquot->dq_dqb.dqb_curspace > number) | ||
770 | dquot->dq_dqb.dqb_curspace -= number; | ||
771 | else | ||
772 | dquot->dq_dqb.dqb_curspace = 0; | ||
773 | if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit) | ||
774 | dquot->dq_dqb.dqb_btime = (time_t) 0; | ||
775 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); | ||
776 | } | ||
777 | |||
778 | static int flag_print_warnings = 1; | ||
779 | |||
780 | static inline int need_print_warning(struct dquot *dquot) | ||
781 | { | ||
782 | if (!flag_print_warnings) | ||
783 | return 0; | ||
784 | |||
785 | switch (dquot->dq_type) { | ||
786 | case USRQUOTA: | ||
787 | return current->fsuid == dquot->dq_id; | ||
788 | case GRPQUOTA: | ||
789 | return in_group_p(dquot->dq_id); | ||
790 | } | ||
791 | return 0; | ||
792 | } | ||
793 | |||
794 | /* Values of warnings */ | ||
795 | #define NOWARN 0 | ||
796 | #define IHARDWARN 1 | ||
797 | #define ISOFTLONGWARN 2 | ||
798 | #define ISOFTWARN 3 | ||
799 | #define BHARDWARN 4 | ||
800 | #define BSOFTLONGWARN 5 | ||
801 | #define BSOFTWARN 6 | ||
802 | |||
803 | /* Print warning to user which exceeded quota */ | ||
804 | static void print_warning(struct dquot *dquot, const char warntype) | ||
805 | { | ||
806 | char *msg = NULL; | ||
807 | int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B : | ||
808 | ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0); | ||
809 | |||
810 | if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags))) | ||
811 | return; | ||
812 | |||
813 | tty_write_message(current->signal->tty, dquot->dq_sb->s_id); | ||
814 | if (warntype == ISOFTWARN || warntype == BSOFTWARN) | ||
815 | tty_write_message(current->signal->tty, ": warning, "); | ||
816 | else | ||
817 | tty_write_message(current->signal->tty, ": write failed, "); | ||
818 | tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]); | ||
819 | switch (warntype) { | ||
820 | case IHARDWARN: | ||
821 | msg = " file limit reached.\r\n"; | ||
822 | break; | ||
823 | case ISOFTLONGWARN: | ||
824 | msg = " file quota exceeded too long.\r\n"; | ||
825 | break; | ||
826 | case ISOFTWARN: | ||
827 | msg = " file quota exceeded.\r\n"; | ||
828 | break; | ||
829 | case BHARDWARN: | ||
830 | msg = " block limit reached.\r\n"; | ||
831 | break; | ||
832 | case BSOFTLONGWARN: | ||
833 | msg = " block quota exceeded too long.\r\n"; | ||
834 | break; | ||
835 | case BSOFTWARN: | ||
836 | msg = " block quota exceeded.\r\n"; | ||
837 | break; | ||
838 | } | ||
839 | tty_write_message(current->signal->tty, msg); | ||
840 | } | ||
841 | |||
842 | static inline void flush_warnings(struct dquot **dquots, char *warntype) | ||
843 | { | ||
844 | int i; | ||
845 | |||
846 | for (i = 0; i < MAXQUOTAS; i++) | ||
847 | if (dquots[i] != NODQUOT && warntype[i] != NOWARN) | ||
848 | print_warning(dquots[i], warntype[i]); | ||
849 | } | ||
850 | |||
851 | static inline char ignore_hardlimit(struct dquot *dquot) | ||
852 | { | ||
853 | struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type]; | ||
854 | |||
855 | return capable(CAP_SYS_RESOURCE) && | ||
856 | (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH)); | ||
857 | } | ||
858 | |||
859 | /* needs dq_data_lock */ | ||
860 | static int check_idq(struct dquot *dquot, ulong inodes, char *warntype) | ||
861 | { | ||
862 | *warntype = NOWARN; | ||
863 | if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags)) | ||
864 | return QUOTA_OK; | ||
865 | |||
866 | if (dquot->dq_dqb.dqb_ihardlimit && | ||
867 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit && | ||
868 | !ignore_hardlimit(dquot)) { | ||
869 | *warntype = IHARDWARN; | ||
870 | return NO_QUOTA; | ||
871 | } | ||
872 | |||
873 | if (dquot->dq_dqb.dqb_isoftlimit && | ||
874 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit && | ||
875 | dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime && | ||
876 | !ignore_hardlimit(dquot)) { | ||
877 | *warntype = ISOFTLONGWARN; | ||
878 | return NO_QUOTA; | ||
879 | } | ||
880 | |||
881 | if (dquot->dq_dqb.dqb_isoftlimit && | ||
882 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit && | ||
883 | dquot->dq_dqb.dqb_itime == 0) { | ||
884 | *warntype = ISOFTWARN; | ||
885 | dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace; | ||
886 | } | ||
887 | |||
888 | return QUOTA_OK; | ||
889 | } | ||
890 | |||
891 | /* needs dq_data_lock */ | ||
892 | static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype) | ||
893 | { | ||
894 | *warntype = 0; | ||
895 | if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags)) | ||
896 | return QUOTA_OK; | ||
897 | |||
898 | if (dquot->dq_dqb.dqb_bhardlimit && | ||
899 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit && | ||
900 | !ignore_hardlimit(dquot)) { | ||
901 | if (!prealloc) | ||
902 | *warntype = BHARDWARN; | ||
903 | return NO_QUOTA; | ||
904 | } | ||
905 | |||
906 | if (dquot->dq_dqb.dqb_bsoftlimit && | ||
907 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit && | ||
908 | dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime && | ||
909 | !ignore_hardlimit(dquot)) { | ||
910 | if (!prealloc) | ||
911 | *warntype = BSOFTLONGWARN; | ||
912 | return NO_QUOTA; | ||
913 | } | ||
914 | |||
915 | if (dquot->dq_dqb.dqb_bsoftlimit && | ||
916 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit && | ||
917 | dquot->dq_dqb.dqb_btime == 0) { | ||
918 | if (!prealloc) { | ||
919 | *warntype = BSOFTWARN; | ||
920 | dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace; | ||
921 | } | ||
922 | else | ||
923 | /* | ||
924 | * We don't allow preallocation to exceed softlimit so exceeding will | ||
925 | * be always printed | ||
926 | */ | ||
927 | return NO_QUOTA; | ||
928 | } | ||
929 | |||
930 | return QUOTA_OK; | ||
931 | } | ||
932 | |||
933 | /* | ||
934 | * Initialize quota pointers in inode | ||
935 | * Transaction must be started at entry | ||
936 | */ | ||
937 | int dquot_initialize(struct inode *inode, int type) | ||
938 | { | ||
939 | unsigned int id = 0; | ||
940 | int cnt, ret = 0; | ||
941 | |||
942 | /* First test before acquiring semaphore - solves deadlocks when we | ||
943 | * re-enter the quota code and are already holding the semaphore */ | ||
944 | if (IS_NOQUOTA(inode)) | ||
945 | return 0; | ||
946 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
947 | /* Having dqptr_sem we know NOQUOTA flags can't be altered... */ | ||
948 | if (IS_NOQUOTA(inode)) | ||
949 | goto out_err; | ||
950 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
951 | if (type != -1 && cnt != type) | ||
952 | continue; | ||
953 | if (inode->i_dquot[cnt] == NODQUOT) { | ||
954 | switch (cnt) { | ||
955 | case USRQUOTA: | ||
956 | id = inode->i_uid; | ||
957 | break; | ||
958 | case GRPQUOTA: | ||
959 | id = inode->i_gid; | ||
960 | break; | ||
961 | } | ||
962 | inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt); | ||
963 | } | ||
964 | } | ||
965 | out_err: | ||
966 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
967 | return ret; | ||
968 | } | ||
969 | |||
970 | /* | ||
971 | * Release all quotas referenced by inode | ||
972 | * Transaction must be started at an entry | ||
973 | */ | ||
974 | int dquot_drop(struct inode *inode) | ||
975 | { | ||
976 | int cnt; | ||
977 | |||
978 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
979 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
980 | if (inode->i_dquot[cnt] != NODQUOT) { | ||
981 | dqput(inode->i_dquot[cnt]); | ||
982 | inode->i_dquot[cnt] = NODQUOT; | ||
983 | } | ||
984 | } | ||
985 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
986 | return 0; | ||
987 | } | ||
988 | |||
989 | /* | ||
990 | * Following four functions update i_blocks+i_bytes fields and | ||
991 | * quota information (together with appropriate checks) | ||
992 | * NOTE: We absolutely rely on the fact that caller dirties | ||
993 | * the inode (usually macros in quotaops.h care about this) and | ||
994 | * holds a handle for the current transaction so that dquot write and | ||
995 | * inode write go into the same transaction. | ||
996 | */ | ||
997 | |||
998 | /* | ||
999 | * This operation can block, but only after everything is updated | ||
1000 | */ | ||
1001 | int dquot_alloc_space(struct inode *inode, qsize_t number, int warn) | ||
1002 | { | ||
1003 | int cnt, ret = NO_QUOTA; | ||
1004 | char warntype[MAXQUOTAS]; | ||
1005 | |||
1006 | /* First test before acquiring semaphore - solves deadlocks when we | ||
1007 | * re-enter the quota code and are already holding the semaphore */ | ||
1008 | if (IS_NOQUOTA(inode)) { | ||
1009 | out_add: | ||
1010 | inode_add_bytes(inode, number); | ||
1011 | return QUOTA_OK; | ||
1012 | } | ||
1013 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
1014 | warntype[cnt] = NOWARN; | ||
1015 | |||
1016 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1017 | if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */ | ||
1018 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1019 | goto out_add; | ||
1020 | } | ||
1021 | spin_lock(&dq_data_lock); | ||
1022 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1023 | if (inode->i_dquot[cnt] == NODQUOT) | ||
1024 | continue; | ||
1025 | if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA) | ||
1026 | goto warn_put_all; | ||
1027 | } | ||
1028 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1029 | if (inode->i_dquot[cnt] == NODQUOT) | ||
1030 | continue; | ||
1031 | dquot_incr_space(inode->i_dquot[cnt], number); | ||
1032 | } | ||
1033 | inode_add_bytes(inode, number); | ||
1034 | ret = QUOTA_OK; | ||
1035 | warn_put_all: | ||
1036 | spin_unlock(&dq_data_lock); | ||
1037 | if (ret == QUOTA_OK) | ||
1038 | /* Dirtify all the dquots - this can block when journalling */ | ||
1039 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
1040 | if (inode->i_dquot[cnt]) | ||
1041 | mark_dquot_dirty(inode->i_dquot[cnt]); | ||
1042 | flush_warnings(inode->i_dquot, warntype); | ||
1043 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1044 | return ret; | ||
1045 | } | ||
1046 | |||
1047 | /* | ||
1048 | * This operation can block, but only after everything is updated | ||
1049 | */ | ||
1050 | int dquot_alloc_inode(const struct inode *inode, unsigned long number) | ||
1051 | { | ||
1052 | int cnt, ret = NO_QUOTA; | ||
1053 | char warntype[MAXQUOTAS]; | ||
1054 | |||
1055 | /* First test before acquiring semaphore - solves deadlocks when we | ||
1056 | * re-enter the quota code and are already holding the semaphore */ | ||
1057 | if (IS_NOQUOTA(inode)) | ||
1058 | return QUOTA_OK; | ||
1059 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
1060 | warntype[cnt] = NOWARN; | ||
1061 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1062 | if (IS_NOQUOTA(inode)) { | ||
1063 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1064 | return QUOTA_OK; | ||
1065 | } | ||
1066 | spin_lock(&dq_data_lock); | ||
1067 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1068 | if (inode->i_dquot[cnt] == NODQUOT) | ||
1069 | continue; | ||
1070 | if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA) | ||
1071 | goto warn_put_all; | ||
1072 | } | ||
1073 | |||
1074 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1075 | if (inode->i_dquot[cnt] == NODQUOT) | ||
1076 | continue; | ||
1077 | dquot_incr_inodes(inode->i_dquot[cnt], number); | ||
1078 | } | ||
1079 | ret = QUOTA_OK; | ||
1080 | warn_put_all: | ||
1081 | spin_unlock(&dq_data_lock); | ||
1082 | if (ret == QUOTA_OK) | ||
1083 | /* Dirtify all the dquots - this can block when journalling */ | ||
1084 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
1085 | if (inode->i_dquot[cnt]) | ||
1086 | mark_dquot_dirty(inode->i_dquot[cnt]); | ||
1087 | flush_warnings((struct dquot **)inode->i_dquot, warntype); | ||
1088 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1089 | return ret; | ||
1090 | } | ||
1091 | |||
1092 | /* | ||
1093 | * This operation can block, but only after everything is updated | ||
1094 | */ | ||
1095 | int dquot_free_space(struct inode *inode, qsize_t number) | ||
1096 | { | ||
1097 | unsigned int cnt; | ||
1098 | |||
1099 | /* First test before acquiring semaphore - solves deadlocks when we | ||
1100 | * re-enter the quota code and are already holding the semaphore */ | ||
1101 | if (IS_NOQUOTA(inode)) { | ||
1102 | out_sub: | ||
1103 | inode_sub_bytes(inode, number); | ||
1104 | return QUOTA_OK; | ||
1105 | } | ||
1106 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1107 | /* Now recheck reliably when holding dqptr_sem */ | ||
1108 | if (IS_NOQUOTA(inode)) { | ||
1109 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1110 | goto out_sub; | ||
1111 | } | ||
1112 | spin_lock(&dq_data_lock); | ||
1113 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1114 | if (inode->i_dquot[cnt] == NODQUOT) | ||
1115 | continue; | ||
1116 | dquot_decr_space(inode->i_dquot[cnt], number); | ||
1117 | } | ||
1118 | inode_sub_bytes(inode, number); | ||
1119 | spin_unlock(&dq_data_lock); | ||
1120 | /* Dirtify all the dquots - this can block when journalling */ | ||
1121 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
1122 | if (inode->i_dquot[cnt]) | ||
1123 | mark_dquot_dirty(inode->i_dquot[cnt]); | ||
1124 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1125 | return QUOTA_OK; | ||
1126 | } | ||
1127 | |||
1128 | /* | ||
1129 | * This operation can block, but only after everything is updated | ||
1130 | */ | ||
1131 | int dquot_free_inode(const struct inode *inode, unsigned long number) | ||
1132 | { | ||
1133 | unsigned int cnt; | ||
1134 | |||
1135 | /* First test before acquiring semaphore - solves deadlocks when we | ||
1136 | * re-enter the quota code and are already holding the semaphore */ | ||
1137 | if (IS_NOQUOTA(inode)) | ||
1138 | return QUOTA_OK; | ||
1139 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1140 | /* Now recheck reliably when holding dqptr_sem */ | ||
1141 | if (IS_NOQUOTA(inode)) { | ||
1142 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1143 | return QUOTA_OK; | ||
1144 | } | ||
1145 | spin_lock(&dq_data_lock); | ||
1146 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1147 | if (inode->i_dquot[cnt] == NODQUOT) | ||
1148 | continue; | ||
1149 | dquot_decr_inodes(inode->i_dquot[cnt], number); | ||
1150 | } | ||
1151 | spin_unlock(&dq_data_lock); | ||
1152 | /* Dirtify all the dquots - this can block when journalling */ | ||
1153 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
1154 | if (inode->i_dquot[cnt]) | ||
1155 | mark_dquot_dirty(inode->i_dquot[cnt]); | ||
1156 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1157 | return QUOTA_OK; | ||
1158 | } | ||
1159 | |||
1160 | /* | ||
1161 | * Transfer the number of inode and blocks from one diskquota to an other. | ||
1162 | * | ||
1163 | * This operation can block, but only after everything is updated | ||
1164 | * A transaction must be started when entering this function. | ||
1165 | */ | ||
1166 | int dquot_transfer(struct inode *inode, struct iattr *iattr) | ||
1167 | { | ||
1168 | qsize_t space; | ||
1169 | struct dquot *transfer_from[MAXQUOTAS]; | ||
1170 | struct dquot *transfer_to[MAXQUOTAS]; | ||
1171 | int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid, | ||
1172 | chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid; | ||
1173 | char warntype[MAXQUOTAS]; | ||
1174 | |||
1175 | /* First test before acquiring semaphore - solves deadlocks when we | ||
1176 | * re-enter the quota code and are already holding the semaphore */ | ||
1177 | if (IS_NOQUOTA(inode)) | ||
1178 | return QUOTA_OK; | ||
1179 | /* Clear the arrays */ | ||
1180 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1181 | transfer_to[cnt] = transfer_from[cnt] = NODQUOT; | ||
1182 | warntype[cnt] = NOWARN; | ||
1183 | } | ||
1184 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1185 | /* Now recheck reliably when holding dqptr_sem */ | ||
1186 | if (IS_NOQUOTA(inode)) { /* File without quota accounting? */ | ||
1187 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1188 | return QUOTA_OK; | ||
1189 | } | ||
1190 | /* First build the transfer_to list - here we can block on | ||
1191 | * reading/instantiating of dquots. We know that the transaction for | ||
1192 | * us was already started so we don't violate lock ranking here */ | ||
1193 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1194 | switch (cnt) { | ||
1195 | case USRQUOTA: | ||
1196 | if (!chuid) | ||
1197 | continue; | ||
1198 | transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt); | ||
1199 | break; | ||
1200 | case GRPQUOTA: | ||
1201 | if (!chgid) | ||
1202 | continue; | ||
1203 | transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt); | ||
1204 | break; | ||
1205 | } | ||
1206 | } | ||
1207 | spin_lock(&dq_data_lock); | ||
1208 | space = inode_get_bytes(inode); | ||
1209 | /* Build the transfer_from list and check the limits */ | ||
1210 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1211 | if (transfer_to[cnt] == NODQUOT) | ||
1212 | continue; | ||
1213 | transfer_from[cnt] = inode->i_dquot[cnt]; | ||
1214 | if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA || | ||
1215 | check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA) | ||
1216 | goto warn_put_all; | ||
1217 | } | ||
1218 | |||
1219 | /* | ||
1220 | * Finally perform the needed transfer from transfer_from to transfer_to | ||
1221 | */ | ||
1222 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1223 | /* | ||
1224 | * Skip changes for same uid or gid or for turned off quota-type. | ||
1225 | */ | ||
1226 | if (transfer_to[cnt] == NODQUOT) | ||
1227 | continue; | ||
1228 | |||
1229 | /* Due to IO error we might not have transfer_from[] structure */ | ||
1230 | if (transfer_from[cnt]) { | ||
1231 | dquot_decr_inodes(transfer_from[cnt], 1); | ||
1232 | dquot_decr_space(transfer_from[cnt], space); | ||
1233 | } | ||
1234 | |||
1235 | dquot_incr_inodes(transfer_to[cnt], 1); | ||
1236 | dquot_incr_space(transfer_to[cnt], space); | ||
1237 | |||
1238 | inode->i_dquot[cnt] = transfer_to[cnt]; | ||
1239 | } | ||
1240 | ret = QUOTA_OK; | ||
1241 | warn_put_all: | ||
1242 | spin_unlock(&dq_data_lock); | ||
1243 | /* Dirtify all the dquots - this can block when journalling */ | ||
1244 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1245 | if (transfer_from[cnt]) | ||
1246 | mark_dquot_dirty(transfer_from[cnt]); | ||
1247 | if (transfer_to[cnt]) | ||
1248 | mark_dquot_dirty(transfer_to[cnt]); | ||
1249 | } | ||
1250 | flush_warnings(transfer_to, warntype); | ||
1251 | |||
1252 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1253 | if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT) | ||
1254 | dqput(transfer_from[cnt]); | ||
1255 | if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT) | ||
1256 | dqput(transfer_to[cnt]); | ||
1257 | } | ||
1258 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); | ||
1259 | return ret; | ||
1260 | } | ||
1261 | |||
1262 | /* | ||
1263 | * Write info of quota file to disk | ||
1264 | */ | ||
1265 | int dquot_commit_info(struct super_block *sb, int type) | ||
1266 | { | ||
1267 | int ret; | ||
1268 | struct quota_info *dqopt = sb_dqopt(sb); | ||
1269 | |||
1270 | down(&dqopt->dqio_sem); | ||
1271 | ret = dqopt->ops[type]->write_file_info(sb, type); | ||
1272 | up(&dqopt->dqio_sem); | ||
1273 | return ret; | ||
1274 | } | ||
1275 | |||
1276 | /* | ||
1277 | * Definitions of diskquota operations. | ||
1278 | */ | ||
1279 | struct dquot_operations dquot_operations = { | ||
1280 | .initialize = dquot_initialize, | ||
1281 | .drop = dquot_drop, | ||
1282 | .alloc_space = dquot_alloc_space, | ||
1283 | .alloc_inode = dquot_alloc_inode, | ||
1284 | .free_space = dquot_free_space, | ||
1285 | .free_inode = dquot_free_inode, | ||
1286 | .transfer = dquot_transfer, | ||
1287 | .write_dquot = dquot_commit, | ||
1288 | .acquire_dquot = dquot_acquire, | ||
1289 | .release_dquot = dquot_release, | ||
1290 | .mark_dirty = dquot_mark_dquot_dirty, | ||
1291 | .write_info = dquot_commit_info | ||
1292 | }; | ||
1293 | |||
1294 | static inline void set_enable_flags(struct quota_info *dqopt, int type) | ||
1295 | { | ||
1296 | switch (type) { | ||
1297 | case USRQUOTA: | ||
1298 | dqopt->flags |= DQUOT_USR_ENABLED; | ||
1299 | break; | ||
1300 | case GRPQUOTA: | ||
1301 | dqopt->flags |= DQUOT_GRP_ENABLED; | ||
1302 | break; | ||
1303 | } | ||
1304 | } | ||
1305 | |||
1306 | static inline void reset_enable_flags(struct quota_info *dqopt, int type) | ||
1307 | { | ||
1308 | switch (type) { | ||
1309 | case USRQUOTA: | ||
1310 | dqopt->flags &= ~DQUOT_USR_ENABLED; | ||
1311 | break; | ||
1312 | case GRPQUOTA: | ||
1313 | dqopt->flags &= ~DQUOT_GRP_ENABLED; | ||
1314 | break; | ||
1315 | } | ||
1316 | } | ||
1317 | |||
1318 | /* | ||
1319 | * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount) | ||
1320 | */ | ||
1321 | int vfs_quota_off(struct super_block *sb, int type) | ||
1322 | { | ||
1323 | int cnt; | ||
1324 | struct quota_info *dqopt = sb_dqopt(sb); | ||
1325 | struct inode *toputinode[MAXQUOTAS]; | ||
1326 | struct vfsmount *toputmnt[MAXQUOTAS]; | ||
1327 | |||
1328 | /* We need to serialize quota_off() for device */ | ||
1329 | down(&dqopt->dqonoff_sem); | ||
1330 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { | ||
1331 | toputinode[cnt] = NULL; | ||
1332 | toputmnt[cnt] = NULL; | ||
1333 | if (type != -1 && cnt != type) | ||
1334 | continue; | ||
1335 | if (!sb_has_quota_enabled(sb, cnt)) | ||
1336 | continue; | ||
1337 | reset_enable_flags(dqopt, cnt); | ||
1338 | |||
1339 | /* Note: these are blocking operations */ | ||
1340 | drop_dquot_ref(sb, cnt); | ||
1341 | invalidate_dquots(sb, cnt); | ||
1342 | /* | ||
1343 | * Now all dquots should be invalidated, all writes done so we should be only | ||
1344 | * users of the info. No locks needed. | ||
1345 | */ | ||
1346 | if (info_dirty(&dqopt->info[cnt])) | ||
1347 | sb->dq_op->write_info(sb, cnt); | ||
1348 | if (dqopt->ops[cnt]->free_file_info) | ||
1349 | dqopt->ops[cnt]->free_file_info(sb, cnt); | ||
1350 | put_quota_format(dqopt->info[cnt].dqi_format); | ||
1351 | |||
1352 | toputinode[cnt] = dqopt->files[cnt]; | ||
1353 | toputmnt[cnt] = dqopt->mnt[cnt]; | ||
1354 | dqopt->files[cnt] = NULL; | ||
1355 | dqopt->mnt[cnt] = NULL; | ||
1356 | dqopt->info[cnt].dqi_flags = 0; | ||
1357 | dqopt->info[cnt].dqi_igrace = 0; | ||
1358 | dqopt->info[cnt].dqi_bgrace = 0; | ||
1359 | dqopt->ops[cnt] = NULL; | ||
1360 | } | ||
1361 | up(&dqopt->dqonoff_sem); | ||
1362 | /* Sync the superblock so that buffers with quota data are written to | ||
1363 | * disk (and so userspace sees correct data afterwards). | ||
1364 | * The reference to vfsmnt we are still holding protects us from | ||
1365 | * umount (we don't have it only when quotas are turned on/off for | ||
1366 | * journal replay but in that case we are guarded by the fs anyway). */ | ||
1367 | if (sb->s_op->sync_fs) | ||
1368 | sb->s_op->sync_fs(sb, 1); | ||
1369 | sync_blockdev(sb->s_bdev); | ||
1370 | /* Now the quota files are just ordinary files and we can set the | ||
1371 | * inode flags back. Moreover we discard the pagecache so that | ||
1372 | * userspace sees the writes we did bypassing the pagecache. We | ||
1373 | * must also discard the blockdev buffers so that we see the | ||
1374 | * changes done by userspace on the next quotaon() */ | ||
1375 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) | ||
1376 | if (toputinode[cnt]) { | ||
1377 | down(&dqopt->dqonoff_sem); | ||
1378 | /* If quota was reenabled in the meantime, we have | ||
1379 | * nothing to do */ | ||
1380 | if (!sb_has_quota_enabled(sb, cnt)) { | ||
1381 | down(&toputinode[cnt]->i_sem); | ||
1382 | toputinode[cnt]->i_flags &= ~(S_IMMUTABLE | | ||
1383 | S_NOATIME | S_NOQUOTA); | ||
1384 | truncate_inode_pages(&toputinode[cnt]->i_data, 0); | ||
1385 | up(&toputinode[cnt]->i_sem); | ||
1386 | mark_inode_dirty(toputinode[cnt]); | ||
1387 | iput(toputinode[cnt]); | ||
1388 | } | ||
1389 | up(&dqopt->dqonoff_sem); | ||
1390 | /* We don't hold the reference when we turned on quotas | ||
1391 | * just for the journal replay... */ | ||
1392 | if (toputmnt[cnt]) | ||
1393 | mntput(toputmnt[cnt]); | ||
1394 | } | ||
1395 | if (sb->s_bdev) | ||
1396 | invalidate_bdev(sb->s_bdev, 0); | ||
1397 | return 0; | ||
1398 | } | ||
1399 | |||
1400 | /* | ||
1401 | * Turn quotas on on a device | ||
1402 | */ | ||
1403 | |||
1404 | /* Helper function when we already have the inode */ | ||
1405 | static int vfs_quota_on_inode(struct inode *inode, int type, int format_id) | ||
1406 | { | ||
1407 | struct quota_format_type *fmt = find_quota_format(format_id); | ||
1408 | struct super_block *sb = inode->i_sb; | ||
1409 | struct quota_info *dqopt = sb_dqopt(sb); | ||
1410 | int error; | ||
1411 | int oldflags = -1; | ||
1412 | |||
1413 | if (!fmt) | ||
1414 | return -ESRCH; | ||
1415 | if (!S_ISREG(inode->i_mode)) { | ||
1416 | error = -EACCES; | ||
1417 | goto out_fmt; | ||
1418 | } | ||
1419 | if (IS_RDONLY(inode)) { | ||
1420 | error = -EROFS; | ||
1421 | goto out_fmt; | ||
1422 | } | ||
1423 | if (!sb->s_op->quota_write || !sb->s_op->quota_read) { | ||
1424 | error = -EINVAL; | ||
1425 | goto out_fmt; | ||
1426 | } | ||
1427 | |||
1428 | /* As we bypass the pagecache we must now flush the inode so that | ||
1429 | * we see all the changes from userspace... */ | ||
1430 | write_inode_now(inode, 1); | ||
1431 | /* And now flush the block cache so that kernel sees the changes */ | ||
1432 | invalidate_bdev(sb->s_bdev, 0); | ||
1433 | down(&inode->i_sem); | ||
1434 | down(&dqopt->dqonoff_sem); | ||
1435 | if (sb_has_quota_enabled(sb, type)) { | ||
1436 | error = -EBUSY; | ||
1437 | goto out_lock; | ||
1438 | } | ||
1439 | /* We don't want quota and atime on quota files (deadlocks possible) | ||
1440 | * Also nobody should write to the file - we use special IO operations | ||
1441 | * which ignore the immutable bit. */ | ||
1442 | down_write(&dqopt->dqptr_sem); | ||
1443 | oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA); | ||
1444 | inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE; | ||
1445 | up_write(&dqopt->dqptr_sem); | ||
1446 | |||
1447 | error = -EIO; | ||
1448 | dqopt->files[type] = igrab(inode); | ||
1449 | if (!dqopt->files[type]) | ||
1450 | goto out_lock; | ||
1451 | error = -EINVAL; | ||
1452 | if (!fmt->qf_ops->check_quota_file(sb, type)) | ||
1453 | goto out_file_init; | ||
1454 | |||
1455 | dqopt->ops[type] = fmt->qf_ops; | ||
1456 | dqopt->info[type].dqi_format = fmt; | ||
1457 | INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list); | ||
1458 | down(&dqopt->dqio_sem); | ||
1459 | if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) { | ||
1460 | up(&dqopt->dqio_sem); | ||
1461 | goto out_file_init; | ||
1462 | } | ||
1463 | up(&dqopt->dqio_sem); | ||
1464 | up(&inode->i_sem); | ||
1465 | set_enable_flags(dqopt, type); | ||
1466 | |||
1467 | add_dquot_ref(sb, type); | ||
1468 | up(&dqopt->dqonoff_sem); | ||
1469 | |||
1470 | return 0; | ||
1471 | |||
1472 | out_file_init: | ||
1473 | dqopt->files[type] = NULL; | ||
1474 | iput(inode); | ||
1475 | out_lock: | ||
1476 | up(&dqopt->dqonoff_sem); | ||
1477 | if (oldflags != -1) { | ||
1478 | down_write(&dqopt->dqptr_sem); | ||
1479 | /* Set the flags back (in the case of accidental quotaon() | ||
1480 | * on a wrong file we don't want to mess up the flags) */ | ||
1481 | inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE); | ||
1482 | inode->i_flags |= oldflags; | ||
1483 | up_write(&dqopt->dqptr_sem); | ||
1484 | } | ||
1485 | up(&inode->i_sem); | ||
1486 | out_fmt: | ||
1487 | put_quota_format(fmt); | ||
1488 | |||
1489 | return error; | ||
1490 | } | ||
1491 | |||
1492 | /* Actual function called from quotactl() */ | ||
1493 | int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path) | ||
1494 | { | ||
1495 | struct nameidata nd; | ||
1496 | int error; | ||
1497 | |||
1498 | error = path_lookup(path, LOOKUP_FOLLOW, &nd); | ||
1499 | if (error < 0) | ||
1500 | return error; | ||
1501 | error = security_quota_on(nd.dentry); | ||
1502 | if (error) | ||
1503 | goto out_path; | ||
1504 | /* Quota file not on the same filesystem? */ | ||
1505 | if (nd.mnt->mnt_sb != sb) | ||
1506 | error = -EXDEV; | ||
1507 | else { | ||
1508 | error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id); | ||
1509 | if (!error) | ||
1510 | sb_dqopt(sb)->mnt[type] = mntget(nd.mnt); | ||
1511 | } | ||
1512 | out_path: | ||
1513 | path_release(&nd); | ||
1514 | return error; | ||
1515 | } | ||
1516 | |||
1517 | /* | ||
1518 | * This function is used when filesystem needs to initialize quotas | ||
1519 | * during mount time. | ||
1520 | */ | ||
1521 | int vfs_quota_on_mount(int type, int format_id, struct dentry *dentry) | ||
1522 | { | ||
1523 | int error; | ||
1524 | |||
1525 | error = security_quota_on(dentry); | ||
1526 | if (error) | ||
1527 | return error; | ||
1528 | return vfs_quota_on_inode(dentry->d_inode, type, format_id); | ||
1529 | } | ||
1530 | |||
1531 | /* Generic routine for getting common part of quota structure */ | ||
1532 | static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di) | ||
1533 | { | ||
1534 | struct mem_dqblk *dm = &dquot->dq_dqb; | ||
1535 | |||
1536 | spin_lock(&dq_data_lock); | ||
1537 | di->dqb_bhardlimit = dm->dqb_bhardlimit; | ||
1538 | di->dqb_bsoftlimit = dm->dqb_bsoftlimit; | ||
1539 | di->dqb_curspace = dm->dqb_curspace; | ||
1540 | di->dqb_ihardlimit = dm->dqb_ihardlimit; | ||
1541 | di->dqb_isoftlimit = dm->dqb_isoftlimit; | ||
1542 | di->dqb_curinodes = dm->dqb_curinodes; | ||
1543 | di->dqb_btime = dm->dqb_btime; | ||
1544 | di->dqb_itime = dm->dqb_itime; | ||
1545 | di->dqb_valid = QIF_ALL; | ||
1546 | spin_unlock(&dq_data_lock); | ||
1547 | } | ||
1548 | |||
1549 | int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di) | ||
1550 | { | ||
1551 | struct dquot *dquot; | ||
1552 | |||
1553 | down(&sb_dqopt(sb)->dqonoff_sem); | ||
1554 | if (!(dquot = dqget(sb, id, type))) { | ||
1555 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1556 | return -ESRCH; | ||
1557 | } | ||
1558 | do_get_dqblk(dquot, di); | ||
1559 | dqput(dquot); | ||
1560 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1561 | return 0; | ||
1562 | } | ||
1563 | |||
1564 | /* Generic routine for setting common part of quota structure */ | ||
1565 | static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di) | ||
1566 | { | ||
1567 | struct mem_dqblk *dm = &dquot->dq_dqb; | ||
1568 | int check_blim = 0, check_ilim = 0; | ||
1569 | |||
1570 | spin_lock(&dq_data_lock); | ||
1571 | if (di->dqb_valid & QIF_SPACE) { | ||
1572 | dm->dqb_curspace = di->dqb_curspace; | ||
1573 | check_blim = 1; | ||
1574 | } | ||
1575 | if (di->dqb_valid & QIF_BLIMITS) { | ||
1576 | dm->dqb_bsoftlimit = di->dqb_bsoftlimit; | ||
1577 | dm->dqb_bhardlimit = di->dqb_bhardlimit; | ||
1578 | check_blim = 1; | ||
1579 | } | ||
1580 | if (di->dqb_valid & QIF_INODES) { | ||
1581 | dm->dqb_curinodes = di->dqb_curinodes; | ||
1582 | check_ilim = 1; | ||
1583 | } | ||
1584 | if (di->dqb_valid & QIF_ILIMITS) { | ||
1585 | dm->dqb_isoftlimit = di->dqb_isoftlimit; | ||
1586 | dm->dqb_ihardlimit = di->dqb_ihardlimit; | ||
1587 | check_ilim = 1; | ||
1588 | } | ||
1589 | if (di->dqb_valid & QIF_BTIME) | ||
1590 | dm->dqb_btime = di->dqb_btime; | ||
1591 | if (di->dqb_valid & QIF_ITIME) | ||
1592 | dm->dqb_itime = di->dqb_itime; | ||
1593 | |||
1594 | if (check_blim) { | ||
1595 | if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) { | ||
1596 | dm->dqb_btime = 0; | ||
1597 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); | ||
1598 | } | ||
1599 | else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */ | ||
1600 | dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace; | ||
1601 | } | ||
1602 | if (check_ilim) { | ||
1603 | if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) { | ||
1604 | dm->dqb_itime = 0; | ||
1605 | clear_bit(DQ_INODES_B, &dquot->dq_flags); | ||
1606 | } | ||
1607 | else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */ | ||
1608 | dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace; | ||
1609 | } | ||
1610 | if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit) | ||
1611 | clear_bit(DQ_FAKE_B, &dquot->dq_flags); | ||
1612 | else | ||
1613 | set_bit(DQ_FAKE_B, &dquot->dq_flags); | ||
1614 | spin_unlock(&dq_data_lock); | ||
1615 | mark_dquot_dirty(dquot); | ||
1616 | } | ||
1617 | |||
1618 | int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di) | ||
1619 | { | ||
1620 | struct dquot *dquot; | ||
1621 | |||
1622 | down(&sb_dqopt(sb)->dqonoff_sem); | ||
1623 | if (!(dquot = dqget(sb, id, type))) { | ||
1624 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1625 | return -ESRCH; | ||
1626 | } | ||
1627 | do_set_dqblk(dquot, di); | ||
1628 | dqput(dquot); | ||
1629 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1630 | return 0; | ||
1631 | } | ||
1632 | |||
1633 | /* Generic routine for getting common part of quota file information */ | ||
1634 | int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) | ||
1635 | { | ||
1636 | struct mem_dqinfo *mi; | ||
1637 | |||
1638 | down(&sb_dqopt(sb)->dqonoff_sem); | ||
1639 | if (!sb_has_quota_enabled(sb, type)) { | ||
1640 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1641 | return -ESRCH; | ||
1642 | } | ||
1643 | mi = sb_dqopt(sb)->info + type; | ||
1644 | spin_lock(&dq_data_lock); | ||
1645 | ii->dqi_bgrace = mi->dqi_bgrace; | ||
1646 | ii->dqi_igrace = mi->dqi_igrace; | ||
1647 | ii->dqi_flags = mi->dqi_flags & DQF_MASK; | ||
1648 | ii->dqi_valid = IIF_ALL; | ||
1649 | spin_unlock(&dq_data_lock); | ||
1650 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1651 | return 0; | ||
1652 | } | ||
1653 | |||
1654 | /* Generic routine for setting common part of quota file information */ | ||
1655 | int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) | ||
1656 | { | ||
1657 | struct mem_dqinfo *mi; | ||
1658 | |||
1659 | down(&sb_dqopt(sb)->dqonoff_sem); | ||
1660 | if (!sb_has_quota_enabled(sb, type)) { | ||
1661 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1662 | return -ESRCH; | ||
1663 | } | ||
1664 | mi = sb_dqopt(sb)->info + type; | ||
1665 | spin_lock(&dq_data_lock); | ||
1666 | if (ii->dqi_valid & IIF_BGRACE) | ||
1667 | mi->dqi_bgrace = ii->dqi_bgrace; | ||
1668 | if (ii->dqi_valid & IIF_IGRACE) | ||
1669 | mi->dqi_igrace = ii->dqi_igrace; | ||
1670 | if (ii->dqi_valid & IIF_FLAGS) | ||
1671 | mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK); | ||
1672 | spin_unlock(&dq_data_lock); | ||
1673 | mark_info_dirty(sb, type); | ||
1674 | /* Force write to disk */ | ||
1675 | sb->dq_op->write_info(sb, type); | ||
1676 | up(&sb_dqopt(sb)->dqonoff_sem); | ||
1677 | return 0; | ||
1678 | } | ||
1679 | |||
1680 | struct quotactl_ops vfs_quotactl_ops = { | ||
1681 | .quota_on = vfs_quota_on, | ||
1682 | .quota_off = vfs_quota_off, | ||
1683 | .quota_sync = vfs_quota_sync, | ||
1684 | .get_info = vfs_get_dqinfo, | ||
1685 | .set_info = vfs_set_dqinfo, | ||
1686 | .get_dqblk = vfs_get_dqblk, | ||
1687 | .set_dqblk = vfs_set_dqblk | ||
1688 | }; | ||
1689 | |||
1690 | static ctl_table fs_dqstats_table[] = { | ||
1691 | { | ||
1692 | .ctl_name = FS_DQ_LOOKUPS, | ||
1693 | .procname = "lookups", | ||
1694 | .data = &dqstats.lookups, | ||
1695 | .maxlen = sizeof(int), | ||
1696 | .mode = 0444, | ||
1697 | .proc_handler = &proc_dointvec, | ||
1698 | }, | ||
1699 | { | ||
1700 | .ctl_name = FS_DQ_DROPS, | ||
1701 | .procname = "drops", | ||
1702 | .data = &dqstats.drops, | ||
1703 | .maxlen = sizeof(int), | ||
1704 | .mode = 0444, | ||
1705 | .proc_handler = &proc_dointvec, | ||
1706 | }, | ||
1707 | { | ||
1708 | .ctl_name = FS_DQ_READS, | ||
1709 | .procname = "reads", | ||
1710 | .data = &dqstats.reads, | ||
1711 | .maxlen = sizeof(int), | ||
1712 | .mode = 0444, | ||
1713 | .proc_handler = &proc_dointvec, | ||
1714 | }, | ||
1715 | { | ||
1716 | .ctl_name = FS_DQ_WRITES, | ||
1717 | .procname = "writes", | ||
1718 | .data = &dqstats.writes, | ||
1719 | .maxlen = sizeof(int), | ||
1720 | .mode = 0444, | ||
1721 | .proc_handler = &proc_dointvec, | ||
1722 | }, | ||
1723 | { | ||
1724 | .ctl_name = FS_DQ_CACHE_HITS, | ||
1725 | .procname = "cache_hits", | ||
1726 | .data = &dqstats.cache_hits, | ||
1727 | .maxlen = sizeof(int), | ||
1728 | .mode = 0444, | ||
1729 | .proc_handler = &proc_dointvec, | ||
1730 | }, | ||
1731 | { | ||
1732 | .ctl_name = FS_DQ_ALLOCATED, | ||
1733 | .procname = "allocated_dquots", | ||
1734 | .data = &dqstats.allocated_dquots, | ||
1735 | .maxlen = sizeof(int), | ||
1736 | .mode = 0444, | ||
1737 | .proc_handler = &proc_dointvec, | ||
1738 | }, | ||
1739 | { | ||
1740 | .ctl_name = FS_DQ_FREE, | ||
1741 | .procname = "free_dquots", | ||
1742 | .data = &dqstats.free_dquots, | ||
1743 | .maxlen = sizeof(int), | ||
1744 | .mode = 0444, | ||
1745 | .proc_handler = &proc_dointvec, | ||
1746 | }, | ||
1747 | { | ||
1748 | .ctl_name = FS_DQ_SYNCS, | ||
1749 | .procname = "syncs", | ||
1750 | .data = &dqstats.syncs, | ||
1751 | .maxlen = sizeof(int), | ||
1752 | .mode = 0444, | ||
1753 | .proc_handler = &proc_dointvec, | ||
1754 | }, | ||
1755 | { | ||
1756 | .ctl_name = FS_DQ_WARNINGS, | ||
1757 | .procname = "warnings", | ||
1758 | .data = &flag_print_warnings, | ||
1759 | .maxlen = sizeof(int), | ||
1760 | .mode = 0644, | ||
1761 | .proc_handler = &proc_dointvec, | ||
1762 | }, | ||
1763 | { .ctl_name = 0 }, | ||
1764 | }; | ||
1765 | |||
1766 | static ctl_table fs_table[] = { | ||
1767 | { | ||
1768 | .ctl_name = FS_DQSTATS, | ||
1769 | .procname = "quota", | ||
1770 | .mode = 0555, | ||
1771 | .child = fs_dqstats_table, | ||
1772 | }, | ||
1773 | { .ctl_name = 0 }, | ||
1774 | }; | ||
1775 | |||
1776 | static ctl_table sys_table[] = { | ||
1777 | { | ||
1778 | .ctl_name = CTL_FS, | ||
1779 | .procname = "fs", | ||
1780 | .mode = 0555, | ||
1781 | .child = fs_table, | ||
1782 | }, | ||
1783 | { .ctl_name = 0 }, | ||
1784 | }; | ||
1785 | |||
1786 | static int __init dquot_init(void) | ||
1787 | { | ||
1788 | int i; | ||
1789 | unsigned long nr_hash, order; | ||
1790 | |||
1791 | printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__); | ||
1792 | |||
1793 | register_sysctl_table(sys_table, 0); | ||
1794 | |||
1795 | dquot_cachep = kmem_cache_create("dquot", | ||
1796 | sizeof(struct dquot), sizeof(unsigned long) * 4, | ||
1797 | SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_PANIC, | ||
1798 | NULL, NULL); | ||
1799 | |||
1800 | order = 0; | ||
1801 | dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order); | ||
1802 | if (!dquot_hash) | ||
1803 | panic("Cannot create dquot hash table"); | ||
1804 | |||
1805 | /* Find power-of-two hlist_heads which can fit into allocation */ | ||
1806 | nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head); | ||
1807 | dq_hash_bits = 0; | ||
1808 | do { | ||
1809 | dq_hash_bits++; | ||
1810 | } while (nr_hash >> dq_hash_bits); | ||
1811 | dq_hash_bits--; | ||
1812 | |||
1813 | nr_hash = 1UL << dq_hash_bits; | ||
1814 | dq_hash_mask = nr_hash - 1; | ||
1815 | for (i = 0; i < nr_hash; i++) | ||
1816 | INIT_HLIST_HEAD(dquot_hash + i); | ||
1817 | |||
1818 | printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n", | ||
1819 | nr_hash, order, (PAGE_SIZE << order)); | ||
1820 | |||
1821 | set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory); | ||
1822 | |||
1823 | return 0; | ||
1824 | } | ||
1825 | module_init(dquot_init); | ||
1826 | |||
1827 | EXPORT_SYMBOL(register_quota_format); | ||
1828 | EXPORT_SYMBOL(unregister_quota_format); | ||
1829 | EXPORT_SYMBOL(dqstats); | ||
1830 | EXPORT_SYMBOL(dq_data_lock); | ||
1831 | EXPORT_SYMBOL(vfs_quota_on); | ||
1832 | EXPORT_SYMBOL(vfs_quota_on_mount); | ||
1833 | EXPORT_SYMBOL(vfs_quota_off); | ||
1834 | EXPORT_SYMBOL(vfs_quota_sync); | ||
1835 | EXPORT_SYMBOL(vfs_get_dqinfo); | ||
1836 | EXPORT_SYMBOL(vfs_set_dqinfo); | ||
1837 | EXPORT_SYMBOL(vfs_get_dqblk); | ||
1838 | EXPORT_SYMBOL(vfs_set_dqblk); | ||
1839 | EXPORT_SYMBOL(dquot_commit); | ||
1840 | EXPORT_SYMBOL(dquot_commit_info); | ||
1841 | EXPORT_SYMBOL(dquot_acquire); | ||
1842 | EXPORT_SYMBOL(dquot_release); | ||
1843 | EXPORT_SYMBOL(dquot_mark_dquot_dirty); | ||
1844 | EXPORT_SYMBOL(dquot_initialize); | ||
1845 | EXPORT_SYMBOL(dquot_drop); | ||
1846 | EXPORT_SYMBOL(dquot_alloc_space); | ||
1847 | EXPORT_SYMBOL(dquot_alloc_inode); | ||
1848 | EXPORT_SYMBOL(dquot_free_space); | ||
1849 | EXPORT_SYMBOL(dquot_free_inode); | ||
1850 | EXPORT_SYMBOL(dquot_transfer); | ||