namei.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_nohighmem(inode);
  103. inode->i_mapping->a_ops = &exofs_aops;
  104. memset(oi->i_data, 0, sizeof(oi->i_data));
  105. err = page_symlink(inode, symname, l);
  106. if (err)
  107. goto out_fail;
  108. } else {
  109. /* fast symlink */
  110. inode->i_op = &simple_symlink_inode_operations;
  111. inode->i_link = (char *)oi->i_data;
  112. memcpy(oi->i_data, symname, l);
  113. inode->i_size = l-1;
  114. }
  115. mark_inode_dirty(inode);
  116. err = exofs_add_nondir(dentry, inode);
  117. out:
  118. return err;
  119. out_fail:
  120. inode_dec_link_count(inode);
  121. iput(inode);
  122. goto out;
  123. }
  124. static int exofs_link(struct dentry *old_dentry, struct inode *dir,
  125. struct dentry *dentry)
  126. {
  127. struct inode *inode = d_inode(old_dentry);
  128. inode->i_ctime = current_time(inode);
  129. inode_inc_link_count(inode);
  130. ihold(inode);
  131. return exofs_add_nondir(dentry, inode);
  132. }
  133. static int exofs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  134. {
  135. struct inode *inode;
  136. int err;
  137. inode_inc_link_count(dir);
  138. inode = exofs_new_inode(dir, S_IFDIR | mode);
  139. err = PTR_ERR(inode);
  140. if (IS_ERR(inode))
  141. goto out_dir;
  142. inode->i_op = &exofs_dir_inode_operations;
  143. inode->i_fop = &exofs_dir_operations;
  144. inode->i_mapping->a_ops = &exofs_aops;
  145. inode_inc_link_count(inode);
  146. err = exofs_make_empty(inode, dir);
  147. if (err)
  148. goto out_fail;
  149. err = exofs_add_link(dentry, inode);
  150. if (err)
  151. goto out_fail;
  152. d_instantiate(dentry, inode);
  153. out:
  154. return err;
  155. out_fail:
  156. inode_dec_link_count(inode);
  157. inode_dec_link_count(inode);
  158. iput(inode);
  159. out_dir:
  160. inode_dec_link_count(dir);
  161. goto out;
  162. }
  163. static int exofs_unlink(struct inode *dir, struct dentry *dentry)
  164. {
  165. struct inode *inode = d_inode(dentry);
  166. struct exofs_dir_entry *de;
  167. struct page *page;
  168. int err = -ENOENT;
  169. de = exofs_find_entry(dir, dentry, &page);
  170. if (!de)
  171. goto out;
  172. err = exofs_delete_entry(de, page);
  173. if (err)
  174. goto out;
  175. inode->i_ctime = dir->i_ctime;
  176. inode_dec_link_count(inode);
  177. err = 0;
  178. out:
  179. return err;
  180. }
  181. static int exofs_rmdir(struct inode *dir, struct dentry *dentry)
  182. {
  183. struct inode *inode = d_inode(dentry);
  184. int err = -ENOTEMPTY;
  185. if (exofs_empty_dir(inode)) {
  186. err = exofs_unlink(dir, dentry);
  187. if (!err) {
  188. inode->i_size = 0;
  189. inode_dec_link_count(inode);
  190. inode_dec_link_count(dir);
  191. }
  192. }
  193. return err;
  194. }
  195. static int exofs_rename(struct inode *old_dir, struct dentry *old_dentry,
  196. struct inode *new_dir, struct dentry *new_dentry,
  197. unsigned int flags)
  198. {
  199. struct inode *old_inode = d_inode(old_dentry);
  200. struct inode *new_inode = d_inode(new_dentry);
  201. struct page *dir_page = NULL;
  202. struct exofs_dir_entry *dir_de = NULL;
  203. struct page *old_page;
  204. struct exofs_dir_entry *old_de;
  205. int err = -ENOENT;
  206. if (flags & ~RENAME_NOREPLACE)
  207. return -EINVAL;
  208. old_de = exofs_find_entry(old_dir, old_dentry, &old_page);
  209. if (!old_de)
  210. goto out;
  211. if (S_ISDIR(old_inode->i_mode)) {
  212. err = -EIO;
  213. dir_de = exofs_dotdot(old_inode, &dir_page);
  214. if (!dir_de)
  215. goto out_old;
  216. }
  217. if (new_inode) {
  218. struct page *new_page;
  219. struct exofs_dir_entry *new_de;
  220. err = -ENOTEMPTY;
  221. if (dir_de && !exofs_empty_dir(new_inode))
  222. goto out_dir;
  223. err = -ENOENT;
  224. new_de = exofs_find_entry(new_dir, new_dentry, &new_page);
  225. if (!new_de)
  226. goto out_dir;
  227. err = exofs_set_link(new_dir, new_de, new_page, old_inode);
  228. new_inode->i_ctime = current_time(new_inode);
  229. if (dir_de)
  230. drop_nlink(new_inode);
  231. inode_dec_link_count(new_inode);
  232. if (err)
  233. goto out_dir;
  234. } else {
  235. err = exofs_add_link(new_dentry, old_inode);
  236. if (err)
  237. goto out_dir;
  238. if (dir_de)
  239. inode_inc_link_count(new_dir);
  240. }
  241. old_inode->i_ctime = current_time(old_inode);
  242. exofs_delete_entry(old_de, old_page);
  243. mark_inode_dirty(old_inode);
  244. if (dir_de) {
  245. err = exofs_set_link(old_inode, dir_de, dir_page, new_dir);
  246. inode_dec_link_count(old_dir);
  247. if (err)
  248. goto out_dir;
  249. }
  250. return 0;
  251. out_dir:
  252. if (dir_de) {
  253. kunmap(dir_page);
  254. put_page(dir_page);
  255. }
  256. out_old:
  257. kunmap(old_page);
  258. put_page(old_page);
  259. out:
  260. return err;
  261. }
  262. const struct inode_operations exofs_dir_inode_operations = {
  263. .create = exofs_create,
  264. .lookup = exofs_lookup,
  265. .link = exofs_link,
  266. .unlink = exofs_unlink,
  267. .symlink = exofs_symlink,
  268. .mkdir = exofs_mkdir,
  269. .rmdir = exofs_rmdir,
  270. .mknod = exofs_mknod,
  271. .rename = exofs_rename,
  272. .setattr = exofs_setattr,
  273. };
  274. const struct inode_operations exofs_special_inode_operations = {
  275. .setattr = exofs_setattr,
  276. };