~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/overlayfs/dir.c

Version: ~ [ linux-5.8-rc4 ] ~ [ linux-5.7.7 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.50 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.131 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.187 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.229 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.229 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/namei.h>
 12 #include <linux/xattr.h>
 13 #include <linux/security.h>
 14 #include <linux/cred.h>
 15 #include "overlayfs.h"
 16 
 17 void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
 18 {
 19         int err;
 20 
 21         dget(wdentry);
 22         if (d_is_dir(wdentry))
 23                 err = ovl_do_rmdir(wdir, wdentry);
 24         else
 25                 err = ovl_do_unlink(wdir, wdentry);
 26         dput(wdentry);
 27 
 28         if (err) {
 29                 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
 30                        wdentry, err);
 31         }
 32 }
 33 
 34 struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
 35 {
 36         struct dentry *temp;
 37         char name[20];
 38 
 39         snprintf(name, sizeof(name), "#%lx", (unsigned long) dentry);
 40 
 41         temp = lookup_one_len(name, workdir, strlen(name));
 42         if (!IS_ERR(temp) && temp->d_inode) {
 43                 pr_err("overlayfs: workdir/%s already exists\n", name);
 44                 dput(temp);
 45                 temp = ERR_PTR(-EIO);
 46         }
 47 
 48         return temp;
 49 }
 50 
 51 /* caller holds i_mutex on workdir */
 52 static struct dentry *ovl_whiteout(struct dentry *workdir,
 53                                    struct dentry *dentry)
 54 {
 55         int err;
 56         struct dentry *whiteout;
 57         struct inode *wdir = workdir->d_inode;
 58 
 59         whiteout = ovl_lookup_temp(workdir, dentry);
 60         if (IS_ERR(whiteout))
 61                 return whiteout;
 62 
 63         err = ovl_do_whiteout(wdir, whiteout);
 64         if (err) {
 65                 dput(whiteout);
 66                 whiteout = ERR_PTR(err);
 67         }
 68 
 69         return whiteout;
 70 }
 71 
 72 int ovl_create_real(struct inode *dir, struct dentry *newdentry,
 73                     struct kstat *stat, const char *link,
 74                     struct dentry *hardlink, bool debug)
 75 {
 76         int err;
 77 
 78         if (newdentry->d_inode)
 79                 return -ESTALE;
 80 
 81         if (hardlink) {
 82                 err = ovl_do_link(hardlink, dir, newdentry, debug);
 83         } else {
 84                 switch (stat->mode & S_IFMT) {
 85                 case S_IFREG:
 86                         err = ovl_do_create(dir, newdentry, stat->mode, debug);
 87                         break;
 88 
 89                 case S_IFDIR:
 90                         err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
 91                         break;
 92 
 93                 case S_IFCHR:
 94                 case S_IFBLK:
 95                 case S_IFIFO:
 96                 case S_IFSOCK:
 97                         err = ovl_do_mknod(dir, newdentry,
 98                                            stat->mode, stat->rdev, debug);
 99                         break;
100 
101                 case S_IFLNK:
102                         err = ovl_do_symlink(dir, newdentry, link, debug);
103                         break;
104 
105                 default:
106                         err = -EPERM;
107                 }
108         }
109         if (!err && WARN_ON(!newdentry->d_inode)) {
110                 /*
111                  * Not quite sure if non-instantiated dentry is legal or not.
112                  * VFS doesn't seem to care so check and warn here.
113                  */
114                 err = -ENOENT;
115         }
116         return err;
117 }
118 
119 static int ovl_set_opaque(struct dentry *upperdentry)
120 {
121         return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
122 }
123 
124 static void ovl_remove_opaque(struct dentry *upperdentry)
125 {
126         int err;
127 
128         err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
129         if (err) {
130                 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
131                         upperdentry->d_name.name, err);
132         }
133 }
134 
135 static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
136                          struct kstat *stat)
137 {
138         int err;
139         enum ovl_path_type type;
140         struct path realpath;
141 
142         type = ovl_path_real(dentry, &realpath);
143         err = vfs_getattr(&realpath, stat);
144         if (err)
145                 return err;
146 
147         stat->dev = dentry->d_sb->s_dev;
148         stat->ino = dentry->d_inode->i_ino;
149 
150         /*
151          * It's probably not worth it to count subdirs to get the
152          * correct link count.  nlink=1 seems to pacify 'find' and
153          * other utilities.
154          */
155         if (OVL_TYPE_MERGE(type))
156                 stat->nlink = 1;
157 
158         return 0;
159 }
160 
161 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
162                             struct kstat *stat, const char *link,
163                             struct dentry *hardlink)
164 {
165         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
166         struct inode *udir = upperdir->d_inode;
167         struct dentry *newdentry;
168         int err;
169 
170         inode_lock_nested(udir, I_MUTEX_PARENT);
171         newdentry = lookup_one_len(dentry->d_name.name, upperdir,
172                                    dentry->d_name.len);
173         err = PTR_ERR(newdentry);
174         if (IS_ERR(newdentry))
175                 goto out_unlock;
176         err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
177         if (err)
178                 goto out_dput;
179 
180         ovl_dentry_version_inc(dentry->d_parent);
181         ovl_dentry_update(dentry, newdentry);
182         ovl_copyattr(newdentry->d_inode, inode);
183         d_instantiate(dentry, inode);
184         newdentry = NULL;
185 out_dput:
186         dput(newdentry);
187 out_unlock:
188         inode_unlock(udir);
189         return err;
190 }
191 
192 static int ovl_lock_rename_workdir(struct dentry *workdir,
193                                    struct dentry *upperdir)
194 {
195         /* Workdir should not be the same as upperdir */
196         if (workdir == upperdir)
197                 goto err;
198 
199         /* Workdir should not be subdir of upperdir and vice versa */
200         if (lock_rename(workdir, upperdir) != NULL)
201                 goto err_unlock;
202 
203         return 0;
204 
205 err_unlock:
206         unlock_rename(workdir, upperdir);
207 err:
208         pr_err("overlayfs: failed to lock workdir+upperdir\n");
209         return -EIO;
210 }
211 
212 static struct dentry *ovl_clear_empty(struct dentry *dentry,
213                                       struct list_head *list)
214 {
215         struct dentry *workdir = ovl_workdir(dentry);
216         struct inode *wdir = workdir->d_inode;
217         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
218         struct inode *udir = upperdir->d_inode;
219         struct path upperpath;
220         struct dentry *upper;
221         struct dentry *opaquedir;
222         struct kstat stat;
223         int err;
224 
225         if (WARN_ON(!workdir))
226                 return ERR_PTR(-EROFS);
227 
228         err = ovl_lock_rename_workdir(workdir, upperdir);
229         if (err)
230                 goto out;
231 
232         ovl_path_upper(dentry, &upperpath);
233         err = vfs_getattr(&upperpath, &stat);
234         if (err)
235                 goto out_unlock;
236 
237         err = -ESTALE;
238         if (!S_ISDIR(stat.mode))
239                 goto out_unlock;
240         upper = upperpath.dentry;
241         if (upper->d_parent->d_inode != udir)
242                 goto out_unlock;
243 
244         opaquedir = ovl_lookup_temp(workdir, dentry);
245         err = PTR_ERR(opaquedir);
246         if (IS_ERR(opaquedir))
247                 goto out_unlock;
248 
249         err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
250         if (err)
251                 goto out_dput;
252 
253         err = ovl_copy_xattr(upper, opaquedir);
254         if (err)
255                 goto out_cleanup;
256 
257         err = ovl_set_opaque(opaquedir);
258         if (err)
259                 goto out_cleanup;
260 
261         inode_lock(opaquedir->d_inode);
262         err = ovl_set_attr(opaquedir, &stat);
263         inode_unlock(opaquedir->d_inode);
264         if (err)
265                 goto out_cleanup;
266 
267         err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
268         if (err)
269                 goto out_cleanup;
270 
271         ovl_cleanup_whiteouts(upper, list);
272         ovl_cleanup(wdir, upper);
273         unlock_rename(workdir, upperdir);
274 
275         /* dentry's upper doesn't match now, get rid of it */
276         d_drop(dentry);
277 
278         return opaquedir;
279 
280 out_cleanup:
281         ovl_cleanup(wdir, opaquedir);
282 out_dput:
283         dput(opaquedir);
284 out_unlock:
285         unlock_rename(workdir, upperdir);
286 out:
287         return ERR_PTR(err);
288 }
289 
290 static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
291 {
292         int err;
293         struct dentry *ret = NULL;
294         LIST_HEAD(list);
295 
296         err = ovl_check_empty_dir(dentry, &list);
297         if (err)
298                 ret = ERR_PTR(err);
299         else {
300                 /*
301                  * If no upperdentry then skip clearing whiteouts.
302                  *
303                  * Can race with copy-up, since we don't hold the upperdir
304                  * mutex.  Doesn't matter, since copy-up can't create a
305                  * non-empty directory from an empty one.
306                  */
307                 if (ovl_dentry_upper(dentry))
308                         ret = ovl_clear_empty(dentry, &list);
309         }
310 
311         ovl_cache_free(&list);
312 
313         return ret;
314 }
315 
316 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
317                                     struct kstat *stat, const char *link,
318                                     struct dentry *hardlink)
319 {
320         struct dentry *workdir = ovl_workdir(dentry);
321         struct inode *wdir = workdir->d_inode;
322         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
323         struct inode *udir = upperdir->d_inode;
324         struct dentry *upper;
325         struct dentry *newdentry;
326         int err;
327 
328         if (WARN_ON(!workdir))
329                 return -EROFS;
330 
331         err = ovl_lock_rename_workdir(workdir, upperdir);
332         if (err)
333                 goto out;
334 
335         newdentry = ovl_lookup_temp(workdir, dentry);
336         err = PTR_ERR(newdentry);
337         if (IS_ERR(newdentry))
338                 goto out_unlock;
339 
340         upper = lookup_one_len(dentry->d_name.name, upperdir,
341                                dentry->d_name.len);
342         err = PTR_ERR(upper);
343         if (IS_ERR(upper))
344                 goto out_dput;
345 
346         err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
347         if (err)
348                 goto out_dput2;
349 
350         if (S_ISDIR(stat->mode)) {
351                 err = ovl_set_opaque(newdentry);
352                 if (err)
353                         goto out_cleanup;
354 
355                 err = ovl_do_rename(wdir, newdentry, udir, upper,
356                                     RENAME_EXCHANGE);
357                 if (err)
358                         goto out_cleanup;
359 
360                 ovl_cleanup(wdir, upper);
361         } else {
362                 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
363                 if (err)
364                         goto out_cleanup;
365         }
366         ovl_dentry_version_inc(dentry->d_parent);
367         ovl_dentry_update(dentry, newdentry);
368         ovl_copyattr(newdentry->d_inode, inode);
369         d_instantiate(dentry, inode);
370         newdentry = NULL;
371 out_dput2:
372         dput(upper);
373 out_dput:
374         dput(newdentry);
375 out_unlock:
376         unlock_rename(workdir, upperdir);
377 out:
378         return err;
379 
380 out_cleanup:
381         ovl_cleanup(wdir, newdentry);
382         goto out_dput2;
383 }
384 
385 static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev,
386                               const char *link, struct dentry *hardlink)
387 {
388         int err;
389         struct inode *inode;
390         struct kstat stat = {
391                 .mode = mode,
392                 .rdev = rdev,
393         };
394 
395         err = -ENOMEM;
396         inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
397         if (!inode)
398                 goto out;
399 
400         err = ovl_copy_up(dentry->d_parent);
401         if (err)
402                 goto out_iput;
403 
404         if (!ovl_dentry_is_opaque(dentry)) {
405                 err = ovl_create_upper(dentry, inode, &stat, link, hardlink);
406         } else {
407                 const struct cred *old_cred;
408                 struct cred *override_cred;
409 
410                 err = -ENOMEM;
411                 override_cred = prepare_creds();
412                 if (!override_cred)
413                         goto out_iput;
414 
415                 /*
416                  * CAP_SYS_ADMIN for setting opaque xattr
417                  * CAP_DAC_OVERRIDE for create in workdir, rename
418                  * CAP_FOWNER for removing whiteout from sticky dir
419                  */
420                 cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
421                 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
422                 cap_raise(override_cred->cap_effective, CAP_FOWNER);
423                 old_cred = override_creds(override_cred);
424 
425                 err = ovl_create_over_whiteout(dentry, inode, &stat, link,
426                                                hardlink);
427 
428                 revert_creds(old_cred);
429                 put_cred(override_cred);
430         }
431 
432         if (!err)
433                 inode = NULL;
434 out_iput:
435         iput(inode);
436 out:
437         return err;
438 }
439 
440 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
441                              const char *link)
442 {
443         int err;
444 
445         err = ovl_want_write(dentry);
446         if (!err) {
447                 err = ovl_create_or_link(dentry, mode, rdev, link, NULL);
448                 ovl_drop_write(dentry);
449         }
450 
451         return err;
452 }
453 
454 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
455                       bool excl)
456 {
457         return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
458 }
459 
460 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
461 {
462         return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
463 }
464 
465 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
466                      dev_t rdev)
467 {
468         /* Don't allow creation of "whiteout" on overlay */
469         if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
470                 return -EPERM;
471 
472         return ovl_create_object(dentry, mode, rdev, NULL);
473 }
474 
475 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
476                        const char *link)
477 {
478         return ovl_create_object(dentry, S_IFLNK, 0, link);
479 }
480 
481 static int ovl_link(struct dentry *old, struct inode *newdir,
482                     struct dentry *new)
483 {
484         int err;
485         struct dentry *upper;
486 
487         err = ovl_want_write(old);
488         if (err)
489                 goto out;
490 
491         err = ovl_copy_up(old);
492         if (err)
493                 goto out_drop_write;
494 
495         upper = ovl_dentry_upper(old);
496         err = ovl_create_or_link(new, upper->d_inode->i_mode, 0, NULL, upper);
497 
498 out_drop_write:
499         ovl_drop_write(old);
500 out:
501         return err;
502 }
503 
504 static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
505 {
506         struct dentry *workdir = ovl_workdir(dentry);
507         struct inode *wdir = workdir->d_inode;
508         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
509         struct inode *udir = upperdir->d_inode;
510         struct dentry *whiteout;
511         struct dentry *upper;
512         struct dentry *opaquedir = NULL;
513         int err;
514         int flags = 0;
515 
516         if (WARN_ON(!workdir))
517                 return -EROFS;
518 
519         if (is_dir) {
520                 if (OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) {
521                         opaquedir = ovl_check_empty_and_clear(dentry);
522                         err = PTR_ERR(opaquedir);
523                         if (IS_ERR(opaquedir))
524                                 goto out;
525                 } else {
526                         LIST_HEAD(list);
527 
528                         /*
529                          * When removing an empty opaque directory, then it
530                          * makes no sense to replace it with an exact replica of
531                          * itself.  But emptiness still needs to be checked.
532                          */
533                         err = ovl_check_empty_dir(dentry, &list);
534                         ovl_cache_free(&list);
535                         if (err)
536                                 goto out;
537                 }
538         }
539 
540         err = ovl_lock_rename_workdir(workdir, upperdir);
541         if (err)
542                 goto out_dput;
543 
544         upper = lookup_one_len(dentry->d_name.name, upperdir,
545                                dentry->d_name.len);
546         err = PTR_ERR(upper);
547         if (IS_ERR(upper))
548                 goto out_unlock;
549 
550         err = -ESTALE;
551         if ((opaquedir && upper != opaquedir) ||
552             (!opaquedir && ovl_dentry_upper(dentry) &&
553              upper != ovl_dentry_upper(dentry))) {
554                 goto out_dput_upper;
555         }
556 
557         whiteout = ovl_whiteout(workdir, dentry);
558         err = PTR_ERR(whiteout);
559         if (IS_ERR(whiteout))
560                 goto out_dput_upper;
561 
562         if (d_is_dir(upper))
563                 flags = RENAME_EXCHANGE;
564 
565         err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
566         if (err)
567                 goto kill_whiteout;
568         if (flags)
569                 ovl_cleanup(wdir, upper);
570 
571         ovl_dentry_version_inc(dentry->d_parent);
572 out_d_drop:
573         d_drop(dentry);
574         dput(whiteout);
575 out_dput_upper:
576         dput(upper);
577 out_unlock:
578         unlock_rename(workdir, upperdir);
579 out_dput:
580         dput(opaquedir);
581 out:
582         return err;
583 
584 kill_whiteout:
585         ovl_cleanup(wdir, whiteout);
586         goto out_d_drop;
587 }
588 
589 static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
590 {
591         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
592         struct inode *dir = upperdir->d_inode;
593         struct dentry *upper;
594         int err;
595 
596         inode_lock_nested(dir, I_MUTEX_PARENT);
597         upper = lookup_one_len(dentry->d_name.name, upperdir,
598                                dentry->d_name.len);
599         err = PTR_ERR(upper);
600         if (IS_ERR(upper))
601                 goto out_unlock;
602 
603         err = -ESTALE;
604         if (upper == ovl_dentry_upper(dentry)) {
605                 if (is_dir)
606                         err = vfs_rmdir(dir, upper);
607                 else
608                         err = vfs_unlink(dir, upper, NULL);
609                 ovl_dentry_version_inc(dentry->d_parent);
610         }
611         dput(upper);
612 
613         /*
614          * Keeping this dentry hashed would mean having to release
615          * upperpath/lowerpath, which could only be done if we are the
616          * sole user of this dentry.  Too tricky...  Just unhash for
617          * now.
618          */
619         if (!err)
620                 d_drop(dentry);
621 out_unlock:
622         inode_unlock(dir);
623 
624         return err;
625 }
626 
627 static inline int ovl_check_sticky(struct dentry *dentry)
628 {
629         struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
630         struct inode *inode = ovl_dentry_real(dentry)->d_inode;
631 
632         if (check_sticky(dir, inode))
633                 return -EPERM;
634 
635         return 0;
636 }
637 
638 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
639 {
640         enum ovl_path_type type;
641         int err;
642 
643         err = ovl_check_sticky(dentry);
644         if (err)
645                 goto out;
646 
647         err = ovl_want_write(dentry);
648         if (err)
649                 goto out;
650 
651         err = ovl_copy_up(dentry->d_parent);
652         if (err)
653                 goto out_drop_write;
654 
655         type = ovl_path_type(dentry);
656         if (OVL_TYPE_PURE_UPPER(type)) {
657                 err = ovl_remove_upper(dentry, is_dir);
658         } else {
659                 const struct cred *old_cred;
660                 struct cred *override_cred;
661 
662                 err = -ENOMEM;
663                 override_cred = prepare_creds();
664                 if (!override_cred)
665                         goto out_drop_write;
666 
667                 /*
668                  * CAP_SYS_ADMIN for setting xattr on whiteout, opaque dir
669                  * CAP_DAC_OVERRIDE for create in workdir, rename
670                  * CAP_FOWNER for removing whiteout from sticky dir
671                  * CAP_FSETID for chmod of opaque dir
672                  * CAP_CHOWN for chown of opaque dir
673                  */
674                 cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
675                 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
676                 cap_raise(override_cred->cap_effective, CAP_FOWNER);
677                 cap_raise(override_cred->cap_effective, CAP_FSETID);
678                 cap_raise(override_cred->cap_effective, CAP_CHOWN);
679                 old_cred = override_creds(override_cred);
680 
681                 err = ovl_remove_and_whiteout(dentry, is_dir);
682 
683                 revert_creds(old_cred);
684                 put_cred(override_cred);
685         }
686 out_drop_write:
687         ovl_drop_write(dentry);
688 out:
689         return err;
690 }
691 
692 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
693 {
694         return ovl_do_remove(dentry, false);
695 }
696 
697 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
698 {
699         return ovl_do_remove(dentry, true);
700 }
701 
702 static int ovl_rename2(struct inode *olddir, struct dentry *old,
703                        struct inode *newdir, struct dentry *new,
704                        unsigned int flags)
705 {
706         int err;
707         enum ovl_path_type old_type;
708         enum ovl_path_type new_type;
709         struct dentry *old_upperdir;
710         struct dentry *new_upperdir;
711         struct dentry *olddentry;
712         struct dentry *newdentry;
713         struct dentry *trap;
714         bool old_opaque;
715         bool new_opaque;
716         bool cleanup_whiteout = false;
717         bool overwrite = !(flags & RENAME_EXCHANGE);
718         bool is_dir = d_is_dir(old);
719         bool new_is_dir = false;
720         struct dentry *opaquedir = NULL;
721         const struct cred *old_cred = NULL;
722         struct cred *override_cred = NULL;
723 
724         err = -EINVAL;
725         if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
726                 goto out;
727 
728         flags &= ~RENAME_NOREPLACE;
729 
730         err = ovl_check_sticky(old);
731         if (err)
732                 goto out;
733 
734         /* Don't copy up directory trees */
735         old_type = ovl_path_type(old);
736         err = -EXDEV;
737         if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
738                 goto out;
739 
740         if (new->d_inode) {
741                 err = ovl_check_sticky(new);
742                 if (err)
743                         goto out;
744 
745                 if (d_is_dir(new))
746                         new_is_dir = true;
747 
748                 new_type = ovl_path_type(new);
749                 err = -EXDEV;
750                 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
751                         goto out;
752 
753                 err = 0;
754                 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
755                         if (ovl_dentry_lower(old)->d_inode ==
756                             ovl_dentry_lower(new)->d_inode)
757                                 goto out;
758                 }
759                 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
760                         if (ovl_dentry_upper(old)->d_inode ==
761                             ovl_dentry_upper(new)->d_inode)
762                                 goto out;
763                 }
764         } else {
765                 if (ovl_dentry_is_opaque(new))
766                         new_type = __OVL_PATH_UPPER;
767                 else
768                         new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
769         }
770 
771         err = ovl_want_write(old);
772         if (err)
773                 goto out;
774 
775         err = ovl_copy_up(old);
776         if (err)
777                 goto out_drop_write;
778 
779         err = ovl_copy_up(new->d_parent);
780         if (err)
781                 goto out_drop_write;
782         if (!overwrite) {
783                 err = ovl_copy_up(new);
784                 if (err)
785                         goto out_drop_write;
786         }
787 
788         old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
789         new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
790 
791         if (old_opaque || new_opaque) {
792                 err = -ENOMEM;
793                 override_cred = prepare_creds();
794                 if (!override_cred)
795                         goto out_drop_write;
796 
797                 /*
798                  * CAP_SYS_ADMIN for setting xattr on whiteout, opaque dir
799                  * CAP_DAC_OVERRIDE for create in workdir
800                  * CAP_FOWNER for removing whiteout from sticky dir
801                  * CAP_FSETID for chmod of opaque dir
802                  * CAP_CHOWN for chown of opaque dir
803                  */
804                 cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
805                 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
806                 cap_raise(override_cred->cap_effective, CAP_FOWNER);
807                 cap_raise(override_cred->cap_effective, CAP_FSETID);
808                 cap_raise(override_cred->cap_effective, CAP_CHOWN);
809                 old_cred = override_creds(override_cred);
810         }
811 
812         if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
813                 opaquedir = ovl_check_empty_and_clear(new);
814                 err = PTR_ERR(opaquedir);
815                 if (IS_ERR(opaquedir)) {
816                         opaquedir = NULL;
817                         goto out_revert_creds;
818                 }
819         }
820 
821         if (overwrite) {
822                 if (old_opaque) {
823                         if (new->d_inode || !new_opaque) {
824                                 /* Whiteout source */
825                                 flags |= RENAME_WHITEOUT;
826                         } else {
827                                 /* Switch whiteouts */
828                                 flags |= RENAME_EXCHANGE;
829                         }
830                 } else if (is_dir && !new->d_inode && new_opaque) {
831                         flags |= RENAME_EXCHANGE;
832                         cleanup_whiteout = true;
833                 }
834         }
835 
836         old_upperdir = ovl_dentry_upper(old->d_parent);
837         new_upperdir = ovl_dentry_upper(new->d_parent);
838 
839         trap = lock_rename(new_upperdir, old_upperdir);
840 
841 
842         olddentry = lookup_one_len(old->d_name.name, old_upperdir,
843                                    old->d_name.len);
844         err = PTR_ERR(olddentry);
845         if (IS_ERR(olddentry))
846                 goto out_unlock;
847 
848         err = -ESTALE;
849         if (olddentry != ovl_dentry_upper(old))
850                 goto out_dput_old;
851 
852         newdentry = lookup_one_len(new->d_name.name, new_upperdir,
853                                    new->d_name.len);
854         err = PTR_ERR(newdentry);
855         if (IS_ERR(newdentry))
856                 goto out_dput_old;
857 
858         err = -ESTALE;
859         if (ovl_dentry_upper(new)) {
860                 if (opaquedir) {
861                         if (newdentry != opaquedir)
862                                 goto out_dput;
863                 } else {
864                         if (newdentry != ovl_dentry_upper(new))
865                                 goto out_dput;
866                 }
867         } else {
868                 if (!d_is_negative(newdentry) &&
869                     (!new_opaque || !ovl_is_whiteout(newdentry)))
870                         goto out_dput;
871         }
872 
873         if (olddentry == trap)
874                 goto out_dput;
875         if (newdentry == trap)
876                 goto out_dput;
877 
878         if (is_dir && !old_opaque && new_opaque) {
879                 err = ovl_set_opaque(olddentry);
880                 if (err)
881                         goto out_dput;
882         }
883         if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
884                 err = ovl_set_opaque(newdentry);
885                 if (err)
886                         goto out_dput;
887         }
888 
889         if (old_opaque || new_opaque) {
890                 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
891                                     new_upperdir->d_inode, newdentry,
892                                     flags);
893         } else {
894                 /* No debug for the plain case */
895                 BUG_ON(flags & ~RENAME_EXCHANGE);
896                 err = vfs_rename(old_upperdir->d_inode, olddentry,
897                                  new_upperdir->d_inode, newdentry,
898                                  NULL, flags);
899         }
900 
901         if (err) {
902                 if (is_dir && !old_opaque && new_opaque)
903                         ovl_remove_opaque(olddentry);
904                 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
905                         ovl_remove_opaque(newdentry);
906                 goto out_dput;
907         }
908 
909         if (is_dir && old_opaque && !new_opaque)
910                 ovl_remove_opaque(olddentry);
911         if (!overwrite && new_is_dir && !old_opaque && new_opaque)
912                 ovl_remove_opaque(newdentry);
913 
914         /*
915          * Old dentry now lives in different location. Dentries in
916          * lowerstack are stale. We cannot drop them here because
917          * access to them is lockless. This could be only pure upper
918          * or opaque directory - numlower is zero. Or upper non-dir
919          * entry - its pureness is tracked by flag opaque.
920          */
921         if (old_opaque != new_opaque) {
922                 ovl_dentry_set_opaque(old, new_opaque);
923                 if (!overwrite)
924                         ovl_dentry_set_opaque(new, old_opaque);
925         }
926 
927         if (cleanup_whiteout)
928                 ovl_cleanup(old_upperdir->d_inode, newdentry);
929 
930         ovl_dentry_version_inc(old->d_parent);
931         ovl_dentry_version_inc(new->d_parent);
932 
933 out_dput:
934         dput(newdentry);
935 out_dput_old:
936         dput(olddentry);
937 out_unlock:
938         unlock_rename(new_upperdir, old_upperdir);
939 out_revert_creds:
940         if (old_opaque || new_opaque) {
941                 revert_creds(old_cred);
942                 put_cred(override_cred);
943         }
944 out_drop_write:
945         ovl_drop_write(old);
946 out:
947         dput(opaquedir);
948         return err;
949 }
950 
951 const struct inode_operations ovl_dir_inode_operations = {
952         .lookup         = ovl_lookup,
953         .mkdir          = ovl_mkdir,
954         .symlink        = ovl_symlink,
955         .unlink         = ovl_unlink,
956         .rmdir          = ovl_rmdir,
957         .rename2        = ovl_rename2,
958         .link           = ovl_link,
959         .setattr        = ovl_setattr,
960         .create         = ovl_create,
961         .mknod          = ovl_mknod,
962         .permission     = ovl_permission,
963         .getattr        = ovl_dir_getattr,
964         .setxattr       = ovl_setxattr,
965         .getxattr       = ovl_getxattr,
966         .listxattr      = ovl_listxattr,
967         .removexattr    = ovl_removexattr,
968 };
969 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp