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 error = 0;
  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. error = security_inode_copy_up_xattr(name);
  70. if (error < 0 && error != -EOPNOTSUPP)
  71. break;
  72. if (error == 1) {
  73. error = 0;
  74. continue; /* Discard */
  75. }
  76. retry:
  77. size = vfs_getxattr(old, name, value, value_size);
  78. if (size == -ERANGE)
  79. size = vfs_getxattr(old, name, NULL, 0);
  80. if (size < 0) {
  81. error = size;
  82. break;
  83. }
  84. if (size > value_size) {
  85. void *new;
  86. new = krealloc(value, size, GFP_KERNEL);
  87. if (!new) {
  88. error = -ENOMEM;
  89. break;
  90. }
  91. value = new;
  92. value_size = size;
  93. goto retry;
  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. int error = 0;
  111. if (len == 0)
  112. return 0;
  113. old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
  114. if (IS_ERR(old_file))
  115. return PTR_ERR(old_file);
  116. new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
  117. if (IS_ERR(new_file)) {
  118. error = PTR_ERR(new_file);
  119. goto out_fput;
  120. }
  121. /* Try to use clone_file_range to clone up within the same fs */
  122. error = do_clone_file_range(old_file, 0, new_file, 0, len);
  123. if (!error)
  124. goto out;
  125. /* Couldn't clone, so now we try to copy the data */
  126. error = 0;
  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 tmpfile;
  342. bool origin;
  343. bool indexed;
  344. bool metacopy;
  345. };
  346. static int ovl_link_up(struct ovl_copy_up_ctx *c)
  347. {
  348. int err;
  349. struct dentry *upper;
  350. struct dentry *upperdir = ovl_dentry_upper(c->parent);
  351. struct inode *udir = d_inode(upperdir);
  352. /* Mark parent "impure" because it may now contain non-pure upper */
  353. err = ovl_set_impure(c->parent, upperdir);
  354. if (err)
  355. return err;
  356. err = ovl_set_nlink_lower(c->dentry);
  357. if (err)
  358. return err;
  359. inode_lock_nested(udir, I_MUTEX_PARENT);
  360. upper = lookup_one_len(c->dentry->d_name.name, upperdir,
  361. c->dentry->d_name.len);
  362. err = PTR_ERR(upper);
  363. if (!IS_ERR(upper)) {
  364. err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
  365. dput(upper);
  366. if (!err) {
  367. /* Restore timestamps on parent (best effort) */
  368. ovl_set_timestamps(upperdir, &c->pstat);
  369. ovl_dentry_set_upper_alias(c->dentry);
  370. }
  371. }
  372. inode_unlock(udir);
  373. if (err)
  374. return err;
  375. err = ovl_set_nlink_upper(c->dentry);
  376. return err;
  377. }
  378. static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
  379. struct dentry **newdentry)
  380. {
  381. int err;
  382. struct dentry *upper;
  383. struct inode *udir = d_inode(c->destdir);
  384. upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
  385. if (IS_ERR(upper))
  386. return PTR_ERR(upper);
  387. if (c->tmpfile)
  388. err = ovl_do_link(temp, udir, upper);
  389. else
  390. err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
  391. if (!err)
  392. *newdentry = dget(c->tmpfile ? upper : temp);
  393. dput(upper);
  394. return err;
  395. }
  396. static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c)
  397. {
  398. int err;
  399. struct dentry *temp;
  400. const struct cred *old_creds = NULL;
  401. struct cred *new_creds = NULL;
  402. struct ovl_cattr cattr = {
  403. /* Can't properly set mode on creation because of the umask */
  404. .mode = c->stat.mode & S_IFMT,
  405. .rdev = c->stat.rdev,
  406. .link = c->link
  407. };
  408. err = security_inode_copy_up(c->dentry, &new_creds);
  409. temp = ERR_PTR(err);
  410. if (err < 0)
  411. goto out;
  412. if (new_creds)
  413. old_creds = override_creds(new_creds);
  414. if (c->tmpfile)
  415. temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
  416. else
  417. temp = ovl_create_temp(c->workdir, &cattr);
  418. out:
  419. if (new_creds) {
  420. revert_creds(old_creds);
  421. put_cred(new_creds);
  422. }
  423. return temp;
  424. }
  425. static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
  426. {
  427. int err;
  428. /*
  429. * Copy up data first and then xattrs. Writing data after
  430. * xattrs will remove security.capability xattr automatically.
  431. */
  432. if (S_ISREG(c->stat.mode) && !c->metacopy) {
  433. struct path upperpath, datapath;
  434. ovl_path_upper(c->dentry, &upperpath);
  435. if (WARN_ON(upperpath.dentry != NULL))
  436. return -EIO;
  437. upperpath.dentry = temp;
  438. ovl_path_lowerdata(c->dentry, &datapath);
  439. err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
  440. if (err)
  441. return err;
  442. }
  443. err = ovl_copy_xattr(c->lowerpath.dentry, temp);
  444. if (err)
  445. return err;
  446. /*
  447. * Store identifier of lower inode in upper inode xattr to
  448. * allow lookup of the copy up origin inode.
  449. *
  450. * Don't set origin when we are breaking the association with a lower
  451. * hard link.
  452. */
  453. if (c->origin) {
  454. err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
  455. if (err)
  456. return err;
  457. }
  458. if (c->metacopy) {
  459. err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
  460. NULL, 0, -EOPNOTSUPP);
  461. if (err)
  462. return err;
  463. }
  464. inode_lock(temp->d_inode);
  465. if (c->metacopy)
  466. err = ovl_set_size(temp, &c->stat);
  467. if (!err)
  468. err = ovl_set_attr(temp, &c->stat);
  469. inode_unlock(temp->d_inode);
  470. return err;
  471. }
  472. static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
  473. {
  474. struct inode *udir = c->destdir->d_inode;
  475. struct inode *inode;
  476. struct dentry *newdentry = NULL;
  477. struct dentry *temp;
  478. int err;
  479. temp = ovl_get_tmpfile(c);
  480. if (IS_ERR(temp))
  481. return PTR_ERR(temp);
  482. err = ovl_copy_up_inode(c, temp);
  483. if (err)
  484. goto out;
  485. if (S_ISDIR(c->stat.mode) && c->indexed) {
  486. err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
  487. if (err)
  488. goto out;
  489. }
  490. if (c->tmpfile) {
  491. inode_lock_nested(udir, I_MUTEX_PARENT);
  492. err = ovl_install_temp(c, temp, &newdentry);
  493. inode_unlock(udir);
  494. } else {
  495. err = ovl_install_temp(c, temp, &newdentry);
  496. }
  497. if (err)
  498. goto out;
  499. if (!c->metacopy)
  500. ovl_set_upperdata(d_inode(c->dentry));
  501. inode = d_inode(c->dentry);
  502. ovl_inode_update(inode, newdentry);
  503. if (S_ISDIR(inode->i_mode))
  504. ovl_set_flag(OVL_WHITEOUTS, inode);
  505. out:
  506. if (err && !c->tmpfile)
  507. ovl_cleanup(d_inode(c->workdir), temp);
  508. dput(temp);
  509. return err;
  510. }
  511. /*
  512. * Copy up a single dentry
  513. *
  514. * All renames start with copy up of source if necessary. The actual
  515. * rename will only proceed once the copy up was successful. Copy up uses
  516. * upper parent i_mutex for exclusion. Since rename can change d_parent it
  517. * is possible that the copy up will lock the old parent. At that point
  518. * the file will have already been copied up anyway.
  519. */
  520. static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
  521. {
  522. int err;
  523. struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
  524. bool to_index = false;
  525. /*
  526. * Indexed non-dir is copied up directly to the index entry and then
  527. * hardlinked to upper dir. Indexed dir is copied up to indexdir,
  528. * then index entry is created and then copied up dir installed.
  529. * Copying dir up to indexdir instead of workdir simplifies locking.
  530. */
  531. if (ovl_need_index(c->dentry)) {
  532. c->indexed = true;
  533. if (S_ISDIR(c->stat.mode))
  534. c->workdir = ovl_indexdir(c->dentry->d_sb);
  535. else
  536. to_index = true;
  537. }
  538. if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
  539. c->origin = true;
  540. if (to_index) {
  541. c->destdir = ovl_indexdir(c->dentry->d_sb);
  542. err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
  543. if (err)
  544. return err;
  545. } else if (WARN_ON(!c->parent)) {
  546. /* Disconnected dentry must be copied up to index dir */
  547. return -EIO;
  548. } else {
  549. /*
  550. * Mark parent "impure" because it may now contain non-pure
  551. * upper
  552. */
  553. err = ovl_set_impure(c->parent, c->destdir);
  554. if (err)
  555. return err;
  556. }
  557. /* Should we copyup with O_TMPFILE or with workdir? */
  558. if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
  559. c->tmpfile = true;
  560. err = ovl_copy_up_locked(c);
  561. } else {
  562. err = ovl_lock_rename_workdir(c->workdir, c->destdir);
  563. if (!err) {
  564. err = ovl_copy_up_locked(c);
  565. unlock_rename(c->workdir, c->destdir);
  566. }
  567. }
  568. if (err)
  569. goto out;
  570. if (c->indexed)
  571. ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
  572. if (to_index) {
  573. /* Initialize nlink for copy up of disconnected dentry */
  574. err = ovl_set_nlink_upper(c->dentry);
  575. } else {
  576. struct inode *udir = d_inode(c->destdir);
  577. /* Restore timestamps on parent (best effort) */
  578. inode_lock(udir);
  579. ovl_set_timestamps(c->destdir, &c->pstat);
  580. inode_unlock(udir);
  581. ovl_dentry_set_upper_alias(c->dentry);
  582. }
  583. out:
  584. if (to_index)
  585. kfree(c->destname.name);
  586. return err;
  587. }
  588. static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
  589. int flags)
  590. {
  591. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  592. if (!ofs->config.metacopy)
  593. return false;
  594. if (!S_ISREG(mode))
  595. return false;
  596. if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
  597. return false;
  598. return true;
  599. }
  600. /* Copy up data of an inode which was copied up metadata only in the past. */
  601. static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
  602. {
  603. struct path upperpath, datapath;
  604. int err;
  605. char *capability = NULL;
  606. ssize_t uninitialized_var(cap_size);
  607. ovl_path_upper(c->dentry, &upperpath);
  608. if (WARN_ON(upperpath.dentry == NULL))
  609. return -EIO;
  610. ovl_path_lowerdata(c->dentry, &datapath);
  611. if (WARN_ON(datapath.dentry == NULL))
  612. return -EIO;
  613. if (c->stat.size) {
  614. err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
  615. &capability, 0);
  616. if (err < 0 && err != -ENODATA)
  617. goto out;
  618. }
  619. err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
  620. if (err)
  621. goto out_free;
  622. /*
  623. * Writing to upper file will clear security.capability xattr. We
  624. * don't want that to happen for normal copy-up operation.
  625. */
  626. if (capability) {
  627. err = ovl_do_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
  628. capability, cap_size, 0);
  629. if (err)
  630. goto out_free;
  631. }
  632. err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
  633. if (err)
  634. goto out_free;
  635. ovl_set_upperdata(d_inode(c->dentry));
  636. out_free:
  637. kfree(capability);
  638. out:
  639. return err;
  640. }
  641. static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  642. int flags)
  643. {
  644. int err;
  645. DEFINE_DELAYED_CALL(done);
  646. struct path parentpath;
  647. struct ovl_copy_up_ctx ctx = {
  648. .parent = parent,
  649. .dentry = dentry,
  650. .workdir = ovl_workdir(dentry),
  651. };
  652. if (WARN_ON(!ctx.workdir))
  653. return -EROFS;
  654. ovl_path_lower(dentry, &ctx.lowerpath);
  655. err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
  656. STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  657. if (err)
  658. return err;
  659. ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
  660. if (parent) {
  661. ovl_path_upper(parent, &parentpath);
  662. ctx.destdir = parentpath.dentry;
  663. ctx.destname = dentry->d_name;
  664. err = vfs_getattr(&parentpath, &ctx.pstat,
  665. STATX_ATIME | STATX_MTIME,
  666. AT_STATX_SYNC_AS_STAT);
  667. if (err)
  668. return err;
  669. }
  670. /* maybe truncate regular file. this has no effect on dirs */
  671. if (flags & O_TRUNC)
  672. ctx.stat.size = 0;
  673. if (S_ISLNK(ctx.stat.mode)) {
  674. ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
  675. if (IS_ERR(ctx.link))
  676. return PTR_ERR(ctx.link);
  677. }
  678. err = ovl_copy_up_start(dentry, flags);
  679. /* err < 0: interrupted, err > 0: raced with another copy-up */
  680. if (unlikely(err)) {
  681. if (err > 0)
  682. err = 0;
  683. } else {
  684. if (!ovl_dentry_upper(dentry))
  685. err = ovl_do_copy_up(&ctx);
  686. if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
  687. err = ovl_link_up(&ctx);
  688. if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
  689. err = ovl_copy_up_meta_inode_data(&ctx);
  690. ovl_copy_up_end(dentry);
  691. }
  692. do_delayed_call(&done);
  693. return err;
  694. }
  695. int ovl_copy_up_flags(struct dentry *dentry, int flags)
  696. {
  697. int err = 0;
  698. const struct cred *old_cred;
  699. bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
  700. /*
  701. * With NFS export, copy up can get called for a disconnected non-dir.
  702. * In this case, we will copy up lower inode to index dir without
  703. * linking it to upper dir.
  704. */
  705. if (WARN_ON(disconnected && d_is_dir(dentry)))
  706. return -EIO;
  707. old_cred = ovl_override_creds(dentry->d_sb);
  708. while (!err) {
  709. struct dentry *next;
  710. struct dentry *parent = NULL;
  711. if (ovl_already_copied_up(dentry, flags))
  712. break;
  713. next = dget(dentry);
  714. /* find the topmost dentry not yet copied up */
  715. for (; !disconnected;) {
  716. parent = dget_parent(next);
  717. if (ovl_dentry_upper(parent))
  718. break;
  719. dput(next);
  720. next = parent;
  721. }
  722. err = ovl_copy_up_one(parent, next, flags);
  723. dput(parent);
  724. dput(next);
  725. }
  726. revert_creds(old_cred);
  727. return err;
  728. }
  729. static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
  730. {
  731. /* Copy up of disconnected dentry does not set upper alias */
  732. if (ovl_already_copied_up(dentry, flags))
  733. return false;
  734. if (special_file(d_inode(dentry)->i_mode))
  735. return false;
  736. if (!ovl_open_flags_need_copy_up(flags))
  737. return false;
  738. return true;
  739. }
  740. int ovl_maybe_copy_up(struct dentry *dentry, int flags)
  741. {
  742. int err = 0;
  743. if (ovl_open_need_copy_up(dentry, flags)) {
  744. err = ovl_want_write(dentry);
  745. if (!err) {
  746. err = ovl_copy_up_flags(dentry, flags);
  747. ovl_drop_write(dentry);
  748. }
  749. }
  750. return err;
  751. }
  752. int ovl_copy_up_with_data(struct dentry *dentry)
  753. {
  754. return ovl_copy_up_flags(dentry, O_WRONLY);
  755. }
  756. int ovl_copy_up(struct dentry *dentry)
  757. {
  758. return ovl_copy_up_flags(dentry, 0);
  759. }