copy_up.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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_real_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_real_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_real_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_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
  316. err = PTR_ERR(temp);
  317. if (IS_ERR(temp))
  318. goto free_name;
  319. err = ovl_set_upper_fh(upper, temp);
  320. if (err)
  321. goto out;
  322. index = lookup_one_len(name.name, indexdir, name.len);
  323. if (IS_ERR(index)) {
  324. err = PTR_ERR(index);
  325. } else {
  326. err = ovl_do_rename(dir, temp, dir, index, 0);
  327. dput(index);
  328. }
  329. out:
  330. if (err)
  331. ovl_cleanup(dir, temp);
  332. dput(temp);
  333. free_name:
  334. kfree(name.name);
  335. return err;
  336. }
  337. struct ovl_copy_up_ctx {
  338. struct dentry *parent;
  339. struct dentry *dentry;
  340. struct path lowerpath;
  341. struct kstat stat;
  342. struct kstat pstat;
  343. const char *link;
  344. struct dentry *destdir;
  345. struct qstr destname;
  346. struct dentry *workdir;
  347. bool tmpfile;
  348. bool origin;
  349. bool indexed;
  350. };
  351. static int ovl_link_up(struct ovl_copy_up_ctx *c)
  352. {
  353. int err;
  354. struct dentry *upper;
  355. struct dentry *upperdir = ovl_dentry_upper(c->parent);
  356. struct inode *udir = d_inode(upperdir);
  357. /* Mark parent "impure" because it may now contain non-pure upper */
  358. err = ovl_set_impure(c->parent, upperdir);
  359. if (err)
  360. return err;
  361. err = ovl_set_nlink_lower(c->dentry);
  362. if (err)
  363. return err;
  364. inode_lock_nested(udir, I_MUTEX_PARENT);
  365. upper = lookup_one_len(c->dentry->d_name.name, upperdir,
  366. c->dentry->d_name.len);
  367. err = PTR_ERR(upper);
  368. if (!IS_ERR(upper)) {
  369. err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
  370. dput(upper);
  371. if (!err) {
  372. /* Restore timestamps on parent (best effort) */
  373. ovl_set_timestamps(upperdir, &c->pstat);
  374. ovl_dentry_set_upper_alias(c->dentry);
  375. }
  376. }
  377. inode_unlock(udir);
  378. if (err)
  379. return err;
  380. err = ovl_set_nlink_upper(c->dentry);
  381. return err;
  382. }
  383. static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
  384. struct dentry **newdentry)
  385. {
  386. int err;
  387. struct dentry *upper;
  388. struct inode *udir = d_inode(c->destdir);
  389. upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
  390. if (IS_ERR(upper))
  391. return PTR_ERR(upper);
  392. if (c->tmpfile)
  393. err = ovl_do_link(temp, udir, upper);
  394. else
  395. err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
  396. if (!err)
  397. *newdentry = dget(c->tmpfile ? upper : temp);
  398. dput(upper);
  399. return err;
  400. }
  401. static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c)
  402. {
  403. int err;
  404. struct dentry *temp;
  405. const struct cred *old_creds = NULL;
  406. struct cred *new_creds = NULL;
  407. struct ovl_cattr cattr = {
  408. /* Can't properly set mode on creation because of the umask */
  409. .mode = c->stat.mode & S_IFMT,
  410. .rdev = c->stat.rdev,
  411. .link = c->link
  412. };
  413. err = security_inode_copy_up(c->dentry, &new_creds);
  414. temp = ERR_PTR(err);
  415. if (err < 0)
  416. goto out;
  417. if (new_creds)
  418. old_creds = override_creds(new_creds);
  419. if (c->tmpfile)
  420. temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
  421. else
  422. temp = ovl_create_temp(c->workdir, &cattr);
  423. out:
  424. if (new_creds) {
  425. revert_creds(old_creds);
  426. put_cred(new_creds);
  427. }
  428. return temp;
  429. }
  430. static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
  431. {
  432. int err;
  433. if (S_ISREG(c->stat.mode)) {
  434. struct path upperpath;
  435. ovl_path_upper(c->dentry, &upperpath);
  436. BUG_ON(upperpath.dentry != NULL);
  437. upperpath.dentry = temp;
  438. err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
  439. if (err)
  440. return err;
  441. }
  442. err = ovl_copy_xattr(c->lowerpath.dentry, temp);
  443. if (err)
  444. return err;
  445. inode_lock(temp->d_inode);
  446. err = ovl_set_attr(temp, &c->stat);
  447. inode_unlock(temp->d_inode);
  448. if (err)
  449. return err;
  450. /*
  451. * Store identifier of lower inode in upper inode xattr to
  452. * allow lookup of the copy up origin inode.
  453. *
  454. * Don't set origin when we are breaking the association with a lower
  455. * hard link.
  456. */
  457. if (c->origin) {
  458. err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
  459. if (err)
  460. return err;
  461. }
  462. return 0;
  463. }
  464. static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
  465. {
  466. struct inode *udir = c->destdir->d_inode;
  467. struct inode *inode;
  468. struct dentry *newdentry = NULL;
  469. struct dentry *temp;
  470. int err;
  471. temp = ovl_get_tmpfile(c);
  472. if (IS_ERR(temp))
  473. return PTR_ERR(temp);
  474. err = ovl_copy_up_inode(c, temp);
  475. if (err)
  476. goto out;
  477. if (S_ISDIR(c->stat.mode) && c->indexed) {
  478. err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
  479. if (err)
  480. goto out;
  481. }
  482. if (c->tmpfile) {
  483. inode_lock_nested(udir, I_MUTEX_PARENT);
  484. err = ovl_install_temp(c, temp, &newdentry);
  485. inode_unlock(udir);
  486. } else {
  487. err = ovl_install_temp(c, temp, &newdentry);
  488. }
  489. if (err)
  490. goto out;
  491. inode = d_inode(c->dentry);
  492. ovl_inode_update(inode, newdentry);
  493. if (S_ISDIR(inode->i_mode))
  494. ovl_set_flag(OVL_WHITEOUTS, inode);
  495. out:
  496. if (err && !c->tmpfile)
  497. ovl_cleanup(d_inode(c->workdir), temp);
  498. dput(temp);
  499. return err;
  500. }
  501. /*
  502. * Copy up a single dentry
  503. *
  504. * All renames start with copy up of source if necessary. The actual
  505. * rename will only proceed once the copy up was successful. Copy up uses
  506. * upper parent i_mutex for exclusion. Since rename can change d_parent it
  507. * is possible that the copy up will lock the old parent. At that point
  508. * the file will have already been copied up anyway.
  509. */
  510. static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
  511. {
  512. int err;
  513. struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
  514. bool to_index = false;
  515. /*
  516. * Indexed non-dir is copied up directly to the index entry and then
  517. * hardlinked to upper dir. Indexed dir is copied up to indexdir,
  518. * then index entry is created and then copied up dir installed.
  519. * Copying dir up to indexdir instead of workdir simplifies locking.
  520. */
  521. if (ovl_need_index(c->dentry)) {
  522. c->indexed = true;
  523. if (S_ISDIR(c->stat.mode))
  524. c->workdir = ovl_indexdir(c->dentry->d_sb);
  525. else
  526. to_index = true;
  527. }
  528. if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
  529. c->origin = true;
  530. if (to_index) {
  531. c->destdir = ovl_indexdir(c->dentry->d_sb);
  532. err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
  533. if (err)
  534. return err;
  535. } else if (WARN_ON(!c->parent)) {
  536. /* Disconnected dentry must be copied up to index dir */
  537. return -EIO;
  538. } else {
  539. /*
  540. * Mark parent "impure" because it may now contain non-pure
  541. * upper
  542. */
  543. err = ovl_set_impure(c->parent, c->destdir);
  544. if (err)
  545. return err;
  546. }
  547. /* Should we copyup with O_TMPFILE or with workdir? */
  548. if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
  549. c->tmpfile = true;
  550. err = ovl_copy_up_locked(c);
  551. } else {
  552. err = ovl_lock_rename_workdir(c->workdir, c->destdir);
  553. if (!err) {
  554. err = ovl_copy_up_locked(c);
  555. unlock_rename(c->workdir, c->destdir);
  556. }
  557. }
  558. if (err)
  559. goto out;
  560. if (c->indexed)
  561. ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
  562. if (to_index) {
  563. /* Initialize nlink for copy up of disconnected dentry */
  564. err = ovl_set_nlink_upper(c->dentry);
  565. } else {
  566. struct inode *udir = d_inode(c->destdir);
  567. /* Restore timestamps on parent (best effort) */
  568. inode_lock(udir);
  569. ovl_set_timestamps(c->destdir, &c->pstat);
  570. inode_unlock(udir);
  571. ovl_dentry_set_upper_alias(c->dentry);
  572. }
  573. out:
  574. if (to_index)
  575. kfree(c->destname.name);
  576. return err;
  577. }
  578. static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  579. int flags)
  580. {
  581. int err;
  582. DEFINE_DELAYED_CALL(done);
  583. struct path parentpath;
  584. struct ovl_copy_up_ctx ctx = {
  585. .parent = parent,
  586. .dentry = dentry,
  587. .workdir = ovl_workdir(dentry),
  588. };
  589. if (WARN_ON(!ctx.workdir))
  590. return -EROFS;
  591. ovl_path_lower(dentry, &ctx.lowerpath);
  592. err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
  593. STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  594. if (err)
  595. return err;
  596. if (parent) {
  597. ovl_path_upper(parent, &parentpath);
  598. ctx.destdir = parentpath.dentry;
  599. ctx.destname = dentry->d_name;
  600. err = vfs_getattr(&parentpath, &ctx.pstat,
  601. STATX_ATIME | STATX_MTIME,
  602. AT_STATX_SYNC_AS_STAT);
  603. if (err)
  604. return err;
  605. }
  606. /* maybe truncate regular file. this has no effect on dirs */
  607. if (flags & O_TRUNC)
  608. ctx.stat.size = 0;
  609. if (S_ISLNK(ctx.stat.mode)) {
  610. ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
  611. if (IS_ERR(ctx.link))
  612. return PTR_ERR(ctx.link);
  613. }
  614. ovl_do_check_copy_up(ctx.lowerpath.dentry);
  615. err = ovl_copy_up_start(dentry);
  616. /* err < 0: interrupted, err > 0: raced with another copy-up */
  617. if (unlikely(err)) {
  618. if (err > 0)
  619. err = 0;
  620. } else {
  621. if (!ovl_dentry_upper(dentry))
  622. err = ovl_do_copy_up(&ctx);
  623. if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
  624. err = ovl_link_up(&ctx);
  625. ovl_copy_up_end(dentry);
  626. }
  627. do_delayed_call(&done);
  628. return err;
  629. }
  630. int ovl_copy_up_flags(struct dentry *dentry, int flags)
  631. {
  632. int err = 0;
  633. const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
  634. bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
  635. /*
  636. * With NFS export, copy up can get called for a disconnected non-dir.
  637. * In this case, we will copy up lower inode to index dir without
  638. * linking it to upper dir.
  639. */
  640. if (WARN_ON(disconnected && d_is_dir(dentry)))
  641. return -EIO;
  642. while (!err) {
  643. struct dentry *next;
  644. struct dentry *parent = NULL;
  645. /*
  646. * Check if copy-up has happened as well as for upper alias (in
  647. * case of hard links) is there.
  648. *
  649. * Both checks are lockless:
  650. * - false negatives: will recheck under oi->lock
  651. * - false positives:
  652. * + ovl_dentry_upper() uses memory barriers to ensure the
  653. * upper dentry is up-to-date
  654. * + ovl_dentry_has_upper_alias() relies on locking of
  655. * upper parent i_rwsem to prevent reordering copy-up
  656. * with rename.
  657. */
  658. if (ovl_dentry_upper(dentry) &&
  659. (ovl_dentry_has_upper_alias(dentry) || disconnected))
  660. break;
  661. next = dget(dentry);
  662. /* find the topmost dentry not yet copied up */
  663. for (; !disconnected;) {
  664. parent = dget_parent(next);
  665. if (ovl_dentry_upper(parent))
  666. break;
  667. dput(next);
  668. next = parent;
  669. }
  670. err = ovl_copy_up_one(parent, next, flags);
  671. dput(parent);
  672. dput(next);
  673. }
  674. revert_creds(old_cred);
  675. return err;
  676. }
  677. int ovl_copy_up(struct dentry *dentry)
  678. {
  679. return ovl_copy_up_flags(dentry, 0);
  680. }