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
);
68 inode_unlock(upperdentry
->d_inode
);
70 ovl_drop_write(dentry
);
75 static int ovl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
80 ovl_path_real(dentry
, &realpath
);
81 return vfs_getattr(&realpath
, stat
);
84 int ovl_permission(struct inode
*inode
, int mask
)
87 struct dentry
*alias
= NULL
;
88 struct inode
*realinode
;
89 struct dentry
*realdentry
;
93 if (S_ISDIR(inode
->i_mode
)) {
94 oe
= inode
->i_private
;
95 } else if (mask
& MAY_NOT_BLOCK
) {
99 * For non-directories find an alias and get the info
102 alias
= d_find_any_alias(inode
);
106 oe
= alias
->d_fsdata
;
109 realdentry
= ovl_entry_real(oe
, &is_upper
);
111 if (ovl_is_default_permissions(inode
)) {
113 struct path realpath
= { .dentry
= realdentry
};
115 if (mask
& MAY_NOT_BLOCK
)
118 realpath
.mnt
= ovl_entry_mnt_real(oe
, inode
, is_upper
);
120 err
= vfs_getattr(&realpath
, &stat
);
124 if ((stat
.mode
^ inode
->i_mode
) & S_IFMT
)
127 inode
->i_mode
= stat
.mode
;
128 inode
->i_uid
= stat
.uid
;
129 inode
->i_gid
= stat
.gid
;
131 return generic_permission(inode
, mask
);
134 /* Careful in RCU walk mode */
135 realinode
= ACCESS_ONCE(realdentry
->d_inode
);
137 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
142 if (mask
& MAY_WRITE
) {
143 umode_t mode
= realinode
->i_mode
;
146 * Writes will always be redirected to upper layer, so
147 * ignore lower layer being read-only.
149 * If the overlay itself is read-only then proceed
150 * with the permission check, don't return EROFS.
151 * This will only happen if this is the lower layer of
154 * If upper fs becomes read-only after the overlay was
155 * constructed return EROFS to prevent modification of
159 if (is_upper
&& !IS_RDONLY(inode
) && IS_RDONLY(realinode
) &&
160 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
164 err
= __inode_permission(realinode
, mask
);
170 static const char *ovl_get_link(struct dentry
*dentry
,
172 struct delayed_call
*done
)
174 struct dentry
*realdentry
;
175 struct inode
*realinode
;
178 return ERR_PTR(-ECHILD
);
180 realdentry
= ovl_dentry_real(dentry
);
181 realinode
= realdentry
->d_inode
;
183 if (WARN_ON(!realinode
->i_op
->get_link
))
184 return ERR_PTR(-EPERM
);
186 return realinode
->i_op
->get_link(realdentry
, realinode
, done
);
189 static int ovl_readlink(struct dentry
*dentry
, char __user
*buf
, int bufsiz
)
191 struct path realpath
;
192 struct inode
*realinode
;
194 ovl_path_real(dentry
, &realpath
);
195 realinode
= realpath
.dentry
->d_inode
;
197 if (!realinode
->i_op
->readlink
)
200 touch_atime(&realpath
);
202 return realinode
->i_op
->readlink(realpath
.dentry
, buf
, bufsiz
);
206 static bool ovl_is_private_xattr(const char *name
)
208 return strncmp(name
, OVL_XATTR_PRE_NAME
, OVL_XATTR_PRE_LEN
) == 0;
211 int ovl_setxattr(struct dentry
*dentry
, const char *name
,
212 const void *value
, size_t size
, int flags
)
215 struct dentry
*upperdentry
;
217 err
= ovl_want_write(dentry
);
222 if (ovl_is_private_xattr(name
))
225 err
= ovl_copy_up(dentry
);
229 upperdentry
= ovl_dentry_upper(dentry
);
230 err
= vfs_setxattr(upperdentry
, name
, value
, size
, flags
);
233 ovl_drop_write(dentry
);
238 static bool ovl_need_xattr_filter(struct dentry
*dentry
,
239 enum ovl_path_type type
)
241 if ((type
& (__OVL_PATH_PURE
| __OVL_PATH_UPPER
)) == __OVL_PATH_UPPER
)
242 return S_ISDIR(dentry
->d_inode
->i_mode
);
247 ssize_t
ovl_getxattr(struct dentry
*dentry
, const char *name
,
248 void *value
, size_t size
)
250 struct path realpath
;
251 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
253 if (ovl_need_xattr_filter(dentry
, type
) && ovl_is_private_xattr(name
))
256 return vfs_getxattr(realpath
.dentry
, name
, value
, size
);
259 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
261 struct path realpath
;
262 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
266 res
= vfs_listxattr(realpath
.dentry
, list
, size
);
267 if (res
<= 0 || size
== 0)
270 if (!ovl_need_xattr_filter(dentry
, type
))
273 /* filter out private xattrs */
274 for (off
= 0; off
< res
;) {
275 char *s
= list
+ off
;
276 size_t slen
= strlen(s
) + 1;
278 BUG_ON(off
+ slen
> res
);
280 if (ovl_is_private_xattr(s
)) {
282 memmove(s
, s
+ slen
, res
- off
);
291 int ovl_removexattr(struct dentry
*dentry
, const char *name
)
294 struct path realpath
;
295 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
297 err
= ovl_want_write(dentry
);
302 if (ovl_need_xattr_filter(dentry
, type
) && ovl_is_private_xattr(name
))
305 if (!OVL_TYPE_UPPER(type
)) {
306 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
310 err
= ovl_copy_up(dentry
);
314 ovl_path_upper(dentry
, &realpath
);
317 err
= vfs_removexattr(realpath
.dentry
, name
);
319 ovl_drop_write(dentry
);
324 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
325 struct dentry
*realdentry
)
327 if (OVL_TYPE_UPPER(type
))
330 if (special_file(realdentry
->d_inode
->i_mode
))
333 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
339 struct inode
*ovl_d_select_inode(struct dentry
*dentry
, unsigned file_flags
)
342 struct path realpath
;
343 enum ovl_path_type type
;
345 if (d_is_dir(dentry
))
346 return d_backing_inode(dentry
);
348 type
= ovl_path_real(dentry
, &realpath
);
349 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
350 err
= ovl_want_write(dentry
);
354 if (file_flags
& O_TRUNC
)
355 err
= ovl_copy_up_truncate(dentry
);
357 err
= ovl_copy_up(dentry
);
358 ovl_drop_write(dentry
);
362 ovl_path_upper(dentry
, &realpath
);
365 if (realpath
.dentry
->d_flags
& DCACHE_OP_SELECT_INODE
)
366 return realpath
.dentry
->d_op
->d_select_inode(realpath
.dentry
, file_flags
);
368 return d_backing_inode(realpath
.dentry
);
371 static const struct inode_operations ovl_file_inode_operations
= {
372 .setattr
= ovl_setattr
,
373 .permission
= ovl_permission
,
374 .getattr
= ovl_getattr
,
375 .setxattr
= ovl_setxattr
,
376 .getxattr
= ovl_getxattr
,
377 .listxattr
= ovl_listxattr
,
378 .removexattr
= ovl_removexattr
,
381 static const struct inode_operations ovl_symlink_inode_operations
= {
382 .setattr
= ovl_setattr
,
383 .get_link
= ovl_get_link
,
384 .readlink
= ovl_readlink
,
385 .getattr
= ovl_getattr
,
386 .setxattr
= ovl_setxattr
,
387 .getxattr
= ovl_getxattr
,
388 .listxattr
= ovl_listxattr
,
389 .removexattr
= ovl_removexattr
,
392 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
,
393 struct ovl_entry
*oe
)
397 inode
= new_inode(sb
);
403 inode
->i_ino
= get_next_ino();
404 inode
->i_mode
= mode
;
405 inode
->i_flags
|= S_NOATIME
| S_NOCMTIME
;
409 inode
->i_private
= oe
;
410 inode
->i_op
= &ovl_dir_inode_operations
;
411 inode
->i_fop
= &ovl_dir_operations
;
415 inode
->i_op
= &ovl_symlink_inode_operations
;
423 inode
->i_op
= &ovl_file_inode_operations
;
427 WARN(1, "illegal file type: %i\n", mode
);