summaryrefslogtreecommitdiffstats
path: root/fs/hpfs/inode.c
blob: 99bfa1004cfd9d888d325a758a079368c0dc7317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 *  linux/fs/hpfs/inode.c
 *
 *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
 *
 *  inode VFS functions
 */

#include "hpfs_fn.h"

static const struct file_operations hpfs_file_ops =
{
	NULL,				/* lseek - default */
	hpfs_file_read,			/* read */
	hpfs_file_write,		/* write */
	NULL,				/* readdir - bad */
	NULL,				/* poll - default */
	NULL,				/* ioctl - default */
	generic_file_mmap/*hpfs_mmap*/,	/* mmap */
	hpfs_open,			/* open */
	NULL,				/* flush */
	hpfs_file_release,		/* release */
	hpfs_file_fsync,		/* fsync */
	NULL,				/* fasync */
	NULL,				/* check_media_change */
	NULL,				/* revalidate */
	NULL,				/* lock */
};

static const struct inode_operations hpfs_file_iops =
{
	(nonconst *) & hpfs_file_ops,	/* default file operations */
	NULL,				/* create */
	NULL,				/* lookup */
	NULL,				/* link */
	NULL,				/* unlink */
	NULL,				/* symlink */
	NULL,				/* mkdir */
	NULL,				/* rmdir */
	NULL,				/* mknod */
	NULL,				/* rename */
	NULL,				/* readlink */
	NULL,				/* follow_link */
	generic_readpage,		/* readpage */
	NULL,				/* writepage */
	(int (*)(struct inode *, int))
	&hpfs_bmap,			/* bmap */
	&hpfs_truncate,			/* truncate */
	NULL,				/* permission */
	NULL,				/* smap */
	NULL,				/* updatepage */
	NULL,				/* revalidate */
};

static const struct file_operations hpfs_dir_ops =
{
	NULL,				/* lseek - default */
	hpfs_dir_read,			/* read */
	NULL,				/* write - bad */
	hpfs_readdir,			/* readdir */
	NULL,				/* poll - default */
	NULL,				/* ioctl - default */
	NULL,				/* mmap */
	hpfs_open,			/* open */
	NULL,				/* flush */
	hpfs_dir_release,		/* no special release code */
	hpfs_file_fsync,		/* fsync */
	NULL,				/* fasync */
	NULL,				/* check_media_change */
	NULL,				/* revalidate */
	NULL,				/* lock */
};

static const struct inode_operations hpfs_dir_iops =
{
	(nonconst *) & hpfs_dir_ops,	/* default directory file ops */
	hpfs_create,			/* create */
	hpfs_lookup,			/* lookup */
	NULL,				/* link */
	hpfs_unlink,			/* unlink */
	hpfs_symlink,			/* symlink */
	hpfs_mkdir,			/* mkdir */
	hpfs_rmdir,			/* rmdir */
	hpfs_mknod,			/* mknod */
	hpfs_rename,			/* rename */
	NULL,				/* readlink */
	NULL,				/* follow_link */
	NULL,				/* readpage */
	NULL,				/* writepage */
	NULL,				/* bmap */
	NULL,				/* truncate */
	NULL,				/* permission */
	NULL,				/* smap */
	NULL,				/* updatepage */
	NULL,				/* revalidate */
};

const struct inode_operations hpfs_symlink_iops =
{
	NULL,				/* default file operations */
	NULL,				/* create */
	NULL,				/* lookup */
	NULL,				/* link */
	NULL,				/* unlink */
	NULL,				/* symlink */
	NULL,				/* mkdir */
	NULL,				/* rmdir */
	NULL,				/* mknod */
	NULL,				/* rename */
	hpfs_readlink,			/* readlink */
	hpfs_follow_link,		/* follow_link */
	NULL,				/* readpage */
	NULL,				/* writepage */
	NULL,				/* bmap */
	NULL,				/* truncate */
	NULL,				/* permission */
	NULL,				/* smap */
	NULL,				/* updatepage */
	NULL,				/* revalidate */
};


void hpfs_read_inode(struct inode *i)
{
	struct buffer_head *bh;
	struct fnode *fnode;
	struct super_block *sb = i->i_sb;
	unsigned char *ea;
	int ea_size;
	i->i_op = 0;
	/*i->i_hpfs_sem = MUTEX;*/
	init_MUTEX(&i->i_hpfs_sem);
	i->i_uid = sb->s_hpfs_uid;
	i->i_gid = sb->s_hpfs_gid;
	i->i_mode = sb->s_hpfs_mode;
	i->i_hpfs_conv = sb->s_hpfs_conv;
	i->i_blksize = 512;
	i->i_size = -1;
	i->i_blocks = -1;
	
	i->i_hpfs_dno = 0;
	i->i_hpfs_n_secs = 0;
	i->i_hpfs_file_sec = 0;
	i->i_hpfs_disk_sec = 0;
	i->i_hpfs_dpos = 0;
	i->i_hpfs_dsubdno = 0;
	i->i_hpfs_ea_mode = 0;
	i->i_hpfs_ea_uid = 0;
	i->i_hpfs_ea_gid = 0;
	i->i_hpfs_ea_size = 0;
	i->i_version = ++event;

	i->i_hpfs_rddir_off = NULL;
	i->i_hpfs_dirty = 0;

	i->i_atime = 0;
	i->i_mtime = 0;
	i->i_ctime = 0;

	if (!i->i_sb->s_hpfs_rd_inode)
		hpfs_error(i->i_sb, "read_inode: s_hpfs_rd_inode == 0");
	if (i->i_sb->s_hpfs_rd_inode == 2) {
		i->i_mode |= S_IFREG;
		i->i_mode &= ~0111;
		i->i_op = (struct inode_operations *) &hpfs_file_iops;
		i->i_nlink = 1;
		return;
	}
	if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
		/*i->i_mode |= S_IFREG;
		i->i_mode &= ~0111;
		i->i_op = (struct inode_operations *) &hpfs_file_iops;
		i->i_nlink = 0;*/
		make_bad_inode(i);
		return;
	}
	if (i->i_sb->s_hpfs_eas) {
		if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
			if (ea_size == 2) {
				i->i_uid = ea[0] + (ea[1] << 8);
				i->i_hpfs_ea_uid = 1;
			}
			kfree(ea);
		}
		if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
			if (ea_size == 2) {
				i->i_gid = ea[0] + (ea[1] << 8);
				i->i_hpfs_ea_gid = 1;
			}
			kfree(ea);
		}
		if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
			kfree(ea);
			i->i_mode = S_IFLNK | 0777;
			i->i_op = (struct inode_operations *) &hpfs_symlink_iops;
			i->i_nlink = 1;
			i->i_size = ea_size;
			i->i_blocks = 1;
			brelse(bh);
			return;
		}
		if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
			int rdev = 0;
			umode_t mode = sb->s_hpfs_mode;
			if (ea_size == 2) {
				mode = ea[0] + (ea[1] << 8);
				i->i_hpfs_ea_mode = 1;
			}
			kfree(ea);
			i->i_mode = mode;
			if (S_ISBLK(mode) || S_ISCHR(mode)) {
				if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
					if (ea_size == 4)
						rdev = ea[0] + (ea[1] << 8) + (ea[2] << 16) + (ea[3] << 24);
					kfree(ea);
				}
			}
			if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
				brelse(bh);
				i->i_nlink = 1;
				i->i_size = 0;
				i->i_blocks = 1;
				init_special_inode(i, mode, rdev);
				return;
			}
		}
	}
	if (fnode->dirflag) {
		unsigned n_dnodes, n_subdirs;
		i->i_mode |= S_IFDIR;
		i->i_op = (struct inode_operations *) &hpfs_dir_iops;
		i->i_hpfs_parent_dir = fnode->up;
		i->i_hpfs_dno = fnode->u.external[0].disk_secno;
		if (sb->s_hpfs_chk >= 2) {
			struct buffer_head *bh0;
			if (hpfs_map_fnode(sb, i->i_hpfs_parent_dir, &bh0)) brelse(bh0);
		}
		n_dnodes = 0; n_subdirs = 0;
		hpfs_count_dnodes(i->i_sb, i->i_hpfs_dno, &n_dnodes, &n_subdirs, NULL);
		i->i_blocks = 4 * n_dnodes;
		i->i_size = 2048 * n_dnodes;
		i->i_nlink = 2 + n_subdirs;
	} else {
		i->i_mode |= S_IFREG;
		if (!i->i_hpfs_ea_mode) i->i_mode &= ~0111;
		i->i_op = (struct inode_operations *) &hpfs_file_iops;
		i->i_nlink = 1;
		i->i_size = fnode->file_size;
		i->i_blocks = ((i->i_size + 511) >> 9) + 1;
	}
	brelse(bh);
}

void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
{
	if (fnode->acl_size_l || fnode->acl_size_s) {
		/* Some unknown structures like ACL may be in fnode,
		   we'd better not overwrite them */
		hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 stuctures", i->i_ino);
	} else if (i->i_sb->s_hpfs_eas >= 2) {
		unsigned char ea[4];
		if ((i->i_uid != i->i_sb->s_hpfs_uid) || i->i_hpfs_ea_uid) {
			ea[0] = i->i_uid & 0xff;
			ea[1] = i->i_uid >> 8;
			hpfs_set_ea(i, fnode, "UID", ea, 2);
			i->i_hpfs_ea_uid = 1;
		}
		if ((i->i_gid != i->i_sb->s_hpfs_gid) || i->i_hpfs_ea_gid) {
			ea[0] = i->i_gid & 0xff;
			ea[1] = i->i_gid >> 8;
			hpfs_set_ea(i, fnode, "GID", ea, 2);
			i->i_hpfs_ea_gid = 1;
		}
		if (!S_ISLNK(i->i_mode))
			if ((i->i_mode != ((i->i_sb->s_hpfs_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
			  | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
			  && i->i_mode != ((i->i_sb->s_hpfs_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
			  | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || i->i_hpfs_ea_mode) {
				ea[0] = i->i_mode & 0xff;
				ea[1] = i->i_mode >> 8;
				hpfs_set_ea(i, fnode, "MODE", ea, 2);
				i->i_hpfs_ea_mode = 1;
			}
		if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
			int d = kdev_t_to_nr(i->i_rdev);
			ea[0] = d & 0xff;
			ea[1] = (d >> 8) & 0xff;
			ea[2] = (d >> 16) & 0xff;
			ea[3] = d >> 24;
			hpfs_set_ea(i, fnode, "DEV", ea, 4);
		}
	}
}

void hpfs_write_inode(struct inode *i)
{
	struct inode *parent;
	if (!i->i_nlink) return;
	if (i->i_ino == i->i_sb->s_hpfs_root) return;
	if (i->i_hpfs_rddir_off && !i->i_count) {
		if (*i->i_hpfs_rddir_off) printk("HPFS: write_inode: some position still there\n");
		kfree(i->i_hpfs_rddir_off);
		i->i_hpfs_rddir_off = NULL;
	}
	i->i_hpfs_dirty = 0;
	hpfs_lock_iget(i->i_sb, 1);
	parent = iget(i->i_sb, i->i_hpfs_parent_dir);
	hpfs_unlock_iget(i->i_sb);
	hpfs_lock_inode(parent);
	hpfs_write_inode_nolock(i);
	hpfs_unlock_inode(parent);
	iput(parent);
}

void hpfs_write_inode_nolock(struct inode *i)
{
	struct buffer_head *bh;
	struct fnode *fnode;
	struct quad_buffer_head qbh;
	struct hpfs_dirent *de;
	if (i->i_ino == i->i_sb->s_hpfs_root) return;
	if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
	if (i->i_ino != i->i_sb->s_hpfs_root) {
		if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
			brelse(bh);
			return;
		}
	} else de = NULL;
	if (S_ISREG(i->i_mode)) {
		fnode->file_size = de->file_size = i->i_size;
	} else if (S_ISDIR(i->i_mode)) {
		fnode->file_size = de->file_size = 0;
	}
	hpfs_write_inode_ea(i, fnode);
	if (de) {
		de->write_date = gmt_to_local(i->i_sb, i->i_mtime);
		de->read_date = gmt_to_local(i->i_sb, i->i_atime);
		de->creation_date = gmt_to_local(i->i_sb, i->i_ctime);
		de->read_only = !(i->i_mode & 0222);
		de->ea_size = i->i_hpfs_ea_size;
		hpfs_mark_4buffers_dirty(&qbh);
		hpfs_brelse4(&qbh);
	}
	if (S_ISDIR(i->i_mode)) {
		if ((de = map_dirent(i, i->i_hpfs_dno, "\001\001", 2, NULL, &qbh))) {
			de->write_date = gmt_to_local(i->i_sb, i->i_mtime);
			de->read_date = gmt_to_local(i->i_sb, i->i_atime);
			de->creation_date = gmt_to_local(i->i_sb, i->i_ctime);
			de->read_only = !(i->i_mode & 0222);
			de->ea_size = /*i->i_hpfs_ea_size*/0;
			de->file_size = 0;
			hpfs_mark_4buffers_dirty(&qbh);
			hpfs_brelse4(&qbh);
		} else hpfs_error(i->i_sb, "directory %08x doesn't have '.' entry", i->i_ino);
	}
	mark_buffer_dirty(bh, 1);
	brelse(bh);
}

int hpfs_notify_change(struct dentry *dentry, struct iattr *attr)
{
	struct inode *inode = dentry->d_inode;
	int error;
	if (inode->i_sb->s_hpfs_root == inode->i_ino) return -EINVAL;
	if ((error = inode_change_ok(inode, attr))) return error;
	inode_setattr(inode, attr);
	hpfs_write_inode(inode);
	return 0;
}

void hpfs_write_if_changed(struct inode *inode)
{
	if (inode->i_hpfs_dirty) {
		hpfs_write_inode(inode);
	}
}

void hpfs_delete_inode(struct inode *inode)
{
	hpfs_remove_fnode(inode->i_sb, inode->i_ino);
}