blob: 8774ebb5d80ac64ca24794c97d78ede6bfca101e [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
11#include <linux/slab.h>
12#include <linux/xattr.h>
13#include "overlayfs.h"
14
15static int ovl_copy_up_last(struct dentry *dentry, struct iattr *attr,
16 bool no_data)
17{
18 int err;
19 struct dentry *parent;
20 struct kstat stat;
21 struct path lowerpath;
22
23 parent = dget_parent(dentry);
24 err = ovl_copy_up(parent);
25 if (err)
26 goto out_dput_parent;
27
28 ovl_path_lower(dentry, &lowerpath);
29 err = vfs_getattr(&lowerpath, &stat);
30 if (err)
31 goto out_dput_parent;
32
33 if (no_data)
34 stat.size = 0;
35
36 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat, attr);
37
38out_dput_parent:
39 dput(parent);
40 return err;
41}
42
43int ovl_setattr(struct dentry *dentry, struct iattr *attr)
44{
45 int err;
46 struct dentry *upperdentry;
47
48 err = ovl_want_write(dentry);
49 if (err)
50 goto out;
51
52 upperdentry = ovl_dentry_upper(dentry);
53 if (upperdentry) {
54 mutex_lock(&upperdentry->d_inode->i_mutex);
55 err = notify_change(upperdentry, attr, NULL);
Konstantin Khlebnikova91db622016-01-31 16:21:29 +030056 if (!err)
57 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020058 mutex_unlock(&upperdentry->d_inode->i_mutex);
59 } else {
60 err = ovl_copy_up_last(dentry, attr, false);
61 }
62 ovl_drop_write(dentry);
63out:
64 return err;
65}
66
67static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
68 struct kstat *stat)
69{
70 struct path realpath;
71
72 ovl_path_real(dentry, &realpath);
73 return vfs_getattr(&realpath, stat);
74}
75
76int ovl_permission(struct inode *inode, int mask)
77{
78 struct ovl_entry *oe;
79 struct dentry *alias = NULL;
80 struct inode *realinode;
81 struct dentry *realdentry;
82 bool is_upper;
83 int err;
84
85 if (S_ISDIR(inode->i_mode)) {
86 oe = inode->i_private;
87 } else if (mask & MAY_NOT_BLOCK) {
88 return -ECHILD;
89 } else {
90 /*
91 * For non-directories find an alias and get the info
92 * from there.
93 */
94 alias = d_find_any_alias(inode);
95 if (WARN_ON(!alias))
96 return -ENOENT;
97
98 oe = alias->d_fsdata;
99 }
100
101 realdentry = ovl_entry_real(oe, &is_upper);
102
103 /* Careful in RCU walk mode */
104 realinode = ACCESS_ONCE(realdentry->d_inode);
105 if (!realinode) {
106 WARN_ON(!(mask & MAY_NOT_BLOCK));
107 err = -ENOENT;
108 goto out_dput;
109 }
110
111 if (mask & MAY_WRITE) {
112 umode_t mode = realinode->i_mode;
113
114 /*
115 * Writes will always be redirected to upper layer, so
116 * ignore lower layer being read-only.
117 *
118 * If the overlay itself is read-only then proceed
119 * with the permission check, don't return EROFS.
120 * This will only happen if this is the lower layer of
121 * another overlayfs.
122 *
123 * If upper fs becomes read-only after the overlay was
124 * constructed return EROFS to prevent modification of
125 * upper layer.
126 */
127 err = -EROFS;
128 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
129 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
130 goto out_dput;
131 }
132
133 err = __inode_permission(realinode, mask);
134out_dput:
135 dput(alias);
136 return err;
137}
138
139
140struct ovl_link_data {
141 struct dentry *realdentry;
142 void *cookie;
143};
144
145static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
146{
147 void *ret;
148 struct dentry *realdentry;
149 struct inode *realinode;
150
151 realdentry = ovl_dentry_real(dentry);
152 realinode = realdentry->d_inode;
153
154 if (WARN_ON(!realinode->i_op->follow_link))
155 return ERR_PTR(-EPERM);
156
157 ret = realinode->i_op->follow_link(realdentry, nd);
158 if (IS_ERR(ret))
159 return ret;
160
161 if (realinode->i_op->put_link) {
162 struct ovl_link_data *data;
163
164 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
165 if (!data) {
166 realinode->i_op->put_link(realdentry, nd, ret);
167 return ERR_PTR(-ENOMEM);
168 }
169 data->realdentry = realdentry;
170 data->cookie = ret;
171
172 return data;
173 } else {
174 return NULL;
175 }
176}
177
178static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
179{
180 struct inode *realinode;
181 struct ovl_link_data *data = c;
182
183 if (!data)
184 return;
185
186 realinode = data->realdentry->d_inode;
187 realinode->i_op->put_link(data->realdentry, nd, data->cookie);
188 kfree(data);
189}
190
191static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
192{
193 struct path realpath;
194 struct inode *realinode;
195
196 ovl_path_real(dentry, &realpath);
197 realinode = realpath.dentry->d_inode;
198
199 if (!realinode->i_op->readlink)
200 return -EINVAL;
201
202 touch_atime(&realpath);
203
204 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
205}
206
207
208static bool ovl_is_private_xattr(const char *name)
209{
210 return strncmp(name, "trusted.overlay.", 14) == 0;
211}
212
213int ovl_setxattr(struct dentry *dentry, const char *name,
214 const void *value, size_t size, int flags)
215{
216 int err;
217 struct dentry *upperdentry;
218
219 err = ovl_want_write(dentry);
220 if (err)
221 goto out;
222
223 err = -EPERM;
224 if (ovl_is_private_xattr(name))
225 goto out_drop_write;
226
227 err = ovl_copy_up(dentry);
228 if (err)
229 goto out_drop_write;
230
231 upperdentry = ovl_dentry_upper(dentry);
232 err = vfs_setxattr(upperdentry, name, value, size, flags);
233
234out_drop_write:
235 ovl_drop_write(dentry);
236out:
237 return err;
238}
239
Miklos Szeredi52148462014-11-20 16:40:00 +0100240static bool ovl_need_xattr_filter(struct dentry *dentry,
241 enum ovl_path_type type)
242{
243 return type == OVL_PATH_UPPER && S_ISDIR(dentry->d_inode->i_mode);
244}
245
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200246ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
247 void *value, size_t size)
248{
Miklos Szeredi52148462014-11-20 16:40:00 +0100249 struct path realpath;
250 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
251
252 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200253 return -ENODATA;
254
Miklos Szeredi52148462014-11-20 16:40:00 +0100255 return vfs_getxattr(realpath.dentry, name, value, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256}
257
258ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
259{
Miklos Szeredi52148462014-11-20 16:40:00 +0100260 struct path realpath;
261 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200262 ssize_t res;
263 int off;
264
Miklos Szeredi52148462014-11-20 16:40:00 +0100265 res = vfs_listxattr(realpath.dentry, list, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200266 if (res <= 0 || size == 0)
267 return res;
268
Miklos Szeredi52148462014-11-20 16:40:00 +0100269 if (!ovl_need_xattr_filter(dentry, type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270 return res;
271
272 /* filter out private xattrs */
273 for (off = 0; off < res;) {
274 char *s = list + off;
275 size_t slen = strlen(s) + 1;
276
277 BUG_ON(off + slen > res);
278
279 if (ovl_is_private_xattr(s)) {
280 res -= slen;
281 memmove(s, s + slen, res - off);
282 } else {
283 off += slen;
284 }
285 }
286
287 return res;
288}
289
290int ovl_removexattr(struct dentry *dentry, const char *name)
291{
292 int err;
293 struct path realpath;
Miklos Szeredi52148462014-11-20 16:40:00 +0100294 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200295
296 err = ovl_want_write(dentry);
297 if (err)
298 goto out;
299
Miklos Szeredi52148462014-11-20 16:40:00 +0100300 err = -ENODATA;
301 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200302 goto out_drop_write;
303
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200304 if (type == OVL_PATH_LOWER) {
305 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
306 if (err < 0)
307 goto out_drop_write;
308
309 err = ovl_copy_up(dentry);
310 if (err)
311 goto out_drop_write;
312
313 ovl_path_upper(dentry, &realpath);
314 }
315
316 err = vfs_removexattr(realpath.dentry, name);
317out_drop_write:
318 ovl_drop_write(dentry);
319out:
320 return err;
321}
322
323static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
324 struct dentry *realdentry)
325{
326 if (type != OVL_PATH_LOWER)
327 return false;
328
329 if (special_file(realdentry->d_inode->i_mode))
330 return false;
331
332 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
333 return false;
334
335 return true;
336}
337
David Howellse93f29f2015-06-18 14:32:31 +0100338struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339{
340 int err;
341 struct path realpath;
342 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343
Al Virod706c402015-07-12 10:39:45 -0400344 if (d_is_dir(dentry))
345 return d_backing_inode(dentry);
346
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200347 type = ovl_path_real(dentry, &realpath);
David Howellse93f29f2015-06-18 14:32:31 +0100348 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200349 err = ovl_want_write(dentry);
350 if (err)
David Howellse93f29f2015-06-18 14:32:31 +0100351 return ERR_PTR(err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352
David Howellse93f29f2015-06-18 14:32:31 +0100353 if (file_flags & O_TRUNC)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200354 err = ovl_copy_up_last(dentry, NULL, true);
355 else
356 err = ovl_copy_up(dentry);
David Howells5d4beb92015-06-18 14:32:23 +0100357 ovl_drop_write(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200358 if (err)
David Howellse93f29f2015-06-18 14:32:31 +0100359 return ERR_PTR(err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360
361 ovl_path_upper(dentry, &realpath);
362 }
363
David Howellse93f29f2015-06-18 14:32:31 +0100364 return d_backing_inode(realpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200365}
366
367static const struct inode_operations ovl_file_inode_operations = {
368 .setattr = ovl_setattr,
369 .permission = ovl_permission,
370 .getattr = ovl_getattr,
371 .setxattr = ovl_setxattr,
372 .getxattr = ovl_getxattr,
373 .listxattr = ovl_listxattr,
374 .removexattr = ovl_removexattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200375};
376
377static const struct inode_operations ovl_symlink_inode_operations = {
378 .setattr = ovl_setattr,
379 .follow_link = ovl_follow_link,
380 .put_link = ovl_put_link,
381 .readlink = ovl_readlink,
382 .getattr = ovl_getattr,
383 .setxattr = ovl_setxattr,
384 .getxattr = ovl_getxattr,
385 .listxattr = ovl_listxattr,
386 .removexattr = ovl_removexattr,
387};
388
389struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
390 struct ovl_entry *oe)
391{
392 struct inode *inode;
393
394 inode = new_inode(sb);
395 if (!inode)
396 return NULL;
397
398 mode &= S_IFMT;
399
400 inode->i_ino = get_next_ino();
401 inode->i_mode = mode;
402 inode->i_flags |= S_NOATIME | S_NOCMTIME;
403
404 switch (mode) {
405 case S_IFDIR:
406 inode->i_private = oe;
407 inode->i_op = &ovl_dir_inode_operations;
408 inode->i_fop = &ovl_dir_operations;
409 break;
410
411 case S_IFLNK:
412 inode->i_op = &ovl_symlink_inode_operations;
413 break;
414
415 case S_IFREG:
416 case S_IFSOCK:
417 case S_IFBLK:
418 case S_IFCHR:
419 case S_IFIFO:
420 inode->i_op = &ovl_file_inode_operations;
421 break;
422
423 default:
424 WARN(1, "illegal file type: %i\n", mode);
425 iput(inode);
426 inode = NULL;
427 }
428
429 return inode;
430
431}