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