hostfs_kern.c 22.4 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10
 * Licensed under the GPL
 *
 * Ported the filesystem routines to 2.5.
 * 2003-02-10 Petr Baudis <pasky@ucw.cz>
 */

#include <linux/fs.h>
#include <linux/module.h>
J
Jeff Dike 已提交
11
#include <linux/mm.h>
L
Linus Torvalds 已提交
12 13
#include <linux/pagemap.h>
#include <linux/statfs.h>
14
#include <linux/slab.h>
M
Miklos Szeredi 已提交
15
#include <linux/seq_file.h>
J
Jiri Kosina 已提交
16
#include <linux/mount.h>
L
Linus Torvalds 已提交
17 18
#include "hostfs.h"
#include "init.h"
J
Jeff Dike 已提交
19
#include "kern.h"
L
Linus Torvalds 已提交
20 21 22 23

struct hostfs_inode_info {
	char *host_filename;
	int fd;
24
	fmode_t mode;
L
Linus Torvalds 已提交
25 26 27 28 29
	struct inode vfs_inode;
};

static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
{
J
Jeff Dike 已提交
30
	return list_entry(inode, struct hostfs_inode_info, vfs_inode);
L
Linus Torvalds 已提交
31 32
}

J
Josef Sipek 已提交
33
#define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
L
Linus Torvalds 已提交
34

35
static int hostfs_d_delete(struct dentry *dentry)
L
Linus Torvalds 已提交
36
{
J
Jeff Dike 已提交
37
	return 1;
L
Linus Torvalds 已提交
38 39
}

40
static const struct dentry_operations hostfs_dentry_ops = {
L
Linus Torvalds 已提交
41 42 43 44
	.d_delete		= hostfs_d_delete,
};

/* Changed in hostfs_args before the kernel starts running */
45
static char *root_ino = "";
L
Linus Torvalds 已提交
46 47 48 49
static int append = 0;

#define HOSTFS_SUPER_MAGIC 0x00c0ffee

50 51
static const struct inode_operations hostfs_iops;
static const struct inode_operations hostfs_dir_iops;
52
static const struct address_space_operations hostfs_link_aops;
L
Linus Torvalds 已提交
53 54 55 56 57 58 59

#ifndef MODULE
static int __init hostfs_args(char *options, int *add)
{
	char *ptr;

	ptr = strchr(options, ',');
J
Jeff Dike 已提交
60
	if (ptr != NULL)
L
Linus Torvalds 已提交
61
		*ptr++ = '\0';
J
Jeff Dike 已提交
62
	if (*options != '\0')
L
Linus Torvalds 已提交
63 64 65
		root_ino = options;

	options = ptr;
J
Jeff Dike 已提交
66
	while (options) {
L
Linus Torvalds 已提交
67
		ptr = strchr(options, ',');
J
Jeff Dike 已提交
68
		if (ptr != NULL)
L
Linus Torvalds 已提交
69
			*ptr++ = '\0';
J
Jeff Dike 已提交
70 71
		if (*options != '\0') {
			if (!strcmp(options, "append"))
L
Linus Torvalds 已提交
72 73 74 75 76 77
				append = 1;
			else printf("hostfs_args - unsupported option - %s\n",
				    options);
		}
		options = ptr;
	}
J
Jeff Dike 已提交
78
	return 0;
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

__uml_setup("hostfs=", hostfs_args,
"hostfs=<root dir>,<flags>,...\n"
"    This is used to set hostfs parameters.  The root directory argument\n"
"    is used to confine all hostfs mounts to within the specified directory\n"
"    tree on the host.  If this isn't specified, then a user inside UML can\n"
"    mount anything on the host that's accessible to the user that's running\n"
"    it.\n"
"    The only flag currently supported is 'append', which specifies that all\n"
"    files opened by hostfs will be opened in append mode.\n\n"
);
#endif

static char *dentry_name(struct dentry *dentry, int extra)
{
	struct dentry *parent;
	char *root, *name;
	int len;

	len = 0;
	parent = dentry;
J
Jeff Dike 已提交
101
	while (parent->d_parent != parent) {
L
Linus Torvalds 已提交
102 103 104 105 106 107 108
		len += parent->d_name.len + 1;
		parent = parent->d_parent;
	}

	root = HOSTFS_I(parent->d_inode)->host_filename;
	len += strlen(root);
	name = kmalloc(len + extra + 1, GFP_KERNEL);
J
Jeff Dike 已提交
109
	if (name == NULL)
J
Jeff Dike 已提交
110
		return NULL;
L
Linus Torvalds 已提交
111 112 113

	name[len] = '\0';
	parent = dentry;
J
Jeff Dike 已提交
114
	while (parent->d_parent != parent) {
L
Linus Torvalds 已提交
115 116 117 118 119 120 121
		len -= parent->d_name.len + 1;
		name[len] = '/';
		strncpy(&name[len + 1], parent->d_name.name,
			parent->d_name.len);
		parent = parent->d_parent;
	}
	strncpy(name, root, strlen(root));
J
Jeff Dike 已提交
122
	return name;
L
Linus Torvalds 已提交
123 124 125 126 127 128 129
}

static char *inode_name(struct inode *ino, int extra)
{
	struct dentry *dentry;

	dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
J
Jeff Dike 已提交
130
	return dentry_name(dentry, extra);
L
Linus Torvalds 已提交
131 132 133 134
}

static int read_name(struct inode *ino, char *name)
{
J
Jeff Dike 已提交
135 136
	/*
	 * The non-int inode fields are copied into ints by stat_file and
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147
	 * then copied into the inode because passing the actual pointers
	 * in and having them treated as int * breaks on big-endian machines
	 */
	int err;
	int i_mode, i_nlink, i_blksize;
	unsigned long long i_size;
	unsigned long long i_ino;
	unsigned long long i_blocks;

	err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
			&ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
148
			&ino->i_ctime, &i_blksize, &i_blocks, -1);
J
Jeff Dike 已提交
149
	if (err)
J
Jeff Dike 已提交
150
		return err;
L
Linus Torvalds 已提交
151 152 153 154 155 156

	ino->i_ino = i_ino;
	ino->i_mode = i_mode;
	ino->i_nlink = i_nlink;
	ino->i_size = i_size;
	ino->i_blocks = i_blocks;
J
Jeff Dike 已提交
157
	return 0;
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165
}

static char *follow_link(char *link)
{
	int len, n;
	char *name, *resolved, *end;

	len = 64;
J
Jeff Dike 已提交
166
	while (1) {
L
Linus Torvalds 已提交
167 168
		n = -ENOMEM;
		name = kmalloc(len, GFP_KERNEL);
J
Jeff Dike 已提交
169
		if (name == NULL)
L
Linus Torvalds 已提交
170 171
			goto out;

172
		n = hostfs_do_readlink(link, name, len);
J
Jeff Dike 已提交
173
		if (n < len)
L
Linus Torvalds 已提交
174 175 176 177
			break;
		len *= 2;
		kfree(name);
	}
J
Jeff Dike 已提交
178
	if (n < 0)
L
Linus Torvalds 已提交
179 180
		goto out_free;

J
Jeff Dike 已提交
181
	if (*name == '/')
J
Jeff Dike 已提交
182
		return name;
L
Linus Torvalds 已提交
183 184

	end = strrchr(link, '/');
J
Jeff Dike 已提交
185
	if (end == NULL)
J
Jeff Dike 已提交
186
		return name;
L
Linus Torvalds 已提交
187 188 189 190 191

	*(end + 1) = '\0';
	len = strlen(link) + strlen(name) + 1;

	resolved = kmalloc(len, GFP_KERNEL);
J
Jeff Dike 已提交
192
	if (resolved == NULL) {
L
Linus Torvalds 已提交
193 194 195 196 197 198 199
		n = -ENOMEM;
		goto out_free;
	}

	sprintf(resolved, "%s%s", link, name);
	kfree(name);
	kfree(link);
J
Jeff Dike 已提交
200
	return resolved;
L
Linus Torvalds 已提交
201 202 203 204

 out_free:
	kfree(name);
 out:
J
Jeff Dike 已提交
205
	return ERR_PTR(n);
L
Linus Torvalds 已提交
206 207
}

208
static int hostfs_read_inode(struct inode *ino)
L
Linus Torvalds 已提交
209 210 211 212
{
	char *name;
	int err = 0;

J
Jeff Dike 已提交
213 214
	/*
	 * Unfortunately, we are called from iget() when we don't have a dentry
L
Linus Torvalds 已提交
215 216
	 * allocated yet.
	 */
J
Jeff Dike 已提交
217
	if (list_empty(&ino->i_dentry))
L
Linus Torvalds 已提交
218 219 220 221
		goto out;

	err = -ENOMEM;
	name = inode_name(ino, 0);
J
Jeff Dike 已提交
222
	if (name == NULL)
L
Linus Torvalds 已提交
223 224
		goto out;

J
Jeff Dike 已提交
225
	if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
L
Linus Torvalds 已提交
226
		name = follow_link(name);
J
Jeff Dike 已提交
227
		if (IS_ERR(name)) {
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235
			err = PTR_ERR(name);
			goto out;
		}
	}

	err = read_name(ino, name);
	kfree(name);
 out:
J
Jeff Dike 已提交
236
	return err;
L
Linus Torvalds 已提交
237 238
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static struct inode *hostfs_iget(struct super_block *sb)
{
	struct inode *inode;
	long ret;

	inode = iget_locked(sb, 0);
	if (!inode)
		return ERR_PTR(-ENOMEM);
	if (inode->i_state & I_NEW) {
		ret = hostfs_read_inode(inode);
		if (ret < 0) {
			iget_failed(inode);
			return ERR_PTR(ret);
		}
		unlock_new_inode(inode);
	}
	return inode;
}

258
int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
L
Linus Torvalds 已提交
259
{
J
Jeff Dike 已提交
260 261
	/*
	 * do_statfs uses struct statfs64 internally, but the linux kernel
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271
	 * struct statfs still has 32-bit versions for most of these fields,
	 * so we convert them here
	 */
	int err;
	long long f_blocks;
	long long f_bfree;
	long long f_bavail;
	long long f_files;
	long long f_ffree;

272
	err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
L
Linus Torvalds 已提交
273 274 275
			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
			&sf->f_namelen, sf->f_spare);
J
Jeff Dike 已提交
276
	if (err)
J
Jeff Dike 已提交
277
		return err;
L
Linus Torvalds 已提交
278 279 280 281 282 283
	sf->f_blocks = f_blocks;
	sf->f_bfree = f_bfree;
	sf->f_bavail = f_bavail;
	sf->f_files = f_files;
	sf->f_ffree = f_ffree;
	sf->f_type = HOSTFS_SUPER_MAGIC;
J
Jeff Dike 已提交
284
	return 0;
L
Linus Torvalds 已提交
285 286 287 288 289 290 291
}

static struct inode *hostfs_alloc_inode(struct super_block *sb)
{
	struct hostfs_inode_info *hi;

	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
J
Jeff Dike 已提交
292
	if (hi == NULL)
J
Jeff Dike 已提交
293
		return NULL;
L
Linus Torvalds 已提交
294 295 296 297 298

	*hi = ((struct hostfs_inode_info) { .host_filename	= NULL,
					    .fd			= -1,
					    .mode		= 0 });
	inode_init_once(&hi->vfs_inode);
J
Jeff Dike 已提交
299
	return &hi->vfs_inode;
L
Linus Torvalds 已提交
300 301 302 303
}

static void hostfs_delete_inode(struct inode *inode)
{
304
	truncate_inode_pages(&inode->i_data, 0);
J
Jeff Dike 已提交
305
	if (HOSTFS_I(inode)->fd != -1) {
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313
		close_file(&HOSTFS_I(inode)->fd);
		HOSTFS_I(inode)->fd = -1;
	}
	clear_inode(inode);
}

static void hostfs_destroy_inode(struct inode *inode)
{
J
Jesper Juhl 已提交
314
	kfree(HOSTFS_I(inode)->host_filename);
L
Linus Torvalds 已提交
315

J
Jeff Dike 已提交
316 317 318 319 320
	/*
	 * XXX: This should not happen, probably. The check is here for
	 * additional safety.
	 */
	if (HOSTFS_I(inode)->fd != -1) {
L
Linus Torvalds 已提交
321 322 323 324 325 326 327
		close_file(&HOSTFS_I(inode)->fd);
		printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
	}

	kfree(HOSTFS_I(inode));
}

M
Miklos Szeredi 已提交
328 329 330 331 332 333 334 335 336 337 338 339
static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
{
	struct inode *root = vfs->mnt_sb->s_root->d_inode;
	const char *root_path = HOSTFS_I(root)->host_filename;
	size_t offset = strlen(root_ino) + 1;

	if (strlen(root_path) > offset)
		seq_printf(seq, ",%s", root_path + offset);

	return 0;
}

340
static const struct super_operations hostfs_sbops = {
L
Linus Torvalds 已提交
341 342 343 344 345
	.alloc_inode	= hostfs_alloc_inode,
	.drop_inode	= generic_delete_inode,
	.delete_inode   = hostfs_delete_inode,
	.destroy_inode	= hostfs_destroy_inode,
	.statfs		= hostfs_statfs,
M
Miklos Szeredi 已提交
346
	.show_options	= hostfs_show_options,
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355
};

int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
{
	void *dir;
	char *name;
	unsigned long long next, ino;
	int error, len;

J
Josef Sipek 已提交
356
	name = dentry_name(file->f_path.dentry, 0);
J
Jeff Dike 已提交
357
	if (name == NULL)
J
Jeff Dike 已提交
358
		return -ENOMEM;
L
Linus Torvalds 已提交
359 360
	dir = open_dir(name, &error);
	kfree(name);
J
Jeff Dike 已提交
361
	if (dir == NULL)
J
Jeff Dike 已提交
362
		return -error;
L
Linus Torvalds 已提交
363
	next = file->f_pos;
J
Jeff Dike 已提交
364
	while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
L
Linus Torvalds 已提交
365 366
		error = (*filldir)(ent, name, len, file->f_pos,
				   ino, DT_UNKNOWN);
J
Jeff Dike 已提交
367
		if (error) break;
L
Linus Torvalds 已提交
368 369 370
		file->f_pos = next;
	}
	close_dir(dir);
J
Jeff Dike 已提交
371
	return 0;
L
Linus Torvalds 已提交
372 373 374 375 376
}

int hostfs_file_open(struct inode *ino, struct file *file)
{
	char *name;
377 378
	fmode_t mode = 0;
	int r = 0, w = 0, fd;
L
Linus Torvalds 已提交
379 380

	mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
J
Jeff Dike 已提交
381
	if ((mode & HOSTFS_I(ino)->mode) == mode)
J
Jeff Dike 已提交
382
		return 0;
L
Linus Torvalds 已提交
383

J
Jeff Dike 已提交
384 385
	/*
	 * The file may already have been opened, but with the wrong access,
L
Linus Torvalds 已提交
386 387
	 * so this resets things and reopens the file with the new access.
	 */
J
Jeff Dike 已提交
388
	if (HOSTFS_I(ino)->fd != -1) {
L
Linus Torvalds 已提交
389 390 391 392 393
		close_file(&HOSTFS_I(ino)->fd);
		HOSTFS_I(ino)->fd = -1;
	}

	HOSTFS_I(ino)->mode |= mode;
J
Jeff Dike 已提交
394
	if (HOSTFS_I(ino)->mode & FMODE_READ)
L
Linus Torvalds 已提交
395
		r = 1;
J
Jeff Dike 已提交
396
	if (HOSTFS_I(ino)->mode & FMODE_WRITE)
L
Linus Torvalds 已提交
397
		w = 1;
J
Jeff Dike 已提交
398
	if (w)
L
Linus Torvalds 已提交
399 400
		r = 1;

J
Josef Sipek 已提交
401
	name = dentry_name(file->f_path.dentry, 0);
J
Jeff Dike 已提交
402
	if (name == NULL)
J
Jeff Dike 已提交
403
		return -ENOMEM;
L
Linus Torvalds 已提交
404 405 406

	fd = open_file(name, r, w, append);
	kfree(name);
J
Jeff Dike 已提交
407
	if (fd < 0)
J
Jeff Dike 已提交
408
		return fd;
L
Linus Torvalds 已提交
409 410
	FILE_HOSTFS_I(file)->fd = fd;

J
Jeff Dike 已提交
411
	return 0;
L
Linus Torvalds 已提交
412 413
}

414
int hostfs_fsync(struct file *file, int datasync)
L
Linus Torvalds 已提交
415
{
416
	return fsync_file(HOSTFS_I(file->f_mapping->host)->fd, datasync);
L
Linus Torvalds 已提交
417 418
}

419
static const struct file_operations hostfs_file_fops = {
L
Linus Torvalds 已提交
420
	.llseek		= generic_file_llseek,
421
	.read		= do_sync_read,
422
	.splice_read	= generic_file_splice_read,
L
Linus Torvalds 已提交
423 424
	.aio_read	= generic_file_aio_read,
	.aio_write	= generic_file_aio_write,
425
	.write		= do_sync_write,
L
Linus Torvalds 已提交
426 427 428 429 430 431
	.mmap		= generic_file_mmap,
	.open		= hostfs_file_open,
	.release	= NULL,
	.fsync		= hostfs_fsync,
};

432
static const struct file_operations hostfs_dir_fops = {
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	.llseek		= generic_file_llseek,
	.readdir	= hostfs_readdir,
	.read		= generic_read_dir,
};

int hostfs_writepage(struct page *page, struct writeback_control *wbc)
{
	struct address_space *mapping = page->mapping;
	struct inode *inode = mapping->host;
	char *buffer;
	unsigned long long base;
	int count = PAGE_CACHE_SIZE;
	int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
	int err;

	if (page->index >= end_index)
		count = inode->i_size & (PAGE_CACHE_SIZE-1);

	buffer = kmap(page);
	base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;

	err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
J
Jeff Dike 已提交
455
	if (err != count) {
L
Linus Torvalds 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
		ClearPageUptodate(page);
		goto out;
	}

	if (base > inode->i_size)
		inode->i_size = base;

	if (PageError(page))
		ClearPageError(page);
	err = 0;

 out:
	kunmap(page);

	unlock_page(page);
	return err;
}

int hostfs_readpage(struct file *file, struct page *page)
{
	char *buffer;
	long long start;
	int err = 0;

	start = (long long) page->index << PAGE_CACHE_SHIFT;
	buffer = kmap(page);
	err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
			PAGE_CACHE_SIZE);
J
Jeff Dike 已提交
484 485
	if (err < 0)
		goto out;
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495

	memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);

	flush_dcache_page(page);
	SetPageUptodate(page);
	if (PageError(page)) ClearPageError(page);
	err = 0;
 out:
	kunmap(page);
	unlock_page(page);
J
Jeff Dike 已提交
496
	return err;
L
Linus Torvalds 已提交
497 498
}

N
Nick Piggin 已提交
499 500 501
int hostfs_write_begin(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned flags,
			struct page **pagep, void **fsdata)
L
Linus Torvalds 已提交
502
{
N
Nick Piggin 已提交
503
	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
L
Linus Torvalds 已提交
504

505
	*pagep = grab_cache_page_write_begin(mapping, index, flags);
N
Nick Piggin 已提交
506 507 508
	if (!*pagep)
		return -ENOMEM;
	return 0;
L
Linus Torvalds 已提交
509 510
}

N
Nick Piggin 已提交
511 512 513
int hostfs_write_end(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned copied,
			struct page *page, void *fsdata)
L
Linus Torvalds 已提交
514 515
{
	struct inode *inode = mapping->host;
N
Nick Piggin 已提交
516 517 518
	void *buffer;
	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
	int err;
L
Linus Torvalds 已提交
519 520

	buffer = kmap(page);
N
Nick Piggin 已提交
521 522
	err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
	kunmap(page);
523

N
Nick Piggin 已提交
524 525
	if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
		SetPageUptodate(page);
526

J
Jeff Dike 已提交
527 528
	/*
	 * If err > 0, write_file has added err to pos, so we are comparing
N
Nick Piggin 已提交
529 530 531 532 533 534
	 * i_size against the last byte written.
	 */
	if (err > 0 && (pos > inode->i_size))
		inode->i_size = pos;
	unlock_page(page);
	page_cache_release(page);
L
Linus Torvalds 已提交
535

J
Jeff Dike 已提交
536
	return err;
L
Linus Torvalds 已提交
537 538
}

539
static const struct address_space_operations hostfs_aops = {
L
Linus Torvalds 已提交
540 541
	.writepage 	= hostfs_writepage,
	.readpage	= hostfs_readpage,
542
	.set_page_dirty = __set_page_dirty_nobuffers,
N
Nick Piggin 已提交
543 544
	.write_begin	= hostfs_write_begin,
	.write_end	= hostfs_write_end,
L
Linus Torvalds 已提交
545 546 547 548 549 550 551 552 553
};

static int init_inode(struct inode *inode, struct dentry *dentry)
{
	char *name;
	int type, err = -ENOMEM;
	int maj, min;
	dev_t rdev = 0;

J
Jeff Dike 已提交
554
	if (dentry) {
L
Linus Torvalds 已提交
555
		name = dentry_name(dentry, 0);
J
Jeff Dike 已提交
556
		if (name == NULL)
L
Linus Torvalds 已提交
557 558
			goto out;
		type = file_type(name, &maj, &min);
J
Jeff Dike 已提交
559
		/* Reencode maj and min with the kernel encoding.*/
L
Linus Torvalds 已提交
560 561 562 563 564 565
		rdev = MKDEV(maj, min);
		kfree(name);
	}
	else type = OS_TYPE_DIR;

	err = 0;
J
Jeff Dike 已提交
566
	if (type == OS_TYPE_SYMLINK)
L
Linus Torvalds 已提交
567
		inode->i_op = &page_symlink_inode_operations;
J
Jeff Dike 已提交
568
	else if (type == OS_TYPE_DIR)
L
Linus Torvalds 已提交
569 570 571
		inode->i_op = &hostfs_dir_iops;
	else inode->i_op = &hostfs_iops;

J
Jeff Dike 已提交
572
	if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
L
Linus Torvalds 已提交
573 574
	else inode->i_fop = &hostfs_file_fops;

J
Jeff Dike 已提交
575
	if (type == OS_TYPE_SYMLINK)
L
Linus Torvalds 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
		inode->i_mapping->a_ops = &hostfs_link_aops;
	else inode->i_mapping->a_ops = &hostfs_aops;

	switch (type) {
	case OS_TYPE_CHARDEV:
		init_special_inode(inode, S_IFCHR, rdev);
		break;
	case OS_TYPE_BLOCKDEV:
		init_special_inode(inode, S_IFBLK, rdev);
		break;
	case OS_TYPE_FIFO:
		init_special_inode(inode, S_IFIFO, 0);
		break;
	case OS_TYPE_SOCK:
		init_special_inode(inode, S_IFSOCK, 0);
		break;
	}
 out:
J
Jeff Dike 已提交
594
	return err;
L
Linus Torvalds 已提交
595 596 597
}

int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
J
Jeff Dike 已提交
598
		  struct nameidata *nd)
L
Linus Torvalds 已提交
599 600 601 602 603
{
	struct inode *inode;
	char *name;
	int error, fd;

604 605 606
	inode = hostfs_iget(dir->i_sb);
	if (IS_ERR(inode)) {
		error = PTR_ERR(inode);
J
Jeff Dike 已提交
607
		goto out;
608
	}
L
Linus Torvalds 已提交
609 610

	error = init_inode(inode, dentry);
J
Jeff Dike 已提交
611
	if (error)
L
Linus Torvalds 已提交
612 613 614 615
		goto out_put;

	error = -ENOMEM;
	name = dentry_name(dentry, 0);
J
Jeff Dike 已提交
616
	if (name == NULL)
L
Linus Torvalds 已提交
617 618 619 620 621 622
		goto out_put;

	fd = file_create(name,
			 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
			 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
			 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
J
Jeff Dike 已提交
623
	if (fd < 0)
L
Linus Torvalds 已提交
624 625 626 627
		error = fd;
	else error = read_name(inode, name);

	kfree(name);
J
Jeff Dike 已提交
628
	if (error)
L
Linus Torvalds 已提交
629 630 631 632 633
		goto out_put;

	HOSTFS_I(inode)->fd = fd;
	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
	d_instantiate(dentry, inode);
J
Jeff Dike 已提交
634
	return 0;
L
Linus Torvalds 已提交
635 636 637 638

 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
639
	return error;
L
Linus Torvalds 已提交
640 641 642
}

struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
J
Jeff Dike 已提交
643
			     struct nameidata *nd)
L
Linus Torvalds 已提交
644 645 646 647 648
{
	struct inode *inode;
	char *name;
	int err;

649 650 651
	inode = hostfs_iget(ino->i_sb);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
L
Linus Torvalds 已提交
652
		goto out;
653
	}
L
Linus Torvalds 已提交
654 655

	err = init_inode(inode, dentry);
J
Jeff Dike 已提交
656
	if (err)
L
Linus Torvalds 已提交
657 658 659 660
		goto out_put;

	err = -ENOMEM;
	name = dentry_name(dentry, 0);
J
Jeff Dike 已提交
661
	if (name == NULL)
L
Linus Torvalds 已提交
662 663 664 665
		goto out_put;

	err = read_name(inode, name);
	kfree(name);
J
Jeff Dike 已提交
666
	if (err == -ENOENT) {
L
Linus Torvalds 已提交
667 668 669
		iput(inode);
		inode = NULL;
	}
J
Jeff Dike 已提交
670
	else if (err)
L
Linus Torvalds 已提交
671 672 673 674
		goto out_put;

	d_add(dentry, inode);
	dentry->d_op = &hostfs_dentry_ops;
J
Jeff Dike 已提交
675
	return NULL;
L
Linus Torvalds 已提交
676 677 678 679

 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
680
	return ERR_PTR(err);
L
Linus Torvalds 已提交
681 682 683 684
}

static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
{
J
Jeff Dike 已提交
685
	char *file;
L
Linus Torvalds 已提交
686 687 688
	int len;

	file = inode_name(ino, dentry->d_name.len + 1);
J
Jeff Dike 已提交
689
	if (file == NULL)
J
Jeff Dike 已提交
690 691
		return NULL;
	strcat(file, "/");
L
Linus Torvalds 已提交
692
	len = strlen(file);
J
Jeff Dike 已提交
693
	strncat(file, dentry->d_name.name, dentry->d_name.len);
L
Linus Torvalds 已提交
694
	file[len + dentry->d_name.len] = '\0';
J
Jeff Dike 已提交
695
	return file;
L
Linus Torvalds 已提交
696 697 698 699
}

int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
{
J
Jeff Dike 已提交
700 701
	char *from_name, *to_name;
	int err;
L
Linus Torvalds 已提交
702

J
Jeff Dike 已提交
703
	if ((from_name = inode_dentry_name(ino, from)) == NULL)
J
Jeff Dike 已提交
704 705
		return -ENOMEM;
	to_name = dentry_name(to, 0);
J
Jeff Dike 已提交
706
	if (to_name == NULL) {
L
Linus Torvalds 已提交
707
		kfree(from_name);
J
Jeff Dike 已提交
708
		return -ENOMEM;
L
Linus Torvalds 已提交
709
	}
J
Jeff Dike 已提交
710 711 712 713
	err = link_file(to_name, from_name);
	kfree(from_name);
	kfree(to_name);
	return err;
L
Linus Torvalds 已提交
714 715 716 717 718 719 720
}

int hostfs_unlink(struct inode *ino, struct dentry *dentry)
{
	char *file;
	int err;

J
Jeff Dike 已提交
721
	if ((file = inode_dentry_name(ino, dentry)) == NULL)
J
Jeff Dike 已提交
722
		return -ENOMEM;
J
Jeff Dike 已提交
723
	if (append)
J
Jeff Dike 已提交
724
		return -EPERM;
L
Linus Torvalds 已提交
725 726 727

	err = unlink_file(file);
	kfree(file);
J
Jeff Dike 已提交
728
	return err;
L
Linus Torvalds 已提交
729 730 731 732 733 734 735
}

int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
{
	char *file;
	int err;

J
Jeff Dike 已提交
736
	if ((file = inode_dentry_name(ino, dentry)) == NULL)
J
Jeff Dike 已提交
737
		return -ENOMEM;
L
Linus Torvalds 已提交
738 739
	err = make_symlink(file, to);
	kfree(file);
J
Jeff Dike 已提交
740
	return err;
L
Linus Torvalds 已提交
741 742 743 744 745 746 747
}

int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
{
	char *file;
	int err;

J
Jeff Dike 已提交
748
	if ((file = inode_dentry_name(ino, dentry)) == NULL)
J
Jeff Dike 已提交
749
		return -ENOMEM;
L
Linus Torvalds 已提交
750 751
	err = do_mkdir(file, mode);
	kfree(file);
J
Jeff Dike 已提交
752
	return err;
L
Linus Torvalds 已提交
753 754 755 756 757 758 759
}

int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
{
	char *file;
	int err;

J
Jeff Dike 已提交
760
	if ((file = inode_dentry_name(ino, dentry)) == NULL)
J
Jeff Dike 已提交
761
		return -ENOMEM;
L
Linus Torvalds 已提交
762 763
	err = do_rmdir(file);
	kfree(file);
J
Jeff Dike 已提交
764
	return err;
L
Linus Torvalds 已提交
765 766 767 768 769 770
}

int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
{
	struct inode *inode;
	char *name;
771
	int err;
L
Linus Torvalds 已提交
772

773 774 775
	inode = hostfs_iget(dir->i_sb);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
L
Linus Torvalds 已提交
776
		goto out;
777
	}
L
Linus Torvalds 已提交
778 779

	err = init_inode(inode, dentry);
J
Jeff Dike 已提交
780
	if (err)
L
Linus Torvalds 已提交
781 782 783 784
		goto out_put;

	err = -ENOMEM;
	name = dentry_name(dentry, 0);
J
Jeff Dike 已提交
785
	if (name == NULL)
L
Linus Torvalds 已提交
786 787 788
		goto out_put;

	init_special_inode(inode, mode, dev);
J
Johannes Stezenbach 已提交
789
	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
J
Jeff Dike 已提交
790
	if (err)
L
Linus Torvalds 已提交
791 792 793 794
		goto out_free;

	err = read_name(inode, name);
	kfree(name);
J
Jeff Dike 已提交
795
	if (err)
L
Linus Torvalds 已提交
796 797 798
		goto out_put;

	d_instantiate(dentry, inode);
J
Jeff Dike 已提交
799
	return 0;
L
Linus Torvalds 已提交
800 801 802 803 804 805

 out_free:
	kfree(name);
 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
806
	return err;
L
Linus Torvalds 已提交
807 808 809 810 811 812 813 814
}

int hostfs_rename(struct inode *from_ino, struct dentry *from,
		  struct inode *to_ino, struct dentry *to)
{
	char *from_name, *to_name;
	int err;

J
Jeff Dike 已提交
815
	if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
J
Jeff Dike 已提交
816
		return -ENOMEM;
J
Jeff Dike 已提交
817
	if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
L
Linus Torvalds 已提交
818
		kfree(from_name);
J
Jeff Dike 已提交
819
		return -ENOMEM;
L
Linus Torvalds 已提交
820 821 822 823
	}
	err = rename_file(from_name, to_name);
	kfree(from_name);
	kfree(to_name);
J
Jeff Dike 已提交
824
	return err;
L
Linus Torvalds 已提交
825 826
}

827
int hostfs_permission(struct inode *ino, int desired)
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835
{
	char *name;
	int r = 0, w = 0, x = 0, err;

	if (desired & MAY_READ) r = 1;
	if (desired & MAY_WRITE) w = 1;
	if (desired & MAY_EXEC) x = 1;
	name = inode_name(ino, 0);
J
Jeff Dike 已提交
836 837
	if (name == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
838 839

	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
J
Jeff Dike 已提交
840
	    S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
L
Linus Torvalds 已提交
841 842 843 844
		err = 0;
	else
		err = access_file(name, r, w, x);
	kfree(name);
J
Jeff Dike 已提交
845
	if (!err)
L
Linus Torvalds 已提交
846 847 848 849 850 851
		err = generic_permission(ino, desired, NULL);
	return err;
}

int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
{
C
Christoph Hellwig 已提交
852
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
853 854 855 856
	struct hostfs_iattr attrs;
	char *name;
	int err;

C
Christoph Hellwig 已提交
857
	int fd = HOSTFS_I(inode)->fd;
858

C
Christoph Hellwig 已提交
859
	err = inode_change_ok(inode, attr);
L
Linus Torvalds 已提交
860 861 862
	if (err)
		return err;

J
Jeff Dike 已提交
863
	if (append)
L
Linus Torvalds 已提交
864 865 866
		attr->ia_valid &= ~ATTR_SIZE;

	attrs.ia_valid = 0;
J
Jeff Dike 已提交
867
	if (attr->ia_valid & ATTR_MODE) {
L
Linus Torvalds 已提交
868 869 870
		attrs.ia_valid |= HOSTFS_ATTR_MODE;
		attrs.ia_mode = attr->ia_mode;
	}
J
Jeff Dike 已提交
871
	if (attr->ia_valid & ATTR_UID) {
L
Linus Torvalds 已提交
872 873 874
		attrs.ia_valid |= HOSTFS_ATTR_UID;
		attrs.ia_uid = attr->ia_uid;
	}
J
Jeff Dike 已提交
875
	if (attr->ia_valid & ATTR_GID) {
L
Linus Torvalds 已提交
876 877 878
		attrs.ia_valid |= HOSTFS_ATTR_GID;
		attrs.ia_gid = attr->ia_gid;
	}
J
Jeff Dike 已提交
879
	if (attr->ia_valid & ATTR_SIZE) {
L
Linus Torvalds 已提交
880 881 882
		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
		attrs.ia_size = attr->ia_size;
	}
J
Jeff Dike 已提交
883
	if (attr->ia_valid & ATTR_ATIME) {
L
Linus Torvalds 已提交
884 885 886
		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
		attrs.ia_atime = attr->ia_atime;
	}
J
Jeff Dike 已提交
887
	if (attr->ia_valid & ATTR_MTIME) {
L
Linus Torvalds 已提交
888 889 890
		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
		attrs.ia_mtime = attr->ia_mtime;
	}
J
Jeff Dike 已提交
891
	if (attr->ia_valid & ATTR_CTIME) {
L
Linus Torvalds 已提交
892 893 894
		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
		attrs.ia_ctime = attr->ia_ctime;
	}
J
Jeff Dike 已提交
895
	if (attr->ia_valid & ATTR_ATIME_SET) {
L
Linus Torvalds 已提交
896 897
		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
	}
J
Jeff Dike 已提交
898
	if (attr->ia_valid & ATTR_MTIME_SET) {
L
Linus Torvalds 已提交
899 900 901
		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
	}
	name = dentry_name(dentry, 0);
J
Jeff Dike 已提交
902
	if (name == NULL)
J
Jeff Dike 已提交
903
		return -ENOMEM;
904
	err = set_attr(name, &attrs, fd);
L
Linus Torvalds 已提交
905
	kfree(name);
J
Jeff Dike 已提交
906
	if (err)
J
Jeff Dike 已提交
907
		return err;
L
Linus Torvalds 已提交
908

C
Christoph Hellwig 已提交
909 910 911 912 913 914 915 916 917 918 919 920
	if ((attr->ia_valid & ATTR_SIZE) &&
	    attr->ia_size != i_size_read(inode)) {
		int error;

		error = vmtruncate(inode, attr->ia_size);
		if (err)
			return err;
	}

	setattr_copy(inode, attr);
	mark_inode_dirty(inode);
	return 0;
L
Linus Torvalds 已提交
921 922
}

923
static const struct inode_operations hostfs_iops = {
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934 935
	.create		= hostfs_create,
	.link		= hostfs_link,
	.unlink		= hostfs_unlink,
	.symlink	= hostfs_symlink,
	.mkdir		= hostfs_mkdir,
	.rmdir		= hostfs_rmdir,
	.mknod		= hostfs_mknod,
	.rename		= hostfs_rename,
	.permission	= hostfs_permission,
	.setattr	= hostfs_setattr,
};

936
static const struct inode_operations hostfs_dir_iops = {
L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	.create		= hostfs_create,
	.lookup		= hostfs_lookup,
	.link		= hostfs_link,
	.unlink		= hostfs_unlink,
	.symlink	= hostfs_symlink,
	.mkdir		= hostfs_mkdir,
	.rmdir		= hostfs_rmdir,
	.mknod		= hostfs_mknod,
	.rename		= hostfs_rename,
	.permission	= hostfs_permission,
	.setattr	= hostfs_setattr,
};

int hostfs_link_readpage(struct file *file, struct page *page)
{
	char *buffer, *name;
	int err;

	buffer = kmap(page);
	name = inode_name(page->mapping->host, 0);
J
Jeff Dike 已提交
957
	if (name == NULL)
J
Jeff Dike 已提交
958
		return -ENOMEM;
959
	err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
960
	kfree(name);
J
Jeff Dike 已提交
961
	if (err == PAGE_CACHE_SIZE)
L
Linus Torvalds 已提交
962
		err = -E2BIG;
J
Jeff Dike 已提交
963
	else if (err > 0) {
L
Linus Torvalds 已提交
964 965 966 967 968 969 970
		flush_dcache_page(page);
		SetPageUptodate(page);
		if (PageError(page)) ClearPageError(page);
		err = 0;
	}
	kunmap(page);
	unlock_page(page);
J
Jeff Dike 已提交
971
	return err;
L
Linus Torvalds 已提交
972 973
}

974
static const struct address_space_operations hostfs_link_aops = {
L
Linus Torvalds 已提交
975 976 977 978 979 980
	.readpage	= hostfs_link_readpage,
};

static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
{
	struct inode *root_inode;
981
	char *host_root_path, *req_root = d;
L
Linus Torvalds 已提交
982 983 984 985 986 987
	int err;

	sb->s_blocksize = 1024;
	sb->s_blocksize_bits = 10;
	sb->s_magic = HOSTFS_SUPER_MAGIC;
	sb->s_op = &hostfs_sbops;
988
	sb->s_maxbytes = MAX_LFS_FILESIZE;
L
Linus Torvalds 已提交
989

990
	/* NULL is printed as <NULL> by sprintf: avoid that. */
991 992
	if (req_root == NULL)
		req_root = "";
L
Linus Torvalds 已提交
993 994

	err = -ENOMEM;
995 996
	host_root_path = kmalloc(strlen(root_ino) + 1
				 + strlen(req_root) + 1, GFP_KERNEL);
J
Jeff Dike 已提交
997
	if (host_root_path == NULL)
L
Linus Torvalds 已提交
998 999
		goto out;

1000
	sprintf(host_root_path, "%s/%s", root_ino, req_root);
L
Linus Torvalds 已提交
1001

1002 1003 1004
	root_inode = hostfs_iget(sb);
	if (IS_ERR(root_inode)) {
		err = PTR_ERR(root_inode);
L
Linus Torvalds 已提交
1005
		goto out_free;
1006
	}
L
Linus Torvalds 已提交
1007 1008

	err = init_inode(root_inode, NULL);
J
Jeff Dike 已提交
1009
	if (err)
L
Linus Torvalds 已提交
1010 1011
		goto out_put;

1012
	HOSTFS_I(root_inode)->host_filename = host_root_path;
J
Jeff Dike 已提交
1013 1014 1015 1016
	/*
	 * Avoid that in the error path, iput(root_inode) frees again
	 * host_root_path through hostfs_destroy_inode!
	 */
1017
	host_root_path = NULL;
L
Linus Torvalds 已提交
1018 1019 1020

	err = -ENOMEM;
	sb->s_root = d_alloc_root(root_inode);
J
Jeff Dike 已提交
1021
	if (sb->s_root == NULL)
L
Linus Torvalds 已提交
1022 1023
		goto out_put;

1024
	err = hostfs_read_inode(root_inode);
J
Jeff Dike 已提交
1025
	if (err) {
J
Jeff Dike 已提交
1026 1027 1028
		/* No iput in this case because the dput does that for us */
		dput(sb->s_root);
		sb->s_root = NULL;
1029
		goto out;
J
Jeff Dike 已提交
1030
	}
L
Linus Torvalds 已提交
1031

J
Jeff Dike 已提交
1032
	return 0;
L
Linus Torvalds 已提交
1033

J
Jeff Dike 已提交
1034 1035 1036
out_put:
	iput(root_inode);
out_free:
1037
	kfree(host_root_path);
J
Jeff Dike 已提交
1038 1039
out:
	return err;
L
Linus Torvalds 已提交
1040 1041
}

1042 1043 1044
static int hostfs_read_sb(struct file_system_type *type,
			  int flags, const char *dev_name,
			  void *data, struct vfsmount *mnt)
L
Linus Torvalds 已提交
1045
{
1046
	return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
L
Linus Torvalds 已提交
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
}

static struct file_system_type hostfs_type = {
	.owner 		= THIS_MODULE,
	.name 		= "hostfs",
	.get_sb 	= hostfs_read_sb,
	.kill_sb	= kill_anon_super,
	.fs_flags 	= 0,
};

static int __init init_hostfs(void)
{
J
Jeff Dike 已提交
1059
	return register_filesystem(&hostfs_type);
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
}

static void __exit exit_hostfs(void)
{
	unregister_filesystem(&hostfs_type);
}

module_init(init_hostfs)
module_exit(exit_hostfs)
MODULE_LICENSE("GPL");