copy_up.c 8.5 KB

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