inode.c 8.9 KB

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