hostfs_kern.c 20.7 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
	struct inode vfs_inode;
27
	struct mutex open_mutex;
L
Linus Torvalds 已提交
28 29 30 31
};

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

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

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

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

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

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

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

__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

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

90 91 92 93
	root = dentry->d_sb->s_fs_info;
	len = strlen(root);
	if (IS_ERR(p)) {
		__putname(name);
J
Jeff Dike 已提交
94
		return NULL;
L
Linus Torvalds 已提交
95
	}
96 97 98 99 100 101 102

	/*
	 * This function relies on the fact that dentry_path_raw() will place
	 * the path name at the end of the provided buffer.
	 */
	BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);

103
	strlcpy(name, root, PATH_MAX);
104 105 106 107
	if (len > p - name) {
		__putname(name);
		return NULL;
	}
108 109 110 111

	if (p > name + len)
		strcpy(name + len, p);

J
Jeff Dike 已提交
112
	return name;
L
Linus Torvalds 已提交
113 114
}

115 116 117 118 119 120
static char *dentry_name(struct dentry *dentry)
{
	char *name = __getname();
	if (!name)
		return NULL;

121
	return __dentry_name(dentry, name);
122 123
}

A
Al Viro 已提交
124
static char *inode_name(struct inode *ino)
L
Linus Torvalds 已提交
125 126
{
	struct dentry *dentry;
N
Nick Piggin 已提交
127
	char *name;
L
Linus Torvalds 已提交
128

N
Nick Piggin 已提交
129 130
	dentry = d_find_alias(ino);
	if (!dentry)
131
		return NULL;
N
Nick Piggin 已提交
132 133 134 135 136 137

	name = dentry_name(dentry);

	dput(dentry);

	return name;
L
Linus Torvalds 已提交
138 139 140 141 142 143 144
}

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

145 146
	name = __getname();
	if (!name) {
L
Linus Torvalds 已提交
147
		n = -ENOMEM;
148
		goto out_free;
L
Linus Torvalds 已提交
149
	}
150 151

	n = hostfs_do_readlink(link, name, PATH_MAX);
J
Jeff Dike 已提交
152
	if (n < 0)
L
Linus Torvalds 已提交
153
		goto out_free;
154 155 156 157
	else if (n == PATH_MAX) {
		n = -E2BIG;
		goto out_free;
	}
L
Linus Torvalds 已提交
158

J
Jeff Dike 已提交
159
	if (*name == '/')
J
Jeff Dike 已提交
160
		return name;
L
Linus Torvalds 已提交
161 162

	end = strrchr(link, '/');
J
Jeff Dike 已提交
163
	if (end == NULL)
J
Jeff Dike 已提交
164
		return name;
L
Linus Torvalds 已提交
165 166 167 168 169

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

	resolved = kmalloc(len, GFP_KERNEL);
J
Jeff Dike 已提交
170
	if (resolved == NULL) {
L
Linus Torvalds 已提交
171 172 173 174 175
		n = -ENOMEM;
		goto out_free;
	}

	sprintf(resolved, "%s%s", link, name);
176
	__putname(name);
L
Linus Torvalds 已提交
177
	kfree(link);
J
Jeff Dike 已提交
178
	return resolved;
L
Linus Torvalds 已提交
179 180

 out_free:
181
	__putname(name);
J
Jeff Dike 已提交
182
	return ERR_PTR(n);
L
Linus Torvalds 已提交
183 184
}

185 186
static struct inode *hostfs_iget(struct super_block *sb)
{
A
Al Viro 已提交
187
	struct inode *inode = new_inode(sb);
188 189 190 191 192
	if (!inode)
		return ERR_PTR(-ENOMEM);
	return inode;
}

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

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

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

226
	hi = kmalloc(sizeof(*hi), GFP_KERNEL_ACCOUNT);
J
Jeff Dike 已提交
227
	if (hi == NULL)
J
Jeff Dike 已提交
228
		return NULL;
229
	hi->fd = -1;
230
	hi->mode = 0;
L
Linus Torvalds 已提交
231
	inode_init_once(&hi->vfs_inode);
232
	mutex_init(&hi->open_mutex);
J
Jeff Dike 已提交
233
	return &hi->vfs_inode;
L
Linus Torvalds 已提交
234 235
}

236
static void hostfs_evict_inode(struct inode *inode)
L
Linus Torvalds 已提交
237
{
238
	truncate_inode_pages_final(&inode->i_data);
239
	clear_inode(inode);
J
Jeff Dike 已提交
240
	if (HOSTFS_I(inode)->fd != -1) {
L
Linus Torvalds 已提交
241 242 243 244 245
		close_file(&HOSTFS_I(inode)->fd);
		HOSTFS_I(inode)->fd = -1;
	}
}

N
Nick Piggin 已提交
246
static void hostfs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
247
{
N
Nick Piggin 已提交
248
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
249 250
	kfree(HOSTFS_I(inode));
}
N
Nick Piggin 已提交
251 252 253 254 255

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

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

	if (strlen(root_path) > offset)
263
		seq_show_option(seq, root_path + offset, NULL);
M
Miklos Szeredi 已提交
264

265 266 267
	if (append)
		seq_puts(seq, ",append");

M
Miklos Szeredi 已提交
268 269 270
	return 0;
}

271
static const struct super_operations hostfs_sbops = {
L
Linus Torvalds 已提交
272 273
	.alloc_inode	= hostfs_alloc_inode,
	.destroy_inode	= hostfs_destroy_inode,
274
	.evict_inode	= hostfs_evict_inode,
L
Linus Torvalds 已提交
275
	.statfs		= hostfs_statfs,
M
Miklos Szeredi 已提交
276
	.show_options	= hostfs_show_options,
L
Linus Torvalds 已提交
277 278
};

J
James Hogan 已提交
279
static int hostfs_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
280 281 282 283 284
{
	void *dir;
	char *name;
	unsigned long long next, ino;
	int error, len;
285
	unsigned int type;
L
Linus Torvalds 已提交
286

A
Al Viro 已提交
287
	name = dentry_name(file->f_path.dentry);
J
Jeff Dike 已提交
288
	if (name == NULL)
J
Jeff Dike 已提交
289
		return -ENOMEM;
L
Linus Torvalds 已提交
290
	dir = open_dir(name, &error);
291
	__putname(name);
J
Jeff Dike 已提交
292
	if (dir == NULL)
J
Jeff Dike 已提交
293
		return -error;
A
Al Viro 已提交
294
	next = ctx->pos;
295
	seek_dir(dir, next);
296
	while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
A
Al Viro 已提交
297 298 299
		if (!dir_emit(ctx, name, len, ino, type))
			break;
		ctx->pos = next;
L
Linus Torvalds 已提交
300 301
	}
	close_dir(dir);
J
Jeff Dike 已提交
302
	return 0;
L
Linus Torvalds 已提交
303 304
}

305
static int hostfs_open(struct inode *ino, struct file *file)
L
Linus Torvalds 已提交
306 307
{
	char *name;
308
	fmode_t mode;
309
	int err;
310
	int r, w, fd;
L
Linus Torvalds 已提交
311 312

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

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

318
retry:
319 320
	r = w = 0;

321
	if (mode & FMODE_READ)
L
Linus Torvalds 已提交
322
		r = 1;
323
	if (mode & FMODE_WRITE)
324
		r = w = 1;
L
Linus Torvalds 已提交
325

A
Al Viro 已提交
326
	name = dentry_name(file->f_path.dentry);
J
Jeff Dike 已提交
327
	if (name == NULL)
J
Jeff Dike 已提交
328
		return -ENOMEM;
L
Linus Torvalds 已提交
329 330

	fd = open_file(name, r, w, append);
331
	__putname(name);
J
Jeff Dike 已提交
332
	if (fd < 0)
J
Jeff Dike 已提交
333
		return fd;
334

335
	mutex_lock(&HOSTFS_I(ino)->open_mutex);
336 337
	/* somebody else had handled it first? */
	if ((mode & HOSTFS_I(ino)->mode) == mode) {
338
		mutex_unlock(&HOSTFS_I(ino)->open_mutex);
339
		close_file(&fd);
340 341 342 343
		return 0;
	}
	if ((mode | HOSTFS_I(ino)->mode) != mode) {
		mode |= HOSTFS_I(ino)->mode;
344
		mutex_unlock(&HOSTFS_I(ino)->open_mutex);
345 346 347 348 349 350 351 352 353
		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) {
354
			mutex_unlock(&HOSTFS_I(ino)->open_mutex);
355 356 357 358
			return err;
		}
	}
	HOSTFS_I(ino)->mode = mode;
359
	mutex_unlock(&HOSTFS_I(ino)->open_mutex);
L
Linus Torvalds 已提交
360

J
Jeff Dike 已提交
361
	return 0;
L
Linus Torvalds 已提交
362 363
}

R
Richard Weinberger 已提交
364 365 366 367 368 369 370
static int hostfs_file_release(struct inode *inode, struct file *file)
{
	filemap_write_and_wait(inode->i_mapping);

	return 0;
}

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

377
	ret = file_write_and_wait_range(file, start, end);
378 379 380
	if (ret)
		return ret;

A
Al Viro 已提交
381
	inode_lock(inode);
382
	ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
A
Al Viro 已提交
383
	inode_unlock(inode);
384 385

	return ret;
L
Linus Torvalds 已提交
386 387
}

388
static const struct file_operations hostfs_file_fops = {
L
Linus Torvalds 已提交
389
	.llseek		= generic_file_llseek,
390
	.splice_read	= generic_file_splice_read,
391
	.read_iter	= generic_file_read_iter,
392
	.write_iter	= generic_file_write_iter,
L
Linus Torvalds 已提交
393
	.mmap		= generic_file_mmap,
394
	.open		= hostfs_open,
R
Richard Weinberger 已提交
395
	.release	= hostfs_file_release,
L
Linus Torvalds 已提交
396 397 398
	.fsync		= hostfs_fsync,
};

399
static const struct file_operations hostfs_dir_fops = {
L
Linus Torvalds 已提交
400
	.llseek		= generic_file_llseek,
A
Al Viro 已提交
401
	.iterate_shared	= hostfs_readdir,
L
Linus Torvalds 已提交
402
	.read		= generic_read_dir,
403 404
	.open		= hostfs_open,
	.fsync		= hostfs_fsync,
L
Linus Torvalds 已提交
405 406
};

J
James Hogan 已提交
407
static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
L
Linus Torvalds 已提交
408 409 410 411
{
	struct address_space *mapping = page->mapping;
	struct inode *inode = mapping->host;
	char *buffer;
R
Richard Weinberger 已提交
412
	loff_t base = page_offset(page);
413 414
	int count = PAGE_SIZE;
	int end_index = inode->i_size >> PAGE_SHIFT;
L
Linus Torvalds 已提交
415 416 417
	int err;

	if (page->index >= end_index)
418
		count = inode->i_size & (PAGE_SIZE-1);
L
Linus Torvalds 已提交
419 420 421 422

	buffer = kmap(page);

	err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
J
Jeff Dike 已提交
423
	if (err != count) {
L
Linus Torvalds 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		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 已提交
442
static int hostfs_readpage(struct file *file, struct page *page)
L
Linus Torvalds 已提交
443 444
{
	char *buffer;
R
Richard Weinberger 已提交
445
	loff_t start = page_offset(page);
446
	int bytes_read, ret = 0;
L
Linus Torvalds 已提交
447 448

	buffer = kmap(page);
449
	bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
450
			PAGE_SIZE);
451
	if (bytes_read < 0) {
452 453
		ClearPageUptodate(page);
		SetPageError(page);
454
		ret = bytes_read;
J
Jeff Dike 已提交
455
		goto out;
456
	}
L
Linus Torvalds 已提交
457

458
	memset(buffer + bytes_read, 0, PAGE_SIZE - bytes_read);
L
Linus Torvalds 已提交
459

460
	ClearPageError(page);
L
Linus Torvalds 已提交
461
	SetPageUptodate(page);
462

L
Linus Torvalds 已提交
463
 out:
464
	flush_dcache_page(page);
L
Linus Torvalds 已提交
465 466
	kunmap(page);
	unlock_page(page);
467
	return ret;
L
Linus Torvalds 已提交
468 469
}

J
James Hogan 已提交
470 471 472
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 已提交
473
{
474
	pgoff_t index = pos >> PAGE_SHIFT;
L
Linus Torvalds 已提交
475

476
	*pagep = grab_cache_page_write_begin(mapping, index, flags);
N
Nick Piggin 已提交
477 478 479
	if (!*pagep)
		return -ENOMEM;
	return 0;
L
Linus Torvalds 已提交
480 481
}

J
James Hogan 已提交
482 483 484
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 已提交
485 486
{
	struct inode *inode = mapping->host;
N
Nick Piggin 已提交
487
	void *buffer;
488
	unsigned from = pos & (PAGE_SIZE - 1);
N
Nick Piggin 已提交
489
	int err;
L
Linus Torvalds 已提交
490 491

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

495
	if (!PageUptodate(page) && err == PAGE_SIZE)
N
Nick Piggin 已提交
496
		SetPageUptodate(page);
497

J
Jeff Dike 已提交
498 499
	/*
	 * If err > 0, write_file has added err to pos, so we are comparing
N
Nick Piggin 已提交
500 501 502 503 504
	 * i_size against the last byte written.
	 */
	if (err > 0 && (pos > inode->i_size))
		inode->i_size = pos;
	unlock_page(page);
505
	put_page(page);
L
Linus Torvalds 已提交
506

J
Jeff Dike 已提交
507
	return err;
L
Linus Torvalds 已提交
508 509
}

510
static const struct address_space_operations hostfs_aops = {
L
Linus Torvalds 已提交
511 512
	.writepage 	= hostfs_writepage,
	.readpage	= hostfs_readpage,
513
	.set_page_dirty = __set_page_dirty_nobuffers,
N
Nick Piggin 已提交
514 515
	.write_begin	= hostfs_write_begin,
	.write_end	= hostfs_write_end,
L
Linus Torvalds 已提交
516 517
};

518
static int read_name(struct inode *ino, char *name)
L
Linus Torvalds 已提交
519
{
520 521 522 523 524
	dev_t rdev;
	struct hostfs_stat st;
	int err = stat_file(name, &st, -1);
	if (err)
		return err;
L
Linus Torvalds 已提交
525

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

529 530
	switch (st.mode & S_IFMT) {
	case S_IFLNK:
A
Al Viro 已提交
531
		ino->i_op = &hostfs_link_iops;
L
Linus Torvalds 已提交
532
		break;
533 534 535
	case S_IFDIR:
		ino->i_op = &hostfs_dir_iops;
		ino->i_fop = &hostfs_dir_fops;
L
Linus Torvalds 已提交
536
		break;
537 538 539 540 541 542
	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 已提交
543
		break;
544
	case S_IFREG:
545 546 547
		ino->i_op = &hostfs_iops;
		ino->i_fop = &hostfs_file_fops;
		ino->i_mapping->a_ops = &hostfs_aops;
548 549 550
		break;
	default:
		return -EIO;
L
Linus Torvalds 已提交
551
	}
552 553 554

	ino->i_ino = st.ino;
	ino->i_mode = st.mode;
M
Miklos Szeredi 已提交
555
	set_nlink(ino, st.nlink);
556 557
	i_uid_write(ino, st.uid);
	i_gid_write(ino, st.gid);
558 559 560
	ino->i_atime = timespec_to_timespec64(st.atime);
	ino->i_mtime = timespec_to_timespec64(st.mtime);
	ino->i_ctime = timespec_to_timespec64(st.ctime);
561 562 563
	ino->i_size = st.size;
	ino->i_blocks = st.blocks;
	return 0;
L
Linus Torvalds 已提交
564 565
}

J
James Hogan 已提交
566 567
static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
			 bool excl)
L
Linus Torvalds 已提交
568 569 570 571 572
{
	struct inode *inode;
	char *name;
	int error, fd;

573 574 575
	inode = hostfs_iget(dir->i_sb);
	if (IS_ERR(inode)) {
		error = PTR_ERR(inode);
J
Jeff Dike 已提交
576
		goto out;
577
	}
L
Linus Torvalds 已提交
578 579

	error = -ENOMEM;
A
Al Viro 已提交
580
	name = dentry_name(dentry);
J
Jeff Dike 已提交
581
	if (name == NULL)
L
Linus Torvalds 已提交
582 583
		goto out_put;

584
	fd = file_create(name, mode & 0777);
585
	if (fd < 0)
L
Linus Torvalds 已提交
586
		error = fd;
587
	else
A
Al Viro 已提交
588
		error = read_name(inode, name);
L
Linus Torvalds 已提交
589

590
	__putname(name);
J
Jeff Dike 已提交
591
	if (error)
L
Linus Torvalds 已提交
592 593 594 595 596
		goto out_put;

	HOSTFS_I(inode)->fd = fd;
	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
	d_instantiate(dentry, inode);
J
Jeff Dike 已提交
597
	return 0;
L
Linus Torvalds 已提交
598 599 600 601

 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
602
	return error;
L
Linus Torvalds 已提交
603 604
}

J
James Hogan 已提交
605 606
static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
				    unsigned int flags)
L
Linus Torvalds 已提交
607 608 609 610 611
{
	struct inode *inode;
	char *name;
	int err;

612
	inode = hostfs_iget(ino->i_sb);
613
	if (IS_ERR(inode))
L
Linus Torvalds 已提交
614 615 616
		goto out;

	err = -ENOMEM;
A
Al Viro 已提交
617
	name = dentry_name(dentry);
618 619 620 621 622
	if (name) {
		err = read_name(inode, name);
		__putname(name);
	}
	if (err) {
L
Linus Torvalds 已提交
623
		iput(inode);
624
		inode = (err == -ENOENT) ? NULL : ERR_PTR(err);
L
Linus Torvalds 已提交
625 626
	}
 out:
627
	return d_splice_alias(inode, dentry);
L
Linus Torvalds 已提交
628 629
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	init_special_inode(inode, mode, dev);
J
Johannes Stezenbach 已提交
720
	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
V
Vegard Nossum 已提交
721
	if (err)
L
Linus Torvalds 已提交
722 723 724
		goto out_free;

	err = read_name(inode, name);
725
	__putname(name);
A
Al Viro 已提交
726 727
	if (err)
		goto out_put;
L
Linus Torvalds 已提交
728 729

	d_instantiate(dentry, inode);
J
Jeff Dike 已提交
730
	return 0;
L
Linus Torvalds 已提交
731 732

 out_free:
733
	__putname(name);
L
Linus Torvalds 已提交
734 735 736
 out_put:
	iput(inode);
 out:
J
Jeff Dike 已提交
737
	return err;
L
Linus Torvalds 已提交
738 739
}

M
Miklos Szeredi 已提交
740 741 742
static int hostfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
			  struct inode *new_dir, struct dentry *new_dentry,
			  unsigned int flags)
L
Linus Torvalds 已提交
743
{
M
Miklos Szeredi 已提交
744
	char *old_name, *new_name;
L
Linus Torvalds 已提交
745 746
	int err;

M
Miklos Szeredi 已提交
747 748 749 750 751
	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
		return -EINVAL;

	old_name = dentry_name(old_dentry);
	if (old_name == NULL)
J
Jeff Dike 已提交
752
		return -ENOMEM;
M
Miklos Szeredi 已提交
753 754 755
	new_name = dentry_name(new_dentry);
	if (new_name == NULL) {
		__putname(old_name);
J
Jeff Dike 已提交
756
		return -ENOMEM;
L
Linus Torvalds 已提交
757
	}
M
Miklos Szeredi 已提交
758 759 760 761 762 763 764
	if (!flags)
		err = rename_file(old_name, new_name);
	else
		err = rename2_file(old_name, new_name, flags);

	__putname(old_name);
	__putname(new_name);
J
Jeff Dike 已提交
765
	return err;
L
Linus Torvalds 已提交
766 767
}

J
James Hogan 已提交
768
static int hostfs_permission(struct inode *ino, int desired)
L
Linus Torvalds 已提交
769 770 771 772
{
	char *name;
	int r = 0, w = 0, x = 0, err;

773
	if (desired & MAY_NOT_BLOCK)
774 775
		return -ECHILD;

L
Linus Torvalds 已提交
776 777 778
	if (desired & MAY_READ) r = 1;
	if (desired & MAY_WRITE) w = 1;
	if (desired & MAY_EXEC) x = 1;
A
Al Viro 已提交
779
	name = inode_name(ino);
J
Jeff Dike 已提交
780 781
	if (name == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
782 783

	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
J
Jeff Dike 已提交
784
	    S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
L
Linus Torvalds 已提交
785 786 787
		err = 0;
	else
		err = access_file(name, r, w, x);
788
	__putname(name);
J
Jeff Dike 已提交
789
	if (!err)
790
		err = generic_permission(ino, desired);
L
Linus Torvalds 已提交
791 792 793
	return err;
}

J
James Hogan 已提交
794
static int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
L
Linus Torvalds 已提交
795
{
796
	struct inode *inode = d_inode(dentry);
L
Linus Torvalds 已提交
797 798 799 800
	struct hostfs_iattr attrs;
	char *name;
	int err;

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

803
	err = setattr_prepare(dentry, attr);
L
Linus Torvalds 已提交
804 805 806
	if (err)
		return err;

J
Jeff Dike 已提交
807
	if (append)
L
Linus Torvalds 已提交
808 809 810
		attr->ia_valid &= ~ATTR_SIZE;

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

C
Christoph Hellwig 已提交
853
	if ((attr->ia_valid & ATTR_SIZE) &&
854
	    attr->ia_size != i_size_read(inode))
M
Marco Stornelli 已提交
855
		truncate_setsize(inode, attr->ia_size);
C
Christoph Hellwig 已提交
856 857 858 859

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

862
static const struct inode_operations hostfs_iops = {
L
Linus Torvalds 已提交
863 864 865 866
	.permission	= hostfs_permission,
	.setattr	= hostfs_setattr,
};

867
static const struct inode_operations hostfs_dir_iops = {
L
Linus Torvalds 已提交
868 869 870 871 872 873 874 875
	.create		= hostfs_create,
	.lookup		= hostfs_lookup,
	.link		= hostfs_link,
	.unlink		= hostfs_unlink,
	.symlink	= hostfs_symlink,
	.mkdir		= hostfs_mkdir,
	.rmdir		= hostfs_rmdir,
	.mknod		= hostfs_mknod,
876
	.rename		= hostfs_rename2,
L
Linus Torvalds 已提交
877 878 879 880
	.permission	= hostfs_permission,
	.setattr	= hostfs_setattr,
};

881
static const char *hostfs_get_link(struct dentry *dentry,
882 883
				   struct inode *inode,
				   struct delayed_call *done)
A
Al Viro 已提交
884
{
885 886 887
	char *link;
	if (!dentry)
		return ERR_PTR(-ECHILD);
888
	link = kmalloc(PATH_MAX, GFP_KERNEL);
A
Al Viro 已提交
889 890 891 892
	if (link) {
		char *path = dentry_name(dentry);
		int err = -ENOMEM;
		if (path) {
A
Al Viro 已提交
893
			err = hostfs_do_readlink(path, link, PATH_MAX);
A
Al Viro 已提交
894 895
			if (err == PATH_MAX)
				err = -E2BIG;
896
			__putname(path);
A
Al Viro 已提交
897 898
		}
		if (err < 0) {
899
			kfree(link);
900
			return ERR_PTR(err);
A
Al Viro 已提交
901 902
		}
	} else {
903
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
904
	}
A
Al Viro 已提交
905

906 907
	set_delayed_call(done, kfree_link, link);
	return link;
L
Linus Torvalds 已提交
908 909
}

A
Al Viro 已提交
910
static const struct inode_operations hostfs_link_iops = {
911
	.get_link	= hostfs_get_link,
L
Linus Torvalds 已提交
912 913 914 915 916
};

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

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

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

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

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

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

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

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

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

J
Jeff Dike 已提交
964
	return 0;
L
Linus Torvalds 已提交
965

J
Jeff Dike 已提交
966 967 968 969
out_put:
	iput(root_inode);
out:
	return err;
L
Linus Torvalds 已提交
970 971
}

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

979 980 981 982 983 984
static void hostfs_kill_sb(struct super_block *s)
{
	kill_anon_super(s);
	kfree(s->s_fs_info);
}

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

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

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

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