]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/overlayfs/copy_up.c
ovl: use vfs_get_link()
[mirror_ubuntu-zesty-kernel.git] / fs / overlayfs / copy_up.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/module.h>
11 #include <linux/fs.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/splice.h>
15 #include <linux/xattr.h>
16 #include <linux/security.h>
17 #include <linux/uaccess.h>
18 #include <linux/sched.h>
19 #include <linux/namei.h>
20 #include <linux/fdtable.h>
21 #include <linux/ratelimit.h>
22 #include "overlayfs.h"
23
24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25
26 static bool __read_mostly ovl_check_copy_up;
27 module_param_named(check_copy_up, ovl_check_copy_up, bool,
28 S_IWUSR | S_IRUGO);
29 MODULE_PARM_DESC(ovl_check_copy_up,
30 "Warn on copy-up when causing process also has a R/O fd open");
31
32 static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
33 {
34 const struct dentry *dentry = data;
35
36 if (f->f_inode == d_inode(dentry))
37 pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
38 f, fd, current->pid, current->comm);
39 return 0;
40 }
41
42 /*
43 * Check the fds open by this process and warn if something like the following
44 * scenario is about to occur:
45 *
46 * fd1 = open("foo", O_RDONLY);
47 * fd2 = open("foo", O_RDWR);
48 */
49 static void ovl_do_check_copy_up(struct dentry *dentry)
50 {
51 if (ovl_check_copy_up)
52 iterate_fd(current->files, 0, ovl_check_fd, dentry);
53 }
54
55 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
56 {
57 ssize_t list_size, size, value_size = 0;
58 char *buf, *name, *value = NULL;
59 int uninitialized_var(error);
60 size_t slen;
61
62 if (!old->d_inode->i_op->getxattr ||
63 !new->d_inode->i_op->getxattr)
64 return 0;
65
66 list_size = vfs_listxattr(old, NULL, 0);
67 if (list_size <= 0) {
68 if (list_size == -EOPNOTSUPP)
69 return 0;
70 return list_size;
71 }
72
73 buf = kzalloc(list_size, GFP_KERNEL);
74 if (!buf)
75 return -ENOMEM;
76
77 list_size = vfs_listxattr(old, buf, list_size);
78 if (list_size <= 0) {
79 error = list_size;
80 goto out;
81 }
82
83 for (name = buf; list_size; name += slen) {
84 slen = strnlen(name, list_size) + 1;
85
86 /* underlying fs providing us with an broken xattr list? */
87 if (WARN_ON(slen > list_size)) {
88 error = -EIO;
89 break;
90 }
91 list_size -= slen;
92
93 if (ovl_is_private_xattr(name))
94 continue;
95 retry:
96 size = vfs_getxattr(old, name, value, value_size);
97 if (size == -ERANGE)
98 size = vfs_getxattr(old, name, NULL, 0);
99
100 if (size < 0) {
101 error = size;
102 break;
103 }
104
105 if (size > value_size) {
106 void *new;
107
108 new = krealloc(value, size, GFP_KERNEL);
109 if (!new) {
110 error = -ENOMEM;
111 break;
112 }
113 value = new;
114 value_size = size;
115 goto retry;
116 }
117
118 error = vfs_setxattr(new, name, value, size, 0);
119 if (error)
120 break;
121 }
122 kfree(value);
123 out:
124 kfree(buf);
125 return error;
126 }
127
128 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
129 {
130 struct file *old_file;
131 struct file *new_file;
132 loff_t old_pos = 0;
133 loff_t new_pos = 0;
134 int error = 0;
135
136 if (len == 0)
137 return 0;
138
139 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
140 if (IS_ERR(old_file))
141 return PTR_ERR(old_file);
142
143 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
144 if (IS_ERR(new_file)) {
145 error = PTR_ERR(new_file);
146 goto out_fput;
147 }
148
149 /* FIXME: copy up sparse files efficiently */
150 while (len) {
151 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
152 long bytes;
153
154 if (len < this_len)
155 this_len = len;
156
157 if (signal_pending_state(TASK_KILLABLE, current)) {
158 error = -EINTR;
159 break;
160 }
161
162 bytes = do_splice_direct(old_file, &old_pos,
163 new_file, &new_pos,
164 this_len, SPLICE_F_MOVE);
165 if (bytes <= 0) {
166 error = bytes;
167 break;
168 }
169 WARN_ON(old_pos != new_pos);
170
171 len -= bytes;
172 }
173
174 fput(new_file);
175 out_fput:
176 fput(old_file);
177 return error;
178 }
179
180 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
181 {
182 struct iattr attr = {
183 .ia_valid =
184 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
185 .ia_atime = stat->atime,
186 .ia_mtime = stat->mtime,
187 };
188
189 return notify_change(upperdentry, &attr, NULL);
190 }
191
192 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
193 {
194 int err = 0;
195
196 if (!S_ISLNK(stat->mode)) {
197 struct iattr attr = {
198 .ia_valid = ATTR_MODE,
199 .ia_mode = stat->mode,
200 };
201 err = notify_change(upperdentry, &attr, NULL);
202 }
203 if (!err) {
204 struct iattr attr = {
205 .ia_valid = ATTR_UID | ATTR_GID,
206 .ia_uid = stat->uid,
207 .ia_gid = stat->gid,
208 };
209 err = notify_change(upperdentry, &attr, NULL);
210 }
211 if (!err)
212 ovl_set_timestamps(upperdentry, stat);
213
214 return err;
215 }
216
217 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
218 struct dentry *dentry, struct path *lowerpath,
219 struct kstat *stat, const char *link)
220 {
221 struct inode *wdir = workdir->d_inode;
222 struct inode *udir = upperdir->d_inode;
223 struct dentry *newdentry = NULL;
224 struct dentry *upper = NULL;
225 umode_t mode = stat->mode;
226 int err;
227
228 newdentry = ovl_lookup_temp(workdir, dentry);
229 err = PTR_ERR(newdentry);
230 if (IS_ERR(newdentry))
231 goto out;
232
233 upper = lookup_one_len(dentry->d_name.name, upperdir,
234 dentry->d_name.len);
235 err = PTR_ERR(upper);
236 if (IS_ERR(upper))
237 goto out1;
238
239 /* Can't properly set mode on creation because of the umask */
240 stat->mode &= S_IFMT;
241 err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
242 stat->mode = mode;
243 if (err)
244 goto out2;
245
246 if (S_ISREG(stat->mode)) {
247 struct path upperpath;
248
249 ovl_path_upper(dentry, &upperpath);
250 BUG_ON(upperpath.dentry != NULL);
251 upperpath.dentry = newdentry;
252
253 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
254 if (err)
255 goto out_cleanup;
256 }
257
258 err = ovl_copy_xattr(lowerpath->dentry, newdentry);
259 if (err)
260 goto out_cleanup;
261
262 inode_lock(newdentry->d_inode);
263 err = ovl_set_attr(newdentry, stat);
264 inode_unlock(newdentry->d_inode);
265 if (err)
266 goto out_cleanup;
267
268 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
269 if (err)
270 goto out_cleanup;
271
272 ovl_dentry_update(dentry, newdentry);
273 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
274 newdentry = NULL;
275
276 /*
277 * Non-directores become opaque when copied up.
278 */
279 if (!S_ISDIR(stat->mode))
280 ovl_dentry_set_opaque(dentry, true);
281 out2:
282 dput(upper);
283 out1:
284 dput(newdentry);
285 out:
286 return err;
287
288 out_cleanup:
289 ovl_cleanup(wdir, newdentry);
290 goto out2;
291 }
292
293 /*
294 * Copy up a single dentry
295 *
296 * Directory renames only allowed on "pure upper" (already created on
297 * upper filesystem, never copied up). Directories which are on lower or
298 * are merged may not be renamed. For these -EXDEV is returned and
299 * userspace has to deal with it. This means, when copying up a
300 * directory we can rely on it and ancestors being stable.
301 *
302 * Non-directory renames start with copy up of source if necessary. The
303 * actual rename will only proceed once the copy up was successful. Copy
304 * up uses upper parent i_mutex for exclusion. Since rename can change
305 * d_parent it is possible that the copy up will lock the old parent. At
306 * that point the file will have already been copied up anyway.
307 */
308 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
309 struct path *lowerpath, struct kstat *stat)
310 {
311 DEFINE_DELAYED_CALL(done);
312 struct dentry *workdir = ovl_workdir(dentry);
313 int err;
314 struct kstat pstat;
315 struct path parentpath;
316 struct dentry *lowerdentry = lowerpath->dentry;
317 struct dentry *upperdir;
318 struct dentry *upperdentry;
319 const char *link = NULL;
320
321 if (WARN_ON(!workdir))
322 return -EROFS;
323
324 ovl_do_check_copy_up(lowerdentry);
325
326 ovl_path_upper(parent, &parentpath);
327 upperdir = parentpath.dentry;
328
329 err = vfs_getattr(&parentpath, &pstat);
330 if (err)
331 return err;
332
333 if (S_ISLNK(stat->mode)) {
334 link = vfs_get_link(lowerdentry, &done);
335 if (IS_ERR(link))
336 return PTR_ERR(link);
337 }
338
339 err = -EIO;
340 if (lock_rename(workdir, upperdir) != NULL) {
341 pr_err("overlayfs: failed to lock workdir+upperdir\n");
342 goto out_unlock;
343 }
344 upperdentry = ovl_dentry_upper(dentry);
345 if (upperdentry) {
346 /* Raced with another copy-up? Nothing to do, then... */
347 err = 0;
348 goto out_unlock;
349 }
350
351 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
352 stat, link);
353 if (!err) {
354 /* Restore timestamps on parent (best effort) */
355 ovl_set_timestamps(upperdir, &pstat);
356 }
357 out_unlock:
358 unlock_rename(workdir, upperdir);
359 do_delayed_call(&done);
360
361 return err;
362 }
363
364 int ovl_copy_up(struct dentry *dentry)
365 {
366 int err = 0;
367 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
368
369 while (!err) {
370 struct dentry *next;
371 struct dentry *parent;
372 struct path lowerpath;
373 struct kstat stat;
374 enum ovl_path_type type = ovl_path_type(dentry);
375
376 if (OVL_TYPE_UPPER(type))
377 break;
378
379 next = dget(dentry);
380 /* find the topmost dentry not yet copied up */
381 for (;;) {
382 parent = dget_parent(next);
383
384 type = ovl_path_type(parent);
385 if (OVL_TYPE_UPPER(type))
386 break;
387
388 dput(next);
389 next = parent;
390 }
391
392 ovl_path_lower(next, &lowerpath);
393 err = vfs_getattr(&lowerpath, &stat);
394 if (!err)
395 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
396
397 dput(parent);
398 dput(next);
399 }
400 revert_creds(old_cred);
401
402 return err;
403 }