copy_up.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. static int ovl_copy_acl(struct ovl_fs *ofs, const struct path *path,
  41. struct dentry *dentry, const char *acl_name)
  42. {
  43. int err;
  44. struct posix_acl *clone, *real_acl = NULL;
  45. real_acl = ovl_get_acl_path(path, acl_name, false);
  46. if (!real_acl)
  47. return 0;
  48. if (IS_ERR(real_acl)) {
  49. err = PTR_ERR(real_acl);
  50. if (err == -ENODATA || err == -EOPNOTSUPP)
  51. return 0;
  52. return err;
  53. }
  54. clone = posix_acl_clone(real_acl, GFP_KERNEL);
  55. posix_acl_release(real_acl); /* release original acl */
  56. if (!clone)
  57. return -ENOMEM;
  58. err = ovl_do_set_acl(ofs, dentry, acl_name, clone);
  59. /* release cloned acl */
  60. posix_acl_release(clone);
  61. return err;
  62. }
  63. int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct dentry *new)
  64. {
  65. struct dentry *old = oldpath->dentry;
  66. ssize_t list_size, size, value_size = 0;
  67. char *buf, *name, *value = NULL;
  68. int error = 0;
  69. size_t slen;
  70. if (!old->d_inode->i_op->listxattr || !new->d_inode->i_op->listxattr)
  71. return 0;
  72. list_size = vfs_listxattr(old, NULL, 0);
  73. if (list_size <= 0) {
  74. if (list_size == -EOPNOTSUPP)
  75. return 0;
  76. return list_size;
  77. }
  78. buf = kvzalloc(list_size, GFP_KERNEL);
  79. if (!buf)
  80. return -ENOMEM;
  81. list_size = vfs_listxattr(old, buf, list_size);
  82. if (list_size <= 0) {
  83. error = list_size;
  84. goto out;
  85. }
  86. for (name = buf; list_size; name += slen) {
  87. slen = strnlen(name, list_size) + 1;
  88. /* underlying fs providing us with an broken xattr list? */
  89. if (WARN_ON(slen > list_size)) {
  90. error = -EIO;
  91. break;
  92. }
  93. list_size -= slen;
  94. if (ovl_is_private_xattr(sb, name))
  95. continue;
  96. error = security_inode_copy_up_xattr(old, name);
  97. if (error == -ECANCELED) {
  98. error = 0;
  99. continue; /* Discard */
  100. }
  101. if (error < 0 && error != -EOPNOTSUPP)
  102. break;
  103. if (is_posix_acl_xattr(name)) {
  104. error = ovl_copy_acl(OVL_FS(sb), oldpath, new, name);
  105. if (!error)
  106. continue;
  107. /* POSIX ACLs must be copied. */
  108. break;
  109. }
  110. retry:
  111. size = ovl_do_getxattr(oldpath, name, value, value_size);
  112. if (size == -ERANGE)
  113. size = ovl_do_getxattr(oldpath, name, NULL, 0);
  114. if (size < 0) {
  115. error = size;
  116. break;
  117. }
  118. if (size > value_size) {
  119. void *new;
  120. new = kvmalloc(size, GFP_KERNEL);
  121. if (!new) {
  122. error = -ENOMEM;
  123. break;
  124. }
  125. kvfree(value);
  126. value = new;
  127. value_size = size;
  128. goto retry;
  129. }
  130. error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0);
  131. if (error) {
  132. if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
  133. break;
  134. /* Ignore failure to copy unknown xattrs */
  135. error = 0;
  136. }
  137. }
  138. kvfree(value);
  139. out:
  140. kvfree(buf);
  141. return error;
  142. }
  143. static int ovl_copy_fileattr(struct inode *inode, const struct path *old,
  144. const struct path *new)
  145. {
  146. struct fileattr oldfa = { .flags_valid = true };
  147. struct fileattr newfa = { .flags_valid = true };
  148. int err;
  149. err = ovl_real_fileattr_get(old, &oldfa);
  150. if (err) {
  151. /* Ntfs-3g returns -EINVAL for "no fileattr support" */
  152. if (err == -ENOTTY || err == -EINVAL)
  153. return 0;
  154. pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n",
  155. old->dentry, err);
  156. return err;
  157. }
  158. /*
  159. * We cannot set immutable and append-only flags on upper inode,
  160. * because we would not be able to link upper inode to upper dir
  161. * not set overlay private xattr on upper inode.
  162. * Store these flags in overlay.protattr xattr instead.
  163. */
  164. if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) {
  165. err = ovl_set_protattr(inode, new->dentry, &oldfa);
  166. if (err == -EPERM)
  167. pr_warn_once("copying fileattr: no xattr on upper\n");
  168. else if (err)
  169. return err;
  170. }
  171. /* Don't bother copying flags if none are set */
  172. if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK))
  173. return 0;
  174. err = ovl_real_fileattr_get(new, &newfa);
  175. if (err) {
  176. /*
  177. * Returning an error if upper doesn't support fileattr will
  178. * result in a regression, so revert to the old behavior.
  179. */
  180. if (err == -ENOTTY || err == -EINVAL) {
  181. pr_warn_once("copying fileattr: no support on upper\n");
  182. return 0;
  183. }
  184. pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n",
  185. new->dentry, err);
  186. return err;
  187. }
  188. BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL);
  189. newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
  190. newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
  191. BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
  192. newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
  193. newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
  194. return ovl_real_fileattr_set(new, &newfa);
  195. }
  196. static int ovl_verify_area(loff_t pos, loff_t pos2, loff_t len, loff_t totlen)
  197. {
  198. loff_t tmp;
  199. if (pos != pos2)
  200. return -EIO;
  201. if (pos < 0 || len < 0 || totlen < 0)
  202. return -EIO;
  203. if (check_add_overflow(pos, len, &tmp))
  204. return -EIO;
  205. return 0;
  206. }
  207. static int ovl_sync_file(struct path *path)
  208. {
  209. struct file *new_file;
  210. int err;
  211. new_file = ovl_path_open(path, O_LARGEFILE | O_RDONLY);
  212. if (IS_ERR(new_file))
  213. return PTR_ERR(new_file);
  214. err = vfs_fsync(new_file, 0);
  215. fput(new_file);
  216. return err;
  217. }
  218. static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry,
  219. struct file *new_file, loff_t len,
  220. bool datasync)
  221. {
  222. struct path datapath;
  223. struct file *old_file;
  224. loff_t old_pos = 0;
  225. loff_t new_pos = 0;
  226. loff_t cloned;
  227. loff_t data_pos = -1;
  228. loff_t hole_len;
  229. bool skip_hole = false;
  230. int error = 0;
  231. ovl_path_lowerdata(dentry, &datapath);
  232. if (WARN_ON_ONCE(datapath.dentry == NULL) ||
  233. WARN_ON_ONCE(len < 0))
  234. return -EIO;
  235. old_file = ovl_path_open(&datapath, O_LARGEFILE | O_RDONLY);
  236. if (IS_ERR(old_file))
  237. return PTR_ERR(old_file);
  238. /* Try to use clone_file_range to clone up within the same fs */
  239. cloned = vfs_clone_file_range(old_file, 0, new_file, 0, len, 0);
  240. if (cloned == len)
  241. goto out_fput;
  242. /* Couldn't clone, so now we try to copy the data */
  243. error = rw_verify_area(READ, old_file, &old_pos, len);
  244. if (!error)
  245. error = rw_verify_area(WRITE, new_file, &new_pos, len);
  246. if (error)
  247. goto out_fput;
  248. /* Check if lower fs supports seek operation */
  249. if (old_file->f_mode & FMODE_LSEEK)
  250. skip_hole = true;
  251. while (len) {
  252. size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  253. ssize_t bytes;
  254. if (len < this_len)
  255. this_len = len;
  256. if (signal_pending_state(TASK_KILLABLE, current)) {
  257. error = -EINTR;
  258. break;
  259. }
  260. /*
  261. * Fill zero for hole will cost unnecessary disk space
  262. * and meanwhile slow down the copy-up speed, so we do
  263. * an optimization for hole during copy-up, it relies
  264. * on SEEK_DATA implementation in lower fs so if lower
  265. * fs does not support it, copy-up will behave as before.
  266. *
  267. * Detail logic of hole detection as below:
  268. * When we detect next data position is larger than current
  269. * position we will skip that hole, otherwise we copy
  270. * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
  271. * it may not recognize all kind of holes and sometimes
  272. * only skips partial of hole area. However, it will be
  273. * enough for most of the use cases.
  274. *
  275. * We do not hold upper sb_writers throughout the loop to avert
  276. * lockdep warning with llseek of lower file in nested overlay:
  277. * - upper sb_writers
  278. * -- lower ovl_inode_lock (ovl_llseek)
  279. */
  280. if (skip_hole && data_pos < old_pos) {
  281. data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
  282. if (data_pos > old_pos) {
  283. hole_len = data_pos - old_pos;
  284. len -= hole_len;
  285. old_pos = new_pos = data_pos;
  286. continue;
  287. } else if (data_pos == -ENXIO) {
  288. break;
  289. } else if (data_pos < 0) {
  290. skip_hole = false;
  291. }
  292. }
  293. error = ovl_verify_area(old_pos, new_pos, this_len, len);
  294. if (error)
  295. break;
  296. bytes = do_splice_direct(old_file, &old_pos,
  297. new_file, &new_pos,
  298. this_len, SPLICE_F_MOVE);
  299. if (bytes <= 0) {
  300. error = bytes;
  301. break;
  302. }
  303. WARN_ON(old_pos != new_pos);
  304. len -= bytes;
  305. }
  306. /* call fsync once, either now or later along with metadata */
  307. if (!error && ovl_should_sync(ofs) && datasync)
  308. error = vfs_fsync(new_file, 0);
  309. out_fput:
  310. fput(old_file);
  311. return error;
  312. }
  313. static int ovl_set_size(struct ovl_fs *ofs,
  314. struct dentry *upperdentry, struct kstat *stat)
  315. {
  316. struct iattr attr = {
  317. .ia_valid = ATTR_SIZE,
  318. .ia_size = stat->size,
  319. };
  320. return ovl_do_notify_change(ofs, upperdentry, &attr);
  321. }
  322. static int ovl_set_timestamps(struct ovl_fs *ofs, struct dentry *upperdentry,
  323. struct kstat *stat)
  324. {
  325. struct iattr attr = {
  326. .ia_valid =
  327. ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_CTIME,
  328. .ia_atime = stat->atime,
  329. .ia_mtime = stat->mtime,
  330. };
  331. return ovl_do_notify_change(ofs, upperdentry, &attr);
  332. }
  333. int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upperdentry,
  334. struct kstat *stat)
  335. {
  336. int err = 0;
  337. if (!S_ISLNK(stat->mode)) {
  338. struct iattr attr = {
  339. .ia_valid = ATTR_MODE,
  340. .ia_mode = stat->mode,
  341. };
  342. err = ovl_do_notify_change(ofs, upperdentry, &attr);
  343. }
  344. if (!err) {
  345. struct iattr attr = {
  346. .ia_valid = ATTR_UID | ATTR_GID,
  347. .ia_vfsuid = VFSUIDT_INIT(stat->uid),
  348. .ia_vfsgid = VFSGIDT_INIT(stat->gid),
  349. };
  350. err = ovl_do_notify_change(ofs, upperdentry, &attr);
  351. }
  352. if (!err)
  353. ovl_set_timestamps(ofs, upperdentry, stat);
  354. return err;
  355. }
  356. struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct inode *realinode,
  357. bool is_upper)
  358. {
  359. struct ovl_fh *fh;
  360. int fh_type, dwords;
  361. int buflen = MAX_HANDLE_SZ;
  362. uuid_t *uuid = &realinode->i_sb->s_uuid;
  363. int err;
  364. /* Make sure the real fid stays 32bit aligned */
  365. BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
  366. BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
  367. fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
  368. if (!fh)
  369. return ERR_PTR(-ENOMEM);
  370. /*
  371. * We encode a non-connectable file handle for non-dir, because we
  372. * only need to find the lower inode number and we don't want to pay
  373. * the price or reconnecting the dentry.
  374. */
  375. dwords = buflen >> 2;
  376. fh_type = exportfs_encode_inode_fh(realinode, (void *)fh->fb.fid,
  377. &dwords, NULL, 0);
  378. buflen = (dwords << 2);
  379. err = -EIO;
  380. if (fh_type < 0 || fh_type == FILEID_INVALID ||
  381. WARN_ON(buflen > MAX_HANDLE_SZ))
  382. goto out_err;
  383. fh->fb.version = OVL_FH_VERSION;
  384. fh->fb.magic = OVL_FH_MAGIC;
  385. fh->fb.type = fh_type;
  386. fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
  387. /*
  388. * When we will want to decode an overlay dentry from this handle
  389. * and all layers are on the same fs, if we get a disconncted real
  390. * dentry when we decode fid, the only way to tell if we should assign
  391. * it to upperdentry or to lowerstack is by checking this flag.
  392. */
  393. if (is_upper)
  394. fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
  395. fh->fb.len = sizeof(fh->fb) + buflen;
  396. if (ovl_origin_uuid(ofs))
  397. fh->fb.uuid = *uuid;
  398. return fh;
  399. out_err:
  400. kfree(fh);
  401. return ERR_PTR(err);
  402. }
  403. struct ovl_fh *ovl_get_origin_fh(struct ovl_fs *ofs, struct dentry *origin)
  404. {
  405. /*
  406. * When lower layer doesn't support export operations store a 'null' fh,
  407. * so we can use the overlay.origin xattr to distignuish between a copy
  408. * up and a pure upper inode.
  409. */
  410. if (!ovl_can_decode_fh(origin->d_sb))
  411. return NULL;
  412. return ovl_encode_real_fh(ofs, d_inode(origin), false);
  413. }
  414. int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh,
  415. struct dentry *upper)
  416. {
  417. int err;
  418. /*
  419. * Do not fail when upper doesn't support xattrs.
  420. */
  421. err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
  422. fh ? fh->fb.len : 0, 0);
  423. /* Ignore -EPERM from setting "user.*" on symlink/special */
  424. return err == -EPERM ? 0 : err;
  425. }
  426. /* Store file handle of @upper dir in @index dir entry */
  427. static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
  428. struct dentry *index)
  429. {
  430. const struct ovl_fh *fh;
  431. int err;
  432. fh = ovl_encode_real_fh(ofs, d_inode(upper), true);
  433. if (IS_ERR(fh))
  434. return PTR_ERR(fh);
  435. err = ovl_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
  436. kfree(fh);
  437. return err;
  438. }
  439. /*
  440. * Create and install index entry.
  441. *
  442. * Caller must hold i_mutex on indexdir.
  443. */
  444. static int ovl_create_index(struct dentry *dentry, const struct ovl_fh *fh,
  445. struct dentry *upper)
  446. {
  447. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  448. struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
  449. struct inode *dir = d_inode(indexdir);
  450. struct dentry *index = NULL;
  451. struct dentry *temp = NULL;
  452. struct qstr name = { };
  453. int err;
  454. /*
  455. * For now this is only used for creating index entry for directories,
  456. * because non-dir are copied up directly to index and then hardlinked
  457. * to upper dir.
  458. *
  459. * TODO: implement create index for non-dir, so we can call it when
  460. * encoding file handle for non-dir in case index does not exist.
  461. */
  462. if (WARN_ON(!d_is_dir(dentry)))
  463. return -EIO;
  464. /* Directory not expected to be indexed before copy up */
  465. if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
  466. return -EIO;
  467. err = ovl_get_index_name_fh(fh, &name);
  468. if (err)
  469. return err;
  470. temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0));
  471. err = PTR_ERR(temp);
  472. if (IS_ERR(temp))
  473. goto free_name;
  474. err = ovl_set_upper_fh(ofs, upper, temp);
  475. if (err)
  476. goto out;
  477. index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
  478. if (IS_ERR(index)) {
  479. err = PTR_ERR(index);
  480. } else {
  481. err = ovl_do_rename(ofs, dir, temp, dir, index, 0);
  482. dput(index);
  483. }
  484. out:
  485. if (err)
  486. ovl_cleanup(ofs, dir, temp);
  487. dput(temp);
  488. free_name:
  489. kfree(name.name);
  490. return err;
  491. }
  492. struct ovl_copy_up_ctx {
  493. struct dentry *parent;
  494. struct dentry *dentry;
  495. struct path lowerpath;
  496. struct kstat stat;
  497. struct kstat pstat;
  498. const char *link;
  499. struct dentry *destdir;
  500. struct qstr destname;
  501. struct dentry *workdir;
  502. const struct ovl_fh *origin_fh;
  503. bool origin;
  504. bool indexed;
  505. bool metacopy;
  506. bool metacopy_digest;
  507. bool metadata_fsync;
  508. };
  509. static int ovl_link_up(struct ovl_copy_up_ctx *c)
  510. {
  511. int err;
  512. struct dentry *upper;
  513. struct dentry *upperdir = ovl_dentry_upper(c->parent);
  514. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  515. struct inode *udir = d_inode(upperdir);
  516. ovl_start_write(c->dentry);
  517. /* Mark parent "impure" because it may now contain non-pure upper */
  518. err = ovl_set_impure(c->parent, upperdir);
  519. if (err)
  520. goto out;
  521. err = ovl_set_nlink_lower(c->dentry);
  522. if (err)
  523. goto out;
  524. inode_lock_nested(udir, I_MUTEX_PARENT);
  525. upper = ovl_lookup_upper(ofs, c->dentry->d_name.name, upperdir,
  526. c->dentry->d_name.len);
  527. err = PTR_ERR(upper);
  528. if (!IS_ERR(upper)) {
  529. err = ovl_do_link(ofs, ovl_dentry_upper(c->dentry), udir, upper);
  530. if (!err) {
  531. /* Restore timestamps on parent (best effort) */
  532. ovl_set_timestamps(ofs, upperdir, &c->pstat);
  533. ovl_dentry_set_upper_alias(c->dentry);
  534. ovl_dentry_update_reval(c->dentry, upper);
  535. }
  536. dput(upper);
  537. }
  538. inode_unlock(udir);
  539. if (err)
  540. goto out;
  541. err = ovl_set_nlink_upper(c->dentry);
  542. out:
  543. ovl_end_write(c->dentry);
  544. return err;
  545. }
  546. static int ovl_copy_up_data(struct ovl_copy_up_ctx *c, const struct path *temp)
  547. {
  548. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  549. struct file *new_file;
  550. int err;
  551. if (!S_ISREG(c->stat.mode) || c->metacopy || !c->stat.size)
  552. return 0;
  553. new_file = ovl_path_open(temp, O_LARGEFILE | O_WRONLY);
  554. if (IS_ERR(new_file))
  555. return PTR_ERR(new_file);
  556. err = ovl_copy_up_file(ofs, c->dentry, new_file, c->stat.size,
  557. !c->metadata_fsync);
  558. fput(new_file);
  559. return err;
  560. }
  561. static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp)
  562. {
  563. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  564. struct inode *inode = d_inode(c->dentry);
  565. struct path upperpath = { .mnt = ovl_upper_mnt(ofs), .dentry = temp };
  566. int err;
  567. err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp);
  568. if (err)
  569. return err;
  570. if (inode->i_flags & OVL_COPY_I_FLAGS_MASK &&
  571. (S_ISREG(c->stat.mode) || S_ISDIR(c->stat.mode))) {
  572. /*
  573. * Copy the fileattr inode flags that are the source of already
  574. * copied i_flags
  575. */
  576. err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath);
  577. if (err)
  578. return err;
  579. }
  580. /*
  581. * Store identifier of lower inode in upper inode xattr to
  582. * allow lookup of the copy up origin inode.
  583. *
  584. * Don't set origin when we are breaking the association with a lower
  585. * hard link.
  586. */
  587. if (c->origin) {
  588. err = ovl_set_origin_fh(ofs, c->origin_fh, temp);
  589. if (err)
  590. return err;
  591. }
  592. if (c->metacopy) {
  593. struct path lowerdatapath;
  594. struct ovl_metacopy metacopy_data = OVL_METACOPY_INIT;
  595. ovl_path_lowerdata(c->dentry, &lowerdatapath);
  596. if (WARN_ON_ONCE(lowerdatapath.dentry == NULL))
  597. return -EIO;
  598. err = ovl_get_verity_digest(ofs, &lowerdatapath, &metacopy_data);
  599. if (err)
  600. return err;
  601. if (metacopy_data.digest_algo)
  602. c->metacopy_digest = true;
  603. err = ovl_set_metacopy_xattr(ofs, temp, &metacopy_data);
  604. if (err)
  605. return err;
  606. }
  607. inode_lock(temp->d_inode);
  608. if (S_ISREG(c->stat.mode))
  609. err = ovl_set_size(ofs, temp, &c->stat);
  610. if (!err)
  611. err = ovl_set_attr(ofs, temp, &c->stat);
  612. inode_unlock(temp->d_inode);
  613. /* fsync metadata before moving it into upper dir */
  614. if (!err && ovl_should_sync(ofs) && c->metadata_fsync)
  615. err = ovl_sync_file(&upperpath);
  616. return err;
  617. }
  618. struct ovl_cu_creds {
  619. const struct cred *old;
  620. struct cred *new;
  621. };
  622. static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
  623. {
  624. int err;
  625. cc->old = cc->new = NULL;
  626. err = security_inode_copy_up(dentry, &cc->new);
  627. if (err < 0)
  628. return err;
  629. if (cc->new)
  630. cc->old = override_creds(cc->new);
  631. return 0;
  632. }
  633. static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
  634. {
  635. if (cc->new) {
  636. revert_creds(cc->old);
  637. put_cred(cc->new);
  638. }
  639. }
  640. /*
  641. * Copyup using workdir to prepare temp file. Used when copying up directories,
  642. * special files or when upper fs doesn't support O_TMPFILE.
  643. */
  644. static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
  645. {
  646. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  647. struct inode *inode;
  648. struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
  649. struct path path = { .mnt = ovl_upper_mnt(ofs) };
  650. struct dentry *temp, *upper, *trap;
  651. struct ovl_cu_creds cc;
  652. int err;
  653. struct ovl_cattr cattr = {
  654. /* Can't properly set mode on creation because of the umask */
  655. .mode = c->stat.mode & S_IFMT,
  656. .rdev = c->stat.rdev,
  657. .link = c->link
  658. };
  659. err = ovl_prep_cu_creds(c->dentry, &cc);
  660. if (err)
  661. return err;
  662. ovl_start_write(c->dentry);
  663. inode_lock_nested(wdir, I_MUTEX_PARENT);
  664. temp = ovl_create_temp(ofs, c->workdir, &cattr);
  665. inode_unlock(wdir);
  666. ovl_end_write(c->dentry);
  667. ovl_revert_cu_creds(&cc);
  668. if (IS_ERR(temp))
  669. return PTR_ERR(temp);
  670. /*
  671. * Copy up data first and then xattrs. Writing data after
  672. * xattrs will remove security.capability xattr automatically.
  673. */
  674. path.dentry = temp;
  675. err = ovl_copy_up_data(c, &path);
  676. /*
  677. * We cannot hold lock_rename() throughout this helper, because of
  678. * lock ordering with sb_writers, which shouldn't be held when calling
  679. * ovl_copy_up_data(), so lock workdir and destdir and make sure that
  680. * temp wasn't moved before copy up completion or cleanup.
  681. */
  682. ovl_start_write(c->dentry);
  683. trap = lock_rename(c->workdir, c->destdir);
  684. if (trap || temp->d_parent != c->workdir) {
  685. /* temp or workdir moved underneath us? abort without cleanup */
  686. dput(temp);
  687. err = -EIO;
  688. if (IS_ERR(trap))
  689. goto out;
  690. goto unlock;
  691. } else if (err) {
  692. goto cleanup;
  693. }
  694. err = ovl_copy_up_metadata(c, temp);
  695. if (err)
  696. goto cleanup;
  697. if (S_ISDIR(c->stat.mode) && c->indexed) {
  698. err = ovl_create_index(c->dentry, c->origin_fh, temp);
  699. if (err)
  700. goto cleanup;
  701. }
  702. upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
  703. c->destname.len);
  704. err = PTR_ERR(upper);
  705. if (IS_ERR(upper))
  706. goto cleanup;
  707. err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0);
  708. dput(upper);
  709. if (err)
  710. goto cleanup;
  711. inode = d_inode(c->dentry);
  712. if (c->metacopy_digest)
  713. ovl_set_flag(OVL_HAS_DIGEST, inode);
  714. else
  715. ovl_clear_flag(OVL_HAS_DIGEST, inode);
  716. ovl_clear_flag(OVL_VERIFIED_DIGEST, inode);
  717. if (!c->metacopy)
  718. ovl_set_upperdata(inode);
  719. ovl_inode_update(inode, temp);
  720. if (S_ISDIR(inode->i_mode))
  721. ovl_set_flag(OVL_WHITEOUTS, inode);
  722. unlock:
  723. unlock_rename(c->workdir, c->destdir);
  724. out:
  725. ovl_end_write(c->dentry);
  726. return err;
  727. cleanup:
  728. ovl_cleanup(ofs, wdir, temp);
  729. dput(temp);
  730. goto unlock;
  731. }
  732. /* Copyup using O_TMPFILE which does not require cross dir locking */
  733. static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
  734. {
  735. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  736. struct inode *udir = d_inode(c->destdir);
  737. struct dentry *temp, *upper;
  738. struct file *tmpfile;
  739. struct ovl_cu_creds cc;
  740. int err;
  741. err = ovl_prep_cu_creds(c->dentry, &cc);
  742. if (err)
  743. return err;
  744. ovl_start_write(c->dentry);
  745. tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode);
  746. ovl_end_write(c->dentry);
  747. ovl_revert_cu_creds(&cc);
  748. if (IS_ERR(tmpfile))
  749. return PTR_ERR(tmpfile);
  750. temp = tmpfile->f_path.dentry;
  751. if (!c->metacopy && c->stat.size) {
  752. err = ovl_copy_up_file(ofs, c->dentry, tmpfile, c->stat.size,
  753. !c->metadata_fsync);
  754. if (err)
  755. goto out_fput;
  756. }
  757. ovl_start_write(c->dentry);
  758. err = ovl_copy_up_metadata(c, temp);
  759. if (err)
  760. goto out;
  761. inode_lock_nested(udir, I_MUTEX_PARENT);
  762. upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
  763. c->destname.len);
  764. err = PTR_ERR(upper);
  765. if (!IS_ERR(upper)) {
  766. err = ovl_do_link(ofs, temp, udir, upper);
  767. dput(upper);
  768. }
  769. inode_unlock(udir);
  770. if (err)
  771. goto out;
  772. if (c->metacopy_digest)
  773. ovl_set_flag(OVL_HAS_DIGEST, d_inode(c->dentry));
  774. else
  775. ovl_clear_flag(OVL_HAS_DIGEST, d_inode(c->dentry));
  776. ovl_clear_flag(OVL_VERIFIED_DIGEST, d_inode(c->dentry));
  777. if (!c->metacopy)
  778. ovl_set_upperdata(d_inode(c->dentry));
  779. ovl_inode_update(d_inode(c->dentry), dget(temp));
  780. out:
  781. ovl_end_write(c->dentry);
  782. out_fput:
  783. fput(tmpfile);
  784. return err;
  785. }
  786. /*
  787. * Copy up a single dentry
  788. *
  789. * All renames start with copy up of source if necessary. The actual
  790. * rename will only proceed once the copy up was successful. Copy up uses
  791. * upper parent i_mutex for exclusion. Since rename can change d_parent it
  792. * is possible that the copy up will lock the old parent. At that point
  793. * the file will have already been copied up anyway.
  794. */
  795. static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
  796. {
  797. int err;
  798. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  799. struct dentry *origin = c->lowerpath.dentry;
  800. struct ovl_fh *fh = NULL;
  801. bool to_index = false;
  802. /*
  803. * Indexed non-dir is copied up directly to the index entry and then
  804. * hardlinked to upper dir. Indexed dir is copied up to indexdir,
  805. * then index entry is created and then copied up dir installed.
  806. * Copying dir up to indexdir instead of workdir simplifies locking.
  807. */
  808. if (ovl_need_index(c->dentry)) {
  809. c->indexed = true;
  810. if (S_ISDIR(c->stat.mode))
  811. c->workdir = ovl_indexdir(c->dentry->d_sb);
  812. else
  813. to_index = true;
  814. }
  815. if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) {
  816. fh = ovl_get_origin_fh(ofs, origin);
  817. if (IS_ERR(fh))
  818. return PTR_ERR(fh);
  819. /* origin_fh may be NULL */
  820. c->origin_fh = fh;
  821. c->origin = true;
  822. }
  823. if (to_index) {
  824. c->destdir = ovl_indexdir(c->dentry->d_sb);
  825. err = ovl_get_index_name(ofs, origin, &c->destname);
  826. if (err)
  827. goto out_free_fh;
  828. } else if (WARN_ON(!c->parent)) {
  829. /* Disconnected dentry must be copied up to index dir */
  830. err = -EIO;
  831. goto out_free_fh;
  832. } else {
  833. /*
  834. * c->dentry->d_name is stabilzed by ovl_copy_up_start(),
  835. * because if we got here, it means that c->dentry has no upper
  836. * alias and changing ->d_name means going through ovl_rename()
  837. * that will call ovl_copy_up() on source and target dentry.
  838. */
  839. c->destname = c->dentry->d_name;
  840. /*
  841. * Mark parent "impure" because it may now contain non-pure
  842. * upper
  843. */
  844. ovl_start_write(c->dentry);
  845. err = ovl_set_impure(c->parent, c->destdir);
  846. ovl_end_write(c->dentry);
  847. if (err)
  848. goto out_free_fh;
  849. }
  850. /* Should we copyup with O_TMPFILE or with workdir? */
  851. if (S_ISREG(c->stat.mode) && ofs->tmpfile)
  852. err = ovl_copy_up_tmpfile(c);
  853. else
  854. err = ovl_copy_up_workdir(c);
  855. if (err)
  856. goto out;
  857. if (c->indexed)
  858. ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
  859. ovl_start_write(c->dentry);
  860. if (to_index) {
  861. /* Initialize nlink for copy up of disconnected dentry */
  862. err = ovl_set_nlink_upper(c->dentry);
  863. } else {
  864. struct inode *udir = d_inode(c->destdir);
  865. /* Restore timestamps on parent (best effort) */
  866. inode_lock(udir);
  867. ovl_set_timestamps(ofs, c->destdir, &c->pstat);
  868. inode_unlock(udir);
  869. ovl_dentry_set_upper_alias(c->dentry);
  870. ovl_dentry_update_reval(c->dentry, ovl_dentry_upper(c->dentry));
  871. }
  872. ovl_end_write(c->dentry);
  873. out:
  874. if (to_index)
  875. kfree(c->destname.name);
  876. out_free_fh:
  877. kfree(fh);
  878. return err;
  879. }
  880. static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
  881. int flags)
  882. {
  883. struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  884. if (!ofs->config.metacopy)
  885. return false;
  886. if (!S_ISREG(mode))
  887. return false;
  888. if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
  889. return false;
  890. /* Fall back to full copy if no fsverity on source data and we require verity */
  891. if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
  892. struct path lowerdata;
  893. ovl_path_lowerdata(dentry, &lowerdata);
  894. if (WARN_ON_ONCE(lowerdata.dentry == NULL) ||
  895. ovl_ensure_verity_loaded(&lowerdata) ||
  896. !fsverity_active(d_inode(lowerdata.dentry))) {
  897. return false;
  898. }
  899. }
  900. return true;
  901. }
  902. static ssize_t ovl_getxattr_value(const struct path *path, char *name, char **value)
  903. {
  904. ssize_t res;
  905. char *buf;
  906. res = ovl_do_getxattr(path, name, NULL, 0);
  907. if (res == -ENODATA || res == -EOPNOTSUPP)
  908. res = 0;
  909. if (res > 0) {
  910. buf = kzalloc(res, GFP_KERNEL);
  911. if (!buf)
  912. return -ENOMEM;
  913. res = ovl_do_getxattr(path, name, buf, res);
  914. if (res < 0)
  915. kfree(buf);
  916. else
  917. *value = buf;
  918. }
  919. return res;
  920. }
  921. /* Copy up data of an inode which was copied up metadata only in the past. */
  922. static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
  923. {
  924. struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
  925. struct path upperpath;
  926. int err;
  927. char *capability = NULL;
  928. ssize_t cap_size;
  929. ovl_path_upper(c->dentry, &upperpath);
  930. if (WARN_ON(upperpath.dentry == NULL))
  931. return -EIO;
  932. if (c->stat.size) {
  933. err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS,
  934. &capability);
  935. if (cap_size < 0)
  936. goto out;
  937. }
  938. err = ovl_copy_up_data(c, &upperpath);
  939. if (err)
  940. goto out_free;
  941. /*
  942. * Writing to upper file will clear security.capability xattr. We
  943. * don't want that to happen for normal copy-up operation.
  944. */
  945. ovl_start_write(c->dentry);
  946. if (capability) {
  947. err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS,
  948. capability, cap_size, 0);
  949. }
  950. if (!err) {
  951. err = ovl_removexattr(ofs, upperpath.dentry,
  952. OVL_XATTR_METACOPY);
  953. }
  954. ovl_end_write(c->dentry);
  955. if (err)
  956. goto out_free;
  957. ovl_clear_flag(OVL_HAS_DIGEST, d_inode(c->dentry));
  958. ovl_clear_flag(OVL_VERIFIED_DIGEST, d_inode(c->dentry));
  959. ovl_set_upperdata(d_inode(c->dentry));
  960. out_free:
  961. kfree(capability);
  962. out:
  963. return err;
  964. }
  965. static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  966. int flags)
  967. {
  968. int err;
  969. DEFINE_DELAYED_CALL(done);
  970. struct path parentpath;
  971. struct ovl_copy_up_ctx ctx = {
  972. .parent = parent,
  973. .dentry = dentry,
  974. .workdir = ovl_workdir(dentry),
  975. };
  976. if (WARN_ON(!ctx.workdir))
  977. return -EROFS;
  978. ovl_path_lower(dentry, &ctx.lowerpath);
  979. err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
  980. STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  981. if (err)
  982. return err;
  983. if (!kuid_has_mapping(current_user_ns(), ctx.stat.uid) ||
  984. !kgid_has_mapping(current_user_ns(), ctx.stat.gid))
  985. return -EOVERFLOW;
  986. /*
  987. * With metacopy disabled, we fsync after final metadata copyup, for
  988. * both regular files and directories to get atomic copyup semantics
  989. * on filesystems that do not use strict metadata ordering (e.g. ubifs).
  990. *
  991. * With metacopy enabled we want to avoid fsync on all meta copyup
  992. * that will hurt performance of workloads such as chown -R, so we
  993. * only fsync on data copyup as legacy behavior.
  994. */
  995. ctx.metadata_fsync = !OVL_FS(dentry->d_sb)->config.metacopy &&
  996. (S_ISREG(ctx.stat.mode) || S_ISDIR(ctx.stat.mode));
  997. ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
  998. if (parent) {
  999. ovl_path_upper(parent, &parentpath);
  1000. ctx.destdir = parentpath.dentry;
  1001. err = vfs_getattr(&parentpath, &ctx.pstat,
  1002. STATX_ATIME | STATX_MTIME,
  1003. AT_STATX_SYNC_AS_STAT);
  1004. if (err)
  1005. return err;
  1006. }
  1007. /* maybe truncate regular file. this has no effect on dirs */
  1008. if (flags & O_TRUNC)
  1009. ctx.stat.size = 0;
  1010. if (S_ISLNK(ctx.stat.mode)) {
  1011. ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
  1012. if (IS_ERR(ctx.link))
  1013. return PTR_ERR(ctx.link);
  1014. }
  1015. err = ovl_copy_up_start(dentry, flags);
  1016. /* err < 0: interrupted, err > 0: raced with another copy-up */
  1017. if (unlikely(err)) {
  1018. if (err > 0)
  1019. err = 0;
  1020. } else {
  1021. if (!ovl_dentry_upper(dentry))
  1022. err = ovl_do_copy_up(&ctx);
  1023. if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
  1024. err = ovl_link_up(&ctx);
  1025. if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
  1026. err = ovl_copy_up_meta_inode_data(&ctx);
  1027. ovl_copy_up_end(dentry);
  1028. }
  1029. do_delayed_call(&done);
  1030. return err;
  1031. }
  1032. static int ovl_copy_up_flags(struct dentry *dentry, int flags)
  1033. {
  1034. int err = 0;
  1035. const struct cred *old_cred;
  1036. bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
  1037. /*
  1038. * With NFS export, copy up can get called for a disconnected non-dir.
  1039. * In this case, we will copy up lower inode to index dir without
  1040. * linking it to upper dir.
  1041. */
  1042. if (WARN_ON(disconnected && d_is_dir(dentry)))
  1043. return -EIO;
  1044. /*
  1045. * We may not need lowerdata if we are only doing metacopy up, but it is
  1046. * not very important to optimize this case, so do lazy lowerdata lookup
  1047. * before any copy up, so we can do it before taking ovl_inode_lock().
  1048. */
  1049. err = ovl_verify_lowerdata(dentry);
  1050. if (err)
  1051. return err;
  1052. old_cred = ovl_override_creds(dentry->d_sb);
  1053. while (!err) {
  1054. struct dentry *next;
  1055. struct dentry *parent = NULL;
  1056. if (ovl_already_copied_up(dentry, flags))
  1057. break;
  1058. next = dget(dentry);
  1059. /* find the topmost dentry not yet copied up */
  1060. for (; !disconnected;) {
  1061. parent = dget_parent(next);
  1062. if (ovl_dentry_upper(parent))
  1063. break;
  1064. dput(next);
  1065. next = parent;
  1066. }
  1067. err = ovl_copy_up_one(parent, next, flags);
  1068. dput(parent);
  1069. dput(next);
  1070. }
  1071. revert_creds(old_cred);
  1072. return err;
  1073. }
  1074. static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
  1075. {
  1076. /* Copy up of disconnected dentry does not set upper alias */
  1077. if (ovl_already_copied_up(dentry, flags))
  1078. return false;
  1079. if (special_file(d_inode(dentry)->i_mode))
  1080. return false;
  1081. if (!ovl_open_flags_need_copy_up(flags))
  1082. return false;
  1083. return true;
  1084. }
  1085. int ovl_maybe_copy_up(struct dentry *dentry, int flags)
  1086. {
  1087. if (!ovl_open_need_copy_up(dentry, flags))
  1088. return 0;
  1089. return ovl_copy_up_flags(dentry, flags);
  1090. }
  1091. int ovl_copy_up_with_data(struct dentry *dentry)
  1092. {
  1093. return ovl_copy_up_flags(dentry, O_WRONLY);
  1094. }
  1095. int ovl_copy_up(struct dentry *dentry)
  1096. {
  1097. return ovl_copy_up_flags(dentry, 0);
  1098. }