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/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/namei.h>
20 #include "hostfs.h"
21 #include <init.h>
22 #include <kern.h>
23
24 struct hostfs_inode_info {
25 int fd;
26 fmode_t mode;
27 struct inode vfs_inode;
28 struct mutex open_mutex;
29 dev_t dev;
30 struct hostfs_timespec btime;
31 };
32
HOSTFS_I(struct inode * inode)33 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
34 {
35 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
36 }
37
38 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
39
40 static struct kmem_cache *hostfs_inode_cache;
41
42 /* Changed in hostfs_args before the kernel starts running */
43 static char *root_ino = "";
44 static int append = 0;
45
46 static const struct inode_operations hostfs_iops;
47 static const struct inode_operations hostfs_dir_iops;
48 static const struct inode_operations hostfs_link_iops;
49
50 #ifndef MODULE
hostfs_args(char * options,int * add)51 static int __init hostfs_args(char *options, int *add)
52 {
53 char *ptr;
54
55 ptr = strchr(options, ',');
56 if (ptr != NULL)
57 *ptr++ = '\0';
58 if (*options != '\0')
59 root_ino = options;
60
61 options = ptr;
62 while (options) {
63 ptr = strchr(options, ',');
64 if (ptr != NULL)
65 *ptr++ = '\0';
66 if (*options != '\0') {
67 if (!strcmp(options, "append"))
68 append = 1;
69 else printf("hostfs_args - unsupported option - %s\n",
70 options);
71 }
72 options = ptr;
73 }
74 return 0;
75 }
76
77 __uml_setup("hostfs=", hostfs_args,
78 "hostfs=<root dir>,<flags>,...\n"
79 " This is used to set hostfs parameters. The root directory argument\n"
80 " is used to confine all hostfs mounts to within the specified directory\n"
81 " tree on the host. If this isn't specified, then a user inside UML can\n"
82 " mount anything on the host that's accessible to the user that's running\n"
83 " it.\n"
84 " The only flag currently supported is 'append', which specifies that all\n"
85 " files opened by hostfs will be opened in append mode.\n\n"
86 );
87 #endif
88
__dentry_name(struct dentry * dentry,char * name)89 static char *__dentry_name(struct dentry *dentry, char *name)
90 {
91 char *p = dentry_path_raw(dentry, name, PATH_MAX);
92 char *root;
93 size_t len;
94
95 root = dentry->d_sb->s_fs_info;
96 len = strlen(root);
97 if (IS_ERR(p)) {
98 __putname(name);
99 return NULL;
100 }
101
102 /*
103 * This function relies on the fact that dentry_path_raw() will place
104 * the path name at the end of the provided buffer.
105 */
106 BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);
107
108 strscpy(name, root, PATH_MAX);
109 if (len > p - name) {
110 __putname(name);
111 return NULL;
112 }
113
114 if (p > name + len)
115 strcpy(name + len, p);
116
117 return name;
118 }
119
dentry_name(struct dentry * dentry)120 static char *dentry_name(struct dentry *dentry)
121 {
122 char *name = __getname();
123 if (!name)
124 return NULL;
125
126 return __dentry_name(dentry, name);
127 }
128
inode_name(struct inode * ino)129 static char *inode_name(struct inode *ino)
130 {
131 struct dentry *dentry;
132 char *name;
133
134 dentry = d_find_alias(ino);
135 if (!dentry)
136 return NULL;
137
138 name = dentry_name(dentry);
139
140 dput(dentry);
141
142 return name;
143 }
144
follow_link(char * link)145 static char *follow_link(char *link)
146 {
147 char *name, *resolved, *end;
148 int n;
149
150 name = kmalloc(PATH_MAX, GFP_KERNEL);
151 if (!name) {
152 n = -ENOMEM;
153 goto out_free;
154 }
155
156 n = hostfs_do_readlink(link, name, PATH_MAX);
157 if (n < 0)
158 goto out_free;
159 else if (n == PATH_MAX) {
160 n = -E2BIG;
161 goto out_free;
162 }
163
164 if (*name == '/')
165 return name;
166
167 end = strrchr(link, '/');
168 if (end == NULL)
169 return name;
170
171 *(end + 1) = '\0';
172
173 resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
174 if (resolved == NULL) {
175 n = -ENOMEM;
176 goto out_free;
177 }
178
179 kfree(name);
180 return resolved;
181
182 out_free:
183 kfree(name);
184 return ERR_PTR(n);
185 }
186
hostfs_statfs(struct dentry * dentry,struct kstatfs * sf)187 static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
188 {
189 /*
190 * do_statfs uses struct statfs64 internally, but the linux kernel
191 * struct statfs still has 32-bit versions for most of these fields,
192 * so we convert them here
193 */
194 int err;
195 long long f_blocks;
196 long long f_bfree;
197 long long f_bavail;
198 long long f_files;
199 long long f_ffree;
200
201 err = do_statfs(dentry->d_sb->s_fs_info,
202 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
203 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
204 &sf->f_namelen);
205 if (err)
206 return err;
207 sf->f_blocks = f_blocks;
208 sf->f_bfree = f_bfree;
209 sf->f_bavail = f_bavail;
210 sf->f_files = f_files;
211 sf->f_ffree = f_ffree;
212 sf->f_type = HOSTFS_SUPER_MAGIC;
213 return 0;
214 }
215
hostfs_alloc_inode(struct super_block * sb)216 static struct inode *hostfs_alloc_inode(struct super_block *sb)
217 {
218 struct hostfs_inode_info *hi;
219
220 hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
221 if (hi == NULL)
222 return NULL;
223 hi->fd = -1;
224 hi->mode = 0;
225 hi->dev = 0;
226 inode_init_once(&hi->vfs_inode);
227 mutex_init(&hi->open_mutex);
228 return &hi->vfs_inode;
229 }
230
hostfs_evict_inode(struct inode * inode)231 static void hostfs_evict_inode(struct inode *inode)
232 {
233 truncate_inode_pages_final(&inode->i_data);
234 clear_inode(inode);
235 if (HOSTFS_I(inode)->fd != -1) {
236 close_file(&HOSTFS_I(inode)->fd);
237 HOSTFS_I(inode)->fd = -1;
238 HOSTFS_I(inode)->dev = 0;
239 }
240 }
241
hostfs_free_inode(struct inode * inode)242 static void hostfs_free_inode(struct inode *inode)
243 {
244 kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
245 }
246
hostfs_show_options(struct seq_file * seq,struct dentry * root)247 static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
248 {
249 const char *root_path = root->d_sb->s_fs_info;
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_writepage(struct page * page,struct writeback_control * wbc)399 static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
400 {
401 struct address_space *mapping = page->mapping;
402 struct inode *inode = mapping->host;
403 char *buffer;
404 loff_t base = page_offset(page);
405 int count = PAGE_SIZE;
406 int end_index = inode->i_size >> PAGE_SHIFT;
407 int err;
408
409 if (page->index >= end_index)
410 count = inode->i_size & (PAGE_SIZE-1);
411
412 buffer = kmap_local_page(page);
413
414 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
415 if (err != count) {
416 if (err >= 0)
417 err = -EIO;
418 mapping_set_error(mapping, err);
419 goto out;
420 }
421
422 if (base > inode->i_size)
423 inode->i_size = base;
424
425 err = 0;
426
427 out:
428 kunmap_local(buffer);
429 unlock_page(page);
430
431 return err;
432 }
433
hostfs_read_folio(struct file * file,struct folio * folio)434 static int hostfs_read_folio(struct file *file, struct folio *folio)
435 {
436 struct page *page = &folio->page;
437 char *buffer;
438 loff_t start = page_offset(page);
439 int bytes_read, ret = 0;
440
441 buffer = kmap_local_page(page);
442 bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
443 PAGE_SIZE);
444 if (bytes_read < 0) {
445 ClearPageUptodate(page);
446 SetPageError(page);
447 ret = bytes_read;
448 goto out;
449 }
450
451 memset(buffer + bytes_read, 0, PAGE_SIZE - bytes_read);
452
453 ClearPageError(page);
454 SetPageUptodate(page);
455
456 out:
457 flush_dcache_page(page);
458 kunmap_local(buffer);
459 unlock_page(page);
460
461 return ret;
462 }
463
hostfs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)464 static int hostfs_write_begin(struct file *file, struct address_space *mapping,
465 loff_t pos, unsigned len,
466 struct page **pagep, void **fsdata)
467 {
468 pgoff_t index = pos >> PAGE_SHIFT;
469
470 *pagep = grab_cache_page_write_begin(mapping, index);
471 if (!*pagep)
472 return -ENOMEM;
473 return 0;
474 }
475
hostfs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)476 static int hostfs_write_end(struct file *file, struct address_space *mapping,
477 loff_t pos, unsigned len, unsigned copied,
478 struct page *page, void *fsdata)
479 {
480 struct inode *inode = mapping->host;
481 void *buffer;
482 unsigned from = pos & (PAGE_SIZE - 1);
483 int err;
484
485 buffer = kmap_local_page(page);
486 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
487 kunmap_local(buffer);
488
489 if (!PageUptodate(page) && err == PAGE_SIZE)
490 SetPageUptodate(page);
491
492 /*
493 * If err > 0, write_file has added err to pos, so we are comparing
494 * i_size against the last byte written.
495 */
496 if (err > 0 && (pos > inode->i_size))
497 inode->i_size = pos;
498 unlock_page(page);
499 put_page(page);
500
501 return err;
502 }
503
504 static const struct address_space_operations hostfs_aops = {
505 .writepage = hostfs_writepage,
506 .read_folio = hostfs_read_folio,
507 .dirty_folio = filemap_dirty_folio,
508 .write_begin = hostfs_write_begin,
509 .write_end = hostfs_write_end,
510 };
511
hostfs_inode_update(struct inode * ino,const struct hostfs_stat * st)512 static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st)
513 {
514 set_nlink(ino, st->nlink);
515 i_uid_write(ino, st->uid);
516 i_gid_write(ino, st->gid);
517 ino->i_atime =
518 (struct timespec64){ st->atime.tv_sec, st->atime.tv_nsec };
519 ino->i_mtime =
520 (struct timespec64){ st->mtime.tv_sec, st->mtime.tv_nsec };
521 inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec);
522 ino->i_size = st->size;
523 ino->i_blocks = st->blocks;
524 return 0;
525 }
526
hostfs_inode_set(struct inode * ino,void * data)527 static int hostfs_inode_set(struct inode *ino, void *data)
528 {
529 struct hostfs_stat *st = data;
530 dev_t dev, rdev;
531
532 /* Reencode maj and min with the kernel encoding.*/
533 rdev = MKDEV(st->rdev.maj, st->rdev.min);
534 dev = MKDEV(st->dev.maj, st->dev.min);
535
536 switch (st->mode & S_IFMT) {
537 case S_IFLNK:
538 ino->i_op = &hostfs_link_iops;
539 break;
540 case S_IFDIR:
541 ino->i_op = &hostfs_dir_iops;
542 ino->i_fop = &hostfs_dir_fops;
543 break;
544 case S_IFCHR:
545 case S_IFBLK:
546 case S_IFIFO:
547 case S_IFSOCK:
548 init_special_inode(ino, st->mode & S_IFMT, rdev);
549 ino->i_op = &hostfs_iops;
550 break;
551 case S_IFREG:
552 ino->i_op = &hostfs_iops;
553 ino->i_fop = &hostfs_file_fops;
554 ino->i_mapping->a_ops = &hostfs_aops;
555 break;
556 default:
557 return -EIO;
558 }
559
560 HOSTFS_I(ino)->dev = dev;
561 HOSTFS_I(ino)->btime = st->btime;
562 ino->i_ino = st->ino;
563 ino->i_mode = st->mode;
564 return hostfs_inode_update(ino, st);
565 }
566
hostfs_inode_test(struct inode * inode,void * data)567 static int hostfs_inode_test(struct inode *inode, void *data)
568 {
569 const struct hostfs_stat *st = data;
570 dev_t dev = MKDEV(st->dev.maj, st->dev.min);
571
572 return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == dev &&
573 (inode->i_mode & S_IFMT) == (st->mode & S_IFMT) &&
574 HOSTFS_I(inode)->btime.tv_sec == st->btime.tv_sec &&
575 HOSTFS_I(inode)->btime.tv_nsec == st->btime.tv_nsec;
576 }
577
hostfs_iget(struct super_block * sb,char * name)578 static struct inode *hostfs_iget(struct super_block *sb, char *name)
579 {
580 struct inode *inode;
581 struct hostfs_stat st;
582 int err = stat_file(name, &st, -1);
583
584 if (err)
585 return ERR_PTR(err);
586
587 inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set,
588 &st);
589 if (!inode)
590 return ERR_PTR(-ENOMEM);
591
592 if (inode->i_state & I_NEW) {
593 unlock_new_inode(inode);
594 } else {
595 spin_lock(&inode->i_lock);
596 hostfs_inode_update(inode, &st);
597 spin_unlock(&inode->i_lock);
598 }
599
600 return inode;
601 }
602
hostfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)603 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir,
604 struct dentry *dentry, umode_t mode, bool excl)
605 {
606 struct inode *inode;
607 char *name;
608 int fd;
609
610 name = dentry_name(dentry);
611 if (name == NULL)
612 return -ENOMEM;
613
614 fd = file_create(name, mode & 0777);
615 if (fd < 0) {
616 __putname(name);
617 return fd;
618 }
619
620 inode = hostfs_iget(dir->i_sb, name);
621 __putname(name);
622 if (IS_ERR(inode))
623 return PTR_ERR(inode);
624
625 HOSTFS_I(inode)->fd = fd;
626 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
627 d_instantiate(dentry, inode);
628 return 0;
629 }
630
hostfs_lookup(struct inode * ino,struct dentry * dentry,unsigned int flags)631 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
632 unsigned int flags)
633 {
634 struct inode *inode = NULL;
635 char *name;
636
637 name = dentry_name(dentry);
638 if (name == NULL)
639 return ERR_PTR(-ENOMEM);
640
641 inode = hostfs_iget(ino->i_sb, name);
642 __putname(name);
643 if (IS_ERR(inode)) {
644 if (PTR_ERR(inode) == -ENOENT)
645 inode = NULL;
646 else
647 return ERR_CAST(inode);
648 }
649
650 return d_splice_alias(inode, dentry);
651 }
652
hostfs_link(struct dentry * to,struct inode * ino,struct dentry * from)653 static int hostfs_link(struct dentry *to, struct inode *ino,
654 struct dentry *from)
655 {
656 char *from_name, *to_name;
657 int err;
658
659 if ((from_name = dentry_name(from)) == NULL)
660 return -ENOMEM;
661 to_name = dentry_name(to);
662 if (to_name == NULL) {
663 __putname(from_name);
664 return -ENOMEM;
665 }
666 err = link_file(to_name, from_name);
667 __putname(from_name);
668 __putname(to_name);
669 return err;
670 }
671
hostfs_unlink(struct inode * ino,struct dentry * dentry)672 static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
673 {
674 char *file;
675 int err;
676
677 if (append)
678 return -EPERM;
679
680 if ((file = dentry_name(dentry)) == NULL)
681 return -ENOMEM;
682
683 err = unlink_file(file);
684 __putname(file);
685 return err;
686 }
687
hostfs_symlink(struct mnt_idmap * idmap,struct inode * ino,struct dentry * dentry,const char * to)688 static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino,
689 struct dentry *dentry, const char *to)
690 {
691 char *file;
692 int err;
693
694 if ((file = dentry_name(dentry)) == NULL)
695 return -ENOMEM;
696 err = make_symlink(file, to);
697 __putname(file);
698 return err;
699 }
700
hostfs_mkdir(struct mnt_idmap * idmap,struct inode * ino,struct dentry * dentry,umode_t mode)701 static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino,
702 struct dentry *dentry, umode_t mode)
703 {
704 char *file;
705 int err;
706
707 if ((file = dentry_name(dentry)) == NULL)
708 return -ENOMEM;
709 err = do_mkdir(file, mode);
710 __putname(file);
711 return err;
712 }
713
hostfs_rmdir(struct inode * ino,struct dentry * dentry)714 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
715 {
716 char *file;
717 int err;
718
719 if ((file = dentry_name(dentry)) == NULL)
720 return -ENOMEM;
721 err = hostfs_do_rmdir(file);
722 __putname(file);
723 return err;
724 }
725
hostfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)726 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
727 struct dentry *dentry, umode_t mode, dev_t dev)
728 {
729 struct inode *inode;
730 char *name;
731 int err;
732
733 name = dentry_name(dentry);
734 if (name == NULL)
735 return -ENOMEM;
736
737 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
738 if (err) {
739 __putname(name);
740 return err;
741 }
742
743 inode = hostfs_iget(dir->i_sb, name);
744 __putname(name);
745 if (IS_ERR(inode))
746 return PTR_ERR(inode);
747
748 d_instantiate(dentry, inode);
749 return 0;
750 }
751
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)752 static int hostfs_rename2(struct mnt_idmap *idmap,
753 struct inode *old_dir, struct dentry *old_dentry,
754 struct inode *new_dir, struct dentry *new_dentry,
755 unsigned int flags)
756 {
757 char *old_name, *new_name;
758 int err;
759
760 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
761 return -EINVAL;
762
763 old_name = dentry_name(old_dentry);
764 if (old_name == NULL)
765 return -ENOMEM;
766 new_name = dentry_name(new_dentry);
767 if (new_name == NULL) {
768 __putname(old_name);
769 return -ENOMEM;
770 }
771 if (!flags)
772 err = rename_file(old_name, new_name);
773 else
774 err = rename2_file(old_name, new_name, flags);
775
776 __putname(old_name);
777 __putname(new_name);
778 return err;
779 }
780
hostfs_permission(struct mnt_idmap * idmap,struct inode * ino,int desired)781 static int hostfs_permission(struct mnt_idmap *idmap,
782 struct inode *ino, int desired)
783 {
784 char *name;
785 int r = 0, w = 0, x = 0, err;
786
787 if (desired & MAY_NOT_BLOCK)
788 return -ECHILD;
789
790 if (desired & MAY_READ) r = 1;
791 if (desired & MAY_WRITE) w = 1;
792 if (desired & MAY_EXEC) x = 1;
793 name = inode_name(ino);
794 if (name == NULL)
795 return -ENOMEM;
796
797 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
798 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
799 err = 0;
800 else
801 err = access_file(name, r, w, x);
802 __putname(name);
803 if (!err)
804 err = generic_permission(&nop_mnt_idmap, ino, desired);
805 return err;
806 }
807
hostfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)808 static int hostfs_setattr(struct mnt_idmap *idmap,
809 struct dentry *dentry, struct iattr *attr)
810 {
811 struct inode *inode = d_inode(dentry);
812 struct hostfs_iattr attrs;
813 char *name;
814 int err;
815
816 int fd = HOSTFS_I(inode)->fd;
817
818 err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
819 if (err)
820 return err;
821
822 if (append)
823 attr->ia_valid &= ~ATTR_SIZE;
824
825 attrs.ia_valid = 0;
826 if (attr->ia_valid & ATTR_MODE) {
827 attrs.ia_valid |= HOSTFS_ATTR_MODE;
828 attrs.ia_mode = attr->ia_mode;
829 }
830 if (attr->ia_valid & ATTR_UID) {
831 attrs.ia_valid |= HOSTFS_ATTR_UID;
832 attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
833 }
834 if (attr->ia_valid & ATTR_GID) {
835 attrs.ia_valid |= HOSTFS_ATTR_GID;
836 attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
837 }
838 if (attr->ia_valid & ATTR_SIZE) {
839 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
840 attrs.ia_size = attr->ia_size;
841 }
842 if (attr->ia_valid & ATTR_ATIME) {
843 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
844 attrs.ia_atime = (struct hostfs_timespec)
845 { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec };
846 }
847 if (attr->ia_valid & ATTR_MTIME) {
848 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
849 attrs.ia_mtime = (struct hostfs_timespec)
850 { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec };
851 }
852 if (attr->ia_valid & ATTR_CTIME) {
853 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
854 attrs.ia_ctime = (struct hostfs_timespec)
855 { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec };
856 }
857 if (attr->ia_valid & ATTR_ATIME_SET) {
858 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
859 }
860 if (attr->ia_valid & ATTR_MTIME_SET) {
861 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
862 }
863 name = dentry_name(dentry);
864 if (name == NULL)
865 return -ENOMEM;
866 err = set_attr(name, &attrs, fd);
867 __putname(name);
868 if (err)
869 return err;
870
871 if ((attr->ia_valid & ATTR_SIZE) &&
872 attr->ia_size != i_size_read(inode))
873 truncate_setsize(inode, attr->ia_size);
874
875 setattr_copy(&nop_mnt_idmap, inode, attr);
876 mark_inode_dirty(inode);
877 return 0;
878 }
879
880 static const struct inode_operations hostfs_iops = {
881 .permission = hostfs_permission,
882 .setattr = hostfs_setattr,
883 };
884
885 static const struct inode_operations hostfs_dir_iops = {
886 .create = hostfs_create,
887 .lookup = hostfs_lookup,
888 .link = hostfs_link,
889 .unlink = hostfs_unlink,
890 .symlink = hostfs_symlink,
891 .mkdir = hostfs_mkdir,
892 .rmdir = hostfs_rmdir,
893 .mknod = hostfs_mknod,
894 .rename = hostfs_rename2,
895 .permission = hostfs_permission,
896 .setattr = hostfs_setattr,
897 };
898
hostfs_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)899 static const char *hostfs_get_link(struct dentry *dentry,
900 struct inode *inode,
901 struct delayed_call *done)
902 {
903 char *link;
904 if (!dentry)
905 return ERR_PTR(-ECHILD);
906 link = kmalloc(PATH_MAX, GFP_KERNEL);
907 if (link) {
908 char *path = dentry_name(dentry);
909 int err = -ENOMEM;
910 if (path) {
911 err = hostfs_do_readlink(path, link, PATH_MAX);
912 if (err == PATH_MAX)
913 err = -E2BIG;
914 __putname(path);
915 }
916 if (err < 0) {
917 kfree(link);
918 return ERR_PTR(err);
919 }
920 } else {
921 return ERR_PTR(-ENOMEM);
922 }
923
924 set_delayed_call(done, kfree_link, link);
925 return link;
926 }
927
928 static const struct inode_operations hostfs_link_iops = {
929 .get_link = hostfs_get_link,
930 };
931
hostfs_fill_sb_common(struct super_block * sb,void * d,int silent)932 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
933 {
934 struct inode *root_inode;
935 char *host_root_path, *req_root = d;
936 int err;
937
938 sb->s_blocksize = 1024;
939 sb->s_blocksize_bits = 10;
940 sb->s_magic = HOSTFS_SUPER_MAGIC;
941 sb->s_op = &hostfs_sbops;
942 sb->s_d_op = &simple_dentry_operations;
943 sb->s_maxbytes = MAX_LFS_FILESIZE;
944 err = super_setup_bdi(sb);
945 if (err)
946 return err;
947
948 /* NULL is printed as '(null)' by printf(): avoid that. */
949 if (req_root == NULL)
950 req_root = "";
951
952 sb->s_fs_info = host_root_path =
953 kasprintf(GFP_KERNEL, "%s/%s", root_ino, req_root);
954 if (host_root_path == NULL)
955 return -ENOMEM;
956
957 root_inode = hostfs_iget(sb, host_root_path);
958 if (IS_ERR(root_inode))
959 return PTR_ERR(root_inode);
960
961 if (S_ISLNK(root_inode->i_mode)) {
962 char *name;
963
964 iput(root_inode);
965 name = follow_link(host_root_path);
966 if (IS_ERR(name))
967 return PTR_ERR(name);
968
969 root_inode = hostfs_iget(sb, name);
970 kfree(name);
971 if (IS_ERR(root_inode))
972 return PTR_ERR(root_inode);
973 }
974
975 sb->s_root = d_make_root(root_inode);
976 if (sb->s_root == NULL)
977 return -ENOMEM;
978
979 return 0;
980 }
981
hostfs_read_sb(struct file_system_type * type,int flags,const char * dev_name,void * data)982 static struct dentry *hostfs_read_sb(struct file_system_type *type,
983 int flags, const char *dev_name,
984 void *data)
985 {
986 return mount_nodev(type, flags, data, hostfs_fill_sb_common);
987 }
988
hostfs_kill_sb(struct super_block * s)989 static void hostfs_kill_sb(struct super_block *s)
990 {
991 kill_anon_super(s);
992 kfree(s->s_fs_info);
993 }
994
995 static struct file_system_type hostfs_type = {
996 .owner = THIS_MODULE,
997 .name = "hostfs",
998 .mount = hostfs_read_sb,
999 .kill_sb = hostfs_kill_sb,
1000 .fs_flags = 0,
1001 };
1002 MODULE_ALIAS_FS("hostfs");
1003
init_hostfs(void)1004 static int __init init_hostfs(void)
1005 {
1006 hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
1007 if (!hostfs_inode_cache)
1008 return -ENOMEM;
1009 return register_filesystem(&hostfs_type);
1010 }
1011
exit_hostfs(void)1012 static void __exit exit_hostfs(void)
1013 {
1014 unregister_filesystem(&hostfs_type);
1015 kmem_cache_destroy(hostfs_inode_cache);
1016 }
1017
1018 module_init(init_hostfs)
1019 module_exit(exit_hostfs)
1020 MODULE_LICENSE("GPL");
1021