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 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
67 attr->ia_valid &= ~ATTR_MODE;
69 mutex_lock(&upperdentry->d_inode->i_mutex);
70 err = notify_change(upperdentry, attr, NULL);
72 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
73 mutex_unlock(&upperdentry->d_inode->i_mutex);
75 ovl_drop_write(dentry);
80 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
85 ovl_path_real(dentry, &realpath);
86 return vfs_getattr(&realpath, stat);
89 int ovl_permission(struct inode *inode, int mask)
92 struct dentry *alias = NULL;
93 struct inode *realinode;
94 struct dentry *realdentry;
98 if (S_ISDIR(inode->i_mode)) {
99 oe = inode->i_private;
100 } else if (mask & MAY_NOT_BLOCK) {
104 * For non-directories find an alias and get the info
107 alias = d_find_any_alias(inode);
111 oe = alias->d_fsdata;
114 realdentry = ovl_entry_real(oe, &is_upper);
116 /* Careful in RCU walk mode */
117 realinode = ACCESS_ONCE(realdentry->d_inode);
119 WARN_ON(!(mask & MAY_NOT_BLOCK));
124 if (mask & MAY_WRITE) {
125 umode_t mode = realinode->i_mode;
128 * Writes will always be redirected to upper layer, so
129 * ignore lower layer being read-only.
131 * If the overlay itself is read-only then proceed
132 * with the permission check, don't return EROFS.
133 * This will only happen if this is the lower layer of
136 * If upper fs becomes read-only after the overlay was
137 * constructed return EROFS to prevent modification of
141 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
142 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
146 err = __inode_permission(realinode, mask);
153 struct ovl_link_data {
154 struct dentry *realdentry;
158 static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
160 struct dentry *realdentry;
161 struct inode *realinode;
162 struct ovl_link_data *data = NULL;
165 realdentry = ovl_dentry_real(dentry);
166 realinode = realdentry->d_inode;
168 if (WARN_ON(!realinode->i_op->follow_link))
169 return ERR_PTR(-EPERM);
171 if (realinode->i_op->put_link) {
172 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
174 return ERR_PTR(-ENOMEM);
175 data->realdentry = realdentry;
178 ret = realinode->i_op->follow_link(realdentry, cookie);
179 if (IS_ERR_OR_NULL(ret)) {
185 data->cookie = *cookie;
192 static void ovl_put_link(struct inode *unused, void *c)
194 struct inode *realinode;
195 struct ovl_link_data *data = c;
200 realinode = data->realdentry->d_inode;
201 realinode->i_op->put_link(realinode, data->cookie);
205 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
207 struct path realpath;
208 struct inode *realinode;
210 ovl_path_real(dentry, &realpath);
211 realinode = realpath.dentry->d_inode;
213 if (!realinode->i_op->readlink)
216 touch_atime(&realpath);
218 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
222 bool ovl_is_private_xattr(const char *name)
224 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
227 int ovl_setxattr(struct dentry *dentry, const char *name,
228 const void *value, size_t size, int flags)
231 struct dentry *upperdentry;
233 err = ovl_want_write(dentry);
238 if (ovl_is_private_xattr(name))
241 err = ovl_copy_up(dentry);
245 upperdentry = ovl_dentry_upper(dentry);
246 err = vfs_setxattr(upperdentry, name, value, size, flags);
249 ovl_drop_write(dentry);
254 static bool ovl_need_xattr_filter(struct dentry *dentry,
255 enum ovl_path_type type)
257 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
258 return S_ISDIR(dentry->d_inode->i_mode);
263 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
264 void *value, size_t size)
266 struct path realpath;
267 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
269 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
272 return vfs_getxattr(realpath.dentry, name, value, size);
275 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
277 struct path realpath;
278 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
283 res = vfs_listxattr(realpath.dentry, list, size);
284 if (res <= 0 || size == 0)
287 if (!ovl_need_xattr_filter(dentry, type))
290 /* filter out private xattrs */
291 for (s = list, len = res; len;) {
292 size_t slen = strnlen(s, len) + 1;
294 /* underlying fs providing us with an broken xattr list? */
295 if (WARN_ON(slen > len))
299 if (ovl_is_private_xattr(s)) {
301 memmove(s, s + slen, len);
310 int ovl_removexattr(struct dentry *dentry, const char *name)
313 struct path realpath;
314 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
316 err = ovl_want_write(dentry);
321 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
324 if (!OVL_TYPE_UPPER(type)) {
325 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
329 err = ovl_copy_up(dentry);
333 ovl_path_upper(dentry, &realpath);
336 err = vfs_removexattr(realpath.dentry, name);
338 ovl_drop_write(dentry);
343 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
344 struct dentry *realdentry)
346 if (OVL_TYPE_UPPER(type))
349 if (special_file(realdentry->d_inode->i_mode))
352 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
358 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
361 struct path realpath;
362 enum ovl_path_type type;
364 if (d_is_dir(dentry))
365 return d_backing_inode(dentry);
367 type = ovl_path_real(dentry, &realpath);
368 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
369 err = ovl_want_write(dentry);
373 if (file_flags & O_TRUNC)
374 err = ovl_copy_up_truncate(dentry);
376 err = ovl_copy_up(dentry);
377 ovl_drop_write(dentry);
381 ovl_path_upper(dentry, &realpath);
384 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
385 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
387 return d_backing_inode(realpath.dentry);
390 static const struct inode_operations ovl_file_inode_operations = {
391 .setattr = ovl_setattr,
392 .permission = ovl_permission,
393 .getattr = ovl_getattr,
394 .setxattr = ovl_setxattr,
395 .getxattr = ovl_getxattr,
396 .listxattr = ovl_listxattr,
397 .removexattr = ovl_removexattr,
400 static const struct inode_operations ovl_symlink_inode_operations = {
401 .setattr = ovl_setattr,
402 .follow_link = ovl_follow_link,
403 .put_link = ovl_put_link,
404 .readlink = ovl_readlink,
405 .getattr = ovl_getattr,
406 .setxattr = ovl_setxattr,
407 .getxattr = ovl_getxattr,
408 .listxattr = ovl_listxattr,
409 .removexattr = ovl_removexattr,
412 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
413 struct ovl_entry *oe)
417 inode = new_inode(sb);
421 inode->i_ino = get_next_ino();
422 inode->i_mode = mode;
423 inode->i_flags |= S_NOATIME | S_NOCMTIME;
428 inode->i_private = oe;
429 inode->i_op = &ovl_dir_inode_operations;
430 inode->i_fop = &ovl_dir_operations;
434 inode->i_op = &ovl_symlink_inode_operations;
442 inode->i_op = &ovl_file_inode_operations;
446 WARN(1, "illegal file type: %i\n", mode);