Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszer...
[cascardo/linux.git] / fs / overlayfs / inode.c
1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
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  */
9
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/xattr.h>
13 #include "overlayfs.h"
14
15 static int ovl_copy_up_truncate(struct dentry *dentry)
16 {
17         int err;
18         struct dentry *parent;
19         struct kstat stat;
20         struct path lowerpath;
21
22         parent = dget_parent(dentry);
23         err = ovl_copy_up(parent);
24         if (err)
25                 goto out_dput_parent;
26
27         ovl_path_lower(dentry, &lowerpath);
28         err = vfs_getattr(&lowerpath, &stat);
29         if (err)
30                 goto out_dput_parent;
31
32         stat.size = 0;
33         err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
34
35 out_dput_parent:
36         dput(parent);
37         return err;
38 }
39
40 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
41 {
42         int err;
43         struct dentry *upperdentry;
44
45         /*
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.
49          *
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).
53          */
54         err = inode_change_ok(dentry->d_inode, attr);
55         if (err)
56                 return err;
57
58         err = ovl_want_write(dentry);
59         if (err)
60                 goto out;
61
62         err = ovl_copy_up(dentry);
63         if (!err) {
64                 upperdentry = ovl_dentry_upper(dentry);
65
66                 mutex_lock(&upperdentry->d_inode->i_mutex);
67                 err = notify_change(upperdentry, attr, NULL);
68                 mutex_unlock(&upperdentry->d_inode->i_mutex);
69         }
70         ovl_drop_write(dentry);
71 out:
72         return err;
73 }
74
75 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
76                          struct kstat *stat)
77 {
78         struct path realpath;
79
80         ovl_path_real(dentry, &realpath);
81         return vfs_getattr(&realpath, stat);
82 }
83
84 int ovl_permission(struct inode *inode, int mask)
85 {
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;
92
93         if (S_ISDIR(inode->i_mode)) {
94                 oe = inode->i_private;
95         } else if (mask & MAY_NOT_BLOCK) {
96                 return -ECHILD;
97         } else {
98                 /*
99                  * For non-directories find an alias and get the info
100                  * from there.
101                  */
102                 alias = d_find_any_alias(inode);
103                 if (WARN_ON(!alias))
104                         return -ENOENT;
105
106                 oe = alias->d_fsdata;
107         }
108
109         realdentry = ovl_entry_real(oe, &is_upper);
110
111         if (ovl_is_default_permissions(inode)) {
112                 struct kstat stat;
113                 struct path realpath = { .dentry = realdentry };
114
115                 if (mask & MAY_NOT_BLOCK)
116                         return -ECHILD;
117
118                 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
119
120                 err = vfs_getattr(&realpath, &stat);
121                 if (err)
122                         return err;
123
124                 if ((stat.mode ^ inode->i_mode) & S_IFMT)
125                         return -ESTALE;
126
127                 inode->i_mode = stat.mode;
128                 inode->i_uid = stat.uid;
129                 inode->i_gid = stat.gid;
130
131                 return generic_permission(inode, mask);
132         }
133
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;
140         }
141
142         if (mask & MAY_WRITE) {
143                 umode_t mode = realinode->i_mode;
144
145                 /*
146                  * Writes will always be redirected to upper layer, so
147                  * ignore lower layer being read-only.
148                  *
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.
153                  *
154                  * If upper fs becomes read-only after the overlay was
155                  * constructed return EROFS to prevent modification of
156                  * upper layer.
157                  */
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;
162         }
163
164         err = __inode_permission(realinode, mask);
165 out_dput:
166         dput(alias);
167         return err;
168 }
169
170 static const char *ovl_get_link(struct dentry *dentry,
171                                 struct inode *inode,
172                                 struct delayed_call *done)
173 {
174         struct dentry *realdentry;
175         struct inode *realinode;
176
177         if (!dentry)
178                 return ERR_PTR(-ECHILD);
179
180         realdentry = ovl_dentry_real(dentry);
181         realinode = realdentry->d_inode;
182
183         if (WARN_ON(!realinode->i_op->get_link))
184                 return ERR_PTR(-EPERM);
185
186         return realinode->i_op->get_link(realdentry, realinode, done);
187 }
188
189 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
190 {
191         struct path realpath;
192         struct inode *realinode;
193
194         ovl_path_real(dentry, &realpath);
195         realinode = realpath.dentry->d_inode;
196
197         if (!realinode->i_op->readlink)
198                 return -EINVAL;
199
200         touch_atime(&realpath);
201
202         return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
203 }
204
205
206 static bool ovl_is_private_xattr(const char *name)
207 {
208         return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
209 }
210
211 int ovl_setxattr(struct dentry *dentry, const char *name,
212                  const void *value, size_t size, int flags)
213 {
214         int err;
215         struct dentry *upperdentry;
216
217         err = ovl_want_write(dentry);
218         if (err)
219                 goto out;
220
221         err = -EPERM;
222         if (ovl_is_private_xattr(name))
223                 goto out_drop_write;
224
225         err = ovl_copy_up(dentry);
226         if (err)
227                 goto out_drop_write;
228
229         upperdentry = ovl_dentry_upper(dentry);
230         err = vfs_setxattr(upperdentry, name, value, size, flags);
231
232 out_drop_write:
233         ovl_drop_write(dentry);
234 out:
235         return err;
236 }
237
238 static bool ovl_need_xattr_filter(struct dentry *dentry,
239                                   enum ovl_path_type type)
240 {
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;
245 }
246
247 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
248                      void *value, size_t size)
249 {
250         struct path realpath;
251         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
252
253         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
254                 return -ENODATA;
255
256         return vfs_getxattr(realpath.dentry, name, value, size);
257 }
258
259 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
260 {
261         struct path realpath;
262         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
263         ssize_t res;
264         int off;
265
266         res = vfs_listxattr(realpath.dentry, list, size);
267         if (res <= 0 || size == 0)
268                 return res;
269
270         if (!ovl_need_xattr_filter(dentry, type))
271                 return res;
272
273         /* filter out private xattrs */
274         for (off = 0; off < res;) {
275                 char *s = list + off;
276                 size_t slen = strlen(s) + 1;
277
278                 BUG_ON(off + slen > res);
279
280                 if (ovl_is_private_xattr(s)) {
281                         res -= slen;
282                         memmove(s, s + slen, res - off);
283                 } else {
284                         off += slen;
285                 }
286         }
287
288         return res;
289 }
290
291 int ovl_removexattr(struct dentry *dentry, const char *name)
292 {
293         int err;
294         struct path realpath;
295         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
296
297         err = ovl_want_write(dentry);
298         if (err)
299                 goto out;
300
301         err = -ENODATA;
302         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
303                 goto out_drop_write;
304
305         if (!OVL_TYPE_UPPER(type)) {
306                 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
307                 if (err < 0)
308                         goto out_drop_write;
309
310                 err = ovl_copy_up(dentry);
311                 if (err)
312                         goto out_drop_write;
313
314                 ovl_path_upper(dentry, &realpath);
315         }
316
317         err = vfs_removexattr(realpath.dentry, name);
318 out_drop_write:
319         ovl_drop_write(dentry);
320 out:
321         return err;
322 }
323
324 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
325                                   struct dentry *realdentry)
326 {
327         if (OVL_TYPE_UPPER(type))
328                 return false;
329
330         if (special_file(realdentry->d_inode->i_mode))
331                 return false;
332
333         if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
334                 return false;
335
336         return true;
337 }
338
339 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
340 {
341         int err;
342         struct path realpath;
343         enum ovl_path_type type;
344
345         if (d_is_dir(dentry))
346                 return d_backing_inode(dentry);
347
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);
353
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);
361
362                 ovl_path_upper(dentry, &realpath);
363         }
364
365         if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
366                 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
367
368         return d_backing_inode(realpath.dentry);
369 }
370
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,
379 };
380
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,
390 };
391
392 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
393                             struct ovl_entry *oe)
394 {
395         struct inode *inode;
396
397         inode = new_inode(sb);
398         if (!inode)
399                 return NULL;
400
401         mode &= S_IFMT;
402
403         inode->i_ino = get_next_ino();
404         inode->i_mode = mode;
405         inode->i_flags |= S_NOATIME | S_NOCMTIME;
406
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;
413
414         case S_IFLNK:
415                 inode->i_op = &ovl_symlink_inode_operations;
416                 break;
417
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;
425
426         default:
427                 WARN(1, "illegal file type: %i\n", mode);
428                 iput(inode);
429                 inode = NULL;
430         }
431
432         return inode;
433 }