blob: 3b2cec29972b167359359de4bd1891975ffd9cc0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "hpfs_fn.h"
11
12void 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
17 i->i_uid = hpfs_sb(sb)->sb_uid;
18 i->i_gid = hpfs_sb(sb)->sb_gid;
19 i->i_mode = hpfs_sb(sb)->sb_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 i->i_size = -1;
21 i->i_blocks = -1;
22
23 hpfs_inode->i_dno = 0;
24 hpfs_inode->i_n_secs = 0;
25 hpfs_inode->i_file_sec = 0;
26 hpfs_inode->i_disk_sec = 0;
27 hpfs_inode->i_dpos = 0;
28 hpfs_inode->i_dsubdno = 0;
29 hpfs_inode->i_ea_mode = 0;
30 hpfs_inode->i_ea_uid = 0;
31 hpfs_inode->i_ea_gid = 0;
32 hpfs_inode->i_ea_size = 0;
33
34 hpfs_inode->i_rddir_off = NULL;
35 hpfs_inode->i_dirty = 0;
36
37 i->i_ctime.tv_sec = i->i_ctime.tv_nsec = 0;
38 i->i_mtime.tv_sec = i->i_mtime.tv_nsec = 0;
39 i->i_atime.tv_sec = i->i_atime.tv_nsec = 0;
40}
41
42void hpfs_read_inode(struct inode *i)
43{
44 struct buffer_head *bh;
45 struct fnode *fnode;
46 struct super_block *sb = i->i_sb;
47 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
Al Viro7e7742e2010-01-31 17:09:29 -050048 void *ea;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int ea_size;
50
51 if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
52 /*i->i_mode |= S_IFREG;
53 i->i_mode &= ~0111;
54 i->i_op = &hpfs_file_iops;
55 i->i_fop = &hpfs_file_ops;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +020056 clear_nlink(i);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 make_bad_inode(i);
58 return;
59 }
60 if (hpfs_sb(i->i_sb)->sb_eas) {
61 if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
62 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050063 i->i_uid = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 hpfs_inode->i_ea_uid = 1;
65 }
66 kfree(ea);
67 }
68 if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
69 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050070 i->i_gid = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 hpfs_inode->i_ea_gid = 1;
72 }
73 kfree(ea);
74 }
75 if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
76 kfree(ea);
77 i->i_mode = S_IFLNK | 0777;
78 i->i_op = &page_symlink_inode_operations;
79 i->i_data.a_ops = &hpfs_symlink_aops;
Miklos Szeredibfe86842011-10-28 14:13:29 +020080 set_nlink(i, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 i->i_size = ea_size;
82 i->i_blocks = 1;
83 brelse(bh);
84 return;
85 }
86 if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
87 int rdev = 0;
88 umode_t mode = hpfs_sb(sb)->sb_mode;
89 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050090 mode = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 hpfs_inode->i_ea_mode = 1;
92 }
93 kfree(ea);
94 i->i_mode = mode;
95 if (S_ISBLK(mode) || S_ISCHR(mode)) {
96 if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
97 if (ea_size == 4)
Al Viro2ef10312005-12-24 14:31:53 -050098 rdev = le32_to_cpu(*(__le32*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 kfree(ea);
100 }
101 }
102 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
103 brelse(bh);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200104 set_nlink(i, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 i->i_size = 0;
106 i->i_blocks = 1;
107 init_special_inode(i, mode,
108 new_decode_dev(rdev));
109 return;
110 }
111 }
112 }
113 if (fnode->dirflag) {
Al Viro7e7742e2010-01-31 17:09:29 -0500114 int n_dnodes, n_subdirs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 i->i_mode |= S_IFDIR;
116 i->i_op = &hpfs_dir_iops;
117 i->i_fop = &hpfs_dir_ops;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200118 hpfs_inode->i_parent_dir = le32_to_cpu(fnode->up);
119 hpfs_inode->i_dno = le32_to_cpu(fnode->u.external[0].disk_secno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (hpfs_sb(sb)->sb_chk >= 2) {
121 struct buffer_head *bh0;
122 if (hpfs_map_fnode(sb, hpfs_inode->i_parent_dir, &bh0)) brelse(bh0);
123 }
124 n_dnodes = 0; n_subdirs = 0;
125 hpfs_count_dnodes(i->i_sb, hpfs_inode->i_dno, &n_dnodes, &n_subdirs, NULL);
126 i->i_blocks = 4 * n_dnodes;
127 i->i_size = 2048 * n_dnodes;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200128 set_nlink(i, 2 + n_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 } else {
130 i->i_mode |= S_IFREG;
131 if (!hpfs_inode->i_ea_mode) i->i_mode &= ~0111;
132 i->i_op = &hpfs_file_iops;
133 i->i_fop = &hpfs_file_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200134 set_nlink(i, 1);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200135 i->i_size = le32_to_cpu(fnode->file_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 i->i_blocks = ((i->i_size + 511) >> 9) + 1;
137 i->i_data.a_ops = &hpfs_aops;
138 hpfs_i(i)->mmu_private = i->i_size;
139 }
140 brelse(bh);
141}
142
143static void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
144{
145 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200146 /*if (le32_to_cpu(fnode->acl_size_l) || le16_to_cpu(fnode->acl_size_s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 Some unknown structures like ACL may be in fnode,
148 we'd better not overwrite them
149 hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 stuctures", i->i_ino);
150 } else*/ if (hpfs_sb(i->i_sb)->sb_eas >= 2) {
Al Viro2ef10312005-12-24 14:31:53 -0500151 __le32 ea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if ((i->i_uid != hpfs_sb(i->i_sb)->sb_uid) || hpfs_inode->i_ea_uid) {
153 ea = cpu_to_le32(i->i_uid);
154 hpfs_set_ea(i, fnode, "UID", (char*)&ea, 2);
155 hpfs_inode->i_ea_uid = 1;
156 }
157 if ((i->i_gid != hpfs_sb(i->i_sb)->sb_gid) || hpfs_inode->i_ea_gid) {
158 ea = cpu_to_le32(i->i_gid);
159 hpfs_set_ea(i, fnode, "GID", (char *)&ea, 2);
160 hpfs_inode->i_ea_gid = 1;
161 }
162 if (!S_ISLNK(i->i_mode))
163 if ((i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
164 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
165 && i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
166 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || hpfs_inode->i_ea_mode) {
167 ea = cpu_to_le32(i->i_mode);
Al Viro2ef10312005-12-24 14:31:53 -0500168 /* sick, but legal */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 hpfs_set_ea(i, fnode, "MODE", (char *)&ea, 2);
170 hpfs_inode->i_ea_mode = 1;
171 }
172 if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
173 ea = cpu_to_le32(new_encode_dev(i->i_rdev));
174 hpfs_set_ea(i, fnode, "DEV", (char *)&ea, 4);
175 }
176 }
177}
178
179void hpfs_write_inode(struct inode *i)
180{
181 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
182 struct inode *parent;
183 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
184 if (hpfs_inode->i_rddir_off && !atomic_read(&i->i_count)) {
185 if (*hpfs_inode->i_rddir_off) printk("HPFS: write_inode: some position still there\n");
186 kfree(hpfs_inode->i_rddir_off);
187 hpfs_inode->i_rddir_off = NULL;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!i->i_nlink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return;
191 }
192 parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
193 if (parent) {
194 hpfs_inode->i_dirty = 0;
195 if (parent->i_state & I_NEW) {
196 hpfs_init_inode(parent);
197 hpfs_read_inode(parent);
198 unlock_new_inode(parent);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 hpfs_write_inode_nolock(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 iput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205void hpfs_write_inode_nolock(struct inode *i)
206{
207 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
208 struct buffer_head *bh;
209 struct fnode *fnode;
210 struct quad_buffer_head qbh;
211 struct hpfs_dirent *de;
212 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
213 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
214 if (i->i_ino != hpfs_sb(i->i_sb)->sb_root && i->i_nlink) {
215 if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
216 brelse(bh);
217 return;
218 }
219 } else de = NULL;
220 if (S_ISREG(i->i_mode)) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200221 fnode->file_size = cpu_to_le32(i->i_size);
222 if (de) de->file_size = cpu_to_le32(i->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else if (S_ISDIR(i->i_mode)) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200224 fnode->file_size = cpu_to_le32(0);
225 if (de) de->file_size = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 hpfs_write_inode_ea(i, fnode);
228 if (de) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200229 de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
230 de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
231 de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 de->read_only = !(i->i_mode & 0222);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200233 de->ea_size = cpu_to_le32(hpfs_inode->i_ea_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 hpfs_mark_4buffers_dirty(&qbh);
235 hpfs_brelse4(&qbh);
236 }
237 if (S_ISDIR(i->i_mode)) {
238 if ((de = map_dirent(i, hpfs_inode->i_dno, "\001\001", 2, NULL, &qbh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200239 de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
240 de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
241 de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 de->read_only = !(i->i_mode & 0222);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200243 de->ea_size = cpu_to_le32(/*hpfs_inode->i_ea_size*/0);
244 de->file_size = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 hpfs_mark_4buffers_dirty(&qbh);
246 hpfs_brelse4(&qbh);
Randy Dunlap18debbb2006-12-06 20:37:05 -0800247 } else
248 hpfs_error(i->i_sb,
249 "directory %08lx doesn't have '.' entry",
250 (unsigned long)i->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 mark_buffer_dirty(bh);
253 brelse(bh);
254}
255
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200256int hpfs_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct inode *inode = dentry->d_inode;
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200259 int error = -EINVAL;
260
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100261 hpfs_lock(inode->i_sb);
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200262 if (inode->i_ino == hpfs_sb(inode->i_sb)->sb_root)
263 goto out_unlock;
Mikulas Patocka48f10e82011-05-08 20:44:00 +0200264 if ((attr->ia_valid & ATTR_UID) && attr->ia_uid >= 0x10000)
265 goto out_unlock;
266 if ((attr->ia_valid & ATTR_GID) && attr->ia_gid >= 0x10000)
267 goto out_unlock;
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200268 if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
269 goto out_unlock;
270
271 error = inode_change_ok(inode, attr);
272 if (error)
273 goto out_unlock;
274
Christoph Hellwig10257742010-06-04 11:30:02 +0200275 if ((attr->ia_valid & ATTR_SIZE) &&
276 attr->ia_size != i_size_read(inode)) {
277 error = vmtruncate(inode, attr->ia_size);
278 if (error)
Dr. David Alan Gilbert274052e2010-12-13 17:09:52 +0000279 goto out_unlock;
Christoph Hellwig10257742010-06-04 11:30:02 +0200280 }
281
282 setattr_copy(inode, attr);
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200283
284 hpfs_write_inode(inode);
285
286 out_unlock:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100287 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return error;
289}
290
291void hpfs_write_if_changed(struct inode *inode)
292{
293 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
294
295 if (hpfs_inode->i_dirty)
296 hpfs_write_inode(inode);
297}
298
Al Viroea544002010-06-07 00:18:40 -0400299void hpfs_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Mark Fashehfef26652005-09-09 13:01:31 -0700301 truncate_inode_pages(&inode->i_data, 0);
Al Viroea544002010-06-07 00:18:40 -0400302 end_writeback(inode);
303 if (!inode->i_nlink) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100304 hpfs_lock(inode->i_sb);
Al Viroea544002010-06-07 00:18:40 -0400305 hpfs_remove_fnode(inode->i_sb, inode->i_ino);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100306 hpfs_unlock(inode->i_sb);
Al Viroea544002010-06-07 00:18:40 -0400307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}