]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/ceph/quota.c
1 // SPDX-License-Identifier: GPL-2.0
3 * quota.c - CephFS quota
5 * Copyright (C) 2017-2018 SUSE
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.
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.
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/>.
22 #include "mds_client.h"
24 static inline bool ceph_has_quota(struct ceph_inode_info
*ci
)
26 return (ci
&& (ci
->i_max_files
|| ci
->i_max_bytes
));
29 void ceph_handle_quota(struct ceph_mds_client
*mdsc
,
30 struct ceph_mds_session
*session
,
33 struct super_block
*sb
= mdsc
->fsc
->sb
;
34 struct ceph_mds_quota
*h
= msg
->front
.iov_base
;
35 struct ceph_vino vino
;
37 struct ceph_inode_info
*ci
;
39 if (msg
->front
.iov_len
!= sizeof(*h
)) {
40 pr_err("%s corrupt message mds%d len %d\n", __func__
,
41 session
->s_mds
, (int)msg
->front
.iov_len
);
46 /* increment msg sequence number */
47 mutex_lock(&session
->s_mutex
);
49 mutex_unlock(&session
->s_mutex
);
52 vino
.ino
= le64_to_cpu(h
->ino
);
53 vino
.snap
= CEPH_NOSNAP
;
54 inode
= ceph_find_inode(sb
, vino
);
56 pr_warn("Failed to find inode %llu\n", vino
.ino
);
59 ci
= ceph_inode(inode
);
61 spin_lock(&ci
->i_ceph_lock
);
62 ci
->i_rbytes
= le64_to_cpu(h
->rbytes
);
63 ci
->i_rfiles
= le64_to_cpu(h
->rfiles
);
64 ci
->i_rsubdirs
= le64_to_cpu(h
->rsubdirs
);
65 ci
->i_max_bytes
= le64_to_cpu(h
->max_bytes
);
66 ci
->i_max_files
= le64_to_cpu(h
->max_files
);
67 spin_unlock(&ci
->i_ceph_lock
);
73 * This function walks through the snaprealm for an inode and returns the
74 * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
75 * or max_bytes). If the root is reached, return the root ceph_snap_realm
78 * Note that the caller is responsible for calling ceph_put_snap_realm() on the
81 static struct ceph_snap_realm
*get_quota_realm(struct ceph_mds_client
*mdsc
,
84 struct ceph_inode_info
*ci
= NULL
;
85 struct ceph_snap_realm
*realm
, *next
;
86 struct ceph_vino vino
;
90 realm
= ceph_inode(inode
)->i_snap_realm
;
91 ceph_get_snap_realm(mdsc
, realm
);
93 vino
.ino
= realm
->ino
;
94 vino
.snap
= CEPH_NOSNAP
;
95 in
= ceph_find_inode(inode
->i_sb
, vino
);
97 pr_warn("Failed to find inode for %llu\n", vino
.ino
);
101 has_quota
= ceph_has_quota(ci
);
104 next
= realm
->parent
;
105 if (has_quota
|| !next
)
108 ceph_get_snap_realm(mdsc
, next
);
109 ceph_put_snap_realm(mdsc
, realm
);
113 ceph_put_snap_realm(mdsc
, realm
);
118 bool ceph_quota_is_same_realm(struct inode
*old
, struct inode
*new)
120 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(old
)->mdsc
;
121 struct ceph_snap_realm
*old_realm
, *new_realm
;
124 down_read(&mdsc
->snap_rwsem
);
125 old_realm
= get_quota_realm(mdsc
, old
);
126 new_realm
= get_quota_realm(mdsc
, new);
127 is_same
= (old_realm
== new_realm
);
128 up_read(&mdsc
->snap_rwsem
);
131 ceph_put_snap_realm(mdsc
, old_realm
);
133 ceph_put_snap_realm(mdsc
, new_realm
);
138 enum quota_check_op
{
139 QUOTA_CHECK_MAX_FILES_OP
, /* check quota max_files limit */
140 QUOTA_CHECK_MAX_BYTES_OP
/* check quota max_files limit */
144 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
145 * realm, it will execute quota check operation defined by the 'op' parameter.
146 * The snaprealm walk is interrupted if the quota check detects that the quota
147 * is exceeded or if the root inode is reached.
149 static bool check_quota_exceeded(struct inode
*inode
, enum quota_check_op op
,
152 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(inode
)->mdsc
;
153 struct ceph_inode_info
*ci
;
154 struct ceph_snap_realm
*realm
, *next
;
155 struct ceph_vino vino
;
158 bool exceeded
= false;
160 down_read(&mdsc
->snap_rwsem
);
161 realm
= ceph_inode(inode
)->i_snap_realm
;
162 ceph_get_snap_realm(mdsc
, realm
);
164 vino
.ino
= realm
->ino
;
165 vino
.snap
= CEPH_NOSNAP
;
166 in
= ceph_find_inode(inode
->i_sb
, vino
);
168 pr_warn("Failed to find inode for %llu\n", vino
.ino
);
172 spin_lock(&ci
->i_ceph_lock
);
173 if (op
== QUOTA_CHECK_MAX_FILES_OP
) {
174 max
= ci
->i_max_files
;
175 rvalue
= ci
->i_rfiles
+ ci
->i_rsubdirs
;
177 max
= ci
->i_max_bytes
;
178 rvalue
= ci
->i_rbytes
;
180 spin_unlock(&ci
->i_ceph_lock
);
182 case QUOTA_CHECK_MAX_FILES_OP
:
183 exceeded
= (max
&& (rvalue
>= max
));
185 case QUOTA_CHECK_MAX_BYTES_OP
:
186 exceeded
= (max
&& (rvalue
+ delta
> max
));
189 /* Shouldn't happen */
190 pr_warn("Invalid quota check op (%d)\n", op
);
191 exceeded
= true; /* Just break the loop */
195 next
= realm
->parent
;
196 if (exceeded
|| !next
)
198 ceph_get_snap_realm(mdsc
, next
);
199 ceph_put_snap_realm(mdsc
, realm
);
202 ceph_put_snap_realm(mdsc
, realm
);
203 up_read(&mdsc
->snap_rwsem
);
209 * ceph_quota_is_max_files_exceeded - check if we can create a new file
210 * @inode: directory where a new file is being created
212 * This functions returns true is max_files quota allows a new file to be
213 * created. It is necessary to walk through the snaprealm hierarchy (until the
214 * FS root) to check all realms with quotas set.
216 bool ceph_quota_is_max_files_exceeded(struct inode
*inode
)
218 WARN_ON(!S_ISDIR(inode
->i_mode
));
220 return check_quota_exceeded(inode
, QUOTA_CHECK_MAX_FILES_OP
, 0);
224 * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
225 * @inode: inode being written
226 * @newsize: new size if write succeeds
228 * This functions returns true is max_bytes quota allows a file size to reach
229 * @newsize; it returns false otherwise.
231 bool ceph_quota_is_max_bytes_exceeded(struct inode
*inode
, loff_t newsize
)
233 loff_t size
= i_size_read(inode
);
235 /* return immediately if we're decreasing file size */
239 return check_quota_exceeded(inode
, QUOTA_CHECK_MAX_BYTES_OP
, (newsize
- size
));