switch stat_file() to passing a single struct rather than fsckloads of pointers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / hostfs / hostfs_kern.c
blob420a826ae0f2859809f7d1506fdb9fbdb9e6f18f
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 int fd;
23 fmode_t mode;
24 struct inode vfs_inode;
27 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
29 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
32 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
34 static int hostfs_d_delete(struct dentry *dentry)
36 return 1;
39 static const struct dentry_operations hostfs_dentry_ops = {
40 .d_delete = hostfs_d_delete,
43 /* Changed in hostfs_args before the kernel starts running */
44 static char *root_ino = "";
45 static int append = 0;
47 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
49 static const struct inode_operations hostfs_iops;
50 static const struct inode_operations hostfs_dir_iops;
51 static const struct address_space_operations hostfs_link_aops;
53 #ifndef MODULE
54 static int __init hostfs_args(char *options, int *add)
56 char *ptr;
58 ptr = strchr(options, ',');
59 if (ptr != NULL)
60 *ptr++ = '\0';
61 if (*options != '\0')
62 root_ino = options;
64 options = ptr;
65 while (options) {
66 ptr = strchr(options, ',');
67 if (ptr != NULL)
68 *ptr++ = '\0';
69 if (*options != '\0') {
70 if (!strcmp(options, "append"))
71 append = 1;
72 else printf("hostfs_args - unsupported option - %s\n",
73 options);
75 options = ptr;
77 return 0;
80 __uml_setup("hostfs=", hostfs_args,
81 "hostfs=<root dir>,<flags>,...\n"
82 " This is used to set hostfs parameters. The root directory argument\n"
83 " is used to confine all hostfs mounts to within the specified directory\n"
84 " tree on the host. If this isn't specified, then a user inside UML can\n"
85 " mount anything on the host that's accessible to the user that's running\n"
86 " it.\n"
87 " The only flag currently supported is 'append', which specifies that all\n"
88 " files opened by hostfs will be opened in append mode.\n\n"
90 #endif
92 static char *dentry_name(struct dentry *dentry, int extra)
94 struct dentry *parent;
95 char *root, *name;
96 int len;
98 len = 0;
99 parent = dentry;
100 while (parent->d_parent != parent) {
101 len += parent->d_name.len + 1;
102 parent = parent->d_parent;
105 root = parent->d_sb->s_fs_info;
106 len += strlen(root);
107 name = kmalloc(len + extra + 1, GFP_KERNEL);
108 if (name == NULL)
109 return NULL;
111 name[len] = '\0';
112 parent = dentry;
113 while (parent->d_parent != parent) {
114 len -= parent->d_name.len + 1;
115 name[len] = '/';
116 strncpy(&name[len + 1], parent->d_name.name,
117 parent->d_name.len);
118 parent = parent->d_parent;
120 strncpy(name, root, strlen(root));
121 return name;
124 static char *inode_name(struct inode *ino, int extra)
126 struct dentry *dentry;
128 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
129 return dentry_name(dentry, extra);
132 static int read_name(struct inode *ino, char *name)
134 struct hostfs_stat st;
135 int err = stat_file(name, &st, -1);
136 if (err)
137 return err;
139 ino->i_ino = st.ino;
140 ino->i_mode = st.mode;
141 ino->i_nlink = st.nlink;
142 ino->i_uid = st.uid;
143 ino->i_gid = st.gid;
144 ino->i_atime = st.atime;
145 ino->i_mtime = st.mtime;
146 ino->i_ctime = st.ctime;
147 ino->i_size = st.size;
148 ino->i_blocks = st.blocks;
149 return 0;
152 static char *follow_link(char *link)
154 int len, n;
155 char *name, *resolved, *end;
157 len = 64;
158 while (1) {
159 n = -ENOMEM;
160 name = kmalloc(len, GFP_KERNEL);
161 if (name == NULL)
162 goto out;
164 n = hostfs_do_readlink(link, name, len);
165 if (n < len)
166 break;
167 len *= 2;
168 kfree(name);
170 if (n < 0)
171 goto out_free;
173 if (*name == '/')
174 return name;
176 end = strrchr(link, '/');
177 if (end == NULL)
178 return name;
180 *(end + 1) = '\0';
181 len = strlen(link) + strlen(name) + 1;
183 resolved = kmalloc(len, GFP_KERNEL);
184 if (resolved == NULL) {
185 n = -ENOMEM;
186 goto out_free;
189 sprintf(resolved, "%s%s", link, name);
190 kfree(name);
191 kfree(link);
192 return resolved;
194 out_free:
195 kfree(name);
196 out:
197 return ERR_PTR(n);
200 static struct inode *hostfs_iget(struct super_block *sb)
202 struct inode *inode = new_inode(sb);
203 if (!inode)
204 return ERR_PTR(-ENOMEM);
205 return inode;
208 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
211 * do_statfs uses struct statfs64 internally, but the linux kernel
212 * struct statfs still has 32-bit versions for most of these fields,
213 * so we convert them here
215 int err;
216 long long f_blocks;
217 long long f_bfree;
218 long long f_bavail;
219 long long f_files;
220 long long f_ffree;
222 err = do_statfs(dentry->d_sb->s_fs_info,
223 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
224 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
225 &sf->f_namelen, sf->f_spare);
226 if (err)
227 return err;
228 sf->f_blocks = f_blocks;
229 sf->f_bfree = f_bfree;
230 sf->f_bavail = f_bavail;
231 sf->f_files = f_files;
232 sf->f_ffree = f_ffree;
233 sf->f_type = HOSTFS_SUPER_MAGIC;
234 return 0;
237 static struct inode *hostfs_alloc_inode(struct super_block *sb)
239 struct hostfs_inode_info *hi;
241 hi = kzalloc(sizeof(*hi), GFP_KERNEL);
242 if (hi == NULL)
243 return NULL;
244 hi->fd = -1;
245 inode_init_once(&hi->vfs_inode);
246 return &hi->vfs_inode;
249 static void hostfs_evict_inode(struct inode *inode)
251 truncate_inode_pages(&inode->i_data, 0);
252 end_writeback(inode);
253 if (HOSTFS_I(inode)->fd != -1) {
254 close_file(&HOSTFS_I(inode)->fd);
255 HOSTFS_I(inode)->fd = -1;
259 static void hostfs_destroy_inode(struct inode *inode)
261 kfree(HOSTFS_I(inode));
264 static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
266 const char *root_path = vfs->mnt_sb->s_fs_info;
267 size_t offset = strlen(root_ino) + 1;
269 if (strlen(root_path) > offset)
270 seq_printf(seq, ",%s", root_path + offset);
272 return 0;
275 static const struct super_operations hostfs_sbops = {
276 .alloc_inode = hostfs_alloc_inode,
277 .destroy_inode = hostfs_destroy_inode,
278 .evict_inode = hostfs_evict_inode,
279 .statfs = hostfs_statfs,
280 .show_options = hostfs_show_options,
283 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
285 void *dir;
286 char *name;
287 unsigned long long next, ino;
288 int error, len;
290 name = dentry_name(file->f_path.dentry, 0);
291 if (name == NULL)
292 return -ENOMEM;
293 dir = open_dir(name, &error);
294 kfree(name);
295 if (dir == NULL)
296 return -error;
297 next = file->f_pos;
298 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
299 error = (*filldir)(ent, name, len, file->f_pos,
300 ino, DT_UNKNOWN);
301 if (error) break;
302 file->f_pos = next;
304 close_dir(dir);
305 return 0;
308 int hostfs_file_open(struct inode *ino, struct file *file)
310 char *name;
311 fmode_t mode = 0;
312 int r = 0, w = 0, fd;
314 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
315 if ((mode & HOSTFS_I(ino)->mode) == mode)
316 return 0;
319 * The file may already have been opened, but with the wrong access,
320 * so this resets things and reopens the file with the new access.
322 if (HOSTFS_I(ino)->fd != -1) {
323 close_file(&HOSTFS_I(ino)->fd);
324 HOSTFS_I(ino)->fd = -1;
327 HOSTFS_I(ino)->mode |= mode;
328 if (HOSTFS_I(ino)->mode & FMODE_READ)
329 r = 1;
330 if (HOSTFS_I(ino)->mode & FMODE_WRITE)
331 w = 1;
332 if (w)
333 r = 1;
335 name = dentry_name(file->f_path.dentry, 0);
336 if (name == NULL)
337 return -ENOMEM;
339 fd = open_file(name, r, w, append);
340 kfree(name);
341 if (fd < 0)
342 return fd;
343 FILE_HOSTFS_I(file)->fd = fd;
345 return 0;
348 int hostfs_fsync(struct file *file, int datasync)
350 return fsync_file(HOSTFS_I(file->f_mapping->host)->fd, datasync);
353 static const struct file_operations hostfs_file_fops = {
354 .llseek = generic_file_llseek,
355 .read = do_sync_read,
356 .splice_read = generic_file_splice_read,
357 .aio_read = generic_file_aio_read,
358 .aio_write = generic_file_aio_write,
359 .write = do_sync_write,
360 .mmap = generic_file_mmap,
361 .open = hostfs_file_open,
362 .release = NULL,
363 .fsync = hostfs_fsync,
366 static const struct file_operations hostfs_dir_fops = {
367 .llseek = generic_file_llseek,
368 .readdir = hostfs_readdir,
369 .read = generic_read_dir,
372 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
374 struct address_space *mapping = page->mapping;
375 struct inode *inode = mapping->host;
376 char *buffer;
377 unsigned long long base;
378 int count = PAGE_CACHE_SIZE;
379 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
380 int err;
382 if (page->index >= end_index)
383 count = inode->i_size & (PAGE_CACHE_SIZE-1);
385 buffer = kmap(page);
386 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
388 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
389 if (err != count) {
390 ClearPageUptodate(page);
391 goto out;
394 if (base > inode->i_size)
395 inode->i_size = base;
397 if (PageError(page))
398 ClearPageError(page);
399 err = 0;
401 out:
402 kunmap(page);
404 unlock_page(page);
405 return err;
408 int hostfs_readpage(struct file *file, struct page *page)
410 char *buffer;
411 long long start;
412 int err = 0;
414 start = (long long) page->index << PAGE_CACHE_SHIFT;
415 buffer = kmap(page);
416 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
417 PAGE_CACHE_SIZE);
418 if (err < 0)
419 goto out;
421 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
423 flush_dcache_page(page);
424 SetPageUptodate(page);
425 if (PageError(page)) ClearPageError(page);
426 err = 0;
427 out:
428 kunmap(page);
429 unlock_page(page);
430 return err;
433 int hostfs_write_begin(struct file *file, struct address_space *mapping,
434 loff_t pos, unsigned len, unsigned flags,
435 struct page **pagep, void **fsdata)
437 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
439 *pagep = grab_cache_page_write_begin(mapping, index, flags);
440 if (!*pagep)
441 return -ENOMEM;
442 return 0;
445 int hostfs_write_end(struct file *file, struct address_space *mapping,
446 loff_t pos, unsigned len, unsigned copied,
447 struct page *page, void *fsdata)
449 struct inode *inode = mapping->host;
450 void *buffer;
451 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
452 int err;
454 buffer = kmap(page);
455 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
456 kunmap(page);
458 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
459 SetPageUptodate(page);
462 * If err > 0, write_file has added err to pos, so we are comparing
463 * i_size against the last byte written.
465 if (err > 0 && (pos > inode->i_size))
466 inode->i_size = pos;
467 unlock_page(page);
468 page_cache_release(page);
470 return err;
473 static const struct address_space_operations hostfs_aops = {
474 .writepage = hostfs_writepage,
475 .readpage = hostfs_readpage,
476 .set_page_dirty = __set_page_dirty_nobuffers,
477 .write_begin = hostfs_write_begin,
478 .write_end = hostfs_write_end,
481 static void init_inode(struct inode *inode, char *path)
483 int type;
484 int maj, min;
485 dev_t rdev = 0;
487 type = file_type(path, &maj, &min);
488 /* Reencode maj and min with the kernel encoding.*/
489 rdev = MKDEV(maj, min);
491 if (type == OS_TYPE_SYMLINK)
492 inode->i_op = &page_symlink_inode_operations;
493 else if (type == OS_TYPE_DIR)
494 inode->i_op = &hostfs_dir_iops;
495 else inode->i_op = &hostfs_iops;
497 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
498 else inode->i_fop = &hostfs_file_fops;
500 if (type == OS_TYPE_SYMLINK)
501 inode->i_mapping->a_ops = &hostfs_link_aops;
502 else inode->i_mapping->a_ops = &hostfs_aops;
504 switch (type) {
505 case OS_TYPE_CHARDEV:
506 init_special_inode(inode, S_IFCHR, rdev);
507 break;
508 case OS_TYPE_BLOCKDEV:
509 init_special_inode(inode, S_IFBLK, rdev);
510 break;
511 case OS_TYPE_FIFO:
512 init_special_inode(inode, S_IFIFO, 0);
513 break;
514 case OS_TYPE_SOCK:
515 init_special_inode(inode, S_IFSOCK, 0);
516 break;
520 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
521 struct nameidata *nd)
523 struct inode *inode;
524 char *name;
525 int error, fd;
527 inode = hostfs_iget(dir->i_sb);
528 if (IS_ERR(inode)) {
529 error = PTR_ERR(inode);
530 goto out;
533 error = -ENOMEM;
534 name = dentry_name(dentry, 0);
535 if (name == NULL)
536 goto out_put;
538 fd = file_create(name,
539 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
540 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
541 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
542 if (fd < 0) {
543 error = fd;
544 } else {
545 error = read_name(inode, name);
546 init_inode(inode, name);
549 kfree(name);
550 if (error)
551 goto out_put;
553 HOSTFS_I(inode)->fd = fd;
554 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
555 d_instantiate(dentry, inode);
556 return 0;
558 out_put:
559 iput(inode);
560 out:
561 return error;
564 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
565 struct nameidata *nd)
567 struct inode *inode;
568 char *name;
569 int err;
571 inode = hostfs_iget(ino->i_sb);
572 if (IS_ERR(inode)) {
573 err = PTR_ERR(inode);
574 goto out;
577 err = -ENOMEM;
578 name = dentry_name(dentry, 0);
579 if (name == NULL)
580 goto out_put;
582 err = read_name(inode, name);
583 init_inode(inode, name);
585 kfree(name);
586 if (err == -ENOENT) {
587 iput(inode);
588 inode = NULL;
590 else if (err)
591 goto out_put;
593 d_add(dentry, inode);
594 dentry->d_op = &hostfs_dentry_ops;
595 return NULL;
597 out_put:
598 iput(inode);
599 out:
600 return ERR_PTR(err);
603 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
605 char *file;
606 int len;
608 file = inode_name(ino, dentry->d_name.len + 1);
609 if (file == NULL)
610 return NULL;
611 strcat(file, "/");
612 len = strlen(file);
613 strncat(file, dentry->d_name.name, dentry->d_name.len);
614 file[len + dentry->d_name.len] = '\0';
615 return file;
618 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
620 char *from_name, *to_name;
621 int err;
623 if ((from_name = inode_dentry_name(ino, from)) == NULL)
624 return -ENOMEM;
625 to_name = dentry_name(to, 0);
626 if (to_name == NULL) {
627 kfree(from_name);
628 return -ENOMEM;
630 err = link_file(to_name, from_name);
631 kfree(from_name);
632 kfree(to_name);
633 return err;
636 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
638 char *file;
639 int err;
641 if ((file = inode_dentry_name(ino, dentry)) == NULL)
642 return -ENOMEM;
643 if (append)
644 return -EPERM;
646 err = unlink_file(file);
647 kfree(file);
648 return err;
651 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
653 char *file;
654 int err;
656 if ((file = inode_dentry_name(ino, dentry)) == NULL)
657 return -ENOMEM;
658 err = make_symlink(file, to);
659 kfree(file);
660 return err;
663 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
665 char *file;
666 int err;
668 if ((file = inode_dentry_name(ino, dentry)) == NULL)
669 return -ENOMEM;
670 err = do_mkdir(file, mode);
671 kfree(file);
672 return err;
675 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
677 char *file;
678 int err;
680 if ((file = inode_dentry_name(ino, dentry)) == NULL)
681 return -ENOMEM;
682 err = do_rmdir(file);
683 kfree(file);
684 return err;
687 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
689 struct inode *inode;
690 char *name;
691 int err;
693 inode = hostfs_iget(dir->i_sb);
694 if (IS_ERR(inode)) {
695 err = PTR_ERR(inode);
696 goto out;
699 err = -ENOMEM;
700 name = dentry_name(dentry, 0);
701 if (name == NULL)
702 goto out_put;
704 init_special_inode(inode, mode, dev);
705 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
706 if (err)
707 goto out_free;
709 err = read_name(inode, name);
710 init_inode(inode, name);
711 if (err)
712 goto out_put;
713 kfree(name);
714 if (err)
715 goto out_put;
717 d_instantiate(dentry, inode);
718 return 0;
720 out_free:
721 kfree(name);
722 out_put:
723 iput(inode);
724 out:
725 return err;
728 int hostfs_rename(struct inode *from_ino, struct dentry *from,
729 struct inode *to_ino, struct dentry *to)
731 char *from_name, *to_name;
732 int err;
734 if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
735 return -ENOMEM;
736 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
737 kfree(from_name);
738 return -ENOMEM;
740 err = rename_file(from_name, to_name);
741 kfree(from_name);
742 kfree(to_name);
743 return err;
746 int hostfs_permission(struct inode *ino, int desired)
748 char *name;
749 int r = 0, w = 0, x = 0, err;
751 if (desired & MAY_READ) r = 1;
752 if (desired & MAY_WRITE) w = 1;
753 if (desired & MAY_EXEC) x = 1;
754 name = inode_name(ino, 0);
755 if (name == NULL)
756 return -ENOMEM;
758 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
759 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
760 err = 0;
761 else
762 err = access_file(name, r, w, x);
763 kfree(name);
764 if (!err)
765 err = generic_permission(ino, desired, NULL);
766 return err;
769 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
771 struct inode *inode = dentry->d_inode;
772 struct hostfs_iattr attrs;
773 char *name;
774 int err;
776 int fd = HOSTFS_I(inode)->fd;
778 err = inode_change_ok(inode, attr);
779 if (err)
780 return err;
782 if (append)
783 attr->ia_valid &= ~ATTR_SIZE;
785 attrs.ia_valid = 0;
786 if (attr->ia_valid & ATTR_MODE) {
787 attrs.ia_valid |= HOSTFS_ATTR_MODE;
788 attrs.ia_mode = attr->ia_mode;
790 if (attr->ia_valid & ATTR_UID) {
791 attrs.ia_valid |= HOSTFS_ATTR_UID;
792 attrs.ia_uid = attr->ia_uid;
794 if (attr->ia_valid & ATTR_GID) {
795 attrs.ia_valid |= HOSTFS_ATTR_GID;
796 attrs.ia_gid = attr->ia_gid;
798 if (attr->ia_valid & ATTR_SIZE) {
799 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
800 attrs.ia_size = attr->ia_size;
802 if (attr->ia_valid & ATTR_ATIME) {
803 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
804 attrs.ia_atime = attr->ia_atime;
806 if (attr->ia_valid & ATTR_MTIME) {
807 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
808 attrs.ia_mtime = attr->ia_mtime;
810 if (attr->ia_valid & ATTR_CTIME) {
811 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
812 attrs.ia_ctime = attr->ia_ctime;
814 if (attr->ia_valid & ATTR_ATIME_SET) {
815 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
817 if (attr->ia_valid & ATTR_MTIME_SET) {
818 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
820 name = dentry_name(dentry, 0);
821 if (name == NULL)
822 return -ENOMEM;
823 err = set_attr(name, &attrs, fd);
824 kfree(name);
825 if (err)
826 return err;
828 if ((attr->ia_valid & ATTR_SIZE) &&
829 attr->ia_size != i_size_read(inode)) {
830 int error;
832 error = vmtruncate(inode, attr->ia_size);
833 if (err)
834 return err;
837 setattr_copy(inode, attr);
838 mark_inode_dirty(inode);
839 return 0;
842 static const struct inode_operations hostfs_iops = {
843 .create = hostfs_create,
844 .link = hostfs_link,
845 .unlink = hostfs_unlink,
846 .symlink = hostfs_symlink,
847 .mkdir = hostfs_mkdir,
848 .rmdir = hostfs_rmdir,
849 .mknod = hostfs_mknod,
850 .rename = hostfs_rename,
851 .permission = hostfs_permission,
852 .setattr = hostfs_setattr,
855 static const struct inode_operations hostfs_dir_iops = {
856 .create = hostfs_create,
857 .lookup = hostfs_lookup,
858 .link = hostfs_link,
859 .unlink = hostfs_unlink,
860 .symlink = hostfs_symlink,
861 .mkdir = hostfs_mkdir,
862 .rmdir = hostfs_rmdir,
863 .mknod = hostfs_mknod,
864 .rename = hostfs_rename,
865 .permission = hostfs_permission,
866 .setattr = hostfs_setattr,
869 int hostfs_link_readpage(struct file *file, struct page *page)
871 char *buffer, *name;
872 int err;
874 buffer = kmap(page);
875 name = inode_name(page->mapping->host, 0);
876 if (name == NULL)
877 return -ENOMEM;
878 err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE);
879 kfree(name);
880 if (err == PAGE_CACHE_SIZE)
881 err = -E2BIG;
882 else if (err > 0) {
883 flush_dcache_page(page);
884 SetPageUptodate(page);
885 if (PageError(page)) ClearPageError(page);
886 err = 0;
888 kunmap(page);
889 unlock_page(page);
890 return err;
893 static const struct address_space_operations hostfs_link_aops = {
894 .readpage = hostfs_link_readpage,
897 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
899 struct inode *root_inode;
900 char *host_root_path, *req_root = d;
901 int err;
903 sb->s_blocksize = 1024;
904 sb->s_blocksize_bits = 10;
905 sb->s_magic = HOSTFS_SUPER_MAGIC;
906 sb->s_op = &hostfs_sbops;
907 sb->s_maxbytes = MAX_LFS_FILESIZE;
909 /* NULL is printed as <NULL> by sprintf: avoid that. */
910 if (req_root == NULL)
911 req_root = "";
913 err = -ENOMEM;
914 sb->s_fs_info = host_root_path =
915 kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
916 if (host_root_path == NULL)
917 goto out;
919 sprintf(host_root_path, "%s/%s", root_ino, req_root);
921 root_inode = new_inode(sb);
922 if (!root_inode)
923 goto out;
925 root_inode->i_op = &hostfs_dir_iops;
926 root_inode->i_fop = &hostfs_dir_fops;
928 if (file_type(host_root_path, NULL, NULL) == OS_TYPE_SYMLINK) {
929 char *name = follow_link(host_root_path);
930 if (IS_ERR(name))
931 err = PTR_ERR(name);
932 else
933 err = read_name(root_inode, name);
934 kfree(name);
935 } else {
936 err = read_name(root_inode, host_root_path);
938 if (err)
939 goto out_put;
941 err = -ENOMEM;
942 sb->s_root = d_alloc_root(root_inode);
943 if (sb->s_root == NULL)
944 goto out_put;
946 return 0;
948 out_put:
949 iput(root_inode);
950 out:
951 return err;
954 static int hostfs_read_sb(struct file_system_type *type,
955 int flags, const char *dev_name,
956 void *data, struct vfsmount *mnt)
958 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
961 static void hostfs_kill_sb(struct super_block *s)
963 kill_anon_super(s);
964 kfree(s->s_fs_info);
967 static struct file_system_type hostfs_type = {
968 .owner = THIS_MODULE,
969 .name = "hostfs",
970 .get_sb = hostfs_read_sb,
971 .kill_sb = hostfs_kill_sb,
972 .fs_flags = 0,
975 static int __init init_hostfs(void)
977 return register_filesystem(&hostfs_type);
980 static void __exit exit_hostfs(void)
982 unregister_filesystem(&hostfs_type);
985 module_init(init_hostfs)
986 module_exit(exit_hostfs)
987 MODULE_LICENSE("GPL");