dir.c
13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
*
* Copyright (C) 2011 Novell Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*/
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/xattr.h>
#include <linux/security.h>
#include <linux/cred.h>
#include "overlayfs.h"
static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
{
int err;
struct dentry *newdentry;
const struct cred *old_cred;
struct cred *override_cred;
/* FIXME: recheck lower dentry to see if whiteout is really needed */
err = -ENOMEM;
override_cred = prepare_creds();
if (!override_cred)
goto out;
/*
* CAP_SYS_ADMIN for setxattr
* CAP_DAC_OVERRIDE for symlink creation
* CAP_FOWNER for unlink in sticky directory
*/
cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
cap_raise(override_cred->cap_effective, CAP_FOWNER);
override_cred->fsuid = GLOBAL_ROOT_UID;
override_cred->fsgid = GLOBAL_ROOT_GID;
old_cred = override_creds(override_cred);
newdentry = lookup_one_len(dentry->d_name.name, upperdir,
dentry->d_name.len);
err = PTR_ERR(newdentry);
if (IS_ERR(newdentry))
goto out_put_cred;
/* Just been removed within the same locked region */
WARN_ON(newdentry->d_inode);
err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
if (err)
goto out_dput;
ovl_dentry_version_inc(dentry->d_parent);
err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
if (err)
vfs_unlink(upperdir->d_inode, newdentry, NULL);
out_dput:
dput(newdentry);
out_put_cred:
revert_creds(old_cred);
put_cred(override_cred);
out:
if (err) {
/*
* There's no way to recover from failure to whiteout.
* What should we do? Log a big fat error and... ?
*/
pr_err("overlayfs: ERROR - failed to whiteout '%s'\n",
dentry->d_name.name);
}
return err;
}
static struct dentry *ovl_lookup_create(struct dentry *upperdir,
struct dentry *template)
{
int err;
struct dentry *newdentry;
struct qstr *name = &template->d_name;
newdentry = lookup_one_len(name->name, upperdir, name->len);
if (IS_ERR(newdentry))
return newdentry;
if (newdentry->d_inode) {
const struct cred *old_cred;
struct cred *override_cred;
/* No need to check whiteout if lower parent is non-existent */
err = -EEXIST;
if (!ovl_dentry_lower(template->d_parent))
goto out_dput;
if (!S_ISLNK(newdentry->d_inode->i_mode))
goto out_dput;
err = -ENOMEM;
override_cred = prepare_creds();
if (!override_cred)
goto out_dput;
/*
* CAP_SYS_ADMIN for getxattr
* CAP_FOWNER for unlink in sticky directory
*/
cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
cap_raise(override_cred->cap_effective, CAP_FOWNER);
old_cred = override_creds(override_cred);
err = -EEXIST;
if (ovl_is_whiteout(newdentry))
err = vfs_unlink(upperdir->d_inode, newdentry, NULL);
revert_creds(old_cred);
put_cred(override_cred);
if (err)
goto out_dput;
dput(newdentry);
newdentry = lookup_one_len(name->name, upperdir, name->len);
if (IS_ERR(newdentry)) {
ovl_whiteout(upperdir, template);
return newdentry;
}
/*
* Whiteout just been successfully removed, parent
* i_mutex is still held, there's no way the lookup
* could return positive.
*/
WARN_ON(newdentry->d_inode);
}
return newdentry;
out_dput:
dput(newdentry);
return ERR_PTR(err);
}
struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
struct kstat *stat, const char *link)
{
int err;
struct dentry *newdentry;
struct inode *dir = upperdir->d_inode;
newdentry = ovl_lookup_create(upperdir, dentry);
if (IS_ERR(newdentry))
goto out;
switch (stat->mode & S_IFMT) {
case S_IFREG:
err = vfs_create(dir, newdentry, stat->mode, NULL);
break;
case S_IFDIR:
err = vfs_mkdir(dir, newdentry, stat->mode);
break;
case S_IFCHR:
case S_IFBLK:
case S_IFIFO:
case S_IFSOCK:
err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
break;
case S_IFLNK:
err = vfs_symlink(dir, newdentry, link);
break;
default:
err = -EPERM;
}
if (err) {
if (ovl_dentry_is_opaque(dentry))
ovl_whiteout(upperdir, dentry);
dput(newdentry);
newdentry = ERR_PTR(err);
} else if (WARN_ON(!newdentry->d_inode)) {
/*
* Not quite sure if non-instantiated dentry is legal or not.
* VFS doesn't seem to care so check and warn here.
*/
dput(newdentry);
newdentry = ERR_PTR(-ENOENT);
}
out:
return newdentry;
}
static int ovl_set_opaque(struct dentry *upperdentry)
{
int err;
const struct cred *old_cred;
struct cred *override_cred;
override_cred = prepare_creds();
if (!override_cred)
return -ENOMEM;
/* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
old_cred = override_creds(override_cred);
err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
revert_creds(old_cred);
put_cred(override_cred);
return err;
}
static int ovl_remove_opaque(struct dentry *upperdentry)
{
int err;
const struct cred *old_cred;
struct cred *override_cred;
override_cred = prepare_creds();
if (!override_cred)
return -ENOMEM;
/* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
old_cred = override_creds(override_cred);
err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
revert_creds(old_cred);
put_cred(override_cred);
return err;
}
static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
struct kstat *stat)
{
int err;
enum ovl_path_type type;
struct path realpath;
type = ovl_path_real(dentry, &realpath);
err = vfs_getattr(&realpath, stat);
if (err)
return err;
stat->dev = dentry->d_sb->s_dev;
stat->ino = dentry->d_inode->i_ino;
/*
* It's probably not worth it to count subdirs to get the
* correct link count. nlink=1 seems to pacify 'find' and
* other utilities.
*/
if (type == OVL_PATH_MERGE)
stat->nlink = 1;
return 0;
}
static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
const char *link)
{
int err;
struct dentry *newdentry;
struct dentry *upperdir;
struct inode *inode;
struct kstat stat = {
.mode = mode,
.rdev = rdev,
};
err = -ENOMEM;
inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
if (!inode)
goto out;
err = ovl_copy_up(dentry->d_parent);
if (err)
goto out_iput;
upperdir = ovl_dentry_upper(dentry->d_parent);
mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
err = PTR_ERR(newdentry);
if (IS_ERR(newdentry))
goto out_unlock;
ovl_dentry_version_inc(dentry->d_parent);
if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
err = ovl_set_opaque(newdentry);
if (err) {
vfs_rmdir(upperdir->d_inode, newdentry);
ovl_whiteout(upperdir, dentry);
goto out_dput;
}
}
ovl_dentry_update(dentry, newdentry);
ovl_copyattr(newdentry->d_inode, inode);
d_instantiate(dentry, inode);
inode = NULL;
newdentry = NULL;
err = 0;
out_dput:
dput(newdentry);
out_unlock:
mutex_unlock(&upperdir->d_inode->i_mutex);
out_iput:
iput(inode);
out:
return err;
}
static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
bool excl)
{
return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
}
static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
}
static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
dev_t rdev)
{
return ovl_create_object(dentry, mode, rdev, NULL);
}
static int ovl_symlink(struct inode *dir, struct dentry *dentry,
const char *link)
{
return ovl_create_object(dentry, S_IFLNK, 0, link);
}
static int ovl_do_remove(struct dentry *dentry, bool is_dir)
{
int err;
enum ovl_path_type type;
struct path realpath;
struct dentry *upperdir;
err = ovl_copy_up(dentry->d_parent);
if (err)
return err;
upperdir = ovl_dentry_upper(dentry->d_parent);
mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
type = ovl_path_real(dentry, &realpath);
if (type != OVL_PATH_LOWER) {
err = -ESTALE;
if (realpath.dentry->d_parent != upperdir)
goto out_d_drop;
/* FIXME: create whiteout up front and rename to target */
if (is_dir)
err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
else
err = vfs_unlink(upperdir->d_inode, realpath.dentry, NULL);
if (err)
goto out_d_drop;
ovl_dentry_version_inc(dentry->d_parent);
}
if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
err = ovl_whiteout(upperdir, dentry);
/*
* Keeping this dentry hashed would mean having to release
* upperpath/lowerpath, which could only be done if we are the
* sole user of this dentry. Too tricky... Just unhash for
* now.
*/
out_d_drop:
d_drop(dentry);
mutex_unlock(&upperdir->d_inode->i_mutex);
return err;
}
static int ovl_unlink(struct inode *dir, struct dentry *dentry)
{
return ovl_do_remove(dentry, false);
}
static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
{
int err;
enum ovl_path_type type;
type = ovl_path_type(dentry);
if (type != OVL_PATH_UPPER) {
err = ovl_check_empty_and_clear(dentry, type);
if (err)
return err;
}
return ovl_do_remove(dentry, true);
}
static int ovl_link(struct dentry *old, struct inode *newdir,
struct dentry *new)
{
int err;
struct dentry *olddentry;
struct dentry *newdentry;
struct dentry *upperdir;
struct inode *newinode;
err = ovl_copy_up(old);
if (err)
goto out;
err = ovl_copy_up(new->d_parent);
if (err)
goto out;
upperdir = ovl_dentry_upper(new->d_parent);
mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
newdentry = ovl_lookup_create(upperdir, new);
err = PTR_ERR(newdentry);
if (IS_ERR(newdentry))
goto out_unlock;
olddentry = ovl_dentry_upper(old);
err = vfs_link(olddentry, upperdir->d_inode, newdentry, NULL);
if (!err) {
if (WARN_ON(!newdentry->d_inode)) {
dput(newdentry);
err = -ENOENT;
goto out_unlock;
}
newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
new->d_fsdata);
if (!newinode) {
err = -ENOMEM;
goto link_fail;
}
ovl_copyattr(upperdir->d_inode, newinode);
ovl_dentry_version_inc(new->d_parent);
ovl_dentry_update(new, newdentry);
d_instantiate(new, newinode);
} else {
link_fail:
if (ovl_dentry_is_opaque(new))
ovl_whiteout(upperdir, new);
dput(newdentry);
}
out_unlock:
mutex_unlock(&upperdir->d_inode->i_mutex);
out:
return err;
}
static int ovl_rename(struct inode *olddir, struct dentry *old,
struct inode *newdir, struct dentry *new)
{
int err;
enum ovl_path_type old_type;
enum ovl_path_type new_type;
struct dentry *old_upperdir;
struct dentry *new_upperdir;
struct dentry *olddentry;
struct dentry *newdentry;
struct dentry *trap;
bool old_opaque;
bool new_opaque;
bool new_create = false;
bool is_dir = S_ISDIR(old->d_inode->i_mode);
/* Don't copy up directory trees */
old_type = ovl_path_type(old);
if (old_type != OVL_PATH_UPPER && is_dir)
return -EXDEV;
if (new->d_inode) {
new_type = ovl_path_type(new);
if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
if (ovl_dentry_lower(old)->d_inode ==
ovl_dentry_lower(new)->d_inode)
return 0;
}
if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
if (ovl_dentry_upper(old)->d_inode ==
ovl_dentry_upper(new)->d_inode)
return 0;
}
if (new_type != OVL_PATH_UPPER &&
S_ISDIR(new->d_inode->i_mode)) {
err = ovl_check_empty_and_clear(new, new_type);
if (err)
return err;
}
} else {
new_type = OVL_PATH_UPPER;
}
err = ovl_copy_up(old);
if (err)
return err;
err = ovl_copy_up(new->d_parent);
if (err)
return err;
old_upperdir = ovl_dentry_upper(old->d_parent);
new_upperdir = ovl_dentry_upper(new->d_parent);
trap = lock_rename(new_upperdir, old_upperdir);
olddentry = ovl_dentry_upper(old);
newdentry = ovl_dentry_upper(new);
if (newdentry) {
dget(newdentry);
} else {
new_create = true;
newdentry = ovl_lookup_create(new_upperdir, new);
err = PTR_ERR(newdentry);
if (IS_ERR(newdentry))
goto out_unlock;
}
err = -ESTALE;
if (olddentry->d_parent != old_upperdir)
goto out_dput;
if (newdentry->d_parent != new_upperdir)
goto out_dput;
if (olddentry == trap)
goto out_dput;
if (newdentry == trap)
goto out_dput;
old_opaque = ovl_dentry_is_opaque(old);
new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
if (is_dir && !old_opaque && new_opaque) {
err = ovl_set_opaque(olddentry);
if (err)
goto out_dput;
}
err = vfs_rename(old_upperdir->d_inode, olddentry,
new_upperdir->d_inode, newdentry, NULL);
if (err) {
if (new_create && ovl_dentry_is_opaque(new))
ovl_whiteout(new_upperdir, new);
if (is_dir && !old_opaque && new_opaque)
ovl_remove_opaque(olddentry);
goto out_dput;
}
if (old_type != OVL_PATH_UPPER || old_opaque)
err = ovl_whiteout(old_upperdir, old);
if (is_dir && old_opaque && !new_opaque)
ovl_remove_opaque(olddentry);
if (old_opaque != new_opaque)
ovl_dentry_set_opaque(old, new_opaque);
ovl_dentry_version_inc(old->d_parent);
ovl_dentry_version_inc(new->d_parent);
out_dput:
dput(newdentry);
out_unlock:
unlock_rename(new_upperdir, old_upperdir);
return err;
}
const struct inode_operations ovl_dir_inode_operations = {
.lookup = ovl_lookup,
.mkdir = ovl_mkdir,
.symlink = ovl_symlink,
.unlink = ovl_unlink,
.rmdir = ovl_rmdir,
.rename = ovl_rename,
.link = ovl_link,
.setattr = ovl_setattr,
.create = ovl_create,
.mknod = ovl_mknod,
.permission = ovl_permission,
.getattr = ovl_dir_getattr,
.setxattr = ovl_setxattr,
.getxattr = ovl_getxattr,
.listxattr = ovl_listxattr,
.removexattr = ovl_removexattr,
};