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;
45 err = ovl_want_write(dentry);
49 err = ovl_copy_up(dentry);
51 upperdentry = ovl_dentry_upper(dentry);
53 mutex_lock(&upperdentry->d_inode->i_mutex);
54 err = notify_change(upperdentry, attr, NULL);
55 mutex_unlock(&upperdentry->d_inode->i_mutex);
57 ovl_drop_write(dentry);
62 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
67 ovl_path_real(dentry, &realpath);
68 return vfs_getattr(&realpath, stat);
71 int ovl_permission(struct inode *inode, int mask)
74 struct dentry *alias = NULL;
75 struct inode *realinode;
76 struct dentry *realdentry;
80 if (S_ISDIR(inode->i_mode)) {
81 oe = inode->i_private;
82 } else if (mask & MAY_NOT_BLOCK) {
86 * For non-directories find an alias and get the info
89 alias = d_find_any_alias(inode);
96 realdentry = ovl_entry_real(oe, &is_upper);
98 /* Careful in RCU walk mode */
99 realinode = ACCESS_ONCE(realdentry->d_inode);
101 WARN_ON(!(mask & MAY_NOT_BLOCK));
106 if (mask & MAY_WRITE) {
107 umode_t mode = realinode->i_mode;
110 * Writes will always be redirected to upper layer, so
111 * ignore lower layer being read-only.
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
118 * If upper fs becomes read-only after the overlay was
119 * constructed return EROFS to prevent modification of
123 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
124 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
128 err = __inode_permission(realinode, mask);
135 struct ovl_link_data {
136 struct dentry *realdentry;
140 static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
142 struct dentry *realdentry;
143 struct inode *realinode;
144 struct ovl_link_data *data = NULL;
147 realdentry = ovl_dentry_real(dentry);
148 realinode = realdentry->d_inode;
150 if (WARN_ON(!realinode->i_op->follow_link))
151 return ERR_PTR(-EPERM);
153 if (realinode->i_op->put_link) {
154 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
156 return ERR_PTR(-ENOMEM);
157 data->realdentry = realdentry;
160 ret = realinode->i_op->follow_link(realdentry, cookie);
161 if (IS_ERR_OR_NULL(ret)) {
167 data->cookie = *cookie;
174 static void ovl_put_link(struct inode *unused, void *c)
176 struct inode *realinode;
177 struct ovl_link_data *data = c;
182 realinode = data->realdentry->d_inode;
183 realinode->i_op->put_link(realinode, data->cookie);
187 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
189 struct path realpath;
190 struct inode *realinode;
192 ovl_path_real(dentry, &realpath);
193 realinode = realpath.dentry->d_inode;
195 if (!realinode->i_op->readlink)
198 touch_atime(&realpath);
200 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
204 static bool ovl_is_private_xattr(const char *name)
206 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
209 int ovl_setxattr(struct dentry *dentry, const char *name,
210 const void *value, size_t size, int flags)
213 struct dentry *upperdentry;
215 err = ovl_want_write(dentry);
220 if (ovl_is_private_xattr(name))
223 err = ovl_copy_up(dentry);
227 upperdentry = ovl_dentry_upper(dentry);
228 err = vfs_setxattr(upperdentry, name, value, size, flags);
231 ovl_drop_write(dentry);
236 static bool ovl_need_xattr_filter(struct dentry *dentry,
237 enum ovl_path_type type)
239 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
240 return S_ISDIR(dentry->d_inode->i_mode);
245 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
246 void *value, size_t size)
248 struct path realpath;
249 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
251 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
254 return vfs_getxattr(realpath.dentry, name, value, size);
257 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
259 struct path realpath;
260 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
264 res = vfs_listxattr(realpath.dentry, list, size);
265 if (res <= 0 || size == 0)
268 if (!ovl_need_xattr_filter(dentry, type))
271 /* filter out private xattrs */
272 for (off = 0; off < res;) {
273 char *s = list + off;
274 size_t slen = strlen(s) + 1;
276 BUG_ON(off + slen > res);
278 if (ovl_is_private_xattr(s)) {
280 memmove(s, s + slen, res - off);
289 int ovl_removexattr(struct dentry *dentry, const char *name)
292 struct path realpath;
293 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
295 err = ovl_want_write(dentry);
300 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
303 if (!OVL_TYPE_UPPER(type)) {
304 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
308 err = ovl_copy_up(dentry);
312 ovl_path_upper(dentry, &realpath);
315 err = vfs_removexattr(realpath.dentry, name);
317 ovl_drop_write(dentry);
322 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
323 struct dentry *realdentry)
325 if (OVL_TYPE_UPPER(type))
328 if (special_file(realdentry->d_inode->i_mode))
331 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
337 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
340 struct path realpath;
341 enum ovl_path_type type;
343 if (d_is_dir(dentry))
344 return d_backing_inode(dentry);
346 type = ovl_path_real(dentry, &realpath);
347 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
348 err = ovl_want_write(dentry);
352 if (file_flags & O_TRUNC)
353 err = ovl_copy_up_truncate(dentry);
355 err = ovl_copy_up(dentry);
356 ovl_drop_write(dentry);
360 ovl_path_upper(dentry, &realpath);
363 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
364 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
366 return d_backing_inode(realpath.dentry);
369 static const struct inode_operations ovl_file_inode_operations = {
370 .setattr = ovl_setattr,
371 .permission = ovl_permission,
372 .getattr = ovl_getattr,
373 .setxattr = ovl_setxattr,
374 .getxattr = ovl_getxattr,
375 .listxattr = ovl_listxattr,
376 .removexattr = ovl_removexattr,
379 static const struct inode_operations ovl_symlink_inode_operations = {
380 .setattr = ovl_setattr,
381 .follow_link = ovl_follow_link,
382 .put_link = ovl_put_link,
383 .readlink = ovl_readlink,
384 .getattr = ovl_getattr,
385 .setxattr = ovl_setxattr,
386 .getxattr = ovl_getxattr,
387 .listxattr = ovl_listxattr,
388 .removexattr = ovl_removexattr,
391 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
392 struct ovl_entry *oe)
396 inode = new_inode(sb);
402 inode->i_ino = get_next_ino();
403 inode->i_mode = mode;
404 inode->i_flags |= S_NOATIME | S_NOCMTIME;
408 inode->i_private = oe;
409 inode->i_op = &ovl_dir_inode_operations;
410 inode->i_fop = &ovl_dir_operations;
414 inode->i_op = &ovl_symlink_inode_operations;
422 inode->i_op = &ovl_file_inode_operations;
426 WARN(1, "illegal file type: %i\n", mode);