f2fs: don't need to call set_page_dirty for io error
[linux-2.6/btrfs-unstable.git] / fs / overlayfs / inode.c
blob49e204560655a8f1737a2a03a38b7a88604a5a4c
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_truncate(struct dentry *dentry)
17 int err;
18 struct dentry *parent;
19 struct kstat stat;
20 struct path lowerpath;
22 parent = dget_parent(dentry);
23 err = ovl_copy_up(parent);
24 if (err)
25 goto out_dput_parent;
27 ovl_path_lower(dentry, &lowerpath);
28 err = vfs_getattr(&lowerpath, &stat);
29 if (err)
30 goto out_dput_parent;
32 stat.size = 0;
33 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
35 out_dput_parent:
36 dput(parent);
37 return err;
40 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
42 int err;
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);
55 if (err)
56 return err;
58 err = ovl_want_write(dentry);
59 if (err)
60 goto out;
62 err = ovl_copy_up(dentry);
63 if (!err) {
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);
71 out:
72 return err;
75 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
76 struct kstat *stat)
78 struct path realpath;
80 ovl_path_real(dentry, &realpath);
81 return vfs_getattr(&realpath, stat);
84 int ovl_permission(struct inode *inode, int mask)
86 struct ovl_entry *oe;
87 struct dentry *alias = NULL;
88 struct inode *realinode;
89 struct dentry *realdentry;
90 bool is_upper;
91 int err;
93 if (S_ISDIR(inode->i_mode)) {
94 oe = inode->i_private;
95 } else if (mask & MAY_NOT_BLOCK) {
96 return -ECHILD;
97 } else {
99 * For non-directories find an alias and get the info
100 * from there.
102 alias = d_find_any_alias(inode);
103 if (WARN_ON(!alias))
104 return -ENOENT;
106 oe = alias->d_fsdata;
109 realdentry = ovl_entry_real(oe, &is_upper);
111 if (ovl_is_default_permissions(inode)) {
112 struct kstat stat;
113 struct path realpath = { .dentry = realdentry };
115 if (mask & MAY_NOT_BLOCK)
116 return -ECHILD;
118 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
120 err = vfs_getattr(&realpath, &stat);
121 if (err)
122 return err;
124 if ((stat.mode ^ inode->i_mode) & S_IFMT)
125 return -ESTALE;
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);
136 if (!realinode) {
137 WARN_ON(!(mask & MAY_NOT_BLOCK));
138 err = -ENOENT;
139 goto out_dput;
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
152 * another overlayfs.
154 * If upper fs becomes read-only after the overlay was
155 * constructed return EROFS to prevent modification of
156 * upper layer.
158 err = -EROFS;
159 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
160 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
161 goto out_dput;
164 err = __inode_permission(realinode, mask);
165 out_dput:
166 dput(alias);
167 return err;
170 static const char *ovl_get_link(struct dentry *dentry,
171 struct inode *inode,
172 struct delayed_call *done)
174 struct dentry *realdentry;
175 struct inode *realinode;
177 if (!dentry)
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)
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 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
341 int err;
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);
351 if (err)
352 return ERR_PTR(err);
354 if (file_flags & O_TRUNC)
355 err = ovl_copy_up_truncate(dentry);
356 else
357 err = ovl_copy_up(dentry);
358 ovl_drop_write(dentry);
359 if (err)
360 return ERR_PTR(err);
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)
395 struct inode *inode;
397 inode = new_inode(sb);
398 if (!inode)
399 return NULL;
401 mode &= S_IFMT;
403 inode->i_ino = get_next_ino();
404 inode->i_mode = mode;
405 inode->i_flags |= S_NOATIME | S_NOCMTIME;
407 switch (mode) {
408 case S_IFDIR:
409 inode->i_private = oe;
410 inode->i_op = &ovl_dir_inode_operations;
411 inode->i_fop = &ovl_dir_operations;
412 break;
414 case S_IFLNK:
415 inode->i_op = &ovl_symlink_inode_operations;
416 break;
418 case S_IFREG:
419 case S_IFSOCK:
420 case S_IFBLK:
421 case S_IFCHR:
422 case S_IFIFO:
423 inode->i_op = &ovl_file_inode_operations;
424 break;
426 default:
427 WARN(1, "illegal file type: %i\n", mode);
428 iput(inode);
429 inode = NULL;
432 return inode;