Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / 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                 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
67                         attr->ia_valid &= ~ATTR_MODE;
68
69                 mutex_lock(&upperdentry->d_inode->i_mutex);
70                 err = notify_change(upperdentry, attr, NULL);
71                 if (!err)
72                         ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
73                 mutex_unlock(&upperdentry->d_inode->i_mutex);
74         }
75         ovl_drop_write(dentry);
76 out:
77         return err;
78 }
79
80 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
81                          struct kstat *stat)
82 {
83         struct path realpath;
84
85         ovl_path_real(dentry, &realpath);
86         return vfs_getattr(&realpath, stat);
87 }
88
89 int ovl_permission(struct inode *inode, int mask)
90 {
91         struct ovl_entry *oe;
92         struct dentry *alias = NULL;
93         struct inode *realinode;
94         struct dentry *realdentry;
95         bool is_upper;
96         int err;
97
98         if (S_ISDIR(inode->i_mode)) {
99                 oe = inode->i_private;
100         } else if (mask & MAY_NOT_BLOCK) {
101                 return -ECHILD;
102         } else {
103                 /*
104                  * For non-directories find an alias and get the info
105                  * from there.
106                  */
107                 alias = d_find_any_alias(inode);
108                 if (WARN_ON(!alias))
109                         return -ENOENT;
110
111                 oe = alias->d_fsdata;
112         }
113
114         realdentry = ovl_entry_real(oe, &is_upper);
115
116         /* Careful in RCU walk mode */
117         realinode = ACCESS_ONCE(realdentry->d_inode);
118         if (!realinode) {
119                 WARN_ON(!(mask & MAY_NOT_BLOCK));
120                 err = -ENOENT;
121                 goto out_dput;
122         }
123
124         if (mask & MAY_WRITE) {
125                 umode_t mode = realinode->i_mode;
126
127                 /*
128                  * Writes will always be redirected to upper layer, so
129                  * ignore lower layer being read-only.
130                  *
131                  * If the overlay itself is read-only then proceed
132                  * with the permission check, don't return EROFS.
133                  * This will only happen if this is the lower layer of
134                  * another overlayfs.
135                  *
136                  * If upper fs becomes read-only after the overlay was
137                  * constructed return EROFS to prevent modification of
138                  * upper layer.
139                  */
140                 err = -EROFS;
141                 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
142                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
143                         goto out_dput;
144         }
145
146         err = __inode_permission(realinode, mask);
147 out_dput:
148         dput(alias);
149         return err;
150 }
151
152
153 struct ovl_link_data {
154         struct dentry *realdentry;
155         void *cookie;
156 };
157
158 static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
159 {
160         struct dentry *realdentry;
161         struct inode *realinode;
162         struct ovl_link_data *data = NULL;
163         const char *ret;
164
165         realdentry = ovl_dentry_real(dentry);
166         realinode = realdentry->d_inode;
167
168         if (WARN_ON(!realinode->i_op->follow_link))
169                 return ERR_PTR(-EPERM);
170
171         if (realinode->i_op->put_link) {
172                 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
173                 if (!data)
174                         return ERR_PTR(-ENOMEM);
175                 data->realdentry = realdentry;
176         }
177
178         ret = realinode->i_op->follow_link(realdentry, cookie);
179         if (IS_ERR_OR_NULL(ret)) {
180                 kfree(data);
181                 return ret;
182         }
183
184         if (data)
185                 data->cookie = *cookie;
186
187         *cookie = data;
188
189         return ret;
190 }
191
192 static void ovl_put_link(struct inode *unused, void *c)
193 {
194         struct inode *realinode;
195         struct ovl_link_data *data = c;
196
197         if (!data)
198                 return;
199
200         realinode = data->realdentry->d_inode;
201         realinode->i_op->put_link(realinode, data->cookie);
202         kfree(data);
203 }
204
205 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
206 {
207         struct path realpath;
208         struct inode *realinode;
209
210         ovl_path_real(dentry, &realpath);
211         realinode = realpath.dentry->d_inode;
212
213         if (!realinode->i_op->readlink)
214                 return -EINVAL;
215
216         touch_atime(&realpath);
217
218         return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
219 }
220
221
222 bool ovl_is_private_xattr(const char *name)
223 {
224         return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
225 }
226
227 int ovl_setxattr(struct dentry *dentry, const char *name,
228                  const void *value, size_t size, int flags)
229 {
230         int err;
231         struct dentry *upperdentry;
232
233         err = ovl_want_write(dentry);
234         if (err)
235                 goto out;
236
237         err = -EPERM;
238         if (ovl_is_private_xattr(name))
239                 goto out_drop_write;
240
241         err = ovl_copy_up(dentry);
242         if (err)
243                 goto out_drop_write;
244
245         upperdentry = ovl_dentry_upper(dentry);
246         err = vfs_setxattr(upperdentry, name, value, size, flags);
247
248 out_drop_write:
249         ovl_drop_write(dentry);
250 out:
251         return err;
252 }
253
254 static bool ovl_need_xattr_filter(struct dentry *dentry,
255                                   enum ovl_path_type type)
256 {
257         if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
258                 return S_ISDIR(dentry->d_inode->i_mode);
259         else
260                 return false;
261 }
262
263 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
264                      void *value, size_t size)
265 {
266         struct path realpath;
267         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
268
269         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
270                 return -ENODATA;
271
272         return vfs_getxattr(realpath.dentry, name, value, size);
273 }
274
275 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
276 {
277         struct path realpath;
278         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
279         ssize_t res;
280         size_t len;
281         char *s;
282
283         res = vfs_listxattr(realpath.dentry, list, size);
284         if (res <= 0 || size == 0)
285                 return res;
286
287         if (!ovl_need_xattr_filter(dentry, type))
288                 return res;
289
290         /* filter out private xattrs */
291         for (s = list, len = res; len;) {
292                 size_t slen = strnlen(s, len) + 1;
293
294                 /* underlying fs providing us with an broken xattr list? */
295                 if (WARN_ON(slen > len))
296                         return -EIO;
297
298                 len -= slen;
299                 if (ovl_is_private_xattr(s)) {
300                         res -= slen;
301                         memmove(s, s + slen, len);
302                 } else {
303                         s += slen;
304                 }
305         }
306
307         return res;
308 }
309
310 int ovl_removexattr(struct dentry *dentry, const char *name)
311 {
312         int err;
313         struct path realpath;
314         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
315
316         err = ovl_want_write(dentry);
317         if (err)
318                 goto out;
319
320         err = -ENODATA;
321         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
322                 goto out_drop_write;
323
324         if (!OVL_TYPE_UPPER(type)) {
325                 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
326                 if (err < 0)
327                         goto out_drop_write;
328
329                 err = ovl_copy_up(dentry);
330                 if (err)
331                         goto out_drop_write;
332
333                 ovl_path_upper(dentry, &realpath);
334         }
335
336         err = vfs_removexattr(realpath.dentry, name);
337 out_drop_write:
338         ovl_drop_write(dentry);
339 out:
340         return err;
341 }
342
343 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
344                                   struct dentry *realdentry)
345 {
346         if (OVL_TYPE_UPPER(type))
347                 return false;
348
349         if (special_file(realdentry->d_inode->i_mode))
350                 return false;
351
352         if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
353                 return false;
354
355         return true;
356 }
357
358 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
359 {
360         int err;
361         struct path realpath;
362         enum ovl_path_type type;
363
364         if (d_is_dir(dentry))
365                 return d_backing_inode(dentry);
366
367         type = ovl_path_real(dentry, &realpath);
368         if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
369                 err = ovl_want_write(dentry);
370                 if (err)
371                         return ERR_PTR(err);
372
373                 if (file_flags & O_TRUNC)
374                         err = ovl_copy_up_truncate(dentry);
375                 else
376                         err = ovl_copy_up(dentry);
377                 ovl_drop_write(dentry);
378                 if (err)
379                         return ERR_PTR(err);
380
381                 ovl_path_upper(dentry, &realpath);
382         }
383
384         if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
385                 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
386
387         return d_backing_inode(realpath.dentry);
388 }
389
390 static const struct inode_operations ovl_file_inode_operations = {
391         .setattr        = ovl_setattr,
392         .permission     = ovl_permission,
393         .getattr        = ovl_getattr,
394         .setxattr       = ovl_setxattr,
395         .getxattr       = ovl_getxattr,
396         .listxattr      = ovl_listxattr,
397         .removexattr    = ovl_removexattr,
398 };
399
400 static const struct inode_operations ovl_symlink_inode_operations = {
401         .setattr        = ovl_setattr,
402         .follow_link    = ovl_follow_link,
403         .put_link       = ovl_put_link,
404         .readlink       = ovl_readlink,
405         .getattr        = ovl_getattr,
406         .setxattr       = ovl_setxattr,
407         .getxattr       = ovl_getxattr,
408         .listxattr      = ovl_listxattr,
409         .removexattr    = ovl_removexattr,
410 };
411
412 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
413                             struct ovl_entry *oe)
414 {
415         struct inode *inode;
416
417         inode = new_inode(sb);
418         if (!inode)
419                 return NULL;
420
421         inode->i_ino = get_next_ino();
422         inode->i_mode = mode;
423         inode->i_flags |= S_NOATIME | S_NOCMTIME;
424
425         mode &= S_IFMT;
426         switch (mode) {
427         case S_IFDIR:
428                 inode->i_private = oe;
429                 inode->i_op = &ovl_dir_inode_operations;
430                 inode->i_fop = &ovl_dir_operations;
431                 break;
432
433         case S_IFLNK:
434                 inode->i_op = &ovl_symlink_inode_operations;
435                 break;
436
437         case S_IFREG:
438         case S_IFSOCK:
439         case S_IFBLK:
440         case S_IFCHR:
441         case S_IFIFO:
442                 inode->i_op = &ovl_file_inode_operations;
443                 break;
444
445         default:
446                 WARN(1, "illegal file type: %i\n", mode);
447                 iput(inode);
448                 inode = NULL;
449         }
450
451         return inode;
452 }