namei.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/minix/namei.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include "minix.h"
  8. static int add_nondir(struct dentry *dentry, struct inode *inode)
  9. {
  10. int err = minix_add_link(dentry, inode);
  11. if (!err) {
  12. d_instantiate(dentry, inode);
  13. return 0;
  14. }
  15. inode_dec_link_count(inode);
  16. iput(inode);
  17. return err;
  18. }
  19. static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
  20. {
  21. struct inode * inode = NULL;
  22. ino_t ino;
  23. if (dentry->d_name.len > minix_sb(dir->i_sb)->s_namelen)
  24. return ERR_PTR(-ENAMETOOLONG);
  25. ino = minix_inode_by_name(dentry);
  26. if (ino)
  27. inode = minix_iget(dir->i_sb, ino);
  28. return d_splice_alias(inode, dentry);
  29. }
  30. static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  31. {
  32. int error;
  33. struct inode *inode;
  34. if (!old_valid_dev(rdev))
  35. return -EINVAL;
  36. inode = minix_new_inode(dir, mode, &error);
  37. if (inode) {
  38. minix_set_inode(inode, rdev);
  39. mark_inode_dirty(inode);
  40. error = add_nondir(dentry, inode);
  41. }
  42. return error;
  43. }
  44. static int minix_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  45. {
  46. int error;
  47. struct inode *inode = minix_new_inode(dir, mode, &error);
  48. if (inode) {
  49. minix_set_inode(inode, 0);
  50. mark_inode_dirty(inode);
  51. d_tmpfile(dentry, inode);
  52. }
  53. return error;
  54. }
  55. static int minix_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  56. bool excl)
  57. {
  58. return minix_mknod(dir, dentry, mode, 0);
  59. }
  60. static int minix_symlink(struct inode * dir, struct dentry *dentry,
  61. const char * symname)
  62. {
  63. int err = -ENAMETOOLONG;
  64. int i = strlen(symname)+1;
  65. struct inode * inode;
  66. if (i > dir->i_sb->s_blocksize)
  67. goto out;
  68. inode = minix_new_inode(dir, S_IFLNK | 0777, &err);
  69. if (!inode)
  70. goto out;
  71. minix_set_inode(inode, 0);
  72. err = page_symlink(inode, symname, i);
  73. if (err)
  74. goto out_fail;
  75. err = add_nondir(dentry, inode);
  76. out:
  77. return err;
  78. out_fail:
  79. inode_dec_link_count(inode);
  80. iput(inode);
  81. goto out;
  82. }
  83. static int minix_link(struct dentry * old_dentry, struct inode * dir,
  84. struct dentry *dentry)
  85. {
  86. struct inode *inode = d_inode(old_dentry);
  87. inode->i_ctime = current_time(inode);
  88. inode_inc_link_count(inode);
  89. ihold(inode);
  90. return add_nondir(dentry, inode);
  91. }
  92. static int minix_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
  93. {
  94. struct inode * inode;
  95. int err;
  96. inode_inc_link_count(dir);
  97. inode = minix_new_inode(dir, S_IFDIR | mode, &err);
  98. if (!inode)
  99. goto out_dir;
  100. minix_set_inode(inode, 0);
  101. inode_inc_link_count(inode);
  102. err = minix_make_empty(inode, dir);
  103. if (err)
  104. goto out_fail;
  105. err = minix_add_link(dentry, inode);
  106. if (err)
  107. goto out_fail;
  108. d_instantiate(dentry, inode);
  109. out:
  110. return err;
  111. out_fail:
  112. inode_dec_link_count(inode);
  113. inode_dec_link_count(inode);
  114. iput(inode);
  115. out_dir:
  116. inode_dec_link_count(dir);
  117. goto out;
  118. }
  119. static int minix_unlink(struct inode * dir, struct dentry *dentry)
  120. {
  121. int err = -ENOENT;
  122. struct inode * inode = d_inode(dentry);
  123. struct page * page;
  124. struct minix_dir_entry * de;
  125. de = minix_find_entry(dentry, &page);
  126. if (!de)
  127. goto end_unlink;
  128. err = minix_delete_entry(de, page);
  129. if (err)
  130. goto end_unlink;
  131. inode->i_ctime = dir->i_ctime;
  132. inode_dec_link_count(inode);
  133. end_unlink:
  134. return err;
  135. }
  136. static int minix_rmdir(struct inode * dir, struct dentry *dentry)
  137. {
  138. struct inode * inode = d_inode(dentry);
  139. int err = -ENOTEMPTY;
  140. if (minix_empty_dir(inode)) {
  141. err = minix_unlink(dir, dentry);
  142. if (!err) {
  143. inode_dec_link_count(dir);
  144. inode_dec_link_count(inode);
  145. }
  146. }
  147. return err;
  148. }
  149. static int minix_rename(struct inode * old_dir, struct dentry *old_dentry,
  150. struct inode * new_dir, struct dentry *new_dentry,
  151. unsigned int flags)
  152. {
  153. struct inode * old_inode = d_inode(old_dentry);
  154. struct inode * new_inode = d_inode(new_dentry);
  155. struct page * dir_page = NULL;
  156. struct minix_dir_entry * dir_de = NULL;
  157. struct page * old_page;
  158. struct minix_dir_entry * old_de;
  159. int err = -ENOENT;
  160. if (flags & ~RENAME_NOREPLACE)
  161. return -EINVAL;
  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(new_inode);
  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. put_page(dir_page);
  204. }
  205. out_old:
  206. kunmap(old_page);
  207. put_page(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. };