namei.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * linux/fs/ufs/namei.c
  3. *
  4. * Migration to usage of "page cache" on May 2006 by
  5. * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
  6. *
  7. * Copyright (C) 1998
  8. * Daniel Pirkl <daniel.pirkl@email.cz>
  9. * Charles University, Faculty of Mathematics and Physics
  10. *
  11. * from
  12. *
  13. * linux/fs/ext2/namei.c
  14. *
  15. * Copyright (C) 1992, 1993, 1994, 1995
  16. * Remy Card (card@masi.ibp.fr)
  17. * Laboratoire MASI - Institut Blaise Pascal
  18. * Universite Pierre et Marie Curie (Paris VI)
  19. *
  20. * from
  21. *
  22. * linux/fs/minix/namei.c
  23. *
  24. * Copyright (C) 1991, 1992 Linus Torvalds
  25. *
  26. * Big-endian to little-endian byte-swapping/bitmaps by
  27. * David S. Miller (davem@caip.rutgers.edu), 1995
  28. */
  29. #include <linux/time.h>
  30. #include <linux/fs.h>
  31. #include "ufs_fs.h"
  32. #include "ufs.h"
  33. #include "util.h"
  34. static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
  35. {
  36. int err = ufs_add_link(dentry, inode);
  37. if (!err) {
  38. d_instantiate_new(dentry, inode);
  39. return 0;
  40. }
  41. inode_dec_link_count(inode);
  42. unlock_new_inode(inode);
  43. iput(inode);
  44. return err;
  45. }
  46. static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
  47. {
  48. struct inode * inode = NULL;
  49. ino_t ino;
  50. if (dentry->d_name.len > UFS_MAXNAMLEN)
  51. return ERR_PTR(-ENAMETOOLONG);
  52. ino = ufs_inode_by_name(dir, &dentry->d_name);
  53. if (ino)
  54. inode = ufs_iget(dir->i_sb, ino);
  55. return d_splice_alias(inode, dentry);
  56. }
  57. /*
  58. * By the time this is called, we already have created
  59. * the directory cache entry for the new file, but it
  60. * is so far negative - it has no inode.
  61. *
  62. * If the create succeeds, we fill in the inode information
  63. * with d_instantiate().
  64. */
  65. static int ufs_create (struct inode * dir, struct dentry * dentry, umode_t mode,
  66. bool excl)
  67. {
  68. struct inode *inode;
  69. inode = ufs_new_inode(dir, mode);
  70. if (IS_ERR(inode))
  71. return PTR_ERR(inode);
  72. inode->i_op = &ufs_file_inode_operations;
  73. inode->i_fop = &ufs_file_operations;
  74. inode->i_mapping->a_ops = &ufs_aops;
  75. mark_inode_dirty(inode);
  76. return ufs_add_nondir(dentry, inode);
  77. }
  78. static int ufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  79. {
  80. struct inode *inode;
  81. int err;
  82. if (!old_valid_dev(rdev))
  83. return -EINVAL;
  84. inode = ufs_new_inode(dir, mode);
  85. err = PTR_ERR(inode);
  86. if (!IS_ERR(inode)) {
  87. init_special_inode(inode, mode, rdev);
  88. ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev);
  89. mark_inode_dirty(inode);
  90. err = ufs_add_nondir(dentry, inode);
  91. }
  92. return err;
  93. }
  94. static int ufs_symlink (struct inode * dir, struct dentry * dentry,
  95. const char * symname)
  96. {
  97. struct super_block * sb = dir->i_sb;
  98. int err;
  99. unsigned l = strlen(symname)+1;
  100. struct inode * inode;
  101. if (l > sb->s_blocksize)
  102. return -ENAMETOOLONG;
  103. inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
  104. err = PTR_ERR(inode);
  105. if (IS_ERR(inode))
  106. return err;
  107. if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) {
  108. /* slow symlink */
  109. inode->i_op = &ufs_symlink_inode_operations;
  110. inode->i_mapping->a_ops = &ufs_aops;
  111. err = page_symlink(inode, symname, l);
  112. if (err)
  113. goto out_fail;
  114. } else {
  115. /* fast symlink */
  116. inode->i_op = &ufs_fast_symlink_inode_operations;
  117. inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
  118. memcpy(inode->i_link, symname, l);
  119. inode->i_size = l-1;
  120. }
  121. mark_inode_dirty(inode);
  122. return ufs_add_nondir(dentry, inode);
  123. out_fail:
  124. inode_dec_link_count(inode);
  125. unlock_new_inode(inode);
  126. iput(inode);
  127. return err;
  128. }
  129. static int ufs_link (struct dentry * old_dentry, struct inode * dir,
  130. struct dentry *dentry)
  131. {
  132. struct inode *inode = d_inode(old_dentry);
  133. int error;
  134. inode->i_ctime = CURRENT_TIME_SEC;
  135. inode_inc_link_count(inode);
  136. ihold(inode);
  137. error = ufs_add_link(dentry, inode);
  138. if (error) {
  139. inode_dec_link_count(inode);
  140. iput(inode);
  141. } else
  142. d_instantiate(dentry, inode);
  143. return error;
  144. }
  145. static int ufs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
  146. {
  147. struct inode * inode;
  148. int err;
  149. inode_inc_link_count(dir);
  150. inode = ufs_new_inode(dir, S_IFDIR|mode);
  151. err = PTR_ERR(inode);
  152. if (IS_ERR(inode))
  153. goto out_dir;
  154. inode->i_op = &ufs_dir_inode_operations;
  155. inode->i_fop = &ufs_dir_operations;
  156. inode->i_mapping->a_ops = &ufs_aops;
  157. inode_inc_link_count(inode);
  158. err = ufs_make_empty(inode, dir);
  159. if (err)
  160. goto out_fail;
  161. err = ufs_add_link(dentry, inode);
  162. if (err)
  163. goto out_fail;
  164. d_instantiate_new(dentry, inode);
  165. return 0;
  166. out_fail:
  167. inode_dec_link_count(inode);
  168. inode_dec_link_count(inode);
  169. unlock_new_inode(inode);
  170. iput (inode);
  171. out_dir:
  172. inode_dec_link_count(dir);
  173. return err;
  174. }
  175. static int ufs_unlink(struct inode *dir, struct dentry *dentry)
  176. {
  177. struct inode * inode = d_inode(dentry);
  178. struct ufs_dir_entry *de;
  179. struct page *page;
  180. int err = -ENOENT;
  181. de = ufs_find_entry(dir, &dentry->d_name, &page);
  182. if (!de)
  183. goto out;
  184. err = ufs_delete_entry(dir, de, page);
  185. if (err)
  186. goto out;
  187. inode->i_ctime = dir->i_ctime;
  188. inode_dec_link_count(inode);
  189. err = 0;
  190. out:
  191. return err;
  192. }
  193. static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
  194. {
  195. struct inode * inode = d_inode(dentry);
  196. int err= -ENOTEMPTY;
  197. if (ufs_empty_dir (inode)) {
  198. err = ufs_unlink(dir, dentry);
  199. if (!err) {
  200. inode->i_size = 0;
  201. inode_dec_link_count(inode);
  202. inode_dec_link_count(dir);
  203. }
  204. }
  205. return err;
  206. }
  207. static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry,
  208. struct inode *new_dir, struct dentry *new_dentry)
  209. {
  210. struct inode *old_inode = d_inode(old_dentry);
  211. struct inode *new_inode = d_inode(new_dentry);
  212. struct page *dir_page = NULL;
  213. struct ufs_dir_entry * dir_de = NULL;
  214. struct page *old_page;
  215. struct ufs_dir_entry *old_de;
  216. int err = -ENOENT;
  217. old_de = ufs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  218. if (!old_de)
  219. goto out;
  220. if (S_ISDIR(old_inode->i_mode)) {
  221. err = -EIO;
  222. dir_de = ufs_dotdot(old_inode, &dir_page);
  223. if (!dir_de)
  224. goto out_old;
  225. }
  226. if (new_inode) {
  227. struct page *new_page;
  228. struct ufs_dir_entry *new_de;
  229. err = -ENOTEMPTY;
  230. if (dir_de && !ufs_empty_dir(new_inode))
  231. goto out_dir;
  232. err = -ENOENT;
  233. new_de = ufs_find_entry(new_dir, &new_dentry->d_name, &new_page);
  234. if (!new_de)
  235. goto out_dir;
  236. ufs_set_link(new_dir, new_de, new_page, old_inode, 1);
  237. new_inode->i_ctime = CURRENT_TIME_SEC;
  238. if (dir_de)
  239. drop_nlink(new_inode);
  240. inode_dec_link_count(new_inode);
  241. } else {
  242. err = ufs_add_link(new_dentry, old_inode);
  243. if (err)
  244. goto out_dir;
  245. if (dir_de)
  246. inode_inc_link_count(new_dir);
  247. }
  248. /*
  249. * Like most other Unix systems, set the ctime for inodes on a
  250. * rename.
  251. */
  252. old_inode->i_ctime = CURRENT_TIME_SEC;
  253. ufs_delete_entry(old_dir, old_de, old_page);
  254. mark_inode_dirty(old_inode);
  255. if (dir_de) {
  256. if (old_dir != new_dir)
  257. ufs_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  258. else {
  259. kunmap(dir_page);
  260. page_cache_release(dir_page);
  261. }
  262. inode_dec_link_count(old_dir);
  263. }
  264. return 0;
  265. out_dir:
  266. if (dir_de) {
  267. kunmap(dir_page);
  268. page_cache_release(dir_page);
  269. }
  270. out_old:
  271. kunmap(old_page);
  272. page_cache_release(old_page);
  273. out:
  274. return err;
  275. }
  276. const struct inode_operations ufs_dir_inode_operations = {
  277. .create = ufs_create,
  278. .lookup = ufs_lookup,
  279. .link = ufs_link,
  280. .unlink = ufs_unlink,
  281. .symlink = ufs_symlink,
  282. .mkdir = ufs_mkdir,
  283. .rmdir = ufs_rmdir,
  284. .mknod = ufs_mknod,
  285. .rename = ufs_rename,
  286. };