inode.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/xattr.h>
  12. #include "overlayfs.h"
  13. static int ovl_copy_up_last(struct dentry *dentry, struct iattr *attr,
  14. bool no_data)
  15. {
  16. int err;
  17. struct dentry *parent;
  18. struct kstat stat;
  19. struct path lowerpath;
  20. parent = dget_parent(dentry);
  21. err = ovl_copy_up(parent);
  22. if (err)
  23. goto out_dput_parent;
  24. ovl_path_lower(dentry, &lowerpath);
  25. err = vfs_getattr(&lowerpath, &stat);
  26. if (err)
  27. goto out_dput_parent;
  28. if (no_data)
  29. stat.size = 0;
  30. err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat, attr);
  31. out_dput_parent:
  32. dput(parent);
  33. return err;
  34. }
  35. int ovl_setattr(struct dentry *dentry, struct iattr *attr)
  36. {
  37. int err;
  38. struct dentry *upperdentry;
  39. err = ovl_want_write(dentry);
  40. if (err)
  41. goto out;
  42. upperdentry = ovl_dentry_upper(dentry);
  43. if (upperdentry) {
  44. mutex_lock(&upperdentry->d_inode->i_mutex);
  45. err = notify_change(upperdentry, attr, NULL);
  46. mutex_unlock(&upperdentry->d_inode->i_mutex);
  47. } else {
  48. err = ovl_copy_up_last(dentry, attr, false);
  49. }
  50. ovl_drop_write(dentry);
  51. out:
  52. return err;
  53. }
  54. static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
  55. struct kstat *stat)
  56. {
  57. struct path realpath;
  58. ovl_path_real(dentry, &realpath);
  59. return vfs_getattr(&realpath, stat);
  60. }
  61. int ovl_permission(struct inode *inode, int mask)
  62. {
  63. struct ovl_entry *oe;
  64. struct dentry *alias = NULL;
  65. struct inode *realinode;
  66. struct dentry *realdentry;
  67. bool is_upper;
  68. int err;
  69. if (S_ISDIR(inode->i_mode)) {
  70. oe = inode->i_private;
  71. } else if (mask & MAY_NOT_BLOCK) {
  72. return -ECHILD;
  73. } else {
  74. /*
  75. * For non-directories find an alias and get the info
  76. * from there.
  77. */
  78. alias = d_find_any_alias(inode);
  79. if (WARN_ON(!alias))
  80. return -ENOENT;
  81. oe = alias->d_fsdata;
  82. }
  83. realdentry = ovl_entry_real(oe, &is_upper);
  84. /* Careful in RCU walk mode */
  85. realinode = ACCESS_ONCE(realdentry->d_inode);
  86. if (!realinode) {
  87. WARN_ON(!(mask & MAY_NOT_BLOCK));
  88. err = -ENOENT;
  89. goto out_dput;
  90. }
  91. if (mask & MAY_WRITE) {
  92. umode_t mode = realinode->i_mode;
  93. /*
  94. * Writes will always be redirected to upper layer, so
  95. * ignore lower layer being read-only.
  96. *
  97. * If the overlay itself is read-only then proceed
  98. * with the permission check, don't return EROFS.
  99. * This will only happen if this is the lower layer of
  100. * another overlayfs.
  101. *
  102. * If upper fs becomes read-only after the overlay was
  103. * constructed return EROFS to prevent modification of
  104. * upper layer.
  105. */
  106. err = -EROFS;
  107. if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
  108. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  109. goto out_dput;
  110. }
  111. err = __inode_permission(realinode, mask);
  112. out_dput:
  113. dput(alias);
  114. return err;
  115. }
  116. struct ovl_link_data {
  117. struct dentry *realdentry;
  118. void *cookie;
  119. };
  120. static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
  121. {
  122. struct dentry *realdentry;
  123. struct inode *realinode;
  124. struct ovl_link_data *data = NULL;
  125. const char *ret;
  126. realdentry = ovl_dentry_real(dentry);
  127. realinode = realdentry->d_inode;
  128. if (WARN_ON(!realinode->i_op->follow_link))
  129. return ERR_PTR(-EPERM);
  130. if (realinode->i_op->put_link) {
  131. data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
  132. if (!data)
  133. return ERR_PTR(-ENOMEM);
  134. data->realdentry = realdentry;
  135. }
  136. ret = realinode->i_op->follow_link(realdentry, cookie);
  137. if (IS_ERR_OR_NULL(ret)) {
  138. kfree(data);
  139. return ret;
  140. }
  141. if (data)
  142. data->cookie = *cookie;
  143. *cookie = data;
  144. return ret;
  145. }
  146. static void ovl_put_link(struct inode *unused, void *c)
  147. {
  148. struct inode *realinode;
  149. struct ovl_link_data *data = c;
  150. if (!data)
  151. return;
  152. realinode = data->realdentry->d_inode;
  153. realinode->i_op->put_link(realinode, data->cookie);
  154. kfree(data);
  155. }
  156. static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  157. {
  158. struct path realpath;
  159. struct inode *realinode;
  160. ovl_path_real(dentry, &realpath);
  161. realinode = realpath.dentry->d_inode;
  162. if (!realinode->i_op->readlink)
  163. return -EINVAL;
  164. touch_atime(&realpath);
  165. return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
  166. }
  167. static bool ovl_is_private_xattr(const char *name)
  168. {
  169. return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
  170. }
  171. int ovl_setxattr(struct dentry *dentry, const char *name,
  172. const void *value, size_t size, int flags)
  173. {
  174. int err;
  175. struct dentry *upperdentry;
  176. err = ovl_want_write(dentry);
  177. if (err)
  178. goto out;
  179. err = -EPERM;
  180. if (ovl_is_private_xattr(name))
  181. goto out_drop_write;
  182. err = ovl_copy_up(dentry);
  183. if (err)
  184. goto out_drop_write;
  185. upperdentry = ovl_dentry_upper(dentry);
  186. err = vfs_setxattr(upperdentry, name, value, size, flags);
  187. out_drop_write:
  188. ovl_drop_write(dentry);
  189. out:
  190. return err;
  191. }
  192. static bool ovl_need_xattr_filter(struct dentry *dentry,
  193. enum ovl_path_type type)
  194. {
  195. if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
  196. return S_ISDIR(dentry->d_inode->i_mode);
  197. else
  198. return false;
  199. }
  200. ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
  201. void *value, size_t size)
  202. {
  203. struct path realpath;
  204. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  205. if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
  206. return -ENODATA;
  207. return vfs_getxattr(realpath.dentry, name, value, size);
  208. }
  209. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  210. {
  211. struct path realpath;
  212. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  213. ssize_t res;
  214. int off;
  215. res = vfs_listxattr(realpath.dentry, list, size);
  216. if (res <= 0 || size == 0)
  217. return res;
  218. if (!ovl_need_xattr_filter(dentry, type))
  219. return res;
  220. /* filter out private xattrs */
  221. for (off = 0; off < res;) {
  222. char *s = list + off;
  223. size_t slen = strlen(s) + 1;
  224. BUG_ON(off + slen > res);
  225. if (ovl_is_private_xattr(s)) {
  226. res -= slen;
  227. memmove(s, s + slen, res - off);
  228. } else {
  229. off += slen;
  230. }
  231. }
  232. return res;
  233. }
  234. int ovl_removexattr(struct dentry *dentry, const char *name)
  235. {
  236. int err;
  237. struct path realpath;
  238. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  239. err = ovl_want_write(dentry);
  240. if (err)
  241. goto out;
  242. err = -ENODATA;
  243. if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
  244. goto out_drop_write;
  245. if (!OVL_TYPE_UPPER(type)) {
  246. err = vfs_getxattr(realpath.dentry, name, NULL, 0);
  247. if (err < 0)
  248. goto out_drop_write;
  249. err = ovl_copy_up(dentry);
  250. if (err)
  251. goto out_drop_write;
  252. ovl_path_upper(dentry, &realpath);
  253. }
  254. err = vfs_removexattr(realpath.dentry, name);
  255. out_drop_write:
  256. ovl_drop_write(dentry);
  257. out:
  258. return err;
  259. }
  260. static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
  261. struct dentry *realdentry)
  262. {
  263. if (OVL_TYPE_UPPER(type))
  264. return false;
  265. if (special_file(realdentry->d_inode->i_mode))
  266. return false;
  267. if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  268. return false;
  269. return true;
  270. }
  271. struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
  272. {
  273. int err;
  274. struct path realpath;
  275. enum ovl_path_type type;
  276. type = ovl_path_real(dentry, &realpath);
  277. if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
  278. err = ovl_want_write(dentry);
  279. if (err)
  280. return ERR_PTR(err);
  281. if (file_flags & O_TRUNC)
  282. err = ovl_copy_up_last(dentry, NULL, true);
  283. else
  284. err = ovl_copy_up(dentry);
  285. ovl_drop_write(dentry);
  286. if (err)
  287. return ERR_PTR(err);
  288. ovl_path_upper(dentry, &realpath);
  289. }
  290. return d_backing_inode(realpath.dentry);
  291. }
  292. static const struct inode_operations ovl_file_inode_operations = {
  293. .setattr = ovl_setattr,
  294. .permission = ovl_permission,
  295. .getattr = ovl_getattr,
  296. .setxattr = ovl_setxattr,
  297. .getxattr = ovl_getxattr,
  298. .listxattr = ovl_listxattr,
  299. .removexattr = ovl_removexattr,
  300. };
  301. static const struct inode_operations ovl_symlink_inode_operations = {
  302. .setattr = ovl_setattr,
  303. .follow_link = ovl_follow_link,
  304. .put_link = ovl_put_link,
  305. .readlink = ovl_readlink,
  306. .getattr = ovl_getattr,
  307. .setxattr = ovl_setxattr,
  308. .getxattr = ovl_getxattr,
  309. .listxattr = ovl_listxattr,
  310. .removexattr = ovl_removexattr,
  311. };
  312. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
  313. struct ovl_entry *oe)
  314. {
  315. struct inode *inode;
  316. inode = new_inode(sb);
  317. if (!inode)
  318. return NULL;
  319. mode &= S_IFMT;
  320. inode->i_ino = get_next_ino();
  321. inode->i_mode = mode;
  322. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  323. switch (mode) {
  324. case S_IFDIR:
  325. inode->i_private = oe;
  326. inode->i_op = &ovl_dir_inode_operations;
  327. inode->i_fop = &ovl_dir_operations;
  328. break;
  329. case S_IFLNK:
  330. inode->i_op = &ovl_symlink_inode_operations;
  331. break;
  332. case S_IFREG:
  333. case S_IFSOCK:
  334. case S_IFBLK:
  335. case S_IFCHR:
  336. case S_IFIFO:
  337. inode->i_op = &ovl_file_inode_operations;
  338. break;
  339. default:
  340. WARN(1, "illegal file type: %i\n", mode);
  341. iput(inode);
  342. inode = NULL;
  343. }
  344. return inode;
  345. }