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 if (attr
->ia_valid
& ATTR_SIZE
) {
63 struct inode
*realinode
= d_inode(ovl_dentry_real(dentry
));
66 if (atomic_read(&realinode
->i_writecount
) < 0)
70 err
= ovl_copy_up(dentry
);
72 struct inode
*winode
= NULL
;
74 upperdentry
= ovl_dentry_upper(dentry
);
76 if (attr
->ia_valid
& ATTR_SIZE
) {
77 winode
= d_inode(upperdentry
);
78 err
= get_write_access(winode
);
83 inode_lock(upperdentry
->d_inode
);
84 err
= notify_change(upperdentry
, attr
, NULL
);
86 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
87 inode_unlock(upperdentry
->d_inode
);
90 put_write_access(winode
);
93 ovl_drop_write(dentry
);
98 static int ovl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
101 struct path realpath
;
103 ovl_path_real(dentry
, &realpath
);
104 return vfs_getattr(&realpath
, stat
);
107 int ovl_permission(struct inode
*inode
, int mask
)
109 struct ovl_entry
*oe
;
110 struct dentry
*alias
= NULL
;
111 struct inode
*realinode
;
112 struct dentry
*realdentry
;
116 if (S_ISDIR(inode
->i_mode
)) {
117 oe
= inode
->i_private
;
118 } else if (mask
& MAY_NOT_BLOCK
) {
122 * For non-directories find an alias and get the info
125 alias
= d_find_any_alias(inode
);
129 oe
= alias
->d_fsdata
;
132 realdentry
= ovl_entry_real(oe
, &is_upper
);
134 if (ovl_is_default_permissions(inode
)) {
136 struct path realpath
= { .dentry
= realdentry
};
138 if (mask
& MAY_NOT_BLOCK
)
141 realpath
.mnt
= ovl_entry_mnt_real(oe
, inode
, is_upper
);
143 err
= vfs_getattr(&realpath
, &stat
);
148 if ((stat
.mode
^ inode
->i_mode
) & S_IFMT
)
151 inode
->i_mode
= stat
.mode
;
152 inode
->i_uid
= stat
.uid
;
153 inode
->i_gid
= stat
.gid
;
155 err
= generic_permission(inode
, mask
);
159 /* Careful in RCU walk mode */
160 realinode
= ACCESS_ONCE(realdentry
->d_inode
);
162 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
167 if (mask
& MAY_WRITE
) {
168 umode_t mode
= realinode
->i_mode
;
171 * Writes will always be redirected to upper layer, so
172 * ignore lower layer being read-only.
174 * If the overlay itself is read-only then proceed
175 * with the permission check, don't return EROFS.
176 * This will only happen if this is the lower layer of
179 * If upper fs becomes read-only after the overlay was
180 * constructed return EROFS to prevent modification of
184 if (is_upper
&& !IS_RDONLY(inode
) && IS_RDONLY(realinode
) &&
185 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
189 err
= __inode_permission(realinode
, mask
);
195 static const char *ovl_get_link(struct dentry
*dentry
,
197 struct delayed_call
*done
)
199 struct dentry
*realdentry
;
200 struct inode
*realinode
;
203 return ERR_PTR(-ECHILD
);
205 realdentry
= ovl_dentry_real(dentry
);
206 realinode
= realdentry
->d_inode
;
208 if (WARN_ON(!realinode
->i_op
->get_link
))
209 return ERR_PTR(-EPERM
);
211 return realinode
->i_op
->get_link(realdentry
, realinode
, done
);
214 static int ovl_readlink(struct dentry
*dentry
, char __user
*buf
, int bufsiz
)
216 struct path realpath
;
217 struct inode
*realinode
;
219 ovl_path_real(dentry
, &realpath
);
220 realinode
= realpath
.dentry
->d_inode
;
222 if (!realinode
->i_op
->readlink
)
225 touch_atime(&realpath
);
227 return realinode
->i_op
->readlink(realpath
.dentry
, buf
, bufsiz
);
231 static bool ovl_is_private_xattr(const char *name
)
233 return strncmp(name
, OVL_XATTR_PRE_NAME
, OVL_XATTR_PRE_LEN
) == 0;
236 int ovl_setxattr(struct dentry
*dentry
, struct inode
*inode
,
237 const char *name
, const void *value
,
238 size_t size
, int flags
)
241 struct dentry
*upperdentry
;
243 err
= ovl_want_write(dentry
);
248 if (ovl_is_private_xattr(name
))
251 err
= ovl_copy_up(dentry
);
255 upperdentry
= ovl_dentry_upper(dentry
);
256 err
= vfs_setxattr(upperdentry
, name
, value
, size
, flags
);
259 ovl_drop_write(dentry
);
264 ssize_t
ovl_getxattr(struct dentry
*dentry
, struct inode
*inode
,
265 const char *name
, void *value
, size_t size
)
267 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
269 if (ovl_is_private_xattr(name
))
272 return vfs_getxattr(realdentry
, name
, value
, size
);
275 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
277 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
281 res
= vfs_listxattr(realdentry
, list
, size
);
282 if (res
<= 0 || size
== 0)
285 /* filter out private xattrs */
286 for (off
= 0; off
< res
;) {
287 char *s
= list
+ off
;
288 size_t slen
= strlen(s
) + 1;
290 BUG_ON(off
+ slen
> res
);
292 if (ovl_is_private_xattr(s
)) {
294 memmove(s
, s
+ slen
, res
- off
);
303 int ovl_removexattr(struct dentry
*dentry
, const char *name
)
306 struct path realpath
;
307 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
309 err
= ovl_want_write(dentry
);
314 if (ovl_is_private_xattr(name
))
317 if (!OVL_TYPE_UPPER(type
)) {
318 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
322 err
= ovl_copy_up(dentry
);
326 ovl_path_upper(dentry
, &realpath
);
329 err
= vfs_removexattr(realpath
.dentry
, name
);
331 ovl_drop_write(dentry
);
336 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
337 struct dentry
*realdentry
)
339 if (OVL_TYPE_UPPER(type
))
342 if (special_file(realdentry
->d_inode
->i_mode
))
345 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
351 struct inode
*ovl_d_select_inode(struct dentry
*dentry
, unsigned file_flags
)
354 struct path realpath
;
355 enum ovl_path_type type
;
357 if (d_is_dir(dentry
))
358 return d_backing_inode(dentry
);
360 type
= ovl_path_real(dentry
, &realpath
);
361 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
362 err
= ovl_want_write(dentry
);
366 if (file_flags
& O_TRUNC
)
367 err
= ovl_copy_up_truncate(dentry
);
369 err
= ovl_copy_up(dentry
);
370 ovl_drop_write(dentry
);
374 ovl_path_upper(dentry
, &realpath
);
377 if (realpath
.dentry
->d_flags
& DCACHE_OP_SELECT_INODE
)
378 return realpath
.dentry
->d_op
->d_select_inode(realpath
.dentry
, file_flags
);
380 return d_backing_inode(realpath
.dentry
);
383 static const struct inode_operations ovl_file_inode_operations
= {
384 .setattr
= ovl_setattr
,
385 .permission
= ovl_permission
,
386 .getattr
= ovl_getattr
,
387 .setxattr
= ovl_setxattr
,
388 .getxattr
= ovl_getxattr
,
389 .listxattr
= ovl_listxattr
,
390 .removexattr
= ovl_removexattr
,
393 static const struct inode_operations ovl_symlink_inode_operations
= {
394 .setattr
= ovl_setattr
,
395 .get_link
= ovl_get_link
,
396 .readlink
= ovl_readlink
,
397 .getattr
= ovl_getattr
,
398 .setxattr
= ovl_setxattr
,
399 .getxattr
= ovl_getxattr
,
400 .listxattr
= ovl_listxattr
,
401 .removexattr
= ovl_removexattr
,
404 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
,
405 struct ovl_entry
*oe
)
409 inode
= new_inode(sb
);
415 inode
->i_ino
= get_next_ino();
416 inode
->i_mode
= mode
;
417 inode
->i_flags
|= S_NOATIME
| S_NOCMTIME
;
421 inode
->i_private
= oe
;
422 inode
->i_op
= &ovl_dir_inode_operations
;
423 inode
->i_fop
= &ovl_dir_operations
;
427 inode
->i_op
= &ovl_symlink_inode_operations
;
435 inode
->i_op
= &ovl_file_inode_operations
;
439 WARN(1, "illegal file type: %i\n", mode
);