namei.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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(inode);
  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. unsigned int flags)
  155. {
  156. struct inode * old_inode = d_inode(old_dentry);
  157. struct inode * new_inode = d_inode(new_dentry);
  158. struct page * dir_page = NULL;
  159. struct minix_dir_entry * dir_de = NULL;
  160. struct page * old_page;
  161. struct minix_dir_entry * old_de;
  162. int err = -ENOENT;
  163. if (flags & ~RENAME_NOREPLACE)
  164. return -EINVAL;
  165. old_de = minix_find_entry(old_dentry, &old_page);
  166. if (!old_de)
  167. goto out;
  168. if (S_ISDIR(old_inode->i_mode)) {
  169. err = -EIO;
  170. dir_de = minix_dotdot(old_inode, &dir_page);
  171. if (!dir_de)
  172. goto out_old;
  173. }
  174. if (new_inode) {
  175. struct page * new_page;
  176. struct minix_dir_entry * new_de;
  177. err = -ENOTEMPTY;
  178. if (dir_de && !minix_empty_dir(new_inode))
  179. goto out_dir;
  180. err = -ENOENT;
  181. new_de = minix_find_entry(new_dentry, &new_page);
  182. if (!new_de)
  183. goto out_dir;
  184. minix_set_link(new_de, new_page, old_inode);
  185. new_inode->i_ctime = current_time(new_inode);
  186. if (dir_de)
  187. drop_nlink(new_inode);
  188. inode_dec_link_count(new_inode);
  189. } else {
  190. err = minix_add_link(new_dentry, old_inode);
  191. if (err)
  192. goto out_dir;
  193. if (dir_de)
  194. inode_inc_link_count(new_dir);
  195. }
  196. minix_delete_entry(old_de, old_page);
  197. mark_inode_dirty(old_inode);
  198. if (dir_de) {
  199. minix_set_link(dir_de, dir_page, new_dir);
  200. inode_dec_link_count(old_dir);
  201. }
  202. return 0;
  203. out_dir:
  204. if (dir_de) {
  205. kunmap(dir_page);
  206. put_page(dir_page);
  207. }
  208. out_old:
  209. kunmap(old_page);
  210. put_page(old_page);
  211. out:
  212. return err;
  213. }
  214. /*
  215. * directories can handle most operations...
  216. */
  217. const struct inode_operations minix_dir_inode_operations = {
  218. .create = minix_create,
  219. .lookup = minix_lookup,
  220. .link = minix_link,
  221. .unlink = minix_unlink,
  222. .symlink = minix_symlink,
  223. .mkdir = minix_mkdir,
  224. .rmdir = minix_rmdir,
  225. .mknod = minix_mknod,
  226. .rename = minix_rename,
  227. .getattr = minix_getattr,
  228. .tmpfile = minix_tmpfile,
  229. };