copy_up.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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/module.h>
  10. #include <linux/fs.h>
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/splice.h>
  14. #include <linux/xattr.h>
  15. #include <linux/security.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/sched/signal.h>
  18. #include <linux/cred.h>
  19. #include <linux/namei.h>
  20. #include <linux/fdtable.h>
  21. #include <linux/ratelimit.h>
  22. #include <linux/exportfs.h>
  23. #include "overlayfs.h"
  24. #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
  25. static bool __read_mostly ovl_check_copy_up;
  26. module_param_named(check_copy_up, ovl_check_copy_up, bool,
  27. S_IWUSR | S_IRUGO);
  28. MODULE_PARM_DESC(ovl_check_copy_up,
  29. "Warn on copy-up when causing process also has a R/O fd open");
  30. static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
  31. {
  32. const struct dentry *dentry = data;
  33. if (file_inode(f) == d_inode(dentry))
  34. pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
  35. f, fd, current->pid, current->comm);
  36. return 0;
  37. }
  38. /*
  39. * Check the fds open by this process and warn if something like the following
  40. * scenario is about to occur:
  41. *
  42. * fd1 = open("foo", O_RDONLY);
  43. * fd2 = open("foo", O_RDWR);
  44. */
  45. static void ovl_do_check_copy_up(struct dentry *dentry)
  46. {
  47. if (ovl_check_copy_up)
  48. iterate_fd(current->files, 0, ovl_check_fd, dentry);
  49. }
  50. int ovl_copy_xattr(struct dentry *old, struct dentry *new)
  51. {
  52. ssize_t list_size, size, value_size = 0;
  53. char *buf, *name, *value = NULL;
  54. int uninitialized_var(error);
  55. size_t slen;
  56. if (!(old->d_inode->i_opflags & IOP_XATTR) ||
  57. !(new->d_inode->i_opflags & IOP_XATTR))
  58. return 0;
  59. list_size = vfs_listxattr(old, NULL, 0);
  60. if (list_size <= 0) {
  61. if (list_size == -EOPNOTSUPP)
  62. return 0;
  63. return list_size;
  64. }
  65. buf = kzalloc(list_size, GFP_KERNEL);
  66. if (!buf)
  67. return -ENOMEM;
  68. list_size = vfs_listxattr(old, buf, list_size);
  69. if (list_size <= 0) {
  70. error = list_size;
  71. goto out;
  72. }
  73. for (name = buf; list_size; name += slen) {
  74. slen = strnlen(name, list_size) + 1;
  75. /* underlying fs providing us with an broken xattr list? */
  76. if (WARN_ON(slen > list_size)) {
  77. error = -EIO;
  78. break;
  79. }
  80. list_size -= slen;
  81. if (ovl_is_private_xattr(name))
  82. continue;
  83. retry:
  84. size = vfs_getxattr(old, name, value, value_size);
  85. if (size == -ERANGE)
  86. size = vfs_getxattr(old, name, NULL, 0);
  87. if (size < 0) {
  88. error = size;
  89. break;
  90. }
  91. if (size > value_size) {
  92. void *new;
  93. new = krealloc(value, size, GFP_KERNEL);
  94. if (!new) {
  95. error = -ENOMEM;
  96. break;
  97. }
  98. value = new;
  99. value_size = size;
  100. goto retry;
  101. }
  102. error = security_inode_copy_up_xattr(name);
  103. if (error < 0 && error != -EOPNOTSUPP)
  104. break;
  105. if (error == 1) {
  106. error = 0;
  107. continue; /* Discard */
  108. }
  109. error = vfs_setxattr(new, name, value, size, 0);
  110. if (error)
  111. break;
  112. }
  113. kfree(value);
  114. out:
  115. kfree(buf);
  116. return error;
  117. }
  118. static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
  119. {
  120. struct file *old_file;
  121. struct file *new_file;
  122. loff_t old_pos = 0;
  123. loff_t new_pos = 0;
  124. int error = 0;
  125. if (len == 0)
  126. return 0;
  127. old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
  128. if (IS_ERR(old_file))
  129. return PTR_ERR(old_file);
  130. new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
  131. if (IS_ERR(new_file)) {
  132. error = PTR_ERR(new_file);
  133. goto out_fput;
  134. }
  135. /* Try to use clone_file_range to clone up within the same fs */
  136. error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
  137. if (!error)
  138. goto out;
  139. /* Couldn't clone, so now we try to copy the data */
  140. error = 0;
  141. /* FIXME: copy up sparse files efficiently */
  142. while (len) {
  143. size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  144. long bytes;
  145. if (len < this_len)
  146. this_len = len;
  147. if (signal_pending_state(TASK_KILLABLE, current)) {
  148. error = -EINTR;
  149. break;
  150. }
  151. bytes = do_splice_direct(old_file, &old_pos,
  152. new_file, &new_pos,
  153. this_len, SPLICE_F_MOVE);
  154. if (bytes <= 0) {
  155. error = bytes;
  156. break;
  157. }
  158. WARN_ON(old_pos != new_pos);
  159. len -= bytes;
  160. }
  161. out:
  162. if (!error)
  163. error = vfs_fsync(new_file, 0);
  164. fput(new_file);
  165. out_fput:
  166. fput(old_file);
  167. return error;
  168. }
  169. static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
  170. {
  171. struct iattr attr = {
  172. .ia_valid =
  173. ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
  174. .ia_atime = stat->atime,
  175. .ia_mtime = stat->mtime,
  176. };
  177. return notify_change(upperdentry, &attr, NULL);
  178. }
  179. int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
  180. {
  181. int err = 0;
  182. if (!S_ISLNK(stat->mode)) {
  183. struct iattr attr = {
  184. .ia_valid = ATTR_MODE,
  185. .ia_mode = stat->mode,
  186. };
  187. err = notify_change(upperdentry, &attr, NULL);
  188. }
  189. if (!err) {
  190. struct iattr attr = {
  191. .ia_valid = ATTR_UID | ATTR_GID,
  192. .ia_uid = stat->uid,
  193. .ia_gid = stat->gid,
  194. };
  195. err = notify_change(upperdentry, &attr, NULL);
  196. }
  197. if (!err)
  198. ovl_set_timestamps(upperdentry, stat);
  199. return err;
  200. }
  201. struct ovl_fh *ovl_encode_fh(struct dentry *real, bool is_upper)
  202. {
  203. struct ovl_fh *fh;
  204. int fh_type, fh_len, dwords;
  205. void *buf;
  206. int buflen = MAX_HANDLE_SZ;
  207. uuid_t *uuid = &real->d_sb->s_uuid;
  208. buf = kmalloc(buflen, GFP_KERNEL);
  209. if (!buf)
  210. return ERR_PTR(-ENOMEM);
  211. /*
  212. * We encode a non-connectable file handle for non-dir, because we
  213. * only need to find the lower inode number and we don't want to pay
  214. * the price or reconnecting the dentry.
  215. */
  216. dwords = buflen >> 2;
  217. fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
  218. buflen = (dwords << 2);
  219. fh = ERR_PTR(-EIO);
  220. if (WARN_ON(fh_type < 0) ||
  221. WARN_ON(buflen > MAX_HANDLE_SZ) ||
  222. WARN_ON(fh_type == FILEID_INVALID))
  223. goto out;
  224. BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
  225. fh_len = offsetof(struct ovl_fh, fid) + buflen;
  226. fh = kmalloc(fh_len, GFP_KERNEL);
  227. if (!fh) {
  228. fh = ERR_PTR(-ENOMEM);
  229. goto out;
  230. }
  231. fh->version = OVL_FH_VERSION;
  232. fh->magic = OVL_FH_MAGIC;
  233. fh->type = fh_type;
  234. fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
  235. /*
  236. * When we will want to decode an overlay dentry from this handle
  237. * and all layers are on the same fs, if we get a disconncted real
  238. * dentry when we decode fid, the only way to tell if we should assign
  239. * it to upperdentry or to lowerstack is by checking this flag.
  240. */
  241. if (is_upper)
  242. fh->flags |= OVL_FH_FLAG_PATH_UPPER;
  243. fh->len = fh_len;
  244. fh->uuid = *uuid;
  245. memcpy(fh->fid, buf, buflen);
  246. out:
  247. kfree(buf);
  248. return fh;
  249. }
  250. int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
  251. struct dentry *upper)
  252. {
  253. const struct ovl_fh *fh = NULL;
  254. int err;
  255. /*
  256. * When lower layer doesn't support export operations store a 'null' fh,
  257. * so we can use the overlay.origin xattr to distignuish between a copy
  258. * up and a pure upper inode.
  259. */
  260. if (ovl_can_decode_fh(lower->d_sb)) {
  261. fh = ovl_encode_fh(lower, false);
  262. if (IS_ERR(fh))
  263. return PTR_ERR(fh);
  264. }
  265. /*
  266. * Do not fail when upper doesn't support xattrs.
  267. */
  268. err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
  269. fh ? fh->len : 0, 0);
  270. kfree(fh);
  271. return err;
  272. }
  273. /* Store file handle of @upper dir in @index dir entry */
  274. static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
  275. {
  276. const struct ovl_fh *fh;
  277. int err;
  278. fh = ovl_encode_fh(upper, true);
  279. if (IS_ERR(fh))
  280. return PTR_ERR(fh);
  281. err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);
  282. kfree(fh);
  283. return err;
  284. }
  285. /*
  286. * Create and install index entry.
  287. *
  288. * Caller must hold i_mutex on indexdir.
  289. */
  290. static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
  291. struct dentry *upper)
  292. {
  293. struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
  294. struct inode *dir = d_inode(indexdir);
  295. struct dentry *index = NULL;
  296. struct dentry *temp = NULL;
  297. struct qstr name = { };
  298. int err;
  299. /*
  300. * For now this is only used for creating index entry for directories,
  301. * because non-dir are copied up directly to index and then hardlinked
  302. * to upper dir.
  303. *
  304. * TODO: implement create index for non-dir, so we can call it when
  305. * encoding file handle for non-dir in case index does not exist.
  306. */
  307. if (WARN_ON(!d_is_dir(dentry)))
  308. return -EIO;
  309. /* Directory not expected to be indexed before copy up */
  310. if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
  311. return -EIO;
  312. err = ovl_get_index_name(origin, &name);
  313. if (err)
  314. return err;
  315. temp = ovl_lookup_temp(indexdir);
  316. if (IS_ERR(temp))
  317. goto temp_err;
  318. err = ovl_do_mkdir(dir, temp, S_IFDIR, true);
  319. if (err)
  320. goto out;
  321. err = ovl_set_upper_fh(upper, temp);
  322. if (err)
  323. goto out_cleanup;
  324. index = lookup_one_len(name.name, indexdir, name.len);
  325. if (IS_ERR(index)) {
  326. err = PTR_ERR(index);
  327. } else {
  328. err = ovl_do_rename(dir, temp, dir, index, 0);
  329. dput(index);
  330. }
  331. if (err)
  332. goto out_cleanup;
  333. out:
  334. dput(temp);
  335. kfree(name.name);
  336. return err;
  337. temp_err:
  338. err = PTR_ERR(temp);
  339. temp = NULL;
  340. goto out;
  341. out_cleanup:
  342. ovl_cleanup(dir, temp);
  343. goto out;
  344. }
  345. struct ovl_copy_up_ctx {
  346. struct dentry *parent;
  347. struct dentry *dentry;
  348. struct path lowerpath;
  349. struct kstat stat;
  350. struct kstat pstat;
  351. const char *link;
  352. struct dentry *destdir;
  353. struct qstr destname;
  354. struct dentry *workdir;
  355. bool tmpfile;
  356. bool origin;
  357. bool indexed;
  358. };
  359. static int ovl_link_up(struct ovl_copy_up_ctx *c)
  360. {
  361. int err;
  362. struct dentry *upper;
  363. struct dentry *upperdir = ovl_dentry_upper(c->parent);
  364. struct inode *udir = d_inode(upperdir);
  365. /* Mark parent "impure" because it may now contain non-pure upper */
  366. err = ovl_set_impure(c->parent, upperdir);
  367. if (err)
  368. return err;
  369. err = ovl_set_nlink_lower(c->dentry);
  370. if (err)
  371. return err;
  372. inode_lock_nested(udir, I_MUTEX_PARENT);
  373. upper = lookup_one_len(c->dentry->d_name.name, upperdir,
  374. c->dentry->d_name.len);
  375. err = PTR_ERR(upper);
  376. if (!IS_ERR(upper)) {
  377. err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper,
  378. true);
  379. dput(upper);
  380. if (!err) {
  381. /* Restore timestamps on parent (best effort) */
  382. ovl_set_timestamps(upperdir, &c->pstat);
  383. ovl_dentry_set_upper_alias(c->dentry);
  384. }
  385. }
  386. inode_unlock(udir);
  387. if (err)
  388. return err;
  389. err = ovl_set_nlink_upper(c->dentry);
  390. return err;
  391. }
  392. static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
  393. struct dentry **newdentry)
  394. {
  395. int err;
  396. struct dentry *upper;
  397. struct inode *udir = d_inode(c->destdir);
  398. upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
  399. if (IS_ERR(upper))
  400. return PTR_ERR(upper);
  401. if (c->tmpfile)
  402. err = ovl_do_link(temp, udir, upper, true);
  403. else
  404. err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
  405. if (!err)
  406. *newdentry = dget(c->tmpfile ? upper : temp);
  407. dput(upper);
  408. return err;
  409. }
  410. static int ovl_get_tmpfile(struct ovl_copy_up_ctx *c, struct dentry **tempp)
  411. {
  412. int err;
  413. struct dentry *temp;
  414. const struct cred *old_creds = NULL;
  415. struct cred *new_creds = NULL;
  416. struct cattr cattr = {
  417. /* Can't properly set mode on creation because of the umask */
  418. .mode = c->stat.mode & S_IFMT,
  419. .rdev = c->stat.rdev,
  420. .link = c->link
  421. };
  422. err = security_inode_copy_up(c->dentry, &new_creds);
  423. if (err < 0)
  424. goto out;
  425. if (new_creds)
  426. old_creds = override_creds(new_creds);
  427. if (c->tmpfile) {
  428. temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
  429. if (IS_ERR(temp))
  430. goto temp_err;
  431. } else {
  432. temp = ovl_lookup_temp(c->workdir);
  433. if (IS_ERR(temp))
  434. goto temp_err;
  435. err = ovl_create_real(d_inode(c->workdir), temp, &cattr,
  436. NULL, true);
  437. if (err) {
  438. dput(temp);
  439. goto out;
  440. }
  441. }
  442. err = 0;
  443. *tempp = temp;
  444. out:
  445. if (new_creds) {
  446. revert_creds(old_creds);
  447. put_cred(new_creds);
  448. }
  449. return err;
  450. temp_err:
  451. err = PTR_ERR(temp);
  452. goto out;
  453. }
  454. static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
  455. {
  456. int err;
  457. if (S_ISREG(c->stat.mode)) {
  458. struct path upperpath;
  459. ovl_path_upper(c->dentry, &upperpath);
  460. BUG_ON(upperpath.dentry != NULL);
  461. upperpath.dentry = temp;
  462. err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
  463. if (err)
  464. return err;
  465. }
  466. err = ovl_copy_xattr(c->lowerpath.dentry, temp);
  467. if (err)
  468. return err;
  469. inode_lock(temp->d_inode);
  470. err = ovl_set_attr(temp, &c->stat);
  471. inode_unlock(temp->d_inode);
  472. if (err)
  473. return err;
  474. /*
  475. * Store identifier of lower inode in upper inode xattr to
  476. * allow lookup of the copy up origin inode.
  477. *
  478. * Don't set origin when we are breaking the association with a lower
  479. * hard link.
  480. */
  481. if (c->origin) {
  482. err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
  483. if (err)
  484. return err;
  485. }
  486. return 0;
  487. }
  488. static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
  489. {
  490. struct inode *udir = c->destdir->d_inode;
  491. struct inode *inode;
  492. struct dentry *newdentry = NULL;
  493. struct dentry *temp = NULL;
  494. int err;
  495. err = ovl_get_tmpfile(c, &temp);
  496. if (err)
  497. goto out;
  498. err = ovl_copy_up_inode(c, temp);
  499. if (err)
  500. goto out_cleanup;
  501. if (S_ISDIR(c->stat.mode) && c->indexed) {
  502. err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
  503. if (err)
  504. goto out_cleanup;
  505. }
  506. if (c->tmpfile) {
  507. inode_lock_nested(udir, I_MUTEX_PARENT);
  508. err = ovl_install_temp(c, temp, &newdentry);
  509. inode_unlock(udir);
  510. } else {
  511. err = ovl_install_temp(c, temp, &newdentry);
  512. }
  513. if (err)
  514. goto out_cleanup;
  515. inode = d_inode(c->dentry);
  516. ovl_inode_update(inode, newdentry);
  517. if (S_ISDIR(inode->i_mode))
  518. ovl_set_flag(OVL_WHITEOUTS, inode);
  519. out:
  520. dput(temp);
  521. return err;
  522. out_cleanup:
  523. if (!c->tmpfile)
  524. ovl_cleanup(d_inode(c->workdir), temp);
  525. goto out;
  526. }
  527. /*
  528. * Copy up a single dentry
  529. *
  530. * All renames start with copy up of source if necessary. The actual
  531. * rename will only proceed once the copy up was successful. Copy up uses
  532. * upper parent i_mutex for exclusion. Since rename can change d_parent it
  533. * is possible that the copy up will lock the old parent. At that point
  534. * the file will have already been copied up anyway.
  535. */
  536. static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
  537. {
  538. int err;
  539. struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
  540. bool to_index = false;
  541. /*
  542. * Indexed non-dir is copied up directly to the index entry and then
  543. * hardlinked to upper dir. Indexed dir is copied up to indexdir,
  544. * then index entry is created and then copied up dir installed.
  545. * Copying dir up to indexdir instead of workdir simplifies locking.
  546. */
  547. if (ovl_need_index(c->dentry)) {
  548. c->indexed = true;
  549. if (S_ISDIR(c->stat.mode))
  550. c->workdir = ovl_indexdir(c->dentry->d_sb);
  551. else
  552. to_index = true;
  553. }
  554. if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
  555. c->origin = true;
  556. if (to_index) {
  557. c->destdir = ovl_indexdir(c->dentry->d_sb);
  558. err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
  559. if (err)
  560. return err;
  561. } else if (WARN_ON(!c->parent)) {
  562. /* Disconnected dentry must be copied up to index dir */
  563. return -EIO;
  564. } else {
  565. /*
  566. * Mark parent "impure" because it may now contain non-pure
  567. * upper
  568. */
  569. err = ovl_set_impure(c->parent, c->destdir);
  570. if (err)
  571. return err;
  572. }
  573. /* Should we copyup with O_TMPFILE or with workdir? */
  574. if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
  575. c->tmpfile = true;
  576. err = ovl_copy_up_locked(c);
  577. } else {
  578. err = ovl_lock_rename_workdir(c->workdir, c->destdir);
  579. if (!err) {
  580. err = ovl_copy_up_locked(c);
  581. unlock_rename(c->workdir, c->destdir);
  582. }
  583. }
  584. if (err)
  585. goto out;
  586. if (c->indexed)
  587. ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
  588. if (to_index) {
  589. /* Initialize nlink for copy up of disconnected dentry */
  590. err = ovl_set_nlink_upper(c->dentry);
  591. } else {
  592. struct inode *udir = d_inode(c->destdir);
  593. /* Restore timestamps on parent (best effort) */
  594. inode_lock(udir);
  595. ovl_set_timestamps(c->destdir, &c->pstat);
  596. inode_unlock(udir);
  597. ovl_dentry_set_upper_alias(c->dentry);
  598. }
  599. out:
  600. if (to_index)
  601. kfree(c->destname.name);
  602. return err;
  603. }
  604. static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  605. int flags)
  606. {
  607. int err;
  608. DEFINE_DELAYED_CALL(done);
  609. struct path parentpath;
  610. struct ovl_copy_up_ctx ctx = {
  611. .parent = parent,
  612. .dentry = dentry,
  613. .workdir = ovl_workdir(dentry),
  614. };
  615. if (WARN_ON(!ctx.workdir))
  616. return -EROFS;
  617. ovl_path_lower(dentry, &ctx.lowerpath);
  618. err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
  619. STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  620. if (err)
  621. return err;
  622. if (parent) {
  623. ovl_path_upper(parent, &parentpath);
  624. ctx.destdir = parentpath.dentry;
  625. ctx.destname = dentry->d_name;
  626. err = vfs_getattr(&parentpath, &ctx.pstat,
  627. STATX_ATIME | STATX_MTIME,
  628. AT_STATX_SYNC_AS_STAT);
  629. if (err)
  630. return err;
  631. }
  632. /* maybe truncate regular file. this has no effect on dirs */
  633. if (flags & O_TRUNC)
  634. ctx.stat.size = 0;
  635. if (S_ISLNK(ctx.stat.mode)) {
  636. ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
  637. if (IS_ERR(ctx.link))
  638. return PTR_ERR(ctx.link);
  639. }
  640. ovl_do_check_copy_up(ctx.lowerpath.dentry);
  641. err = ovl_copy_up_start(dentry);
  642. /* err < 0: interrupted, err > 0: raced with another copy-up */
  643. if (unlikely(err)) {
  644. if (err > 0)
  645. err = 0;
  646. } else {
  647. if (!ovl_dentry_upper(dentry))
  648. err = ovl_do_copy_up(&ctx);
  649. if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
  650. err = ovl_link_up(&ctx);
  651. ovl_copy_up_end(dentry);
  652. }
  653. do_delayed_call(&done);
  654. return err;
  655. }
  656. int ovl_copy_up_flags(struct dentry *dentry, int flags)
  657. {
  658. int err = 0;
  659. const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
  660. bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
  661. /*
  662. * With NFS export, copy up can get called for a disconnected non-dir.
  663. * In this case, we will copy up lower inode to index dir without
  664. * linking it to upper dir.
  665. */
  666. if (WARN_ON(disconnected && d_is_dir(dentry)))
  667. return -EIO;
  668. while (!err) {
  669. struct dentry *next;
  670. struct dentry *parent = NULL;
  671. /*
  672. * Check if copy-up has happened as well as for upper alias (in
  673. * case of hard links) is there.
  674. *
  675. * Both checks are lockless:
  676. * - false negatives: will recheck under oi->lock
  677. * - false positives:
  678. * + ovl_dentry_upper() uses memory barriers to ensure the
  679. * upper dentry is up-to-date
  680. * + ovl_dentry_has_upper_alias() relies on locking of
  681. * upper parent i_rwsem to prevent reordering copy-up
  682. * with rename.
  683. */
  684. if (ovl_dentry_upper(dentry) &&
  685. (ovl_dentry_has_upper_alias(dentry) || disconnected))
  686. break;
  687. next = dget(dentry);
  688. /* find the topmost dentry not yet copied up */
  689. for (; !disconnected;) {
  690. parent = dget_parent(next);
  691. if (ovl_dentry_upper(parent))
  692. break;
  693. dput(next);
  694. next = parent;
  695. }
  696. err = ovl_copy_up_one(parent, next, flags);
  697. dput(parent);
  698. dput(next);
  699. }
  700. revert_creds(old_cred);
  701. return err;
  702. }
  703. int ovl_copy_up(struct dentry *dentry)
  704. {
  705. return ovl_copy_up_flags(dentry, 0);
  706. }