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/cred.h>
13 #include <linux/xattr.h>
14 #include <linux/posix_acl.h>
15 #include "overlayfs.h"
17 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
20 struct dentry *upperdentry;
21 const struct cred *old_cred;
24 * Check for permissions before trying to copy-up. This is redundant
25 * since it will be rechecked later by ->setattr() on upper dentry. But
26 * without this, copy-up can be triggered by just about anybody.
28 * We don't initialize inode->size, which just means that
29 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
30 * check for a swapfile (which this won't be anyway).
32 err = setattr_prepare(dentry, attr);
36 err = ovl_want_write(dentry);
40 err = ovl_copy_up(dentry);
42 upperdentry = ovl_dentry_upper(dentry);
44 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
45 attr->ia_valid &= ~ATTR_MODE;
47 inode_lock(upperdentry->d_inode);
48 old_cred = ovl_override_creds(dentry->d_sb);
49 err = notify_change(upperdentry, attr, NULL);
50 revert_creds(old_cred);
52 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
53 inode_unlock(upperdentry->d_inode);
55 ovl_drop_write(dentry);
60 static int ovl_getattr(const struct path *path, struct kstat *stat,
61 u32 request_mask, unsigned int flags)
63 struct dentry *dentry = path->dentry;
64 enum ovl_path_type type;
66 const struct cred *old_cred;
69 type = ovl_path_real(dentry, &realpath);
70 old_cred = ovl_override_creds(dentry->d_sb);
71 err = vfs_getattr(&realpath, stat, request_mask, flags);
76 * When all layers are on the same fs, all real inode number are
77 * unique, so we use the overlay st_dev, which is friendly to du -x.
79 * We also use st_ino of the copy up origin, if we know it.
80 * This guaranties constant st_dev/st_ino across copy up.
82 * If filesystem supports NFS export ops, this also guaranties
83 * persistent st_ino across mount cycle.
85 if (ovl_same_sb(dentry->d_sb)) {
86 if (OVL_TYPE_ORIGIN(type)) {
87 struct kstat lowerstat;
89 ovl_path_lower(dentry, &realpath);
90 err = vfs_getattr(&realpath, &lowerstat,
91 STATX_INO | STATX_NLINK, flags);
95 WARN_ON_ONCE(stat->dev != lowerstat.dev);
97 * Lower hardlinks are broken on copy up to different
98 * upper files, so we cannot use the lower origin st_ino
99 * for those different files, even for the same fs case.
101 if (lowerstat.nlink == 1)
102 stat->ino = lowerstat.ino;
104 stat->dev = dentry->d_sb->s_dev;
107 revert_creds(old_cred);
112 int ovl_permission(struct inode *inode, int mask)
115 struct inode *realinode = ovl_inode_real(inode, &is_upper);
116 const struct cred *old_cred;
119 /* Careful in RCU walk mode */
121 WARN_ON(!(mask & MAY_NOT_BLOCK));
126 * Check overlay inode with the creds of task and underlying inode
127 * with creds of mounter
129 err = generic_permission(inode, mask);
133 old_cred = ovl_override_creds(inode->i_sb);
134 if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
135 mask &= ~(MAY_WRITE | MAY_APPEND);
136 /* Make sure mounter can read file for copy up later */
139 err = inode_permission(realinode, mask);
140 revert_creds(old_cred);
145 static const char *ovl_get_link(struct dentry *dentry,
147 struct delayed_call *done)
149 const struct cred *old_cred;
153 return ERR_PTR(-ECHILD);
155 old_cred = ovl_override_creds(dentry->d_sb);
156 p = vfs_get_link(ovl_dentry_real(dentry), done);
157 revert_creds(old_cred);
161 bool ovl_is_private_xattr(const char *name)
163 return strncmp(name, OVL_XATTR_PREFIX,
164 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
167 int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
168 size_t size, int flags)
171 struct path realpath;
172 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
173 const struct cred *old_cred;
175 err = ovl_want_write(dentry);
179 if (!value && !OVL_TYPE_UPPER(type)) {
180 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
185 err = ovl_copy_up(dentry);
189 if (!OVL_TYPE_UPPER(type))
190 ovl_path_upper(dentry, &realpath);
192 old_cred = ovl_override_creds(dentry->d_sb);
194 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
196 WARN_ON(flags != XATTR_REPLACE);
197 err = vfs_removexattr(realpath.dentry, name);
199 revert_creds(old_cred);
202 ovl_drop_write(dentry);
207 int ovl_xattr_get(struct dentry *dentry, const char *name,
208 void *value, size_t size)
210 struct dentry *realdentry = ovl_dentry_real(dentry);
212 const struct cred *old_cred;
214 old_cred = ovl_override_creds(dentry->d_sb);
215 res = vfs_getxattr(realdentry, name, value, size);
216 revert_creds(old_cred);
220 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
222 struct dentry *realdentry = ovl_dentry_real(dentry);
226 const struct cred *old_cred;
228 old_cred = ovl_override_creds(dentry->d_sb);
229 res = vfs_listxattr(realdentry, list, size);
230 revert_creds(old_cred);
231 if (res <= 0 || size == 0)
234 /* filter out private xattrs */
235 for (s = list, len = res; len;) {
236 size_t slen = strnlen(s, len) + 1;
238 /* underlying fs providing us with an broken xattr list? */
239 if (WARN_ON(slen > len))
243 if (ovl_is_private_xattr(s)) {
245 memmove(s, s + slen, len);
254 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
256 struct inode *realinode = ovl_inode_real(inode, NULL);
257 const struct cred *old_cred;
258 struct posix_acl *acl;
260 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
263 old_cred = ovl_override_creds(inode->i_sb);
264 acl = get_acl(realinode, type);
265 revert_creds(old_cred);
270 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
271 struct dentry *realdentry)
273 if (OVL_TYPE_UPPER(type))
276 if (special_file(realdentry->d_inode->i_mode))
279 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
285 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
288 struct path realpath;
289 enum ovl_path_type type;
291 type = ovl_path_real(dentry, &realpath);
292 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
293 err = ovl_want_write(dentry);
295 err = ovl_copy_up_flags(dentry, file_flags);
296 ovl_drop_write(dentry);
303 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
305 struct dentry *alias;
306 struct path upperpath;
308 if (!(flags & S_ATIME))
311 alias = d_find_any_alias(inode);
315 ovl_path_upper(alias, &upperpath);
316 if (upperpath.dentry) {
317 touch_atime(&upperpath);
318 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
326 static const struct inode_operations ovl_file_inode_operations = {
327 .setattr = ovl_setattr,
328 .permission = ovl_permission,
329 .getattr = ovl_getattr,
330 .listxattr = ovl_listxattr,
331 .get_acl = ovl_get_acl,
332 .update_time = ovl_update_time,
335 static const struct inode_operations ovl_symlink_inode_operations = {
336 .setattr = ovl_setattr,
337 .get_link = ovl_get_link,
338 .getattr = ovl_getattr,
339 .listxattr = ovl_listxattr,
340 .update_time = ovl_update_time,
344 * It is possible to stack overlayfs instance on top of another
345 * overlayfs instance as lower layer. We need to annonate the
346 * stackable i_mutex locks according to stack level of the super
347 * block instance. An overlayfs instance can never be in stack
348 * depth 0 (there is always a real fs below it). An overlayfs
349 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
351 * For example, here is a snip from /proc/lockdep_chains after
352 * dir_iterate of nested overlayfs:
354 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
355 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
356 * [...] &type->i_mutex_dir_key (stack_depth=0)
358 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
360 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
362 #ifdef CONFIG_LOCKDEP
363 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
364 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
366 int depth = inode->i_sb->s_stack_depth - 1;
368 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
371 if (S_ISDIR(inode->i_mode))
372 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
374 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
378 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
380 inode->i_ino = get_next_ino();
381 inode->i_mode = mode;
382 inode->i_flags |= S_NOCMTIME;
383 #ifdef CONFIG_FS_POSIX_ACL
384 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
387 ovl_lockdep_annotate_inode_mutex_key(inode);
389 switch (mode & S_IFMT) {
391 inode->i_op = &ovl_file_inode_operations;
395 inode->i_op = &ovl_dir_inode_operations;
396 inode->i_fop = &ovl_dir_operations;
400 inode->i_op = &ovl_symlink_inode_operations;
404 inode->i_op = &ovl_file_inode_operations;
405 init_special_inode(inode, mode, rdev);
410 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
414 inode = new_inode(sb);
416 ovl_fill_inode(inode, mode, rdev);
421 static int ovl_inode_test(struct inode *inode, void *data)
423 return ovl_inode_real(inode, NULL) == data;
426 static int ovl_inode_set(struct inode *inode, void *data)
428 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
432 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
437 inode = iget5_locked(sb, (unsigned long) realinode,
438 ovl_inode_test, ovl_inode_set, realinode);
439 if (inode && inode->i_state & I_NEW) {
440 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
441 set_nlink(inode, realinode->i_nlink);
442 unlock_new_inode(inode);