quota.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * quota.c - CephFS quota
  4. *
  5. * Copyright (C) 2017-2018 SUSE
  6. */
  7. #include <linux/statfs.h>
  8. #include "super.h"
  9. #include "mds_client.h"
  10. void ceph_adjust_quota_realms_count(struct inode *inode, bool inc)
  11. {
  12. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  13. if (inc)
  14. atomic64_inc(&mdsc->quotarealms_count);
  15. else
  16. atomic64_dec(&mdsc->quotarealms_count);
  17. }
  18. static inline bool ceph_has_realms_with_quotas(struct inode *inode)
  19. {
  20. struct super_block *sb = inode->i_sb;
  21. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
  22. struct inode *root = d_inode(sb->s_root);
  23. if (atomic64_read(&mdsc->quotarealms_count) > 0)
  24. return true;
  25. /* if root is the real CephFS root, we don't have quota realms */
  26. if (root && ceph_ino(root) == CEPH_INO_ROOT)
  27. return false;
  28. /* MDS stray dirs have no quota realms */
  29. if (ceph_vino_is_reserved(ceph_inode(inode)->i_vino))
  30. return false;
  31. /* otherwise, we can't know for sure */
  32. return true;
  33. }
  34. void ceph_handle_quota(struct ceph_mds_client *mdsc,
  35. struct ceph_mds_session *session,
  36. struct ceph_msg *msg)
  37. {
  38. struct super_block *sb = mdsc->fsc->sb;
  39. struct ceph_mds_quota *h = msg->front.iov_base;
  40. struct ceph_vino vino;
  41. struct inode *inode;
  42. struct ceph_inode_info *ci;
  43. if (!ceph_inc_mds_stopping_blocker(mdsc, session))
  44. return;
  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. goto out;
  50. }
  51. /* lookup inode */
  52. vino.ino = le64_to_cpu(h->ino);
  53. vino.snap = CEPH_NOSNAP;
  54. inode = ceph_find_inode(sb, vino);
  55. if (!inode) {
  56. pr_warn("Failed to find inode %llu\n", vino.ino);
  57. goto out;
  58. }
  59. ci = ceph_inode(inode);
  60. spin_lock(&ci->i_ceph_lock);
  61. ci->i_rbytes = le64_to_cpu(h->rbytes);
  62. ci->i_rfiles = le64_to_cpu(h->rfiles);
  63. ci->i_rsubdirs = le64_to_cpu(h->rsubdirs);
  64. __ceph_update_quota(ci, le64_to_cpu(h->max_bytes),
  65. le64_to_cpu(h->max_files));
  66. spin_unlock(&ci->i_ceph_lock);
  67. iput(inode);
  68. out:
  69. ceph_dec_mds_stopping_blocker(mdsc);
  70. }
  71. static struct ceph_quotarealm_inode *
  72. find_quotarealm_inode(struct ceph_mds_client *mdsc, u64 ino)
  73. {
  74. struct ceph_quotarealm_inode *qri = NULL;
  75. struct rb_node **node, *parent = NULL;
  76. mutex_lock(&mdsc->quotarealms_inodes_mutex);
  77. node = &(mdsc->quotarealms_inodes.rb_node);
  78. while (*node) {
  79. parent = *node;
  80. qri = container_of(*node, struct ceph_quotarealm_inode, node);
  81. if (ino < qri->ino)
  82. node = &((*node)->rb_left);
  83. else if (ino > qri->ino)
  84. node = &((*node)->rb_right);
  85. else
  86. break;
  87. }
  88. if (!qri || (qri->ino != ino)) {
  89. /* Not found, create a new one and insert it */
  90. qri = kmalloc(sizeof(*qri), GFP_KERNEL);
  91. if (qri) {
  92. qri->ino = ino;
  93. qri->inode = NULL;
  94. qri->timeout = 0;
  95. mutex_init(&qri->mutex);
  96. rb_link_node(&qri->node, parent, node);
  97. rb_insert_color(&qri->node, &mdsc->quotarealms_inodes);
  98. } else
  99. pr_warn("Failed to alloc quotarealms_inode\n");
  100. }
  101. mutex_unlock(&mdsc->quotarealms_inodes_mutex);
  102. return qri;
  103. }
  104. /*
  105. * This function will try to lookup a realm inode which isn't visible in the
  106. * filesystem mountpoint. A list of these kind of inodes (not visible) is
  107. * maintained in the mdsc and freed only when the filesystem is umounted.
  108. *
  109. * Note that these inodes are kept in this list even if the lookup fails, which
  110. * allows to prevent useless lookup requests.
  111. */
  112. static struct inode *lookup_quotarealm_inode(struct ceph_mds_client *mdsc,
  113. struct super_block *sb,
  114. struct ceph_snap_realm *realm)
  115. {
  116. struct ceph_quotarealm_inode *qri;
  117. struct inode *in;
  118. qri = find_quotarealm_inode(mdsc, realm->ino);
  119. if (!qri)
  120. return NULL;
  121. mutex_lock(&qri->mutex);
  122. if (qri->inode && ceph_is_any_caps(qri->inode)) {
  123. /* A request has already returned the inode */
  124. mutex_unlock(&qri->mutex);
  125. return qri->inode;
  126. }
  127. /* Check if this inode lookup has failed recently */
  128. if (qri->timeout &&
  129. time_before_eq(jiffies, qri->timeout)) {
  130. mutex_unlock(&qri->mutex);
  131. return NULL;
  132. }
  133. if (qri->inode) {
  134. /* get caps */
  135. int ret = __ceph_do_getattr(qri->inode, NULL,
  136. CEPH_STAT_CAP_INODE, true);
  137. if (ret >= 0)
  138. in = qri->inode;
  139. else
  140. in = ERR_PTR(ret);
  141. } else {
  142. in = ceph_lookup_inode(sb, realm->ino);
  143. }
  144. if (IS_ERR(in)) {
  145. dout("Can't lookup inode %llx (err: %ld)\n",
  146. realm->ino, PTR_ERR(in));
  147. qri->timeout = jiffies + msecs_to_jiffies(60 * 1000); /* XXX */
  148. } else {
  149. qri->timeout = 0;
  150. qri->inode = in;
  151. }
  152. mutex_unlock(&qri->mutex);
  153. return in;
  154. }
  155. void ceph_cleanup_quotarealms_inodes(struct ceph_mds_client *mdsc)
  156. {
  157. struct ceph_quotarealm_inode *qri;
  158. struct rb_node *node;
  159. /*
  160. * It should now be safe to clean quotarealms_inode tree without holding
  161. * mdsc->quotarealms_inodes_mutex...
  162. */
  163. mutex_lock(&mdsc->quotarealms_inodes_mutex);
  164. while (!RB_EMPTY_ROOT(&mdsc->quotarealms_inodes)) {
  165. node = rb_first(&mdsc->quotarealms_inodes);
  166. qri = rb_entry(node, struct ceph_quotarealm_inode, node);
  167. rb_erase(node, &mdsc->quotarealms_inodes);
  168. iput(qri->inode);
  169. kfree(qri);
  170. }
  171. mutex_unlock(&mdsc->quotarealms_inodes_mutex);
  172. }
  173. /*
  174. * This function walks through the snaprealm for an inode and returns the
  175. * ceph_snap_realm for the first snaprealm that has quotas set (max_files,
  176. * max_bytes, or any, depending on the 'which_quota' argument). If the root is
  177. * reached, return the root ceph_snap_realm instead.
  178. *
  179. * Note that the caller is responsible for calling ceph_put_snap_realm() on the
  180. * returned realm.
  181. *
  182. * Callers of this function need to hold mdsc->snap_rwsem. However, if there's
  183. * a need to do an inode lookup, this rwsem will be temporarily dropped. Hence
  184. * the 'retry' argument: if rwsem needs to be dropped and 'retry' is 'false'
  185. * this function will return -EAGAIN; otherwise, the snaprealms walk-through
  186. * will be restarted.
  187. */
  188. static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
  189. struct inode *inode,
  190. enum quota_get_realm which_quota,
  191. bool retry)
  192. {
  193. struct ceph_inode_info *ci = NULL;
  194. struct ceph_snap_realm *realm, *next;
  195. struct inode *in;
  196. bool has_quota;
  197. if (ceph_snap(inode) != CEPH_NOSNAP)
  198. return NULL;
  199. restart:
  200. realm = ceph_inode(inode)->i_snap_realm;
  201. if (realm)
  202. ceph_get_snap_realm(mdsc, realm);
  203. else
  204. pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) "
  205. "null i_snap_realm\n", ceph_vinop(inode));
  206. while (realm) {
  207. bool has_inode;
  208. spin_lock(&realm->inodes_with_caps_lock);
  209. has_inode = realm->inode;
  210. in = has_inode ? igrab(realm->inode) : NULL;
  211. spin_unlock(&realm->inodes_with_caps_lock);
  212. if (has_inode && !in)
  213. break;
  214. if (!in) {
  215. up_read(&mdsc->snap_rwsem);
  216. in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
  217. down_read(&mdsc->snap_rwsem);
  218. if (IS_ERR_OR_NULL(in))
  219. break;
  220. ceph_put_snap_realm(mdsc, realm);
  221. if (!retry)
  222. return ERR_PTR(-EAGAIN);
  223. goto restart;
  224. }
  225. ci = ceph_inode(in);
  226. has_quota = __ceph_has_quota(ci, which_quota);
  227. iput(in);
  228. next = realm->parent;
  229. if (has_quota || !next)
  230. return realm;
  231. ceph_get_snap_realm(mdsc, next);
  232. ceph_put_snap_realm(mdsc, realm);
  233. realm = next;
  234. }
  235. if (realm)
  236. ceph_put_snap_realm(mdsc, realm);
  237. return NULL;
  238. }
  239. bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
  240. {
  241. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(old->i_sb);
  242. struct ceph_snap_realm *old_realm, *new_realm;
  243. bool is_same;
  244. restart:
  245. /*
  246. * We need to lookup 2 quota realms atomically, i.e. with snap_rwsem.
  247. * However, get_quota_realm may drop it temporarily. By setting the
  248. * 'retry' parameter to 'false', we'll get -EAGAIN if the rwsem was
  249. * dropped and we can then restart the whole operation.
  250. */
  251. down_read(&mdsc->snap_rwsem);
  252. old_realm = get_quota_realm(mdsc, old, QUOTA_GET_ANY, true);
  253. new_realm = get_quota_realm(mdsc, new, QUOTA_GET_ANY, false);
  254. if (PTR_ERR(new_realm) == -EAGAIN) {
  255. up_read(&mdsc->snap_rwsem);
  256. if (old_realm)
  257. ceph_put_snap_realm(mdsc, old_realm);
  258. goto restart;
  259. }
  260. is_same = (old_realm == new_realm);
  261. up_read(&mdsc->snap_rwsem);
  262. if (old_realm)
  263. ceph_put_snap_realm(mdsc, old_realm);
  264. if (new_realm)
  265. ceph_put_snap_realm(mdsc, new_realm);
  266. return is_same;
  267. }
  268. enum quota_check_op {
  269. QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
  270. QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
  271. QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
  272. limit is approaching */
  273. };
  274. /*
  275. * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
  276. * realm, it will execute quota check operation defined by the 'op' parameter.
  277. * The snaprealm walk is interrupted if the quota check detects that the quota
  278. * is exceeded or if the root inode is reached.
  279. */
  280. static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
  281. loff_t delta)
  282. {
  283. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  284. struct ceph_inode_info *ci;
  285. struct ceph_snap_realm *realm, *next;
  286. struct inode *in;
  287. u64 max, rvalue;
  288. bool exceeded = false;
  289. if (ceph_snap(inode) != CEPH_NOSNAP)
  290. return false;
  291. down_read(&mdsc->snap_rwsem);
  292. restart:
  293. realm = ceph_inode(inode)->i_snap_realm;
  294. if (realm)
  295. ceph_get_snap_realm(mdsc, realm);
  296. else
  297. pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) "
  298. "null i_snap_realm\n", ceph_vinop(inode));
  299. while (realm) {
  300. bool has_inode;
  301. spin_lock(&realm->inodes_with_caps_lock);
  302. has_inode = realm->inode;
  303. in = has_inode ? igrab(realm->inode) : NULL;
  304. spin_unlock(&realm->inodes_with_caps_lock);
  305. if (has_inode && !in)
  306. break;
  307. if (!in) {
  308. up_read(&mdsc->snap_rwsem);
  309. in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
  310. down_read(&mdsc->snap_rwsem);
  311. if (IS_ERR_OR_NULL(in))
  312. break;
  313. ceph_put_snap_realm(mdsc, realm);
  314. goto restart;
  315. }
  316. ci = ceph_inode(in);
  317. spin_lock(&ci->i_ceph_lock);
  318. if (op == QUOTA_CHECK_MAX_FILES_OP) {
  319. max = ci->i_max_files;
  320. rvalue = ci->i_rfiles + ci->i_rsubdirs;
  321. } else {
  322. max = ci->i_max_bytes;
  323. rvalue = ci->i_rbytes;
  324. }
  325. spin_unlock(&ci->i_ceph_lock);
  326. switch (op) {
  327. case QUOTA_CHECK_MAX_FILES_OP:
  328. case QUOTA_CHECK_MAX_BYTES_OP:
  329. exceeded = (max && (rvalue + delta > max));
  330. break;
  331. case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
  332. if (max) {
  333. if (rvalue >= max)
  334. exceeded = true;
  335. else {
  336. /*
  337. * when we're writing more that 1/16th
  338. * of the available space
  339. */
  340. exceeded =
  341. (((max - rvalue) >> 4) < delta);
  342. }
  343. }
  344. break;
  345. default:
  346. /* Shouldn't happen */
  347. pr_warn("Invalid quota check op (%d)\n", op);
  348. exceeded = true; /* Just break the loop */
  349. }
  350. iput(in);
  351. next = realm->parent;
  352. if (exceeded || !next)
  353. break;
  354. ceph_get_snap_realm(mdsc, next);
  355. ceph_put_snap_realm(mdsc, realm);
  356. realm = next;
  357. }
  358. if (realm)
  359. ceph_put_snap_realm(mdsc, realm);
  360. up_read(&mdsc->snap_rwsem);
  361. return exceeded;
  362. }
  363. /*
  364. * ceph_quota_is_max_files_exceeded - check if we can create a new file
  365. * @inode: directory where a new file is being created
  366. *
  367. * This functions returns true is max_files quota allows a new file to be
  368. * created. It is necessary to walk through the snaprealm hierarchy (until the
  369. * FS root) to check all realms with quotas set.
  370. */
  371. bool ceph_quota_is_max_files_exceeded(struct inode *inode)
  372. {
  373. if (!ceph_has_realms_with_quotas(inode))
  374. return false;
  375. WARN_ON(!S_ISDIR(inode->i_mode));
  376. return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 1);
  377. }
  378. /*
  379. * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
  380. * @inode: inode being written
  381. * @newsize: new size if write succeeds
  382. *
  383. * This functions returns true is max_bytes quota allows a file size to reach
  384. * @newsize; it returns false otherwise.
  385. */
  386. bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
  387. {
  388. loff_t size = i_size_read(inode);
  389. if (!ceph_has_realms_with_quotas(inode))
  390. return false;
  391. /* return immediately if we're decreasing file size */
  392. if (newsize <= size)
  393. return false;
  394. return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
  395. }
  396. /*
  397. * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
  398. * @inode: inode being written
  399. * @newsize: new size if write succeeds
  400. *
  401. * This function returns true if the new file size @newsize will be consuming
  402. * more than 1/16th of the available quota space; it returns false otherwise.
  403. */
  404. bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
  405. {
  406. loff_t size = ceph_inode(inode)->i_reported_size;
  407. if (!ceph_has_realms_with_quotas(inode))
  408. return false;
  409. /* return immediately if we're decreasing file size */
  410. if (newsize <= size)
  411. return false;
  412. return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
  413. (newsize - size));
  414. }
  415. /*
  416. * ceph_quota_update_statfs - if root has quota update statfs with quota status
  417. * @fsc: filesystem client instance
  418. * @buf: statfs to update
  419. *
  420. * If the mounted filesystem root has max_bytes quota set, update the filesystem
  421. * statistics with the quota status.
  422. *
  423. * This function returns true if the stats have been updated, false otherwise.
  424. */
  425. bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf)
  426. {
  427. struct ceph_mds_client *mdsc = fsc->mdsc;
  428. struct ceph_inode_info *ci;
  429. struct ceph_snap_realm *realm;
  430. struct inode *in;
  431. u64 total = 0, used, free;
  432. bool is_updated = false;
  433. down_read(&mdsc->snap_rwsem);
  434. realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root),
  435. QUOTA_GET_MAX_BYTES, true);
  436. up_read(&mdsc->snap_rwsem);
  437. if (!realm)
  438. return false;
  439. spin_lock(&realm->inodes_with_caps_lock);
  440. in = realm->inode ? igrab(realm->inode) : NULL;
  441. spin_unlock(&realm->inodes_with_caps_lock);
  442. if (in) {
  443. ci = ceph_inode(in);
  444. spin_lock(&ci->i_ceph_lock);
  445. if (ci->i_max_bytes) {
  446. total = ci->i_max_bytes >> CEPH_BLOCK_SHIFT;
  447. used = ci->i_rbytes >> CEPH_BLOCK_SHIFT;
  448. /* For quota size less than 4MB, use 4KB block size */
  449. if (!total) {
  450. total = ci->i_max_bytes >> CEPH_4K_BLOCK_SHIFT;
  451. used = ci->i_rbytes >> CEPH_4K_BLOCK_SHIFT;
  452. buf->f_frsize = 1 << CEPH_4K_BLOCK_SHIFT;
  453. }
  454. /* It is possible for a quota to be exceeded.
  455. * Report 'zero' in that case
  456. */
  457. free = total > used ? total - used : 0;
  458. /* For quota size less than 4KB, report the
  459. * total=used=4KB,free=0 when quota is full
  460. * and total=free=4KB, used=0 otherwise */
  461. if (!total) {
  462. total = 1;
  463. free = ci->i_max_bytes > ci->i_rbytes ? 1 : 0;
  464. buf->f_frsize = 1 << CEPH_4K_BLOCK_SHIFT;
  465. }
  466. }
  467. spin_unlock(&ci->i_ceph_lock);
  468. if (total) {
  469. buf->f_blocks = total;
  470. buf->f_bfree = free;
  471. buf->f_bavail = free;
  472. is_updated = true;
  473. }
  474. iput(in);
  475. }
  476. ceph_put_snap_realm(mdsc, realm);
  477. return is_updated;
  478. }