]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/overlayfs/inode.c
oom_reaper: avoid pointless atomic_inc_not_zero usage.
[mirror_ubuntu-bionic-kernel.git] / fs / overlayfs / inode.c
CommitLineData
e9be9d5e
MS
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
0f7ff2da 15static int ovl_copy_up_truncate(struct dentry *dentry)
e9be9d5e
MS
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
0f7ff2da
AV
32 stat.size = 0;
33 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
e9be9d5e
MS
34
35out_dput_parent:
36 dput(parent);
37 return err;
38}
39
40int ovl_setattr(struct dentry *dentry, struct iattr *attr)
41{
42 int err;
43 struct dentry *upperdentry;
44
cf9a6784
MS
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
e9be9d5e
MS
58 err = ovl_want_write(dentry);
59 if (err)
60 goto out;
61
acff81ec
MS
62 err = ovl_copy_up(dentry);
63 if (!err) {
64 upperdentry = ovl_dentry_upper(dentry);
65
5955102c 66 inode_lock(upperdentry->d_inode);
e9be9d5e 67 err = notify_change(upperdentry, attr, NULL);
b81de061
KK
68 if (!err)
69 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
5955102c 70 inode_unlock(upperdentry->d_inode);
e9be9d5e
MS
71 }
72 ovl_drop_write(dentry);
73out:
74 return err;
75}
76
77static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
78 struct kstat *stat)
79{
80 struct path realpath;
81
82 ovl_path_real(dentry, &realpath);
83 return vfs_getattr(&realpath, stat);
84}
85
86int ovl_permission(struct inode *inode, int mask)
87{
88 struct ovl_entry *oe;
89 struct dentry *alias = NULL;
90 struct inode *realinode;
91 struct dentry *realdentry;
92 bool is_upper;
93 int err;
94
95 if (S_ISDIR(inode->i_mode)) {
96 oe = inode->i_private;
97 } else if (mask & MAY_NOT_BLOCK) {
98 return -ECHILD;
99 } else {
100 /*
101 * For non-directories find an alias and get the info
102 * from there.
103 */
104 alias = d_find_any_alias(inode);
105 if (WARN_ON(!alias))
106 return -ENOENT;
107
108 oe = alias->d_fsdata;
109 }
110
111 realdentry = ovl_entry_real(oe, &is_upper);
112
8d3095f4
MS
113 if (ovl_is_default_permissions(inode)) {
114 struct kstat stat;
115 struct path realpath = { .dentry = realdentry };
116
117 if (mask & MAY_NOT_BLOCK)
118 return -ECHILD;
119
120 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
121
122 err = vfs_getattr(&realpath, &stat);
123 if (err)
124 return err;
125
126 if ((stat.mode ^ inode->i_mode) & S_IFMT)
127 return -ESTALE;
128
129 inode->i_mode = stat.mode;
130 inode->i_uid = stat.uid;
131 inode->i_gid = stat.gid;
132
133 return generic_permission(inode, mask);
134 }
135
e9be9d5e
MS
136 /* Careful in RCU walk mode */
137 realinode = ACCESS_ONCE(realdentry->d_inode);
138 if (!realinode) {
139 WARN_ON(!(mask & MAY_NOT_BLOCK));
140 err = -ENOENT;
141 goto out_dput;
142 }
143
144 if (mask & MAY_WRITE) {
145 umode_t mode = realinode->i_mode;
146
147 /*
148 * Writes will always be redirected to upper layer, so
149 * ignore lower layer being read-only.
150 *
151 * If the overlay itself is read-only then proceed
152 * with the permission check, don't return EROFS.
153 * This will only happen if this is the lower layer of
154 * another overlayfs.
155 *
156 * If upper fs becomes read-only after the overlay was
157 * constructed return EROFS to prevent modification of
158 * upper layer.
159 */
160 err = -EROFS;
161 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
162 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
163 goto out_dput;
164 }
165
166 err = __inode_permission(realinode, mask);
167out_dput:
168 dput(alias);
169 return err;
170}
171
6b255391 172static const char *ovl_get_link(struct dentry *dentry,
fceef393
AV
173 struct inode *inode,
174 struct delayed_call *done)
e9be9d5e 175{
e9be9d5e
MS
176 struct dentry *realdentry;
177 struct inode *realinode;
178
6b255391
AV
179 if (!dentry)
180 return ERR_PTR(-ECHILD);
181
e9be9d5e
MS
182 realdentry = ovl_dentry_real(dentry);
183 realinode = realdentry->d_inode;
184
6b255391 185 if (WARN_ON(!realinode->i_op->get_link))
e9be9d5e
MS
186 return ERR_PTR(-EPERM);
187
fceef393 188 return realinode->i_op->get_link(realdentry, realinode, done);
e9be9d5e
MS
189}
190
191static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
192{
193 struct path realpath;
194 struct inode *realinode;
195
196 ovl_path_real(dentry, &realpath);
197 realinode = realpath.dentry->d_inode;
198
199 if (!realinode->i_op->readlink)
200 return -EINVAL;
201
202 touch_atime(&realpath);
203
204 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
205}
206
207
208static bool ovl_is_private_xattr(const char *name)
209{
cead89bb 210 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
e9be9d5e
MS
211}
212
3767e255
AV
213int ovl_setxattr(struct dentry *dentry, struct inode *inode,
214 const char *name, const void *value,
215 size_t size, int flags)
e9be9d5e
MS
216{
217 int err;
218 struct dentry *upperdentry;
219
220 err = ovl_want_write(dentry);
221 if (err)
222 goto out;
223
224 err = -EPERM;
225 if (ovl_is_private_xattr(name))
226 goto out_drop_write;
227
228 err = ovl_copy_up(dentry);
229 if (err)
230 goto out_drop_write;
231
232 upperdentry = ovl_dentry_upper(dentry);
233 err = vfs_setxattr(upperdentry, name, value, size, flags);
234
235out_drop_write:
236 ovl_drop_write(dentry);
237out:
238 return err;
239}
240
ce23e640
AV
241ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
242 const char *name, void *value, size_t size)
e9be9d5e 243{
b581755b 244 struct dentry *realdentry = ovl_dentry_real(dentry);
52148463 245
b581755b 246 if (ovl_is_private_xattr(name))
e9be9d5e
MS
247 return -ENODATA;
248
b581755b 249 return vfs_getxattr(realdentry, name, value, size);
e9be9d5e
MS
250}
251
252ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
253{
b581755b 254 struct dentry *realdentry = ovl_dentry_real(dentry);
e9be9d5e
MS
255 ssize_t res;
256 int off;
257
b581755b 258 res = vfs_listxattr(realdentry, list, size);
e9be9d5e
MS
259 if (res <= 0 || size == 0)
260 return res;
261
e9be9d5e
MS
262 /* filter out private xattrs */
263 for (off = 0; off < res;) {
264 char *s = list + off;
265 size_t slen = strlen(s) + 1;
266
267 BUG_ON(off + slen > res);
268
269 if (ovl_is_private_xattr(s)) {
270 res -= slen;
271 memmove(s, s + slen, res - off);
272 } else {
273 off += slen;
274 }
275 }
276
277 return res;
278}
279
280int ovl_removexattr(struct dentry *dentry, const char *name)
281{
282 int err;
283 struct path realpath;
52148463 284 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
e9be9d5e
MS
285
286 err = ovl_want_write(dentry);
287 if (err)
288 goto out;
289
52148463 290 err = -ENODATA;
b581755b 291 if (ovl_is_private_xattr(name))
e9be9d5e
MS
292 goto out_drop_write;
293
1afaba1e 294 if (!OVL_TYPE_UPPER(type)) {
e9be9d5e
MS
295 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
296 if (err < 0)
297 goto out_drop_write;
298
299 err = ovl_copy_up(dentry);
300 if (err)
301 goto out_drop_write;
302
303 ovl_path_upper(dentry, &realpath);
304 }
305
306 err = vfs_removexattr(realpath.dentry, name);
307out_drop_write:
308 ovl_drop_write(dentry);
309out:
310 return err;
311}
312
313static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
314 struct dentry *realdentry)
315{
1afaba1e 316 if (OVL_TYPE_UPPER(type))
e9be9d5e
MS
317 return false;
318
319 if (special_file(realdentry->d_inode->i_mode))
320 return false;
321
322 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
323 return false;
324
325 return true;
326}
327
4bacc9c9 328struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
e9be9d5e
MS
329{
330 int err;
331 struct path realpath;
332 enum ovl_path_type type;
e9be9d5e 333
9391dd00
AV
334 if (d_is_dir(dentry))
335 return d_backing_inode(dentry);
336
e9be9d5e 337 type = ovl_path_real(dentry, &realpath);
4bacc9c9 338 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
e9be9d5e
MS
339 err = ovl_want_write(dentry);
340 if (err)
4bacc9c9 341 return ERR_PTR(err);
e9be9d5e 342
4bacc9c9 343 if (file_flags & O_TRUNC)
0f7ff2da 344 err = ovl_copy_up_truncate(dentry);
e9be9d5e
MS
345 else
346 err = ovl_copy_up(dentry);
f25801ee 347 ovl_drop_write(dentry);
e9be9d5e 348 if (err)
4bacc9c9 349 return ERR_PTR(err);
e9be9d5e
MS
350
351 ovl_path_upper(dentry, &realpath);
352 }
353
1c8a47df
MS
354 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
355 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
356
4bacc9c9 357 return d_backing_inode(realpath.dentry);
e9be9d5e
MS
358}
359
360static const struct inode_operations ovl_file_inode_operations = {
361 .setattr = ovl_setattr,
362 .permission = ovl_permission,
363 .getattr = ovl_getattr,
364 .setxattr = ovl_setxattr,
365 .getxattr = ovl_getxattr,
366 .listxattr = ovl_listxattr,
367 .removexattr = ovl_removexattr,
e9be9d5e
MS
368};
369
370static const struct inode_operations ovl_symlink_inode_operations = {
371 .setattr = ovl_setattr,
6b255391 372 .get_link = ovl_get_link,
e9be9d5e
MS
373 .readlink = ovl_readlink,
374 .getattr = ovl_getattr,
375 .setxattr = ovl_setxattr,
376 .getxattr = ovl_getxattr,
377 .listxattr = ovl_listxattr,
378 .removexattr = ovl_removexattr,
379};
380
381struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
382 struct ovl_entry *oe)
383{
384 struct inode *inode;
385
386 inode = new_inode(sb);
387 if (!inode)
388 return NULL;
389
390 mode &= S_IFMT;
391
392 inode->i_ino = get_next_ino();
393 inode->i_mode = mode;
394 inode->i_flags |= S_NOATIME | S_NOCMTIME;
395
396 switch (mode) {
397 case S_IFDIR:
398 inode->i_private = oe;
399 inode->i_op = &ovl_dir_inode_operations;
400 inode->i_fop = &ovl_dir_operations;
401 break;
402
403 case S_IFLNK:
404 inode->i_op = &ovl_symlink_inode_operations;
405 break;
406
407 case S_IFREG:
408 case S_IFSOCK:
409 case S_IFBLK:
410 case S_IFCHR:
411 case S_IFIFO:
412 inode->i_op = &ovl_file_inode_operations;
413 break;
414
415 default:
416 WARN(1, "illegal file type: %i\n", mode);
417 iput(inode);
418 inode = NULL;
419 }
420
421 return inode;
e9be9d5e 422}