File: filesystem.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (454 lines) | stat: -rw-r--r-- 10,031 bytes parent folder | download
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Copyright (c) 1997, 1998, 1999 The University of Utah and the Flux Group.
 * 
 * This file is part of the OSKit Linux Glue Libraries, which are free
 * software, also known as "open source;" you can redistribute them and/or
 * modify them under the terms of the GNU General Public License (GPL),
 * version 2, as published by the Free Software Foundation (FSF).
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Implements the oskit_filesystem_t COM interface.
 */

#include <oskit/io/blkio.h>
#include <oskit/fs/filesystem.h>
#include <oskit/fs/dir.h>

#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/malloc.h>

#include <assert.h>

#include "current.h"
#include "dev.h"
#include "errno.h"
#include "types.h"

static OSKIT_COMDECL
filesystem_query(oskit_filesystem_t *f,
		 const struct oskit_guid *iid,
		 void **out_ihandle)
{
	gfilesystem_t *fs;

	fs = (gfilesystem_t *)f;
	VERIFY1(fs, fs->sb);
    
	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_filesystem_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &fs->fsi;
		++fs->count;
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;    
}

static OSKIT_COMDECL_U
filesystem_addref(oskit_filesystem_t *f)
{
	gfilesystem_t *fs;

	fs = (gfilesystem_t *)f;
	VERIFY_OR_PANIC(fs, "filesystem");

	return ++fs->count;
}

/*
 * This needs to undo what fs_linux_mount and gfilesystem_create do.
 */
static OSKIT_COMDECL_U
filesystem_release(oskit_filesystem_t *f)
{
	gfilesystem_t *fs;
	oskit_error_t err;
	unsigned newcount;
    
	fs = (gfilesystem_t *)f;
	VERIFY_OR_PANIC(fs, "filesystem");

	newcount = --fs->count;
	if (newcount == 0) {
		/* Temporarily bump the count so the unmount won't fail. */
		fs->count++;
		err = oskit_filesystem_unmount(f);
		assert(err == 0);
		kfree(fs);
	}

	return newcount;
}

/*
 * Obtain filesystem statistics.
 *
 * This is basically the innards of Linux's fs/open.c:sys_statfs.
 */
static OSKIT_COMDECL
filesystem_statfs(oskit_filesystem_t *f, oskit_statfs_t *out_stats)
{
	gfilesystem_t *fs;
	oskit_error_t err;
	struct statfs stats;
	struct task_struct ts;
	unsigned long flags;

	fs = (gfilesystem_t *)f;
	VERIFY1(fs, fs->sb);

	if (fs->sb->s_op->statfs == NULL)
		return OSKIT_E_INVALIDARG;

	err = fs_linux_create_current(&ts);
	if (err)
		return err;
	fs->sb->s_op->statfs(fs->sb, &stats, sizeof stats);
	fs_linux_destroy_current();

	out_stats->bsize   = stats.f_bsize;
	out_stats->frsize  = stats.f_bsize;
	out_stats->blocks  = stats.f_blocks;
	out_stats->bfree   = stats.f_bfree;
	out_stats->bavail  = stats.f_bavail;
	out_stats->files   = stats.f_files;
	out_stats->ffree   = stats.f_ffree;
	out_stats->favail  = stats.f_ffree;
	out_stats->fsid	   = stats.f_fsid.val[0];
	flags		   = fs->sb->s_flags;
	out_stats->namemax = stats.f_namelen;

	out_stats->flag = 0;
	if (flags & MS_RDONLY) out_stats->flag |= OSKIT_FS_RDONLY;
	if (flags & MS_NOEXEC) out_stats->flag |= OSKIT_FS_NOEXEC;
	if (flags & MS_NOSUID) out_stats->flag |= OSKIT_FS_NOSUID;
	if (flags & MS_NODEV)  out_stats->flag |= OSKIT_FS_NODEV;

	return 0;
}

/*
 * Sync the filesystem, optionally waiting for writes to complete.
 *
 * In Linux, sync_dev doesn't wait but fsync_dev does.
 */
static OSKIT_COMDECL
filesystem_sync(oskit_filesystem_t *f, oskit_bool_t wait)
{
	gfilesystem_t *fs;
	oskit_error_t err;
	struct task_struct ts;
	kdev_t dev;

	fs = (gfilesystem_t *)f;
	VERIFY1(fs, fs->sb);

	err = fs_linux_create_current(&ts);
	if (err)
		return err;

	dev = fs->sb->s_dev;
	if (wait && fsync_dev(dev) != 0)
		/* fsync_dev can return 1 if there was an IO error. */
		return OSKIT_EIO;
	else
		sync_dev(dev);

	fs_linux_destroy_current();

	return 0;
}

/*
 * Get a handle to the root dir.
 *
 * Linux stores a pointer to the dentry of the root dir in the
 * super_block struct as s_root.
 */
static OSKIT_COMDECL
filesystem_getroot(oskit_filesystem_t *f, oskit_dir_t **out_dir)
{
	gfilesystem_t *fs;
	gdir_t *dir;
	oskit_error_t err;

	fs = (gfilesystem_t *)f;
	VERIFY1(fs, fs->sb);

	if (out_dir == NULL)
		return OSKIT_E_INVALIDARG;

	err = gfile_create(fs, fs->sb->s_root, (gfile_t **)&dir);
	if (err)
		return err;
	*out_dir = &dir->diri;

	return 0;
}

/*
 * Remount the filesystem.
 *
 * This is basically the innards of Linux's fs/super.c:do_remount_sb.
 */
static OSKIT_COMDECL
filesystem_remount(oskit_filesystem_t *f, oskit_u32_t flags)
{
	gfilesystem_t *fs;
	oskit_error_t err;
	struct task_struct ts;
	struct super_block *sb;
	int linuxerr;

	fs = (gfilesystem_t *)f;
	VERIFY1(fs, fs->sb);

	err = fs_linux_create_current(&ts);
	if (err)
		return err;
	
	sb = fs->sb;
	if (sb->s_op && sb->s_op->remount_fs) {
		linuxerr = sb->s_op->remount_fs(sb, &flags, 0);
		if (linuxerr)
			return errno_to_oskit_error(-linuxerr);
	}
	sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) |
		(flags & MS_RMT_MASK);

	fs_linux_destroy_current();

	return 0;
}

/*
 * Unmount the filesystem regardless of if there are still references to us.
 * This is like shortcutting oskit_fileystem_release and is discouraged.
 * Any references to this fs or files within it after unmount will yield
 * undefined results.
 *
 * This is intended to be like Linux's fs/super.c:sys_umount and do_umount,
 * mostly the latter.
 */
static OSKIT_COMDECL
filesystem_unmount(oskit_filesystem_t *f)
{
	gfilesystem_t *fs;
	oskit_error_t err;
	struct task_struct ts;
	struct super_block *sb;
	kdev_t dev;

	fs = (gfilesystem_t *)f;
	VERIFY1(fs, fs->sb);

	err = fs_linux_create_current(&ts);
	if (err)
		return err;

	sb = fs->sb;
	dev = sb->s_dev;

	if (sb->s_op->umount_begin)
		sb->s_op->umount_begin(sb);

	/*
	 * Shrink dcache, then fsync. This guarantees that if the
	 * filesystem is quiescent at this point, then (a) only the
	 * root entry should be in use and (b) that root entry is
	 * clean.
	 */
	shrink_dcache_sb(sb);
	fsync_dev(dev);

	err = d_umount(sb);
	if (err)
		return err;

	if (sb->s_op) {
		if (sb->s_op->put_super)
			sb->s_op->put_super(sb);
	}

	/* Forget any remaining inodes */
	if (invalidate_inodes(sb)) {
		printk("VFS: Busy inodes after unmount. "
			"Self-destruct in 5 seconds.  Have a nice day...\n");
	}

	sb->s_dev = 0;		/* Free the superblock */

	fsync_dev(dev);

	fs_linux_devtab_delete(dev);
	fs_linux_destroy_current();

	return 0;
}

static OSKIT_COMDECL
filesystem_lookupi(oskit_filesystem_t *f, oskit_ino_t ino,
		   oskit_file_t **out_file)
{
	gfilesystem_t *fs;
	oskit_error_t err = 0;
	struct inode *inode;
	struct dentry *dentry;
	struct task_struct ts;
	gfile_t *file;

	fs = (gfilesystem_t *)f;
	VERIFY1(fs, fs->sb);

/**/	err = fs_linux_create_current(&ts);
	if (err)
		return err;

	/* Get the dentry. */
/**/	inode = iget(fs->sb, ino);
	if (inode == NULL) {
		fs_linux_destroy_current();
		return OSKIT_E_FAIL;
	}

	/* Create a dentry. This is kinda bogus, but so is lookup by inode */
/**/	dentry = d_alloc(NULL, &(const struct qstr) { "", 0, 0 });
	if (dentry == NULL) {
		iput(inode);
		fs_linux_destroy_current();
		return OSKIT_E_FAIL;
	}
	d_instantiate(dentry, inode);
	dentry->d_sb = inode->i_sb;

	/* Make a file from the dentry. */
/**/	err = gfile_create(fs, dentry, &file);
	if (err) {
		iput(inode);
		fs_linux_destroy_current();
		return err;
	}
	*out_file = &file->filei;

	iput(inode);			/* `file' has a ref */
	fs_linux_destroy_current();
	return err;
}

static struct oskit_filesystem_ops fs_ops = {
	filesystem_query,
	filesystem_addref,
	filesystem_release,
	filesystem_statfs,
	filesystem_sync,
	filesystem_getroot,
	filesystem_remount,
	filesystem_unmount,
	filesystem_lookupi,
};



/*
 * Create and initialize a gfilesystem_t.
 */
static oskit_error_t
gfilesystem_create(struct super_block *sb, gfilesystem_t **out_fs)
{
	gfilesystem_t *fs;

	if (out_fs == NULL)
		return OSKIT_E_INVALIDARG;

	fs = kmalloc(sizeof(gfilesystem_t), GFP_KERNEL);
	if (fs == NULL)
		return OSKIT_ENOMEM;
	fs->fsi.ops = &fs_ops;
	fs->count = 1;
	fs->sb = sb;
	*out_fs = fs;

	return 0;

}

/*
 * This is a front end for Linux's mount_root,
 * which fills in a super_block struct based on what it finds in the partition.
 *
 * oskit_filesystem_release is the counterpart to this.
 */
oskit_error_t
fs_linux_mount(oskit_blkio_t *bio, oskit_u32_t flags,
	       oskit_filesystem_t **out_fs)
{
	int linux_flags;
	kdev_t dev;
	struct super_block *sb;
	int linux_err;
	struct task_struct ts;
	oskit_error_t err;
	gfilesystem_t *fs;

	if (out_fs == NULL)
		return OSKIT_E_INVALIDARG;

	/*
	 * Insert this device into the devtab and get a kdev_t for it.
	 */
	err = fs_linux_devtab_insert(bio, &dev);
	if (err)
		return err;

	/*
	 * Convert `flags' into Linux form.
	 */
	linux_flags = 0;
	if (flags & OSKIT_FS_RDONLY) linux_flags |= MS_RDONLY;
	if (flags & OSKIT_FS_NOEXEC) linux_flags |= MS_NOEXEC;
	if (flags & OSKIT_FS_NOSUID) linux_flags |= MS_NOSUID;
	if (flags & OSKIT_FS_NODEV)  linux_flags |= MS_NODEV;

	/*
	 * Call a modified version of Linux's mount_root that takes
	 * parameters rather then using ROOT_DEV and root_mountflags.
	 */
	err = fs_linux_create_current(&ts);
	if (err) {
		fs_linux_devtab_delete(dev);
		return err;
	}
	linux_err = mount_root(dev, linux_flags, &sb);
	if (linux_err) {
		printk("%s: Linux mount_root failed %d\n",
		       __FUNCTION__, linux_err);
		fs_linux_devtab_delete(dev);
		fs_linux_destroy_current();
		return OSKIT_EIO;
	}
	fs_linux_destroy_current();

	/*
	 * Now, create something to return.
	 */
	err = gfilesystem_create(sb, &fs);
	if (err) {
		/* XXX need to split unmount functionality out of
                   filesystem_unmount and do that here.
		   Will still need current to be valid. */
		fs_linux_devtab_delete(dev);
		return err;
	}
	*out_fs = &fs->fsi;

	return 0;
}