ovl: use cached acl on underlying layer
[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 <linux/posix_acl.h>
14 #include "overlayfs.h"
15
16 static int ovl_copy_up_truncate(struct dentry *dentry)
17 {
18         int err;
19         struct dentry *parent;
20         struct kstat stat;
21         struct path lowerpath;
22
23         parent = dget_parent(dentry);
24         err = ovl_copy_up(parent);
25         if (err)
26                 goto out_dput_parent;
27
28         ovl_path_lower(dentry, &lowerpath);
29         err = vfs_getattr(&lowerpath, &stat);
30         if (err)
31                 goto out_dput_parent;
32
33         stat.size = 0;
34         err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
35
36 out_dput_parent:
37         dput(parent);
38         return err;
39 }
40
41 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
42 {
43         int err;
44         struct dentry *upperdentry;
45         const struct cred *old_cred;
46
47         /*
48          * Check for permissions before trying to copy-up.  This is redundant
49          * since it will be rechecked later by ->setattr() on upper dentry.  But
50          * without this, copy-up can be triggered by just about anybody.
51          *
52          * We don't initialize inode->size, which just means that
53          * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
54          * check for a swapfile (which this won't be anyway).
55          */
56         err = inode_change_ok(dentry->d_inode, attr);
57         if (err)
58                 return err;
59
60         err = ovl_want_write(dentry);
61         if (err)
62                 goto out;
63
64         if (attr->ia_valid & ATTR_SIZE) {
65                 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
66
67                 err = -ETXTBSY;
68                 if (atomic_read(&realinode->i_writecount) < 0)
69                         goto out_drop_write;
70         }
71
72         err = ovl_copy_up(dentry);
73         if (!err) {
74                 struct inode *winode = NULL;
75
76                 upperdentry = ovl_dentry_upper(dentry);
77
78                 if (attr->ia_valid & ATTR_SIZE) {
79                         winode = d_inode(upperdentry);
80                         err = get_write_access(winode);
81                         if (err)
82                                 goto out_drop_write;
83                 }
84
85                 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
86                         attr->ia_valid &= ~ATTR_MODE;
87
88                 inode_lock(upperdentry->d_inode);
89                 old_cred = ovl_override_creds(dentry->d_sb);
90                 err = notify_change(upperdentry, attr, NULL);
91                 revert_creds(old_cred);
92                 if (!err)
93                         ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
94                 inode_unlock(upperdentry->d_inode);
95
96                 if (winode)
97                         put_write_access(winode);
98         }
99 out_drop_write:
100         ovl_drop_write(dentry);
101 out:
102         return err;
103 }
104
105 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
106                          struct kstat *stat)
107 {
108         struct path realpath;
109         const struct cred *old_cred;
110         int err;
111
112         ovl_path_real(dentry, &realpath);
113         old_cred = ovl_override_creds(dentry->d_sb);
114         err = vfs_getattr(&realpath, stat);
115         revert_creds(old_cred);
116         return err;
117 }
118
119 int ovl_permission(struct inode *inode, int mask)
120 {
121         bool is_upper;
122         struct inode *realinode = ovl_inode_real(inode, &is_upper);
123         const struct cred *old_cred;
124         int err;
125
126         /* Careful in RCU walk mode */
127         if (!realinode) {
128                 WARN_ON(!(mask & MAY_NOT_BLOCK));
129                 return -ECHILD;
130         }
131
132         /*
133          * Check overlay inode with the creds of task and underlying inode
134          * with creds of mounter
135          */
136         err = generic_permission(inode, mask);
137         if (err)
138                 return err;
139
140         old_cred = ovl_override_creds(inode->i_sb);
141         if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
142                 mask &= ~(MAY_WRITE | MAY_APPEND);
143                 /* Make sure mounter can read file for copy up later */
144                 mask |= MAY_READ;
145         }
146         err = inode_permission(realinode, mask);
147         revert_creds(old_cred);
148
149         return err;
150 }
151
152 static const char *ovl_get_link(struct dentry *dentry,
153                                 struct inode *inode,
154                                 struct delayed_call *done)
155 {
156         struct dentry *realdentry;
157         struct inode *realinode;
158         const struct cred *old_cred;
159         const char *p;
160
161         if (!dentry)
162                 return ERR_PTR(-ECHILD);
163
164         realdentry = ovl_dentry_real(dentry);
165         realinode = realdentry->d_inode;
166
167         if (WARN_ON(!realinode->i_op->get_link))
168                 return ERR_PTR(-EPERM);
169
170         old_cred = ovl_override_creds(dentry->d_sb);
171         p = realinode->i_op->get_link(realdentry, realinode, done);
172         revert_creds(old_cred);
173         return p;
174 }
175
176 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
177 {
178         struct path realpath;
179         struct inode *realinode;
180         const struct cred *old_cred;
181         int err;
182
183         ovl_path_real(dentry, &realpath);
184         realinode = realpath.dentry->d_inode;
185
186         if (!realinode->i_op->readlink)
187                 return -EINVAL;
188
189         old_cred = ovl_override_creds(dentry->d_sb);
190         err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
191         revert_creds(old_cred);
192         return err;
193 }
194
195 bool ovl_is_private_xattr(const char *name)
196 {
197 #define OVL_XATTR_PRE_NAME OVL_XATTR_PREFIX "."
198         return strncmp(name, OVL_XATTR_PRE_NAME,
199                        sizeof(OVL_XATTR_PRE_NAME) - 1) == 0;
200 }
201
202 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
203                  const char *name, const void *value,
204                  size_t size, int flags)
205 {
206         int err;
207         struct dentry *upperdentry;
208         const struct cred *old_cred;
209
210         err = ovl_want_write(dentry);
211         if (err)
212                 goto out;
213
214         err = ovl_copy_up(dentry);
215         if (err)
216                 goto out_drop_write;
217
218         upperdentry = ovl_dentry_upper(dentry);
219         old_cred = ovl_override_creds(dentry->d_sb);
220         err = vfs_setxattr(upperdentry, name, value, size, flags);
221         revert_creds(old_cred);
222
223 out_drop_write:
224         ovl_drop_write(dentry);
225 out:
226         return err;
227 }
228
229 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
230                      const char *name, void *value, size_t size)
231 {
232         struct dentry *realdentry = ovl_dentry_real(dentry);
233         ssize_t res;
234         const struct cred *old_cred;
235
236         if (ovl_is_private_xattr(name))
237                 return -ENODATA;
238
239         old_cred = ovl_override_creds(dentry->d_sb);
240         res = vfs_getxattr(realdentry, name, value, size);
241         revert_creds(old_cred);
242         return res;
243 }
244
245 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
246 {
247         struct dentry *realdentry = ovl_dentry_real(dentry);
248         ssize_t res;
249         int off;
250         const struct cred *old_cred;
251
252         old_cred = ovl_override_creds(dentry->d_sb);
253         res = vfs_listxattr(realdentry, list, size);
254         revert_creds(old_cred);
255         if (res <= 0 || size == 0)
256                 return res;
257
258         /* filter out private xattrs */
259         for (off = 0; off < res;) {
260                 char *s = list + off;
261                 size_t slen = strlen(s) + 1;
262
263                 BUG_ON(off + slen > res);
264
265                 if (ovl_is_private_xattr(s)) {
266                         res -= slen;
267                         memmove(s, s + slen, res - off);
268                 } else {
269                         off += slen;
270                 }
271         }
272
273         return res;
274 }
275
276 int ovl_removexattr(struct dentry *dentry, const char *name)
277 {
278         int err;
279         struct path realpath;
280         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
281         const struct cred *old_cred;
282
283         err = ovl_want_write(dentry);
284         if (err)
285                 goto out;
286
287         err = -ENODATA;
288         if (ovl_is_private_xattr(name))
289                 goto out_drop_write;
290
291         if (!OVL_TYPE_UPPER(type)) {
292                 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
293                 if (err < 0)
294                         goto out_drop_write;
295
296                 err = ovl_copy_up(dentry);
297                 if (err)
298                         goto out_drop_write;
299
300                 ovl_path_upper(dentry, &realpath);
301         }
302
303         old_cred = ovl_override_creds(dentry->d_sb);
304         err = vfs_removexattr(realpath.dentry, name);
305         revert_creds(old_cred);
306 out_drop_write:
307         ovl_drop_write(dentry);
308 out:
309         return err;
310 }
311
312 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
313 {
314         struct inode *realinode = ovl_inode_real(inode, NULL);
315         const struct cred *old_cred;
316         struct posix_acl *acl;
317
318         if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
319                 return NULL;
320
321         if (!realinode->i_op->get_acl)
322                 return NULL;
323
324         old_cred = ovl_override_creds(inode->i_sb);
325         acl = get_acl(realinode, type);
326         revert_creds(old_cred);
327
328         return acl;
329 }
330
331 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
332                                   struct dentry *realdentry)
333 {
334         if (OVL_TYPE_UPPER(type))
335                 return false;
336
337         if (special_file(realdentry->d_inode->i_mode))
338                 return false;
339
340         if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
341                 return false;
342
343         return true;
344 }
345
346 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
347 {
348         int err = 0;
349         struct path realpath;
350         enum ovl_path_type type;
351
352         type = ovl_path_real(dentry, &realpath);
353         if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
354                 err = ovl_want_write(dentry);
355                 if (!err) {
356                         if (file_flags & O_TRUNC)
357                                 err = ovl_copy_up_truncate(dentry);
358                         else
359                                 err = ovl_copy_up(dentry);
360                         ovl_drop_write(dentry);
361                 }
362         }
363
364         return err;
365 }
366
367 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
368 {
369         struct dentry *alias;
370         struct path upperpath;
371
372         if (!(flags & S_ATIME))
373                 return 0;
374
375         alias = d_find_any_alias(inode);
376         if (!alias)
377                 return 0;
378
379         ovl_path_upper(alias, &upperpath);
380         if (upperpath.dentry) {
381                 touch_atime(&upperpath);
382                 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
383         }
384
385         dput(alias);
386
387         return 0;
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       = generic_setxattr,
395         .getxattr       = ovl_getxattr,
396         .listxattr      = ovl_listxattr,
397         .removexattr    = ovl_removexattr,
398         .get_acl        = ovl_get_acl,
399         .update_time    = ovl_update_time,
400 };
401
402 static const struct inode_operations ovl_symlink_inode_operations = {
403         .setattr        = ovl_setattr,
404         .get_link       = ovl_get_link,
405         .readlink       = ovl_readlink,
406         .getattr        = ovl_getattr,
407         .setxattr       = generic_setxattr,
408         .getxattr       = ovl_getxattr,
409         .listxattr      = ovl_listxattr,
410         .removexattr    = ovl_removexattr,
411         .update_time    = ovl_update_time,
412 };
413
414 static void ovl_fill_inode(struct inode *inode, umode_t mode)
415 {
416         inode->i_ino = get_next_ino();
417         inode->i_mode = mode;
418         inode->i_flags |= S_NOCMTIME;
419
420         mode &= S_IFMT;
421         switch (mode) {
422         case S_IFDIR:
423                 inode->i_op = &ovl_dir_inode_operations;
424                 inode->i_fop = &ovl_dir_operations;
425                 break;
426
427         case S_IFLNK:
428                 inode->i_op = &ovl_symlink_inode_operations;
429                 break;
430
431         default:
432                 WARN(1, "illegal file type: %i\n", mode);
433                 /* Fall through */
434
435         case S_IFREG:
436         case S_IFSOCK:
437         case S_IFBLK:
438         case S_IFCHR:
439         case S_IFIFO:
440                 inode->i_op = &ovl_file_inode_operations;
441                 break;
442         }
443 }
444
445 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode)
446 {
447         struct inode *inode;
448
449         inode = new_inode(sb);
450         if (inode)
451                 ovl_fill_inode(inode, mode);
452
453         return inode;
454 }
455
456 static int ovl_inode_test(struct inode *inode, void *data)
457 {
458         return ovl_inode_real(inode, NULL) == data;
459 }
460
461 static int ovl_inode_set(struct inode *inode, void *data)
462 {
463         inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
464         return 0;
465 }
466
467 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
468
469 {
470         struct inode *inode;
471
472         inode = iget5_locked(sb, (unsigned long) realinode,
473                              ovl_inode_test, ovl_inode_set, realinode);
474         if (inode && inode->i_state & I_NEW) {
475                 ovl_fill_inode(inode, realinode->i_mode);
476                 set_nlink(inode, realinode->i_nlink);
477                 unlock_new_inode(inode);
478         }
479
480         return inode;
481 }