1
0

051-0005-ovl-proper-cleanup-of-workdir.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. From eea2fb4851e9dcbab6b991aaf47e2e024f1f55a0 Mon Sep 17 00:00:00 2001
  2. From: Miklos Szeredi <mszeredi@redhat.com>
  3. Date: Thu, 1 Sep 2016 11:11:59 +0200
  4. Subject: [PATCH] ovl: proper cleanup of workdir
  5. When mounting overlayfs it needs a clean "work" directory under the
  6. supplied workdir.
  7. Previously the mount code removed this directory if it already existed and
  8. created a new one. If the removal failed (e.g. directory was not empty)
  9. then it fell back to a read-only mount not using the workdir.
  10. While this has never been reported, it is possible to get a non-empty
  11. "work" dir from a previous mount of overlayfs in case of crash in the
  12. middle of an operation using the work directory.
  13. In this case the left over state should be discarded and the overlay
  14. filesystem will be consistent, guaranteed by the atomicity of operations on
  15. moving to/from the workdir to the upper layer.
  16. This patch implements cleaning out any files left in workdir. It is
  17. implemented using real recursion for simplicity, but the depth is limited
  18. to 2, because the worst case is that of a directory containing whiteouts
  19. under "work".
  20. Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
  21. Cc: <stable@vger.kernel.org>
  22. ---
  23. fs/overlayfs/overlayfs.h | 2 ++
  24. fs/overlayfs/readdir.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++-
  25. fs/overlayfs/super.c | 2 +-
  26. 3 files changed, 65 insertions(+), 2 deletions(-)
  27. --- a/fs/overlayfs/overlayfs.h
  28. +++ b/fs/overlayfs/overlayfs.h
  29. @@ -164,6 +164,8 @@ extern const struct file_operations ovl_
  30. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
  31. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
  32. void ovl_cache_free(struct list_head *list);
  33. +void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
  34. + struct dentry *dentry, int level);
  35. /* inode.c */
  36. int ovl_setattr(struct dentry *dentry, struct iattr *attr);
  37. --- a/fs/overlayfs/readdir.c
  38. +++ b/fs/overlayfs/readdir.c
  39. @@ -247,7 +247,7 @@ static inline int ovl_dir_read(struct pa
  40. err = rdd->err;
  41. } while (!err && rdd->count);
  42. - if (!err && rdd->first_maybe_whiteout)
  43. + if (!err && rdd->first_maybe_whiteout && rdd->dentry)
  44. err = ovl_check_whiteouts(realpath->dentry, rdd);
  45. fput(realfile);
  46. @@ -573,3 +573,64 @@ void ovl_cleanup_whiteouts(struct dentry
  47. }
  48. mutex_unlock(&upper->d_inode->i_mutex);
  49. }
  50. +
  51. +static void ovl_workdir_cleanup_recurse(struct path *path, int level)
  52. +{
  53. + int err;
  54. + struct inode *dir = path->dentry->d_inode;
  55. + LIST_HEAD(list);
  56. + struct ovl_cache_entry *p;
  57. + struct ovl_readdir_data rdd = {
  58. + .ctx.actor = ovl_fill_merge,
  59. + .dentry = NULL,
  60. + .list = &list,
  61. + .root = RB_ROOT,
  62. + .is_lowest = false,
  63. + };
  64. +
  65. + err = ovl_dir_read(path, &rdd);
  66. + if (err)
  67. + goto out;
  68. +
  69. + mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  70. + list_for_each_entry(p, &list, l_node) {
  71. + struct dentry *dentry;
  72. +
  73. + if (p->name[0] == '.') {
  74. + if (p->len == 1)
  75. + continue;
  76. + if (p->len == 2 && p->name[1] == '.')
  77. + continue;
  78. + }
  79. + dentry = lookup_one_len(p->name, path->dentry, p->len);
  80. + if (IS_ERR(dentry))
  81. + continue;
  82. + if (dentry->d_inode)
  83. + ovl_workdir_cleanup(dir, path->mnt, dentry, level);
  84. + dput(dentry);
  85. + }
  86. + mutex_unlock(&dir->i_mutex);
  87. +out:
  88. + ovl_cache_free(&list);
  89. +}
  90. +
  91. +void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
  92. + struct dentry *dentry, int level)
  93. +{
  94. + int err;
  95. +
  96. + if (!d_is_dir(dentry) || level > 1) {
  97. + ovl_cleanup(dir, dentry);
  98. + return;
  99. + }
  100. +
  101. + err = ovl_do_rmdir(dir, dentry);
  102. + if (err) {
  103. + struct path path = { .mnt = mnt, .dentry = dentry };
  104. +
  105. + mutex_unlock(&dir->i_mutex);
  106. + ovl_workdir_cleanup_recurse(&path, level + 1);
  107. + mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  108. + ovl_cleanup(dir, dentry);
  109. + }
  110. +}
  111. --- a/fs/overlayfs/super.c
  112. +++ b/fs/overlayfs/super.c
  113. @@ -784,7 +784,7 @@ retry:
  114. goto out_dput;
  115. retried = true;
  116. - ovl_cleanup(dir, work);
  117. + ovl_workdir_cleanup(dir, mnt, work, 0);
  118. dput(work);
  119. goto retry;
  120. }