1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 *
4 * Copyright (C) 2011 Novell Inc.
5 */
6
7 #include <linux/fs.h>
8 #include <linux/namei.h>
9 #include <linux/xattr.h>
10 #include <linux/security.h>
11 #include <linux/cred.h>
12 #include <linux/module.h>
13 #include <linux/posix_acl.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/atomic.h>
16 #include <linux/ratelimit.h>
17 #include <linux/backing-file.h>
18 #include "overlayfs.h"
19
20 static unsigned short ovl_redirect_max = 256;
21 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
22 MODULE_PARM_DESC(redirect_max,
23 "Maximum length of absolute redirect xattr value");
24
25 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
26
ovl_cleanup_locked(struct ovl_fs * ofs,struct inode * wdir,struct dentry * wdentry)27 static int ovl_cleanup_locked(struct ovl_fs *ofs, struct inode *wdir,
28 struct dentry *wdentry)
29 {
30 int err;
31
32 dget(wdentry);
33 if (d_is_dir(wdentry))
34 err = ovl_do_rmdir(ofs, wdir, wdentry);
35 else
36 err = ovl_do_unlink(ofs, wdir, wdentry);
37 dput(wdentry);
38
39 if (err) {
40 pr_err("cleanup of '%pd2' failed (%i)\n",
41 wdentry, err);
42 }
43
44 return err;
45 }
46
ovl_cleanup(struct ovl_fs * ofs,struct dentry * workdir,struct dentry * wdentry)47 int ovl_cleanup(struct ovl_fs *ofs, struct dentry *workdir,
48 struct dentry *wdentry)
49 {
50 int err;
51
52 err = ovl_parent_lock(workdir, wdentry);
53 if (err)
54 return err;
55
56 ovl_cleanup_locked(ofs, workdir->d_inode, wdentry);
57 ovl_parent_unlock(workdir);
58
59 return 0;
60 }
61
ovl_lookup_temp(struct ovl_fs * ofs,struct dentry * workdir)62 struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir)
63 {
64 struct dentry *temp;
65 char name[20];
66 static atomic_t temp_id = ATOMIC_INIT(0);
67
68 /* counter is allowed to wrap, since temp dentries are ephemeral */
69 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
70
71 temp = ovl_lookup_upper(ofs, name, workdir, strlen(name));
72 if (!IS_ERR(temp) && temp->d_inode) {
73 pr_err("workdir/%s already exists\n", name);
74 dput(temp);
75 temp = ERR_PTR(-EIO);
76 }
77
78 return temp;
79 }
80
ovl_whiteout(struct ovl_fs * ofs)81 static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
82 {
83 int err;
84 struct dentry *whiteout;
85 struct dentry *workdir = ofs->workdir;
86 struct inode *wdir = workdir->d_inode;
87
88 guard(mutex)(&ofs->whiteout_lock);
89
90 if (!ofs->whiteout) {
91 inode_lock_nested(wdir, I_MUTEX_PARENT);
92 whiteout = ovl_lookup_temp(ofs, workdir);
93 if (!IS_ERR(whiteout)) {
94 err = ovl_do_whiteout(ofs, wdir, whiteout);
95 if (err) {
96 dput(whiteout);
97 whiteout = ERR_PTR(err);
98 }
99 }
100 inode_unlock(wdir);
101 if (IS_ERR(whiteout))
102 return whiteout;
103 ofs->whiteout = whiteout;
104 }
105
106 if (!ofs->no_shared_whiteout) {
107 inode_lock_nested(wdir, I_MUTEX_PARENT);
108 whiteout = ovl_lookup_temp(ofs, workdir);
109 if (!IS_ERR(whiteout)) {
110 err = ovl_do_link(ofs, ofs->whiteout, wdir, whiteout);
111 if (err) {
112 dput(whiteout);
113 whiteout = ERR_PTR(err);
114 }
115 }
116 inode_unlock(wdir);
117 if (!IS_ERR(whiteout))
118 return whiteout;
119 if (PTR_ERR(whiteout) != -EMLINK) {
120 pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%lu)\n",
121 ofs->whiteout->d_inode->i_nlink,
122 PTR_ERR(whiteout));
123 ofs->no_shared_whiteout = true;
124 }
125 }
126 whiteout = ofs->whiteout;
127 ofs->whiteout = NULL;
128 return whiteout;
129 }
130
ovl_cleanup_and_whiteout(struct ovl_fs * ofs,struct dentry * dir,struct dentry * dentry)131 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct dentry *dir,
132 struct dentry *dentry)
133 {
134 struct dentry *whiteout;
135 int err;
136 int flags = 0;
137
138 whiteout = ovl_whiteout(ofs);
139 err = PTR_ERR(whiteout);
140 if (IS_ERR(whiteout))
141 return err;
142
143 if (d_is_dir(dentry))
144 flags = RENAME_EXCHANGE;
145
146 err = ovl_lock_rename_workdir(ofs->workdir, whiteout, dir, dentry);
147 if (!err) {
148 err = ovl_do_rename(ofs, ofs->workdir, whiteout, dir, dentry, flags);
149 unlock_rename(ofs->workdir, dir);
150 }
151 if (err)
152 goto kill_whiteout;
153 if (flags)
154 ovl_cleanup(ofs, ofs->workdir, dentry);
155
156 out:
157 dput(whiteout);
158 return err;
159
160 kill_whiteout:
161 ovl_cleanup(ofs, ofs->workdir, whiteout);
162 goto out;
163 }
164
ovl_create_real(struct ovl_fs * ofs,struct dentry * parent,struct dentry * newdentry,struct ovl_cattr * attr)165 struct dentry *ovl_create_real(struct ovl_fs *ofs, struct dentry *parent,
166 struct dentry *newdentry, struct ovl_cattr *attr)
167 {
168 struct inode *dir = parent->d_inode;
169 int err;
170
171 if (IS_ERR(newdentry))
172 return newdentry;
173
174 err = -ESTALE;
175 if (newdentry->d_inode)
176 goto out;
177
178 if (attr->hardlink) {
179 err = ovl_do_link(ofs, attr->hardlink, dir, newdentry);
180 } else {
181 switch (attr->mode & S_IFMT) {
182 case S_IFREG:
183 err = ovl_do_create(ofs, dir, newdentry, attr->mode);
184 break;
185
186 case S_IFDIR:
187 /* mkdir is special... */
188 newdentry = ovl_do_mkdir(ofs, dir, newdentry, attr->mode);
189 err = PTR_ERR_OR_ZERO(newdentry);
190 break;
191
192 case S_IFCHR:
193 case S_IFBLK:
194 case S_IFIFO:
195 case S_IFSOCK:
196 err = ovl_do_mknod(ofs, dir, newdentry, attr->mode,
197 attr->rdev);
198 break;
199
200 case S_IFLNK:
201 err = ovl_do_symlink(ofs, dir, newdentry, attr->link);
202 break;
203
204 default:
205 err = -EPERM;
206 }
207 }
208 if (!err && WARN_ON(!newdentry->d_inode)) {
209 /*
210 * Not quite sure if non-instantiated dentry is legal or not.
211 * VFS doesn't seem to care so check and warn here.
212 */
213 err = -EIO;
214 }
215 out:
216 if (err) {
217 if (!IS_ERR(newdentry))
218 dput(newdentry);
219 return ERR_PTR(err);
220 }
221 return newdentry;
222 }
223
ovl_create_temp(struct ovl_fs * ofs,struct dentry * workdir,struct ovl_cattr * attr)224 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
225 struct ovl_cattr *attr)
226 {
227 struct dentry *ret;
228 inode_lock(workdir->d_inode);
229 ret = ovl_create_real(ofs, workdir,
230 ovl_lookup_temp(ofs, workdir), attr);
231 inode_unlock(workdir->d_inode);
232 return ret;
233 }
234
ovl_set_opaque_xerr(struct dentry * dentry,struct dentry * upper,int xerr)235 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
236 int xerr)
237 {
238 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
239 int err;
240
241 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
242 if (!err)
243 ovl_dentry_set_opaque(dentry);
244
245 return err;
246 }
247
ovl_set_opaque(struct dentry * dentry,struct dentry * upperdentry)248 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
249 {
250 /*
251 * Fail with -EIO when trying to create opaque dir and upper doesn't
252 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
253 * return a specific error for noxattr case.
254 */
255 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
256 }
257
258 /*
259 * Common operations required to be done after creation of file on upper.
260 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
261 * may not use to instantiate the new dentry.
262 */
ovl_instantiate(struct dentry * dentry,struct inode * inode,struct dentry * newdentry,bool hardlink,struct file * tmpfile)263 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
264 struct dentry *newdentry, bool hardlink, struct file *tmpfile)
265 {
266 struct ovl_inode_params oip = {
267 .upperdentry = newdentry,
268 .newinode = inode,
269 };
270
271 ovl_dentry_set_upper_alias(dentry);
272 ovl_dentry_init_reval(dentry, newdentry, NULL);
273
274 if (!hardlink) {
275 /*
276 * ovl_obtain_alias() can be called after ovl_create_real()
277 * and before we get here, so we may get an inode from cache
278 * with the same real upperdentry that is not the inode we
279 * pre-allocated. In this case we will use the cached inode
280 * to instantiate the new dentry.
281 *
282 * XXX: if we ever use ovl_obtain_alias() to decode directory
283 * file handles, need to use ovl_get_inode_locked() and
284 * d_instantiate_new() here to prevent from creating two
285 * hashed directory inode aliases. We then need to return
286 * the obtained alias to ovl_mkdir().
287 */
288 inode = ovl_get_inode(dentry->d_sb, &oip);
289 if (IS_ERR(inode))
290 return PTR_ERR(inode);
291 if (inode == oip.newinode)
292 ovl_set_flag(OVL_UPPERDATA, inode);
293 } else {
294 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
295 dput(newdentry);
296 inc_nlink(inode);
297 }
298
299 if (tmpfile)
300 d_mark_tmpfile(tmpfile, inode);
301
302 d_instantiate(dentry, inode);
303 if (inode != oip.newinode) {
304 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
305 dentry);
306 }
307
308 /* Force lookup of new upper hardlink to find its lower */
309 if (hardlink)
310 d_drop(dentry);
311
312 return 0;
313 }
314
ovl_type_merge(struct dentry * dentry)315 static bool ovl_type_merge(struct dentry *dentry)
316 {
317 return OVL_TYPE_MERGE(ovl_path_type(dentry));
318 }
319
ovl_type_origin(struct dentry * dentry)320 static bool ovl_type_origin(struct dentry *dentry)
321 {
322 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
323 }
324
ovl_create_upper(struct dentry * dentry,struct inode * inode,struct ovl_cattr * attr)325 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
326 struct ovl_cattr *attr)
327 {
328 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
329 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
330 struct inode *udir = upperdir->d_inode;
331 struct dentry *newdentry;
332 int err;
333
334 inode_lock_nested(udir, I_MUTEX_PARENT);
335 newdentry = ovl_create_real(ofs, upperdir,
336 ovl_lookup_upper(ofs, dentry->d_name.name,
337 upperdir, dentry->d_name.len),
338 attr);
339 inode_unlock(udir);
340 if (IS_ERR(newdentry))
341 return PTR_ERR(newdentry);
342
343 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) &&
344 !ovl_allow_offline_changes(ofs)) {
345 /* Setting opaque here is just an optimization, allow to fail */
346 ovl_set_opaque(dentry, newdentry);
347 }
348
349 ovl_dir_modified(dentry->d_parent, false);
350 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink, NULL);
351 if (err)
352 goto out_cleanup;
353 return 0;
354
355 out_cleanup:
356 ovl_cleanup(ofs, upperdir, newdentry);
357 dput(newdentry);
358 return err;
359 }
360
ovl_clear_empty(struct dentry * dentry,struct list_head * list)361 static struct dentry *ovl_clear_empty(struct dentry *dentry,
362 struct list_head *list)
363 {
364 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
365 struct dentry *workdir = ovl_workdir(dentry);
366 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
367 struct path upperpath;
368 struct dentry *upper;
369 struct dentry *opaquedir;
370 struct kstat stat;
371 int err;
372
373 if (WARN_ON(!workdir))
374 return ERR_PTR(-EROFS);
375
376 ovl_path_upper(dentry, &upperpath);
377 err = vfs_getattr(&upperpath, &stat,
378 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
379 if (err)
380 goto out;
381
382 err = -ESTALE;
383 if (!S_ISDIR(stat.mode))
384 goto out;
385 upper = upperpath.dentry;
386
387 opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode));
388 err = PTR_ERR(opaquedir);
389 if (IS_ERR(opaquedir))
390 goto out;
391
392 err = ovl_lock_rename_workdir(workdir, opaquedir, upperdir, upper);
393 if (err)
394 goto out_cleanup_unlocked;
395
396 err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
397 if (err)
398 goto out_cleanup;
399
400 err = ovl_set_opaque(dentry, opaquedir);
401 if (err)
402 goto out_cleanup;
403
404 inode_lock(opaquedir->d_inode);
405 err = ovl_set_attr(ofs, opaquedir, &stat);
406 inode_unlock(opaquedir->d_inode);
407 if (err)
408 goto out_cleanup;
409
410 err = ovl_do_rename(ofs, workdir, opaquedir, upperdir, upper, RENAME_EXCHANGE);
411 unlock_rename(workdir, upperdir);
412 if (err)
413 goto out_cleanup_unlocked;
414
415 ovl_cleanup_whiteouts(ofs, upper, list);
416 ovl_cleanup(ofs, workdir, upper);
417
418 /* dentry's upper doesn't match now, get rid of it */
419 d_drop(dentry);
420
421 return opaquedir;
422
423 out_cleanup:
424 unlock_rename(workdir, upperdir);
425 out_cleanup_unlocked:
426 ovl_cleanup(ofs, workdir, opaquedir);
427 dput(opaquedir);
428 out:
429 return ERR_PTR(err);
430 }
431
ovl_set_upper_acl(struct ovl_fs * ofs,struct dentry * upperdentry,const char * acl_name,struct posix_acl * acl)432 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry,
433 const char *acl_name, struct posix_acl *acl)
434 {
435 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
436 return 0;
437
438 return ovl_do_set_acl(ofs, upperdentry, acl_name, acl);
439 }
440
ovl_create_over_whiteout(struct dentry * dentry,struct inode * inode,struct ovl_cattr * cattr)441 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
442 struct ovl_cattr *cattr)
443 {
444 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
445 struct dentry *workdir = ovl_workdir(dentry);
446 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
447 struct dentry *upper;
448 struct dentry *newdentry;
449 int err;
450 struct posix_acl *acl, *default_acl;
451 bool hardlink = !!cattr->hardlink;
452
453 if (WARN_ON(!workdir))
454 return -EROFS;
455
456 if (!hardlink) {
457 err = posix_acl_create(dentry->d_parent->d_inode,
458 &cattr->mode, &default_acl, &acl);
459 if (err)
460 return err;
461 }
462
463 upper = ovl_lookup_upper_unlocked(ofs, dentry->d_name.name, upperdir,
464 dentry->d_name.len);
465 err = PTR_ERR(upper);
466 if (IS_ERR(upper))
467 goto out;
468
469 err = -ESTALE;
470 if (d_is_negative(upper) || !ovl_upper_is_whiteout(ofs, upper))
471 goto out_dput;
472
473 newdentry = ovl_create_temp(ofs, workdir, cattr);
474 err = PTR_ERR(newdentry);
475 if (IS_ERR(newdentry))
476 goto out_dput;
477
478 err = ovl_lock_rename_workdir(workdir, newdentry, upperdir, upper);
479 if (err)
480 goto out_cleanup_unlocked;
481
482 /*
483 * mode could have been mutilated due to umask (e.g. sgid directory)
484 */
485 if (!hardlink &&
486 !S_ISLNK(cattr->mode) &&
487 newdentry->d_inode->i_mode != cattr->mode) {
488 struct iattr attr = {
489 .ia_valid = ATTR_MODE,
490 .ia_mode = cattr->mode,
491 };
492 inode_lock(newdentry->d_inode);
493 err = ovl_do_notify_change(ofs, newdentry, &attr);
494 inode_unlock(newdentry->d_inode);
495 if (err)
496 goto out_cleanup;
497 }
498 if (!hardlink) {
499 err = ovl_set_upper_acl(ofs, newdentry,
500 XATTR_NAME_POSIX_ACL_ACCESS, acl);
501 if (err)
502 goto out_cleanup;
503
504 err = ovl_set_upper_acl(ofs, newdentry,
505 XATTR_NAME_POSIX_ACL_DEFAULT, default_acl);
506 if (err)
507 goto out_cleanup;
508 }
509
510 if (!hardlink && S_ISDIR(cattr->mode)) {
511 err = ovl_set_opaque(dentry, newdentry);
512 if (err)
513 goto out_cleanup;
514
515 err = ovl_do_rename(ofs, workdir, newdentry, upperdir, upper,
516 RENAME_EXCHANGE);
517 unlock_rename(workdir, upperdir);
518 if (err)
519 goto out_cleanup_unlocked;
520
521 ovl_cleanup(ofs, workdir, upper);
522 } else {
523 err = ovl_do_rename(ofs, workdir, newdentry, upperdir, upper, 0);
524 unlock_rename(workdir, upperdir);
525 if (err)
526 goto out_cleanup_unlocked;
527 }
528 ovl_dir_modified(dentry->d_parent, false);
529 err = ovl_instantiate(dentry, inode, newdentry, hardlink, NULL);
530 if (err) {
531 ovl_cleanup(ofs, upperdir, newdentry);
532 dput(newdentry);
533 }
534 out_dput:
535 dput(upper);
536 out:
537 if (!hardlink) {
538 posix_acl_release(acl);
539 posix_acl_release(default_acl);
540 }
541 return err;
542
543 out_cleanup:
544 unlock_rename(workdir, upperdir);
545 out_cleanup_unlocked:
546 ovl_cleanup(ofs, workdir, newdentry);
547 dput(newdentry);
548 goto out_dput;
549 }
550
ovl_setup_cred_for_create(struct dentry * dentry,struct inode * inode,umode_t mode,const struct cred * old_cred)551 static const struct cred *ovl_setup_cred_for_create(struct dentry *dentry,
552 struct inode *inode,
553 umode_t mode,
554 const struct cred *old_cred)
555 {
556 int err;
557 struct cred *override_cred;
558
559 override_cred = prepare_creds();
560 if (!override_cred)
561 return ERR_PTR(-ENOMEM);
562
563 override_cred->fsuid = inode->i_uid;
564 override_cred->fsgid = inode->i_gid;
565 err = security_dentry_create_files_as(dentry, mode, &dentry->d_name,
566 old_cred, override_cred);
567 if (err) {
568 put_cred(override_cred);
569 return ERR_PTR(err);
570 }
571
572 /*
573 * Caller is going to match this with revert_creds() and drop
574 * referenec on the returned creds.
575 * We must be called with creator creds already, otherwise we risk
576 * leaking creds.
577 */
578 old_cred = override_creds(override_cred);
579 WARN_ON_ONCE(old_cred != ovl_creds(dentry->d_sb));
580
581 return override_cred;
582 }
583
ovl_create_or_link(struct dentry * dentry,struct inode * inode,struct ovl_cattr * attr,bool origin)584 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
585 struct ovl_cattr *attr, bool origin)
586 {
587 int err;
588 const struct cred *old_cred, *new_cred = NULL;
589 struct dentry *parent = dentry->d_parent;
590
591 old_cred = ovl_override_creds(dentry->d_sb);
592
593 /*
594 * When linking a file with copy up origin into a new parent, mark the
595 * new parent dir "impure".
596 */
597 if (origin) {
598 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
599 if (err)
600 goto out_revert_creds;
601 }
602
603 if (!attr->hardlink) {
604 /*
605 * In the creation cases(create, mkdir, mknod, symlink),
606 * ovl should transfer current's fs{u,g}id to underlying
607 * fs. Because underlying fs want to initialize its new
608 * inode owner using current's fs{u,g}id. And in this
609 * case, the @inode is a new inode that is initialized
610 * in inode_init_owner() to current's fs{u,g}id. So use
611 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
612 *
613 * But in the other hardlink case, ovl_link() does not
614 * create a new inode, so just use the ovl mounter's
615 * fs{u,g}id.
616 */
617 new_cred = ovl_setup_cred_for_create(dentry, inode, attr->mode,
618 old_cred);
619 err = PTR_ERR(new_cred);
620 if (IS_ERR(new_cred)) {
621 new_cred = NULL;
622 goto out_revert_creds;
623 }
624 }
625
626 if (!ovl_dentry_is_whiteout(dentry))
627 err = ovl_create_upper(dentry, inode, attr);
628 else
629 err = ovl_create_over_whiteout(dentry, inode, attr);
630
631 out_revert_creds:
632 ovl_revert_creds(old_cred);
633 put_cred(new_cred);
634 return err;
635 }
636
ovl_create_object(struct dentry * dentry,int mode,dev_t rdev,const char * link)637 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
638 const char *link)
639 {
640 int err;
641 struct inode *inode;
642 struct ovl_cattr attr = {
643 .rdev = rdev,
644 .link = link,
645 };
646
647 err = ovl_copy_up(dentry->d_parent);
648 if (err)
649 return err;
650
651 err = ovl_want_write(dentry);
652 if (err)
653 goto out;
654
655 /* Preallocate inode to be used by ovl_get_inode() */
656 err = -ENOMEM;
657 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
658 if (!inode)
659 goto out_drop_write;
660
661 spin_lock(&inode->i_lock);
662 inode->i_state |= I_CREATING;
663 spin_unlock(&inode->i_lock);
664
665 inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode);
666 attr.mode = inode->i_mode;
667
668 err = ovl_create_or_link(dentry, inode, &attr, false);
669 /* Did we end up using the preallocated inode? */
670 if (inode != d_inode(dentry))
671 iput(inode);
672
673 out_drop_write:
674 ovl_drop_write(dentry);
675 out:
676 return err;
677 }
678
ovl_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)679 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir,
680 struct dentry *dentry, umode_t mode, bool excl)
681 {
682 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
683 }
684
ovl_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)685 static struct dentry *ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir,
686 struct dentry *dentry, umode_t mode)
687 {
688 return ERR_PTR(ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL));
689 }
690
ovl_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)691 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir,
692 struct dentry *dentry, umode_t mode, dev_t rdev)
693 {
694 /* Don't allow creation of "whiteout" on overlay */
695 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
696 return -EPERM;
697
698 return ovl_create_object(dentry, mode, rdev, NULL);
699 }
700
ovl_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * link)701 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir,
702 struct dentry *dentry, const char *link)
703 {
704 return ovl_create_object(dentry, S_IFLNK, 0, link);
705 }
706
ovl_set_link_redirect(struct dentry * dentry)707 static int ovl_set_link_redirect(struct dentry *dentry)
708 {
709 const struct cred *old_cred;
710 int err;
711
712 old_cred = ovl_override_creds(dentry->d_sb);
713 err = ovl_set_redirect(dentry, false);
714 ovl_revert_creds(old_cred);
715
716 return err;
717 }
718
ovl_link(struct dentry * old,struct inode * newdir,struct dentry * new)719 static int ovl_link(struct dentry *old, struct inode *newdir,
720 struct dentry *new)
721 {
722 int err;
723 struct inode *inode;
724
725 err = ovl_copy_up(old);
726 if (err)
727 goto out;
728
729 err = ovl_copy_up(new->d_parent);
730 if (err)
731 goto out;
732
733 err = ovl_nlink_start(old);
734 if (err)
735 goto out;
736
737 if (ovl_is_metacopy_dentry(old)) {
738 err = ovl_set_link_redirect(old);
739 if (err)
740 goto out_nlink_end;
741 }
742
743 inode = d_inode(old);
744 ihold(inode);
745
746 err = ovl_create_or_link(new, inode,
747 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
748 ovl_type_origin(old));
749 if (err)
750 iput(inode);
751
752 out_nlink_end:
753 ovl_nlink_end(old);
754 out:
755 return err;
756 }
757
ovl_matches_upper(struct dentry * dentry,struct dentry * upper)758 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
759 {
760 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
761 }
762
ovl_remove_and_whiteout(struct dentry * dentry,struct list_head * list)763 static int ovl_remove_and_whiteout(struct dentry *dentry,
764 struct list_head *list)
765 {
766 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
767 struct dentry *workdir = ovl_workdir(dentry);
768 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
769 struct dentry *upper;
770 struct dentry *opaquedir = NULL;
771 int err;
772
773 if (WARN_ON(!workdir))
774 return -EROFS;
775
776 if (!list_empty(list)) {
777 opaquedir = ovl_clear_empty(dentry, list);
778 err = PTR_ERR(opaquedir);
779 if (IS_ERR(opaquedir))
780 goto out;
781 }
782
783 upper = ovl_lookup_upper_unlocked(ofs, dentry->d_name.name, upperdir,
784 dentry->d_name.len);
785 err = PTR_ERR(upper);
786 if (IS_ERR(upper))
787 goto out_dput;
788
789 err = -ESTALE;
790 if ((opaquedir && upper != opaquedir) ||
791 (!opaquedir && ovl_dentry_upper(dentry) &&
792 !ovl_matches_upper(dentry, upper))) {
793 goto out_dput_upper;
794 }
795
796 err = ovl_cleanup_and_whiteout(ofs, upperdir, upper);
797 if (!err)
798 ovl_dir_modified(dentry->d_parent, true);
799
800 d_drop(dentry);
801 out_dput_upper:
802 dput(upper);
803 out_dput:
804 dput(opaquedir);
805 out:
806 return err;
807 }
808
ovl_remove_upper(struct dentry * dentry,bool is_dir,struct list_head * list)809 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
810 struct list_head *list)
811 {
812 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
813 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
814 struct inode *dir = upperdir->d_inode;
815 struct dentry *upper;
816 struct dentry *opaquedir = NULL;
817 int err;
818
819 if (!list_empty(list)) {
820 opaquedir = ovl_clear_empty(dentry, list);
821 err = PTR_ERR(opaquedir);
822 if (IS_ERR(opaquedir))
823 goto out;
824 }
825
826 inode_lock_nested(dir, I_MUTEX_PARENT);
827 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
828 dentry->d_name.len);
829 err = PTR_ERR(upper);
830 if (IS_ERR(upper))
831 goto out_unlock;
832
833 err = -ESTALE;
834 if ((opaquedir && upper != opaquedir) ||
835 (!opaquedir && !ovl_matches_upper(dentry, upper)))
836 goto out_dput_upper;
837
838 if (is_dir)
839 err = ovl_do_rmdir(ofs, dir, upper);
840 else
841 err = ovl_do_unlink(ofs, dir, upper);
842 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
843
844 /*
845 * Keeping this dentry hashed would mean having to release
846 * upperpath/lowerpath, which could only be done if we are the
847 * sole user of this dentry. Too tricky... Just unhash for
848 * now.
849 */
850 if (!err)
851 d_drop(dentry);
852 out_dput_upper:
853 dput(upper);
854 out_unlock:
855 inode_unlock(dir);
856 dput(opaquedir);
857 out:
858 return err;
859 }
860
ovl_pure_upper(struct dentry * dentry)861 static bool ovl_pure_upper(struct dentry *dentry)
862 {
863 return !ovl_dentry_lower(dentry) &&
864 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
865 }
866
ovl_drop_nlink(struct dentry * dentry)867 static void ovl_drop_nlink(struct dentry *dentry)
868 {
869 struct inode *inode = d_inode(dentry);
870 struct dentry *alias;
871
872 /* Try to find another, hashed alias */
873 spin_lock(&inode->i_lock);
874 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
875 if (alias != dentry && !d_unhashed(alias))
876 break;
877 }
878 spin_unlock(&inode->i_lock);
879
880 /*
881 * Changes to underlying layers may cause i_nlink to lose sync with
882 * reality. In this case prevent the link count from going to zero
883 * prematurely.
884 */
885 if (inode->i_nlink > !!alias)
886 drop_nlink(inode);
887 }
888
ovl_do_remove(struct dentry * dentry,bool is_dir)889 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
890 {
891 int err;
892 const struct cred *old_cred;
893 bool lower_positive = ovl_lower_positive(dentry);
894 LIST_HEAD(list);
895
896 /* No need to clean pure upper removed by vfs_rmdir() */
897 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
898 err = ovl_check_empty_dir(dentry, &list);
899 if (err)
900 goto out;
901 }
902
903 err = ovl_copy_up(dentry->d_parent);
904 if (err)
905 goto out;
906
907 err = ovl_nlink_start(dentry);
908 if (err)
909 goto out;
910
911 old_cred = ovl_override_creds(dentry->d_sb);
912 if (!lower_positive)
913 err = ovl_remove_upper(dentry, is_dir, &list);
914 else
915 err = ovl_remove_and_whiteout(dentry, &list);
916 ovl_revert_creds(old_cred);
917 if (!err) {
918 if (is_dir)
919 clear_nlink(dentry->d_inode);
920 else
921 ovl_drop_nlink(dentry);
922 }
923 ovl_nlink_end(dentry);
924
925 /*
926 * Copy ctime
927 *
928 * Note: we fail to update ctime if there was no copy-up, only a
929 * whiteout
930 */
931 if (ovl_dentry_upper(dentry))
932 ovl_copyattr(d_inode(dentry));
933
934 out:
935 ovl_cache_free(&list);
936 return err;
937 }
938
ovl_unlink(struct inode * dir,struct dentry * dentry)939 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
940 {
941 return ovl_do_remove(dentry, false);
942 }
943
ovl_rmdir(struct inode * dir,struct dentry * dentry)944 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
945 {
946 return ovl_do_remove(dentry, true);
947 }
948
ovl_type_merge_or_lower(struct dentry * dentry)949 static bool ovl_type_merge_or_lower(struct dentry *dentry)
950 {
951 enum ovl_path_type type = ovl_path_type(dentry);
952
953 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
954 }
955
ovl_can_move(struct dentry * dentry)956 static bool ovl_can_move(struct dentry *dentry)
957 {
958 return ovl_redirect_dir(OVL_FS(dentry->d_sb)) ||
959 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
960 }
961
ovl_get_redirect(struct dentry * dentry,bool abs_redirect)962 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
963 {
964 char *buf, *ret;
965 struct dentry *d, *tmp;
966 int buflen = ovl_redirect_max + 1;
967
968 if (!abs_redirect) {
969 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
970 GFP_KERNEL);
971 goto out;
972 }
973
974 buf = ret = kmalloc(buflen, GFP_KERNEL);
975 if (!buf)
976 goto out;
977
978 buflen--;
979 buf[buflen] = '\0';
980 for (d = dget(dentry); !IS_ROOT(d);) {
981 const char *name;
982 int thislen;
983
984 spin_lock(&d->d_lock);
985 name = ovl_dentry_get_redirect(d);
986 if (name) {
987 thislen = strlen(name);
988 } else {
989 name = d->d_name.name;
990 thislen = d->d_name.len;
991 }
992
993 /* If path is too long, fall back to userspace move */
994 if (thislen + (name[0] != '/') > buflen) {
995 ret = ERR_PTR(-EXDEV);
996 spin_unlock(&d->d_lock);
997 goto out_put;
998 }
999
1000 buflen -= thislen;
1001 memcpy(&buf[buflen], name, thislen);
1002 spin_unlock(&d->d_lock);
1003 tmp = dget_parent(d);
1004
1005 dput(d);
1006 d = tmp;
1007
1008 /* Absolute redirect: finished */
1009 if (buf[buflen] == '/')
1010 break;
1011 buflen--;
1012 buf[buflen] = '/';
1013 }
1014 ret = kstrdup(&buf[buflen], GFP_KERNEL);
1015 out_put:
1016 dput(d);
1017 kfree(buf);
1018 out:
1019 return ret ? ret : ERR_PTR(-ENOMEM);
1020 }
1021
ovl_need_absolute_redirect(struct dentry * dentry,bool samedir)1022 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1023 {
1024 struct dentry *lowerdentry;
1025
1026 if (!samedir)
1027 return true;
1028
1029 if (d_is_dir(dentry))
1030 return false;
1031
1032 /*
1033 * For non-dir hardlinked files, we need absolute redirects
1034 * in general as two upper hardlinks could be in different
1035 * dirs. We could put a relative redirect now and convert
1036 * it to absolute redirect later. But when nlink > 1 and
1037 * indexing is on, that means relative redirect needs to be
1038 * converted to absolute during copy up of another lower
1039 * hardllink as well.
1040 *
1041 * So without optimizing too much, just check if lower is
1042 * a hard link or not. If lower is hard link, put absolute
1043 * redirect.
1044 */
1045 lowerdentry = ovl_dentry_lower(dentry);
1046 return (d_inode(lowerdentry)->i_nlink > 1);
1047 }
1048
ovl_set_redirect(struct dentry * dentry,bool samedir)1049 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1050 {
1051 int err;
1052 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1053 const char *redirect = ovl_dentry_get_redirect(dentry);
1054 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1055
1056 if (redirect && (!absolute_redirect || redirect[0] == '/'))
1057 return 0;
1058
1059 redirect = ovl_get_redirect(dentry, absolute_redirect);
1060 if (IS_ERR(redirect))
1061 return PTR_ERR(redirect);
1062
1063 err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1064 OVL_XATTR_REDIRECT,
1065 redirect, strlen(redirect), -EXDEV);
1066 if (!err) {
1067 spin_lock(&dentry->d_lock);
1068 ovl_dentry_set_redirect(dentry, redirect);
1069 spin_unlock(&dentry->d_lock);
1070 } else {
1071 kfree(redirect);
1072 pr_warn_ratelimited("failed to set redirect (%i)\n",
1073 err);
1074 /* Fall back to userspace copy-up */
1075 err = -EXDEV;
1076 }
1077 return err;
1078 }
1079
ovl_rename(struct mnt_idmap * idmap,struct inode * olddir,struct dentry * old,struct inode * newdir,struct dentry * new,unsigned int flags)1080 static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir,
1081 struct dentry *old, struct inode *newdir,
1082 struct dentry *new, unsigned int flags)
1083 {
1084 int err;
1085 struct dentry *old_upperdir;
1086 struct dentry *new_upperdir;
1087 struct dentry *olddentry = NULL;
1088 struct dentry *newdentry = NULL;
1089 struct dentry *trap, *de;
1090 bool old_opaque;
1091 bool new_opaque;
1092 bool cleanup_whiteout = false;
1093 bool update_nlink = false;
1094 bool overwrite = !(flags & RENAME_EXCHANGE);
1095 bool is_dir = d_is_dir(old);
1096 bool new_is_dir = d_is_dir(new);
1097 bool samedir = olddir == newdir;
1098 struct dentry *opaquedir = NULL;
1099 const struct cred *old_cred = NULL;
1100 struct ovl_fs *ofs = OVL_FS(old->d_sb);
1101 LIST_HEAD(list);
1102
1103 err = -EINVAL;
1104 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1105 goto out;
1106
1107 flags &= ~RENAME_NOREPLACE;
1108
1109 /* Don't copy up directory trees */
1110 err = -EXDEV;
1111 if (!ovl_can_move(old))
1112 goto out;
1113 if (!overwrite && !ovl_can_move(new))
1114 goto out;
1115
1116 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1117 err = ovl_check_empty_dir(new, &list);
1118 if (err)
1119 goto out;
1120 }
1121
1122 if (overwrite) {
1123 if (ovl_lower_positive(old)) {
1124 if (!ovl_dentry_is_whiteout(new)) {
1125 /* Whiteout source */
1126 flags |= RENAME_WHITEOUT;
1127 } else {
1128 /* Switch whiteouts */
1129 flags |= RENAME_EXCHANGE;
1130 }
1131 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1132 flags |= RENAME_EXCHANGE;
1133 cleanup_whiteout = true;
1134 }
1135 }
1136
1137 err = ovl_copy_up(old);
1138 if (err)
1139 goto out;
1140
1141 err = ovl_copy_up(new->d_parent);
1142 if (err)
1143 goto out;
1144 if (!overwrite) {
1145 err = ovl_copy_up(new);
1146 if (err)
1147 goto out;
1148 } else if (d_inode(new)) {
1149 err = ovl_nlink_start(new);
1150 if (err)
1151 goto out;
1152
1153 update_nlink = true;
1154 }
1155
1156 if (!update_nlink) {
1157 /* ovl_nlink_start() took ovl_want_write() */
1158 err = ovl_want_write(old);
1159 if (err)
1160 goto out;
1161 }
1162
1163 old_cred = ovl_override_creds(old->d_sb);
1164
1165 if (!list_empty(&list)) {
1166 opaquedir = ovl_clear_empty(new, &list);
1167 err = PTR_ERR(opaquedir);
1168 if (IS_ERR(opaquedir)) {
1169 opaquedir = NULL;
1170 goto out_revert_creds;
1171 }
1172 }
1173
1174 old_upperdir = ovl_dentry_upper(old->d_parent);
1175 new_upperdir = ovl_dentry_upper(new->d_parent);
1176
1177 if (!samedir) {
1178 /*
1179 * When moving a merge dir or non-dir with copy up origin into
1180 * a new parent, we are marking the new parent dir "impure".
1181 * When ovl_iterate() iterates an "impure" upper dir, it will
1182 * lookup the origin inodes of the entries to fill d_ino.
1183 */
1184 if (ovl_type_origin(old)) {
1185 err = ovl_set_impure(new->d_parent, new_upperdir);
1186 if (err)
1187 goto out_revert_creds;
1188 }
1189 if (!overwrite && ovl_type_origin(new)) {
1190 err = ovl_set_impure(old->d_parent, old_upperdir);
1191 if (err)
1192 goto out_revert_creds;
1193 }
1194 }
1195
1196 trap = lock_rename(new_upperdir, old_upperdir);
1197 if (IS_ERR(trap)) {
1198 err = PTR_ERR(trap);
1199 goto out_revert_creds;
1200 }
1201
1202 de = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir,
1203 old->d_name.len);
1204 err = PTR_ERR(de);
1205 if (IS_ERR(de))
1206 goto out_unlock;
1207 olddentry = de;
1208
1209 err = -ESTALE;
1210 if (!ovl_matches_upper(old, olddentry))
1211 goto out_unlock;
1212
1213 de = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir,
1214 new->d_name.len);
1215 err = PTR_ERR(de);
1216 if (IS_ERR(de))
1217 goto out_unlock;
1218 newdentry = de;
1219
1220 old_opaque = ovl_dentry_is_opaque(old);
1221 new_opaque = ovl_dentry_is_opaque(new);
1222
1223 err = -ESTALE;
1224 if (d_inode(new) && ovl_dentry_upper(new)) {
1225 if (opaquedir) {
1226 if (newdentry != opaquedir)
1227 goto out_unlock;
1228 } else {
1229 if (!ovl_matches_upper(new, newdentry))
1230 goto out_unlock;
1231 }
1232 } else {
1233 if (!d_is_negative(newdentry)) {
1234 if (!new_opaque || !ovl_upper_is_whiteout(ofs, newdentry))
1235 goto out_unlock;
1236 } else {
1237 if (flags & RENAME_EXCHANGE)
1238 goto out_unlock;
1239 }
1240 }
1241
1242 if (olddentry == trap)
1243 goto out_unlock;
1244 if (newdentry == trap)
1245 goto out_unlock;
1246
1247 if (olddentry->d_inode == newdentry->d_inode)
1248 goto out_unlock;
1249
1250 err = 0;
1251 if (ovl_type_merge_or_lower(old))
1252 err = ovl_set_redirect(old, samedir);
1253 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1254 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1255 if (err)
1256 goto out_unlock;
1257
1258 if (!overwrite && ovl_type_merge_or_lower(new))
1259 err = ovl_set_redirect(new, samedir);
1260 else if (!overwrite && new_is_dir && !new_opaque &&
1261 ovl_type_merge(old->d_parent))
1262 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1263 if (err)
1264 goto out_unlock;
1265
1266 err = ovl_do_rename(ofs, old_upperdir, olddentry,
1267 new_upperdir, newdentry, flags);
1268 unlock_rename(new_upperdir, old_upperdir);
1269 if (err)
1270 goto out_revert_creds;
1271
1272 if (cleanup_whiteout)
1273 ovl_cleanup(ofs, old_upperdir, newdentry);
1274
1275 if (overwrite && d_inode(new)) {
1276 if (new_is_dir)
1277 clear_nlink(d_inode(new));
1278 else
1279 ovl_drop_nlink(new);
1280 }
1281
1282 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1283 (!overwrite && ovl_type_origin(new)));
1284 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1285 (d_inode(new) && ovl_type_origin(new)));
1286
1287 /* copy ctime: */
1288 ovl_copyattr(d_inode(old));
1289 if (d_inode(new) && ovl_dentry_upper(new))
1290 ovl_copyattr(d_inode(new));
1291
1292 out_revert_creds:
1293 ovl_revert_creds(old_cred);
1294 if (update_nlink)
1295 ovl_nlink_end(new);
1296 else
1297 ovl_drop_write(old);
1298 out:
1299 dput(newdentry);
1300 dput(olddentry);
1301 dput(opaquedir);
1302 ovl_cache_free(&list);
1303 return err;
1304
1305 out_unlock:
1306 unlock_rename(new_upperdir, old_upperdir);
1307 goto out_revert_creds;
1308 }
1309
ovl_create_tmpfile(struct file * file,struct dentry * dentry,struct inode * inode,umode_t mode)1310 static int ovl_create_tmpfile(struct file *file, struct dentry *dentry,
1311 struct inode *inode, umode_t mode)
1312 {
1313 const struct cred *old_cred, *new_cred = NULL;
1314 struct path realparentpath;
1315 struct file *realfile;
1316 struct ovl_file *of;
1317 struct dentry *newdentry;
1318 /* It's okay to set O_NOATIME, since the owner will be current fsuid */
1319 int flags = file->f_flags | OVL_OPEN_FLAGS;
1320 int err;
1321
1322 old_cred = ovl_override_creds(dentry->d_sb);
1323 new_cred = ovl_setup_cred_for_create(dentry, inode, mode, old_cred);
1324 err = PTR_ERR(new_cred);
1325 if (IS_ERR(new_cred)) {
1326 new_cred = NULL;
1327 goto out_revert_creds;
1328 }
1329
1330 ovl_path_upper(dentry->d_parent, &realparentpath);
1331 realfile = backing_tmpfile_open(&file->f_path, flags, &realparentpath,
1332 mode, current_cred());
1333 err = PTR_ERR_OR_ZERO(realfile);
1334 pr_debug("tmpfile/open(%pd2, 0%o) = %i\n", realparentpath.dentry, mode, err);
1335 if (err)
1336 goto out_revert_creds;
1337
1338 of = ovl_file_alloc(realfile);
1339 if (!of) {
1340 fput(realfile);
1341 err = -ENOMEM;
1342 goto out_revert_creds;
1343 }
1344
1345 /* ovl_instantiate() consumes the newdentry reference on success */
1346 newdentry = dget(realfile->f_path.dentry);
1347 err = ovl_instantiate(dentry, inode, newdentry, false, file);
1348 if (!err) {
1349 file->private_data = of;
1350 } else {
1351 dput(newdentry);
1352 ovl_file_free(of);
1353 }
1354 out_revert_creds:
1355 ovl_revert_creds(old_cred);
1356 put_cred(new_cred);
1357 return err;
1358 }
1359
ovl_dummy_open(struct inode * inode,struct file * file)1360 static int ovl_dummy_open(struct inode *inode, struct file *file)
1361 {
1362 return 0;
1363 }
1364
ovl_tmpfile(struct mnt_idmap * idmap,struct inode * dir,struct file * file,umode_t mode)1365 static int ovl_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
1366 struct file *file, umode_t mode)
1367 {
1368 int err;
1369 struct dentry *dentry = file->f_path.dentry;
1370 struct inode *inode;
1371
1372 if (!OVL_FS(dentry->d_sb)->tmpfile)
1373 return -EOPNOTSUPP;
1374
1375 err = ovl_copy_up(dentry->d_parent);
1376 if (err)
1377 return err;
1378
1379 err = ovl_want_write(dentry);
1380 if (err)
1381 return err;
1382
1383 err = -ENOMEM;
1384 inode = ovl_new_inode(dentry->d_sb, mode, 0);
1385 if (!inode)
1386 goto drop_write;
1387
1388 inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
1389 err = ovl_create_tmpfile(file, dentry, inode, inode->i_mode);
1390 if (err)
1391 goto put_inode;
1392
1393 /*
1394 * Check if the preallocated inode was actually used. Having something
1395 * else assigned to the dentry shouldn't happen as that would indicate
1396 * that the backing tmpfile "leaked" out of overlayfs.
1397 */
1398 err = -EIO;
1399 if (WARN_ON(inode != d_inode(dentry)))
1400 goto put_realfile;
1401
1402 /* inode reference was transferred to dentry */
1403 inode = NULL;
1404 err = finish_open(file, dentry, ovl_dummy_open);
1405 put_realfile:
1406 /* Without FMODE_OPENED ->release() won't be called on @file */
1407 if (!(file->f_mode & FMODE_OPENED))
1408 ovl_file_free(file->private_data);
1409 put_inode:
1410 iput(inode);
1411 drop_write:
1412 ovl_drop_write(dentry);
1413 return err;
1414 }
1415
1416 const struct inode_operations ovl_dir_inode_operations = {
1417 .lookup = ovl_lookup,
1418 .mkdir = ovl_mkdir,
1419 .symlink = ovl_symlink,
1420 .unlink = ovl_unlink,
1421 .rmdir = ovl_rmdir,
1422 .rename = ovl_rename,
1423 .link = ovl_link,
1424 .setattr = ovl_setattr,
1425 .create = ovl_create,
1426 .mknod = ovl_mknod,
1427 .permission = ovl_permission,
1428 .getattr = ovl_getattr,
1429 .listxattr = ovl_listxattr,
1430 .get_inode_acl = ovl_get_inode_acl,
1431 .get_acl = ovl_get_acl,
1432 .set_acl = ovl_set_acl,
1433 .update_time = ovl_update_time,
1434 .fileattr_get = ovl_fileattr_get,
1435 .fileattr_set = ovl_fileattr_set,
1436 .tmpfile = ovl_tmpfile,
1437 };
1438