dir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell 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. #include <linux/fs.h>
  10. #include <linux/namei.h>
  11. #include <linux/xattr.h>
  12. #include <linux/security.h>
  13. #include <linux/cred.h>
  14. #include <linux/atomic.h>
  15. #include "overlayfs.h"
  16. void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
  17. {
  18. int err;
  19. dget(wdentry);
  20. if (d_is_dir(wdentry))
  21. err = ovl_do_rmdir(wdir, wdentry);
  22. else
  23. err = ovl_do_unlink(wdir, wdentry);
  24. dput(wdentry);
  25. if (err) {
  26. pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
  27. wdentry, err);
  28. }
  29. }
  30. struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
  31. {
  32. struct dentry *temp;
  33. char name[20];
  34. static atomic_t temp_id = ATOMIC_INIT(0);
  35. /* counter is allowed to wrap, since temp dentries are ephemeral */
  36. snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
  37. temp = lookup_one_len(name, workdir, strlen(name));
  38. if (!IS_ERR(temp) && temp->d_inode) {
  39. pr_err("overlayfs: workdir/%s already exists\n", name);
  40. dput(temp);
  41. temp = ERR_PTR(-EIO);
  42. }
  43. return temp;
  44. }
  45. /* caller holds i_mutex on workdir */
  46. static struct dentry *ovl_whiteout(struct dentry *workdir,
  47. struct dentry *dentry)
  48. {
  49. int err;
  50. struct dentry *whiteout;
  51. struct inode *wdir = workdir->d_inode;
  52. whiteout = ovl_lookup_temp(workdir, dentry);
  53. if (IS_ERR(whiteout))
  54. return whiteout;
  55. err = ovl_do_whiteout(wdir, whiteout);
  56. if (err) {
  57. dput(whiteout);
  58. whiteout = ERR_PTR(err);
  59. }
  60. return whiteout;
  61. }
  62. int ovl_create_real(struct inode *dir, struct dentry *newdentry,
  63. struct kstat *stat, const char *link,
  64. struct dentry *hardlink, bool debug)
  65. {
  66. int err;
  67. if (newdentry->d_inode)
  68. return -ESTALE;
  69. if (hardlink) {
  70. err = ovl_do_link(hardlink, dir, newdentry, debug);
  71. } else {
  72. switch (stat->mode & S_IFMT) {
  73. case S_IFREG:
  74. err = ovl_do_create(dir, newdentry, stat->mode, debug);
  75. break;
  76. case S_IFDIR:
  77. err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
  78. break;
  79. case S_IFCHR:
  80. case S_IFBLK:
  81. case S_IFIFO:
  82. case S_IFSOCK:
  83. err = ovl_do_mknod(dir, newdentry,
  84. stat->mode, stat->rdev, debug);
  85. break;
  86. case S_IFLNK:
  87. err = ovl_do_symlink(dir, newdentry, link, debug);
  88. break;
  89. default:
  90. err = -EPERM;
  91. }
  92. }
  93. if (!err && WARN_ON(!newdentry->d_inode)) {
  94. /*
  95. * Not quite sure if non-instantiated dentry is legal or not.
  96. * VFS doesn't seem to care so check and warn here.
  97. */
  98. err = -ENOENT;
  99. }
  100. return err;
  101. }
  102. static int ovl_set_opaque(struct dentry *upperdentry)
  103. {
  104. return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
  105. }
  106. static void ovl_remove_opaque(struct dentry *upperdentry)
  107. {
  108. int err;
  109. err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
  110. if (err) {
  111. pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
  112. upperdentry->d_name.name, err);
  113. }
  114. }
  115. static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
  116. struct kstat *stat)
  117. {
  118. int err;
  119. enum ovl_path_type type;
  120. struct path realpath;
  121. type = ovl_path_real(dentry, &realpath);
  122. err = vfs_getattr(&realpath, stat);
  123. if (err)
  124. return err;
  125. stat->dev = dentry->d_sb->s_dev;
  126. stat->ino = dentry->d_inode->i_ino;
  127. /*
  128. * It's probably not worth it to count subdirs to get the
  129. * correct link count. nlink=1 seems to pacify 'find' and
  130. * other utilities.
  131. */
  132. if (OVL_TYPE_MERGE(type))
  133. stat->nlink = 1;
  134. return 0;
  135. }
  136. static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
  137. struct kstat *stat, const char *link,
  138. struct dentry *hardlink)
  139. {
  140. struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
  141. struct inode *udir = upperdir->d_inode;
  142. struct dentry *newdentry;
  143. int err;
  144. mutex_lock_nested(&udir->i_mutex, I_MUTEX_PARENT);
  145. newdentry = lookup_one_len(dentry->d_name.name, upperdir,
  146. dentry->d_name.len);
  147. err = PTR_ERR(newdentry);
  148. if (IS_ERR(newdentry))
  149. goto out_unlock;
  150. err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
  151. if (err)
  152. goto out_dput;
  153. ovl_dentry_version_inc(dentry->d_parent);
  154. ovl_dentry_update(dentry, newdentry);
  155. ovl_copyattr(newdentry->d_inode, inode);
  156. d_instantiate(dentry, inode);
  157. newdentry = NULL;
  158. out_dput:
  159. dput(newdentry);
  160. out_unlock:
  161. mutex_unlock(&udir->i_mutex);
  162. return err;
  163. }
  164. static int ovl_lock_rename_workdir(struct dentry *workdir,
  165. struct dentry *upperdir)
  166. {
  167. /* Workdir should not be the same as upperdir */
  168. if (workdir == upperdir)
  169. goto err;
  170. /* Workdir should not be subdir of upperdir and vice versa */
  171. if (lock_rename(workdir, upperdir) != NULL)
  172. goto err_unlock;
  173. return 0;
  174. err_unlock:
  175. unlock_rename(workdir, upperdir);
  176. err:
  177. pr_err("overlayfs: failed to lock workdir+upperdir\n");
  178. return -EIO;
  179. }
  180. static struct dentry *ovl_clear_empty(struct dentry *dentry,
  181. struct list_head *list)
  182. {
  183. struct dentry *workdir = ovl_workdir(dentry);
  184. struct inode *wdir = workdir->d_inode;
  185. struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
  186. struct inode *udir = upperdir->d_inode;
  187. struct path upperpath;
  188. struct dentry *upper;
  189. struct dentry *opaquedir;
  190. struct kstat stat;
  191. int err;
  192. if (WARN_ON(!workdir))
  193. return ERR_PTR(-EROFS);
  194. err = ovl_lock_rename_workdir(workdir, upperdir);
  195. if (err)
  196. goto out;
  197. ovl_path_upper(dentry, &upperpath);
  198. err = vfs_getattr(&upperpath, &stat);
  199. if (err)
  200. goto out_unlock;
  201. err = -ESTALE;
  202. if (!S_ISDIR(stat.mode))
  203. goto out_unlock;
  204. upper = upperpath.dentry;
  205. if (upper->d_parent->d_inode != udir)
  206. goto out_unlock;
  207. opaquedir = ovl_lookup_temp(workdir, dentry);
  208. err = PTR_ERR(opaquedir);
  209. if (IS_ERR(opaquedir))
  210. goto out_unlock;
  211. err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
  212. if (err)
  213. goto out_dput;
  214. err = ovl_copy_xattr(upper, opaquedir);
  215. if (err)
  216. goto out_cleanup;
  217. err = ovl_set_opaque(opaquedir);
  218. if (err)
  219. goto out_cleanup;
  220. mutex_lock(&opaquedir->d_inode->i_mutex);
  221. err = ovl_set_attr(opaquedir, &stat);
  222. mutex_unlock(&opaquedir->d_inode->i_mutex);
  223. if (err)
  224. goto out_cleanup;
  225. err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
  226. if (err)
  227. goto out_cleanup;
  228. ovl_cleanup_whiteouts(upper, list);
  229. ovl_cleanup(wdir, upper);
  230. unlock_rename(workdir, upperdir);
  231. /* dentry's upper doesn't match now, get rid of it */
  232. d_drop(dentry);
  233. return opaquedir;
  234. out_cleanup:
  235. ovl_cleanup(wdir, opaquedir);
  236. out_dput:
  237. dput(opaquedir);
  238. out_unlock:
  239. unlock_rename(workdir, upperdir);
  240. out:
  241. return ERR_PTR(err);
  242. }
  243. static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
  244. {
  245. int err;
  246. struct dentry *ret = NULL;
  247. LIST_HEAD(list);
  248. err = ovl_check_empty_dir(dentry, &list);
  249. if (err)
  250. ret = ERR_PTR(err);
  251. else {
  252. /*
  253. * If no upperdentry then skip clearing whiteouts.
  254. *
  255. * Can race with copy-up, since we don't hold the upperdir
  256. * mutex. Doesn't matter, since copy-up can't create a
  257. * non-empty directory from an empty one.
  258. */
  259. if (ovl_dentry_upper(dentry))
  260. ret = ovl_clear_empty(dentry, &list);
  261. }
  262. ovl_cache_free(&list);
  263. return ret;
  264. }
  265. static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
  266. struct kstat *stat, const char *link,
  267. struct dentry *hardlink)
  268. {
  269. struct dentry *workdir = ovl_workdir(dentry);
  270. struct inode *wdir = workdir->d_inode;
  271. struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
  272. struct inode *udir = upperdir->d_inode;
  273. struct dentry *upper;
  274. struct dentry *newdentry;
  275. int err;
  276. if (WARN_ON(!workdir))
  277. return -EROFS;
  278. err = ovl_lock_rename_workdir(workdir, upperdir);
  279. if (err)
  280. goto out;
  281. newdentry = ovl_lookup_temp(workdir, dentry);
  282. err = PTR_ERR(newdentry);
  283. if (IS_ERR(newdentry))
  284. goto out_unlock;
  285. upper = lookup_one_len(dentry->d_name.name, upperdir,
  286. dentry->d_name.len);
  287. err = PTR_ERR(upper);
  288. if (IS_ERR(upper))
  289. goto out_dput;
  290. err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
  291. if (err)
  292. goto out_dput2;
  293. if (S_ISDIR(stat->mode)) {
  294. err = ovl_set_opaque(newdentry);
  295. if (err)
  296. goto out_cleanup;
  297. err = ovl_do_rename(wdir, newdentry, udir, upper,
  298. RENAME_EXCHANGE);
  299. if (err)
  300. goto out_cleanup;
  301. ovl_cleanup(wdir, upper);
  302. } else {
  303. err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
  304. if (err)
  305. goto out_cleanup;
  306. }
  307. ovl_dentry_version_inc(dentry->d_parent);
  308. ovl_dentry_update(dentry, newdentry);
  309. ovl_copyattr(newdentry->d_inode, inode);
  310. d_instantiate(dentry, inode);
  311. newdentry = NULL;
  312. out_dput2:
  313. dput(upper);
  314. out_dput:
  315. dput(newdentry);
  316. out_unlock:
  317. unlock_rename(workdir, upperdir);
  318. out:
  319. return err;
  320. out_cleanup:
  321. ovl_cleanup(wdir, newdentry);
  322. goto out_dput2;
  323. }
  324. static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev,
  325. const char *link, struct dentry *hardlink)
  326. {
  327. int err;
  328. struct inode *inode;
  329. struct kstat stat = {
  330. .mode = mode,
  331. .rdev = rdev,
  332. };
  333. err = -ENOMEM;
  334. inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
  335. if (!inode)
  336. goto out;
  337. err = ovl_copy_up(dentry->d_parent);
  338. if (err)
  339. goto out_iput;
  340. if (!ovl_dentry_is_opaque(dentry)) {
  341. err = ovl_create_upper(dentry, inode, &stat, link, hardlink);
  342. } else {
  343. const struct cred *old_cred;
  344. struct cred *override_cred;
  345. old_cred = ovl_override_creds(dentry->d_sb);
  346. err = -ENOMEM;
  347. override_cred = prepare_creds();
  348. if (override_cred) {
  349. override_cred->fsuid = old_cred->fsuid;
  350. override_cred->fsgid = old_cred->fsgid;
  351. put_cred(override_creds(override_cred));
  352. put_cred(override_cred);
  353. err = ovl_create_over_whiteout(dentry, inode, &stat,
  354. link, hardlink);
  355. }
  356. revert_creds(old_cred);
  357. }
  358. if (!err)
  359. inode = NULL;
  360. out_iput:
  361. iput(inode);
  362. out:
  363. return err;
  364. }
  365. static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
  366. const char *link)
  367. {
  368. int err;
  369. err = ovl_want_write(dentry);
  370. if (!err) {
  371. err = ovl_create_or_link(dentry, mode, rdev, link, NULL);
  372. ovl_drop_write(dentry);
  373. }
  374. return err;
  375. }
  376. static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  377. bool excl)
  378. {
  379. return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
  380. }
  381. static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  382. {
  383. return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
  384. }
  385. static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  386. dev_t rdev)
  387. {
  388. /* Don't allow creation of "whiteout" on overlay */
  389. if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
  390. return -EPERM;
  391. return ovl_create_object(dentry, mode, rdev, NULL);
  392. }
  393. static int ovl_symlink(struct inode *dir, struct dentry *dentry,
  394. const char *link)
  395. {
  396. return ovl_create_object(dentry, S_IFLNK, 0, link);
  397. }
  398. static int ovl_link(struct dentry *old, struct inode *newdir,
  399. struct dentry *new)
  400. {
  401. int err;
  402. struct dentry *upper;
  403. err = ovl_want_write(old);
  404. if (err)
  405. goto out;
  406. err = ovl_copy_up(old);
  407. if (err)
  408. goto out_drop_write;
  409. upper = ovl_dentry_upper(old);
  410. err = ovl_create_or_link(new, upper->d_inode->i_mode, 0, NULL, upper);
  411. out_drop_write:
  412. ovl_drop_write(old);
  413. out:
  414. return err;
  415. }
  416. static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
  417. {
  418. struct dentry *workdir = ovl_workdir(dentry);
  419. struct inode *wdir = workdir->d_inode;
  420. struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
  421. struct inode *udir = upperdir->d_inode;
  422. struct dentry *whiteout;
  423. struct dentry *upper;
  424. struct dentry *opaquedir = NULL;
  425. int err;
  426. int flags = 0;
  427. if (WARN_ON(!workdir))
  428. return -EROFS;
  429. if (is_dir) {
  430. if (OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) {
  431. opaquedir = ovl_check_empty_and_clear(dentry);
  432. err = PTR_ERR(opaquedir);
  433. if (IS_ERR(opaquedir))
  434. goto out;
  435. } else {
  436. LIST_HEAD(list);
  437. /*
  438. * When removing an empty opaque directory, then it
  439. * makes no sense to replace it with an exact replica of
  440. * itself. But emptiness still needs to be checked.
  441. */
  442. err = ovl_check_empty_dir(dentry, &list);
  443. ovl_cache_free(&list);
  444. if (err)
  445. goto out;
  446. }
  447. }
  448. err = ovl_lock_rename_workdir(workdir, upperdir);
  449. if (err)
  450. goto out_dput;
  451. upper = lookup_one_len(dentry->d_name.name, upperdir,
  452. dentry->d_name.len);
  453. err = PTR_ERR(upper);
  454. if (IS_ERR(upper))
  455. goto out_unlock;
  456. err = -ESTALE;
  457. if ((opaquedir && upper != opaquedir) ||
  458. (!opaquedir && ovl_dentry_upper(dentry) &&
  459. upper != ovl_dentry_upper(dentry))) {
  460. goto out_dput_upper;
  461. }
  462. whiteout = ovl_whiteout(workdir, dentry);
  463. err = PTR_ERR(whiteout);
  464. if (IS_ERR(whiteout))
  465. goto out_dput_upper;
  466. if (d_is_dir(upper))
  467. flags = RENAME_EXCHANGE;
  468. err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
  469. if (err)
  470. goto kill_whiteout;
  471. if (flags)
  472. ovl_cleanup(wdir, upper);
  473. ovl_dentry_version_inc(dentry->d_parent);
  474. out_d_drop:
  475. d_drop(dentry);
  476. dput(whiteout);
  477. out_dput_upper:
  478. dput(upper);
  479. out_unlock:
  480. unlock_rename(workdir, upperdir);
  481. out_dput:
  482. dput(opaquedir);
  483. out:
  484. return err;
  485. kill_whiteout:
  486. ovl_cleanup(wdir, whiteout);
  487. goto out_d_drop;
  488. }
  489. static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
  490. {
  491. struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
  492. struct inode *dir = upperdir->d_inode;
  493. struct dentry *upper;
  494. int err;
  495. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  496. upper = lookup_one_len(dentry->d_name.name, upperdir,
  497. dentry->d_name.len);
  498. err = PTR_ERR(upper);
  499. if (IS_ERR(upper))
  500. goto out_unlock;
  501. err = -ESTALE;
  502. if (upper == ovl_dentry_upper(dentry)) {
  503. if (is_dir)
  504. err = vfs_rmdir(dir, upper);
  505. else
  506. err = vfs_unlink(dir, upper, NULL);
  507. ovl_dentry_version_inc(dentry->d_parent);
  508. }
  509. dput(upper);
  510. /*
  511. * Keeping this dentry hashed would mean having to release
  512. * upperpath/lowerpath, which could only be done if we are the
  513. * sole user of this dentry. Too tricky... Just unhash for
  514. * now.
  515. */
  516. if (!err)
  517. d_drop(dentry);
  518. out_unlock:
  519. mutex_unlock(&dir->i_mutex);
  520. return err;
  521. }
  522. static inline int ovl_check_sticky(struct dentry *dentry)
  523. {
  524. struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
  525. struct inode *inode = ovl_dentry_real(dentry)->d_inode;
  526. if (check_sticky(dir, inode))
  527. return -EPERM;
  528. return 0;
  529. }
  530. static int ovl_do_remove(struct dentry *dentry, bool is_dir)
  531. {
  532. enum ovl_path_type type;
  533. int err;
  534. err = ovl_check_sticky(dentry);
  535. if (err)
  536. goto out;
  537. err = ovl_want_write(dentry);
  538. if (err)
  539. goto out;
  540. err = ovl_copy_up(dentry->d_parent);
  541. if (err)
  542. goto out_drop_write;
  543. type = ovl_path_type(dentry);
  544. if (OVL_TYPE_PURE_UPPER(type)) {
  545. err = ovl_remove_upper(dentry, is_dir);
  546. } else {
  547. const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
  548. err = ovl_remove_and_whiteout(dentry, is_dir);
  549. revert_creds(old_cred);
  550. }
  551. out_drop_write:
  552. ovl_drop_write(dentry);
  553. out:
  554. return err;
  555. }
  556. static int ovl_unlink(struct inode *dir, struct dentry *dentry)
  557. {
  558. return ovl_do_remove(dentry, false);
  559. }
  560. static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
  561. {
  562. return ovl_do_remove(dentry, true);
  563. }
  564. static int ovl_rename2(struct inode *olddir, struct dentry *old,
  565. struct inode *newdir, struct dentry *new,
  566. unsigned int flags)
  567. {
  568. int err;
  569. enum ovl_path_type old_type;
  570. enum ovl_path_type new_type;
  571. struct dentry *old_upperdir;
  572. struct dentry *new_upperdir;
  573. struct dentry *olddentry;
  574. struct dentry *newdentry;
  575. struct dentry *trap;
  576. bool old_opaque;
  577. bool new_opaque;
  578. bool new_create = false;
  579. bool cleanup_whiteout = false;
  580. bool overwrite = !(flags & RENAME_EXCHANGE);
  581. bool is_dir = d_is_dir(old);
  582. bool new_is_dir = false;
  583. struct dentry *opaquedir = NULL;
  584. const struct cred *old_cred = NULL;
  585. err = -EINVAL;
  586. if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
  587. goto out;
  588. flags &= ~RENAME_NOREPLACE;
  589. err = ovl_check_sticky(old);
  590. if (err)
  591. goto out;
  592. /* Don't copy up directory trees */
  593. old_type = ovl_path_type(old);
  594. err = -EXDEV;
  595. if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
  596. goto out;
  597. if (new->d_inode) {
  598. err = ovl_check_sticky(new);
  599. if (err)
  600. goto out;
  601. if (d_is_dir(new))
  602. new_is_dir = true;
  603. new_type = ovl_path_type(new);
  604. err = -EXDEV;
  605. if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
  606. goto out;
  607. err = 0;
  608. if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
  609. if (ovl_dentry_lower(old)->d_inode ==
  610. ovl_dentry_lower(new)->d_inode)
  611. goto out;
  612. }
  613. if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
  614. if (ovl_dentry_upper(old)->d_inode ==
  615. ovl_dentry_upper(new)->d_inode)
  616. goto out;
  617. }
  618. } else {
  619. if (ovl_dentry_is_opaque(new))
  620. new_type = __OVL_PATH_UPPER;
  621. else
  622. new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
  623. }
  624. err = ovl_want_write(old);
  625. if (err)
  626. goto out;
  627. err = ovl_copy_up(old);
  628. if (err)
  629. goto out_drop_write;
  630. err = ovl_copy_up(new->d_parent);
  631. if (err)
  632. goto out_drop_write;
  633. if (!overwrite) {
  634. err = ovl_copy_up(new);
  635. if (err)
  636. goto out_drop_write;
  637. }
  638. old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
  639. new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
  640. if (old_opaque || new_opaque)
  641. old_cred = ovl_override_creds(old->d_sb);
  642. if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
  643. opaquedir = ovl_check_empty_and_clear(new);
  644. err = PTR_ERR(opaquedir);
  645. if (IS_ERR(opaquedir)) {
  646. opaquedir = NULL;
  647. goto out_revert_creds;
  648. }
  649. }
  650. if (overwrite) {
  651. if (old_opaque) {
  652. if (new->d_inode || !new_opaque) {
  653. /* Whiteout source */
  654. flags |= RENAME_WHITEOUT;
  655. } else {
  656. /* Switch whiteouts */
  657. flags |= RENAME_EXCHANGE;
  658. }
  659. } else if (is_dir && !new->d_inode && new_opaque) {
  660. flags |= RENAME_EXCHANGE;
  661. cleanup_whiteout = true;
  662. }
  663. }
  664. old_upperdir = ovl_dentry_upper(old->d_parent);
  665. new_upperdir = ovl_dentry_upper(new->d_parent);
  666. trap = lock_rename(new_upperdir, old_upperdir);
  667. olddentry = lookup_one_len(old->d_name.name, old_upperdir,
  668. old->d_name.len);
  669. err = PTR_ERR(olddentry);
  670. if (IS_ERR(olddentry))
  671. goto out_unlock;
  672. err = -ESTALE;
  673. if (olddentry != ovl_dentry_upper(old))
  674. goto out_dput_old;
  675. newdentry = lookup_one_len(new->d_name.name, new_upperdir,
  676. new->d_name.len);
  677. err = PTR_ERR(newdentry);
  678. if (IS_ERR(newdentry))
  679. goto out_dput_old;
  680. err = -ESTALE;
  681. if (ovl_dentry_upper(new)) {
  682. if (opaquedir) {
  683. if (newdentry != opaquedir)
  684. goto out_dput;
  685. } else {
  686. if (newdentry != ovl_dentry_upper(new))
  687. goto out_dput;
  688. }
  689. } else {
  690. new_create = true;
  691. if (!d_is_negative(newdentry) &&
  692. (!new_opaque || !ovl_is_whiteout(newdentry)))
  693. goto out_dput;
  694. }
  695. if (olddentry == trap)
  696. goto out_dput;
  697. if (newdentry == trap)
  698. goto out_dput;
  699. if (is_dir && !old_opaque && new_opaque) {
  700. err = ovl_set_opaque(olddentry);
  701. if (err)
  702. goto out_dput;
  703. }
  704. if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
  705. err = ovl_set_opaque(newdentry);
  706. if (err)
  707. goto out_dput;
  708. }
  709. if (old_opaque || new_opaque) {
  710. err = ovl_do_rename(old_upperdir->d_inode, olddentry,
  711. new_upperdir->d_inode, newdentry,
  712. flags);
  713. } else {
  714. /* No debug for the plain case */
  715. BUG_ON(flags & ~RENAME_EXCHANGE);
  716. err = vfs_rename(old_upperdir->d_inode, olddentry,
  717. new_upperdir->d_inode, newdentry,
  718. NULL, flags);
  719. }
  720. if (err) {
  721. if (is_dir && !old_opaque && new_opaque)
  722. ovl_remove_opaque(olddentry);
  723. if (!overwrite && new_is_dir && old_opaque && !new_opaque)
  724. ovl_remove_opaque(newdentry);
  725. goto out_dput;
  726. }
  727. if (is_dir && old_opaque && !new_opaque)
  728. ovl_remove_opaque(olddentry);
  729. if (!overwrite && new_is_dir && !old_opaque && new_opaque)
  730. ovl_remove_opaque(newdentry);
  731. /*
  732. * Old dentry now lives in different location. Dentries in
  733. * lowerstack are stale. We cannot drop them here because
  734. * access to them is lockless. This could be only pure upper
  735. * or opaque directory - numlower is zero. Or upper non-dir
  736. * entry - its pureness is tracked by flag opaque.
  737. */
  738. if (old_opaque != new_opaque) {
  739. ovl_dentry_set_opaque(old, new_opaque);
  740. if (!overwrite)
  741. ovl_dentry_set_opaque(new, old_opaque);
  742. }
  743. if (cleanup_whiteout)
  744. ovl_cleanup(old_upperdir->d_inode, newdentry);
  745. ovl_dentry_version_inc(old->d_parent);
  746. ovl_dentry_version_inc(new->d_parent);
  747. out_dput:
  748. dput(newdentry);
  749. out_dput_old:
  750. dput(olddentry);
  751. out_unlock:
  752. unlock_rename(new_upperdir, old_upperdir);
  753. out_revert_creds:
  754. if (old_opaque || new_opaque)
  755. revert_creds(old_cred);
  756. out_drop_write:
  757. ovl_drop_write(old);
  758. out:
  759. dput(opaquedir);
  760. return err;
  761. }
  762. const struct inode_operations ovl_dir_inode_operations = {
  763. .lookup = ovl_lookup,
  764. .mkdir = ovl_mkdir,
  765. .symlink = ovl_symlink,
  766. .unlink = ovl_unlink,
  767. .rmdir = ovl_rmdir,
  768. .rename2 = ovl_rename2,
  769. .link = ovl_link,
  770. .setattr = ovl_setattr,
  771. .create = ovl_create,
  772. .mknod = ovl_mknod,
  773. .permission = ovl_permission,
  774. .getattr = ovl_dir_getattr,
  775. .setxattr = ovl_setxattr,
  776. .getxattr = ovl_getxattr,
  777. .listxattr = ovl_listxattr,
  778. .removexattr = ovl_removexattr,
  779. };