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