Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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