dir.c 23 KB

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