net: Clone skb before setting peeked flag
[linux/fpc-iii.git] / fs / overlayfs / inode.c
blob04f1248846877d019625c861b474702177b38ae5
1 /*
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.
8 */
10 #include <linux/fs.h>
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,
16 bool no_data)
18 int err;
19 struct dentry *parent;
20 struct kstat stat;
21 struct path lowerpath;
23 parent = dget_parent(dentry);
24 err = ovl_copy_up(parent);
25 if (err)
26 goto out_dput_parent;
28 ovl_path_lower(dentry, &lowerpath);
29 err = vfs_getattr(&lowerpath, &stat);
30 if (err)
31 goto out_dput_parent;
33 if (no_data)
34 stat.size = 0;
36 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat, attr);
38 out_dput_parent:
39 dput(parent);
40 return err;
43 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
45 int err;
46 struct dentry *upperdentry;
48 err = ovl_want_write(dentry);
49 if (err)
50 goto out;
52 upperdentry = ovl_dentry_upper(dentry);
53 if (upperdentry) {
54 mutex_lock(&upperdentry->d_inode->i_mutex);
55 err = notify_change(upperdentry, attr, NULL);
56 mutex_unlock(&upperdentry->d_inode->i_mutex);
57 } else {
58 err = ovl_copy_up_last(dentry, attr, false);
60 ovl_drop_write(dentry);
61 out:
62 return err;
65 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
66 struct kstat *stat)
68 struct path realpath;
70 ovl_path_real(dentry, &realpath);
71 return vfs_getattr(&realpath, stat);
74 int ovl_permission(struct inode *inode, int mask)
76 struct ovl_entry *oe;
77 struct dentry *alias = NULL;
78 struct inode *realinode;
79 struct dentry *realdentry;
80 bool is_upper;
81 int err;
83 if (S_ISDIR(inode->i_mode)) {
84 oe = inode->i_private;
85 } else if (mask & MAY_NOT_BLOCK) {
86 return -ECHILD;
87 } else {
89 * For non-directories find an alias and get the info
90 * from there.
92 alias = d_find_any_alias(inode);
93 if (WARN_ON(!alias))
94 return -ENOENT;
96 oe = alias->d_fsdata;
99 realdentry = ovl_entry_real(oe, &is_upper);
101 /* Careful in RCU walk mode */
102 realinode = ACCESS_ONCE(realdentry->d_inode);
103 if (!realinode) {
104 WARN_ON(!(mask & MAY_NOT_BLOCK));
105 err = -ENOENT;
106 goto out_dput;
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
119 * another overlayfs.
121 * If upper fs becomes read-only after the overlay was
122 * constructed return EROFS to prevent modification of
123 * upper layer.
125 err = -EROFS;
126 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
127 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
128 goto out_dput;
131 err = __inode_permission(realinode, mask);
132 out_dput:
133 dput(alias);
134 return err;
138 struct ovl_link_data {
139 struct dentry *realdentry;
140 void *cookie;
143 static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
145 void *ret;
146 struct dentry *realdentry;
147 struct inode *realinode;
149 realdentry = ovl_dentry_real(dentry);
150 realinode = realdentry->d_inode;
152 if (WARN_ON(!realinode->i_op->follow_link))
153 return ERR_PTR(-EPERM);
155 ret = realinode->i_op->follow_link(realdentry, nd);
156 if (IS_ERR(ret))
157 return ret;
159 if (realinode->i_op->put_link) {
160 struct ovl_link_data *data;
162 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
163 if (!data) {
164 realinode->i_op->put_link(realdentry, nd, ret);
165 return ERR_PTR(-ENOMEM);
167 data->realdentry = realdentry;
168 data->cookie = ret;
170 return data;
171 } else {
172 return NULL;
176 static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
178 struct inode *realinode;
179 struct ovl_link_data *data = c;
181 if (!data)
182 return;
184 realinode = data->realdentry->d_inode;
185 realinode->i_op->put_link(data->realdentry, nd, data->cookie);
186 kfree(data);
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)
198 return -EINVAL;
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)
214 int err;
215 struct dentry *upperdentry;
217 err = ovl_want_write(dentry);
218 if (err)
219 goto out;
221 err = -EPERM;
222 if (ovl_is_private_xattr(name))
223 goto out_drop_write;
225 err = ovl_copy_up(dentry);
226 if (err)
227 goto out_drop_write;
229 upperdentry = ovl_dentry_upper(dentry);
230 err = vfs_setxattr(upperdentry, name, value, size, flags);
232 out_drop_write:
233 ovl_drop_write(dentry);
234 out:
235 return err;
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);
243 else
244 return false;
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))
254 return -ENODATA;
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);
263 ssize_t res;
264 int off;
266 res = vfs_listxattr(realpath.dentry, list, size);
267 if (res <= 0 || size == 0)
268 return res;
270 if (!ovl_need_xattr_filter(dentry, type))
271 return res;
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)) {
281 res -= slen;
282 memmove(s, s + slen, res - off);
283 } else {
284 off += slen;
288 return res;
291 int ovl_removexattr(struct dentry *dentry, const char *name)
293 int err;
294 struct path realpath;
295 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
297 err = ovl_want_write(dentry);
298 if (err)
299 goto out;
301 err = -ENODATA;
302 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
303 goto out_drop_write;
305 if (!OVL_TYPE_UPPER(type)) {
306 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
307 if (err < 0)
308 goto out_drop_write;
310 err = ovl_copy_up(dentry);
311 if (err)
312 goto out_drop_write;
314 ovl_path_upper(dentry, &realpath);
317 err = vfs_removexattr(realpath.dentry, name);
318 out_drop_write:
319 ovl_drop_write(dentry);
320 out:
321 return err;
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))
328 return false;
330 if (special_file(realdentry->d_inode->i_mode))
331 return false;
333 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
334 return false;
336 return true;
339 static int ovl_dentry_open(struct dentry *dentry, struct file *file,
340 const struct cred *cred)
342 int err;
343 struct path realpath;
344 enum ovl_path_type type;
345 bool want_write = false;
347 type = ovl_path_real(dentry, &realpath);
348 if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
349 want_write = true;
350 err = ovl_want_write(dentry);
351 if (err)
352 goto out;
354 if (file->f_flags & O_TRUNC)
355 err = ovl_copy_up_last(dentry, NULL, true);
356 else
357 err = ovl_copy_up(dentry);
358 if (err)
359 goto out_drop_write;
361 ovl_path_upper(dentry, &realpath);
364 err = vfs_open(&realpath, file, cred);
365 out_drop_write:
366 if (want_write)
367 ovl_drop_write(dentry);
368 out:
369 return err;
372 static const struct inode_operations ovl_file_inode_operations = {
373 .setattr = ovl_setattr,
374 .permission = ovl_permission,
375 .getattr = ovl_getattr,
376 .setxattr = ovl_setxattr,
377 .getxattr = ovl_getxattr,
378 .listxattr = ovl_listxattr,
379 .removexattr = ovl_removexattr,
380 .dentry_open = ovl_dentry_open,
383 static const struct inode_operations ovl_symlink_inode_operations = {
384 .setattr = ovl_setattr,
385 .follow_link = ovl_follow_link,
386 .put_link = ovl_put_link,
387 .readlink = ovl_readlink,
388 .getattr = ovl_getattr,
389 .setxattr = ovl_setxattr,
390 .getxattr = ovl_getxattr,
391 .listxattr = ovl_listxattr,
392 .removexattr = ovl_removexattr,
395 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
396 struct ovl_entry *oe)
398 struct inode *inode;
400 inode = new_inode(sb);
401 if (!inode)
402 return NULL;
404 mode &= S_IFMT;
406 inode->i_ino = get_next_ino();
407 inode->i_mode = mode;
408 inode->i_flags |= S_NOATIME | S_NOCMTIME;
410 switch (mode) {
411 case S_IFDIR:
412 inode->i_private = oe;
413 inode->i_op = &ovl_dir_inode_operations;
414 inode->i_fop = &ovl_dir_operations;
415 break;
417 case S_IFLNK:
418 inode->i_op = &ovl_symlink_inode_operations;
419 break;
421 case S_IFREG:
422 case S_IFSOCK:
423 case S_IFBLK:
424 case S_IFCHR:
425 case S_IFIFO:
426 inode->i_op = &ovl_file_inode_operations;
427 break;
429 default:
430 WARN(1, "illegal file type: %i\n", mode);
431 iput(inode);
432 inode = NULL;
435 return inode;