xref: /linux/fs/hostfs/hostfs_kern.c (revision 5d4a2e29fba5b2bef95b96a46b338ec4d76fa4fd)
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  *
5  * Ported the filesystem routines to 2.5.
6  * 2003-02-10 Petr Baudis <pasky@ucw.cz>
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/module.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/statfs.h>
14 #include <linux/slab.h>
15 #include <linux/seq_file.h>
16 #include <linux/mount.h>
17 #include "hostfs.h"
18 #include "init.h"
19 #include "kern.h"
20 
21 struct hostfs_inode_info {
22 	char *host_filename;
23 	int fd;
24 	fmode_t mode;
25 	struct inode vfs_inode;
26 };
27 
28 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
29 {
30 	return list_entry(inode, struct hostfs_inode_info, vfs_inode);
31 }
32 
33 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
34 
35 static int hostfs_d_delete(struct dentry *dentry)
36 {
37 	return 1;
38 }
39 
40 static const struct dentry_operations hostfs_dentry_ops = {
41 	.d_delete		= hostfs_d_delete,
42 };
43 
44 /* Changed in hostfs_args before the kernel starts running */
45 static char *root_ino = "";
46 static int append = 0;
47 
48 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
49 
50 static const struct inode_operations hostfs_iops;
51 static const struct inode_operations hostfs_dir_iops;
52 static const struct address_space_operations hostfs_link_aops;
53 
54 #ifndef MODULE
55 static int __init hostfs_args(char *options, int *add)
56 {
57 	char *ptr;
58 
59 	ptr = strchr(options, ',');
60 	if (ptr != NULL)
61 		*ptr++ = '\0';
62 	if (*options != '\0')
63 		root_ino = options;
64 
65 	options = ptr;
66 	while (options) {
67 		ptr = strchr(options, ',');
68 		if (ptr != NULL)
69 			*ptr++ = '\0';
70 		if (*options != '\0') {
71 			if (!strcmp(options, "append"))
72 				append = 1;
73 			else printf("hostfs_args - unsupported option - %s\n",
74 				    options);
75 		}
76 		options = ptr;
77 	}
78 	return 0;
79 }
80 
81 __uml_setup("hostfs=", hostfs_args,
82 "hostfs=<root dir>,<flags>,...\n"
83 "    This is used to set hostfs parameters.  The root directory argument\n"
84 "    is used to confine all hostfs mounts to within the specified directory\n"
85 "    tree on the host.  If this isn't specified, then a user inside UML can\n"
86 "    mount anything on the host that's accessible to the user that's running\n"
87 "    it.\n"
88 "    The only flag currently supported is 'append', which specifies that all\n"
89 "    files opened by hostfs will be opened in append mode.\n\n"
90 );
91 #endif
92 
93 static char *dentry_name(struct dentry *dentry, int extra)
94 {
95 	struct dentry *parent;
96 	char *root, *name;
97 	int len;
98 
99 	len = 0;
100 	parent = dentry;
101 	while (parent->d_parent != parent) {
102 		len += parent->d_name.len + 1;
103 		parent = parent->d_parent;
104 	}
105 
106 	root = HOSTFS_I(parent->d_inode)->host_filename;
107 	len += strlen(root);
108 	name = kmalloc(len + extra + 1, GFP_KERNEL);
109 	if (name == NULL)
110 		return NULL;
111 
112 	name[len] = '\0';
113 	parent = dentry;
114 	while (parent->d_parent != parent) {
115 		len -= parent->d_name.len + 1;
116 		name[len] = '/';
117 		strncpy(&name[len + 1], parent->d_name.name,
118 			parent->d_name.len);
119 		parent = parent->d_parent;
120 	}
121 	strncpy(name, root, strlen(root));
122 	return name;
123 }
124 
125 static char *inode_name(struct inode *ino, int extra)
126 {
127 	struct dentry *dentry;
128 
129 	dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
130 	return dentry_name(dentry, extra);
131 }
132 
133 static int read_name(struct inode *ino, char *name)
134 {
135 	/*
136 	 * The non-int inode fields are copied into ints by stat_file and
137 	 * then copied into the inode because passing the actual pointers
138 	 * in and having them treated as int * breaks on big-endian machines
139 	 */
140 	int err;
141 	int i_mode, i_nlink, i_blksize;
142 	unsigned long long i_size;
143 	unsigned long long i_ino;
144 	unsigned long long i_blocks;
145 
146 	err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
147 			&ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
148 			&ino->i_ctime, &i_blksize, &i_blocks, -1);
149 	if (err)
150 		return err;
151 
152 	ino->i_ino = i_ino;
153 	ino->i_mode = i_mode;
154 	ino->i_nlink = i_nlink;
155 	ino->i_size = i_size;
156 	ino->i_blocks = i_blocks;
157 	return 0;
158 }
159 
160 static char *follow_link(char *link)
161 {
162 	int len, n;
163 	char *name, *resolved, *end;
164 
165 	len = 64;
166 	while (1) {
167 		n = -ENOMEM;
168 		name = kmalloc(len, GFP_KERNEL);
169 		if (name == NULL)
170 			goto out;
171 
172 		n = hostfs_do_readlink(link, name, len);
173 		if (n < len)
174 			break;
175 		len *= 2;
176 		kfree(name);
177 	}
178 	if (n < 0)
179 		goto out_free;
180 
181 	if (*name == '/')
182 		return name;
183 
184 	end = strrchr(link, '/');
185 	if (end == NULL)
186 		return name;
187 
188 	*(end + 1) = '\0';
189 	len = strlen(link) + strlen(name) + 1;
190 
191 	resolved = kmalloc(len, GFP_KERNEL);
192 	if (resolved == NULL) {
193 		n = -ENOMEM;
194 		goto out_free;
195 	}
196 
197 	sprintf(resolved, "%s%s", link, name);
198 	kfree(name);
199 	kfree(link);
200 	return resolved;
201 
202  out_free:
203 	kfree(name);
204  out:
205 	return ERR_PTR(n);
206 }
207 
208 static int hostfs_read_inode(struct inode *ino)
209 {
210 	char *name;
211 	int err = 0;
212 
213 	/*
214 	 * Unfortunately, we are called from iget() when we don't have a dentry
215 	 * allocated yet.
216 	 */
217 	if (list_empty(&ino->i_dentry))
218 		goto out;
219 
220 	err = -ENOMEM;
221 	name = inode_name(ino, 0);
222 	if (name == NULL)
223 		goto out;
224 
225 	if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
226 		name = follow_link(name);
227 		if (IS_ERR(name)) {
228 			err = PTR_ERR(name);
229 			goto out;
230 		}
231 	}
232 
233 	err = read_name(ino, name);
234 	kfree(name);
235  out:
236 	return err;
237 }
238 
239 static struct inode *hostfs_iget(struct super_block *sb)
240 {
241 	struct inode *inode;
242 	long ret;
243 
244 	inode = iget_locked(sb, 0);
245 	if (!inode)
246 		return ERR_PTR(-ENOMEM);
247 	if (inode->i_state & I_NEW) {
248 		ret = hostfs_read_inode(inode);
249 		if (ret < 0) {
250 			iget_failed(inode);
251 			return ERR_PTR(ret);
252 		}
253 		unlock_new_inode(inode);
254 	}
255 	return inode;
256 }
257 
258 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
259 {
260 	/*
261 	 * do_statfs uses struct statfs64 internally, but the linux kernel
262 	 * struct statfs still has 32-bit versions for most of these fields,
263 	 * so we convert them here
264 	 */
265 	int err;
266 	long long f_blocks;
267 	long long f_bfree;
268 	long long f_bavail;
269 	long long f_files;
270 	long long f_ffree;
271 
272 	err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
273 			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
274 			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
275 			&sf->f_namelen, sf->f_spare);
276 	if (err)
277 		return err;
278 	sf->f_blocks = f_blocks;
279 	sf->f_bfree = f_bfree;
280 	sf->f_bavail = f_bavail;
281 	sf->f_files = f_files;
282 	sf->f_ffree = f_ffree;
283 	sf->f_type = HOSTFS_SUPER_MAGIC;
284 	return 0;
285 }
286 
287 static struct inode *hostfs_alloc_inode(struct super_block *sb)
288 {
289 	struct hostfs_inode_info *hi;
290 
291 	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
292 	if (hi == NULL)
293 		return NULL;
294 
295 	*hi = ((struct hostfs_inode_info) { .host_filename	= NULL,
296 					    .fd			= -1,
297 					    .mode		= 0 });
298 	inode_init_once(&hi->vfs_inode);
299 	return &hi->vfs_inode;
300 }
301 
302 static void hostfs_delete_inode(struct inode *inode)
303 {
304 	truncate_inode_pages(&inode->i_data, 0);
305 	if (HOSTFS_I(inode)->fd != -1) {
306 		close_file(&HOSTFS_I(inode)->fd);
307 		HOSTFS_I(inode)->fd = -1;
308 	}
309 	clear_inode(inode);
310 }
311 
312 static void hostfs_destroy_inode(struct inode *inode)
313 {
314 	kfree(HOSTFS_I(inode)->host_filename);
315 
316 	/*
317 	 * XXX: This should not happen, probably. The check is here for
318 	 * additional safety.
319 	 */
320 	if (HOSTFS_I(inode)->fd != -1) {
321 		close_file(&HOSTFS_I(inode)->fd);
322 		printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
323 	}
324 
325 	kfree(HOSTFS_I(inode));
326 }
327 
328 static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
329 {
330 	struct inode *root = vfs->mnt_sb->s_root->d_inode;
331 	const char *root_path = HOSTFS_I(root)->host_filename;
332 	size_t offset = strlen(root_ino) + 1;
333 
334 	if (strlen(root_path) > offset)
335 		seq_printf(seq, ",%s", root_path + offset);
336 
337 	return 0;
338 }
339 
340 static const struct super_operations hostfs_sbops = {
341 	.alloc_inode	= hostfs_alloc_inode,
342 	.drop_inode	= generic_delete_inode,
343 	.delete_inode   = hostfs_delete_inode,
344 	.destroy_inode	= hostfs_destroy_inode,
345 	.statfs		= hostfs_statfs,
346 	.show_options	= hostfs_show_options,
347 };
348 
349 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
350 {
351 	void *dir;
352 	char *name;
353 	unsigned long long next, ino;
354 	int error, len;
355 
356 	name = dentry_name(file->f_path.dentry, 0);
357 	if (name == NULL)
358 		return -ENOMEM;
359 	dir = open_dir(name, &error);
360 	kfree(name);
361 	if (dir == NULL)
362 		return -error;
363 	next = file->f_pos;
364 	while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
365 		error = (*filldir)(ent, name, len, file->f_pos,
366 				   ino, DT_UNKNOWN);
367 		if (error) break;
368 		file->f_pos = next;
369 	}
370 	close_dir(dir);
371 	return 0;
372 }
373 
374 int hostfs_file_open(struct inode *ino, struct file *file)
375 {
376 	char *name;
377 	fmode_t mode = 0;
378 	int r = 0, w = 0, fd;
379 
380 	mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
381 	if ((mode & HOSTFS_I(ino)->mode) == mode)
382 		return 0;
383 
384 	/*
385 	 * The file may already have been opened, but with the wrong access,
386 	 * so this resets things and reopens the file with the new access.
387 	 */
388 	if (HOSTFS_I(ino)->fd != -1) {
389 		close_file(&HOSTFS_I(ino)->fd);
390 		HOSTFS_I(ino)->fd = -1;
391 	}
392 
393 	HOSTFS_I(ino)->mode |= mode;
394 	if (HOSTFS_I(ino)->mode & FMODE_READ)
395 		r = 1;
396 	if (HOSTFS_I(ino)->mode & FMODE_WRITE)
397 		w = 1;
398 	if (w)
399 		r = 1;
400 
401 	name = dentry_name(file->f_path.dentry, 0);
402 	if (name == NULL)
403 		return -ENOMEM;
404 
405 	fd = open_file(name, r, w, append);
406 	kfree(name);
407 	if (fd < 0)
408 		return fd;
409 	FILE_HOSTFS_I(file)->fd = fd;
410 
411 	return 0;
412 }
413 
414 int hostfs_fsync(struct file *file, int datasync)
415 {
416 	return fsync_file(HOSTFS_I(file->f_mapping->host)->fd, datasync);
417 }
418 
419 static const struct file_operations hostfs_file_fops = {
420 	.llseek		= generic_file_llseek,
421 	.read		= do_sync_read,
422 	.splice_read	= generic_file_splice_read,
423 	.aio_read	= generic_file_aio_read,
424 	.aio_write	= generic_file_aio_write,
425 	.write		= do_sync_write,
426 	.mmap		= generic_file_mmap,
427 	.open		= hostfs_file_open,
428 	.release	= NULL,
429 	.fsync		= hostfs_fsync,
430 };
431 
432 static const struct file_operations hostfs_dir_fops = {
433 	.llseek		= generic_file_llseek,
434 	.readdir	= hostfs_readdir,
435 	.read		= generic_read_dir,
436 };
437 
438 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
439 {
440 	struct address_space *mapping = page->mapping;
441 	struct inode *inode = mapping->host;
442 	char *buffer;
443 	unsigned long long base;
444 	int count = PAGE_CACHE_SIZE;
445 	int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
446 	int err;
447 
448 	if (page->index >= end_index)
449 		count = inode->i_size & (PAGE_CACHE_SIZE-1);
450 
451 	buffer = kmap(page);
452 	base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
453 
454 	err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
455 	if (err != count) {
456 		ClearPageUptodate(page);
457 		goto out;
458 	}
459 
460 	if (base > inode->i_size)
461 		inode->i_size = base;
462 
463 	if (PageError(page))
464 		ClearPageError(page);
465 	err = 0;
466 
467  out:
468 	kunmap(page);
469 
470 	unlock_page(page);
471 	return err;
472 }
473 
474 int hostfs_readpage(struct file *file, struct page *page)
475 {
476 	char *buffer;
477 	long long start;
478 	int err = 0;
479 
480 	start = (long long) page->index << PAGE_CACHE_SHIFT;
481 	buffer = kmap(page);
482 	err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
483 			PAGE_CACHE_SIZE);
484 	if (err < 0)
485 		goto out;
486 
487 	memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
488 
489 	flush_dcache_page(page);
490 	SetPageUptodate(page);
491 	if (PageError(page)) ClearPageError(page);
492 	err = 0;
493  out:
494 	kunmap(page);
495 	unlock_page(page);
496 	return err;
497 }
498 
499 int hostfs_write_begin(struct file *file, struct address_space *mapping,
500 			loff_t pos, unsigned len, unsigned flags,
501 			struct page **pagep, void **fsdata)
502 {
503 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
504 
505 	*pagep = grab_cache_page_write_begin(mapping, index, flags);
506 	if (!*pagep)
507 		return -ENOMEM;
508 	return 0;
509 }
510 
511 int hostfs_write_end(struct file *file, struct address_space *mapping,
512 			loff_t pos, unsigned len, unsigned copied,
513 			struct page *page, void *fsdata)
514 {
515 	struct inode *inode = mapping->host;
516 	void *buffer;
517 	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
518 	int err;
519 
520 	buffer = kmap(page);
521 	err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
522 	kunmap(page);
523 
524 	if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
525 		SetPageUptodate(page);
526 
527 	/*
528 	 * If err > 0, write_file has added err to pos, so we are comparing
529 	 * i_size against the last byte written.
530 	 */
531 	if (err > 0 && (pos > inode->i_size))
532 		inode->i_size = pos;
533 	unlock_page(page);
534 	page_cache_release(page);
535 
536 	return err;
537 }
538 
539 static const struct address_space_operations hostfs_aops = {
540 	.writepage 	= hostfs_writepage,
541 	.readpage	= hostfs_readpage,
542 	.set_page_dirty = __set_page_dirty_nobuffers,
543 	.write_begin	= hostfs_write_begin,
544 	.write_end	= hostfs_write_end,
545 };
546 
547 static int init_inode(struct inode *inode, struct dentry *dentry)
548 {
549 	char *name;
550 	int type, err = -ENOMEM;
551 	int maj, min;
552 	dev_t rdev = 0;
553 
554 	if (dentry) {
555 		name = dentry_name(dentry, 0);
556 		if (name == NULL)
557 			goto out;
558 		type = file_type(name, &maj, &min);
559 		/* Reencode maj and min with the kernel encoding.*/
560 		rdev = MKDEV(maj, min);
561 		kfree(name);
562 	}
563 	else type = OS_TYPE_DIR;
564 
565 	err = 0;
566 	if (type == OS_TYPE_SYMLINK)
567 		inode->i_op = &page_symlink_inode_operations;
568 	else if (type == OS_TYPE_DIR)
569 		inode->i_op = &hostfs_dir_iops;
570 	else inode->i_op = &hostfs_iops;
571 
572 	if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
573 	else inode->i_fop = &hostfs_file_fops;
574 
575 	if (type == OS_TYPE_SYMLINK)
576 		inode->i_mapping->a_ops = &hostfs_link_aops;
577 	else inode->i_mapping->a_ops = &hostfs_aops;
578 
579 	switch (type) {
580 	case OS_TYPE_CHARDEV:
581 		init_special_inode(inode, S_IFCHR, rdev);
582 		break;
583 	case OS_TYPE_BLOCKDEV:
584 		init_special_inode(inode, S_IFBLK, rdev);
585 		break;
586 	case OS_TYPE_FIFO:
587 		init_special_inode(inode, S_IFIFO, 0);
588 		break;
589 	case OS_TYPE_SOCK:
590 		init_special_inode(inode, S_IFSOCK, 0);
591 		break;
592 	}
593  out:
594 	return err;
595 }
596 
597 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
598 		  struct nameidata *nd)
599 {
600 	struct inode *inode;
601 	char *name;
602 	int error, fd;
603 
604 	inode = hostfs_iget(dir->i_sb);
605 	if (IS_ERR(inode)) {
606 		error = PTR_ERR(inode);
607 		goto out;
608 	}
609 
610 	error = init_inode(inode, dentry);
611 	if (error)
612 		goto out_put;
613 
614 	error = -ENOMEM;
615 	name = dentry_name(dentry, 0);
616 	if (name == NULL)
617 		goto out_put;
618 
619 	fd = file_create(name,
620 			 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
621 			 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
622 			 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
623 	if (fd < 0)
624 		error = fd;
625 	else error = read_name(inode, name);
626 
627 	kfree(name);
628 	if (error)
629 		goto out_put;
630 
631 	HOSTFS_I(inode)->fd = fd;
632 	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
633 	d_instantiate(dentry, inode);
634 	return 0;
635 
636  out_put:
637 	iput(inode);
638  out:
639 	return error;
640 }
641 
642 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
643 			     struct nameidata *nd)
644 {
645 	struct inode *inode;
646 	char *name;
647 	int err;
648 
649 	inode = hostfs_iget(ino->i_sb);
650 	if (IS_ERR(inode)) {
651 		err = PTR_ERR(inode);
652 		goto out;
653 	}
654 
655 	err = init_inode(inode, dentry);
656 	if (err)
657 		goto out_put;
658 
659 	err = -ENOMEM;
660 	name = dentry_name(dentry, 0);
661 	if (name == NULL)
662 		goto out_put;
663 
664 	err = read_name(inode, name);
665 	kfree(name);
666 	if (err == -ENOENT) {
667 		iput(inode);
668 		inode = NULL;
669 	}
670 	else if (err)
671 		goto out_put;
672 
673 	d_add(dentry, inode);
674 	dentry->d_op = &hostfs_dentry_ops;
675 	return NULL;
676 
677  out_put:
678 	iput(inode);
679  out:
680 	return ERR_PTR(err);
681 }
682 
683 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
684 {
685 	char *file;
686 	int len;
687 
688 	file = inode_name(ino, dentry->d_name.len + 1);
689 	if (file == NULL)
690 		return NULL;
691 	strcat(file, "/");
692 	len = strlen(file);
693 	strncat(file, dentry->d_name.name, dentry->d_name.len);
694 	file[len + dentry->d_name.len] = '\0';
695 	return file;
696 }
697 
698 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
699 {
700 	char *from_name, *to_name;
701 	int err;
702 
703 	if ((from_name = inode_dentry_name(ino, from)) == NULL)
704 		return -ENOMEM;
705 	to_name = dentry_name(to, 0);
706 	if (to_name == NULL) {
707 		kfree(from_name);
708 		return -ENOMEM;
709 	}
710 	err = link_file(to_name, from_name);
711 	kfree(from_name);
712 	kfree(to_name);
713 	return err;
714 }
715 
716 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
717 {
718 	char *file;
719 	int err;
720 
721 	if ((file = inode_dentry_name(ino, dentry)) == NULL)
722 		return -ENOMEM;
723 	if (append)
724 		return -EPERM;
725 
726 	err = unlink_file(file);
727 	kfree(file);
728 	return err;
729 }
730 
731 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
732 {
733 	char *file;
734 	int err;
735 
736 	if ((file = inode_dentry_name(ino, dentry)) == NULL)
737 		return -ENOMEM;
738 	err = make_symlink(file, to);
739 	kfree(file);
740 	return err;
741 }
742 
743 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
744 {
745 	char *file;
746 	int err;
747 
748 	if ((file = inode_dentry_name(ino, dentry)) == NULL)
749 		return -ENOMEM;
750 	err = do_mkdir(file, mode);
751 	kfree(file);
752 	return err;
753 }
754 
755 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
756 {
757 	char *file;
758 	int err;
759 
760 	if ((file = inode_dentry_name(ino, dentry)) == NULL)
761 		return -ENOMEM;
762 	err = do_rmdir(file);
763 	kfree(file);
764 	return err;
765 }
766 
767 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
768 {
769 	struct inode *inode;
770 	char *name;
771 	int err;
772 
773 	inode = hostfs_iget(dir->i_sb);
774 	if (IS_ERR(inode)) {
775 		err = PTR_ERR(inode);
776 		goto out;
777 	}
778 
779 	err = init_inode(inode, dentry);
780 	if (err)
781 		goto out_put;
782 
783 	err = -ENOMEM;
784 	name = dentry_name(dentry, 0);
785 	if (name == NULL)
786 		goto out_put;
787 
788 	init_special_inode(inode, mode, dev);
789 	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
790 	if (err)
791 		goto out_free;
792 
793 	err = read_name(inode, name);
794 	kfree(name);
795 	if (err)
796 		goto out_put;
797 
798 	d_instantiate(dentry, inode);
799 	return 0;
800 
801  out_free:
802 	kfree(name);
803  out_put:
804 	iput(inode);
805  out:
806 	return err;
807 }
808 
809 int hostfs_rename(struct inode *from_ino, struct dentry *from,
810 		  struct inode *to_ino, struct dentry *to)
811 {
812 	char *from_name, *to_name;
813 	int err;
814 
815 	if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
816 		return -ENOMEM;
817 	if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
818 		kfree(from_name);
819 		return -ENOMEM;
820 	}
821 	err = rename_file(from_name, to_name);
822 	kfree(from_name);
823 	kfree(to_name);
824 	return err;
825 }
826 
827 int hostfs_permission(struct inode *ino, int desired)
828 {
829 	char *name;
830 	int r = 0, w = 0, x = 0, err;
831 
832 	if (desired & MAY_READ) r = 1;
833 	if (desired & MAY_WRITE) w = 1;
834 	if (desired & MAY_EXEC) x = 1;
835 	name = inode_name(ino, 0);
836 	if (name == NULL)
837 		return -ENOMEM;
838 
839 	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
840 	    S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
841 		err = 0;
842 	else
843 		err = access_file(name, r, w, x);
844 	kfree(name);
845 	if (!err)
846 		err = generic_permission(ino, desired, NULL);
847 	return err;
848 }
849 
850 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
851 {
852 	struct hostfs_iattr attrs;
853 	char *name;
854 	int err;
855 
856 	int fd = HOSTFS_I(dentry->d_inode)->fd;
857 
858 	err = inode_change_ok(dentry->d_inode, attr);
859 	if (err)
860 		return err;
861 
862 	if (append)
863 		attr->ia_valid &= ~ATTR_SIZE;
864 
865 	attrs.ia_valid = 0;
866 	if (attr->ia_valid & ATTR_MODE) {
867 		attrs.ia_valid |= HOSTFS_ATTR_MODE;
868 		attrs.ia_mode = attr->ia_mode;
869 	}
870 	if (attr->ia_valid & ATTR_UID) {
871 		attrs.ia_valid |= HOSTFS_ATTR_UID;
872 		attrs.ia_uid = attr->ia_uid;
873 	}
874 	if (attr->ia_valid & ATTR_GID) {
875 		attrs.ia_valid |= HOSTFS_ATTR_GID;
876 		attrs.ia_gid = attr->ia_gid;
877 	}
878 	if (attr->ia_valid & ATTR_SIZE) {
879 		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
880 		attrs.ia_size = attr->ia_size;
881 	}
882 	if (attr->ia_valid & ATTR_ATIME) {
883 		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
884 		attrs.ia_atime = attr->ia_atime;
885 	}
886 	if (attr->ia_valid & ATTR_MTIME) {
887 		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
888 		attrs.ia_mtime = attr->ia_mtime;
889 	}
890 	if (attr->ia_valid & ATTR_CTIME) {
891 		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
892 		attrs.ia_ctime = attr->ia_ctime;
893 	}
894 	if (attr->ia_valid & ATTR_ATIME_SET) {
895 		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
896 	}
897 	if (attr->ia_valid & ATTR_MTIME_SET) {
898 		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
899 	}
900 	name = dentry_name(dentry, 0);
901 	if (name == NULL)
902 		return -ENOMEM;
903 	err = set_attr(name, &attrs, fd);
904 	kfree(name);
905 	if (err)
906 		return err;
907 
908 	return inode_setattr(dentry->d_inode, attr);
909 }
910 
911 static const struct inode_operations hostfs_iops = {
912 	.create		= hostfs_create,
913 	.link		= hostfs_link,
914 	.unlink		= hostfs_unlink,
915 	.symlink	= hostfs_symlink,
916 	.mkdir		= hostfs_mkdir,
917 	.rmdir		= hostfs_rmdir,
918 	.mknod		= hostfs_mknod,
919 	.rename		= hostfs_rename,
920 	.permission	= hostfs_permission,
921 	.setattr	= hostfs_setattr,
922 };
923 
924 static const struct inode_operations hostfs_dir_iops = {
925 	.create		= hostfs_create,
926 	.lookup		= hostfs_lookup,
927 	.link		= hostfs_link,
928 	.unlink		= hostfs_unlink,
929 	.symlink	= hostfs_symlink,
930 	.mkdir		= hostfs_mkdir,
931 	.rmdir		= hostfs_rmdir,
932 	.mknod		= hostfs_mknod,
933 	.rename		= hostfs_rename,
934 	.permission	= hostfs_permission,
935 	.setattr	= hostfs_setattr,
936 };
937 
938 int hostfs_link_readpage(struct file *file, struct page *page)
939 {
940 	char *buffer, *name;
941 	int err;
942 
943 	buffer = kmap(page);
944 	name = inode_name(page->mapping->host, 0);
945 	if (name == NULL)
946 		return -ENOMEM;
947 	err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE);
948 	kfree(name);
949 	if (err == PAGE_CACHE_SIZE)
950 		err = -E2BIG;
951 	else if (err > 0) {
952 		flush_dcache_page(page);
953 		SetPageUptodate(page);
954 		if (PageError(page)) ClearPageError(page);
955 		err = 0;
956 	}
957 	kunmap(page);
958 	unlock_page(page);
959 	return err;
960 }
961 
962 static const struct address_space_operations hostfs_link_aops = {
963 	.readpage	= hostfs_link_readpage,
964 };
965 
966 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
967 {
968 	struct inode *root_inode;
969 	char *host_root_path, *req_root = d;
970 	int err;
971 
972 	sb->s_blocksize = 1024;
973 	sb->s_blocksize_bits = 10;
974 	sb->s_magic = HOSTFS_SUPER_MAGIC;
975 	sb->s_op = &hostfs_sbops;
976 	sb->s_maxbytes = MAX_LFS_FILESIZE;
977 
978 	/* NULL is printed as <NULL> by sprintf: avoid that. */
979 	if (req_root == NULL)
980 		req_root = "";
981 
982 	err = -ENOMEM;
983 	host_root_path = kmalloc(strlen(root_ino) + 1
984 				 + strlen(req_root) + 1, GFP_KERNEL);
985 	if (host_root_path == NULL)
986 		goto out;
987 
988 	sprintf(host_root_path, "%s/%s", root_ino, req_root);
989 
990 	root_inode = hostfs_iget(sb);
991 	if (IS_ERR(root_inode)) {
992 		err = PTR_ERR(root_inode);
993 		goto out_free;
994 	}
995 
996 	err = init_inode(root_inode, NULL);
997 	if (err)
998 		goto out_put;
999 
1000 	HOSTFS_I(root_inode)->host_filename = host_root_path;
1001 	/*
1002 	 * Avoid that in the error path, iput(root_inode) frees again
1003 	 * host_root_path through hostfs_destroy_inode!
1004 	 */
1005 	host_root_path = NULL;
1006 
1007 	err = -ENOMEM;
1008 	sb->s_root = d_alloc_root(root_inode);
1009 	if (sb->s_root == NULL)
1010 		goto out_put;
1011 
1012 	err = hostfs_read_inode(root_inode);
1013 	if (err) {
1014 		/* No iput in this case because the dput does that for us */
1015 		dput(sb->s_root);
1016 		sb->s_root = NULL;
1017 		goto out;
1018 	}
1019 
1020 	return 0;
1021 
1022 out_put:
1023 	iput(root_inode);
1024 out_free:
1025 	kfree(host_root_path);
1026 out:
1027 	return err;
1028 }
1029 
1030 static int hostfs_read_sb(struct file_system_type *type,
1031 			  int flags, const char *dev_name,
1032 			  void *data, struct vfsmount *mnt)
1033 {
1034 	return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1035 }
1036 
1037 static struct file_system_type hostfs_type = {
1038 	.owner 		= THIS_MODULE,
1039 	.name 		= "hostfs",
1040 	.get_sb 	= hostfs_read_sb,
1041 	.kill_sb	= kill_anon_super,
1042 	.fs_flags 	= 0,
1043 };
1044 
1045 static int __init init_hostfs(void)
1046 {
1047 	return register_filesystem(&hostfs_type);
1048 }
1049 
1050 static void __exit exit_hostfs(void)
1051 {
1052 	unregister_filesystem(&hostfs_type);
1053 }
1054 
1055 module_init(init_hostfs)
1056 module_exit(exit_hostfs)
1057 MODULE_LICENSE("GPL");
1058