]> Git Repo - J-linux.git/blob - fs/hostfs/hostfs_kern.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / fs / hostfs / hostfs_kern.c
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 };
37
38 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
39 {
40         return list_entry(inode, struct hostfs_inode_info, vfs_inode);
41 }
42
43 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
44
45 static struct kmem_cache *hostfs_inode_cache;
46
47 /* Changed in hostfs_args before the kernel starts running */
48 static char *root_ino = "";
49 static int append = 0;
50
51 static const struct inode_operations hostfs_iops;
52 static const struct inode_operations hostfs_dir_iops;
53 static const struct inode_operations hostfs_link_iops;
54
55 #ifndef MODULE
56 static int __init hostfs_args(char *options, int *add)
57 {
58         char *ptr;
59
60         *add = 0;
61         ptr = strchr(options, ',');
62         if (ptr != NULL)
63                 *ptr++ = '\0';
64         if (*options != '\0')
65                 root_ino = options;
66
67         options = ptr;
68         while (options) {
69                 ptr = strchr(options, ',');
70                 if (ptr != NULL)
71                         *ptr++ = '\0';
72                 if (*options != '\0') {
73                         if (!strcmp(options, "append"))
74                                 append = 1;
75                         else printf("hostfs_args - unsupported option - %s\n",
76                                     options);
77                 }
78                 options = ptr;
79         }
80         return 0;
81 }
82
83 __uml_setup("hostfs=", hostfs_args,
84 "hostfs=<root dir>,<flags>,...\n"
85 "    This is used to set hostfs parameters.  The root directory argument\n"
86 "    is used to confine all hostfs mounts to within the specified directory\n"
87 "    tree on the host.  If this isn't specified, then a user inside UML can\n"
88 "    mount anything on the host that's accessible to the user that's running\n"
89 "    it.\n"
90 "    The only flag currently supported is 'append', which specifies that all\n"
91 "    files opened by hostfs will be opened in append mode.\n\n"
92 );
93 #endif
94
95 static char *__dentry_name(struct dentry *dentry, char *name)
96 {
97         char *p = dentry_path_raw(dentry, name, PATH_MAX);
98         char *root;
99         size_t len;
100         struct hostfs_fs_info *fsi;
101
102         fsi = dentry->d_sb->s_fs_info;
103         root = fsi->host_root_path;
104         len = strlen(root);
105         if (IS_ERR(p)) {
106                 __putname(name);
107                 return NULL;
108         }
109
110         /*
111          * This function relies on the fact that dentry_path_raw() will place
112          * the path name at the end of the provided buffer.
113          */
114         BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);
115
116         strscpy(name, root, PATH_MAX);
117         if (len > p - name) {
118                 __putname(name);
119                 return NULL;
120         }
121
122         if (p > name + len)
123                 strcpy(name + len, p);
124
125         return name;
126 }
127
128 static char *dentry_name(struct dentry *dentry)
129 {
130         char *name = __getname();
131         if (!name)
132                 return NULL;
133
134         return __dentry_name(dentry, name);
135 }
136
137 static char *inode_name(struct inode *ino)
138 {
139         struct dentry *dentry;
140         char *name;
141
142         dentry = d_find_alias(ino);
143         if (!dentry)
144                 return NULL;
145
146         name = dentry_name(dentry);
147
148         dput(dentry);
149
150         return name;
151 }
152
153 static char *follow_link(char *link)
154 {
155         char *name, *resolved, *end;
156         int n;
157
158         name = kmalloc(PATH_MAX, GFP_KERNEL);
159         if (!name) {
160                 n = -ENOMEM;
161                 goto out_free;
162         }
163
164         n = hostfs_do_readlink(link, name, PATH_MAX);
165         if (n < 0)
166                 goto out_free;
167         else if (n == PATH_MAX) {
168                 n = -E2BIG;
169                 goto out_free;
170         }
171
172         if (*name == '/')
173                 return name;
174
175         end = strrchr(link, '/');
176         if (end == NULL)
177                 return name;
178
179         *(end + 1) = '\0';
180
181         resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
182         if (resolved == NULL) {
183                 n = -ENOMEM;
184                 goto out_free;
185         }
186
187         kfree(name);
188         return resolved;
189
190  out_free:
191         kfree(name);
192         return ERR_PTR(n);
193 }
194
195 static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
196 {
197         /*
198          * do_statfs uses struct statfs64 internally, but the linux kernel
199          * struct statfs still has 32-bit versions for most of these fields,
200          * so we convert them here
201          */
202         int err;
203         long long f_blocks;
204         long long f_bfree;
205         long long f_bavail;
206         long long f_files;
207         long long f_ffree;
208         struct hostfs_fs_info *fsi;
209
210         fsi = dentry->d_sb->s_fs_info;
211         err = do_statfs(fsi->host_root_path,
212                         &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
213                         &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
214                         &sf->f_namelen);
215         if (err)
216                 return err;
217         sf->f_blocks = f_blocks;
218         sf->f_bfree = f_bfree;
219         sf->f_bavail = f_bavail;
220         sf->f_files = f_files;
221         sf->f_ffree = f_ffree;
222         sf->f_type = HOSTFS_SUPER_MAGIC;
223         return 0;
224 }
225
226 static struct inode *hostfs_alloc_inode(struct super_block *sb)
227 {
228         struct hostfs_inode_info *hi;
229
230         hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
231         if (hi == NULL)
232                 return NULL;
233         hi->fd = -1;
234         hi->mode = 0;
235         hi->dev = 0;
236         inode_init_once(&hi->vfs_inode);
237         mutex_init(&hi->open_mutex);
238         return &hi->vfs_inode;
239 }
240
241 static void hostfs_evict_inode(struct inode *inode)
242 {
243         truncate_inode_pages_final(&inode->i_data);
244         clear_inode(inode);
245         if (HOSTFS_I(inode)->fd != -1) {
246                 close_file(&HOSTFS_I(inode)->fd);
247                 HOSTFS_I(inode)->fd = -1;
248                 HOSTFS_I(inode)->dev = 0;
249         }
250 }
251
252 static void hostfs_free_inode(struct inode *inode)
253 {
254         kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
255 }
256
257 static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
258 {
259         struct hostfs_fs_info *fsi;
260         const char *root_path;
261
262         fsi = root->d_sb->s_fs_info;
263         root_path = fsi->host_root_path;
264         size_t offset = strlen(root_ino) + 1;
265
266         if (strlen(root_path) > offset)
267                 seq_show_option(seq, root_path + offset, NULL);
268
269         if (append)
270                 seq_puts(seq, ",append");
271
272         return 0;
273 }
274
275 static const struct super_operations hostfs_sbops = {
276         .alloc_inode    = hostfs_alloc_inode,
277         .free_inode     = hostfs_free_inode,
278         .drop_inode     = generic_delete_inode,
279         .evict_inode    = hostfs_evict_inode,
280         .statfs         = hostfs_statfs,
281         .show_options   = hostfs_show_options,
282 };
283
284 static int hostfs_readdir(struct file *file, struct dir_context *ctx)
285 {
286         void *dir;
287         char *name;
288         unsigned long long next, ino;
289         int error, len;
290         unsigned int type;
291
292         name = dentry_name(file->f_path.dentry);
293         if (name == NULL)
294                 return -ENOMEM;
295         dir = open_dir(name, &error);
296         __putname(name);
297         if (dir == NULL)
298                 return -error;
299         next = ctx->pos;
300         seek_dir(dir, next);
301         while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
302                 if (!dir_emit(ctx, name, len, ino, type))
303                         break;
304                 ctx->pos = next;
305         }
306         close_dir(dir);
307         return 0;
308 }
309
310 static int hostfs_open(struct inode *ino, struct file *file)
311 {
312         char *name;
313         fmode_t mode;
314         int err;
315         int r, w, fd;
316
317         mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
318         if ((mode & HOSTFS_I(ino)->mode) == mode)
319                 return 0;
320
321         mode |= HOSTFS_I(ino)->mode;
322
323 retry:
324         r = w = 0;
325
326         if (mode & FMODE_READ)
327                 r = 1;
328         if (mode & FMODE_WRITE)
329                 r = w = 1;
330
331         name = dentry_name(file_dentry(file));
332         if (name == NULL)
333                 return -ENOMEM;
334
335         fd = open_file(name, r, w, append);
336         __putname(name);
337         if (fd < 0)
338                 return fd;
339
340         mutex_lock(&HOSTFS_I(ino)->open_mutex);
341         /* somebody else had handled it first? */
342         if ((mode & HOSTFS_I(ino)->mode) == mode) {
343                 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
344                 close_file(&fd);
345                 return 0;
346         }
347         if ((mode | HOSTFS_I(ino)->mode) != mode) {
348                 mode |= HOSTFS_I(ino)->mode;
349                 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
350                 close_file(&fd);
351                 goto retry;
352         }
353         if (HOSTFS_I(ino)->fd == -1) {
354                 HOSTFS_I(ino)->fd = fd;
355         } else {
356                 err = replace_file(fd, HOSTFS_I(ino)->fd);
357                 close_file(&fd);
358                 if (err < 0) {
359                         mutex_unlock(&HOSTFS_I(ino)->open_mutex);
360                         return err;
361                 }
362         }
363         HOSTFS_I(ino)->mode = mode;
364         mutex_unlock(&HOSTFS_I(ino)->open_mutex);
365
366         return 0;
367 }
368
369 static int hostfs_file_release(struct inode *inode, struct file *file)
370 {
371         filemap_write_and_wait(inode->i_mapping);
372
373         return 0;
374 }
375
376 static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
377                         int datasync)
378 {
379         struct inode *inode = file->f_mapping->host;
380         int ret;
381
382         ret = file_write_and_wait_range(file, start, end);
383         if (ret)
384                 return ret;
385
386         inode_lock(inode);
387         ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
388         inode_unlock(inode);
389
390         return ret;
391 }
392
393 static const struct file_operations hostfs_file_fops = {
394         .llseek         = generic_file_llseek,
395         .splice_read    = filemap_splice_read,
396         .splice_write   = iter_file_splice_write,
397         .read_iter      = generic_file_read_iter,
398         .write_iter     = generic_file_write_iter,
399         .mmap           = generic_file_mmap,
400         .open           = hostfs_open,
401         .release        = hostfs_file_release,
402         .fsync          = hostfs_fsync,
403 };
404
405 static const struct file_operations hostfs_dir_fops = {
406         .llseek         = generic_file_llseek,
407         .iterate_shared = hostfs_readdir,
408         .read           = generic_read_dir,
409         .open           = hostfs_open,
410         .fsync          = hostfs_fsync,
411 };
412
413 static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
414 {
415         struct address_space *mapping = page->mapping;
416         struct inode *inode = mapping->host;
417         char *buffer;
418         loff_t base = page_offset(page);
419         int count = PAGE_SIZE;
420         int end_index = inode->i_size >> PAGE_SHIFT;
421         int err;
422
423         if (page->index >= end_index)
424                 count = inode->i_size & (PAGE_SIZE-1);
425
426         buffer = kmap_local_page(page);
427
428         err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
429         if (err != count) {
430                 if (err >= 0)
431                         err = -EIO;
432                 mapping_set_error(mapping, err);
433                 goto out;
434         }
435
436         if (base > inode->i_size)
437                 inode->i_size = base;
438
439         err = 0;
440
441  out:
442         kunmap_local(buffer);
443         unlock_page(page);
444
445         return err;
446 }
447
448 static int hostfs_read_folio(struct file *file, struct folio *folio)
449 {
450         char *buffer;
451         loff_t start = folio_pos(folio);
452         int bytes_read, ret = 0;
453
454         buffer = kmap_local_folio(folio, 0);
455         bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
456                         PAGE_SIZE);
457         if (bytes_read < 0)
458                 ret = bytes_read;
459         else
460                 buffer = folio_zero_tail(folio, bytes_read, buffer + bytes_read);
461         kunmap_local(buffer);
462
463         folio_end_read(folio, ret == 0);
464         return ret;
465 }
466
467 static int hostfs_write_begin(struct file *file, struct address_space *mapping,
468                               loff_t pos, unsigned len,
469                               struct folio **foliop, void **fsdata)
470 {
471         pgoff_t index = pos >> PAGE_SHIFT;
472
473         *foliop = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
474                         mapping_gfp_mask(mapping));
475         if (IS_ERR(*foliop))
476                 return PTR_ERR(*foliop);
477         return 0;
478 }
479
480 static int hostfs_write_end(struct file *file, struct address_space *mapping,
481                             loff_t pos, unsigned len, unsigned copied,
482                             struct folio *folio, void *fsdata)
483 {
484         struct inode *inode = mapping->host;
485         void *buffer;
486         size_t from = offset_in_folio(folio, pos);
487         int err;
488
489         buffer = kmap_local_folio(folio, from);
490         err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer, copied);
491         kunmap_local(buffer);
492
493         if (!folio_test_uptodate(folio) && err == folio_size(folio))
494                 folio_mark_uptodate(folio);
495
496         /*
497          * If err > 0, write_file has added err to pos, so we are comparing
498          * i_size against the last byte written.
499          */
500         if (err > 0 && (pos > inode->i_size))
501                 inode->i_size = pos;
502         folio_unlock(folio);
503         folio_put(folio);
504
505         return err;
506 }
507
508 static const struct address_space_operations hostfs_aops = {
509         .writepage      = hostfs_writepage,
510         .read_folio     = hostfs_read_folio,
511         .dirty_folio    = filemap_dirty_folio,
512         .write_begin    = hostfs_write_begin,
513         .write_end      = hostfs_write_end,
514 };
515
516 static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st)
517 {
518         set_nlink(ino, st->nlink);
519         i_uid_write(ino, st->uid);
520         i_gid_write(ino, st->gid);
521         inode_set_atime_to_ts(ino, (struct timespec64){
522                         st->atime.tv_sec,
523                         st->atime.tv_nsec,
524                 });
525         inode_set_mtime_to_ts(ino, (struct timespec64){
526                         st->mtime.tv_sec,
527                         st->mtime.tv_nsec,
528                 });
529         inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec);
530         ino->i_size = st->size;
531         ino->i_blocks = st->blocks;
532         return 0;
533 }
534
535 static int hostfs_inode_set(struct inode *ino, void *data)
536 {
537         struct hostfs_stat *st = data;
538         dev_t dev, rdev;
539
540         /* Reencode maj and min with the kernel encoding.*/
541         rdev = MKDEV(st->rdev.maj, st->rdev.min);
542         dev = MKDEV(st->dev.maj, st->dev.min);
543
544         switch (st->mode & S_IFMT) {
545         case S_IFLNK:
546                 ino->i_op = &hostfs_link_iops;
547                 break;
548         case S_IFDIR:
549                 ino->i_op = &hostfs_dir_iops;
550                 ino->i_fop = &hostfs_dir_fops;
551                 break;
552         case S_IFCHR:
553         case S_IFBLK:
554         case S_IFIFO:
555         case S_IFSOCK:
556                 init_special_inode(ino, st->mode & S_IFMT, rdev);
557                 ino->i_op = &hostfs_iops;
558                 break;
559         case S_IFREG:
560                 ino->i_op = &hostfs_iops;
561                 ino->i_fop = &hostfs_file_fops;
562                 ino->i_mapping->a_ops = &hostfs_aops;
563                 break;
564         default:
565                 return -EIO;
566         }
567
568         HOSTFS_I(ino)->dev = dev;
569         ino->i_ino = st->ino;
570         ino->i_mode = st->mode;
571         return hostfs_inode_update(ino, st);
572 }
573
574 static int hostfs_inode_test(struct inode *inode, void *data)
575 {
576         const struct hostfs_stat *st = data;
577         dev_t dev = MKDEV(st->dev.maj, st->dev.min);
578
579         return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == dev;
580 }
581
582 static struct inode *hostfs_iget(struct super_block *sb, char *name)
583 {
584         struct inode *inode;
585         struct hostfs_stat st;
586         int err = stat_file(name, &st, -1);
587
588         if (err)
589                 return ERR_PTR(err);
590
591         inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set,
592                              &st);
593         if (!inode)
594                 return ERR_PTR(-ENOMEM);
595
596         if (inode->i_state & I_NEW) {
597                 unlock_new_inode(inode);
598         } else {
599                 spin_lock(&inode->i_lock);
600                 hostfs_inode_update(inode, &st);
601                 spin_unlock(&inode->i_lock);
602         }
603
604         return inode;
605 }
606
607 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir,
608                          struct dentry *dentry, umode_t mode, bool excl)
609 {
610         struct inode *inode;
611         char *name;
612         int fd;
613
614         name = dentry_name(dentry);
615         if (name == NULL)
616                 return -ENOMEM;
617
618         fd = file_create(name, mode & 0777);
619         if (fd < 0) {
620                 __putname(name);
621                 return fd;
622         }
623
624         inode = hostfs_iget(dir->i_sb, name);
625         __putname(name);
626         if (IS_ERR(inode))
627                 return PTR_ERR(inode);
628
629         HOSTFS_I(inode)->fd = fd;
630         HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
631         d_instantiate(dentry, inode);
632         return 0;
633 }
634
635 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
636                                     unsigned int flags)
637 {
638         struct inode *inode = NULL;
639         char *name;
640
641         name = dentry_name(dentry);
642         if (name == NULL)
643                 return ERR_PTR(-ENOMEM);
644
645         inode = hostfs_iget(ino->i_sb, name);
646         __putname(name);
647         if (inode == ERR_PTR(-ENOENT))
648                 inode = NULL;
649
650         return d_splice_alias(inode, dentry);
651 }
652
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
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
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
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
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
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
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
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
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
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
932 static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc)
933 {
934         struct hostfs_fs_info *fsi = sb->s_fs_info;
935         struct inode *root_inode;
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         root_inode = hostfs_iget(sb, fsi->host_root_path);
949         if (IS_ERR(root_inode))
950                 return PTR_ERR(root_inode);
951
952         if (S_ISLNK(root_inode->i_mode)) {
953                 char *name;
954
955                 iput(root_inode);
956                 name = follow_link(fsi->host_root_path);
957                 if (IS_ERR(name))
958                         return PTR_ERR(name);
959
960                 root_inode = hostfs_iget(sb, name);
961                 kfree(name);
962                 if (IS_ERR(root_inode))
963                         return PTR_ERR(root_inode);
964         }
965
966         sb->s_root = d_make_root(root_inode);
967         if (sb->s_root == NULL)
968                 return -ENOMEM;
969
970         return 0;
971 }
972
973 enum hostfs_parma {
974         Opt_hostfs,
975 };
976
977 static const struct fs_parameter_spec hostfs_param_specs[] = {
978         fsparam_string_empty("hostfs",          Opt_hostfs),
979         {}
980 };
981
982 static int hostfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
983 {
984         struct hostfs_fs_info *fsi = fc->s_fs_info;
985         struct fs_parse_result result;
986         char *host_root;
987         int opt;
988
989         opt = fs_parse(fc, hostfs_param_specs, param, &result);
990         if (opt < 0)
991                 return opt;
992
993         switch (opt) {
994         case Opt_hostfs:
995                 host_root = param->string;
996                 if (!*host_root)
997                         host_root = "";
998                 fsi->host_root_path =
999                         kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root);
1000                 if (fsi->host_root_path == NULL)
1001                         return -ENOMEM;
1002                 break;
1003         }
1004
1005         return 0;
1006 }
1007
1008 static int hostfs_parse_monolithic(struct fs_context *fc, void *data)
1009 {
1010         struct hostfs_fs_info *fsi = fc->s_fs_info;
1011         char *host_root = (char *)data;
1012
1013         /* NULL is printed as '(null)' by printf(): avoid that. */
1014         if (host_root == NULL)
1015                 host_root = "";
1016
1017         fsi->host_root_path =
1018                 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root);
1019         if (fsi->host_root_path == NULL)
1020                 return -ENOMEM;
1021
1022         return 0;
1023 }
1024
1025 static int hostfs_fc_get_tree(struct fs_context *fc)
1026 {
1027         return get_tree_nodev(fc, hostfs_fill_super);
1028 }
1029
1030 static void hostfs_fc_free(struct fs_context *fc)
1031 {
1032         struct hostfs_fs_info *fsi = fc->s_fs_info;
1033
1034         if (!fsi)
1035                 return;
1036
1037         kfree(fsi->host_root_path);
1038         kfree(fsi);
1039 }
1040
1041 static const struct fs_context_operations hostfs_context_ops = {
1042         .parse_monolithic = hostfs_parse_monolithic,
1043         .parse_param    = hostfs_parse_param,
1044         .get_tree       = hostfs_fc_get_tree,
1045         .free           = hostfs_fc_free,
1046 };
1047
1048 static int hostfs_init_fs_context(struct fs_context *fc)
1049 {
1050         struct hostfs_fs_info *fsi;
1051
1052         fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
1053         if (!fsi)
1054                 return -ENOMEM;
1055
1056         fc->s_fs_info = fsi;
1057         fc->ops = &hostfs_context_ops;
1058         return 0;
1059 }
1060
1061 static void hostfs_kill_sb(struct super_block *s)
1062 {
1063         kill_anon_super(s);
1064         kfree(s->s_fs_info);
1065 }
1066
1067 static struct file_system_type hostfs_type = {
1068         .owner                  = THIS_MODULE,
1069         .name                   = "hostfs",
1070         .init_fs_context        = hostfs_init_fs_context,
1071         .kill_sb                = hostfs_kill_sb,
1072         .fs_flags               = 0,
1073 };
1074 MODULE_ALIAS_FS("hostfs");
1075
1076 static int __init init_hostfs(void)
1077 {
1078         hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
1079         if (!hostfs_inode_cache)
1080                 return -ENOMEM;
1081         return register_filesystem(&hostfs_type);
1082 }
1083
1084 static void __exit exit_hostfs(void)
1085 {
1086         unregister_filesystem(&hostfs_type);
1087         kmem_cache_destroy(hostfs_inode_cache);
1088 }
1089
1090 module_init(init_hostfs)
1091 module_exit(exit_hostfs)
1092 MODULE_DESCRIPTION("User-Mode Linux Host filesystem");
1093 MODULE_LICENSE("GPL");
This page took 0.090889 seconds and 4 git commands to generate.