namei.c 7.3 KB

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