| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/quota.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "blockdev-util.h" |
| 8 |
#include "chattr-util.h" |
| 9 |
#include "device-util.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "quota-util.h" |
| 12 |
|
| 13 |
int quotactl_fd_with_fallback(int fd, int cmd, int id, void *addr) { |
| 14 |
int r; |
| 15 |
|
| 16 |
/* Emulates quotactl_fd() on older kernels that lack it. (i.e. kernels < 5.14) */ |
| 17 |
|
| 18 |
r = RET_NERRNO(quotactl_fd(fd, cmd, id, addr)); |
| 19 |
if (!ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 20 |
return r; |
| 21 |
|
| 22 |
dev_t devno; |
| 23 |
r = get_block_device_fd(fd, &devno); |
| 24 |
if (r < 0) |
| 25 |
return r; |
| 26 |
if (devno == 0) /* Doesn't have a block device */ |
| 27 |
return -ENODEV; |
| 28 |
|
| 29 |
_cleanup_free_ char *devnode = NULL; |
| 30 |
r = devname_from_devnum(S_IFBLK, devno, &devnode); |
| 31 |
if (r < 0) |
| 32 |
return r; |
| 33 |
|
| 34 |
return RET_NERRNO(quotactl(cmd, devnode, id, addr)); |
| 35 |
} |
| 36 |
|
| 37 |
int quota_query_proj_id(int fd, uint32_t proj_id, struct dqblk *ret_req) { |
| 38 |
int r; |
| 39 |
|
| 40 |
assert(fd >= 0); |
| 41 |
assert(ret_req); |
| 42 |
|
| 43 |
r = quotactl_fd_with_fallback(fd, QCMD_FIXED(Q_GETQUOTA, PRJQUOTA), proj_id, ret_req); |
| 44 |
if (r == -ESRCH || ERRNO_IS_NEG_NOT_SUPPORTED(r) || ERRNO_IS_NEG_PRIVILEGE(r)) { |
| 45 |
*ret_req = (struct dqblk) {}; |
| 46 |
return false; |
| 47 |
} |
| 48 |
if (r < 0) |
| 49 |
return r; |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
int quota_proj_id_set_recursive(int fd, uint32_t proj_id, bool verify_exclusive) { |
| 55 |
int r; |
| 56 |
|
| 57 |
assert(fd >= 0); |
| 58 |
|
| 59 |
/* Confirm only the current inode has the project id (in case of race conditions) */ |
| 60 |
if (verify_exclusive) { |
| 61 |
/* Set to top level first because of the case where directories already exist with multiple subdirectories, |
| 62 |
* in which case, number of inodes will be > 1 if applied recursively only */ |
| 63 |
r = set_proj_id(fd, proj_id); |
| 64 |
if (r < 0) |
| 65 |
return r; |
| 66 |
|
| 67 |
struct dqblk req; |
| 68 |
r = quotactl_fd_with_fallback(fd, QCMD_FIXED(Q_GETQUOTA, PRJQUOTA), proj_id, &req); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
|
| 72 |
if (req.dqb_curinodes == 0) |
| 73 |
return -ENOTRECOVERABLE; |
| 74 |
|
| 75 |
if (req.dqb_curinodes != 1) |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
r = set_proj_id_recursive(fd, proj_id); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
bool quota_dqblk_is_populated(const struct dqblk *req) { |
| 87 |
assert(req); |
| 88 |
|
| 89 |
return FLAGS_SET(req->dqb_valid, QIF_BLIMITS|QIF_SPACE|QIF_ILIMITS|QIF_INODES|QIF_BTIME|QIF_ITIME) && |
| 90 |
(req->dqb_bhardlimit > 0 || |
| 91 |
req->dqb_bsoftlimit > 0 || |
| 92 |
req->dqb_ihardlimit > 0 || |
| 93 |
req->dqb_isoftlimit > 0 || |
| 94 |
req->dqb_curspace > 0 || |
| 95 |
req->dqb_curinodes > 0 || |
| 96 |
req->dqb_btime > 0 || |
| 97 |
req->dqb_itime > 0); |
| 98 |
} |