forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/gfs2/quota.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
34 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
4
- *
5
- * This copyrighted material is made available to anyone wishing to use,
6
- * modify, copy, or redistribute it subject to the terms and conditions
7
- * of the GNU General Public License version 2.
85 */
96
107 /*
....@@ -118,7 +115,7 @@
118115 struct gfs2_sbd *sdp;
119116
120117 while (!list_empty(list)) {
121
- qd = list_entry(list->next, struct gfs2_quota_data, qd_lru);
118
+ qd = list_first_entry(list, struct gfs2_quota_data, qd_lru);
122119 sdp = qd->qd_gl->gl_name.ln_sbd;
123120
124121 list_del(&qd->qd_lru);
....@@ -528,36 +525,48 @@
528525 }
529526
530527 /**
531
- * gfs2_qa_alloc - make sure we have a quota allocations data structure,
532
- * if necessary
528
+ * gfs2_qa_get - make sure we have a quota allocations data structure,
529
+ * if necessary
533530 * @ip: the inode for this reservation
534531 */
535
-int gfs2_qa_alloc(struct gfs2_inode *ip)
532
+int gfs2_qa_get(struct gfs2_inode *ip)
536533 {
537
- int error = 0;
538534 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
535
+ struct inode *inode = &ip->i_inode;
539536
540537 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
541538 return 0;
542539
543
- down_write(&ip->i_rw_mutex);
540
+ spin_lock(&inode->i_lock);
544541 if (ip->i_qadata == NULL) {
545
- ip->i_qadata = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS);
546
- if (!ip->i_qadata)
547
- error = -ENOMEM;
542
+ struct gfs2_qadata *tmp;
543
+
544
+ spin_unlock(&inode->i_lock);
545
+ tmp = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS);
546
+ if (!tmp)
547
+ return -ENOMEM;
548
+
549
+ spin_lock(&inode->i_lock);
550
+ if (ip->i_qadata == NULL)
551
+ ip->i_qadata = tmp;
552
+ else
553
+ kmem_cache_free(gfs2_qadata_cachep, tmp);
548554 }
549
- up_write(&ip->i_rw_mutex);
550
- return error;
555
+ ip->i_qadata->qa_ref++;
556
+ spin_unlock(&inode->i_lock);
557
+ return 0;
551558 }
552559
553
-void gfs2_qa_delete(struct gfs2_inode *ip, atomic_t *wcount)
560
+void gfs2_qa_put(struct gfs2_inode *ip)
554561 {
555
- down_write(&ip->i_rw_mutex);
556
- if (ip->i_qadata && ((wcount == NULL) || (atomic_read(wcount) <= 1))) {
562
+ struct inode *inode = &ip->i_inode;
563
+
564
+ spin_lock(&inode->i_lock);
565
+ if (ip->i_qadata && --ip->i_qadata->qa_ref == 0) {
557566 kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
558567 ip->i_qadata = NULL;
559568 }
560
- up_write(&ip->i_rw_mutex);
569
+ spin_unlock(&inode->i_lock);
561570 }
562571
563572 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
....@@ -569,27 +578,27 @@
569578 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
570579 return 0;
571580
572
- if (ip->i_qadata == NULL) {
573
- error = gfs2_rsqa_alloc(ip);
574
- if (error)
575
- return error;
576
- }
581
+ error = gfs2_qa_get(ip);
582
+ if (error)
583
+ return error;
577584
578585 qd = ip->i_qadata->qa_qd;
579586
580587 if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) ||
581
- gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
582
- return -EIO;
588
+ gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) {
589
+ error = -EIO;
590
+ goto out;
591
+ }
583592
584593 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
585594 if (error)
586
- goto out;
595
+ goto out_unhold;
587596 ip->i_qadata->qa_qd_num++;
588597 qd++;
589598
590599 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
591600 if (error)
592
- goto out;
601
+ goto out_unhold;
593602 ip->i_qadata->qa_qd_num++;
594603 qd++;
595604
....@@ -597,7 +606,7 @@
597606 !uid_eq(uid, ip->i_inode.i_uid)) {
598607 error = qdsb_get(sdp, make_kqid_uid(uid), qd);
599608 if (error)
600
- goto out;
609
+ goto out_unhold;
601610 ip->i_qadata->qa_qd_num++;
602611 qd++;
603612 }
....@@ -606,14 +615,15 @@
606615 !gid_eq(gid, ip->i_inode.i_gid)) {
607616 error = qdsb_get(sdp, make_kqid_gid(gid), qd);
608617 if (error)
609
- goto out;
618
+ goto out_unhold;
610619 ip->i_qadata->qa_qd_num++;
611620 qd++;
612621 }
613622
614
-out:
623
+out_unhold:
615624 if (error)
616625 gfs2_quota_unhold(ip);
626
+out:
617627 return error;
618628 }
619629
....@@ -624,6 +634,7 @@
624634
625635 if (ip->i_qadata == NULL)
626636 return;
637
+
627638 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
628639
629640 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
....@@ -631,6 +642,7 @@
631642 ip->i_qadata->qa_qd[x] = NULL;
632643 }
633644 ip->i_qadata->qa_qd_num = 0;
645
+ gfs2_qa_put(ip);
634646 }
635647
636648 static int sort_qd(const void *a, const void *b)
....@@ -777,7 +789,7 @@
777789 nbytes = sizeof(struct gfs2_quota);
778790
779791 pg_beg = loc >> PAGE_SHIFT;
780
- pg_off = loc % PAGE_SIZE;
792
+ pg_off = offset_in_page(loc);
781793
782794 /* If the quota straddles a page boundary, split the write in two */
783795 if ((pg_off + nbytes) > PAGE_SIZE) {
....@@ -879,7 +891,7 @@
879891 unsigned int nalloc = 0, blocks;
880892 int error;
881893
882
- error = gfs2_rsqa_alloc(ip);
894
+ error = gfs2_qa_get(ip);
883895 if (error)
884896 return error;
885897
....@@ -887,8 +899,10 @@
887899 &data_blocks, &ind_blocks);
888900
889901 ghs = kmalloc_array(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
890
- if (!ghs)
891
- return -ENOMEM;
902
+ if (!ghs) {
903
+ error = -ENOMEM;
904
+ goto out;
905
+ }
892906
893907 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
894908 inode_lock(&ip->i_inode);
....@@ -896,12 +910,12 @@
896910 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
897911 GL_NOCACHE, &ghs[qx]);
898912 if (error)
899
- goto out;
913
+ goto out_dq;
900914 }
901915
902916 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
903917 if (error)
904
- goto out;
918
+ goto out_dq;
905919
906920 for (x = 0; x < num_qd; x++) {
907921 offset = qd2offset(qda[x]);
....@@ -953,13 +967,15 @@
953967 gfs2_inplace_release(ip);
954968 out_alloc:
955969 gfs2_glock_dq_uninit(&i_gh);
956
-out:
970
+out_dq:
957971 while (qx--)
958972 gfs2_glock_dq_uninit(&ghs[qx]);
959973 inode_unlock(&ip->i_inode);
960974 kfree(ghs);
961975 gfs2_log_flush(ip->i_gl->gl_name.ln_sbd, ip->i_gl,
962976 GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC);
977
+out:
978
+ gfs2_qa_put(ip);
963979 return error;
964980 }
965981
....@@ -1116,7 +1132,7 @@
11161132 int found;
11171133
11181134 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
1119
- goto out;
1135
+ return;
11201136
11211137 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
11221138 struct gfs2_quota_data *qd;
....@@ -1153,7 +1169,6 @@
11531169 qd_unlock(qda[x]);
11541170 }
11551171
1156
-out:
11571172 gfs2_quota_unhold(ip);
11581173 }
11591174
....@@ -1182,7 +1197,7 @@
11821197 *
11831198 * Returns: 0 on success.
11841199 * min_req = ap->min_target ? ap->min_target : ap->target;
1185
- * quota must allow atleast min_req blks for success and
1200
+ * quota must allow at least min_req blks for success and
11861201 * ap->allowed is set to the number of blocks allowed
11871202 *
11881203 * -EDQUOT otherwise, quota violation. ap->allowed is set to number
....@@ -1200,9 +1215,6 @@
12001215 ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */
12011216 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
12021217 return 0;
1203
-
1204
- if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
1205
- return 0;
12061218
12071219 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
12081220 qd = ip->i_qadata->qa_qd[x];
....@@ -1261,6 +1273,9 @@
12611273 if (ip->i_diskflags & GFS2_DIF_SYSTEM)
12621274 return;
12631275
1276
+ if (gfs2_assert_withdraw(sdp, ip->i_qadata &&
1277
+ ip->i_qadata->qa_ref > 0))
1278
+ return;
12641279 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
12651280 qd = ip->i_qadata->qa_qd[x];
12661281
....@@ -1275,7 +1290,7 @@
12751290 {
12761291 struct gfs2_sbd *sdp = sb->s_fs_info;
12771292 struct gfs2_quota_data **qda;
1278
- unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
1293
+ unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder);
12791294 unsigned int num_qd;
12801295 unsigned int x;
12811296 int error = 0;
....@@ -1358,7 +1373,7 @@
13581373 sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
13591374 if (sdp->sd_quota_bitmap == NULL)
13601375 sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
1361
- __GFP_ZERO, PAGE_KERNEL);
1376
+ __GFP_ZERO);
13621377 if (!sdp->sd_quota_bitmap)
13631378 return error;
13641379
....@@ -1443,7 +1458,7 @@
14431458
14441459 spin_lock(&qd_lock);
14451460 while (!list_empty(head)) {
1446
- qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
1461
+ qd = list_last_entry(head, struct gfs2_quota_data, qd_list);
14471462
14481463 list_del(&qd->qd_list);
14491464
....@@ -1477,9 +1492,9 @@
14771492 {
14781493 if (error == 0 || error == -EROFS)
14791494 return;
1480
- if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
1481
- fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
1482
- sdp->sd_log_error = error;
1495
+ if (!gfs2_withdrawn(sdp)) {
1496
+ if (!cmpxchg(&sdp->sd_log_error, 0, error))
1497
+ fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
14831498 wake_up(&sdp->sd_logd_waitq);
14841499 }
14851500 }
....@@ -1506,7 +1521,7 @@
15061521 ip = NULL;
15071522 spin_lock(&sdp->sd_trunc_lock);
15081523 if (!list_empty(&sdp->sd_trunc_list)) {
1509
- ip = list_entry(sdp->sd_trunc_list.next,
1524
+ ip = list_first_entry(&sdp->sd_trunc_list,
15101525 struct gfs2_inode, i_trunc_list);
15111526 list_del_init(&ip->i_trunc_list);
15121527 }
....@@ -1543,6 +1558,8 @@
15431558
15441559 while (!kthread_should_stop()) {
15451560
1561
+ if (gfs2_withdrawn(sdp))
1562
+ goto bypass;
15461563 /* Update the master statfs file */
15471564 if (sdp->sd_statfs_force_sync) {
15481565 int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
....@@ -1563,6 +1580,7 @@
15631580
15641581 try_to_freeze();
15651582
1583
+bypass:
15661584 t = min(quotad_timeo, statfs_timeo);
15671585
15681586 prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
....@@ -1589,7 +1607,7 @@
15891607 case GFS2_QUOTA_ON:
15901608 state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
15911609 state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
1592
- /*FALLTHRU*/
1610
+ fallthrough;
15931611 case GFS2_QUOTA_ACCOUNT:
15941612 state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED |
15951613 QCI_SYSFILE;
....@@ -1676,7 +1694,7 @@
16761694 if (error)
16771695 return error;
16781696
1679
- error = gfs2_rsqa_alloc(ip);
1697
+ error = gfs2_qa_get(ip);
16801698 if (error)
16811699 goto out_put;
16821700
....@@ -1745,6 +1763,7 @@
17451763 out_q:
17461764 gfs2_glock_dq_uninit(&q_gh);
17471765 out_unlockput:
1766
+ gfs2_qa_put(ip);
17481767 inode_unlock(&ip->i_inode);
17491768 out_put:
17501769 qd_put(qd);