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_last(struct dentry *dentry, struct iattr *attr,
19 struct dentry *parent;
21 struct path lowerpath;
23 parent = dget_parent(dentry);
24 err = ovl_copy_up(parent);
28 ovl_path_lower(dentry, &lowerpath);
29 err = vfs_getattr(&lowerpath, &stat);
36 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat, attr);
43 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
46 struct dentry *upperdentry;
48 err = ovl_want_write(dentry);
52 upperdentry = ovl_dentry_upper(dentry);
54 mutex_lock(&upperdentry->d_inode->i_mutex);
55 err = notify_change(upperdentry, attr, NULL);
56 mutex_unlock(&upperdentry->d_inode->i_mutex);
58 err = ovl_copy_up_last(dentry, attr, false);
60 ovl_drop_write(dentry);
65 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
70 ovl_path_real(dentry, &realpath);
71 return vfs_getattr(&realpath, stat);
74 int ovl_permission(struct inode *inode, int mask)
77 struct dentry *alias = NULL;
78 struct inode *realinode;
79 struct dentry *realdentry;
83 if (S_ISDIR(inode->i_mode)) {
84 oe = inode->i_private;
85 } else if (mask & MAY_NOT_BLOCK) {
89 * For non-directories find an alias and get the info
92 alias = d_find_any_alias(inode);
99 realdentry = ovl_entry_real(oe, &is_upper);
101 if (ovl_is_default_permissions(inode)) {
103 struct path realpath = { .dentry = realdentry };
105 if (mask & MAY_NOT_BLOCK)
108 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
110 err = vfs_getattr(&realpath, &stat);
114 if ((stat.mode ^ inode->i_mode) & S_IFMT)
117 inode->i_mode = stat.mode;
118 inode->i_uid = stat.uid;
119 inode->i_gid = stat.gid;
121 return generic_permission(inode, mask);
124 /* Careful in RCU walk mode */
125 realinode = ACCESS_ONCE(realdentry->d_inode);
127 WARN_ON(!(mask & MAY_NOT_BLOCK));
132 if (mask & MAY_WRITE) {
133 umode_t mode = realinode->i_mode;
136 * Writes will always be redirected to upper layer, so
137 * ignore lower layer being read-only.
139 * If the overlay itself is read-only then proceed
140 * with the permission check, don't return EROFS.
141 * This will only happen if this is the lower layer of
144 * If upper fs becomes read-only after the overlay was
145 * constructed return EROFS to prevent modification of
149 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
150 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
154 err = __inode_permission(realinode, mask);
161 struct ovl_link_data {
162 struct dentry *realdentry;
166 static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
168 struct dentry *realdentry;
169 struct inode *realinode;
170 struct ovl_link_data *data = NULL;
173 realdentry = ovl_dentry_real(dentry);
174 realinode = realdentry->d_inode;
176 if (WARN_ON(!realinode->i_op->follow_link))
177 return ERR_PTR(-EPERM);
179 if (realinode->i_op->put_link) {
180 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
182 return ERR_PTR(-ENOMEM);
183 data->realdentry = realdentry;
186 ret = realinode->i_op->follow_link(realdentry, cookie);
187 if (IS_ERR_OR_NULL(ret)) {
193 data->cookie = *cookie;
200 static void ovl_put_link(struct inode *unused, void *c)
202 struct inode *realinode;
203 struct ovl_link_data *data = c;
208 realinode = data->realdentry->d_inode;
209 realinode->i_op->put_link(realinode, data->cookie);
213 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
215 struct path realpath;
216 struct inode *realinode;
218 ovl_path_real(dentry, &realpath);
219 realinode = realpath.dentry->d_inode;
221 if (!realinode->i_op->readlink)
224 touch_atime(&realpath);
226 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
230 static bool ovl_is_private_xattr(const char *name)
232 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
235 int ovl_setxattr(struct dentry *dentry, const char *name,
236 const void *value, size_t size, int flags)
239 struct dentry *upperdentry;
241 err = ovl_want_write(dentry);
246 if (ovl_is_private_xattr(name))
249 err = ovl_copy_up(dentry);
253 upperdentry = ovl_dentry_upper(dentry);
254 err = vfs_setxattr(upperdentry, name, value, size, flags);
257 ovl_drop_write(dentry);
262 static bool ovl_need_xattr_filter(struct dentry *dentry,
263 enum ovl_path_type type)
265 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
266 return S_ISDIR(dentry->d_inode->i_mode);
271 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
272 void *value, size_t size)
274 struct path realpath;
275 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
277 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
280 return vfs_getxattr(realpath.dentry, name, value, size);
283 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
285 struct path realpath;
286 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
290 res = vfs_listxattr(realpath.dentry, list, size);
291 if (res <= 0 || size == 0)
294 if (!ovl_need_xattr_filter(dentry, type))
297 /* filter out private xattrs */
298 for (off = 0; off < res;) {
299 char *s = list + off;
300 size_t slen = strlen(s) + 1;
302 BUG_ON(off + slen > res);
304 if (ovl_is_private_xattr(s)) {
306 memmove(s, s + slen, res - off);
315 int ovl_removexattr(struct dentry *dentry, const char *name)
318 struct path realpath;
319 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
321 err = ovl_want_write(dentry);
326 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
329 if (!OVL_TYPE_UPPER(type)) {
330 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
334 err = ovl_copy_up(dentry);
338 ovl_path_upper(dentry, &realpath);
341 err = vfs_removexattr(realpath.dentry, name);
343 ovl_drop_write(dentry);
348 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
349 struct dentry *realdentry)
351 if (OVL_TYPE_UPPER(type))
354 if (special_file(realdentry->d_inode->i_mode))
357 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
363 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
366 struct path realpath;
367 enum ovl_path_type type;
369 if (d_is_dir(dentry))
370 return d_backing_inode(dentry);
372 type = ovl_path_real(dentry, &realpath);
373 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
374 err = ovl_want_write(dentry);
378 if (file_flags & O_TRUNC)
379 err = ovl_copy_up_last(dentry, NULL, true);
381 err = ovl_copy_up(dentry);
382 ovl_drop_write(dentry);
386 ovl_path_upper(dentry, &realpath);
389 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
390 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
392 return d_backing_inode(realpath.dentry);
395 static const struct inode_operations ovl_file_inode_operations = {
396 .setattr = ovl_setattr,
397 .permission = ovl_permission,
398 .getattr = ovl_getattr,
399 .setxattr = ovl_setxattr,
400 .getxattr = ovl_getxattr,
401 .listxattr = ovl_listxattr,
402 .removexattr = ovl_removexattr,
405 static const struct inode_operations ovl_symlink_inode_operations = {
406 .setattr = ovl_setattr,
407 .follow_link = ovl_follow_link,
408 .put_link = ovl_put_link,
409 .readlink = ovl_readlink,
410 .getattr = ovl_getattr,
411 .setxattr = ovl_setxattr,
412 .getxattr = ovl_getxattr,
413 .listxattr = ovl_listxattr,
414 .removexattr = ovl_removexattr,
417 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
418 struct ovl_entry *oe)
422 inode = new_inode(sb);
428 inode->i_ino = get_next_ino();
429 inode->i_mode = mode;
430 inode->i_flags |= S_NOATIME | S_NOCMTIME;
434 inode->i_private = oe;
435 inode->i_op = &ovl_dir_inode_operations;
436 inode->i_fop = &ovl_dir_operations;
440 inode->i_op = &ovl_symlink_inode_operations;
448 inode->i_op = &ovl_file_inode_operations;
452 WARN(1, "illegal file type: %i\n", mode);