inode.c 8.2 KB

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