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 if (attr->ia_valid & ATTR_SIZE) {
63 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
66 if (atomic_read(&realinode->i_writecount) < 0)
70 err = ovl_copy_up(dentry);
72 struct inode *winode = NULL;
74 upperdentry = ovl_dentry_upper(dentry);
76 if (attr->ia_valid & ATTR_SIZE) {
77 winode = d_inode(upperdentry);
78 err = get_write_access(winode);
83 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
84 attr->ia_valid &= ~ATTR_MODE;
86 inode_lock(upperdentry->d_inode);
87 err = notify_change(upperdentry, attr, NULL);
89 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
90 inode_unlock(upperdentry->d_inode);
93 put_write_access(winode);
96 ovl_drop_write(dentry);
101 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
104 struct path realpath;
106 ovl_path_real(dentry, &realpath);
107 return vfs_getattr(&realpath, stat);
110 int ovl_permission(struct inode *inode, int mask)
112 struct ovl_entry *oe;
113 struct dentry *alias = NULL;
114 struct inode *realinode;
115 struct dentry *realdentry;
119 if (S_ISDIR(inode->i_mode)) {
120 oe = inode->i_private;
121 } else if (mask & MAY_NOT_BLOCK) {
125 * For non-directories find an alias and get the info
128 alias = d_find_any_alias(inode);
132 oe = alias->d_fsdata;
135 realdentry = ovl_entry_real(oe, &is_upper);
137 if (ovl_is_default_permissions(inode)) {
139 struct path realpath = { .dentry = realdentry };
141 if (mask & MAY_NOT_BLOCK)
144 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
146 err = vfs_getattr(&realpath, &stat);
151 if ((stat.mode ^ inode->i_mode) & S_IFMT)
154 inode->i_mode = stat.mode;
155 inode->i_uid = stat.uid;
156 inode->i_gid = stat.gid;
158 err = generic_permission(inode, mask);
162 /* Careful in RCU walk mode */
163 realinode = ACCESS_ONCE(realdentry->d_inode);
165 WARN_ON(!(mask & MAY_NOT_BLOCK));
170 if (mask & MAY_WRITE) {
171 umode_t mode = realinode->i_mode;
174 * Writes will always be redirected to upper layer, so
175 * ignore lower layer being read-only.
177 * If the overlay itself is read-only then proceed
178 * with the permission check, don't return EROFS.
179 * This will only happen if this is the lower layer of
182 * If upper fs becomes read-only after the overlay was
183 * constructed return EROFS to prevent modification of
187 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
188 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
192 err = __inode_permission(realinode, mask);
198 static const char *ovl_get_link(struct dentry *dentry,
200 struct delayed_call *done)
202 struct dentry *realdentry;
203 struct inode *realinode;
206 return ERR_PTR(-ECHILD);
208 realdentry = ovl_dentry_real(dentry);
209 realinode = realdentry->d_inode;
211 if (WARN_ON(!realinode->i_op->get_link))
212 return ERR_PTR(-EPERM);
214 return realinode->i_op->get_link(realdentry, realinode, done);
217 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
219 struct path realpath;
220 struct inode *realinode;
222 ovl_path_real(dentry, &realpath);
223 realinode = realpath.dentry->d_inode;
225 if (!realinode->i_op->readlink)
228 touch_atime(&realpath);
230 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
234 static bool ovl_is_private_xattr(const char *name)
236 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
239 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
240 const char *name, const void *value,
241 size_t size, int flags)
244 struct dentry *upperdentry;
246 err = ovl_want_write(dentry);
251 if (ovl_is_private_xattr(name))
254 err = ovl_copy_up(dentry);
258 upperdentry = ovl_dentry_upper(dentry);
259 err = vfs_setxattr(upperdentry, name, value, size, flags);
262 ovl_drop_write(dentry);
267 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
268 const char *name, void *value, size_t size)
270 struct dentry *realdentry = ovl_dentry_real(dentry);
272 if (ovl_is_private_xattr(name))
275 return vfs_getxattr(realdentry, name, value, size);
278 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
280 struct dentry *realdentry = ovl_dentry_real(dentry);
284 res = vfs_listxattr(realdentry, list, size);
285 if (res <= 0 || size == 0)
288 /* filter out private xattrs */
289 for (off = 0; off < res;) {
290 char *s = list + off;
291 size_t slen = strlen(s) + 1;
293 BUG_ON(off + slen > res);
295 if (ovl_is_private_xattr(s)) {
297 memmove(s, s + slen, res - off);
306 int ovl_removexattr(struct dentry *dentry, const char *name)
309 struct path realpath;
310 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
312 err = ovl_want_write(dentry);
317 if (ovl_is_private_xattr(name))
320 if (!OVL_TYPE_UPPER(type)) {
321 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
325 err = ovl_copy_up(dentry);
329 ovl_path_upper(dentry, &realpath);
332 err = vfs_removexattr(realpath.dentry, name);
334 ovl_drop_write(dentry);
339 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
340 struct dentry *realdentry)
342 if (OVL_TYPE_UPPER(type))
345 if (special_file(realdentry->d_inode->i_mode))
348 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
354 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
357 struct path realpath;
358 enum ovl_path_type type;
360 if (d_is_dir(dentry))
361 return d_backing_inode(dentry);
363 type = ovl_path_real(dentry, &realpath);
364 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
365 err = ovl_want_write(dentry);
369 if (file_flags & O_TRUNC)
370 err = ovl_copy_up_truncate(dentry);
372 err = ovl_copy_up(dentry);
373 ovl_drop_write(dentry);
377 ovl_path_upper(dentry, &realpath);
380 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
381 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
383 return d_backing_inode(realpath.dentry);
386 static const struct inode_operations ovl_file_inode_operations = {
387 .setattr = ovl_setattr,
388 .permission = ovl_permission,
389 .getattr = ovl_getattr,
390 .setxattr = ovl_setxattr,
391 .getxattr = ovl_getxattr,
392 .listxattr = ovl_listxattr,
393 .removexattr = ovl_removexattr,
396 static const struct inode_operations ovl_symlink_inode_operations = {
397 .setattr = ovl_setattr,
398 .get_link = ovl_get_link,
399 .readlink = ovl_readlink,
400 .getattr = ovl_getattr,
401 .setxattr = ovl_setxattr,
402 .getxattr = ovl_getxattr,
403 .listxattr = ovl_listxattr,
404 .removexattr = ovl_removexattr,
407 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
408 struct ovl_entry *oe)
412 inode = new_inode(sb);
416 inode->i_ino = get_next_ino();
417 inode->i_mode = mode;
418 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);