namei.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * linux/fs/sysv/namei.c
  3. *
  4. * minix/namei.c
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * coh/namei.c
  8. * Copyright (C) 1993 Pascal Haible, Bruno Haible
  9. *
  10. * sysv/namei.c
  11. * Copyright (C) 1993 Bruno Haible
  12. * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
  13. */
  14. #include <linux/pagemap.h>
  15. #include "sysv.h"
  16. static int add_nondir(struct dentry *dentry, struct inode *inode)
  17. {
  18. int err = sysv_add_link(dentry, inode);
  19. if (!err) {
  20. d_instantiate(dentry, inode);
  21. return 0;
  22. }
  23. inode_dec_link_count(inode);
  24. iput(inode);
  25. return err;
  26. }
  27. static int sysv_hash(const struct dentry *dentry, const struct inode *inode,
  28. struct qstr *qstr)
  29. {
  30. /* Truncate the name in place, avoids having to define a compare
  31. function. */
  32. if (qstr->len > SYSV_NAMELEN) {
  33. qstr->len = SYSV_NAMELEN;
  34. qstr->hash = full_name_hash(qstr->name, qstr->len);
  35. }
  36. return 0;
  37. }
  38. const struct dentry_operations sysv_dentry_operations = {
  39. .d_hash = sysv_hash,
  40. };
  41. static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  42. {
  43. struct inode * inode = NULL;
  44. ino_t ino;
  45. if (dentry->d_name.len > SYSV_NAMELEN)
  46. return ERR_PTR(-ENAMETOOLONG);
  47. ino = sysv_inode_by_name(dentry);
  48. if (ino) {
  49. inode = sysv_iget(dir->i_sb, ino);
  50. if (IS_ERR(inode))
  51. return ERR_CAST(inode);
  52. }
  53. d_add(dentry, inode);
  54. return NULL;
  55. }
  56. static int sysv_mknod(struct inode * dir, struct dentry * dentry, umode_t mode, dev_t rdev)
  57. {
  58. struct inode * inode;
  59. int err;
  60. if (!old_valid_dev(rdev))
  61. return -EINVAL;
  62. inode = sysv_new_inode(dir, mode);
  63. err = PTR_ERR(inode);
  64. if (!IS_ERR(inode)) {
  65. sysv_set_inode(inode, rdev);
  66. mark_inode_dirty(inode);
  67. err = add_nondir(dentry, inode);
  68. }
  69. return err;
  70. }
  71. static int sysv_create(struct inode * dir, struct dentry * dentry, umode_t mode, struct nameidata *nd)
  72. {
  73. return sysv_mknod(dir, dentry, mode, 0);
  74. }
  75. static int sysv_symlink(struct inode * dir, struct dentry * dentry,
  76. const char * symname)
  77. {
  78. int err = -ENAMETOOLONG;
  79. int l = strlen(symname)+1;
  80. struct inode * inode;
  81. if (l > dir->i_sb->s_blocksize)
  82. goto out;
  83. inode = sysv_new_inode(dir, S_IFLNK|0777);
  84. err = PTR_ERR(inode);
  85. if (IS_ERR(inode))
  86. goto out;
  87. sysv_set_inode(inode, 0);
  88. err = page_symlink(inode, symname, l);
  89. if (err)
  90. goto out_fail;
  91. mark_inode_dirty(inode);
  92. err = add_nondir(dentry, inode);
  93. out:
  94. return err;
  95. out_fail:
  96. inode_dec_link_count(inode);
  97. iput(inode);
  98. goto out;
  99. }
  100. static int sysv_link(struct dentry * old_dentry, struct inode * dir,
  101. struct dentry * dentry)
  102. {
  103. struct inode *inode = old_dentry->d_inode;
  104. inode->i_ctime = CURRENT_TIME_SEC;
  105. inode_inc_link_count(inode);
  106. ihold(inode);
  107. return add_nondir(dentry, inode);
  108. }
  109. static int sysv_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
  110. {
  111. struct inode * inode;
  112. int err;
  113. inode_inc_link_count(dir);
  114. inode = sysv_new_inode(dir, S_IFDIR|mode);
  115. err = PTR_ERR(inode);
  116. if (IS_ERR(inode))
  117. goto out_dir;
  118. sysv_set_inode(inode, 0);
  119. inode_inc_link_count(inode);
  120. err = sysv_make_empty(inode, dir);
  121. if (err)
  122. goto out_fail;
  123. err = sysv_add_link(dentry, inode);
  124. if (err)
  125. goto out_fail;
  126. d_instantiate(dentry, inode);
  127. out:
  128. return err;
  129. out_fail:
  130. inode_dec_link_count(inode);
  131. inode_dec_link_count(inode);
  132. iput(inode);
  133. out_dir:
  134. inode_dec_link_count(dir);
  135. goto out;
  136. }
  137. static int sysv_unlink(struct inode * dir, struct dentry * dentry)
  138. {
  139. struct inode * inode = dentry->d_inode;
  140. struct page * page;
  141. struct sysv_dir_entry * de;
  142. int err = -ENOENT;
  143. de = sysv_find_entry(dentry, &page);
  144. if (!de)
  145. goto out;
  146. err = sysv_delete_entry (de, page);
  147. if (err)
  148. goto out;
  149. inode->i_ctime = dir->i_ctime;
  150. inode_dec_link_count(inode);
  151. out:
  152. return err;
  153. }
  154. static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
  155. {
  156. struct inode *inode = dentry->d_inode;
  157. int err = -ENOTEMPTY;
  158. if (sysv_empty_dir(inode)) {
  159. err = sysv_unlink(dir, dentry);
  160. if (!err) {
  161. inode->i_size = 0;
  162. inode_dec_link_count(inode);
  163. inode_dec_link_count(dir);
  164. }
  165. }
  166. return err;
  167. }
  168. /*
  169. * Anybody can rename anything with this: the permission checks are left to the
  170. * higher-level routines.
  171. */
  172. static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
  173. struct inode * new_dir, struct dentry * new_dentry)
  174. {
  175. struct inode * old_inode = old_dentry->d_inode;
  176. struct inode * new_inode = new_dentry->d_inode;
  177. struct page * dir_page = NULL;
  178. struct sysv_dir_entry * dir_de = NULL;
  179. struct page * old_page;
  180. struct sysv_dir_entry * old_de;
  181. int err = -ENOENT;
  182. old_de = sysv_find_entry(old_dentry, &old_page);
  183. if (!old_de)
  184. goto out;
  185. if (S_ISDIR(old_inode->i_mode)) {
  186. err = -EIO;
  187. dir_de = sysv_dotdot(old_inode, &dir_page);
  188. if (!dir_de)
  189. goto out_old;
  190. }
  191. if (new_inode) {
  192. struct page * new_page;
  193. struct sysv_dir_entry * new_de;
  194. err = -ENOTEMPTY;
  195. if (dir_de && !sysv_empty_dir(new_inode))
  196. goto out_dir;
  197. err = -ENOENT;
  198. new_de = sysv_find_entry(new_dentry, &new_page);
  199. if (!new_de)
  200. goto out_dir;
  201. sysv_set_link(new_de, new_page, old_inode);
  202. new_inode->i_ctime = CURRENT_TIME_SEC;
  203. if (dir_de)
  204. drop_nlink(new_inode);
  205. inode_dec_link_count(new_inode);
  206. } else {
  207. err = sysv_add_link(new_dentry, old_inode);
  208. if (err)
  209. goto out_dir;
  210. if (dir_de)
  211. inode_inc_link_count(new_dir);
  212. }
  213. sysv_delete_entry(old_de, old_page);
  214. mark_inode_dirty(old_inode);
  215. if (dir_de) {
  216. sysv_set_link(dir_de, dir_page, new_dir);
  217. inode_dec_link_count(old_dir);
  218. }
  219. return 0;
  220. out_dir:
  221. if (dir_de) {
  222. kunmap(dir_page);
  223. page_cache_release(dir_page);
  224. }
  225. out_old:
  226. kunmap(old_page);
  227. page_cache_release(old_page);
  228. out:
  229. return err;
  230. }
  231. /*
  232. * directories can handle most operations...
  233. */
  234. const struct inode_operations sysv_dir_inode_operations = {
  235. .create = sysv_create,
  236. .lookup = sysv_lookup,
  237. .link = sysv_link,
  238. .unlink = sysv_unlink,
  239. .symlink = sysv_symlink,
  240. .mkdir = sysv_mkdir,
  241. .rmdir = sysv_rmdir,
  242. .mknod = sysv_mknod,
  243. .rename = sysv_rename,
  244. .getattr = sysv_getattr,
  245. };