4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2017, DataDirect Networks Storage.
24 * Copyright (c) 2017, Intel Corporation.
27 * This file is part of Lustre, http://www.lustre.org/
29 * lustre/utils/lfs_project.c
31 * Author: Wang Shilong <wshilong@ddn.com>
32 * Author: Fan Yong <fan.yong@intel.com>
45 #include <sys/types.h>
47 #include <libcfs/util/list.h>
48 #include <libcfs/util/ioctl.h>
49 #include <sys/ioctl.h>
51 #include "lfs_project.h"
52 #include <lustre/lustreapi.h>
54 struct lfs_project_item {
55 struct list_head lpi_list;
56 char lpi_pathname[PATH_MAX];
60 lfs_project_item_alloc(struct list_head *head, const char *pathname)
62 struct lfs_project_item *lpi;
64 lpi = malloc(sizeof(struct lfs_project_item));
67 "%s: cannot allocate project item for '%s': %s\n",
68 progname, pathname, strerror(ENOMEM));
72 strncpy(lpi->lpi_pathname, pathname, sizeof(lpi->lpi_pathname));
73 list_add_tail(&lpi->lpi_list, head);
78 static int project_get_xattr(const char *pathname, struct fsxattr *fsx)
82 fd = open(pathname, O_RDONLY | O_NOCTTY | O_NDELAY);
84 fprintf(stderr, "%s: failed to open '%s': %s\n",
85 progname, pathname, strerror(errno));
89 ret = ioctl(fd, LL_IOC_FSGETXATTR, fsx);
91 fprintf(stderr, "%s: failed to get xattr for '%s': %s\n",
92 progname, pathname, strerror(errno));
100 project_check_one(const char *pathname, struct project_handle_control *phc)
106 ret = stat(pathname, &st);
108 fprintf(stderr, "%s: failed to stat '%s': %s\n",
109 progname, pathname, strerror(errno));
113 ret = project_get_xattr(pathname, &fsx);
117 /* use top directory's project ID if not specified */
118 if (!phc->assign_projid) {
119 phc->assign_projid = true;
120 phc->projid = fsx.fsx_projid;
123 if (!(fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)) {
125 printf("%s%c", pathname, '\0');
128 printf("%s - project inheritance flag is not set\n",
132 if (fsx.fsx_projid != phc->projid) {
134 printf("%s%c", pathname, '\0');
137 printf("%s - project identifier is not set (inode=%u, tree=%u)\n",
138 pathname, fsx.fsx_projid, phc->projid);
146 project_list_one(const char *pathname, struct project_handle_control *phc)
151 ret = project_get_xattr(pathname, &fsx);
155 printf("%5u %c %s\n", fsx.fsx_projid,
156 (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT) ?
157 'P' : '-', pathname);
164 project_set_one(const char *pathname, struct project_handle_control *phc)
169 fd = project_get_xattr(pathname, &fsx);
173 if ((!phc->set_projid || fsx.fsx_projid == phc->projid) &&
174 (!phc->set_inherit || (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)))
177 if (phc->set_inherit)
178 fsx.fsx_xflags |= FS_XFLAG_PROJINHERIT;
180 fsx.fsx_projid = phc->projid;
182 ret = ioctl(fd, LL_IOC_FSSETXATTR, &fsx);
184 fprintf(stderr, "%s: failed to set xattr for '%s': %s\n",
185 progname, pathname, strerror(errno));
192 project_clear_one(const char *pathname, struct project_handle_control *phc)
197 fd = project_get_xattr(pathname, &fsx);
201 if ((!(fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)) &&
202 (fsx.fsx_projid == 0 || phc->keep_projid))
205 fsx.fsx_xflags &= ~FS_XFLAG_PROJINHERIT;
206 if (!phc->keep_projid)
209 ret = ioctl(fd, LL_IOC_FSSETXATTR, &fsx);
211 fprintf(stderr, "%s: failed to set xattr for '%s': %s\n",
212 progname, pathname, strerror(errno));
219 lfs_project_handle_dir(struct list_head *head, const char *pathname,
220 struct project_handle_control *phc,
221 int (*func)(const char *,
222 struct project_handle_control *))
224 char fullname[PATH_MAX];
230 dir = opendir(pathname);
233 fprintf(stderr, "%s: failed to opendir '%s': %s\n",
234 progname, pathname, strerror(-ret));
238 while ((ent = readdir(dir)) != NULL) {
239 /* skip "." and ".." */
240 if (strcmp(ent->d_name, ".") == 0 ||
241 strcmp(ent->d_name, "..") == 0)
244 if (strlen(ent->d_name) + strlen(pathname) + 1 >=
247 errno = ENAMETOOLONG;
248 fprintf(stderr, "%s: ignored too long path: %s/%s\n",
249 progname, pathname, ent->d_name);
252 snprintf(fullname, PATH_MAX, "%s/%s", pathname,
255 rc = func(fullname, phc);
258 if (phc->recursive && ent->d_type == DT_DIR) {
259 rc = lfs_project_item_alloc(head, fullname);
269 static int lfs_project_iterate(const char *pathname,
270 struct project_handle_control *phc,
271 int (*func)(const char *,
272 struct project_handle_control *))
274 struct lfs_project_item *lpi;
275 struct list_head head;
280 ret = stat(pathname, &st);
282 fprintf(stderr, "%s: failed to stat '%s': %s\n",
283 progname, pathname, strerror(errno));
287 /* list opeation will skip top directory in default */
288 if (!S_ISDIR(st.st_mode) || phc->dironly ||
289 project_list_one != func)
290 ret = func(pathname, phc);
292 /* dironly first, recursive will be ignored */
293 if (!S_ISDIR(st.st_mode) || phc->dironly || ret)
296 INIT_LIST_HEAD(&head);
297 ret = lfs_project_item_alloc(&head, pathname);
301 while (!list_empty(&head)) {
302 lpi = list_entry(head.next, struct lfs_project_item, lpi_list);
303 list_del(&lpi->lpi_list);
304 rc = lfs_project_handle_dir(&head, lpi->lpi_pathname,
315 inline int lfs_project_check(const char *pathname,
316 struct project_handle_control *phc)
318 return lfs_project_iterate(pathname, phc, project_check_one);
321 inline int lfs_project_clear(const char *pathname,
322 struct project_handle_control *phc)
324 return lfs_project_iterate(pathname, phc, project_clear_one);
327 inline int lfs_project_set(const char *pathname,
328 struct project_handle_control *phc)
330 return lfs_project_iterate(pathname, phc, project_set_one);
333 inline int lfs_project_list(const char *pathname,
334 struct project_handle_control *phc)
336 return lfs_project_iterate(pathname, phc, project_list_one);