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