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;
49 * Check for permissions before trying to copy-up. This is redundant
50 * since it will be rechecked later by ->setattr() on upper dentry. But
51 * without this, copy-up can be triggered by just about anybody.
53 * We don't initialize inode->size, which just means that
54 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
55 * check for a swapfile (which this won't be anyway).
57 err = setattr_prepare(dentry, attr);
61 err = ovl_want_write(dentry);
65 err = ovl_copy_up(dentry);
67 upperdentry = ovl_dentry_upper(dentry);
69 mutex_lock(&upperdentry->d_inode->i_mutex);
71 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
72 attr->ia_valid &= ~ATTR_MODE;
74 err = notify_change(upperdentry, attr, NULL);
76 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
77 mutex_unlock(&upperdentry->d_inode->i_mutex);
79 ovl_drop_write(dentry);
84 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
89 ovl_path_real(dentry, &realpath);
90 return vfs_getattr(&realpath, stat);
93 int ovl_permission(struct inode *inode, int mask)
96 struct dentry *alias = NULL;
97 struct inode *realinode;
98 struct dentry *realdentry;
102 if (S_ISDIR(inode->i_mode)) {
103 oe = inode->i_private;
104 } else if (mask & MAY_NOT_BLOCK) {
108 * For non-directories find an alias and get the info
111 alias = d_find_any_alias(inode);
115 oe = alias->d_fsdata;
118 realdentry = ovl_entry_real(oe, &is_upper);
120 /* Careful in RCU walk mode */
121 realinode = ACCESS_ONCE(realdentry->d_inode);
123 WARN_ON(!(mask & MAY_NOT_BLOCK));
128 if (mask & MAY_WRITE) {
129 umode_t mode = realinode->i_mode;
132 * Writes will always be redirected to upper layer, so
133 * ignore lower layer being read-only.
135 * If the overlay itself is read-only then proceed
136 * with the permission check, don't return EROFS.
137 * This will only happen if this is the lower layer of
140 * If upper fs becomes read-only after the overlay was
141 * constructed return EROFS to prevent modification of
145 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
146 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
150 err = __inode_permission(realinode, mask);
157 struct ovl_link_data {
158 struct dentry *realdentry;
162 static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
165 struct dentry *realdentry;
166 struct inode *realinode;
168 realdentry = ovl_dentry_real(dentry);
169 realinode = realdentry->d_inode;
171 if (WARN_ON(!realinode->i_op->follow_link))
172 return ERR_PTR(-EPERM);
174 ret = realinode->i_op->follow_link(realdentry, nd);
178 if (realinode->i_op->put_link) {
179 struct ovl_link_data *data;
181 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
183 realinode->i_op->put_link(realdentry, nd, ret);
184 return ERR_PTR(-ENOMEM);
186 data->realdentry = realdentry;
195 static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
197 struct inode *realinode;
198 struct ovl_link_data *data = c;
203 realinode = data->realdentry->d_inode;
204 realinode->i_op->put_link(data->realdentry, nd, data->cookie);
208 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
210 struct path realpath;
211 struct inode *realinode;
213 ovl_path_real(dentry, &realpath);
214 realinode = realpath.dentry->d_inode;
216 if (!realinode->i_op->readlink)
219 touch_atime(&realpath);
221 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
224 bool ovl_is_private_xattr(const char *name)
226 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
229 int ovl_setxattr(struct dentry *dentry, const char *name,
230 const void *value, size_t size, int flags)
233 struct dentry *upperdentry;
235 err = ovl_want_write(dentry);
240 if (ovl_is_private_xattr(name))
243 err = ovl_copy_up(dentry);
247 upperdentry = ovl_dentry_upper(dentry);
248 err = vfs_setxattr(upperdentry, name, value, size, flags);
251 ovl_drop_write(dentry);
256 static bool ovl_need_xattr_filter(struct dentry *dentry,
257 enum ovl_path_type type)
259 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
260 return S_ISDIR(dentry->d_inode->i_mode);
265 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
266 void *value, size_t size)
268 struct path realpath;
269 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
271 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
274 return vfs_getxattr(realpath.dentry, name, value, size);
277 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
279 struct path realpath;
280 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
285 res = vfs_listxattr(realpath.dentry, list, size);
286 if (res <= 0 || size == 0)
289 if (!ovl_need_xattr_filter(dentry, type))
292 /* filter out private xattrs */
293 for (s = list, len = res; len;) {
294 size_t slen = strnlen(s, len) + 1;
296 /* underlying fs providing us with an broken xattr list? */
297 if (WARN_ON(slen > len))
301 if (ovl_is_private_xattr(s)) {
303 memmove(s, s + slen, len);
312 int ovl_removexattr(struct dentry *dentry, const char *name)
315 struct path realpath;
316 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
318 err = ovl_want_write(dentry);
323 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
326 if (!OVL_TYPE_UPPER(type)) {
327 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
331 err = ovl_copy_up(dentry);
335 ovl_path_upper(dentry, &realpath);
338 err = vfs_removexattr(realpath.dentry, name);
340 ovl_drop_write(dentry);
345 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
346 struct dentry *realdentry)
348 if (OVL_TYPE_UPPER(type))
351 if (special_file(realdentry->d_inode->i_mode))
354 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
360 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
363 struct path realpath;
364 enum ovl_path_type type;
366 if (d_is_dir(dentry))
367 return d_backing_inode(dentry);
369 type = ovl_path_real(dentry, &realpath);
370 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
371 err = ovl_want_write(dentry);
375 if (file_flags & O_TRUNC)
376 err = ovl_copy_up_last(dentry, NULL, true);
378 err = ovl_copy_up(dentry);
379 ovl_drop_write(dentry);
383 ovl_path_upper(dentry, &realpath);
386 return d_backing_inode(realpath.dentry);
389 static const struct inode_operations ovl_file_inode_operations = {
390 .setattr = ovl_setattr,
391 .permission = ovl_permission,
392 .getattr = ovl_getattr,
393 .setxattr = ovl_setxattr,
394 .getxattr = ovl_getxattr,
395 .listxattr = ovl_listxattr,
396 .removexattr = ovl_removexattr,
399 static const struct inode_operations ovl_symlink_inode_operations = {
400 .setattr = ovl_setattr,
401 .follow_link = ovl_follow_link,
402 .put_link = ovl_put_link,
403 .readlink = ovl_readlink,
404 .getattr = ovl_getattr,
405 .setxattr = ovl_setxattr,
406 .getxattr = ovl_getxattr,
407 .listxattr = ovl_listxattr,
408 .removexattr = ovl_removexattr,
411 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
412 struct ovl_entry *oe)
416 inode = new_inode(sb);
420 inode->i_ino = get_next_ino();
421 inode->i_mode = mode;
422 inode->i_flags |= S_NOATIME | S_NOCMTIME;
427 inode->i_private = oe;
428 inode->i_op = &ovl_dir_inode_operations;
429 inode->i_fop = &ovl_dir_operations;
433 inode->i_op = &ovl_symlink_inode_operations;
441 inode->i_op = &ovl_file_inode_operations;
445 WARN(1, "illegal file type: %i\n", mode);