Whamcloud - gitweb
LU-11872 utils: don't follow link files in default
[fs/lustre-release.git] / lustre / utils / lfs_project.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2017, DataDirect Networks Storage.
24  * Copyright (c) 2017, Intel Corporation.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  *
29  * lustre/utils/lfs_project.c
30  *
31  * Author: Wang Shilong <wshilong@ddn.com>
32  * Author: Fan Yong <fan.yong@intel.com>
33  */
34 #include <errno.h>
35 #include <getopt.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <dirent.h>
42 #include <stddef.h>
43 #include <libintl.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <string.h>
47 #include <libcfs/util/list.h>
48 #include <libcfs/util/ioctl.h>
49 #include <sys/ioctl.h>
50
51 #include "lfs_project.h"
52 #include <lustre/lustreapi.h>
53
54 struct lfs_project_item {
55         struct list_head lpi_list;
56         char lpi_pathname[PATH_MAX];
57 };
58
59 static int
60 lfs_project_item_alloc(struct list_head *head, const char *pathname)
61 {
62         struct lfs_project_item *lpi;
63
64         lpi = malloc(sizeof(struct lfs_project_item));
65         if (lpi == NULL) {
66                 fprintf(stderr,
67                         "%s: cannot allocate project item for '%s': %s\n",
68                         progname, pathname, strerror(ENOMEM));
69                 return -ENOMEM;
70         }
71
72         strncpy(lpi->lpi_pathname, pathname, sizeof(lpi->lpi_pathname));
73         list_add_tail(&lpi->lpi_list, head);
74
75         return 0;
76 }
77
78 static const char *mode_to_type(mode_t mode)
79 {
80         switch (mode & S_IFMT) {
81         case S_IFDIR:   return "dir";
82         case S_IFREG:   return "regular";
83         case S_IFLNK:   return "symlink";
84         case S_IFCHR:   return "char device";
85         case S_IFBLK:   return "block device";
86         case S_IFIFO:   return "fifo";
87         case S_IFSOCK:  return "sock";
88         }
89
90         return "unknown";
91 }
92
93 static int project_get_xattr(const char *pathname, struct fsxattr *fsx)
94 {
95         int ret, fd;
96         struct stat st;
97
98         ret = lstat(pathname, &st);
99         if (ret) {
100                 fprintf(stderr, "%s: failed to stat '%s': %s\n",
101                         progname, pathname, strerror(errno));
102                 return -errno;
103         }
104
105         /* currently, only file and dir supported */
106         if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
107                 errno = ENOTSUP;
108                 fprintf(stderr, "%s: unable to get xattr for %s '%s': %s\n",
109                                 progname, mode_to_type(st.st_mode), pathname,
110                                 strerror(errno));
111                 return -errno;
112         }
113
114         fd = open(pathname, O_RDONLY | O_NOCTTY | O_NDELAY);
115         if (fd < 0) {
116                 fprintf(stderr, "%s: failed to open '%s': %s\n",
117                         progname, pathname, strerror(errno));
118                 return -errno;
119         }
120
121         ret = ioctl(fd, LL_IOC_FSGETXATTR, fsx);
122         if (ret) {
123                 fprintf(stderr, "%s: failed to get xattr for '%s': %s\n",
124                         progname, pathname, strerror(errno));
125                 close(fd);
126                 return -errno;
127         }
128         return fd;
129 }
130
131 static int
132 project_check_one(const char *pathname, struct project_handle_control *phc)
133 {
134         struct fsxattr fsx;
135         int ret;
136
137         ret = project_get_xattr(pathname, &fsx);
138         if (ret < 0)
139                 return ret;
140
141         /* use top directory's project ID if not specified */
142         if (!phc->assign_projid) {
143                 phc->assign_projid = true;
144                 phc->projid = fsx.fsx_projid;
145         }
146
147         if (!(fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)) {
148                 if (!phc->newline) {
149                         printf("%s%c", pathname, '\0');
150                         goto out;
151                 }
152                  printf("%s - project inheritance flag is not set\n",
153                         pathname);
154         }
155
156         if (fsx.fsx_projid != phc->projid) {
157                 if (!phc->newline) {
158                         printf("%s%c", pathname, '\0');
159                         goto out;
160                 }
161                 printf("%s - project identifier is not set (inode=%u, tree=%u)\n",
162                        pathname, fsx.fsx_projid, phc->projid);
163         }
164 out:
165         close(ret);
166         return 0;
167 }
168
169 static int
170 project_list_one(const char *pathname, struct project_handle_control *phc)
171 {
172         struct fsxattr fsx;
173         int ret;
174
175         ret = project_get_xattr(pathname, &fsx);
176         if (ret < 0)
177                 return ret;
178
179         printf("%5u %c %s\n", fsx.fsx_projid,
180                (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT) ?
181                 'P' : '-', pathname);
182
183         close(ret);
184         return 0;
185 }
186
187 static int
188 project_set_one(const char *pathname, struct project_handle_control *phc)
189 {
190         struct fsxattr fsx;
191         int fd, ret = 0;
192
193         fd = project_get_xattr(pathname, &fsx);
194         if (fd < 0)
195                 return fd;
196
197         if ((!phc->set_projid || fsx.fsx_projid == phc->projid) &&
198             (!phc->set_inherit || (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)))
199                 goto out;
200
201         if (phc->set_inherit)
202                 fsx.fsx_xflags |= FS_XFLAG_PROJINHERIT;
203         if (phc->set_projid)
204                 fsx.fsx_projid = phc->projid;
205
206         ret = ioctl(fd, LL_IOC_FSSETXATTR, &fsx);
207         if (ret)
208                 fprintf(stderr, "%s: failed to set xattr for '%s': %s\n",
209                         progname, pathname, strerror(errno));
210 out:
211         close(fd);
212         return ret;
213 }
214
215 static int
216 project_clear_one(const char *pathname, struct project_handle_control *phc)
217 {
218         struct fsxattr fsx;
219         int ret = 0, fd;
220
221         fd = project_get_xattr(pathname, &fsx);
222         if (fd < 0)
223                 return fd;
224
225         if ((!(fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)) &&
226              (fsx.fsx_projid == 0 || phc->keep_projid))
227                 goto out;
228
229         fsx.fsx_xflags &= ~FS_XFLAG_PROJINHERIT;
230         if (!phc->keep_projid)
231                 fsx.fsx_projid = 0;
232
233         ret = ioctl(fd, LL_IOC_FSSETXATTR, &fsx);
234         if (ret)
235                 fprintf(stderr, "%s: failed to set xattr for '%s': %s\n",
236                         progname, pathname, strerror(errno));
237 out:
238         close(fd);
239         return ret;
240 }
241
242 static int
243 lfs_project_handle_dir(struct list_head *head, const char *pathname,
244                        struct project_handle_control *phc,
245                        int (*func)(const char *,
246                                    struct project_handle_control *))
247 {
248         char fullname[PATH_MAX];
249         struct dirent *ent;
250         DIR *dir;
251         int ret = 0;
252         int rc;
253
254         dir = opendir(pathname);
255         if (dir == NULL) {
256                 ret = -errno;
257                 fprintf(stderr, "%s: failed to opendir '%s': %s\n",
258                         progname, pathname, strerror(-ret));
259                 return ret;
260         }
261
262         while ((ent = readdir(dir)) != NULL) {
263                 /* skip "." and ".." */
264                 if (strcmp(ent->d_name, ".") == 0 ||
265                     strcmp(ent->d_name, "..") == 0)
266                         continue;
267
268                 if (strlen(ent->d_name) + strlen(pathname) + 1 >=
269                     sizeof(fullname)) {
270                         ret = -ENAMETOOLONG;
271                         errno = ENAMETOOLONG;
272                         fprintf(stderr, "%s: ignored too long path: %s/%s\n",
273                                         progname, pathname, ent->d_name);
274                         continue;
275                 }
276                 snprintf(fullname, PATH_MAX, "%s/%s", pathname,
277                          ent->d_name);
278
279                 rc = func(fullname, phc);
280                 if (rc && !ret)
281                         ret = rc;
282                 if (phc->recursive && ent->d_type == DT_DIR) {
283                         rc = lfs_project_item_alloc(head, fullname);
284                         if (rc && !ret)
285                                 ret = rc;
286                 }
287         }
288
289         closedir(dir);
290         return ret;
291 }
292
293 static int lfs_project_iterate(const char *pathname,
294                                struct project_handle_control *phc,
295                                int (*func)(const char *,
296                                            struct project_handle_control *))
297 {
298         struct lfs_project_item *lpi;
299         struct list_head head;
300         struct stat st;
301         int ret = 0;
302         int rc = 0;
303
304         ret = stat(pathname, &st);
305         if (ret) {
306                 fprintf(stderr, "%s: failed to stat '%s': %s\n",
307                         progname, pathname, strerror(errno));
308                 return ret;
309         }
310
311         /* list opeation will skip top directory in default */
312         if (!S_ISDIR(st.st_mode) || phc->dironly ||
313             project_list_one != func)
314                 ret = func(pathname, phc);
315
316         /* dironly first, recursive will be ignored */
317         if (!S_ISDIR(st.st_mode) || phc->dironly || ret)
318                 return ret;
319
320         INIT_LIST_HEAD(&head);
321         ret = lfs_project_item_alloc(&head, pathname);
322         if (ret)
323                 return ret;
324
325         while (!list_empty(&head)) {
326                 lpi = list_entry(head.next, struct lfs_project_item, lpi_list);
327                 list_del(&lpi->lpi_list);
328                 rc = lfs_project_handle_dir(&head, lpi->lpi_pathname,
329                                              phc, func);
330                 if (!ret && rc)
331                         ret = rc;
332                 free(lpi);
333         }
334
335         return ret;
336 }
337
338
339 inline int lfs_project_check(const char *pathname,
340                              struct project_handle_control *phc)
341 {
342         return lfs_project_iterate(pathname, phc, project_check_one);
343 }
344
345 inline int lfs_project_clear(const char *pathname,
346                              struct project_handle_control *phc)
347 {
348         return lfs_project_iterate(pathname, phc, project_clear_one);
349 }
350
351 inline int lfs_project_set(const char *pathname,
352                            struct project_handle_control *phc)
353 {
354         return lfs_project_iterate(pathname, phc, project_set_one);
355 }
356
357 inline int lfs_project_list(const char *pathname,
358                             struct project_handle_control *phc)
359 {
360         return lfs_project_iterate(pathname, phc, project_list_one);
361 }