Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e9be9d5e MS |
2 | /* |
3 | * | |
4 | * Copyright (C) 2011 Novell Inc. | |
e9be9d5e MS |
5 | */ |
6 | ||
7 | #include <linux/fs.h> | |
8 | #include <linux/slab.h> | |
5b825c3a | 9 | #include <linux/cred.h> |
e9be9d5e | 10 | #include <linux/xattr.h> |
5201dc44 | 11 | #include <linux/posix_acl.h> |
5f8415d6 | 12 | #include <linux/ratelimit.h> |
10c5db28 | 13 | #include <linux/fiemap.h> |
66dbfabf MS |
14 | #include <linux/fileattr.h> |
15 | #include <linux/security.h> | |
332f606b | 16 | #include <linux/namei.h> |
6c0a8bfb CB |
17 | #include <linux/posix_acl.h> |
18 | #include <linux/posix_acl_xattr.h> | |
e9be9d5e MS |
19 | #include "overlayfs.h" |
20 | ||
ba1e563c | 21 | |
c1632a0f | 22 | int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 23 | struct iattr *attr) |
e9be9d5e MS |
24 | { |
25 | int err; | |
a15506ea | 26 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); |
997336f2 | 27 | bool full_copy_up = false; |
e9be9d5e | 28 | struct dentry *upperdentry; |
1175b6b8 | 29 | const struct cred *old_cred; |
e9be9d5e | 30 | |
c1632a0f | 31 | err = setattr_prepare(&nop_mnt_idmap, dentry, attr); |
cf9a6784 MS |
32 | if (err) |
33 | return err; | |
34 | ||
5812160e | 35 | if (attr->ia_valid & ATTR_SIZE) { |
997336f2 VG |
36 | /* Truncate should trigger data copy up as well */ |
37 | full_copy_up = true; | |
5812160e MS |
38 | } |
39 | ||
997336f2 VG |
40 | if (!full_copy_up) |
41 | err = ovl_copy_up(dentry); | |
42 | else | |
43 | err = ovl_copy_up_with_data(dentry); | |
acff81ec | 44 | if (!err) { |
5812160e MS |
45 | struct inode *winode = NULL; |
46 | ||
acff81ec MS |
47 | upperdentry = ovl_dentry_upper(dentry); |
48 | ||
5812160e MS |
49 | if (attr->ia_valid & ATTR_SIZE) { |
50 | winode = d_inode(upperdentry); | |
51 | err = get_write_access(winode); | |
52 | if (err) | |
162d0644 | 53 | goto out; |
5812160e MS |
54 | } |
55 | ||
b99c2d91 MS |
56 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
57 | attr->ia_valid &= ~ATTR_MODE; | |
58 | ||
e67f0216 | 59 | /* |
15fd2ea9 VG |
60 | * We might have to translate ovl file into real file object |
61 | * once use cases emerge. For now, simply don't let underlying | |
62 | * filesystem rely on attr->ia_file | |
e67f0216 VG |
63 | */ |
64 | attr->ia_valid &= ~ATTR_FILE; | |
65 | ||
15fd2ea9 VG |
66 | /* |
67 | * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN | |
68 | * set. Overlayfs does not pass O_TRUNC flag to underlying | |
69 | * filesystem during open -> do not pass ATTR_OPEN. This | |
70 | * disables optimization in fuse which assumes open(O_TRUNC) | |
71 | * already set file size to 0. But we never passed O_TRUNC to | |
72 | * fuse. So by clearing ATTR_OPEN, fuse will be forced to send | |
73 | * setattr request to server. | |
74 | */ | |
75 | attr->ia_valid &= ~ATTR_OPEN; | |
76 | ||
162d0644 AG |
77 | err = ovl_want_write(dentry); |
78 | if (err) | |
79 | goto out_put_write; | |
80 | ||
5955102c | 81 | inode_lock(upperdentry->d_inode); |
1175b6b8 | 82 | old_cred = ovl_override_creds(dentry->d_sb); |
a15506ea | 83 | err = ovl_do_notify_change(ofs, upperdentry, attr); |
1175b6b8 | 84 | revert_creds(old_cred); |
b81de061 | 85 | if (!err) |
2878dffc | 86 | ovl_copyattr(dentry->d_inode); |
5955102c | 87 | inode_unlock(upperdentry->d_inode); |
162d0644 | 88 | ovl_drop_write(dentry); |
5812160e | 89 | |
162d0644 | 90 | out_put_write: |
5812160e MS |
91 | if (winode) |
92 | put_write_access(winode); | |
e9be9d5e | 93 | } |
e9be9d5e MS |
94 | out: |
95 | return err; | |
96 | } | |
97 | ||
c68e7ec5 | 98 | static void ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid) |
da309e8c | 99 | { |
dcb399de AG |
100 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); |
101 | bool samefs = ovl_same_fs(ofs); | |
102 | unsigned int xinobits = ovl_xino_bits(ofs); | |
dfe51d47 | 103 | unsigned int xinoshift = 64 - xinobits; |
da309e8c AG |
104 | |
105 | if (samefs) { | |
106 | /* | |
107 | * When all layers are on the same fs, all real inode | |
108 | * number are unique, so we use the overlay st_dev, | |
109 | * which is friendly to du -x. | |
110 | */ | |
111 | stat->dev = dentry->d_sb->s_dev; | |
c68e7ec5 | 112 | return; |
e487d889 | 113 | } else if (xinobits) { |
e487d889 AG |
114 | /* |
115 | * All inode numbers of underlying fs should not be using the | |
116 | * high xinobits, so we use high xinobits to partition the | |
117 | * overlay st_ino address space. The high bits holds the fsid | |
dfe51d47 | 118 | * (upper fsid is 0). The lowest xinobit is reserved for mapping |
f48bbfb2 | 119 | * the non-persistent inode numbers range in case of overflow. |
dfe51d47 AG |
120 | * This way all overlay inode numbers are unique and use the |
121 | * overlay st_dev. | |
e487d889 | 122 | */ |
926e94d7 | 123 | if (likely(!(stat->ino >> xinoshift))) { |
dfe51d47 | 124 | stat->ino |= ((u64)fsid) << (xinoshift + 1); |
e487d889 | 125 | stat->dev = dentry->d_sb->s_dev; |
c68e7ec5 | 126 | return; |
dcb399de | 127 | } else if (ovl_xino_warn(ofs)) { |
926e94d7 AG |
128 | pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n", |
129 | dentry, stat->ino, xinobits); | |
e487d889 AG |
130 | } |
131 | } | |
132 | ||
133 | /* The inode could not be mapped to a unified st_ino address space */ | |
134 | if (S_ISDIR(dentry->d_inode->i_mode)) { | |
da309e8c AG |
135 | /* |
136 | * Always use the overlay st_dev for directories, so 'find | |
137 | * -xdev' will scan the entire overlay mount and won't cross the | |
138 | * overlay mount boundaries. | |
139 | * | |
140 | * If not all layers are on the same fs the pair {real st_ino; | |
141 | * overlay st_dev} is not unique, so use the non persistent | |
142 | * overlay st_ino for directories. | |
143 | */ | |
144 | stat->dev = dentry->d_sb->s_dev; | |
145 | stat->ino = dentry->d_inode->i_ino; | |
b7bf9908 | 146 | } else { |
da309e8c AG |
147 | /* |
148 | * For non-samefs setup, if we cannot map all layers st_ino | |
149 | * to a unified address space, we need to make sure that st_dev | |
b7bf9908 AG |
150 | * is unique per underlying fs, so we use the unique anonymous |
151 | * bdev assigned to the underlying fs. | |
da309e8c | 152 | */ |
dcb399de | 153 | stat->dev = ofs->fs[fsid].pseudo_dev; |
da309e8c | 154 | } |
da309e8c AG |
155 | } |
156 | ||
b74d24f7 | 157 | int ovl_getattr(struct mnt_idmap *idmap, const struct path *path, |
549c7297 | 158 | struct kstat *stat, u32 request_mask, unsigned int flags) |
e9be9d5e | 159 | { |
a528d35e | 160 | struct dentry *dentry = path->dentry; |
72b608f0 | 161 | enum ovl_path_type type; |
e9be9d5e | 162 | struct path realpath; |
1175b6b8 | 163 | const struct cred *old_cred; |
096a218a AG |
164 | struct inode *inode = d_inode(dentry); |
165 | bool is_dir = S_ISDIR(inode->i_mode); | |
07f1e596 | 166 | int fsid = 0; |
1175b6b8 | 167 | int err; |
67d756c2 VG |
168 | bool metacopy_blocks = false; |
169 | ||
170 | metacopy_blocks = ovl_is_metacopy_dentry(dentry); | |
e9be9d5e | 171 | |
72b608f0 | 172 | type = ovl_path_real(dentry, &realpath); |
1175b6b8 | 173 | old_cred = ovl_override_creds(dentry->d_sb); |
a528d35e | 174 | err = vfs_getattr(&realpath, stat, request_mask, flags); |
72b608f0 AG |
175 | if (err) |
176 | goto out; | |
177 | ||
096a218a AG |
178 | /* Report the effective immutable/append-only STATX flags */ |
179 | generic_fill_statx_attr(inode, stat); | |
180 | ||
72b608f0 | 181 | /* |
da309e8c AG |
182 | * For non-dir or same fs, we use st_ino of the copy up origin. |
183 | * This guaranties constant st_dev/st_ino across copy up. | |
e487d889 AG |
184 | * With xino feature and non-samefs, we use st_ino of the copy up |
185 | * origin masked with high bits that represent the layer id. | |
72b608f0 | 186 | * |
da309e8c | 187 | * If lower filesystem supports NFS file handles, this also guaranties |
72b608f0 AG |
188 | * persistent st_ino across mount cycle. |
189 | */ | |
dcb399de | 190 | if (!is_dir || ovl_same_dev(OVL_FS(dentry->d_sb))) { |
da309e8c | 191 | if (!OVL_TYPE_UPPER(type)) { |
07f1e596 | 192 | fsid = ovl_layer_lower(dentry)->fsid; |
da309e8c | 193 | } else if (OVL_TYPE_ORIGIN(type)) { |
72b608f0 | 194 | struct kstat lowerstat; |
67d756c2 VG |
195 | u32 lowermask = STATX_INO | STATX_BLOCKS | |
196 | (!is_dir ? STATX_NLINK : 0); | |
72b608f0 AG |
197 | |
198 | ovl_path_lower(dentry, &realpath); | |
199 | err = vfs_getattr(&realpath, &lowerstat, | |
5b712091 | 200 | lowermask, flags); |
72b608f0 AG |
201 | if (err) |
202 | goto out; | |
203 | ||
72b608f0 | 204 | /* |
359f392c | 205 | * Lower hardlinks may be broken on copy up to different |
72b608f0 AG |
206 | * upper files, so we cannot use the lower origin st_ino |
207 | * for those different files, even for the same fs case. | |
86eaa130 AG |
208 | * |
209 | * Similarly, several redirected dirs can point to the | |
210 | * same dir on a lower layer. With the "verify_lower" | |
211 | * feature, we do not use the lower origin st_ino, if | |
212 | * we haven't verified that this redirect is unique. | |
213 | * | |
359f392c | 214 | * With inodes index enabled, it is safe to use st_ino |
86eaa130 AG |
215 | * of an indexed origin. The index validates that the |
216 | * upper hardlink is not broken and that a redirected | |
217 | * dir is the only redirect to that origin. | |
72b608f0 | 218 | */ |
86eaa130 AG |
219 | if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || |
220 | (!ovl_verify_lower(dentry->d_sb) && | |
9f99e50d | 221 | (is_dir || lowerstat.nlink == 1))) { |
07f1e596 | 222 | fsid = ovl_layer_lower(dentry)->fsid; |
b7bf9908 | 223 | stat->ino = lowerstat.ino; |
9f99e50d | 224 | } |
67d756c2 VG |
225 | |
226 | /* | |
227 | * If we are querying a metacopy dentry and lower | |
228 | * dentry is data dentry, then use the blocks we | |
229 | * queried just now. We don't have to do additional | |
230 | * vfs_getattr(). If lower itself is metacopy, then | |
231 | * additional vfs_getattr() is unavoidable. | |
232 | */ | |
233 | if (metacopy_blocks && | |
234 | realpath.dentry == ovl_dentry_lowerdata(dentry)) { | |
235 | stat->blocks = lowerstat.blocks; | |
236 | metacopy_blocks = false; | |
237 | } | |
238 | } | |
239 | ||
240 | if (metacopy_blocks) { | |
241 | /* | |
242 | * If lower is not same as lowerdata or if there was | |
243 | * no origin on upper, we can end up here. | |
41665644 AG |
244 | * With lazy lowerdata lookup, guess lowerdata blocks |
245 | * from size to avoid lowerdata lookup on stat(2). | |
67d756c2 VG |
246 | */ |
247 | struct kstat lowerdatastat; | |
248 | u32 lowermask = STATX_BLOCKS; | |
249 | ||
250 | ovl_path_lowerdata(dentry, &realpath); | |
41665644 AG |
251 | if (realpath.dentry) { |
252 | err = vfs_getattr(&realpath, &lowerdatastat, | |
253 | lowermask, flags); | |
254 | if (err) | |
255 | goto out; | |
256 | } else { | |
257 | lowerdatastat.blocks = | |
258 | round_up(stat->size, stat->blksize) >> 9; | |
259 | } | |
67d756c2 | 260 | stat->blocks = lowerdatastat.blocks; |
72b608f0 | 261 | } |
72b608f0 | 262 | } |
5b712091 | 263 | |
c68e7ec5 | 264 | ovl_map_dev_ino(dentry, stat, fsid); |
da309e8c | 265 | |
5b712091 MS |
266 | /* |
267 | * It's probably not worth it to count subdirs to get the | |
268 | * correct link count. nlink=1 seems to pacify 'find' and | |
269 | * other utilities. | |
270 | */ | |
271 | if (is_dir && OVL_TYPE_MERGE(type)) | |
272 | stat->nlink = 1; | |
273 | ||
5f8415d6 AG |
274 | /* |
275 | * Return the overlay inode nlinks for indexed upper inodes. | |
276 | * Overlay inode nlink counts the union of the upper hardlinks | |
277 | * and non-covered lower hardlinks. It does not include the upper | |
278 | * index hardlink. | |
279 | */ | |
280 | if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) | |
281 | stat->nlink = dentry->d_inode->i_nlink; | |
282 | ||
72b608f0 | 283 | out: |
1175b6b8 | 284 | revert_creds(old_cred); |
72b608f0 | 285 | |
1175b6b8 | 286 | return err; |
e9be9d5e MS |
287 | } |
288 | ||
4609e1f1 | 289 | int ovl_permission(struct mnt_idmap *idmap, |
549c7297 | 290 | struct inode *inode, int mask) |
e9be9d5e | 291 | { |
09d8b586 | 292 | struct inode *upperinode = ovl_inode_upper(inode); |
4b7791b2 CB |
293 | struct inode *realinode; |
294 | struct path realpath; | |
c0ca3d70 | 295 | const struct cred *old_cred; |
e9be9d5e MS |
296 | int err; |
297 | ||
e9be9d5e | 298 | /* Careful in RCU walk mode */ |
1a73f5b8 ZC |
299 | realinode = ovl_i_path_real(inode, &realpath); |
300 | if (!realinode) { | |
e9be9d5e | 301 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
a999d7e1 | 302 | return -ECHILD; |
e9be9d5e MS |
303 | } |
304 | ||
c0ca3d70 VG |
305 | /* |
306 | * Check overlay inode with the creds of task and underlying inode | |
307 | * with creds of mounter | |
308 | */ | |
4609e1f1 | 309 | err = generic_permission(&nop_mnt_idmap, inode, mask); |
c0ca3d70 VG |
310 | if (err) |
311 | return err; | |
312 | ||
ec7ba118 MS |
313 | old_cred = ovl_override_creds(inode->i_sb); |
314 | if (!upperinode && | |
315 | !special_file(realinode->i_mode) && mask & MAY_WRITE) { | |
754f8cb7 | 316 | mask &= ~(MAY_WRITE | MAY_APPEND); |
ec7ba118 | 317 | /* Make sure mounter can read file for copy up later */ |
500cac3c VG |
318 | mask |= MAY_READ; |
319 | } | |
4609e1f1 | 320 | err = inode_permission(mnt_idmap(realpath.mnt), realinode, mask); |
c0ca3d70 VG |
321 | revert_creds(old_cred); |
322 | ||
323 | return err; | |
e9be9d5e MS |
324 | } |
325 | ||
6b255391 | 326 | static const char *ovl_get_link(struct dentry *dentry, |
fceef393 AV |
327 | struct inode *inode, |
328 | struct delayed_call *done) | |
e9be9d5e | 329 | { |
1175b6b8 VG |
330 | const struct cred *old_cred; |
331 | const char *p; | |
e9be9d5e | 332 | |
6b255391 AV |
333 | if (!dentry) |
334 | return ERR_PTR(-ECHILD); | |
335 | ||
1175b6b8 | 336 | old_cred = ovl_override_creds(dentry->d_sb); |
7764235b | 337 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
1175b6b8 VG |
338 | revert_creds(old_cred); |
339 | return p; | |
e9be9d5e MS |
340 | } |
341 | ||
ded53656 | 342 | #ifdef CONFIG_FS_POSIX_ACL |
1aa5fef5 CB |
343 | /* |
344 | * Apply the idmapping of the layer to POSIX ACLs. The caller must pass a clone | |
345 | * of the POSIX ACLs retrieved from the lower layer to this function to not | |
346 | * alter the POSIX ACLs for the underlying filesystem. | |
347 | */ | |
6c0a8bfb | 348 | static void ovl_idmap_posix_acl(const struct inode *realinode, |
4d7ca409 | 349 | struct mnt_idmap *idmap, |
1aa5fef5 CB |
350 | struct posix_acl *acl) |
351 | { | |
abfcf55d CB |
352 | struct user_namespace *fs_userns = i_user_ns(realinode); |
353 | ||
1aa5fef5 CB |
354 | for (unsigned int i = 0; i < acl->a_count; i++) { |
355 | vfsuid_t vfsuid; | |
356 | vfsgid_t vfsgid; | |
357 | ||
358 | struct posix_acl_entry *e = &acl->a_entries[i]; | |
359 | switch (e->e_tag) { | |
360 | case ACL_USER: | |
4d7ca409 | 361 | vfsuid = make_vfsuid(idmap, fs_userns, e->e_uid); |
1aa5fef5 CB |
362 | e->e_uid = vfsuid_into_kuid(vfsuid); |
363 | break; | |
364 | case ACL_GROUP: | |
4d7ca409 | 365 | vfsgid = make_vfsgid(idmap, fs_userns, e->e_gid); |
1aa5fef5 CB |
366 | e->e_gid = vfsgid_into_kgid(vfsgid); |
367 | break; | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
6c0a8bfb CB |
372 | /* |
373 | * The @noperm argument is used to skip permission checking and is a temporary | |
374 | * measure. Quoting Miklos from an earlier discussion: | |
375 | * | |
376 | * > So there are two paths to getting an acl: | |
377 | * > 1) permission checking and 2) retrieving the value via getxattr(2). | |
378 | * > This is a similar situation as reading a symlink vs. following it. | |
379 | * > When following a symlink overlayfs always reads the link on the | |
380 | * > underlying fs just as if it was a readlink(2) call, calling | |
381 | * > security_inode_readlink() instead of security_inode_follow_link(). | |
382 | * > This is logical: we are reading the link from the underlying storage, | |
383 | * > and following it on overlayfs. | |
384 | * > | |
385 | * > Applying the same logic to acl: we do need to call the | |
386 | * > security_inode_getxattr() on the underlying fs, even if just want to | |
387 | * > check permissions on overlay. This is currently not done, which is an | |
388 | * > inconsistency. | |
389 | * > | |
390 | * > Maybe adding the check to ovl_get_acl() is the right way to go, but | |
391 | * > I'm a little afraid of a performance regression. Will look into that. | |
392 | * | |
393 | * Until we have made a decision allow this helper to take the @noperm | |
394 | * argument. We should hopefully be able to remove it soon. | |
395 | */ | |
31acceb9 CB |
396 | struct posix_acl *ovl_get_acl_path(const struct path *path, |
397 | const char *acl_name, bool noperm) | |
6c0a8bfb CB |
398 | { |
399 | struct posix_acl *real_acl, *clone; | |
77435322 | 400 | struct mnt_idmap *idmap; |
6c0a8bfb CB |
401 | struct inode *realinode = d_inode(path->dentry); |
402 | ||
77435322 | 403 | idmap = mnt_idmap(path->mnt); |
6c0a8bfb CB |
404 | |
405 | if (noperm) | |
406 | real_acl = get_inode_acl(realinode, posix_acl_type(acl_name)); | |
407 | else | |
77435322 | 408 | real_acl = vfs_get_acl(idmap, path->dentry, acl_name); |
6c0a8bfb CB |
409 | if (IS_ERR_OR_NULL(real_acl)) |
410 | return real_acl; | |
411 | ||
412 | if (!is_idmapped_mnt(path->mnt)) | |
413 | return real_acl; | |
414 | ||
415 | /* | |
416 | * We cannot alter the ACLs returned from the relevant layer as that | |
417 | * would alter the cached values filesystem wide for the lower | |
418 | * filesystem. Instead we can clone the ACLs and then apply the | |
419 | * relevant idmapping of the layer. | |
420 | */ | |
421 | clone = posix_acl_clone(real_acl, GFP_KERNEL); | |
422 | posix_acl_release(real_acl); /* release original acl */ | |
423 | if (!clone) | |
424 | return ERR_PTR(-ENOMEM); | |
425 | ||
4d7ca409 | 426 | ovl_idmap_posix_acl(realinode, idmap, clone); |
6c0a8bfb CB |
427 | return clone; |
428 | } | |
429 | ||
1aa5fef5 CB |
430 | /* |
431 | * When the relevant layer is an idmapped mount we need to take the idmapping | |
432 | * of the layer into account and translate any ACL_{GROUP,USER} values | |
433 | * according to the idmapped mount. | |
434 | * | |
435 | * We cannot alter the ACLs returned from the relevant layer as that would | |
436 | * alter the cached values filesystem wide for the lower filesystem. Instead we | |
437 | * can clone the ACLs and then apply the relevant idmapping of the layer. | |
438 | * | |
439 | * This is obviously only relevant when idmapped layers are used. | |
440 | */ | |
77435322 | 441 | struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap, |
6c0a8bfb CB |
442 | struct inode *inode, int type, |
443 | bool rcu, bool noperm) | |
39a25b2b | 444 | { |
f4e19e59 | 445 | struct inode *realinode; |
6c0a8bfb | 446 | struct posix_acl *acl; |
1aa5fef5 | 447 | struct path realpath; |
39a25b2b | 448 | |
1aa5fef5 | 449 | /* Careful in RCU walk mode */ |
f4e19e59 ZC |
450 | realinode = ovl_i_path_real(inode, &realpath); |
451 | if (!realinode) { | |
1aa5fef5 CB |
452 | WARN_ON(!rcu); |
453 | return ERR_PTR(-ECHILD); | |
454 | } | |
332f606b | 455 | |
f4e19e59 ZC |
456 | if (!IS_POSIXACL(realinode)) |
457 | return NULL; | |
458 | ||
1aa5fef5 | 459 | if (rcu) { |
6c0a8bfb CB |
460 | /* |
461 | * If the layer is idmapped drop out of RCU path walk | |
462 | * so we can clone the ACLs. | |
463 | */ | |
464 | if (is_idmapped_mnt(realpath.mnt)) | |
465 | return ERR_PTR(-ECHILD); | |
466 | ||
1aa5fef5 CB |
467 | acl = get_cached_acl_rcu(realinode, type); |
468 | } else { | |
469 | const struct cred *old_cred; | |
470 | ||
471 | old_cred = ovl_override_creds(inode->i_sb); | |
6c0a8bfb | 472 | acl = ovl_get_acl_path(&realpath, posix_acl_xattr_name(type), noperm); |
1aa5fef5 CB |
473 | revert_creds(old_cred); |
474 | } | |
1175b6b8 | 475 | |
6c0a8bfb | 476 | return acl; |
39a25b2b | 477 | } |
0e641857 CB |
478 | |
479 | static int ovl_set_or_remove_acl(struct dentry *dentry, struct inode *inode, | |
480 | struct posix_acl *acl, int type) | |
481 | { | |
482 | int err; | |
483 | struct path realpath; | |
484 | const char *acl_name; | |
485 | const struct cred *old_cred; | |
486 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); | |
487 | struct dentry *upperdentry = ovl_dentry_upper(dentry); | |
488 | struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); | |
489 | ||
0e641857 CB |
490 | /* |
491 | * If ACL is to be removed from a lower file, check if it exists in | |
492 | * the first place before copying it up. | |
493 | */ | |
494 | acl_name = posix_acl_xattr_name(type); | |
495 | if (!acl && !upperdentry) { | |
496 | struct posix_acl *real_acl; | |
497 | ||
498 | ovl_path_lower(dentry, &realpath); | |
499 | old_cred = ovl_override_creds(dentry->d_sb); | |
77435322 | 500 | real_acl = vfs_get_acl(mnt_idmap(realpath.mnt), realdentry, |
0e641857 CB |
501 | acl_name); |
502 | revert_creds(old_cred); | |
0e641857 CB |
503 | if (IS_ERR(real_acl)) { |
504 | err = PTR_ERR(real_acl); | |
162d0644 | 505 | goto out; |
0e641857 | 506 | } |
5b52aebe | 507 | posix_acl_release(real_acl); |
0e641857 CB |
508 | } |
509 | ||
510 | if (!upperdentry) { | |
511 | err = ovl_copy_up(dentry); | |
512 | if (err) | |
162d0644 | 513 | goto out; |
0e641857 CB |
514 | |
515 | realdentry = ovl_dentry_upper(dentry); | |
516 | } | |
517 | ||
162d0644 AG |
518 | err = ovl_want_write(dentry); |
519 | if (err) | |
520 | goto out; | |
521 | ||
0e641857 CB |
522 | old_cred = ovl_override_creds(dentry->d_sb); |
523 | if (acl) | |
524 | err = ovl_do_set_acl(ofs, realdentry, acl_name, acl); | |
525 | else | |
526 | err = ovl_do_remove_acl(ofs, realdentry, acl_name); | |
527 | revert_creds(old_cred); | |
162d0644 | 528 | ovl_drop_write(dentry); |
0e641857 CB |
529 | |
530 | /* copy c/mtime */ | |
531 | ovl_copyattr(inode); | |
162d0644 | 532 | out: |
0e641857 CB |
533 | return err; |
534 | } | |
535 | ||
13e83a49 | 536 | int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, |
0e641857 CB |
537 | struct posix_acl *acl, int type) |
538 | { | |
539 | int err; | |
540 | struct inode *inode = d_inode(dentry); | |
541 | struct dentry *workdir = ovl_workdir(dentry); | |
542 | struct inode *realinode = ovl_inode_real(inode); | |
543 | ||
544 | if (!IS_POSIXACL(d_inode(workdir))) | |
545 | return -EOPNOTSUPP; | |
546 | if (!realinode->i_op->set_acl) | |
547 | return -EOPNOTSUPP; | |
548 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) | |
549 | return acl ? -EACCES : 0; | |
01beba79 | 550 | if (!inode_owner_or_capable(&nop_mnt_idmap, inode)) |
0e641857 CB |
551 | return -EPERM; |
552 | ||
553 | /* | |
554 | * Check if sgid bit needs to be cleared (actual setacl operation will | |
555 | * be done with mounter's capabilities and so that won't do it for us). | |
556 | */ | |
557 | if (unlikely(inode->i_mode & S_ISGID) && type == ACL_TYPE_ACCESS && | |
558 | !in_group_p(inode->i_gid) && | |
9452e93e | 559 | !capable_wrt_inode_uidgid(&nop_mnt_idmap, inode, CAP_FSETID)) { |
0e641857 CB |
560 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; |
561 | ||
c1632a0f | 562 | err = ovl_setattr(&nop_mnt_idmap, dentry, &iattr); |
0e641857 CB |
563 | if (err) |
564 | return err; | |
565 | } | |
566 | ||
567 | return ovl_set_or_remove_acl(dentry, inode, acl, type); | |
568 | } | |
ded53656 | 569 | #endif |
39a25b2b | 570 | |
913e9928 | 571 | int ovl_update_time(struct inode *inode, int flags) |
d719e8f2 | 572 | { |
8f35cf51 | 573 | if (flags & S_ATIME) { |
f01d0889 | 574 | struct ovl_fs *ofs = OVL_FS(inode->i_sb); |
8f35cf51 | 575 | struct path upperpath = { |
08f4c7c8 | 576 | .mnt = ovl_upper_mnt(ofs), |
8f35cf51 MS |
577 | .dentry = ovl_upperdentry_dereference(OVL_I(inode)), |
578 | }; | |
579 | ||
580 | if (upperpath.dentry) { | |
581 | touch_atime(&upperpath); | |
4ddbd0f1 JL |
582 | inode_set_atime_to_ts(inode, |
583 | inode_get_atime(d_inode(upperpath.dentry))); | |
8f35cf51 | 584 | } |
d719e8f2 | 585 | } |
d719e8f2 MS |
586 | return 0; |
587 | } | |
588 | ||
9e142c41 MS |
589 | static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
590 | u64 start, u64 len) | |
591 | { | |
592 | int err; | |
c11faf32 | 593 | struct inode *realinode = ovl_inode_realdata(inode); |
9e142c41 MS |
594 | const struct cred *old_cred; |
595 | ||
41665644 AG |
596 | if (!realinode) |
597 | return -EIO; | |
598 | ||
9e142c41 MS |
599 | if (!realinode->i_op->fiemap) |
600 | return -EOPNOTSUPP; | |
601 | ||
602 | old_cred = ovl_override_creds(inode->i_sb); | |
603 | err = realinode->i_op->fiemap(realinode, fieinfo, start, len); | |
604 | revert_creds(old_cred); | |
605 | ||
606 | return err; | |
607 | } | |
608 | ||
66dbfabf MS |
609 | /* |
610 | * Work around the fact that security_file_ioctl() takes a file argument. | |
611 | * Introducing security_inode_fileattr_get/set() hooks would solve this issue | |
612 | * properly. | |
613 | */ | |
2d343087 | 614 | static int ovl_security_fileattr(const struct path *realpath, struct fileattr *fa, |
66dbfabf MS |
615 | bool set) |
616 | { | |
66dbfabf MS |
617 | struct file *file; |
618 | unsigned int cmd; | |
619 | int err; | |
620 | ||
72db8211 | 621 | file = dentry_open(realpath, O_RDONLY, current_cred()); |
66dbfabf MS |
622 | if (IS_ERR(file)) |
623 | return PTR_ERR(file); | |
624 | ||
625 | if (set) | |
626 | cmd = fa->fsx_valid ? FS_IOC_FSSETXATTR : FS_IOC_SETFLAGS; | |
627 | else | |
628 | cmd = fa->fsx_valid ? FS_IOC_FSGETXATTR : FS_IOC_GETFLAGS; | |
629 | ||
630 | err = security_file_ioctl(file, cmd, 0); | |
631 | fput(file); | |
632 | ||
633 | return err; | |
634 | } | |
635 | ||
2d343087 | 636 | int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa) |
72db8211 AG |
637 | { |
638 | int err; | |
639 | ||
640 | err = ovl_security_fileattr(realpath, fa, true); | |
641 | if (err) | |
642 | return err; | |
643 | ||
8782a9ae | 644 | return vfs_fileattr_set(mnt_idmap(realpath->mnt), realpath->dentry, fa); |
72db8211 AG |
645 | } |
646 | ||
8782a9ae | 647 | int ovl_fileattr_set(struct mnt_idmap *idmap, |
66dbfabf MS |
648 | struct dentry *dentry, struct fileattr *fa) |
649 | { | |
650 | struct inode *inode = d_inode(dentry); | |
72db8211 | 651 | struct path upperpath; |
66dbfabf | 652 | const struct cred *old_cred; |
096a218a | 653 | unsigned int flags; |
66dbfabf MS |
654 | int err; |
655 | ||
66dbfabf MS |
656 | err = ovl_copy_up(dentry); |
657 | if (!err) { | |
72db8211 | 658 | ovl_path_real(dentry, &upperpath); |
66dbfabf | 659 | |
162d0644 AG |
660 | err = ovl_want_write(dentry); |
661 | if (err) | |
662 | goto out; | |
663 | ||
66dbfabf | 664 | old_cred = ovl_override_creds(inode->i_sb); |
096a218a AG |
665 | /* |
666 | * Store immutable/append-only flags in xattr and clear them | |
667 | * in upper fileattr (in case they were set by older kernel) | |
668 | * so children of "ovl-immutable" directories lower aliases of | |
669 | * "ovl-immutable" hardlinks could be copied up. | |
670 | * Clear xattr when flags are cleared. | |
671 | */ | |
672 | err = ovl_set_protattr(inode, upperpath.dentry, fa); | |
673 | if (!err) | |
674 | err = ovl_real_fileattr_set(&upperpath, fa); | |
66dbfabf | 675 | revert_creds(old_cred); |
162d0644 | 676 | ovl_drop_write(dentry); |
096a218a AG |
677 | |
678 | /* | |
679 | * Merge real inode flags with inode flags read from | |
680 | * overlay.protattr xattr | |
681 | */ | |
682 | flags = ovl_inode_real(inode)->i_flags & OVL_COPY_I_FLAGS_MASK; | |
683 | ||
684 | BUILD_BUG_ON(OVL_PROT_I_FLAGS_MASK & ~OVL_COPY_I_FLAGS_MASK); | |
685 | flags |= inode->i_flags & OVL_PROT_I_FLAGS_MASK; | |
686 | inode_set_flags(inode, flags, OVL_COPY_I_FLAGS_MASK); | |
d8991e86 CX |
687 | |
688 | /* Update ctime */ | |
2878dffc | 689 | ovl_copyattr(inode); |
66dbfabf | 690 | } |
66dbfabf MS |
691 | out: |
692 | return err; | |
693 | } | |
694 | ||
096a218a AG |
695 | /* Convert inode protection flags to fileattr flags */ |
696 | static void ovl_fileattr_prot_flags(struct inode *inode, struct fileattr *fa) | |
697 | { | |
698 | BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL); | |
699 | BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON); | |
700 | ||
701 | if (inode->i_flags & S_APPEND) { | |
702 | fa->flags |= FS_APPEND_FL; | |
703 | fa->fsx_xflags |= FS_XFLAG_APPEND; | |
704 | } | |
705 | if (inode->i_flags & S_IMMUTABLE) { | |
706 | fa->flags |= FS_IMMUTABLE_FL; | |
707 | fa->fsx_xflags |= FS_XFLAG_IMMUTABLE; | |
708 | } | |
709 | } | |
710 | ||
2d343087 | 711 | int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa) |
72db8211 AG |
712 | { |
713 | int err; | |
714 | ||
715 | err = ovl_security_fileattr(realpath, fa, false); | |
716 | if (err) | |
717 | return err; | |
718 | ||
5b0a414d MS |
719 | err = vfs_fileattr_get(realpath->dentry, fa); |
720 | if (err == -ENOIOCTLCMD) | |
721 | err = -ENOTTY; | |
722 | return err; | |
72db8211 AG |
723 | } |
724 | ||
66dbfabf MS |
725 | int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa) |
726 | { | |
727 | struct inode *inode = d_inode(dentry); | |
72db8211 | 728 | struct path realpath; |
66dbfabf MS |
729 | const struct cred *old_cred; |
730 | int err; | |
731 | ||
72db8211 AG |
732 | ovl_path_real(dentry, &realpath); |
733 | ||
66dbfabf | 734 | old_cred = ovl_override_creds(inode->i_sb); |
72db8211 | 735 | err = ovl_real_fileattr_get(&realpath, fa); |
096a218a | 736 | ovl_fileattr_prot_flags(inode, fa); |
66dbfabf MS |
737 | revert_creds(old_cred); |
738 | ||
739 | return err; | |
740 | } | |
741 | ||
e9be9d5e MS |
742 | static const struct inode_operations ovl_file_inode_operations = { |
743 | .setattr = ovl_setattr, | |
744 | .permission = ovl_permission, | |
745 | .getattr = ovl_getattr, | |
e9be9d5e | 746 | .listxattr = ovl_listxattr, |
6c0a8bfb CB |
747 | .get_inode_acl = ovl_get_inode_acl, |
748 | .get_acl = ovl_get_acl, | |
0e641857 | 749 | .set_acl = ovl_set_acl, |
d719e8f2 | 750 | .update_time = ovl_update_time, |
9e142c41 | 751 | .fiemap = ovl_fiemap, |
66dbfabf MS |
752 | .fileattr_get = ovl_fileattr_get, |
753 | .fileattr_set = ovl_fileattr_set, | |
e9be9d5e MS |
754 | }; |
755 | ||
756 | static const struct inode_operations ovl_symlink_inode_operations = { | |
757 | .setattr = ovl_setattr, | |
6b255391 | 758 | .get_link = ovl_get_link, |
e9be9d5e | 759 | .getattr = ovl_getattr, |
e9be9d5e | 760 | .listxattr = ovl_listxattr, |
d719e8f2 | 761 | .update_time = ovl_update_time, |
e9be9d5e MS |
762 | }; |
763 | ||
9e142c41 MS |
764 | static const struct inode_operations ovl_special_inode_operations = { |
765 | .setattr = ovl_setattr, | |
766 | .permission = ovl_permission, | |
767 | .getattr = ovl_getattr, | |
768 | .listxattr = ovl_listxattr, | |
6c0a8bfb CB |
769 | .get_inode_acl = ovl_get_inode_acl, |
770 | .get_acl = ovl_get_acl, | |
0e641857 | 771 | .set_acl = ovl_set_acl, |
9e142c41 MS |
772 | .update_time = ovl_update_time, |
773 | }; | |
774 | ||
69383c59 | 775 | static const struct address_space_operations ovl_aops = { |
5b910bd6 AG |
776 | /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */ |
777 | .direct_IO = noop_direct_IO, | |
778 | }; | |
779 | ||
b1eaa950 AG |
780 | /* |
781 | * It is possible to stack overlayfs instance on top of another | |
a5a84682 | 782 | * overlayfs instance as lower layer. We need to annotate the |
b1eaa950 AG |
783 | * stackable i_mutex locks according to stack level of the super |
784 | * block instance. An overlayfs instance can never be in stack | |
785 | * depth 0 (there is always a real fs below it). An overlayfs | |
f48bbfb2 | 786 | * inode lock will use the lockdep annotation ovl_i_mutex_key[depth]. |
b1eaa950 AG |
787 | * |
788 | * For example, here is a snip from /proc/lockdep_chains after | |
789 | * dir_iterate of nested overlayfs: | |
790 | * | |
791 | * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) | |
792 | * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) | |
793 | * [...] &type->i_mutex_dir_key (stack_depth=0) | |
b1f9d385 AG |
794 | * |
795 | * Locking order w.r.t ovl_want_write() is important for nested overlayfs. | |
796 | * | |
797 | * This chain is valid: | |
798 | * - inode->i_rwsem (inode_lock[2]) | |
799 | * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) | |
800 | * - OVL_I(inode)->lock (ovl_inode_lock[2]) | |
801 | * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) | |
802 | * | |
803 | * And this chain is valid: | |
804 | * - inode->i_rwsem (inode_lock[2]) | |
805 | * - OVL_I(inode)->lock (ovl_inode_lock[2]) | |
806 | * - lowerinode->i_rwsem (inode_lock[1]) | |
807 | * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) | |
808 | * | |
809 | * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is | |
810 | * held, because it is in reverse order of the non-nested case using the same | |
811 | * upper fs: | |
812 | * - inode->i_rwsem (inode_lock[1]) | |
813 | * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) | |
814 | * - OVL_I(inode)->lock (ovl_inode_lock[1]) | |
b1eaa950 AG |
815 | */ |
816 | #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH | |
817 | ||
818 | static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) | |
819 | { | |
820 | #ifdef CONFIG_LOCKDEP | |
821 | static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; | |
822 | static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; | |
4eae06de | 823 | static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; |
b1eaa950 AG |
824 | |
825 | int depth = inode->i_sb->s_stack_depth - 1; | |
826 | ||
827 | if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) | |
828 | depth = 0; | |
829 | ||
830 | if (S_ISDIR(inode->i_mode)) | |
831 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); | |
832 | else | |
833 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); | |
4eae06de AG |
834 | |
835 | lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); | |
b1eaa950 AG |
836 | #endif |
837 | } | |
838 | ||
4d314f78 AG |
839 | static void ovl_next_ino(struct inode *inode) |
840 | { | |
dcb399de | 841 | struct ovl_fs *ofs = OVL_FS(inode->i_sb); |
4d314f78 AG |
842 | |
843 | inode->i_ino = atomic_long_inc_return(&ofs->last_ino); | |
844 | if (unlikely(!inode->i_ino)) | |
845 | inode->i_ino = atomic_long_inc_return(&ofs->last_ino); | |
846 | } | |
847 | ||
62c832ed | 848 | static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) |
e9be9d5e | 849 | { |
dcb399de AG |
850 | struct ovl_fs *ofs = OVL_FS(inode->i_sb); |
851 | int xinobits = ovl_xino_bits(ofs); | |
dfe51d47 | 852 | unsigned int xinoshift = 64 - xinobits; |
12574a9f | 853 | |
695b46e7 | 854 | /* |
6dde1e42 AG |
855 | * When d_ino is consistent with st_ino (samefs or i_ino has enough |
856 | * bits to encode layer), set the same value used for st_ino to i_ino, | |
857 | * so inode number exposed via /proc/locks and a like will be | |
858 | * consistent with d_ino and st_ino values. An i_ino value inconsistent | |
735c907d | 859 | * with d_ino also causes nfsd readdirplus to fail. |
695b46e7 | 860 | */ |
4d314f78 | 861 | inode->i_ino = ino; |
dcb399de | 862 | if (ovl_same_fs(ofs)) { |
dfe51d47 AG |
863 | return; |
864 | } else if (xinobits && likely(!(ino >> xinoshift))) { | |
865 | inode->i_ino |= (unsigned long)fsid << (xinoshift + 1); | |
866 | return; | |
867 | } | |
868 | ||
869 | /* | |
870 | * For directory inodes on non-samefs with xino disabled or xino | |
871 | * overflow, we allocate a non-persistent inode number, to be used for | |
872 | * resolving st_ino collisions in ovl_map_dev_ino(). | |
873 | * | |
874 | * To avoid ino collision with legitimate xino values from upper | |
875 | * layer (fsid 0), use the lowest xinobit to map the non | |
876 | * persistent inode numbers to the unified st_ino address space. | |
877 | */ | |
878 | if (S_ISDIR(inode->i_mode)) { | |
4d314f78 | 879 | ovl_next_ino(inode); |
dfe51d47 AG |
880 | if (xinobits) { |
881 | inode->i_ino &= ~0UL >> xinobits; | |
882 | inode->i_ino |= 1UL << xinoshift; | |
883 | } | |
12574a9f | 884 | } |
62c832ed AG |
885 | } |
886 | ||
887 | void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, | |
888 | unsigned long ino, int fsid) | |
889 | { | |
890 | struct inode *realinode; | |
ffa5723c | 891 | struct ovl_inode *oi = OVL_I(inode); |
62c832ed | 892 | |
0af950f5 AG |
893 | oi->__upperdentry = oip->upperdentry; |
894 | oi->oe = oip->oe; | |
895 | oi->redirect = oip->redirect; | |
2b21da92 | 896 | oi->lowerdata_redirect = oip->lowerdata_redirect; |
62c832ed AG |
897 | |
898 | realinode = ovl_inode_real(inode); | |
2878dffc | 899 | ovl_copyattr(inode); |
62c832ed AG |
900 | ovl_copyflags(realinode, inode); |
901 | ovl_map_ino(inode, ino, fsid); | |
902 | } | |
903 | ||
904 | static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) | |
905 | { | |
e9be9d5e | 906 | inode->i_mode = mode; |
d719e8f2 | 907 | inode->i_flags |= S_NOCMTIME; |
2a3a2a3f MS |
908 | #ifdef CONFIG_FS_POSIX_ACL |
909 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; | |
910 | #endif | |
e9be9d5e | 911 | |
b1eaa950 AG |
912 | ovl_lockdep_annotate_inode_mutex_key(inode); |
913 | ||
ca4c8a3a MS |
914 | switch (mode & S_IFMT) { |
915 | case S_IFREG: | |
916 | inode->i_op = &ovl_file_inode_operations; | |
d1d04ef8 | 917 | inode->i_fop = &ovl_file_operations; |
5b910bd6 | 918 | inode->i_mapping->a_ops = &ovl_aops; |
ca4c8a3a MS |
919 | break; |
920 | ||
e9be9d5e | 921 | case S_IFDIR: |
e9be9d5e MS |
922 | inode->i_op = &ovl_dir_inode_operations; |
923 | inode->i_fop = &ovl_dir_operations; | |
924 | break; | |
925 | ||
926 | case S_IFLNK: | |
927 | inode->i_op = &ovl_symlink_inode_operations; | |
928 | break; | |
929 | ||
51f7e52d | 930 | default: |
9e142c41 | 931 | inode->i_op = &ovl_special_inode_operations; |
ca4c8a3a | 932 | init_special_inode(inode, mode, rdev); |
e9be9d5e | 933 | break; |
51f7e52d MS |
934 | } |
935 | } | |
e9be9d5e | 936 | |
5f8415d6 AG |
937 | /* |
938 | * With inodes index enabled, an overlay inode nlink counts the union of upper | |
939 | * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure | |
940 | * upper inode, the following nlink modifying operations can happen: | |
941 | * | |
942 | * 1. Lower hardlink copy up | |
943 | * 2. Upper hardlink created, unlinked or renamed over | |
944 | * 3. Lower hardlink whiteout or renamed over | |
945 | * | |
946 | * For the first, copy up case, the union nlink does not change, whether the | |
947 | * operation succeeds or fails, but the upper inode nlink may change. | |
948 | * Therefore, before copy up, we store the union nlink value relative to the | |
2d2f2d73 | 949 | * lower inode nlink in the index inode xattr .overlay.nlink. |
5f8415d6 AG |
950 | * |
951 | * For the second, upper hardlink case, the union nlink should be incremented | |
952 | * or decremented IFF the operation succeeds, aligned with nlink change of the | |
953 | * upper inode. Therefore, before link/unlink/rename, we store the union nlink | |
954 | * value relative to the upper inode nlink in the index inode. | |
955 | * | |
956 | * For the last, lower cover up case, we simplify things by preceding the | |
957 | * whiteout or cover up with copy up. This makes sure that there is an index | |
958 | * upper inode where the nlink xattr can be stored before the copied up upper | |
959 | * entry is unlink. | |
960 | */ | |
961 | #define OVL_NLINK_ADD_UPPER (1 << 0) | |
962 | ||
963 | /* | |
964 | * On-disk format for indexed nlink: | |
965 | * | |
966 | * nlink relative to the upper inode - "U[+-]NUM" | |
967 | * nlink relative to the lower inode - "L[+-]NUM" | |
968 | */ | |
969 | ||
970 | static int ovl_set_nlink_common(struct dentry *dentry, | |
971 | struct dentry *realdentry, const char *format) | |
972 | { | |
973 | struct inode *inode = d_inode(dentry); | |
974 | struct inode *realinode = d_inode(realdentry); | |
975 | char buf[13]; | |
976 | int len; | |
977 | ||
978 | len = snprintf(buf, sizeof(buf), format, | |
979 | (int) (inode->i_nlink - realinode->i_nlink)); | |
980 | ||
6787341a MS |
981 | if (WARN_ON(len >= sizeof(buf))) |
982 | return -EIO; | |
983 | ||
c914c0e2 AG |
984 | return ovl_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry), |
985 | OVL_XATTR_NLINK, buf, len); | |
5f8415d6 AG |
986 | } |
987 | ||
988 | int ovl_set_nlink_upper(struct dentry *dentry) | |
989 | { | |
990 | return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); | |
991 | } | |
992 | ||
993 | int ovl_set_nlink_lower(struct dentry *dentry) | |
994 | { | |
995 | return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); | |
996 | } | |
997 | ||
610afc0b | 998 | unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, |
caf70cb2 AG |
999 | struct dentry *upperdentry, |
1000 | unsigned int fallback) | |
5f8415d6 AG |
1001 | { |
1002 | int nlink_diff; | |
1003 | int nlink; | |
1004 | char buf[13]; | |
1005 | int err; | |
1006 | ||
1007 | if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) | |
1008 | return fallback; | |
1009 | ||
dad7017a CB |
1010 | err = ovl_getxattr_upper(ofs, upperdentry, OVL_XATTR_NLINK, |
1011 | &buf, sizeof(buf) - 1); | |
5f8415d6 AG |
1012 | if (err < 0) |
1013 | goto fail; | |
1014 | ||
1015 | buf[err] = '\0'; | |
1016 | if ((buf[0] != 'L' && buf[0] != 'U') || | |
1017 | (buf[1] != '+' && buf[1] != '-')) | |
1018 | goto fail; | |
1019 | ||
1020 | err = kstrtoint(buf + 1, 10, &nlink_diff); | |
1021 | if (err < 0) | |
1022 | goto fail; | |
1023 | ||
1024 | nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; | |
1025 | nlink += nlink_diff; | |
1026 | ||
1027 | if (nlink <= 0) | |
1028 | goto fail; | |
1029 | ||
1030 | return nlink; | |
1031 | ||
1032 | fail: | |
1bd0a3ae | 1033 | pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n", |
5f8415d6 AG |
1034 | upperdentry, err); |
1035 | return fallback; | |
1036 | } | |
1037 | ||
ca4c8a3a | 1038 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) |
51f7e52d MS |
1039 | { |
1040 | struct inode *inode; | |
1041 | ||
1042 | inode = new_inode(sb); | |
1043 | if (inode) | |
62c832ed | 1044 | ovl_fill_inode(inode, mode, rdev); |
51f7e52d MS |
1045 | |
1046 | return inode; | |
1047 | } | |
1048 | ||
1049 | static int ovl_inode_test(struct inode *inode, void *data) | |
1050 | { | |
25b7713a | 1051 | return inode->i_private == data; |
51f7e52d MS |
1052 | } |
1053 | ||
1054 | static int ovl_inode_set(struct inode *inode, void *data) | |
1055 | { | |
25b7713a | 1056 | inode->i_private = data; |
51f7e52d MS |
1057 | return 0; |
1058 | } | |
1059 | ||
b9ac5c27 | 1060 | static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, |
4b91c30a | 1061 | struct dentry *upperdentry, bool strict) |
b9ac5c27 | 1062 | { |
4b91c30a AG |
1063 | /* |
1064 | * For directories, @strict verify from lookup path performs consistency | |
1065 | * checks, so NULL lower/upper in dentry must match NULL lower/upper in | |
1066 | * inode. Non @strict verify from NFS handle decode path passes NULL for | |
1067 | * 'unknown' lower/upper. | |
1068 | */ | |
1069 | if (S_ISDIR(inode->i_mode) && strict) { | |
31747eda AG |
1070 | /* Real lower dir moved to upper layer under us? */ |
1071 | if (!lowerdentry && ovl_inode_lower(inode)) | |
1072 | return false; | |
1073 | ||
1074 | /* Lookup of an uncovered redirect origin? */ | |
1075 | if (!upperdentry && ovl_inode_upper(inode)) | |
1076 | return false; | |
1077 | } | |
1078 | ||
939ae4ef AG |
1079 | /* |
1080 | * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. | |
1081 | * This happens when finding a copied up overlay inode for a renamed | |
1082 | * or hardlinked overlay dentry and lower dentry cannot be followed | |
1083 | * by origin because lower fs does not support file handles. | |
1084 | */ | |
1085 | if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) | |
b9ac5c27 MS |
1086 | return false; |
1087 | ||
1088 | /* | |
1089 | * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. | |
1090 | * This happens when finding a lower alias for a copied up hard link. | |
1091 | */ | |
1092 | if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) | |
1093 | return false; | |
1094 | ||
1095 | return true; | |
1096 | } | |
1097 | ||
4b91c30a AG |
1098 | struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, |
1099 | bool is_upper) | |
9436a1a3 | 1100 | { |
4b91c30a | 1101 | struct inode *inode, *key = d_inode(real); |
9436a1a3 AG |
1102 | |
1103 | inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); | |
1104 | if (!inode) | |
1105 | return NULL; | |
1106 | ||
4b91c30a AG |
1107 | if (!ovl_verify_inode(inode, is_upper ? NULL : real, |
1108 | is_upper ? real : NULL, false)) { | |
9436a1a3 AG |
1109 | iput(inode); |
1110 | return ERR_PTR(-ESTALE); | |
1111 | } | |
1112 | ||
1113 | return inode; | |
1114 | } | |
1115 | ||
146d62e5 AG |
1116 | bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir) |
1117 | { | |
1118 | struct inode *key = d_inode(dir); | |
1119 | struct inode *trap; | |
1120 | bool res; | |
1121 | ||
1122 | trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); | |
1123 | if (!trap) | |
1124 | return false; | |
1125 | ||
1126 | res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) && | |
1127 | !ovl_inode_lower(trap); | |
1128 | ||
1129 | iput(trap); | |
1130 | return res; | |
1131 | } | |
1132 | ||
1133 | /* | |
1134 | * Create an inode cache entry for layer root dir, that will intentionally | |
1135 | * fail ovl_verify_inode(), so any lookup that will find some layer root | |
1136 | * will fail. | |
1137 | */ | |
1138 | struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir) | |
1139 | { | |
1140 | struct inode *key = d_inode(dir); | |
1141 | struct inode *trap; | |
1142 | ||
1143 | if (!d_is_dir(dir)) | |
1144 | return ERR_PTR(-ENOTDIR); | |
1145 | ||
1146 | trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test, | |
1147 | ovl_inode_set, key); | |
1148 | if (!trap) | |
1149 | return ERR_PTR(-ENOMEM); | |
1150 | ||
1151 | if (!(trap->i_state & I_NEW)) { | |
1152 | /* Conflicting layer roots? */ | |
1153 | iput(trap); | |
1154 | return ERR_PTR(-ELOOP); | |
1155 | } | |
1156 | ||
1157 | trap->i_mode = S_IFDIR; | |
1158 | trap->i_flags = S_DEAD; | |
1159 | unlock_new_inode(trap); | |
1160 | ||
1161 | return trap; | |
1162 | } | |
1163 | ||
764baba8 AG |
1164 | /* |
1165 | * Does overlay inode need to be hashed by lower inode? | |
1166 | */ | |
1167 | static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, | |
74c6e384 | 1168 | struct dentry *lower, bool index) |
764baba8 | 1169 | { |
f01d0889 | 1170 | struct ovl_fs *ofs = OVL_FS(sb); |
764baba8 AG |
1171 | |
1172 | /* No, if pure upper */ | |
1173 | if (!lower) | |
1174 | return false; | |
1175 | ||
1176 | /* Yes, if already indexed */ | |
1177 | if (index) | |
1178 | return true; | |
1179 | ||
1180 | /* Yes, if won't be copied up */ | |
08f4c7c8 | 1181 | if (!ovl_upper_mnt(ofs)) |
764baba8 AG |
1182 | return true; |
1183 | ||
1184 | /* No, if lower hardlink is or will be broken on copy up */ | |
1185 | if ((upper || !ovl_indexdir(sb)) && | |
1186 | !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) | |
1187 | return false; | |
1188 | ||
1189 | /* No, if non-indexed upper with NFS export */ | |
16aac5ad | 1190 | if (ofs->config.nfs_export && upper) |
764baba8 AG |
1191 | return false; |
1192 | ||
1193 | /* Otherwise, hash by lower inode for fsnotify */ | |
1194 | return true; | |
1195 | } | |
1196 | ||
01b39dcc AG |
1197 | static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode, |
1198 | struct inode *key) | |
1199 | { | |
1200 | return newinode ? inode_insert5(newinode, (unsigned long) key, | |
1201 | ovl_inode_test, ovl_inode_set, key) : | |
1202 | iget5_locked(sb, (unsigned long) key, | |
1203 | ovl_inode_test, ovl_inode_set, key); | |
1204 | } | |
1205 | ||
ac6a52eb VG |
1206 | struct inode *ovl_get_inode(struct super_block *sb, |
1207 | struct ovl_inode_params *oip) | |
51f7e52d | 1208 | { |
610afc0b | 1209 | struct ovl_fs *ofs = OVL_FS(sb); |
ac6a52eb | 1210 | struct dentry *upperdentry = oip->upperdentry; |
ac900ed4 | 1211 | struct ovl_path *lowerpath = ovl_lowerpath(oip->oe); |
09d8b586 | 1212 | struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; |
51f7e52d | 1213 | struct inode *inode; |
12574a9f | 1214 | struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL; |
dad7017a CB |
1215 | struct path realpath = { |
1216 | .dentry = upperdentry ?: lowerdentry, | |
1217 | .mnt = upperdentry ? ovl_upper_mnt(ofs) : lowerpath->layer->mnt, | |
1218 | }; | |
ac6a52eb VG |
1219 | bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, |
1220 | oip->index); | |
300b124f | 1221 | int fsid = bylower ? lowerpath->layer->fsid : 0; |
28166ab3 | 1222 | bool is_dir; |
695b46e7 | 1223 | unsigned long ino = 0; |
acf3062a | 1224 | int err = oip->newinode ? -EEXIST : -ENOMEM; |
6eaf0111 | 1225 | |
09d8b586 MS |
1226 | if (!realinode) |
1227 | realinode = d_inode(lowerdentry); | |
1228 | ||
6eaf0111 | 1229 | /* |
764baba8 AG |
1230 | * Copy up origin (lower) may exist for non-indexed upper, but we must |
1231 | * not use lower as hash key if this is a broken hardlink. | |
6eaf0111 | 1232 | */ |
31747eda | 1233 | is_dir = S_ISDIR(realinode->i_mode); |
764baba8 AG |
1234 | if (upperdentry || bylower) { |
1235 | struct inode *key = d_inode(bylower ? lowerdentry : | |
1236 | upperdentry); | |
31747eda | 1237 | unsigned int nlink = is_dir ? 1 : realinode->i_nlink; |
b9ac5c27 | 1238 | |
01b39dcc | 1239 | inode = ovl_iget5(sb, oip->newinode, key); |
09d8b586 | 1240 | if (!inode) |
027065b7 | 1241 | goto out_err; |
09d8b586 | 1242 | if (!(inode->i_state & I_NEW)) { |
b9ac5c27 MS |
1243 | /* |
1244 | * Verify that the underlying files stored in the inode | |
1245 | * match those in the dentry. | |
1246 | */ | |
4b91c30a AG |
1247 | if (!ovl_verify_inode(inode, lowerdentry, upperdentry, |
1248 | true)) { | |
b9ac5c27 | 1249 | iput(inode); |
027065b7 VG |
1250 | err = -ESTALE; |
1251 | goto out_err; | |
b9ac5c27 MS |
1252 | } |
1253 | ||
09d8b586 | 1254 | dput(upperdentry); |
0af950f5 | 1255 | ovl_free_entry(oip->oe); |
9cec54c8 | 1256 | kfree(oip->redirect); |
2b21da92 | 1257 | kfree(oip->lowerdata_redirect); |
09d8b586 MS |
1258 | goto out; |
1259 | } | |
e6d2ebdd | 1260 | |
31747eda AG |
1261 | /* Recalculate nlink for non-dir due to indexing */ |
1262 | if (!is_dir) | |
610afc0b MS |
1263 | nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry, |
1264 | nlink); | |
5f8415d6 | 1265 | set_nlink(inode, nlink); |
695b46e7 | 1266 | ino = key->i_ino; |
e6d2ebdd | 1267 | } else { |
764baba8 | 1268 | /* Lower hardlink that will be broken on copy up */ |
0aceb53e | 1269 | inode = new_inode(sb); |
027065b7 VG |
1270 | if (!inode) { |
1271 | err = -ENOMEM; | |
1272 | goto out_err; | |
1273 | } | |
300b124f AG |
1274 | ino = realinode->i_ino; |
1275 | fsid = lowerpath->layer->fsid; | |
e9be9d5e | 1276 | } |
62c832ed AG |
1277 | ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); |
1278 | ovl_inode_init(inode, oip, ino, fsid); | |
13c72075 | 1279 | |
610afc0b | 1280 | if (upperdentry && ovl_is_impuredir(sb, upperdentry)) |
13c72075 MS |
1281 | ovl_set_flag(OVL_IMPURE, inode); |
1282 | ||
ac6a52eb | 1283 | if (oip->index) |
0471a9cd VG |
1284 | ovl_set_flag(OVL_INDEX, inode); |
1285 | ||
a00c2d59 VG |
1286 | if (bylower) |
1287 | ovl_set_flag(OVL_CONST_INO, inode); | |
1288 | ||
b79e05aa | 1289 | /* Check for non-merge dir that may have whiteouts */ |
31747eda | 1290 | if (is_dir) { |
ac900ed4 | 1291 | if (((upperdentry && lowerdentry) || ovl_numlower(oip->oe) > 1) || |
dad7017a | 1292 | ovl_path_check_origin_xattr(ofs, &realpath)) { |
b79e05aa AG |
1293 | ovl_set_flag(OVL_WHITEOUTS, inode); |
1294 | } | |
1295 | } | |
1296 | ||
096a218a AG |
1297 | /* Check for immutable/append-only inode flags in xattr */ |
1298 | if (upperdentry) | |
1299 | ovl_check_protattr(inode, upperdentry); | |
1300 | ||
e6d2ebdd MS |
1301 | if (inode->i_state & I_NEW) |
1302 | unlock_new_inode(inode); | |
1303 | out: | |
e9be9d5e | 1304 | return inode; |
b9ac5c27 | 1305 | |
027065b7 | 1306 | out_err: |
1bd0a3ae | 1307 | pr_warn_ratelimited("failed to get inode (%i)\n", err); |
027065b7 | 1308 | inode = ERR_PTR(err); |
b9ac5c27 | 1309 | goto out; |
e9be9d5e | 1310 | } |