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_last(struct dentry
*dentry
, struct iattr
*attr
,
19 struct dentry
*parent
;
21 struct path lowerpath
;
23 parent
= dget_parent(dentry
);
24 err
= ovl_copy_up(parent
);
28 ovl_path_lower(dentry
, &lowerpath
);
29 err
= vfs_getattr(&lowerpath
, &stat
);
36 err
= ovl_copy_up_one(parent
, dentry
, &lowerpath
, &stat
, attr
);
43 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
46 struct dentry
*upperdentry
;
48 err
= ovl_want_write(dentry
);
52 upperdentry
= ovl_dentry_upper(dentry
);
54 mutex_lock(&upperdentry
->d_inode
->i_mutex
);
55 err
= notify_change(upperdentry
, attr
, NULL
);
56 mutex_unlock(&upperdentry
->d_inode
->i_mutex
);
58 err
= ovl_copy_up_last(dentry
, attr
, false);
60 ovl_drop_write(dentry
);
65 static int ovl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
70 ovl_path_real(dentry
, &realpath
);
71 return vfs_getattr(&realpath
, stat
);
74 int ovl_permission(struct inode
*inode
, int mask
)
77 struct dentry
*alias
= NULL
;
78 struct inode
*realinode
;
79 struct dentry
*realdentry
;
83 if (S_ISDIR(inode
->i_mode
)) {
84 oe
= inode
->i_private
;
85 } else if (mask
& MAY_NOT_BLOCK
) {
89 * For non-directories find an alias and get the info
92 alias
= d_find_any_alias(inode
);
99 realdentry
= ovl_entry_real(oe
, &is_upper
);
101 /* Careful in RCU walk mode */
102 realinode
= ACCESS_ONCE(realdentry
->d_inode
);
104 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
109 if (mask
& MAY_WRITE
) {
110 umode_t mode
= realinode
->i_mode
;
113 * Writes will always be redirected to upper layer, so
114 * ignore lower layer being read-only.
116 * If the overlay itself is read-only then proceed
117 * with the permission check, don't return EROFS.
118 * This will only happen if this is the lower layer of
121 * If upper fs becomes read-only after the overlay was
122 * constructed return EROFS to prevent modification of
126 if (is_upper
&& !IS_RDONLY(inode
) && IS_RDONLY(realinode
) &&
127 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
131 err
= __inode_permission(realinode
, mask
);
138 struct ovl_link_data
{
139 struct dentry
*realdentry
;
143 static const char *ovl_follow_link(struct dentry
*dentry
, void **cookie
)
145 struct dentry
*realdentry
;
146 struct inode
*realinode
;
147 struct ovl_link_data
*data
= NULL
;
150 realdentry
= ovl_dentry_real(dentry
);
151 realinode
= realdentry
->d_inode
;
153 if (WARN_ON(!realinode
->i_op
->follow_link
))
154 return ERR_PTR(-EPERM
);
156 if (realinode
->i_op
->put_link
) {
157 data
= kmalloc(sizeof(struct ovl_link_data
), GFP_KERNEL
);
159 return ERR_PTR(-ENOMEM
);
160 data
->realdentry
= realdentry
;
163 ret
= realinode
->i_op
->follow_link(realdentry
, cookie
);
164 if (IS_ERR_OR_NULL(ret
)) {
170 data
->cookie
= *cookie
;
177 static void ovl_put_link(struct inode
*unused
, void *c
)
179 struct inode
*realinode
;
180 struct ovl_link_data
*data
= c
;
185 realinode
= data
->realdentry
->d_inode
;
186 realinode
->i_op
->put_link(realinode
, data
->cookie
);
190 static int ovl_readlink(struct dentry
*dentry
, char __user
*buf
, int bufsiz
)
192 struct path realpath
;
193 struct inode
*realinode
;
195 ovl_path_real(dentry
, &realpath
);
196 realinode
= realpath
.dentry
->d_inode
;
198 if (!realinode
->i_op
->readlink
)
201 touch_atime(&realpath
);
203 return realinode
->i_op
->readlink(realpath
.dentry
, buf
, bufsiz
);
207 static bool ovl_is_private_xattr(const char *name
)
209 return strncmp(name
, OVL_XATTR_PRE_NAME
, OVL_XATTR_PRE_LEN
) == 0;
212 int ovl_setxattr(struct dentry
*dentry
, const char *name
,
213 const void *value
, size_t size
, int flags
)
216 struct dentry
*upperdentry
;
218 err
= ovl_want_write(dentry
);
223 if (ovl_is_private_xattr(name
))
226 err
= ovl_copy_up(dentry
);
230 upperdentry
= ovl_dentry_upper(dentry
);
231 err
= vfs_setxattr(upperdentry
, name
, value
, size
, flags
);
234 ovl_drop_write(dentry
);
239 static bool ovl_need_xattr_filter(struct dentry
*dentry
,
240 enum ovl_path_type type
)
242 if ((type
& (__OVL_PATH_PURE
| __OVL_PATH_UPPER
)) == __OVL_PATH_UPPER
)
243 return S_ISDIR(dentry
->d_inode
->i_mode
);
248 ssize_t
ovl_getxattr(struct dentry
*dentry
, const char *name
,
249 void *value
, size_t size
)
251 struct path realpath
;
252 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
254 if (ovl_need_xattr_filter(dentry
, type
) && ovl_is_private_xattr(name
))
257 return vfs_getxattr(realpath
.dentry
, name
, value
, size
);
260 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
262 struct path realpath
;
263 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
267 res
= vfs_listxattr(realpath
.dentry
, list
, size
);
268 if (res
<= 0 || size
== 0)
271 if (!ovl_need_xattr_filter(dentry
, type
))
274 /* filter out private xattrs */
275 for (off
= 0; off
< res
;) {
276 char *s
= list
+ off
;
277 size_t slen
= strlen(s
) + 1;
279 BUG_ON(off
+ slen
> res
);
281 if (ovl_is_private_xattr(s
)) {
283 memmove(s
, s
+ slen
, res
- off
);
292 int ovl_removexattr(struct dentry
*dentry
, const char *name
)
295 struct path realpath
;
296 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
298 err
= ovl_want_write(dentry
);
303 if (ovl_need_xattr_filter(dentry
, type
) && ovl_is_private_xattr(name
))
306 if (!OVL_TYPE_UPPER(type
)) {
307 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
311 err
= ovl_copy_up(dentry
);
315 ovl_path_upper(dentry
, &realpath
);
318 err
= vfs_removexattr(realpath
.dentry
, name
);
320 ovl_drop_write(dentry
);
325 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
326 struct dentry
*realdentry
)
328 if (OVL_TYPE_UPPER(type
))
331 if (special_file(realdentry
->d_inode
->i_mode
))
334 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
340 struct inode
*ovl_d_select_inode(struct dentry
*dentry
, unsigned file_flags
)
343 struct path realpath
;
344 enum ovl_path_type type
;
346 if (d_is_dir(dentry
))
347 return d_backing_inode(dentry
);
349 type
= ovl_path_real(dentry
, &realpath
);
350 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
351 err
= ovl_want_write(dentry
);
355 if (file_flags
& O_TRUNC
)
356 err
= ovl_copy_up_last(dentry
, NULL
, true);
358 err
= ovl_copy_up(dentry
);
359 ovl_drop_write(dentry
);
363 ovl_path_upper(dentry
, &realpath
);
366 return d_backing_inode(realpath
.dentry
);
369 static const struct inode_operations ovl_file_inode_operations
= {
370 .setattr
= ovl_setattr
,
371 .permission
= ovl_permission
,
372 .getattr
= ovl_getattr
,
373 .setxattr
= ovl_setxattr
,
374 .getxattr
= ovl_getxattr
,
375 .listxattr
= ovl_listxattr
,
376 .removexattr
= ovl_removexattr
,
379 static const struct inode_operations ovl_symlink_inode_operations
= {
380 .setattr
= ovl_setattr
,
381 .follow_link
= ovl_follow_link
,
382 .put_link
= ovl_put_link
,
383 .readlink
= ovl_readlink
,
384 .getattr
= ovl_getattr
,
385 .setxattr
= ovl_setxattr
,
386 .getxattr
= ovl_getxattr
,
387 .listxattr
= ovl_listxattr
,
388 .removexattr
= ovl_removexattr
,
391 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
,
392 struct ovl_entry
*oe
)
396 inode
= new_inode(sb
);
402 inode
->i_ino
= get_next_ino();
403 inode
->i_mode
= mode
;
404 inode
->i_flags
|= S_NOATIME
| S_NOCMTIME
;
408 inode
->i_private
= oe
;
409 inode
->i_op
= &ovl_dir_inode_operations
;
410 inode
->i_fop
= &ovl_dir_operations
;
414 inode
->i_op
= &ovl_symlink_inode_operations
;
422 inode
->i_op
= &ovl_file_inode_operations
;
426 WARN(1, "illegal file type: %i\n", mode
);