Whamcloud - gitweb
d61b0382454f86a66f1d85cda3eef11de6efbe84
[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 int project_get_xattr(const char *pathname, struct fsxattr *fsx)
79 {
80         int ret, fd;
81
82         fd = open(pathname, O_RDONLY | O_NOCTTY | O_NDELAY);
83         if (fd < 0) {
84                 fprintf(stderr, "%s: failed to open '%s': %s\n",
85                         progname, pathname, strerror(errno));
86                 return -errno;
87         }
88
89         ret = ioctl(fd, LL_IOC_FSGETXATTR, fsx);
90         if (ret) {
91                 fprintf(stderr, "%s: failed to get xattr for '%s': %s\n",
92                         progname, pathname, strerror(errno));
93                 close(fd);
94                 return -errno;
95         }
96         return fd;
97 }
98
99 static int
100 project_check_one(const char *pathname, struct project_handle_control *phc)
101 {
102         struct fsxattr fsx;
103         struct stat st;
104         int ret;
105
106         ret = stat(pathname, &st);
107         if (ret) {
108                 fprintf(stderr, "%s: failed to stat '%s': %s\n",
109                         progname, pathname, strerror(errno));
110                 return -errno;
111         }
112
113         ret = project_get_xattr(pathname, &fsx);
114         if (ret < 0)
115                 return ret;
116
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;
121         }
122
123         if (!(fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)) {
124                 if (!phc->newline) {
125                         printf("%s%c", pathname, '\0');
126                         goto out;
127                 }
128                  printf("%s - project inheritance flag is not set\n",
129                         pathname);
130         }
131
132         if (fsx.fsx_projid != phc->projid) {
133                 if (!phc->newline) {
134                         printf("%s%c", pathname, '\0');
135                         goto out;
136                 }
137                 printf("%s - project identifier is not set (inode=%u, tree=%u)\n",
138                        pathname, fsx.fsx_projid, phc->projid);
139         }
140 out:
141         close(ret);
142         return 0;
143 }
144
145 static int
146 project_list_one(const char *pathname, struct project_handle_control *phc)
147 {
148         struct fsxattr fsx;
149         int ret;
150
151         ret = project_get_xattr(pathname, &fsx);
152         if (ret < 0)
153                 return ret;
154
155         printf("%5u %c %s\n", fsx.fsx_projid,
156                (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT) ?
157                 'P' : '-', pathname);
158
159         close(ret);
160         return 0;
161 }
162
163 static int
164 project_set_one(const char *pathname, struct project_handle_control *phc)
165 {
166         struct fsxattr fsx;
167         int fd, ret = 0;
168
169         fd = project_get_xattr(pathname, &fsx);
170         if (fd < 0)
171                 return fd;
172
173         if ((!phc->set_projid || fsx.fsx_projid == phc->projid) &&
174             (!phc->set_inherit || (fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)))
175                 goto out;
176
177         if (phc->set_inherit)
178                 fsx.fsx_xflags |= FS_XFLAG_PROJINHERIT;
179         if (phc->set_projid)
180                 fsx.fsx_projid = phc->projid;
181
182         ret = ioctl(fd, LL_IOC_FSSETXATTR, &fsx);
183         if (ret)
184                 fprintf(stderr, "%s: failed to set xattr for '%s': %s\n",
185                         progname, pathname, strerror(errno));
186 out:
187         close(fd);
188         return ret;
189 }
190
191 static int
192 project_clear_one(const char *pathname, struct project_handle_control *phc)
193 {
194         struct fsxattr fsx;
195         int ret = 0, fd;
196
197         fd = project_get_xattr(pathname, &fsx);
198         if (fd < 0)
199                 return fd;
200
201         if ((!(fsx.fsx_xflags & FS_XFLAG_PROJINHERIT)) &&
202              (fsx.fsx_projid == 0 || phc->keep_projid))
203                 goto out;
204
205         fsx.fsx_xflags &= ~FS_XFLAG_PROJINHERIT;
206         if (!phc->keep_projid)
207                 fsx.fsx_projid = 0;
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 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 *))
223 {
224         char fullname[PATH_MAX];
225         struct dirent *ent;
226         DIR *dir;
227         int ret = 0;
228         int rc;
229
230         dir = opendir(pathname);
231         if (dir == NULL) {
232                 ret = -errno;
233                 fprintf(stderr, "%s: failed to opendir '%s': %s\n",
234                         progname, pathname, strerror(-ret));
235                 return ret;
236         }
237
238         while ((ent = readdir(dir)) != NULL) {
239                 /* skip "." and ".." */
240                 if (strcmp(ent->d_name, ".") == 0 ||
241                     strcmp(ent->d_name, "..") == 0)
242                         continue;
243
244                 if (strlen(ent->d_name) + strlen(pathname) + 1 >=
245                     sizeof(fullname)) {
246                         ret = -ENAMETOOLONG;
247                         errno = ENAMETOOLONG;
248                         fprintf(stderr, "%s: ignored too long path: %s/%s\n",
249                                         progname, pathname, ent->d_name);
250                         continue;
251                 }
252                 snprintf(fullname, PATH_MAX, "%s/%s", pathname,
253                          ent->d_name);
254
255                 rc = func(fullname, phc);
256                 if (rc && !ret)
257                         ret = rc;
258                 if (phc->recursive && ent->d_type == DT_DIR) {
259                         rc = lfs_project_item_alloc(head, fullname);
260                         if (rc && !ret)
261                                 ret = rc;
262                 }
263         }
264
265         closedir(dir);
266         return ret;
267 }
268
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 *))
273 {
274         struct lfs_project_item *lpi;
275         struct list_head head;
276         struct stat st;
277         int ret = 0;
278         int rc = 0;
279
280         ret = stat(pathname, &st);
281         if (ret) {
282                 fprintf(stderr, "%s: failed to stat '%s': %s\n",
283                         progname, pathname, strerror(errno));
284                 return ret;
285         }
286
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);
291
292         /* dironly first, recursive will be ignored */
293         if (!S_ISDIR(st.st_mode) || phc->dironly || ret)
294                 return ret;
295
296         INIT_LIST_HEAD(&head);
297         ret = lfs_project_item_alloc(&head, pathname);
298         if (ret)
299                 return ret;
300
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,
305                                              phc, func);
306                 if (!ret && rc)
307                         ret = rc;
308                 free(lpi);
309         }
310
311         return ret;
312 }
313
314
315 inline int lfs_project_check(const char *pathname,
316                              struct project_handle_control *phc)
317 {
318         return lfs_project_iterate(pathname, phc, project_check_one);
319 }
320
321 inline int lfs_project_clear(const char *pathname,
322                              struct project_handle_control *phc)
323 {
324         return lfs_project_iterate(pathname, phc, project_clear_one);
325 }
326
327 inline int lfs_project_set(const char *pathname,
328                            struct project_handle_control *phc)
329 {
330         return lfs_project_iterate(pathname, phc, project_set_one);
331 }
332
333 inline int lfs_project_list(const char *pathname,
334                             struct project_handle_control *phc)
335 {
336         return lfs_project_iterate(pathname, phc, project_list_one);
337 }