blob: 1042a9bc97f3710c0930c80333afd303ae10a8d5 [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
Alexey Dobriyan405f5572009-07-11 22:08:37 +04009#include <linux/smp_lock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "hpfs_fn.h"
12
13void hpfs_init_inode(struct inode *i)
14{
15 struct super_block *sb = i->i_sb;
16 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
17
18 i->i_uid = hpfs_sb(sb)->sb_uid;
19 i->i_gid = hpfs_sb(sb)->sb_gid;
20 i->i_mode = hpfs_sb(sb)->sb_mode;
21 hpfs_inode->i_conv = hpfs_sb(sb)->sb_conv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 i->i_size = -1;
23 i->i_blocks = -1;
24
25 hpfs_inode->i_dno = 0;
26 hpfs_inode->i_n_secs = 0;
27 hpfs_inode->i_file_sec = 0;
28 hpfs_inode->i_disk_sec = 0;
29 hpfs_inode->i_dpos = 0;
30 hpfs_inode->i_dsubdno = 0;
31 hpfs_inode->i_ea_mode = 0;
32 hpfs_inode->i_ea_uid = 0;
33 hpfs_inode->i_ea_gid = 0;
34 hpfs_inode->i_ea_size = 0;
35
36 hpfs_inode->i_rddir_off = NULL;
37 hpfs_inode->i_dirty = 0;
38
39 i->i_ctime.tv_sec = i->i_ctime.tv_nsec = 0;
40 i->i_mtime.tv_sec = i->i_mtime.tv_nsec = 0;
41 i->i_atime.tv_sec = i->i_atime.tv_nsec = 0;
42}
43
44void hpfs_read_inode(struct inode *i)
45{
46 struct buffer_head *bh;
47 struct fnode *fnode;
48 struct super_block *sb = i->i_sb;
49 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
Al Viro7e7742e2010-01-31 17:09:29 -050050 void *ea;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int ea_size;
52
53 if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
54 /*i->i_mode |= S_IFREG;
55 i->i_mode &= ~0111;
56 i->i_op = &hpfs_file_iops;
57 i->i_fop = &hpfs_file_ops;
58 i->i_nlink = 0;*/
59 make_bad_inode(i);
60 return;
61 }
62 if (hpfs_sb(i->i_sb)->sb_eas) {
63 if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
64 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050065 i->i_uid = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 hpfs_inode->i_ea_uid = 1;
67 }
68 kfree(ea);
69 }
70 if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
71 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050072 i->i_gid = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 hpfs_inode->i_ea_gid = 1;
74 }
75 kfree(ea);
76 }
77 if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
78 kfree(ea);
79 i->i_mode = S_IFLNK | 0777;
80 i->i_op = &page_symlink_inode_operations;
81 i->i_data.a_ops = &hpfs_symlink_aops;
82 i->i_nlink = 1;
83 i->i_size = ea_size;
84 i->i_blocks = 1;
85 brelse(bh);
86 return;
87 }
88 if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
89 int rdev = 0;
90 umode_t mode = hpfs_sb(sb)->sb_mode;
91 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050092 mode = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 hpfs_inode->i_ea_mode = 1;
94 }
95 kfree(ea);
96 i->i_mode = mode;
97 if (S_ISBLK(mode) || S_ISCHR(mode)) {
98 if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
99 if (ea_size == 4)
Al Viro2ef10312005-12-24 14:31:53 -0500100 rdev = le32_to_cpu(*(__le32*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 kfree(ea);
102 }
103 }
104 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
105 brelse(bh);
106 i->i_nlink = 1;
107 i->i_size = 0;
108 i->i_blocks = 1;
109 init_special_inode(i, mode,
110 new_decode_dev(rdev));
111 return;
112 }
113 }
114 }
115 if (fnode->dirflag) {
Al Viro7e7742e2010-01-31 17:09:29 -0500116 int n_dnodes, n_subdirs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 i->i_mode |= S_IFDIR;
118 i->i_op = &hpfs_dir_iops;
119 i->i_fop = &hpfs_dir_ops;
120 hpfs_inode->i_parent_dir = fnode->up;
121 hpfs_inode->i_dno = fnode->u.external[0].disk_secno;
122 if (hpfs_sb(sb)->sb_chk >= 2) {
123 struct buffer_head *bh0;
124 if (hpfs_map_fnode(sb, hpfs_inode->i_parent_dir, &bh0)) brelse(bh0);
125 }
126 n_dnodes = 0; n_subdirs = 0;
127 hpfs_count_dnodes(i->i_sb, hpfs_inode->i_dno, &n_dnodes, &n_subdirs, NULL);
128 i->i_blocks = 4 * n_dnodes;
129 i->i_size = 2048 * n_dnodes;
130 i->i_nlink = 2 + n_subdirs;
131 } else {
132 i->i_mode |= S_IFREG;
133 if (!hpfs_inode->i_ea_mode) i->i_mode &= ~0111;
134 i->i_op = &hpfs_file_iops;
135 i->i_fop = &hpfs_file_ops;
136 i->i_nlink = 1;
137 i->i_size = fnode->file_size;
138 i->i_blocks = ((i->i_size + 511) >> 9) + 1;
139 i->i_data.a_ops = &hpfs_aops;
140 hpfs_i(i)->mmu_private = i->i_size;
141 }
142 brelse(bh);
143}
144
145static void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
146{
147 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
148 /*if (fnode->acl_size_l || fnode->acl_size_s) {
149 Some unknown structures like ACL may be in fnode,
150 we'd better not overwrite them
151 hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 stuctures", i->i_ino);
152 } else*/ if (hpfs_sb(i->i_sb)->sb_eas >= 2) {
Al Viro2ef10312005-12-24 14:31:53 -0500153 __le32 ea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if ((i->i_uid != hpfs_sb(i->i_sb)->sb_uid) || hpfs_inode->i_ea_uid) {
155 ea = cpu_to_le32(i->i_uid);
156 hpfs_set_ea(i, fnode, "UID", (char*)&ea, 2);
157 hpfs_inode->i_ea_uid = 1;
158 }
159 if ((i->i_gid != hpfs_sb(i->i_sb)->sb_gid) || hpfs_inode->i_ea_gid) {
160 ea = cpu_to_le32(i->i_gid);
161 hpfs_set_ea(i, fnode, "GID", (char *)&ea, 2);
162 hpfs_inode->i_ea_gid = 1;
163 }
164 if (!S_ISLNK(i->i_mode))
165 if ((i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
166 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
167 && i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
168 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || hpfs_inode->i_ea_mode) {
169 ea = cpu_to_le32(i->i_mode);
Al Viro2ef10312005-12-24 14:31:53 -0500170 /* sick, but legal */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 hpfs_set_ea(i, fnode, "MODE", (char *)&ea, 2);
172 hpfs_inode->i_ea_mode = 1;
173 }
174 if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
175 ea = cpu_to_le32(new_encode_dev(i->i_rdev));
176 hpfs_set_ea(i, fnode, "DEV", (char *)&ea, 4);
177 }
178 }
179}
180
181void hpfs_write_inode(struct inode *i)
182{
183 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
184 struct inode *parent;
185 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
186 if (hpfs_inode->i_rddir_off && !atomic_read(&i->i_count)) {
187 if (*hpfs_inode->i_rddir_off) printk("HPFS: write_inode: some position still there\n");
188 kfree(hpfs_inode->i_rddir_off);
189 hpfs_inode->i_rddir_off = NULL;
190 }
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800191 mutex_lock(&hpfs_inode->i_parent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!i->i_nlink) {
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800193 mutex_unlock(&hpfs_inode->i_parent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return;
195 }
196 parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
197 if (parent) {
198 hpfs_inode->i_dirty = 0;
199 if (parent->i_state & I_NEW) {
200 hpfs_init_inode(parent);
201 hpfs_read_inode(parent);
202 unlock_new_inode(parent);
203 }
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800204 mutex_lock(&hpfs_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 hpfs_write_inode_nolock(i);
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800206 mutex_unlock(&hpfs_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 iput(parent);
208 } else {
209 mark_inode_dirty(i);
210 }
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800211 mutex_unlock(&hpfs_inode->i_parent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214void hpfs_write_inode_nolock(struct inode *i)
215{
216 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
217 struct buffer_head *bh;
218 struct fnode *fnode;
219 struct quad_buffer_head qbh;
220 struct hpfs_dirent *de;
221 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
222 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
223 if (i->i_ino != hpfs_sb(i->i_sb)->sb_root && i->i_nlink) {
224 if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
225 brelse(bh);
226 return;
227 }
228 } else de = NULL;
229 if (S_ISREG(i->i_mode)) {
230 fnode->file_size = i->i_size;
231 if (de) de->file_size = i->i_size;
232 } else if (S_ISDIR(i->i_mode)) {
233 fnode->file_size = 0;
234 if (de) de->file_size = 0;
235 }
236 hpfs_write_inode_ea(i, fnode);
237 if (de) {
238 de->write_date = gmt_to_local(i->i_sb, i->i_mtime.tv_sec);
239 de->read_date = gmt_to_local(i->i_sb, i->i_atime.tv_sec);
240 de->creation_date = gmt_to_local(i->i_sb, i->i_ctime.tv_sec);
241 de->read_only = !(i->i_mode & 0222);
242 de->ea_size = hpfs_inode->i_ea_size;
243 hpfs_mark_4buffers_dirty(&qbh);
244 hpfs_brelse4(&qbh);
245 }
246 if (S_ISDIR(i->i_mode)) {
247 if ((de = map_dirent(i, hpfs_inode->i_dno, "\001\001", 2, NULL, &qbh))) {
248 de->write_date = gmt_to_local(i->i_sb, i->i_mtime.tv_sec);
249 de->read_date = gmt_to_local(i->i_sb, i->i_atime.tv_sec);
250 de->creation_date = gmt_to_local(i->i_sb, i->i_ctime.tv_sec);
251 de->read_only = !(i->i_mode & 0222);
252 de->ea_size = /*hpfs_inode->i_ea_size*/0;
253 de->file_size = 0;
254 hpfs_mark_4buffers_dirty(&qbh);
255 hpfs_brelse4(&qbh);
Randy Dunlap18debbb2006-12-06 20:37:05 -0800256 } else
257 hpfs_error(i->i_sb,
258 "directory %08lx doesn't have '.' entry",
259 (unsigned long)i->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 mark_buffer_dirty(bh);
262 brelse(bh);
263}
264
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200265int hpfs_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct inode *inode = dentry->d_inode;
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200268 int error = -EINVAL;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 lock_kernel();
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200271 if (inode->i_ino == hpfs_sb(inode->i_sb)->sb_root)
272 goto out_unlock;
273 if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
274 goto out_unlock;
275
276 error = inode_change_ok(inode, attr);
277 if (error)
278 goto out_unlock;
279
280 error = inode_setattr(inode, attr);
281 if (error)
282 goto out_unlock;
283
284 hpfs_write_inode(inode);
285
286 out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unlock_kernel();
288 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
299void hpfs_delete_inode(struct inode *inode)
300{
Mark Fashehfef26652005-09-09 13:01:31 -0700301 truncate_inode_pages(&inode->i_data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 lock_kernel();
303 hpfs_remove_fnode(inode->i_sb, inode->i_ino);
304 unlock_kernel();
305 clear_inode(inode);
306}