blob: 50e4407398d816b769adc3b1b4a5d11dc8a203f1 [file] [log] [blame]
Miklos Szeredid1d04ef2018-07-18 15:44:41 +02001/*
2 * Copyright (C) 2017 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 */
8
9#include <linux/cred.h>
10#include <linux/file.h>
Miklos Szeredidab5ca82018-07-18 15:44:42 +020011#include <linux/mount.h>
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020012#include <linux/xattr.h>
Miklos Szeredi16914e62018-07-18 15:44:41 +020013#include <linux/uio.h>
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020014#include "overlayfs.h"
15
Vivek Goyal8c444d22018-05-11 11:49:31 -040016static char ovl_whatisit(struct inode *inode, struct inode *realinode)
17{
18 if (realinode != ovl_inode_upper(inode))
19 return 'l';
20 if (ovl_has_upperdata(inode))
21 return 'u';
22 else
23 return 'm';
24}
25
26static struct file *ovl_open_realfile(const struct file *file,
27 struct inode *realinode)
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020028{
29 struct inode *inode = file_inode(file);
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020030 struct file *realfile;
31 const struct cred *old_cred;
32
33 old_cred = ovl_override_creds(inode->i_sb);
34 realfile = open_with_fake_path(&file->f_path, file->f_flags | O_NOATIME,
35 realinode, current_cred());
36 revert_creds(old_cred);
37
38 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
Vivek Goyal8c444d22018-05-11 11:49:31 -040039 file, file, ovl_whatisit(inode, realinode), file->f_flags,
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020040 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
41
42 return realfile;
43}
44
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020045#define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
46
47static int ovl_change_flags(struct file *file, unsigned int flags)
48{
49 struct inode *inode = file_inode(file);
50 int err;
51
52 /* No atime modificaton on underlying */
53 flags |= O_NOATIME;
54
55 /* If some flag changed that cannot be changed then something's amiss */
56 if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
57 return -EIO;
58
59 flags &= OVL_SETFL_MASK;
60
61 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
62 return -EPERM;
63
64 if (flags & O_DIRECT) {
65 if (!file->f_mapping->a_ops ||
66 !file->f_mapping->a_ops->direct_IO)
67 return -EINVAL;
68 }
69
70 if (file->f_op->check_flags) {
71 err = file->f_op->check_flags(flags);
72 if (err)
73 return err;
74 }
75
76 spin_lock(&file->f_lock);
77 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
78 spin_unlock(&file->f_lock);
79
80 return 0;
81}
82
Vivek Goyal8c444d22018-05-11 11:49:31 -040083static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
84 bool allow_meta)
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020085{
86 struct inode *inode = file_inode(file);
Vivek Goyal8c444d22018-05-11 11:49:31 -040087 struct inode *realinode;
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020088
89 real->flags = 0;
90 real->file = file->private_data;
91
Vivek Goyal8c444d22018-05-11 11:49:31 -040092 if (allow_meta)
93 realinode = ovl_inode_real(inode);
94 else
95 realinode = ovl_inode_realdata(inode);
96
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020097 /* Has it been copied up since we'd opened it? */
Vivek Goyal8c444d22018-05-11 11:49:31 -040098 if (unlikely(file_inode(real->file) != realinode)) {
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020099 real->flags = FDPUT_FPUT;
Vivek Goyal8c444d22018-05-11 11:49:31 -0400100 real->file = ovl_open_realfile(file, realinode);
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200101
102 return PTR_ERR_OR_ZERO(real->file);
103 }
104
105 /* Did the flags change since open? */
106 if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME))
107 return ovl_change_flags(real->file, file->f_flags);
108
109 return 0;
110}
111
Vivek Goyal8c444d22018-05-11 11:49:31 -0400112static int ovl_real_fdget(const struct file *file, struct fd *real)
113{
114 return ovl_real_fdget_meta(file, real, false);
115}
116
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200117static int ovl_open(struct inode *inode, struct file *file)
118{
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200119 struct file *realfile;
120 int err;
121
Amir Goldstein34280302019-01-22 07:01:39 +0200122 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200123 if (err)
124 return err;
125
126 /* No longer need these flags, so don't pass them on to underlying fs */
127 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
128
Vivek Goyal8c444d22018-05-11 11:49:31 -0400129 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200130 if (IS_ERR(realfile))
131 return PTR_ERR(realfile);
132
133 file->private_data = realfile;
134
135 return 0;
136}
137
138static int ovl_release(struct inode *inode, struct file *file)
139{
140 fput(file->private_data);
141
142 return 0;
143}
144
145static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
146{
147 struct inode *realinode = ovl_inode_real(file_inode(file));
148
149 return generic_file_llseek_size(file, offset, whence,
150 realinode->i_sb->s_maxbytes,
151 i_size_read(realinode));
152}
153
Miklos Szeredi16914e62018-07-18 15:44:41 +0200154static void ovl_file_accessed(struct file *file)
155{
156 struct inode *inode, *upperinode;
157
158 if (file->f_flags & O_NOATIME)
159 return;
160
161 inode = file_inode(file);
162 upperinode = ovl_inode_upper(inode);
163
164 if (!upperinode)
165 return;
166
167 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
168 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
169 inode->i_mtime = upperinode->i_mtime;
170 inode->i_ctime = upperinode->i_ctime;
171 }
172
173 touch_atime(&file->f_path);
174}
175
176static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb)
177{
178 int ifl = iocb->ki_flags;
179 rwf_t flags = 0;
180
181 if (ifl & IOCB_NOWAIT)
182 flags |= RWF_NOWAIT;
183 if (ifl & IOCB_HIPRI)
184 flags |= RWF_HIPRI;
185 if (ifl & IOCB_DSYNC)
186 flags |= RWF_DSYNC;
187 if (ifl & IOCB_SYNC)
188 flags |= RWF_SYNC;
189
190 return flags;
191}
192
193static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
194{
195 struct file *file = iocb->ki_filp;
196 struct fd real;
197 const struct cred *old_cred;
198 ssize_t ret;
199
200 if (!iov_iter_count(iter))
201 return 0;
202
203 ret = ovl_real_fdget(file, &real);
204 if (ret)
205 return ret;
206
207 old_cred = ovl_override_creds(file_inode(file)->i_sb);
208 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
209 ovl_iocb_to_rwf(iocb));
210 revert_creds(old_cred);
211
212 ovl_file_accessed(file);
213
214 fdput(real);
215
216 return ret;
217}
218
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200219static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
220{
221 struct file *file = iocb->ki_filp;
222 struct inode *inode = file_inode(file);
223 struct fd real;
224 const struct cred *old_cred;
225 ssize_t ret;
226
227 if (!iov_iter_count(iter))
228 return 0;
229
230 inode_lock(inode);
231 /* Update mode */
232 ovl_copyattr(ovl_inode_real(inode), inode);
233 ret = file_remove_privs(file);
234 if (ret)
235 goto out_unlock;
236
237 ret = ovl_real_fdget(file, &real);
238 if (ret)
239 goto out_unlock;
240
241 old_cred = ovl_override_creds(file_inode(file)->i_sb);
Amir Goldstein898cc192018-09-18 16:34:32 +0300242 file_start_write(real.file);
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200243 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
244 ovl_iocb_to_rwf(iocb));
Amir Goldstein898cc192018-09-18 16:34:32 +0300245 file_end_write(real.file);
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200246 revert_creds(old_cred);
247
248 /* Update size */
249 ovl_copyattr(ovl_inode_real(inode), inode);
250
251 fdput(real);
252
253out_unlock:
254 inode_unlock(inode);
255
256 return ret;
257}
258
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200259static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
260{
261 struct fd real;
262 const struct cred *old_cred;
263 int ret;
264
Vivek Goyal8c444d22018-05-11 11:49:31 -0400265 ret = ovl_real_fdget_meta(file, &real, !datasync);
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200266 if (ret)
267 return ret;
268
269 /* Don't sync lower file for fear of receiving EROFS error */
270 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
271 old_cred = ovl_override_creds(file_inode(file)->i_sb);
272 ret = vfs_fsync_range(real.file, start, end, datasync);
273 revert_creds(old_cred);
274 }
275
276 fdput(real);
277
278 return ret;
279}
280
Miklos Szeredi2f502832018-07-18 15:44:42 +0200281static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
282{
283 struct file *realfile = file->private_data;
284 const struct cred *old_cred;
285 int ret;
286
287 if (!realfile->f_op->mmap)
288 return -ENODEV;
289
290 if (WARN_ON(file != vma->vm_file))
291 return -EIO;
292
293 vma->vm_file = get_file(realfile);
294
295 old_cred = ovl_override_creds(file_inode(file)->i_sb);
296 ret = call_mmap(vma->vm_file, vma);
297 revert_creds(old_cred);
298
299 if (ret) {
300 /* Drop reference count from new vm_file value */
301 fput(realfile);
302 } else {
303 /* Drop reference count from previous vm_file value */
304 fput(file);
305 }
306
307 ovl_file_accessed(file);
308
309 return ret;
310}
311
Miklos Szerediaab88482018-07-18 15:44:42 +0200312static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
313{
314 struct inode *inode = file_inode(file);
315 struct fd real;
316 const struct cred *old_cred;
317 int ret;
318
319 ret = ovl_real_fdget(file, &real);
320 if (ret)
321 return ret;
322
323 old_cred = ovl_override_creds(file_inode(file)->i_sb);
324 ret = vfs_fallocate(real.file, mode, offset, len);
325 revert_creds(old_cred);
326
327 /* Update size */
328 ovl_copyattr(ovl_inode_real(inode), inode);
329
330 fdput(real);
331
332 return ret;
333}
334
Amir Goldsteinb833a362018-08-28 10:58:41 +0300335static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
336{
337 struct fd real;
338 const struct cred *old_cred;
339 int ret;
340
341 ret = ovl_real_fdget(file, &real);
342 if (ret)
343 return ret;
344
345 old_cred = ovl_override_creds(file_inode(file)->i_sb);
346 ret = vfs_fadvise(real.file, offset, len, advice);
347 revert_creds(old_cred);
348
349 fdput(real);
350
351 return ret;
352}
353
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200354static long ovl_real_ioctl(struct file *file, unsigned int cmd,
355 unsigned long arg)
356{
357 struct fd real;
358 const struct cred *old_cred;
359 long ret;
360
361 ret = ovl_real_fdget(file, &real);
362 if (ret)
363 return ret;
364
365 old_cred = ovl_override_creds(file_inode(file)->i_sb);
366 ret = vfs_ioctl(real.file, cmd, arg);
367 revert_creds(old_cred);
368
369 fdput(real);
370
371 return ret;
372}
373
374static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
375{
376 long ret;
377 struct inode *inode = file_inode(file);
378
379 switch (cmd) {
380 case FS_IOC_GETFLAGS:
381 ret = ovl_real_ioctl(file, cmd, arg);
382 break;
383
384 case FS_IOC_SETFLAGS:
385 if (!inode_owner_or_capable(inode))
386 return -EACCES;
387
388 ret = mnt_want_write_file(file);
389 if (ret)
390 return ret;
391
Amir Goldstein34280302019-01-22 07:01:39 +0200392 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200393 if (!ret) {
394 ret = ovl_real_ioctl(file, cmd, arg);
395
396 inode_lock(inode);
397 ovl_copyflags(ovl_inode_real(inode), inode);
398 inode_unlock(inode);
399 }
400
401 mnt_drop_write_file(file);
402 break;
403
404 default:
405 ret = -ENOTTY;
406 }
407
408 return ret;
409}
410
411static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
412 unsigned long arg)
413{
414 switch (cmd) {
415 case FS_IOC32_GETFLAGS:
416 cmd = FS_IOC_GETFLAGS;
417 break;
418
419 case FS_IOC32_SETFLAGS:
420 cmd = FS_IOC_SETFLAGS;
421 break;
422
423 default:
424 return -ENOIOCTLCMD;
425 }
426
427 return ovl_ioctl(file, cmd, arg);
428}
429
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200430enum ovl_copyop {
431 OVL_COPY,
432 OVL_CLONE,
433 OVL_DEDUPE,
434};
435
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100436static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200437 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100438 loff_t len, unsigned int flags, enum ovl_copyop op)
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200439{
440 struct inode *inode_out = file_inode(file_out);
441 struct fd real_in, real_out;
442 const struct cred *old_cred;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100443 loff_t ret;
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200444
445 ret = ovl_real_fdget(file_out, &real_out);
446 if (ret)
447 return ret;
448
449 ret = ovl_real_fdget(file_in, &real_in);
450 if (ret) {
451 fdput(real_out);
452 return ret;
453 }
454
455 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
456 switch (op) {
457 case OVL_COPY:
458 ret = vfs_copy_file_range(real_in.file, pos_in,
459 real_out.file, pos_out, len, flags);
460 break;
461
462 case OVL_CLONE:
Amir Goldsteina7253562018-09-18 16:34:34 +0300463 ret = vfs_clone_file_range(real_in.file, pos_in,
Darrick J. Wong452ce652018-10-30 10:41:56 +1100464 real_out.file, pos_out, len, flags);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200465 break;
466
467 case OVL_DEDUPE:
468 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
Darrick J. Wongdf365832018-10-30 10:42:03 +1100469 real_out.file, pos_out, len,
470 flags);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200471 break;
472 }
473 revert_creds(old_cred);
474
475 /* Update size */
476 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
477
478 fdput(real_in);
479 fdput(real_out);
480
481 return ret;
482}
483
484static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
485 struct file *file_out, loff_t pos_out,
486 size_t len, unsigned int flags)
487{
488 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
489 OVL_COPY);
490}
491
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100492static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
493 struct file *file_out, loff_t pos_out,
494 loff_t len, unsigned int remap_flags)
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200495{
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100496 enum ovl_copyop op;
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200497
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100498 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
499 return -EINVAL;
500
501 if (remap_flags & REMAP_FILE_DEDUP)
502 op = OVL_DEDUPE;
503 else
504 op = OVL_CLONE;
505
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200506 /*
507 * Don't copy up because of a dedupe request, this wouldn't make sense
508 * most of the time (data would be duplicated instead of deduplicated).
509 */
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100510 if (op == OVL_DEDUPE &&
511 (!ovl_inode_upper(file_inode(file_in)) ||
512 !ovl_inode_upper(file_inode(file_out))))
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200513 return -EPERM;
514
Darrick J. Wong452ce652018-10-30 10:41:56 +1100515 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
516 remap_flags, op);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200517}
518
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200519const struct file_operations ovl_file_operations = {
520 .open = ovl_open,
521 .release = ovl_release,
522 .llseek = ovl_llseek,
Miklos Szeredi16914e62018-07-18 15:44:41 +0200523 .read_iter = ovl_read_iter,
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200524 .write_iter = ovl_write_iter,
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200525 .fsync = ovl_fsync,
Miklos Szeredi2f502832018-07-18 15:44:42 +0200526 .mmap = ovl_mmap,
Miklos Szerediaab88482018-07-18 15:44:42 +0200527 .fallocate = ovl_fallocate,
Amir Goldsteinb833a362018-08-28 10:58:41 +0300528 .fadvise = ovl_fadvise,
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200529 .unlocked_ioctl = ovl_ioctl,
530 .compat_ioctl = ovl_compat_ioctl,
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200531
532 .copy_file_range = ovl_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100533 .remap_file_range = ovl_remap_file_range,
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200534};