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