blob: 38a0b8b9f8b9b8978abb8ad77dfe9e5c0612adf5 [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
Al Viro0f7ff2d2015-12-06 12:31:07 -050015static int ovl_copy_up_truncate(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016{
17 int err;
18 struct dentry *parent;
19 struct kstat stat;
20 struct path lowerpath;
21
22 parent = dget_parent(dentry);
23 err = ovl_copy_up(parent);
24 if (err)
25 goto out_dput_parent;
26
27 ovl_path_lower(dentry, &lowerpath);
28 err = vfs_getattr(&lowerpath, &stat);
29 if (err)
30 goto out_dput_parent;
31
Al Viro0f7ff2d2015-12-06 12:31:07 -050032 stat.size = 0;
33 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020034
35out_dput_parent:
36 dput(parent);
37 return err;
38}
39
40int ovl_setattr(struct dentry *dentry, struct iattr *attr)
41{
42 int err;
43 struct dentry *upperdentry;
44
45 err = ovl_want_write(dentry);
46 if (err)
47 goto out;
48
Miklos Szerediacff81e2015-12-04 19:18:48 +010049 err = ovl_copy_up(dentry);
50 if (!err) {
51 upperdentry = ovl_dentry_upper(dentry);
52
Miklos Szeredie9be9d52014-10-24 00:14:38 +020053 mutex_lock(&upperdentry->d_inode->i_mutex);
54 err = notify_change(upperdentry, attr, NULL);
55 mutex_unlock(&upperdentry->d_inode->i_mutex);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020056 }
57 ovl_drop_write(dentry);
58out:
59 return err;
60}
61
62static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
63 struct kstat *stat)
64{
65 struct path realpath;
66
67 ovl_path_real(dentry, &realpath);
68 return vfs_getattr(&realpath, stat);
69}
70
71int ovl_permission(struct inode *inode, int mask)
72{
73 struct ovl_entry *oe;
74 struct dentry *alias = NULL;
75 struct inode *realinode;
76 struct dentry *realdentry;
77 bool is_upper;
78 int err;
79
80 if (S_ISDIR(inode->i_mode)) {
81 oe = inode->i_private;
82 } else if (mask & MAY_NOT_BLOCK) {
83 return -ECHILD;
84 } else {
85 /*
86 * For non-directories find an alias and get the info
87 * from there.
88 */
89 alias = d_find_any_alias(inode);
90 if (WARN_ON(!alias))
91 return -ENOENT;
92
93 oe = alias->d_fsdata;
94 }
95
96 realdentry = ovl_entry_real(oe, &is_upper);
97
98 /* Careful in RCU walk mode */
99 realinode = ACCESS_ONCE(realdentry->d_inode);
100 if (!realinode) {
101 WARN_ON(!(mask & MAY_NOT_BLOCK));
102 err = -ENOENT;
103 goto out_dput;
104 }
105
106 if (mask & MAY_WRITE) {
107 umode_t mode = realinode->i_mode;
108
109 /*
110 * Writes will always be redirected to upper layer, so
111 * ignore lower layer being read-only.
112 *
113 * If the overlay itself is read-only then proceed
114 * with the permission check, don't return EROFS.
115 * This will only happen if this is the lower layer of
116 * another overlayfs.
117 *
118 * If upper fs becomes read-only after the overlay was
119 * constructed return EROFS to prevent modification of
120 * upper layer.
121 */
122 err = -EROFS;
123 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
124 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
125 goto out_dput;
126 }
127
128 err = __inode_permission(realinode, mask);
129out_dput:
130 dput(alias);
131 return err;
132}
133
134
135struct ovl_link_data {
136 struct dentry *realdentry;
137 void *cookie;
138};
139
Al Viro6b255392015-11-17 10:20:54 -0500140static const char *ovl_get_link(struct dentry *dentry,
141 struct inode *inode, void **cookie)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200143 struct dentry *realdentry;
144 struct inode *realinode;
NeilBrown3188b292015-03-23 13:37:39 +1100145 struct ovl_link_data *data = NULL;
Al Viro680baac2015-05-02 13:32:22 -0400146 const char *ret;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147
Al Viro6b255392015-11-17 10:20:54 -0500148 if (!dentry)
149 return ERR_PTR(-ECHILD);
150
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200151 realdentry = ovl_dentry_real(dentry);
152 realinode = realdentry->d_inode;
153
Al Viro6b255392015-11-17 10:20:54 -0500154 if (WARN_ON(!realinode->i_op->get_link))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155 return ERR_PTR(-EPERM);
156
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157 if (realinode->i_op->put_link) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
NeilBrown3188b292015-03-23 13:37:39 +1100159 if (!data)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200160 return ERR_PTR(-ENOMEM);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200161 data->realdentry = realdentry;
NeilBrown3188b292015-03-23 13:37:39 +1100162 }
163
Al Viro6b255392015-11-17 10:20:54 -0500164 ret = realinode->i_op->get_link(realdentry, realinode, cookie);
Al Viro680baac2015-05-02 13:32:22 -0400165 if (IS_ERR_OR_NULL(ret)) {
NeilBrown3188b292015-03-23 13:37:39 +1100166 kfree(data);
167 return ret;
168 }
169
170 if (data)
Al Viro680baac2015-05-02 13:32:22 -0400171 data->cookie = *cookie;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200172
Al Viro680baac2015-05-02 13:32:22 -0400173 *cookie = data;
174
175 return ret;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200176}
177
Al Viro5f2c4172015-05-07 11:14:26 -0400178static void ovl_put_link(struct inode *unused, void *c)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200179{
180 struct inode *realinode;
181 struct ovl_link_data *data = c;
182
183 if (!data)
184 return;
185
186 realinode = data->realdentry->d_inode;
Al Viro5f2c4172015-05-07 11:14:26 -0400187 realinode->i_op->put_link(realinode, data->cookie);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200188 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{
hujianyangcead89b2014-11-24 18:25:21 +0800210 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211}
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{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100243 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
244 return S_ISDIR(dentry->d_inode->i_mode);
245 else
246 return false;
Miklos Szeredi52148462014-11-20 16:40:00 +0100247}
248
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200249ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
250 void *value, size_t size)
251{
Miklos Szeredi52148462014-11-20 16:40:00 +0100252 struct path realpath;
253 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
254
255 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256 return -ENODATA;
257
Miklos Szeredi52148462014-11-20 16:40:00 +0100258 return vfs_getxattr(realpath.dentry, name, value, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200259}
260
261ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
262{
Miklos Szeredi52148462014-11-20 16:40:00 +0100263 struct path realpath;
264 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265 ssize_t res;
266 int off;
267
Miklos Szeredi52148462014-11-20 16:40:00 +0100268 res = vfs_listxattr(realpath.dentry, list, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200269 if (res <= 0 || size == 0)
270 return res;
271
Miklos Szeredi52148462014-11-20 16:40:00 +0100272 if (!ovl_need_xattr_filter(dentry, type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273 return res;
274
275 /* filter out private xattrs */
276 for (off = 0; off < res;) {
277 char *s = list + off;
278 size_t slen = strlen(s) + 1;
279
280 BUG_ON(off + slen > res);
281
282 if (ovl_is_private_xattr(s)) {
283 res -= slen;
284 memmove(s, s + slen, res - off);
285 } else {
286 off += slen;
287 }
288 }
289
290 return res;
291}
292
293int ovl_removexattr(struct dentry *dentry, const char *name)
294{
295 int err;
296 struct path realpath;
Miklos Szeredi52148462014-11-20 16:40:00 +0100297 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200298
299 err = ovl_want_write(dentry);
300 if (err)
301 goto out;
302
Miklos Szeredi52148462014-11-20 16:40:00 +0100303 err = -ENODATA;
304 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 goto out_drop_write;
306
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100307 if (!OVL_TYPE_UPPER(type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
309 if (err < 0)
310 goto out_drop_write;
311
312 err = ovl_copy_up(dentry);
313 if (err)
314 goto out_drop_write;
315
316 ovl_path_upper(dentry, &realpath);
317 }
318
319 err = vfs_removexattr(realpath.dentry, name);
320out_drop_write:
321 ovl_drop_write(dentry);
322out:
323 return err;
324}
325
326static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
327 struct dentry *realdentry)
328{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100329 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330 return false;
331
332 if (special_file(realdentry->d_inode->i_mode))
333 return false;
334
335 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
336 return false;
337
338 return true;
339}
340
David Howells4bacc9c2015-06-18 14:32:31 +0100341struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200342{
343 int err;
344 struct path realpath;
345 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346
Al Viro9391dd02015-07-12 10:39:45 -0400347 if (d_is_dir(dentry))
348 return d_backing_inode(dentry);
349
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350 type = ovl_path_real(dentry, &realpath);
David Howells4bacc9c2015-06-18 14:32:31 +0100351 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352 err = ovl_want_write(dentry);
353 if (err)
David Howells4bacc9c2015-06-18 14:32:31 +0100354 return ERR_PTR(err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355
David Howells4bacc9c2015-06-18 14:32:31 +0100356 if (file_flags & O_TRUNC)
Al Viro0f7ff2d2015-12-06 12:31:07 -0500357 err = ovl_copy_up_truncate(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200358 else
359 err = ovl_copy_up(dentry);
David Howellsf25801e2015-06-18 14:32:23 +0100360 ovl_drop_write(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200361 if (err)
David Howells4bacc9c2015-06-18 14:32:31 +0100362 return ERR_PTR(err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200363
364 ovl_path_upper(dentry, &realpath);
365 }
366
Miklos Szeredi1c8a47d2015-10-12 15:56:20 +0200367 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
368 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
369
David Howells4bacc9c2015-06-18 14:32:31 +0100370 return d_backing_inode(realpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371}
372
373static const struct inode_operations ovl_file_inode_operations = {
374 .setattr = ovl_setattr,
375 .permission = ovl_permission,
376 .getattr = ovl_getattr,
377 .setxattr = ovl_setxattr,
378 .getxattr = ovl_getxattr,
379 .listxattr = ovl_listxattr,
380 .removexattr = ovl_removexattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381};
382
383static const struct inode_operations ovl_symlink_inode_operations = {
384 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500385 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386 .put_link = ovl_put_link,
387 .readlink = ovl_readlink,
388 .getattr = ovl_getattr,
389 .setxattr = ovl_setxattr,
390 .getxattr = ovl_getxattr,
391 .listxattr = ovl_listxattr,
392 .removexattr = ovl_removexattr,
393};
394
395struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
396 struct ovl_entry *oe)
397{
398 struct inode *inode;
399
400 inode = new_inode(sb);
401 if (!inode)
402 return NULL;
403
404 mode &= S_IFMT;
405
406 inode->i_ino = get_next_ino();
407 inode->i_mode = mode;
408 inode->i_flags |= S_NOATIME | S_NOCMTIME;
409
410 switch (mode) {
411 case S_IFDIR:
412 inode->i_private = oe;
413 inode->i_op = &ovl_dir_inode_operations;
414 inode->i_fop = &ovl_dir_operations;
415 break;
416
417 case S_IFLNK:
418 inode->i_op = &ovl_symlink_inode_operations;
419 break;
420
421 case S_IFREG:
422 case S_IFSOCK:
423 case S_IFBLK:
424 case S_IFCHR:
425 case S_IFIFO:
426 inode->i_op = &ovl_file_inode_operations;
427 break;
428
429 default:
430 WARN(1, "illegal file type: %i\n", mode);
431 iput(inode);
432 inode = NULL;
433 }
434
435 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200436}