blob: ff90affb94e16ebdd2b2a76156dab515d4a0a050 [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>
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;
20 hpfs_inode->i_conv = hpfs_sb(sb)->sb_conv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 i->i_size = -1;
22 i->i_blocks = -1;
23
24 hpfs_inode->i_dno = 0;
25 hpfs_inode->i_n_secs = 0;
26 hpfs_inode->i_file_sec = 0;
27 hpfs_inode->i_disk_sec = 0;
28 hpfs_inode->i_dpos = 0;
29 hpfs_inode->i_dsubdno = 0;
30 hpfs_inode->i_ea_mode = 0;
31 hpfs_inode->i_ea_uid = 0;
32 hpfs_inode->i_ea_gid = 0;
33 hpfs_inode->i_ea_size = 0;
34
35 hpfs_inode->i_rddir_off = NULL;
36 hpfs_inode->i_dirty = 0;
37
38 i->i_ctime.tv_sec = i->i_ctime.tv_nsec = 0;
39 i->i_mtime.tv_sec = i->i_mtime.tv_nsec = 0;
40 i->i_atime.tv_sec = i->i_atime.tv_nsec = 0;
41}
42
43void hpfs_read_inode(struct inode *i)
44{
45 struct buffer_head *bh;
46 struct fnode *fnode;
47 struct super_block *sb = i->i_sb;
48 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
Al Viro7e7742e2010-01-31 17:09:29 -050049 void *ea;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int ea_size;
51
52 if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
53 /*i->i_mode |= S_IFREG;
54 i->i_mode &= ~0111;
55 i->i_op = &hpfs_file_iops;
56 i->i_fop = &hpfs_file_ops;
57 i->i_nlink = 0;*/
58 make_bad_inode(i);
59 return;
60 }
61 if (hpfs_sb(i->i_sb)->sb_eas) {
62 if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
63 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050064 i->i_uid = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 hpfs_inode->i_ea_uid = 1;
66 }
67 kfree(ea);
68 }
69 if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
70 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050071 i->i_gid = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 hpfs_inode->i_ea_gid = 1;
73 }
74 kfree(ea);
75 }
76 if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
77 kfree(ea);
78 i->i_mode = S_IFLNK | 0777;
79 i->i_op = &page_symlink_inode_operations;
80 i->i_data.a_ops = &hpfs_symlink_aops;
81 i->i_nlink = 1;
82 i->i_size = ea_size;
83 i->i_blocks = 1;
84 brelse(bh);
85 return;
86 }
87 if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
88 int rdev = 0;
89 umode_t mode = hpfs_sb(sb)->sb_mode;
90 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050091 mode = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 hpfs_inode->i_ea_mode = 1;
93 }
94 kfree(ea);
95 i->i_mode = mode;
96 if (S_ISBLK(mode) || S_ISCHR(mode)) {
97 if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
98 if (ea_size == 4)
Al Viro2ef10312005-12-24 14:31:53 -050099 rdev = le32_to_cpu(*(__le32*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kfree(ea);
101 }
102 }
103 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
104 brelse(bh);
105 i->i_nlink = 1;
106 i->i_size = 0;
107 i->i_blocks = 1;
108 init_special_inode(i, mode,
109 new_decode_dev(rdev));
110 return;
111 }
112 }
113 }
114 if (fnode->dirflag) {
Al Viro7e7742e2010-01-31 17:09:29 -0500115 int n_dnodes, n_subdirs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 i->i_mode |= S_IFDIR;
117 i->i_op = &hpfs_dir_iops;
118 i->i_fop = &hpfs_dir_ops;
119 hpfs_inode->i_parent_dir = fnode->up;
120 hpfs_inode->i_dno = fnode->u.external[0].disk_secno;
121 if (hpfs_sb(sb)->sb_chk >= 2) {
122 struct buffer_head *bh0;
123 if (hpfs_map_fnode(sb, hpfs_inode->i_parent_dir, &bh0)) brelse(bh0);
124 }
125 n_dnodes = 0; n_subdirs = 0;
126 hpfs_count_dnodes(i->i_sb, hpfs_inode->i_dno, &n_dnodes, &n_subdirs, NULL);
127 i->i_blocks = 4 * n_dnodes;
128 i->i_size = 2048 * n_dnodes;
129 i->i_nlink = 2 + n_subdirs;
130 } else {
131 i->i_mode |= S_IFREG;
132 if (!hpfs_inode->i_ea_mode) i->i_mode &= ~0111;
133 i->i_op = &hpfs_file_iops;
134 i->i_fop = &hpfs_file_ops;
135 i->i_nlink = 1;
136 i->i_size = fnode->file_size;
137 i->i_blocks = ((i->i_size + 511) >> 9) + 1;
138 i->i_data.a_ops = &hpfs_aops;
139 hpfs_i(i)->mmu_private = i->i_size;
140 }
141 brelse(bh);
142}
143
144static void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
145{
146 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
147 /*if (fnode->acl_size_l || fnode->acl_size_s) {
148 Some unknown structures like ACL may be in fnode,
149 we'd better not overwrite them
150 hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 stuctures", i->i_ino);
151 } else*/ if (hpfs_sb(i->i_sb)->sb_eas >= 2) {
Al Viro2ef10312005-12-24 14:31:53 -0500152 __le32 ea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if ((i->i_uid != hpfs_sb(i->i_sb)->sb_uid) || hpfs_inode->i_ea_uid) {
154 ea = cpu_to_le32(i->i_uid);
155 hpfs_set_ea(i, fnode, "UID", (char*)&ea, 2);
156 hpfs_inode->i_ea_uid = 1;
157 }
158 if ((i->i_gid != hpfs_sb(i->i_sb)->sb_gid) || hpfs_inode->i_ea_gid) {
159 ea = cpu_to_le32(i->i_gid);
160 hpfs_set_ea(i, fnode, "GID", (char *)&ea, 2);
161 hpfs_inode->i_ea_gid = 1;
162 }
163 if (!S_ISLNK(i->i_mode))
164 if ((i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
165 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
166 && i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
167 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || hpfs_inode->i_ea_mode) {
168 ea = cpu_to_le32(i->i_mode);
Al Viro2ef10312005-12-24 14:31:53 -0500169 /* sick, but legal */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 hpfs_set_ea(i, fnode, "MODE", (char *)&ea, 2);
171 hpfs_inode->i_ea_mode = 1;
172 }
173 if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
174 ea = cpu_to_le32(new_encode_dev(i->i_rdev));
175 hpfs_set_ea(i, fnode, "DEV", (char *)&ea, 4);
176 }
177 }
178}
179
180void hpfs_write_inode(struct inode *i)
181{
182 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
183 struct inode *parent;
184 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
185 if (hpfs_inode->i_rddir_off && !atomic_read(&i->i_count)) {
186 if (*hpfs_inode->i_rddir_off) printk("HPFS: write_inode: some position still there\n");
187 kfree(hpfs_inode->i_rddir_off);
188 hpfs_inode->i_rddir_off = NULL;
189 }
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800190 mutex_lock(&hpfs_inode->i_parent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!i->i_nlink) {
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800192 mutex_unlock(&hpfs_inode->i_parent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return;
194 }
195 parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
196 if (parent) {
197 hpfs_inode->i_dirty = 0;
198 if (parent->i_state & I_NEW) {
199 hpfs_init_inode(parent);
200 hpfs_read_inode(parent);
201 unlock_new_inode(parent);
202 }
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800203 mutex_lock(&hpfs_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 hpfs_write_inode_nolock(i);
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800205 mutex_unlock(&hpfs_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 iput(parent);
207 } else {
208 mark_inode_dirty(i);
209 }
Ingo Molnar7bf6d782006-03-23 03:00:42 -0800210 mutex_unlock(&hpfs_inode->i_parent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213void hpfs_write_inode_nolock(struct inode *i)
214{
215 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
216 struct buffer_head *bh;
217 struct fnode *fnode;
218 struct quad_buffer_head qbh;
219 struct hpfs_dirent *de;
220 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
221 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
222 if (i->i_ino != hpfs_sb(i->i_sb)->sb_root && i->i_nlink) {
223 if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
224 brelse(bh);
225 return;
226 }
227 } else de = NULL;
228 if (S_ISREG(i->i_mode)) {
229 fnode->file_size = i->i_size;
230 if (de) de->file_size = i->i_size;
231 } else if (S_ISDIR(i->i_mode)) {
232 fnode->file_size = 0;
233 if (de) de->file_size = 0;
234 }
235 hpfs_write_inode_ea(i, fnode);
236 if (de) {
237 de->write_date = gmt_to_local(i->i_sb, i->i_mtime.tv_sec);
238 de->read_date = gmt_to_local(i->i_sb, i->i_atime.tv_sec);
239 de->creation_date = gmt_to_local(i->i_sb, i->i_ctime.tv_sec);
240 de->read_only = !(i->i_mode & 0222);
241 de->ea_size = hpfs_inode->i_ea_size;
242 hpfs_mark_4buffers_dirty(&qbh);
243 hpfs_brelse4(&qbh);
244 }
245 if (S_ISDIR(i->i_mode)) {
246 if ((de = map_dirent(i, hpfs_inode->i_dno, "\001\001", 2, NULL, &qbh))) {
247 de->write_date = gmt_to_local(i->i_sb, i->i_mtime.tv_sec);
248 de->read_date = gmt_to_local(i->i_sb, i->i_atime.tv_sec);
249 de->creation_date = gmt_to_local(i->i_sb, i->i_ctime.tv_sec);
250 de->read_only = !(i->i_mode & 0222);
251 de->ea_size = /*hpfs_inode->i_ea_size*/0;
252 de->file_size = 0;
253 hpfs_mark_4buffers_dirty(&qbh);
254 hpfs_brelse4(&qbh);
Randy Dunlap18debbb2006-12-06 20:37:05 -0800255 } else
256 hpfs_error(i->i_sb,
257 "directory %08lx doesn't have '.' entry",
258 (unsigned long)i->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 mark_buffer_dirty(bh);
261 brelse(bh);
262}
263
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200264int hpfs_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct inode *inode = dentry->d_inode;
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200267 int error = -EINVAL;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 lock_kernel();
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200270 if (inode->i_ino == hpfs_sb(inode->i_sb)->sb_root)
271 goto out_unlock;
272 if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
273 goto out_unlock;
274
275 error = inode_change_ok(inode, attr);
276 if (error)
277 goto out_unlock;
278
279 error = inode_setattr(inode, attr);
280 if (error)
281 goto out_unlock;
282
283 hpfs_write_inode(inode);
284
285 out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unlock_kernel();
287 return error;
288}
289
290void hpfs_write_if_changed(struct inode *inode)
291{
292 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
293
294 if (hpfs_inode->i_dirty)
295 hpfs_write_inode(inode);
296}
297
298void hpfs_delete_inode(struct inode *inode)
299{
Mark Fashehfef26652005-09-09 13:01:31 -0700300 truncate_inode_pages(&inode->i_data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 lock_kernel();
302 hpfs_remove_fnode(inode->i_sb, inode->i_ino);
303 unlock_kernel();
304 clear_inode(inode);
305}