1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy * CDDL HEADER START
4eda14cbcSMatt Macy *
5eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy *
9eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy * See the License for the specific language governing permissions
12eda14cbcSMatt Macy * and limitations under the License.
13eda14cbcSMatt Macy *
14eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy *
20eda14cbcSMatt Macy * CDDL HEADER END
21eda14cbcSMatt Macy */
22eda14cbcSMatt Macy
23eda14cbcSMatt Macy /*
24eda14cbcSMatt Macy * Copyright (c) 2017, Intle Corporation. All rights reserved.
25eda14cbcSMatt Macy */
26eda14cbcSMatt Macy
27eda14cbcSMatt Macy #include <errno.h>
28eda14cbcSMatt Macy #include <getopt.h>
29eda14cbcSMatt Macy #include <stdio.h>
30eda14cbcSMatt Macy #include <stdlib.h>
31da5137abSMartin Matuska #include <string.h>
32eda14cbcSMatt Macy #include <unistd.h>
33eda14cbcSMatt Macy #include <fcntl.h>
34eda14cbcSMatt Macy #include <dirent.h>
35eda14cbcSMatt Macy #include <stddef.h>
36eda14cbcSMatt Macy #include <libintl.h>
37eda14cbcSMatt Macy #include <sys/stat.h>
38eda14cbcSMatt Macy #include <sys/types.h>
39eda14cbcSMatt Macy #include <sys/list.h>
40eda14cbcSMatt Macy #include <sys/zfs_project.h>
41eda14cbcSMatt Macy
42eda14cbcSMatt Macy #include "zfs_util.h"
43eda14cbcSMatt Macy #include "zfs_projectutil.h"
44eda14cbcSMatt Macy
45eda14cbcSMatt Macy typedef struct zfs_project_item {
46eda14cbcSMatt Macy list_node_t zpi_list;
47eda14cbcSMatt Macy char zpi_name[0];
48eda14cbcSMatt Macy } zfs_project_item_t;
49eda14cbcSMatt Macy
50eda14cbcSMatt Macy static void
zfs_project_item_alloc(list_t * head,const char * name)51eda14cbcSMatt Macy zfs_project_item_alloc(list_t *head, const char *name)
52eda14cbcSMatt Macy {
53eda14cbcSMatt Macy zfs_project_item_t *zpi;
54eda14cbcSMatt Macy
55eda14cbcSMatt Macy zpi = safe_malloc(sizeof (zfs_project_item_t) + strlen(name) + 1);
56eda14cbcSMatt Macy strcpy(zpi->zpi_name, name);
57eda14cbcSMatt Macy list_insert_tail(head, zpi);
58eda14cbcSMatt Macy }
59eda14cbcSMatt Macy
60eda14cbcSMatt Macy static int
zfs_project_sanity_check(const char * name,zfs_project_control_t * zpc,struct stat * st)61eda14cbcSMatt Macy zfs_project_sanity_check(const char *name, zfs_project_control_t *zpc,
62eda14cbcSMatt Macy struct stat *st)
63eda14cbcSMatt Macy {
64eda14cbcSMatt Macy int ret;
65eda14cbcSMatt Macy
66eda14cbcSMatt Macy ret = stat(name, st);
67eda14cbcSMatt Macy if (ret) {
68eda14cbcSMatt Macy (void) fprintf(stderr, gettext("failed to stat %s: %s\n"),
69eda14cbcSMatt Macy name, strerror(errno));
70eda14cbcSMatt Macy return (ret);
71eda14cbcSMatt Macy }
72eda14cbcSMatt Macy
73eda14cbcSMatt Macy if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode)) {
74eda14cbcSMatt Macy (void) fprintf(stderr, gettext("only support project quota on "
75eda14cbcSMatt Macy "regular file or directory\n"));
76eda14cbcSMatt Macy return (-1);
77eda14cbcSMatt Macy }
78eda14cbcSMatt Macy
79eda14cbcSMatt Macy if (!S_ISDIR(st->st_mode)) {
80eda14cbcSMatt Macy if (zpc->zpc_dironly) {
81eda14cbcSMatt Macy (void) fprintf(stderr, gettext(
82eda14cbcSMatt Macy "'-d' option on non-dir target %s\n"), name);
83eda14cbcSMatt Macy return (-1);
84eda14cbcSMatt Macy }
85eda14cbcSMatt Macy
86eda14cbcSMatt Macy if (zpc->zpc_recursive) {
87eda14cbcSMatt Macy (void) fprintf(stderr, gettext(
88eda14cbcSMatt Macy "'-r' option on non-dir target %s\n"), name);
89eda14cbcSMatt Macy return (-1);
90eda14cbcSMatt Macy }
91eda14cbcSMatt Macy }
92eda14cbcSMatt Macy
93eda14cbcSMatt Macy return (0);
94eda14cbcSMatt Macy }
95eda14cbcSMatt Macy
96eda14cbcSMatt Macy static int
zfs_project_load_projid(const char * name,zfs_project_control_t * zpc)97eda14cbcSMatt Macy zfs_project_load_projid(const char *name, zfs_project_control_t *zpc)
98eda14cbcSMatt Macy {
99eda14cbcSMatt Macy zfsxattr_t fsx;
100eda14cbcSMatt Macy int ret, fd;
101eda14cbcSMatt Macy
102eda14cbcSMatt Macy fd = open(name, O_RDONLY | O_NOCTTY);
103eda14cbcSMatt Macy if (fd < 0) {
104eda14cbcSMatt Macy (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
105eda14cbcSMatt Macy name, strerror(errno));
106eda14cbcSMatt Macy return (fd);
107eda14cbcSMatt Macy }
108eda14cbcSMatt Macy
109eda14cbcSMatt Macy ret = ioctl(fd, ZFS_IOC_FSGETXATTR, &fsx);
110eda14cbcSMatt Macy if (ret)
111eda14cbcSMatt Macy (void) fprintf(stderr,
112eda14cbcSMatt Macy gettext("failed to get xattr for %s: %s\n"),
113eda14cbcSMatt Macy name, strerror(errno));
114eda14cbcSMatt Macy else
115eda14cbcSMatt Macy zpc->zpc_expected_projid = fsx.fsx_projid;
116eda14cbcSMatt Macy
117eda14cbcSMatt Macy close(fd);
118eda14cbcSMatt Macy return (ret);
119eda14cbcSMatt Macy }
120eda14cbcSMatt Macy
121eda14cbcSMatt Macy static int
zfs_project_handle_one(const char * name,zfs_project_control_t * zpc)122eda14cbcSMatt Macy zfs_project_handle_one(const char *name, zfs_project_control_t *zpc)
123eda14cbcSMatt Macy {
124eda14cbcSMatt Macy zfsxattr_t fsx;
125eda14cbcSMatt Macy int ret, fd;
126eda14cbcSMatt Macy
127eda14cbcSMatt Macy fd = open(name, O_RDONLY | O_NOCTTY);
128eda14cbcSMatt Macy if (fd < 0) {
129eda14cbcSMatt Macy if (errno == ENOENT && zpc->zpc_ignore_noent)
130eda14cbcSMatt Macy return (0);
131eda14cbcSMatt Macy
132eda14cbcSMatt Macy (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
133eda14cbcSMatt Macy name, strerror(errno));
134eda14cbcSMatt Macy return (fd);
135eda14cbcSMatt Macy }
136eda14cbcSMatt Macy
137eda14cbcSMatt Macy ret = ioctl(fd, ZFS_IOC_FSGETXATTR, &fsx);
138eda14cbcSMatt Macy if (ret) {
139eda14cbcSMatt Macy (void) fprintf(stderr,
140eda14cbcSMatt Macy gettext("failed to get xattr for %s: %s\n"),
141eda14cbcSMatt Macy name, strerror(errno));
142eda14cbcSMatt Macy goto out;
143eda14cbcSMatt Macy }
144eda14cbcSMatt Macy
145eda14cbcSMatt Macy switch (zpc->zpc_op) {
146eda14cbcSMatt Macy case ZFS_PROJECT_OP_LIST:
147eda14cbcSMatt Macy (void) printf("%5u %c %s\n", fsx.fsx_projid,
148eda14cbcSMatt Macy (fsx.fsx_xflags & ZFS_PROJINHERIT_FL) ? 'P' : '-', name);
149eda14cbcSMatt Macy goto out;
150eda14cbcSMatt Macy case ZFS_PROJECT_OP_CHECK:
151eda14cbcSMatt Macy if (fsx.fsx_projid == zpc->zpc_expected_projid &&
152eda14cbcSMatt Macy fsx.fsx_xflags & ZFS_PROJINHERIT_FL)
153eda14cbcSMatt Macy goto out;
154eda14cbcSMatt Macy
155eda14cbcSMatt Macy if (!zpc->zpc_newline) {
156eda14cbcSMatt Macy char c = '\0';
157eda14cbcSMatt Macy
158eda14cbcSMatt Macy (void) printf("%s%c", name, c);
159eda14cbcSMatt Macy goto out;
160eda14cbcSMatt Macy }
161eda14cbcSMatt Macy
162eda14cbcSMatt Macy if (fsx.fsx_projid != zpc->zpc_expected_projid)
163eda14cbcSMatt Macy (void) printf("%s - project ID is not set properly "
164eda14cbcSMatt Macy "(%u/%u)\n", name, fsx.fsx_projid,
165eda14cbcSMatt Macy (uint32_t)zpc->zpc_expected_projid);
166eda14cbcSMatt Macy
167eda14cbcSMatt Macy if (!(fsx.fsx_xflags & ZFS_PROJINHERIT_FL))
168eda14cbcSMatt Macy (void) printf("%s - project inherit flag is not set\n",
169eda14cbcSMatt Macy name);
170eda14cbcSMatt Macy
171eda14cbcSMatt Macy goto out;
172eda14cbcSMatt Macy case ZFS_PROJECT_OP_CLEAR:
173eda14cbcSMatt Macy if (!(fsx.fsx_xflags & ZFS_PROJINHERIT_FL) &&
174eda14cbcSMatt Macy (zpc->zpc_keep_projid ||
175eda14cbcSMatt Macy fsx.fsx_projid == ZFS_DEFAULT_PROJID))
176eda14cbcSMatt Macy goto out;
177eda14cbcSMatt Macy
178eda14cbcSMatt Macy fsx.fsx_xflags &= ~ZFS_PROJINHERIT_FL;
179eda14cbcSMatt Macy if (!zpc->zpc_keep_projid)
180eda14cbcSMatt Macy fsx.fsx_projid = ZFS_DEFAULT_PROJID;
181eda14cbcSMatt Macy break;
182eda14cbcSMatt Macy case ZFS_PROJECT_OP_SET:
183eda14cbcSMatt Macy if (fsx.fsx_projid == zpc->zpc_expected_projid &&
184eda14cbcSMatt Macy (!zpc->zpc_set_flag || fsx.fsx_xflags & ZFS_PROJINHERIT_FL))
185eda14cbcSMatt Macy goto out;
186eda14cbcSMatt Macy
187eda14cbcSMatt Macy fsx.fsx_projid = zpc->zpc_expected_projid;
188eda14cbcSMatt Macy if (zpc->zpc_set_flag)
189eda14cbcSMatt Macy fsx.fsx_xflags |= ZFS_PROJINHERIT_FL;
190eda14cbcSMatt Macy break;
191eda14cbcSMatt Macy default:
192eda14cbcSMatt Macy ASSERT(0);
193eda14cbcSMatt Macy break;
194eda14cbcSMatt Macy }
195eda14cbcSMatt Macy
196eda14cbcSMatt Macy ret = ioctl(fd, ZFS_IOC_FSSETXATTR, &fsx);
197eda14cbcSMatt Macy if (ret)
198eda14cbcSMatt Macy (void) fprintf(stderr,
199eda14cbcSMatt Macy gettext("failed to set xattr for %s: %s\n"),
200eda14cbcSMatt Macy name, strerror(errno));
201eda14cbcSMatt Macy
202eda14cbcSMatt Macy out:
203eda14cbcSMatt Macy close(fd);
204eda14cbcSMatt Macy return (ret);
205eda14cbcSMatt Macy }
206eda14cbcSMatt Macy
207eda14cbcSMatt Macy static int
zfs_project_handle_dir(const char * name,zfs_project_control_t * zpc,list_t * head)208eda14cbcSMatt Macy zfs_project_handle_dir(const char *name, zfs_project_control_t *zpc,
209eda14cbcSMatt Macy list_t *head)
210eda14cbcSMatt Macy {
211eda14cbcSMatt Macy struct dirent *ent;
212eda14cbcSMatt Macy DIR *dir;
213eda14cbcSMatt Macy int ret = 0;
214eda14cbcSMatt Macy
215eda14cbcSMatt Macy dir = opendir(name);
216eda14cbcSMatt Macy if (dir == NULL) {
217eda14cbcSMatt Macy if (errno == ENOENT && zpc->zpc_ignore_noent)
218eda14cbcSMatt Macy return (0);
219eda14cbcSMatt Macy
220eda14cbcSMatt Macy ret = -errno;
221eda14cbcSMatt Macy (void) fprintf(stderr, gettext("failed to opendir %s: %s\n"),
222eda14cbcSMatt Macy name, strerror(errno));
223eda14cbcSMatt Macy return (ret);
224eda14cbcSMatt Macy }
225eda14cbcSMatt Macy
226eda14cbcSMatt Macy /* Non-top item, ignore the case of being removed or renamed by race. */
227eda14cbcSMatt Macy zpc->zpc_ignore_noent = B_TRUE;
228eda14cbcSMatt Macy errno = 0;
229eda14cbcSMatt Macy while (!ret && (ent = readdir(dir)) != NULL) {
230a0b956f5SMartin Matuska char *fullname;
231a0b956f5SMartin Matuska
232eda14cbcSMatt Macy /* skip "." and ".." */
233eda14cbcSMatt Macy if (strcmp(ent->d_name, ".") == 0 ||
234eda14cbcSMatt Macy strcmp(ent->d_name, "..") == 0)
235eda14cbcSMatt Macy continue;
236eda14cbcSMatt Macy
237a0b956f5SMartin Matuska if (strlen(ent->d_name) + strlen(name) + 1 >= PATH_MAX) {
238eda14cbcSMatt Macy errno = ENAMETOOLONG;
239eda14cbcSMatt Macy break;
240eda14cbcSMatt Macy }
241eda14cbcSMatt Macy
242a0b956f5SMartin Matuska if (asprintf(&fullname, "%s/%s", name, ent->d_name) == -1) {
243a0b956f5SMartin Matuska errno = ENOMEM;
244a0b956f5SMartin Matuska break;
245a0b956f5SMartin Matuska }
246a0b956f5SMartin Matuska
247eda14cbcSMatt Macy ret = zfs_project_handle_one(fullname, zpc);
248eda14cbcSMatt Macy if (!ret && zpc->zpc_recursive && ent->d_type == DT_DIR)
249eda14cbcSMatt Macy zfs_project_item_alloc(head, fullname);
250a0b956f5SMartin Matuska
251a0b956f5SMartin Matuska free(fullname);
252eda14cbcSMatt Macy }
253eda14cbcSMatt Macy
254eda14cbcSMatt Macy if (errno && !ret) {
255eda14cbcSMatt Macy ret = -errno;
256eda14cbcSMatt Macy (void) fprintf(stderr, gettext("failed to readdir %s: %s\n"),
257eda14cbcSMatt Macy name, strerror(errno));
258eda14cbcSMatt Macy }
259eda14cbcSMatt Macy
260eda14cbcSMatt Macy closedir(dir);
261eda14cbcSMatt Macy return (ret);
262eda14cbcSMatt Macy }
263eda14cbcSMatt Macy
264eda14cbcSMatt Macy int
zfs_project_handle(const char * name,zfs_project_control_t * zpc)265eda14cbcSMatt Macy zfs_project_handle(const char *name, zfs_project_control_t *zpc)
266eda14cbcSMatt Macy {
267eda14cbcSMatt Macy zfs_project_item_t *zpi;
268eda14cbcSMatt Macy struct stat st;
269eda14cbcSMatt Macy list_t head;
270eda14cbcSMatt Macy int ret;
271eda14cbcSMatt Macy
272eda14cbcSMatt Macy ret = zfs_project_sanity_check(name, zpc, &st);
273eda14cbcSMatt Macy if (ret)
274eda14cbcSMatt Macy return (ret);
275eda14cbcSMatt Macy
276eda14cbcSMatt Macy if ((zpc->zpc_op == ZFS_PROJECT_OP_SET ||
277eda14cbcSMatt Macy zpc->zpc_op == ZFS_PROJECT_OP_CHECK) &&
278eda14cbcSMatt Macy zpc->zpc_expected_projid == ZFS_INVALID_PROJID) {
279eda14cbcSMatt Macy ret = zfs_project_load_projid(name, zpc);
280eda14cbcSMatt Macy if (ret)
281eda14cbcSMatt Macy return (ret);
282eda14cbcSMatt Macy }
283eda14cbcSMatt Macy
284eda14cbcSMatt Macy zpc->zpc_ignore_noent = B_FALSE;
285eda14cbcSMatt Macy ret = zfs_project_handle_one(name, zpc);
286eda14cbcSMatt Macy if (ret || !S_ISDIR(st.st_mode) || zpc->zpc_dironly ||
287eda14cbcSMatt Macy (!zpc->zpc_recursive &&
288eda14cbcSMatt Macy zpc->zpc_op != ZFS_PROJECT_OP_LIST &&
289eda14cbcSMatt Macy zpc->zpc_op != ZFS_PROJECT_OP_CHECK))
290eda14cbcSMatt Macy return (ret);
291eda14cbcSMatt Macy
292eda14cbcSMatt Macy list_create(&head, sizeof (zfs_project_item_t),
293eda14cbcSMatt Macy offsetof(zfs_project_item_t, zpi_list));
294eda14cbcSMatt Macy zfs_project_item_alloc(&head, name);
295eda14cbcSMatt Macy while ((zpi = list_remove_head(&head)) != NULL) {
296eda14cbcSMatt Macy if (!ret)
297eda14cbcSMatt Macy ret = zfs_project_handle_dir(zpi->zpi_name, zpc, &head);
298eda14cbcSMatt Macy free(zpi);
299eda14cbcSMatt Macy }
300eda14cbcSMatt Macy
301eda14cbcSMatt Macy return (ret);
302eda14cbcSMatt Macy }
303