namei.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 = &page_symlink_inode_operations;
  110. inode_nohighmem(inode);
  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 = &simple_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(inode);
  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. d_instantiate_new(dentry, inode);
  166. return 0;
  167. out_fail:
  168. inode_dec_link_count(inode);
  169. inode_dec_link_count(inode);
  170. unlock_new_inode(inode);
  171. iput (inode);
  172. out_dir:
  173. inode_dec_link_count(dir);
  174. return err;
  175. }
  176. static int ufs_unlink(struct inode *dir, struct dentry *dentry)
  177. {
  178. struct inode * inode = d_inode(dentry);
  179. struct ufs_dir_entry *de;
  180. struct page *page;
  181. int err = -ENOENT;
  182. de = ufs_find_entry(dir, &dentry->d_name, &page);
  183. if (!de)
  184. goto out;
  185. err = ufs_delete_entry(dir, de, page);
  186. if (err)
  187. goto out;
  188. inode->i_ctime = dir->i_ctime;
  189. inode_dec_link_count(inode);
  190. err = 0;
  191. out:
  192. return err;
  193. }
  194. static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
  195. {
  196. struct inode * inode = d_inode(dentry);
  197. int err= -ENOTEMPTY;
  198. if (ufs_empty_dir (inode)) {
  199. err = ufs_unlink(dir, dentry);
  200. if (!err) {
  201. inode->i_size = 0;
  202. inode_dec_link_count(inode);
  203. inode_dec_link_count(dir);
  204. }
  205. }
  206. return err;
  207. }
  208. static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry,
  209. struct inode *new_dir, struct dentry *new_dentry,
  210. unsigned int flags)
  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. if (flags & ~RENAME_NOREPLACE)
  220. return -EINVAL;
  221. old_de = ufs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  222. if (!old_de)
  223. goto out;
  224. if (S_ISDIR(old_inode->i_mode)) {
  225. err = -EIO;
  226. dir_de = ufs_dotdot(old_inode, &dir_page);
  227. if (!dir_de)
  228. goto out_old;
  229. }
  230. if (new_inode) {
  231. struct page *new_page;
  232. struct ufs_dir_entry *new_de;
  233. err = -ENOTEMPTY;
  234. if (dir_de && !ufs_empty_dir(new_inode))
  235. goto out_dir;
  236. err = -ENOENT;
  237. new_de = ufs_find_entry(new_dir, &new_dentry->d_name, &new_page);
  238. if (!new_de)
  239. goto out_dir;
  240. ufs_set_link(new_dir, new_de, new_page, old_inode, 1);
  241. new_inode->i_ctime = current_time(new_inode);
  242. if (dir_de)
  243. drop_nlink(new_inode);
  244. inode_dec_link_count(new_inode);
  245. } else {
  246. err = ufs_add_link(new_dentry, old_inode);
  247. if (err)
  248. goto out_dir;
  249. if (dir_de)
  250. inode_inc_link_count(new_dir);
  251. }
  252. /*
  253. * Like most other Unix systems, set the ctime for inodes on a
  254. * rename.
  255. */
  256. old_inode->i_ctime = current_time(old_inode);
  257. ufs_delete_entry(old_dir, old_de, old_page);
  258. mark_inode_dirty(old_inode);
  259. if (dir_de) {
  260. if (old_dir != new_dir)
  261. ufs_set_link(old_inode, dir_de, dir_page, new_dir, 0);
  262. else {
  263. kunmap(dir_page);
  264. put_page(dir_page);
  265. }
  266. inode_dec_link_count(old_dir);
  267. }
  268. return 0;
  269. out_dir:
  270. if (dir_de) {
  271. kunmap(dir_page);
  272. put_page(dir_page);
  273. }
  274. out_old:
  275. kunmap(old_page);
  276. put_page(old_page);
  277. out:
  278. return err;
  279. }
  280. const struct inode_operations ufs_dir_inode_operations = {
  281. .create = ufs_create,
  282. .lookup = ufs_lookup,
  283. .link = ufs_link,
  284. .unlink = ufs_unlink,
  285. .symlink = ufs_symlink,
  286. .mkdir = ufs_mkdir,
  287. .rmdir = ufs_rmdir,
  288. .mknod = ufs_mknod,
  289. .rename = ufs_rename,
  290. };