namei.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com)
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <ooo@electrozaur.com>
  6. *
  7. * Copyrights for code taken from ext2:
  8. * Copyright (C) 1992, 1993, 1994, 1995
  9. * Remy Card (card@masi.ibp.fr)
  10. * Laboratoire MASI - Institut Blaise Pascal
  11. * Universite Pierre et Marie Curie (Paris VI)
  12. * from
  13. * linux/fs/minix/inode.c
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * This file is part of exofs.
  17. *
  18. * exofs is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation. Since it is based on ext2, and the only
  21. * valid version of GPL for the Linux kernel is version 2, the only valid
  22. * version of GPL for exofs is version 2.
  23. *
  24. * exofs is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with exofs; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  32. */
  33. #include "exofs.h"
  34. static inline int exofs_add_nondir(struct dentry *dentry, struct inode *inode)
  35. {
  36. int err = exofs_add_link(dentry, inode);
  37. if (!err) {
  38. d_instantiate(dentry, inode);
  39. return 0;
  40. }
  41. inode_dec_link_count(inode);
  42. iput(inode);
  43. return err;
  44. }
  45. static struct dentry *exofs_lookup(struct inode *dir, struct dentry *dentry,
  46. unsigned int flags)
  47. {
  48. struct inode *inode;
  49. ino_t ino;
  50. if (dentry->d_name.len > EXOFS_NAME_LEN)
  51. return ERR_PTR(-ENAMETOOLONG);
  52. ino = exofs_inode_by_name(dir, dentry);
  53. inode = ino ? exofs_iget(dir->i_sb, ino) : NULL;
  54. return d_splice_alias(inode, dentry);
  55. }
  56. static int exofs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  57. bool excl)
  58. {
  59. struct inode *inode = exofs_new_inode(dir, mode);
  60. int err = PTR_ERR(inode);
  61. if (!IS_ERR(inode)) {
  62. inode->i_op = &exofs_file_inode_operations;
  63. inode->i_fop = &exofs_file_operations;
  64. inode->i_mapping->a_ops = &exofs_aops;
  65. mark_inode_dirty(inode);
  66. err = exofs_add_nondir(dentry, inode);
  67. }
  68. return err;
  69. }
  70. static int exofs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  71. dev_t rdev)
  72. {
  73. struct inode *inode;
  74. int err;
  75. inode = exofs_new_inode(dir, mode);
  76. err = PTR_ERR(inode);
  77. if (!IS_ERR(inode)) {
  78. init_special_inode(inode, inode->i_mode, rdev);
  79. mark_inode_dirty(inode);
  80. err = exofs_add_nondir(dentry, inode);
  81. }
  82. return err;
  83. }
  84. static int exofs_symlink(struct inode *dir, struct dentry *dentry,
  85. const char *symname)
  86. {
  87. struct super_block *sb = dir->i_sb;
  88. int err = -ENAMETOOLONG;
  89. unsigned l = strlen(symname)+1;
  90. struct inode *inode;
  91. struct exofs_i_info *oi;
  92. if (l > sb->s_blocksize)
  93. goto out;
  94. inode = exofs_new_inode(dir, S_IFLNK | S_IRWXUGO);
  95. err = PTR_ERR(inode);
  96. if (IS_ERR(inode))
  97. goto out;
  98. oi = exofs_i(inode);
  99. if (l > sizeof(oi->i_data)) {
  100. /* slow symlink */
  101. inode->i_op = &page_symlink_inode_operations;
  102. inode->i_mapping->a_ops = &exofs_aops;
  103. memset(oi->i_data, 0, sizeof(oi->i_data));
  104. err = page_symlink(inode, symname, l);
  105. if (err)
  106. goto out_fail;
  107. } else {
  108. /* fast symlink */
  109. inode->i_op = &simple_symlink_inode_operations;
  110. inode->i_link = (char *)oi->i_data;
  111. memcpy(oi->i_data, symname, l);
  112. inode->i_size = l-1;
  113. }
  114. mark_inode_dirty(inode);
  115. err = exofs_add_nondir(dentry, inode);
  116. out:
  117. return err;
  118. out_fail:
  119. inode_dec_link_count(inode);
  120. iput(inode);
  121. goto out;
  122. }
  123. static int exofs_link(struct dentry *old_dentry, struct inode *dir,
  124. struct dentry *dentry)
  125. {
  126. struct inode *inode = d_inode(old_dentry);
  127. inode->i_ctime = CURRENT_TIME;
  128. inode_inc_link_count(inode);
  129. ihold(inode);
  130. return exofs_add_nondir(dentry, inode);
  131. }
  132. static int exofs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  133. {
  134. struct inode *inode;
  135. int err;
  136. inode_inc_link_count(dir);
  137. inode = exofs_new_inode(dir, S_IFDIR | mode);
  138. err = PTR_ERR(inode);
  139. if (IS_ERR(inode))
  140. goto out_dir;
  141. inode->i_op = &exofs_dir_inode_operations;
  142. inode->i_fop = &exofs_dir_operations;
  143. inode->i_mapping->a_ops = &exofs_aops;
  144. inode_inc_link_count(inode);
  145. err = exofs_make_empty(inode, dir);
  146. if (err)
  147. goto out_fail;
  148. err = exofs_add_link(dentry, inode);
  149. if (err)
  150. goto out_fail;
  151. d_instantiate(dentry, inode);
  152. out:
  153. return err;
  154. out_fail:
  155. inode_dec_link_count(inode);
  156. inode_dec_link_count(inode);
  157. iput(inode);
  158. out_dir:
  159. inode_dec_link_count(dir);
  160. goto out;
  161. }
  162. static int exofs_unlink(struct inode *dir, struct dentry *dentry)
  163. {
  164. struct inode *inode = d_inode(dentry);
  165. struct exofs_dir_entry *de;
  166. struct page *page;
  167. int err = -ENOENT;
  168. de = exofs_find_entry(dir, dentry, &page);
  169. if (!de)
  170. goto out;
  171. err = exofs_delete_entry(de, page);
  172. if (err)
  173. goto out;
  174. inode->i_ctime = dir->i_ctime;
  175. inode_dec_link_count(inode);
  176. err = 0;
  177. out:
  178. return err;
  179. }
  180. static int exofs_rmdir(struct inode *dir, struct dentry *dentry)
  181. {
  182. struct inode *inode = d_inode(dentry);
  183. int err = -ENOTEMPTY;
  184. if (exofs_empty_dir(inode)) {
  185. err = exofs_unlink(dir, dentry);
  186. if (!err) {
  187. inode->i_size = 0;
  188. inode_dec_link_count(inode);
  189. inode_dec_link_count(dir);
  190. }
  191. }
  192. return err;
  193. }
  194. static int exofs_rename(struct inode *old_dir, struct dentry *old_dentry,
  195. struct inode *new_dir, struct dentry *new_dentry)
  196. {
  197. struct inode *old_inode = d_inode(old_dentry);
  198. struct inode *new_inode = d_inode(new_dentry);
  199. struct page *dir_page = NULL;
  200. struct exofs_dir_entry *dir_de = NULL;
  201. struct page *old_page;
  202. struct exofs_dir_entry *old_de;
  203. int err = -ENOENT;
  204. old_de = exofs_find_entry(old_dir, old_dentry, &old_page);
  205. if (!old_de)
  206. goto out;
  207. if (S_ISDIR(old_inode->i_mode)) {
  208. err = -EIO;
  209. dir_de = exofs_dotdot(old_inode, &dir_page);
  210. if (!dir_de)
  211. goto out_old;
  212. }
  213. if (new_inode) {
  214. struct page *new_page;
  215. struct exofs_dir_entry *new_de;
  216. err = -ENOTEMPTY;
  217. if (dir_de && !exofs_empty_dir(new_inode))
  218. goto out_dir;
  219. err = -ENOENT;
  220. new_de = exofs_find_entry(new_dir, new_dentry, &new_page);
  221. if (!new_de)
  222. goto out_dir;
  223. err = exofs_set_link(new_dir, new_de, new_page, old_inode);
  224. new_inode->i_ctime = CURRENT_TIME;
  225. if (dir_de)
  226. drop_nlink(new_inode);
  227. inode_dec_link_count(new_inode);
  228. if (err)
  229. goto out_dir;
  230. } else {
  231. err = exofs_add_link(new_dentry, old_inode);
  232. if (err)
  233. goto out_dir;
  234. if (dir_de)
  235. inode_inc_link_count(new_dir);
  236. }
  237. old_inode->i_ctime = CURRENT_TIME;
  238. exofs_delete_entry(old_de, old_page);
  239. mark_inode_dirty(old_inode);
  240. if (dir_de) {
  241. err = exofs_set_link(old_inode, dir_de, dir_page, new_dir);
  242. inode_dec_link_count(old_dir);
  243. if (err)
  244. goto out_dir;
  245. }
  246. return 0;
  247. out_dir:
  248. if (dir_de) {
  249. kunmap(dir_page);
  250. page_cache_release(dir_page);
  251. }
  252. out_old:
  253. kunmap(old_page);
  254. page_cache_release(old_page);
  255. out:
  256. return err;
  257. }
  258. const struct inode_operations exofs_dir_inode_operations = {
  259. .create = exofs_create,
  260. .lookup = exofs_lookup,
  261. .link = exofs_link,
  262. .unlink = exofs_unlink,
  263. .symlink = exofs_symlink,
  264. .mkdir = exofs_mkdir,
  265. .rmdir = exofs_rmdir,
  266. .mknod = exofs_mknod,
  267. .rename = exofs_rename,
  268. .setattr = exofs_setattr,
  269. };
  270. const struct inode_operations exofs_special_inode_operations = {
  271. .setattr = exofs_setattr,
  272. };