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