copy_up.c 20 KB

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