quota.c 15 KB

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