Skip to content

Commit ff0f962

Browse files
committed
Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs
Pull overlayfs updates from Miklos Szeredi: "This update contains: - try to clone on copy-up - allow renaming a directory - split source into managable chunks - misc cleanups and fixes It does not contain the read-only fd data inconsistency fix, which Al didn't like. I'll leave that to the next year..." * 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs: (36 commits) ovl: fix reStructuredText syntax errors in documentation ovl: fix return value of ovl_fill_super ovl: clean up kstat usage ovl: fold ovl_copy_up_truncate() into ovl_copy_up() ovl: create directories inside merged parent opaque ovl: opaque cleanup ovl: show redirect_dir mount option ovl: allow setting max size of redirect ovl: allow redirect_dir to default to "on" ovl: check for emptiness of redirect dir ovl: redirect on rename-dir ovl: lookup redirects ovl: consolidate lookup for underlying layers ovl: fix nested overlayfs mount ovl: check namelen ovl: split super.c ovl: use d_is_dir() ovl: simplify lookup ovl: check lower existence of rename target ovl: rename: simplify handling of lower/merged directory ...
2 parents 087a76d + c3c8699 commit ff0f962

17 files changed

Lines changed: 1139 additions & 812 deletions

File tree

‎Documentation/filesystems/overlayfs.txt‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ At mount time, the two directories given as mount options "lowerdir" and
6666
"upperdir" are combined into a merged directory:
6767

6868
mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
69-
workdir=/work /merged
69+
workdir=/work /merged
7070

7171
The "workdir" needs to be an empty directory on the same filesystem
7272
as upperdir.
@@ -118,6 +118,7 @@ programs.
118118

119119
seek offsets are assigned sequentially when the directories are read.
120120
Thus if
121+
121122
- read part of a directory
122123
- remember an offset, and close the directory
123124
- re-open the directory some time later
@@ -130,6 +131,23 @@ directory.
130131
Readdir on directories that are not merged is simply handled by the
131132
underlying directory (upper or lower).
132133

134+
renaming directories
135+
--------------------
136+
137+
When renaming a directory that is on the lower layer or merged (i.e. the
138+
directory was not created on the upper layer to start with) overlayfs can
139+
handle it in two different ways:
140+
141+
1. return EXDEV error: this error is returned by rename(2) when trying to
142+
move a file or directory across filesystem boundaries. Hence
143+
applications are usually prepared to hande this error (mv(1) for example
144+
recursively copies the directory tree). This is the default behavior.
145+
146+
2. If the "redirect_dir" feature is enabled, then the directory will be
147+
copied up (but not the contents). Then the "trusted.overlay.redirect"
148+
extended attribute is set to the path of the original location from the
149+
root of the overlay. Finally the directory is moved to the new
150+
location.
133151

134152
Non-directories
135153
---------------
@@ -185,13 +203,13 @@ filesystem, so both st_dev and st_ino of the file may change.
185203

186204
Any open files referring to this inode will access the old data.
187205

188-
Any file locks (and leases) obtained before copy_up will not apply
189-
to the copied up file.
190-
191206
If a file with multiple hard links is copied up, then this will
192207
"break" the link. Changes will not be propagated to other names
193208
referring to the same inode.
194209

210+
Unless "redirect_dir" feature is enabled, rename(2) on a lower or merged
211+
directory will fail with EXDEV.
212+
195213
Changes to underlying filesystems
196214
---------------------------------
197215

‎fs/ioctl.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd,
223223

224224
if (!src_file.file)
225225
return -EBADF;
226-
ret = vfs_clone_file_range(src_file.file, off, dst_file, destoff, olen);
226+
ret = -EXDEV;
227+
if (src_file.file->f_path.mnt != dst_file->f_path.mnt)
228+
goto fdput;
229+
ret = do_clone_file_range(src_file.file, off, dst_file, destoff, olen);
230+
fdput:
227231
fdput(src_file);
228232
return ret;
229233
}

‎fs/namei.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,11 +4306,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
43064306
bool new_is_dir = false;
43074307
unsigned max_links = new_dir->i_sb->s_max_links;
43084308

4309-
/*
4310-
* Check source == target.
4311-
* On overlayfs need to look at underlying inodes.
4312-
*/
4313-
if (d_real_inode(old_dentry) == d_real_inode(new_dentry))
4309+
if (source == target)
43144310
return 0;
43154311

43164312
error = may_delete(old_dir, old_dentry, is_dir);

‎fs/nfsd/vfs.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,7 @@ __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
509509
__be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
510510
u64 dst_pos, u64 count)
511511
{
512-
return nfserrno(vfs_clone_file_range(src, src_pos, dst, dst_pos,
513-
count));
512+
return nfserrno(do_clone_file_range(src, src_pos, dst, dst_pos, count));
514513
}
515514

516515
ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,

‎fs/overlayfs/Kconfig‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ config OVERLAY_FS
88
merged with the 'upper' object.
99

1010
For more information see Documentation/filesystems/overlayfs.txt
11+
12+
config OVERLAY_FS_REDIRECT_DIR
13+
bool "Overlayfs: turn on redirect dir feature by default"
14+
depends on OVERLAY_FS
15+
help
16+
If this config option is enabled then overlay filesystems will use
17+
redirects when renaming directories by default. In this case it is
18+
still possible to turn off redirects globally with the
19+
"redirect_dir=off" module option or on a filesystem instance basis
20+
with the "redirect_dir=off" mount option.
21+
22+
Note, that redirects are not backward compatible. That is, mounting
23+
an overlay which has redirects on a kernel that doesn't support this
24+
feature will have unexpected results.

‎fs/overlayfs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
obj-$(CONFIG_OVERLAY_FS) += overlay.o
66

7-
overlay-objs := super.o inode.o dir.o readdir.o copy_up.o
7+
overlay-objs := super.o namei.o util.o inode.o dir.o readdir.o copy_up.o

‎fs/overlayfs/copy_up.c‎

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
153153
goto out_fput;
154154
}
155155

156+
/* Try to use clone_file_range to clone up within the same fs */
157+
error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
158+
if (!error)
159+
goto out;
160+
/* Couldn't clone, so now we try to copy the data */
161+
error = 0;
162+
156163
/* FIXME: copy up sparse files efficiently */
157164
while (len) {
158165
size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
@@ -177,7 +184,7 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
177184

178185
len -= bytes;
179186
}
180-
187+
out:
181188
if (!error)
182189
error = vfs_fsync(new_file, 0);
183190
fput(new_file);
@@ -231,10 +238,15 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
231238
struct inode *udir = upperdir->d_inode;
232239
struct dentry *newdentry = NULL;
233240
struct dentry *upper = NULL;
234-
umode_t mode = stat->mode;
235241
int err;
236242
const struct cred *old_creds = NULL;
237243
struct cred *new_creds = NULL;
244+
struct cattr cattr = {
245+
/* Can't properly set mode on creation because of the umask */
246+
.mode = stat->mode & S_IFMT,
247+
.rdev = stat->rdev,
248+
.link = link
249+
};
238250

239251
newdentry = ovl_lookup_temp(workdir, dentry);
240252
err = PTR_ERR(newdentry);
@@ -254,10 +266,7 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
254266
if (new_creds)
255267
old_creds = override_creds(new_creds);
256268

257-
/* Can't properly set mode on creation because of the umask */
258-
stat->mode &= S_IFMT;
259-
err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
260-
stat->mode = mode;
269+
err = ovl_create_real(wdir, newdentry, &cattr, NULL, true);
261270

262271
if (new_creds) {
263272
revert_creds(old_creds);
@@ -296,12 +305,6 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
296305
ovl_dentry_update(dentry, newdentry);
297306
ovl_inode_update(d_inode(dentry), d_inode(newdentry));
298307
newdentry = NULL;
299-
300-
/*
301-
* Non-directores become opaque when copied up.
302-
*/
303-
if (!S_ISDIR(stat->mode))
304-
ovl_dentry_set_opaque(dentry, true);
305308
out2:
306309
dput(upper);
307310
out1:
@@ -317,20 +320,14 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
317320
/*
318321
* Copy up a single dentry
319322
*
320-
* Directory renames only allowed on "pure upper" (already created on
321-
* upper filesystem, never copied up). Directories which are on lower or
322-
* are merged may not be renamed. For these -EXDEV is returned and
323-
* userspace has to deal with it. This means, when copying up a
324-
* directory we can rely on it and ancestors being stable.
325-
*
326-
* Non-directory renames start with copy up of source if necessary. The
327-
* actual rename will only proceed once the copy up was successful. Copy
328-
* up uses upper parent i_mutex for exclusion. Since rename can change
329-
* d_parent it is possible that the copy up will lock the old parent. At
330-
* that point the file will have already been copied up anyway.
323+
* All renames start with copy up of source if necessary. The actual
324+
* rename will only proceed once the copy up was successful. Copy up uses
325+
* upper parent i_mutex for exclusion. Since rename can change d_parent it
326+
* is possible that the copy up will lock the old parent. At that point
327+
* the file will have already been copied up anyway.
331328
*/
332-
int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
333-
struct path *lowerpath, struct kstat *stat)
329+
static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
330+
struct path *lowerpath, struct kstat *stat)
334331
{
335332
DEFINE_DELAYED_CALL(done);
336333
struct dentry *workdir = ovl_workdir(dentry);
@@ -339,7 +336,6 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
339336
struct path parentpath;
340337
struct dentry *lowerdentry = lowerpath->dentry;
341338
struct dentry *upperdir;
342-
struct dentry *upperdentry;
343339
const char *link = NULL;
344340

345341
if (WARN_ON(!workdir))
@@ -365,8 +361,7 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
365361
pr_err("overlayfs: failed to lock workdir+upperdir\n");
366362
goto out_unlock;
367363
}
368-
upperdentry = ovl_dentry_upper(dentry);
369-
if (upperdentry) {
364+
if (ovl_dentry_upper(dentry)) {
370365
/* Raced with another copy-up? Nothing to do, then... */
371366
err = 0;
372367
goto out_unlock;
@@ -385,7 +380,7 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
385380
return err;
386381
}
387382

388-
int ovl_copy_up(struct dentry *dentry)
383+
int ovl_copy_up_flags(struct dentry *dentry, int flags)
389384
{
390385
int err = 0;
391386
const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
@@ -415,6 +410,9 @@ int ovl_copy_up(struct dentry *dentry)
415410

416411
ovl_path_lower(next, &lowerpath);
417412
err = vfs_getattr(&lowerpath, &stat);
413+
/* maybe truncate regular file. this has no effect on dirs */
414+
if (flags & O_TRUNC)
415+
stat.size = 0;
418416
if (!err)
419417
err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
420418

@@ -425,3 +423,8 @@ int ovl_copy_up(struct dentry *dentry)
425423

426424
return err;
427425
}
426+
427+
int ovl_copy_up(struct dentry *dentry)
428+
{
429+
return ovl_copy_up_flags(dentry, 0);
430+
}

0 commit comments

Comments
 (0)