at v4.11 296 lines 6.5 kB view raw
1/* 2 * Copyright (C) 2011 Novell Inc. 3 * Copyright (C) 2016 Red Hat, 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/mount.h> 12#include <linux/slab.h> 13#include <linux/cred.h> 14#include <linux/xattr.h> 15#include "overlayfs.h" 16#include "ovl_entry.h" 17 18int ovl_want_write(struct dentry *dentry) 19{ 20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 21 return mnt_want_write(ofs->upper_mnt); 22} 23 24void ovl_drop_write(struct dentry *dentry) 25{ 26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 27 mnt_drop_write(ofs->upper_mnt); 28} 29 30struct dentry *ovl_workdir(struct dentry *dentry) 31{ 32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 33 return ofs->workdir; 34} 35 36const struct cred *ovl_override_creds(struct super_block *sb) 37{ 38 struct ovl_fs *ofs = sb->s_fs_info; 39 40 return override_creds(ofs->creator_cred); 41} 42 43struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 44{ 45 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 46 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 47 48 if (oe) 49 oe->numlower = numlower; 50 51 return oe; 52} 53 54bool ovl_dentry_remote(struct dentry *dentry) 55{ 56 return dentry->d_flags & 57 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | 58 DCACHE_OP_REAL); 59} 60 61bool ovl_dentry_weird(struct dentry *dentry) 62{ 63 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 64 DCACHE_MANAGE_TRANSIT | 65 DCACHE_OP_HASH | 66 DCACHE_OP_COMPARE); 67} 68 69enum ovl_path_type ovl_path_type(struct dentry *dentry) 70{ 71 struct ovl_entry *oe = dentry->d_fsdata; 72 enum ovl_path_type type = 0; 73 74 if (oe->__upperdentry) { 75 type = __OVL_PATH_UPPER; 76 77 /* 78 * Non-dir dentry can hold lower dentry from previous 79 * location. 80 */ 81 if (oe->numlower && d_is_dir(dentry)) 82 type |= __OVL_PATH_MERGE; 83 } else { 84 if (oe->numlower > 1) 85 type |= __OVL_PATH_MERGE; 86 } 87 return type; 88} 89 90void ovl_path_upper(struct dentry *dentry, struct path *path) 91{ 92 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 93 struct ovl_entry *oe = dentry->d_fsdata; 94 95 path->mnt = ofs->upper_mnt; 96 path->dentry = ovl_upperdentry_dereference(oe); 97} 98 99void ovl_path_lower(struct dentry *dentry, struct path *path) 100{ 101 struct ovl_entry *oe = dentry->d_fsdata; 102 103 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; 104} 105 106enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 107{ 108 enum ovl_path_type type = ovl_path_type(dentry); 109 110 if (!OVL_TYPE_UPPER(type)) 111 ovl_path_lower(dentry, path); 112 else 113 ovl_path_upper(dentry, path); 114 115 return type; 116} 117 118struct dentry *ovl_dentry_upper(struct dentry *dentry) 119{ 120 struct ovl_entry *oe = dentry->d_fsdata; 121 122 return ovl_upperdentry_dereference(oe); 123} 124 125static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) 126{ 127 return oe->numlower ? oe->lowerstack[0].dentry : NULL; 128} 129 130struct dentry *ovl_dentry_lower(struct dentry *dentry) 131{ 132 struct ovl_entry *oe = dentry->d_fsdata; 133 134 return __ovl_dentry_lower(oe); 135} 136 137struct dentry *ovl_dentry_real(struct dentry *dentry) 138{ 139 struct ovl_entry *oe = dentry->d_fsdata; 140 struct dentry *realdentry; 141 142 realdentry = ovl_upperdentry_dereference(oe); 143 if (!realdentry) 144 realdentry = __ovl_dentry_lower(oe); 145 146 return realdentry; 147} 148 149struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) 150{ 151 struct ovl_entry *oe = dentry->d_fsdata; 152 153 return oe->cache; 154} 155 156void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) 157{ 158 struct ovl_entry *oe = dentry->d_fsdata; 159 160 oe->cache = cache; 161} 162 163bool ovl_dentry_is_opaque(struct dentry *dentry) 164{ 165 struct ovl_entry *oe = dentry->d_fsdata; 166 return oe->opaque; 167} 168 169bool ovl_dentry_is_whiteout(struct dentry *dentry) 170{ 171 return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 172} 173 174void ovl_dentry_set_opaque(struct dentry *dentry) 175{ 176 struct ovl_entry *oe = dentry->d_fsdata; 177 178 oe->opaque = true; 179} 180 181bool ovl_redirect_dir(struct super_block *sb) 182{ 183 struct ovl_fs *ofs = sb->s_fs_info; 184 185 return ofs->config.redirect_dir; 186} 187 188void ovl_clear_redirect_dir(struct super_block *sb) 189{ 190 struct ovl_fs *ofs = sb->s_fs_info; 191 192 ofs->config.redirect_dir = false; 193} 194 195const char *ovl_dentry_get_redirect(struct dentry *dentry) 196{ 197 struct ovl_entry *oe = dentry->d_fsdata; 198 199 return oe->redirect; 200} 201 202void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 203{ 204 struct ovl_entry *oe = dentry->d_fsdata; 205 206 kfree(oe->redirect); 207 oe->redirect = redirect; 208} 209 210void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) 211{ 212 struct ovl_entry *oe = dentry->d_fsdata; 213 214 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode)); 215 WARN_ON(oe->__upperdentry); 216 /* 217 * Make sure upperdentry is consistent before making it visible to 218 * ovl_upperdentry_dereference(). 219 */ 220 smp_wmb(); 221 oe->__upperdentry = upperdentry; 222} 223 224void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper) 225{ 226 WRITE_ONCE(inode->i_private, (unsigned long) realinode | 227 (is_upper ? OVL_ISUPPER_MASK : 0)); 228} 229 230void ovl_inode_update(struct inode *inode, struct inode *upperinode) 231{ 232 WARN_ON(!upperinode); 233 WARN_ON(!inode_unhashed(inode)); 234 WRITE_ONCE(inode->i_private, 235 (unsigned long) upperinode | OVL_ISUPPER_MASK); 236 if (!S_ISDIR(upperinode->i_mode)) 237 __insert_inode_hash(inode, (unsigned long) upperinode); 238} 239 240void ovl_dentry_version_inc(struct dentry *dentry) 241{ 242 struct ovl_entry *oe = dentry->d_fsdata; 243 244 WARN_ON(!inode_is_locked(dentry->d_inode)); 245 oe->version++; 246} 247 248u64 ovl_dentry_version_get(struct dentry *dentry) 249{ 250 struct ovl_entry *oe = dentry->d_fsdata; 251 252 WARN_ON(!inode_is_locked(dentry->d_inode)); 253 return oe->version; 254} 255 256bool ovl_is_whiteout(struct dentry *dentry) 257{ 258 struct inode *inode = dentry->d_inode; 259 260 return inode && IS_WHITEOUT(inode); 261} 262 263struct file *ovl_path_open(struct path *path, int flags) 264{ 265 return dentry_open(path, flags | O_NOATIME, current_cred()); 266} 267 268int ovl_copy_up_start(struct dentry *dentry) 269{ 270 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 271 struct ovl_entry *oe = dentry->d_fsdata; 272 int err; 273 274 spin_lock(&ofs->copyup_wq.lock); 275 err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying); 276 if (!err) { 277 if (oe->__upperdentry) 278 err = 1; /* Already copied up */ 279 else 280 oe->copying = true; 281 } 282 spin_unlock(&ofs->copyup_wq.lock); 283 284 return err; 285} 286 287void ovl_copy_up_end(struct dentry *dentry) 288{ 289 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 290 struct ovl_entry *oe = dentry->d_fsdata; 291 292 spin_lock(&ofs->copyup_wq.lock); 293 oe->copying = false; 294 wake_up_locked(&ofs->copyup_wq); 295 spin_unlock(&ofs->copyup_wq.lock); 296}