namei.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. unlock_new_inode(inode);
  39. d_instantiate(dentry, inode);
  40. return 0;
  41. }
  42. inode_dec_link_count(inode);
  43. unlock_new_inode(inode);
  44. iput(inode);
  45. return err;
  46. }
  47. static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
  48. {
  49. struct inode * inode = NULL;
  50. ino_t ino;
  51. if (dentry->d_name.len > UFS_MAXNAMLEN)
  52. return ERR_PTR(-ENAMETOOLONG);
  53. ino = ufs_inode_by_name(dir, &dentry->d_name);
  54. if (ino)
  55. inode = ufs_iget(dir->i_sb, ino);
  56. return d_splice_alias(inode, dentry);
  57. }
  58. /*
  59. * By the time this is called, we already have created
  60. * the directory cache entry for the new file, but it
  61. * is so far negative - it has no inode.
  62. *
  63. * If the create succeeds, we fill in the inode information
  64. * with d_instantiate().
  65. */
  66. static int ufs_create (struct inode * dir, struct dentry * dentry, umode_t mode,
  67. bool excl)
  68. {
  69. struct inode *inode;
  70. inode = ufs_new_inode(dir, mode);
  71. if (IS_ERR(inode))
  72. return PTR_ERR(inode);
  73. inode->i_op = &ufs_file_inode_operations;
  74. inode->i_fop = &ufs_file_operations;
  75. inode->i_mapping->a_ops = &ufs_aops;
  76. mark_inode_dirty(inode);
  77. return ufs_add_nondir(dentry, inode);
  78. }
  79. static int ufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  80. {
  81. struct inode *inode;
  82. int err;
  83. if (!old_valid_dev(rdev))
  84. return -EINVAL;
  85. inode = ufs_new_inode(dir, mode);
  86. err = PTR_ERR(inode);
  87. if (!IS_ERR(inode)) {
  88. init_special_inode(inode, mode, rdev);
  89. ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev);
  90. mark_inode_dirty(inode);
  91. err = ufs_add_nondir(dentry, inode);
  92. }
  93. return err;
  94. }
  95. static int ufs_symlink (struct inode * dir, struct dentry * dentry,
  96. const char * symname)
  97. {
  98. struct super_block * sb = dir->i_sb;
  99. int err;
  100. unsigned l = strlen(symname)+1;
  101. struct inode * inode;
  102. if (l > sb->s_blocksize)
  103. return -ENAMETOOLONG;
  104. inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
  105. err = PTR_ERR(inode);
  106. if (IS_ERR(inode))
  107. return err;
  108. if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) {
  109. /* slow symlink */
  110. inode->i_op = &ufs_symlink_inode_operations;
  111. inode->i_mapping->a_ops = &ufs_aops;
  112. err = page_symlink(inode, symname, l);
  113. if (err)
  114. goto out_fail;
  115. } else {
  116. /* fast symlink */
  117. inode->i_op = &ufs_fast_symlink_inode_operations;
  118. inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
  119. memcpy(inode->i_link, symname, l);
  120. inode->i_size = l-1;
  121. }
  122. mark_inode_dirty(inode);
  123. return ufs_add_nondir(dentry, inode);
  124. out_fail:
  125. inode_dec_link_count(inode);
  126. unlock_new_inode(inode);
  127. iput(inode);
  128. return err;
  129. }
  130. static int ufs_link (struct dentry * old_dentry, struct inode * dir,
  131. struct dentry *dentry)
  132. {
  133. struct inode *inode = d_inode(old_dentry);
  134. int error;
  135. inode->i_ctime = CURRENT_TIME_SEC;
  136. inode_inc_link_count(inode);
  137. ihold(inode);
  138. error = ufs_add_link(dentry, inode);
  139. if (error) {
  140. inode_dec_link_count(inode);
  141. iput(inode);
  142. } else
  143. d_instantiate(dentry, inode);
  144. return error;
  145. }
  146. static int ufs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
  147. {
  148. struct inode * inode;
  149. int err;
  150. inode_inc_link_count(dir);
  151. inode = ufs_new_inode(dir, S_IFDIR|mode);
  152. err = PTR_ERR(inode);
  153. if (IS_ERR(inode))
  154. goto out_dir;
  155. inode->i_op = &ufs_dir_inode_operations;
  156. inode->i_fop = &ufs_dir_operations;
  157. inode->i_mapping->a_ops = &ufs_aops;
  158. inode_inc_link_count(inode);
  159. err = ufs_make_empty(inode, dir);
  160. if (err)
  161. goto out_fail;
  162. err = ufs_add_link(dentry, inode);
  163. if (err)
  164. goto out_fail;
  165. unlock_new_inode(inode);
  166. d_instantiate(dentry, inode);
  167. return 0;
  168. out_fail:
  169. inode_dec_link_count(inode);
  170. inode_dec_link_count(inode);
  171. unlock_new_inode(inode);
  172. iput (inode);
  173. out_dir:
  174. inode_dec_link_count(dir);
  175. return err;
  176. }
  177. static int ufs_unlink(struct inode *dir, struct dentry *dentry)
  178. {
  179. struct inode * inode = d_inode(dentry);
  180. struct ufs_dir_entry *de;
  181. struct page *page;
  182. int err = -ENOENT;
  183. de = ufs_find_entry(dir, &dentry->d_name, &page);
  184. if (!de)
  185. goto out;
  186. err = ufs_delete_entry(dir, de, page);
  187. if (err)
  188. goto out;
  189. inode->i_ctime = dir->i_ctime;
  190. inode_dec_link_count(inode);
  191. err = 0;
  192. out:
  193. return err;
  194. }
  195. static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
  196. {
  197. struct inode * inode = d_inode(dentry);
  198. int err= -ENOTEMPTY;
  199. if (ufs_empty_dir (inode)) {
  200. err = ufs_unlink(dir, dentry);
  201. if (!err) {
  202. inode->i_size = 0;
  203. inode_dec_link_count(inode);
  204. inode_dec_link_count(dir);
  205. }
  206. }
  207. return err;
  208. }
  209. static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry,
  210. struct inode *new_dir, struct dentry *new_dentry)
  211. {
  212. struct inode *old_inode = d_inode(old_dentry);
  213. struct inode *new_inode = d_inode(new_dentry);
  214. struct page *dir_page = NULL;
  215. struct ufs_dir_entry * dir_de = NULL;
  216. struct page *old_page;
  217. struct ufs_dir_entry *old_de;
  218. int err = -ENOENT;
  219. old_de = ufs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  220. if (!old_de)
  221. goto out;
  222. if (S_ISDIR(old_inode->i_mode)) {
  223. err = -EIO;
  224. dir_de = ufs_dotdot(old_inode, &dir_page);
  225. if (!dir_de)
  226. goto out_old;
  227. }
  228. if (new_inode) {
  229. struct page *new_page;
  230. struct ufs_dir_entry *new_de;
  231. err = -ENOTEMPTY;
  232. if (dir_de && !ufs_empty_dir(new_inode))
  233. goto out_dir;
  234. err = -ENOENT;
  235. new_de = ufs_find_entry(new_dir, &new_dentry->d_name, &new_page);
  236. if (!new_de)
  237. goto out_dir;
  238. ufs_set_link(new_dir, new_de, new_page, old_inode, 1);
  239. new_inode->i_ctime = CURRENT_TIME_SEC;
  240. if (dir_de)
  241. drop_nlink(new_inode);
  242. inode_dec_link_count(new_inode);
  243. } else {
  244. err = ufs_add_link(new_dentry, old_inode);
  245. if (err)
  246. goto out_dir;
  247. if (dir_de)
  248. inode_inc_link_count(new_dir);
  249. }
  250. /*
  251. * Like most other Unix systems, set the ctime for inodes on a
  252. * rename.
  253. */
  254. old_inode->i_ctime = CURRENT_TIME_SEC;
  255. ufs_delete_entry(old_dir, old_de, old_page);
  256. mark_inode_dirty(old_inode);
  257. if (dir_de) {
  258. if (old_dir != new_dir)
  259. ufs_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  260. else {
  261. kunmap(dir_page);
  262. page_cache_release(dir_page);
  263. }
  264. inode_dec_link_count(old_dir);
  265. }
  266. return 0;
  267. out_dir:
  268. if (dir_de) {
  269. kunmap(dir_page);
  270. page_cache_release(dir_page);
  271. }
  272. out_old:
  273. kunmap(old_page);
  274. page_cache_release(old_page);
  275. out:
  276. return err;
  277. }
  278. const struct inode_operations ufs_dir_inode_operations = {
  279. .create = ufs_create,
  280. .lookup = ufs_lookup,
  281. .link = ufs_link,
  282. .unlink = ufs_unlink,
  283. .symlink = ufs_symlink,
  284. .mkdir = ufs_mkdir,
  285. .rmdir = ufs_rmdir,
  286. .mknod = ufs_mknod,
  287. .rename = ufs_rename,
  288. };