Whamcloud - gitweb
New release 2.12.7
[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) - 1);
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                              struct stat *st)
95 {
96         int ret, fd;
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                 return -errno;
126         }
127         return fd;
128 }
129
130 static int
131 project_check_one(const char *pathname, struct project_handle_control *phc)
132 {
133         struct fsxattr fsx;
134         int ret;
135         struct stat st;
136
137         ret = project_get_xattr(pathname, &fsx, &st);
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             S_ISDIR(st.st_mode)) {
149                 if (!phc->newline) {
150                         printf("%s%c", pathname, '\0');
151                         goto out;
152                 }
153                  printf("%s - project inheritance flag is not set\n",
154                         pathname);
155         }
156
157         if (fsx.fsx_projid != phc->projid) {
158                 if (!phc->newline) {
159                         printf("%s%c", pathname, '\0');
160                         goto out;
161                 }
162                 printf("%s - project identifier is not set (inode=%u, tree=%u)\n",
163                        pathname, fsx.fsx_projid, phc->projid);
164         }
165 out:
166         close(ret);
167         return 0;
168 }
169
170 static int
171 project_list_one(const char *pathname, struct project_handle_control *phc)
172 {
173         struct fsxattr fsx;
174         struct stat st;
175         int ret;
176
177         ret = project_get_xattr(pathname, &fsx, &st);
178         if (ret < 0)
179                 return ret;
180
181         printf("%5u %c %s\n", fsx.fsx_projid,
182                (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT) ?
183                 'P' : '-', pathname);
184
185         close(ret);
186         return 0;
187 }
188
189 static int
190 project_set_one(const char *pathname, struct project_handle_control *phc)
191 {
192         struct fsxattr fsx;
193         struct stat st;
194         int fd, ret = 0;
195
196         fd = project_get_xattr(pathname, &fsx, &st);
197         if (fd < 0)
198                 return fd;
199
200         if ((!phc->set_projid || fsx.fsx_projid == phc->projid) &&
201             (!phc->set_inherit || (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)))
202                 goto out;
203
204         if (phc->set_inherit)
205                 fsx.fsx_xflags |= FS_XFLAG_PROJINHERIT;
206         if (phc->set_projid)
207                 fsx.fsx_projid = phc->projid;
208
209         ret = ioctl(fd, LL_IOC_FSSETXATTR, &fsx);
210         if (ret)
211                 fprintf(stderr, "%s: failed to set xattr for '%s': %s\n",
212                         progname, pathname, strerror(errno));
213 out:
214         close(fd);
215         return ret;
216 }
217
218 static int
219 project_clear_one(const char *pathname, struct project_handle_control *phc)
220 {
221         struct fsxattr fsx;
222         struct stat st;
223         int ret = 0, fd;
224
225         fd = project_get_xattr(pathname, &fsx, &st);
226         if (fd < 0)
227                 return fd;
228
229         if ((!(fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)) &&
230              (fsx.fsx_projid == 0 || phc->keep_projid))
231                 goto out;
232
233         fsx.fsx_xflags &= ~FS_XFLAG_PROJINHERIT;
234         if (!phc->keep_projid)
235                 fsx.fsx_projid = 0;
236
237         ret = ioctl(fd, LL_IOC_FSSETXATTR, &fsx);
238         if (ret)
239                 fprintf(stderr, "%s: failed to set xattr for '%s': %s\n",
240                         progname, pathname, strerror(errno));
241 out:
242         close(fd);
243         return ret;
244 }
245
246 static int
247 lfs_project_handle_dir(struct list_head *head, const char *pathname,
248                        struct project_handle_control *phc,
249                        int (*func)(const char *,
250                                    struct project_handle_control *))
251 {
252         char fullname[PATH_MAX];
253         struct dirent *ent;
254         DIR *dir;
255         int ret = 0;
256         int rc;
257
258         dir = opendir(pathname);
259         if (dir == NULL) {
260                 ret = -errno;
261                 fprintf(stderr, "%s: failed to opendir '%s': %s\n",
262                         progname, pathname, strerror(-ret));
263                 return ret;
264         }
265
266         while ((ent = readdir(dir)) != NULL) {
267                 /* skip "." and ".." */
268                 if (strcmp(ent->d_name, ".") == 0 ||
269                     strcmp(ent->d_name, "..") == 0)
270                         continue;
271
272                 if (strlen(ent->d_name) + strlen(pathname) + 1 >=
273                     sizeof(fullname)) {
274                         ret = -ENAMETOOLONG;
275                         errno = ENAMETOOLONG;
276                         fprintf(stderr, "%s: ignored too long path: %s/%s\n",
277                                         progname, pathname, ent->d_name);
278                         continue;
279                 }
280                 snprintf(fullname, PATH_MAX, "%s/%s", pathname,
281                          ent->d_name);
282
283                 rc = func(fullname, phc);
284                 if (rc && !ret)
285                         ret = rc;
286                 if (phc->recursive && ent->d_type == DT_DIR) {
287                         rc = lfs_project_item_alloc(head, fullname);
288                         if (rc && !ret)
289                                 ret = rc;
290                 }
291         }
292
293         closedir(dir);
294         return ret;
295 }
296
297 static int lfs_project_iterate(const char *pathname,
298                                struct project_handle_control *phc,
299                                int (*func)(const char *,
300                                            struct project_handle_control *))
301 {
302         struct lfs_project_item *lpi;
303         struct list_head head;
304         struct stat st;
305         int ret = 0;
306         int rc = 0;
307
308         ret = stat(pathname, &st);
309         if (ret) {
310                 fprintf(stderr, "%s: failed to stat '%s': %s\n",
311                         progname, pathname, strerror(errno));
312                 return ret;
313         }
314
315         /* list opeation will skip top directory in default */
316         if (!S_ISDIR(st.st_mode) || phc->dironly ||
317             project_list_one != func)
318                 ret = func(pathname, phc);
319
320         /* dironly first, recursive will be ignored */
321         if (!S_ISDIR(st.st_mode) || phc->dironly || ret)
322                 return ret;
323
324         INIT_LIST_HEAD(&head);
325         ret = lfs_project_item_alloc(&head, pathname);
326         if (ret)
327                 return ret;
328
329         while (!list_empty(&head)) {
330                 lpi = list_entry(head.next, struct lfs_project_item, lpi_list);
331                 list_del(&lpi->lpi_list);
332                 rc = lfs_project_handle_dir(&head, lpi->lpi_pathname,
333                                              phc, func);
334                 if (!ret && rc)
335                         ret = rc;
336                 free(lpi);
337         }
338
339         return ret;
340 }
341
342
343 inline int lfs_project_check(const char *pathname,
344                              struct project_handle_control *phc)
345 {
346         return lfs_project_iterate(pathname, phc, project_check_one);
347 }
348
349 inline int lfs_project_clear(const char *pathname,
350                              struct project_handle_control *phc)
351 {
352         return lfs_project_iterate(pathname, phc, project_clear_one);
353 }
354
355 inline int lfs_project_set(const char *pathname,
356                            struct project_handle_control *phc)
357 {
358         return lfs_project_iterate(pathname, phc, project_set_one);
359 }
360
361 inline int lfs_project_list(const char *pathname,
362                             struct project_handle_control *phc)
363 {
364         return lfs_project_iterate(pathname, phc, project_list_one);
365 }