quota.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include <linux/statfs.h>
  21. #include "super.h"
  22. #include "mds_client.h"
  23. void ceph_adjust_quota_realms_count(struct inode *inode, bool inc)
  24. {
  25. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  26. if (inc)
  27. atomic64_inc(&mdsc->quotarealms_count);
  28. else
  29. atomic64_dec(&mdsc->quotarealms_count);
  30. }
  31. static inline bool ceph_has_realms_with_quotas(struct inode *inode)
  32. {
  33. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  34. return atomic64_read(&mdsc->quotarealms_count) > 0;
  35. }
  36. void ceph_handle_quota(struct ceph_mds_client *mdsc,
  37. struct ceph_mds_session *session,
  38. struct ceph_msg *msg)
  39. {
  40. struct super_block *sb = mdsc->fsc->sb;
  41. struct ceph_mds_quota *h = msg->front.iov_base;
  42. struct ceph_vino vino;
  43. struct inode *inode;
  44. struct ceph_inode_info *ci;
  45. if (msg->front.iov_len < sizeof(*h)) {
  46. pr_err("%s corrupt message mds%d len %d\n", __func__,
  47. session->s_mds, (int)msg->front.iov_len);
  48. ceph_msg_dump(msg);
  49. return;
  50. }
  51. /* increment msg sequence number */
  52. mutex_lock(&session->s_mutex);
  53. session->s_seq++;
  54. mutex_unlock(&session->s_mutex);
  55. /* lookup inode */
  56. vino.ino = le64_to_cpu(h->ino);
  57. vino.snap = CEPH_NOSNAP;
  58. inode = ceph_find_inode(sb, vino);
  59. if (!inode) {
  60. pr_warn("Failed to find inode %llu\n", vino.ino);
  61. return;
  62. }
  63. ci = ceph_inode(inode);
  64. spin_lock(&ci->i_ceph_lock);
  65. ci->i_rbytes = le64_to_cpu(h->rbytes);
  66. ci->i_rfiles = le64_to_cpu(h->rfiles);
  67. ci->i_rsubdirs = le64_to_cpu(h->rsubdirs);
  68. __ceph_update_quota(ci, le64_to_cpu(h->max_bytes),
  69. le64_to_cpu(h->max_files));
  70. spin_unlock(&ci->i_ceph_lock);
  71. iput(inode);
  72. }
  73. /*
  74. * This function walks through the snaprealm for an inode and returns the
  75. * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
  76. * or max_bytes). If the root is reached, return the root ceph_snap_realm
  77. * instead.
  78. *
  79. * Note that the caller is responsible for calling ceph_put_snap_realm() on the
  80. * returned realm.
  81. */
  82. static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
  83. struct inode *inode)
  84. {
  85. struct ceph_inode_info *ci = NULL;
  86. struct ceph_snap_realm *realm, *next;
  87. struct inode *in;
  88. bool has_quota;
  89. if (ceph_snap(inode) != CEPH_NOSNAP)
  90. return NULL;
  91. realm = ceph_inode(inode)->i_snap_realm;
  92. if (realm)
  93. ceph_get_snap_realm(mdsc, realm);
  94. else
  95. pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) "
  96. "null i_snap_realm\n", ceph_vinop(inode));
  97. while (realm) {
  98. spin_lock(&realm->inodes_with_caps_lock);
  99. in = realm->inode ? igrab(realm->inode) : NULL;
  100. spin_unlock(&realm->inodes_with_caps_lock);
  101. if (!in)
  102. break;
  103. ci = ceph_inode(in);
  104. has_quota = __ceph_has_any_quota(ci);
  105. iput(in);
  106. next = realm->parent;
  107. if (has_quota || !next)
  108. return realm;
  109. ceph_get_snap_realm(mdsc, next);
  110. ceph_put_snap_realm(mdsc, realm);
  111. realm = next;
  112. }
  113. if (realm)
  114. ceph_put_snap_realm(mdsc, realm);
  115. return NULL;
  116. }
  117. bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
  118. {
  119. struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc;
  120. struct ceph_snap_realm *old_realm, *new_realm;
  121. bool is_same;
  122. down_read(&mdsc->snap_rwsem);
  123. old_realm = get_quota_realm(mdsc, old);
  124. new_realm = get_quota_realm(mdsc, new);
  125. is_same = (old_realm == new_realm);
  126. up_read(&mdsc->snap_rwsem);
  127. if (old_realm)
  128. ceph_put_snap_realm(mdsc, old_realm);
  129. if (new_realm)
  130. ceph_put_snap_realm(mdsc, new_realm);
  131. return is_same;
  132. }
  133. enum quota_check_op {
  134. QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
  135. QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
  136. QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
  137. limit is approaching */
  138. };
  139. /*
  140. * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
  141. * realm, it will execute quota check operation defined by the 'op' parameter.
  142. * The snaprealm walk is interrupted if the quota check detects that the quota
  143. * is exceeded or if the root inode is reached.
  144. */
  145. static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
  146. loff_t delta)
  147. {
  148. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  149. struct ceph_inode_info *ci;
  150. struct ceph_snap_realm *realm, *next;
  151. struct inode *in;
  152. u64 max, rvalue;
  153. bool exceeded = false;
  154. if (ceph_snap(inode) != CEPH_NOSNAP)
  155. return false;
  156. down_read(&mdsc->snap_rwsem);
  157. realm = ceph_inode(inode)->i_snap_realm;
  158. if (realm)
  159. ceph_get_snap_realm(mdsc, realm);
  160. else
  161. pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) "
  162. "null i_snap_realm\n", ceph_vinop(inode));
  163. while (realm) {
  164. spin_lock(&realm->inodes_with_caps_lock);
  165. in = realm->inode ? igrab(realm->inode) : NULL;
  166. spin_unlock(&realm->inodes_with_caps_lock);
  167. if (!in)
  168. break;
  169. ci = ceph_inode(in);
  170. spin_lock(&ci->i_ceph_lock);
  171. if (op == QUOTA_CHECK_MAX_FILES_OP) {
  172. max = ci->i_max_files;
  173. rvalue = ci->i_rfiles + ci->i_rsubdirs;
  174. } else {
  175. max = ci->i_max_bytes;
  176. rvalue = ci->i_rbytes;
  177. }
  178. spin_unlock(&ci->i_ceph_lock);
  179. switch (op) {
  180. case QUOTA_CHECK_MAX_FILES_OP:
  181. exceeded = (max && (rvalue >= max));
  182. break;
  183. case QUOTA_CHECK_MAX_BYTES_OP:
  184. exceeded = (max && (rvalue + delta > max));
  185. break;
  186. case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
  187. if (max) {
  188. if (rvalue >= max)
  189. exceeded = true;
  190. else {
  191. /*
  192. * when we're writing more that 1/16th
  193. * of the available space
  194. */
  195. exceeded =
  196. (((max - rvalue) >> 4) < delta);
  197. }
  198. }
  199. break;
  200. default:
  201. /* Shouldn't happen */
  202. pr_warn("Invalid quota check op (%d)\n", op);
  203. exceeded = true; /* Just break the loop */
  204. }
  205. iput(in);
  206. next = realm->parent;
  207. if (exceeded || !next)
  208. break;
  209. ceph_get_snap_realm(mdsc, next);
  210. ceph_put_snap_realm(mdsc, realm);
  211. realm = next;
  212. }
  213. if (realm)
  214. ceph_put_snap_realm(mdsc, realm);
  215. up_read(&mdsc->snap_rwsem);
  216. return exceeded;
  217. }
  218. /*
  219. * ceph_quota_is_max_files_exceeded - check if we can create a new file
  220. * @inode: directory where a new file is being created
  221. *
  222. * This functions returns true is max_files quota allows a new file to be
  223. * created. It is necessary to walk through the snaprealm hierarchy (until the
  224. * FS root) to check all realms with quotas set.
  225. */
  226. bool ceph_quota_is_max_files_exceeded(struct inode *inode)
  227. {
  228. if (!ceph_has_realms_with_quotas(inode))
  229. return false;
  230. WARN_ON(!S_ISDIR(inode->i_mode));
  231. return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
  232. }
  233. /*
  234. * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
  235. * @inode: inode being written
  236. * @newsize: new size if write succeeds
  237. *
  238. * This functions returns true is max_bytes quota allows a file size to reach
  239. * @newsize; it returns false otherwise.
  240. */
  241. bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
  242. {
  243. loff_t size = i_size_read(inode);
  244. if (!ceph_has_realms_with_quotas(inode))
  245. return false;
  246. /* return immediately if we're decreasing file size */
  247. if (newsize <= size)
  248. return false;
  249. return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
  250. }
  251. /*
  252. * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
  253. * @inode: inode being written
  254. * @newsize: new size if write succeeds
  255. *
  256. * This function returns true if the new file size @newsize will be consuming
  257. * more than 1/16th of the available quota space; it returns false otherwise.
  258. */
  259. bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
  260. {
  261. loff_t size = ceph_inode(inode)->i_reported_size;
  262. if (!ceph_has_realms_with_quotas(inode))
  263. return false;
  264. /* return immediately if we're decreasing file size */
  265. if (newsize <= size)
  266. return false;
  267. return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
  268. (newsize - size));
  269. }
  270. /*
  271. * ceph_quota_update_statfs - if root has quota update statfs with quota status
  272. * @fsc: filesystem client instance
  273. * @buf: statfs to update
  274. *
  275. * If the mounted filesystem root has max_bytes quota set, update the filesystem
  276. * statistics with the quota status.
  277. *
  278. * This function returns true if the stats have been updated, false otherwise.
  279. */
  280. bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf)
  281. {
  282. struct ceph_mds_client *mdsc = fsc->mdsc;
  283. struct ceph_inode_info *ci;
  284. struct ceph_snap_realm *realm;
  285. struct inode *in;
  286. u64 total = 0, used, free;
  287. bool is_updated = false;
  288. down_read(&mdsc->snap_rwsem);
  289. realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root));
  290. up_read(&mdsc->snap_rwsem);
  291. if (!realm)
  292. return false;
  293. spin_lock(&realm->inodes_with_caps_lock);
  294. in = realm->inode ? igrab(realm->inode) : NULL;
  295. spin_unlock(&realm->inodes_with_caps_lock);
  296. if (in) {
  297. ci = ceph_inode(in);
  298. spin_lock(&ci->i_ceph_lock);
  299. if (ci->i_max_bytes) {
  300. total = ci->i_max_bytes >> CEPH_BLOCK_SHIFT;
  301. used = ci->i_rbytes >> CEPH_BLOCK_SHIFT;
  302. /* It is possible for a quota to be exceeded.
  303. * Report 'zero' in that case
  304. */
  305. free = total > used ? total - used : 0;
  306. }
  307. spin_unlock(&ci->i_ceph_lock);
  308. if (total) {
  309. buf->f_blocks = total;
  310. buf->f_bfree = free;
  311. buf->f_bavail = free;
  312. is_updated = true;
  313. }
  314. iput(in);
  315. }
  316. ceph_put_snap_realm(mdsc, realm);
  317. return is_updated;
  318. }