]>
Commit | Line | Data |
---|---|---|
bbb1e54d MS |
1 | /* |
2 | * Copyright (C) 2011 Novell Inc. | |
3 | * Copyright (C) 2016 Red Hat, Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/fs.h> | |
11 | #include <linux/mount.h> | |
12 | #include <linux/slab.h> | |
5b825c3a | 13 | #include <linux/cred.h> |
bbb1e54d | 14 | #include <linux/xattr.h> |
02bcd157 AG |
15 | #include <linux/exportfs.h> |
16 | #include <linux/uuid.h> | |
caf70cb2 AG |
17 | #include <linux/namei.h> |
18 | #include <linux/ratelimit.h> | |
bbb1e54d | 19 | #include "overlayfs.h" |
bbb1e54d MS |
20 | |
21 | int ovl_want_write(struct dentry *dentry) | |
22 | { | |
23 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
24 | return mnt_want_write(ofs->upper_mnt); | |
25 | } | |
26 | ||
27 | void ovl_drop_write(struct dentry *dentry) | |
28 | { | |
29 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
30 | mnt_drop_write(ofs->upper_mnt); | |
31 | } | |
32 | ||
33 | struct dentry *ovl_workdir(struct dentry *dentry) | |
34 | { | |
35 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
36 | return ofs->workdir; | |
37 | } | |
38 | ||
39 | const struct cred *ovl_override_creds(struct super_block *sb) | |
40 | { | |
41 | struct ovl_fs *ofs = sb->s_fs_info; | |
42 | ||
43 | return override_creds(ofs->creator_cred); | |
44 | } | |
45 | ||
7bcd74b9 AG |
46 | struct super_block *ovl_same_sb(struct super_block *sb) |
47 | { | |
48 | struct ovl_fs *ofs = sb->s_fs_info; | |
49 | ||
5148626b AG |
50 | if (!ofs->numlowerfs) |
51 | return ofs->upper_mnt->mnt_sb; | |
52 | else if (ofs->numlowerfs == 1 && !ofs->upper_mnt) | |
53 | return ofs->lower_fs[0].sb; | |
54 | else | |
55 | return NULL; | |
7bcd74b9 AG |
56 | } |
57 | ||
e487d889 AG |
58 | /* |
59 | * Check if underlying fs supports file handles and try to determine encoding | |
60 | * type, in order to deduce maximum inode number used by fs. | |
61 | * | |
62 | * Return 0 if file handles are not supported. | |
63 | * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. | |
64 | * Return -1 if fs uses a non default encoding with unknown inode size. | |
65 | */ | |
66 | int ovl_can_decode_fh(struct super_block *sb) | |
02bcd157 | 67 | { |
e487d889 AG |
68 | if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry || |
69 | uuid_is_null(&sb->s_uuid)) | |
70 | return 0; | |
71 | ||
72 | return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; | |
02bcd157 AG |
73 | } |
74 | ||
75 | struct dentry *ovl_indexdir(struct super_block *sb) | |
76 | { | |
77 | struct ovl_fs *ofs = sb->s_fs_info; | |
78 | ||
79 | return ofs->indexdir; | |
80 | } | |
81 | ||
f168f109 AG |
82 | /* Index all files on copy up. For now only enabled for NFS export */ |
83 | bool ovl_index_all(struct super_block *sb) | |
84 | { | |
85 | struct ovl_fs *ofs = sb->s_fs_info; | |
86 | ||
87 | return ofs->config.nfs_export && ofs->config.index; | |
88 | } | |
89 | ||
90 | /* Verify lower origin on lookup. For now only enabled for NFS export */ | |
91 | bool ovl_verify_lower(struct super_block *sb) | |
92 | { | |
93 | struct ovl_fs *ofs = sb->s_fs_info; | |
94 | ||
95 | return ofs->config.nfs_export && ofs->config.index; | |
96 | } | |
97 | ||
bbb1e54d MS |
98 | struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
99 | { | |
100 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); | |
101 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); | |
102 | ||
103 | if (oe) | |
104 | oe->numlower = numlower; | |
105 | ||
106 | return oe; | |
107 | } | |
108 | ||
109 | bool ovl_dentry_remote(struct dentry *dentry) | |
110 | { | |
111 | return dentry->d_flags & | |
112 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | | |
113 | DCACHE_OP_REAL); | |
114 | } | |
115 | ||
116 | bool ovl_dentry_weird(struct dentry *dentry) | |
117 | { | |
118 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | | |
119 | DCACHE_MANAGE_TRANSIT | | |
120 | DCACHE_OP_HASH | | |
121 | DCACHE_OP_COMPARE); | |
122 | } | |
123 | ||
124 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
125 | { | |
126 | struct ovl_entry *oe = dentry->d_fsdata; | |
127 | enum ovl_path_type type = 0; | |
128 | ||
09d8b586 | 129 | if (ovl_dentry_upper(dentry)) { |
bbb1e54d MS |
130 | type = __OVL_PATH_UPPER; |
131 | ||
132 | /* | |
59548503 | 133 | * Non-dir dentry can hold lower dentry of its copy up origin. |
bbb1e54d | 134 | */ |
59548503 | 135 | if (oe->numlower) { |
60124877 VG |
136 | if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) |
137 | type |= __OVL_PATH_ORIGIN; | |
0b17c28a VG |
138 | if (d_is_dir(dentry) || |
139 | !ovl_has_upperdata(d_inode(dentry))) | |
59548503 AG |
140 | type |= __OVL_PATH_MERGE; |
141 | } | |
bbb1e54d MS |
142 | } else { |
143 | if (oe->numlower > 1) | |
144 | type |= __OVL_PATH_MERGE; | |
145 | } | |
146 | return type; | |
147 | } | |
148 | ||
149 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
150 | { | |
151 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
bbb1e54d MS |
152 | |
153 | path->mnt = ofs->upper_mnt; | |
09d8b586 | 154 | path->dentry = ovl_dentry_upper(dentry); |
bbb1e54d MS |
155 | } |
156 | ||
157 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
158 | { | |
159 | struct ovl_entry *oe = dentry->d_fsdata; | |
160 | ||
b9343632 CR |
161 | if (oe->numlower) { |
162 | path->mnt = oe->lowerstack[0].layer->mnt; | |
163 | path->dentry = oe->lowerstack[0].dentry; | |
164 | } else { | |
165 | *path = (struct path) { }; | |
166 | } | |
bbb1e54d MS |
167 | } |
168 | ||
4f93b426 VG |
169 | void ovl_path_lowerdata(struct dentry *dentry, struct path *path) |
170 | { | |
171 | struct ovl_entry *oe = dentry->d_fsdata; | |
172 | ||
173 | if (oe->numlower) { | |
174 | path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt; | |
175 | path->dentry = oe->lowerstack[oe->numlower - 1].dentry; | |
176 | } else { | |
177 | *path = (struct path) { }; | |
178 | } | |
179 | } | |
180 | ||
bbb1e54d MS |
181 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
182 | { | |
183 | enum ovl_path_type type = ovl_path_type(dentry); | |
184 | ||
185 | if (!OVL_TYPE_UPPER(type)) | |
186 | ovl_path_lower(dentry, path); | |
187 | else | |
188 | ovl_path_upper(dentry, path); | |
189 | ||
190 | return type; | |
191 | } | |
192 | ||
193 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
194 | { | |
09d8b586 | 195 | return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); |
bbb1e54d MS |
196 | } |
197 | ||
198 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
199 | { | |
200 | struct ovl_entry *oe = dentry->d_fsdata; | |
201 | ||
09d8b586 | 202 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
bbb1e54d MS |
203 | } |
204 | ||
da309e8c AG |
205 | struct ovl_layer *ovl_layer_lower(struct dentry *dentry) |
206 | { | |
207 | struct ovl_entry *oe = dentry->d_fsdata; | |
208 | ||
209 | return oe->numlower ? oe->lowerstack[0].layer : NULL; | |
210 | } | |
211 | ||
647d253f VG |
212 | /* |
213 | * ovl_dentry_lower() could return either a data dentry or metacopy dentry | |
214 | * dependig on what is stored in lowerstack[0]. At times we need to find | |
215 | * lower dentry which has data (and not metacopy dentry). This helper | |
216 | * returns the lower data dentry. | |
217 | */ | |
218 | struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) | |
219 | { | |
220 | struct ovl_entry *oe = dentry->d_fsdata; | |
221 | ||
222 | return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL; | |
223 | } | |
224 | ||
bbb1e54d MS |
225 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
226 | { | |
09d8b586 | 227 | return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); |
bbb1e54d MS |
228 | } |
229 | ||
1d88f183 MS |
230 | struct dentry *ovl_i_dentry_upper(struct inode *inode) |
231 | { | |
232 | return ovl_upperdentry_dereference(OVL_I(inode)); | |
233 | } | |
234 | ||
09d8b586 | 235 | struct inode *ovl_inode_upper(struct inode *inode) |
25b7713a | 236 | { |
1d88f183 | 237 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
25b7713a | 238 | |
09d8b586 MS |
239 | return upperdentry ? d_inode(upperdentry) : NULL; |
240 | } | |
25b7713a | 241 | |
09d8b586 MS |
242 | struct inode *ovl_inode_lower(struct inode *inode) |
243 | { | |
244 | return OVL_I(inode)->lower; | |
245 | } | |
25b7713a | 246 | |
09d8b586 MS |
247 | struct inode *ovl_inode_real(struct inode *inode) |
248 | { | |
249 | return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); | |
25b7713a MS |
250 | } |
251 | ||
2664bd08 VG |
252 | /* Return inode which contains lower data. Do not return metacopy */ |
253 | struct inode *ovl_inode_lowerdata(struct inode *inode) | |
254 | { | |
255 | if (WARN_ON(!S_ISREG(inode->i_mode))) | |
256 | return NULL; | |
257 | ||
258 | return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); | |
259 | } | |
09d8b586 | 260 | |
4823d49c VG |
261 | /* Return real inode which contains data. Does not return metacopy inode */ |
262 | struct inode *ovl_inode_realdata(struct inode *inode) | |
263 | { | |
264 | struct inode *upperinode; | |
265 | ||
266 | upperinode = ovl_inode_upper(inode); | |
267 | if (upperinode && ovl_has_upperdata(inode)) | |
268 | return upperinode; | |
269 | ||
270 | return ovl_inode_lowerdata(inode); | |
271 | } | |
272 | ||
4edb83bb | 273 | struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) |
bbb1e54d | 274 | { |
4edb83bb | 275 | return OVL_I(inode)->cache; |
bbb1e54d MS |
276 | } |
277 | ||
4edb83bb | 278 | void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) |
bbb1e54d | 279 | { |
4edb83bb | 280 | OVL_I(inode)->cache = cache; |
bbb1e54d MS |
281 | } |
282 | ||
c62520a8 AG |
283 | void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) |
284 | { | |
285 | set_bit(flag, &OVL_E(dentry)->flags); | |
286 | } | |
287 | ||
288 | void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) | |
289 | { | |
290 | clear_bit(flag, &OVL_E(dentry)->flags); | |
291 | } | |
292 | ||
293 | bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) | |
294 | { | |
295 | return test_bit(flag, &OVL_E(dentry)->flags); | |
296 | } | |
297 | ||
bbb1e54d MS |
298 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
299 | { | |
c62520a8 | 300 | return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); |
bbb1e54d MS |
301 | } |
302 | ||
303 | bool ovl_dentry_is_whiteout(struct dentry *dentry) | |
304 | { | |
305 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); | |
306 | } | |
307 | ||
5cf5b477 | 308 | void ovl_dentry_set_opaque(struct dentry *dentry) |
bbb1e54d | 309 | { |
c62520a8 | 310 | ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); |
bbb1e54d MS |
311 | } |
312 | ||
55acc661 | 313 | /* |
aa3ff3c1 AG |
314 | * For hard links and decoded file handles, it's possible for ovl_dentry_upper() |
315 | * to return positive, while there's no actual upper alias for the inode. | |
316 | * Copy up code needs to know about the existence of the upper alias, so it | |
317 | * can't use ovl_dentry_upper(). | |
55acc661 MS |
318 | */ |
319 | bool ovl_dentry_has_upper_alias(struct dentry *dentry) | |
320 | { | |
c62520a8 | 321 | return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); |
55acc661 MS |
322 | } |
323 | ||
324 | void ovl_dentry_set_upper_alias(struct dentry *dentry) | |
325 | { | |
c62520a8 | 326 | ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); |
55acc661 MS |
327 | } |
328 | ||
0c288874 VG |
329 | static bool ovl_should_check_upperdata(struct inode *inode) |
330 | { | |
331 | if (!S_ISREG(inode->i_mode)) | |
332 | return false; | |
333 | ||
334 | if (!ovl_inode_lower(inode)) | |
335 | return false; | |
336 | ||
337 | return true; | |
338 | } | |
339 | ||
340 | bool ovl_has_upperdata(struct inode *inode) | |
341 | { | |
342 | if (!ovl_should_check_upperdata(inode)) | |
343 | return true; | |
344 | ||
345 | if (!ovl_test_flag(OVL_UPPERDATA, inode)) | |
346 | return false; | |
347 | /* | |
348 | * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of | |
349 | * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure | |
350 | * if setting of OVL_UPPERDATA is visible, then effects of writes | |
351 | * before that are visible too. | |
352 | */ | |
353 | smp_rmb(); | |
354 | return true; | |
355 | } | |
356 | ||
357 | void ovl_set_upperdata(struct inode *inode) | |
358 | { | |
359 | /* | |
360 | * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure | |
361 | * if OVL_UPPERDATA flag is visible, then effects of write operations | |
362 | * before it are visible as well. | |
363 | */ | |
364 | smp_wmb(); | |
365 | ovl_set_flag(OVL_UPPERDATA, inode); | |
366 | } | |
367 | ||
368 | /* Caller should hold ovl_inode->lock */ | |
369 | bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) | |
370 | { | |
371 | if (!ovl_open_flags_need_copy_up(flags)) | |
372 | return false; | |
373 | ||
374 | return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); | |
375 | } | |
376 | ||
377 | bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) | |
378 | { | |
379 | if (!ovl_open_flags_need_copy_up(flags)) | |
380 | return false; | |
381 | ||
382 | return !ovl_has_upperdata(d_inode(dentry)); | |
383 | } | |
384 | ||
a6c60655 MS |
385 | bool ovl_redirect_dir(struct super_block *sb) |
386 | { | |
387 | struct ovl_fs *ofs = sb->s_fs_info; | |
388 | ||
21a22878 | 389 | return ofs->config.redirect_dir && !ofs->noxattr; |
a6c60655 MS |
390 | } |
391 | ||
392 | const char *ovl_dentry_get_redirect(struct dentry *dentry) | |
393 | { | |
cf31c463 | 394 | return OVL_I(d_inode(dentry))->redirect; |
a6c60655 MS |
395 | } |
396 | ||
397 | void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) | |
398 | { | |
cf31c463 | 399 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
a6c60655 | 400 | |
cf31c463 MS |
401 | kfree(oi->redirect); |
402 | oi->redirect = redirect; | |
a6c60655 MS |
403 | } |
404 | ||
09d8b586 | 405 | void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, |
2664bd08 | 406 | struct dentry *lowerdentry, struct dentry *lowerdata) |
bbb1e54d | 407 | { |
695b46e7 AG |
408 | struct inode *realinode = d_inode(upperdentry ?: lowerdentry); |
409 | ||
09d8b586 MS |
410 | if (upperdentry) |
411 | OVL_I(inode)->__upperdentry = upperdentry; | |
412 | if (lowerdentry) | |
31747eda | 413 | OVL_I(inode)->lower = igrab(d_inode(lowerdentry)); |
2664bd08 VG |
414 | if (lowerdata) |
415 | OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata)); | |
bbb1e54d | 416 | |
695b46e7 | 417 | ovl_copyattr(realinode, inode); |
4f357295 | 418 | ovl_copyflags(realinode, inode); |
695b46e7 AG |
419 | if (!inode->i_ino) |
420 | inode->i_ino = realinode->i_ino; | |
bbb1e54d MS |
421 | } |
422 | ||
09d8b586 | 423 | void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) |
bbb1e54d | 424 | { |
09d8b586 | 425 | struct inode *upperinode = d_inode(upperdentry); |
e6d2ebdd | 426 | |
09d8b586 MS |
427 | WARN_ON(OVL_I(inode)->__upperdentry); |
428 | ||
25b7713a | 429 | /* |
09d8b586 | 430 | * Make sure upperdentry is consistent before making it visible |
25b7713a MS |
431 | */ |
432 | smp_wmb(); | |
09d8b586 | 433 | OVL_I(inode)->__upperdentry = upperdentry; |
31747eda | 434 | if (inode_unhashed(inode)) { |
695b46e7 AG |
435 | if (!inode->i_ino) |
436 | inode->i_ino = upperinode->i_ino; | |
25b7713a | 437 | inode->i_private = upperinode; |
bbb1e54d | 438 | __insert_inode_hash(inode, (unsigned long) upperinode); |
25b7713a | 439 | } |
bbb1e54d MS |
440 | } |
441 | ||
d9854c87 | 442 | static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity) |
bbb1e54d | 443 | { |
04a01ac7 | 444 | struct inode *inode = d_inode(dentry); |
bbb1e54d | 445 | |
04a01ac7 | 446 | WARN_ON(!inode_is_locked(inode)); |
4edb83bb MS |
447 | /* |
448 | * Version is used by readdir code to keep cache consistent. For merge | |
449 | * dirs all changes need to be noted. For non-merge dirs, cache only | |
450 | * contains impure (ones which have been copied up and have origins) | |
451 | * entries, so only need to note changes to impure entries. | |
452 | */ | |
453 | if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity) | |
454 | OVL_I(inode)->version++; | |
bbb1e54d MS |
455 | } |
456 | ||
d9854c87 MS |
457 | void ovl_dir_modified(struct dentry *dentry, bool impurity) |
458 | { | |
459 | /* Copy mtime/ctime */ | |
460 | ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry)); | |
461 | ||
462 | ovl_dentry_version_inc(dentry, impurity); | |
463 | } | |
464 | ||
bbb1e54d MS |
465 | u64 ovl_dentry_version_get(struct dentry *dentry) |
466 | { | |
04a01ac7 | 467 | struct inode *inode = d_inode(dentry); |
bbb1e54d | 468 | |
04a01ac7 MS |
469 | WARN_ON(!inode_is_locked(inode)); |
470 | return OVL_I(inode)->version; | |
bbb1e54d MS |
471 | } |
472 | ||
473 | bool ovl_is_whiteout(struct dentry *dentry) | |
474 | { | |
475 | struct inode *inode = dentry->d_inode; | |
476 | ||
477 | return inode && IS_WHITEOUT(inode); | |
478 | } | |
479 | ||
480 | struct file *ovl_path_open(struct path *path, int flags) | |
481 | { | |
482 | return dentry_open(path, flags | O_NOATIME, current_cred()); | |
483 | } | |
39d3d60a | 484 | |
0c288874 VG |
485 | /* Caller should hold ovl_inode->lock */ |
486 | static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) | |
487 | { | |
488 | bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; | |
489 | ||
490 | if (ovl_dentry_upper(dentry) && | |
491 | (ovl_dentry_has_upper_alias(dentry) || disconnected) && | |
492 | !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) | |
493 | return true; | |
494 | ||
495 | return false; | |
496 | } | |
497 | ||
498 | bool ovl_already_copied_up(struct dentry *dentry, int flags) | |
2002df85 VG |
499 | { |
500 | bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; | |
501 | ||
502 | /* | |
503 | * Check if copy-up has happened as well as for upper alias (in | |
504 | * case of hard links) is there. | |
505 | * | |
506 | * Both checks are lockless: | |
507 | * - false negatives: will recheck under oi->lock | |
508 | * - false positives: | |
509 | * + ovl_dentry_upper() uses memory barriers to ensure the | |
510 | * upper dentry is up-to-date | |
511 | * + ovl_dentry_has_upper_alias() relies on locking of | |
512 | * upper parent i_rwsem to prevent reordering copy-up | |
513 | * with rename. | |
514 | */ | |
515 | if (ovl_dentry_upper(dentry) && | |
0c288874 VG |
516 | (ovl_dentry_has_upper_alias(dentry) || disconnected) && |
517 | !ovl_dentry_needs_data_copy_up(dentry, flags)) | |
2002df85 VG |
518 | return true; |
519 | ||
520 | return false; | |
521 | } | |
522 | ||
0c288874 | 523 | int ovl_copy_up_start(struct dentry *dentry, int flags) |
39d3d60a | 524 | { |
a015dafc | 525 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
39d3d60a AG |
526 | int err; |
527 | ||
a015dafc | 528 | err = mutex_lock_interruptible(&oi->lock); |
0c288874 | 529 | if (!err && ovl_already_copied_up_locked(dentry, flags)) { |
a015dafc AG |
530 | err = 1; /* Already copied up */ |
531 | mutex_unlock(&oi->lock); | |
39d3d60a | 532 | } |
39d3d60a AG |
533 | |
534 | return err; | |
535 | } | |
536 | ||
537 | void ovl_copy_up_end(struct dentry *dentry) | |
538 | { | |
a015dafc | 539 | mutex_unlock(&OVL_I(d_inode(dentry))->lock); |
39d3d60a | 540 | } |
82b749b2 | 541 | |
b79e05aa AG |
542 | bool ovl_check_origin_xattr(struct dentry *dentry) |
543 | { | |
544 | int res; | |
545 | ||
546 | res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); | |
547 | ||
548 | /* Zero size value means "copied up but origin unknown" */ | |
549 | if (res >= 0) | |
550 | return true; | |
551 | ||
552 | return false; | |
553 | } | |
554 | ||
f3a15685 AG |
555 | bool ovl_check_dir_xattr(struct dentry *dentry, const char *name) |
556 | { | |
557 | int res; | |
558 | char val; | |
559 | ||
560 | if (!d_is_dir(dentry)) | |
561 | return false; | |
562 | ||
563 | res = vfs_getxattr(dentry, name, &val, 1); | |
564 | if (res == 1 && val == 'y') | |
565 | return true; | |
566 | ||
567 | return false; | |
568 | } | |
569 | ||
82b749b2 AG |
570 | int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, |
571 | const char *name, const void *value, size_t size, | |
572 | int xerr) | |
573 | { | |
574 | int err; | |
575 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
576 | ||
577 | if (ofs->noxattr) | |
578 | return xerr; | |
579 | ||
580 | err = ovl_do_setxattr(upperdentry, name, value, size, 0); | |
581 | ||
582 | if (err == -EOPNOTSUPP) { | |
583 | pr_warn("overlayfs: cannot set %s xattr on upper\n", name); | |
584 | ofs->noxattr = true; | |
585 | return xerr; | |
586 | } | |
587 | ||
588 | return err; | |
589 | } | |
f3a15685 AG |
590 | |
591 | int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) | |
592 | { | |
593 | int err; | |
f3a15685 | 594 | |
13c72075 | 595 | if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) |
f3a15685 AG |
596 | return 0; |
597 | ||
598 | /* | |
599 | * Do not fail when upper doesn't support xattrs. | |
600 | * Upper inodes won't have origin nor redirect xattr anyway. | |
601 | */ | |
602 | err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, | |
603 | "y", 1, 0); | |
604 | if (!err) | |
13c72075 | 605 | ovl_set_flag(OVL_IMPURE, d_inode(dentry)); |
f3a15685 AG |
606 | |
607 | return err; | |
608 | } | |
13c72075 MS |
609 | |
610 | void ovl_set_flag(unsigned long flag, struct inode *inode) | |
611 | { | |
612 | set_bit(flag, &OVL_I(inode)->flags); | |
613 | } | |
614 | ||
4edb83bb MS |
615 | void ovl_clear_flag(unsigned long flag, struct inode *inode) |
616 | { | |
617 | clear_bit(flag, &OVL_I(inode)->flags); | |
618 | } | |
619 | ||
13c72075 MS |
620 | bool ovl_test_flag(unsigned long flag, struct inode *inode) |
621 | { | |
622 | return test_bit(flag, &OVL_I(inode)->flags); | |
623 | } | |
ad0af710 AG |
624 | |
625 | /** | |
626 | * Caller must hold a reference to inode to prevent it from being freed while | |
627 | * it is marked inuse. | |
628 | */ | |
629 | bool ovl_inuse_trylock(struct dentry *dentry) | |
630 | { | |
631 | struct inode *inode = d_inode(dentry); | |
632 | bool locked = false; | |
633 | ||
634 | spin_lock(&inode->i_lock); | |
635 | if (!(inode->i_state & I_OVL_INUSE)) { | |
636 | inode->i_state |= I_OVL_INUSE; | |
637 | locked = true; | |
638 | } | |
639 | spin_unlock(&inode->i_lock); | |
640 | ||
641 | return locked; | |
642 | } | |
643 | ||
644 | void ovl_inuse_unlock(struct dentry *dentry) | |
645 | { | |
646 | if (dentry) { | |
647 | struct inode *inode = d_inode(dentry); | |
648 | ||
649 | spin_lock(&inode->i_lock); | |
650 | WARN_ON(!(inode->i_state & I_OVL_INUSE)); | |
651 | inode->i_state &= ~I_OVL_INUSE; | |
652 | spin_unlock(&inode->i_lock); | |
653 | } | |
654 | } | |
5f8415d6 | 655 | |
24b33ee1 AG |
656 | /* |
657 | * Does this overlay dentry need to be indexed on copy up? | |
658 | */ | |
659 | bool ovl_need_index(struct dentry *dentry) | |
660 | { | |
661 | struct dentry *lower = ovl_dentry_lower(dentry); | |
662 | ||
663 | if (!lower || !ovl_indexdir(dentry->d_sb)) | |
664 | return false; | |
665 | ||
fbd2d207 | 666 | /* Index all files for NFS export and consistency verification */ |
016b720f | 667 | if (ovl_index_all(dentry->d_sb)) |
fbd2d207 AG |
668 | return true; |
669 | ||
24b33ee1 AG |
670 | /* Index only lower hardlinks on copy up */ |
671 | if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) | |
672 | return true; | |
673 | ||
674 | return false; | |
675 | } | |
676 | ||
9f4ec904 | 677 | /* Caller must hold OVL_I(inode)->lock */ |
caf70cb2 AG |
678 | static void ovl_cleanup_index(struct dentry *dentry) |
679 | { | |
e7dd0e71 AG |
680 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
681 | struct inode *dir = indexdir->d_inode; | |
caf70cb2 AG |
682 | struct dentry *lowerdentry = ovl_dentry_lower(dentry); |
683 | struct dentry *upperdentry = ovl_dentry_upper(dentry); | |
684 | struct dentry *index = NULL; | |
685 | struct inode *inode; | |
686 | struct qstr name; | |
687 | int err; | |
688 | ||
689 | err = ovl_get_index_name(lowerdentry, &name); | |
690 | if (err) | |
691 | goto fail; | |
692 | ||
693 | inode = d_inode(upperdentry); | |
89a17556 | 694 | if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { |
caf70cb2 AG |
695 | pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", |
696 | upperdentry, inode->i_ino, inode->i_nlink); | |
697 | /* | |
698 | * We either have a bug with persistent union nlink or a lower | |
699 | * hardlink was added while overlay is mounted. Adding a lower | |
700 | * hardlink and then unlinking all overlay hardlinks would drop | |
701 | * overlay nlink to zero before all upper inodes are unlinked. | |
702 | * As a safety measure, when that situation is detected, set | |
703 | * the overlay nlink to the index inode nlink minus one for the | |
704 | * index entry itself. | |
705 | */ | |
706 | set_nlink(d_inode(dentry), inode->i_nlink - 1); | |
707 | ovl_set_nlink_upper(dentry); | |
708 | goto out; | |
709 | } | |
710 | ||
711 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
e7dd0e71 | 712 | index = lookup_one_len(name.name, indexdir, name.len); |
caf70cb2 | 713 | err = PTR_ERR(index); |
e7dd0e71 | 714 | if (IS_ERR(index)) { |
9f4ec904 | 715 | index = NULL; |
e7dd0e71 AG |
716 | } else if (ovl_index_all(dentry->d_sb)) { |
717 | /* Whiteout orphan index to block future open by handle */ | |
718 | err = ovl_cleanup_and_whiteout(indexdir, dir, index); | |
719 | } else { | |
720 | /* Cleanup orphan index entries */ | |
721 | err = ovl_cleanup(dir, index); | |
722 | } | |
9f4ec904 | 723 | |
caf70cb2 AG |
724 | inode_unlock(dir); |
725 | if (err) | |
726 | goto fail; | |
727 | ||
728 | out: | |
729 | dput(index); | |
730 | return; | |
731 | ||
732 | fail: | |
733 | pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err); | |
734 | goto out; | |
735 | } | |
736 | ||
5f8415d6 AG |
737 | /* |
738 | * Operations that change overlay inode and upper inode nlink need to be | |
739 | * synchronized with copy up for persistent nlink accounting. | |
740 | */ | |
741 | int ovl_nlink_start(struct dentry *dentry, bool *locked) | |
742 | { | |
743 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); | |
744 | const struct cred *old_cred; | |
745 | int err; | |
746 | ||
89a17556 | 747 | if (!d_inode(dentry)) |
5f8415d6 AG |
748 | return 0; |
749 | ||
750 | /* | |
751 | * With inodes index is enabled, we store the union overlay nlink | |
24b33ee1 | 752 | * in an xattr on the index inode. When whiting out an indexed lower, |
5f8415d6 AG |
753 | * we need to decrement the overlay persistent nlink, but before the |
754 | * first copy up, we have no upper index inode to store the xattr. | |
755 | * | |
24b33ee1 | 756 | * As a workaround, before whiteout/rename over an indexed lower, |
5f8415d6 AG |
757 | * copy up to create the upper index. Creating the upper index will |
758 | * initialize the overlay nlink, so it could be dropped if unlink | |
759 | * or rename succeeds. | |
760 | * | |
761 | * TODO: implement metadata only index copy up when called with | |
762 | * ovl_copy_up_flags(dentry, O_PATH). | |
763 | */ | |
24b33ee1 | 764 | if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { |
5f8415d6 AG |
765 | err = ovl_copy_up(dentry); |
766 | if (err) | |
767 | return err; | |
768 | } | |
769 | ||
770 | err = mutex_lock_interruptible(&oi->lock); | |
771 | if (err) | |
772 | return err; | |
773 | ||
89a17556 | 774 | if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
5f8415d6 AG |
775 | goto out; |
776 | ||
777 | old_cred = ovl_override_creds(dentry->d_sb); | |
778 | /* | |
779 | * The overlay inode nlink should be incremented/decremented IFF the | |
780 | * upper operation succeeds, along with nlink change of upper inode. | |
781 | * Therefore, before link/unlink/rename, we store the union nlink | |
782 | * value relative to the upper inode nlink in an upper inode xattr. | |
783 | */ | |
784 | err = ovl_set_nlink_upper(dentry); | |
785 | revert_creds(old_cred); | |
786 | ||
787 | out: | |
788 | if (err) | |
789 | mutex_unlock(&oi->lock); | |
790 | else | |
791 | *locked = true; | |
792 | ||
793 | return err; | |
794 | } | |
795 | ||
796 | void ovl_nlink_end(struct dentry *dentry, bool locked) | |
797 | { | |
caf70cb2 AG |
798 | if (locked) { |
799 | if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) && | |
800 | d_inode(dentry)->i_nlink == 0) { | |
801 | const struct cred *old_cred; | |
802 | ||
803 | old_cred = ovl_override_creds(dentry->d_sb); | |
804 | ovl_cleanup_index(dentry); | |
805 | revert_creds(old_cred); | |
806 | } | |
807 | ||
5f8415d6 | 808 | mutex_unlock(&OVL_I(d_inode(dentry))->lock); |
caf70cb2 | 809 | } |
5f8415d6 | 810 | } |
5820dc08 AG |
811 | |
812 | int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) | |
813 | { | |
814 | /* Workdir should not be the same as upperdir */ | |
815 | if (workdir == upperdir) | |
816 | goto err; | |
817 | ||
818 | /* Workdir should not be subdir of upperdir and vice versa */ | |
819 | if (lock_rename(workdir, upperdir) != NULL) | |
820 | goto err_unlock; | |
821 | ||
822 | return 0; | |
823 | ||
824 | err_unlock: | |
825 | unlock_rename(workdir, upperdir); | |
826 | err: | |
827 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); | |
828 | return -EIO; | |
829 | } | |
9d3dfea3 VG |
830 | |
831 | /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ | |
832 | int ovl_check_metacopy_xattr(struct dentry *dentry) | |
833 | { | |
834 | int res; | |
835 | ||
836 | /* Only regular files can have metacopy xattr */ | |
837 | if (!S_ISREG(d_inode(dentry)->i_mode)) | |
838 | return 0; | |
839 | ||
840 | res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0); | |
841 | if (res < 0) { | |
842 | if (res == -ENODATA || res == -EOPNOTSUPP) | |
843 | return 0; | |
844 | goto out; | |
845 | } | |
846 | ||
847 | return 1; | |
848 | out: | |
849 | pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res); | |
850 | return res; | |
851 | } | |
67d756c2 VG |
852 | |
853 | bool ovl_is_metacopy_dentry(struct dentry *dentry) | |
854 | { | |
855 | struct ovl_entry *oe = dentry->d_fsdata; | |
856 | ||
857 | if (!d_is_reg(dentry)) | |
858 | return false; | |
859 | ||
860 | if (ovl_dentry_upper(dentry)) { | |
861 | if (!ovl_has_upperdata(d_inode(dentry))) | |
862 | return true; | |
863 | return false; | |
864 | } | |
865 | ||
866 | return (oe->numlower > 1); | |
867 | } |