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;
44 const struct cred *old_cred;
47 * Check for permissions before trying to copy-up. This is redundant
48 * since it will be rechecked later by ->setattr() on upper dentry. But
49 * without this, copy-up can be triggered by just about anybody.
51 * We don't initialize inode->size, which just means that
52 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
53 * check for a swapfile (which this won't be anyway).
55 err = inode_change_ok(dentry->d_inode, attr);
59 err = ovl_want_write(dentry);
63 if (attr->ia_valid & ATTR_SIZE) {
64 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
67 if (atomic_read(&realinode->i_writecount) < 0)
71 err = ovl_copy_up(dentry);
73 struct inode *winode = NULL;
75 upperdentry = ovl_dentry_upper(dentry);
77 if (attr->ia_valid & ATTR_SIZE) {
78 winode = d_inode(upperdentry);
79 err = get_write_access(winode);
84 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
85 attr->ia_valid &= ~ATTR_MODE;
87 inode_lock(upperdentry->d_inode);
88 old_cred = ovl_override_creds(dentry->d_sb);
89 err = notify_change(upperdentry, attr, NULL);
90 revert_creds(old_cred);
92 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
93 inode_unlock(upperdentry->d_inode);
96 put_write_access(winode);
99 ovl_drop_write(dentry);
104 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
107 struct path realpath;
108 const struct cred *old_cred;
111 ovl_path_real(dentry, &realpath);
112 old_cred = ovl_override_creds(dentry->d_sb);
113 err = vfs_getattr(&realpath, stat);
114 revert_creds(old_cred);
118 int ovl_permission(struct inode *inode, int mask)
120 struct ovl_entry *oe = inode->i_private;
122 struct dentry *realdentry = ovl_entry_real(oe, &is_upper);
123 struct inode *realinode;
124 const struct cred *old_cred;
127 /* Careful in RCU walk mode */
128 realinode = d_inode_rcu(realdentry);
130 WARN_ON(!(mask & MAY_NOT_BLOCK));
135 * Check overlay inode with the creds of task and underlying inode
136 * with creds of mounter
138 err = generic_permission(inode, mask);
142 old_cred = ovl_override_creds(inode->i_sb);
144 mask &= ~(MAY_WRITE | MAY_APPEND);
145 err = inode_permission(realinode, mask);
146 revert_creds(old_cred);
151 static const char *ovl_get_link(struct dentry *dentry,
153 struct delayed_call *done)
155 struct dentry *realdentry;
156 struct inode *realinode;
157 const struct cred *old_cred;
161 return ERR_PTR(-ECHILD);
163 realdentry = ovl_dentry_real(dentry);
164 realinode = realdentry->d_inode;
166 if (WARN_ON(!realinode->i_op->get_link))
167 return ERR_PTR(-EPERM);
169 old_cred = ovl_override_creds(dentry->d_sb);
170 p = realinode->i_op->get_link(realdentry, realinode, done);
171 revert_creds(old_cred);
175 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
177 struct path realpath;
178 struct inode *realinode;
179 const struct cred *old_cred;
182 ovl_path_real(dentry, &realpath);
183 realinode = realpath.dentry->d_inode;
185 if (!realinode->i_op->readlink)
188 old_cred = ovl_override_creds(dentry->d_sb);
189 err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
190 revert_creds(old_cred);
194 static bool ovl_is_private_xattr(const char *name)
196 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
199 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
200 const char *name, const void *value,
201 size_t size, int flags)
204 struct dentry *upperdentry;
205 const struct cred *old_cred;
207 err = ovl_want_write(dentry);
212 if (ovl_is_private_xattr(name))
215 err = ovl_copy_up(dentry);
219 upperdentry = ovl_dentry_upper(dentry);
220 old_cred = ovl_override_creds(dentry->d_sb);
221 err = vfs_setxattr(upperdentry, name, value, size, flags);
222 revert_creds(old_cred);
225 ovl_drop_write(dentry);
230 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
231 const char *name, void *value, size_t size)
233 struct dentry *realdentry = ovl_dentry_real(dentry);
235 const struct cred *old_cred;
237 if (ovl_is_private_xattr(name))
240 old_cred = ovl_override_creds(dentry->d_sb);
241 res = vfs_getxattr(realdentry, name, value, size);
242 revert_creds(old_cred);
246 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
248 struct dentry *realdentry = ovl_dentry_real(dentry);
251 const struct cred *old_cred;
253 old_cred = ovl_override_creds(dentry->d_sb);
254 res = vfs_listxattr(realdentry, list, size);
255 revert_creds(old_cred);
256 if (res <= 0 || size == 0)
259 /* filter out private xattrs */
260 for (off = 0; off < res;) {
261 char *s = list + off;
262 size_t slen = strlen(s) + 1;
264 BUG_ON(off + slen > res);
266 if (ovl_is_private_xattr(s)) {
268 memmove(s, s + slen, res - off);
277 int ovl_removexattr(struct dentry *dentry, const char *name)
280 struct path realpath;
281 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
282 const struct cred *old_cred;
284 err = ovl_want_write(dentry);
289 if (ovl_is_private_xattr(name))
292 if (!OVL_TYPE_UPPER(type)) {
293 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
297 err = ovl_copy_up(dentry);
301 ovl_path_upper(dentry, &realpath);
304 old_cred = ovl_override_creds(dentry->d_sb);
305 err = vfs_removexattr(realpath.dentry, name);
306 revert_creds(old_cred);
308 ovl_drop_write(dentry);
313 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
315 struct inode *realinode = ovl_inode_real(inode);
316 const struct cred *old_cred;
317 struct posix_acl *acl;
319 if (!IS_POSIXACL(realinode))
322 if (!realinode->i_op->get_acl)
325 old_cred = ovl_override_creds(inode->i_sb);
326 acl = realinode->i_op->get_acl(realinode, type);
327 revert_creds(old_cred);
332 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
333 struct dentry *realdentry)
335 if (OVL_TYPE_UPPER(type))
338 if (special_file(realdentry->d_inode->i_mode))
341 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
347 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
350 struct path realpath;
351 enum ovl_path_type type;
353 type = ovl_path_real(dentry, &realpath);
354 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
355 err = ovl_want_write(dentry);
357 if (file_flags & O_TRUNC)
358 err = ovl_copy_up_truncate(dentry);
360 err = ovl_copy_up(dentry);
361 ovl_drop_write(dentry);
368 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
370 struct dentry *alias;
371 struct path upperpath;
373 if (!(flags & S_ATIME))
376 alias = d_find_any_alias(inode);
380 ovl_path_upper(alias, &upperpath);
381 if (upperpath.dentry) {
382 touch_atime(&upperpath);
383 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
391 static const struct inode_operations ovl_file_inode_operations = {
392 .setattr = ovl_setattr,
393 .permission = ovl_permission,
394 .getattr = ovl_getattr,
395 .setxattr = ovl_setxattr,
396 .getxattr = ovl_getxattr,
397 .listxattr = ovl_listxattr,
398 .removexattr = ovl_removexattr,
399 .get_acl = ovl_get_acl,
400 .update_time = ovl_update_time,
403 static const struct inode_operations ovl_symlink_inode_operations = {
404 .setattr = ovl_setattr,
405 .get_link = ovl_get_link,
406 .readlink = ovl_readlink,
407 .getattr = ovl_getattr,
408 .setxattr = ovl_setxattr,
409 .getxattr = ovl_getxattr,
410 .listxattr = ovl_listxattr,
411 .removexattr = ovl_removexattr,
412 .update_time = ovl_update_time,
415 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
416 struct ovl_entry *oe)
420 inode = new_inode(sb);
424 inode->i_ino = get_next_ino();
425 inode->i_mode = mode;
426 inode->i_flags |= S_NOCMTIME;
427 inode->i_private = oe;
432 inode->i_op = &ovl_dir_inode_operations;
433 inode->i_fop = &ovl_dir_operations;
437 inode->i_op = &ovl_symlink_inode_operations;
445 inode->i_op = &ovl_file_inode_operations;
449 WARN(1, "illegal file type: %i\n", mode);