inode.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hpfs/inode.c
  4. *
  5. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  6. *
  7. * inode VFS functions
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/user_namespace.h>
  11. #include "hpfs_fn.h"
  12. void hpfs_init_inode(struct inode *i)
  13. {
  14. struct super_block *sb = i->i_sb;
  15. struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  16. i->i_uid = hpfs_sb(sb)->sb_uid;
  17. i->i_gid = hpfs_sb(sb)->sb_gid;
  18. i->i_mode = hpfs_sb(sb)->sb_mode;
  19. i->i_size = -1;
  20. i->i_blocks = -1;
  21. hpfs_inode->i_dno = 0;
  22. hpfs_inode->i_n_secs = 0;
  23. hpfs_inode->i_file_sec = 0;
  24. hpfs_inode->i_disk_sec = 0;
  25. hpfs_inode->i_dpos = 0;
  26. hpfs_inode->i_dsubdno = 0;
  27. hpfs_inode->i_ea_mode = 0;
  28. hpfs_inode->i_ea_uid = 0;
  29. hpfs_inode->i_ea_gid = 0;
  30. hpfs_inode->i_ea_size = 0;
  31. hpfs_inode->i_rddir_off = NULL;
  32. hpfs_inode->i_dirty = 0;
  33. i->i_ctime.tv_sec = i->i_ctime.tv_nsec = 0;
  34. i->i_mtime.tv_sec = i->i_mtime.tv_nsec = 0;
  35. i->i_atime.tv_sec = i->i_atime.tv_nsec = 0;
  36. }
  37. void hpfs_read_inode(struct inode *i)
  38. {
  39. struct buffer_head *bh;
  40. struct fnode *fnode;
  41. struct super_block *sb = i->i_sb;
  42. struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  43. void *ea;
  44. int ea_size;
  45. if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
  46. /*i->i_mode |= S_IFREG;
  47. i->i_mode &= ~0111;
  48. i->i_op = &hpfs_file_iops;
  49. i->i_fop = &hpfs_file_ops;
  50. clear_nlink(i);*/
  51. make_bad_inode(i);
  52. return;
  53. }
  54. if (hpfs_sb(i->i_sb)->sb_eas) {
  55. if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
  56. if (ea_size == 2) {
  57. i_uid_write(i, le16_to_cpu(*(__le16*)ea));
  58. hpfs_inode->i_ea_uid = 1;
  59. }
  60. kfree(ea);
  61. }
  62. if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
  63. if (ea_size == 2) {
  64. i_gid_write(i, le16_to_cpu(*(__le16*)ea));
  65. hpfs_inode->i_ea_gid = 1;
  66. }
  67. kfree(ea);
  68. }
  69. if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
  70. kfree(ea);
  71. i->i_mode = S_IFLNK | 0777;
  72. i->i_op = &page_symlink_inode_operations;
  73. inode_nohighmem(i);
  74. i->i_data.a_ops = &hpfs_symlink_aops;
  75. set_nlink(i, 1);
  76. i->i_size = ea_size;
  77. i->i_blocks = 1;
  78. brelse(bh);
  79. return;
  80. }
  81. if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
  82. int rdev = 0;
  83. umode_t mode = hpfs_sb(sb)->sb_mode;
  84. if (ea_size == 2) {
  85. mode = le16_to_cpu(*(__le16*)ea);
  86. hpfs_inode->i_ea_mode = 1;
  87. }
  88. kfree(ea);
  89. i->i_mode = mode;
  90. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  91. if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
  92. if (ea_size == 4)
  93. rdev = le32_to_cpu(*(__le32*)ea);
  94. kfree(ea);
  95. }
  96. }
  97. if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
  98. brelse(bh);
  99. set_nlink(i, 1);
  100. i->i_size = 0;
  101. i->i_blocks = 1;
  102. init_special_inode(i, mode,
  103. new_decode_dev(rdev));
  104. return;
  105. }
  106. }
  107. }
  108. if (fnode_is_dir(fnode)) {
  109. int n_dnodes, n_subdirs;
  110. i->i_mode |= S_IFDIR;
  111. i->i_op = &hpfs_dir_iops;
  112. i->i_fop = &hpfs_dir_ops;
  113. hpfs_inode->i_parent_dir = le32_to_cpu(fnode->up);
  114. hpfs_inode->i_dno = le32_to_cpu(fnode->u.external[0].disk_secno);
  115. if (hpfs_sb(sb)->sb_chk >= 2) {
  116. struct buffer_head *bh0;
  117. if (hpfs_map_fnode(sb, hpfs_inode->i_parent_dir, &bh0)) brelse(bh0);
  118. }
  119. n_dnodes = 0; n_subdirs = 0;
  120. hpfs_count_dnodes(i->i_sb, hpfs_inode->i_dno, &n_dnodes, &n_subdirs, NULL);
  121. i->i_blocks = 4 * n_dnodes;
  122. i->i_size = 2048 * n_dnodes;
  123. set_nlink(i, 2 + n_subdirs);
  124. } else {
  125. i->i_mode |= S_IFREG;
  126. if (!hpfs_inode->i_ea_mode) i->i_mode &= ~0111;
  127. i->i_op = &hpfs_file_iops;
  128. i->i_fop = &hpfs_file_ops;
  129. set_nlink(i, 1);
  130. i->i_size = le32_to_cpu(fnode->file_size);
  131. i->i_blocks = ((i->i_size + 511) >> 9) + 1;
  132. i->i_data.a_ops = &hpfs_aops;
  133. hpfs_i(i)->mmu_private = i->i_size;
  134. }
  135. brelse(bh);
  136. }
  137. static void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
  138. {
  139. struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  140. /*if (le32_to_cpu(fnode->acl_size_l) || le16_to_cpu(fnode->acl_size_s)) {
  141. Some unknown structures like ACL may be in fnode,
  142. we'd better not overwrite them
  143. hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 structures", i->i_ino);
  144. } else*/ if (hpfs_sb(i->i_sb)->sb_eas >= 2) {
  145. __le32 ea;
  146. if (!uid_eq(i->i_uid, hpfs_sb(i->i_sb)->sb_uid) || hpfs_inode->i_ea_uid) {
  147. ea = cpu_to_le32(i_uid_read(i));
  148. hpfs_set_ea(i, fnode, "UID", (char*)&ea, 2);
  149. hpfs_inode->i_ea_uid = 1;
  150. }
  151. if (!gid_eq(i->i_gid, hpfs_sb(i->i_sb)->sb_gid) || hpfs_inode->i_ea_gid) {
  152. ea = cpu_to_le32(i_gid_read(i));
  153. hpfs_set_ea(i, fnode, "GID", (char *)&ea, 2);
  154. hpfs_inode->i_ea_gid = 1;
  155. }
  156. if (!S_ISLNK(i->i_mode))
  157. if ((i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
  158. | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
  159. && i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
  160. | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || hpfs_inode->i_ea_mode) {
  161. ea = cpu_to_le32(i->i_mode);
  162. /* sick, but legal */
  163. hpfs_set_ea(i, fnode, "MODE", (char *)&ea, 2);
  164. hpfs_inode->i_ea_mode = 1;
  165. }
  166. if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
  167. ea = cpu_to_le32(new_encode_dev(i->i_rdev));
  168. hpfs_set_ea(i, fnode, "DEV", (char *)&ea, 4);
  169. }
  170. }
  171. }
  172. void hpfs_write_inode(struct inode *i)
  173. {
  174. struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  175. struct inode *parent;
  176. if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
  177. if (hpfs_inode->i_rddir_off && !atomic_read(&i->i_count)) {
  178. if (*hpfs_inode->i_rddir_off)
  179. pr_err("write_inode: some position still there\n");
  180. kfree(hpfs_inode->i_rddir_off);
  181. hpfs_inode->i_rddir_off = NULL;
  182. }
  183. if (!i->i_nlink) {
  184. return;
  185. }
  186. parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
  187. if (parent) {
  188. hpfs_inode->i_dirty = 0;
  189. if (parent->i_state & I_NEW) {
  190. hpfs_init_inode(parent);
  191. hpfs_read_inode(parent);
  192. unlock_new_inode(parent);
  193. }
  194. hpfs_write_inode_nolock(i);
  195. iput(parent);
  196. }
  197. }
  198. void hpfs_write_inode_nolock(struct inode *i)
  199. {
  200. struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  201. struct buffer_head *bh;
  202. struct fnode *fnode;
  203. struct quad_buffer_head qbh;
  204. struct hpfs_dirent *de;
  205. if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
  206. if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
  207. if (i->i_ino != hpfs_sb(i->i_sb)->sb_root && i->i_nlink) {
  208. if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
  209. brelse(bh);
  210. return;
  211. }
  212. } else de = NULL;
  213. if (S_ISREG(i->i_mode)) {
  214. fnode->file_size = cpu_to_le32(i->i_size);
  215. if (de) de->file_size = cpu_to_le32(i->i_size);
  216. } else if (S_ISDIR(i->i_mode)) {
  217. fnode->file_size = cpu_to_le32(0);
  218. if (de) de->file_size = cpu_to_le32(0);
  219. }
  220. hpfs_write_inode_ea(i, fnode);
  221. if (de) {
  222. de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
  223. de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
  224. de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
  225. de->read_only = !(i->i_mode & 0222);
  226. de->ea_size = cpu_to_le32(hpfs_inode->i_ea_size);
  227. hpfs_mark_4buffers_dirty(&qbh);
  228. hpfs_brelse4(&qbh);
  229. }
  230. if (S_ISDIR(i->i_mode)) {
  231. if ((de = map_dirent(i, hpfs_inode->i_dno, "\001\001", 2, NULL, &qbh))) {
  232. de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
  233. de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
  234. de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
  235. de->read_only = !(i->i_mode & 0222);
  236. de->ea_size = cpu_to_le32(/*hpfs_inode->i_ea_size*/0);
  237. de->file_size = cpu_to_le32(0);
  238. hpfs_mark_4buffers_dirty(&qbh);
  239. hpfs_brelse4(&qbh);
  240. } else
  241. hpfs_error(i->i_sb,
  242. "directory %08lx doesn't have '.' entry",
  243. (unsigned long)i->i_ino);
  244. }
  245. mark_buffer_dirty(bh);
  246. brelse(bh);
  247. }
  248. int hpfs_setattr(struct dentry *dentry, struct iattr *attr)
  249. {
  250. struct inode *inode = d_inode(dentry);
  251. int error = -EINVAL;
  252. hpfs_lock(inode->i_sb);
  253. if (inode->i_ino == hpfs_sb(inode->i_sb)->sb_root)
  254. goto out_unlock;
  255. if ((attr->ia_valid & ATTR_UID) &&
  256. from_kuid(&init_user_ns, attr->ia_uid) >= 0x10000)
  257. goto out_unlock;
  258. if ((attr->ia_valid & ATTR_GID) &&
  259. from_kgid(&init_user_ns, attr->ia_gid) >= 0x10000)
  260. goto out_unlock;
  261. if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
  262. goto out_unlock;
  263. error = setattr_prepare(dentry, attr);
  264. if (error)
  265. goto out_unlock;
  266. if ((attr->ia_valid & ATTR_SIZE) &&
  267. attr->ia_size != i_size_read(inode)) {
  268. error = inode_newsize_ok(inode, attr->ia_size);
  269. if (error)
  270. goto out_unlock;
  271. truncate_setsize(inode, attr->ia_size);
  272. hpfs_truncate(inode);
  273. }
  274. setattr_copy(inode, attr);
  275. hpfs_write_inode(inode);
  276. out_unlock:
  277. hpfs_unlock(inode->i_sb);
  278. return error;
  279. }
  280. void hpfs_write_if_changed(struct inode *inode)
  281. {
  282. struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  283. if (hpfs_inode->i_dirty)
  284. hpfs_write_inode(inode);
  285. }
  286. void hpfs_evict_inode(struct inode *inode)
  287. {
  288. truncate_inode_pages_final(&inode->i_data);
  289. clear_inode(inode);
  290. if (!inode->i_nlink) {
  291. hpfs_lock(inode->i_sb);
  292. hpfs_remove_fnode(inode->i_sb, inode->i_ino);
  293. hpfs_unlock(inode->i_sb);
  294. }
  295. }