3 * Copyright (C) 2011 Novell Inc.
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.
11 #include <linux/slab.h>
12 #include <linux/xattr.h>
13 #include "overlayfs.h"
15 static int ovl_copy_up_truncate(struct dentry *dentry)
18 struct dentry *parent;
20 struct path lowerpath;
22 parent = dget_parent(dentry);
23 err = ovl_copy_up(parent);
27 ovl_path_lower(dentry, &lowerpath);
28 err = vfs_getattr(&lowerpath, &stat);
33 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
40 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
43 struct dentry *upperdentry;
46 * Check for permissions before trying to copy-up. This is redundant
47 * since it will be rechecked later by ->setattr() on upper dentry. But
48 * without this, copy-up can be triggered by just about anybody.
50 * We don't initialize inode->size, which just means that
51 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
52 * check for a swapfile (which this won't be anyway).
54 err = inode_change_ok(dentry->d_inode, attr);
58 err = ovl_want_write(dentry);
62 err = ovl_copy_up(dentry);
64 upperdentry = ovl_dentry_upper(dentry);
66 mutex_lock(&upperdentry->d_inode->i_mutex);
67 err = notify_change(upperdentry, attr, NULL);
69 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
70 mutex_unlock(&upperdentry->d_inode->i_mutex);
72 ovl_drop_write(dentry);
77 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
82 ovl_path_real(dentry, &realpath);
83 return vfs_getattr(&realpath, stat);
86 int ovl_permission(struct inode *inode, int mask)
89 struct dentry *alias = NULL;
90 struct inode *realinode;
91 struct dentry *realdentry;
95 if (S_ISDIR(inode->i_mode)) {
96 oe = inode->i_private;
97 } else if (mask & MAY_NOT_BLOCK) {
101 * For non-directories find an alias and get the info
104 alias = d_find_any_alias(inode);
108 oe = alias->d_fsdata;
111 realdentry = ovl_entry_real(oe, &is_upper);
113 /* Careful in RCU walk mode */
114 realinode = ACCESS_ONCE(realdentry->d_inode);
116 WARN_ON(!(mask & MAY_NOT_BLOCK));
121 if (mask & MAY_WRITE) {
122 umode_t mode = realinode->i_mode;
125 * Writes will always be redirected to upper layer, so
126 * ignore lower layer being read-only.
128 * If the overlay itself is read-only then proceed
129 * with the permission check, don't return EROFS.
130 * This will only happen if this is the lower layer of
133 * If upper fs becomes read-only after the overlay was
134 * constructed return EROFS to prevent modification of
138 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
139 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
143 err = __inode_permission(realinode, mask);
150 struct ovl_link_data {
151 struct dentry *realdentry;
155 static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
157 struct dentry *realdentry;
158 struct inode *realinode;
159 struct ovl_link_data *data = NULL;
162 realdentry = ovl_dentry_real(dentry);
163 realinode = realdentry->d_inode;
165 if (WARN_ON(!realinode->i_op->follow_link))
166 return ERR_PTR(-EPERM);
168 if (realinode->i_op->put_link) {
169 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
171 return ERR_PTR(-ENOMEM);
172 data->realdentry = realdentry;
175 ret = realinode->i_op->follow_link(realdentry, cookie);
176 if (IS_ERR_OR_NULL(ret)) {
182 data->cookie = *cookie;
189 static void ovl_put_link(struct inode *unused, void *c)
191 struct inode *realinode;
192 struct ovl_link_data *data = c;
197 realinode = data->realdentry->d_inode;
198 realinode->i_op->put_link(realinode, data->cookie);
202 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
204 struct path realpath;
205 struct inode *realinode;
207 ovl_path_real(dentry, &realpath);
208 realinode = realpath.dentry->d_inode;
210 if (!realinode->i_op->readlink)
213 touch_atime(&realpath);
215 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
219 static bool ovl_is_private_xattr(const char *name)
221 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
224 int ovl_setxattr(struct dentry *dentry, const char *name,
225 const void *value, size_t size, int flags)
228 struct dentry *upperdentry;
230 err = ovl_want_write(dentry);
235 if (ovl_is_private_xattr(name))
238 err = ovl_copy_up(dentry);
242 upperdentry = ovl_dentry_upper(dentry);
243 err = vfs_setxattr(upperdentry, name, value, size, flags);
246 ovl_drop_write(dentry);
251 static bool ovl_need_xattr_filter(struct dentry *dentry,
252 enum ovl_path_type type)
254 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
255 return S_ISDIR(dentry->d_inode->i_mode);
260 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
261 void *value, size_t size)
263 struct path realpath;
264 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
266 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
269 return vfs_getxattr(realpath.dentry, name, value, size);
272 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
274 struct path realpath;
275 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
279 res = vfs_listxattr(realpath.dentry, list, size);
280 if (res <= 0 || size == 0)
283 if (!ovl_need_xattr_filter(dentry, type))
286 /* filter out private xattrs */
287 for (off = 0; off < res;) {
288 char *s = list + off;
289 size_t slen = strlen(s) + 1;
291 BUG_ON(off + slen > res);
293 if (ovl_is_private_xattr(s)) {
295 memmove(s, s + slen, res - off);
304 int ovl_removexattr(struct dentry *dentry, const char *name)
307 struct path realpath;
308 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
310 err = ovl_want_write(dentry);
315 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
318 if (!OVL_TYPE_UPPER(type)) {
319 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
323 err = ovl_copy_up(dentry);
327 ovl_path_upper(dentry, &realpath);
330 err = vfs_removexattr(realpath.dentry, name);
332 ovl_drop_write(dentry);
337 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
338 struct dentry *realdentry)
340 if (OVL_TYPE_UPPER(type))
343 if (special_file(realdentry->d_inode->i_mode))
346 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
352 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
355 struct path realpath;
356 enum ovl_path_type type;
358 if (d_is_dir(dentry))
359 return d_backing_inode(dentry);
361 type = ovl_path_real(dentry, &realpath);
362 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
363 err = ovl_want_write(dentry);
367 if (file_flags & O_TRUNC)
368 err = ovl_copy_up_truncate(dentry);
370 err = ovl_copy_up(dentry);
371 ovl_drop_write(dentry);
375 ovl_path_upper(dentry, &realpath);
378 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
379 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
381 return d_backing_inode(realpath.dentry);
384 static const struct inode_operations ovl_file_inode_operations = {
385 .setattr = ovl_setattr,
386 .permission = ovl_permission,
387 .getattr = ovl_getattr,
388 .setxattr = ovl_setxattr,
389 .getxattr = ovl_getxattr,
390 .listxattr = ovl_listxattr,
391 .removexattr = ovl_removexattr,
394 static const struct inode_operations ovl_symlink_inode_operations = {
395 .setattr = ovl_setattr,
396 .follow_link = ovl_follow_link,
397 .put_link = ovl_put_link,
398 .readlink = ovl_readlink,
399 .getattr = ovl_getattr,
400 .setxattr = ovl_setxattr,
401 .getxattr = ovl_getxattr,
402 .listxattr = ovl_listxattr,
403 .removexattr = ovl_removexattr,
406 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
407 struct ovl_entry *oe)
411 inode = new_inode(sb);
417 inode->i_ino = get_next_ino();
418 inode->i_mode = mode;
419 inode->i_flags |= S_NOATIME | S_NOCMTIME;
423 inode->i_private = oe;
424 inode->i_op = &ovl_dir_inode_operations;
425 inode->i_fop = &ovl_dir_operations;
429 inode->i_op = &ovl_symlink_inode_operations;
437 inode->i_op = &ovl_file_inode_operations;
441 WARN(1, "illegal file type: %i\n", mode);