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 <[email protected]>
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/magic.h>
11 #include <linux/module.h>
12 #include <linux/mm.h>
13 #include <linux/pagemap.h>
14 #include <linux/statfs.h>
15 #include <linux/slab.h>
16 #include <linux/seq_file.h>
17 #include <linux/writeback.h>
18 #include <linux/mount.h>
19 #include <linux/fs_context.h>
20 #include <linux/fs_parser.h>
21 #include <linux/namei.h>
22 #include "hostfs.h"
23 #include <init.h>
24 #include <kern.h>
25 
26 struct hostfs_fs_info {
27 	char *host_root_path;
28 };
29 
30 struct hostfs_inode_info {
31 	int fd;
32 	fmode_t mode;
33 	struct inode vfs_inode;
34 	struct mutex open_mutex;
35 	dev_t dev;
36 	struct hostfs_timespec btime;
37 };
38 
HOSTFS_I(struct inode * inode)39 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
40 {
41 	return list_entry(inode, struct hostfs_inode_info, vfs_inode);
42 }
43 
44 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
45 
46 static struct kmem_cache *hostfs_inode_cache;
47 
48 /* Changed in hostfs_args before the kernel starts running */
49 static char *root_ino = "";
50 static int append = 0;
51 
52 static const struct inode_operations hostfs_iops;
53 static const struct inode_operations hostfs_dir_iops;
54 static const struct inode_operations hostfs_link_iops;
55 
56 #ifndef MODULE
hostfs_args(char * options,int * add)57 static int __init hostfs_args(char *options, int *add)
58 {
59 	char *ptr;
60 
61 	*add = 0;
62 	ptr = strchr(options, ',');
63 	if (ptr != NULL)
64 		*ptr++ = '\0';
65 	if (*options != '\0')
66 		root_ino = options;
67 
68 	options = ptr;
69 	while (options) {
70 		ptr = strchr(options, ',');
71 		if (ptr != NULL)
72 			*ptr++ = '\0';
73 		if (*options != '\0') {
74 			if (!strcmp(options, "append"))
75 				append = 1;
76 			else printf("hostfs_args - unsupported option - %s\n",
77 				    options);
78 		}
79 		options = ptr;
80 	}
81 	return 0;
82 }
83 
84 __uml_setup("hostfs=", hostfs_args,
85 "hostfs=<root dir>,<flags>,...\n"
86 "    This is used to set hostfs parameters.  The root directory argument\n"
87 "    is used to confine all hostfs mounts to within the specified directory\n"
88 "    tree on the host.  If this isn't specified, then a user inside UML can\n"
89 "    mount anything on the host that's accessible to the user that's running\n"
90 "    it.\n"
91 "    The only flag currently supported is 'append', which specifies that all\n"
92 "    files opened by hostfs will be opened in append mode.\n\n"
93 );
94 #endif
95 
__dentry_name(struct dentry * dentry,char * name)96 static char *__dentry_name(struct dentry *dentry, char *name)
97 {
98 	char *p = dentry_path_raw(dentry, name, PATH_MAX);
99 	struct hostfs_fs_info *fsi = dentry->d_sb->s_fs_info;
100 	char *root = fsi->host_root_path;
101 	size_t len = strlen(root);
102 
103 	if (IS_ERR(p) || len > p - name) {
104 		__putname(name);
105 		return NULL;
106 	}
107 
108 	memcpy(name, root, len);
109 	memmove(name + len, p, name + PATH_MAX - p);
110 
111 	return name;
112 }
113 
dentry_name(struct dentry * dentry)114 static char *dentry_name(struct dentry *dentry)
115 {
116 	char *name = __getname();
117 	if (!name)
118 		return NULL;
119 
120 	return __dentry_name(dentry, name);
121 }
122 
inode_name(struct inode * ino)123 static char *inode_name(struct inode *ino)
124 {
125 	struct dentry *dentry;
126 	char *name;
127 
128 	dentry = d_find_alias(ino);
129 	if (!dentry)
130 		return NULL;
131 
132 	name = dentry_name(dentry);
133 
134 	dput(dentry);
135 
136 	return name;
137 }
138 
follow_link(char * link)139 static char *follow_link(char *link)
140 {
141 	char *name, *resolved, *end;
142 	int n;
143 
144 	name = kmalloc(PATH_MAX, GFP_KERNEL);
145 	if (!name) {
146 		n = -ENOMEM;
147 		goto out_free;
148 	}
149 
150 	n = hostfs_do_readlink(link, name, PATH_MAX);
151 	if (n < 0)
152 		goto out_free;
153 	else if (n == PATH_MAX) {
154 		n = -E2BIG;
155 		goto out_free;
156 	}
157 
158 	if (*name == '/')
159 		return name;
160 
161 	end = strrchr(link, '/');
162 	if (end == NULL)
163 		return name;
164 
165 	*(end + 1) = '\0';
166 
167 	resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
168 	if (resolved == NULL) {
169 		n = -ENOMEM;
170 		goto out_free;
171 	}
172 
173 	kfree(name);
174 	return resolved;
175 
176  out_free:
177 	kfree(name);
178 	return ERR_PTR(n);
179 }
180 
hostfs_statfs(struct dentry * dentry,struct kstatfs * sf)181 static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
182 {
183 	/*
184 	 * do_statfs uses struct statfs64 internally, but the linux kernel
185 	 * struct statfs still has 32-bit versions for most of these fields,
186 	 * so we convert them here
187 	 */
188 	int err;
189 	long long f_blocks;
190 	long long f_bfree;
191 	long long f_bavail;
192 	long long f_files;
193 	long long f_ffree;
194 	struct hostfs_fs_info *fsi;
195 
196 	fsi = dentry->d_sb->s_fs_info;
197 	err = do_statfs(fsi->host_root_path,
198 			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
199 			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
200 			&sf->f_namelen);
201 	if (err)
202 		return err;
203 	sf->f_blocks = f_blocks;
204 	sf->f_bfree = f_bfree;
205 	sf->f_bavail = f_bavail;
206 	sf->f_files = f_files;
207 	sf->f_ffree = f_ffree;
208 	sf->f_type = HOSTFS_SUPER_MAGIC;
209 	return 0;
210 }
211 
hostfs_alloc_inode(struct super_block * sb)212 static struct inode *hostfs_alloc_inode(struct super_block *sb)
213 {
214 	struct hostfs_inode_info *hi;
215 
216 	hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
217 	if (hi == NULL)
218 		return NULL;
219 	hi->fd = -1;
220 	hi->mode = 0;
221 	hi->dev = 0;
222 	inode_init_once(&hi->vfs_inode);
223 	mutex_init(&hi->open_mutex);
224 	return &hi->vfs_inode;
225 }
226 
hostfs_evict_inode(struct inode * inode)227 static void hostfs_evict_inode(struct inode *inode)
228 {
229 	truncate_inode_pages_final(&inode->i_data);
230 	clear_inode(inode);
231 	if (HOSTFS_I(inode)->fd != -1) {
232 		close_file(&HOSTFS_I(inode)->fd);
233 		HOSTFS_I(inode)->fd = -1;
234 		HOSTFS_I(inode)->dev = 0;
235 	}
236 }
237 
hostfs_free_inode(struct inode * inode)238 static void hostfs_free_inode(struct inode *inode)
239 {
240 	kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
241 }
242 
hostfs_show_options(struct seq_file * seq,struct dentry * root)243 static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
244 {
245 	struct hostfs_fs_info *fsi;
246 	const char *root_path;
247 
248 	fsi = root->d_sb->s_fs_info;
249 	root_path = fsi->host_root_path;
250 	size_t offset = strlen(root_ino) + 1;
251 
252 	if (strlen(root_path) > offset)
253 		seq_show_option(seq, root_path + offset, NULL);
254 
255 	if (append)
256 		seq_puts(seq, ",append");
257 
258 	return 0;
259 }
260 
261 static const struct super_operations hostfs_sbops = {
262 	.alloc_inode	= hostfs_alloc_inode,
263 	.free_inode	= hostfs_free_inode,
264 	.drop_inode	= generic_delete_inode,
265 	.evict_inode	= hostfs_evict_inode,
266 	.statfs		= hostfs_statfs,
267 	.show_options	= hostfs_show_options,
268 };
269 
hostfs_readdir(struct file * file,struct dir_context * ctx)270 static int hostfs_readdir(struct file *file, struct dir_context *ctx)
271 {
272 	void *dir;
273 	char *name;
274 	unsigned long long next, ino;
275 	int error, len;
276 	unsigned int type;
277 
278 	name = dentry_name(file->f_path.dentry);
279 	if (name == NULL)
280 		return -ENOMEM;
281 	dir = open_dir(name, &error);
282 	__putname(name);
283 	if (dir == NULL)
284 		return -error;
285 	next = ctx->pos;
286 	seek_dir(dir, next);
287 	while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
288 		if (!dir_emit(ctx, name, len, ino, type))
289 			break;
290 		ctx->pos = next;
291 	}
292 	close_dir(dir);
293 	return 0;
294 }
295 
hostfs_open(struct inode * ino,struct file * file)296 static int hostfs_open(struct inode *ino, struct file *file)
297 {
298 	char *name;
299 	fmode_t mode;
300 	int err;
301 	int r, w, fd;
302 
303 	mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
304 	if ((mode & HOSTFS_I(ino)->mode) == mode)
305 		return 0;
306 
307 	mode |= HOSTFS_I(ino)->mode;
308 
309 retry:
310 	r = w = 0;
311 
312 	if (mode & FMODE_READ)
313 		r = 1;
314 	if (mode & FMODE_WRITE)
315 		r = w = 1;
316 
317 	name = dentry_name(file_dentry(file));
318 	if (name == NULL)
319 		return -ENOMEM;
320 
321 	fd = open_file(name, r, w, append);
322 	__putname(name);
323 	if (fd < 0)
324 		return fd;
325 
326 	mutex_lock(&HOSTFS_I(ino)->open_mutex);
327 	/* somebody else had handled it first? */
328 	if ((mode & HOSTFS_I(ino)->mode) == mode) {
329 		mutex_unlock(&HOSTFS_I(ino)->open_mutex);
330 		close_file(&fd);
331 		return 0;
332 	}
333 	if ((mode | HOSTFS_I(ino)->mode) != mode) {
334 		mode |= HOSTFS_I(ino)->mode;
335 		mutex_unlock(&HOSTFS_I(ino)->open_mutex);
336 		close_file(&fd);
337 		goto retry;
338 	}
339 	if (HOSTFS_I(ino)->fd == -1) {
340 		HOSTFS_I(ino)->fd = fd;
341 	} else {
342 		err = replace_file(fd, HOSTFS_I(ino)->fd);
343 		close_file(&fd);
344 		if (err < 0) {
345 			mutex_unlock(&HOSTFS_I(ino)->open_mutex);
346 			return err;
347 		}
348 	}
349 	HOSTFS_I(ino)->mode = mode;
350 	mutex_unlock(&HOSTFS_I(ino)->open_mutex);
351 
352 	return 0;
353 }
354 
hostfs_file_release(struct inode * inode,struct file * file)355 static int hostfs_file_release(struct inode *inode, struct file *file)
356 {
357 	filemap_write_and_wait(inode->i_mapping);
358 
359 	return 0;
360 }
361 
hostfs_fsync(struct file * file,loff_t start,loff_t end,int datasync)362 static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
363 			int datasync)
364 {
365 	struct inode *inode = file->f_mapping->host;
366 	int ret;
367 
368 	ret = file_write_and_wait_range(file, start, end);
369 	if (ret)
370 		return ret;
371 
372 	inode_lock(inode);
373 	ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
374 	inode_unlock(inode);
375 
376 	return ret;
377 }
378 
379 static const struct file_operations hostfs_file_fops = {
380 	.llseek		= generic_file_llseek,
381 	.splice_read	= filemap_splice_read,
382 	.splice_write	= iter_file_splice_write,
383 	.read_iter	= generic_file_read_iter,
384 	.write_iter	= generic_file_write_iter,
385 	.mmap		= generic_file_mmap,
386 	.open		= hostfs_open,
387 	.release	= hostfs_file_release,
388 	.fsync		= hostfs_fsync,
389 };
390 
391 static const struct file_operations hostfs_dir_fops = {
392 	.llseek		= generic_file_llseek,
393 	.iterate_shared	= hostfs_readdir,
394 	.read		= generic_read_dir,
395 	.open		= hostfs_open,
396 	.fsync		= hostfs_fsync,
397 };
398 
hostfs_writepages(struct address_space * mapping,struct writeback_control * wbc)399 static int hostfs_writepages(struct address_space *mapping,
400 		struct writeback_control *wbc)
401 {
402 	struct inode *inode = mapping->host;
403 	struct folio *folio = NULL;
404 	loff_t i_size = i_size_read(inode);
405 	int err = 0;
406 
407 	while ((folio = writeback_iter(mapping, wbc, folio, &err))) {
408 		loff_t pos = folio_pos(folio);
409 		size_t count = folio_size(folio);
410 		char *buffer;
411 		int ret;
412 
413 		if (count > i_size - pos)
414 			count = i_size - pos;
415 
416 		buffer = kmap_local_folio(folio, 0);
417 		ret = write_file(HOSTFS_I(inode)->fd, &pos, buffer, count);
418 		kunmap_local(buffer);
419 		folio_unlock(folio);
420 		if (ret != count) {
421 			err = ret < 0 ? ret : -EIO;
422 			mapping_set_error(mapping, err);
423 		}
424 	}
425 
426 	return err;
427 }
428 
hostfs_read_folio(struct file * file,struct folio * folio)429 static int hostfs_read_folio(struct file *file, struct folio *folio)
430 {
431 	char *buffer;
432 	loff_t start = folio_pos(folio);
433 	int bytes_read, ret = 0;
434 
435 	buffer = kmap_local_folio(folio, 0);
436 	bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
437 			PAGE_SIZE);
438 	if (bytes_read < 0)
439 		ret = bytes_read;
440 	else
441 		buffer = folio_zero_tail(folio, bytes_read, buffer + bytes_read);
442 	kunmap_local(buffer);
443 
444 	folio_end_read(folio, ret == 0);
445 	return ret;
446 }
447 
hostfs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct folio ** foliop,void ** fsdata)448 static int hostfs_write_begin(struct file *file, struct address_space *mapping,
449 			      loff_t pos, unsigned len,
450 			      struct folio **foliop, void **fsdata)
451 {
452 	pgoff_t index = pos >> PAGE_SHIFT;
453 
454 	*foliop = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
455 			mapping_gfp_mask(mapping));
456 	if (IS_ERR(*foliop))
457 		return PTR_ERR(*foliop);
458 	return 0;
459 }
460 
hostfs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct folio * folio,void * fsdata)461 static int hostfs_write_end(struct file *file, struct address_space *mapping,
462 			    loff_t pos, unsigned len, unsigned copied,
463 			    struct folio *folio, void *fsdata)
464 {
465 	struct inode *inode = mapping->host;
466 	void *buffer;
467 	size_t from = offset_in_folio(folio, pos);
468 	int err;
469 
470 	buffer = kmap_local_folio(folio, from);
471 	err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer, copied);
472 	kunmap_local(buffer);
473 
474 	if (!folio_test_uptodate(folio) && err == folio_size(folio))
475 		folio_mark_uptodate(folio);
476 
477 	/*
478 	 * If err > 0, write_file has added err to pos, so we are comparing
479 	 * i_size against the last byte written.
480 	 */
481 	if (err > 0 && (pos > inode->i_size))
482 		inode->i_size = pos;
483 	folio_unlock(folio);
484 	folio_put(folio);
485 
486 	return err;
487 }
488 
489 static const struct address_space_operations hostfs_aops = {
490 	.writepages 	= hostfs_writepages,
491 	.read_folio	= hostfs_read_folio,
492 	.dirty_folio	= filemap_dirty_folio,
493 	.write_begin	= hostfs_write_begin,
494 	.write_end	= hostfs_write_end,
495 	.migrate_folio	= filemap_migrate_folio,
496 };
497 
hostfs_inode_update(struct inode * ino,const struct hostfs_stat * st)498 static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st)
499 {
500 	set_nlink(ino, st->nlink);
501 	i_uid_write(ino, st->uid);
502 	i_gid_write(ino, st->gid);
503 	inode_set_atime_to_ts(ino, (struct timespec64){
504 			st->atime.tv_sec,
505 			st->atime.tv_nsec,
506 		});
507 	inode_set_mtime_to_ts(ino, (struct timespec64){
508 			st->mtime.tv_sec,
509 			st->mtime.tv_nsec,
510 		});
511 	inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec);
512 	ino->i_size = st->size;
513 	ino->i_blocks = st->blocks;
514 	return 0;
515 }
516 
hostfs_inode_set(struct inode * ino,void * data)517 static int hostfs_inode_set(struct inode *ino, void *data)
518 {
519 	struct hostfs_stat *st = data;
520 	dev_t dev, rdev;
521 
522 	/* Reencode maj and min with the kernel encoding.*/
523 	rdev = MKDEV(st->rdev.maj, st->rdev.min);
524 	dev = MKDEV(st->dev.maj, st->dev.min);
525 
526 	switch (st->mode & S_IFMT) {
527 	case S_IFLNK:
528 		ino->i_op = &hostfs_link_iops;
529 		break;
530 	case S_IFDIR:
531 		ino->i_op = &hostfs_dir_iops;
532 		ino->i_fop = &hostfs_dir_fops;
533 		break;
534 	case S_IFCHR:
535 	case S_IFBLK:
536 	case S_IFIFO:
537 	case S_IFSOCK:
538 		init_special_inode(ino, st->mode & S_IFMT, rdev);
539 		ino->i_op = &hostfs_iops;
540 		break;
541 	case S_IFREG:
542 		ino->i_op = &hostfs_iops;
543 		ino->i_fop = &hostfs_file_fops;
544 		ino->i_mapping->a_ops = &hostfs_aops;
545 		break;
546 	default:
547 		return -EIO;
548 	}
549 
550 	HOSTFS_I(ino)->dev = dev;
551 	HOSTFS_I(ino)->btime = st->btime;
552 	ino->i_ino = st->ino;
553 	ino->i_mode = st->mode;
554 	return hostfs_inode_update(ino, st);
555 }
556 
hostfs_inode_test(struct inode * inode,void * data)557 static int hostfs_inode_test(struct inode *inode, void *data)
558 {
559 	const struct hostfs_stat *st = data;
560 	dev_t dev = MKDEV(st->dev.maj, st->dev.min);
561 
562 	return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == dev &&
563 	       (inode->i_mode & S_IFMT) == (st->mode & S_IFMT) &&
564 	       HOSTFS_I(inode)->btime.tv_sec == st->btime.tv_sec &&
565 	       HOSTFS_I(inode)->btime.tv_nsec == st->btime.tv_nsec;
566 }
567 
hostfs_iget(struct super_block * sb,char * name)568 static struct inode *hostfs_iget(struct super_block *sb, char *name)
569 {
570 	struct inode *inode;
571 	struct hostfs_stat st;
572 	int err = stat_file(name, &st, -1);
573 
574 	if (err)
575 		return ERR_PTR(err);
576 
577 	inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set,
578 			     &st);
579 	if (!inode)
580 		return ERR_PTR(-ENOMEM);
581 
582 	if (inode->i_state & I_NEW) {
583 		unlock_new_inode(inode);
584 	} else {
585 		spin_lock(&inode->i_lock);
586 		hostfs_inode_update(inode, &st);
587 		spin_unlock(&inode->i_lock);
588 	}
589 
590 	return inode;
591 }
592 
hostfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)593 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir,
594 			 struct dentry *dentry, umode_t mode, bool excl)
595 {
596 	struct inode *inode;
597 	char *name;
598 	int fd;
599 
600 	name = dentry_name(dentry);
601 	if (name == NULL)
602 		return -ENOMEM;
603 
604 	fd = file_create(name, mode & 0777);
605 	if (fd < 0) {
606 		__putname(name);
607 		return fd;
608 	}
609 
610 	inode = hostfs_iget(dir->i_sb, name);
611 	__putname(name);
612 	if (IS_ERR(inode))
613 		return PTR_ERR(inode);
614 
615 	HOSTFS_I(inode)->fd = fd;
616 	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
617 	d_instantiate(dentry, inode);
618 	return 0;
619 }
620 
hostfs_lookup(struct inode * ino,struct dentry * dentry,unsigned int flags)621 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
622 				    unsigned int flags)
623 {
624 	struct inode *inode = NULL;
625 	char *name;
626 
627 	name = dentry_name(dentry);
628 	if (name == NULL)
629 		return ERR_PTR(-ENOMEM);
630 
631 	inode = hostfs_iget(ino->i_sb, name);
632 	__putname(name);
633 	if (inode == ERR_PTR(-ENOENT))
634 		inode = NULL;
635 
636 	return d_splice_alias(inode, dentry);
637 }
638 
hostfs_link(struct dentry * to,struct inode * ino,struct dentry * from)639 static int hostfs_link(struct dentry *to, struct inode *ino,
640 		       struct dentry *from)
641 {
642 	char *from_name, *to_name;
643 	int err;
644 
645 	if ((from_name = dentry_name(from)) == NULL)
646 		return -ENOMEM;
647 	to_name = dentry_name(to);
648 	if (to_name == NULL) {
649 		__putname(from_name);
650 		return -ENOMEM;
651 	}
652 	err = link_file(to_name, from_name);
653 	__putname(from_name);
654 	__putname(to_name);
655 	return err;
656 }
657 
hostfs_unlink(struct inode * ino,struct dentry * dentry)658 static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
659 {
660 	char *file;
661 	int err;
662 
663 	if (append)
664 		return -EPERM;
665 
666 	if ((file = dentry_name(dentry)) == NULL)
667 		return -ENOMEM;
668 
669 	err = unlink_file(file);
670 	__putname(file);
671 	return err;
672 }
673 
hostfs_symlink(struct mnt_idmap * idmap,struct inode * ino,struct dentry * dentry,const char * to)674 static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino,
675 			  struct dentry *dentry, const char *to)
676 {
677 	char *file;
678 	int err;
679 
680 	if ((file = dentry_name(dentry)) == NULL)
681 		return -ENOMEM;
682 	err = make_symlink(file, to);
683 	__putname(file);
684 	return err;
685 }
686 
hostfs_mkdir(struct mnt_idmap * idmap,struct inode * ino,struct dentry * dentry,umode_t mode)687 static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino,
688 			struct dentry *dentry, umode_t mode)
689 {
690 	char *file;
691 	int err;
692 
693 	if ((file = dentry_name(dentry)) == NULL)
694 		return -ENOMEM;
695 	err = do_mkdir(file, mode);
696 	__putname(file);
697 	return err;
698 }
699 
hostfs_rmdir(struct inode * ino,struct dentry * dentry)700 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
701 {
702 	char *file;
703 	int err;
704 
705 	if ((file = dentry_name(dentry)) == NULL)
706 		return -ENOMEM;
707 	err = hostfs_do_rmdir(file);
708 	__putname(file);
709 	return err;
710 }
711 
hostfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)712 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
713 			struct dentry *dentry, umode_t mode, dev_t dev)
714 {
715 	struct inode *inode;
716 	char *name;
717 	int err;
718 
719 	name = dentry_name(dentry);
720 	if (name == NULL)
721 		return -ENOMEM;
722 
723 	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
724 	if (err) {
725 		__putname(name);
726 		return err;
727 	}
728 
729 	inode = hostfs_iget(dir->i_sb, name);
730 	__putname(name);
731 	if (IS_ERR(inode))
732 		return PTR_ERR(inode);
733 
734 	d_instantiate(dentry, inode);
735 	return 0;
736 }
737 
hostfs_rename2(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)738 static int hostfs_rename2(struct mnt_idmap *idmap,
739 			  struct inode *old_dir, struct dentry *old_dentry,
740 			  struct inode *new_dir, struct dentry *new_dentry,
741 			  unsigned int flags)
742 {
743 	char *old_name, *new_name;
744 	int err;
745 
746 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
747 		return -EINVAL;
748 
749 	old_name = dentry_name(old_dentry);
750 	if (old_name == NULL)
751 		return -ENOMEM;
752 	new_name = dentry_name(new_dentry);
753 	if (new_name == NULL) {
754 		__putname(old_name);
755 		return -ENOMEM;
756 	}
757 	if (!flags)
758 		err = rename_file(old_name, new_name);
759 	else
760 		err = rename2_file(old_name, new_name, flags);
761 
762 	__putname(old_name);
763 	__putname(new_name);
764 	return err;
765 }
766 
hostfs_permission(struct mnt_idmap * idmap,struct inode * ino,int desired)767 static int hostfs_permission(struct mnt_idmap *idmap,
768 			     struct inode *ino, int desired)
769 {
770 	char *name;
771 	int r = 0, w = 0, x = 0, err;
772 
773 	if (desired & MAY_NOT_BLOCK)
774 		return -ECHILD;
775 
776 	if (desired & MAY_READ) r = 1;
777 	if (desired & MAY_WRITE) w = 1;
778 	if (desired & MAY_EXEC) x = 1;
779 	name = inode_name(ino);
780 	if (name == NULL)
781 		return -ENOMEM;
782 
783 	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
784 	    S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
785 		err = 0;
786 	else
787 		err = access_file(name, r, w, x);
788 	__putname(name);
789 	if (!err)
790 		err = generic_permission(&nop_mnt_idmap, ino, desired);
791 	return err;
792 }
793 
hostfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)794 static int hostfs_setattr(struct mnt_idmap *idmap,
795 			  struct dentry *dentry, struct iattr *attr)
796 {
797 	struct inode *inode = d_inode(dentry);
798 	struct hostfs_iattr attrs;
799 	char *name;
800 	int err;
801 
802 	int fd = HOSTFS_I(inode)->fd;
803 
804 	err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
805 	if (err)
806 		return err;
807 
808 	if (append)
809 		attr->ia_valid &= ~ATTR_SIZE;
810 
811 	attrs.ia_valid = 0;
812 	if (attr->ia_valid & ATTR_MODE) {
813 		attrs.ia_valid |= HOSTFS_ATTR_MODE;
814 		attrs.ia_mode = attr->ia_mode;
815 	}
816 	if (attr->ia_valid & ATTR_UID) {
817 		attrs.ia_valid |= HOSTFS_ATTR_UID;
818 		attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
819 	}
820 	if (attr->ia_valid & ATTR_GID) {
821 		attrs.ia_valid |= HOSTFS_ATTR_GID;
822 		attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
823 	}
824 	if (attr->ia_valid & ATTR_SIZE) {
825 		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
826 		attrs.ia_size = attr->ia_size;
827 	}
828 	if (attr->ia_valid & ATTR_ATIME) {
829 		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
830 		attrs.ia_atime = (struct hostfs_timespec)
831 			{ attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec };
832 	}
833 	if (attr->ia_valid & ATTR_MTIME) {
834 		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
835 		attrs.ia_mtime = (struct hostfs_timespec)
836 			{ attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec };
837 	}
838 	if (attr->ia_valid & ATTR_CTIME) {
839 		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
840 		attrs.ia_ctime = (struct hostfs_timespec)
841 			{ attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec };
842 	}
843 	if (attr->ia_valid & ATTR_ATIME_SET) {
844 		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
845 	}
846 	if (attr->ia_valid & ATTR_MTIME_SET) {
847 		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
848 	}
849 	name = dentry_name(dentry);
850 	if (name == NULL)
851 		return -ENOMEM;
852 	err = set_attr(name, &attrs, fd);
853 	__putname(name);
854 	if (err)
855 		return err;
856 
857 	if ((attr->ia_valid & ATTR_SIZE) &&
858 	    attr->ia_size != i_size_read(inode))
859 		truncate_setsize(inode, attr->ia_size);
860 
861 	setattr_copy(&nop_mnt_idmap, inode, attr);
862 	mark_inode_dirty(inode);
863 	return 0;
864 }
865 
866 static const struct inode_operations hostfs_iops = {
867 	.permission	= hostfs_permission,
868 	.setattr	= hostfs_setattr,
869 };
870 
871 static const struct inode_operations hostfs_dir_iops = {
872 	.create		= hostfs_create,
873 	.lookup		= hostfs_lookup,
874 	.link		= hostfs_link,
875 	.unlink		= hostfs_unlink,
876 	.symlink	= hostfs_symlink,
877 	.mkdir		= hostfs_mkdir,
878 	.rmdir		= hostfs_rmdir,
879 	.mknod		= hostfs_mknod,
880 	.rename		= hostfs_rename2,
881 	.permission	= hostfs_permission,
882 	.setattr	= hostfs_setattr,
883 };
884 
hostfs_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)885 static const char *hostfs_get_link(struct dentry *dentry,
886 				   struct inode *inode,
887 				   struct delayed_call *done)
888 {
889 	char *link;
890 	if (!dentry)
891 		return ERR_PTR(-ECHILD);
892 	link = kmalloc(PATH_MAX, GFP_KERNEL);
893 	if (link) {
894 		char *path = dentry_name(dentry);
895 		int err = -ENOMEM;
896 		if (path) {
897 			err = hostfs_do_readlink(path, link, PATH_MAX);
898 			if (err == PATH_MAX)
899 				err = -E2BIG;
900 			__putname(path);
901 		}
902 		if (err < 0) {
903 			kfree(link);
904 			return ERR_PTR(err);
905 		}
906 	} else {
907 		return ERR_PTR(-ENOMEM);
908 	}
909 
910 	set_delayed_call(done, kfree_link, link);
911 	return link;
912 }
913 
914 static const struct inode_operations hostfs_link_iops = {
915 	.get_link	= hostfs_get_link,
916 };
917 
hostfs_fill_super(struct super_block * sb,struct fs_context * fc)918 static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc)
919 {
920 	struct hostfs_fs_info *fsi = sb->s_fs_info;
921 	struct inode *root_inode;
922 	int err;
923 
924 	sb->s_blocksize = 1024;
925 	sb->s_blocksize_bits = 10;
926 	sb->s_magic = HOSTFS_SUPER_MAGIC;
927 	sb->s_op = &hostfs_sbops;
928 	sb->s_d_op = &simple_dentry_operations;
929 	sb->s_maxbytes = MAX_LFS_FILESIZE;
930 	err = super_setup_bdi(sb);
931 	if (err)
932 		return err;
933 
934 	root_inode = hostfs_iget(sb, fsi->host_root_path);
935 	if (IS_ERR(root_inode))
936 		return PTR_ERR(root_inode);
937 
938 	if (S_ISLNK(root_inode->i_mode)) {
939 		char *name;
940 
941 		iput(root_inode);
942 		name = follow_link(fsi->host_root_path);
943 		if (IS_ERR(name))
944 			return PTR_ERR(name);
945 
946 		root_inode = hostfs_iget(sb, name);
947 		kfree(name);
948 		if (IS_ERR(root_inode))
949 			return PTR_ERR(root_inode);
950 	}
951 
952 	sb->s_root = d_make_root(root_inode);
953 	if (sb->s_root == NULL)
954 		return -ENOMEM;
955 
956 	return 0;
957 }
958 
959 enum hostfs_parma {
960 	Opt_hostfs,
961 };
962 
963 static const struct fs_parameter_spec hostfs_param_specs[] = {
964 	fsparam_string_empty("hostfs",		Opt_hostfs),
965 	{}
966 };
967 
hostfs_parse_param(struct fs_context * fc,struct fs_parameter * param)968 static int hostfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
969 {
970 	struct hostfs_fs_info *fsi = fc->s_fs_info;
971 	struct fs_parse_result result;
972 	char *host_root;
973 	int opt;
974 
975 	opt = fs_parse(fc, hostfs_param_specs, param, &result);
976 	if (opt < 0)
977 		return opt;
978 
979 	switch (opt) {
980 	case Opt_hostfs:
981 		host_root = param->string;
982 		if (!*host_root)
983 			host_root = "";
984 		fsi->host_root_path =
985 			kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root);
986 		if (fsi->host_root_path == NULL)
987 			return -ENOMEM;
988 		break;
989 	}
990 
991 	return 0;
992 }
993 
hostfs_parse_monolithic(struct fs_context * fc,void * data)994 static int hostfs_parse_monolithic(struct fs_context *fc, void *data)
995 {
996 	struct hostfs_fs_info *fsi = fc->s_fs_info;
997 	char *host_root = (char *)data;
998 
999 	/* NULL is printed as '(null)' by printf(): avoid that. */
1000 	if (host_root == NULL)
1001 		host_root = "";
1002 
1003 	fsi->host_root_path =
1004 		kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root);
1005 	if (fsi->host_root_path == NULL)
1006 		return -ENOMEM;
1007 
1008 	return 0;
1009 }
1010 
hostfs_fc_get_tree(struct fs_context * fc)1011 static int hostfs_fc_get_tree(struct fs_context *fc)
1012 {
1013 	return get_tree_nodev(fc, hostfs_fill_super);
1014 }
1015 
hostfs_fc_free(struct fs_context * fc)1016 static void hostfs_fc_free(struct fs_context *fc)
1017 {
1018 	struct hostfs_fs_info *fsi = fc->s_fs_info;
1019 
1020 	if (!fsi)
1021 		return;
1022 
1023 	kfree(fsi->host_root_path);
1024 	kfree(fsi);
1025 }
1026 
1027 static const struct fs_context_operations hostfs_context_ops = {
1028 	.parse_monolithic = hostfs_parse_monolithic,
1029 	.parse_param	= hostfs_parse_param,
1030 	.get_tree	= hostfs_fc_get_tree,
1031 	.free		= hostfs_fc_free,
1032 };
1033 
hostfs_init_fs_context(struct fs_context * fc)1034 static int hostfs_init_fs_context(struct fs_context *fc)
1035 {
1036 	struct hostfs_fs_info *fsi;
1037 
1038 	fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
1039 	if (!fsi)
1040 		return -ENOMEM;
1041 
1042 	fc->s_fs_info = fsi;
1043 	fc->ops = &hostfs_context_ops;
1044 	return 0;
1045 }
1046 
hostfs_kill_sb(struct super_block * s)1047 static void hostfs_kill_sb(struct super_block *s)
1048 {
1049 	kill_anon_super(s);
1050 	kfree(s->s_fs_info);
1051 }
1052 
1053 static struct file_system_type hostfs_type = {
1054 	.owner			= THIS_MODULE,
1055 	.name			= "hostfs",
1056 	.init_fs_context	= hostfs_init_fs_context,
1057 	.kill_sb		= hostfs_kill_sb,
1058 	.fs_flags		= 0,
1059 };
1060 MODULE_ALIAS_FS("hostfs");
1061 
init_hostfs(void)1062 static int __init init_hostfs(void)
1063 {
1064 	hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
1065 	if (!hostfs_inode_cache)
1066 		return -ENOMEM;
1067 	return register_filesystem(&hostfs_type);
1068 }
1069 
exit_hostfs(void)1070 static void __exit exit_hostfs(void)
1071 {
1072 	unregister_filesystem(&hostfs_type);
1073 	kmem_cache_destroy(hostfs_inode_cache);
1074 }
1075 
1076 module_init(init_hostfs)
1077 module_exit(exit_hostfs)
1078 MODULE_DESCRIPTION("User-Mode Linux Host filesystem");
1079 MODULE_LICENSE("GPL");
1080