namei.c 5.9 KB

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