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 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;
67 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
70 type = ovl_path_real(dentry, &realpath);
71 old_cred = ovl_override_creds(dentry->d_sb);
72 err = vfs_getattr(&realpath, stat, request_mask, flags);
77 * When all layers are on the same fs, all real inode number are
78 * unique, so we use the overlay st_dev, which is friendly to du -x.
80 * We also use st_ino of the copy up origin, if we know it.
81 * This guaranties constant st_dev/st_ino across copy up.
83 * If filesystem supports NFS export ops, this also guaranties
84 * persistent st_ino across mount cycle.
86 if (ovl_same_sb(dentry->d_sb)) {
87 if (OVL_TYPE_ORIGIN(type)) {
88 struct kstat lowerstat;
89 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
91 ovl_path_lower(dentry, &realpath);
92 err = vfs_getattr(&realpath, &lowerstat,
97 WARN_ON_ONCE(stat->dev != lowerstat.dev);
99 * Lower hardlinks are broken on copy up to different
100 * upper files, so we cannot use the lower origin st_ino
101 * for those different files, even for the same fs case.
103 if (is_dir || lowerstat.nlink == 1)
104 stat->ino = lowerstat.ino;
106 stat->dev = dentry->d_sb->s_dev;
109 * If not all layers are on the same fs the pair {real st_ino;
110 * overlay st_dev} is not unique, so use the non persistent
113 * Always use the overlay st_dev for directories, so 'find
114 * -xdev' will scan the entire overlay mount and won't cross the
115 * overlay mount boundaries.
117 stat->dev = dentry->d_sb->s_dev;
118 stat->ino = dentry->d_inode->i_ino;
122 * It's probably not worth it to count subdirs to get the
123 * correct link count. nlink=1 seems to pacify 'find' and
126 if (is_dir && OVL_TYPE_MERGE(type))
130 revert_creds(old_cred);
135 int ovl_permission(struct inode *inode, int mask)
138 struct inode *realinode = ovl_inode_real(inode, &is_upper);
139 const struct cred *old_cred;
142 /* Careful in RCU walk mode */
144 WARN_ON(!(mask & MAY_NOT_BLOCK));
149 * Check overlay inode with the creds of task and underlying inode
150 * with creds of mounter
152 err = generic_permission(inode, mask);
156 old_cred = ovl_override_creds(inode->i_sb);
157 if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
158 mask &= ~(MAY_WRITE | MAY_APPEND);
159 /* Make sure mounter can read file for copy up later */
162 err = inode_permission(realinode, mask);
163 revert_creds(old_cred);
168 static const char *ovl_get_link(struct dentry *dentry,
170 struct delayed_call *done)
172 const struct cred *old_cred;
176 return ERR_PTR(-ECHILD);
178 old_cred = ovl_override_creds(dentry->d_sb);
179 p = vfs_get_link(ovl_dentry_real(dentry), done);
180 revert_creds(old_cred);
184 bool ovl_is_private_xattr(const char *name)
186 return strncmp(name, OVL_XATTR_PREFIX,
187 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
190 int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
191 size_t size, int flags)
194 struct path realpath;
195 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
196 const struct cred *old_cred;
198 err = ovl_want_write(dentry);
202 if (!value && !OVL_TYPE_UPPER(type)) {
203 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
208 err = ovl_copy_up(dentry);
212 if (!OVL_TYPE_UPPER(type))
213 ovl_path_upper(dentry, &realpath);
215 old_cred = ovl_override_creds(dentry->d_sb);
217 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
219 WARN_ON(flags != XATTR_REPLACE);
220 err = vfs_removexattr(realpath.dentry, name);
222 revert_creds(old_cred);
225 ovl_drop_write(dentry);
230 int ovl_xattr_get(struct dentry *dentry, const char *name,
231 void *value, size_t size)
233 struct dentry *realdentry = ovl_dentry_real(dentry);
235 const struct cred *old_cred;
237 old_cred = ovl_override_creds(dentry->d_sb);
238 res = vfs_getxattr(realdentry, name, value, size);
239 revert_creds(old_cred);
243 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
245 struct dentry *realdentry = ovl_dentry_real(dentry);
249 const struct cred *old_cred;
251 old_cred = ovl_override_creds(dentry->d_sb);
252 res = vfs_listxattr(realdentry, list, size);
253 revert_creds(old_cred);
254 if (res <= 0 || size == 0)
257 /* filter out private xattrs */
258 for (s = list, len = res; len;) {
259 size_t slen = strnlen(s, len) + 1;
261 /* underlying fs providing us with an broken xattr list? */
262 if (WARN_ON(slen > len))
266 if (ovl_is_private_xattr(s)) {
268 memmove(s, s + slen, len);
277 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
279 struct inode *realinode = ovl_inode_real(inode, NULL);
280 const struct cred *old_cred;
281 struct posix_acl *acl;
283 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
286 old_cred = ovl_override_creds(inode->i_sb);
287 acl = get_acl(realinode, type);
288 revert_creds(old_cred);
293 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
294 struct dentry *realdentry)
296 if (OVL_TYPE_UPPER(type))
299 if (special_file(realdentry->d_inode->i_mode))
302 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
308 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
311 struct path realpath;
312 enum ovl_path_type type;
314 type = ovl_path_real(dentry, &realpath);
315 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
316 err = ovl_want_write(dentry);
318 err = ovl_copy_up_flags(dentry, file_flags);
319 ovl_drop_write(dentry);
326 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
328 struct dentry *alias;
329 struct path upperpath;
331 if (!(flags & S_ATIME))
334 alias = d_find_any_alias(inode);
338 ovl_path_upper(alias, &upperpath);
339 if (upperpath.dentry) {
340 touch_atime(&upperpath);
341 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
349 static const struct inode_operations ovl_file_inode_operations = {
350 .setattr = ovl_setattr,
351 .permission = ovl_permission,
352 .getattr = ovl_getattr,
353 .listxattr = ovl_listxattr,
354 .get_acl = ovl_get_acl,
355 .update_time = ovl_update_time,
358 static const struct inode_operations ovl_symlink_inode_operations = {
359 .setattr = ovl_setattr,
360 .get_link = ovl_get_link,
361 .getattr = ovl_getattr,
362 .listxattr = ovl_listxattr,
363 .update_time = ovl_update_time,
367 * It is possible to stack overlayfs instance on top of another
368 * overlayfs instance as lower layer. We need to annonate the
369 * stackable i_mutex locks according to stack level of the super
370 * block instance. An overlayfs instance can never be in stack
371 * depth 0 (there is always a real fs below it). An overlayfs
372 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
374 * For example, here is a snip from /proc/lockdep_chains after
375 * dir_iterate of nested overlayfs:
377 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
378 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
379 * [...] &type->i_mutex_dir_key (stack_depth=0)
381 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
383 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
385 #ifdef CONFIG_LOCKDEP
386 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
387 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
389 int depth = inode->i_sb->s_stack_depth - 1;
391 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
394 if (S_ISDIR(inode->i_mode))
395 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
397 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
401 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
403 inode->i_ino = get_next_ino();
404 inode->i_mode = mode;
405 inode->i_flags |= S_NOCMTIME;
406 #ifdef CONFIG_FS_POSIX_ACL
407 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
410 ovl_lockdep_annotate_inode_mutex_key(inode);
412 switch (mode & S_IFMT) {
414 inode->i_op = &ovl_file_inode_operations;
418 inode->i_op = &ovl_dir_inode_operations;
419 inode->i_fop = &ovl_dir_operations;
423 inode->i_op = &ovl_symlink_inode_operations;
427 inode->i_op = &ovl_file_inode_operations;
428 init_special_inode(inode, mode, rdev);
433 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
437 inode = new_inode(sb);
439 ovl_fill_inode(inode, mode, rdev);
444 static int ovl_inode_test(struct inode *inode, void *data)
446 return ovl_inode_real(inode, NULL) == data;
449 static int ovl_inode_set(struct inode *inode, void *data)
451 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
455 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
460 inode = iget5_locked(sb, (unsigned long) realinode,
461 ovl_inode_test, ovl_inode_set, realinode);
462 if (inode && inode->i_state & I_NEW) {
463 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
464 set_nlink(inode, realinode->i_nlink);
465 unlock_new_inode(inode);