1 diff -urp linux-3.0.61-0.orig/fs/quota/dquot.c linux-3.0.61-0/fs/quota/dquot.c
2 --- linux-3.0.61-0.orig/fs/quota/dquot.c 2013-04-10 15:15:11.000000000 -0400
3 +++ linux-3.0.61-0/fs/quota/dquot.c 2013-04-24 10:27:22.000000000 -0400
6 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
8 - * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
9 - * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
10 - * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
11 - * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
12 - * modifications of quota state (on quotaon and quotaoff) and readers who care
13 - * about latest values take it as well.
14 + * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures.
15 + * dq_state_lock protects modifications of quota state (on quotaon and quotaoff)
16 + * and readers who care about latest values take it as well.
18 - * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
19 + * The spinlock ordering is hence: i_lock > dq_data_lock > dq_list_lock,
20 * dq_list_lock > dq_state_lock
22 * Note that some things (eg. sb pointer, type, id) doesn't change during
23 * the life of the dquot structure and so needn't to be protected by a lock
25 - * Any operation working on dquots via inode pointers must hold dqptr_sem. If
26 - * operation is just reading pointers from inode (or not using them at all) the
27 - * read lock is enough. If pointers are altered function must hold write lock.
28 + * Any operation working on dquots via inode pointers must hold i_lock.
29 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
30 * inode is a quota file). Functions adding pointers from inode to dquots have
31 - * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
32 - * have to do all pointer modifications before dropping dqptr_sem. This makes
33 + * to check this flag under i_lock and then (if S_NOQUOTA is not set) they
34 + * have to do all pointer modifications before dropping i_lock. This makes
35 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
36 * then drops all pointers to dquots from an inode.
39 * spinlock to internal buffers before writing.
41 * Lock ordering (including related VFS locks) is the following:
42 - * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
44 - * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
45 - * dqptr_sem. But filesystem has to count with the fact that functions such as
46 - * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
47 - * from inside a transaction to keep filesystem consistency after a crash. Also
48 - * filesystems usually want to do some IO on dquot from ->mark_dirty which is
49 - * called with dqptr_sem held.
50 - * i_mutex on quota files is special (it's below dqio_mutex)
51 + * i_mutex > dqonoff_sem > journal_lock > dquot->dq_lock > dqio_mutex
52 + * i_mutex on quota files is special (it's below dqio_mutex)
55 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
56 @@ -955,7 +943,6 @@ static inline int dqput_blocks(struct dq
58 * Remove references to dquots from inode and add dquot to list for freeing
59 * if we have the last reference to dquot
60 - * We can't race with anybody because we hold dqptr_sem for writing...
62 static int remove_inode_dquot_ref(struct inode *inode, int type,
63 struct list_head *tofree_head)
64 @@ -1016,13 +1003,15 @@ static void remove_dquot_ref(struct supe
65 * We have to scan also I_NEW inodes because they can already
66 * have quota pointer initialized. Luckily, we need to touch
67 * only quota pointers and these have separate locking
71 + spin_lock(&inode->i_lock);
72 if (!IS_NOQUOTA(inode)) {
73 if (unlikely(inode_get_rsv_space(inode) > 0))
75 remove_inode_dquot_ref(inode, type, tofree_head);
77 + spin_unlock(&inode->i_lock);
79 spin_unlock(&inode_sb_list_lock);
80 #ifdef CONFIG_QUOTA_DEBUG
81 @@ -1040,9 +1029,7 @@ static void drop_dquot_ref(struct super_
82 LIST_HEAD(tofree_head);
85 - down_write(&sb_dqopt(sb)->dqptr_sem);
86 remove_dquot_ref(sb, type, &tofree_head);
87 - up_write(&sb_dqopt(sb)->dqptr_sem);
88 put_dquot_list(&tofree_head);
91 @@ -1349,9 +1336,6 @@ static int dquot_active(const struct ino
93 * Initialize quota pointers in inode
95 - * We do things in a bit complicated way but by that we avoid calling
96 - * dqget() and thus filesystem callbacks under dqptr_sem.
98 * It is better to call this function outside of any transaction as it
99 * might need a lot of space in journal for dquot structure allocation.
101 @@ -1384,7 +1368,7 @@ static void __dquot_initialize(struct in
102 got[cnt] = dqget(sb, id, cnt);
105 - down_write(&sb_dqopt(sb)->dqptr_sem);
106 + spin_lock(&inode->i_lock);
107 if (IS_NOQUOTA(inode))
109 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
110 @@ -1404,12 +1388,16 @@ static void __dquot_initialize(struct in
111 * did a write before quota was turned on
113 rsv = inode_get_rsv_space(inode);
115 + if (unlikely(rsv)) {
116 + spin_lock(&dq_data_lock);
117 dquot_resv_space(inode->i_dquot[cnt], rsv);
118 + spin_unlock(&dq_data_lock);
123 - up_write(&sb_dqopt(sb)->dqptr_sem);
124 + spin_unlock(&inode->i_lock);
126 /* Drop unused references */
129 @@ -1428,12 +1416,12 @@ static void __dquot_drop(struct inode *i
131 struct dquot *put[MAXQUOTAS];
133 - down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
134 + spin_lock(&inode->i_lock);
135 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
136 put[cnt] = inode->i_dquot[cnt];
137 inode->i_dquot[cnt] = NULL;
139 - up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
140 + spin_unlock(&inode->i_lock);
144 @@ -1473,27 +1461,42 @@ static qsize_t *inode_reserved_space(str
145 return inode->i_sb->dq_op->get_reserved_space(inode);
148 +static inline void __inode_add_rsv_space(struct inode *inode, qsize_t number)
150 + *inode_reserved_space(inode) += number;
153 void inode_add_rsv_space(struct inode *inode, qsize_t number)
155 spin_lock(&inode->i_lock);
156 - *inode_reserved_space(inode) += number;
157 + __inode_add_rsv_space(inode, number);
158 spin_unlock(&inode->i_lock);
160 EXPORT_SYMBOL(inode_add_rsv_space);
162 -void inode_claim_rsv_space(struct inode *inode, qsize_t number)
163 +static inline void __inode_claim_rsv_space(struct inode *inode, qsize_t number)
165 - spin_lock(&inode->i_lock);
166 *inode_reserved_space(inode) -= number;
167 __inode_add_bytes(inode, number);
170 +void inode_claim_rsv_space(struct inode *inode, qsize_t number)
172 + spin_lock(&inode->i_lock);
173 + __inode_claim_rsv_space(inode, number);
174 spin_unlock(&inode->i_lock);
176 EXPORT_SYMBOL(inode_claim_rsv_space);
178 +static inline void __inode_sub_rsv_space(struct inode *inode, qsize_t number)
180 + *inode_reserved_space(inode) -= number;
183 void inode_sub_rsv_space(struct inode *inode, qsize_t number)
185 spin_lock(&inode->i_lock);
186 - *inode_reserved_space(inode) -= number;
187 + __inode_sub_rsv_space(inode, number);
188 spin_unlock(&inode->i_lock);
190 EXPORT_SYMBOL(inode_sub_rsv_space);
191 @@ -1504,9 +1507,8 @@ static qsize_t inode_get_rsv_space(struc
193 if (!inode->i_sb->dq_op->get_reserved_space)
195 - spin_lock(&inode->i_lock);
197 ret = *inode_reserved_space(inode);
198 - spin_unlock(&inode->i_lock);
202 @@ -1514,17 +1516,17 @@ static void inode_incr_space(struct inod
206 - inode_add_rsv_space(inode, number);
207 + __inode_add_rsv_space(inode, number);
209 - inode_add_bytes(inode, number);
210 + __inode_add_bytes(inode, number);
213 static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
216 - inode_sub_rsv_space(inode, number);
217 + __inode_sub_rsv_space(inode, number);
219 - inode_sub_bytes(inode, number);
220 + __inode_sub_bytes(inode, number);
224 @@ -1547,6 +1549,7 @@ int __dquot_alloc_space(struct inode *in
225 int warn = flags & DQUOT_SPACE_WARN;
226 int reserve = flags & DQUOT_SPACE_RESERVE;
227 int nofail = flags & DQUOT_SPACE_NOFAIL;
228 + struct dquot *dquot[MAXQUOTAS] = { NULL };
231 * First test before acquiring mutex - solves deadlocks when we
232 @@ -1557,15 +1560,17 @@ int __dquot_alloc_space(struct inode *in
236 - down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
237 + spin_lock(&inode->i_lock);
238 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
239 warntype[cnt] = QUOTA_NL_NOWARN;
241 spin_lock(&dq_data_lock);
242 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
243 - if (!inode->i_dquot[cnt])
244 + dquot[cnt] = inode->i_dquot[cnt];
247 - ret = check_bdq(inode->i_dquot[cnt], number, !warn,
248 + atomic_inc(&dquot[cnt]->dq_count);
249 + ret = check_bdq(dquot[cnt], number, !warn,
251 if (ret && !nofail) {
252 spin_unlock(&dq_data_lock);
253 @@ -1573,22 +1578,23 @@ int __dquot_alloc_space(struct inode *in
256 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
257 - if (!inode->i_dquot[cnt])
261 - dquot_resv_space(inode->i_dquot[cnt], number);
262 + dquot_resv_space(dquot[cnt], number);
264 - dquot_incr_space(inode->i_dquot[cnt], number);
265 + dquot_incr_space(dquot[cnt], number);
267 inode_incr_space(inode, number, reserve);
268 spin_unlock(&dq_data_lock);
269 + spin_unlock(&inode->i_lock);
273 - mark_all_dquot_dirty(inode->i_dquot);
274 + mark_all_dquot_dirty(dquot);
276 - flush_warnings(inode->i_dquot, warntype);
277 - up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
278 + flush_warnings(dquot, warntype);
283 @@ -1601,6 +1607,7 @@ int dquot_alloc_inode(const struct inode
286 char warntype[MAXQUOTAS];
287 + struct dquot *dquot[MAXQUOTAS] = { NULL };
289 /* First test before acquiring mutex - solves deadlocks when we
290 * re-enter the quota code and are already holding the mutex */
291 @@ -1608,28 +1615,33 @@ int dquot_alloc_inode(const struct inode
293 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
294 warntype[cnt] = QUOTA_NL_NOWARN;
295 - down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
297 + spin_lock(&((struct inode *)inode)->i_lock);
298 spin_lock(&dq_data_lock);
299 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
300 - if (!inode->i_dquot[cnt])
301 + dquot[cnt] = inode->i_dquot[cnt];
304 - ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
305 + atomic_inc(&dquot[cnt]->dq_count);
306 + ret = check_idq(dquot[cnt], 1, warntype + cnt);
311 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
312 - if (!inode->i_dquot[cnt])
315 - dquot_incr_inodes(inode->i_dquot[cnt], 1);
316 + dquot_incr_inodes(dquot[cnt], 1);
320 spin_unlock(&dq_data_lock);
321 + spin_unlock(&((struct inode *)inode)->i_lock);
324 - mark_all_dquot_dirty(inode->i_dquot);
325 - flush_warnings(inode->i_dquot, warntype);
326 - up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
327 + mark_all_dquot_dirty(dquot);
328 + flush_warnings(dquot, warntype);
332 EXPORT_SYMBOL(dquot_alloc_inode);
333 @@ -1639,6 +1651,7 @@ EXPORT_SYMBOL(dquot_alloc_inode);
335 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
337 + struct dquot *dquot[MAXQUOTAS] = { NULL };
340 if (!dquot_active(inode)) {
341 @@ -1646,19 +1659,23 @@ int dquot_claim_space_nodirty(struct ino
345 - down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
346 + spin_lock(&inode->i_lock);
347 spin_lock(&dq_data_lock);
348 /* Claim reserved quotas to allocated quotas */
349 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
350 - if (inode->i_dquot[cnt])
351 - dquot_claim_reserved_space(inode->i_dquot[cnt],
353 + dquot[cnt] = inode->i_dquot[cnt];
355 + atomic_inc(&dquot[cnt]->dq_count);
356 + dquot_claim_reserved_space(dquot[cnt], number);
359 /* Update inode bytes */
360 - inode_claim_rsv_space(inode, number);
361 + __inode_claim_rsv_space(inode, number);
362 spin_unlock(&dq_data_lock);
363 - mark_all_dquot_dirty(inode->i_dquot);
364 - up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
365 + spin_unlock(&inode->i_lock);
367 + mark_all_dquot_dirty(dquot);
371 EXPORT_SYMBOL(dquot_claim_space_nodirty);
372 @@ -1671,6 +1688,7 @@ void __dquot_free_space(struct inode *in
374 char warntype[MAXQUOTAS];
375 int reserve = flags & DQUOT_SPACE_RESERVE;
376 + struct dquot *dquot[MAXQUOTAS] = { NULL };
378 /* First test before acquiring mutex - solves deadlocks when we
379 * re-enter the quota code and are already holding the mutex */
380 @@ -1679,26 +1697,29 @@ void __dquot_free_space(struct inode *in
384 - down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
385 + spin_lock(&inode->i_lock);
386 spin_lock(&dq_data_lock);
387 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
388 - if (!inode->i_dquot[cnt])
389 + dquot[cnt] = inode->i_dquot[cnt];
392 - warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
393 + atomic_inc(&dquot[cnt]->dq_count);
394 + warntype[cnt] = info_bdq_free(dquot[cnt], number);
396 - dquot_free_reserved_space(inode->i_dquot[cnt], number);
397 + dquot_free_reserved_space(dquot[cnt], number);
399 - dquot_decr_space(inode->i_dquot[cnt], number);
400 + dquot_decr_space(dquot[cnt], number);
402 inode_decr_space(inode, number, reserve);
403 spin_unlock(&dq_data_lock);
404 + spin_unlock(&inode->i_lock);
408 - mark_all_dquot_dirty(inode->i_dquot);
409 + mark_all_dquot_dirty(dquot);
411 - flush_warnings(inode->i_dquot, warntype);
412 - up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
413 + flush_warnings(dquot, warntype);
416 EXPORT_SYMBOL(__dquot_free_space);
418 @@ -1707,26 +1728,31 @@ EXPORT_SYMBOL(__dquot_free_space);
420 void dquot_free_inode(const struct inode *inode)
423 + struct dquot *dquot[MAXQUOTAS] = { NULL };
424 char warntype[MAXQUOTAS];
427 /* First test before acquiring mutex - solves deadlocks when we
428 * re-enter the quota code and are already holding the mutex */
429 if (!dquot_active(inode))
432 - down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
433 + spin_lock(&((struct inode *)inode)->i_lock);
434 spin_lock(&dq_data_lock);
435 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
436 - if (!inode->i_dquot[cnt])
437 + dquot[cnt] = inode->i_dquot[cnt];
440 - warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
441 - dquot_decr_inodes(inode->i_dquot[cnt], 1);
442 + atomic_inc(&dquot[cnt]->dq_count);
443 + warntype[cnt] = info_idq_free(dquot[cnt], 1);
444 + dquot_decr_inodes(dquot[cnt], 1);
446 spin_unlock(&dq_data_lock);
447 - mark_all_dquot_dirty(inode->i_dquot);
448 - flush_warnings(inode->i_dquot, warntype);
449 - up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
450 + spin_unlock(&((struct inode *)inode)->i_lock);
452 + mark_all_dquot_dirty(dquot);
453 + flush_warnings(dquot, warntype);
456 EXPORT_SYMBOL(dquot_free_inode);
458 @@ -1757,13 +1783,13 @@ int __dquot_transfer(struct inode *inode
459 /* Initialize the arrays */
460 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
461 warntype_to[cnt] = QUOTA_NL_NOWARN;
462 - down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
463 + spin_lock(&inode->i_lock);
464 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
465 - up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
466 + spin_unlock(&inode->i_lock);
469 spin_lock(&dq_data_lock);
470 - cur_space = inode_get_bytes(inode);
471 + cur_space = __inode_get_bytes(inode);
472 rsv_space = inode_get_rsv_space(inode);
473 space = cur_space + rsv_space;
474 /* Build the transfer_from list and check the limits */
475 @@ -1811,7 +1837,7 @@ int __dquot_transfer(struct inode *inode
476 inode->i_dquot[cnt] = transfer_to[cnt];
478 spin_unlock(&dq_data_lock);
479 - up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
480 + spin_unlock(&inode->i_lock);
482 mark_all_dquot_dirty(transfer_from);
483 mark_all_dquot_dirty(transfer_to);
484 @@ -1825,7 +1851,7 @@ int __dquot_transfer(struct inode *inode
487 spin_unlock(&dq_data_lock);
488 - up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
489 + spin_unlock(&inode->i_lock);
490 flush_warnings(transfer_to, warntype_to);
493 diff -urp linux-3.0.61-0.orig/fs/quota/quota.c linux-3.0.61-0/fs/quota/quota.c
494 --- linux-3.0.61-0.orig/fs/quota/quota.c 2013-04-10 15:15:08.000000000 -0400
495 +++ linux-3.0.61-0/fs/quota/quota.c 2013-04-24 10:27:22.000000000 -0400
496 @@ -79,13 +79,13 @@ static int quota_getfmt(struct super_blo
500 - down_read(&sb_dqopt(sb)->dqptr_sem);
501 + mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
502 if (!sb_has_quota_active(sb, type)) {
503 - up_read(&sb_dqopt(sb)->dqptr_sem);
504 + mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
507 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
508 - up_read(&sb_dqopt(sb)->dqptr_sem);
509 + mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
510 if (copy_to_user(addr, &fmt, sizeof(fmt)))
513 diff -urp linux-3.0.61-0.orig/fs/stat.c linux-3.0.61-0/fs/stat.c
514 --- linux-3.0.61-0.orig/fs/stat.c 2013-04-10 15:15:08.000000000 -0400
515 +++ linux-3.0.61-0/fs/stat.c 2013-04-24 10:27:22.000000000 -0400
516 @@ -435,9 +435,8 @@ void inode_add_bytes(struct inode *inode
518 EXPORT_SYMBOL(inode_add_bytes);
520 -void inode_sub_bytes(struct inode *inode, loff_t bytes)
521 +void __inode_sub_bytes(struct inode *inode, loff_t bytes)
523 - spin_lock(&inode->i_lock);
524 inode->i_blocks -= bytes >> 9;
526 if (inode->i_bytes < bytes) {
527 @@ -445,17 +444,28 @@ void inode_sub_bytes(struct inode *inode
528 inode->i_bytes += 512;
530 inode->i_bytes -= bytes;
533 +void inode_sub_bytes(struct inode *inode, loff_t bytes)
535 + spin_lock(&inode->i_lock);
536 + __inode_sub_bytes(inode, bytes);
537 spin_unlock(&inode->i_lock);
540 EXPORT_SYMBOL(inode_sub_bytes);
542 +loff_t __inode_get_bytes(struct inode *inode)
544 + return (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
547 loff_t inode_get_bytes(struct inode *inode)
551 spin_lock(&inode->i_lock);
552 - ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
553 + ret = __inode_get_bytes(inode);
554 spin_unlock(&inode->i_lock);
557 diff -urp linux-3.0.61-0.orig/fs/super.c linux-3.0.61-0/fs/super.c
558 --- linux-3.0.61-0.orig/fs/super.c 2013-04-10 15:15:08.000000000 -0400
559 +++ linux-3.0.61-0/fs/super.c 2013-04-24 10:27:22.000000000 -0400
560 @@ -108,7 +108,6 @@ static struct super_block *alloc_super(s
561 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
562 mutex_init(&s->s_dquot.dqio_mutex);
563 mutex_init(&s->s_dquot.dqonoff_mutex);
564 - init_rwsem(&s->s_dquot.dqptr_sem);
565 init_waitqueue_head(&s->s_wait_unfrozen);
566 s->s_maxbytes = MAX_NON_LFS;
567 s->s_op = &default_op;
568 diff -urp linux-3.0.61-0.orig/include/linux/fs.h linux-3.0.61-0/include/linux/fs.h
569 --- linux-3.0.61-0.orig/include/linux/fs.h 2013-04-24 10:27:55.000000000 -0400
570 +++ linux-3.0.61-0/include/linux/fs.h 2013-04-22 17:42:39.000000000 -0400
571 @@ -2450,7 +2450,9 @@ extern void generic_fillattr(struct inod
572 extern int vfs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
573 void __inode_add_bytes(struct inode *inode, loff_t bytes);
574 void inode_add_bytes(struct inode *inode, loff_t bytes);
575 +void __inode_sub_bytes(struct inode *inode, loff_t bytes);
576 void inode_sub_bytes(struct inode *inode, loff_t bytes);
577 +loff_t __inode_get_bytes(struct inode *inode);
578 loff_t inode_get_bytes(struct inode *inode);
579 void inode_set_bytes(struct inode *inode, loff_t bytes);