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 = inode->i_private;
114 struct dentry *realdentry = ovl_entry_real(oe, &is_upper);
115 struct inode *realinode;
116 const struct cred *old_cred;
119 if (ovl_is_default_permissions(inode)) {
121 struct path realpath = { .dentry = realdentry };
123 if (mask & MAY_NOT_BLOCK)
126 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
128 err = vfs_getattr(&realpath, &stat);
132 if ((stat.mode ^ inode->i_mode) & S_IFMT)
135 inode->i_mode = stat.mode;
136 inode->i_uid = stat.uid;
137 inode->i_gid = stat.gid;
139 return generic_permission(inode, mask);
142 /* Careful in RCU walk mode */
143 realinode = d_inode_rcu(realdentry);
145 WARN_ON(!(mask & MAY_NOT_BLOCK));
149 if (mask & MAY_WRITE) {
150 umode_t mode = realinode->i_mode;
153 * Writes will always be redirected to upper layer, so
154 * ignore lower layer being read-only.
156 * If the overlay itself is read-only then proceed
157 * with the permission check, don't return EROFS.
158 * This will only happen if this is the lower layer of
161 * If upper fs becomes read-only after the overlay was
162 * constructed return EROFS to prevent modification of
165 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
166 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
171 * Check overlay inode with the creds of task and underlying inode
172 * with creds of mounter
174 err = generic_permission(inode, mask);
178 old_cred = ovl_override_creds(inode->i_sb);
179 err = __inode_permission(realinode, mask);
180 revert_creds(old_cred);
185 static const char *ovl_get_link(struct dentry *dentry,
187 struct delayed_call *done)
189 struct dentry *realdentry;
190 struct inode *realinode;
193 return ERR_PTR(-ECHILD);
195 realdentry = ovl_dentry_real(dentry);
196 realinode = realdentry->d_inode;
198 if (WARN_ON(!realinode->i_op->get_link))
199 return ERR_PTR(-EPERM);
201 return realinode->i_op->get_link(realdentry, realinode, done);
204 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
206 struct path realpath;
207 struct inode *realinode;
209 ovl_path_real(dentry, &realpath);
210 realinode = realpath.dentry->d_inode;
212 if (!realinode->i_op->readlink)
215 touch_atime(&realpath);
217 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
221 static bool ovl_is_private_xattr(const char *name)
223 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
226 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
227 const char *name, const void *value,
228 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 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
255 const char *name, void *value, size_t size)
257 struct dentry *realdentry = ovl_dentry_real(dentry);
259 if (ovl_is_private_xattr(name))
262 return vfs_getxattr(realdentry, name, value, size);
265 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
267 struct dentry *realdentry = ovl_dentry_real(dentry);
271 res = vfs_listxattr(realdentry, list, size);
272 if (res <= 0 || size == 0)
275 /* filter out private xattrs */
276 for (off = 0; off < res;) {
277 char *s = list + off;
278 size_t slen = strlen(s) + 1;
280 BUG_ON(off + slen > res);
282 if (ovl_is_private_xattr(s)) {
284 memmove(s, s + slen, res - off);
293 int ovl_removexattr(struct dentry *dentry, const char *name)
296 struct path realpath;
297 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
299 err = ovl_want_write(dentry);
304 if (ovl_is_private_xattr(name))
307 if (!OVL_TYPE_UPPER(type)) {
308 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
312 err = ovl_copy_up(dentry);
316 ovl_path_upper(dentry, &realpath);
319 err = vfs_removexattr(realpath.dentry, name);
321 ovl_drop_write(dentry);
326 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
328 struct inode *realinode = ovl_inode_real(inode);
330 if (!IS_POSIXACL(realinode))
333 if (!realinode->i_op->get_acl)
336 return realinode->i_op->get_acl(realinode, type);
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 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
357 struct path realpath;
358 enum ovl_path_type type;
360 type = ovl_path_real(dentry, &realpath);
361 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
362 err = ovl_want_write(dentry);
364 if (file_flags & O_TRUNC)
365 err = ovl_copy_up_truncate(dentry);
367 err = ovl_copy_up(dentry);
368 ovl_drop_write(dentry);
375 static const struct inode_operations ovl_file_inode_operations = {
376 .setattr = ovl_setattr,
377 .permission = ovl_permission,
378 .getattr = ovl_getattr,
379 .setxattr = ovl_setxattr,
380 .getxattr = ovl_getxattr,
381 .listxattr = ovl_listxattr,
382 .removexattr = ovl_removexattr,
383 .get_acl = ovl_get_acl,
386 static const struct inode_operations ovl_symlink_inode_operations = {
387 .setattr = ovl_setattr,
388 .get_link = ovl_get_link,
389 .readlink = ovl_readlink,
390 .getattr = ovl_getattr,
391 .setxattr = ovl_setxattr,
392 .getxattr = ovl_getxattr,
393 .listxattr = ovl_listxattr,
394 .removexattr = ovl_removexattr,
397 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
398 struct ovl_entry *oe)
402 inode = new_inode(sb);
406 inode->i_ino = get_next_ino();
407 inode->i_mode = mode;
408 inode->i_flags |= S_NOATIME | S_NOCMTIME;
409 inode->i_private = oe;
414 inode->i_op = &ovl_dir_inode_operations;
415 inode->i_fop = &ovl_dir_operations;
419 inode->i_op = &ovl_symlink_inode_operations;
427 inode->i_op = &ovl_file_inode_operations;
431 WARN(1, "illegal file type: %i\n", mode);