copy_up.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/fs.h>
  10. #include <linux/slab.h>
  11. #include <linux/file.h>
  12. #include <linux/splice.h>
  13. #include <linux/xattr.h>
  14. #include <linux/security.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/sched.h>
  17. #include <linux/namei.h>
  18. #include "overlayfs.h"
  19. #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
  20. int ovl_copy_xattr(struct dentry *old, struct dentry *new)
  21. {
  22. ssize_t list_size, size, value_size = 0;
  23. char *buf, *name, *value = NULL;
  24. int uninitialized_var(error);
  25. if (!old->d_inode->i_op->getxattr ||
  26. !new->d_inode->i_op->getxattr)
  27. return 0;
  28. list_size = vfs_listxattr(old, NULL, 0);
  29. if (list_size <= 0) {
  30. if (list_size == -EOPNOTSUPP)
  31. return 0;
  32. return list_size;
  33. }
  34. buf = kzalloc(list_size, GFP_KERNEL);
  35. if (!buf)
  36. return -ENOMEM;
  37. list_size = vfs_listxattr(old, buf, list_size);
  38. if (list_size <= 0) {
  39. error = list_size;
  40. goto out;
  41. }
  42. for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
  43. retry:
  44. size = vfs_getxattr(old, name, value, value_size);
  45. if (size == -ERANGE)
  46. size = vfs_getxattr(old, name, NULL, 0);
  47. if (size < 0) {
  48. error = size;
  49. break;
  50. }
  51. if (size > value_size) {
  52. void *new;
  53. new = krealloc(value, size, GFP_KERNEL);
  54. if (!new) {
  55. error = -ENOMEM;
  56. break;
  57. }
  58. value = new;
  59. value_size = size;
  60. goto retry;
  61. }
  62. error = vfs_setxattr(new, name, value, size, 0);
  63. if (error)
  64. break;
  65. }
  66. kfree(value);
  67. out:
  68. kfree(buf);
  69. return error;
  70. }
  71. static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
  72. {
  73. struct file *old_file;
  74. struct file *new_file;
  75. loff_t old_pos = 0;
  76. loff_t new_pos = 0;
  77. int error = 0;
  78. if (len == 0)
  79. return 0;
  80. old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
  81. if (IS_ERR(old_file))
  82. return PTR_ERR(old_file);
  83. new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
  84. if (IS_ERR(new_file)) {
  85. error = PTR_ERR(new_file);
  86. goto out_fput;
  87. }
  88. /* FIXME: copy up sparse files efficiently */
  89. while (len) {
  90. size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  91. long bytes;
  92. if (len < this_len)
  93. this_len = len;
  94. if (signal_pending_state(TASK_KILLABLE, current)) {
  95. error = -EINTR;
  96. break;
  97. }
  98. bytes = do_splice_direct(old_file, &old_pos,
  99. new_file, &new_pos,
  100. this_len, SPLICE_F_MOVE);
  101. if (bytes <= 0) {
  102. error = bytes;
  103. break;
  104. }
  105. WARN_ON(old_pos != new_pos);
  106. len -= bytes;
  107. }
  108. fput(new_file);
  109. out_fput:
  110. fput(old_file);
  111. return error;
  112. }
  113. static char *ovl_read_symlink(struct dentry *realdentry)
  114. {
  115. int res;
  116. char *buf;
  117. struct inode *inode = realdentry->d_inode;
  118. mm_segment_t old_fs;
  119. res = -EINVAL;
  120. if (!inode->i_op->readlink)
  121. goto err;
  122. res = -ENOMEM;
  123. buf = (char *) __get_free_page(GFP_KERNEL);
  124. if (!buf)
  125. goto err;
  126. old_fs = get_fs();
  127. set_fs(get_ds());
  128. /* The cast to a user pointer is valid due to the set_fs() */
  129. res = inode->i_op->readlink(realdentry,
  130. (char __user *)buf, PAGE_SIZE - 1);
  131. set_fs(old_fs);
  132. if (res < 0) {
  133. free_page((unsigned long) buf);
  134. goto err;
  135. }
  136. buf[res] = '\0';
  137. return buf;
  138. err:
  139. return ERR_PTR(res);
  140. }
  141. static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
  142. {
  143. struct iattr attr = {
  144. .ia_valid =
  145. ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
  146. .ia_atime = stat->atime,
  147. .ia_mtime = stat->mtime,
  148. };
  149. return notify_change(upperdentry, &attr, NULL);
  150. }
  151. int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
  152. {
  153. int err = 0;
  154. if (!S_ISLNK(stat->mode)) {
  155. struct iattr attr = {
  156. .ia_valid = ATTR_MODE,
  157. .ia_mode = stat->mode,
  158. };
  159. err = notify_change(upperdentry, &attr, NULL);
  160. }
  161. if (!err) {
  162. struct iattr attr = {
  163. .ia_valid = ATTR_UID | ATTR_GID,
  164. .ia_uid = stat->uid,
  165. .ia_gid = stat->gid,
  166. };
  167. err = notify_change(upperdentry, &attr, NULL);
  168. }
  169. if (!err)
  170. ovl_set_timestamps(upperdentry, stat);
  171. return err;
  172. }
  173. static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
  174. struct dentry *dentry, struct path *lowerpath,
  175. struct kstat *stat, const char *link)
  176. {
  177. struct inode *wdir = workdir->d_inode;
  178. struct inode *udir = upperdir->d_inode;
  179. struct dentry *newdentry = NULL;
  180. struct dentry *upper = NULL;
  181. umode_t mode = stat->mode;
  182. int err;
  183. newdentry = ovl_lookup_temp(workdir, dentry);
  184. err = PTR_ERR(newdentry);
  185. if (IS_ERR(newdentry))
  186. goto out;
  187. upper = lookup_one_len(dentry->d_name.name, upperdir,
  188. dentry->d_name.len);
  189. err = PTR_ERR(upper);
  190. if (IS_ERR(upper))
  191. goto out1;
  192. /* Can't properly set mode on creation because of the umask */
  193. stat->mode &= S_IFMT;
  194. err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
  195. stat->mode = mode;
  196. if (err)
  197. goto out2;
  198. if (S_ISREG(stat->mode)) {
  199. struct path upperpath;
  200. ovl_path_upper(dentry, &upperpath);
  201. BUG_ON(upperpath.dentry != NULL);
  202. upperpath.dentry = newdentry;
  203. err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
  204. if (err)
  205. goto out_cleanup;
  206. }
  207. err = ovl_copy_xattr(lowerpath->dentry, newdentry);
  208. if (err)
  209. goto out_cleanup;
  210. inode_lock(newdentry->d_inode);
  211. err = ovl_set_attr(newdentry, stat);
  212. inode_unlock(newdentry->d_inode);
  213. if (err)
  214. goto out_cleanup;
  215. err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
  216. if (err)
  217. goto out_cleanup;
  218. ovl_dentry_update(dentry, newdentry);
  219. newdentry = NULL;
  220. /*
  221. * Non-directores become opaque when copied up.
  222. */
  223. if (!S_ISDIR(stat->mode))
  224. ovl_dentry_set_opaque(dentry, true);
  225. out2:
  226. dput(upper);
  227. out1:
  228. dput(newdentry);
  229. out:
  230. return err;
  231. out_cleanup:
  232. ovl_cleanup(wdir, newdentry);
  233. goto out2;
  234. }
  235. /*
  236. * Copy up a single dentry
  237. *
  238. * Directory renames only allowed on "pure upper" (already created on
  239. * upper filesystem, never copied up). Directories which are on lower or
  240. * are merged may not be renamed. For these -EXDEV is returned and
  241. * userspace has to deal with it. This means, when copying up a
  242. * directory we can rely on it and ancestors being stable.
  243. *
  244. * Non-directory renames start with copy up of source if necessary. The
  245. * actual rename will only proceed once the copy up was successful. Copy
  246. * up uses upper parent i_mutex for exclusion. Since rename can change
  247. * d_parent it is possible that the copy up will lock the old parent. At
  248. * that point the file will have already been copied up anyway.
  249. */
  250. int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  251. struct path *lowerpath, struct kstat *stat)
  252. {
  253. struct dentry *workdir = ovl_workdir(dentry);
  254. int err;
  255. struct kstat pstat;
  256. struct path parentpath;
  257. struct dentry *upperdir;
  258. struct dentry *upperdentry;
  259. const struct cred *old_cred;
  260. struct cred *override_cred;
  261. char *link = NULL;
  262. if (WARN_ON(!workdir))
  263. return -EROFS;
  264. ovl_path_upper(parent, &parentpath);
  265. upperdir = parentpath.dentry;
  266. err = vfs_getattr(&parentpath, &pstat);
  267. if (err)
  268. return err;
  269. if (S_ISLNK(stat->mode)) {
  270. link = ovl_read_symlink(lowerpath->dentry);
  271. if (IS_ERR(link))
  272. return PTR_ERR(link);
  273. }
  274. err = -ENOMEM;
  275. override_cred = prepare_creds();
  276. if (!override_cred)
  277. goto out_free_link;
  278. override_cred->fsuid = stat->uid;
  279. override_cred->fsgid = stat->gid;
  280. /*
  281. * CAP_SYS_ADMIN for copying up extended attributes
  282. * CAP_DAC_OVERRIDE for create
  283. * CAP_FOWNER for chmod, timestamp update
  284. * CAP_FSETID for chmod
  285. * CAP_CHOWN for chown
  286. * CAP_MKNOD for mknod
  287. */
  288. cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  289. cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
  290. cap_raise(override_cred->cap_effective, CAP_FOWNER);
  291. cap_raise(override_cred->cap_effective, CAP_FSETID);
  292. cap_raise(override_cred->cap_effective, CAP_CHOWN);
  293. cap_raise(override_cred->cap_effective, CAP_MKNOD);
  294. old_cred = override_creds(override_cred);
  295. err = -EIO;
  296. if (lock_rename(workdir, upperdir) != NULL) {
  297. pr_err("overlayfs: failed to lock workdir+upperdir\n");
  298. goto out_unlock;
  299. }
  300. upperdentry = ovl_dentry_upper(dentry);
  301. if (upperdentry) {
  302. /* Raced with another copy-up? Nothing to do, then... */
  303. err = 0;
  304. goto out_unlock;
  305. }
  306. err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
  307. stat, link);
  308. if (!err) {
  309. /* Restore timestamps on parent (best effort) */
  310. ovl_set_timestamps(upperdir, &pstat);
  311. }
  312. out_unlock:
  313. unlock_rename(workdir, upperdir);
  314. revert_creds(old_cred);
  315. put_cred(override_cred);
  316. out_free_link:
  317. if (link)
  318. free_page((unsigned long) link);
  319. return err;
  320. }
  321. int ovl_copy_up(struct dentry *dentry)
  322. {
  323. int err;
  324. err = 0;
  325. while (!err) {
  326. struct dentry *next;
  327. struct dentry *parent;
  328. struct path lowerpath;
  329. struct kstat stat;
  330. enum ovl_path_type type = ovl_path_type(dentry);
  331. if (OVL_TYPE_UPPER(type))
  332. break;
  333. next = dget(dentry);
  334. /* find the topmost dentry not yet copied up */
  335. for (;;) {
  336. parent = dget_parent(next);
  337. type = ovl_path_type(parent);
  338. if (OVL_TYPE_UPPER(type))
  339. break;
  340. dput(next);
  341. next = parent;
  342. }
  343. ovl_path_lower(next, &lowerpath);
  344. err = vfs_getattr(&lowerpath, &stat);
  345. if (!err)
  346. err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
  347. dput(parent);
  348. dput(next);
  349. }
  350. return err;
  351. }