inode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
  121. {
  122. void *ret;
  123. struct dentry *realdentry;
  124. struct inode *realinode;
  125. realdentry = ovl_dentry_real(dentry);
  126. realinode = realdentry->d_inode;
  127. if (WARN_ON(!realinode->i_op->follow_link))
  128. return ERR_PTR(-EPERM);
  129. ret = realinode->i_op->follow_link(realdentry, nd);
  130. if (IS_ERR(ret))
  131. return ret;
  132. if (realinode->i_op->put_link) {
  133. struct ovl_link_data *data;
  134. data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
  135. if (!data) {
  136. realinode->i_op->put_link(realdentry, nd, ret);
  137. return ERR_PTR(-ENOMEM);
  138. }
  139. data->realdentry = realdentry;
  140. data->cookie = ret;
  141. return data;
  142. } else {
  143. return NULL;
  144. }
  145. }
  146. static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, 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(data->realdentry, nd, 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. static int ovl_dentry_open(struct dentry *dentry, struct file *file,
  272. const struct cred *cred)
  273. {
  274. int err;
  275. struct path realpath;
  276. enum ovl_path_type type;
  277. bool want_write = false;
  278. type = ovl_path_real(dentry, &realpath);
  279. if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
  280. want_write = true;
  281. err = ovl_want_write(dentry);
  282. if (err)
  283. goto out;
  284. if (file->f_flags & O_TRUNC)
  285. err = ovl_copy_up_last(dentry, NULL, true);
  286. else
  287. err = ovl_copy_up(dentry);
  288. if (err)
  289. goto out_drop_write;
  290. ovl_path_upper(dentry, &realpath);
  291. }
  292. err = vfs_open(&realpath, file, cred);
  293. out_drop_write:
  294. if (want_write)
  295. ovl_drop_write(dentry);
  296. out:
  297. return err;
  298. }
  299. static const struct inode_operations ovl_file_inode_operations = {
  300. .setattr = ovl_setattr,
  301. .permission = ovl_permission,
  302. .getattr = ovl_getattr,
  303. .setxattr = ovl_setxattr,
  304. .getxattr = ovl_getxattr,
  305. .listxattr = ovl_listxattr,
  306. .removexattr = ovl_removexattr,
  307. .dentry_open = ovl_dentry_open,
  308. };
  309. static const struct inode_operations ovl_symlink_inode_operations = {
  310. .setattr = ovl_setattr,
  311. .follow_link = ovl_follow_link,
  312. .put_link = ovl_put_link,
  313. .readlink = ovl_readlink,
  314. .getattr = ovl_getattr,
  315. .setxattr = ovl_setxattr,
  316. .getxattr = ovl_getxattr,
  317. .listxattr = ovl_listxattr,
  318. .removexattr = ovl_removexattr,
  319. };
  320. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
  321. struct ovl_entry *oe)
  322. {
  323. struct inode *inode;
  324. inode = new_inode(sb);
  325. if (!inode)
  326. return NULL;
  327. mode &= S_IFMT;
  328. inode->i_ino = get_next_ino();
  329. inode->i_mode = mode;
  330. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  331. switch (mode) {
  332. case S_IFDIR:
  333. inode->i_private = oe;
  334. inode->i_op = &ovl_dir_inode_operations;
  335. inode->i_fop = &ovl_dir_operations;
  336. break;
  337. case S_IFLNK:
  338. inode->i_op = &ovl_symlink_inode_operations;
  339. break;
  340. case S_IFREG:
  341. case S_IFSOCK:
  342. case S_IFBLK:
  343. case S_IFCHR:
  344. case S_IFIFO:
  345. inode->i_op = &ovl_file_inode_operations;
  346. break;
  347. default:
  348. WARN(1, "illegal file type: %i\n", mode);
  349. iput(inode);
  350. inode = NULL;
  351. }
  352. return inode;
  353. }