namei.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * linux/fs/minix/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include "minix.h"
  7. static int add_nondir(struct dentry *dentry, struct inode *inode)
  8. {
  9. int err = minix_add_link(dentry, inode);
  10. if (!err) {
  11. d_instantiate(dentry, inode);
  12. return 0;
  13. }
  14. inode_dec_link_count(inode);
  15. iput(inode);
  16. return err;
  17. }
  18. static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
  19. {
  20. struct inode * inode = NULL;
  21. ino_t ino;
  22. if (dentry->d_name.len > minix_sb(dir->i_sb)->s_namelen)
  23. return ERR_PTR(-ENAMETOOLONG);
  24. ino = minix_inode_by_name(dentry);
  25. if (ino) {
  26. inode = minix_iget(dir->i_sb, ino);
  27. if (IS_ERR(inode))
  28. return ERR_CAST(inode);
  29. }
  30. d_add(dentry, inode);
  31. return NULL;
  32. }
  33. static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  34. {
  35. int error;
  36. struct inode *inode;
  37. if (!old_valid_dev(rdev))
  38. return -EINVAL;
  39. inode = minix_new_inode(dir, mode, &error);
  40. if (inode) {
  41. minix_set_inode(inode, rdev);
  42. mark_inode_dirty(inode);
  43. error = add_nondir(dentry, inode);
  44. }
  45. return error;
  46. }
  47. static int minix_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  48. {
  49. int error;
  50. struct inode *inode = minix_new_inode(dir, mode, &error);
  51. if (inode) {
  52. minix_set_inode(inode, 0);
  53. mark_inode_dirty(inode);
  54. d_tmpfile(dentry, inode);
  55. }
  56. return error;
  57. }
  58. static int minix_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  59. bool excl)
  60. {
  61. return minix_mknod(dir, dentry, mode, 0);
  62. }
  63. static int minix_symlink(struct inode * dir, struct dentry *dentry,
  64. const char * symname)
  65. {
  66. int err = -ENAMETOOLONG;
  67. int i = strlen(symname)+1;
  68. struct inode * inode;
  69. if (i > dir->i_sb->s_blocksize)
  70. goto out;
  71. inode = minix_new_inode(dir, S_IFLNK | 0777, &err);
  72. if (!inode)
  73. goto out;
  74. minix_set_inode(inode, 0);
  75. err = page_symlink(inode, symname, i);
  76. if (err)
  77. goto out_fail;
  78. err = add_nondir(dentry, inode);
  79. out:
  80. return err;
  81. out_fail:
  82. inode_dec_link_count(inode);
  83. iput(inode);
  84. goto out;
  85. }
  86. static int minix_link(struct dentry * old_dentry, struct inode * dir,
  87. struct dentry *dentry)
  88. {
  89. struct inode *inode = d_inode(old_dentry);
  90. inode->i_ctime = CURRENT_TIME_SEC;
  91. inode_inc_link_count(inode);
  92. ihold(inode);
  93. return add_nondir(dentry, inode);
  94. }
  95. static int minix_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
  96. {
  97. struct inode * inode;
  98. int err;
  99. inode_inc_link_count(dir);
  100. inode = minix_new_inode(dir, S_IFDIR | mode, &err);
  101. if (!inode)
  102. goto out_dir;
  103. minix_set_inode(inode, 0);
  104. inode_inc_link_count(inode);
  105. err = minix_make_empty(inode, dir);
  106. if (err)
  107. goto out_fail;
  108. err = minix_add_link(dentry, inode);
  109. if (err)
  110. goto out_fail;
  111. d_instantiate(dentry, inode);
  112. out:
  113. return err;
  114. out_fail:
  115. inode_dec_link_count(inode);
  116. inode_dec_link_count(inode);
  117. iput(inode);
  118. out_dir:
  119. inode_dec_link_count(dir);
  120. goto out;
  121. }
  122. static int minix_unlink(struct inode * dir, struct dentry *dentry)
  123. {
  124. int err = -ENOENT;
  125. struct inode * inode = d_inode(dentry);
  126. struct page * page;
  127. struct minix_dir_entry * de;
  128. de = minix_find_entry(dentry, &page);
  129. if (!de)
  130. goto end_unlink;
  131. err = minix_delete_entry(de, page);
  132. if (err)
  133. goto end_unlink;
  134. inode->i_ctime = dir->i_ctime;
  135. inode_dec_link_count(inode);
  136. end_unlink:
  137. return err;
  138. }
  139. static int minix_rmdir(struct inode * dir, struct dentry *dentry)
  140. {
  141. struct inode * inode = d_inode(dentry);
  142. int err = -ENOTEMPTY;
  143. if (minix_empty_dir(inode)) {
  144. err = minix_unlink(dir, dentry);
  145. if (!err) {
  146. inode_dec_link_count(dir);
  147. inode_dec_link_count(inode);
  148. }
  149. }
  150. return err;
  151. }
  152. static int minix_rename(struct inode * old_dir, struct dentry *old_dentry,
  153. struct inode * new_dir, struct dentry *new_dentry)
  154. {
  155. struct inode * old_inode = d_inode(old_dentry);
  156. struct inode * new_inode = d_inode(new_dentry);
  157. struct page * dir_page = NULL;
  158. struct minix_dir_entry * dir_de = NULL;
  159. struct page * old_page;
  160. struct minix_dir_entry * old_de;
  161. int err = -ENOENT;
  162. old_de = minix_find_entry(old_dentry, &old_page);
  163. if (!old_de)
  164. goto out;
  165. if (S_ISDIR(old_inode->i_mode)) {
  166. err = -EIO;
  167. dir_de = minix_dotdot(old_inode, &dir_page);
  168. if (!dir_de)
  169. goto out_old;
  170. }
  171. if (new_inode) {
  172. struct page * new_page;
  173. struct minix_dir_entry * new_de;
  174. err = -ENOTEMPTY;
  175. if (dir_de && !minix_empty_dir(new_inode))
  176. goto out_dir;
  177. err = -ENOENT;
  178. new_de = minix_find_entry(new_dentry, &new_page);
  179. if (!new_de)
  180. goto out_dir;
  181. minix_set_link(new_de, new_page, old_inode);
  182. new_inode->i_ctime = CURRENT_TIME_SEC;
  183. if (dir_de)
  184. drop_nlink(new_inode);
  185. inode_dec_link_count(new_inode);
  186. } else {
  187. err = minix_add_link(new_dentry, old_inode);
  188. if (err)
  189. goto out_dir;
  190. if (dir_de)
  191. inode_inc_link_count(new_dir);
  192. }
  193. minix_delete_entry(old_de, old_page);
  194. mark_inode_dirty(old_inode);
  195. if (dir_de) {
  196. minix_set_link(dir_de, dir_page, new_dir);
  197. inode_dec_link_count(old_dir);
  198. }
  199. return 0;
  200. out_dir:
  201. if (dir_de) {
  202. kunmap(dir_page);
  203. page_cache_release(dir_page);
  204. }
  205. out_old:
  206. kunmap(old_page);
  207. page_cache_release(old_page);
  208. out:
  209. return err;
  210. }
  211. /*
  212. * directories can handle most operations...
  213. */
  214. const struct inode_operations minix_dir_inode_operations = {
  215. .create = minix_create,
  216. .lookup = minix_lookup,
  217. .link = minix_link,
  218. .unlink = minix_unlink,
  219. .symlink = minix_symlink,
  220. .mkdir = minix_mkdir,
  221. .rmdir = minix_rmdir,
  222. .mknod = minix_mknod,
  223. .rename = minix_rename,
  224. .getattr = minix_getattr,
  225. .tmpfile = minix_tmpfile,
  226. };