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