]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/ceph/quota.c
cf1c78c4a4d29ac574afda868b8e53f5321c714b
[mirror_ubuntu-bionic-kernel.git] / fs / ceph / quota.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * quota.c - CephFS quota
4 *
5 * Copyright (C) 2017-2018 SUSE
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "super.h"
22 #include "mds_client.h"
23
24 void ceph_handle_quota(struct ceph_mds_client *mdsc,
25 struct ceph_mds_session *session,
26 struct ceph_msg *msg)
27 {
28 struct super_block *sb = mdsc->fsc->sb;
29 struct ceph_mds_quota *h = msg->front.iov_base;
30 struct ceph_vino vino;
31 struct inode *inode;
32 struct ceph_inode_info *ci;
33
34 if (msg->front.iov_len != sizeof(*h)) {
35 pr_err("%s corrupt message mds%d len %d\n", __func__,
36 session->s_mds, (int)msg->front.iov_len);
37 ceph_msg_dump(msg);
38 return;
39 }
40
41 /* increment msg sequence number */
42 mutex_lock(&session->s_mutex);
43 session->s_seq++;
44 mutex_unlock(&session->s_mutex);
45
46 /* lookup inode */
47 vino.ino = le64_to_cpu(h->ino);
48 vino.snap = CEPH_NOSNAP;
49 inode = ceph_find_inode(sb, vino);
50 if (!inode) {
51 pr_warn("Failed to find inode %llu\n", vino.ino);
52 return;
53 }
54 ci = ceph_inode(inode);
55
56 spin_lock(&ci->i_ceph_lock);
57 ci->i_rbytes = le64_to_cpu(h->rbytes);
58 ci->i_rfiles = le64_to_cpu(h->rfiles);
59 ci->i_rsubdirs = le64_to_cpu(h->rsubdirs);
60 ci->i_max_bytes = le64_to_cpu(h->max_bytes);
61 ci->i_max_files = le64_to_cpu(h->max_files);
62 spin_unlock(&ci->i_ceph_lock);
63
64 iput(inode);
65 }
66
67 enum quota_check_op {
68 QUOTA_CHECK_MAX_FILES_OP /* check quota max_files limit */
69 };
70
71 /*
72 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
73 * realm, it will execute quota check operation defined by the 'op' parameter.
74 * The snaprealm walk is interrupted if the quota check detects that the quota
75 * is exceeded or if the root inode is reached.
76 */
77 static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
78 loff_t delta)
79 {
80 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
81 struct ceph_inode_info *ci;
82 struct ceph_snap_realm *realm, *next;
83 struct ceph_vino vino;
84 struct inode *in;
85 u64 max, rvalue;
86 bool is_root;
87 bool exceeded = false;
88
89 down_read(&mdsc->snap_rwsem);
90 realm = ceph_inode(inode)->i_snap_realm;
91 ceph_get_snap_realm(mdsc, realm);
92 while (realm) {
93 vino.ino = realm->ino;
94 vino.snap = CEPH_NOSNAP;
95 in = ceph_find_inode(inode->i_sb, vino);
96 if (!in) {
97 pr_warn("Failed to find inode for %llu\n", vino.ino);
98 break;
99 }
100 ci = ceph_inode(in);
101 spin_lock(&ci->i_ceph_lock);
102 if (op == QUOTA_CHECK_MAX_FILES_OP) {
103 max = ci->i_max_files;
104 rvalue = ci->i_rfiles + ci->i_rsubdirs;
105 }
106 is_root = (ci->i_vino.ino == CEPH_INO_ROOT);
107 spin_unlock(&ci->i_ceph_lock);
108 switch (op) {
109 case QUOTA_CHECK_MAX_FILES_OP:
110 exceeded = (max && (rvalue >= max));
111 break;
112 default:
113 /* Shouldn't happen */
114 pr_warn("Invalid quota check op (%d)\n", op);
115 exceeded = true; /* Just break the loop */
116 }
117 iput(in);
118
119 if (is_root || exceeded)
120 break;
121 next = realm->parent;
122 ceph_get_snap_realm(mdsc, next);
123 ceph_put_snap_realm(mdsc, realm);
124 realm = next;
125 }
126 ceph_put_snap_realm(mdsc, realm);
127 up_read(&mdsc->snap_rwsem);
128
129 return exceeded;
130 }
131
132 /*
133 * ceph_quota_is_max_files_exceeded - check if we can create a new file
134 * @inode: directory where a new file is being created
135 *
136 * This functions returns true is max_files quota allows a new file to be
137 * created. It is necessary to walk through the snaprealm hierarchy (until the
138 * FS root) to check all realms with quotas set.
139 */
140 bool ceph_quota_is_max_files_exceeded(struct inode *inode)
141 {
142 WARN_ON(!S_ISDIR(inode->i_mode));
143
144 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
145 }