namei.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(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_SEC;
  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. {
  174. struct inode * old_inode = d_inode(old_dentry);
  175. struct inode * new_inode = d_inode(new_dentry);
  176. struct page * dir_page = NULL;
  177. struct sysv_dir_entry * dir_de = NULL;
  178. struct page * old_page;
  179. struct sysv_dir_entry * old_de;
  180. int err = -ENOENT;
  181. old_de = sysv_find_entry(old_dentry, &old_page);
  182. if (!old_de)
  183. goto out;
  184. if (S_ISDIR(old_inode->i_mode)) {
  185. err = -EIO;
  186. dir_de = sysv_dotdot(old_inode, &dir_page);
  187. if (!dir_de)
  188. goto out_old;
  189. }
  190. if (new_inode) {
  191. struct page * new_page;
  192. struct sysv_dir_entry * new_de;
  193. err = -ENOTEMPTY;
  194. if (dir_de && !sysv_empty_dir(new_inode))
  195. goto out_dir;
  196. err = -ENOENT;
  197. new_de = sysv_find_entry(new_dentry, &new_page);
  198. if (!new_de)
  199. goto out_dir;
  200. sysv_set_link(new_de, new_page, old_inode);
  201. new_inode->i_ctime = CURRENT_TIME_SEC;
  202. if (dir_de)
  203. drop_nlink(new_inode);
  204. inode_dec_link_count(new_inode);
  205. } else {
  206. err = sysv_add_link(new_dentry, old_inode);
  207. if (err)
  208. goto out_dir;
  209. if (dir_de)
  210. inode_inc_link_count(new_dir);
  211. }
  212. sysv_delete_entry(old_de, old_page);
  213. mark_inode_dirty(old_inode);
  214. if (dir_de) {
  215. sysv_set_link(dir_de, dir_page, new_dir);
  216. inode_dec_link_count(old_dir);
  217. }
  218. return 0;
  219. out_dir:
  220. if (dir_de) {
  221. kunmap(dir_page);
  222. page_cache_release(dir_page);
  223. }
  224. out_old:
  225. kunmap(old_page);
  226. page_cache_release(old_page);
  227. out:
  228. return err;
  229. }
  230. /*
  231. * directories can handle most operations...
  232. */
  233. const struct inode_operations sysv_dir_inode_operations = {
  234. .create = sysv_create,
  235. .lookup = sysv_lookup,
  236. .link = sysv_link,
  237. .unlink = sysv_unlink,
  238. .symlink = sysv_symlink,
  239. .mkdir = sysv_mkdir,
  240. .rmdir = sysv_rmdir,
  241. .mknod = sysv_mknod,
  242. .rename = sysv_rename,
  243. .getattr = sysv_getattr,
  244. };