copy_up.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2011 Novell Inc.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/fs.h>
  8. #include <linux/slab.h>
  9. #include <linux/file.h>
  10. #include <linux/fileattr.h>
  11. #include <linux/splice.h>
  12. #include <linux/xattr.h>
  13. #include <linux/security.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/cred.h>
  17. #include <linux/namei.h>
  18. #include <linux/fdtable.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/exportfs.h>
  21. #include "overlayfs.h"
  22. #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
  23. static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
  24. {
  25. pr_warn("\"check_copy_up\" module option is obsolete\n");
  26. return 0;
  27. }
  28. static int ovl_ccup_get(char *buf, const struct kernel_param *param)
  29. {
  30. return sprintf(buf, "N\n");
  31. }
  32. module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
  33. MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
  34. static bool ovl_must_copy_xattr(const char *name)
  35. {
  36. return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
  37. !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
  38. !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
  39. }
  40. int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct dentry *new)
  41. {
  42. struct dentry *old = oldpath->dentry;
  43. ssize_t list_size, size, value_size = 0;
  44. char *buf, *name, *value = NULL;
  45. int error = 0;
  46. size_t slen;
  47. if (!(old->d_inode->i_opflags & IOP_XATTR) ||
  48. !(new->d_inode->i_opflags & IOP_XATTR))
  49. return 0;
  50. list_size = vfs_listxattr(old, NULL, 0);
  51. if (list_size <= 0) {
  52. if (list_size == -EOPNOTSUPP)
  53. return 0;
  54. return list_size;
  55. }
  56. buf = kvzalloc(list_size, GFP_KERNEL);
  57. if (!buf)
  58. return -ENOMEM;
  59. list_size = vfs_listxattr(old, buf, list_size);
  60. if (list_size <= 0) {
  61. error = list_size;
  62. goto out;
  63. }
  64. for (name = buf; list_size; name += slen) {
  65. slen = strnlen(name, list_size) + 1;
  66. /* underlying fs providing us with an broken xattr list? */
  67. if (WARN_ON(slen > list_size)) {
  68. error = -EIO;
  69. break;
  70. }
  71. list_size -= slen;
  72. if (ovl_is_private_xattr(sb, name))
  73. continue;
  74. error = security_inode_copy_up_xattr(name);
  75. if (error < 0 && error != -EOPNOTSUPP)
  76. break;
  77. if (error == 1) {
  78. error = 0;
  79. continue; /* Discard */
  80. }
  81. retry:
  82. size = ovl_do_getxattr(oldpath, name, value, value_size);
  83. if (size == -ERANGE)
  84. size = ovl_do_getxattr(oldpath, name, NULL, 0);
  85. if (size < 0) {
  86. error = size;
  87. break;
  88. }
  89. if (size > value_size) {
  90. void *new;
  91. new = kvmalloc(size, GFP_KERNEL);
  92. if (!new) {
  93. error = -ENOMEM;
  94. break;
  95. }
  96. kvfree(value);
  97. value = new;
  98. value_size = size;
  99. goto retry;
  100. }
  101. error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0);
  102. if (error) {
  103. if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
  104. break;
  105. /* Ignore failure to copy unknown xattrs */
  106. error = 0;
  107. }
  108. }
  109. kvfree(value);
  110. out:
  111. kvfree(buf);
  112. return error;
  113. }
  114. static int ovl_copy_fileattr(struct inode *inode, const struct path *old,
  115. const struct path *new)
  116. {
  117. struct fileattr oldfa = { .flags_valid = true };
  118. struct fileattr newfa = { .flags_valid = true };
  119. int err;
  120. err = ovl_real_fileattr_get(old, &oldfa);
  121. if (err) {
  122. /* Ntfs-3g returns -EINVAL for "no fileattr support" */
  123. if (err == -ENOTTY || err == -EINVAL)
  124. return 0;
  125. pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n",
  126. old->dentry, err);
  127. return err;
  128. }
  129. /*
  130. * We cannot set immutable and append-only flags on upper inode,
  131. * because we would not be able to link upper inode to upper dir
  132. * not set overlay private xattr on upper inode.
  133. * Store these flags in overlay.protattr xattr instead.
  134. */
  135. if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) {
  136. err = ovl_set_protattr(inode, new->dentry, &oldfa);
  137. if (err == -EPERM)
  138. pr_warn_once("copying fileattr: no xattr on upper\n");
  139. else if (err)
  140. return err;
  141. }
  142. /* Don't bother copying flags if none are set */
  143. if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK))
  144. return 0;
  145. err = ovl_real_fileattr_get(new, &newfa);
  146. if (err) {
  147. /*
  148. * Returning an error if upper doesn't support fileattr will
  149. * result in a regression, so revert to the old behavior.
  150. */
  151. if (err == -ENOTTY || err == -EINVAL) {
  152. pr_warn_once("copying fileattr: no support on upper\n");
  153. return 0;
  154. }
  155. pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n",
  156. new->dentry, err);
  157. return err;
  158. }
  159. BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL);
  160. newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
  161. newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
  162. BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
  163. newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
  164. newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
  165. return ovl_real_fileattr_set(new, &newfa);
  166. }
  167. static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry,
  168. struct file *new_file, loff_t len)
  169. {
  170. struct path datapath;
  171. struct file *old_file;
  172. loff_t old_pos = 0;
  173. loff_t new_pos = 0;
  174. loff_t cloned;
  175. loff_t data_pos = -1;
  176. loff_t hole_len;
  177. bool skip_hole = false;
  178. int error = 0;
  179. ovl_path_lowerdata(dentry, &datapath);
  180. if (WARN_ON(datapath.dentry == NULL))
  181. return -EIO;
  182. old_file = ovl_path_open(&datapath, O_LARGEFILE | O_RDONLY);
  183. if (IS_ERR(old_file))
  184. return PTR_ERR(old_file);
  185. /* Try to use clone_file_range to clone up within the same fs */
  186. cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
  187. if (cloned == len)
  188. goto out_fput;
  189. /* Couldn't clone, so now we try to copy the data */
  190. /* Check if lower fs supports seek operation */
  191. if (old_file->f_mode & FMODE_LSEEK)
  192. skip_hole = true;
  193. while (len) {
  194. size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  195. long bytes;
  196. if (len < this_len)
  197. this_len = len;
  198. if (signal_pending_state(TASK_KILLABLE, current)) {
  199. error = -EINTR;
  200. break;
  201. }
  202. /*
  203. * Fill zero for hole will cost unnecessary disk space
  204. * and meanwhile slow down the copy-up speed, so we do
  205. * an optimization for hole during copy-up, it relies
  206. * on SEEK_DATA implementation in lower fs so if lower
  207. * fs does not support it, copy-up will behave as before.
  208. *
  209. * Detail logic of hole detection as below:
  210. * When we detect next data position is larger than current
  211. * position we will skip that hole, otherwise we copy
  212. * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
  213. * it may not recognize all kind of holes and sometimes
  214. * only skips partial of hole area. However, it will be
  215. * enough for most of the use cases.
  216. */
  217. if (skip_hole && data_pos < old_pos) {
  218. data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
  219. if (data_pos > old_pos) {
  220. hole_len = data_pos - old_pos;
  221. len -= hole_len;
  222. old_pos = new_pos = data_pos;
  223. continue;
  224. } else if (data_pos == -ENXIO) {
  225. break;
  226. } else if (data_pos < 0) {
  227. skip_hole = false;
  228. }
  229. }
  230. bytes = do_splice_direct(old_file, &old_pos,
  231. new_file, &new_pos,
  232. this_len, SPLICE_F_MOVE);
  233. if (bytes <= 0) {
  234. error = bytes;
  235. break;
  236. }
  237. WARN_ON(old_pos != new_pos);
  238. len -= bytes;
  239. }
  240. if (!error && ovl_should_sync(ofs))
  241. error = vfs_fsync(new_file, 0);
  242. out_fput:
  243. fput(old_file);
  244. return error;
  245. }
  246. static int ovl_set_size(struct ovl_fs *ofs,
  247. struct dentry *upperdentry, struct kstat *stat)
  248. {
  249. struct iattr attr = {
  250. .ia_valid = ATTR_SIZE,
  251. .ia_size = stat->size,
  252. };
  253. return ovl_do_notify_change(ofs, upperdentry, &attr);
  254. }
  255. static int ovl_set_timestamps(struct ovl_fs *ofs, struct dentry *upperdentry,
  256. struct kstat *stat)
  257. {
  258. struct iattr attr = {
  259. .ia_valid =
  260. ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_CTIME,
  261. .ia_atime = stat->atime,
  262. .ia_mtime = stat->mtime,
  263. };
  264. return ovl_do_notify_change(ofs, upperdentry, &attr);
  265. }
  266. int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upperdentry,
  267. struct kstat *stat)
  268. {
  269. int err = 0;
  270. if (!S_ISLNK(stat->mode)) {
  271. struct iattr attr = {
  272. .ia_valid = ATTR_MODE,
  273. .ia_mode = stat->mode,
  274. };
  275. err = ovl_do_notify_change(ofs, upperdentry, &attr);
  276. }
  277. if (!err) {
  278. struct iattr attr = {
  279. .ia_valid = ATTR_UID | ATTR_GID,
  280. .ia_vfsuid = VFSUIDT_INIT(stat->uid),
  281. .ia_vfsgid = VFSGIDT_INIT(stat->gid),
  282. };
  283. err = ovl_do_notify_change(ofs, upperdentry, &attr);
  284. }
  285. if (!err)
  286. ovl_set_timestamps(ofs, upperdentry, stat);
  287. return err;
  288. }
  289. struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
  290. bool is_upper)
  291. {
  292. struct ovl_fh *fh;
  293. int fh_type, dwords;
  294. int buflen = MAX_HANDLE_SZ;
  295. uuid_t *uuid = &real->d_sb->s_uuid;
  296. int err;
  297. /* Make sure the real fid stays 32bit aligned */
  298. BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
  299. BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
  300. fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
  301. if (!fh)
  302. return ERR_PTR(-ENOMEM);
  303. /*
  304. * We encode a non-connectable file handle for non-dir, because we
  305. * only need to find the lower inode number and we don't want to pay
  306. * the price or reconnecting the dentry.
  307. */
  308. dwords = buflen >> 2;
  309. fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
  310. buflen = (dwords << 2);
  311. err = -EIO;
  312. if (WARN_ON(fh_type < 0) ||
  313. WARN_ON(buflen > MAX_HANDLE_SZ) ||
  314. WARN_ON(fh_type == FILEID_INVALID))
  315. goto out_err;
  316. fh->fb.version = OVL_FH_VERSION;
  317. fh->fb.magic = OVL_FH_MAGIC;
  318. fh->fb.type = fh_type;
  319. fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
  320. /*
  321. * When we will want to decode an overlay dentry from this handle
  322. * and all layers are on the same fs, if we get a disconncted real
  323. * dentry when we decode fid, the only way to tell if we should assign
  324. * it to upperdentry or to lowerstack is by checking this flag.
  325. */
  326. if (is_upper)
  327. fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
  328. fh->fb.len = sizeof(fh->fb) + buflen;
  329. if (ofs->config.uuid)
  330. fh->fb.uuid = *uuid;
  331. return fh;
  332. out_err:
  333. kfree(fh);
  334. return ERR_PTR(err);
  335. }
  336. int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
  337. struct dentry *upper)
  338. {
  339. const struct ovl_fh *fh = NULL;
  340. int err;
  341. /*
  342. * When lower layer doesn't support export operations store a 'null' fh,
  343. * so we can use the overlay.origin xattr to distignuish between a copy
  344. * up and a pure upper inode.
  345. */
  346. if (ovl_can_decode_fh(lower->d_sb)) {
  347. fh = ovl_encode_real_fh(ofs, lower, false);
  348. if (IS_ERR(fh))
  349. return PTR_ERR(fh);
  350. }
  351. /*
  352. * Do not fail when upper doesn't support xattrs.
  353. */
  354. err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
  355. fh ? fh->fb.len : 0, 0);
  356. kfree(fh);
  357. /* Ignore -EPERM from setting "user.*" on symlink/special */
  358. return err == -EPERM ? 0 : err;
  359. }
  360. /* Store file handle of @upper dir in @index dir entry */
  361. static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
  362. struct dentry *index)
  363. {
  364. const struct ovl_fh *fh;
  365. int err;
  366. fh = ovl_encode_real_fh(ofs, upper, true);
  367. if (IS_ERR(fh))
  368. return PTR_ERR(fh);
  369. err = ovl_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
  370. kfree(fh);
  371. return err;
  372. }
  373. /*
  374. * Create and install index entry.
  375. *
  376. * Caller must hold i_mutex on indexdir.
  377. */
  378. static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
  379. struct dentry *upper)
  380. {
  381. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  382. struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
  383. struct inode *dir = d_inode(indexdir);
  384. struct dentry *index = NULL;
  385. struct dentry *temp = NULL;
  386. struct qstr name = { };
  387. int err;
  388. /*
  389. * For now this is only used for creating index entry for directories,
  390. * because non-dir are copied up directly to index and then hardlinked
  391. * to upper dir.
  392. *
  393. * TODO: implement create index for non-dir, so we can call it when
  394. * encoding file handle for non-dir in case index does not exist.
  395. */
  396. if (WARN_ON(!d_is_dir(dentry)))
  397. return -EIO;
  398. /* Directory not expected to be indexed before copy up */
  399. if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
  400. return -EIO;
  401. err = ovl_get_index_name(ofs, origin, &name);
  402. if (err)
  403. return err;
  404. temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0));
  405. err = PTR_ERR(temp);
  406. if (IS_ERR(temp))
  407. goto free_name;
  408. err = ovl_set_upper_fh(ofs, upper, temp);
  409. if (err)
  410. goto out;
  411. index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
  412. if (IS_ERR(index)) {
  413. err = PTR_ERR(index);
  414. } else {
  415. err = ovl_do_rename(ofs, dir, temp, dir, index, 0);
  416. dput(index);
  417. }
  418. out:
  419. if (err)
  420. ovl_cleanup(ofs, dir, temp);
  421. dput(temp);
  422. free_name:
  423. kfree(name.name);
  424. return err;
  425. }
  426. struct ovl_copy_up_ctx {
  427. struct dentry *parent;
  428. struct dentry *dentry;
  429. struct path lowerpath;
  430. struct kstat stat;
  431. struct kstat pstat;
  432. const char *link;
  433. struct dentry *destdir;
  434. struct qstr destname;
  435. struct dentry *workdir;
  436. bool origin;
  437. bool indexed;
  438. bool metacopy;
  439. };
  440. static int ovl_link_up(struct ovl_copy_up_ctx *c)
  441. {
  442. int err;
  443. struct dentry *upper;
  444. struct dentry *upperdir = ovl_dentry_upper(c->parent);
  445. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  446. struct inode *udir = d_inode(upperdir);
  447. /* Mark parent "impure" because it may now contain non-pure upper */
  448. err = ovl_set_impure(c->parent, upperdir);
  449. if (err)
  450. return err;
  451. err = ovl_set_nlink_lower(c->dentry);
  452. if (err)
  453. return err;
  454. inode_lock_nested(udir, I_MUTEX_PARENT);
  455. upper = ovl_lookup_upper(ofs, c->dentry->d_name.name, upperdir,
  456. c->dentry->d_name.len);
  457. err = PTR_ERR(upper);
  458. if (!IS_ERR(upper)) {
  459. err = ovl_do_link(ofs, ovl_dentry_upper(c->dentry), udir, upper);
  460. dput(upper);
  461. if (!err) {
  462. /* Restore timestamps on parent (best effort) */
  463. ovl_set_timestamps(ofs, upperdir, &c->pstat);
  464. ovl_dentry_set_upper_alias(c->dentry);
  465. ovl_dentry_update_reval(c->dentry, upper);
  466. }
  467. }
  468. inode_unlock(udir);
  469. if (err)
  470. return err;
  471. err = ovl_set_nlink_upper(c->dentry);
  472. return err;
  473. }
  474. static int ovl_copy_up_data(struct ovl_copy_up_ctx *c, const struct path *temp)
  475. {
  476. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  477. struct file *new_file;
  478. int err;
  479. if (!S_ISREG(c->stat.mode) || c->metacopy || !c->stat.size)
  480. return 0;
  481. new_file = ovl_path_open(temp, O_LARGEFILE | O_WRONLY);
  482. if (IS_ERR(new_file))
  483. return PTR_ERR(new_file);
  484. err = ovl_copy_up_file(ofs, c->dentry, new_file, c->stat.size);
  485. fput(new_file);
  486. return err;
  487. }
  488. static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp)
  489. {
  490. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  491. struct inode *inode = d_inode(c->dentry);
  492. struct path upperpath = { .mnt = ovl_upper_mnt(ofs), .dentry = temp };
  493. int err;
  494. err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp);
  495. if (err)
  496. return err;
  497. if (inode->i_flags & OVL_COPY_I_FLAGS_MASK &&
  498. (S_ISREG(c->stat.mode) || S_ISDIR(c->stat.mode))) {
  499. /*
  500. * Copy the fileattr inode flags that are the source of already
  501. * copied i_flags
  502. */
  503. err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath);
  504. if (err)
  505. return err;
  506. }
  507. /*
  508. * Store identifier of lower inode in upper inode xattr to
  509. * allow lookup of the copy up origin inode.
  510. *
  511. * Don't set origin when we are breaking the association with a lower
  512. * hard link.
  513. */
  514. if (c->origin) {
  515. err = ovl_set_origin(ofs, c->lowerpath.dentry, temp);
  516. if (err)
  517. return err;
  518. }
  519. if (c->metacopy) {
  520. err = ovl_check_setxattr(ofs, temp, OVL_XATTR_METACOPY,
  521. NULL, 0, -EOPNOTSUPP);
  522. if (err)
  523. return err;
  524. }
  525. inode_lock(temp->d_inode);
  526. if (S_ISREG(c->stat.mode))
  527. err = ovl_set_size(ofs, temp, &c->stat);
  528. if (!err)
  529. err = ovl_set_attr(ofs, temp, &c->stat);
  530. inode_unlock(temp->d_inode);
  531. return err;
  532. }
  533. struct ovl_cu_creds {
  534. const struct cred *old;
  535. struct cred *new;
  536. };
  537. static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
  538. {
  539. int err;
  540. cc->old = cc->new = NULL;
  541. err = security_inode_copy_up(dentry, &cc->new);
  542. if (err < 0)
  543. return err;
  544. if (cc->new)
  545. cc->old = override_creds(cc->new);
  546. return 0;
  547. }
  548. static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
  549. {
  550. if (cc->new) {
  551. revert_creds(cc->old);
  552. put_cred(cc->new);
  553. }
  554. }
  555. /*
  556. * Copyup using workdir to prepare temp file. Used when copying up directories,
  557. * special files or when upper fs doesn't support O_TMPFILE.
  558. */
  559. static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
  560. {
  561. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  562. struct inode *inode;
  563. struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
  564. struct path path = { .mnt = ovl_upper_mnt(ofs) };
  565. struct dentry *temp, *upper;
  566. struct ovl_cu_creds cc;
  567. int err;
  568. struct ovl_cattr cattr = {
  569. /* Can't properly set mode on creation because of the umask */
  570. .mode = c->stat.mode & S_IFMT,
  571. .rdev = c->stat.rdev,
  572. .link = c->link
  573. };
  574. /* workdir and destdir could be the same when copying up to indexdir */
  575. err = -EIO;
  576. if (lock_rename(c->workdir, c->destdir) != NULL)
  577. goto unlock;
  578. err = ovl_prep_cu_creds(c->dentry, &cc);
  579. if (err)
  580. goto unlock;
  581. temp = ovl_create_temp(ofs, c->workdir, &cattr);
  582. ovl_revert_cu_creds(&cc);
  583. err = PTR_ERR(temp);
  584. if (IS_ERR(temp))
  585. goto unlock;
  586. /*
  587. * Copy up data first and then xattrs. Writing data after
  588. * xattrs will remove security.capability xattr automatically.
  589. */
  590. path.dentry = temp;
  591. err = ovl_copy_up_data(c, &path);
  592. if (err)
  593. goto cleanup;
  594. err = ovl_copy_up_metadata(c, temp);
  595. if (err)
  596. goto cleanup;
  597. if (S_ISDIR(c->stat.mode) && c->indexed) {
  598. err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
  599. if (err)
  600. goto cleanup;
  601. }
  602. upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
  603. c->destname.len);
  604. err = PTR_ERR(upper);
  605. if (IS_ERR(upper))
  606. goto cleanup;
  607. err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0);
  608. dput(upper);
  609. if (err)
  610. goto cleanup;
  611. if (!c->metacopy)
  612. ovl_set_upperdata(d_inode(c->dentry));
  613. inode = d_inode(c->dentry);
  614. ovl_inode_update(inode, temp);
  615. if (S_ISDIR(inode->i_mode))
  616. ovl_set_flag(OVL_WHITEOUTS, inode);
  617. unlock:
  618. unlock_rename(c->workdir, c->destdir);
  619. return err;
  620. cleanup:
  621. ovl_cleanup(ofs, wdir, temp);
  622. dput(temp);
  623. goto unlock;
  624. }
  625. /* Copyup using O_TMPFILE which does not require cross dir locking */
  626. static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
  627. {
  628. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  629. struct inode *udir = d_inode(c->destdir);
  630. struct dentry *temp, *upper;
  631. struct file *tmpfile;
  632. struct ovl_cu_creds cc;
  633. int err;
  634. err = ovl_prep_cu_creds(c->dentry, &cc);
  635. if (err)
  636. return err;
  637. tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode);
  638. ovl_revert_cu_creds(&cc);
  639. if (IS_ERR(tmpfile))
  640. return PTR_ERR(tmpfile);
  641. temp = tmpfile->f_path.dentry;
  642. if (!c->metacopy && c->stat.size) {
  643. err = ovl_copy_up_file(ofs, c->dentry, tmpfile, c->stat.size);
  644. if (err)
  645. goto out_fput;
  646. }
  647. err = ovl_copy_up_metadata(c, temp);
  648. if (err)
  649. goto out_fput;
  650. inode_lock_nested(udir, I_MUTEX_PARENT);
  651. upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
  652. c->destname.len);
  653. err = PTR_ERR(upper);
  654. if (!IS_ERR(upper)) {
  655. err = ovl_do_link(ofs, temp, udir, upper);
  656. dput(upper);
  657. }
  658. inode_unlock(udir);
  659. if (err)
  660. goto out_fput;
  661. if (!c->metacopy)
  662. ovl_set_upperdata(d_inode(c->dentry));
  663. ovl_inode_update(d_inode(c->dentry), dget(temp));
  664. out_fput:
  665. fput(tmpfile);
  666. return err;
  667. }
  668. /*
  669. * Copy up a single dentry
  670. *
  671. * All renames start with copy up of source if necessary. The actual
  672. * rename will only proceed once the copy up was successful. Copy up uses
  673. * upper parent i_mutex for exclusion. Since rename can change d_parent it
  674. * is possible that the copy up will lock the old parent. At that point
  675. * the file will have already been copied up anyway.
  676. */
  677. static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
  678. {
  679. int err;
  680. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  681. bool to_index = false;
  682. /*
  683. * Indexed non-dir is copied up directly to the index entry and then
  684. * hardlinked to upper dir. Indexed dir is copied up to indexdir,
  685. * then index entry is created and then copied up dir installed.
  686. * Copying dir up to indexdir instead of workdir simplifies locking.
  687. */
  688. if (ovl_need_index(c->dentry)) {
  689. c->indexed = true;
  690. if (S_ISDIR(c->stat.mode))
  691. c->workdir = ovl_indexdir(c->dentry->d_sb);
  692. else
  693. to_index = true;
  694. }
  695. if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
  696. c->origin = true;
  697. if (to_index) {
  698. c->destdir = ovl_indexdir(c->dentry->d_sb);
  699. err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname);
  700. if (err)
  701. return err;
  702. } else if (WARN_ON(!c->parent)) {
  703. /* Disconnected dentry must be copied up to index dir */
  704. return -EIO;
  705. } else {
  706. /*
  707. * Mark parent "impure" because it may now contain non-pure
  708. * upper
  709. */
  710. err = ovl_set_impure(c->parent, c->destdir);
  711. if (err)
  712. return err;
  713. }
  714. /* Should we copyup with O_TMPFILE or with workdir? */
  715. if (S_ISREG(c->stat.mode) && ofs->tmpfile)
  716. err = ovl_copy_up_tmpfile(c);
  717. else
  718. err = ovl_copy_up_workdir(c);
  719. if (err)
  720. goto out;
  721. if (c->indexed)
  722. ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
  723. if (to_index) {
  724. /* Initialize nlink for copy up of disconnected dentry */
  725. err = ovl_set_nlink_upper(c->dentry);
  726. } else {
  727. struct inode *udir = d_inode(c->destdir);
  728. /* Restore timestamps on parent (best effort) */
  729. inode_lock(udir);
  730. ovl_set_timestamps(ofs, c->destdir, &c->pstat);
  731. inode_unlock(udir);
  732. ovl_dentry_set_upper_alias(c->dentry);
  733. ovl_dentry_update_reval(c->dentry, ovl_dentry_upper(c->dentry));
  734. }
  735. out:
  736. if (to_index)
  737. kfree(c->destname.name);
  738. return err;
  739. }
  740. static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
  741. int flags)
  742. {
  743. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  744. if (!ofs->config.metacopy)
  745. return false;
  746. if (!S_ISREG(mode))
  747. return false;
  748. if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
  749. return false;
  750. return true;
  751. }
  752. static ssize_t ovl_getxattr_value(const struct path *path, char *name, char **value)
  753. {
  754. ssize_t res;
  755. char *buf;
  756. res = ovl_do_getxattr(path, name, NULL, 0);
  757. if (res == -ENODATA || res == -EOPNOTSUPP)
  758. res = 0;
  759. if (res > 0) {
  760. buf = kzalloc(res, GFP_KERNEL);
  761. if (!buf)
  762. return -ENOMEM;
  763. res = ovl_do_getxattr(path, name, buf, res);
  764. if (res < 0)
  765. kfree(buf);
  766. else
  767. *value = buf;
  768. }
  769. return res;
  770. }
  771. /* Copy up data of an inode which was copied up metadata only in the past. */
  772. static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
  773. {
  774. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  775. struct path upperpath;
  776. int err;
  777. char *capability = NULL;
  778. ssize_t cap_size;
  779. ovl_path_upper(c->dentry, &upperpath);
  780. if (WARN_ON(upperpath.dentry == NULL))
  781. return -EIO;
  782. if (c->stat.size) {
  783. err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS,
  784. &capability);
  785. if (cap_size < 0)
  786. goto out;
  787. }
  788. err = ovl_copy_up_data(c, &upperpath);
  789. if (err)
  790. goto out_free;
  791. /*
  792. * Writing to upper file will clear security.capability xattr. We
  793. * don't want that to happen for normal copy-up operation.
  794. */
  795. if (capability) {
  796. err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS,
  797. capability, cap_size, 0);
  798. if (err)
  799. goto out_free;
  800. }
  801. err = ovl_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
  802. if (err)
  803. goto out_free;
  804. ovl_set_upperdata(d_inode(c->dentry));
  805. out_free:
  806. kfree(capability);
  807. out:
  808. return err;
  809. }
  810. static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  811. int flags)
  812. {
  813. int err;
  814. DEFINE_DELAYED_CALL(done);
  815. struct path parentpath;
  816. struct ovl_copy_up_ctx ctx = {
  817. .parent = parent,
  818. .dentry = dentry,
  819. .workdir = ovl_workdir(dentry),
  820. };
  821. if (WARN_ON(!ctx.workdir))
  822. return -EROFS;
  823. ovl_path_lower(dentry, &ctx.lowerpath);
  824. err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
  825. STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  826. if (err)
  827. return err;
  828. if (!kuid_has_mapping(current_user_ns(), ctx.stat.uid) ||
  829. !kgid_has_mapping(current_user_ns(), ctx.stat.gid))
  830. return -EOVERFLOW;
  831. ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
  832. if (parent) {
  833. ovl_path_upper(parent, &parentpath);
  834. ctx.destdir = parentpath.dentry;
  835. ctx.destname = dentry->d_name;
  836. err = vfs_getattr(&parentpath, &ctx.pstat,
  837. STATX_ATIME | STATX_MTIME,
  838. AT_STATX_SYNC_AS_STAT);
  839. if (err)
  840. return err;
  841. }
  842. /* maybe truncate regular file. this has no effect on dirs */
  843. if (flags & O_TRUNC)
  844. ctx.stat.size = 0;
  845. if (S_ISLNK(ctx.stat.mode)) {
  846. ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
  847. if (IS_ERR(ctx.link))
  848. return PTR_ERR(ctx.link);
  849. }
  850. err = ovl_copy_up_start(dentry, flags);
  851. /* err < 0: interrupted, err > 0: raced with another copy-up */
  852. if (unlikely(err)) {
  853. if (err > 0)
  854. err = 0;
  855. } else {
  856. if (!ovl_dentry_upper(dentry))
  857. err = ovl_do_copy_up(&ctx);
  858. if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
  859. err = ovl_link_up(&ctx);
  860. if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
  861. err = ovl_copy_up_meta_inode_data(&ctx);
  862. ovl_copy_up_end(dentry);
  863. }
  864. do_delayed_call(&done);
  865. return err;
  866. }
  867. static int ovl_copy_up_flags(struct dentry *dentry, int flags)
  868. {
  869. int err = 0;
  870. const struct cred *old_cred;
  871. bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
  872. /*
  873. * With NFS export, copy up can get called for a disconnected non-dir.
  874. * In this case, we will copy up lower inode to index dir without
  875. * linking it to upper dir.
  876. */
  877. if (WARN_ON(disconnected && d_is_dir(dentry)))
  878. return -EIO;
  879. old_cred = ovl_override_creds(dentry->d_sb);
  880. while (!err) {
  881. struct dentry *next;
  882. struct dentry *parent = NULL;
  883. if (ovl_already_copied_up(dentry, flags))
  884. break;
  885. next = dget(dentry);
  886. /* find the topmost dentry not yet copied up */
  887. for (; !disconnected;) {
  888. parent = dget_parent(next);
  889. if (ovl_dentry_upper(parent))
  890. break;
  891. dput(next);
  892. next = parent;
  893. }
  894. err = ovl_copy_up_one(parent, next, flags);
  895. dput(parent);
  896. dput(next);
  897. }
  898. ovl_revert_creds(dentry->d_sb, old_cred);
  899. return err;
  900. }
  901. static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
  902. {
  903. /* Copy up of disconnected dentry does not set upper alias */
  904. if (ovl_already_copied_up(dentry, flags))
  905. return false;
  906. if (special_file(d_inode(dentry)->i_mode))
  907. return false;
  908. if (!ovl_open_flags_need_copy_up(flags))
  909. return false;
  910. return true;
  911. }
  912. int ovl_maybe_copy_up(struct dentry *dentry, int flags)
  913. {
  914. int err = 0;
  915. if (ovl_open_need_copy_up(dentry, flags)) {
  916. err = ovl_want_write(dentry);
  917. if (!err) {
  918. err = ovl_copy_up_flags(dentry, flags);
  919. ovl_drop_write(dentry);
  920. }
  921. }
  922. return err;
  923. }
  924. int ovl_copy_up_with_data(struct dentry *dentry)
  925. {
  926. return ovl_copy_up_flags(dentry, O_WRONLY);
  927. }
  928. int ovl_copy_up(struct dentry *dentry)
  929. {
  930. return ovl_copy_up_flags(dentry, 0);
  931. }