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 inode_lock(upperdentry->d_inode);
67 err = notify_change(upperdentry, attr, NULL);
69 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
70 inode_unlock(upperdentry->d_inode);
72 ovl_drop_write(dentry);
77 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
82 ovl_path_real(dentry, &realpath);
83 return vfs_getattr(&realpath, stat);
86 int ovl_permission(struct inode *inode, int mask)
89 struct dentry *alias = NULL;
90 struct inode *realinode;
91 struct dentry *realdentry;
95 if (S_ISDIR(inode->i_mode)) {
96 oe = inode->i_private;
97 } else if (mask & MAY_NOT_BLOCK) {
101 * For non-directories find an alias and get the info
104 alias = d_find_any_alias(inode);
108 oe = alias->d_fsdata;
111 realdentry = ovl_entry_real(oe, &is_upper);
113 if (ovl_is_default_permissions(inode)) {
115 struct path realpath = { .dentry = realdentry };
117 if (mask & MAY_NOT_BLOCK)
120 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
122 err = vfs_getattr(&realpath, &stat);
126 if ((stat.mode ^ inode->i_mode) & S_IFMT)
129 inode->i_mode = stat.mode;
130 inode->i_uid = stat.uid;
131 inode->i_gid = stat.gid;
133 return generic_permission(inode, mask);
136 /* Careful in RCU walk mode */
137 realinode = ACCESS_ONCE(realdentry->d_inode);
139 WARN_ON(!(mask & MAY_NOT_BLOCK));
144 if (mask & MAY_WRITE) {
145 umode_t mode = realinode->i_mode;
148 * Writes will always be redirected to upper layer, so
149 * ignore lower layer being read-only.
151 * If the overlay itself is read-only then proceed
152 * with the permission check, don't return EROFS.
153 * This will only happen if this is the lower layer of
156 * If upper fs becomes read-only after the overlay was
157 * constructed return EROFS to prevent modification of
161 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
162 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
166 err = __inode_permission(realinode, mask);
172 static const char *ovl_get_link(struct dentry *dentry,
174 struct delayed_call *done)
176 struct dentry *realdentry;
177 struct inode *realinode;
180 return ERR_PTR(-ECHILD);
182 realdentry = ovl_dentry_real(dentry);
183 realinode = realdentry->d_inode;
185 if (WARN_ON(!realinode->i_op->get_link))
186 return ERR_PTR(-EPERM);
188 return realinode->i_op->get_link(realdentry, realinode, done);
191 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
193 struct path realpath;
194 struct inode *realinode;
196 ovl_path_real(dentry, &realpath);
197 realinode = realpath.dentry->d_inode;
199 if (!realinode->i_op->readlink)
202 touch_atime(&realpath);
204 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
208 static bool ovl_is_private_xattr(const char *name)
210 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
213 int ovl_setxattr(struct dentry *dentry, const char *name,
214 const void *value, size_t size, int flags)
217 struct dentry *upperdentry;
219 err = ovl_want_write(dentry);
224 if (ovl_is_private_xattr(name))
227 err = ovl_copy_up(dentry);
231 upperdentry = ovl_dentry_upper(dentry);
232 err = vfs_setxattr(upperdentry, name, value, size, flags);
235 ovl_drop_write(dentry);
240 static bool ovl_need_xattr_filter(struct dentry *dentry,
241 enum ovl_path_type type)
243 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
244 return S_ISDIR(dentry->d_inode->i_mode);
249 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
250 void *value, size_t size)
252 struct path realpath;
253 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
255 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
258 return vfs_getxattr(realpath.dentry, name, value, size);
261 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
263 struct path realpath;
264 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
268 res = vfs_listxattr(realpath.dentry, list, size);
269 if (res <= 0 || size == 0)
272 if (!ovl_need_xattr_filter(dentry, type))
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_need_xattr_filter(dentry, type) && 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 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
327 struct dentry *realdentry)
329 if (OVL_TYPE_UPPER(type))
332 if (special_file(realdentry->d_inode->i_mode))
335 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
341 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
344 struct path realpath;
345 enum ovl_path_type type;
347 if (d_is_dir(dentry))
348 return d_backing_inode(dentry);
350 type = ovl_path_real(dentry, &realpath);
351 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
352 err = ovl_want_write(dentry);
356 if (file_flags & O_TRUNC)
357 err = ovl_copy_up_truncate(dentry);
359 err = ovl_copy_up(dentry);
360 ovl_drop_write(dentry);
364 ovl_path_upper(dentry, &realpath);
367 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
368 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
370 return d_backing_inode(realpath.dentry);
373 static const struct inode_operations ovl_file_inode_operations = {
374 .setattr = ovl_setattr,
375 .permission = ovl_permission,
376 .getattr = ovl_getattr,
377 .setxattr = ovl_setxattr,
378 .getxattr = ovl_getxattr,
379 .listxattr = ovl_listxattr,
380 .removexattr = ovl_removexattr,
383 static const struct inode_operations ovl_symlink_inode_operations = {
384 .setattr = ovl_setattr,
385 .get_link = ovl_get_link,
386 .readlink = ovl_readlink,
387 .getattr = ovl_getattr,
388 .setxattr = ovl_setxattr,
389 .getxattr = ovl_getxattr,
390 .listxattr = ovl_listxattr,
391 .removexattr = ovl_removexattr,
394 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
395 struct ovl_entry *oe)
399 inode = new_inode(sb);
405 inode->i_ino = get_next_ino();
406 inode->i_mode = mode;
407 inode->i_flags |= S_NOATIME | S_NOCMTIME;
411 inode->i_private = oe;
412 inode->i_op = &ovl_dir_inode_operations;
413 inode->i_fop = &ovl_dir_operations;
417 inode->i_op = &ovl_symlink_inode_operations;
425 inode->i_op = &ovl_file_inode_operations;
429 WARN(1, "illegal file type: %i\n", mode);