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
)
121 struct inode
*realinode
= ovl_inode_real(inode
, &is_upper
);
122 const struct cred
*old_cred
;
125 /* Careful in RCU walk mode */
127 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
132 * Check overlay inode with the creds of task and underlying inode
133 * with creds of mounter
135 err
= generic_permission(inode
, mask
);
139 old_cred
= ovl_override_creds(inode
->i_sb
);
141 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
142 err
= inode_permission(realinode
, mask
);
143 revert_creds(old_cred
);
148 static const char *ovl_get_link(struct dentry
*dentry
,
150 struct delayed_call
*done
)
152 struct dentry
*realdentry
;
153 struct inode
*realinode
;
154 const struct cred
*old_cred
;
158 return ERR_PTR(-ECHILD
);
160 realdentry
= ovl_dentry_real(dentry
);
161 realinode
= realdentry
->d_inode
;
163 if (WARN_ON(!realinode
->i_op
->get_link
))
164 return ERR_PTR(-EPERM
);
166 old_cred
= ovl_override_creds(dentry
->d_sb
);
167 p
= realinode
->i_op
->get_link(realdentry
, realinode
, done
);
168 revert_creds(old_cred
);
172 static int ovl_readlink(struct dentry
*dentry
, char __user
*buf
, int bufsiz
)
174 struct path realpath
;
175 struct inode
*realinode
;
176 const struct cred
*old_cred
;
179 ovl_path_real(dentry
, &realpath
);
180 realinode
= realpath
.dentry
->d_inode
;
182 if (!realinode
->i_op
->readlink
)
185 old_cred
= ovl_override_creds(dentry
->d_sb
);
186 err
= realinode
->i_op
->readlink(realpath
.dentry
, buf
, bufsiz
);
187 revert_creds(old_cred
);
191 static bool ovl_is_private_xattr(const char *name
)
193 return strncmp(name
, OVL_XATTR_PRE_NAME
, OVL_XATTR_PRE_LEN
) == 0;
196 int ovl_setxattr(struct dentry
*dentry
, struct inode
*inode
,
197 const char *name
, const void *value
,
198 size_t size
, int flags
)
201 struct dentry
*upperdentry
;
202 const struct cred
*old_cred
;
204 err
= ovl_want_write(dentry
);
209 if (ovl_is_private_xattr(name
))
212 err
= ovl_copy_up(dentry
);
216 upperdentry
= ovl_dentry_upper(dentry
);
217 old_cred
= ovl_override_creds(dentry
->d_sb
);
218 err
= vfs_setxattr(upperdentry
, name
, value
, size
, flags
);
219 revert_creds(old_cred
);
222 ovl_drop_write(dentry
);
227 ssize_t
ovl_getxattr(struct dentry
*dentry
, struct inode
*inode
,
228 const char *name
, void *value
, size_t size
)
230 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
232 const struct cred
*old_cred
;
234 if (ovl_is_private_xattr(name
))
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
);
248 const struct cred
*old_cred
;
250 old_cred
= ovl_override_creds(dentry
->d_sb
);
251 res
= vfs_listxattr(realdentry
, list
, size
);
252 revert_creds(old_cred
);
253 if (res
<= 0 || size
== 0)
256 /* filter out private xattrs */
257 for (off
= 0; off
< res
;) {
258 char *s
= list
+ off
;
259 size_t slen
= strlen(s
) + 1;
261 BUG_ON(off
+ slen
> res
);
263 if (ovl_is_private_xattr(s
)) {
265 memmove(s
, s
+ slen
, res
- off
);
274 int ovl_removexattr(struct dentry
*dentry
, const char *name
)
277 struct path realpath
;
278 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
279 const struct cred
*old_cred
;
281 err
= ovl_want_write(dentry
);
286 if (ovl_is_private_xattr(name
))
289 if (!OVL_TYPE_UPPER(type
)) {
290 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
294 err
= ovl_copy_up(dentry
);
298 ovl_path_upper(dentry
, &realpath
);
301 old_cred
= ovl_override_creds(dentry
->d_sb
);
302 err
= vfs_removexattr(realpath
.dentry
, name
);
303 revert_creds(old_cred
);
305 ovl_drop_write(dentry
);
310 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
312 struct inode
*realinode
= ovl_inode_real(inode
, NULL
);
313 const struct cred
*old_cred
;
314 struct posix_acl
*acl
;
316 if (!IS_POSIXACL(realinode
))
319 if (!realinode
->i_op
->get_acl
)
322 old_cred
= ovl_override_creds(inode
->i_sb
);
323 acl
= realinode
->i_op
->get_acl(realinode
, type
);
324 revert_creds(old_cred
);
329 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
330 struct dentry
*realdentry
)
332 if (OVL_TYPE_UPPER(type
))
335 if (special_file(realdentry
->d_inode
->i_mode
))
338 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
344 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
347 struct path realpath
;
348 enum ovl_path_type type
;
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
);
354 if (file_flags
& O_TRUNC
)
355 err
= ovl_copy_up_truncate(dentry
);
357 err
= ovl_copy_up(dentry
);
358 ovl_drop_write(dentry
);
365 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
367 struct dentry
*alias
;
368 struct path upperpath
;
370 if (!(flags
& S_ATIME
))
373 alias
= d_find_any_alias(inode
);
377 ovl_path_upper(alias
, &upperpath
);
378 if (upperpath
.dentry
) {
379 touch_atime(&upperpath
);
380 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
388 static const struct inode_operations ovl_file_inode_operations
= {
389 .setattr
= ovl_setattr
,
390 .permission
= ovl_permission
,
391 .getattr
= ovl_getattr
,
392 .setxattr
= ovl_setxattr
,
393 .getxattr
= ovl_getxattr
,
394 .listxattr
= ovl_listxattr
,
395 .removexattr
= ovl_removexattr
,
396 .get_acl
= ovl_get_acl
,
397 .update_time
= ovl_update_time
,
400 static const struct inode_operations ovl_symlink_inode_operations
= {
401 .setattr
= ovl_setattr
,
402 .get_link
= ovl_get_link
,
403 .readlink
= ovl_readlink
,
404 .getattr
= ovl_getattr
,
405 .setxattr
= ovl_setxattr
,
406 .getxattr
= ovl_getxattr
,
407 .listxattr
= ovl_listxattr
,
408 .removexattr
= ovl_removexattr
,
409 .update_time
= ovl_update_time
,
412 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
)
416 inode
= new_inode(sb
);
420 inode
->i_ino
= get_next_ino();
421 inode
->i_mode
= mode
;
422 inode
->i_flags
|= S_NOCMTIME
;
427 inode
->i_op
= &ovl_dir_inode_operations
;
428 inode
->i_fop
= &ovl_dir_operations
;
432 inode
->i_op
= &ovl_symlink_inode_operations
;
440 inode
->i_op
= &ovl_file_inode_operations
;
444 WARN(1, "illegal file type: %i\n", mode
);