xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_quota.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011 Pawel Jakub Dawidek
25  * Copyright (c) 2012, 2015, 2018 by Delphix. All rights reserved.
26  * Copyright (c) 2014 Integros [integros.com]
27  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28  */
29 
30 /* Portions Copyright 2010 Robert Milkowski */
31 
32 #include <sys/avl.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/sa.h>
35 #include <sys/sa_impl.h>
36 #include <sys/zap.h>
37 #include <sys/zfs_project.h>
38 #include <sys/zfs_quota.h>
39 #include <sys/zfs_znode.h>
40 
41 int
zpl_get_file_info(dmu_object_type_t bonustype,const void * data,zfs_file_info_t * zoi)42 zpl_get_file_info(dmu_object_type_t bonustype, const void *data,
43     zfs_file_info_t *zoi)
44 {
45 	/*
46 	 * Is it a valid type of object to track?
47 	 */
48 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
49 		return (SET_ERROR(ENOENT));
50 
51 	zoi->zfi_project = ZFS_DEFAULT_PROJID;
52 
53 	/*
54 	 * If we have a NULL data pointer
55 	 * then assume the id's aren't changing and
56 	 * return EEXIST to the dmu to let it know to
57 	 * use the same ids
58 	 */
59 	if (data == NULL)
60 		return (SET_ERROR(EEXIST));
61 
62 	if (bonustype == DMU_OT_ZNODE) {
63 		const znode_phys_t *znp = data;
64 		zoi->zfi_user = znp->zp_uid;
65 		zoi->zfi_group = znp->zp_gid;
66 		zoi->zfi_generation = znp->zp_gen;
67 		return (0);
68 	}
69 
70 	const sa_hdr_phys_t *sap = data;
71 	if (sap->sa_magic == 0) {
72 		/*
73 		 * This should only happen for newly created files
74 		 * that haven't had the znode data filled in yet.
75 		 */
76 		zoi->zfi_user = 0;
77 		zoi->zfi_group = 0;
78 		zoi->zfi_generation = 0;
79 		return (0);
80 	}
81 
82 	sa_hdr_phys_t sa = *sap;
83 	boolean_t swap = B_FALSE;
84 	if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
85 		sa.sa_magic = SA_MAGIC;
86 		sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
87 		swap = B_TRUE;
88 	}
89 	VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
90 
91 	int hdrsize = sa_hdrsize(&sa);
92 	VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
93 
94 	uintptr_t data_after_hdr = (uintptr_t)data + hdrsize;
95 	zoi->zfi_user = *((uint64_t *)(data_after_hdr + SA_UID_OFFSET));
96 	zoi->zfi_group = *((uint64_t *)(data_after_hdr + SA_GID_OFFSET));
97 	zoi->zfi_generation = *((uint64_t *)(data_after_hdr + SA_GEN_OFFSET));
98 	uint64_t flags = *((uint64_t *)(data_after_hdr + SA_FLAGS_OFFSET));
99 	if (swap)
100 		flags = BSWAP_64(flags);
101 
102 	if (flags & ZFS_PROJID) {
103 		zoi->zfi_project =
104 		    *((uint64_t *)(data_after_hdr + SA_PROJID_OFFSET));
105 	}
106 
107 	if (swap) {
108 		zoi->zfi_user = BSWAP_64(zoi->zfi_user);
109 		zoi->zfi_group = BSWAP_64(zoi->zfi_group);
110 		zoi->zfi_project = BSWAP_64(zoi->zfi_project);
111 		zoi->zfi_generation = BSWAP_64(zoi->zfi_generation);
112 	}
113 	return (0);
114 }
115 
116 static void
fuidstr_to_sid(zfsvfs_t * zfsvfs,const char * fuidstr,char * domainbuf,int buflen,uid_t * ridp)117 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
118     char *domainbuf, int buflen, uid_t *ridp)
119 {
120 	uint64_t fuid;
121 	const char *domain;
122 
123 	fuid = zfs_strtonum(fuidstr, NULL);
124 
125 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
126 	if (domain)
127 		(void) strlcpy(domainbuf, domain, buflen);
128 	else
129 		domainbuf[0] = '\0';
130 	*ridp = FUID_RID(fuid);
131 }
132 
133 static uint64_t
zfs_userquota_prop_to_obj(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type)134 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
135 {
136 	switch (type) {
137 	case ZFS_PROP_USERUSED:
138 	case ZFS_PROP_USEROBJUSED:
139 		return (DMU_USERUSED_OBJECT);
140 	case ZFS_PROP_GROUPUSED:
141 	case ZFS_PROP_GROUPOBJUSED:
142 		return (DMU_GROUPUSED_OBJECT);
143 	case ZFS_PROP_PROJECTUSED:
144 	case ZFS_PROP_PROJECTOBJUSED:
145 		return (DMU_PROJECTUSED_OBJECT);
146 	case ZFS_PROP_USERQUOTA:
147 		return (zfsvfs->z_userquota_obj);
148 	case ZFS_PROP_GROUPQUOTA:
149 		return (zfsvfs->z_groupquota_obj);
150 	case ZFS_PROP_USEROBJQUOTA:
151 		return (zfsvfs->z_userobjquota_obj);
152 	case ZFS_PROP_GROUPOBJQUOTA:
153 		return (zfsvfs->z_groupobjquota_obj);
154 	case ZFS_PROP_PROJECTQUOTA:
155 		return (zfsvfs->z_projectquota_obj);
156 	case ZFS_PROP_PROJECTOBJQUOTA:
157 		return (zfsvfs->z_projectobjquota_obj);
158 	default:
159 		return (ZFS_NO_OBJECT);
160 	}
161 }
162 
163 int
zfs_userspace_many(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,uint64_t * cookiep,void * vbuf,uint64_t * bufsizep)164 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
165     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
166 {
167 	int error;
168 	zap_cursor_t zc;
169 	zap_attribute_t *za;
170 	zfs_useracct_t *buf = vbuf;
171 	uint64_t obj;
172 	int offset = 0;
173 
174 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
175 		return (SET_ERROR(ENOTSUP));
176 
177 	if ((type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
178 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
179 	    type == ZFS_PROP_PROJECTOBJUSED) &&
180 	    !dmu_objset_projectquota_present(zfsvfs->z_os))
181 		return (SET_ERROR(ENOTSUP));
182 
183 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
184 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
185 	    type == ZFS_PROP_PROJECTOBJUSED ||
186 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
187 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
188 		return (SET_ERROR(ENOTSUP));
189 
190 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
191 	if (obj == ZFS_NO_OBJECT) {
192 		*bufsizep = 0;
193 		return (0);
194 	}
195 
196 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
197 	    type == ZFS_PROP_PROJECTOBJUSED)
198 		offset = DMU_OBJACCT_PREFIX_LEN;
199 
200 	za = zap_attribute_alloc();
201 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
202 	    (error = zap_cursor_retrieve(&zc, za)) == 0;
203 	    zap_cursor_advance(&zc)) {
204 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
205 		    *bufsizep)
206 			break;
207 
208 		/*
209 		 * skip object quota (with zap name prefix DMU_OBJACCT_PREFIX)
210 		 * when dealing with block quota and vice versa.
211 		 */
212 		if ((offset > 0) != (strncmp(za->za_name, DMU_OBJACCT_PREFIX,
213 		    DMU_OBJACCT_PREFIX_LEN) == 0))
214 			continue;
215 
216 		fuidstr_to_sid(zfsvfs, za->za_name + offset,
217 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
218 
219 		buf->zu_space = za->za_first_integer;
220 		buf++;
221 	}
222 	if (error == ENOENT)
223 		error = 0;
224 
225 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
226 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
227 	*cookiep = zap_cursor_serialize(&zc);
228 	zap_cursor_fini(&zc);
229 	zap_attribute_free(za);
230 	return (error);
231 }
232 
233 int
zfs_userspace_one(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t * valp)234 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
235     const char *domain, uint64_t rid, uint64_t *valp)
236 {
237 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
238 	int offset = 0;
239 	int err;
240 	uint64_t obj;
241 
242 	*valp = 0;
243 
244 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
245 		return (SET_ERROR(ENOTSUP));
246 
247 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
248 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
249 	    type == ZFS_PROP_PROJECTOBJUSED ||
250 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
251 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
252 		return (SET_ERROR(ENOTSUP));
253 
254 	if (type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
255 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
256 	    type == ZFS_PROP_PROJECTOBJUSED) {
257 		if (!dmu_objset_projectquota_present(zfsvfs->z_os))
258 			return (SET_ERROR(ENOTSUP));
259 		if (!zpl_is_valid_projid(rid))
260 			return (SET_ERROR(EINVAL));
261 	}
262 
263 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
264 	if (obj == ZFS_NO_OBJECT)
265 		return (0);
266 
267 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
268 	    type == ZFS_PROP_PROJECTOBJUSED) {
269 		strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
270 		offset = DMU_OBJACCT_PREFIX_LEN;
271 	}
272 
273 	err = zfs_id_to_fuidstr(zfsvfs, domain, rid, buf + offset,
274 	    sizeof (buf) - offset, B_FALSE);
275 	if (err)
276 		return (err);
277 
278 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
279 	if (err == ENOENT)
280 		err = 0;
281 	return (err);
282 }
283 
284 int
zfs_set_userquota(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t quota)285 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
286     const char *domain, uint64_t rid, uint64_t quota)
287 {
288 	char buf[32];
289 	int err;
290 	dmu_tx_t *tx;
291 	uint64_t *objp;
292 	boolean_t fuid_dirtied;
293 
294 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
295 		return (SET_ERROR(ENOTSUP));
296 
297 	switch (type) {
298 	case ZFS_PROP_USERQUOTA:
299 		objp = &zfsvfs->z_userquota_obj;
300 		break;
301 	case ZFS_PROP_GROUPQUOTA:
302 		objp = &zfsvfs->z_groupquota_obj;
303 		break;
304 	case ZFS_PROP_USEROBJQUOTA:
305 		objp = &zfsvfs->z_userobjquota_obj;
306 		break;
307 	case ZFS_PROP_GROUPOBJQUOTA:
308 		objp = &zfsvfs->z_groupobjquota_obj;
309 		break;
310 	case ZFS_PROP_PROJECTQUOTA:
311 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
312 			return (SET_ERROR(ENOTSUP));
313 		if (!zpl_is_valid_projid(rid))
314 			return (SET_ERROR(EINVAL));
315 
316 		objp = &zfsvfs->z_projectquota_obj;
317 		break;
318 	case ZFS_PROP_PROJECTOBJQUOTA:
319 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
320 			return (SET_ERROR(ENOTSUP));
321 		if (!zpl_is_valid_projid(rid))
322 			return (SET_ERROR(EINVAL));
323 
324 		objp = &zfsvfs->z_projectobjquota_obj;
325 		break;
326 	default:
327 		return (SET_ERROR(EINVAL));
328 	}
329 
330 	err = zfs_id_to_fuidstr(zfsvfs, domain, rid, buf, sizeof (buf), B_TRUE);
331 	if (err)
332 		return (err);
333 	fuid_dirtied = zfsvfs->z_fuid_dirty;
334 
335 	tx = dmu_tx_create(zfsvfs->z_os);
336 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
337 	if (*objp == 0) {
338 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
339 		    zfs_userquota_prop_prefixes[type]);
340 	}
341 	if (fuid_dirtied)
342 		zfs_fuid_txhold(zfsvfs, tx);
343 	err = dmu_tx_assign(tx, DMU_TX_WAIT);
344 	if (err) {
345 		dmu_tx_abort(tx);
346 		return (err);
347 	}
348 
349 	mutex_enter(&zfsvfs->z_lock);
350 	if (*objp == 0) {
351 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
352 		    DMU_OT_NONE, 0, tx);
353 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
354 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
355 	}
356 	mutex_exit(&zfsvfs->z_lock);
357 
358 	if (quota == 0) {
359 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
360 		if (err == ENOENT)
361 			err = 0;
362 	} else {
363 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
364 	}
365 	ASSERT(err == 0);
366 	if (fuid_dirtied)
367 		zfs_fuid_sync(zfsvfs, tx);
368 	dmu_tx_commit(tx);
369 	return (err);
370 }
371 
372 boolean_t
zfs_id_overobjquota(zfsvfs_t * zfsvfs,uint64_t usedobj,uint64_t id)373 zfs_id_overobjquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
374 {
375 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
376 	uint64_t used, quota, quotaobj;
377 	int err;
378 
379 	if (!dmu_objset_userobjspace_present(zfsvfs->z_os)) {
380 		if (dmu_objset_userobjspace_upgradable(zfsvfs->z_os)) {
381 			dsl_pool_config_enter(
382 			    dmu_objset_pool(zfsvfs->z_os), FTAG);
383 			dmu_objset_id_quota_upgrade(zfsvfs->z_os);
384 			dsl_pool_config_exit(
385 			    dmu_objset_pool(zfsvfs->z_os), FTAG);
386 		}
387 		return (B_FALSE);
388 	}
389 
390 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
391 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
392 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
393 				dsl_pool_config_enter(
394 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
395 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
396 				dsl_pool_config_exit(
397 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
398 			}
399 			return (B_FALSE);
400 		}
401 		quotaobj = zfsvfs->z_projectobjquota_obj;
402 	} else if (usedobj == DMU_USERUSED_OBJECT) {
403 		quotaobj = zfsvfs->z_userobjquota_obj;
404 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
405 		quotaobj = zfsvfs->z_groupobjquota_obj;
406 	} else {
407 		return (B_FALSE);
408 	}
409 	if (quotaobj == 0 || zfsvfs->z_replay)
410 		return (B_FALSE);
411 
412 	(void) snprintf(buf, sizeof (buf), "%llx", (longlong_t)id);
413 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
414 	if (err != 0)
415 		return (B_FALSE);
416 
417 	(void) snprintf(buf, sizeof (buf), DMU_OBJACCT_PREFIX "%llx",
418 	    (longlong_t)id);
419 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
420 	if (err != 0)
421 		return (B_FALSE);
422 	return (used >= quota);
423 }
424 
425 boolean_t
zfs_id_overblockquota(zfsvfs_t * zfsvfs,uint64_t usedobj,uint64_t id)426 zfs_id_overblockquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
427 {
428 	char buf[20];
429 	uint64_t used, quota, quotaobj;
430 	int err;
431 
432 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
433 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
434 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
435 				dsl_pool_config_enter(
436 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
437 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
438 				dsl_pool_config_exit(
439 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
440 			}
441 			return (B_FALSE);
442 		}
443 		quotaobj = zfsvfs->z_projectquota_obj;
444 	} else if (usedobj == DMU_USERUSED_OBJECT) {
445 		quotaobj = zfsvfs->z_userquota_obj;
446 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
447 		quotaobj = zfsvfs->z_groupquota_obj;
448 	} else {
449 		return (B_FALSE);
450 	}
451 	if (quotaobj == 0 || zfsvfs->z_replay)
452 		return (B_FALSE);
453 
454 	(void) snprintf(buf, sizeof (buf), "%llx", (longlong_t)id);
455 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
456 	if (err != 0)
457 		return (B_FALSE);
458 
459 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
460 	if (err != 0)
461 		return (B_FALSE);
462 	return (used >= quota);
463 }
464 
465 boolean_t
zfs_id_overquota(zfsvfs_t * zfsvfs,uint64_t usedobj,uint64_t id)466 zfs_id_overquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
467 {
468 	return (zfs_id_overblockquota(zfsvfs, usedobj, id) ||
469 	    zfs_id_overobjquota(zfsvfs, usedobj, id));
470 }
471 
472 EXPORT_SYMBOL(zpl_get_file_info);
473 EXPORT_SYMBOL(zfs_userspace_one);
474 EXPORT_SYMBOL(zfs_userspace_many);
475 EXPORT_SYMBOL(zfs_set_userquota);
476 EXPORT_SYMBOL(zfs_id_overblockquota);
477 EXPORT_SYMBOL(zfs_id_overobjquota);
478 EXPORT_SYMBOL(zfs_id_overquota);
479