4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2017, Intle Corporation. All rights reserved.
37 #include <sys/types.h>
39 #include <sys/zfs_project.h>
42 #include "zfs_projectutil.h"
44 typedef struct zfs_project_item {
50 zfs_project_item_alloc(list_t *head, const char *name)
52 zfs_project_item_t *zpi;
54 zpi = safe_malloc(sizeof (zfs_project_item_t) + strlen(name) + 1);
55 strcpy(zpi->zpi_name, name);
56 list_insert_tail(head, zpi);
60 zfs_project_sanity_check(const char *name, zfs_project_control_t *zpc,
67 (void) fprintf(stderr, gettext("failed to stat %s: %s\n"),
68 name, strerror(errno));
72 if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) {
73 (void) fprintf(stderr, gettext("only support project quota on "
74 "regular file or directory\n"));
78 if (!S_ISDIR(st->st_mode)) {
79 if (zpc->zpc_dironly) {
80 (void) fprintf(stderr, gettext(
81 "'-d' option on non-dir target %s\n"), name);
85 if (zpc->zpc_recursive) {
86 (void) fprintf(stderr, gettext(
87 "'-r' option on non-dir target %s\n"), name);
96 zfs_project_load_projid(const char *name, zfs_project_control_t *zpc)
101 fd = open(name, O_RDONLY | O_NOCTTY);
103 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
104 name, strerror(errno));
108 ret = ioctl(fd, ZFS_IOC_FSGETXATTR, &fsx);
110 (void) fprintf(stderr,
111 gettext("failed to get xattr for %s: %s\n"),
112 name, strerror(errno));
114 zpc->zpc_expected_projid = fsx.fsx_projid;
121 zfs_project_handle_one(const char *name, zfs_project_control_t *zpc)
126 fd = open(name, O_RDONLY | O_NOCTTY);
128 if (errno == ENOENT && zpc->zpc_ignore_noent)
131 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
132 name, strerror(errno));
136 ret = ioctl(fd, ZFS_IOC_FSGETXATTR, &fsx);
138 (void) fprintf(stderr,
139 gettext("failed to get xattr for %s: %s\n"),
140 name, strerror(errno));
144 switch (zpc->zpc_op) {
145 case ZFS_PROJECT_OP_LIST:
146 (void) printf("%5u %c %s\n", fsx.fsx_projid,
147 (fsx.fsx_xflags & ZFS_PROJINHERIT_FL) ? 'P' : '-', name);
149 case ZFS_PROJECT_OP_CHECK:
150 if (fsx.fsx_projid == zpc->zpc_expected_projid &&
151 fsx.fsx_xflags & ZFS_PROJINHERIT_FL)
154 if (!zpc->zpc_newline) {
157 (void) printf("%s%c", name, c);
161 if (fsx.fsx_projid != zpc->zpc_expected_projid)
162 (void) printf("%s - project ID is not set properly "
163 "(%u/%u)\n", name, fsx.fsx_projid,
164 (uint32_t)zpc->zpc_expected_projid);
166 if (!(fsx.fsx_xflags & ZFS_PROJINHERIT_FL))
167 (void) printf("%s - project inherit flag is not set\n",
171 case ZFS_PROJECT_OP_CLEAR:
172 if (!(fsx.fsx_xflags & ZFS_PROJINHERIT_FL) &&
173 (zpc->zpc_keep_projid ||
174 fsx.fsx_projid == ZFS_DEFAULT_PROJID))
177 fsx.fsx_xflags &= ~ZFS_PROJINHERIT_FL;
178 if (!zpc->zpc_keep_projid)
179 fsx.fsx_projid = ZFS_DEFAULT_PROJID;
181 case ZFS_PROJECT_OP_SET:
182 if (fsx.fsx_projid == zpc->zpc_expected_projid &&
183 (!zpc->zpc_set_flag || fsx.fsx_xflags & ZFS_PROJINHERIT_FL))
186 fsx.fsx_projid = zpc->zpc_expected_projid;
187 if (zpc->zpc_set_flag)
188 fsx.fsx_xflags |= ZFS_PROJINHERIT_FL;
195 ret = ioctl(fd, ZFS_IOC_FSSETXATTR, &fsx);
197 (void) fprintf(stderr,
198 gettext("failed to set xattr for %s: %s\n"),
199 name, strerror(errno));
207 zfs_project_handle_dir(const char *name, zfs_project_control_t *zpc,
210 char fullname[PATH_MAX];
217 if (errno == ENOENT && zpc->zpc_ignore_noent)
221 (void) fprintf(stderr, gettext("failed to opendir %s: %s\n"),
222 name, strerror(errno));
226 /* Non-top item, ignore the case of being removed or renamed by race. */
227 zpc->zpc_ignore_noent = B_TRUE;
229 while (!ret && (ent = readdir(dir)) != NULL) {
230 /* skip "." and ".." */
231 if (strcmp(ent->d_name, ".") == 0 ||
232 strcmp(ent->d_name, "..") == 0)
235 if (strlen(ent->d_name) + strlen(name) >=
236 sizeof (fullname) + 1) {
237 errno = ENAMETOOLONG;
241 sprintf(fullname, "%s/%s", name, ent->d_name);
242 ret = zfs_project_handle_one(fullname, zpc);
243 if (!ret && zpc->zpc_recursive && ent->d_type == DT_DIR)
244 zfs_project_item_alloc(head, fullname);
249 (void) fprintf(stderr, gettext("failed to readdir %s: %s\n"),
250 name, strerror(errno));
258 zfs_project_handle(const char *name, zfs_project_control_t *zpc)
260 zfs_project_item_t *zpi;
265 ret = zfs_project_sanity_check(name, zpc, &st);
269 if ((zpc->zpc_op == ZFS_PROJECT_OP_SET ||
270 zpc->zpc_op == ZFS_PROJECT_OP_CHECK) &&
271 zpc->zpc_expected_projid == ZFS_INVALID_PROJID) {
272 ret = zfs_project_load_projid(name, zpc);
277 zpc->zpc_ignore_noent = B_FALSE;
278 ret = zfs_project_handle_one(name, zpc);
279 if (ret || !S_ISDIR(st.st_mode) || zpc->zpc_dironly ||
280 (!zpc->zpc_recursive &&
281 zpc->zpc_op != ZFS_PROJECT_OP_LIST &&
282 zpc->zpc_op != ZFS_PROJECT_OP_CHECK))
285 list_create(&head, sizeof (zfs_project_item_t),
286 offsetof(zfs_project_item_t, zpi_list));
287 zfs_project_item_alloc(&head, name);
288 while ((zpi = list_remove_head(&head)) != NULL) {
290 ret = zfs_project_handle_dir(zpi->zpi_name, zpc, &head);