hostfs_kern.c 20.5 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
 * Licensed under the GPL
 *
 * Ported the filesystem routines to 2.5.
 * 2003-02-10 Petr Baudis <pasky@ucw.cz>
 */

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

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

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

A
Al Viro 已提交
34
#define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
L
Linus Torvalds 已提交
35 36

/* Changed in hostfs_args before the kernel starts running */
37
static char *root_ino = "";
L
Linus Torvalds 已提交
38 39
static int append = 0;

40 41
static const struct inode_operations hostfs_iops;
static const struct inode_operations hostfs_dir_iops;
A
Al Viro 已提交
42
static const struct inode_operations hostfs_link_iops;
L
Linus Torvalds 已提交
43 44 45 46 47 48 49

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

	ptr = strchr(options, ',');
J
Jeff Dike 已提交
50
	if (ptr != NULL)
L
Linus Torvalds 已提交
51
		*ptr++ = '\0';
J
Jeff Dike 已提交
52
	if (*options != '\0')
L
Linus Torvalds 已提交
53 54 55
		root_ino = options;

	options = ptr;
J
Jeff Dike 已提交
56
	while (options) {
L
Linus Torvalds 已提交
57
		ptr = strchr(options, ',');
J
Jeff Dike 已提交
58
		if (ptr != NULL)
L
Linus Torvalds 已提交
59
			*ptr++ = '\0';
J
Jeff Dike 已提交
60 61
		if (*options != '\0') {
			if (!strcmp(options, "append"))
L
Linus Torvalds 已提交
62 63 64 65 66 67
				append = 1;
			else printf("hostfs_args - unsupported option - %s\n",
				    options);
		}
		options = ptr;
	}
J
Jeff Dike 已提交
68
	return 0;
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

__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

83
static char *__dentry_name(struct dentry *dentry, char *name)
L
Linus Torvalds 已提交
84
{
N
Nick Piggin 已提交
85
	char *p = dentry_path_raw(dentry, name, PATH_MAX);
86 87
	char *root;
	size_t len;
L
Linus Torvalds 已提交
88

89 90 91 92
	root = dentry->d_sb->s_fs_info;
	len = strlen(root);
	if (IS_ERR(p)) {
		__putname(name);
J
Jeff Dike 已提交
93
		return NULL;
L
Linus Torvalds 已提交
94
	}
95
	strlcpy(name, root, PATH_MAX);
96 97 98 99 100 101 102 103 104
	if (len > p - name) {
		__putname(name);
		return NULL;
	}
	if (p > name + len) {
		char *s = name + len;
		while ((*s++ = *p++) != '\0')
			;
	}
J
Jeff Dike 已提交
105
	return name;
L
Linus Torvalds 已提交
106 107
}

108 109 110 111 112 113
static char *dentry_name(struct dentry *dentry)
{
	char *name = __getname();
	if (!name)
		return NULL;

114
	return __dentry_name(dentry, name);
115 116
}

A
Al Viro 已提交
117
static char *inode_name(struct inode *ino)
L
Linus Torvalds 已提交
118 119
{
	struct dentry *dentry;
N
Nick Piggin 已提交
120
	char *name;
L
Linus Torvalds 已提交
121

N
Nick Piggin 已提交
122 123
	dentry = d_find_alias(ino);
	if (!dentry)
124
		return NULL;
N
Nick Piggin 已提交
125 126 127 128 129 130

	name = dentry_name(dentry);

	dput(dentry);

	return name;
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138
}

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

	len = 64;
J
Jeff Dike 已提交
139
	while (1) {
L
Linus Torvalds 已提交
140 141
		n = -ENOMEM;
		name = kmalloc(len, GFP_KERNEL);
J
Jeff Dike 已提交
142
		if (name == NULL)
L
Linus Torvalds 已提交
143 144
			goto out;

145
		n = hostfs_do_readlink(link, name, len);
J
Jeff Dike 已提交
146
		if (n < len)
L
Linus Torvalds 已提交
147 148 149 150
			break;
		len *= 2;
		kfree(name);
	}
J
Jeff Dike 已提交
151
	if (n < 0)
L
Linus Torvalds 已提交
152 153
		goto out_free;

J
Jeff Dike 已提交
154
	if (*name == '/')
J
Jeff Dike 已提交
155
		return name;
L
Linus Torvalds 已提交
156 157

	end = strrchr(link, '/');
J
Jeff Dike 已提交
158
	if (end == NULL)
J
Jeff Dike 已提交
159
		return name;
L
Linus Torvalds 已提交
160 161 162 163 164

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

	resolved = kmalloc(len, GFP_KERNEL);
J
Jeff Dike 已提交
165
	if (resolved == NULL) {
L
Linus Torvalds 已提交
166 167 168 169 170 171 172
		n = -ENOMEM;
		goto out_free;
	}

	sprintf(resolved, "%s%s", link, name);
	kfree(name);
	kfree(link);
J
Jeff Dike 已提交
173
	return resolved;
L
Linus Torvalds 已提交
174 175 176 177

 out_free:
	kfree(name);
 out:
J
Jeff Dike 已提交
178
	return ERR_PTR(n);
L
Linus Torvalds 已提交
179 180
}

181 182
static struct inode *hostfs_iget(struct super_block *sb)
{
A
Al Viro 已提交
183
	struct inode *inode = new_inode(sb);
184 185 186 187 188
	if (!inode)
		return ERR_PTR(-ENOMEM);
	return inode;
}

J
James Hogan 已提交
189
static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
L
Linus Torvalds 已提交
190
{
J
Jeff Dike 已提交
191 192
	/*
	 * do_statfs uses struct statfs64 internally, but the linux kernel
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200 201 202
	 * 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;

203
	err = do_statfs(dentry->d_sb->s_fs_info,
L
Linus Torvalds 已提交
204 205
			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
206
			&sf->f_namelen);
J
Jeff Dike 已提交
207
	if (err)
J
Jeff Dike 已提交
208
		return err;
L
Linus Torvalds 已提交
209 210 211 212 213 214
	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 已提交
215
	return 0;
L
Linus Torvalds 已提交
216 217 218 219 220 221
}

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

222
	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
J
Jeff Dike 已提交
223
	if (hi == NULL)
J
Jeff Dike 已提交
224
		return NULL;
225
	hi->fd = -1;
226
	hi->mode = 0;
L
Linus Torvalds 已提交
227
	inode_init_once(&hi->vfs_inode);
J
Jeff Dike 已提交
228
	return &hi->vfs_inode;
L
Linus Torvalds 已提交
229 230
}

231
static void hostfs_evict_inode(struct inode *inode)
L
Linus Torvalds 已提交
232
{
233
	truncate_inode_pages(&inode->i_data, 0);
234
	clear_inode(inode);
J
Jeff Dike 已提交
235
	if (HOSTFS_I(inode)->fd != -1) {
L
Linus Torvalds 已提交
236 237 238 239 240
		close_file(&HOSTFS_I(inode)->fd);
		HOSTFS_I(inode)->fd = -1;
	}
}

N
Nick Piggin 已提交
241
static void hostfs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
242
{
N
Nick Piggin 已提交
243
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
244 245
	kfree(HOSTFS_I(inode));
}
N
Nick Piggin 已提交
246 247 248 249 250

static void hostfs_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, hostfs_i_callback);
}
L
Linus Torvalds 已提交
251

252
static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
M
Miklos Szeredi 已提交
253
{
254
	const char *root_path = root->d_sb->s_fs_info;
M
Miklos Szeredi 已提交
255 256 257 258 259 260 261 262
	size_t offset = strlen(root_ino) + 1;

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

	return 0;
}

263
static const struct super_operations hostfs_sbops = {
L
Linus Torvalds 已提交
264 265
	.alloc_inode	= hostfs_alloc_inode,
	.destroy_inode	= hostfs_destroy_inode,
266
	.evict_inode	= hostfs_evict_inode,
L
Linus Torvalds 已提交
267
	.statfs		= hostfs_statfs,
M
Miklos Szeredi 已提交
268
	.show_options	= hostfs_show_options,
L
Linus Torvalds 已提交
269 270
};

J
James Hogan 已提交
271
static int hostfs_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
272 273 274 275 276
{
	void *dir;
	char *name;
	unsigned long long next, ino;
	int error, len;
277
	unsigned int type;
L
Linus Torvalds 已提交
278

A
Al Viro 已提交
279
	name = dentry_name(file->f_path.dentry);
J
Jeff Dike 已提交
280
	if (name == NULL)
J
Jeff Dike 已提交
281
		return -ENOMEM;
L
Linus Torvalds 已提交
282
	dir = open_dir(name, &error);
283
	__putname(name);
J
Jeff Dike 已提交
284
	if (dir == NULL)
J
Jeff Dike 已提交
285
		return -error;
A
Al Viro 已提交
286
	next = ctx->pos;
287
	while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
A
Al Viro 已提交
288 289 290
		if (!dir_emit(ctx, name, len, ino, type))
			break;
		ctx->pos = next;
L
Linus Torvalds 已提交
291 292
	}
	close_dir(dir);
J
Jeff Dike 已提交
293
	return 0;
L
Linus Torvalds 已提交
294 295
}

J
James Hogan 已提交
296
static int hostfs_file_open(struct inode *ino, struct file *file)
L
Linus Torvalds 已提交
297
{
298
	static DEFINE_MUTEX(open_mutex);
L
Linus Torvalds 已提交
299
	char *name;
300
	fmode_t mode = 0;
301
	int err;
302
	int r = 0, w = 0, fd;
L
Linus Torvalds 已提交
303 304

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

308
	mode |= HOSTFS_I(ino)->mode;
L
Linus Torvalds 已提交
309

310 311
retry:
	if (mode & FMODE_READ)
L
Linus Torvalds 已提交
312
		r = 1;
313
	if (mode & FMODE_WRITE)
L
Linus Torvalds 已提交
314
		w = 1;
J
Jeff Dike 已提交
315
	if (w)
L
Linus Torvalds 已提交
316 317
		r = 1;

A
Al Viro 已提交
318
	name = dentry_name(file->f_path.dentry);
J
Jeff Dike 已提交
319
	if (name == NULL)
J
Jeff Dike 已提交
320
		return -ENOMEM;
L
Linus Torvalds 已提交
321 322

	fd = open_file(name, r, w, append);
323
	__putname(name);
J
Jeff Dike 已提交
324
	if (fd < 0)
J
Jeff Dike 已提交
325
		return fd;
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

	mutex_lock(&open_mutex);
	/* somebody else had handled it first? */
	if ((mode & HOSTFS_I(ino)->mode) == mode) {
		mutex_unlock(&open_mutex);
		return 0;
	}
	if ((mode | HOSTFS_I(ino)->mode) != mode) {
		mode |= HOSTFS_I(ino)->mode;
		mutex_unlock(&open_mutex);
		close_file(&fd);
		goto retry;
	}
	if (HOSTFS_I(ino)->fd == -1) {
		HOSTFS_I(ino)->fd = fd;
	} else {
		err = replace_file(fd, HOSTFS_I(ino)->fd);
		close_file(&fd);
		if (err < 0) {
			mutex_unlock(&open_mutex);
			return err;
		}
	}
	HOSTFS_I(ino)->mode = mode;
	mutex_unlock(&open_mutex);
L
Linus Torvalds 已提交
351

J
Jeff Dike 已提交
352
	return 0;
L
Linus Torvalds 已提交
353 354
}

R
Richard Weinberger 已提交
355 356 357 358 359 360 361
static int hostfs_file_release(struct inode *inode, struct file *file)
{
	filemap_write_and_wait(inode->i_mapping);

	return 0;
}

J
James Hogan 已提交
362 363
static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
			int datasync)
L
Linus Torvalds 已提交
364
{
365 366 367 368 369 370 371 372 373 374 375 376
	struct inode *inode = file->f_mapping->host;
	int ret;

	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
	if (ret)
		return ret;

	mutex_lock(&inode->i_mutex);
	ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
	mutex_unlock(&inode->i_mutex);

	return ret;
L
Linus Torvalds 已提交
377 378
}

379
static const struct file_operations hostfs_file_fops = {
L
Linus Torvalds 已提交
380
	.llseek		= generic_file_llseek,
381
	.read		= do_sync_read,
382
	.splice_read	= generic_file_splice_read,
L
Linus Torvalds 已提交
383 384
	.aio_read	= generic_file_aio_read,
	.aio_write	= generic_file_aio_write,
385
	.write		= do_sync_write,
L
Linus Torvalds 已提交
386 387
	.mmap		= generic_file_mmap,
	.open		= hostfs_file_open,
R
Richard Weinberger 已提交
388
	.release	= hostfs_file_release,
L
Linus Torvalds 已提交
389 390 391
	.fsync		= hostfs_fsync,
};

392
static const struct file_operations hostfs_dir_fops = {
L
Linus Torvalds 已提交
393
	.llseek		= generic_file_llseek,
A
Al Viro 已提交
394
	.iterate	= hostfs_readdir,
L
Linus Torvalds 已提交
395 396 397
	.read		= generic_read_dir,
};

J
James Hogan 已提交
398
static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
{
	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 已提交
415
	if (err != count) {
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
		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;
}

J
James Hogan 已提交
434
static int hostfs_readpage(struct file *file, struct page *page)
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443
{
	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 已提交
444 445
	if (err < 0)
		goto out;
L
Linus Torvalds 已提交
446 447 448 449 450 451 452 453 454 455

	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 已提交
456
	return err;
L
Linus Torvalds 已提交
457 458
}

J
James Hogan 已提交
459 460 461
static 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 已提交
462
{
N
Nick Piggin 已提交
463
	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
L
Linus Torvalds 已提交
464

465
	*pagep = grab_cache_page_write_begin(mapping, index, flags);
N
Nick Piggin 已提交
466 467 468
	if (!*pagep)
		return -ENOMEM;
	return 0;
L
Linus Torvalds 已提交
469 470
}

J
James Hogan 已提交
471 472 473
static 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 已提交
474 475
{
	struct inode *inode = mapping->host;
N
Nick Piggin 已提交
476 477 478
	void *buffer;
	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
	int err;
L
Linus Torvalds 已提交
479 480

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

N
Nick Piggin 已提交
484 485
	if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
		SetPageUptodate(page);
486

J
Jeff Dike 已提交
487 488
	/*
	 * If err > 0, write_file has added err to pos, so we are comparing
N
Nick Piggin 已提交
489 490 491 492 493 494
	 * 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 已提交
495

J
Jeff Dike 已提交
496
	return err;
L
Linus Torvalds 已提交
497 498
}

499
static const struct address_space_operations hostfs_aops = {
L
Linus Torvalds 已提交
500 501
	.writepage 	= hostfs_writepage,
	.readpage	= hostfs_readpage,
502
	.set_page_dirty = __set_page_dirty_nobuffers,
N
Nick Piggin 已提交
503 504
	.write_begin	= hostfs_write_begin,
	.write_end	= hostfs_write_end,
L
Linus Torvalds 已提交
505 506
};

507
static int read_name(struct inode *ino, char *name)
L
Linus Torvalds 已提交
508
{
509 510 511 512 513
	dev_t rdev;
	struct hostfs_stat st;
	int err = stat_file(name, &st, -1);
	if (err)
		return err;
L
Linus Torvalds 已提交
514

A
Al Viro 已提交
515
	/* Reencode maj and min with the kernel encoding.*/
516
	rdev = MKDEV(st.maj, st.min);
L
Linus Torvalds 已提交
517

518 519
	switch (st.mode & S_IFMT) {
	case S_IFLNK:
A
Al Viro 已提交
520
		ino->i_op = &hostfs_link_iops;
L
Linus Torvalds 已提交
521
		break;
522 523 524
	case S_IFDIR:
		ino->i_op = &hostfs_dir_iops;
		ino->i_fop = &hostfs_dir_fops;
L
Linus Torvalds 已提交
525
		break;
526 527 528 529 530 531
	case S_IFCHR:
	case S_IFBLK:
	case S_IFIFO:
	case S_IFSOCK:
		init_special_inode(ino, st.mode & S_IFMT, rdev);
		ino->i_op = &hostfs_iops;
L
Linus Torvalds 已提交
532
		break;
533 534 535 536 537

	default:
		ino->i_op = &hostfs_iops;
		ino->i_fop = &hostfs_file_fops;
		ino->i_mapping->a_ops = &hostfs_aops;
L
Linus Torvalds 已提交
538
	}
539 540 541

	ino->i_ino = st.ino;
	ino->i_mode = st.mode;
M
Miklos Szeredi 已提交
542
	set_nlink(ino, st.nlink);
543 544
	i_uid_write(ino, st.uid);
	i_gid_write(ino, st.gid);
545 546 547 548 549 550
	ino->i_atime = st.atime;
	ino->i_mtime = st.mtime;
	ino->i_ctime = st.ctime;
	ino->i_size = st.size;
	ino->i_blocks = st.blocks;
	return 0;
L
Linus Torvalds 已提交
551 552
}

J
James Hogan 已提交
553 554
static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
			 bool excl)
L
Linus Torvalds 已提交
555 556 557 558 559
{
	struct inode *inode;
	char *name;
	int error, fd;

560 561 562
	inode = hostfs_iget(dir->i_sb);
	if (IS_ERR(inode)) {
		error = PTR_ERR(inode);
J
Jeff Dike 已提交
563
		goto out;
564
	}
L
Linus Torvalds 已提交
565 566

	error = -ENOMEM;
A
Al Viro 已提交
567
	name = dentry_name(dentry);
J
Jeff Dike 已提交
568
	if (name == NULL)
L
Linus Torvalds 已提交
569 570 571 572 573 574
		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);
575
	if (fd < 0)
L
Linus Torvalds 已提交
576
		error = fd;
577
	else
A
Al Viro 已提交
578
		error = read_name(inode, name);
L
Linus Torvalds 已提交
579

580
	__putname(name);
J
Jeff Dike 已提交
581
	if (error)
L
Linus Torvalds 已提交
582 583 584 585 586
		goto out_put;

	HOSTFS_I(inode)->fd = fd;
	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
	d_instantiate(dentry, inode);
J
Jeff Dike 已提交
587
	return 0;
L
Linus Torvalds 已提交
588 589 590 591

 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
592
	return error;
L
Linus Torvalds 已提交
593 594
}

J
James Hogan 已提交
595 596
static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
				    unsigned int flags)
L
Linus Torvalds 已提交
597 598 599 600 601
{
	struct inode *inode;
	char *name;
	int err;

602 603 604
	inode = hostfs_iget(ino->i_sb);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
L
Linus Torvalds 已提交
605
		goto out;
606
	}
L
Linus Torvalds 已提交
607 608

	err = -ENOMEM;
A
Al Viro 已提交
609
	name = dentry_name(dentry);
J
Jeff Dike 已提交
610
	if (name == NULL)
L
Linus Torvalds 已提交
611 612 613
		goto out_put;

	err = read_name(inode, name);
A
Al Viro 已提交
614

615
	__putname(name);
J
Jeff Dike 已提交
616
	if (err == -ENOENT) {
L
Linus Torvalds 已提交
617 618 619
		iput(inode);
		inode = NULL;
	}
J
Jeff Dike 已提交
620
	else if (err)
L
Linus Torvalds 已提交
621 622 623
		goto out_put;

	d_add(dentry, inode);
J
Jeff Dike 已提交
624
	return NULL;
L
Linus Torvalds 已提交
625 626 627 628

 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
629
	return ERR_PTR(err);
L
Linus Torvalds 已提交
630 631
}

J
James Hogan 已提交
632 633
static int hostfs_link(struct dentry *to, struct inode *ino,
		       struct dentry *from)
L
Linus Torvalds 已提交
634
{
J
Jeff Dike 已提交
635 636
	char *from_name, *to_name;
	int err;
L
Linus Torvalds 已提交
637

A
Al Viro 已提交
638
	if ((from_name = dentry_name(from)) == NULL)
J
Jeff Dike 已提交
639
		return -ENOMEM;
A
Al Viro 已提交
640
	to_name = dentry_name(to);
J
Jeff Dike 已提交
641
	if (to_name == NULL) {
642
		__putname(from_name);
J
Jeff Dike 已提交
643
		return -ENOMEM;
L
Linus Torvalds 已提交
644
	}
J
Jeff Dike 已提交
645
	err = link_file(to_name, from_name);
646 647
	__putname(from_name);
	__putname(to_name);
J
Jeff Dike 已提交
648
	return err;
L
Linus Torvalds 已提交
649 650
}

J
James Hogan 已提交
651
static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
L
Linus Torvalds 已提交
652 653 654 655
{
	char *file;
	int err;

J
Jeff Dike 已提交
656
	if (append)
J
Jeff Dike 已提交
657
		return -EPERM;
L
Linus Torvalds 已提交
658

A
Al Viro 已提交
659 660 661
	if ((file = dentry_name(dentry)) == NULL)
		return -ENOMEM;

L
Linus Torvalds 已提交
662
	err = unlink_file(file);
663
	__putname(file);
J
Jeff Dike 已提交
664
	return err;
L
Linus Torvalds 已提交
665 666
}

J
James Hogan 已提交
667 668
static int hostfs_symlink(struct inode *ino, struct dentry *dentry,
			  const char *to)
L
Linus Torvalds 已提交
669 670 671 672
{
	char *file;
	int err;

A
Al Viro 已提交
673
	if ((file = dentry_name(dentry)) == NULL)
J
Jeff Dike 已提交
674
		return -ENOMEM;
L
Linus Torvalds 已提交
675
	err = make_symlink(file, to);
676
	__putname(file);
J
Jeff Dike 已提交
677
	return err;
L
Linus Torvalds 已提交
678 679
}

J
James Hogan 已提交
680
static int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
681 682 683 684
{
	char *file;
	int err;

A
Al Viro 已提交
685
	if ((file = dentry_name(dentry)) == NULL)
J
Jeff Dike 已提交
686
		return -ENOMEM;
L
Linus Torvalds 已提交
687
	err = do_mkdir(file, mode);
688
	__putname(file);
J
Jeff Dike 已提交
689
	return err;
L
Linus Torvalds 已提交
690 691
}

J
James Hogan 已提交
692
static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
L
Linus Torvalds 已提交
693 694 695 696
{
	char *file;
	int err;

A
Al Viro 已提交
697
	if ((file = dentry_name(dentry)) == NULL)
J
Jeff Dike 已提交
698
		return -ENOMEM;
L
Linus Torvalds 已提交
699
	err = do_rmdir(file);
700
	__putname(file);
J
Jeff Dike 已提交
701
	return err;
L
Linus Torvalds 已提交
702 703
}

A
Al Viro 已提交
704
static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
L
Linus Torvalds 已提交
705 706 707
{
	struct inode *inode;
	char *name;
708
	int err;
L
Linus Torvalds 已提交
709

710 711 712
	inode = hostfs_iget(dir->i_sb);
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
L
Linus Torvalds 已提交
713
		goto out;
714
	}
L
Linus Torvalds 已提交
715 716

	err = -ENOMEM;
A
Al Viro 已提交
717
	name = dentry_name(dentry);
J
Jeff Dike 已提交
718
	if (name == NULL)
L
Linus Torvalds 已提交
719 720 721
		goto out_put;

	init_special_inode(inode, mode, dev);
J
Johannes Stezenbach 已提交
722
	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
723
	if (!err)
L
Linus Torvalds 已提交
724 725 726
		goto out_free;

	err = read_name(inode, name);
727
	__putname(name);
A
Al Viro 已提交
728 729
	if (err)
		goto out_put;
J
Jeff Dike 已提交
730
	if (err)
L
Linus Torvalds 已提交
731 732 733
		goto out_put;

	d_instantiate(dentry, inode);
J
Jeff Dike 已提交
734
	return 0;
L
Linus Torvalds 已提交
735 736

 out_free:
737
	__putname(name);
L
Linus Torvalds 已提交
738 739 740
 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
741
	return err;
L
Linus Torvalds 已提交
742 743
}

J
James Hogan 已提交
744 745
static int hostfs_rename(struct inode *from_ino, struct dentry *from,
			 struct inode *to_ino, struct dentry *to)
L
Linus Torvalds 已提交
746 747 748 749
{
	char *from_name, *to_name;
	int err;

A
Al Viro 已提交
750
	if ((from_name = dentry_name(from)) == NULL)
J
Jeff Dike 已提交
751
		return -ENOMEM;
A
Al Viro 已提交
752
	if ((to_name = dentry_name(to)) == NULL) {
753
		__putname(from_name);
J
Jeff Dike 已提交
754
		return -ENOMEM;
L
Linus Torvalds 已提交
755 756
	}
	err = rename_file(from_name, to_name);
757 758
	__putname(from_name);
	__putname(to_name);
J
Jeff Dike 已提交
759
	return err;
L
Linus Torvalds 已提交
760 761
}

J
James Hogan 已提交
762
static int hostfs_permission(struct inode *ino, int desired)
L
Linus Torvalds 已提交
763 764 765 766
{
	char *name;
	int r = 0, w = 0, x = 0, err;

767
	if (desired & MAY_NOT_BLOCK)
768 769
		return -ECHILD;

L
Linus Torvalds 已提交
770 771 772
	if (desired & MAY_READ) r = 1;
	if (desired & MAY_WRITE) w = 1;
	if (desired & MAY_EXEC) x = 1;
A
Al Viro 已提交
773
	name = inode_name(ino);
J
Jeff Dike 已提交
774 775
	if (name == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
776 777

	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
J
Jeff Dike 已提交
778
	    S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
L
Linus Torvalds 已提交
779 780 781
		err = 0;
	else
		err = access_file(name, r, w, x);
782
	__putname(name);
J
Jeff Dike 已提交
783
	if (!err)
784
		err = generic_permission(ino, desired);
L
Linus Torvalds 已提交
785 786 787
	return err;
}

J
James Hogan 已提交
788
static int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
L
Linus Torvalds 已提交
789
{
C
Christoph Hellwig 已提交
790
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
791 792 793 794
	struct hostfs_iattr attrs;
	char *name;
	int err;

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

C
Christoph Hellwig 已提交
797
	err = inode_change_ok(inode, attr);
L
Linus Torvalds 已提交
798 799 800
	if (err)
		return err;

J
Jeff Dike 已提交
801
	if (append)
L
Linus Torvalds 已提交
802 803 804
		attr->ia_valid &= ~ATTR_SIZE;

	attrs.ia_valid = 0;
J
Jeff Dike 已提交
805
	if (attr->ia_valid & ATTR_MODE) {
L
Linus Torvalds 已提交
806 807 808
		attrs.ia_valid |= HOSTFS_ATTR_MODE;
		attrs.ia_mode = attr->ia_mode;
	}
J
Jeff Dike 已提交
809
	if (attr->ia_valid & ATTR_UID) {
L
Linus Torvalds 已提交
810
		attrs.ia_valid |= HOSTFS_ATTR_UID;
811
		attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
L
Linus Torvalds 已提交
812
	}
J
Jeff Dike 已提交
813
	if (attr->ia_valid & ATTR_GID) {
L
Linus Torvalds 已提交
814
		attrs.ia_valid |= HOSTFS_ATTR_GID;
815
		attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
L
Linus Torvalds 已提交
816
	}
J
Jeff Dike 已提交
817
	if (attr->ia_valid & ATTR_SIZE) {
L
Linus Torvalds 已提交
818 819 820
		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
		attrs.ia_size = attr->ia_size;
	}
J
Jeff Dike 已提交
821
	if (attr->ia_valid & ATTR_ATIME) {
L
Linus Torvalds 已提交
822 823 824
		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
		attrs.ia_atime = attr->ia_atime;
	}
J
Jeff Dike 已提交
825
	if (attr->ia_valid & ATTR_MTIME) {
L
Linus Torvalds 已提交
826 827 828
		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
		attrs.ia_mtime = attr->ia_mtime;
	}
J
Jeff Dike 已提交
829
	if (attr->ia_valid & ATTR_CTIME) {
L
Linus Torvalds 已提交
830 831 832
		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
		attrs.ia_ctime = attr->ia_ctime;
	}
J
Jeff Dike 已提交
833
	if (attr->ia_valid & ATTR_ATIME_SET) {
L
Linus Torvalds 已提交
834 835
		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
	}
J
Jeff Dike 已提交
836
	if (attr->ia_valid & ATTR_MTIME_SET) {
L
Linus Torvalds 已提交
837 838
		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
	}
A
Al Viro 已提交
839
	name = dentry_name(dentry);
J
Jeff Dike 已提交
840
	if (name == NULL)
J
Jeff Dike 已提交
841
		return -ENOMEM;
842
	err = set_attr(name, &attrs, fd);
843
	__putname(name);
J
Jeff Dike 已提交
844
	if (err)
J
Jeff Dike 已提交
845
		return err;
L
Linus Torvalds 已提交
846

C
Christoph Hellwig 已提交
847
	if ((attr->ia_valid & ATTR_SIZE) &&
848
	    attr->ia_size != i_size_read(inode))
M
Marco Stornelli 已提交
849
		truncate_setsize(inode, attr->ia_size);
C
Christoph Hellwig 已提交
850 851 852 853

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

856
static const struct inode_operations hostfs_iops = {
L
Linus Torvalds 已提交
857 858 859 860
	.permission	= hostfs_permission,
	.setattr	= hostfs_setattr,
};

861
static const struct inode_operations hostfs_dir_iops = {
L
Linus Torvalds 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874
	.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,
};

A
Al Viro 已提交
875 876 877 878 879 880 881
static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
{
	char *link = __getname();
	if (link) {
		char *path = dentry_name(dentry);
		int err = -ENOMEM;
		if (path) {
A
Al Viro 已提交
882
			err = hostfs_do_readlink(path, link, PATH_MAX);
A
Al Viro 已提交
883 884
			if (err == PATH_MAX)
				err = -E2BIG;
885
			__putname(path);
A
Al Viro 已提交
886 887 888 889 890 891 892
		}
		if (err < 0) {
			__putname(link);
			link = ERR_PTR(err);
		}
	} else {
		link = ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
893
	}
A
Al Viro 已提交
894 895 896 897 898 899 900 901 902 903

	nd_set_link(nd, link);
	return NULL;
}

static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
{
	char *s = nd_get_link(nd);
	if (!IS_ERR(s))
		__putname(s);
L
Linus Torvalds 已提交
904 905
}

A
Al Viro 已提交
906 907 908 909
static const struct inode_operations hostfs_link_iops = {
	.readlink	= generic_readlink,
	.follow_link	= hostfs_follow_link,
	.put_link	= hostfs_put_link,
L
Linus Torvalds 已提交
910 911 912 913 914
};

static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
{
	struct inode *root_inode;
915
	char *host_root_path, *req_root = d;
L
Linus Torvalds 已提交
916 917 918 919 920 921
	int err;

	sb->s_blocksize = 1024;
	sb->s_blocksize_bits = 10;
	sb->s_magic = HOSTFS_SUPER_MAGIC;
	sb->s_op = &hostfs_sbops;
922
	sb->s_d_op = &simple_dentry_operations;
923
	sb->s_maxbytes = MAX_LFS_FILESIZE;
L
Linus Torvalds 已提交
924

925
	/* NULL is printed as <NULL> by sprintf: avoid that. */
926 927
	if (req_root == NULL)
		req_root = "";
L
Linus Torvalds 已提交
928 929

	err = -ENOMEM;
930 931
	sb->s_fs_info = host_root_path =
		kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
J
Jeff Dike 已提交
932
	if (host_root_path == NULL)
L
Linus Torvalds 已提交
933 934
		goto out;

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

A
Al Viro 已提交
937 938
	root_inode = new_inode(sb);
	if (!root_inode)
939
		goto out;
L
Linus Torvalds 已提交
940

941 942 943
	err = read_name(root_inode, host_root_path);
	if (err)
		goto out_put;
A
Al Viro 已提交
944

945
	if (S_ISLNK(root_inode->i_mode)) {
A
Al Viro 已提交
946 947 948 949 950 951
		char *name = follow_link(host_root_path);
		if (IS_ERR(name))
			err = PTR_ERR(name);
		else
			err = read_name(root_inode, name);
		kfree(name);
952 953
		if (err)
			goto out_put;
A
Al Viro 已提交
954
	}
L
Linus Torvalds 已提交
955 956

	err = -ENOMEM;
957
	sb->s_root = d_make_root(root_inode);
J
Jeff Dike 已提交
958
	if (sb->s_root == NULL)
959
		goto out;
L
Linus Torvalds 已提交
960

J
Jeff Dike 已提交
961
	return 0;
L
Linus Torvalds 已提交
962

J
Jeff Dike 已提交
963 964 965 966
out_put:
	iput(root_inode);
out:
	return err;
L
Linus Torvalds 已提交
967 968
}

A
Al Viro 已提交
969
static struct dentry *hostfs_read_sb(struct file_system_type *type,
970
			  int flags, const char *dev_name,
A
Al Viro 已提交
971
			  void *data)
L
Linus Torvalds 已提交
972
{
A
Al Viro 已提交
973
	return mount_nodev(type, flags, data, hostfs_fill_sb_common);
L
Linus Torvalds 已提交
974 975
}

976 977 978 979 980 981
static void hostfs_kill_sb(struct super_block *s)
{
	kill_anon_super(s);
	kfree(s->s_fs_info);
}

L
Linus Torvalds 已提交
982 983 984
static struct file_system_type hostfs_type = {
	.owner 		= THIS_MODULE,
	.name 		= "hostfs",
A
Al Viro 已提交
985
	.mount	 	= hostfs_read_sb,
986
	.kill_sb	= hostfs_kill_sb,
L
Linus Torvalds 已提交
987 988
	.fs_flags 	= 0,
};
989
MODULE_ALIAS_FS("hostfs");
L
Linus Torvalds 已提交
990 991 992

static int __init init_hostfs(void)
{
J
Jeff Dike 已提交
993
	return register_filesystem(&hostfs_type);
L
Linus Torvalds 已提交
994 995 996 997 998 999 1000 1001 1002 1003
}

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

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