namei.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ufs/namei.c
  4. *
  5. * Migration to usage of "page cache" on May 2006 by
  6. * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
  7. *
  8. * Copyright (C) 1998
  9. * Daniel Pirkl <daniel.pirkl@email.cz>
  10. * Charles University, Faculty of Mathematics and Physics
  11. *
  12. * from
  13. *
  14. * linux/fs/ext2/namei.c
  15. *
  16. * Copyright (C) 1992, 1993, 1994, 1995
  17. * Remy Card (card@masi.ibp.fr)
  18. * Laboratoire MASI - Institut Blaise Pascal
  19. * Universite Pierre et Marie Curie (Paris VI)
  20. *
  21. * from
  22. *
  23. * linux/fs/minix/namei.c
  24. *
  25. * Copyright (C) 1991, 1992 Linus Torvalds
  26. *
  27. * Big-endian to little-endian byte-swapping/bitmaps by
  28. * David S. Miller (davem@caip.rutgers.edu), 1995
  29. */
  30. #include <linux/time.h>
  31. #include <linux/fs.h>
  32. #include "ufs_fs.h"
  33. #include "ufs.h"
  34. #include "util.h"
  35. static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
  36. {
  37. int err = ufs_add_link(dentry, inode);
  38. if (!err) {
  39. d_instantiate_new(dentry, inode);
  40. return 0;
  41. }
  42. inode_dec_link_count(inode);
  43. discard_new_inode(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. discard_new_inode(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(inode);
  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. discard_new_inode(inode);
  170. out_dir:
  171. inode_dec_link_count(dir);
  172. return err;
  173. }
  174. static int ufs_unlink(struct inode *dir, struct dentry *dentry)
  175. {
  176. struct inode * inode = d_inode(dentry);
  177. struct ufs_dir_entry *de;
  178. struct page *page;
  179. int err = -ENOENT;
  180. de = ufs_find_entry(dir, &dentry->d_name, &page);
  181. if (!de)
  182. goto out;
  183. err = ufs_delete_entry(dir, de, page);
  184. if (err)
  185. goto out;
  186. inode->i_ctime = dir->i_ctime;
  187. inode_dec_link_count(inode);
  188. err = 0;
  189. out:
  190. return err;
  191. }
  192. static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
  193. {
  194. struct inode * inode = d_inode(dentry);
  195. int err= -ENOTEMPTY;
  196. if (ufs_empty_dir (inode)) {
  197. err = ufs_unlink(dir, dentry);
  198. if (!err) {
  199. inode->i_size = 0;
  200. inode_dec_link_count(inode);
  201. inode_dec_link_count(dir);
  202. }
  203. }
  204. return err;
  205. }
  206. static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry,
  207. struct inode *new_dir, struct dentry *new_dentry,
  208. unsigned int flags)
  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. if (flags & ~RENAME_NOREPLACE)
  218. return -EINVAL;
  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(new_inode);
  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(old_inode);
  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. put_page(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. put_page(dir_page);
  271. }
  272. out_old:
  273. kunmap(old_page);
  274. put_page(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. };